xref: /openbmc/linux/drivers/tee/optee/device.c (revision 9853f130)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 Linaro Ltd.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/tee_drv.h>
11 #include <linux/uuid.h>
12 #include "optee_private.h"
13 
14 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
15 {
16 	if (ver->impl_id == TEE_IMPL_ID_OPTEE)
17 		return 1;
18 	else
19 		return 0;
20 }
21 
22 static int get_devices(struct tee_context *ctx, u32 session,
23 		       struct tee_shm *device_shm, u32 *shm_size,
24 		       u32 func)
25 {
26 	int ret = 0;
27 	struct tee_ioctl_invoke_arg inv_arg;
28 	struct tee_param param[4];
29 
30 	memset(&inv_arg, 0, sizeof(inv_arg));
31 	memset(&param, 0, sizeof(param));
32 
33 	inv_arg.func = func;
34 	inv_arg.session = session;
35 	inv_arg.num_params = 4;
36 
37 	/* Fill invoke cmd params */
38 	param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
39 	param[0].u.memref.shm = device_shm;
40 	param[0].u.memref.size = *shm_size;
41 	param[0].u.memref.shm_offs = 0;
42 
43 	ret = tee_client_invoke_func(ctx, &inv_arg, param);
44 	if ((ret < 0) || ((inv_arg.ret != TEEC_SUCCESS) &&
45 			  (inv_arg.ret != TEEC_ERROR_SHORT_BUFFER))) {
46 		pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n",
47 		       inv_arg.ret);
48 		return -EINVAL;
49 	}
50 
51 	*shm_size = param[0].u.memref.size;
52 
53 	return 0;
54 }
55 
56 static void optee_release_device(struct device *dev)
57 {
58 	struct tee_client_device *optee_device = to_tee_client_device(dev);
59 
60 	kfree(optee_device);
61 }
62 
63 static ssize_t need_supplicant_show(struct device *dev,
64 				    struct device_attribute *attr,
65 				    char *buf)
66 {
67 	return 0;
68 }
69 
70 static DEVICE_ATTR_RO(need_supplicant);
71 
72 static int optee_register_device(const uuid_t *device_uuid, u32 func)
73 {
74 	struct tee_client_device *optee_device = NULL;
75 	int rc;
76 
77 	optee_device = kzalloc(sizeof(*optee_device), GFP_KERNEL);
78 	if (!optee_device)
79 		return -ENOMEM;
80 
81 	optee_device->dev.bus = &tee_bus_type;
82 	optee_device->dev.release = optee_release_device;
83 	if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) {
84 		kfree(optee_device);
85 		return -ENOMEM;
86 	}
87 	uuid_copy(&optee_device->id.uuid, device_uuid);
88 
89 	rc = device_register(&optee_device->dev);
90 	if (rc) {
91 		pr_err("device registration failed, err: %d\n", rc);
92 		put_device(&optee_device->dev);
93 	}
94 
95 	if (func == PTA_CMD_GET_DEVICES_SUPP)
96 		device_create_file(&optee_device->dev,
97 				   &dev_attr_need_supplicant);
98 
99 	return rc;
100 }
101 
102 static int __optee_enumerate_devices(u32 func)
103 {
104 	const uuid_t pta_uuid =
105 		UUID_INIT(0x7011a688, 0xddde, 0x4053,
106 			  0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8);
107 	struct tee_ioctl_open_session_arg sess_arg;
108 	struct tee_shm *device_shm = NULL;
109 	const uuid_t *device_uuid = NULL;
110 	struct tee_context *ctx = NULL;
111 	u32 shm_size = 0, idx, num_devices = 0;
112 	int rc;
113 
114 	memset(&sess_arg, 0, sizeof(sess_arg));
115 
116 	/* Open context with OP-TEE driver */
117 	ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
118 	if (IS_ERR(ctx))
119 		return -ENODEV;
120 
121 	/* Open session with device enumeration pseudo TA */
122 	export_uuid(sess_arg.uuid, &pta_uuid);
123 	sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
124 	sess_arg.num_params = 0;
125 
126 	rc = tee_client_open_session(ctx, &sess_arg, NULL);
127 	if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) {
128 		/* Device enumeration pseudo TA not found */
129 		rc = 0;
130 		goto out_ctx;
131 	}
132 
133 	rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func);
134 	if (rc < 0 || !shm_size)
135 		goto out_sess;
136 
137 	device_shm = tee_shm_alloc_kernel_buf(ctx, shm_size);
138 	if (IS_ERR(device_shm)) {
139 		pr_err("tee_shm_alloc_kernel_buf failed\n");
140 		rc = PTR_ERR(device_shm);
141 		goto out_sess;
142 	}
143 
144 	rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func);
145 	if (rc < 0)
146 		goto out_shm;
147 
148 	device_uuid = tee_shm_get_va(device_shm, 0);
149 	if (IS_ERR(device_uuid)) {
150 		pr_err("tee_shm_get_va failed\n");
151 		rc = PTR_ERR(device_uuid);
152 		goto out_shm;
153 	}
154 
155 	num_devices = shm_size / sizeof(uuid_t);
156 
157 	for (idx = 0; idx < num_devices; idx++) {
158 		rc = optee_register_device(&device_uuid[idx], func);
159 		if (rc)
160 			goto out_shm;
161 	}
162 
163 out_shm:
164 	tee_shm_free(device_shm);
165 out_sess:
166 	tee_client_close_session(ctx, sess_arg.session);
167 out_ctx:
168 	tee_client_close_context(ctx);
169 
170 	return rc;
171 }
172 
173 int optee_enumerate_devices(u32 func)
174 {
175 	return  __optee_enumerate_devices(func);
176 }
177 
178 static int __optee_unregister_device(struct device *dev, void *data)
179 {
180 	if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta")))
181 		device_unregister(dev);
182 
183 	return 0;
184 }
185 
186 void optee_unregister_devices(void)
187 {
188 	bus_for_each_dev(&tee_bus_type, NULL, NULL,
189 			 __optee_unregister_device);
190 }
191