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