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