xref: /openbmc/linux/drivers/tee/optee/optee_smc.h (revision ef22ccbc)
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  * Call with struct optee_msg_arg as argument
109  *
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_*
116  * a4-6	Not used
117  * a7	Hypervisor Client ID register
118  *
119  * Normal return register usage:
120  * a0	Return value, OPTEE_SMC_RETURN_*
121  * a1-3	Not used
122  * a4-7	Preserved
123  *
124  * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
125  * a0	Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
126  * a1-3	Preserved
127  * a4-7	Preserved
128  *
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
133  *
134  * Possible return values:
135  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
136  *					function.
137  * OPTEE_SMC_RETURN_OK			Call completed, result updated in
138  *					the previously supplied struct
139  *					optee_msg_arg.
140  * OPTEE_SMC_RETURN_ETHREAD_LIMIT	Number of Trusted OS threads exceeded,
141  *					try again later.
142  * OPTEE_SMC_RETURN_EBADADDR		Bad physical pointer to struct
143  *					optee_msg_arg.
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
146  *					world.
147  */
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)
151 
152 /*
153  * Get Shared Memory Config
154  *
155  * Returns the Secure/Non-secure shared memory config.
156  *
157  * Call register usage:
158  * a0	SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
159  * a1-6	Not used
160  * a7	Hypervisor Client ID register
161  *
162  * Have config return register usage:
163  * a0	OPTEE_SMC_RETURN_OK
164  * a1	Physical address of start of SHM
165  * a2	Size of of SHM
166  * a3	Cache settings of memory, as defined by the
167  *	OPTEE_SMC_SHM_* values above
168  * a4-7	Preserved
169  *
170  * Not available register usage:
171  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
172  * a1-3 Not used
173  * a4-7	Preserved
174  */
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)
178 
179 struct optee_smc_get_shm_config_result {
180 	unsigned long status;
181 	unsigned long start;
182 	unsigned long size;
183 	unsigned long settings;
184 };
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 
212 /*
213  * Secure world supports commands "register/unregister shared memory",
214  * secure world accepts command buffers located in any parts of non-secure RAM
215  */
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)
221 
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)
225 
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 /*
234  * Disable and empties cache of shared memory objects
235  *
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.
241  *
242  * Call register usage:
243  * a0	SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
244  * a1-6	Not used
245  * a7	Hypervisor Client ID register
246  *
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
251  * a3-7	Preserved
252  *
253  * Cache empty return register usage:
254  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
255  * a1-7	Preserved
256  *
257  * Not idle return register usage:
258  * a0	OPTEE_SMC_RETURN_EBUSY
259  * a1-7	Preserved
260  */
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)
264 
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;
270 };
271 
272 /*
273  * Enable cache of shared memory objects
274  *
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.
279  *
280  * Call register usage:
281  * a0	SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
282  * a1-6	Not used
283  * a7	Hypervisor Client ID register
284  *
285  * Normal return register usage:
286  * a0	OPTEE_SMC_RETURN_OK
287  * a1-7	Preserved
288  *
289  * Not idle return register usage:
290  * a0	OPTEE_SMC_RETURN_EBUSY
291  * a1-7	Preserved
292  */
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)
296 
297 /*
298  * Query OP-TEE about number of supported threads
299  *
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.
303  *
304  * Call requests usage:
305  * a0	SMC Function ID, OPTEE_SMC_GET_THREAD_COUNT
306  * a1-6 Not used
307  * a7	Hypervisor Client ID register
308  *
309  * Normal return register usage:
310  * a0	OPTEE_SMC_RETURN_OK
311  * a1	Number of threads
312  * a2-7 Preserved
313  *
314  * Error return:
315  * a0	OPTEE_SMC_RETURN_UNKNOWN_FUNCTION   Requested call is not implemented
316  * a1-7	Preserved
317  */
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)
321 
322 /*
323  * Resume from RPC (for example after processing a foreign interrupt)
324  *
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
329  *
330  * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
331  *
332  * Possible return values
333  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
334  *					function.
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
339  *					world.
340  * OPTEE_SMC_RETURN_ERESUME		Resume failed, the opaque resume
341  *					information was corrupt.
342  */
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)
346 
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
350 
351 #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
352 	((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
353 
354 #define OPTEE_SMC_RPC_VAL(func)		((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
355 
356 /*
357  * Allocate memory for RPC parameter passing. The memory is used to hold a
358  * struct optee_msg_arg.
359  *
360  * "Call" register usage:
361  * a0	This value, OPTEE_SMC_RETURN_RPC_ALLOC
362  * a1	Size in bytes of required argument memory
363  * a2	Not used
364  * a3	Resume information, must be preserved
365  * a4-5	Not used
366  * a6-7	Resume information, must be preserved
367  *
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
372  *	be allocated.
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
375  *	be allocated
376  * a3	Preserved
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
381  * a6-7	Preserved
382  */
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)
386 
387 /*
388  * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
389  *
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
393  *	argument memory
394  * a2	Lower 32 bits of 64-bit shared memory cookie belonging to this
395  *	argument memory
396  * a3-7	Resume information, must be preserved
397  *
398  * "Return" register usage:
399  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
400  * a1-2	Not used
401  * a3-7	Preserved
402  */
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)
406 
407 /*
408  * Deliver a foreign interrupt in normal world.
409  *
410  * "Call" register usage:
411  * a0	OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
412  * a1-7	Resume information, must be preserved
413  *
414  * "Return" register usage:
415  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
416  * a1-7	Preserved
417  */
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)
421 
422 /*
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
431  *
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
436  *	be updated
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
439  *	be updated
440  * a3-7	Resume information, must be preserved
441  *
442  * "Return" register usage:
443  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
444  * a1-2	Not used
445  * a3-7	Preserved
446  */
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)
450 
451 /* Returned in a0 */
452 #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
453 
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))
464 
465 static inline bool __optee_smc_return_is_rpc(u32 ret)
466 {
467 	return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
468 	       (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
469 			OPTEE_SMC_RETURN_RPC_PREFIX;
470 }
471 
472 #endif /* OPTEE_SMC_H */
473