• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: (GPL-2.0 OR 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 <linux/arm-smccc.h>
9 #include <linux/bitops.h>
10 
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))
17 
18 /*
19  * Function specified by SMC Calling convention.
20  */
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)
26 
27 /*
28  * Normal cached memory (write-back), shareable for SMP systems and not
29  * shareable for UP systems.
30  */
31 #define OPTEE_SMC_SHM_CACHED		1
32 
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 /*
40  * Function specified by SMC Calling convention
41  *
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
46  */
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)
52 
53 /*
54  * Function specified by SMC Calling convention
55  *
56  * Returns 2.0 if using API specified in this file without further extensions.
57  * see also OPTEE_MSG_REVISION_* in optee_msg.h
58  */
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)
64 
65 struct optee_smc_calls_revision_result {
66 	unsigned long major;
67 	unsigned long minor;
68 	unsigned long reserved0;
69 	unsigned long reserved1;
70 };
71 
72 /*
73  * Get UUID of Trusted OS.
74  *
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.
77  *
78  * Returns UUID in a0-4 in the same way as OPTEE_SMC_CALLS_UID
79  * described above.
80  */
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)
84 
85 /*
86  * Get revision of Trusted OS.
87  *
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.
91  *
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.
95  */
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)
99 
100 struct optee_smc_call_get_os_revision_result {
101 	unsigned long major;
102 	unsigned long minor;
103 	unsigned long build_id;
104 	unsigned long reserved1;
105 };
106 
107 /*
108  * Load Trusted OS from optee/tee.bin in the Linux firmware.
109  *
110  * WARNING: Use this cautiously as it could lead to insecure loading of the
111  * Trusted OS.
112  * This SMC instructs EL3 to load a binary and execute it as the Trusted OS.
113  *
114  * Call register usage:
115  * a0 SMC Function ID, OPTEE_SMC_CALL_LOAD_IMAGE
116  * a1 Upper 32bit of a 64bit size for the payload
117  * a2 Lower 32bit of a 64bit size for the payload
118  * a3 Upper 32bit of the physical address for the payload
119  * a4 Lower 32bit of the physical address for the payload
120  *
121  * The payload is in the OP-TEE image format.
122  *
123  * Returns result in a0, 0 on success and an error code otherwise.
124  */
125 #define OPTEE_SMC_FUNCID_LOAD_IMAGE 2
126 #define OPTEE_SMC_CALL_LOAD_IMAGE \
127 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
128 			   ARM_SMCCC_OWNER_TRUSTED_OS_END, \
129 			   OPTEE_SMC_FUNCID_LOAD_IMAGE)
130 
131 /*
132  * Call with struct optee_msg_arg as argument
133  *
134  * When called with OPTEE_SMC_CALL_WITH_RPC_ARG or
135  * OPTEE_SMC_CALL_WITH_REGD_ARG in a0 there is one RPC struct optee_msg_arg
136  * following after the first struct optee_msg_arg. The RPC struct
137  * optee_msg_arg has reserved space for the number of RPC parameters as
138  * returned by OPTEE_SMC_EXCHANGE_CAPABILITIES.
139  *
140  * When calling these functions, normal world has a few responsibilities:
141  * 1. It must be able to handle eventual RPCs
142  * 2. Non-secure interrupts should not be masked
143  * 3. If asynchronous notifications has been negotiated successfully, then
144  *    the interrupt for asynchronous notifications should be unmasked
145  *    during this call.
146  *
147  * Call register usage, OPTEE_SMC_CALL_WITH_ARG and
148  * OPTEE_SMC_CALL_WITH_RPC_ARG:
149  * a0	SMC Function ID, OPTEE_SMC_CALL_WITH_ARG or OPTEE_SMC_CALL_WITH_RPC_ARG
150  * a1	Upper 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
151  * a2	Lower 32 bits of a 64-bit physical pointer to a struct optee_msg_arg
152  * a3	Cache settings, not used if physical pointer is in a predefined shared
153  *	memory area else per OPTEE_SMC_SHM_*
154  * a4-6	Not used
155  * a7	Hypervisor Client ID register
156  *
157  * Call register usage, OPTEE_SMC_CALL_WITH_REGD_ARG:
158  * a0	SMC Function ID, OPTEE_SMC_CALL_WITH_REGD_ARG
159  * a1	Upper 32 bits of a 64-bit shared memory cookie
160  * a2	Lower 32 bits of a 64-bit shared memory cookie
161  * a3	Offset of the struct optee_msg_arg in the shared memory with the
162  *	supplied cookie
163  * a4-6	Not used
164  * a7	Hypervisor Client ID register
165  *
166  * Normal return register usage:
167  * a0	Return value, OPTEE_SMC_RETURN_*
168  * a1-3	Not used
169  * a4-7	Preserved
170  *
171  * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
172  * a0	Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
173  * a1-3	Preserved
174  * a4-7	Preserved
175  *
176  * RPC return register usage:
177  * a0	Return value, OPTEE_SMC_RETURN_IS_RPC(val)
178  * a1-2	RPC parameters
179  * a3-7	Resume information, must be preserved
180  *
181  * Possible return values:
182  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
183  *					function.
184  * OPTEE_SMC_RETURN_OK			Call completed, result updated in
185  *					the previously supplied struct
186  *					optee_msg_arg.
187  * OPTEE_SMC_RETURN_ETHREAD_LIMIT	Number of Trusted OS threads exceeded,
188  *					try again later.
189  * OPTEE_SMC_RETURN_EBADADDR		Bad physical pointer to struct
190  *					optee_msg_arg.
191  * OPTEE_SMC_RETURN_EBADCMD		Bad/unknown cmd in struct optee_msg_arg
192  * OPTEE_SMC_RETURN_IS_RPC()		Call suspended by RPC call to normal
193  *					world.
194  */
195 #define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
196 #define OPTEE_SMC_CALL_WITH_ARG \
197 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
198 #define OPTEE_SMC_CALL_WITH_RPC_ARG \
199 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_RPC_ARG)
200 #define OPTEE_SMC_CALL_WITH_REGD_ARG \
201 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_REGD_ARG)
202 
203 /*
204  * Get Shared Memory Config
205  *
206  * Returns the Secure/Non-secure shared memory config.
207  *
208  * Call register usage:
209  * a0	SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
210  * a1-6	Not used
211  * a7	Hypervisor Client ID register
212  *
213  * Have config return register usage:
214  * a0	OPTEE_SMC_RETURN_OK
215  * a1	Physical address of start of SHM
216  * a2	Size of SHM
217  * a3	Cache settings of memory, as defined by the
218  *	OPTEE_SMC_SHM_* values above
219  * a4-7	Preserved
220  *
221  * Not available register usage:
222  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
223  * a1-3 Not used
224  * a4-7	Preserved
225  */
226 #define OPTEE_SMC_FUNCID_GET_SHM_CONFIG	7
227 #define OPTEE_SMC_GET_SHM_CONFIG \
228 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
229 
230 struct optee_smc_get_shm_config_result {
231 	unsigned long status;
232 	unsigned long start;
233 	unsigned long size;
234 	unsigned long settings;
235 };
236 
237 /*
238  * Exchanges capabilities between normal world and secure world
239  *
240  * Call register usage:
241  * a0	SMC Function ID, OPTEE_SMC_EXCHANGE_CAPABILITIES
242  * a1	bitfield of normal world capabilities OPTEE_SMC_NSEC_CAP_*
243  * a2-6	Not used
244  * a7	Hypervisor Client ID register
245  *
246  * Normal return register usage:
247  * a0	OPTEE_SMC_RETURN_OK
248  * a1	bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
249  * a2	The maximum secure world notification number
250  * a3	Bit[7:0]: Number of parameters needed for RPC to be supplied
251  *		  as the second MSG arg struct for
252  *		  OPTEE_SMC_CALL_WITH_ARG
253  *	Bit[31:8]: Reserved (MBZ)
254  * a4-7	Preserved
255  *
256  * Error return register usage:
257  * a0	OPTEE_SMC_RETURN_ENOTAVAIL, can't use the capabilities from normal world
258  * a1	bitfield of secure world capabilities OPTEE_SMC_SEC_CAP_*
259  * a2-7 Preserved
260  */
261 /* Normal world works as a uniprocessor system */
262 #define OPTEE_SMC_NSEC_CAP_UNIPROCESSOR		BIT(0)
263 /* Secure world has reserved shared memory for normal world to use */
264 #define OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM	BIT(0)
265 /* Secure world can communicate via previously unregistered shared memory */
266 #define OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM	BIT(1)
267 
268 /*
269  * Secure world supports commands "register/unregister shared memory",
270  * secure world accepts command buffers located in any parts of non-secure RAM
271  */
272 #define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM		BIT(2)
273 /* Secure world is built with virtualization support */
274 #define OPTEE_SMC_SEC_CAP_VIRTUALIZATION	BIT(3)
275 /* Secure world supports Shared Memory with a NULL reference */
276 #define OPTEE_SMC_SEC_CAP_MEMREF_NULL		BIT(4)
277 /* Secure world supports asynchronous notification of normal world */
278 #define OPTEE_SMC_SEC_CAP_ASYNC_NOTIF		BIT(5)
279 /* Secure world supports pre-allocating RPC arg struct */
280 #define OPTEE_SMC_SEC_CAP_RPC_ARG		BIT(6)
281 
282 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES	9
283 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
284 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
285 
286 struct optee_smc_exchange_capabilities_result {
287 	unsigned long status;
288 	unsigned long capabilities;
289 	unsigned long max_notif_value;
290 	unsigned long data;
291 };
292 
293 /*
294  * Disable and empties cache of shared memory objects
295  *
296  * Secure world can cache frequently used shared memory objects, for
297  * example objects used as RPC arguments. When secure world is idle this
298  * function returns one shared memory reference to free. To disable the
299  * cache and free all cached objects this function has to be called until
300  * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
301  *
302  * Call register usage:
303  * a0	SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
304  * a1-6	Not used
305  * a7	Hypervisor Client ID register
306  *
307  * Normal return register usage:
308  * a0	OPTEE_SMC_RETURN_OK
309  * a1	Upper 32 bits of a 64-bit Shared memory cookie
310  * a2	Lower 32 bits of a 64-bit Shared memory cookie
311  * a3-7	Preserved
312  *
313  * Cache empty return register usage:
314  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
315  * a1-7	Preserved
316  *
317  * Not idle return register usage:
318  * a0	OPTEE_SMC_RETURN_EBUSY
319  * a1-7	Preserved
320  */
321 #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE	10
322 #define OPTEE_SMC_DISABLE_SHM_CACHE \
323 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
324 
325 struct optee_smc_disable_shm_cache_result {
326 	unsigned long status;
327 	unsigned long shm_upper32;
328 	unsigned long shm_lower32;
329 	unsigned long reserved0;
330 };
331 
332 /*
333  * Enable cache of shared memory objects
334  *
335  * Secure world can cache frequently used shared memory objects, for
336  * example objects used as RPC arguments. When secure world is idle this
337  * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
338  * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
339  *
340  * Call register usage:
341  * a0	SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
342  * a1-6	Not used
343  * a7	Hypervisor Client ID register
344  *
345  * Normal return register usage:
346  * a0	OPTEE_SMC_RETURN_OK
347  * a1-7	Preserved
348  *
349  * Not idle return register usage:
350  * a0	OPTEE_SMC_RETURN_EBUSY
351  * a1-7	Preserved
352  */
353 #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE	11
354 #define OPTEE_SMC_ENABLE_SHM_CACHE \
355 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
356 
357 /*
358  * Query OP-TEE about number of supported threads
359  *
360  * Normal World OS or Hypervisor issues this call to find out how many
361  * threads OP-TEE supports. That is how many standard calls can be issued
362  * in parallel before OP-TEE will return OPTEE_SMC_RETURN_ETHREAD_LIMIT.
363  *
364  * Call requests usage:
365  * a0	SMC Function ID, OPTEE_SMC_GET_THREAD_COUNT
366  * a1-6 Not used
367  * a7	Hypervisor Client ID register
368  *
369  * Normal return register usage:
370  * a0	OPTEE_SMC_RETURN_OK
371  * a1	Number of threads
372  * a2-7 Preserved
373  *
374  * Error return:
375  * a0	OPTEE_SMC_RETURN_UNKNOWN_FUNCTION   Requested call is not implemented
376  * a1-7	Preserved
377  */
378 #define OPTEE_SMC_FUNCID_GET_THREAD_COUNT	15
379 #define OPTEE_SMC_GET_THREAD_COUNT \
380 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_THREAD_COUNT)
381 
382 /*
383  * Inform OP-TEE that normal world is able to receive asynchronous
384  * notifications.
385  *
386  * Call requests usage:
387  * a0	SMC Function ID, OPTEE_SMC_ENABLE_ASYNC_NOTIF
388  * a1-6	Not used
389  * a7	Hypervisor Client ID register
390  *
391  * Normal return register usage:
392  * a0	OPTEE_SMC_RETURN_OK
393  * a1-7	Preserved
394  *
395  * Not supported return register usage:
396  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
397  * a1-7	Preserved
398  */
399 #define OPTEE_SMC_FUNCID_ENABLE_ASYNC_NOTIF	16
400 #define OPTEE_SMC_ENABLE_ASYNC_NOTIF \
401 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_ASYNC_NOTIF)
402 
403 /*
404  * Retrieve a value of notifications pending since the last call of this
405  * function.
406  *
407  * OP-TEE keeps a record of all posted values. When an interrupt is
408  * received which indicates that there are posted values this function
409  * should be called until all pended values have been retrieved. When a
410  * value is retrieved, it's cleared from the record in secure world.
411  *
412  * It is expected that this function is called from an interrupt handler
413  * in normal world.
414  *
415  * Call requests usage:
416  * a0	SMC Function ID, OPTEE_SMC_GET_ASYNC_NOTIF_VALUE
417  * a1-6	Not used
418  * a7	Hypervisor Client ID register
419  *
420  * Normal return register usage:
421  * a0	OPTEE_SMC_RETURN_OK
422  * a1	value
423  * a2	Bit[0]: OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID if the value in a1 is
424  *		valid, else 0 if no values where pending
425  * a2	Bit[1]: OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING if another value is
426  *		pending, else 0.
427  *	Bit[31:2]: MBZ
428  * a3-7	Preserved
429  *
430  * Not supported return register usage:
431  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
432  * a1-7	Preserved
433  */
434 #define OPTEE_SMC_ASYNC_NOTIF_VALUE_VALID	BIT(0)
435 #define OPTEE_SMC_ASYNC_NOTIF_VALUE_PENDING	BIT(1)
436 
437 /*
438  * Notification that OP-TEE expects a yielding call to do some bottom half
439  * work in a driver.
440  */
441 #define OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF	0
442 
443 #define OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE	17
444 #define OPTEE_SMC_GET_ASYNC_NOTIF_VALUE \
445 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE)
446 
447 /* See OPTEE_SMC_CALL_WITH_RPC_ARG above */
448 #define OPTEE_SMC_FUNCID_CALL_WITH_RPC_ARG	18
449 
450 /* See OPTEE_SMC_CALL_WITH_REGD_ARG above */
451 #define OPTEE_SMC_FUNCID_CALL_WITH_REGD_ARG	19
452 
453 /*
454  * Resume from RPC (for example after processing a foreign interrupt)
455  *
456  * Call register usage:
457  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
458  * a1-3	Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
459  *	OPTEE_SMC_RETURN_RPC in a0
460  *
461  * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
462  *
463  * Possible return values
464  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
465  *					function.
466  * OPTEE_SMC_RETURN_OK			Original call completed, result
467  *					updated in the previously supplied.
468  *					struct optee_msg_arg
469  * OPTEE_SMC_RETURN_RPC			Call suspended by RPC call to normal
470  *					world.
471  * OPTEE_SMC_RETURN_ERESUME		Resume failed, the opaque resume
472  *					information was corrupt.
473  */
474 #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC	3
475 #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
476 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
477 
478 #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK	0xFFFF0000
479 #define OPTEE_SMC_RETURN_RPC_PREFIX		0xFFFF0000
480 #define OPTEE_SMC_RETURN_RPC_FUNC_MASK		0x0000FFFF
481 
482 #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
483 	((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
484 
485 #define OPTEE_SMC_RPC_VAL(func)		((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
486 
487 /*
488  * Allocate memory for RPC parameter passing. The memory is used to hold a
489  * struct optee_msg_arg.
490  *
491  * "Call" register usage:
492  * a0	This value, OPTEE_SMC_RETURN_RPC_ALLOC
493  * a1	Size in bytes of required argument memory
494  * a2	Not used
495  * a3	Resume information, must be preserved
496  * a4-5	Not used
497  * a6-7	Resume information, must be preserved
498  *
499  * "Return" register usage:
500  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
501  * a1	Upper 32 bits of 64-bit physical pointer to allocated
502  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
503  *	be allocated.
504  * a2	Lower 32 bits of 64-bit physical pointer to allocated
505  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
506  *	be allocated
507  * a3	Preserved
508  * a4	Upper 32 bits of 64-bit Shared memory cookie used when freeing
509  *	the memory or doing an RPC
510  * a5	Lower 32 bits of 64-bit Shared memory cookie used when freeing
511  *	the memory or doing an RPC
512  * a6-7	Preserved
513  */
514 #define OPTEE_SMC_RPC_FUNC_ALLOC	0
515 #define OPTEE_SMC_RETURN_RPC_ALLOC \
516 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
517 
518 /*
519  * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
520  *
521  * "Call" register usage:
522  * a0	This value, OPTEE_SMC_RETURN_RPC_FREE
523  * a1	Upper 32 bits of 64-bit shared memory cookie belonging to this
524  *	argument memory
525  * a2	Lower 32 bits of 64-bit shared memory cookie belonging to this
526  *	argument memory
527  * a3-7	Resume information, must be preserved
528  *
529  * "Return" register usage:
530  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
531  * a1-2	Not used
532  * a3-7	Preserved
533  */
534 #define OPTEE_SMC_RPC_FUNC_FREE		2
535 #define OPTEE_SMC_RETURN_RPC_FREE \
536 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
537 
538 /*
539  * Deliver a foreign interrupt in normal world.
540  *
541  * "Call" register usage:
542  * a0	OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
543  * a1-7	Resume information, must be preserved
544  *
545  * "Return" register usage:
546  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
547  * a1-7	Preserved
548  */
549 #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR	4
550 #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
551 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
552 
553 /*
554  * Do an RPC request. The supplied struct optee_msg_arg tells which
555  * request to do and the parameters for the request. The following fields
556  * are used (the rest are unused):
557  * - cmd		the Request ID
558  * - ret		return value of the request, filled in by normal world
559  * - num_params		number of parameters for the request
560  * - params		the parameters
561  * - param_attrs	attributes of the parameters
562  *
563  * "Call" register usage:
564  * a0	OPTEE_SMC_RETURN_RPC_CMD
565  * a1	Upper 32 bits of a 64-bit Shared memory cookie holding a
566  *	struct optee_msg_arg, must be preserved, only the data should
567  *	be updated
568  * a2	Lower 32 bits of a 64-bit Shared memory cookie holding a
569  *	struct optee_msg_arg, must be preserved, only the data should
570  *	be updated
571  * a3-7	Resume information, must be preserved
572  *
573  * "Return" register usage:
574  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
575  * a1-2	Not used
576  * a3-7	Preserved
577  */
578 #define OPTEE_SMC_RPC_FUNC_CMD		5
579 #define OPTEE_SMC_RETURN_RPC_CMD \
580 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
581 
582 /* Returned in a0 */
583 #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
584 
585 /* Returned in a0 only from Trusted OS functions */
586 #define OPTEE_SMC_RETURN_OK		0x0
587 #define OPTEE_SMC_RETURN_ETHREAD_LIMIT	0x1
588 #define OPTEE_SMC_RETURN_EBUSY		0x2
589 #define OPTEE_SMC_RETURN_ERESUME	0x3
590 #define OPTEE_SMC_RETURN_EBADADDR	0x4
591 #define OPTEE_SMC_RETURN_EBADCMD	0x5
592 #define OPTEE_SMC_RETURN_ENOMEM		0x6
593 #define OPTEE_SMC_RETURN_ENOTAVAIL	0x7
594 #define OPTEE_SMC_RETURN_IS_RPC(ret)	__optee_smc_return_is_rpc((ret))
595 
__optee_smc_return_is_rpc(u32 ret)596 static inline bool __optee_smc_return_is_rpc(u32 ret)
597 {
598 	return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
599 	       (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
600 			OPTEE_SMC_RETURN_RPC_PREFIX;
601 }
602 
603 #endif /* OPTEE_SMC_H */
604