optee_smc.h 15 KB

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