xref: /linux/drivers/tee/optee/device.c (revision 0439fcff)
1c3fa24afSSumit Garg // SPDX-License-Identifier: GPL-2.0
2c3fa24afSSumit Garg /*
3c3fa24afSSumit Garg  * Copyright (C) 2019 Linaro Ltd.
4c3fa24afSSumit Garg  */
5c3fa24afSSumit Garg 
6c3fa24afSSumit Garg #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7c3fa24afSSumit Garg 
8c3fa24afSSumit Garg #include <linux/kernel.h>
9c3fa24afSSumit Garg #include <linux/slab.h>
10*0439fcffSSumit Garg #include <linux/tee_core.h>
11c3fa24afSSumit Garg #include <linux/uuid.h>
12c3fa24afSSumit Garg #include "optee_private.h"
13c3fa24afSSumit Garg 
optee_ctx_match(struct tee_ioctl_version_data * ver,const void * data)14c3fa24afSSumit Garg static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
15c3fa24afSSumit Garg {
16c3fa24afSSumit Garg 	if (ver->impl_id == TEE_IMPL_ID_OPTEE)
17c3fa24afSSumit Garg 		return 1;
18c3fa24afSSumit Garg 	else
19c3fa24afSSumit Garg 		return 0;
20c3fa24afSSumit Garg }
21c3fa24afSSumit Garg 
get_devices(struct tee_context * ctx,u32 session,struct tee_shm * device_shm,u32 * shm_size,u32 func)22c3fa24afSSumit Garg static int get_devices(struct tee_context *ctx, u32 session,
235f178bb7SMaxim Uvarov 		       struct tee_shm *device_shm, u32 *shm_size,
245f178bb7SMaxim Uvarov 		       u32 func)
25c3fa24afSSumit Garg {
2662ade1beSYueHaibing 	int ret = 0;
2750ceca68SNathan Chancellor 	struct tee_ioctl_invoke_arg inv_arg;
2850ceca68SNathan Chancellor 	struct tee_param param[4];
2950ceca68SNathan Chancellor 
3050ceca68SNathan Chancellor 	memset(&inv_arg, 0, sizeof(inv_arg));
3150ceca68SNathan Chancellor 	memset(&param, 0, sizeof(param));
32c3fa24afSSumit Garg 
335f178bb7SMaxim Uvarov 	inv_arg.func = func;
34c3fa24afSSumit Garg 	inv_arg.session = session;
35c3fa24afSSumit Garg 	inv_arg.num_params = 4;
36c3fa24afSSumit Garg 
37c3fa24afSSumit Garg 	/* Fill invoke cmd params */
38c3fa24afSSumit Garg 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
39c3fa24afSSumit Garg 	param[0].u.memref.shm = device_shm;
40c3fa24afSSumit Garg 	param[0].u.memref.size = *shm_size;
41c3fa24afSSumit Garg 	param[0].u.memref.shm_offs = 0;
42c3fa24afSSumit Garg 
43c3fa24afSSumit Garg 	ret = tee_client_invoke_func(ctx, &inv_arg, param);
44c3fa24afSSumit Garg 	if ((ret < 0) || ((inv_arg.ret != TEEC_SUCCESS) &&
45c3fa24afSSumit Garg 			  (inv_arg.ret != TEEC_ERROR_SHORT_BUFFER))) {
46c3fa24afSSumit Garg 		pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n",
47c3fa24afSSumit Garg 		       inv_arg.ret);
48c3fa24afSSumit Garg 		return -EINVAL;
49c3fa24afSSumit Garg 	}
50c3fa24afSSumit Garg 
51c3fa24afSSumit Garg 	*shm_size = param[0].u.memref.size;
52c3fa24afSSumit Garg 
53c3fa24afSSumit Garg 	return 0;
54c3fa24afSSumit Garg }
55c3fa24afSSumit Garg 
optee_release_device(struct device * dev)567f565d0eSSumit Garg static void optee_release_device(struct device *dev)
577f565d0eSSumit Garg {
587f565d0eSSumit Garg 	struct tee_client_device *optee_device = to_tee_client_device(dev);
597f565d0eSSumit Garg 
607f565d0eSSumit Garg 	kfree(optee_device);
617f565d0eSSumit Garg }
627f565d0eSSumit Garg 
need_supplicant_show(struct device * dev,struct device_attribute * attr,char * buf)637269cba5SSumit Garg static ssize_t need_supplicant_show(struct device *dev,
647269cba5SSumit Garg 				    struct device_attribute *attr,
657269cba5SSumit Garg 				    char *buf)
667269cba5SSumit Garg {
677269cba5SSumit Garg 	return 0;
687269cba5SSumit Garg }
697269cba5SSumit Garg 
707269cba5SSumit Garg static DEVICE_ATTR_RO(need_supplicant);
717269cba5SSumit Garg 
optee_register_device(const uuid_t * device_uuid,u32 func)727269cba5SSumit Garg static int optee_register_device(const uuid_t *device_uuid, u32 func)
73c3fa24afSSumit Garg {
74c3fa24afSSumit Garg 	struct tee_client_device *optee_device = NULL;
75c3fa24afSSumit Garg 	int rc;
76c3fa24afSSumit Garg 
77c3fa24afSSumit Garg 	optee_device = kzalloc(sizeof(*optee_device), GFP_KERNEL);
78c3fa24afSSumit Garg 	if (!optee_device)
79c3fa24afSSumit Garg 		return -ENOMEM;
80c3fa24afSSumit Garg 
81c3fa24afSSumit Garg 	optee_device->dev.bus = &tee_bus_type;
827f565d0eSSumit Garg 	optee_device->dev.release = optee_release_device;
8358df195cSMaxim Uvarov 	if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) {
8458df195cSMaxim Uvarov 		kfree(optee_device);
8558df195cSMaxim Uvarov 		return -ENOMEM;
8658df195cSMaxim Uvarov 	}
87c3fa24afSSumit Garg 	uuid_copy(&optee_device->id.uuid, device_uuid);
88c3fa24afSSumit Garg 
89c3fa24afSSumit Garg 	rc = device_register(&optee_device->dev);
90c3fa24afSSumit Garg 	if (rc) {
91c3fa24afSSumit Garg 		pr_err("device registration failed, err: %d\n", rc);
92cce616e0SYang Yingliang 		put_device(&optee_device->dev);
9395915ba4SSumit Garg 		return rc;
94c3fa24afSSumit Garg 	}
95c3fa24afSSumit Garg 
967269cba5SSumit Garg 	if (func == PTA_CMD_GET_DEVICES_SUPP)
977269cba5SSumit Garg 		device_create_file(&optee_device->dev,
987269cba5SSumit Garg 				   &dev_attr_need_supplicant);
997269cba5SSumit Garg 
10095915ba4SSumit Garg 	return 0;
101c3fa24afSSumit Garg }
102c3fa24afSSumit Garg 
__optee_enumerate_devices(u32 func)1035f178bb7SMaxim Uvarov static int __optee_enumerate_devices(u32 func)
104c3fa24afSSumit Garg {
105c3fa24afSSumit Garg 	const uuid_t pta_uuid =
106c3fa24afSSumit Garg 		UUID_INIT(0x7011a688, 0xddde, 0x4053,
107c3fa24afSSumit Garg 			  0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8);
10850ceca68SNathan Chancellor 	struct tee_ioctl_open_session_arg sess_arg;
109c3fa24afSSumit Garg 	struct tee_shm *device_shm = NULL;
110c3fa24afSSumit Garg 	const uuid_t *device_uuid = NULL;
111c3fa24afSSumit Garg 	struct tee_context *ctx = NULL;
112c3fa24afSSumit Garg 	u32 shm_size = 0, idx, num_devices = 0;
113c3fa24afSSumit Garg 	int rc;
114c3fa24afSSumit Garg 
11550ceca68SNathan Chancellor 	memset(&sess_arg, 0, sizeof(sess_arg));
11650ceca68SNathan Chancellor 
117c3fa24afSSumit Garg 	/* Open context with OP-TEE driver */
118c3fa24afSSumit Garg 	ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
119c3fa24afSSumit Garg 	if (IS_ERR(ctx))
120c3fa24afSSumit Garg 		return -ENODEV;
121c3fa24afSSumit Garg 
122c3fa24afSSumit Garg 	/* Open session with device enumeration pseudo TA */
12357222a1bSAndy Shevchenko 	export_uuid(sess_arg.uuid, &pta_uuid);
124c3fa24afSSumit Garg 	sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
125c3fa24afSSumit Garg 	sess_arg.num_params = 0;
126c3fa24afSSumit Garg 
127c3fa24afSSumit Garg 	rc = tee_client_open_session(ctx, &sess_arg, NULL);
128c3fa24afSSumit Garg 	if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) {
129c3fa24afSSumit Garg 		/* Device enumeration pseudo TA not found */
130c3fa24afSSumit Garg 		rc = 0;
131c3fa24afSSumit Garg 		goto out_ctx;
132c3fa24afSSumit Garg 	}
133c3fa24afSSumit Garg 
1345f178bb7SMaxim Uvarov 	rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func);
135c3fa24afSSumit Garg 	if (rc < 0 || !shm_size)
136c3fa24afSSumit Garg 		goto out_sess;
137c3fa24afSSumit Garg 
1385d41f1b3SJens Wiklander 	device_shm = tee_shm_alloc_kernel_buf(ctx, shm_size);
139c3fa24afSSumit Garg 	if (IS_ERR(device_shm)) {
1405d41f1b3SJens Wiklander 		pr_err("tee_shm_alloc_kernel_buf failed\n");
141c3fa24afSSumit Garg 		rc = PTR_ERR(device_shm);
142c3fa24afSSumit Garg 		goto out_sess;
143c3fa24afSSumit Garg 	}
144c3fa24afSSumit Garg 
1455f178bb7SMaxim Uvarov 	rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func);
146c3fa24afSSumit Garg 	if (rc < 0)
147c3fa24afSSumit Garg 		goto out_shm;
148c3fa24afSSumit Garg 
149c3fa24afSSumit Garg 	device_uuid = tee_shm_get_va(device_shm, 0);
150c3fa24afSSumit Garg 	if (IS_ERR(device_uuid)) {
151c3fa24afSSumit Garg 		pr_err("tee_shm_get_va failed\n");
152c3fa24afSSumit Garg 		rc = PTR_ERR(device_uuid);
153c3fa24afSSumit Garg 		goto out_shm;
154c3fa24afSSumit Garg 	}
155c3fa24afSSumit Garg 
156c3fa24afSSumit Garg 	num_devices = shm_size / sizeof(uuid_t);
157c3fa24afSSumit Garg 
158c3fa24afSSumit Garg 	for (idx = 0; idx < num_devices; idx++) {
1597269cba5SSumit Garg 		rc = optee_register_device(&device_uuid[idx], func);
160c3fa24afSSumit Garg 		if (rc)
161c3fa24afSSumit Garg 			goto out_shm;
162c3fa24afSSumit Garg 	}
163c3fa24afSSumit Garg 
164c3fa24afSSumit Garg out_shm:
165c3fa24afSSumit Garg 	tee_shm_free(device_shm);
166c3fa24afSSumit Garg out_sess:
167c3fa24afSSumit Garg 	tee_client_close_session(ctx, sess_arg.session);
168c3fa24afSSumit Garg out_ctx:
169c3fa24afSSumit Garg 	tee_client_close_context(ctx);
170c3fa24afSSumit Garg 
171c3fa24afSSumit Garg 	return rc;
172c3fa24afSSumit Garg }
1735f178bb7SMaxim Uvarov 
optee_enumerate_devices(u32 func)1745f178bb7SMaxim Uvarov int optee_enumerate_devices(u32 func)
1755f178bb7SMaxim Uvarov {
1765f178bb7SMaxim Uvarov 	return  __optee_enumerate_devices(func);
1775f178bb7SMaxim Uvarov }
1787f565d0eSSumit Garg 
__optee_unregister_device(struct device * dev,void * data)1797f565d0eSSumit Garg static int __optee_unregister_device(struct device *dev, void *data)
1807f565d0eSSumit Garg {
1817f565d0eSSumit Garg 	if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta")))
1827f565d0eSSumit Garg 		device_unregister(dev);
1837f565d0eSSumit Garg 
1847f565d0eSSumit Garg 	return 0;
1857f565d0eSSumit Garg }
1867f565d0eSSumit Garg 
optee_unregister_devices(void)1877f565d0eSSumit Garg void optee_unregister_devices(void)
1887f565d0eSSumit Garg {
1897f565d0eSSumit Garg 	bus_for_each_dev(&tee_bus_type, NULL, NULL,
1907f565d0eSSumit Garg 			 __optee_unregister_device);
1917f565d0eSSumit Garg }
192