tee.h 11 KB

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