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