xref: /linux/drivers/tee/optee/core.c (revision d88e0493a054c9fe72ade41a42d42e958ee6503d)
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/mm.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/tee_drv.h>
17 #include <linux/types.h>
18 #include <linux/workqueue.h>
19 #include "optee_private.h"
20 
21 int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
22 			       size_t size, size_t align,
23 			       int (*shm_register)(struct tee_context *ctx,
24 						   struct tee_shm *shm,
25 						   struct page **pages,
26 						   size_t num_pages,
27 						   unsigned long start))
28 {
29 	unsigned int order = get_order(size);
30 	struct page *page;
31 	int rc = 0;
32 
33 	/*
34 	 * Ignore alignment since this is already going to be page aligned
35 	 * and there's no need for any larger alignment.
36 	 */
37 	page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
38 	if (!page)
39 		return -ENOMEM;
40 
41 	shm->kaddr = page_address(page);
42 	shm->paddr = page_to_phys(page);
43 	shm->size = PAGE_SIZE << order;
44 
45 	if (shm_register) {
46 		unsigned int nr_pages = 1 << order, i;
47 		struct page **pages;
48 
49 		pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
50 		if (!pages) {
51 			rc = -ENOMEM;
52 			goto err;
53 		}
54 
55 		for (i = 0; i < nr_pages; i++)
56 			pages[i] = page + i;
57 
58 		rc = shm_register(shm->ctx, shm, pages, nr_pages,
59 				  (unsigned long)shm->kaddr);
60 		kfree(pages);
61 		if (rc)
62 			goto err;
63 	}
64 
65 	return 0;
66 
67 err:
68 	free_pages((unsigned long)shm->kaddr, order);
69 	return rc;
70 }
71 
72 static void optee_bus_scan(struct work_struct *work)
73 {
74 	WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
75 }
76 
77 int optee_open(struct tee_context *ctx, bool cap_memref_null)
78 {
79 	struct optee_context_data *ctxdata;
80 	struct tee_device *teedev = ctx->teedev;
81 	struct optee *optee = tee_get_drvdata(teedev);
82 
83 	ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
84 	if (!ctxdata)
85 		return -ENOMEM;
86 
87 	if (teedev == optee->supp_teedev) {
88 		bool busy = true;
89 
90 		mutex_lock(&optee->supp.mutex);
91 		if (!optee->supp.ctx) {
92 			busy = false;
93 			optee->supp.ctx = ctx;
94 		}
95 		mutex_unlock(&optee->supp.mutex);
96 		if (busy) {
97 			kfree(ctxdata);
98 			return -EBUSY;
99 		}
100 
101 		if (!optee->scan_bus_done) {
102 			INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
103 			optee->scan_bus_wq = create_workqueue("optee_bus_scan");
104 			if (!optee->scan_bus_wq) {
105 				kfree(ctxdata);
106 				return -ECHILD;
107 			}
108 			queue_work(optee->scan_bus_wq, &optee->scan_bus_work);
109 			optee->scan_bus_done = true;
110 		}
111 	}
112 	mutex_init(&ctxdata->mutex);
113 	INIT_LIST_HEAD(&ctxdata->sess_list);
114 
115 	ctx->cap_memref_null = cap_memref_null;
116 	ctx->data = ctxdata;
117 	return 0;
118 }
119 
120 static void optee_release_helper(struct tee_context *ctx,
121 				 int (*close_session)(struct tee_context *ctx,
122 						      u32 session))
123 {
124 	struct optee_context_data *ctxdata = ctx->data;
125 	struct optee_session *sess;
126 	struct optee_session *sess_tmp;
127 
128 	if (!ctxdata)
129 		return;
130 
131 	list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
132 				 list_node) {
133 		list_del(&sess->list_node);
134 		close_session(ctx, sess->session_id);
135 		kfree(sess);
136 	}
137 	kfree(ctxdata);
138 	ctx->data = NULL;
139 }
140 
141 void optee_release(struct tee_context *ctx)
142 {
143 	optee_release_helper(ctx, optee_close_session_helper);
144 }
145 
146 void optee_release_supp(struct tee_context *ctx)
147 {
148 	struct optee *optee = tee_get_drvdata(ctx->teedev);
149 
150 	optee_release_helper(ctx, optee_close_session_helper);
151 	if (optee->scan_bus_wq) {
152 		destroy_workqueue(optee->scan_bus_wq);
153 		optee->scan_bus_wq = NULL;
154 	}
155 	optee_supp_release(&optee->supp);
156 }
157 
158 void optee_remove_common(struct optee *optee)
159 {
160 	/* Unregister OP-TEE specific client devices on TEE bus */
161 	optee_unregister_devices();
162 
163 	optee_notif_uninit(optee);
164 	teedev_close_context(optee->ctx);
165 	/*
166 	 * The two devices have to be unregistered before we can free the
167 	 * other resources.
168 	 */
169 	tee_device_unregister(optee->supp_teedev);
170 	tee_device_unregister(optee->teedev);
171 
172 	tee_shm_pool_free(optee->pool);
173 	optee_supp_uninit(&optee->supp);
174 	mutex_destroy(&optee->call_queue.mutex);
175 }
176 
177 static int smc_abi_rc;
178 static int ffa_abi_rc;
179 
180 static int optee_core_init(void)
181 {
182 	/*
183 	 * The kernel may have crashed at the same time that all available
184 	 * secure world threads were suspended and we cannot reschedule the
185 	 * suspended threads without access to the crashed kernel's wait_queue.
186 	 * Therefore, we cannot reliably initialize the OP-TEE driver in the
187 	 * kdump kernel.
188 	 */
189 	if (is_kdump_kernel())
190 		return -ENODEV;
191 
192 	smc_abi_rc = optee_smc_abi_register();
193 	ffa_abi_rc = optee_ffa_abi_register();
194 
195 	/* If both failed there's no point with this module */
196 	if (smc_abi_rc && ffa_abi_rc)
197 		return smc_abi_rc;
198 	return 0;
199 }
200 module_init(optee_core_init);
201 
202 static void optee_core_exit(void)
203 {
204 	if (!smc_abi_rc)
205 		optee_smc_abi_unregister();
206 	if (!ffa_abi_rc)
207 		optee_ffa_abi_unregister();
208 }
209 module_exit(optee_core_exit);
210 
211 MODULE_AUTHOR("Linaro");
212 MODULE_DESCRIPTION("OP-TEE driver");
213 MODULE_VERSION("1.0");
214 MODULE_LICENSE("GPL v2");
215 MODULE_ALIAS("platform:optee");
216