optee_msg.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*
  3. * Copyright (c) 2015-2018, 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 used to communicate with
  11. * an instance of OP-TEE running in secure world. This file is based on
  12. * https://github.com/OP-TEE/optee_os/blob/master/core/include/optee_msg.h
  13. * and may need to be updated when introducing new features.
  14. *
  15. * This file is divided into three sections.
  16. * 1. Formatting of messages.
  17. * 2. Requests from normal world
  18. * 3. Requests from secure world, Remote Procedure Call (RPC), handled by
  19. * tee-supplicant.
  20. */
  21. /*****************************************************************************
  22. * Part 1 - formatting of messages
  23. *****************************************************************************/
  24. #define OPTEE_MSG_ATTR_TYPE_NONE 0x0
  25. #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT 0x1
  26. #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT 0x2
  27. #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT 0x3
  28. #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT 0x5
  29. #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT 0x6
  30. #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT 0x7
  31. #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT 0x9
  32. #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT 0xa
  33. #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT 0xb
  34. #define OPTEE_MSG_ATTR_TYPE_MASK GENMASK(7, 0)
  35. /*
  36. * Meta parameter to be absorbed by the Secure OS and not passed
  37. * to the Trusted Application.
  38. *
  39. * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
  40. */
  41. #define OPTEE_MSG_ATTR_META BIT(8)
  42. /*
  43. * Pointer to a list of pages used to register user-defined SHM buffer.
  44. * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
  45. * buf_ptr should point to the beginning of the buffer. Buffer will contain
  46. * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
  47. * that page addresses list. Page addresses are stored as 64 bit values.
  48. * Last entry on a page should point to the next page of buffer.
  49. * Every entry in buffer should point to a 4k page beginning (12 least
  50. * significant bits must be equal to zero).
  51. *
  52. * 12 least significant bints of optee_msg_param.u.tmem.buf_ptr should hold page
  53. * offset of the user buffer.
  54. *
  55. * So, entries should be placed like members of this structure:
  56. *
  57. * struct page_data {
  58. * uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
  59. * uint64_t next_page_data;
  60. * };
  61. *
  62. * Structure is designed to exactly fit into the page size
  63. * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
  64. *
  65. * The size of 4KB is chosen because this is the smallest page size for ARM
  66. * architectures. If REE uses larger pages, it should divide them to 4KB ones.
  67. */
  68. #define OPTEE_MSG_ATTR_NONCONTIG BIT(9)
  69. /*
  70. * Memory attributes for caching passed with temp memrefs. The actual value
  71. * used is defined outside the message protocol with the exception of
  72. * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
  73. * defined for the memory range should be used. If optee_smc.h is used as
  74. * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
  75. */
  76. #define OPTEE_MSG_ATTR_CACHE_SHIFT 16
  77. #define OPTEE_MSG_ATTR_CACHE_MASK GENMASK(2, 0)
  78. #define OPTEE_MSG_ATTR_CACHE_PREDEFINED 0
  79. /*
  80. * Page size used in non-contiguous buffer entries
  81. */
  82. #define OPTEE_MSG_NONCONTIG_PAGE_SIZE 4096
  83. /**
  84. * struct optee_msg_param_tmem - temporary memory reference parameter
  85. * @buf_ptr: Address of the buffer
  86. * @size: Size of the buffer
  87. * @shm_ref: Temporary shared memory reference, pointer to a struct tee_shm
  88. *
  89. * Secure and normal world communicates pointers as physical address
  90. * instead of the virtual address. This is because secure and normal world
  91. * have completely independent memory mapping. Normal world can even have a
  92. * hypervisor which need to translate the guest physical address (AKA IPA
  93. * in ARM documentation) to a real physical address before passing the
  94. * structure to secure world.
  95. */
  96. struct optee_msg_param_tmem {
  97. u64 buf_ptr;
  98. u64 size;
  99. u64 shm_ref;
  100. };
  101. /**
  102. * struct optee_msg_param_rmem - registered memory reference parameter
  103. * @offs: Offset into shared memory reference
  104. * @size: Size of the buffer
  105. * @shm_ref: Shared memory reference, pointer to a struct tee_shm
  106. */
  107. struct optee_msg_param_rmem {
  108. u64 offs;
  109. u64 size;
  110. u64 shm_ref;
  111. };
  112. /**
  113. * struct optee_msg_param_value - opaque value parameter
  114. *
  115. * Value parameters are passed unchecked between normal and secure world.
  116. */
  117. struct optee_msg_param_value {
  118. u64 a;
  119. u64 b;
  120. u64 c;
  121. };
  122. /**
  123. * struct optee_msg_param - parameter used together with struct optee_msg_arg
  124. * @attr: attributes
  125. * @tmem: parameter by temporary memory reference
  126. * @rmem: parameter by registered memory reference
  127. * @value: parameter by opaque value
  128. *
  129. * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
  130. * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value,
  131. * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
  132. * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates @rmem,
  133. * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
  134. */
  135. struct optee_msg_param {
  136. u64 attr;
  137. union {
  138. struct optee_msg_param_tmem tmem;
  139. struct optee_msg_param_rmem rmem;
  140. struct optee_msg_param_value value;
  141. } u;
  142. };
  143. /**
  144. * struct optee_msg_arg - call argument
  145. * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
  146. * @func: Trusted Application function, specific to the Trusted Application,
  147. * used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
  148. * @session: In parameter for all OPTEE_MSG_CMD_* except
  149. * OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
  150. * @cancel_id: Cancellation id, a unique value to identify this request
  151. * @ret: return value
  152. * @ret_origin: origin of the return value
  153. * @num_params: number of parameters supplied to the OS Command
  154. * @params: the parameters supplied to the OS Command
  155. *
  156. * All normal calls to Trusted OS uses this struct. If cmd requires further
  157. * information than what these field holds it can be passed as a parameter
  158. * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
  159. * attrs field). All parameters tagged as meta has to come first.
  160. *
  161. * Temp memref parameters can be fragmented if supported by the Trusted OS
  162. * (when optee_smc.h is bearer of this protocol this is indicated with
  163. * OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM). If a logical memref parameter is
  164. * fragmented then has all but the last fragment the
  165. * OPTEE_MSG_ATTR_FRAGMENT bit set in attrs. Even if a memref is fragmented
  166. * it will still be presented as a single logical memref to the Trusted
  167. * Application.
  168. */
  169. struct optee_msg_arg {
  170. u32 cmd;
  171. u32 func;
  172. u32 session;
  173. u32 cancel_id;
  174. u32 pad;
  175. u32 ret;
  176. u32 ret_origin;
  177. u32 num_params;
  178. /* num_params tells the actual number of element in params */
  179. struct optee_msg_param params[0];
  180. };
  181. /**
  182. * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
  183. *
  184. * @num_params: Number of parameters embedded in the struct optee_msg_arg
  185. *
  186. * Returns the size of the struct optee_msg_arg together with the number
  187. * of embedded parameters.
  188. */
  189. #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
  190. (sizeof(struct optee_msg_arg) + \
  191. sizeof(struct optee_msg_param) * (num_params))
  192. /*****************************************************************************
  193. * Part 2 - requests from normal world
  194. *****************************************************************************/
  195. /*
  196. * Return the following UID if using API specified in this file without
  197. * further extensions:
  198. * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
  199. * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
  200. * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
  201. */
  202. #define OPTEE_MSG_UID_0 0x384fb3e0
  203. #define OPTEE_MSG_UID_1 0xe7f811e3
  204. #define OPTEE_MSG_UID_2 0xaf630002
  205. #define OPTEE_MSG_UID_3 0xa5d5c51b
  206. #define OPTEE_MSG_FUNCID_CALLS_UID 0xFF01
  207. /*
  208. * Returns 2.0 if using API specified in this file without further
  209. * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
  210. * and OPTEE_MSG_REVISION_MINOR
  211. */
  212. #define OPTEE_MSG_REVISION_MAJOR 2
  213. #define OPTEE_MSG_REVISION_MINOR 0
  214. #define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03
  215. /*
  216. * Get UUID of Trusted OS.
  217. *
  218. * Used by non-secure world to figure out which Trusted OS is installed.
  219. * Note that returned UUID is the UUID of the Trusted OS, not of the API.
  220. *
  221. * Returns UUID in 4 32-bit words in the same way as
  222. * OPTEE_MSG_FUNCID_CALLS_UID described above.
  223. */
  224. #define OPTEE_MSG_OS_OPTEE_UUID_0 0x486178e0
  225. #define OPTEE_MSG_OS_OPTEE_UUID_1 0xe7f811e3
  226. #define OPTEE_MSG_OS_OPTEE_UUID_2 0xbc5e0002
  227. #define OPTEE_MSG_OS_OPTEE_UUID_3 0xa5d5c51b
  228. #define OPTEE_MSG_FUNCID_GET_OS_UUID 0x0000
  229. /*
  230. * Get revision of Trusted OS.
  231. *
  232. * Used by non-secure world to figure out which version of the Trusted OS
  233. * is installed. Note that the returned revision is the revision of the
  234. * Trusted OS, not of the API.
  235. *
  236. * Returns revision in 2 32-bit words in the same way as
  237. * OPTEE_MSG_CALLS_REVISION described above.
  238. */
  239. #define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001
  240. /*
  241. * Do a secure call with struct optee_msg_arg as argument
  242. * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
  243. *
  244. * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
  245. * The first two parameters are tagged as meta, holding two value
  246. * parameters to pass the following information:
  247. * param[0].u.value.a-b uuid of Trusted Application
  248. * param[1].u.value.a-b uuid of Client
  249. * param[1].u.value.c Login class of client TEE_LOGIN_*
  250. *
  251. * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
  252. * session to a Trusted Application. struct optee_msg_arg::func is Trusted
  253. * Application function, specific to the Trusted Application.
  254. *
  255. * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
  256. * Trusted Application.
  257. *
  258. * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
  259. *
  260. * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
  261. * information is passed as:
  262. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
  263. * [| OPTEE_MSG_ATTR_FRAGMENT]
  264. * [in] param[0].u.tmem.buf_ptr physical address (of first fragment)
  265. * [in] param[0].u.tmem.size size (of first fragment)
  266. * [in] param[0].u.tmem.shm_ref holds shared memory reference
  267. * ...
  268. * The shared memory can optionally be fragmented, temp memrefs can follow
  269. * each other with all but the last with the OPTEE_MSG_ATTR_FRAGMENT bit set.
  270. *
  271. * OPTEE_MSG_CMD_UNREGISTER_SHM unregisteres a previously registered shared
  272. * memory reference. The information is passed as:
  273. * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
  274. * [in] param[0].u.rmem.shm_ref holds shared memory reference
  275. * [in] param[0].u.rmem.offs 0
  276. * [in] param[0].u.rmem.size 0
  277. */
  278. #define OPTEE_MSG_CMD_OPEN_SESSION 0
  279. #define OPTEE_MSG_CMD_INVOKE_COMMAND 1
  280. #define OPTEE_MSG_CMD_CLOSE_SESSION 2
  281. #define OPTEE_MSG_CMD_CANCEL 3
  282. #define OPTEE_MSG_CMD_REGISTER_SHM 4
  283. #define OPTEE_MSG_CMD_UNREGISTER_SHM 5
  284. #define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004
  285. /*****************************************************************************
  286. * Part 3 - Requests from secure world, RPC
  287. *****************************************************************************/
  288. /*
  289. * All RPC is done with a struct optee_msg_arg as bearer of information,
  290. * struct optee_msg_arg::arg holds values defined by OPTEE_MSG_RPC_CMD_* below
  291. *
  292. * RPC communication with tee-supplicant is reversed compared to normal
  293. * client communication desribed above. The supplicant receives requests
  294. * and sends responses.
  295. */
  296. /*
  297. * Load a TA into memory, defined in tee-supplicant
  298. */
  299. #define OPTEE_MSG_RPC_CMD_LOAD_TA 0
  300. /*
  301. * Reserved
  302. */
  303. #define OPTEE_MSG_RPC_CMD_RPMB 1
  304. /*
  305. * File system access, defined in tee-supplicant
  306. */
  307. #define OPTEE_MSG_RPC_CMD_FS 2
  308. /*
  309. * Get time
  310. *
  311. * Returns number of seconds and nano seconds since the Epoch,
  312. * 1970-01-01 00:00:00 +0000 (UTC).
  313. *
  314. * [out] param[0].u.value.a Number of seconds
  315. * [out] param[0].u.value.b Number of nano seconds.
  316. */
  317. #define OPTEE_MSG_RPC_CMD_GET_TIME 3
  318. /*
  319. * Wait queue primitive, helper for secure world to implement a wait queue.
  320. *
  321. * If secure world need to wait for a secure world mutex it issues a sleep
  322. * request instead of spinning in secure world. Conversely is a wakeup
  323. * request issued when a secure world mutex with a thread waiting thread is
  324. * unlocked.
  325. *
  326. * Waiting on a key
  327. * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
  328. * [in] param[0].u.value.b wait key
  329. *
  330. * Waking up a key
  331. * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
  332. * [in] param[0].u.value.b wakeup key
  333. */
  334. #define OPTEE_MSG_RPC_CMD_WAIT_QUEUE 4
  335. #define OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP 0
  336. #define OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP 1
  337. /*
  338. * Suspend execution
  339. *
  340. * [in] param[0].value .a number of milliseconds to suspend
  341. */
  342. #define OPTEE_MSG_RPC_CMD_SUSPEND 5
  343. /*
  344. * Allocate a piece of shared memory
  345. *
  346. * Shared memory can optionally be fragmented, to support that additional
  347. * spare param entries are allocated to make room for eventual fragments.
  348. * The spare param entries has .attr = OPTEE_MSG_ATTR_TYPE_NONE when
  349. * unused. All returned temp memrefs except the last should have the
  350. * OPTEE_MSG_ATTR_FRAGMENT bit set in the attr field.
  351. *
  352. * [in] param[0].u.value.a type of memory one of
  353. * OPTEE_MSG_RPC_SHM_TYPE_* below
  354. * [in] param[0].u.value.b requested size
  355. * [in] param[0].u.value.c required alignment
  356. *
  357. * [out] param[0].u.tmem.buf_ptr physical address (of first fragment)
  358. * [out] param[0].u.tmem.size size (of first fragment)
  359. * [out] param[0].u.tmem.shm_ref shared memory reference
  360. * ...
  361. * [out] param[n].u.tmem.buf_ptr physical address
  362. * [out] param[n].u.tmem.size size
  363. * [out] param[n].u.tmem.shm_ref shared memory reference (same value
  364. * as in param[n-1].u.tmem.shm_ref)
  365. */
  366. #define OPTEE_MSG_RPC_CMD_SHM_ALLOC 6
  367. /* Memory that can be shared with a non-secure user space application */
  368. #define OPTEE_MSG_RPC_SHM_TYPE_APPL 0
  369. /* Memory only shared with non-secure kernel */
  370. #define OPTEE_MSG_RPC_SHM_TYPE_KERNEL 1
  371. /*
  372. * Free shared memory previously allocated with OPTEE_MSG_RPC_CMD_SHM_ALLOC
  373. *
  374. * [in] param[0].u.value.a type of memory one of
  375. * OPTEE_MSG_RPC_SHM_TYPE_* above
  376. * [in] param[0].u.value.b value of shared memory reference
  377. * returned in param[0].u.tmem.shm_ref
  378. * above
  379. */
  380. #define OPTEE_MSG_RPC_CMD_SHM_FREE 7
  381. /*
  382. * Access a device on an i2c bus
  383. *
  384. * [in] param[0].u.value.a mode: RD(0), WR(1)
  385. * [in] param[0].u.value.b i2c adapter
  386. * [in] param[0].u.value.c i2c chip
  387. *
  388. * [in] param[1].u.value.a i2c control flags
  389. *
  390. * [in/out] memref[2] buffer to exchange the transfer data
  391. * with the secure world
  392. *
  393. * [out] param[3].u.value.a bytes transferred by the driver
  394. */
  395. #define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21
  396. /* I2C master transfer modes */
  397. #define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD 0
  398. #define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR 1
  399. /* I2C master control flags */
  400. #define OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT BIT(0)
  401. #endif /* _OPTEE_MSG_H */