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