tee.h 11 KB

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