tee.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_optee_ta_uuid - OP-TEE Trusted Application (TA) UUID format
  46. *
  47. * Used to identify an OP-TEE TA and define suitable to initialize structs
  48. * of this format is distributed with the interface of the TA. The
  49. * individual fields of this struct doesn't have any special meaning in
  50. * OP-TEE. See RFC4122 for details on the format.
  51. */
  52. struct tee_optee_ta_uuid {
  53. u32 time_low;
  54. u16 time_mid;
  55. u16 time_hi_and_version;
  56. u8 clock_seq_and_node[8];
  57. };
  58. /**
  59. * struct tee_shm - memory shared with the TEE
  60. * @dev: The TEE device
  61. * @link: List node in the list in struct struct tee_uclass_priv
  62. * @addr: Pointer to the shared memory
  63. * @size: Size of the the shared memory
  64. * @flags: TEE_SHM_* above
  65. */
  66. struct tee_shm {
  67. struct udevice *dev;
  68. struct list_head link;
  69. void *addr;
  70. ulong size;
  71. u32 flags;
  72. };
  73. /**
  74. * struct tee_param_memref - memory reference for a Trusted Application
  75. * @shm_offs: Offset in bytes into the shared memory object @shm
  76. * @size: Size in bytes of the memory reference
  77. * @shm: Pointer to a shared memory object for the buffer
  78. *
  79. * Used as a part of struct tee_param, see that for more information.
  80. */
  81. struct tee_param_memref {
  82. ulong shm_offs;
  83. ulong size;
  84. struct tee_shm *shm;
  85. };
  86. /**
  87. * struct tee_param_value - value parameter for a Trusted Application
  88. * @a, @b, @c: Parameters passed by value
  89. *
  90. * Used as a part of struct tee_param, see that for more information.
  91. */
  92. struct tee_param_value {
  93. u64 a;
  94. u64 b;
  95. u64 c;
  96. };
  97. /**
  98. * struct tee_param - invoke parameter for a Trusted Application
  99. * @attr: Attributes
  100. * @u.memref: Memref parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
  101. * TEE_PARAM_ATTR_TYPE_MEMREF_* above
  102. * @u.value: Value parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
  103. * TEE_PARAM_ATTR_TYPE_VALUE_* above
  104. *
  105. * Parameters to TA are passed using an array of this struct, for
  106. * flexibility both value parameters and memory refereces can be used.
  107. */
  108. struct tee_param {
  109. u64 attr;
  110. union {
  111. struct tee_param_memref memref;
  112. struct tee_param_value value;
  113. } u;
  114. };
  115. /**
  116. * struct tee_open_session_arg - extra arguments for tee_open_session()
  117. * @uuid: [in] UUID of the Trusted Application
  118. * @clnt_uuid: [in] Normally zeroes
  119. * @clnt_login: [in] Normally 0
  120. * @session: [out] Session id
  121. * @ret: [out] return value
  122. * @ret_origin: [out] origin of the return value
  123. */
  124. struct tee_open_session_arg {
  125. u8 uuid[TEE_UUID_LEN];
  126. u8 clnt_uuid[TEE_UUID_LEN];
  127. u32 clnt_login;
  128. u32 session;
  129. u32 ret;
  130. u32 ret_origin;
  131. };
  132. /**
  133. * struct tee_invoke_arg - extra arguments for tee_invoke_func()
  134. * @func: [in] Trusted Application function, specific to the TA
  135. * @session: [in] Session id, from open session
  136. * @ret: [out] return value
  137. * @ret_origin: [out] origin of the return value
  138. */
  139. struct tee_invoke_arg {
  140. u32 func;
  141. u32 session;
  142. u32 ret;
  143. u32 ret_origin;
  144. };
  145. /**
  146. * struct tee_version_data - description of TEE
  147. * @gen_caps: Generic capabilities, TEE_GEN_CAP_* above
  148. */
  149. struct tee_version_data {
  150. u32 gen_caps;
  151. };
  152. /**
  153. * struct tee_driver_ops - TEE driver operations
  154. * @get_version: Query capabilities of TEE device,
  155. * @open_session: Opens a session to a Trusted Application in the TEE,
  156. * @close_session: Closes a session to Trusted Application,
  157. * @invoke_func: Invokes a function in a Trusted Application,
  158. * @shm_register: Registers memory shared with the TEE
  159. * @shm_unregister: Unregisters memory shared with the TEE
  160. */
  161. struct tee_driver_ops {
  162. /**
  163. * get_version() - Query capabilities of TEE device
  164. * @dev: The TEE device
  165. * @vers: Pointer to version data
  166. */
  167. void (*get_version)(struct udevice *dev, struct tee_version_data *vers);
  168. /**
  169. * open_session() - Open a session to a Trusted Application
  170. * @dev: The TEE device
  171. * @arg: Open session arguments
  172. * @num_param: Number of elements in @param
  173. * @param: Parameters for Trusted Application
  174. *
  175. * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
  176. * TEE_SUCCESS the session identifier is available in @arg->session.
  177. */
  178. int (*open_session)(struct udevice *dev,
  179. struct tee_open_session_arg *arg, uint num_param,
  180. struct tee_param *param);
  181. /**
  182. * close_session() - Close a session to a Trusted Application
  183. * @dev: The TEE device
  184. * @session: Session id
  185. *
  186. * Return < 0 on error else 0, regardless the session will not be valid
  187. * after this function has returned.
  188. */
  189. int (*close_session)(struct udevice *dev, u32 session);
  190. /**
  191. * tee_invoke_func() - Invoke a function in a Trusted Application
  192. * @dev: The TEE device
  193. * @arg: Invoke arguments
  194. * @num_param: Number of elements in @param
  195. * @param: Parameters for Trusted Application
  196. *
  197. * Returns < 0 on error else see @arg->ret for result.
  198. */
  199. int (*invoke_func)(struct udevice *dev, struct tee_invoke_arg *arg,
  200. uint num_param, struct tee_param *param);
  201. /**
  202. * shm_register() - Registers memory shared with the TEE
  203. * @dev: The TEE device
  204. * @shm: Pointer to a shared memory object
  205. * Returns 0 on success or < 0 on failure.
  206. */
  207. int (*shm_register)(struct udevice *dev, struct tee_shm *shm);
  208. /**
  209. * shm_unregister() - Unregisters memory shared with the TEE
  210. * @dev: The TEE device
  211. * @shm: Pointer to a shared memory object
  212. * Returns 0 on success or < 0 on failure.
  213. */
  214. int (*shm_unregister)(struct udevice *dev, struct tee_shm *shm);
  215. };
  216. /**
  217. * __tee_shm_add() - Internal helper function to register shared memory
  218. * @dev: The TEE device
  219. * @align: Required alignment of allocated memory block if
  220. * (@flags & TEE_SHM_ALLOC)
  221. * @addr: Address of memory block, ignored if (@flags & TEE_SHM_ALLOC)
  222. * @size: Size of memory block
  223. * @flags: TEE_SHM_* above
  224. * @shmp: If the function return 0, this holds the allocated
  225. * struct tee_shm
  226. *
  227. * returns 0 on success or < 0 on failure.
  228. */
  229. int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
  230. u32 flags, struct tee_shm **shmp);
  231. /**
  232. * tee_shm_alloc() - Allocate shared memory
  233. * @dev: The TEE device
  234. * @size: Size of memory block
  235. * @flags: TEE_SHM_* above
  236. * @shmp: If the function return 0, this holds the allocated
  237. * struct tee_shm
  238. *
  239. * returns 0 on success or < 0 on failure.
  240. */
  241. int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
  242. struct tee_shm **shmp);
  243. /**
  244. * tee_shm_register() - Registers shared memory
  245. * @dev: The TEE device
  246. * @addr: Address of memory block
  247. * @size: Size of memory block
  248. * @flags: TEE_SHM_* above
  249. * @shmp: If the function return 0, this holds the allocated
  250. * struct tee_shm
  251. *
  252. * returns 0 on success or < 0 on failure.
  253. */
  254. int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
  255. struct tee_shm **shmp);
  256. /**
  257. * tee_shm_free() - Frees shared memory
  258. * @shm: Shared memory object
  259. */
  260. void tee_shm_free(struct tee_shm *shm);
  261. /**
  262. * tee_shm_is_registered() - Check register status of shared memory object
  263. * @shm: Pointer to shared memory object
  264. * @dev: The TEE device
  265. *
  266. * Returns true if the shared memory object is registered for the supplied
  267. * TEE device
  268. */
  269. bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev);
  270. /**
  271. * tee_find_device() - Look up a TEE device
  272. * @start: if not NULL, continue search after this device
  273. * @match: function to check TEE device, returns != 0 if the device
  274. * matches
  275. * @data: data for match function
  276. * @vers: if not NULL, version data of TEE device of the device returned
  277. *
  278. * Returns a probed TEE device of the first TEE device matched by the
  279. * match() callback or NULL.
  280. */
  281. struct udevice *tee_find_device(struct udevice *start,
  282. int (*match)(struct tee_version_data *vers,
  283. const void *data),
  284. const void *data,
  285. struct tee_version_data *vers);
  286. /**
  287. * tee_get_version() - Query capabilities of TEE device
  288. * @dev: The TEE device
  289. * @vers: Pointer to version data
  290. */
  291. void tee_get_version(struct udevice *dev, struct tee_version_data *vers);
  292. /**
  293. * tee_open_session() - Open a session to a Trusted Application
  294. * @dev: The TEE device
  295. * @arg: Open session arguments
  296. * @num_param: Number of elements in @param
  297. * @param: Parameters for Trusted Application
  298. *
  299. * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
  300. * TEE_SUCCESS the session identifier is available in @arg->session.
  301. */
  302. int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
  303. uint num_param, struct tee_param *param);
  304. /**
  305. * tee_close_session() - Close a session to a Trusted Application
  306. * @dev: The TEE device
  307. * @session: Session id
  308. *
  309. * Return < 0 on error else 0, regardless the session will not be valid
  310. * after this function has returned.
  311. */
  312. int tee_close_session(struct udevice *dev, u32 session);
  313. /**
  314. * tee_invoke_func() - Invoke a function in a Trusted Application
  315. * @dev: The TEE device
  316. * @arg: Invoke arguments
  317. * @num_param: Number of elements in @param
  318. * @param: Parameters for Trusted Application
  319. *
  320. * Returns < 0 on error else see @arg->ret for result.
  321. */
  322. int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
  323. uint num_param, struct tee_param *param);
  324. /**
  325. * tee_optee_ta_uuid_from_octets() - Converts to struct tee_optee_ta_uuid
  326. * @d: Destination struct
  327. * @s: Source UUID octets
  328. *
  329. * Conversion to a struct tee_optee_ta_uuid represantion from binary octet
  330. * representation.
  331. */
  332. void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
  333. const u8 s[TEE_UUID_LEN]);
  334. /**
  335. * tee_optee_ta_uuid_to_octets() - Converts from struct tee_optee_ta_uuid
  336. * @d: Destination UUID octets
  337. * @s: Source struct
  338. *
  339. * Conversion from a struct tee_optee_ta_uuid represantion to binary octet
  340. * representation.
  341. */
  342. void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
  343. const struct tee_optee_ta_uuid *s);
  344. #endif /* __TEE_H */