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