tee_drv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. unsigned long start);
  103. int (*shm_unregister)(struct tee_context *ctx, struct tee_shm *shm);
  104. };
  105. /**
  106. * struct tee_desc - Describes the TEE driver to the subsystem
  107. * @name: name of driver
  108. * @ops: driver operations vtable
  109. * @owner: module providing the driver
  110. * @flags: Extra properties of driver, defined by TEE_DESC_* below
  111. */
  112. #define TEE_DESC_PRIVILEGED 0x1
  113. struct tee_desc {
  114. const char *name;
  115. const struct tee_driver_ops *ops;
  116. struct module *owner;
  117. u32 flags;
  118. };
  119. /**
  120. * tee_device_alloc() - Allocate a new struct tee_device instance
  121. * @teedesc: Descriptor for this driver
  122. * @dev: Parent device for this device
  123. * @pool: Shared memory pool, NULL if not used
  124. * @driver_data: Private driver data for this device
  125. *
  126. * Allocates a new struct tee_device instance. The device is
  127. * removed by tee_device_unregister().
  128. *
  129. * @returns a pointer to a 'struct tee_device' or an ERR_PTR on failure
  130. */
  131. struct tee_device *tee_device_alloc(const struct tee_desc *teedesc,
  132. struct device *dev,
  133. struct tee_shm_pool *pool,
  134. void *driver_data);
  135. /**
  136. * tee_device_register() - Registers a TEE device
  137. * @teedev: Device to register
  138. *
  139. * tee_device_unregister() need to be called to remove the @teedev if
  140. * this function fails.
  141. *
  142. * @returns < 0 on failure
  143. */
  144. int tee_device_register(struct tee_device *teedev);
  145. /**
  146. * tee_device_unregister() - Removes a TEE device
  147. * @teedev: Device to unregister
  148. *
  149. * This function should be called to remove the @teedev even if
  150. * tee_device_register() hasn't been called yet. Does nothing if
  151. * @teedev is NULL.
  152. */
  153. void tee_device_unregister(struct tee_device *teedev);
  154. /**
  155. * struct tee_shm - shared memory object
  156. * @teedev: device used to allocate the object
  157. * @ctx: context using the object, if NULL the context is gone
  158. * @link link element
  159. * @paddr: physical address of the shared memory
  160. * @kaddr: virtual address of the shared memory
  161. * @size: size of shared memory
  162. * @offset: offset of buffer in user space
  163. * @pages: locked pages from userspace
  164. * @num_pages: number of locked pages
  165. * @dmabuf: dmabuf used to for exporting to user space
  166. * @flags: defined by TEE_SHM_* in tee_drv.h
  167. * @id: unique id of a shared memory object on this device
  168. *
  169. * This pool is only supposed to be accessed directly from the TEE
  170. * subsystem and from drivers that implements their own shm pool manager.
  171. */
  172. struct tee_shm {
  173. struct tee_device *teedev;
  174. struct tee_context *ctx;
  175. struct list_head link;
  176. phys_addr_t paddr;
  177. void *kaddr;
  178. size_t size;
  179. unsigned int offset;
  180. struct page **pages;
  181. size_t num_pages;
  182. struct dma_buf *dmabuf;
  183. u32 flags;
  184. int id;
  185. };
  186. /**
  187. * struct tee_shm_pool_mgr - shared memory manager
  188. * @ops: operations
  189. * @private_data: private data for the shared memory manager
  190. */
  191. struct tee_shm_pool_mgr {
  192. const struct tee_shm_pool_mgr_ops *ops;
  193. void *private_data;
  194. };
  195. /**
  196. * struct tee_shm_pool_mgr_ops - shared memory pool manager operations
  197. * @alloc: called when allocating shared memory
  198. * @free: called when freeing shared memory
  199. * @destroy_poolmgr: called when destroying the pool manager
  200. */
  201. struct tee_shm_pool_mgr_ops {
  202. int (*alloc)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm,
  203. size_t size);
  204. void (*free)(struct tee_shm_pool_mgr *poolmgr, struct tee_shm *shm);
  205. void (*destroy_poolmgr)(struct tee_shm_pool_mgr *poolmgr);
  206. };
  207. /**
  208. * tee_shm_pool_alloc() - Create a shared memory pool from shm managers
  209. * @priv_mgr: manager for driver private shared memory allocations
  210. * @dmabuf_mgr: manager for dma-buf shared memory allocations
  211. *
  212. * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
  213. * in @dmabuf, others will use the range provided by @priv.
  214. *
  215. * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
  216. */
  217. struct tee_shm_pool *tee_shm_pool_alloc(struct tee_shm_pool_mgr *priv_mgr,
  218. struct tee_shm_pool_mgr *dmabuf_mgr);
  219. /*
  220. * tee_shm_pool_mgr_alloc_res_mem() - Create a shm manager for reserved
  221. * memory
  222. * @vaddr: Virtual address of start of pool
  223. * @paddr: Physical address of start of pool
  224. * @size: Size in bytes of the pool
  225. *
  226. * @returns pointer to a 'struct tee_shm_pool_mgr' or an ERR_PTR on failure.
  227. */
  228. struct tee_shm_pool_mgr *tee_shm_pool_mgr_alloc_res_mem(unsigned long vaddr,
  229. phys_addr_t paddr,
  230. size_t size,
  231. int min_alloc_order);
  232. /**
  233. * tee_shm_pool_mgr_destroy() - Free a shared memory manager
  234. */
  235. static inline void tee_shm_pool_mgr_destroy(struct tee_shm_pool_mgr *poolm)
  236. {
  237. poolm->ops->destroy_poolmgr(poolm);
  238. }
  239. /**
  240. * struct tee_shm_pool_mem_info - holds information needed to create a shared
  241. * memory pool
  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. struct tee_shm_pool_mem_info {
  247. unsigned long vaddr;
  248. phys_addr_t paddr;
  249. size_t size;
  250. };
  251. /**
  252. * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
  253. * memory range
  254. * @priv_info: Information for driver private shared memory pool
  255. * @dmabuf_info: Information for dma-buf shared memory pool
  256. *
  257. * Start and end of pools will must be page aligned.
  258. *
  259. * Allocation with the flag TEE_SHM_DMA_BUF set will use the range supplied
  260. * in @dmabuf, others will use the range provided by @priv.
  261. *
  262. * @returns pointer to a 'struct tee_shm_pool' or an ERR_PTR on failure.
  263. */
  264. struct tee_shm_pool *
  265. tee_shm_pool_alloc_res_mem(struct tee_shm_pool_mem_info *priv_info,
  266. struct tee_shm_pool_mem_info *dmabuf_info);
  267. /**
  268. * tee_shm_pool_free() - Free a shared memory pool
  269. * @pool: The shared memory pool to free
  270. *
  271. * The must be no remaining shared memory allocated from this pool when
  272. * this function is called.
  273. */
  274. void tee_shm_pool_free(struct tee_shm_pool *pool);
  275. /**
  276. * tee_get_drvdata() - Return driver_data pointer
  277. * @returns the driver_data pointer supplied to tee_register().
  278. */
  279. void *tee_get_drvdata(struct tee_device *teedev);
  280. /**
  281. * tee_shm_alloc() - Allocate shared memory
  282. * @ctx: Context that allocates the shared memory
  283. * @size: Requested size of shared memory
  284. * @flags: Flags setting properties for the requested shared memory.
  285. *
  286. * Memory allocated as global shared memory is automatically freed when the
  287. * TEE file pointer is closed. The @flags field uses the bits defined by
  288. * TEE_SHM_* above. TEE_SHM_MAPPED must currently always be set. If
  289. * TEE_SHM_DMA_BUF global shared memory will be allocated and associated
  290. * with a dma-buf handle, else driver private memory.
  291. *
  292. * @returns a pointer to 'struct tee_shm'
  293. */
  294. struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags);
  295. /**
  296. * tee_shm_priv_alloc() - Allocate shared memory privately
  297. * @dev: Device that allocates the shared memory
  298. * @size: Requested size of shared memory
  299. *
  300. * Allocates shared memory buffer that is not associated with any client
  301. * context. Such buffers are owned by TEE driver and used for internal calls.
  302. *
  303. * @returns a pointer to 'struct tee_shm'
  304. */
  305. struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size);
  306. /**
  307. * tee_shm_register() - Register shared memory buffer
  308. * @ctx: Context that registers the shared memory
  309. * @addr: Address is userspace of the shared buffer
  310. * @length: Length of the shared buffer
  311. * @flags: Flags setting properties for the requested shared memory.
  312. *
  313. * @returns a pointer to 'struct tee_shm'
  314. */
  315. struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
  316. size_t length, u32 flags);
  317. /**
  318. * tee_shm_is_registered() - Check if shared memory object in registered in TEE
  319. * @shm: Shared memory handle
  320. * @returns true if object is registered in TEE
  321. */
  322. static inline bool tee_shm_is_registered(struct tee_shm *shm)
  323. {
  324. return shm && (shm->flags & TEE_SHM_REGISTER);
  325. }
  326. /**
  327. * tee_shm_free() - Free shared memory
  328. * @shm: Handle to shared memory to free
  329. */
  330. void tee_shm_free(struct tee_shm *shm);
  331. /**
  332. * tee_shm_put() - Decrease reference count on a shared memory handle
  333. * @shm: Shared memory handle
  334. */
  335. void tee_shm_put(struct tee_shm *shm);
  336. /**
  337. * tee_shm_va2pa() - Get physical address of a virtual address
  338. * @shm: Shared memory handle
  339. * @va: Virtual address to tranlsate
  340. * @pa: Returned physical address
  341. * @returns 0 on success and < 0 on failure
  342. */
  343. int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa);
  344. /**
  345. * tee_shm_pa2va() - Get virtual address of a physical address
  346. * @shm: Shared memory handle
  347. * @pa: Physical address to tranlsate
  348. * @va: Returned virtual address
  349. * @returns 0 on success and < 0 on failure
  350. */
  351. int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va);
  352. /**
  353. * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
  354. * @shm: Shared memory handle
  355. * @offs: Offset from start of this shared memory
  356. * @returns virtual address of the shared memory + offs if offs is within
  357. * the bounds of this shared memory, else an ERR_PTR
  358. */
  359. void *tee_shm_get_va(struct tee_shm *shm, size_t offs);
  360. /**
  361. * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
  362. * @shm: Shared memory handle
  363. * @offs: Offset from start of this shared memory
  364. * @pa: Physical address to return
  365. * @returns 0 if offs is within the bounds of this shared memory, else an
  366. * error code.
  367. */
  368. int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa);
  369. /**
  370. * tee_shm_get_size() - Get size of shared memory buffer
  371. * @shm: Shared memory handle
  372. * @returns size of shared memory
  373. */
  374. static inline size_t tee_shm_get_size(struct tee_shm *shm)
  375. {
  376. return shm->size;
  377. }
  378. /**
  379. * tee_shm_get_pages() - Get list of pages that hold shared buffer
  380. * @shm: Shared memory handle
  381. * @num_pages: Number of pages will be stored there
  382. * @returns pointer to pages array
  383. */
  384. static inline struct page **tee_shm_get_pages(struct tee_shm *shm,
  385. size_t *num_pages)
  386. {
  387. *num_pages = shm->num_pages;
  388. return shm->pages;
  389. }
  390. /**
  391. * tee_shm_get_page_offset() - Get shared buffer offset from page start
  392. * @shm: Shared memory handle
  393. * @returns page offset of shared buffer
  394. */
  395. static inline size_t tee_shm_get_page_offset(struct tee_shm *shm)
  396. {
  397. return shm->offset;
  398. }
  399. /**
  400. * tee_shm_get_id() - Get id of a shared memory object
  401. * @shm: Shared memory handle
  402. * @returns id
  403. */
  404. static inline int tee_shm_get_id(struct tee_shm *shm)
  405. {
  406. return shm->id;
  407. }
  408. /**
  409. * tee_shm_get_from_id() - Find shared memory object and increase reference
  410. * count
  411. * @ctx: Context owning the shared memory
  412. * @id: Id of shared memory object
  413. * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
  414. */
  415. struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id);
  416. static inline bool tee_param_is_memref(struct tee_param *param)
  417. {
  418. switch (param->attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) {
  419. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT:
  420. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT:
  421. case TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT:
  422. return true;
  423. default:
  424. return false;
  425. }
  426. }
  427. #endif /*__TEE_DRV_H*/