1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2015-2016, Linaro Limited
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>
19 * The file describes the API provided by the generic TEE driver to the
20 * specific TEE driver.
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_PRIV BIT(7) /* Memory private to TEE driver */
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.
52 struct tee_device *teedev;
53 struct list_head list_shm;
60 struct tee_param_memref {
66 struct tee_param_value {
75 struct tee_param_memref memref;
76 struct tee_param_value value;
81 * struct tee_driver_ops - driver operations vtable
82 * @get_version: returns version of driver
83 * @open: called when the device file is opened
84 * @release: release this open file
85 * @open_session: open a new session
86 * @close_session: close a session
87 * @invoke_func: invoke a trusted function
88 * @cancel_req: request cancel of an ongoing invoke or open
89 * @supp_revc: called for supplicant to get a command
90 * @supp_send: called for supplicant to send a response
91 * @shm_register: register shared memory buffer in TEE
92 * @shm_unregister: unregister shared memory buffer in TEE
94 struct tee_driver_ops {
95 void (*get_version)(struct tee_device *teedev,
96 struct tee_ioctl_version_data *vers);
97 int (*open)(struct tee_context *ctx);
98 void (*release)(struct tee_context *ctx);
99 int (*open_session)(struct tee_context *ctx,
100 struct tee_ioctl_open_session_arg *arg,
101 struct tee_param *param);
102 int (*close_session)(struct tee_context *ctx, u32 session);
103 int (*invoke_func)(struct tee_context *ctx,
104 struct tee_ioctl_invoke_arg *arg,
105 struct tee_param *param);
106 int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
107 int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
108 struct tee_param *param);
109 int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
110 struct tee_param *param);
111 int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
112 struct page **pages, size_t num_pages,
113 unsigned long start);
114 int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
118 * struct tee_desc - Describes the TEE driver to the subsystem
119 * @name: name of driver
120 * @ops: driver operations vtable
121 * @owner: module providing the driver
122 * @flags: Extra properties of driver, defined by TEE_DESC_* below
124 #define TEE_DESC_PRIVILEGED 0x1
127 const struct tee_driver_ops *ops;
128 struct module *owner;
133 * tee_device_alloc() - Allocate a new struct tee_device instance
134 * @teedesc: Descriptor for this driver
135 * @dev: Parent device for this device
136 * @pool: Shared memory pool, NULL if not used
137 * @driver_data: Private driver data for this device
139 * Allocates a new struct tee_device instance. The device is
140 * removed by tee_device_unregister().
142 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
144 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
146 struct tee_shm_pool *pool,
150 * tee_device_register() - Registers a TEE device
151 * @teedev: Device to register
153 * tee_device_unregister() need to be called to remove the @teedev if
154 * this function fails.
156 * @returns < 0 on failure
158 int tee_device_register(struct tee_device *teedev);
161 * tee_device_unregister() - Removes a TEE device
162 * @teedev: Device to unregister
164 * This function should be called to remove the @teedev even if
165 * tee_device_register() hasn't been called yet. Does nothing if
168 void tee_device_unregister(struct tee_device *teedev);
171 * struct tee_shm - shared memory object
172 * @teedev: device used to allocate the object
173 * @ctx: context using the object, if NULL the context is gone
175 * @paddr: physical address of the shared memory
176 * @kaddr: virtual address of the shared memory
177 * @size: size of shared memory
178 * @offset: offset of buffer in user space
179 * @pages: locked pages from userspace
180 * @num_pages: number of locked pages
181 * @refcount: reference counter
182 * @flags: defined by TEE_SHM_* in tee_drv.h
183 * @id: unique id of a shared memory object on this device
185 * This pool is only supposed to be accessed directly from the TEE
186 * subsystem and from drivers that implements their own shm pool manager.
189 struct tee_device *teedev;
190 struct tee_context *ctx;
191 struct list_head link;
204 * struct tee_shm_pool_mgr - shared memory manager
206 * @private_data: private data for the shared memory manager
208 struct tee_shm_pool_mgr {
209 const struct tee_shm_pool_mgr_ops *ops;
214 * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
215 * @alloc: called when allocating shared memory
216 * @free: called when freeing shared memory
217 * @destroy_poolmgr: called when destroying the pool manager
219 struct tee_shm_pool_mgr_ops {
220 int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
222 void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
223 void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr);
227 * tee_shm_pool_alloc() - Create a shared memory pool from shm managers
228 * @priv_mgr: manager for driver private shared memory allocations
229 * @dmabuf_mgr: manager for dma-buf shared memory allocations
231 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
232 * in @dmabuf, others will use the range provided by @priv.
234 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
236 struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
237 struct tee_shm_pool_mgr *dmabuf_mgr);
240 * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved
242 * @vaddr: Virtual address of start of pool
243 * @paddr: Physical address of start of pool
244 * @size: Size in bytes of the pool
246 * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failure.
248 struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
251 int min_alloc_order);
254 * tee_shm_pool_mgr_destroy() - Free a shared memory manager
256 static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm)
258 poolm->ops->destroy_poolmgr(poolm);
262 * struct tee_shm_pool_mem_info - holds information needed to create a shared
264 * @vaddr: Virtual address of start of pool
265 * @paddr: Physical address of start of pool
266 * @size: Size in bytes of the pool
268 struct tee_shm_pool_mem_info {
275 * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
277 * @priv_info: Information for driver private shared memory pool
278 * @dmabuf_info: Information for dma-buf shared memory pool
280 * Start and end of pools will must be page aligned.
282 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
283 * in @dmabuf, others will use the range provided by @priv.
285 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
287 struct tee_shm_pool *
288 tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
289 struct tee_shm_pool_mem_info *dmabuf_info);
292 * tee_shm_pool_free() - Free a shared memory pool
293 * @pool: The shared memory pool to free
295 * The must be no remaining shared memory allocated from this pool when
296 * this function is called.
298 void tee_shm_pool_free(struct tee_shm_pool *pool);
301 * tee_get_drvdata() - Return driver_data pointer
302 * @returns the driver_data pointer supplied to tee_register().
304 void *tee_get_drvdata(struct tee_device *teedev);
307 * tee_shm_alloc() - Allocate shared memory
308 * @ctx: Context that allocates the shared memory
309 * @size: Requested size of shared memory
310 * @flags: Flags setting properties for the requested shared memory.
312 * Memory allocated as global shared memory is automatically freed when the
313 * TEE file pointer is closed. The @flags field uses the bits defined by
314 * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
315 * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
316 * with a dma-buf handle, else driver private memory.
318 * @returns a pointer to 'struct tee_shm'
320 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
321 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);
324 * tee_shm_priv_alloc() - Allocate shared memory privately
325 * @dev: Device that allocates the shared memory
326 * @size: Requested size of shared memory
328 * Allocates shared memory buffer that is not associated with any client
329 * context. Such buffers are owned by TEE driver and used for internal calls.
331 * @returns a pointer to 'struct tee_shm'
333 struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size);
336 * tee_shm_register() - Register shared memory buffer
337 * @ctx: Context that registers the shared memory
338 * @addr: Address is userspace of the shared buffer
339 * @length: Length of the shared buffer
340 * @flags: Flags setting properties for the requested shared memory.
342 * @returns a pointer to 'struct tee_shm'
344 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
345 size_t length, u32 flags);
348 * tee_shm_is_registered() - Check if shared memory object in registered in TEE
349 * @shm: Shared memory handle
350 * @returns true if object is registered in TEE
352 static inline bool tee_shm_is_registered(struct tee_shm *shm)
354 return shm && (shm->flags & TEE_SHM_REGISTER);
358 * tee_shm_free() - Free shared memory
359 * @shm: Handle to shared memory to free
361 void tee_shm_free(struct tee_shm *shm);
364 * tee_shm_put() - Decrease reference count on a shared memory handle
365 * @shm: Shared memory handle
367 void tee_shm_put(struct tee_shm *shm);
370 * tee_shm_va2pa() - Get physical address of a virtual address
371 * @shm: Shared memory handle
372 * @va: Virtual address to tranlsate
373 * @pa: Returned physical address
374 * @returns 0 on success and < 0 on failure
376 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
379 * tee_shm_pa2va() - Get virtual address of a physical address
380 * @shm: Shared memory handle
381 * @pa: Physical address to tranlsate
382 * @va: Returned virtual address
383 * @returns 0 on success and < 0 on failure
385 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
388 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
389 * @shm: Shared memory handle
390 * @offs: Offset from start of this shared memory
391 * @returns virtual address of the shared memory + offs if offs is within
392 * the bounds of this shared memory, else an ERR_PTR
394 void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
397 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
398 * @shm: Shared memory handle
399 * @offs: Offset from start of this shared memory
400 * @pa: Physical address to return
401 * @returns 0 if offs is within the bounds of this shared memory, else an
404 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
407 * tee_shm_get_size() - Get size of shared memory buffer
408 * @shm: Shared memory handle
409 * @returns size of shared memory
411 static inline size_t tee_shm_get_size(struct tee_shm *shm)
417 * tee_shm_get_pages() - Get list of pages that hold shared buffer
418 * @shm: Shared memory handle
419 * @num_pages: Number of pages will be stored there
420 * @returns pointer to pages array
422 static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
425 *num_pages = shm->num_pages;
430 * tee_shm_get_page_offset() - Get shared buffer offset from page start
431 * @shm: Shared memory handle
432 * @returns page offset of shared buffer
434 static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
440 * tee_shm_get_id() - Get id of a shared memory object
441 * @shm: Shared memory handle
444 static inline int tee_shm_get_id(struct tee_shm *shm)
450 * tee_shm_get_from_id() - Find shared memory object and increase reference
452 * @ctx: Context owning the shared memory
453 * @id: Id of shared memory object
454 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
456 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
459 * tee_client_open_context() - Open a TEE context
460 * @start: if not NULL, continue search after this context
461 * @match: function to check TEE device
462 * @data: data for match function
463 * @vers: if not NULL, version data of TEE device of the context returned
465 * This function does an operation similar to open("/dev/teeX") in user space.
466 * A returned context must be released with tee_client_close_context().
468 * Returns a TEE context of the first TEE device matched by the match()
469 * callback or an ERR_PTR.
472 tee_client_open_context(struct tee_context *start,
473 int (*match)(struct tee_ioctl_version_data *,
475 const void *data, struct tee_ioctl_version_data *vers);
478 * tee_client_close_context() - Close a TEE context
479 * @ctx: TEE context to close
481 * Note that all sessions previously opened with this context will be
482 * closed when this function is called.
484 void tee_client_close_context(struct tee_context *ctx);
487 * tee_client_get_version() - Query version of TEE
488 * @ctx: TEE context to TEE to query
489 * @vers: Pointer to version data
491 void tee_client_get_version(struct tee_context *ctx,
492 struct tee_ioctl_version_data *vers);
495 * tee_client_open_session() - Open a session to a Trusted Application
497 * @arg: Open session arguments, see description of
498 * struct tee_ioctl_open_session_arg
499 * @param: Parameters passed to the Trusted Application
501 * Returns < 0 on error else see @arg->ret for result. If @arg->ret
502 * is TEEC_SUCCESS the session identifier is available in @arg->session.
504 int tee_client_open_session(struct tee_context *ctx,
505 struct tee_ioctl_open_session_arg *arg,
506 struct tee_param *param);
509 * tee_client_close_session() - Close a session to a Trusted Application
511 * @session: Session id
513 * Return < 0 on error else 0, regardless the session will not be
514 * valid after this function has returned.
516 int tee_client_close_session(struct tee_context *ctx, u32 session);
519 * tee_client_invoke_func() - Invoke a function in a Trusted Application
521 * @arg: Invoke arguments, see description of
522 * struct tee_ioctl_invoke_arg
523 * @param: Parameters passed to the Trusted Application
525 * Returns < 0 on error else see @arg->ret for result.
527 int tee_client_invoke_func(struct tee_context *ctx,
528 struct tee_ioctl_invoke_arg *arg,
529 struct tee_param *param);
532 * tee_client_cancel_req() - Request cancellation of the previous open-session
533 * or invoke-command operations in a Trusted Application
535 * @arg: Cancellation arguments, see description of
536 * struct tee_ioctl_cancel_arg
538 * Returns < 0 on error else 0 if the cancellation was successfully requested.
540 int tee_client_cancel_req(struct tee_context *ctx,
541 struct tee_ioctl_cancel_arg *arg);
543 static inline bool tee_param_is_memref(struct tee_param *param)
545 switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
546 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
547 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
548 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
555 extern struct bus_type tee_bus_type;
558 * struct tee_client_device - tee based device
559 * @id: device identifier
560 * @dev: device structure
562 struct tee_client_device {
563 struct tee_client_device_id id;
567 #define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
570 * struct tee_client_driver - tee client driver
571 * @id_table: device id table supported by this driver
572 * @driver: driver structure
574 struct tee_client_driver {
575 const struct tee_client_device_id *id_table;
576 struct device_driver driver;
579 #define to_tee_client_driver(d) \
580 container_of(d, struct tee_client_driver, driver)
583 * teedev_open() - Open a struct tee_device
584 * @teedev: Device to open
586 * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
588 struct tee_context *teedev_open(struct tee_device *teedev);
591 * teedev_close_context() - closes a struct tee_context
592 * @ctx: The struct tee_context to close
594 void teedev_close_context(struct tee_context *ctx);
596 #endif /*__TEE_DRV_H*/