tee_drv.h 14 KB

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