xref: /openbmc/u-boot/drivers/tee/optee/optee_smc.h (revision 6744c0d6)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015-2018, Linaro Limited
4  */
5 
6 #ifndef OPTEE_SMC_H
7 #define OPTEE_SMC_H
8 
9 #include <linux/arm-smccc.h>
10 #include <linux/bitops.h>
11 
12 /*
13  * This file is based on
14  * https://github.com/OP-TEE/optee_os/blob/master/core/arch/arm/include/sm/optee_smc.h
15  * and may need to be updated when introducing new features.
16  */
17 
18 #define OPTEE_SMC_STD_CALL_VAL(func_num) \
19 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_STD_CALL, ARM_SMCCC_SMC_32, \
20 			   ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
21 #define OPTEE_SMC_FAST_CALL_VAL(func_num) \
22 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
23 			   ARM_SMCCC_OWNER_TRUSTED_OS, (func_num))
24 
25 /*
26  * Function specified by SMC Calling convention.
27  */
28 #define OPTEE_SMC_FUNCID_CALLS_COUNT	0xFF00
29 #define OPTEE_SMC_CALLS_COUNT \
30 	ARM_SMCCC_CALL_VAL(OPTEE_SMC_FAST_CALL, SMCCC_SMC_32, \
31 			   SMCCC_OWNER_TRUSTED_OS_END, \
32 			   OPTEE_SMC_FUNCID_CALLS_COUNT)
33 
34 /*
35  * Normal cached memory (write-back), shareable for SMP systems and not
36  * shareable for UP systems.
37  */
38 #define OPTEE_SMC_SHM_CACHED		1
39 
40 /*
41  * a0..a7 is used as register names in the descriptions below, on arm32
42  * that translates to r0..r7 and on arm64 to w0..w7. In both cases it's
43  * 32-bit registers.
44  */
45 
46 /*
47  * Function specified by SMC Calling convention
48  *
49  * Return one of the following UIDs if using API specified in this file
50  * without further extentions:
51  * 65cb6b93-af0c-4617-8ed6-644a8d1140f8
52  * see also OPTEE_SMC_UID_* in optee_msg.h
53  */
54 #define OPTEE_SMC_FUNCID_CALLS_UID OPTEE_MSG_FUNCID_CALLS_UID
55 #define OPTEE_SMC_CALLS_UID \
56 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
57 			   ARM_SMCCC_OWNER_TRUSTED_OS_END, \
58 			   OPTEE_SMC_FUNCID_CALLS_UID)
59 
60 /*
61  * Function specified by SMC Calling convention
62  *
63  * Returns 2.0 if using API specified in this file without further extentions.
64  * see also OPTEE_MSG_REVISION_* in optee_msg.h
65  */
66 #define OPTEE_SMC_FUNCID_CALLS_REVISION OPTEE_MSG_FUNCID_CALLS_REVISION
67 #define OPTEE_SMC_CALLS_REVISION \
68 	ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
69 			   ARM_SMCCC_OWNER_TRUSTED_OS_END, \
70 			   OPTEE_SMC_FUNCID_CALLS_REVISION)
71 
72 struct optee_smc_calls_revision_result {
73 	unsigned long major;
74 	unsigned long minor;
75 	unsigned long reserved0;
76 	unsigned long reserved1;
77 };
78 
79 /*
80  * Get UUID of Trusted OS.
81  *
82  * Used by non-secure world to figure out which Trusted OS is installed.
83  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
84  *
85  * Returns UUID in a0-4 in the same way as OPTEE_SMC_CALLS_UID
86  * described above.
87  */
88 #define OPTEE_SMC_FUNCID_GET_OS_UUID OPTEE_MSG_FUNCID_GET_OS_UUID
89 #define OPTEE_SMC_CALL_GET_OS_UUID \
90 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_UUID)
91 
92 /*
93  * Get revision of Trusted OS.
94  *
95  * Used by non-secure world to figure out which version of the Trusted OS
96  * is installed. Note that the returned revision is the revision of the
97  * Trusted OS, not of the API.
98  *
99  * Returns revision in a0-1 in the same way as OPTEE_SMC_CALLS_REVISION
100  * described above. May optionally return a 32-bit build identifier in a2,
101  * with zero meaning unspecified.
102  */
103 #define OPTEE_SMC_FUNCID_GET_OS_REVISION OPTEE_MSG_FUNCID_GET_OS_REVISION
104 #define OPTEE_SMC_CALL_GET_OS_REVISION \
105 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_OS_REVISION)
106 
107 struct optee_smc_call_get_os_revision_result {
108 	unsigned long major;
109 	unsigned long minor;
110 	unsigned long build_id;
111 	unsigned long reserved1;
112 };
113 
114 /*
115  * Call with struct optee_msg_arg as argument
116  *
117  * Call register usage:
118  * a0	SMC Function ID, OPTEE_SMC*CALL_WITH_ARG
119  * a1	Upper 32bit of a 64bit physical pointer to a struct optee_msg_arg
120  * a2	Lower 32bit of a 64bit physical pointer to a struct optee_msg_arg
121  * a3	Cache settings, not used if physical pointer is in a predefined shared
122  *	memory area else per OPTEE_SMC_SHM_*
123  * a4-6	Not used
124  * a7	Hypervisor Client ID register
125  *
126  * Normal return register usage:
127  * a0	Return value, OPTEE_SMC_RETURN_*
128  * a1-3	Not used
129  * a4-7	Preserved
130  *
131  * OPTEE_SMC_RETURN_ETHREAD_LIMIT return register usage:
132  * a0	Return value, OPTEE_SMC_RETURN_ETHREAD_LIMIT
133  * a1-3	Preserved
134  * a4-7	Preserved
135  *
136  * RPC return register usage:
137  * a0	Return value, OPTEE_SMC_RETURN_IS_RPC(val)
138  * a1-2	RPC parameters
139  * a3-7	Resume information, must be preserved
140  *
141  * Possible return values:
142  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
143  *					function.
144  * OPTEE_SMC_RETURN_OK			Call completed, result updated in
145  *					the previously supplied struct
146  *					optee_msg_arg.
147  * OPTEE_SMC_RETURN_ETHREAD_LIMIT	Number of Trusted OS threads exceeded,
148  *					try again later.
149  * OPTEE_SMC_RETURN_EBADADDR		Bad physcial pointer to struct
150  *					optee_msg_arg.
151  * OPTEE_SMC_RETURN_EBADCMD		Bad/unknown cmd in struct optee_msg_arg
152  * OPTEE_SMC_RETURN_IS_RPC()		Call suspended by RPC call to normal
153  *					world.
154  */
155 #define OPTEE_SMC_FUNCID_CALL_WITH_ARG OPTEE_MSG_FUNCID_CALL_WITH_ARG
156 #define OPTEE_SMC_CALL_WITH_ARG \
157 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_CALL_WITH_ARG)
158 
159 /*
160  * Get Shared Memory Config
161  *
162  * Returns the Secure/Non-secure shared memory config.
163  *
164  * Call register usage:
165  * a0	SMC Function ID, OPTEE_SMC_GET_SHM_CONFIG
166  * a1-6	Not used
167  * a7	Hypervisor Client ID register
168  *
169  * Have config return register usage:
170  * a0	OPTEE_SMC_RETURN_OK
171  * a1	Physical address of start of SHM
172  * a2	Size of of SHM
173  * a3	Cache settings of memory, as defined by the
174  *	OPTEE_SMC_SHM_* values above
175  * a4-7	Preserved
176  *
177  * Not available register usage:
178  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
179  * a1-3 Not used
180  * a4-7	Preserved
181  */
182 #define OPTEE_SMC_FUNCID_GET_SHM_CONFIG	7
183 #define OPTEE_SMC_GET_SHM_CONFIG \
184 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_SHM_CONFIG)
185 
186 struct optee_smc_get_shm_config_result {
187 	unsigned long status;
188 	unsigned long start;
189 	unsigned long size;
190 	unsigned long settings;
191 };
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 /*
220  * Secure world supports commands "register/unregister shared memory",
221  * secure world accepts command buffers located in any parts of non-secure RAM
222  */
223 #define OPTEE_SMC_SEC_CAP_DYNAMIC_SHM		BIT(2)
224 
225 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES	9
226 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
227 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
228 
229 struct optee_smc_exchange_capabilities_result {
230 	unsigned long status;
231 	unsigned long capabilities;
232 	unsigned long reserved0;
233 	unsigned long reserved1;
234 };
235 
236 /*
237  * Disable and empties cache of shared memory objects
238  *
239  * Secure world can cache frequently used shared memory objects, for
240  * example objects used as RPC arguments. When secure world is idle this
241  * function returns one shared memory reference to free. To disable the
242  * cache and free all cached objects this function has to be called until
243  * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
244  *
245  * Call register usage:
246  * a0	SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
247  * a1-6	Not used
248  * a7	Hypervisor Client ID register
249  *
250  * Normal return register usage:
251  * a0	OPTEE_SMC_RETURN_OK
252  * a1	Upper 32bit of a 64bit Shared memory cookie
253  * a2	Lower 32bit of a 64bit Shared memory cookie
254  * a3-7	Preserved
255  *
256  * Cache empty return register usage:
257  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
258  * a1-7	Preserved
259  *
260  * Not idle return register usage:
261  * a0	OPTEE_SMC_RETURN_EBUSY
262  * a1-7	Preserved
263  */
264 #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE	10
265 #define OPTEE_SMC_DISABLE_SHM_CACHE \
266 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
267 
268 struct optee_smc_disable_shm_cache_result {
269 	unsigned long status;
270 	unsigned long shm_upper32;
271 	unsigned long shm_lower32;
272 	unsigned long reserved0;
273 };
274 
275 /*
276  * Enable cache of shared memory objects
277  *
278  * Secure world can cache frequently used shared memory objects, for
279  * example objects used as RPC arguments. When secure world is idle this
280  * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
281  * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
282  *
283  * Call register usage:
284  * a0	SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
285  * a1-6	Not used
286  * a7	Hypervisor Client ID register
287  *
288  * Normal return register usage:
289  * a0	OPTEE_SMC_RETURN_OK
290  * a1-7	Preserved
291  *
292  * Not idle return register usage:
293  * a0	OPTEE_SMC_RETURN_EBUSY
294  * a1-7	Preserved
295  */
296 #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE	11
297 #define OPTEE_SMC_ENABLE_SHM_CACHE \
298 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
299 
300 /*
301  * Resume from RPC (for example after processing a foreign interrupt)
302  *
303  * Call register usage:
304  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
305  * a1-3	Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
306  *	OPTEE_SMC_RETURN_RPC in a0
307  *
308  * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
309  *
310  * Possible return values
311  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
312  *					function.
313  * OPTEE_SMC_RETURN_OK			Original call completed, result
314  *					updated in the previously supplied.
315  *					struct optee_msg_arg
316  * OPTEE_SMC_RETURN_RPC			Call suspended by RPC call to normal
317  *					world.
318  * OPTEE_SMC_RETURN_ERESUME		Resume failed, the opaque resume
319  *					information was corrupt.
320  */
321 #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC	3
322 #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
323 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
324 
325 #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK	0xFFFF0000
326 #define OPTEE_SMC_RETURN_RPC_PREFIX		0xFFFF0000
327 #define OPTEE_SMC_RETURN_RPC_FUNC_MASK		0x0000FFFF
328 
329 #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
330 	((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
331 
332 #define OPTEE_SMC_RPC_VAL(func)		((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
333 
334 /*
335  * Allocate memory for RPC parameter passing. The memory is used to hold a
336  * struct optee_msg_arg.
337  *
338  * "Call" register usage:
339  * a0	This value, OPTEE_SMC_RETURN_RPC_ALLOC
340  * a1	Size in bytes of required argument memory
341  * a2	Not used
342  * a3	Resume information, must be preserved
343  * a4-5	Not used
344  * a6-7	Resume information, must be preserved
345  *
346  * "Return" register usage:
347  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
348  * a1	Upper 32bits of 64bit physical pointer to allocated
349  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
350  *	be allocated.
351  * a2	Lower 32bits of 64bit physical pointer to allocated
352  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
353  *	be allocated
354  * a3	Preserved
355  * a4	Upper 32bits of 64bit Shared memory cookie used when freeing
356  *	the memory or doing an RPC
357  * a5	Lower 32bits of 64bit Shared memory cookie used when freeing
358  *	the memory or doing an RPC
359  * a6-7	Preserved
360  */
361 #define OPTEE_SMC_RPC_FUNC_ALLOC	0
362 #define OPTEE_SMC_RETURN_RPC_ALLOC \
363 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
364 
365 /*
366  * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
367  *
368  * "Call" register usage:
369  * a0	This value, OPTEE_SMC_RETURN_RPC_FREE
370  * a1	Upper 32bits of 64bit shared memory cookie belonging to this
371  *	argument memory
372  * a2	Lower 32bits of 64bit shared memory cookie belonging to this
373  *	argument memory
374  * a3-7	Resume information, must be preserved
375  *
376  * "Return" register usage:
377  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
378  * a1-2	Not used
379  * a3-7	Preserved
380  */
381 #define OPTEE_SMC_RPC_FUNC_FREE		2
382 #define OPTEE_SMC_RETURN_RPC_FREE \
383 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
384 
385 /*
386  * Deliver foreign interrupt to normal world.
387  *
388  * "Call" register usage:
389  * a0	OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
390  * a1-7	Resume information, must be preserved
391  *
392  * "Return" register usage:
393  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
394  * a1-7	Preserved
395  */
396 #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR		4
397 #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
398 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
399 
400 /*
401  * Do an RPC request. The supplied struct optee_msg_arg tells which
402  * request to do and the parameters for the request. The following fields
403  * are used (the rest are unused):
404  * - cmd		the Request ID
405  * - ret		return value of the request, filled in by normal world
406  * - num_params		number of parameters for the request
407  * - params		the parameters
408  * - param_attrs	attributes of the parameters
409  *
410  * "Call" register usage:
411  * a0	OPTEE_SMC_RETURN_RPC_CMD
412  * a1	Upper 32bit of a 64bit Shared memory cookie holding a
413  *	struct optee_msg_arg, must be preserved, only the data should
414  *	be updated
415  * a2	Lower 32bit of a 64bit Shared memory cookie holding a
416  *	struct optee_msg_arg, must be preserved, only the data should
417  *	be updated
418  * a3-7	Resume information, must be preserved
419  *
420  * "Return" register usage:
421  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
422  * a1-2	Not used
423  * a3-7	Preserved
424  */
425 #define OPTEE_SMC_RPC_FUNC_CMD		5
426 #define OPTEE_SMC_RETURN_RPC_CMD \
427 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
428 
429 /* Returned in a0 */
430 #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
431 
432 /* Returned in a0 only from Trusted OS functions */
433 #define OPTEE_SMC_RETURN_OK		0x0
434 #define OPTEE_SMC_RETURN_ETHREAD_LIMIT	0x1
435 #define OPTEE_SMC_RETURN_EBUSY		0x2
436 #define OPTEE_SMC_RETURN_ERESUME	0x3
437 #define OPTEE_SMC_RETURN_EBADADDR	0x4
438 #define OPTEE_SMC_RETURN_EBADCMD	0x5
439 #define OPTEE_SMC_RETURN_ENOMEM		0x6
440 #define OPTEE_SMC_RETURN_ENOTAVAIL	0x7
441 #define OPTEE_SMC_RETURN_IS_RPC(ret)	__optee_smc_return_is_rpc((ret))
442 
443 static inline bool __optee_smc_return_is_rpc(u32 ret)
444 {
445 	return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
446 	       (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
447 			OPTEE_SMC_RETURN_RPC_PREFIX;
448 }
449 
450 #endif /* OPTEE_SMC_H */
451