xref: /linux/drivers/tee/optee/optee_private.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
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/notifier.h>
11 #include <linux/rhashtable.h>
12 #include <linux/rpmb.h>
13 #include <linux/semaphore.h>
14 #include <linux/tee_core.h>
15 #include <linux/types.h>
16 #include "optee_msg.h"
17 
18 #define DRIVER_NAME "optee"
19 
20 #define OPTEE_MAX_ARG_SIZE	1024
21 
22 /* Some Global Platform error codes used in this driver */
23 #define TEEC_SUCCESS			0x00000000
24 #define TEEC_ERROR_BAD_PARAMETERS	0xFFFF0006
25 #define TEEC_ERROR_ITEM_NOT_FOUND	0xFFFF0008
26 #define TEEC_ERROR_NOT_SUPPORTED	0xFFFF000A
27 #define TEEC_ERROR_COMMUNICATION	0xFFFF000E
28 #define TEEC_ERROR_OUT_OF_MEMORY	0xFFFF000C
29 #define TEEC_ERROR_BUSY			0xFFFF000D
30 #define TEEC_ERROR_SHORT_BUFFER		0xFFFF0010
31 
32 /* API Return Codes are from the GP TEE Internal Core API Specification */
33 #define TEE_ERROR_TIMEOUT		0xFFFF3001
34 #define TEE_ERROR_STORAGE_NOT_AVAILABLE	0xF0100003
35 
36 #define TEEC_ORIGIN_COMMS		0x00000002
37 
38 /*
39  * This value should be larger than the number threads in secure world to
40  * meet the need from secure world. The number of threads in secure world
41  * are usually not even close to 255 so we should be safe for now.
42  */
43 #define OPTEE_DEFAULT_MAX_NOTIF_VALUE	255
44 
45 typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
46 				unsigned long, unsigned long, unsigned long,
47 				unsigned long, unsigned long,
48 				struct arm_smccc_res *);
49 
50 /**
51  * struct optee_call_waiter - TEE entry may need to wait for a free TEE thread
52  * @list_node:	reference in waiters list
53  * @c:		waiting completion reference
54  * @sys_thread:	true if waiter belongs to a system thread
55  */
56 struct optee_call_waiter {
57 	struct list_head list_node;
58 	struct completion c;
59 	bool sys_thread;
60 };
61 
62 /**
63  * struct optee_call_queue - OP-TEE call queue management
64  * @mutex:			serializes access to this struct
65  * @waiters:			list of threads waiting to enter OP-TEE
66  * @total_thread_count:		overall number of thread context in OP-TEE or 0
67  * @free_thread_count:		number of threads context free in OP-TEE
68  * @sys_thread_req_count:	number of registered system thread sessions
69  */
70 struct optee_call_queue {
71 	/* Serializes access to this struct */
72 	struct mutex mutex;
73 	struct list_head waiters;
74 	int total_thread_count;
75 	int free_thread_count;
76 	int sys_thread_req_count;
77 };
78 
79 struct optee_notif {
80 	u_int max_key;
81 	/* Serializes access to the elements below in this struct */
82 	spinlock_t lock;
83 	struct list_head db;
84 	u_long *bitmap;
85 };
86 
87 #define OPTEE_SHM_ARG_ALLOC_PRIV	BIT(0)
88 #define OPTEE_SHM_ARG_SHARED		BIT(1)
89 struct optee_shm_arg_entry;
90 struct optee_shm_arg_cache {
91 	u32 flags;
92 	/* Serializes access to this struct */
93 	struct mutex mutex;
94 	struct list_head shm_args;
95 };
96 
97 /**
98  * struct optee_supp - supplicant synchronization struct
99  * @mutex:	held while accessing content of this struct
100  * @ctx:	the context of current connected supplicant.
101  *		if !NULL the supplicant device is available for use,
102  *		else busy
103  * @req_id:	current request id if supplicant is doing synchronous
104  *		communication, else -1
105  * @reqs:	queued request not yet retrieved by supplicant
106  * @idr:	IDR holding all requests currently being processed
107  *		by supplicant
108  * @reqs_c:	completion used by supplicant when waiting for a
109  *		request to be queued.
110  */
111 struct optee_supp {
112 	/* Serializes access to this struct */
113 	struct mutex mutex;
114 	struct tee_context *ctx;
115 
116 	int req_id;
117 	struct list_head reqs;
118 	struct idr idr;
119 	struct completion reqs_c;
120 };
121 
122 /**
123  * struct optee_pcpu - per cpu notif private struct passed to work functions
124  * @optee:	optee device reference
125  */
126 struct optee_pcpu {
127 	struct optee *optee;
128 };
129 
130 /**
131  * struct optee_smc - optee smc communication struct
132  * @invoke_fn:		handler function to invoke secure monitor
133  * @memremaped_shm:	virtual address of memory in shared memory pool
134  * @sec_caps:		secure world capabilities defined by
135  *			OPTEE_SMC_SEC_CAP_* in optee_smc.h
136  * @notif_irq:		interrupt used as async notification by OP-TEE or 0
137  * @optee_pcpu:		per_cpu optee instance for per cpu work or NULL
138  * @notif_pcpu_wq:	workqueue for per cpu asynchronous notification or NULL
139  * @notif_pcpu_work:	work for per cpu asynchronous notification
140  * @notif_cpuhp_state:	CPU hotplug state assigned for pcpu interrupt management
141  */
142 struct optee_smc {
143 	optee_invoke_fn *invoke_fn;
144 	void *memremaped_shm;
145 	u32 sec_caps;
146 	unsigned int notif_irq;
147 	struct optee_pcpu __percpu *optee_pcpu;
148 	struct workqueue_struct *notif_pcpu_wq;
149 	struct work_struct notif_pcpu_work;
150 	unsigned int notif_cpuhp_state;
151 };
152 
153 /**
154  * struct optee_ffa -  FFA communication struct
155  * @ffa_dev:		FFA device, contains the destination id, the id of
156  *			OP-TEE in secure world
157  * @bottom_half_value:	notification ID used for bottom half signalling or
158  *			U32_MAX if unused
159  * @mutex:		serializes access to @global_ids
160  * @global_ids:		FF-A shared memory global handle translation
161  * @notif_wq:		workqueue for FF-A asynchronous notification
162  * @notif_work:		work for FF-A asynchronous notification
163  */
164 struct optee_ffa {
165 	struct ffa_device *ffa_dev;
166 	u32 bottom_half_value;
167 	/* Serializes access to @global_ids */
168 	struct mutex mutex;
169 	struct rhashtable global_ids;
170 	struct workqueue_struct *notif_wq;
171 	struct work_struct notif_work;
172 };
173 
174 struct optee;
175 
176 /**
177  * struct optee_revision - OP-TEE OS revision reported by secure world
178  * @os_major:		OP-TEE OS major version
179  * @os_minor:		OP-TEE OS minor version
180  * @os_build_id:	OP-TEE OS build identifier (0 if unspecified)
181  *
182  * Values come from OPTEE_SMC_CALL_GET_OS_REVISION (SMC ABI) or
183  * OPTEE_FFA_GET_OS_VERSION (FF-A ABI); this is the trusted OS revision, not an
184  * FF-A ABI version.
185  */
186 struct optee_revision {
187 	u32 os_major;
188 	u32 os_minor;
189 	u64 os_build_id;
190 };
191 
192 int optee_get_revision(struct tee_device *teedev, char *buf, size_t len);
193 
194 /**
195  * struct optee_ops - OP-TEE driver internal operations
196  * @do_call_with_arg:	enters OP-TEE in secure world
197  * @to_msg_param:	converts from struct tee_param to OPTEE_MSG parameters
198  * @from_msg_param:	converts from OPTEE_MSG parameters to struct tee_param
199  * @lend_protmem:	lends physically contiguous memory as restricted
200  *			memory, inaccessible by the kernel
201  * @reclaim_protmem:	reclaims restricted memory previously lent with
202  *			@lend_protmem() and makes it accessible by the
203  *			kernel again
204  *
205  * These OPs are only supposed to be used internally in the OP-TEE driver
206  * as a way of abstracting the different methods of entering OP-TEE in
207  * secure world.
208  */
209 struct optee_ops {
210 	int (*do_call_with_arg)(struct tee_context *ctx,
211 				struct tee_shm *shm_arg, u_int offs,
212 				bool system_thread);
213 	int (*to_msg_param)(struct optee *optee,
214 			    struct optee_msg_param *msg_params,
215 			    size_t num_params, const struct tee_param *params);
216 	int (*from_msg_param)(struct optee *optee, struct tee_param *params,
217 			      size_t num_params,
218 			      const struct optee_msg_param *msg_params);
219 	int (*lend_protmem)(struct optee *optee, struct tee_shm *protmem,
220 			    u32 *mem_attr, unsigned int ma_count,
221 			    u32 use_case);
222 	int (*reclaim_protmem)(struct optee *optee, struct tee_shm *protmem);
223 };
224 
225 /**
226  * struct optee - main service struct
227  * @supp_teedev:		supplicant device
228  * @teedev:			client device
229  * @ops:			internal callbacks for different ways to reach
230  *				secure world
231  * @ctx:			driver internal TEE context
232  * @smc:			specific to SMC ABI
233  * @ffa:			specific to FF-A ABI
234  * @shm_arg_cache:		shared memory cache argument
235  * @call_queue:			queue of threads waiting to call @invoke_fn
236  * @notif:			notification synchronization struct
237  * @supp:			supplicant synchronization struct for RPC to
238  *				supplicant
239  * @pool:			shared memory pool
240  * @rpmb_dev_mutex:		mutex protecting @rpmb_dev
241  * @rpmb_dev:			current RPMB device or NULL
242  * @rpmb_intf:			RPMB notifier block
243  * @rpc_param_count:		if > 0 number of RPC parameters to make room for
244  * @scan_bus_done:		flag if device registation was already done
245  * @rpmb_scan_bus_done:		flag if device registation of RPMB dependent
246  *				devices was already done
247  * @in_kernel_rpmb_routing:	flag if OP-TEE supports in-kernel RPMB routing
248  * @scan_bus_work:		workq to scan optee bus and register optee
249  *				drivers
250  * @rpmb_scan_bus_work:		workq to for an RPMB device and to scan optee
251  *				bus and register RPMB dependent optee drivers
252  * @revision:			OP-TEE OS revision
253  */
254 struct optee {
255 	struct tee_device *supp_teedev;
256 	struct tee_device *teedev;
257 	const struct optee_ops *ops;
258 	struct tee_context *ctx;
259 	union {
260 		struct optee_smc smc;
261 		struct optee_ffa ffa;
262 	};
263 	struct optee_shm_arg_cache shm_arg_cache;
264 	struct optee_call_queue call_queue;
265 	struct optee_notif notif;
266 	struct optee_supp supp;
267 	struct tee_shm_pool *pool;
268 	/* Protects rpmb_dev pointer */
269 	struct mutex rpmb_dev_mutex;
270 	struct rpmb_dev *rpmb_dev;
271 	struct notifier_block rpmb_intf;
272 	unsigned int rpc_param_count;
273 	bool scan_bus_done;
274 	bool rpmb_scan_bus_done;
275 	bool in_kernel_rpmb_routing;
276 	struct work_struct scan_bus_work;
277 	struct work_struct rpmb_scan_bus_work;
278 	struct optee_revision revision;
279 };
280 
281 struct optee_session {
282 	struct list_head list_node;
283 	u32 session_id;
284 	bool use_sys_thread;
285 };
286 
287 struct optee_context_data {
288 	/* Serializes access to this struct */
289 	struct mutex mutex;
290 	struct list_head sess_list;
291 };
292 
293 struct optee_rpc_param {
294 	u32	a0;
295 	u32	a1;
296 	u32	a2;
297 	u32	a3;
298 	u32	a4;
299 	u32	a5;
300 	u32	a6;
301 	u32	a7;
302 };
303 
304 /* Holds context that is preserved during one STD call */
305 struct optee_call_ctx {
306 	/* information about pages list used in last allocation */
307 	void *pages_list;
308 	size_t num_entries;
309 };
310 
311 extern struct blocking_notifier_head optee_rpmb_intf_added;
312 
313 int optee_set_dma_mask(struct optee *optee, u_int pa_width);
314 
315 int optee_notif_init(struct optee *optee, u_int max_key);
316 void optee_notif_uninit(struct optee *optee);
317 int optee_notif_wait(struct optee *optee, u_int key, u32 timeout);
318 int optee_notif_send(struct optee *optee, u_int key);
319 
320 u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
321 			struct tee_param *param);
322 
323 void optee_supp_init(struct optee_supp *supp);
324 void optee_supp_uninit(struct optee_supp *supp);
325 void optee_supp_release(struct optee_supp *supp);
326 struct tee_protmem_pool *optee_protmem_alloc_dyn_pool(struct optee *optee,
327 						      enum tee_dma_heap_id id);
328 
329 int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
330 		    struct tee_param *param);
331 int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
332 		    struct tee_param *param);
333 
334 int optee_open_session(struct tee_context *ctx,
335 		       struct tee_ioctl_open_session_arg *arg,
336 		       struct tee_param *param);
337 int optee_system_session(struct tee_context *ctx, u32 session);
338 int optee_close_session_helper(struct tee_context *ctx, u32 session,
339 			       bool system_thread);
340 int optee_close_session(struct tee_context *ctx, u32 session);
341 int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
342 		      struct tee_param *param);
343 int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
344 
345 #define PTA_CMD_GET_DEVICES		0x0
346 #define PTA_CMD_GET_DEVICES_SUPP	0x1
347 #define PTA_CMD_GET_DEVICES_RPMB	0x2
348 int optee_enumerate_devices(u32 func);
349 void optee_unregister_devices(void);
350 void optee_bus_scan_rpmb(struct work_struct *work);
351 int optee_rpmb_intf_rdev(struct notifier_block *intf, unsigned long action,
352 			 void *data);
353 
354 void optee_set_dev_group(struct optee *optee);
355 void optee_remove_common(struct optee *optee);
356 int optee_open(struct tee_context *ctx, bool cap_memref_null);
357 void optee_release(struct tee_context *ctx);
358 void optee_release_supp(struct tee_context *ctx);
359 
360 static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,
361 					      const struct optee_msg_param *mp)
362 {
363 	p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
364 		  attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
365 	p->u.value.a = mp->u.value.a;
366 	p->u.value.b = mp->u.value.b;
367 	p->u.value.c = mp->u.value.c;
368 }
369 
370 static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
371 					    const struct tee_param *p)
372 {
373 	mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
374 		   TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
375 	mp->u.value.a = p->u.value.a;
376 	mp->u.value.b = p->u.value.b;
377 	mp->u.value.c = p->u.value.c;
378 }
379 
380 void optee_cq_init(struct optee_call_queue *cq, int thread_count);
381 void optee_cq_wait_init(struct optee_call_queue *cq,
382 			struct optee_call_waiter *w, bool sys_thread);
383 void optee_cq_wait_for_completion(struct optee_call_queue *cq,
384 				  struct optee_call_waiter *w);
385 void optee_cq_wait_final(struct optee_call_queue *cq,
386 			 struct optee_call_waiter *w);
387 int optee_check_mem_type(unsigned long start, size_t num_pages);
388 
389 void optee_shm_arg_cache_init(struct optee *optee, u32 flags);
390 void optee_shm_arg_cache_uninit(struct optee *optee);
391 struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,
392 					size_t num_params,
393 					struct optee_shm_arg_entry **entry,
394 					struct tee_shm **shm_ret,
395 					u_int *offs);
396 void optee_free_msg_arg(struct tee_context *ctx,
397 			struct optee_shm_arg_entry *entry, u_int offs);
398 size_t optee_msg_arg_size(size_t rpc_param_count);
399 
400 
401 struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
402 void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
403 void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
404 		   struct optee_msg_arg *arg);
405 
406 int optee_do_bottom_half(struct tee_context *ctx);
407 int optee_stop_async_notif(struct tee_context *ctx);
408 
409 /*
410  * Small helpers
411  */
412 
413 static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
414 {
415 	return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
416 }
417 
418 static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
419 {
420 	*reg0 = val >> 32;
421 	*reg1 = val;
422 }
423 
424 /* Registration of the ABIs */
425 int optee_smc_abi_register(void);
426 void optee_smc_abi_unregister(void);
427 int optee_ffa_abi_register(void);
428 void optee_ffa_abi_unregister(void);
429 
430 #endif /*OPTEE_PRIVATE_H*/
431