1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2021, Linaro Limited
4 * Copyright (c) 2016, EPAM Systems
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/crash_dump.h>
10 #include <linux/errno.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"
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,
29 unsigned int order = get_order(size);
33 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
37 shm->kaddr = page_address(page);
38 shm->paddr = page_to_phys(page);
39 shm->size = PAGE_SIZE << order;
42 unsigned int nr_pages = 1 << order, i;
45 pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
51 for (i = 0; i < nr_pages; i++) {
56 shm->flags |= TEE_SHM_REGISTER;
57 rc = shm_register(shm->ctx, shm, pages, nr_pages,
58 (unsigned long)shm->kaddr);
67 __free_pages(page, order);
71 static void optee_bus_scan(struct work_struct *work)
73 WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
76 int optee_open(struct tee_context *ctx, bool cap_memref_null)
78 struct optee_context_data *ctxdata;
79 struct tee_device *teedev = ctx->teedev;
80 struct optee *optee = tee_get_drvdata(teedev);
82 ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
86 if (teedev == optee->supp_teedev) {
89 mutex_lock(&optee->supp.mutex);
90 if (!optee->supp.ctx) {
92 optee->supp.ctx = ctx;
94 mutex_unlock(&optee->supp.mutex);
100 if (!optee->scan_bus_done) {
101 INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
102 optee->scan_bus_wq = create_workqueue("optee_bus_scan");
103 if (!optee->scan_bus_wq) {
107 queue_work(optee->scan_bus_wq, &optee->scan_bus_work);
108 optee->scan_bus_done = true;
111 mutex_init(&ctxdata->mutex);
112 INIT_LIST_HEAD(&ctxdata->sess_list);
114 ctx->cap_memref_null = cap_memref_null;
119 static void optee_release_helper(struct tee_context *ctx,
120 int (*close_session)(struct tee_context *ctx,
123 struct optee_context_data *ctxdata = ctx->data;
124 struct optee_session *sess;
125 struct optee_session *sess_tmp;
130 list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
132 list_del(&sess->list_node);
133 close_session(ctx, sess->session_id);
140 void optee_release(struct tee_context *ctx)
142 optee_release_helper(ctx, optee_close_session_helper);
145 void optee_release_supp(struct tee_context *ctx)
147 struct optee *optee = tee_get_drvdata(ctx->teedev);
149 optee_release_helper(ctx, optee_close_session_helper);
150 if (optee->scan_bus_wq) {
151 destroy_workqueue(optee->scan_bus_wq);
152 optee->scan_bus_wq = NULL;
154 optee_supp_release(&optee->supp);
157 void optee_remove_common(struct optee *optee)
159 /* Unregister OP-TEE specific client devices on TEE bus */
160 optee_unregister_devices();
162 optee_notif_uninit(optee);
164 * The two devices have to be unregistered before we can free the
167 tee_device_unregister(optee->supp_teedev);
168 tee_device_unregister(optee->teedev);
170 tee_shm_pool_free(optee->pool);
171 optee_supp_uninit(&optee->supp);
172 mutex_destroy(&optee->call_queue.mutex);
175 static int smc_abi_rc;
176 static int ffa_abi_rc;
178 static int optee_core_init(void)
181 * The kernel may have crashed at the same time that all available
182 * secure world threads were suspended and we cannot reschedule the
183 * suspended threads without access to the crashed kernel's wait_queue.
184 * Therefore, we cannot reliably initialize the OP-TEE driver in the
187 if (is_kdump_kernel())
190 smc_abi_rc = optee_smc_abi_register();
191 ffa_abi_rc = optee_ffa_abi_register();
193 /* If both failed there's no point with this module */
194 if (smc_abi_rc && ffa_abi_rc)
198 module_init(optee_core_init);
200 static void optee_core_exit(void)
203 optee_smc_abi_unregister();
205 optee_ffa_abi_unregister();
207 module_exit(optee_core_exit);
209 MODULE_AUTHOR("Linaro");
210 MODULE_DESCRIPTION("OP-TEE driver");
211 MODULE_VERSION("1.0");
212 MODULE_LICENSE("GPL v2");
213 MODULE_ALIAS("platform:optee");