optee_msg.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
  2. /*
  3. * Copyright (c) 2015-2021, Linaro Limited
  4. */
  5. #ifndef _OPTEE_MSG_H
  6. #define _OPTEE_MSG_H
  7. #include <linux/bitops.h>
  8. #include <linux/types.h>
  9. /*
  10. * This file defines the OP-TEE message protocol (ABI) used to communicate
  11. * with an instance of OP-TEE running in secure world.
  12. *
  13. * This file is divided into two sections.
  14. * 1. Formatting of messages.
  15. * 2. Requests from normal world
  16. */
  17. /*****************************************************************************
  18. * Part 1 - formatting of messages
  19. *****************************************************************************/
  20. #define OPTEE_MSG_ATTR_TYPE_NONE 0x0
  21. #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT 0x1
  22. #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT 0x2
  23. #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT 0x3
  24. #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT 0x5
  25. #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT 0x6
  26. #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT 0x7
  27. #define OPTEE_MSG_ATTR_TYPE_FMEM_INPUT OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
  28. #define OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT
  29. #define OPTEE_MSG_ATTR_TYPE_FMEM_INOUT OPTEE_MSG_ATTR_TYPE_RMEM_INOUT
  30. #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT 0x9
  31. #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT 0xa
  32. #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT 0xb
  33. #define OPTEE_MSG_ATTR_TYPE_MASK GENMASK(7, 0)
  34. /*
  35. * Meta parameter to be absorbed by the Secure OS and not passed
  36. * to the Trusted Application.
  37. *
  38. * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
  39. */
  40. #define OPTEE_MSG_ATTR_META BIT(8)
  41. /*
  42. * Pointer to a list of pages used to register user-defined SHM buffer.
  43. * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
  44. * buf_ptr should point to the beginning of the buffer. Buffer will contain
  45. * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
  46. * that page addresses list. Page addresses are stored as 64 bit values.
  47. * Last entry on a page should point to the next page of buffer.
  48. * Every entry in buffer should point to a 4k page beginning (12 least
  49. * significant bits must be equal to zero).
  50. *
  51. * 12 least significant bits of optee_msg_param.u.tmem.buf_ptr should hold
  52. * page offset of user buffer.
  53. *
  54. * So, entries should be placed like members of this structure:
  55. *
  56. * struct page_data {
  57. * uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
  58. * uint64_t next_page_data;
  59. * };
  60. *
  61. * Structure is designed to exactly fit into the page size
  62. * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
  63. *
  64. * The size of 4KB is chosen because this is the smallest page size for ARM
  65. * architectures. If REE uses larger pages, it should divide them to 4KB ones.
  66. */
  67. #define OPTEE_MSG_ATTR_NONCONTIG BIT(9)
  68. /*
  69. * Memory attributes for caching passed with temp memrefs. The actual value
  70. * used is defined outside the message protocol with the exception of
  71. * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
  72. * defined for the memory range should be used. If optee_smc.h is used as
  73. * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
  74. */
  75. #define OPTEE_MSG_ATTR_CACHE_SHIFT 16
  76. #define OPTEE_MSG_ATTR_CACHE_MASK GENMASK(2, 0)
  77. #define OPTEE_MSG_ATTR_CACHE_PREDEFINED 0
  78. /*
  79. * Same values as TEE_LOGIN_* from TEE Internal API
  80. */
  81. #define OPTEE_MSG_LOGIN_PUBLIC 0x00000000
  82. #define OPTEE_MSG_LOGIN_USER 0x00000001
  83. #define OPTEE_MSG_LOGIN_GROUP 0x00000002
  84. #define OPTEE_MSG_LOGIN_APPLICATION 0x00000004
  85. #define OPTEE_MSG_LOGIN_APPLICATION_USER 0x00000005
  86. #define OPTEE_MSG_LOGIN_APPLICATION_GROUP 0x00000006
  87. /*
  88. * Page size used in non-contiguous buffer entries
  89. */
  90. #define OPTEE_MSG_NONCONTIG_PAGE_SIZE 4096
  91. #define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID 0xffffffffffffffff
  92. /**
  93. * struct optee_msg_param_tmem - temporary memory reference parameter
  94. * @buf_ptr: Address of the buffer
  95. * @size: Size of the buffer
  96. * @shm_ref: Temporary shared memory reference, pointer to a struct tee_shm
  97. *
  98. * Secure and normal world communicates pointers as physical address
  99. * instead of the virtual address. This is because secure and normal world
  100. * have completely independent memory mapping. Normal world can even have a
  101. * hypervisor which need to translate the guest physical address (AKA IPA
  102. * in ARM documentation) to a real physical address before passing the
  103. * structure to secure world.
  104. */
  105. struct optee_msg_param_tmem {
  106. u64 buf_ptr;
  107. u64 size;
  108. u64 shm_ref;
  109. };
  110. /**
  111. * struct optee_msg_param_rmem - registered memory reference parameter
  112. * @offs: Offset into shared memory reference
  113. * @size: Size of the buffer
  114. * @shm_ref: Shared memory reference, pointer to a struct tee_shm
  115. */
  116. struct optee_msg_param_rmem {
  117. u64 offs;
  118. u64 size;
  119. u64 shm_ref;
  120. };
  121. /**
  122. * struct optee_msg_param_fmem - FF-A memory reference parameter
  123. * @offs_lower: Lower bits of offset into shared memory reference
  124. * @offs_upper: Upper bits of offset into shared memory reference
  125. * @internal_offs: Internal offset into the first page of shared memory
  126. * reference
  127. * @size: Size of the buffer
  128. * @global_id: Global identifier of the shared memory
  129. */
  130. struct optee_msg_param_fmem {
  131. u32 offs_low;
  132. u16 offs_high;
  133. u16 internal_offs;
  134. u64 size;
  135. u64 global_id;
  136. };
  137. /**
  138. * struct optee_msg_param_value - opaque value parameter
  139. *
  140. * Value parameters are passed unchecked between normal and secure world.
  141. */
  142. struct optee_msg_param_value {
  143. u64 a;
  144. u64 b;
  145. u64 c;
  146. };
  147. /**
  148. * struct optee_msg_param - parameter used together with struct optee_msg_arg
  149. * @attr: attributes
  150. * @tmem: parameter by temporary memory reference
  151. * @rmem: parameter by registered memory reference
  152. * @fmem: parameter by FF-A registered memory reference
  153. * @value: parameter by opaque value
  154. * @octets: parameter by octet string
  155. *
  156. * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
  157. * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,
  158. * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
  159. * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates
  160. * @rmem or @fmem depending on the conduit.
  161. * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
  162. */
  163. struct optee_msg_param {
  164. u64 attr;
  165. union {
  166. struct optee_msg_param_tmem tmem;
  167. struct optee_msg_param_rmem rmem;
  168. struct optee_msg_param_fmem fmem;
  169. struct optee_msg_param_value value;
  170. u8 octets[24];
  171. } u;
  172. };
  173. /**
  174. * struct optee_msg_arg - call argument
  175. * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
  176. * @func: Trusted Application function, specific to the Trusted Application,
  177. * used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
  178. * @session: In parameter for all OPTEE_MSG_CMD_* except
  179. * OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
  180. * @cancel_id: Cancellation id, a unique value to identify this request
  181. * @ret: return value
  182. * @ret_origin: origin of the return value
  183. * @num_params: number of parameters supplied to the OS Command
  184. * @params: the parameters supplied to the OS Command
  185. *
  186. * All normal calls to Trusted OS uses this struct. If cmd requires further
  187. * information than what these fields hold it can be passed as a parameter
  188. * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
  189. * attrs field). All parameters tagged as meta have to come first.
  190. */
  191. struct optee_msg_arg {
  192. u32 cmd;
  193. u32 func;
  194. u32 session;
  195. u32 cancel_id;
  196. u32 pad;
  197. u32 ret;
  198. u32 ret_origin;
  199. u32 num_params;
  200. /* num_params tells the actual number of element in params */
  201. struct optee_msg_param params[];
  202. };
  203. /**
  204. * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
  205. *
  206. * @num_params: Number of parameters embedded in the struct optee_msg_arg
  207. *
  208. * Returns the size of the struct optee_msg_arg together with the number
  209. * of embedded parameters.
  210. */
  211. #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
  212. (sizeof(struct optee_msg_arg) + \
  213. sizeof(struct optee_msg_param) * (num_params))
  214. /*****************************************************************************
  215. * Part 2 - requests from normal world
  216. *****************************************************************************/
  217. /*
  218. * Return the following UID if using API specified in this file without
  219. * further extensions:
  220. * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
  221. * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
  222. * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
  223. *
  224. * In the case where the OP-TEE image is loaded by the kernel, this will
  225. * initially return an alternate UID to reflect that we are communicating with
  226. * the TF-A image loading service at that time instead of OP-TEE. That UID is:
  227. * a3fbeab1-1246-315d-c7c4-06b9c03cbea4.
  228. * Represented in 4 32-bit words in OPTEE_MSG_IMAGE_LOAD_UID_0,
  229. * OPTEE_MSG_IMAGE_LOAD_UID_1, OPTEE_MSG_IMAGE_LOAD_UID_2,
  230. * OPTEE_MSG_IMAGE_LOAD_UID_3.
  231. */
  232. #define OPTEE_MSG_UID_0 0x384fb3e0
  233. #define OPTEE_MSG_UID_1 0xe7f811e3
  234. #define OPTEE_MSG_UID_2 0xaf630002
  235. #define OPTEE_MSG_UID_3 0xa5d5c51b
  236. #define OPTEE_MSG_IMAGE_LOAD_UID_0 0xa3fbeab1
  237. #define OPTEE_MSG_IMAGE_LOAD_UID_1 0x1246315d
  238. #define OPTEE_MSG_IMAGE_LOAD_UID_2 0xc7c406b9
  239. #define OPTEE_MSG_IMAGE_LOAD_UID_3 0xc03cbea4
  240. #define OPTEE_MSG_FUNCID_CALLS_UID 0xFF01
  241. /*
  242. * Returns 2.0 if using API specified in this file without further
  243. * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
  244. * and OPTEE_MSG_REVISION_MINOR
  245. */
  246. #define OPTEE_MSG_REVISION_MAJOR 2
  247. #define OPTEE_MSG_REVISION_MINOR 0
  248. #define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03
  249. /*
  250. * Get UUID of Trusted OS.
  251. *
  252. * Used by non-secure world to figure out which Trusted OS is installed.
  253. * Note that returned UUID is the UUID of the Trusted OS, not of the API.
  254. *
  255. * Returns UUID in 4 32-bit words in the same way as
  256. * OPTEE_MSG_FUNCID_CALLS_UID described above.
  257. */
  258. #define OPTEE_MSG_OS_OPTEE_UUID_0 0x486178e0
  259. #define OPTEE_MSG_OS_OPTEE_UUID_1 0xe7f811e3
  260. #define OPTEE_MSG_OS_OPTEE_UUID_2 0xbc5e0002
  261. #define OPTEE_MSG_OS_OPTEE_UUID_3 0xa5d5c51b
  262. #define OPTEE_MSG_FUNCID_GET_OS_UUID 0x0000
  263. /*
  264. * Get revision of Trusted OS.
  265. *
  266. * Used by non-secure world to figure out which version of the Trusted OS
  267. * is installed. Note that the returned revision is the revision of the
  268. * Trusted OS, not of the API.
  269. *
  270. * Returns revision in 2 32-bit words in the same way as
  271. * OPTEE_MSG_CALLS_REVISION described above.
  272. */
  273. #define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001
  274. /*
  275. * Values used in OPTEE_MSG_CMD_LEND_PROTMEM below
  276. * OPTEE_MSG_PROTMEM_RESERVED Reserved
  277. * OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY Secure Video Playback
  278. * OPTEE_MSG_PROTMEM_TRUSTED_UI Trused UI
  279. * OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD Secure Video Recording
  280. */
  281. #define OPTEE_MSG_PROTMEM_RESERVED 0
  282. #define OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY 1
  283. #define OPTEE_MSG_PROTMEM_TRUSTED_UI 2
  284. #define OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD 3
  285. /*
  286. * Do a secure call with struct optee_msg_arg as argument
  287. * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
  288. *
  289. * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
  290. * The first two parameters are tagged as meta, holding two value
  291. * parameters to pass the following information:
  292. * param[0].u.value.a-b uuid of Trusted Application
  293. * param[1].u.value.a-b uuid of Client
  294. * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
  295. *
  296. * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
  297. * session to a Trusted Application. struct optee_msg_arg::func is Trusted
  298. * Application function, specific to the Trusted Application.
  299. *
  300. * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
  301. * Trusted Application.
  302. *
  303. * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
  304. *
  305. * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
  306. * information is passed as:
  307. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
  308. * [| OPTEE_MSG_ATTR_NONCONTIG]
  309. * [in] param[0].u.tmem.buf_ptr physical address (of first fragment)
  310. * [in] param[0].u.tmem.size size (of first fragment)
  311. * [in] param[0].u.tmem.shm_ref holds shared memory reference
  312. *
  313. * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared
  314. * memory reference. The information is passed as:
  315. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
  316. * [in] param[0].u.rmem.shm_ref holds shared memory reference
  317. * [in] param[0].u.rmem.offs 0
  318. * [in] param[0].u.rmem.size 0
  319. *
  320. * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing
  321. * of a driver.
  322. *
  323. * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is
  324. * normal world unable to process asynchronous notifications. Typically
  325. * used when the driver is shut down.
  326. *
  327. * OPTEE_MSG_CMD_LEND_PROTMEM lends protected memory. The passed normal
  328. * physical memory is protected from normal world access. The memory
  329. * should be unmapped prior to this call since it becomes inaccessible
  330. * during the request.
  331. * Parameters are passed as:
  332. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
  333. * [in] param[0].u.value.a OPTEE_MSG_PROTMEM_* defined above
  334. * [in] param[1].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
  335. * [in] param[1].u.tmem.buf_ptr physical address
  336. * [in] param[1].u.tmem.size size
  337. * [in] param[1].u.tmem.shm_ref holds protected memory reference
  338. *
  339. * OPTEE_MSG_CMD_RECLAIM_PROTMEM reclaims a previously lent protected
  340. * memory reference. The physical memory is accessible by the normal world
  341. * after this function has return and can be mapped again. The information
  342. * is passed as:
  343. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
  344. * [in] param[0].u.value.a holds protected memory cookie
  345. *
  346. * OPTEE_MSG_CMD_GET_PROTMEM_CONFIG get configuration for a specific
  347. * protected memory use case. Parameters are passed as:
  348. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INOUT
  349. * [in] param[0].value.a OPTEE_MSG_PROTMEM_*
  350. * [in] param[1].attr OPTEE_MSG_ATTR_TYPE_{R,F}MEM_OUTPUT
  351. * [in] param[1].u.{r,f}mem Buffer or NULL
  352. * [in] param[1].u.{r,f}mem.size Provided size of buffer or 0 for query
  353. * output for the protected use case:
  354. * [out] param[0].value.a Minimal size of protected memory
  355. * [out] param[0].value.b Required alignment of size and start of
  356. * protected memory
  357. * [out] param[0].value.c PA width, max 64
  358. * [out] param[1].{r,f}mem.size Size of output data
  359. * [out] param[1].{r,f}mem If non-NULL, contains an array of
  360. * uint32_t memory attributes that must be
  361. * included when lending memory for this
  362. * use case
  363. *
  364. * OPTEE_MSG_CMD_ASSIGN_PROTMEM assigns use-case to protected memory
  365. * previously lent using the FFA_LEND framework ABI. Parameters are passed
  366. * as:
  367. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
  368. * [in] param[0].u.value.a holds protected memory cookie
  369. * [in] param[0].u.value.b OPTEE_MSG_PROTMEM_* defined above
  370. */
  371. #define OPTEE_MSG_CMD_OPEN_SESSION 0
  372. #define OPTEE_MSG_CMD_INVOKE_COMMAND 1
  373. #define OPTEE_MSG_CMD_CLOSE_SESSION 2
  374. #define OPTEE_MSG_CMD_CANCEL 3
  375. #define OPTEE_MSG_CMD_REGISTER_SHM 4
  376. #define OPTEE_MSG_CMD_UNREGISTER_SHM 5
  377. #define OPTEE_MSG_CMD_DO_BOTTOM_HALF 6
  378. #define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF 7
  379. #define OPTEE_MSG_CMD_LEND_PROTMEM 8
  380. #define OPTEE_MSG_CMD_RECLAIM_PROTMEM 9
  381. #define OPTEE_MSG_CMD_GET_PROTMEM_CONFIG 10
  382. #define OPTEE_MSG_CMD_ASSIGN_PROTMEM 11
  383. #define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004
  384. #endif /* _OPTEE_MSG_H */