tee.h 11 KB

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