blob: e9be4a45ff3e4362938cda7b16a4e795f34ab95c [file] [log] [blame]
Jens Wiklander967c9cc2015-03-11 14:39:39 +01001/*
2 * Copyright (c) 2015-2016, Linaro Limited
3 *
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.
7 *
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.
12 *
13 */
14
15#ifndef __TEE_DRV_H
16#define __TEE_DRV_H
17
18#include <linux/types.h>
19#include <linux/idr.h>
20#include <linux/list.h>
21#include <linux/tee.h>
22
23/*
24 * The file describes the API provided by the generic TEE driver to the
25 * specific TEE driver.
26 */
27
28#define TEE_SHM_MAPPED 0x1 /* Memory mapped by the kernel */
29#define TEE_SHM_DMA_BUF 0x2 /* Memory with dma-buf handle */
30
Jerome Forissier999616b2017-05-31 13:21:05 +020031struct device;
Jens Wiklander967c9cc2015-03-11 14:39:39 +010032struct tee_device;
33struct tee_shm;
34struct tee_shm_pool;
35
36/**
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 */
42struct tee_context {
43 struct tee_device *teedev;
44 struct list_head list_shm;
45 void *data;
46};
47
48struct tee_param_memref {
49 size_t shm_offs;
50 size_t size;
51 struct tee_shm *shm;
52};
53
54struct tee_param_value {
55 u64 a;
56 u64 b;
57 u64 c;
58};
59
60struct tee_param {
61 u64 attr;
62 union {
63 struct tee_param_memref memref;
64 struct tee_param_value value;
65 } u;
66};
67
68/**
69 * struct tee_driver_ops - driver operations vtable
70 * @get_version: returns version of driver
71 * @open: called when the device file is opened
72 * @release: release this open file
73 * @open_session: open a new session
74 * @close_session: close a session
75 * @invoke_func: invoke a trusted function
76 * @cancel_req: request cancel of an ongoing invoke or open
77 * @supp_revc: called for supplicant to get a command
78 * @supp_send: called for supplicant to send a response
79 */
80struct tee_driver_ops {
81 void (*get_version)(struct tee_device *teedev,
82 struct tee_ioctl_version_data *vers);
83 int (*open)(struct tee_context *ctx);
84 void (*release)(struct tee_context *ctx);
85 int (*open_session)(struct tee_context *ctx,
86 struct tee_ioctl_open_session_arg *arg,
87 struct tee_param *param);
88 int (*close_session)(struct tee_context *ctx, u32 session);
89 int (*invoke_func)(struct tee_context *ctx,
90 struct tee_ioctl_invoke_arg *arg,
91 struct tee_param *param);
92 int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
93 int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
94 struct tee_param *param);
95 int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
96 struct tee_param *param);
97};
98
99/**
100 * struct tee_desc - Describes the TEE driver to the subsystem
101 * @name: name of driver
102 * @ops: driver operations vtable
103 * @owner: module providing the driver
104 * @flags: Extra properties of driver, defined by TEE_DESC_* below
105 */
106#define TEE_DESC_PRIVILEGED 0x1
107struct tee_desc {
108 const char *name;
109 const struct tee_driver_ops *ops;
110 struct module *owner;
111 u32 flags;
112};
113
114/**
115 * tee_device_alloc() - Allocate a new struct tee_device instance
116 * @teedesc: Descriptor for this driver
117 * @dev: Parent device for this device
118 * @pool: Shared memory pool, NULL if not used
119 * @driver_data: Private driver data for this device
120 *
121 * Allocates a new struct tee_device instance. The device is
122 * removed by tee_device_unregister().
123 *
124 * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
125 */
126struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
127 struct device *dev,
128 struct tee_shm_pool *pool,
129 void *driver_data);
130
131/**
132 * tee_device_register() - Registers a TEE device
133 * @teedev: Device to register
134 *
135 * tee_device_unregister() need to be called to remove the @teedev if
136 * this function fails.
137 *
138 * @returns < 0 on failure
139 */
140int tee_device_register(struct tee_device *teedev);
141
142/**
143 * tee_device_unregister() - Removes a TEE device
144 * @teedev: Device to unregister
145 *
146 * This function should be called to remove the @teedev even if
147 * tee_device_register() hasn't been called yet. Does nothing if
148 * @teedev is NULL.
149 */
150void tee_device_unregister(struct tee_device *teedev);
151
152/**
Jens Wiklandere2aca5d2017-11-29 14:48:25 +0200153 * struct tee_shm - shared memory object
154 * @teedev: device used to allocate the object
155 * @ctx: context using the object, if NULL the context is gone
156 * @link link element
157 * @paddr: physical address of the shared memory
158 * @kaddr: virtual address of the shared memory
159 * @size: size of shared memory
160 * @offset: offset of buffer in user space
161 * @pages: locked pages from userspace
162 * @num_pages: number of locked pages
163 * @dmabuf: dmabuf used to for exporting to user space
164 * @flags: defined by TEE_SHM_* in tee_drv.h
165 * @id: unique id of a shared memory object on this device
166 *
167 * This pool is only supposed to be accessed directly from the TEE
168 * subsystem and from drivers that implements their own shm pool manager.
169 */
170struct tee_shm {
171 struct tee_device *teedev;
172 struct tee_context *ctx;
173 struct list_head link;
174 phys_addr_t paddr;
175 void *kaddr;
176 size_t size;
177 unsigned int offset;
178 struct page **pages;
179 size_t num_pages;
180 struct dma_buf *dmabuf;
181 u32 flags;
182 int id;
183};
184
185/**
186 * struct tee_shm_pool_mgr - shared memory manager
187 * @ops: operations
188 * @private_data: private data for the shared memory manager
189 */
190struct tee_shm_pool_mgr {
191 const struct tee_shm_pool_mgr_ops *ops;
192 void *private_data;
193};
194
195/**
196 * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
197 * @alloc: called when allocating shared memory
198 * @free: called when freeing shared memory
199 * @destroy_poolmgr: called when destroying the pool manager
200 */
201struct tee_shm_pool_mgr_ops {
202 int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
203 size_t size);
204 void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
205 void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr);
206};
207
208/**
209 * tee_shm_pool_alloc() - Create a shared memory pool from shm managers
210 * @priv_mgr: manager for driver private shared memory allocations
211 * @dmabuf_mgr: manager for dma-buf shared memory allocations
212 *
213 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
214 * in @dmabuf, others will use the range provided by @priv.
215 *
216 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
217 */
218struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
219 struct tee_shm_pool_mgr *dmabuf_mgr);
220
221/*
222 * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved
223 * memory
224 * @vaddr: Virtual address of start of pool
225 * @paddr: Physical address of start of pool
226 * @size: Size in bytes of the pool
227 *
228 * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failure.
229 */
230struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
231 phys_addr_t paddr,
232 size_t size,
233 int min_alloc_order);
234
235/**
236 * tee_shm_pool_mgr_destroy() - Free a shared memory manager
237 */
238static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm)
239{
240 poolm->ops->destroy_poolmgr(poolm);
241}
242
243/**
Jens Wiklander967c9cc2015-03-11 14:39:39 +0100244 * struct tee_shm_pool_mem_info - holds information needed to create a shared
245 * memory pool
246 * @vaddr: Virtual address of start of pool
247 * @paddr: Physical address of start of pool
248 * @size: Size in bytes of the pool
249 */
250struct tee_shm_pool_mem_info {
251 unsigned long vaddr;
252 phys_addr_t paddr;
253 size_t size;
254};
255
256/**
257 * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
258 * memory range
259 * @priv_info: Information for driver private shared memory pool
260 * @dmabuf_info: Information for dma-buf shared memory pool
261 *
262 * Start and end of pools will must be page aligned.
263 *
264 * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
265 * in @dmabuf, others will use the range provided by @priv.
266 *
267 * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
268 */
269struct tee_shm_pool *
270tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
271 struct tee_shm_pool_mem_info *dmabuf_info);
272
273/**
274 * tee_shm_pool_free() - Free a shared memory pool
275 * @pool: The shared memory pool to free
276 *
277 * The must be no remaining shared memory allocated from this pool when
278 * this function is called.
279 */
280void tee_shm_pool_free(struct tee_shm_pool *pool);
281
282/**
283 * tee_get_drvdata() - Return driver_data pointer
284 * @returns the driver_data pointer supplied to tee_register().
285 */
286void *tee_get_drvdata(struct tee_device *teedev);
287
288/**
289 * tee_shm_alloc() - Allocate shared memory
290 * @ctx: Context that allocates the shared memory
291 * @size: Requested size of shared memory
292 * @flags: Flags setting properties for the requested shared memory.
293 *
294 * Memory allocated as global shared memory is automatically freed when the
295 * TEE file pointer is closed. The @flags field uses the bits defined by
296 * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
297 * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
298 * with a dma-buf handle, else driver private memory.
299 *
300 * @returns a pointer to 'struct tee_shm'
301 */
302struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
303
304/**
305 * tee_shm_free() - Free shared memory
306 * @shm: Handle to shared memory to free
307 */
308void tee_shm_free(struct tee_shm *shm);
309
310/**
311 * tee_shm_put() - Decrease reference count on a shared memory handle
312 * @shm: Shared memory handle
313 */
314void tee_shm_put(struct tee_shm *shm);
315
316/**
317 * tee_shm_va2pa() - Get physical address of a virtual address
318 * @shm: Shared memory handle
319 * @va: Virtual address to tranlsate
320 * @pa: Returned physical address
321 * @returns 0 on success and < 0 on failure
322 */
323int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
324
325/**
326 * tee_shm_pa2va() - Get virtual address of a physical address
327 * @shm: Shared memory handle
328 * @pa: Physical address to tranlsate
329 * @va: Returned virtual address
330 * @returns 0 on success and < 0 on failure
331 */
332int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
333
334/**
335 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
336 * @shm: Shared memory handle
337 * @offs: Offset from start of this shared memory
338 * @returns virtual address of the shared memory + offs if offs is within
339 * the bounds of this shared memory, else an ERR_PTR
340 */
341void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
342
343/**
344 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
345 * @shm: Shared memory handle
346 * @offs: Offset from start of this shared memory
347 * @pa: Physical address to return
348 * @returns 0 if offs is within the bounds of this shared memory, else an
349 * error code.
350 */
351int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
352
353/**
354 * tee_shm_get_id() - Get id of a shared memory object
355 * @shm: Shared memory handle
356 * @returns id
357 */
358int tee_shm_get_id(struct tee_shm *shm);
359
360/**
361 * tee_shm_get_from_id() - Find shared memory object and increase reference
362 * count
363 * @ctx: Context owning the shared memory
364 * @id: Id of shared memory object
365 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
366 */
367struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
368
369#endif /*__TEE_DRV_H*/