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