optee_smc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. May optionally return a 32-bit build identifier in a2,
  106. * with zero meaning unspecified.
  107. */
  108. #define OPTEE_SMC_FUNCID_GET_OS_REVISION OPTEE_MSG_FUNCID_GET_OS_REVISION
  109. #define OPTEE_SMC_CALL_GET_OS_REVISION \
  110. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_REVISION)
  111. struct optee_smc_call_get_os_revision_result {
  112. unsigned long major;
  113. unsigned long minor;
  114. unsigned long build_id;
  115. unsigned long reserved1;
  116. };
  117. /*
  118. * Call with struct optee_msg_arg as argument
  119. *
  120. * Call register usage:
  121. * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
  122. * a1 Upper 32bit of a 64bit physical pointer to a struct optee_msg_arg
  123. * a2 Lower 32bit of a 64bit physical pointer to a struct optee_msg_arg
  124. * a3 Cache settings, not used if physical pointer is in a predefined shared
  125. * memory area else per OPTEE_SMC_SHM_*
  126. * a4-6 Not used
  127. * a7 Hypervisor Client ID register
  128. *
  129. * Normal return register usage:
  130. * a0 Return value, OPTEE_SMC_RETURN_*
  131. * a1-3 Not used
  132. * a4-7 Preserved
  133. *
  134. * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
  135. * a0 Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
  136. * a1-3 Preserved
  137. * a4-7 Preserved
  138. *
  139. * RPC return register usage:
  140. * a0 Return value, OPTEE_SMC_RETURN_IS_RPC(val)
  141. * a1-2 RPC parameters
  142. * a3-7 Resume information, must be preserved
  143. *
  144. * Possible return values:
  145. * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
  146. * function.
  147. * OPTEE_SMC_RETURN_OK Call completed, result updated in
  148. * the previously supplied struct
  149. * optee_msg_arg.
  150. * OPTEE_SMC_RETURN_ETHREAD_LIMIT Number of Trusted OS threads exceeded,
  151. * try again later.
  152. * OPTEE_SMC_RETURN_EBADADDR Bad physcial pointer to struct
  153. * optee_msg_arg.
  154. * OPTEE_SMC_RETURN_EBADCMD Bad/unknown cmd in struct optee_msg_arg
  155. * OPTEE_SMC_RETURN_IS_RPC() Call suspended by RPC call to normal
  156. * world.
  157. */
  158. #define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
  159. #define OPTEE_SMC_CALL_WITH_ARG \
  160. OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
  161. /*
  162. * Get Shared Memory Config
  163. *
  164. * Returns the Secure/Non-secure shared memory config.
  165. *
  166. * Call register usage:
  167. * a0 SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
  168. * a1-6 Not used
  169. * a7 Hypervisor Client ID register
  170. *
  171. * Have config return register usage:
  172. * a0 OPTEE_SMC_RETURN_OK
  173. * a1 Physical address of start of SHM
  174. * a2 Size of of SHM
  175. * a3 Cache settings of memory, as defined by the
  176. * OPTEE_SMC_SHM_* values above
  177. * a4-7 Preserved
  178. *
  179. * Not available register usage:
  180. * a0 OPTEE_SMC_RETURN_ENOTAVAIL
  181. * a1-3 Not used
  182. * a4-7 Preserved
  183. */
  184. #define OPTEE_SMC_FUNCID_GET_SHM_CONFIG 7
  185. #define OPTEE_SMC_GET_SHM_CONFIG \
  186. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
  187. struct optee_smc_get_shm_config_result {
  188. unsigned long status;
  189. unsigned long start;
  190. unsigned long size;
  191. unsigned long settings;
  192. };
  193. /*
  194. * Exchanges capabilities between normal world and secure world
  195. *
  196. * Call register usage:
  197. * a0 SMC Function ID, OPTEE_SMC_EXCHANGE_CAPABILITIES
  198. * a1 bitfield of normal world capabilities OPTEE_SMC_NSEC_CAP_*
  199. * a2-6 Not used
  200. * a7 Hypervisor Client ID register
  201. *
  202. * Normal return register usage:
  203. * a0 OPTEE_SMC_RETURN_OK
  204. * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
  205. * a2-7 Preserved
  206. *
  207. * Error return register usage:
  208. * a0 OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world
  209. * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
  210. * a2-7 Preserved
  211. */
  212. /* Normal world works as a uniprocessor system */
  213. #define OPTEE_SMC_NSEC_CAP_UNIPROCESSOR BIT(0)
  214. /* Secure world has reserved shared memory for normal world to use */
  215. #define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM BIT(0)
  216. /* Secure world can communicate via previously unregistered shared memory */
  217. #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM BIT(1)
  218. /*
  219. * Secure world supports commands "register/unregister shared memory",
  220. * secure world accepts command buffers located in any parts of non-secure RAM
  221. */
  222. #define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM BIT(2)
  223. #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
  224. #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
  225. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
  226. struct optee_smc_exchange_capabilities_result {
  227. unsigned long status;
  228. unsigned long capabilities;
  229. unsigned long reserved0;
  230. unsigned long reserved1;
  231. };
  232. /*
  233. * Disable and empties cache of shared memory objects
  234. *
  235. * Secure world can cache frequently used shared memory objects, for
  236. * example objects used as RPC arguments. When secure world is idle this
  237. * function returns one shared memory reference to free. To disable the
  238. * cache and free all cached objects this function has to be called until
  239. * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
  240. *
  241. * Call register usage:
  242. * a0 SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
  243. * a1-6 Not used
  244. * a7 Hypervisor Client ID register
  245. *
  246. * Normal return register usage:
  247. * a0 OPTEE_SMC_RETURN_OK
  248. * a1 Upper 32bit of a 64bit Shared memory cookie
  249. * a2 Lower 32bit of a 64bit Shared memory cookie
  250. * a3-7 Preserved
  251. *
  252. * Cache empty return register usage:
  253. * a0 OPTEE_SMC_RETURN_ENOTAVAIL
  254. * a1-7 Preserved
  255. *
  256. * Not idle return register usage:
  257. * a0 OPTEE_SMC_RETURN_EBUSY
  258. * a1-7 Preserved
  259. */
  260. #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE 10
  261. #define OPTEE_SMC_DISABLE_SHM_CACHE \
  262. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
  263. struct optee_smc_disable_shm_cache_result {
  264. unsigned long status;
  265. unsigned long shm_upper32;
  266. unsigned long shm_lower32;
  267. unsigned long reserved0;
  268. };
  269. /*
  270. * Enable cache of shared memory objects
  271. *
  272. * Secure world can cache frequently used shared memory objects, for
  273. * example objects used as RPC arguments. When secure world is idle this
  274. * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
  275. * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
  276. *
  277. * Call register usage:
  278. * a0 SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
  279. * a1-6 Not used
  280. * a7 Hypervisor Client ID register
  281. *
  282. * Normal return register usage:
  283. * a0 OPTEE_SMC_RETURN_OK
  284. * a1-7 Preserved
  285. *
  286. * Not idle return register usage:
  287. * a0 OPTEE_SMC_RETURN_EBUSY
  288. * a1-7 Preserved
  289. */
  290. #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE 11
  291. #define OPTEE_SMC_ENABLE_SHM_CACHE \
  292. OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
  293. /*
  294. * Resume from RPC (for example after processing a foreign interrupt)
  295. *
  296. * Call register usage:
  297. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
  298. * a1-3 Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
  299. * OPTEE_SMC_RETURN_RPC in a0
  300. *
  301. * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
  302. *
  303. * Possible return values
  304. * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
  305. * function.
  306. * OPTEE_SMC_RETURN_OK Original call completed, result
  307. * updated in the previously supplied.
  308. * struct optee_msg_arg
  309. * OPTEE_SMC_RETURN_RPC Call suspended by RPC call to normal
  310. * world.
  311. * OPTEE_SMC_RETURN_ERESUME Resume failed, the opaque resume
  312. * information was corrupt.
  313. */
  314. #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC 3
  315. #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
  316. OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
  317. #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK 0xFFFF0000
  318. #define OPTEE_SMC_RETURN_RPC_PREFIX 0xFFFF0000
  319. #define OPTEE_SMC_RETURN_RPC_FUNC_MASK 0x0000FFFF
  320. #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
  321. ((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
  322. #define OPTEE_SMC_RPC_VAL(func) ((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
  323. /*
  324. * Allocate memory for RPC parameter passing. The memory is used to hold a
  325. * struct optee_msg_arg.
  326. *
  327. * "Call" register usage:
  328. * a0 This value, OPTEE_SMC_RETURN_RPC_ALLOC
  329. * a1 Size in bytes of required argument memory
  330. * a2 Not used
  331. * a3 Resume information, must be preserved
  332. * a4-5 Not used
  333. * a6-7 Resume information, must be preserved
  334. *
  335. * "Return" register usage:
  336. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  337. * a1 Upper 32bits of 64bit physical pointer to allocated
  338. * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
  339. * be allocated.
  340. * a2 Lower 32bits of 64bit physical pointer to allocated
  341. * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
  342. * be allocated
  343. * a3 Preserved
  344. * a4 Upper 32bits of 64bit Shared memory cookie used when freeing
  345. * the memory or doing an RPC
  346. * a5 Lower 32bits of 64bit Shared memory cookie used when freeing
  347. * the memory or doing an RPC
  348. * a6-7 Preserved
  349. */
  350. #define OPTEE_SMC_RPC_FUNC_ALLOC 0
  351. #define OPTEE_SMC_RETURN_RPC_ALLOC \
  352. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
  353. /*
  354. * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
  355. *
  356. * "Call" register usage:
  357. * a0 This value, OPTEE_SMC_RETURN_RPC_FREE
  358. * a1 Upper 32bits of 64bit shared memory cookie belonging to this
  359. * argument memory
  360. * a2 Lower 32bits of 64bit shared memory cookie belonging to this
  361. * argument memory
  362. * a3-7 Resume information, must be preserved
  363. *
  364. * "Return" register usage:
  365. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  366. * a1-2 Not used
  367. * a3-7 Preserved
  368. */
  369. #define OPTEE_SMC_RPC_FUNC_FREE 2
  370. #define OPTEE_SMC_RETURN_RPC_FREE \
  371. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
  372. /*
  373. * Deliver foreign interrupt to normal world.
  374. *
  375. * "Call" register usage:
  376. * a0 OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
  377. * a1-7 Resume information, must be preserved
  378. *
  379. * "Return" register usage:
  380. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  381. * a1-7 Preserved
  382. */
  383. #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR 4
  384. #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
  385. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
  386. /*
  387. * Do an RPC request. The supplied struct optee_msg_arg tells which
  388. * request to do and the parameters for the request. The following fields
  389. * are used (the rest are unused):
  390. * - cmd the Request ID
  391. * - ret return value of the request, filled in by normal world
  392. * - num_params number of parameters for the request
  393. * - params the parameters
  394. * - param_attrs attributes of the parameters
  395. *
  396. * "Call" register usage:
  397. * a0 OPTEE_SMC_RETURN_RPC_CMD
  398. * a1 Upper 32bit of a 64bit Shared memory cookie holding a
  399. * struct optee_msg_arg, must be preserved, only the data should
  400. * be updated
  401. * a2 Lower 32bit of a 64bit Shared memory cookie holding a
  402. * struct optee_msg_arg, must be preserved, only the data should
  403. * be updated
  404. * a3-7 Resume information, must be preserved
  405. *
  406. * "Return" register usage:
  407. * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
  408. * a1-2 Not used
  409. * a3-7 Preserved
  410. */
  411. #define OPTEE_SMC_RPC_FUNC_CMD 5
  412. #define OPTEE_SMC_RETURN_RPC_CMD \
  413. OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
  414. /* Returned in a0 */
  415. #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
  416. /* Returned in a0 only from Trusted OS functions */
  417. #define OPTEE_SMC_RETURN_OK 0x0
  418. #define OPTEE_SMC_RETURN_ETHREAD_LIMIT 0x1
  419. #define OPTEE_SMC_RETURN_EBUSY 0x2
  420. #define OPTEE_SMC_RETURN_ERESUME 0x3
  421. #define OPTEE_SMC_RETURN_EBADADDR 0x4
  422. #define OPTEE_SMC_RETURN_EBADCMD 0x5
  423. #define OPTEE_SMC_RETURN_ENOMEM 0x6
  424. #define OPTEE_SMC_RETURN_ENOTAVAIL 0x7
  425. #define OPTEE_SMC_RETURN_IS_RPC(ret) __optee_smc_return_is_rpc((ret))
  426. static inline bool __optee_smc_return_is_rpc(u32 ret)
  427. {
  428. return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
  429. (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
  430. OPTEE_SMC_RETURN_RPC_PREFIX;
  431. }
  432. #endif /* OPTEE_SMC_H */