1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
3 * Copyright (c) 2015-2021, Linaro Limited
8 #include <linux/arm-smccc.h>
9 #include <linux/bitops.h>
11 #define OPTEE_SMC_STD_CALL_VAL(func_num) \
12 ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL, ARM_SMCCC_SMC_32, \
13 ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
14 #define OPTEE_SMC_FAST_CALL_VAL(func_num) \
15 ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
16 ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
19 * Function specified by SMC Calling convention.
21 #define OPTEE_SMC_FUNCID_CALLS_COUNT 0xFF00
22 #define OPTEE_SMC_CALLS_COUNT \
23 ARM_SMCCC_CALL_VAL(OPTEE_SMC_FAST_CALL, SMCCC_SMC_32, \
24 SMCCC_OWNER_TRUSTED_OS_END, \
25 OPTEE_SMC_FUNCID_CALLS_COUNT)
28 * Normal cached memory (write-back), shareable for SMP systems and not
29 * shareable for UP systems.
31 #define OPTEE_SMC_SHM_CACHED 1
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
40 * Function specified by SMC Calling convention
42 * Return the following UID if using API specified in this file
43 * without further extensions:
44 * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
45 * see also OPTEE_MSG_UID_* in optee_msg.h
47 #define OPTEE_SMC_FUNCID_CALLS_UID OPTEE_MSG_FUNCID_CALLS_UID
48 #define OPTEE_SMC_CALLS_UID \
49 ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
50 ARM_SMCCC_OWNER_TRUSTED_OS_END, \
51 OPTEE_SMC_FUNCID_CALLS_UID)
54 * Function specified by SMC Calling convention
56 * Returns 2.0 if using API specified in this file without further extensions.
57 * see also OPTEE_MSG_REVISION_* in optee_msg.h
59 #define OPTEE_SMC_FUNCID_CALLS_REVISION OPTEE_MSG_FUNCID_CALLS_REVISION
60 #define OPTEE_SMC_CALLS_REVISION \
61 ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
62 ARM_SMCCC_OWNER_TRUSTED_OS_END, \
63 OPTEE_SMC_FUNCID_CALLS_REVISION)
65 struct optee_smc_calls_revision_result {
68 unsigned long reserved0;
69 unsigned long reserved1;
73 * Get UUID of Trusted OS.
75 * Used by non-secure world to figure out which Trusted OS is installed.
76 * Note that returned UUID is the UUID of the Trusted OS, not of the API.
78 * Returns UUID in a0-4 in the same way as OPTEE_SMC_CALLS_UID
81 #define OPTEE_SMC_FUNCID_GET_OS_UUID OPTEE_MSG_FUNCID_GET_OS_UUID
82 #define OPTEE_SMC_CALL_GET_OS_UUID \
83 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_UUID)
86 * Get revision of Trusted OS.
88 * Used by non-secure world to figure out which version of the Trusted OS
89 * is installed. Note that the returned revision is the revision of the
90 * Trusted OS, not of the API.
92 * Returns revision in a0-1 in the same way as OPTEE_SMC_CALLS_REVISION
93 * described above. May optionally return a 32-bit build identifier in a2,
94 * with zero meaning unspecified.
96 #define OPTEE_SMC_FUNCID_GET_OS_REVISION OPTEE_MSG_FUNCID_GET_OS_REVISION
97 #define OPTEE_SMC_CALL_GET_OS_REVISION \
98 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_REVISION)
100 struct optee_smc_call_get_os_revision_result {
103 unsigned long build_id;
104 unsigned long reserved1;
108 * Call with struct optee_msg_arg as argument
110 * Call register usage:
111 * a0 SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
112 * a1 Upper 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
113 * a2 Lower 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
114 * a3 Cache settings, not used if physical pointer is in a predefined shared
115 * memory area else per OPTEE_SMC_SHM_*
117 * a7 Hypervisor Client ID register
119 * Normal return register usage:
120 * a0 Return value, OPTEE_SMC_RETURN_*
124 * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
125 * a0 Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
129 * RPC return register usage:
130 * a0 Return value, OPTEE_SMC_RETURN_IS_RPC(val)
131 * a1-2 RPC parameters
132 * a3-7 Resume information, must be preserved
134 * Possible return values:
135 * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
137 * OPTEE_SMC_RETURN_OK Call completed, result updated in
138 * the previously supplied struct
140 * OPTEE_SMC_RETURN_ETHREAD_LIMIT Number of Trusted OS threads exceeded,
142 * OPTEE_SMC_RETURN_EBADADDR Bad physical pointer to struct
144 * OPTEE_SMC_RETURN_EBADCMD Bad/unknown cmd in struct optee_msg_arg
145 * OPTEE_SMC_RETURN_IS_RPC() Call suspended by RPC call to normal
148 #define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
149 #define OPTEE_SMC_CALL_WITH_ARG \
150 OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
153 * Get Shared Memory Config
155 * Returns the Secure/Non-secure shared memory config.
157 * Call register usage:
158 * a0 SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
160 * a7 Hypervisor Client ID register
162 * Have config return register usage:
163 * a0 OPTEE_SMC_RETURN_OK
164 * a1 Physical address of start of SHM
166 * a3 Cache settings of memory, as defined by the
167 * OPTEE_SMC_SHM_* values above
170 * Not available register usage:
171 * a0 OPTEE_SMC_RETURN_ENOTAVAIL
175 #define OPTEE_SMC_FUNCID_GET_SHM_CONFIG 7
176 #define OPTEE_SMC_GET_SHM_CONFIG \
177 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
179 struct optee_smc_get_shm_config_result {
180 unsigned long status;
183 unsigned long settings;
187 * Exchanges capabilities between normal world and secure world
189 * Call register usage:
190 * a0 SMC Function ID, OPTEE_SMC_EXCHANGE_CAPABILITIES
191 * a1 bitfield of normal world capabilities OPTEE_SMC_NSEC_CAP_*
193 * a7 Hypervisor Client ID register
195 * Normal return register usage:
196 * a0 OPTEE_SMC_RETURN_OK
197 * a1 bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
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_*
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)
213 * Secure world supports commands "register/unregister shared memory",
214 * secure world accepts command buffers located in any parts of non-secure RAM
216 #define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM BIT(2)
217 /* Secure world is built with virtualization support */
218 #define OPTEE_SMC_SEC_CAP_VIRTUALIZATION BIT(3)
219 /* Secure world supports Shared Memory with a NULL reference */
220 #define OPTEE_SMC_SEC_CAP_MEMREF_NULL BIT(4)
222 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9
223 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
224 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;
234 * Disable and empties cache of shared memory objects
236 * Secure world can cache frequently used shared memory objects, for
237 * example objects used as RPC arguments. When secure world is idle this
238 * function returns one shared memory reference to free. To disable the
239 * cache and free all cached objects this function has to be called until
240 * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
242 * Call register usage:
243 * a0 SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
245 * a7 Hypervisor Client ID register
247 * Normal return register usage:
248 * a0 OPTEE_SMC_RETURN_OK
249 * a1 Upper 32 bits of a 64-bit Shared memory cookie
250 * a2 Lower 32 bits of a 64-bit Shared memory cookie
253 * Cache empty return register usage:
254 * a0 OPTEE_SMC_RETURN_ENOTAVAIL
257 * Not idle return register usage:
258 * a0 OPTEE_SMC_RETURN_EBUSY
261 #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE 10
262 #define OPTEE_SMC_DISABLE_SHM_CACHE \
263 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
265 struct optee_smc_disable_shm_cache_result {
266 unsigned long status;
267 unsigned long shm_upper32;
268 unsigned long shm_lower32;
269 unsigned long reserved0;
273 * Enable cache of shared memory objects
275 * Secure world can cache frequently used shared memory objects, for
276 * example objects used as RPC arguments. When secure world is idle this
277 * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
278 * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
280 * Call register usage:
281 * a0 SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
283 * a7 Hypervisor Client ID register
285 * Normal return register usage:
286 * a0 OPTEE_SMC_RETURN_OK
289 * Not idle return register usage:
290 * a0 OPTEE_SMC_RETURN_EBUSY
293 #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE 11
294 #define OPTEE_SMC_ENABLE_SHM_CACHE \
295 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
298 * Query OP-TEE about number of supported threads
300 * Normal World OS or Hypervisor issues this call to find out how many
301 * threads OP-TEE supports. That is how many standard calls can be issued
302 * in parallel before OP-TEE will return OPTEE_SMC_RETURN_ETHREAD_LIMIT.
304 * Call requests usage:
305 * a0 SMC Function ID, OPTEE_SMC_GET_THREAD_COUNT
307 * a7 Hypervisor Client ID register
309 * Normal return register usage:
310 * a0 OPTEE_SMC_RETURN_OK
311 * a1 Number of threads
315 * a0 OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Requested call is not implemented
318 #define OPTEE_SMC_FUNCID_GET_THREAD_COUNT 15
319 #define OPTEE_SMC_GET_THREAD_COUNT \
320 OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)
323 * Resume from RPC (for example after processing a foreign interrupt)
325 * Call register usage:
326 * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
327 * a1-3 Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
328 * OPTEE_SMC_RETURN_RPC in a0
330 * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
332 * Possible return values
333 * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION Trusted OS does not recognize this
335 * OPTEE_SMC_RETURN_OK Original call completed, result
336 * updated in the previously supplied.
337 * struct optee_msg_arg
338 * OPTEE_SMC_RETURN_RPC Call suspended by RPC call to normal
340 * OPTEE_SMC_RETURN_ERESUME Resume failed, the opaque resume
341 * information was corrupt.
343 #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC 3
344 #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
345 OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
347 #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK 0xFFFF0000
348 #define OPTEE_SMC_RETURN_RPC_PREFIX 0xFFFF0000
349 #define OPTEE_SMC_RETURN_RPC_FUNC_MASK 0x0000FFFF
351 #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
352 ((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
354 #define OPTEE_SMC_RPC_VAL(func) ((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
357 * Allocate memory for RPC parameter passing. The memory is used to hold a
358 * struct optee_msg_arg.
360 * "Call" register usage:
361 * a0 This value, OPTEE_SMC_RETURN_RPC_ALLOC
362 * a1 Size in bytes of required argument memory
364 * a3 Resume information, must be preserved
366 * a6-7 Resume information, must be preserved
368 * "Return" register usage:
369 * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
370 * a1 Upper 32 bits of 64-bit physical pointer to allocated
371 * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
373 * a2 Lower 32 bits of 64-bit physical pointer to allocated
374 * memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
377 * a4 Upper 32 bits of 64-bit Shared memory cookie used when freeing
378 * the memory or doing an RPC
379 * a5 Lower 32 bits of 64-bit Shared memory cookie used when freeing
380 * the memory or doing an RPC
383 #define OPTEE_SMC_RPC_FUNC_ALLOC 0
384 #define OPTEE_SMC_RETURN_RPC_ALLOC \
385 OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
388 * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
390 * "Call" register usage:
391 * a0 This value, OPTEE_SMC_RETURN_RPC_FREE
392 * a1 Upper 32 bits of 64-bit shared memory cookie belonging to this
394 * a2 Lower 32 bits of 64-bit shared memory cookie belonging to this
396 * a3-7 Resume information, must be preserved
398 * "Return" register usage:
399 * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
403 #define OPTEE_SMC_RPC_FUNC_FREE 2
404 #define OPTEE_SMC_RETURN_RPC_FREE \
405 OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
408 * Deliver a foreign interrupt in normal world.
410 * "Call" register usage:
411 * a0 OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
412 * a1-7 Resume information, must be preserved
414 * "Return" register usage:
415 * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
418 #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR 4
419 #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
420 OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
423 * Do an RPC request. The supplied struct optee_msg_arg tells which
424 * request to do and the parameters for the request. The following fields
425 * are used (the rest are unused):
426 * - cmd the Request ID
427 * - ret return value of the request, filled in by normal world
428 * - num_params number of parameters for the request
429 * - params the parameters
430 * - param_attrs attributes of the parameters
432 * "Call" register usage:
433 * a0 OPTEE_SMC_RETURN_RPC_CMD
434 * a1 Upper 32 bits of a 64-bit Shared memory cookie holding a
435 * struct optee_msg_arg, must be preserved, only the data should
437 * a2 Lower 32 bits of a 64-bit Shared memory cookie holding a
438 * struct optee_msg_arg, must be preserved, only the data should
440 * a3-7 Resume information, must be preserved
442 * "Return" register usage:
443 * a0 SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
447 #define OPTEE_SMC_RPC_FUNC_CMD 5
448 #define OPTEE_SMC_RETURN_RPC_CMD \
449 OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
452 #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
454 /* Returned in a0 only from Trusted OS functions */
455 #define OPTEE_SMC_RETURN_OK 0x0
456 #define OPTEE_SMC_RETURN_ETHREAD_LIMIT 0x1
457 #define OPTEE_SMC_RETURN_EBUSY 0x2
458 #define OPTEE_SMC_RETURN_ERESUME 0x3
459 #define OPTEE_SMC_RETURN_EBADADDR 0x4
460 #define OPTEE_SMC_RETURN_EBADCMD 0x5
461 #define OPTEE_SMC_RETURN_ENOMEM 0x6
462 #define OPTEE_SMC_RETURN_ENOTAVAIL 0x7
463 #define OPTEE_SMC_RETURN_IS_RPC(ret) __optee_smc_return_is_rpc((ret))
465 static inline bool __optee_smc_return_is_rpc(u32 ret)
467 return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
468 (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
469 OPTEE_SMC_RETURN_RPC_PREFIX;
472 #endif /* OPTEE_SMC_H */