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