tee_drv.h 19 KB

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