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