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		U(0x0)
22 #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT		U(0x1)
23 #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT	U(0x2)
24 #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT		U(0x3)
25 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT		U(0x5)
26 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT		U(0x6)
27 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT		U(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		U(0x9)
32 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT		U(0xa)
33 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT		U(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		U(16)
81 #define OPTEE_MSG_ATTR_CACHE_MASK		GENMASK_32(2, 0)
82 #define OPTEE_MSG_ATTR_CACHE_PREDEFINED		U(0)
83 
84 /*
85  * Same values as TEE_LOGIN_* from TEE Internal API
86  */
87 #define OPTEE_MSG_LOGIN_PUBLIC			U(0x00000000)
88 #define OPTEE_MSG_LOGIN_USER			U(0x00000001)
89 #define OPTEE_MSG_LOGIN_GROUP			U(0x00000002)
90 #define OPTEE_MSG_LOGIN_APPLICATION		U(0x00000004)
91 #define OPTEE_MSG_LOGIN_APPLICATION_USER	U(0x00000005)
92 #define OPTEE_MSG_LOGIN_APPLICATION_GROUP	U(0x00000006)
93 
94 /*
95  * Page size used in non-contiguous buffer entries
96  */
97 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE		U(4096)
98 
99 #define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID	0xffffffffffffffff
100 
101 #ifndef __ASSEMBLER__
102 /**
103  * struct optee_msg_param_tmem - temporary memory reference parameter
104  * @buf_ptr:	Address of the buffer
105  * @size:	Size of the buffer
106  * @shm_ref:	Temporary shared memory reference, pointer to a struct tee_shm
107  *
108  * Secure and normal world communicates pointers as physical address
109  * instead of the virtual address. This is because secure and normal world
110  * have completely independent memory mapping. Normal world can even have a
111  * hypervisor which need to translate the guest physical address (AKA IPA
112  * in ARM documentation) to a real physical address before passing the
113  * structure to secure world.
114  */
115 struct optee_msg_param_tmem {
116 	uint64_t buf_ptr;
117 	uint64_t size;
118 	uint64_t shm_ref;
119 };
120 
121 /**
122  * struct optee_msg_param_rmem - registered memory reference parameter
123  * @offs:	Offset into shared memory reference
124  * @size:	Size of the buffer
125  * @shm_ref:	Shared memory reference, pointer to a struct tee_shm
126  */
127 struct optee_msg_param_rmem {
128 	uint64_t offs;
129 	uint64_t size;
130 	uint64_t shm_ref;
131 };
132 
133 /**
134  * struct optee_msg_param_fmem - FF-A memory reference parameter
135  * @offs_lower:	   Lower bits of offset into shared memory reference
136  * @offs_upper:	   Upper bits of offset into shared memory reference
137  * @internal_offs: Internal offset into the first page of shared memory
138  *		   reference
139  * @size:	   Size of the buffer
140  * @global_id:	   Global identifier of the shared memory
141  */
142 struct optee_msg_param_fmem {
143 	uint32_t offs_low;
144 	uint16_t offs_high;
145 	uint16_t internal_offs;
146 	uint64_t size;
147 	uint64_t global_id;
148 };
149 
150 /**
151  * struct optee_msg_param_value - opaque value parameter
152  *
153  * Value parameters are passed unchecked between normal and secure world.
154  */
155 struct optee_msg_param_value {
156 	uint64_t a;
157 	uint64_t b;
158 	uint64_t c;
159 };
160 
161 /**
162  * struct optee_msg_param - parameter used together with struct optee_msg_arg
163  * @attr:	attributes
164  * @tmem:	parameter by temporary memory reference
165  * @rmem:	parameter by registered memory reference
166  * @fmem:	parameter by FF-A registered memory reference
167  * @value:	parameter by opaque value
168  *
169  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
170  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value,
171  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
172  * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates
173  * @rmem or @fmem depending on the conduit.
174  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
175  */
176 struct optee_msg_param {
177 	uint64_t attr;
178 	union {
179 		struct optee_msg_param_tmem tmem;
180 		struct optee_msg_param_rmem rmem;
181 		struct optee_msg_param_fmem fmem;
182 		struct optee_msg_param_value value;
183 	} u;
184 };
185 
186 /**
187  * struct optee_msg_arg - call argument
188  * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
189  * @func: Trusted Application function, specific to the Trusted Application,
190  *	     used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
191  * @session: In parameter for all OPTEE_MSG_CMD_* except
192  *	     OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
193  * @cancel_id: Cancellation id, a unique value to identify this request
194  * @ret: return value
195  * @ret_origin: origin of the return value
196  * @num_params: number of parameters supplied to the OS Command
197  * @params: the parameters supplied to the OS Command
198  *
199  * All normal calls to Trusted OS uses this struct. If cmd requires further
200  * information than what these fields hold it can be passed as a parameter
201  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
202  * attrs field). All parameters tagged as meta have to come first.
203  */
204 struct optee_msg_arg {
205 	uint32_t cmd;
206 	uint32_t func;
207 	uint32_t session;
208 	uint32_t cancel_id;
209 	uint32_t pad;
210 	uint32_t ret;
211 	uint32_t ret_origin;
212 	uint32_t num_params;
213 
214 	/* num_params tells the actual number of element in params */
215 	struct optee_msg_param params[];
216 };
217 
218 /**
219  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
220  *
221  * @num_params: Number of parameters embedded in the struct optee_msg_arg
222  *
223  * Returns the size of the struct optee_msg_arg together with the number
224  * of embedded parameters.
225  */
226 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
227 	(sizeof(struct optee_msg_arg) + \
228 	 sizeof(struct optee_msg_param) * (num_params))
229 
230 /*
231  * Defines the maximum value of @num_params that can be passed to
232  * OPTEE_MSG_GET_ARG_SIZE without a risk of crossing page boundary.
233  */
234 #define OPTEE_MSG_MAX_NUM_PARAMS	\
235 	((OPTEE_MSG_NONCONTIG_PAGE_SIZE - sizeof(struct optee_msg_arg)) / \
236 	 sizeof(struct optee_msg_param))
237 
238 #endif /*__ASSEMBLER__*/
239 
240 /*****************************************************************************
241  * Part 2 - requests from normal world
242  *****************************************************************************/
243 
244 /*
245  * Return the following UID if using API specified in this file without
246  * further extensions:
247  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
248  * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
249  * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
250  */
251 #define OPTEE_MSG_UID_0			U(0x384fb3e0)
252 #define OPTEE_MSG_UID_1			U(0xe7f811e3)
253 #define OPTEE_MSG_UID_2			U(0xaf630002)
254 #define OPTEE_MSG_UID_3			U(0xa5d5c51b)
255 #define OPTEE_MSG_FUNCID_CALLS_UID	U(0xFF01)
256 
257 /*
258  * Returns 2.0 if using API specified in this file without further
259  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
260  * and OPTEE_MSG_REVISION_MINOR
261  */
262 #define OPTEE_MSG_REVISION_MAJOR	U(2)
263 #define OPTEE_MSG_REVISION_MINOR	U(0)
264 #define OPTEE_MSG_FUNCID_CALLS_REVISION	U(0xFF03)
265 
266 /*
267  * Get UUID of Trusted OS.
268  *
269  * Used by non-secure world to figure out which Trusted OS is installed.
270  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
271  *
272  * Returns UUID in 4 32-bit words in the same way as
273  * OPTEE_MSG_FUNCID_CALLS_UID described above.
274  */
275 #define OPTEE_MSG_OS_OPTEE_UUID_0	U(0x486178e0)
276 #define OPTEE_MSG_OS_OPTEE_UUID_1	U(0xe7f811e3)
277 #define OPTEE_MSG_OS_OPTEE_UUID_2	U(0xbc5e0002)
278 #define OPTEE_MSG_OS_OPTEE_UUID_3	U(0xa5d5c51b)
279 #define OPTEE_MSG_FUNCID_GET_OS_UUID	U(0x0000)
280 
281 /*
282  * Get revision of Trusted OS.
283  *
284  * Used by non-secure world to figure out which version of the Trusted OS
285  * is installed. Note that the returned revision is the revision of the
286  * Trusted OS, not of the API.
287  *
288  * Returns revision in 2 32-bit words in the same way as
289  * OPTEE_MSG_CALLS_REVISION described above.
290  */
291 #define OPTEE_MSG_FUNCID_GET_OS_REVISION	U(0x0001)
292 
293 /*
294  * Do a secure call with struct optee_msg_arg as argument
295  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
296  *
297  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
298  * The first two parameters are tagged as meta, holding two value
299  * parameters to pass the following information:
300  * param[0].u.value.a-b uuid of Trusted Application
301  * param[1].u.value.a-b uuid of Client
302  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
303  *
304  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
305  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
306  * Application function, specific to the Trusted Application.
307  *
308  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
309  * Trusted Application.
310  *
311  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
312  *
313  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
314  * information is passed as:
315  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
316  *					[| OPTEE_MSG_ATTR_NONCONTIG]
317  * [in] param[0].u.tmem.buf_ptr		physical address (of first fragment)
318  * [in] param[0].u.tmem.size		size (of first fragment)
319  * [in] param[0].u.tmem.shm_ref		holds shared memory reference
320  *
321  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisteres a previously registered shared
322  * memory reference. The information is passed as:
323  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
324  * [in] param[0].u.rmem.shm_ref		holds shared memory reference
325  * [in] param[0].u.rmem.offs		0
326  * [in] param[0].u.rmem.size		0
327  *
328  * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing
329  * of a driver.
330  *
331  * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is
332  * normal world unable to process asynchronous notifications. Typically
333  * used when the driver is shut down.
334  */
335 #define OPTEE_MSG_CMD_OPEN_SESSION	U(0)
336 #define OPTEE_MSG_CMD_INVOKE_COMMAND	U(1)
337 #define OPTEE_MSG_CMD_CLOSE_SESSION	U(2)
338 #define OPTEE_MSG_CMD_CANCEL		U(3)
339 #define OPTEE_MSG_CMD_REGISTER_SHM	U(4)
340 #define OPTEE_MSG_CMD_UNREGISTER_SHM	U(5)
341 #define OPTEE_MSG_CMD_DO_BOTTOM_HALF	U(6)
342 #define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF	U(7)
343 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG	U(0x0004)
344 
345 #endif /* _OPTEE_MSG_H */
346