xref: /optee_os/core/include/optee_msg.h (revision 0cbde2726c59633a117a322fbaa5c9a77e5749e1)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015-2020, Linaro Limited
4  */
5 #ifndef __OPTEE_MSG_H
6 #define __OPTEE_MSG_H
7 
8 #include <compiler.h>
9 #include <types_ext.h>
10 #include <util.h>
11 
12 /*
13  * This file defines the OP-TEE message protocol (ABI) used to communicate
14  * with an instance of OP-TEE running in secure world.
15  *
16  * This file is divided into two sections.
17  * 1. Formatting of messages.
18  * 2. Requests from normal world
19  */
20 
21 /*****************************************************************************
22  * Part 1 - formatting of messages
23  *****************************************************************************/
24 
25 #define OPTEE_MSG_ATTR_TYPE_NONE		U(0x0)
26 #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT		U(0x1)
27 #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT	U(0x2)
28 #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT		U(0x3)
29 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT		U(0x5)
30 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT		U(0x6)
31 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT		U(0x7)
32 #define OPTEE_MSG_ATTR_TYPE_FMEM_INPUT		OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
33 #define OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT		OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT
34 #define OPTEE_MSG_ATTR_TYPE_FMEM_INOUT		OPTEE_MSG_ATTR_TYPE_RMEM_INOUT
35 #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT		U(0x9)
36 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT		U(0xa)
37 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT		U(0xb)
38 
39 #define OPTEE_MSG_ATTR_TYPE_MASK		GENMASK_32(7, 0)
40 
41 /*
42  * Meta parameter to be absorbed by the Secure OS and not passed
43  * to the Trusted Application.
44  *
45  * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
46  */
47 #define OPTEE_MSG_ATTR_META			BIT(8)
48 
49 /*
50  * Pointer to a list of pages used to register user-defined SHM buffer.
51  * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
52  * buf_ptr should point to the beginning of the buffer. Buffer will contain
53  * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
54  * that page addresses list. Page addresses are stored as 64 bit values.
55  * Last entry on a page should point to the next page of buffer.
56  * Every entry in buffer should point to a 4k page beginning (12 least
57  * significant bits must be equal to zero).
58  *
59  * 12 least significant bits of optee_msg_param.u.tmem.buf_ptr should hold
60  * page offset of user buffer.
61  *
62  * So, entries should be placed like members of this structure:
63  *
64  * struct page_data {
65  *   uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
66  *   uint64_t next_page_data;
67  * };
68  *
69  * Structure is designed to exactly fit into the page size
70  * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
71  *
72  * The size of 4KB is chosen because this is the smallest page size for ARM
73  * architectures. If REE uses larger pages, it should divide them to 4KB ones.
74  */
75 #define OPTEE_MSG_ATTR_NONCONTIG		BIT(9)
76 
77 /*
78  * Memory attributes for caching passed with temp memrefs. The actual value
79  * used is defined outside the message protocol with the exception of
80  * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
81  * defined for the memory range should be used. If optee_smc.h is used as
82  * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
83  */
84 #define OPTEE_MSG_ATTR_CACHE_SHIFT		U(16)
85 #define OPTEE_MSG_ATTR_CACHE_MASK		GENMASK_32(2, 0)
86 #define OPTEE_MSG_ATTR_CACHE_PREDEFINED		U(0)
87 
88 /*
89  * Same values as TEE_LOGIN_* from TEE Internal API
90  */
91 #define OPTEE_MSG_LOGIN_PUBLIC			U(0x00000000)
92 #define OPTEE_MSG_LOGIN_USER			U(0x00000001)
93 #define OPTEE_MSG_LOGIN_GROUP			U(0x00000002)
94 #define OPTEE_MSG_LOGIN_APPLICATION		U(0x00000004)
95 #define OPTEE_MSG_LOGIN_APPLICATION_USER	U(0x00000005)
96 #define OPTEE_MSG_LOGIN_APPLICATION_GROUP	U(0x00000006)
97 
98 /*
99  * Page size used in non-contiguous buffer entries
100  */
101 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE		U(4096)
102 
103 #define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID	0xffffffffffffffff
104 
105 #ifndef __ASSEMBLER__
106 /**
107  * struct optee_msg_param_tmem - temporary memory reference parameter
108  * @buf_ptr:	Address of the buffer
109  * @size:	Size of the buffer
110  * @shm_ref:	Temporary shared memory reference, pointer to a struct tee_shm
111  *
112  * Secure and normal world communicates pointers as physical address
113  * instead of the virtual address. This is because secure and normal world
114  * have completely independent memory mapping. Normal world can even have a
115  * hypervisor which need to translate the guest physical address (AKA IPA
116  * in ARM documentation) to a real physical address before passing the
117  * structure to secure world.
118  */
119 struct optee_msg_param_tmem {
120 	uint64_t buf_ptr;
121 	uint64_t size;
122 	uint64_t shm_ref;
123 };
124 
125 /**
126  * struct optee_msg_param_rmem - registered memory reference parameter
127  * @offs:	Offset into shared memory reference
128  * @size:	Size of the buffer
129  * @shm_ref:	Shared memory reference, pointer to a struct tee_shm
130  */
131 struct optee_msg_param_rmem {
132 	uint64_t offs;
133 	uint64_t size;
134 	uint64_t shm_ref;
135 };
136 
137 /**
138  * struct optee_msg_param_fmem - FF-A memory reference parameter
139  * @offs_lower:	   Lower bits of offset into shared memory reference
140  * @offs_upper:	   Upper bits of offset into shared memory reference
141  * @internal_offs: Internal offset into the first page of shared memory
142  *		   reference
143  * @size:	   Size of the buffer
144  * @global_id:	   Global identifier of the shared memory
145  */
146 struct optee_msg_param_fmem {
147 	uint32_t offs_low;
148 	uint16_t offs_high;
149 	uint16_t internal_offs;
150 	uint64_t size;
151 	uint64_t global_id;
152 };
153 
154 /**
155  * struct optee_msg_param_value - opaque value parameter
156  *
157  * Value parameters are passed unchecked between normal and secure world.
158  */
159 struct optee_msg_param_value {
160 	uint64_t a;
161 	uint64_t b;
162 	uint64_t c;
163 };
164 
165 /**
166  * struct optee_msg_param - parameter used together with struct optee_msg_arg
167  * @attr:	attributes
168  * @tmem:	parameter by temporary memory reference
169  * @rmem:	parameter by registered memory reference
170  * @fmem:	parameter by FF-A registered memory reference
171  * @value:	parameter by opaque value
172  * @octets:	parameter by octet string
173  *
174  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
175  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,
176  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
177  * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates
178  * @rmem or @fmem depending on the conduit.
179  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
180  */
181 struct optee_msg_param {
182 	uint64_t attr;
183 	union {
184 		struct optee_msg_param_tmem tmem;
185 		struct optee_msg_param_rmem rmem;
186 		struct optee_msg_param_fmem fmem;
187 		struct optee_msg_param_value value;
188 		uint8_t octets[24];
189 	} u;
190 };
191 
192 /**
193  * struct optee_msg_arg - call argument
194  * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
195  * @func: Trusted Application function, specific to the Trusted Application,
196  *	     used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
197  * @session: In parameter for all OPTEE_MSG_CMD_* except
198  *	     OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
199  * @cancel_id: Cancellation id, a unique value to identify this request
200  * @ret: return value
201  * @ret_origin: origin of the return value
202  * @num_params: number of parameters supplied to the OS Command
203  * @params: the parameters supplied to the OS Command
204  *
205  * All normal calls to Trusted OS uses this struct. If cmd requires further
206  * information than what these fields hold it can be passed as a parameter
207  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
208  * attrs field). All parameters tagged as meta have to come first.
209  */
210 struct optee_msg_arg {
211 	uint32_t cmd;
212 	uint32_t func;
213 	uint32_t session;
214 	uint32_t cancel_id;
215 	uint32_t pad;
216 	uint32_t ret;
217 	uint32_t ret_origin;
218 	uint32_t num_params;
219 
220 	/* num_params tells the actual number of element in params */
221 	struct optee_msg_param params[];
222 };
223 
224 /**
225  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
226  *
227  * @num_params: Number of parameters embedded in the struct optee_msg_arg
228  *
229  * Returns the size of the struct optee_msg_arg together with the number
230  * of embedded parameters.
231  */
232 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
233 	(sizeof(struct optee_msg_arg) + \
234 	 sizeof(struct optee_msg_param) * (num_params))
235 
236 /*
237  * Defines the maximum value of @num_params that can be passed to
238  * OPTEE_MSG_GET_ARG_SIZE without a risk of crossing page boundary.
239  */
240 #define OPTEE_MSG_MAX_NUM_PARAMS	\
241 	((OPTEE_MSG_NONCONTIG_PAGE_SIZE - sizeof(struct optee_msg_arg)) / \
242 	 sizeof(struct optee_msg_param))
243 
244 #endif /*__ASSEMBLER__*/
245 
246 /*****************************************************************************
247  * Part 2 - requests from normal world
248  *****************************************************************************/
249 
250 /*
251  * Return the following UID if using API specified in this file without
252  * further extensions:
253  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
254  * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
255  * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
256  */
257 #define OPTEE_MSG_UID_0			U(0x384fb3e0)
258 #define OPTEE_MSG_UID_1			U(0xe7f811e3)
259 #define OPTEE_MSG_UID_2			U(0xaf630002)
260 #define OPTEE_MSG_UID_3			U(0xa5d5c51b)
261 #define OPTEE_MSG_FUNCID_CALLS_UID	U(0xFF01)
262 
263 /*
264  * Returns 2.0 if using API specified in this file without further
265  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
266  * and OPTEE_MSG_REVISION_MINOR
267  */
268 #define OPTEE_MSG_REVISION_MAJOR	U(2)
269 #define OPTEE_MSG_REVISION_MINOR	U(0)
270 #define OPTEE_MSG_FUNCID_CALLS_REVISION	U(0xFF03)
271 
272 /*
273  * Get UUID of Trusted OS.
274  *
275  * Used by non-secure world to figure out which Trusted OS is installed.
276  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
277  *
278  * Returns UUID in 4 32-bit words in the same way as
279  * OPTEE_MSG_FUNCID_CALLS_UID described above.
280  */
281 #define OPTEE_MSG_OS_OPTEE_UUID_0	U(0x486178e0)
282 #define OPTEE_MSG_OS_OPTEE_UUID_1	U(0xe7f811e3)
283 #define OPTEE_MSG_OS_OPTEE_UUID_2	U(0xbc5e0002)
284 #define OPTEE_MSG_OS_OPTEE_UUID_3	U(0xa5d5c51b)
285 #define OPTEE_MSG_FUNCID_GET_OS_UUID	U(0x0000)
286 
287 /*
288  * Get revision of Trusted OS.
289  *
290  * Used by non-secure world to figure out which version of the Trusted OS
291  * is installed. Note that the returned revision is the revision of the
292  * Trusted OS, not of the API.
293  *
294  * Returns revision in 2 32-bit words in the same way as
295  * OPTEE_MSG_CALLS_REVISION described above.
296  */
297 #define OPTEE_MSG_FUNCID_GET_OS_REVISION	U(0x0001)
298 
299 /*
300  * Do a secure call with struct optee_msg_arg as argument
301  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
302  *
303  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
304  * The first two parameters are tagged as meta, holding two value
305  * parameters to pass the following information:
306  * param[0].u.value.a-b uuid of Trusted Application
307  * param[1].u.value.a-b uuid of Client
308  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
309  *
310  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
311  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
312  * Application function, specific to the Trusted Application.
313  *
314  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
315  * Trusted Application.
316  *
317  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
318  *
319  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
320  * information is passed as:
321  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
322  *					[| OPTEE_MSG_ATTR_NONCONTIG]
323  * [in] param[0].u.tmem.buf_ptr		physical address (of first fragment)
324  * [in] param[0].u.tmem.size		size (of first fragment)
325  * [in] param[0].u.tmem.shm_ref		holds shared memory reference
326  *
327  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared
328  * memory reference. The information is passed as:
329  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
330  * [in] param[0].u.rmem.shm_ref		holds shared memory reference
331  * [in] param[0].u.rmem.offs		0
332  * [in] param[0].u.rmem.size		0
333  *
334  * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing
335  * of a driver.
336  *
337  * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is
338  * normal world unable to process asynchronous notifications. Typically
339  * used when the driver is shut down.
340  */
341 #define OPTEE_MSG_CMD_OPEN_SESSION	U(0)
342 #define OPTEE_MSG_CMD_INVOKE_COMMAND	U(1)
343 #define OPTEE_MSG_CMD_CLOSE_SESSION	U(2)
344 #define OPTEE_MSG_CMD_CANCEL		U(3)
345 #define OPTEE_MSG_CMD_REGISTER_SHM	U(4)
346 #define OPTEE_MSG_CMD_UNREGISTER_SHM	U(5)
347 #define OPTEE_MSG_CMD_DO_BOTTOM_HALF	U(6)
348 #define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF	U(7)
349 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG	U(0x0004)
350 
351 #endif /* __OPTEE_MSG_H */
352