2 * Copyright (c) 2015-2016, Linaro Limited
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 #include <linux/device.h>
19 #include <linux/idr.h>
20 #include <linux/kref.h>
21 #include <linux/list.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/tee.h>
24 #include <linux/types.h>
25 #include <linux/uuid.h>
28 * The file describes the API provided by the generic TEE driver to the
29 * specific TEE driver.
32 #define TEE_SHM_MAPPED BIT(0) /* Memory mapped by the kernel */
33 #define TEE_SHM_DMA_BUF BIT(1) /* Memory with dma-buf handle */
34 #define TEE_SHM_EXT_DMA_BUF BIT(2) /* Memory with dma-buf handle */
35 #define TEE_SHM_REGISTER BIT(3) /* Memory registered in secure world */
36 #define TEE_SHM_USER_MAPPED BIT(4) /* Memory mapped in user space */
37 #define TEE_SHM_POOL BIT(5) /* Memory allocated from pool */
45 * struct tee_context - driver specific context on file pointer data
46 * @teedev: pointer to this drivers struct tee_device
47 * @list_shm: List of shared memory object owned by this context
48 * @data: driver specific context data, managed by the driver
49 * @refcount: reference counter for this structure
50 * @releasing: flag that indicates if context is being released right now.
51 * It is needed to break circular dependency on context during
52 * shared memory release.
53 * @supp_nowait: flag that indicates that requests in this context should not
54 * wait for tee-supplicant daemon to be started if not present
55 * and just return with an error code. It is needed for requests
56 * that arises from TEE based kernel drivers that should be
57 * non-blocking in nature.
60 struct tee_device *teedev;
61 struct list_head list_shm;
68 struct tee_param_memref {
74 struct tee_param_value {
83 struct tee_param_memref memref;
84 struct tee_param_value value;
89 * struct tee_driver_ops - driver operations vtable
90 * @get_version: returns version of driver
91 * @open: called when the device file is opened
92 * @release: release this open file
93 * @open_session: open a new session
94 * @close_session: close a session
95 * @invoke_func: invoke a trusted function
96 * @cancel_req: request cancel of an ongoing invoke or open
97 * @supp_revc: called for supplicant to get a command
98 * @supp_send: called for supplicant to send a response
99 * @shm_register: register shared memory buffer in TEE
100 * @shm_unregister: unregister shared memory buffer in TEE
102 struct tee_driver_ops {
103 void (*get_version)(struct tee_device *teedev,
104 struct tee_ioctl_version_data *vers);
105 int (*open)(struct tee_context *ctx);
106 void (*release)(struct tee_context *ctx);
107 int (*open_session)(struct tee_context *ctx,
108 struct tee_ioctl_open_session_arg *arg,
109 struct tee_param *param);
110 int (*close_session)(struct tee_context *ctx, u32 session);
111 int (*invoke_func)(struct tee_context *ctx,
112 struct tee_ioctl_invoke_arg *arg,
113 struct tee_param *param);
114 int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
115 int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
116 struct tee_param *param);
117 int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
118 struct tee_param *param);
119 int (*shm_register)(struct tee_context *ctx, struct tee_shm *shm,
120 struct page **pages, size_t num_pages,
121 unsigned long start);
122 int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
126 * struct tee_desc - Describes the TEE driver to the subsystem
127 * @name: name of driver
128 * @ops: driver operations vtable
129 * @owner: module providing the driver
130 * @flags: Extra properties of driver, defined by TEE_DESC_* below
132 #define TEE_DESC_PRIVILEGED 0x1
135 const struct tee_driver_ops *ops;
136 struct module *owner;
141 * tee_device_alloc() - Allocate a new struct tee_device instance
142 * @teedesc: Descriptor for this driver
143 * @dev: Parent device for this device
144 * @pool: Shared memory pool, NULL if not used
145 * @driver_data: Private driver data for this device
147 * Allocates a new struct tee_device instance. The device is
148 * removed by tee_device_unregister().
150 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
152 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
154 struct tee_shm_pool *pool,
158 * tee_device_register() - Registers a TEE device
159 * @teedev: Device to register
161 * tee_device_unregister() need to be called to remove the @teedev if
162 * this function fails.
164 * @returns < 0 on failure
166 int tee_device_register(struct tee_device *teedev);
169 * tee_device_unregister() - Removes a TEE device
170 * @teedev: Device to unregister
172 * This function should be called to remove the @teedev even if
173 * tee_device_register() hasn't been called yet. Does nothing if
176 void tee_device_unregister(struct tee_device *teedev);
179 * struct tee_shm - shared memory object
180 * @teedev: device used to allocate the object
181 * @ctx: context using the object, if NULL the context is gone
183 * @paddr: physical address of the shared memory
184 * @kaddr: virtual address of the shared memory
185 * @size: size of shared memory
186 * @offset: offset of buffer in user space
187 * @pages: locked pages from userspace
188 * @num_pages: number of locked pages
189 * @dmabuf: dmabuf used to for exporting to user space
190 * @flags: defined by TEE_SHM_* in tee_drv.h
191 * @id: unique id of a shared memory object on this device
193 * This pool is only supposed to be accessed directly from the TEE
194 * subsystem and from drivers that implements their own shm pool manager.
197 struct tee_device *teedev;
198 struct tee_context *ctx;
199 struct list_head link;
206 struct dma_buf *dmabuf;
212 * struct tee_shm_pool_mgr - shared memory manager
214 * @private_data: private data for the shared memory manager
216 struct tee_shm_pool_mgr {
217 const struct tee_shm_pool_mgr_ops *ops;
222 * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
223 * @alloc: called when allocating shared memory
224 * @free: called when freeing shared memory
225 * @destroy_poolmgr: called when destroying the pool manager
227 struct tee_shm_pool_mgr_ops {
228 int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
230 void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
231 void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr);
235 * tee_shm_pool_alloc() - Create a shared memory pool from shm managers
236 * @priv_mgr: manager for driver private shared memory allocations
237 * @dmabuf_mgr: manager for dma-buf shared memory allocations
239 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
240 * in @dmabuf, others will use the range provided by @priv.
242 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
244 struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
245 struct tee_shm_pool_mgr *dmabuf_mgr);
248 * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved
250 * @vaddr: Virtual address of start of pool
251 * @paddr: Physical address of start of pool
252 * @size: Size in bytes of the pool
254 * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failure.
256 struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
259 int min_alloc_order);
262 * tee_shm_pool_mgr_destroy() - Free a shared memory manager
264 static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm)
266 poolm->ops->destroy_poolmgr(poolm);
270 * struct tee_shm_pool_mem_info - holds information needed to create a shared
272 * @vaddr: Virtual address of start of pool
273 * @paddr: Physical address of start of pool
274 * @size: Size in bytes of the pool
276 struct tee_shm_pool_mem_info {
283 * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
285 * @priv_info: Information for driver private shared memory pool
286 * @dmabuf_info: Information for dma-buf shared memory pool
288 * Start and end of pools will must be page aligned.
290 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
291 * in @dmabuf, others will use the range provided by @priv.
293 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
295 struct tee_shm_pool *
296 tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
297 struct tee_shm_pool_mem_info *dmabuf_info);
300 * tee_shm_pool_free() - Free a shared memory pool
301 * @pool: The shared memory pool to free
303 * The must be no remaining shared memory allocated from this pool when
304 * this function is called.
306 void tee_shm_pool_free(struct tee_shm_pool *pool);
309 * tee_get_drvdata() - Return driver_data pointer
310 * @returns the driver_data pointer supplied to tee_register().
312 void *tee_get_drvdata(struct tee_device *teedev);
315 * tee_shm_alloc() - Allocate shared memory
316 * @ctx: Context that allocates the shared memory
317 * @size: Requested size of shared memory
318 * @flags: Flags setting properties for the requested shared memory.
320 * Memory allocated as global shared memory is automatically freed when the
321 * TEE file pointer is closed. The @flags field uses the bits defined by
322 * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
323 * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
324 * with a dma-buf handle, else driver private memory.
326 * @returns a pointer to 'struct tee_shm'
328 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
331 * tee_shm_priv_alloc() - Allocate shared memory privately
332 * @dev: Device that allocates the shared memory
333 * @size: Requested size of shared memory
335 * Allocates shared memory buffer that is not associated with any client
336 * context. Such buffers are owned by TEE driver and used for internal calls.
338 * @returns a pointer to 'struct tee_shm'
340 struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size);
343 * tee_shm_register() - Register shared memory buffer
344 * @ctx: Context that registers the shared memory
345 * @addr: Address is userspace of the shared buffer
346 * @length: Length of the shared buffer
347 * @flags: Flags setting properties for the requested shared memory.
349 * @returns a pointer to 'struct tee_shm'
351 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
352 size_t length, u32 flags);
355 * tee_shm_is_registered() - Check if shared memory object in registered in TEE
356 * @shm: Shared memory handle
357 * @returns true if object is registered in TEE
359 static inline bool tee_shm_is_registered(struct tee_shm *shm)
361 return shm && (shm->flags & TEE_SHM_REGISTER);
365 * tee_shm_free() - Free shared memory
366 * @shm: Handle to shared memory to free
368 void tee_shm_free(struct tee_shm *shm);
371 * tee_shm_put() - Decrease reference count on a shared memory handle
372 * @shm: Shared memory handle
374 void tee_shm_put(struct tee_shm *shm);
377 * tee_shm_va2pa() - Get physical address of a virtual address
378 * @shm: Shared memory handle
379 * @va: Virtual address to tranlsate
380 * @pa: Returned physical address
381 * @returns 0 on success and < 0 on failure
383 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
386 * tee_shm_pa2va() - Get virtual address of a physical address
387 * @shm: Shared memory handle
388 * @pa: Physical address to tranlsate
389 * @va: Returned virtual address
390 * @returns 0 on success and < 0 on failure
392 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
395 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
396 * @shm: Shared memory handle
397 * @offs: Offset from start of this shared memory
398 * @returns virtual address of the shared memory + offs if offs is within
399 * the bounds of this shared memory, else an ERR_PTR
401 void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
404 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
405 * @shm: Shared memory handle
406 * @offs: Offset from start of this shared memory
407 * @pa: Physical address to return
408 * @returns 0 if offs is within the bounds of this shared memory, else an
411 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
414 * tee_shm_get_size() - Get size of shared memory buffer
415 * @shm: Shared memory handle
416 * @returns size of shared memory
418 static inline size_t tee_shm_get_size(struct tee_shm *shm)
424 * tee_shm_get_pages() - Get list of pages that hold shared buffer
425 * @shm: Shared memory handle
426 * @num_pages: Number of pages will be stored there
427 * @returns pointer to pages array
429 static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
432 *num_pages = shm->num_pages;
437 * tee_shm_get_page_offset() - Get shared buffer offset from page start
438 * @shm: Shared memory handle
439 * @returns page offset of shared buffer
441 static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
447 * tee_shm_get_id() - Get id of a shared memory object
448 * @shm: Shared memory handle
451 static inline int tee_shm_get_id(struct tee_shm *shm)
457 * tee_shm_get_from_id() - Find shared memory object and increase reference
459 * @ctx: Context owning the shared memory
460 * @id: Id of shared memory object
461 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
463 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
466 * tee_client_open_context() - Open a TEE context
467 * @start: if not NULL, continue search after this context
468 * @match: function to check TEE device
469 * @data: data for match function
470 * @vers: if not NULL, version data of TEE device of the context returned
472 * This function does an operation similar to open("/dev/teeX") in user space.
473 * A returned context must be released with tee_client_close_context().
475 * Returns a TEE context of the first TEE device matched by the match()
476 * callback or an ERR_PTR.
479 tee_client_open_context(struct tee_context *start,
480 int (*match)(struct tee_ioctl_version_data *,
482 const void *data, struct tee_ioctl_version_data *vers);
485 * tee_client_close_context() - Close a TEE context
486 * @ctx: TEE context to close
488 * Note that all sessions previously opened with this context will be
489 * closed when this function is called.
491 void tee_client_close_context(struct tee_context *ctx);
494 * tee_client_get_version() - Query version of TEE
495 * @ctx: TEE context to TEE to query
496 * @vers: Pointer to version data
498 void tee_client_get_version(struct tee_context *ctx,
499 struct tee_ioctl_version_data *vers);
502 * tee_client_open_session() - Open a session to a Trusted Application
504 * @arg: Open session arguments, see description of
505 * struct tee_ioctl_open_session_arg
506 * @param: Parameters passed to the Trusted Application
508 * Returns < 0 on error else see @arg->ret for result. If @arg->ret
509 * is TEEC_SUCCESS the session identifier is available in @arg->session.
511 int tee_client_open_session(struct tee_context *ctx,
512 struct tee_ioctl_open_session_arg *arg,
513 struct tee_param *param);
516 * tee_client_close_session() - Close a session to a Trusted Application
518 * @session: Session id
520 * Return < 0 on error else 0, regardless the session will not be
521 * valid after this function has returned.
523 int tee_client_close_session(struct tee_context *ctx, u32 session);
526 * tee_client_invoke_func() - Invoke a function in a Trusted Application
528 * @arg: Invoke arguments, see description of
529 * struct tee_ioctl_invoke_arg
530 * @param: Parameters passed to the Trusted Application
532 * Returns < 0 on error else see @arg->ret for result.
534 int tee_client_invoke_func(struct tee_context *ctx,
535 struct tee_ioctl_invoke_arg *arg,
536 struct tee_param *param);
539 * tee_client_cancel_req() - Request cancellation of the previous open-session
540 * or invoke-command operations in a Trusted Application
542 * @arg: Cancellation arguments, see description of
543 * struct tee_ioctl_cancel_arg
545 * Returns < 0 on error else 0 if the cancellation was successfully requested.
547 int tee_client_cancel_req(struct tee_context *ctx,
548 struct tee_ioctl_cancel_arg *arg);
550 static inline bool tee_param_is_memref(struct tee_param *param)
552 switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
553 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
554 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
555 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
562 extern struct bus_type tee_bus_type;
565 * struct tee_client_device - tee based device
566 * @id: device identifier
567 * @dev: device structure
569 struct tee_client_device {
570 struct tee_client_device_id id;
574 #define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
577 * struct tee_client_driver - tee client driver
578 * @id_table: device id table supported by this driver
579 * @driver: driver structure
581 struct tee_client_driver {
582 const struct tee_client_device_id *id_table;
583 struct device_driver driver;
586 #define to_tee_client_driver(d) \
587 container_of(d, struct tee_client_driver, driver)
589 #endif /*__TEE_DRV_H*/