1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
2 | /* |
3 | * Copyright (c) 2015-2021, 2023 Linaro Limited |
4 | */ |
5 | |
6 | #ifndef OPTEE_PRIVATE_H |
7 | #define OPTEE_PRIVATE_H |
8 | |
9 | #include <linux/arm-smccc.h> |
10 | #include <linux/rhashtable.h> |
11 | #include <linux/semaphore.h> |
12 | #include <linux/tee_drv.h> |
13 | #include <linux/types.h> |
14 | #include "optee_msg.h" |
15 | |
16 | #define DRIVER_NAME "optee" |
17 | |
18 | #define OPTEE_MAX_ARG_SIZE 1024 |
19 | |
20 | /* Some Global Platform error codes used in this driver */ |
21 | #define TEEC_SUCCESS 0x00000000 |
22 | #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006 |
23 | #define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A |
24 | #define TEEC_ERROR_COMMUNICATION 0xFFFF000E |
25 | #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C |
26 | #define TEEC_ERROR_BUSY 0xFFFF000D |
27 | #define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010 |
28 | |
29 | #define TEEC_ORIGIN_COMMS 0x00000002 |
30 | |
31 | /* |
32 | * This value should be larger than the number threads in secure world to |
33 | * meet the need from secure world. The number of threads in secure world |
34 | * are usually not even close to 255 so we should be safe for now. |
35 | */ |
36 | #define OPTEE_DEFAULT_MAX_NOTIF_VALUE 255 |
37 | |
38 | typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long, |
39 | unsigned long, unsigned long, unsigned long, |
40 | unsigned long, unsigned long, |
41 | struct arm_smccc_res *); |
42 | |
43 | /* |
44 | * struct optee_call_waiter - TEE entry may need to wait for a free TEE thread |
45 | * @list_node Reference in waiters list |
46 | * @c Waiting completion reference |
47 | * @sys_thread True if waiter belongs to a system thread |
48 | */ |
49 | struct optee_call_waiter { |
50 | struct list_head list_node; |
51 | struct completion c; |
52 | bool sys_thread; |
53 | }; |
54 | |
55 | /* |
56 | * struct optee_call_queue - OP-TEE call queue management |
57 | * @mutex Serializes access to this struct |
58 | * @waiters List of threads waiting to enter OP-TEE |
59 | * @total_thread_count Overall number of thread context in OP-TEE or 0 |
60 | * @free_thread_count Number of threads context free in OP-TEE |
61 | * @sys_thread_req_count Number of registered system thread sessions |
62 | */ |
63 | struct optee_call_queue { |
64 | /* Serializes access to this struct */ |
65 | struct mutex mutex; |
66 | struct list_head waiters; |
67 | int total_thread_count; |
68 | int free_thread_count; |
69 | int sys_thread_req_count; |
70 | }; |
71 | |
72 | struct optee_notif { |
73 | u_int max_key; |
74 | /* Serializes access to the elements below in this struct */ |
75 | spinlock_t lock; |
76 | struct list_head db; |
77 | u_long *bitmap; |
78 | }; |
79 | |
80 | #define OPTEE_SHM_ARG_ALLOC_PRIV BIT(0) |
81 | #define OPTEE_SHM_ARG_SHARED BIT(1) |
82 | struct optee_shm_arg_entry; |
83 | struct optee_shm_arg_cache { |
84 | u32 flags; |
85 | /* Serializes access to this struct */ |
86 | struct mutex mutex; |
87 | struct list_head shm_args; |
88 | }; |
89 | |
90 | /** |
91 | * struct optee_supp - supplicant synchronization struct |
92 | * @ctx the context of current connected supplicant. |
93 | * if !NULL the supplicant device is available for use, |
94 | * else busy |
95 | * @mutex: held while accessing content of this struct |
96 | * @req_id: current request id if supplicant is doing synchronous |
97 | * communication, else -1 |
98 | * @reqs: queued request not yet retrieved by supplicant |
99 | * @idr: IDR holding all requests currently being processed |
100 | * by supplicant |
101 | * @reqs_c: completion used by supplicant when waiting for a |
102 | * request to be queued. |
103 | */ |
104 | struct optee_supp { |
105 | /* Serializes access to this struct */ |
106 | struct mutex mutex; |
107 | struct tee_context *ctx; |
108 | |
109 | int req_id; |
110 | struct list_head reqs; |
111 | struct idr idr; |
112 | struct completion reqs_c; |
113 | }; |
114 | |
115 | /* |
116 | * struct optee_pcpu - per cpu notif private struct passed to work functions |
117 | * @optee optee device reference |
118 | */ |
119 | struct optee_pcpu { |
120 | struct optee *optee; |
121 | }; |
122 | |
123 | /* |
124 | * struct optee_smc - optee smc communication struct |
125 | * @invoke_fn handler function to invoke secure monitor |
126 | * @memremaped_shm virtual address of memory in shared memory pool |
127 | * @sec_caps: secure world capabilities defined by |
128 | * OPTEE_SMC_SEC_CAP_* in optee_smc.h |
129 | * @notif_irq interrupt used as async notification by OP-TEE or 0 |
130 | * @optee_pcpu per_cpu optee instance for per cpu work or NULL |
131 | * @notif_pcpu_wq workqueue for per cpu asynchronous notification or NULL |
132 | * @notif_pcpu_work work for per cpu asynchronous notification |
133 | * @notif_cpuhp_state CPU hotplug state assigned for pcpu interrupt management |
134 | */ |
135 | struct optee_smc { |
136 | optee_invoke_fn *invoke_fn; |
137 | void *memremaped_shm; |
138 | u32 sec_caps; |
139 | unsigned int notif_irq; |
140 | struct optee_pcpu __percpu *optee_pcpu; |
141 | struct workqueue_struct *notif_pcpu_wq; |
142 | struct work_struct notif_pcpu_work; |
143 | unsigned int notif_cpuhp_state; |
144 | }; |
145 | |
146 | /** |
147 | * struct optee_ffa_data - FFA communication struct |
148 | * @ffa_dev FFA device, contains the destination id, the id of |
149 | * OP-TEE in secure world |
150 | * @bottom_half_value Notification ID used for bottom half signalling or |
151 | * U32_MAX if unused |
152 | * @mutex Serializes access to @global_ids |
153 | * @global_ids FF-A shared memory global handle translation |
154 | */ |
155 | struct optee_ffa { |
156 | struct ffa_device *ffa_dev; |
157 | u32 bottom_half_value; |
158 | /* Serializes access to @global_ids */ |
159 | struct mutex mutex; |
160 | struct rhashtable global_ids; |
161 | }; |
162 | |
163 | struct optee; |
164 | |
165 | /** |
166 | * struct optee_ops - OP-TEE driver internal operations |
167 | * @do_call_with_arg: enters OP-TEE in secure world |
168 | * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters |
169 | * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param |
170 | * |
171 | * These OPs are only supposed to be used internally in the OP-TEE driver |
172 | * as a way of abstracting the different methogs of entering OP-TEE in |
173 | * secure world. |
174 | */ |
175 | struct optee_ops { |
176 | int (*do_call_with_arg)(struct tee_context *ctx, |
177 | struct tee_shm *shm_arg, u_int offs, |
178 | bool system_thread); |
179 | int (*to_msg_param)(struct optee *optee, |
180 | struct optee_msg_param *msg_params, |
181 | size_t num_params, const struct tee_param *params); |
182 | int (*from_msg_param)(struct optee *optee, struct tee_param *params, |
183 | size_t num_params, |
184 | const struct optee_msg_param *msg_params); |
185 | }; |
186 | |
187 | /** |
188 | * struct optee - main service struct |
189 | * @supp_teedev: supplicant device |
190 | * @teedev: client device |
191 | * @ops: internal callbacks for different ways to reach secure |
192 | * world |
193 | * @ctx: driver internal TEE context |
194 | * @smc: specific to SMC ABI |
195 | * @ffa: specific to FF-A ABI |
196 | * @call_queue: queue of threads waiting to call @invoke_fn |
197 | * @notif: notification synchronization struct |
198 | * @supp: supplicant synchronization struct for RPC to supplicant |
199 | * @pool: shared memory pool |
200 | * @rpc_param_count: If > 0 number of RPC parameters to make room for |
201 | * @scan_bus_done flag if device registation was already done. |
202 | * @scan_bus_work workq to scan optee bus and register optee drivers |
203 | */ |
204 | struct optee { |
205 | struct tee_device *supp_teedev; |
206 | struct tee_device *teedev; |
207 | const struct optee_ops *ops; |
208 | struct tee_context *ctx; |
209 | union { |
210 | struct optee_smc smc; |
211 | struct optee_ffa ffa; |
212 | }; |
213 | struct optee_shm_arg_cache shm_arg_cache; |
214 | struct optee_call_queue call_queue; |
215 | struct optee_notif notif; |
216 | struct optee_supp supp; |
217 | struct tee_shm_pool *pool; |
218 | unsigned int rpc_param_count; |
219 | bool scan_bus_done; |
220 | struct work_struct scan_bus_work; |
221 | }; |
222 | |
223 | struct optee_session { |
224 | struct list_head list_node; |
225 | u32 session_id; |
226 | bool use_sys_thread; |
227 | }; |
228 | |
229 | struct optee_context_data { |
230 | /* Serializes access to this struct */ |
231 | struct mutex mutex; |
232 | struct list_head sess_list; |
233 | }; |
234 | |
235 | struct optee_rpc_param { |
236 | u32 a0; |
237 | u32 a1; |
238 | u32 a2; |
239 | u32 a3; |
240 | u32 a4; |
241 | u32 a5; |
242 | u32 a6; |
243 | u32 a7; |
244 | }; |
245 | |
246 | /* Holds context that is preserved during one STD call */ |
247 | struct optee_call_ctx { |
248 | /* information about pages list used in last allocation */ |
249 | void *pages_list; |
250 | size_t num_entries; |
251 | }; |
252 | |
253 | int optee_notif_init(struct optee *optee, u_int max_key); |
254 | void optee_notif_uninit(struct optee *optee); |
255 | int optee_notif_wait(struct optee *optee, u_int key); |
256 | int optee_notif_send(struct optee *optee, u_int key); |
257 | |
258 | u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params, |
259 | struct tee_param *param); |
260 | |
261 | void optee_supp_init(struct optee_supp *supp); |
262 | void optee_supp_uninit(struct optee_supp *supp); |
263 | void optee_supp_release(struct optee_supp *supp); |
264 | |
265 | int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params, |
266 | struct tee_param *param); |
267 | int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params, |
268 | struct tee_param *param); |
269 | |
270 | int optee_open_session(struct tee_context *ctx, |
271 | struct tee_ioctl_open_session_arg *arg, |
272 | struct tee_param *param); |
273 | int optee_system_session(struct tee_context *ctx, u32 session); |
274 | int optee_close_session_helper(struct tee_context *ctx, u32 session, |
275 | bool system_thread); |
276 | int optee_close_session(struct tee_context *ctx, u32 session); |
277 | int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg, |
278 | struct tee_param *param); |
279 | int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session); |
280 | |
281 | #define PTA_CMD_GET_DEVICES 0x0 |
282 | #define PTA_CMD_GET_DEVICES_SUPP 0x1 |
283 | int optee_enumerate_devices(u32 func); |
284 | void optee_unregister_devices(void); |
285 | |
286 | int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm, |
287 | size_t size, size_t align, |
288 | int (*shm_register)(struct tee_context *ctx, |
289 | struct tee_shm *shm, |
290 | struct page **pages, |
291 | size_t num_pages, |
292 | unsigned long start)); |
293 | void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm, |
294 | int (*shm_unregister)(struct tee_context *ctx, |
295 | struct tee_shm *shm)); |
296 | |
297 | |
298 | void optee_remove_common(struct optee *optee); |
299 | int optee_open(struct tee_context *ctx, bool cap_memref_null); |
300 | void optee_release(struct tee_context *ctx); |
301 | void optee_release_supp(struct tee_context *ctx); |
302 | |
303 | static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr, |
304 | const struct optee_msg_param *mp) |
305 | { |
306 | p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT + |
307 | attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; |
308 | p->u.value.a = mp->u.value.a; |
309 | p->u.value.b = mp->u.value.b; |
310 | p->u.value.c = mp->u.value.c; |
311 | } |
312 | |
313 | static inline void optee_to_msg_param_value(struct optee_msg_param *mp, |
314 | const struct tee_param *p) |
315 | { |
316 | mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr - |
317 | TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT; |
318 | mp->u.value.a = p->u.value.a; |
319 | mp->u.value.b = p->u.value.b; |
320 | mp->u.value.c = p->u.value.c; |
321 | } |
322 | |
323 | void optee_cq_init(struct optee_call_queue *cq, int thread_count); |
324 | void optee_cq_wait_init(struct optee_call_queue *cq, |
325 | struct optee_call_waiter *w, bool sys_thread); |
326 | void optee_cq_wait_for_completion(struct optee_call_queue *cq, |
327 | struct optee_call_waiter *w); |
328 | void optee_cq_wait_final(struct optee_call_queue *cq, |
329 | struct optee_call_waiter *w); |
330 | int optee_check_mem_type(unsigned long start, size_t num_pages); |
331 | |
332 | void optee_shm_arg_cache_init(struct optee *optee, u32 flags); |
333 | void optee_shm_arg_cache_uninit(struct optee *optee); |
334 | struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx, |
335 | size_t num_params, |
336 | struct optee_shm_arg_entry **entry, |
337 | struct tee_shm **shm_ret, |
338 | u_int *offs); |
339 | void optee_free_msg_arg(struct tee_context *ctx, |
340 | struct optee_shm_arg_entry *entry, u_int offs); |
341 | size_t optee_msg_arg_size(size_t rpc_param_count); |
342 | |
343 | |
344 | struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz); |
345 | void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm); |
346 | void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee, |
347 | struct optee_msg_arg *arg); |
348 | |
349 | int optee_do_bottom_half(struct tee_context *ctx); |
350 | int optee_stop_async_notif(struct tee_context *ctx); |
351 | |
352 | /* |
353 | * Small helpers |
354 | */ |
355 | |
356 | static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1) |
357 | { |
358 | return (void *)(unsigned long)(((u64)reg0 << 32) | reg1); |
359 | } |
360 | |
361 | static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val) |
362 | { |
363 | *reg0 = val >> 32; |
364 | *reg1 = val; |
365 | } |
366 | |
367 | /* Registration of the ABIs */ |
368 | int optee_smc_abi_register(void); |
369 | void optee_smc_abi_unregister(void); |
370 | int optee_ffa_abi_register(void); |
371 | void optee_ffa_abi_unregister(void); |
372 | |
373 | #endif /*OPTEE_PRIVATE_H*/ |
374 | |