xref: /linux/drivers/tee/optee/core.c (revision 95444b9eeb8c5c0330563931d70c61ca3b101548)
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/slab.h>
14 #include <linux/string.h>
15 #include <linux/tee_core.h>
16 #include <linux/types.h>
17 #include "optee_private.h"
18 
19 static void optee_bus_scan(struct work_struct *work)
20 {
21 	WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
22 }
23 
24 int optee_open(struct tee_context *ctx, bool cap_memref_null)
25 {
26 	struct optee_context_data *ctxdata;
27 	struct tee_device *teedev = ctx->teedev;
28 	struct optee *optee = tee_get_drvdata(teedev);
29 
30 	ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
31 	if (!ctxdata)
32 		return -ENOMEM;
33 
34 	if (teedev == optee->supp_teedev) {
35 		bool busy = true;
36 
37 		mutex_lock(&optee->supp.mutex);
38 		if (!optee->supp.ctx) {
39 			busy = false;
40 			optee->supp.ctx = ctx;
41 		}
42 		mutex_unlock(&optee->supp.mutex);
43 		if (busy) {
44 			kfree(ctxdata);
45 			return -EBUSY;
46 		}
47 
48 		if (!optee->scan_bus_done) {
49 			INIT_WORK(&optee->scan_bus_work, optee_bus_scan);
50 			schedule_work(&optee->scan_bus_work);
51 			optee->scan_bus_done = true;
52 		}
53 	}
54 	mutex_init(&ctxdata->mutex);
55 	INIT_LIST_HEAD(&ctxdata->sess_list);
56 
57 	ctx->cap_memref_null = cap_memref_null;
58 	ctx->data = ctxdata;
59 	return 0;
60 }
61 
62 static void optee_release_helper(struct tee_context *ctx,
63 				 int (*close_session)(struct tee_context *ctx,
64 						      u32 session,
65 						      bool system_thread))
66 {
67 	struct optee_context_data *ctxdata = ctx->data;
68 	struct optee_session *sess;
69 	struct optee_session *sess_tmp;
70 
71 	if (!ctxdata)
72 		return;
73 
74 	list_for_each_entry_safe(sess, sess_tmp, &ctxdata->sess_list,
75 				 list_node) {
76 		list_del(&sess->list_node);
77 		close_session(ctx, sess->session_id, sess->use_sys_thread);
78 		kfree(sess);
79 	}
80 	kfree(ctxdata);
81 	ctx->data = NULL;
82 }
83 
84 void optee_release(struct tee_context *ctx)
85 {
86 	optee_release_helper(ctx, optee_close_session_helper);
87 }
88 
89 void optee_release_supp(struct tee_context *ctx)
90 {
91 	struct optee *optee = tee_get_drvdata(ctx->teedev);
92 
93 	optee_release_helper(ctx, optee_close_session_helper);
94 
95 	optee_supp_release(&optee->supp);
96 }
97 
98 void optee_remove_common(struct optee *optee)
99 {
100 	/* Unregister OP-TEE specific client devices on TEE bus */
101 	optee_unregister_devices();
102 
103 	optee_notif_uninit(optee);
104 	optee_shm_arg_cache_uninit(optee);
105 	teedev_close_context(optee->ctx);
106 	/*
107 	 * The two devices have to be unregistered before we can free the
108 	 * other resources.
109 	 */
110 	tee_device_unregister(optee->supp_teedev);
111 	tee_device_unregister(optee->teedev);
112 
113 	tee_shm_pool_free(optee->pool);
114 	optee_supp_uninit(&optee->supp);
115 	mutex_destroy(&optee->call_queue.mutex);
116 }
117 
118 static int smc_abi_rc;
119 static int ffa_abi_rc;
120 
121 static int __init optee_core_init(void)
122 {
123 	/*
124 	 * The kernel may have crashed at the same time that all available
125 	 * secure world threads were suspended and we cannot reschedule the
126 	 * suspended threads without access to the crashed kernel's wait_queue.
127 	 * Therefore, we cannot reliably initialize the OP-TEE driver in the
128 	 * kdump kernel.
129 	 */
130 	if (is_kdump_kernel())
131 		return -ENODEV;
132 
133 	smc_abi_rc = optee_smc_abi_register();
134 	ffa_abi_rc = optee_ffa_abi_register();
135 
136 	/* If both failed there's no point with this module */
137 	if (smc_abi_rc && ffa_abi_rc)
138 		return smc_abi_rc;
139 	return 0;
140 }
141 module_init(optee_core_init);
142 
143 static void __exit optee_core_exit(void)
144 {
145 	if (!smc_abi_rc)
146 		optee_smc_abi_unregister();
147 	if (!ffa_abi_rc)
148 		optee_ffa_abi_unregister();
149 }
150 module_exit(optee_core_exit);
151 
152 MODULE_AUTHOR("Linaro");
153 MODULE_DESCRIPTION("OP-TEE driver");
154 MODULE_VERSION("1.0");
155 MODULE_LICENSE("GPL v2");
156 MODULE_ALIAS("platform:optee");
157