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