tee.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2018 Linaro Limited
  4. */
  5. #ifndef __TEE_H
  6. #define __TEE_H
  7. #define TEE_UUID_LEN 16
  8. #define TEE_GEN_CAP_GP BIT(0) /* GlobalPlatform compliant TEE */
  9. #define TEE_GEN_CAP_REG_MEM BIT(1) /* Supports registering shared memory */
  10. #define TEE_SHM_REGISTER BIT(0) /* In list of shared memory */
  11. #define TEE_SHM_SEC_REGISTER BIT(1) /* TEE notified of this memory */
  12. #define TEE_SHM_ALLOC BIT(2) /* The memory is malloced() and must */
  13. /* be freed() */
  14. #define TEE_PARAM_ATTR_TYPE_NONE 0 /* parameter not used */
  15. #define TEE_PARAM_ATTR_TYPE_VALUE_INPUT 1
  16. #define TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT 2
  17. #define TEE_PARAM_ATTR_TYPE_VALUE_INOUT 3 /* input and output */
  18. #define TEE_PARAM_ATTR_TYPE_MEMREF_INPUT 5
  19. #define TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT 6
  20. #define TEE_PARAM_ATTR_TYPE_MEMREF_INOUT 7 /* input and output */
  21. #define TEE_PARAM_ATTR_TYPE_MASK 0xff
  22. #define TEE_PARAM_ATTR_META 0x100
  23. #define TEE_PARAM_ATTR_MASK (TEE_PARAM_ATTR_TYPE_MASK | \
  24. TEE_PARAM_ATTR_META)
  25. /*
  26. * Some Global Platform error codes which has a meaning if the
  27. * TEE_GEN_CAP_GP bit is returned by the driver in
  28. * struct tee_version_data::gen_caps
  29. */
  30. #define TEE_SUCCESS 0x00000000
  31. #define TEE_ERROR_GENERIC 0xffff0000
  32. #define TEE_ERROR_BAD_PARAMETERS 0xffff0006
  33. #define TEE_ERROR_ITEM_NOT_FOUND 0xffff0008
  34. #define TEE_ERROR_NOT_IMPLEMENTED 0xffff0009
  35. #define TEE_ERROR_NOT_SUPPORTED 0xffff000a
  36. #define TEE_ERROR_COMMUNICATION 0xffff000e
  37. #define TEE_ERROR_SECURITY 0xffff000f
  38. #define TEE_ERROR_OUT_OF_MEMORY 0xffff000c
  39. #define TEE_ERROR_TARGET_DEAD 0xffff3024
  40. #define TEE_ORIGIN_COMMS 0x00000002
  41. #define TEE_ORIGIN_TEE 0x00000003
  42. #define TEE_ORIGIN_TRUSTED_APP 0x00000004
  43. struct udevice;
  44. /**
  45. * struct tee_shm - memory shared with the TEE
  46. * @dev: The TEE device
  47. * @link: List node in the list in struct struct tee_uclass_priv
  48. * @addr: Pointer to the shared memory
  49. * @size: Size of the the shared memory
  50. * @flags: TEE_SHM_* above
  51. */
  52. struct tee_shm {
  53. struct udevice *dev;
  54. struct list_head link;
  55. void *addr;
  56. ulong size;
  57. u32 flags;
  58. };
  59. /**
  60. * struct tee_param_memref - memory reference for a Trusted Application
  61. * @shm_offs: Offset in bytes into the shared memory object @shm
  62. * @size: Size in bytes of the memory reference
  63. * @shm: Pointer to a shared memory object for the buffer
  64. *
  65. * Used as a part of struct tee_param, see that for more information.
  66. */
  67. struct tee_param_memref {
  68. ulong shm_offs;
  69. ulong size;
  70. struct tee_shm *shm;
  71. };
  72. /**
  73. * struct tee_param_value - value parameter for a Trusted Application
  74. * @a, @b, @c: Parameters passed by value
  75. *
  76. * Used as a part of struct tee_param, see that for more information.
  77. */
  78. struct tee_param_value {
  79. u64 a;
  80. u64 b;
  81. u64 c;
  82. };
  83. /**
  84. * struct tee_param - invoke parameter for a Trusted Application
  85. * @attr: Attributes
  86. * @u.memref: Memref parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
  87. * TEE_PARAM_ATTR_TYPE_MEMREF_* above
  88. * @u.value: Value parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
  89. * TEE_PARAM_ATTR_TYPE_VALUE_* above
  90. *
  91. * Parameters to TA are passed using an array of this struct, for
  92. * flexibility both value parameters and memory refereces can be used.
  93. */
  94. struct tee_param {
  95. u64 attr;
  96. union {
  97. struct tee_param_memref memref;
  98. struct tee_param_value value;
  99. } u;
  100. };
  101. /**
  102. * struct tee_open_session_arg - extra arguments for tee_open_session()
  103. * @uuid: [in] UUID of the Trusted Application
  104. * @clnt_uuid: [in] Normally zeroes
  105. * @clnt_login: [in] Normally 0
  106. * @session: [out] Session id
  107. * @ret: [out] return value
  108. * @ret_origin: [out] origin of the return value
  109. */
  110. struct tee_open_session_arg {
  111. u8 uuid[TEE_UUID_LEN];
  112. u8 clnt_uuid[TEE_UUID_LEN];
  113. u32 clnt_login;
  114. u32 session;
  115. u32 ret;
  116. u32 ret_origin;
  117. };
  118. /**
  119. * struct tee_invoke_arg - extra arguments for tee_invoke_func()
  120. * @func: [in] Trusted Application function, specific to the TA
  121. * @session: [in] Session id, from open session
  122. * @ret: [out] return value
  123. * @ret_origin: [out] origin of the return value
  124. */
  125. struct tee_invoke_arg {
  126. u32 func;
  127. u32 session;
  128. u32 ret;
  129. u32 ret_origin;
  130. };
  131. /**
  132. * struct tee_version_data - description of TEE
  133. * @gen_caps: Generic capabilities, TEE_GEN_CAP_* above
  134. */
  135. struct tee_version_data {
  136. u32 gen_caps;
  137. };
  138. /**
  139. * struct tee_driver_ops - TEE driver operations
  140. * @get_version: Query capabilities of TEE device,
  141. * @open_session: Opens a session to a Trusted Application in the TEE,
  142. * @close_session: Closes a session to Trusted Application,
  143. * @invoke_func: Invokes a function in a Trusted Application,
  144. * @shm_register: Registers memory shared with the TEE
  145. * @shm_unregister: Unregisters memory shared with the TEE
  146. */
  147. struct tee_driver_ops {
  148. /**
  149. * get_version() - Query capabilities of TEE device
  150. * @dev: The TEE device
  151. * @vers: Pointer to version data
  152. */
  153. void (*get_version)(struct udevice *dev, struct tee_version_data *vers);
  154. /**
  155. * open_session() - Open a session to a Trusted Application
  156. * @dev: The TEE device
  157. * @arg: Open session arguments
  158. * @num_param: Number of elements in @param
  159. * @param: Parameters for Trusted Application
  160. *
  161. * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
  162. * TEE_SUCCESS the session identifier is available in @arg->session.
  163. */
  164. int (*open_session)(struct udevice *dev,
  165. struct tee_open_session_arg *arg, uint num_param,
  166. struct tee_param *param);
  167. /**
  168. * close_session() - Close a session to a Trusted Application
  169. * @dev: The TEE device
  170. * @session: Session id
  171. *
  172. * Return < 0 on error else 0, regardless the session will not be valid
  173. * after this function has returned.
  174. */
  175. int (*close_session)(struct udevice *dev, u32 session);
  176. /**
  177. * tee_invoke_func() - Invoke a function in a Trusted Application
  178. * @dev: The TEE device
  179. * @arg: Invoke arguments
  180. * @num_param: Number of elements in @param
  181. * @param: Parameters for Trusted Application
  182. *
  183. * Returns < 0 on error else see @arg->ret for result.
  184. */
  185. int (*invoke_func)(struct udevice *dev, struct tee_invoke_arg *arg,
  186. uint num_param, struct tee_param *param);
  187. /**
  188. * shm_register() - Registers memory shared with the TEE
  189. * @dev: The TEE device
  190. * @shm: Pointer to a shared memory object
  191. * Returns 0 on success or < 0 on failure.
  192. */
  193. int (*shm_register)(struct udevice *dev, struct tee_shm *shm);
  194. /**
  195. * shm_unregister() - Unregisters memory shared with the TEE
  196. * @dev: The TEE device
  197. * @shm: Pointer to a shared memory object
  198. * Returns 0 on success or < 0 on failure.
  199. */
  200. int (*shm_unregister)(struct udevice *dev, struct tee_shm *shm);
  201. };
  202. /**
  203. * __tee_shm_add() - Internal helper function to register shared memory
  204. * @dev: The TEE device
  205. * @align: Required alignment of allocated memory block if
  206. * (@flags & TEE_SHM_ALLOC)
  207. * @addr: Address of memory block, ignored if (@flags & TEE_SHM_ALLOC)
  208. * @size: Size of memory block
  209. * @flags: TEE_SHM_* above
  210. * @shmp: If the function return 0, this holds the allocated
  211. * struct tee_shm
  212. *
  213. * returns 0 on success or < 0 on failure.
  214. */
  215. int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
  216. u32 flags, struct tee_shm **shmp);
  217. /**
  218. * tee_shm_alloc() - Allocate shared memory
  219. * @dev: The TEE device
  220. * @size: Size of memory block
  221. * @flags: TEE_SHM_* above
  222. * @shmp: If the function return 0, this holds the allocated
  223. * struct tee_shm
  224. *
  225. * returns 0 on success or < 0 on failure.
  226. */
  227. int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
  228. struct tee_shm **shmp);
  229. /**
  230. * tee_shm_register() - Registers shared memory
  231. * @dev: The TEE device
  232. * @addr: Address of memory block
  233. * @size: Size of memory block
  234. * @flags: TEE_SHM_* above
  235. * @shmp: If the function return 0, this holds the allocated
  236. * struct tee_shm
  237. *
  238. * returns 0 on success or < 0 on failure.
  239. */
  240. int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
  241. struct tee_shm **shmp);
  242. /**
  243. * tee_shm_free() - Frees shared memory
  244. * @shm: Shared memory object
  245. */
  246. void tee_shm_free(struct tee_shm *shm);
  247. /**
  248. * tee_shm_is_registered() - Check register status of shared memory object
  249. * @shm: Pointer to shared memory object
  250. * @dev: The TEE device
  251. *
  252. * Returns true if the shared memory object is registered for the supplied
  253. * TEE device
  254. */
  255. bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev);
  256. /**
  257. * tee_find_device() - Look up a TEE device
  258. * @start: if not NULL, continue search after this device
  259. * @match: function to check TEE device, returns != 0 if the device
  260. * matches
  261. * @data: data for match function
  262. * @vers: if not NULL, version data of TEE device of the device returned
  263. *
  264. * Returns a probed TEE device of the first TEE device matched by the
  265. * match() callback or NULL.
  266. */
  267. struct udevice *tee_find_device(struct udevice *start,
  268. int (*match)(struct tee_version_data *vers,
  269. const void *data),
  270. const void *data,
  271. struct tee_version_data *vers);
  272. /**
  273. * tee_get_version() - Query capabilities of TEE device
  274. * @dev: The TEE device
  275. * @vers: Pointer to version data
  276. */
  277. void tee_get_version(struct udevice *dev, struct tee_version_data *vers);
  278. /**
  279. * tee_open_session() - Open a session to a Trusted Application
  280. * @dev: The TEE device
  281. * @arg: Open session arguments
  282. * @num_param: Number of elements in @param
  283. * @param: Parameters for Trusted Application
  284. *
  285. * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
  286. * TEE_SUCCESS the session identifier is available in @arg->session.
  287. */
  288. int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
  289. uint num_param, struct tee_param *param);
  290. /**
  291. * tee_close_session() - Close a session to a Trusted Application
  292. * @dev: The TEE device
  293. * @session: Session id
  294. *
  295. * Return < 0 on error else 0, regardless the session will not be valid
  296. * after this function has returned.
  297. */
  298. int tee_close_session(struct udevice *dev, u32 session);
  299. /**
  300. * tee_invoke_func() - Invoke a function in a Trusted Application
  301. * @dev: The TEE device
  302. * @arg: Invoke arguments
  303. * @num_param: Number of elements in @param
  304. * @param: Parameters for Trusted Application
  305. *
  306. * Returns < 0 on error else see @arg->ret for result.
  307. */
  308. int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
  309. uint num_param, struct tee_param *param);
  310. #endif /* __TEE_H */