19c92ab61SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2967c9ccaSJens Wiklander /*
30439fcffSSumit Garg * Copyright (c) 2015-2024 Linaro Limited
4967c9ccaSJens Wiklander */
5967c9ccaSJens Wiklander
6967c9ccaSJens Wiklander #ifndef __TEE_DRV_H
7967c9ccaSJens Wiklander #define __TEE_DRV_H
8967c9ccaSJens Wiklander
90fc1db9dSSumit Garg #include <linux/device.h>
10217e0250SVolodymyr Babchuk #include <linux/kref.h>
11967c9ccaSJens Wiklander #include <linux/list.h>
120fc1db9dSSumit Garg #include <linux/mod_devicetable.h>
13967c9ccaSJens Wiklander #include <linux/tee.h>
140fc1db9dSSumit Garg #include <linux/types.h>
15967c9ccaSJens Wiklander
16967c9ccaSJens Wiklander /*
170439fcffSSumit Garg * The file describes the API provided by the TEE subsystem to the
180439fcffSSumit Garg * TEE client drivers.
19967c9ccaSJens Wiklander */
20967c9ccaSJens Wiklander
21967c9ccaSJens Wiklander struct tee_device;
22967c9ccaSJens Wiklander
23967c9ccaSJens Wiklander /**
24967c9ccaSJens Wiklander * struct tee_context - driver specific context on file pointer data
25967c9ccaSJens Wiklander * @teedev: pointer to this drivers struct tee_device
26967c9ccaSJens Wiklander * @data: driver specific context data, managed by the driver
27217e0250SVolodymyr Babchuk * @refcount: reference counter for this structure
28217e0250SVolodymyr Babchuk * @releasing: flag that indicates if context is being released right now.
29217e0250SVolodymyr Babchuk * It is needed to break circular dependency on context during
30217e0250SVolodymyr Babchuk * shared memory release.
3142bf4152SSumit Garg * @supp_nowait: flag that indicates that requests in this context should not
3242bf4152SSumit Garg * wait for tee-supplicant daemon to be started if not present
3342bf4152SSumit Garg * and just return with an error code. It is needed for requests
3442bf4152SSumit Garg * that arises from TEE based kernel drivers that should be
3542bf4152SSumit Garg * non-blocking in nature.
36ba171d3fSCedric Neveux * @cap_memref_null: flag indicating if the TEE Client support shared
37ba171d3fSCedric Neveux * memory buffer with a NULL pointer.
38967c9ccaSJens Wiklander */
39967c9ccaSJens Wiklander struct tee_context {
40967c9ccaSJens Wiklander struct tee_device *teedev;
41967c9ccaSJens Wiklander void *data;
42217e0250SVolodymyr Babchuk struct kref refcount;
43217e0250SVolodymyr Babchuk bool releasing;
4442bf4152SSumit Garg bool supp_nowait;
45ba171d3fSCedric Neveux bool cap_memref_null;
46967c9ccaSJens Wiklander };
47967c9ccaSJens Wiklander
480439fcffSSumit Garg /**
490439fcffSSumit Garg * struct tee_shm - shared memory object
500439fcffSSumit Garg * @ctx: context using the object
510439fcffSSumit Garg * @paddr: physical address of the shared memory
520439fcffSSumit Garg * @kaddr: virtual address of the shared memory
530439fcffSSumit Garg * @size: size of shared memory
540439fcffSSumit Garg * @offset: offset of buffer in user space
550439fcffSSumit Garg * @pages: locked pages from userspace
560439fcffSSumit Garg * @num_pages: number of locked pages
570439fcffSSumit Garg * @refcount: reference counter
580439fcffSSumit Garg * @flags: defined by TEE_SHM_* in tee_core.h
590439fcffSSumit Garg * @id: unique id of a shared memory object on this device, shared
600439fcffSSumit Garg * with user space
610439fcffSSumit Garg * @sec_world_id:
620439fcffSSumit Garg * secure world assigned id of this shared memory object, not
630439fcffSSumit Garg * used by all drivers
640439fcffSSumit Garg */
650439fcffSSumit Garg struct tee_shm {
660439fcffSSumit Garg struct tee_context *ctx;
670439fcffSSumit Garg phys_addr_t paddr;
680439fcffSSumit Garg void *kaddr;
690439fcffSSumit Garg size_t size;
700439fcffSSumit Garg unsigned int offset;
710439fcffSSumit Garg struct page **pages;
720439fcffSSumit Garg size_t num_pages;
730439fcffSSumit Garg refcount_t refcount;
740439fcffSSumit Garg u32 flags;
750439fcffSSumit Garg int id;
760439fcffSSumit Garg u64 sec_world_id;
770439fcffSSumit Garg };
780439fcffSSumit Garg
79967c9ccaSJens Wiklander struct tee_param_memref {
80967c9ccaSJens Wiklander size_t shm_offs;
81967c9ccaSJens Wiklander size_t size;
82967c9ccaSJens Wiklander struct tee_shm *shm;
83967c9ccaSJens Wiklander };
84967c9ccaSJens Wiklander
85967c9ccaSJens Wiklander struct tee_param_value {
86967c9ccaSJens Wiklander u64 a;
87967c9ccaSJens Wiklander u64 b;
88967c9ccaSJens Wiklander u64 c;
89967c9ccaSJens Wiklander };
90967c9ccaSJens Wiklander
91967c9ccaSJens Wiklander struct tee_param {
92967c9ccaSJens Wiklander u64 attr;
93967c9ccaSJens Wiklander union {
94967c9ccaSJens Wiklander struct tee_param_memref memref;
95967c9ccaSJens Wiklander struct tee_param_value value;
96967c9ccaSJens Wiklander } u;
97967c9ccaSJens Wiklander };
98967c9ccaSJens Wiklander
99967c9ccaSJens Wiklander /**
1000439fcffSSumit Garg * tee_shm_alloc_kernel_buf() - Allocate kernel shared memory for a
1010439fcffSSumit Garg * particular TEE client driver
1020439fcffSSumit Garg * @ctx: The TEE context for shared memory allocation
1030439fcffSSumit Garg * @size: Shared memory allocation size
1040439fcffSSumit Garg * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
105967c9ccaSJens Wiklander */
106dc7019b7SJens Wiklander struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size);
107967c9ccaSJens Wiklander
1080439fcffSSumit Garg /**
1090439fcffSSumit Garg * tee_shm_register_kernel_buf() - Register kernel shared memory for a
1100439fcffSSumit Garg * particular TEE client driver
1110439fcffSSumit Garg * @ctx: The TEE context for shared memory registration
1120439fcffSSumit Garg * @addr: Kernel buffer address
1130439fcffSSumit Garg * @length: Kernel buffer length
1140439fcffSSumit Garg * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
1150439fcffSSumit Garg */
116056d3fedSJens Wiklander struct tee_shm *tee_shm_register_kernel_buf(struct tee_context *ctx,
117056d3fedSJens Wiklander void *addr, size_t length);
118033ddf12SJens Wiklander
119033ddf12SJens Wiklander /**
120967c9ccaSJens Wiklander * tee_shm_free() - Free shared memory
121967c9ccaSJens Wiklander * @shm: Handle to shared memory to free
122967c9ccaSJens Wiklander */
123967c9ccaSJens Wiklander void tee_shm_free(struct tee_shm *shm);
124967c9ccaSJens Wiklander
125967c9ccaSJens Wiklander /**
126967c9ccaSJens Wiklander * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
127967c9ccaSJens Wiklander * @shm: Shared memory handle
128967c9ccaSJens Wiklander * @offs: Offset from start of this shared memory
129967c9ccaSJens Wiklander * @returns virtual address of the shared memory + offs if offs is within
130967c9ccaSJens Wiklander * the bounds of this shared memory, else an ERR_PTR
131967c9ccaSJens Wiklander */
132967c9ccaSJens Wiklander void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
133967c9ccaSJens Wiklander
134967c9ccaSJens Wiklander /**
135967c9ccaSJens Wiklander * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
136967c9ccaSJens Wiklander * @shm: Shared memory handle
137967c9ccaSJens Wiklander * @offs: Offset from start of this shared memory
138967c9ccaSJens Wiklander * @pa: Physical address to return
139967c9ccaSJens Wiklander * @returns 0 if offs is within the bounds of this shared memory, else an
140967c9ccaSJens Wiklander * error code.
141967c9ccaSJens Wiklander */
142967c9ccaSJens Wiklander int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
143967c9ccaSJens Wiklander
144967c9ccaSJens Wiklander /**
145b25946adSVolodymyr Babchuk * tee_shm_get_size() - Get size of shared memory buffer
146b25946adSVolodymyr Babchuk * @shm: Shared memory handle
147b25946adSVolodymyr Babchuk * @returns size of shared memory
148b25946adSVolodymyr Babchuk */
tee_shm_get_size(struct tee_shm * shm)149b25946adSVolodymyr Babchuk static inline size_t tee_shm_get_size(struct tee_shm *shm)
150b25946adSVolodymyr Babchuk {
151b25946adSVolodymyr Babchuk return shm->size;
152b25946adSVolodymyr Babchuk }
153b25946adSVolodymyr Babchuk
154b25946adSVolodymyr Babchuk /**
155e0c69ae8SVolodymyr Babchuk * tee_shm_get_pages() - Get list of pages that hold shared buffer
156e0c69ae8SVolodymyr Babchuk * @shm: Shared memory handle
157e0c69ae8SVolodymyr Babchuk * @num_pages: Number of pages will be stored there
158e0c69ae8SVolodymyr Babchuk * @returns pointer to pages array
159e0c69ae8SVolodymyr Babchuk */
tee_shm_get_pages(struct tee_shm * shm,size_t * num_pages)160e0c69ae8SVolodymyr Babchuk static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
161e0c69ae8SVolodymyr Babchuk size_t *num_pages)
162e0c69ae8SVolodymyr Babchuk {
163e0c69ae8SVolodymyr Babchuk *num_pages = shm->num_pages;
164e0c69ae8SVolodymyr Babchuk return shm->pages;
165e0c69ae8SVolodymyr Babchuk }
166e0c69ae8SVolodymyr Babchuk
167e0c69ae8SVolodymyr Babchuk /**
168b25946adSVolodymyr Babchuk * tee_shm_get_page_offset() - Get shared buffer offset from page start
169b25946adSVolodymyr Babchuk * @shm: Shared memory handle
170b25946adSVolodymyr Babchuk * @returns page offset of shared buffer
171b25946adSVolodymyr Babchuk */
tee_shm_get_page_offset(struct tee_shm * shm)172b25946adSVolodymyr Babchuk static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
173b25946adSVolodymyr Babchuk {
174b25946adSVolodymyr Babchuk return shm->offset;
175b25946adSVolodymyr Babchuk }
176b25946adSVolodymyr Babchuk
177b25946adSVolodymyr Babchuk /**
17825559c22SJens Wiklander * tee_client_open_context() - Open a TEE context
17925559c22SJens Wiklander * @start: if not NULL, continue search after this context
18025559c22SJens Wiklander * @match: function to check TEE device
18125559c22SJens Wiklander * @data: data for match function
18225559c22SJens Wiklander * @vers: if not NULL, version data of TEE device of the context returned
18325559c22SJens Wiklander *
18425559c22SJens Wiklander * This function does an operation similar to open("/dev/teeX") in user space.
18525559c22SJens Wiklander * A returned context must be released with tee_client_close_context().
18625559c22SJens Wiklander *
18725559c22SJens Wiklander * Returns a TEE context of the first TEE device matched by the match()
18825559c22SJens Wiklander * callback or an ERR_PTR.
18925559c22SJens Wiklander */
19025559c22SJens Wiklander struct tee_context *
19125559c22SJens Wiklander tee_client_open_context(struct tee_context *start,
19225559c22SJens Wiklander int (*match)(struct tee_ioctl_version_data *,
19325559c22SJens Wiklander const void *),
19425559c22SJens Wiklander const void *data, struct tee_ioctl_version_data *vers);
19525559c22SJens Wiklander
19625559c22SJens Wiklander /**
19725559c22SJens Wiklander * tee_client_close_context() - Close a TEE context
19825559c22SJens Wiklander * @ctx: TEE context to close
19925559c22SJens Wiklander *
20025559c22SJens Wiklander * Note that all sessions previously opened with this context will be
20125559c22SJens Wiklander * closed when this function is called.
20225559c22SJens Wiklander */
20325559c22SJens Wiklander void tee_client_close_context(struct tee_context *ctx);
20425559c22SJens Wiklander
20525559c22SJens Wiklander /**
20625559c22SJens Wiklander * tee_client_get_version() - Query version of TEE
20725559c22SJens Wiklander * @ctx: TEE context to TEE to query
20825559c22SJens Wiklander * @vers: Pointer to version data
20925559c22SJens Wiklander */
21025559c22SJens Wiklander void tee_client_get_version(struct tee_context *ctx,
21125559c22SJens Wiklander struct tee_ioctl_version_data *vers);
21225559c22SJens Wiklander
21325559c22SJens Wiklander /**
21425559c22SJens Wiklander * tee_client_open_session() - Open a session to a Trusted Application
21525559c22SJens Wiklander * @ctx: TEE context
21625559c22SJens Wiklander * @arg: Open session arguments, see description of
21725559c22SJens Wiklander * struct tee_ioctl_open_session_arg
21825559c22SJens Wiklander * @param: Parameters passed to the Trusted Application
21925559c22SJens Wiklander *
22025559c22SJens Wiklander * Returns < 0 on error else see @arg->ret for result. If @arg->ret
22125559c22SJens Wiklander * is TEEC_SUCCESS the session identifier is available in @arg->session.
22225559c22SJens Wiklander */
22325559c22SJens Wiklander int tee_client_open_session(struct tee_context *ctx,
22425559c22SJens Wiklander struct tee_ioctl_open_session_arg *arg,
22525559c22SJens Wiklander struct tee_param *param);
22625559c22SJens Wiklander
22725559c22SJens Wiklander /**
22825559c22SJens Wiklander * tee_client_close_session() - Close a session to a Trusted Application
22925559c22SJens Wiklander * @ctx: TEE Context
23025559c22SJens Wiklander * @session: Session id
23125559c22SJens Wiklander *
23225559c22SJens Wiklander * Return < 0 on error else 0, regardless the session will not be
23325559c22SJens Wiklander * valid after this function has returned.
23425559c22SJens Wiklander */
23525559c22SJens Wiklander int tee_client_close_session(struct tee_context *ctx, u32 session);
23625559c22SJens Wiklander
23725559c22SJens Wiklander /**
238a9214a88SEtienne Carriere * tee_client_system_session() - Declare session as a system session
239a9214a88SEtienne Carriere * @ctx: TEE Context
240a9214a88SEtienne Carriere * @session: Session id
241a9214a88SEtienne Carriere *
242a9214a88SEtienne Carriere * This function requests TEE to provision an entry context ready to use for
243a9214a88SEtienne Carriere * that session only. The provisioned entry context is used for command
244a9214a88SEtienne Carriere * invocation and session closure, not for command cancelling requests.
245a9214a88SEtienne Carriere * TEE releases the provisioned context upon session closure.
246a9214a88SEtienne Carriere *
247a9214a88SEtienne Carriere * Return < 0 on error else 0 if an entry context has been provisioned.
248a9214a88SEtienne Carriere */
249a9214a88SEtienne Carriere int tee_client_system_session(struct tee_context *ctx, u32 session);
250a9214a88SEtienne Carriere
251a9214a88SEtienne Carriere /**
25225559c22SJens Wiklander * tee_client_invoke_func() - Invoke a function in a Trusted Application
25325559c22SJens Wiklander * @ctx: TEE Context
25425559c22SJens Wiklander * @arg: Invoke arguments, see description of
25525559c22SJens Wiklander * struct tee_ioctl_invoke_arg
25625559c22SJens Wiklander * @param: Parameters passed to the Trusted Application
25725559c22SJens Wiklander *
25825559c22SJens Wiklander * Returns < 0 on error else see @arg->ret for result.
25925559c22SJens Wiklander */
26025559c22SJens Wiklander int tee_client_invoke_func(struct tee_context *ctx,
26125559c22SJens Wiklander struct tee_ioctl_invoke_arg *arg,
26225559c22SJens Wiklander struct tee_param *param);
26325559c22SJens Wiklander
2644f062dc1SIgor Opaniuk /**
2654f062dc1SIgor Opaniuk * tee_client_cancel_req() - Request cancellation of the previous open-session
2664f062dc1SIgor Opaniuk * or invoke-command operations in a Trusted Application
2674f062dc1SIgor Opaniuk * @ctx: TEE Context
2684f062dc1SIgor Opaniuk * @arg: Cancellation arguments, see description of
2694f062dc1SIgor Opaniuk * struct tee_ioctl_cancel_arg
2704f062dc1SIgor Opaniuk *
2714f062dc1SIgor Opaniuk * Returns < 0 on error else 0 if the cancellation was successfully requested.
2724f062dc1SIgor Opaniuk */
2734f062dc1SIgor Opaniuk int tee_client_cancel_req(struct tee_context *ctx,
2744f062dc1SIgor Opaniuk struct tee_ioctl_cancel_arg *arg);
2754f062dc1SIgor Opaniuk
276469f6acdSRicardo B. Marliere extern const struct bus_type tee_bus_type;
2770fc1db9dSSumit Garg
2780fc1db9dSSumit Garg /**
2790fc1db9dSSumit Garg * struct tee_client_device - tee based device
2800fc1db9dSSumit Garg * @id: device identifier
2810fc1db9dSSumit Garg * @dev: device structure
2820fc1db9dSSumit Garg */
2830fc1db9dSSumit Garg struct tee_client_device {
2840fc1db9dSSumit Garg struct tee_client_device_id id;
2850fc1db9dSSumit Garg struct device dev;
2860fc1db9dSSumit Garg };
2870fc1db9dSSumit Garg
2880fc1db9dSSumit Garg #define to_tee_client_device(d) container_of(d, struct tee_client_device, dev)
2890fc1db9dSSumit Garg
2900fc1db9dSSumit Garg /**
2910fc1db9dSSumit Garg * struct tee_client_driver - tee client driver
2920fc1db9dSSumit Garg * @id_table: device id table supported by this driver
2930fc1db9dSSumit Garg * @driver: driver structure
2940fc1db9dSSumit Garg */
2950fc1db9dSSumit Garg struct tee_client_driver {
2960fc1db9dSSumit Garg const struct tee_client_device_id *id_table;
2970fc1db9dSSumit Garg struct device_driver driver;
2980fc1db9dSSumit Garg };
2990fc1db9dSSumit Garg
3000fc1db9dSSumit Garg #define to_tee_client_driver(d) \
301*d69d8048SGreg Kroah-Hartman container_of_const(d, struct tee_client_driver, driver)
3020fc1db9dSSumit Garg
303967c9ccaSJens Wiklander #endif /*__TEE_DRV_H*/
304