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