accel/ivpu: Move recovery work to system_unbound_wq
[drm/drm-misc.git] / drivers / tee / optee / device.c
blob950b4661d5dfe519b629a23ffc8389b872f343d2
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 Linaro Ltd.
4 */
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/tee_core.h>
11 #include <linux/uuid.h>
12 #include "optee_private.h"
14 static int optee_ctx_match(struct tee_ioctl_version_data *ver, const void *data)
16 if (ver->impl_id == TEE_IMPL_ID_OPTEE)
17 return 1;
18 else
19 return 0;
22 static int get_devices(struct tee_context *ctx, u32 session,
23 struct tee_shm *device_shm, u32 *shm_size,
24 u32 func)
26 int ret = 0;
27 struct tee_ioctl_invoke_arg inv_arg;
28 struct tee_param param[4];
30 memset(&inv_arg, 0, sizeof(inv_arg));
31 memset(&param, 0, sizeof(param));
33 inv_arg.func = func;
34 inv_arg.session = session;
35 inv_arg.num_params = 4;
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;
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))) {
47 * TEE_ERROR_STORAGE_NOT_AVAILABLE is returned when getting
48 * the list of device TAs that depends on RPMB but a usable
49 * RPMB device isn't found.
51 if (inv_arg.ret == TEE_ERROR_STORAGE_NOT_AVAILABLE)
52 return -ENODEV;
53 pr_err("PTA_CMD_GET_DEVICES invoke function err: %x\n",
54 inv_arg.ret);
55 return -EINVAL;
58 *shm_size = param[0].u.memref.size;
60 return 0;
63 static void optee_release_device(struct device *dev)
65 struct tee_client_device *optee_device = to_tee_client_device(dev);
67 kfree(optee_device);
70 static ssize_t need_supplicant_show(struct device *dev,
71 struct device_attribute *attr,
72 char *buf)
74 return 0;
77 static DEVICE_ATTR_RO(need_supplicant);
79 static int optee_register_device(const uuid_t *device_uuid, u32 func)
81 struct tee_client_device *optee_device = NULL;
82 int rc;
84 optee_device = kzalloc(sizeof(*optee_device), GFP_KERNEL);
85 if (!optee_device)
86 return -ENOMEM;
88 optee_device->dev.bus = &tee_bus_type;
89 optee_device->dev.release = optee_release_device;
90 if (dev_set_name(&optee_device->dev, "optee-ta-%pUb", device_uuid)) {
91 kfree(optee_device);
92 return -ENOMEM;
94 uuid_copy(&optee_device->id.uuid, device_uuid);
96 rc = device_register(&optee_device->dev);
97 if (rc) {
98 pr_err("device registration failed, err: %d\n", rc);
99 put_device(&optee_device->dev);
100 return rc;
103 if (func == PTA_CMD_GET_DEVICES_SUPP)
104 device_create_file(&optee_device->dev,
105 &dev_attr_need_supplicant);
107 return 0;
110 static int __optee_enumerate_devices(u32 func)
112 const uuid_t pta_uuid =
113 UUID_INIT(0x7011a688, 0xddde, 0x4053,
114 0xa5, 0xa9, 0x7b, 0x3c, 0x4d, 0xdf, 0x13, 0xb8);
115 struct tee_ioctl_open_session_arg sess_arg;
116 struct tee_shm *device_shm = NULL;
117 const uuid_t *device_uuid = NULL;
118 struct tee_context *ctx = NULL;
119 u32 shm_size = 0, idx, num_devices = 0;
120 int rc;
122 memset(&sess_arg, 0, sizeof(sess_arg));
124 /* Open context with OP-TEE driver */
125 ctx = tee_client_open_context(NULL, optee_ctx_match, NULL, NULL);
126 if (IS_ERR(ctx))
127 return -ENODEV;
129 /* Open session with device enumeration pseudo TA */
130 export_uuid(sess_arg.uuid, &pta_uuid);
131 sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
132 sess_arg.num_params = 0;
134 rc = tee_client_open_session(ctx, &sess_arg, NULL);
135 if ((rc < 0) || (sess_arg.ret != TEEC_SUCCESS)) {
136 /* Device enumeration pseudo TA not found */
137 rc = 0;
138 goto out_ctx;
141 rc = get_devices(ctx, sess_arg.session, NULL, &shm_size, func);
142 if (rc < 0 || !shm_size)
143 goto out_sess;
145 device_shm = tee_shm_alloc_kernel_buf(ctx, shm_size);
146 if (IS_ERR(device_shm)) {
147 pr_err("tee_shm_alloc_kernel_buf failed\n");
148 rc = PTR_ERR(device_shm);
149 goto out_sess;
152 rc = get_devices(ctx, sess_arg.session, device_shm, &shm_size, func);
153 if (rc < 0)
154 goto out_shm;
156 device_uuid = tee_shm_get_va(device_shm, 0);
157 if (IS_ERR(device_uuid)) {
158 pr_err("tee_shm_get_va failed\n");
159 rc = PTR_ERR(device_uuid);
160 goto out_shm;
163 num_devices = shm_size / sizeof(uuid_t);
165 for (idx = 0; idx < num_devices; idx++) {
166 rc = optee_register_device(&device_uuid[idx], func);
167 if (rc)
168 goto out_shm;
171 out_shm:
172 tee_shm_free(device_shm);
173 out_sess:
174 tee_client_close_session(ctx, sess_arg.session);
175 out_ctx:
176 tee_client_close_context(ctx);
178 return rc;
181 int optee_enumerate_devices(u32 func)
183 return __optee_enumerate_devices(func);
186 static int __optee_unregister_device(struct device *dev, void *data)
188 if (!strncmp(dev_name(dev), "optee-ta", strlen("optee-ta")))
189 device_unregister(dev);
191 return 0;
194 void optee_unregister_devices(void)
196 bus_for_each_dev(&tee_bus_type, NULL, NULL,
197 __optee_unregister_device);