xref: /openbmc/linux/drivers/tee/optee/optee_smc.h (revision 70a59dd8)
1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
2 /*
3  * Copyright (c) 2015-2019, 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 one of the following UIDs if using API specified in this file
43  * without further extentions:
44  * 65cb6b93-af0c-4617-8ed6-644a8d1140f8
45  * see also OPTEE_SMC_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 extentions.
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 32bit of a 64bit physical pointer to a struct optee_msg_arg
113  * a2	Lower 32bit of a 64bit 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 physcial 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 
218 /* Secure world supports Shared Memory with a NULL buffer reference */
219 #define OPTEE_SMC_SEC_CAP_MEMREF_NULL		BIT(4)
220 
221 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES	9
222 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \
223 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES)
224 
225 struct optee_smc_exchange_capabilities_result {
226 	unsigned long status;
227 	unsigned long capabilities;
228 	unsigned long reserved0;
229 	unsigned long reserved1;
230 };
231 
232 /*
233  * Disable and empties cache of shared memory objects
234  *
235  * Secure world can cache frequently used shared memory objects, for
236  * example objects used as RPC arguments. When secure world is idle this
237  * function returns one shared memory reference to free. To disable the
238  * cache and free all cached objects this function has to be called until
239  * it returns OPTEE_SMC_RETURN_ENOTAVAIL.
240  *
241  * Call register usage:
242  * a0	SMC Function ID, OPTEE_SMC_DISABLE_SHM_CACHE
243  * a1-6	Not used
244  * a7	Hypervisor Client ID register
245  *
246  * Normal return register usage:
247  * a0	OPTEE_SMC_RETURN_OK
248  * a1	Upper 32bit of a 64bit Shared memory cookie
249  * a2	Lower 32bit of a 64bit Shared memory cookie
250  * a3-7	Preserved
251  *
252  * Cache empty return register usage:
253  * a0	OPTEE_SMC_RETURN_ENOTAVAIL
254  * a1-7	Preserved
255  *
256  * Not idle return register usage:
257  * a0	OPTEE_SMC_RETURN_EBUSY
258  * a1-7	Preserved
259  */
260 #define OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE	10
261 #define OPTEE_SMC_DISABLE_SHM_CACHE \
262 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_DISABLE_SHM_CACHE)
263 
264 struct optee_smc_disable_shm_cache_result {
265 	unsigned long status;
266 	unsigned long shm_upper32;
267 	unsigned long shm_lower32;
268 	unsigned long reserved0;
269 };
270 
271 /*
272  * Enable cache of shared memory objects
273  *
274  * Secure world can cache frequently used shared memory objects, for
275  * example objects used as RPC arguments. When secure world is idle this
276  * function returns OPTEE_SMC_RETURN_OK and the cache is enabled. If
277  * secure world isn't idle OPTEE_SMC_RETURN_EBUSY is returned.
278  *
279  * Call register usage:
280  * a0	SMC Function ID, OPTEE_SMC_ENABLE_SHM_CACHE
281  * a1-6	Not used
282  * a7	Hypervisor Client ID register
283  *
284  * Normal return register usage:
285  * a0	OPTEE_SMC_RETURN_OK
286  * a1-7	Preserved
287  *
288  * Not idle return register usage:
289  * a0	OPTEE_SMC_RETURN_EBUSY
290  * a1-7	Preserved
291  */
292 #define OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE	11
293 #define OPTEE_SMC_ENABLE_SHM_CACHE \
294 	OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_ENABLE_SHM_CACHE)
295 
296 /*
297  * Resume from RPC (for example after processing a foreign interrupt)
298  *
299  * Call register usage:
300  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC
301  * a1-3	Value of a1-3 when OPTEE_SMC_CALL_WITH_ARG returned
302  *	OPTEE_SMC_RETURN_RPC in a0
303  *
304  * Return register usage is the same as for OPTEE_SMC_*CALL_WITH_ARG above.
305  *
306  * Possible return values
307  * OPTEE_SMC_RETURN_UNKNOWN_FUNCTION	Trusted OS does not recognize this
308  *					function.
309  * OPTEE_SMC_RETURN_OK			Original call completed, result
310  *					updated in the previously supplied.
311  *					struct optee_msg_arg
312  * OPTEE_SMC_RETURN_RPC			Call suspended by RPC call to normal
313  *					world.
314  * OPTEE_SMC_RETURN_ERESUME		Resume failed, the opaque resume
315  *					information was corrupt.
316  */
317 #define OPTEE_SMC_FUNCID_RETURN_FROM_RPC	3
318 #define OPTEE_SMC_CALL_RETURN_FROM_RPC \
319 	OPTEE_SMC_STD_CALL_VAL(OPTEE_SMC_FUNCID_RETURN_FROM_RPC)
320 
321 #define OPTEE_SMC_RETURN_RPC_PREFIX_MASK	0xFFFF0000
322 #define OPTEE_SMC_RETURN_RPC_PREFIX		0xFFFF0000
323 #define OPTEE_SMC_RETURN_RPC_FUNC_MASK		0x0000FFFF
324 
325 #define OPTEE_SMC_RETURN_GET_RPC_FUNC(ret) \
326 	((ret) & OPTEE_SMC_RETURN_RPC_FUNC_MASK)
327 
328 #define OPTEE_SMC_RPC_VAL(func)		((func) | OPTEE_SMC_RETURN_RPC_PREFIX)
329 
330 /*
331  * Allocate memory for RPC parameter passing. The memory is used to hold a
332  * struct optee_msg_arg.
333  *
334  * "Call" register usage:
335  * a0	This value, OPTEE_SMC_RETURN_RPC_ALLOC
336  * a1	Size in bytes of required argument memory
337  * a2	Not used
338  * a3	Resume information, must be preserved
339  * a4-5	Not used
340  * a6-7	Resume information, must be preserved
341  *
342  * "Return" register usage:
343  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
344  * a1	Upper 32bits of 64bit physical pointer to allocated
345  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
346  *	be allocated.
347  * a2	Lower 32bits of 64bit physical pointer to allocated
348  *	memory, (a1 == 0 && a2 == 0) if size was 0 or if memory can't
349  *	be allocated
350  * a3	Preserved
351  * a4	Upper 32bits of 64bit Shared memory cookie used when freeing
352  *	the memory or doing an RPC
353  * a5	Lower 32bits of 64bit Shared memory cookie used when freeing
354  *	the memory or doing an RPC
355  * a6-7	Preserved
356  */
357 #define OPTEE_SMC_RPC_FUNC_ALLOC	0
358 #define OPTEE_SMC_RETURN_RPC_ALLOC \
359 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_ALLOC)
360 
361 /*
362  * Free memory previously allocated by OPTEE_SMC_RETURN_RPC_ALLOC
363  *
364  * "Call" register usage:
365  * a0	This value, OPTEE_SMC_RETURN_RPC_FREE
366  * a1	Upper 32bits of 64bit shared memory cookie belonging to this
367  *	argument memory
368  * a2	Lower 32bits of 64bit shared memory cookie belonging to this
369  *	argument memory
370  * a3-7	Resume information, must be preserved
371  *
372  * "Return" register usage:
373  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
374  * a1-2	Not used
375  * a3-7	Preserved
376  */
377 #define OPTEE_SMC_RPC_FUNC_FREE		2
378 #define OPTEE_SMC_RETURN_RPC_FREE \
379 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FREE)
380 
381 /*
382  * Deliver foreign interrupt to normal world.
383  *
384  * "Call" register usage:
385  * a0	OPTEE_SMC_RETURN_RPC_FOREIGN_INTR
386  * a1-7	Resume information, must be preserved
387  *
388  * "Return" register usage:
389  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
390  * a1-7	Preserved
391  */
392 #define OPTEE_SMC_RPC_FUNC_FOREIGN_INTR		4
393 #define OPTEE_SMC_RETURN_RPC_FOREIGN_INTR \
394 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_FOREIGN_INTR)
395 
396 /*
397  * Do an RPC request. The supplied struct optee_msg_arg tells which
398  * request to do and the parameters for the request. The following fields
399  * are used (the rest are unused):
400  * - cmd		the Request ID
401  * - ret		return value of the request, filled in by normal world
402  * - num_params		number of parameters for the request
403  * - params		the parameters
404  * - param_attrs	attributes of the parameters
405  *
406  * "Call" register usage:
407  * a0	OPTEE_SMC_RETURN_RPC_CMD
408  * a1	Upper 32bit of a 64bit Shared memory cookie holding a
409  *	struct optee_msg_arg, must be preserved, only the data should
410  *	be updated
411  * a2	Lower 32bit of a 64bit Shared memory cookie holding a
412  *	struct optee_msg_arg, must be preserved, only the data should
413  *	be updated
414  * a3-7	Resume information, must be preserved
415  *
416  * "Return" register usage:
417  * a0	SMC Function ID, OPTEE_SMC_CALL_RETURN_FROM_RPC.
418  * a1-2	Not used
419  * a3-7	Preserved
420  */
421 #define OPTEE_SMC_RPC_FUNC_CMD		5
422 #define OPTEE_SMC_RETURN_RPC_CMD \
423 	OPTEE_SMC_RPC_VAL(OPTEE_SMC_RPC_FUNC_CMD)
424 
425 /* Returned in a0 */
426 #define OPTEE_SMC_RETURN_UNKNOWN_FUNCTION 0xFFFFFFFF
427 
428 /* Returned in a0 only from Trusted OS functions */
429 #define OPTEE_SMC_RETURN_OK		0x0
430 #define OPTEE_SMC_RETURN_ETHREAD_LIMIT	0x1
431 #define OPTEE_SMC_RETURN_EBUSY		0x2
432 #define OPTEE_SMC_RETURN_ERESUME	0x3
433 #define OPTEE_SMC_RETURN_EBADADDR	0x4
434 #define OPTEE_SMC_RETURN_EBADCMD	0x5
435 #define OPTEE_SMC_RETURN_ENOMEM		0x6
436 #define OPTEE_SMC_RETURN_ENOTAVAIL	0x7
437 #define OPTEE_SMC_RETURN_IS_RPC(ret)	__optee_smc_return_is_rpc((ret))
438 
439 static inline bool __optee_smc_return_is_rpc(u32 ret)
440 {
441 	return ret != OPTEE_SMC_RETURN_UNKNOWN_FUNCTION &&
442 	       (ret & OPTEE_SMC_RETURN_RPC_PREFIX_MASK) ==
443 			OPTEE_SMC_RETURN_RPC_PREFIX;
444 }
445 
446 #endif /* OPTEE_SMC_H */
447