1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2015-2022 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_DYNAMIC BIT(0) /* Dynamic shared memory registered */
25 #define TEE_SHM_USER_MAPPED BIT(1) /* Memory mapped in user space */
26 #define TEE_SHM_POOL BIT(2) /* Memory allocated from pool */
27 #define TEE_SHM_PRIV BIT(3) /* Memory private to TEE driver */
35 * struct tee_context - driver specific context on file pointer data
36 * @teedev: pointer to this drivers struct tee_device
37 * @list_shm: List of shared memory object owned by this context
38 * @data: driver specific context data, managed by the driver
39 * @refcount: reference counter for this structure
40 * @releasing: flag that indicates if context is being released right now.
41 * It is needed to break circular dependency on context during
42 * shared memory release.
43 * @supp_nowait: flag that indicates that requests in this context should not
44 * wait for tee-supplicant daemon to be started if not present
45 * and just return with an error code. It is needed for requests
46 * that arises from TEE based kernel drivers that should be
47 * non-blocking in nature.
48 * @cap_memref_null: flag indicating if the TEE Client support shared
49 * memory buffer with a NULL pointer.
52 struct tee_device *teedev;
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 * @system_session: declare session as a system session
88 * @invoke_func: invoke a trusted function
89 * @cancel_req: request cancel of an ongoing invoke or open
90 * @supp_recv: called for supplicant to get a command
91 * @supp_send: called for supplicant to send a response
92 * @shm_register: register shared memory buffer in TEE
93 * @shm_unregister: unregister shared memory buffer in TEE
95 struct tee_driver_ops {
96 void (*get_version)(struct tee_device *teedev,
97 struct tee_ioctl_version_data *vers);
98 int (*open)(struct tee_context *ctx);
99 void (*release)(struct tee_context *ctx);
100 int (*open_session)(struct tee_context *ctx,
101 struct tee_ioctl_open_session_arg *arg,
102 struct tee_param *param);
103 int (*close_session)(struct tee_context *ctx, u32 session);
104 int (*system_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);
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
126 #define TEE_DESC_PRIVILEGED 0x1
129 const struct tee_driver_ops *ops;
130 struct module *owner;
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
141 * Allocates a new struct tee_device instance. The device is
142 * removed by tee_device_unregister().
144 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
146 struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
148 struct tee_shm_pool *pool,
152 * tee_device_register() - Registers a TEE device
153 * @teedev: Device to register
155 * tee_device_unregister() need to be called to remove the @teedev if
156 * this function fails.
158 * @returns < 0 on failure
160 int tee_device_register(struct tee_device *teedev);
163 * tee_device_unregister() - Removes a TEE device
164 * @teedev: Device to unregister
166 * This function should be called to remove the @teedev even if
167 * tee_device_register() hasn't been called yet. Does nothing if
170 void tee_device_unregister(struct tee_device *teedev);
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
178 * Based on connection method calculates UUIDv5 based client UUID.
180 * For group based logins verifies that calling process has specified
183 * @return < 0 on failure
185 int tee_session_calc_client_uuid(uuid_t *uuid, u32 connection_method,
186 const u8 connection_data[TEE_IOCTL_UUID_LEN]);
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 * @refcount: reference counter
198 * @flags: defined by TEE_SHM_* in tee_drv.h
199 * @id: unique id of a shared memory object on this device, shared
202 * secure world assigned id of this shared memory object, not
203 * used by all drivers
205 * This pool is only supposed to be accessed directly from the TEE
206 * subsystem and from drivers that implements their own shm pool manager.
209 struct tee_context *ctx;
223 * struct tee_shm_pool - shared memory pool
225 * @private_data: private data for the shared memory manager
227 struct tee_shm_pool {
228 const struct tee_shm_pool_ops *ops;
233 * struct tee_shm_pool_ops - shared memory pool operations
234 * @alloc: called when allocating shared memory
235 * @free: called when freeing shared memory
236 * @destroy_pool: called when destroying the pool
238 struct tee_shm_pool_ops {
239 int (*alloc)(struct tee_shm_pool *pool, struct tee_shm *shm,
240 size_t size, size_t align);
241 void (*free)(struct tee_shm_pool *pool, struct tee_shm *shm);
242 void (*destroy_pool)(struct tee_shm_pool *pool);
246 * tee_shm_pool_alloc_res_mem() - Create a shm manager for reserved memory
247 * @vaddr: Virtual address of start of pool
248 * @paddr: Physical address of start of pool
249 * @size: Size in bytes of the pool
251 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
253 struct tee_shm_pool *tee_shm_pool_alloc_res_mem(unsigned long vaddr,
254 phys_addr_t paddr, size_t size,
255 int min_alloc_order);
258 * tee_shm_pool_free() - Free a shared memory pool
259 * @pool: The shared memory pool to free
261 * The must be no remaining shared memory allocated from this pool when
262 * this function is called.
264 static inline void tee_shm_pool_free(struct tee_shm_pool *pool)
266 pool->ops->destroy_pool(pool);
270 * tee_get_drvdata() - Return driver_data pointer
271 * @returns the driver_data pointer supplied to tee_register().
273 void *tee_get_drvdata(struct tee_device *teedev);
275 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
276 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);
278 struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
279 void *addr, size_t length);
282 * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
283 * @shm: Shared memory handle
284 * @returns true if object is dynamic shared memory
286 static inline bool tee_shm_is_dynamic(struct tee_shm *shm)
288 return shm && (shm->flags & TEE_SHM_DYNAMIC);
292 * tee_shm_free() - Free shared memory
293 * @shm: Handle to shared memory to free
295 void tee_shm_free(struct tee_shm *shm);
298 * tee_shm_put() - Decrease reference count on a shared memory handle
299 * @shm: Shared memory handle
301 void tee_shm_put(struct tee_shm *shm);
304 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
305 * @shm: Shared memory handle
306 * @offs: Offset from start of this shared memory
307 * @returns virtual address of the shared memory + offs if offs is within
308 * the bounds of this shared memory, else an ERR_PTR
310 void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
313 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
314 * @shm: Shared memory handle
315 * @offs: Offset from start of this shared memory
316 * @pa: Physical address to return
317 * @returns 0 if offs is within the bounds of this shared memory, else an
320 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
323 * tee_shm_get_size() - Get size of shared memory buffer
324 * @shm: Shared memory handle
325 * @returns size of shared memory
327 static inline size_t tee_shm_get_size(struct tee_shm *shm)
333 * tee_shm_get_pages() - Get list of pages that hold shared buffer
334 * @shm: Shared memory handle
335 * @num_pages: Number of pages will be stored there
336 * @returns pointer to pages array
338 static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
341 *num_pages = shm->num_pages;
346 * tee_shm_get_page_offset() - Get shared buffer offset from page start
347 * @shm: Shared memory handle
348 * @returns page offset of shared buffer
350 static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
356 * tee_shm_get_id() - Get id of a shared memory object
357 * @shm: Shared memory handle
360 static inline int tee_shm_get_id(struct tee_shm *shm)
366 * tee_shm_get_from_id() - Find shared memory object and increase reference
368 * @ctx: Context owning the shared memory
369 * @id: Id of shared memory object
370 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
372 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
375 * tee_client_open_context() - Open a TEE context
376 * @start: if not NULL, continue search after this context
377 * @match: function to check TEE device
378 * @data: data for match function
379 * @vers: if not NULL, version data of TEE device of the context returned
381 * This function does an operation similar to open("/dev/teeX") in user space.
382 * A returned context must be released with tee_client_close_context().
384 * Returns a TEE context of the first TEE device matched by the match()
385 * callback or an ERR_PTR.
388 tee_client_open_context(struct tee_context *start,
389 int (*match)(struct tee_ioctl_version_data *,
391 const void *data, struct tee_ioctl_version_data *vers);
394 * tee_client_close_context() - Close a TEE context
395 * @ctx: TEE context to close
397 * Note that all sessions previously opened with this context will be
398 * closed when this function is called.
400 void tee_client_close_context(struct tee_context *ctx);
403 * tee_client_get_version() - Query version of TEE
404 * @ctx: TEE context to TEE to query
405 * @vers: Pointer to version data
407 void tee_client_get_version(struct tee_context *ctx,
408 struct tee_ioctl_version_data *vers);
411 * tee_client_open_session() - Open a session to a Trusted Application
413 * @arg: Open session arguments, see description of
414 * struct tee_ioctl_open_session_arg
415 * @param: Parameters passed to the Trusted Application
417 * Returns < 0 on error else see @arg->ret for result. If @arg->ret
418 * is TEEC_SUCCESS the session identifier is available in @arg->session.
420 int tee_client_open_session(struct tee_context *ctx,
421 struct tee_ioctl_open_session_arg *arg,
422 struct tee_param *param);
425 * tee_client_close_session() - Close a session to a Trusted Application
427 * @session: Session id
429 * Return < 0 on error else 0, regardless the session will not be
430 * valid after this function has returned.
432 int tee_client_close_session(struct tee_context *ctx, u32 session);
435 * tee_client_system_session() - Declare session as a system session
437 * @session: Session id
439 * This function requests TEE to provision an entry context ready to use for
440 * that session only. The provisioned entry context is used for command
441 * invocation and session closure, not for command cancelling requests.
442 * TEE releases the provisioned context upon session closure.
444 * Return < 0 on error else 0 if an entry context has been provisioned.
446 int tee_client_system_session(struct tee_context *ctx, u32 session);
449 * tee_client_invoke_func() - Invoke a function in a Trusted Application
451 * @arg: Invoke arguments, see description of
452 * struct tee_ioctl_invoke_arg
453 * @param: Parameters passed to the Trusted Application
455 * Returns < 0 on error else see @arg->ret for result.
457 int tee_client_invoke_func(struct tee_context *ctx,
458 struct tee_ioctl_invoke_arg *arg,
459 struct tee_param *param);
462 * tee_client_cancel_req() - Request cancellation of the previous open-session
463 * or invoke-command operations in a Trusted Application
465 * @arg: Cancellation arguments, see description of
466 * struct tee_ioctl_cancel_arg
468 * Returns < 0 on error else 0 if the cancellation was successfully requested.
470 int tee_client_cancel_req(struct tee_context *ctx,
471 struct tee_ioctl_cancel_arg *arg);
473 static inline bool tee_param_is_memref(struct tee_param *param)
475 switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
476 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
477 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
478 case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
485 extern const struct bus_type tee_bus_type;
488 * struct tee_client_device - tee based device
489 * @id: device identifier
490 * @dev: device structure
492 struct tee_client_device {
493 struct tee_client_device_id id;
497 #define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
500 * struct tee_client_driver - tee client driver
501 * @id_table: device id table supported by this driver
502 * @driver: driver structure
504 struct tee_client_driver {
505 const struct tee_client_device_id *id_table;
506 struct device_driver driver;
509 #define to_tee_client_driver(d) \
510 container_of(d, struct tee_client_driver, driver)
513 * teedev_open() - Open a struct tee_device
514 * @teedev: Device to open
516 * @return a pointer to struct tee_context on success or an ERR_PTR on failure.
518 struct tee_context *teedev_open(struct tee_device *teedev);
521 * teedev_close_context() - closes a struct tee_context
522 * @ctx: The struct tee_context to close
524 void teedev_close_context(struct tee_context *ctx);
526 #endif /*__TEE_DRV_H*/