optee_smc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*
  3. * Copyright (c) 2015-2018, Linaro Limited
  4. */
  5. #ifndef OPTEE_SMC_H
  6. #define OPTEE_SMC_H
  7. #include <linux/arm-smccc.h>
  8. #include <linux/bitops.h>
  9. /*
  10. * This file is based on
  11. * https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/sm/optee_smc.h
  12. * and may need to be updated when introducing new features.
  13. */
  14. #define OPTEE_SMC_STD_CALL_VAL(func_num) \
  15. ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL, ARM_SMCCC_SMC_32, \
  16. ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
  17. #define OPTEE_SMC_FAST_CALL_VAL(func_num) \
  18. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
  19. ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
  20. /*
  21. * Function specified by SMC Calling convention.
  22. */
  23. #define OPTEE_SMC_FUNCID_CALLS_COUNT 0xFF00
  24. #define OPTEE_SMC_CALLS_COUNT \
  25. ARM_SMCCC_CALL_VAL(OPTEE_SMC_FAST_CALL, SMCCC_SMC_32, \
  26. SMCCC_OWNER_TRUSTED_OS_END, \
  27. OPTEE_SMC_FUNCID_CALLS_COUNT)
  28. /*
  29. * Normal cached memory (write-back), shareable for SMP systems and not
  30. * shareable for UP systems.
  31. */
  32. #define OPTEE_SMC_SHM_CACHED 1
  33. /*
  34. * a0..a7 is used as register names in the descriptions below, on arm32
  35. * that translates to r0..r7 and on arm64 to w0..w7. In both cases it's
  36. * 32-bit registers.
  37. */
  38. /*
  39. * Function specified by SMC Calling convention
  40. *
  41. * Return one of the following UIDs if using API specified in this file
  42. * without further extentions:
  43. * 65cb6b93-af0c-4617-8ed6-644a8d1140f8
  44. * see also OPTEE_SMC_UID_* in optee_msg.h
  45. */
  46. #define OPTEE_SMC_FUNCID_CALLS_UID OPTEE_MSG_FUNCID_CALLS_UID
  47. #define OPTEE_SMC_CALLS_UID \
  48. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
  49. ARM_SMCCC_OWNER_TRUSTED_OS_END, \
  50. OPTEE_SMC_FUNCID_CALLS_UID)
  51. /*
  52. * Function specified by SMC Calling convention
  53. *
  54. * Returns 2.0 if using API specified in this file without further extentions.
  55. * see also OPTEE_MSG_REVISION_* in optee_msg.h
  56. */
  57. #define OPTEE_SMC_FUNCID_CALLS_REVISION OPTEE_MSG_FUNCID_CALLS_REVISION
  58. #define OPTEE_SMC_CALLS_REVISION \
  59. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
  60. ARM_SMCCC_OWNER_TRUSTED_OS_END, \
  61. OPTEE_SMC_FUNCID_CALLS_REVISION)
  62. struct optee_smc_calls_revision_result {
  63. unsigned long major;
  64. unsigned long minor;
  65. unsigned long reserved0;
  66. unsigned long reserved1;
  67. };
  68. /*
  69. * Get UUID of Trusted OS.
  70. *
  71. * Used by non-secure world to figure out which Trusted OS is installed.
  72. * Note that returned UUID is the UUID of the Trusted OS, not of the API.
  73. *
  74. * Returns UUID in a0-4 in the same way as OPTEE_SMC_CALLS_UID
  75. * described above.
  76. */
  77. #define OPTEE_SMC_FUNCID_GET_OS_UUID OPTEE_MSG_FUNCID_GET_OS_UUID
  78. #define OPTEE_SMC_CALL_GET_OS_UUID \
  79. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_UUID)
  80. /*
  81. * Get revision of Trusted OS.
  82. *
  83. * Used by non-secure world to figure out which version of the Trusted OS
  84. * is installed. Note that the returned revision is the revision of the
  85. * Trusted OS, not of the API.
  86. *
  87. * Returns revision in a0-1 in the same way as OPTEE_SMC_CALLS_REVISION
  88. * described above. May optionally return a 32-bit build identifier in a2,
  89. * with zero meaning unspecified.
  90. */
  91. #define OPTEE_SMC_FUNCID_GET_OS_REVISION OPTEE_MSG_FUNCID_GET_OS_REVISION
  92. #define OPTEE_SMC_CALL_GET_OS_REVISION \
  93. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_REVISION)
  94. struct optee_smc_call_get_os_revision_result {
  95. unsigned long major;
  96. unsigned long minor;
  97. unsigned long build_id;
  98. unsigned long reserved1;
  99. };
  100. /*
  101. * Call with struct optee_msg_arg as argument
  102. *
  103. * Call register usage:
  104. * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
  105. * a1 Upper 32bit of a 64bit physical pointer to a struct optee_msg_arg
  106. * a2 Lower 32bit of a 64bit physical pointer to a struct optee_msg_arg
  107. * a3 Cache settings, not used if physical pointer is in a predefined shared
  108. * memory area else per OPTEE_SMC_SHM_*
  109. * a4-6 Not used
  110. * a7 Hypervisor Client ID register
  111. *
  112. * Normal return register usage:
  113. * a0 Return value, OPTEE_SMC_RETURN_*
  114. * a1-3 Not used
  115. * a4-7 Preserved
  116. *
  117. * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
  118. * a0 Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
  119. * a1-3 Preserved
  120. * a4-7 Preserved
  121. *
  122. * RPC return register usage:
  123. * a0 Return value, OPTEE_SMC_RETURN_IS_RPC(val)
  124. * a1-2 RPC parameters
  125. * a3-7 Resume information, must be preserved
  126. *
  127. * Possible return values:
  128. * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
  129. * function.
  130. * OPTEE_SMC_RETURN_OK Call completed, result updated in
  131. * the previously supplied struct
  132. * optee_msg_arg.
  133. * OPTEE_SMC_RETURN_ETHREAD_LIMIT Number of Trusted OS threads exceeded,
  134. * try again later.
  135. * OPTEE_SMC_RETURN_EBADADDR Bad physcial pointer to struct
  136. * optee_msg_arg.
  137. * OPTEE_SMC_RETURN_EBADCMD Bad/unknown cmd in struct optee_msg_arg
  138. * OPTEE_SMC_RETURN_IS_RPC() Call suspended by RPC call to normal
  139. * world.
  140. */
  141. #define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
  142. #define OPTEE_SMC_CALL_WITH_ARG \
  143. OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
  144. /*
  145. * Get Shared Memory Config
  146. *
  147. * Returns the Secure/Non-secure shared memory config.
  148. *
  149. * Call register usage:
  150. * a0 SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
  151. * a1-6 Not used
  152. * a7 Hypervisor Client ID register
  153. *
  154. * Have config return register usage:
  155. * a0 OPTEE_SMC_RETURN_OK
  156. * a1 Physical address of start of SHM
  157. * a2 Size of of SHM
  158. * a3 Cache settings of memory, as defined by the
  159. * OPTEE_SMC_SHM_* values above
  160. * a4-7 Preserved
  161. *
  162. * Not available register usage:
  163. * a0 OPTEE_SMC_RETURN_ENOTAVAIL
  164. * a1-3 Not used
  165. * a4-7 Preserved
  166. */
  167. #define OPTEE_SMC_FUNCID_GET_SHM_CONFIG 7
  168. #define OPTEE_SMC_GET_SHM_CONFIG \
  169. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
  170. struct optee_smc_get_shm_config_result {
  171. unsigned long status;
  172. unsigned long start;
  173. unsigned long size;
  174. unsigned long settings;
  175. };
  176. /*
  177. * Exchanges capabilities between normal world and secure world
  178. *
  179. * Call register usage:
  180. * a0 SMC Function ID, OPTEE_SMC_EXCHANGE_CAPABILITIES
  181. * a1 bitfield of normal world capabilities OPTEE_SMC_NSEC_CAP_*
  182. * a2-6 Not used
  183. * a7 Hypervisor Client ID register
  184. *
  185. * Normal return register usage:
  186. * a0 OPTEE_SMC_RETURN_OK
  187. * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
  188. * a2-7 Preserved
  189. *
  190. * Error return register usage:
  191. * a0 OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world
  192. * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
  193. * a2-7 Preserved
  194. */
  195. /* Normal world works as a uniprocessor system */
  196. #define OPTEE_SMC_NSEC_CAP_UNIPROCESSOR BIT(0)
  197. /* Secure world has reserved shared memory for normal world to use */
  198. #define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM BIT(0)
  199. /* Secure world can communicate via previously unregistered shared memory */
  200. #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM BIT(1)
  201. /*
  202. * Secure world supports commands "register/unregister shared memory",
  203. * secure world accepts command buffers located in any parts of non-secure RAM
  204. */
  205. #define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM BIT(2)
  206. #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
  207. #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
  208. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
  209. struct optee_smc_exchange_capabilities_result {
  210. unsigned long status;
  211. unsigned long capabilities;
  212. unsigned long reserved0;
  213. unsigned long reserved1;
  214. };
  215. /*
  216. * Disable and empties cache of shared memory objects
  217. *
  218. * Secure world can cache frequently used shared memory objects, for
  219. * example objects used as RPC arguments. When secure world is idle this
  220. * function returns one shared memory reference to free. To disable the
  221. * cache and free all cached objects this function has to be called until
  222. * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
  223. *
  224. * Call register usage:
  225. * a0 SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
  226. * a1-6 Not used
  227. * a7 Hypervisor Client ID register
  228. *
  229. * Normal return register usage:
  230. * a0 OPTEE_SMC_RETURN_OK
  231. * a1 Upper 32bit of a 64bit Shared memory cookie
  232. * a2 Lower 32bit of a 64bit Shared memory cookie
  233. * a3-7 Preserved
  234. *
  235. * Cache empty return register usage:
  236. * a0 OPTEE_SMC_RETURN_ENOTAVAIL
  237. * a1-7 Preserved
  238. *
  239. * Not idle return register usage:
  240. * a0 OPTEE_SMC_RETURN_EBUSY
  241. * a1-7 Preserved
  242. */
  243. #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE 10
  244. #define OPTEE_SMC_DISABLE_SHM_CACHE \
  245. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
  246. struct optee_smc_disable_shm_cache_result {
  247. unsigned long status;
  248. unsigned long shm_upper32;
  249. unsigned long shm_lower32;
  250. unsigned long reserved0;
  251. };
  252. /*
  253. * Enable cache of shared memory objects
  254. *
  255. * Secure world can cache frequently used shared memory objects, for
  256. * example objects used as RPC arguments. When secure world is idle this
  257. * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
  258. * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
  259. *
  260. * Call register usage:
  261. * a0 SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
  262. * a1-6 Not used
  263. * a7 Hypervisor Client ID register
  264. *
  265. * Normal return register usage:
  266. * a0 OPTEE_SMC_RETURN_OK
  267. * a1-7 Preserved
  268. *
  269. * Not idle return register usage:
  270. * a0 OPTEE_SMC_RETURN_EBUSY
  271. * a1-7 Preserved
  272. */
  273. #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE 11
  274. #define OPTEE_SMC_ENABLE_SHM_CACHE \
  275. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
  276. /*
  277. * Resume from RPC (for example after processing a foreign interrupt)
  278. *
  279. * Call register usage:
  280. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
  281. * a1-3 Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
  282. * OPTEE_SMC_RETURN_RPC in a0
  283. *
  284. * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
  285. *
  286. * Possible return values
  287. * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
  288. * function.
  289. * OPTEE_SMC_RETURN_OK Original call completed, result
  290. * updated in the previously supplied.
  291. * struct optee_msg_arg
  292. * OPTEE_SMC_RETURN_RPC Call suspended by RPC call to normal
  293. * world.
  294. * OPTEE_SMC_RETURN_ERESUME Resume failed, the opaque resume
  295. * information was corrupt.
  296. */
  297. #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC 3
  298. #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
  299. OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
  300. #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK 0xFFFF0000
  301. #define OPTEE_SMC_RETURN_RPC_PREFIX 0xFFFF0000
  302. #define OPTEE_SMC_RETURN_RPC_FUNC_MASK 0x0000FFFF
  303. #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
  304. ((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
  305. #define OPTEE_SMC_RPC_VAL(func) ((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
  306. /*
  307. * Allocate memory for RPC parameter passing. The memory is used to hold a
  308. * struct optee_msg_arg.
  309. *
  310. * "Call" register usage:
  311. * a0 This value, OPTEE_SMC_RETURN_RPC_ALLOC
  312. * a1 Size in bytes of required argument memory
  313. * a2 Not used
  314. * a3 Resume information, must be preserved
  315. * a4-5 Not used
  316. * a6-7 Resume information, must be preserved
  317. *
  318. * "Return" register usage:
  319. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  320. * a1 Upper 32bits of 64bit physical pointer to allocated
  321. * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
  322. * be allocated.
  323. * a2 Lower 32bits of 64bit physical pointer to allocated
  324. * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
  325. * be allocated
  326. * a3 Preserved
  327. * a4 Upper 32bits of 64bit Shared memory cookie used when freeing
  328. * the memory or doing an RPC
  329. * a5 Lower 32bits of 64bit Shared memory cookie used when freeing
  330. * the memory or doing an RPC
  331. * a6-7 Preserved
  332. */
  333. #define OPTEE_SMC_RPC_FUNC_ALLOC 0
  334. #define OPTEE_SMC_RETURN_RPC_ALLOC \
  335. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
  336. /*
  337. * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
  338. *
  339. * "Call" register usage:
  340. * a0 This value, OPTEE_SMC_RETURN_RPC_FREE
  341. * a1 Upper 32bits of 64bit shared memory cookie belonging to this
  342. * argument memory
  343. * a2 Lower 32bits of 64bit shared memory cookie belonging to this
  344. * argument memory
  345. * a3-7 Resume information, must be preserved
  346. *
  347. * "Return" register usage:
  348. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  349. * a1-2 Not used
  350. * a3-7 Preserved
  351. */
  352. #define OPTEE_SMC_RPC_FUNC_FREE 2
  353. #define OPTEE_SMC_RETURN_RPC_FREE \
  354. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
  355. /*
  356. * Deliver foreign interrupt to normal world.
  357. *
  358. * "Call" register usage:
  359. * a0 OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
  360. * a1-7 Resume information, must be preserved
  361. *
  362. * "Return" register usage:
  363. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  364. * a1-7 Preserved
  365. */
  366. #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR 4
  367. #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
  368. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
  369. /*
  370. * Do an RPC request. The supplied struct optee_msg_arg tells which
  371. * request to do and the parameters for the request. The following fields
  372. * are used (the rest are unused):
  373. * - cmd the Request ID
  374. * - ret return value of the request, filled in by normal world
  375. * - num_params number of parameters for the request
  376. * - params the parameters
  377. * - param_attrs attributes of the parameters
  378. *
  379. * "Call" register usage:
  380. * a0 OPTEE_SMC_RETURN_RPC_CMD
  381. * a1 Upper 32bit of a 64bit Shared memory cookie holding a
  382. * struct optee_msg_arg, must be preserved, only the data should
  383. * be updated
  384. * a2 Lower 32bit of a 64bit Shared memory cookie holding a
  385. * struct optee_msg_arg, must be preserved, only the data should
  386. * be updated
  387. * a3-7 Resume information, must be preserved
  388. *
  389. * "Return" register usage:
  390. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  391. * a1-2 Not used
  392. * a3-7 Preserved
  393. */
  394. #define OPTEE_SMC_RPC_FUNC_CMD 5
  395. #define OPTEE_SMC_RETURN_RPC_CMD \
  396. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
  397. /* Returned in a0 */
  398. #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
  399. /* Returned in a0 only from Trusted OS functions */
  400. #define OPTEE_SMC_RETURN_OK 0x0
  401. #define OPTEE_SMC_RETURN_ETHREAD_LIMIT 0x1
  402. #define OPTEE_SMC_RETURN_EBUSY 0x2
  403. #define OPTEE_SMC_RETURN_ERESUME 0x3
  404. #define OPTEE_SMC_RETURN_EBADADDR 0x4
  405. #define OPTEE_SMC_RETURN_EBADCMD 0x5
  406. #define OPTEE_SMC_RETURN_ENOMEM 0x6
  407. #define OPTEE_SMC_RETURN_ENOTAVAIL 0x7
  408. #define OPTEE_SMC_RETURN_IS_RPC(ret) __optee_smc_return_is_rpc((ret))
  409. static inline bool __optee_smc_return_is_rpc(u32 ret)
  410. {
  411. return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
  412. (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
  413. OPTEE_SMC_RETURN_RPC_PREFIX;
  414. }
  415. #endif /* OPTEE_SMC_H */