tee_drv.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  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. #ifndef __TEE_DRV_H
  15. #define __TEE_DRV_H
  16. #include <linux/types.h>
  17. #include <linux/idr.h>
  18. #include <linux/list.h>
  19. #include <linux/tee.h>
  20. /*
  21. * The file describes the API provided by the generic TEE driver to the
  22. * specific TEE driver.
  23. */
  24. #define TEE_SHM_MAPPED BIT(0) /* Memory mapped by the kernel */
  25. #define TEE_SHM_DMA_BUF BIT(1) /* Memory with dma-buf handle */
  26. #define TEE_SHM_EXT_DMA_BUF BIT(2) /* Memory with dma-buf handle */
  27. struct tee_device;
  28. struct tee_shm;
  29. struct tee_shm_pool;
  30. /**
  31. * struct tee_context - driver specific context on file pointer data
  32. * @teedev: pointer to this drivers struct tee_device
  33. * @list_shm: List of shared memory object owned by this context
  34. * @data: driver specific context data, managed by the driver
  35. */
  36. struct tee_context {
  37. struct tee_device *teedev;
  38. struct list_head list_shm;
  39. void *data;
  40. };
  41. struct tee_param_memref {
  42. size_t shm_offs;
  43. size_t size;
  44. struct tee_shm *shm;
  45. };
  46. struct tee_param_value {
  47. u64 a;
  48. u64 b;
  49. u64 c;
  50. };
  51. struct tee_param {
  52. u64 attr;
  53. union {
  54. struct tee_param_memref memref;
  55. struct tee_param_value value;
  56. } u;
  57. };
  58. /**
  59. * struct tee_driver_ops - driver operations vtable
  60. * @get_version: returns version of driver
  61. * @open: called when the device file is opened
  62. * @release: release this open file
  63. * @open_session: open a new session
  64. * @close_session: close a session
  65. * @invoke_func: invoke a trusted function
  66. * @cancel_req: request cancel of an ongoing invoke or open
  67. * @supp_revc: called for supplicant to get a command
  68. * @supp_send: called for supplicant to send a response
  69. */
  70. struct tee_driver_ops {
  71. void (*get_version)(struct tee_device *teedev,
  72. struct tee_ioctl_version_data *vers);
  73. int (*open)(struct tee_context *ctx);
  74. void (*release)(struct tee_context *ctx);
  75. int (*open_session)(struct tee_context *ctx,
  76. struct tee_ioctl_open_session_arg *arg,
  77. struct tee_param *param);
  78. int (*close_session)(struct tee_context *ctx, u32 session);
  79. int (*invoke_func)(struct tee_context *ctx,
  80. struct tee_ioctl_invoke_arg *arg,
  81. struct tee_param *param);
  82. int (*cancel_req)(struct tee_context *ctx, u32 cancel_id, u32 session);
  83. int (*supp_recv)(struct tee_context *ctx, u32 *func, u32 *num_params,
  84. struct tee_param *param);
  85. int (*supp_send)(struct tee_context *ctx, u32 ret, u32 num_params,
  86. struct tee_param *param);
  87. };
  88. /**
  89. * struct tee_desc - Describes the TEE driver to the subsystem
  90. * @name: name of driver
  91. * @ops: driver operations vtable
  92. * @owner: module providing the driver
  93. * @flags: Extra properties of driver, defined by TEE_DESC_* below
  94. */
  95. #define TEE_DESC_PRIVILEGED 0x1
  96. struct tee_desc {
  97. const char *name;
  98. const struct tee_driver_ops *ops;
  99. struct module *owner;
  100. u32 flags;
  101. };
  102. /**
  103. * tee_device_alloc() - Allocate a new struct tee_device instance
  104. * @teedesc: Descriptor for this driver
  105. * @dev: Parent device for this device
  106. * @pool: Shared memory pool, NULL if not used
  107. * @driver_data: Private driver data for this device
  108. *
  109. * Allocates a new struct tee_device instance. The device is
  110. * removed by tee_device_unregister().
  111. *
  112. * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
  113. */
  114. struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
  115. struct device *dev,
  116. struct tee_shm_pool *pool,
  117. void *driver_data);
  118. /**
  119. * tee_device_register() - Registers a TEE device
  120. * @teedev: Device to register
  121. *
  122. * tee_device_unregister() need to be called to remove the @teedev if
  123. * this function fails.
  124. *
  125. * @returns < 0 on failure
  126. */
  127. int tee_device_register(struct tee_device *teedev);
  128. /**
  129. * tee_device_unregister() - Removes a TEE device
  130. * @teedev: Device to unregister
  131. *
  132. * This function should be called to remove the @teedev even if
  133. * tee_device_register() hasn't been called yet. Does nothing if
  134. * @teedev is NULL.
  135. */
  136. void tee_device_unregister(struct tee_device *teedev);
  137. /**
  138. * struct tee_shm_pool_mem_info - holds information needed to create a shared
  139. * memory pool
  140. * @vaddr: Virtual address of start of pool
  141. * @paddr: Physical address of start of pool
  142. * @size: Size in bytes of the pool
  143. */
  144. struct tee_shm_pool_mem_info {
  145. unsigned long vaddr;
  146. phys_addr_t paddr;
  147. size_t size;
  148. };
  149. /**
  150. * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
  151. * memory range
  152. * @priv_info: Information for driver private shared memory pool
  153. * @dmabuf_info: Information for dma-buf shared memory pool
  154. *
  155. * Start and end of pools will must be page aligned.
  156. *
  157. * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
  158. * in @dmabuf, others will use the range provided by @priv.
  159. *
  160. * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
  161. */
  162. struct tee_shm_pool *
  163. tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
  164. struct tee_shm_pool_mem_info *dmabuf_info);
  165. /**
  166. * tee_shm_pool_free() - Free a shared memory pool
  167. * @pool: The shared memory pool to free
  168. *
  169. * The must be no remaining shared memory allocated from this pool when
  170. * this function is called.
  171. */
  172. void tee_shm_pool_free(struct tee_shm_pool *pool);
  173. /**
  174. * tee_get_drvdata() - Return driver_data pointer
  175. * @returns the driver_data pointer supplied to tee_register().
  176. */
  177. void *tee_get_drvdata(struct tee_device *teedev);
  178. /**
  179. * tee_shm_alloc() - Allocate shared memory
  180. * @ctx: Context that allocates the shared memory
  181. * @size: Requested size of shared memory
  182. * @flags: Flags setting properties for the requested shared memory.
  183. *
  184. * Memory allocated as global shared memory is automatically freed when the
  185. * TEE file pointer is closed. The @flags field uses the bits defined by
  186. * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
  187. * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
  188. * with a dma-buf handle, else driver private memory.
  189. *
  190. * @returns a pointer to 'struct tee_shm'
  191. */
  192. struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
  193. /**
  194. * tee_shm_register_fd() - Register shared memory from file descriptor
  195. *
  196. * @ctx: Context that allocates the shared memory
  197. * @fd: shared memory file descriptor reference.
  198. *
  199. * @returns a pointer to 'struct tee_shm'
  200. */
  201. struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd);
  202. /**
  203. * tee_shm_free() - Free shared memory
  204. * @shm: Handle to shared memory to free
  205. */
  206. void tee_shm_free(struct tee_shm *shm);
  207. /**
  208. * tee_shm_put() - Decrease reference count on a shared memory handle
  209. * @shm: Shared memory handle
  210. */
  211. void tee_shm_put(struct tee_shm *shm);
  212. /**
  213. * tee_shm_va2pa() - Get physical address of a virtual address
  214. * @shm: Shared memory handle
  215. * @va: Virtual address to tranlsate
  216. * @pa: Returned physical address
  217. * @returns 0 on success and < 0 on failure
  218. */
  219. int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
  220. /**
  221. * tee_shm_pa2va() - Get virtual address of a physical address
  222. * @shm: Shared memory handle
  223. * @pa: Physical address to tranlsate
  224. * @va: Returned virtual address
  225. * @returns 0 on success and < 0 on failure
  226. */
  227. int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
  228. /**
  229. * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
  230. * @shm: Shared memory handle
  231. * @offs: Offset from start of this shared memory
  232. * @returns virtual address of the shared memory + offs if offs is within
  233. * the bounds of this shared memory, else an ERR_PTR
  234. */
  235. void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
  236. /**
  237. * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
  238. * @shm: Shared memory handle
  239. * @offs: Offset from start of this shared memory
  240. * @pa: Physical address to return
  241. * @returns 0 if offs is within the bounds of this shared memory, else an
  242. * error code.
  243. */
  244. int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
  245. /**
  246. * tee_shm_get_id() - Get id of a shared memory object
  247. * @shm: Shared memory handle
  248. * @returns id
  249. */
  250. int tee_shm_get_id(struct tee_shm *shm);
  251. /**
  252. * tee_shm_get_from_id() - Find shared memory object and increase reference
  253. * count
  254. * @ctx: Context owning the shared memory
  255. * @id: Id of shared memory object
  256. * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
  257. */
  258. struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
  259. struct tee_context *tee_client_open_context(struct tee_context *start,
  260. int (*match)(struct tee_ioctl_version_data *,
  261. const void *),
  262. const void *data, struct tee_ioctl_version_data *vers);
  263. void tee_client_close_context(struct tee_context *ctx);
  264. void tee_client_get_version(struct tee_context *ctx,
  265. struct tee_ioctl_version_data *vers);
  266. int tee_client_open_session(struct tee_context *ctx,
  267. struct tee_ioctl_open_session_arg *arg,
  268. struct tee_param *param);
  269. int tee_client_close_session(struct tee_context *ctx, u32 session);
  270. int tee_client_invoke_func(struct tee_context *ctx,
  271. struct tee_ioctl_invoke_arg *arg,
  272. struct tee_param *param);
  273. #endif /*__TEE_DRV_H*/