xref: /linux-6.15/include/linux/tee_drv.h (revision e33bcbab)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2015-2016, Linaro Limited
4  */
5 
6 #ifndef __TEE_DRV_H
7 #define __TEE_DRV_H
8 
9 #include <linux/device.h>
10 #include <linux/idr.h>
11 #include <linux/kref.h>
12 #include <linux/list.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/tee.h>
15 #include <linux/types.h>
16 #include <linux/uuid.h>
17 
18 /*
19  * The file describes the API provided by the generic TEE driver to the
20  * specific TEE driver.
21  */
22 
23 #define TEE_SHM_MAPPED		BIT(0)	/* Memory mapped by the kernel */
24 #define TEE_SHM_DMA_BUF		BIT(1)	/* Memory with dma-buf handle */
25 #define TEE_SHM_EXT_DMA_BUF	BIT(2)	/* Memory with dma-buf handle */
26 #define TEE_SHM_REGISTER	BIT(3)  /* Memory registered in secure world */
27 #define TEE_SHM_USER_MAPPED	BIT(4)  /* Memory mapped in user space */
28 #define TEE_SHM_POOL		BIT(5)  /* Memory allocated from pool */
29 
30 struct device;
31 struct tee_device;
32 struct tee_shm;
33 struct tee_shm_pool;
34 
35 /**
36  * struct tee_context - driver specific context on file pointer data
37  * @teedev:	pointer to this drivers struct tee_device
38  * @list_shm:	List of shared memory object owned by this context
39  * @data:	driver specific context data, managed by the driver
40  * @refcount:	reference counter for this structure
41  * @releasing:  flag that indicates if context is being released right now.
42  *		It is needed to break circular dependency on context during
43  *              shared memory release.
44  * @supp_nowait: flag that indicates that requests in this context should not
45  *              wait for tee-supplicant daemon to be started if not present
46  *              and just return with an error code. It is needed for requests
47  *              that arises from TEE based kernel drivers that should be
48  *              non-blocking in nature.
49  */
50 struct tee_context {
51 	struct tee_device *teedev;
52 	void *data;
53 	struct kref refcount;
54 	bool releasing;
55 	bool supp_nowait;
56 };
57 
58 struct tee_param_memref {
59 	size_t shm_offs;
60 	size_t size;
61 	struct tee_shm *shm;
62 };
63 
64 struct tee_param_value {
65 	u64 a;
66 	u64 b;
67 	u64 c;
68 };
69 
70 struct tee_param {
71 	u64 attr;
72 	union {
73 		struct tee_param_memref memref;
74 		struct tee_param_value value;
75 	} u;
76 };
77 
78 /**
79  * struct tee_driver_ops - driver operations vtable
80  * @get_version:	returns version of driver
81  * @open:		called when the device file is opened
82  * @release:		release this open file
83  * @open_session:	open a new session
84  * @close_session:	close a session
85  * @invoke_func:	invoke a trusted function
86  * @cancel_req:		request cancel of an ongoing invoke or open
87  * @supp_revc:		called for supplicant to get a command
88  * @supp_send:		called for supplicant to send a response
89  * @shm_register:	register shared memory buffer in TEE
90  * @shm_unregister:	unregister shared memory buffer in TEE
91  */
92 struct tee_driver_ops {
93 	void (*get_version)(struct tee_device *teedev,
94 			    struct tee_ioctl_version_data *vers);
95 	int (*open)(struct tee_context *ctx);
96 	void (*release)(struct tee_context *ctx);
97 	int (*open_session)(struct tee_context *ctx,
98 			    struct tee_ioctl_open_session_arg *arg,
99 			    struct tee_param *param);
100 	int (*close_session)(struct tee_context *ctx, u32 session);
101 	int (*invoke_func)(struct tee_context *ctx,
102 			   struct tee_ioctl_invoke_arg *arg,
103 			   struct tee_param *param);
104 	int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
105 	int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
106 			 struct tee_param *param);
107 	int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
108 			 struct tee_param *param);
109 	int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
110 			    struct page **pages, size_t num_pages,
111 			    unsigned long start);
112 	int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
113 };
114 
115 /**
116  * struct tee_desc - Describes the TEE driver to the subsystem
117  * @name:	name of driver
118  * @ops:	driver operations vtable
119  * @owner:	module providing the driver
120  * @flags:	Extra properties of driver, defined by TEE_DESC_* below
121  */
122 #define TEE_DESC_PRIVILEGED	0x1
123 struct tee_desc {
124 	const char *name;
125 	const struct tee_driver_ops *ops;
126 	struct module *owner;
127 	u32 flags;
128 };
129 
130 /**
131  * tee_device_alloc() - Allocate a new struct tee_device instance
132  * @teedesc:	Descriptor for this driver
133  * @dev:	Parent device for this device
134  * @pool:	Shared memory pool, NULL if not used
135  * @driver_data: Private driver data for this device
136  *
137  * Allocates a new struct tee_device instance. The device is
138  * removed by tee_device_unregister().
139  *
140  * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
141  */
142 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
143 				    struct device *dev,
144 				    struct tee_shm_pool *pool,
145 				    void *driver_data);
146 
147 /**
148  * tee_device_register() - Registers a TEE device
149  * @teedev:	Device to register
150  *
151  * tee_device_unregister() need to be called to remove the @teedev if
152  * this function fails.
153  *
154  * @returns < 0 on failure
155  */
156 int tee_device_register(struct tee_device *teedev);
157 
158 /**
159  * tee_device_unregister() - Removes a TEE device
160  * @teedev:	Device to unregister
161  *
162  * This function should be called to remove the @teedev even if
163  * tee_device_register() hasn't been called yet. Does nothing if
164  * @teedev is NULL.
165  */
166 void tee_device_unregister(struct tee_device *teedev);
167 
168 /**
169  * tee_session_calc_client_uuid() - Calculates client UUID for session
170  * @uuid:		Resulting UUID
171  * @connection_method:	Connection method for session (TEE_IOCTL_LOGIN_*)
172  * @connectuon_data:	Connection data for opening session
173  *
174  * Based on connection method calculates UUIDv5 based client UUID.
175  *
176  * For group based logins verifies that calling process has specified
177  * credentials.
178  *
179  * @return < 0 on failure
180  */
181 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
182 				 const u8 connection_data[TEE_IOCTL_UUID_LEN]);
183 
184 /**
185  * struct tee_shm - shared memory object
186  * @ctx:	context using the object
187  * @paddr:	physical address of the shared memory
188  * @kaddr:	virtual address of the shared memory
189  * @size:	size of shared memory
190  * @offset:	offset of buffer in user space
191  * @pages:	locked pages from userspace
192  * @num_pages:	number of locked pages
193  * @dmabuf:	dmabuf used to for exporting to user space
194  * @flags:	defined by TEE_SHM_* in tee_drv.h
195  * @id:		unique id of a shared memory object on this device
196  *
197  * This pool is only supposed to be accessed directly from the TEE
198  * subsystem and from drivers that implements their own shm pool manager.
199  */
200 struct tee_shm {
201 	struct tee_context *ctx;
202 	phys_addr_t paddr;
203 	void *kaddr;
204 	size_t size;
205 	unsigned int offset;
206 	struct page **pages;
207 	size_t num_pages;
208 	struct dma_buf *dmabuf;
209 	u32 flags;
210 	int id;
211 };
212 
213 /**
214  * struct tee_shm_pool_mgr - shared memory manager
215  * @ops:		operations
216  * @private_data:	private data for the shared memory manager
217  */
218 struct tee_shm_pool_mgr {
219 	const struct tee_shm_pool_mgr_ops *ops;
220 	void *private_data;
221 };
222 
223 /**
224  * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
225  * @alloc:		called when allocating shared memory
226  * @free:		called when freeing shared memory
227  * @destroy_poolmgr:	called when destroying the pool manager
228  */
229 struct tee_shm_pool_mgr_ops {
230 	int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
231 		     size_t size);
232 	void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
233 	void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr);
234 };
235 
236 /**
237  * tee_shm_pool_alloc() - Create a shared memory pool from shm managers
238  * @priv_mgr:	manager for driver private shared memory allocations
239  * @dmabuf_mgr:	manager for dma-buf shared memory allocations
240  *
241  * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
242  * in @dmabuf, others will use the range provided by @priv.
243  *
244  * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
245  */
246 struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
247 					struct tee_shm_pool_mgr *dmabuf_mgr);
248 
249 /*
250  * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved
251  * memory
252  * @vaddr:	Virtual address of start of pool
253  * @paddr:	Physical address of start of pool
254  * @size:	Size in bytes of the pool
255  *
256  * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failure.
257  */
258 struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
259 							phys_addr_t paddr,
260 							size_t size,
261 							int min_alloc_order);
262 
263 /**
264  * tee_shm_pool_mgr_destroy() - Free a shared memory manager
265  */
266 static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm)
267 {
268 	poolm->ops->destroy_poolmgr(poolm);
269 }
270 
271 /**
272  * struct tee_shm_pool_mem_info - holds information needed to create a shared
273  * memory pool
274  * @vaddr:	Virtual address of start of pool
275  * @paddr:	Physical address of start of pool
276  * @size:	Size in bytes of the pool
277  */
278 struct tee_shm_pool_mem_info {
279 	unsigned long vaddr;
280 	phys_addr_t paddr;
281 	size_t size;
282 };
283 
284 /**
285  * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
286  * memory range
287  * @priv_info:	 Information for driver private shared memory pool
288  * @dmabuf_info: Information for dma-buf shared memory pool
289  *
290  * Start and end of pools will must be page aligned.
291  *
292  * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
293  * in @dmabuf, others will use the range provided by @priv.
294  *
295  * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
296  */
297 struct tee_shm_pool *
298 tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
299 			   struct tee_shm_pool_mem_info *dmabuf_info);
300 
301 /**
302  * tee_shm_pool_free() - Free a shared memory pool
303  * @pool:	The shared memory pool to free
304  *
305  * The must be no remaining shared memory allocated from this pool when
306  * this function is called.
307  */
308 void tee_shm_pool_free(struct tee_shm_pool *pool);
309 
310 /**
311  * tee_get_drvdata() - Return driver_data pointer
312  * @returns the driver_data pointer supplied to tee_register().
313  */
314 void *tee_get_drvdata(struct tee_device *teedev);
315 
316 /**
317  * tee_shm_alloc() - Allocate shared memory
318  * @ctx:	Context that allocates the shared memory
319  * @size:	Requested size of shared memory
320  * @flags:	Flags setting properties for the requested shared memory.
321  *
322  * Memory allocated as global shared memory is automatically freed when the
323  * TEE file pointer is closed. The @flags field uses the bits defined by
324  * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
325  * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
326  * with a dma-buf handle, else driver private memory.
327  *
328  * @returns a pointer to 'struct tee_shm'
329  */
330 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
331 
332 /**
333  * tee_shm_register() - Register shared memory buffer
334  * @ctx:	Context that registers the shared memory
335  * @addr:	Address is userspace of the shared buffer
336  * @length:	Length of the shared buffer
337  * @flags:	Flags setting properties for the requested shared memory.
338  *
339  * @returns a pointer to 'struct tee_shm'
340  */
341 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
342 				 size_t length, u32 flags);
343 
344 /**
345  * tee_shm_is_registered() - Check if shared memory object in registered in TEE
346  * @shm:	Shared memory handle
347  * @returns true if object is registered in TEE
348  */
349 static inline bool tee_shm_is_registered(struct tee_shm *shm)
350 {
351 	return shm && (shm->flags & TEE_SHM_REGISTER);
352 }
353 
354 /**
355  * tee_shm_free() - Free shared memory
356  * @shm:	Handle to shared memory to free
357  */
358 void tee_shm_free(struct tee_shm *shm);
359 
360 /**
361  * tee_shm_put() - Decrease reference count on a shared memory handle
362  * @shm:	Shared memory handle
363  */
364 void tee_shm_put(struct tee_shm *shm);
365 
366 /**
367  * tee_shm_va2pa() - Get physical address of a virtual address
368  * @shm:	Shared memory handle
369  * @va:		Virtual address to tranlsate
370  * @pa:		Returned physical address
371  * @returns 0 on success and < 0 on failure
372  */
373 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
374 
375 /**
376  * tee_shm_pa2va() - Get virtual address of a physical address
377  * @shm:	Shared memory handle
378  * @pa:		Physical address to tranlsate
379  * @va:		Returned virtual address
380  * @returns 0 on success and < 0 on failure
381  */
382 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
383 
384 /**
385  * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
386  * @shm:	Shared memory handle
387  * @offs:	Offset from start of this shared memory
388  * @returns virtual address of the shared memory + offs if offs is within
389  *	the bounds of this shared memory, else an ERR_PTR
390  */
391 void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
392 
393 /**
394  * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
395  * @shm:	Shared memory handle
396  * @offs:	Offset from start of this shared memory
397  * @pa:		Physical address to return
398  * @returns 0 if offs is within the bounds of this shared memory, else an
399  *	error code.
400  */
401 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
402 
403 /**
404  * tee_shm_get_size() - Get size of shared memory buffer
405  * @shm:	Shared memory handle
406  * @returns size of shared memory
407  */
408 static inline size_t tee_shm_get_size(struct tee_shm *shm)
409 {
410 	return shm->size;
411 }
412 
413 /**
414  * tee_shm_get_pages() - Get list of pages that hold shared buffer
415  * @shm:	Shared memory handle
416  * @num_pages:	Number of pages will be stored there
417  * @returns pointer to pages array
418  */
419 static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
420 					      size_t *num_pages)
421 {
422 	*num_pages = shm->num_pages;
423 	return shm->pages;
424 }
425 
426 /**
427  * tee_shm_get_page_offset() - Get shared buffer offset from page start
428  * @shm:	Shared memory handle
429  * @returns page offset of shared buffer
430  */
431 static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
432 {
433 	return shm->offset;
434 }
435 
436 /**
437  * tee_shm_get_id() - Get id of a shared memory object
438  * @shm:	Shared memory handle
439  * @returns id
440  */
441 static inline int tee_shm_get_id(struct tee_shm *shm)
442 {
443 	return shm->id;
444 }
445 
446 /**
447  * tee_shm_get_from_id() - Find shared memory object and increase reference
448  * count
449  * @ctx:	Context owning the shared memory
450  * @id:		Id of shared memory object
451  * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
452  */
453 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
454 
455 /**
456  * tee_client_open_context() - Open a TEE context
457  * @start:	if not NULL, continue search after this context
458  * @match:	function to check TEE device
459  * @data:	data for match function
460  * @vers:	if not NULL, version data of TEE device of the context returned
461  *
462  * This function does an operation similar to open("/dev/teeX") in user space.
463  * A returned context must be released with tee_client_close_context().
464  *
465  * Returns a TEE context of the first TEE device matched by the match()
466  * callback or an ERR_PTR.
467  */
468 struct tee_context *
469 tee_client_open_context(struct tee_context *start,
470 			int (*match)(struct tee_ioctl_version_data *,
471 				     const void *),
472 			const void *data, struct tee_ioctl_version_data *vers);
473 
474 /**
475  * tee_client_close_context() - Close a TEE context
476  * @ctx:	TEE context to close
477  *
478  * Note that all sessions previously opened with this context will be
479  * closed when this function is called.
480  */
481 void tee_client_close_context(struct tee_context *ctx);
482 
483 /**
484  * tee_client_get_version() - Query version of TEE
485  * @ctx:	TEE context to TEE to query
486  * @vers:	Pointer to version data
487  */
488 void tee_client_get_version(struct tee_context *ctx,
489 			    struct tee_ioctl_version_data *vers);
490 
491 /**
492  * tee_client_open_session() - Open a session to a Trusted Application
493  * @ctx:	TEE context
494  * @arg:	Open session arguments, see description of
495  *		struct tee_ioctl_open_session_arg
496  * @param:	Parameters passed to the Trusted Application
497  *
498  * Returns < 0 on error else see @arg->ret for result. If @arg->ret
499  * is TEEC_SUCCESS the session identifier is available in @arg->session.
500  */
501 int tee_client_open_session(struct tee_context *ctx,
502 			    struct tee_ioctl_open_session_arg *arg,
503 			    struct tee_param *param);
504 
505 /**
506  * tee_client_close_session() - Close a session to a Trusted Application
507  * @ctx:	TEE Context
508  * @session:	Session id
509  *
510  * Return < 0 on error else 0, regardless the session will not be
511  * valid after this function has returned.
512  */
513 int tee_client_close_session(struct tee_context *ctx, u32 session);
514 
515 /**
516  * tee_client_invoke_func() - Invoke a function in a Trusted Application
517  * @ctx:	TEE Context
518  * @arg:	Invoke arguments, see description of
519  *		struct tee_ioctl_invoke_arg
520  * @param:	Parameters passed to the Trusted Application
521  *
522  * Returns < 0 on error else see @arg->ret for result.
523  */
524 int tee_client_invoke_func(struct tee_context *ctx,
525 			   struct tee_ioctl_invoke_arg *arg,
526 			   struct tee_param *param);
527 
528 /**
529  * tee_client_cancel_req() - Request cancellation of the previous open-session
530  * or invoke-command operations in a Trusted Application
531  * @ctx:       TEE Context
532  * @arg:       Cancellation arguments, see description of
533  *             struct tee_ioctl_cancel_arg
534  *
535  * Returns < 0 on error else 0 if the cancellation was successfully requested.
536  */
537 int tee_client_cancel_req(struct tee_context *ctx,
538 			  struct tee_ioctl_cancel_arg *arg);
539 
540 static inline bool tee_param_is_memref(struct tee_param *param)
541 {
542 	switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
543 	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
544 	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
545 	case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
546 		return true;
547 	default:
548 		return false;
549 	}
550 }
551 
552 extern struct bus_type tee_bus_type;
553 
554 /**
555  * struct tee_client_device - tee based device
556  * @id:			device identifier
557  * @dev:		device structure
558  */
559 struct tee_client_device {
560 	struct tee_client_device_id id;
561 	struct device dev;
562 };
563 
564 #define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
565 
566 /**
567  * struct tee_client_driver - tee client driver
568  * @id_table:		device id table supported by this driver
569  * @driver:		driver structure
570  */
571 struct tee_client_driver {
572 	const struct tee_client_device_id *id_table;
573 	struct device_driver driver;
574 };
575 
576 #define to_tee_client_driver(d) \
577 		container_of(d, struct tee_client_driver, driver)
578 
579 #endif /*__TEE_DRV_H*/
580