xref: /linux/drivers/tee/optee/core.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2021, Linaro Limited
4  * Copyright (c) 2016, EPAM Systems
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/crash_dump.h>
10 #include <linux/errno.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/rpmb.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/tee_core.h>
17 #include <linux/types.h>
18 #include "optee_private.h"
19 
20 struct blocking_notifier_head optee_rpmb_intf_added =
21 	BLOCKING_NOTIFIER_INIT(optee_rpmb_intf_added);
22 
23 static int rpmb_add_dev(struct device *dev)
24 {
25 	blocking_notifier_call_chain(&optee_rpmb_intf_added, 0,
26 				     to_rpmb_dev(dev));
27 
28 	return 0;
29 }
30 
31 static struct class_interface rpmb_class_intf = {
32 	.add_dev = rpmb_add_dev,
33 };
34 
35 void optee_bus_scan_rpmb(struct work_struct *work)
36 {
37 	struct optee *optee = container_of(work, struct optee,
38 					   rpmb_scan_bus_work);
39 	int ret;
40 
41 	if (!optee->rpmb_scan_bus_done) {
42 		ret = optee_enumerate_devices(PTA_CMD_GET_DEVICES_RPMB);
43 		optee->rpmb_scan_bus_done = !ret;
44 		if (ret && ret != -ENODEV)
45 			pr_info("Scanning for RPMB device: ret %d\n", ret);
46 	}
47 }
48 
49 int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action,
50 			 void *data)
51 {
52 	struct optee *optee = container_of(intf, struct optee, rpmb_intf);
53 
54 	schedule_work(&optee->rpmb_scan_bus_work);
55 
56 	return 0;
57 }
58 
59 int optee_set_dma_mask(struct optee *optee, u_int pa_width)
60 {
61 	u64 mask = DMA_BIT_MASK(min(64, pa_width));
62 
63 	return dma_coerce_mask_and_coherent(&optee->teedev->dev, mask);
64 }
65 
66 int optee_get_revision(struct tee_device *teedev, char *buf, size_t len)
67 {
68 	struct optee *optee = tee_get_drvdata(teedev);
69 	u64 build_id;
70 
71 	if (!optee)
72 		return -ENODEV;
73 	if (!buf || !len)
74 		return -EINVAL;
75 
76 	build_id = optee->revision.os_build_id;
77 	if (build_id)
78 		scnprintf(buf, len, "%u.%u (%016llx)",
79 			  optee->revision.os_major,
80 			  optee->revision.os_minor,
81 			  (unsigned long long)build_id);
82 	else
83 		scnprintf(buf, len, "%u.%u", optee->revision.os_major,
84 			  optee->revision.os_minor);
85 
86 	return 0;
87 }
88 
89 static void optee_bus_scan(struct work_struct *work)
90 {
91 	WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
92 }
93 
94 static ssize_t rpmb_routing_model_show(struct device *dev,
95 				       struct device_attribute *attr, char *buf)
96 {
97 	struct optee *optee = dev_get_drvdata(dev);
98 	const char *s;
99 
100 	if (optee->in_kernel_rpmb_routing)
101 		s = "kernel";
102 	else
103 		s = "user";
104 
105 	return sysfs_emit(buf, "%s\n", s);
106 }
107 static DEVICE_ATTR_RO(rpmb_routing_model);
108 
109 static struct attribute *optee_dev_attrs[] = {
110 	&dev_attr_rpmb_routing_model.attr,
111 	NULL
112 };
113 
114 ATTRIBUTE_GROUPS(optee_dev);
115 
116 void optee_set_dev_group(struct optee *optee)
117 {
118 	tee_device_set_dev_groups(optee->teedev, optee_dev_groups);
119 	tee_device_set_dev_groups(optee->supp_teedev, optee_dev_groups);
120 }
121 
122 int optee_open(struct tee_context *ctx, bool cap_memref_null)
123 {
124 	struct optee_context_data *ctxdata;
125 	struct tee_device *teedev = ctx->teedev;
126 	struct optee *optee = tee_get_drvdata(teedev);
127 
128 	ctxdata = kzalloc_obj(*ctxdata, GFP_KERNEL);
129 	if (!ctxdata)
130 		return -ENOMEM;
131 
132 	if (teedev == optee->supp_teedev) {
133 		bool busy = true;
134 
135 		mutex_lock(&optee->supp.mutex);
136 		if (!optee->supp.ctx) {
137 			busy = false;
138 			optee->supp.ctx = ctx;
139 		}
140 		mutex_unlock(&optee->supp.mutex);
141 		if (busy) {
142 			kfree(ctxdata);
143 			return -EBUSY;
144 		}
145 
146 		if (!optee->scan_bus_done) {
147 			INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
148 			schedule_work(&optee->scan_bus_work);
149 			optee->scan_bus_done = true;
150 		}
151 	}
152 	mutex_init(&ctxdata->mutex);
153 	INIT_LIST_HEAD(&ctxdata->sess_list);
154 
155 	ctx->cap_memref_null = cap_memref_null;
156 	ctx->data = ctxdata;
157 	return 0;
158 }
159 
160 static void optee_release_helper(struct tee_context *ctx,
161 				 int (*close_session)(struct tee_context *ctx,
162 						      u32 session,
163 						      bool system_thread))
164 {
165 	struct optee_context_data *ctxdata = ctx->data;
166 	struct optee_session *sess;
167 	struct optee_session *sess_tmp;
168 
169 	if (!ctxdata)
170 		return;
171 
172 	list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
173 				 list_node) {
174 		list_del(&sess->list_node);
175 		close_session(ctx, sess->session_id, sess->use_sys_thread);
176 		kfree(sess);
177 	}
178 	kfree(ctxdata);
179 	ctx->data = NULL;
180 }
181 
182 void optee_release(struct tee_context *ctx)
183 {
184 	optee_release_helper(ctx, optee_close_session_helper);
185 }
186 
187 void optee_release_supp(struct tee_context *ctx)
188 {
189 	struct optee *optee = tee_get_drvdata(ctx->teedev);
190 
191 	optee_release_helper(ctx, optee_close_session_helper);
192 
193 	optee_supp_release(&optee->supp);
194 }
195 
196 void optee_remove_common(struct optee *optee)
197 {
198 	blocking_notifier_chain_unregister(&optee_rpmb_intf_added,
199 					   &optee->rpmb_intf);
200 	cancel_work_sync(&optee->rpmb_scan_bus_work);
201 	/* Unregister OP-TEE specific client devices on TEE bus */
202 	optee_unregister_devices();
203 
204 	optee_notif_uninit(optee);
205 	optee_shm_arg_cache_uninit(optee);
206 	teedev_close_context(optee->ctx);
207 	/*
208 	 * The two devices have to be unregistered before we can free the
209 	 * other resources.
210 	 */
211 	tee_device_unregister(optee->supp_teedev);
212 	tee_device_unregister(optee->teedev);
213 
214 	tee_shm_pool_free(optee->pool);
215 	optee_supp_uninit(&optee->supp);
216 	mutex_destroy(&optee->call_queue.mutex);
217 	rpmb_dev_put(optee->rpmb_dev);
218 	mutex_destroy(&optee->rpmb_dev_mutex);
219 }
220 
221 static int smc_abi_rc;
222 static int ffa_abi_rc;
223 static bool intf_is_regged;
224 
225 static int __init optee_core_init(void)
226 {
227 	int rc;
228 
229 	/*
230 	 * The kernel may have crashed at the same time that all available
231 	 * secure world threads were suspended and we cannot reschedule the
232 	 * suspended threads without access to the crashed kernel's wait_queue.
233 	 * Therefore, we cannot reliably initialize the OP-TEE driver in the
234 	 * kdump kernel.
235 	 */
236 	if (is_kdump_kernel())
237 		return -ENODEV;
238 
239 	if (IS_REACHABLE(CONFIG_RPMB)) {
240 		rc = rpmb_interface_register(&rpmb_class_intf);
241 		if (rc)
242 			return rc;
243 		intf_is_regged = true;
244 	}
245 
246 	smc_abi_rc = optee_smc_abi_register();
247 	ffa_abi_rc = optee_ffa_abi_register();
248 
249 	/* If both failed there's no point with this module */
250 	if (smc_abi_rc && ffa_abi_rc) {
251 		if (IS_REACHABLE(CONFIG_RPMB)) {
252 			rpmb_interface_unregister(&rpmb_class_intf);
253 			intf_is_regged = false;
254 		}
255 		return smc_abi_rc;
256 	}
257 
258 	return 0;
259 }
260 module_init(optee_core_init);
261 
262 static void __exit optee_core_exit(void)
263 {
264 	if (IS_REACHABLE(CONFIG_RPMB) && intf_is_regged) {
265 		rpmb_interface_unregister(&rpmb_class_intf);
266 		intf_is_regged = false;
267 	}
268 
269 	if (!smc_abi_rc)
270 		optee_smc_abi_unregister();
271 	if (!ffa_abi_rc)
272 		optee_ffa_abi_unregister();
273 }
274 module_exit(optee_core_exit);
275 
276 MODULE_AUTHOR("Linaro");
277 MODULE_DESCRIPTION("OP-TEE driver");
278 MODULE_VERSION("1.0");
279 MODULE_LICENSE("GPL v2");
280 MODULE_ALIAS("platform:optee");
281