1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015-2021, Linaro Limited
4  */
5 #ifndef OPTEE_SMC_H
6 #define OPTEE_SMC_H
7 
8 #include <stdint.h>
9 
10 /*
11  * This file is exported by OP-TEE and is in kept in sync between secure
12  * world and normal world kernel driver. We're following ARM SMC Calling
13  * Convention as specified in
14  * http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html
15  *
16  * This file depends on optee_msg.h being included to expand the SMC id
17  * macros below.
18  */
19 
20 #define OPTEE_SMC_32			U(0)
21 #define OPTEE_SMC_64			U(0x40000000)
22 #define OPTEE_SMC_FAST_CALL		U(0x80000000)
23 #define OPTEE_SMC_STD_CALL		U(0)
24 
25 #define OPTEE_SMC_OWNER_MASK		U(0x3F)
26 #define OPTEE_SMC_OWNER_SHIFT		U(24)
27 
28 #define OPTEE_SMC_FUNC_MASK		U(0xFFFF)
29 
30 #define OPTEE_SMC_IS_FAST_CALL(smc_val)	((smc_val) & OPTEE_SMC_FAST_CALL)
31 #define OPTEE_SMC_IS_64(smc_val)	((smc_val) & OPTEE_SMC_64)
32 #define OPTEE_SMC_FUNC_NUM(smc_val)	((smc_val) & OPTEE_SMC_FUNC_MASK)
33 #define OPTEE_SMC_OWNER_NUM(smc_val) \
34 	(((smc_val) >> OPTEE_SMC_OWNER_SHIFT) & OPTEE_SMC_OWNER_MASK)
35 
36 #define OPTEE_SMC_CALL_VAL(type, calling_convention, owner, func_num) \
37 			((type) | (calling_convention) | \
38 			(((owner) & OPTEE_SMC_OWNER_MASK) << \
39 				OPTEE_SMC_OWNER_SHIFT) |\
40 			((func_num) & OPTEE_SMC_FUNC_MASK))
41 
42 #define OPTEE_SMC_STD_CALL_VAL(func_num) \
43 	OPTEE_SMC_CALL_VAL(OPTEE_SMC_32, OPTEE_SMC_STD_CALL, \
44 			   OPTEE_SMC_OWNER_TRUSTED_OS, (func_num))
45 #define OPTEE_SMC_FAST_CALL_VAL(func_num) \
46 	OPTEE_SMC_CALL_VAL(OPTEE_SMC_32, OPTEE_SMC_FAST_CALL, \
47 			   OPTEE_SMC_OWNER_TRUSTED_OS, (func_num))
48 
49 #define OPTEE_SMC_OWNER_ARCH		U(0)
50 #define OPTEE_SMC_OWNER_CPU		U(1)
51 #define OPTEE_SMC_OWNER_SIP		U(2)
52 #define OPTEE_SMC_OWNER_OEM		U(3)
53 #define OPTEE_SMC_OWNER_STANDARD	U(4)
54 #define OPTEE_SMC_OWNER_TRUSTED_APP	U(48)
55 #define OPTEE_SMC_OWNER_TRUSTED_OS	U(50)
56 
57 #define OPTEE_SMC_OWNER_TRUSTED_OS_OPTEED U(62)
58 #define OPTEE_SMC_OWNER_TRUSTED_OS_API	U(63)
59 
60 /*
61  * Function specified by SMC Calling convention.
62  */
63 #define OPTEE_SMC_FUNCID_CALLS_COUNT	U(0xFF00)
64 #define OPTEE_SMC_CALLS_COUNT \
65 	OPTEE_SMC_CALL_VAL(OPTEE_SMC_32, OPTEE_SMC_FAST_CALL, \
66 			   OPTEE_SMC_OWNER_TRUSTED_OS_API, \
67 			   OPTEE_SMC_FUNCID_CALLS_COUNT)
68 
69 /*
70  * Normal cached memory (write-back), shareable for SMP systems and not
71  * shareable for UP systems.
72  */
73 #define OPTEE_SMC_SHM_CACHED		U(1)
74 
75 /*
76  * a0..a7 is used as register names in the descriptions below, on arm32
77  * that translates to r0..r7 and on arm64 to w0..w7. In both cases it's
78  * 32-bit registers.
79  */
80 
81 /*
82  * Function specified by SMC Calling convention
83  *
84  * Return the following UID if using API specified in this file
85  * without further extensions:
86  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
87  * see also OPTEE_MSG_UID_* in optee_msg.h
88  */
89 #define OPTEE_SMC_FUNCID_CALLS_UID OPTEE_MSG_FUNCID_CALLS_UID
90 #define OPTEE_SMC_CALLS_UID \
91 	OPTEE_SMC_CALL_VAL(OPTEE_SMC_32, OPTEE_SMC_FAST_CALL, \
92 			   OPTEE_SMC_OWNER_TRUSTED_OS_API, \
93 			   OPTEE_SMC_FUNCID_CALLS_UID)
94 
95 /*
96  * Function specified by SMC Calling convention
97  *
98  * Returns 2.0 if using API specified in this file without further extensions.
99  * see also OPTEE_MSG_REVISION_* in optee_msg.h
100  */
101 #define OPTEE_SMC_FUNCID_CALLS_REVISION OPTEE_MSG_FUNCID_CALLS_REVISION
102 #define OPTEE_SMC_CALLS_REVISION \
103 	OPTEE_SMC_CALL_VAL(OPTEE_SMC_32, OPTEE_SMC_FAST_CALL, \
104 			   OPTEE_SMC_OWNER_TRUSTED_OS_API, \
105 			   OPTEE_SMC_FUNCID_CALLS_REVISION)
106 
107 /*
108  * Get UUID of Trusted OS.
109  *
110  * Used by non-secure world to figure out which Trusted OS is installed.
111  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
112  *
113  * Returns UUID in a0-4 in the same way as OPTEE_SMC_CALLS_UID
114  * described above.
115  */
116 #define OPTEE_SMC_FUNCID_GET_OS_UUID OPTEE_MSG_FUNCID_GET_OS_UUID
117 #define OPTEE_SMC_CALL_GET_OS_UUID \
118 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_UUID)
119 
120 /*
121  * Get revision of Trusted OS.
122  *
123  * Used by non-secure world to figure out which version of the Trusted OS
124  * is installed. Note that the returned revision is the revision of the
125  * Trusted OS, not of the API.
126  *
127  * Returns revision in a0-1 in the same way as OPTEE_SMC_CALLS_REVISION
128  * described above. May optionally return a 32-bit build identifier in a2,
129  * with zero meaning unspecified.
130  */
131 #define OPTEE_SMC_FUNCID_GET_OS_REVISION OPTEE_MSG_FUNCID_GET_OS_REVISION
132 #define OPTEE_SMC_CALL_GET_OS_REVISION \
133 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_REVISION)
134 
135 /*
136  * Call with struct optee_msg_arg as argument
137  *
138  * When calling this function normal world has a few responsibilities:
139  * 1. It must be able to handle eventual RPCs
140  * 2. Non-secure interrupts should not be masked
141  * 3. If asynchronous notifications has be negotiated successfully, then
142  *    the interrupt for asynchronous notifications should be unmasked
143  *    during this call.
144  *
145  * Call register usage:
146  * a0	SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
147  * a1	Upper 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
148  * a2	Lower 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
149  * a3	Cache settings, not used if physical pointer is in a predefined shared
150  *	memory area else per OPTEE_SMC_SHM_*
151  * a4-6	Not used
152  * a7	Hypervisor Client ID register
153  *
154  * Normal return register usage:
155  * a0	Return value, OPTEE_SMC_RETURN_*
156  * a1-3	Not used
157  * a4-7	Preserved
158  *
159  * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
160  * a0	Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
161  * a1-3	Preserved
162  * a4-7	Preserved
163  *
164  * RPC return register usage:
165  * a0	Return value, OPTEE_SMC_RETURN_IS_RPC(val)
166  * a1-2	RPC parameters
167  * a3-7	Resume information, must be preserved
168  *
169  * Possible return values:
170  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
171  *					function.
172  * OPTEE_SMC_RETURN_OK			Call completed, result updated in
173  *					the previously supplied struct
174  *					optee_msg_arg.
175  * OPTEE_SMC_RETURN_ETHREAD_LIMIT	Number of Trusted OS threads exceeded,
176  *					try again later.
177  * OPTEE_SMC_RETURN_EBADADDR		Bad physical pointer to struct
178  *					optee_msg_arg.
179  * OPTEE_SMC_RETURN_EBADCMD		Bad/unknown cmd in struct optee_msg_arg
180  * OPTEE_SMC_RETURN_IS_RPC()		Call suspended by RPC call to normal
181  *					world.
182  */
183 #define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
184 #define OPTEE_SMC_CALL_WITH_ARG \
185 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
186 
187 /*
188  * Get Shared Memory Config
189  *
190  * Returns the Secure/Non-secure shared memory config.
191  *
192  * Call register usage:
193  * a0	SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
194  * a1-6	Not used
195  * a7	Hypervisor Client ID register
196  *
197  * Have config return register usage:
198  * a0	OPTEE_SMC_RETURN_OK
199  * a1	Physical address of start of SHM
200  * a2	Size of of SHM
201  * a3	Cache settings of memory, as defined by the
202  *	OPTEE_SMC_SHM_* values above
203  * a4-7	Preserved
204  *
205  * Not available register usage:
206  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
207  * a1-3 Not used
208  * a4-7	Preserved
209  */
210 #define OPTEE_SMC_FUNCID_GET_SHM_CONFIG	7
211 #define OPTEE_SMC_GET_SHM_CONFIG \
212 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
213 
214 /*
215  * Configures L2CC mutex
216  *
217  * Disables, enables usage of L2CC mutex. Returns or sets physical address
218  * of L2CC mutex.
219  *
220  * Call register usage:
221  * a0	SMC Function ID, OPTEE_SMC_L2CC_MUTEX
222  * a1	OPTEE_SMC_L2CC_MUTEX_GET_ADDR	Get physical address of mutex
223  *	OPTEE_SMC_L2CC_MUTEX_SET_ADDR	Set physical address of mutex
224  *	OPTEE_SMC_L2CC_MUTEX_ENABLE	Enable usage of mutex
225  *	OPTEE_SMC_L2CC_MUTEX_DISABLE	Disable usage of mutex
226  * a2	if a1 == OPTEE_SMC_L2CC_MUTEX_SET_ADDR, upper 32bit of a 64bit
227  *      physical address of mutex
228  * a3	if a1 == OPTEE_SMC_L2CC_MUTEX_SET_ADDR, lower 32bit of a 64bit
229  *      physical address of mutex
230  * a3-6	Not used
231  * a7	Hypervisor Client ID register
232  *
233  * Have config return register usage:
234  * a0	OPTEE_SMC_RETURN_OK
235  * a1	Preserved
236  * a2	if a1 == OPTEE_SMC_L2CC_MUTEX_GET_ADDR, upper 32bit of a 64bit
237  *      physical address of mutex
238  * a3	if a1 == OPTEE_SMC_L2CC_MUTEX_GET_ADDR, lower 32bit of a 64bit
239  *      physical address of mutex
240  * a3-7	Preserved
241  *
242  * Error return register usage:
243  * a0	OPTEE_SMC_RETURN_ENOTAVAIL	Physical address not available
244  *	OPTEE_SMC_RETURN_EBADADDR	Bad supplied physical address
245  *	OPTEE_SMC_RETURN_EBADCMD	Unsupported value in a1
246  * a1-7	Preserved
247  */
248 #define OPTEE_SMC_L2CC_MUTEX_GET_ADDR	U(0)
249 #define OPTEE_SMC_L2CC_MUTEX_SET_ADDR	U(1)
250 #define OPTEE_SMC_L2CC_MUTEX_ENABLE	U(2)
251 #define OPTEE_SMC_L2CC_MUTEX_DISABLE	U(3)
252 #define OPTEE_SMC_FUNCID_L2CC_MUTEX	U(8)
253 #define OPTEE_SMC_L2CC_MUTEX \
254 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_L2CC_MUTEX)
255 
256 /*
257  * Exchanges capabilities between normal world and secure world
258  *
259  * Call register usage:
260  * a0	SMC Function ID, OPTEE_SMC_EXCHANGE_CAPABILITIES
261  * a1	bitfield of normal world capabilities OPTEE_SMC_NSEC_CAP_*
262  * a2-6	Not used
263  * a7	Hypervisor Client ID register
264  *
265  * Normal return register usage:
266  * a0	OPTEE_SMC_RETURN_OK
267  * a1	bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
268  * a2	The maximum secure world notification number
269  * a3-7	Preserved
270  *
271  * Error return register usage:
272  * a0	OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world
273  * a1	bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
274  * a2-7 Preserved
275  */
276 /* Normal world works as a uniprocessor system */
277 #define OPTEE_SMC_NSEC_CAP_UNIPROCESSOR		BIT(0)
278 /* Secure world has reserved shared memory for normal world to use */
279 #define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM	BIT(0)
280 /* Secure world can communicate via previously unregistered shared memory */
281 #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM	BIT(1)
282 /*
283  * Secure world supports commands "register/unregister shared memory",
284  * secure world accepts command buffers located in any parts of non-secure RAM
285  */
286 #define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM		BIT(2)
287 /* Secure world is built with virtualization support */
288 #define OPTEE_SMC_SEC_CAP_VIRTUALIZATION	BIT(3)
289 /* Secure world supports Shared Memory with a NULL reference */
290 #define OPTEE_SMC_SEC_CAP_MEMREF_NULL		BIT(4)
291 /* Secure world supports asynchronous notification of normal world */
292 #define OPTEE_SMC_SEC_CAP_ASYNC_NOTIF		BIT(5)
293 
294 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES	U(9)
295 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
296 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
297 
298 /*
299  * Disable and empties cache of shared memory objects
300  *
301  * Secure world can cache frequently used shared memory objects, for
302  * example objects used as RPC arguments. When secure world is idle this
303  * function returns one shared memory reference to free. To disable the
304  * cache and free all cached objects this function has to be called until
305  * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
306  *
307  * Call register usage:
308  * a0	SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
309  * a1-6	Not used
310  * a7	Hypervisor Client ID register
311  *
312  * Normal return register usage:
313  * a0	OPTEE_SMC_RETURN_OK
314  * a1	Upper 32 bits of a 64-bit Shared memory cookie
315  * a2	Lower 32 bits of a 64-bit Shared memory cookie
316  * a3-7	Preserved
317  *
318  * Cache empty return register usage:
319  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
320  * a1-7	Preserved
321  *
322  * Not idle return register usage:
323  * a0	OPTEE_SMC_RETURN_EBUSY
324  * a1-7	Preserved
325  */
326 #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE	U(10)
327 #define OPTEE_SMC_DISABLE_SHM_CACHE \
328 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
329 
330 /*
331  * Enable cache of shared memory objects
332  *
333  * Secure world can cache frequently used shared memory objects, for
334  * example objects used as RPC arguments. When secure world is idle this
335  * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
336  * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
337  *
338  * Call register usage:
339  * a0	SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
340  * a1-6	Not used
341  * a7	Hypervisor Client ID register
342  *
343  * Normal return register usage:
344  * a0	OPTEE_SMC_RETURN_OK
345  * a1-7	Preserved
346  *
347  * Not idle return register usage:
348  * a0	OPTEE_SMC_RETURN_EBUSY
349  * a1-7	Preserved
350  */
351 #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE	U(11)
352 #define OPTEE_SMC_ENABLE_SHM_CACHE \
353 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
354 
355 /*
356  * Release of secondary cores
357  *
358  * OP-TEE in secure world is in charge of the release process of secondary
359  * cores. The Rich OS issue the this request to ask OP-TEE to boot up the
360  * secondary cores, go through the OP-TEE per-core initialization, and then
361  * switch to the Non-seCure world with the Rich OS provided entry address.
362  * The secondary cores enter Non-Secure world in SVC mode, with Thumb, FIQ,
363  * IRQ and Abort bits disabled.
364  *
365  * Call register usage:
366  * a0	SMC Function ID, OPTEE_SMC_BOOT_SECONDARY
367  * a1	Index of secondary core to boot
368  * a2	Upper 32 bits of a 64-bit Non-Secure world entry physical address
369  * a3	Lower 32 bits of a 64-bit Non-Secure world entry physical address
370  * a4-7	Not used
371  *
372  * Normal return register usage:
373  * a0	OPTEE_SMC_RETURN_OK
374  * a1-7	Preserved
375  *
376  * Error return:
377  * a0	OPTEE_SMC_RETURN_EBADCMD		Core index out of range
378  * a1-7	Preserved
379  *
380  * Not idle return register usage:
381  * a0	OPTEE_SMC_RETURN_EBUSY
382  * a1-7	Preserved
383  */
384 #define OPTEE_SMC_FUNCID_BOOT_SECONDARY  U(12)
385 #define OPTEE_SMC_BOOT_SECONDARY \
386 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_BOOT_SECONDARY)
387 
388 /*
389  * Inform OP-TEE about a new virtual machine
390  *
391  * Hypervisor issues this call during virtual machine (guest) creation.
392  * OP-TEE records client id of new virtual machine and prepares
393  * to receive requests from it. This call is available only if OP-TEE
394  * was built with virtualization support.
395  *
396  * Call requests usage:
397  * a0	SMC Function ID, OPTEE_SMC_VM_CREATED
398  * a1	Hypervisor Client ID of newly created virtual machine
399  * a2-6 Not used
400  * a7	Hypervisor Client ID register. Must be 0, because only hypervisor
401  *      can issue this call
402  *
403  * Normal return register usage:
404  * a0	OPTEE_SMC_RETURN_OK
405  * a1-7	Preserved
406  *
407  * Error return:
408  * a0	OPTEE_SMC_RETURN_ENOTAVAIL	OP-TEE have no resources for
409  *					another VM
410  * a1-7	Preserved
411  *
412  */
413 #define OPTEE_SMC_FUNCID_VM_CREATED	U(13)
414 #define OPTEE_SMC_VM_CREATED \
415 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_VM_CREATED)
416 
417 /*
418  * Inform OP-TEE about shutdown of a virtual machine
419  *
420  * Hypervisor issues this call during virtual machine (guest) destruction.
421  * OP-TEE will clean up all resources associated with this VM. This call is
422  * available only if OP-TEE was built with virtualization support.
423  *
424  * Call requests usage:
425  * a0	SMC Function ID, OPTEE_SMC_VM_DESTROYED
426  * a1	Hypervisor Client ID of virtual machine being shut down
427  * a2-6 Not used
428  * a7	Hypervisor Client ID register. Must be 0, because only hypervisor
429  *      can issue this call
430  *
431  * Normal return register usage:
432  * a0	OPTEE_SMC_RETURN_OK
433  * a1-7	Preserved
434  *
435  */
436 #define OPTEE_SMC_FUNCID_VM_DESTROYED	U(14)
437 #define OPTEE_SMC_VM_DESTROYED \
438 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_VM_DESTROYED)
439 
440 /*
441  * Query OP-TEE about number of supported threads
442  *
443  * Normal World OS or Hypervisor issues this call to find out how many
444  * threads OP-TEE supports. That is how many standard calls can be issued
445  * in parallel before OP-TEE will return OPTEE_SMC_RETURN_ETHREAD_LIMIT.
446  *
447  * Call requests usage:
448  * a0	SMC Function ID, OPTEE_SMC_GET_THREAD_COUNT
449  * a1-6 Not used
450  * a7	Hypervisor Client ID register
451  *
452  * Normal return register usage:
453  * a0	OPTEE_SMC_RETURN_OK
454  * a1	Number of threads
455  * a2-7 Preserved
456  *
457  * Error return:
458  * a0	OPTEE_SMC_RETURN_UNKNOWN_FUNCTION   Requested call is not implemented
459  * a1-7	Preserved
460  */
461 #define OPTEE_SMC_FUNCID_GET_THREAD_COUNT	U(15)
462 #define OPTEE_SMC_GET_THREAD_COUNT \
463 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)
464 
465 /*
466  * Inform OP-TEE that normal world is able to receive asynchronous
467  * notifications.
468  *
469  * Call requests usage:
470  * a0	SMC Function ID, OPTEE_SMC_ENABLE_ASYNC_NOTIF
471  * a1-6	Not used
472  * a7	Hypervisor Client ID register
473  *
474  * Normal return register usage:
475  * a0	OPTEE_SMC_RETURN_OK
476  * a1-7	Preserved
477  *
478  * Not supported return register usage:
479  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
480  * a1-7	Preserved
481  */
482 #define OPTEE_SMC_FUNCID_ENABLE_ASYNC_NOTIF	16
483 #define OPTEE_SMC_ENABLE_ASYNC_NOTIF \
484 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_ASYNC_NOTIF)
485 
486 /*
487  * Retrieve a value of notifications pending since the last call of this
488  * function.
489  *
490  * OP-TEE keeps a record of all posted values. When an interrupt is
491  * received which indicates that there are posted values this function
492  * should be called until all pended values have been retrieved. When a
493  * value is retrieved it's cleared from the record in secure world.
494  *
495  * It is expected that this function is called from an interrupt handler
496  * in normal world.
497  *
498  * Call requests usage:
499  * a0	SMC Function ID, OPTEE_SMC_GET_ASYNC_NOTIF_VALUE
500  * a1-6	Not used
501  * a7	Hypervisor Client ID register
502  *
503  * Normal return register usage:
504  * a0	OPTEE_SMC_RETURN_OK
505  * a1	value
506  * a2	Bit[0]: OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID if the value in a1 is
507  *		valid, else 0 if no values where pending
508  * a2	Bit[1]: OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING if another value is
509  *		pending, else 0.
510  *	Bit[31:2]: MBZ
511  * a3-7	Preserved
512  *
513  * Not supported return register usage:
514  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
515  * a1-7	Preserved
516  */
517 #define OPTEE_SMC_ASYNC_NOTIF_VALID	BIT(0)
518 #define OPTEE_SMC_ASYNC_NOTIF_PENDING	BIT(1)
519 
520 /*
521  * Notification that OP-TEE expects a yielding call to do some bottom half
522  * work in a driver.
523  */
524 #define OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF	0
525 
526 #define OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE	17
527 #define OPTEE_SMC_GET_ASYNC_NOTIF_VALUE \
528 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE)
529 
530 /*
531  * Resume from RPC (for example after processing a foreign interrupt)
532  *
533  * Call register usage:
534  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
535  * a1-3	Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
536  *	OPTEE_SMC_RETURN_RPC in a0
537  *
538  * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
539  *
540  * Possible return values
541  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
542  *					function.
543  * OPTEE_SMC_RETURN_OK			Original call completed, result
544  *					updated in the previously supplied.
545  *					struct optee_msg_arg
546  * OPTEE_SMC_RETURN_RPC			Call suspended by RPC call to normal
547  *					world.
548  * OPTEE_SMC_RETURN_ERESUME		Resume failed, the opaque resume
549  *					information was corrupt.
550  */
551 #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC	U(3)
552 #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
553 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
554 
555 #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK	U(0xFFFF0000)
556 #define OPTEE_SMC_RETURN_RPC_PREFIX		U(0xFFFF0000)
557 #define OPTEE_SMC_RETURN_RPC_FUNC_MASK		U(0x0000FFFF)
558 
559 #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
560 	((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
561 
562 #define OPTEE_SMC_RPC_VAL(func)		((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
563 
564 /*
565  * Allocate memory for RPC parameter passing. The memory is used to hold a
566  * struct optee_msg_arg.
567  *
568  * "Call" register usage:
569  * a0	This value, OPTEE_SMC_RETURN_RPC_ALLOC
570  * a1	Size in bytes of required argument memory
571  * a2	Not used
572  * a3	Resume information, must be preserved
573  * a4-5	Not used
574  * a6-7	Resume information, must be preserved
575  *
576  * "Return" register usage:
577  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
578  * a1	Upper 32 bits of 64-bit physical pointer to allocated
579  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
580  *	be allocated.
581  * a2	Lower 32 bits of 64-bit physical pointer to allocated
582  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
583  *	be allocated
584  * a3	Preserved
585  * a4	Upper 32 bits of 64-bit Shared memory cookie used when freeing
586  *	the memory or doing an RPC
587  * a5	Lower 32 bits of 64-bit Shared memory cookie used when freeing
588  *	the memory or doing an RPC
589  * a6-7	Preserved
590  */
591 #define OPTEE_SMC_RPC_FUNC_ALLOC	U(0)
592 #define OPTEE_SMC_RETURN_RPC_ALLOC \
593 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
594 
595 /*
596  * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
597  *
598  * "Call" register usage:
599  * a0	This value, OPTEE_SMC_RETURN_RPC_FREE
600  * a1	Upper 32 bits of 64-bit shared memory cookie belonging to this
601  *	argument memory
602  * a2	Lower 32 bits of 64-bit shared memory cookie belonging to this
603  *	argument memory
604  * a3-7	Resume information, must be preserved
605  *
606  * "Return" register usage:
607  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
608  * a1-2	Not used
609  * a3-7	Preserved
610  */
611 #define OPTEE_SMC_RPC_FUNC_FREE		U(2)
612 #define OPTEE_SMC_RETURN_RPC_FREE \
613 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
614 
615 /*
616  * Deliver a foreign interrupt in normal world.
617  *
618  * "Call" register usage:
619  * a0	OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
620  * a1-7	Resume information, must be preserved
621  *
622  * "Return" register usage:
623  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
624  * a1-7	Preserved
625  */
626 #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR	U(4)
627 #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
628 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
629 
630 /*
631  * Do an RPC request. The supplied struct optee_msg_arg tells which
632  * request to do and the parameters for the request. The following fields
633  * are used (the rest are unused):
634  * - cmd		the Request ID
635  * - ret		return value of the request, filled in by normal world
636  * - num_params		number of parameters for the request
637  * - params		the parameters
638  * - param_attrs	attributes of the parameters
639  *
640  * "Call" register usage:
641  * a0	OPTEE_SMC_RETURN_RPC_CMD
642  * a1	Upper 32 bits of a 64-bit Shared memory cookie holding a
643  *	struct optee_msg_arg, must be preserved, only the data should
644  *	be updated
645  * a2	Lower 32 bits of a 64-bit Shared memory cookie holding a
646  *	struct optee_msg_arg, must be preserved, only the data should
647  *	be updated
648  * a3-7	Resume information, must be preserved
649  *
650  * "Return" register usage:
651  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
652  * a1-2	Not used
653  * a3-7	Preserved
654  */
655 #define OPTEE_SMC_RPC_FUNC_CMD		U(5)
656 #define OPTEE_SMC_RETURN_RPC_CMD \
657 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
658 
659 /* Returned in a0 */
660 #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION U(0xFFFFFFFF)
661 
662 /* Returned in a0 only from Trusted OS functions */
663 #define OPTEE_SMC_RETURN_OK		U(0x0)
664 #define OPTEE_SMC_RETURN_ETHREAD_LIMIT	U(0x1)
665 #define OPTEE_SMC_RETURN_EBUSY		U(0x2)
666 #define OPTEE_SMC_RETURN_ERESUME	U(0x3)
667 #define OPTEE_SMC_RETURN_EBADADDR	U(0x4)
668 #define OPTEE_SMC_RETURN_EBADCMD	U(0x5)
669 #define OPTEE_SMC_RETURN_ENOMEM		U(0x6)
670 #define OPTEE_SMC_RETURN_ENOTAVAIL	U(0x7)
671 #define OPTEE_SMC_RETURN_IS_RPC(ret) \
672 	(((ret) != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION) && \
673 	((((ret) & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) == \
674 		OPTEE_SMC_RETURN_RPC_PREFIX)))
675 
676 #endif /* OPTEE_SMC_H */
677