xref: /linux/drivers/tee/optee/optee_msg.h (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 /* SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) */
2 /*
3  * Copyright (c) 2015-2021, Linaro Limited
4  */
5 #ifndef _OPTEE_MSG_H
6 #define _OPTEE_MSG_H
7 
8 #include <linux/bitops.h>
9 #include <linux/types.h>
10 
11 /*
12  * This file defines the OP-TEE message protocol (ABI) used to communicate
13  * with an instance of OP-TEE running in secure world.
14  *
15  * This file is divided into two sections.
16  * 1. Formatting of messages.
17  * 2. Requests from normal world
18  */
19 
20 /*****************************************************************************
21  * Part 1 - formatting of messages
22  *****************************************************************************/
23 
24 #define OPTEE_MSG_ATTR_TYPE_NONE		0x0
25 #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT		0x1
26 #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT	0x2
27 #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT		0x3
28 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT		0x5
29 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT		0x6
30 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT		0x7
31 #define OPTEE_MSG_ATTR_TYPE_FMEM_INPUT		OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
32 #define OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT		OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT
33 #define OPTEE_MSG_ATTR_TYPE_FMEM_INOUT		OPTEE_MSG_ATTR_TYPE_RMEM_INOUT
34 #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT		0x9
35 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT		0xa
36 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT		0xb
37 
38 #define OPTEE_MSG_ATTR_TYPE_MASK		GENMASK(7, 0)
39 
40 /*
41  * Meta parameter to be absorbed by the Secure OS and not passed
42  * to the Trusted Application.
43  *
44  * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
45  */
46 #define OPTEE_MSG_ATTR_META			BIT(8)
47 
48 /*
49  * Pointer to a list of pages used to register user-defined SHM buffer.
50  * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
51  * buf_ptr should point to the beginning of the buffer. Buffer will contain
52  * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
53  * that page addresses list. Page addresses are stored as 64 bit values.
54  * Last entry on a page should point to the next page of buffer.
55  * Every entry in buffer should point to a 4k page beginning (12 least
56  * significant bits must be equal to zero).
57  *
58  * 12 least significant bits of optee_msg_param.u.tmem.buf_ptr should hold
59  * page offset of user buffer.
60  *
61  * So, entries should be placed like members of this structure:
62  *
63  * struct page_data {
64  *   uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
65  *   uint64_t next_page_data;
66  * };
67  *
68  * Structure is designed to exactly fit into the page size
69  * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
70  *
71  * The size of 4KB is chosen because this is the smallest page size for ARM
72  * architectures. If REE uses larger pages, it should divide them to 4KB ones.
73  */
74 #define OPTEE_MSG_ATTR_NONCONTIG		BIT(9)
75 
76 /*
77  * Memory attributes for caching passed with temp memrefs. The actual value
78  * used is defined outside the message protocol with the exception of
79  * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
80  * defined for the memory range should be used. If optee_smc.h is used as
81  * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
82  */
83 #define OPTEE_MSG_ATTR_CACHE_SHIFT		16
84 #define OPTEE_MSG_ATTR_CACHE_MASK		GENMASK(2, 0)
85 #define OPTEE_MSG_ATTR_CACHE_PREDEFINED		0
86 
87 /*
88  * Same values as TEE_LOGIN_* from TEE Internal API
89  */
90 #define OPTEE_MSG_LOGIN_PUBLIC			0x00000000
91 #define OPTEE_MSG_LOGIN_USER			0x00000001
92 #define OPTEE_MSG_LOGIN_GROUP			0x00000002
93 #define OPTEE_MSG_LOGIN_APPLICATION		0x00000004
94 #define OPTEE_MSG_LOGIN_APPLICATION_USER	0x00000005
95 #define OPTEE_MSG_LOGIN_APPLICATION_GROUP	0x00000006
96 
97 /*
98  * Page size used in non-contiguous buffer entries
99  */
100 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE		4096
101 
102 #define OPTEE_MSG_FMEM_INVALID_GLOBAL_ID	0xffffffffffffffff
103 
104 /**
105  * struct optee_msg_param_tmem - temporary memory reference parameter
106  * @buf_ptr:	address of the buffer
107  * @size:	size of the buffer
108  * @shm_ref:	temporary shared memory reference, pointer to a struct tee_shm
109  *
110  * Secure and normal world communicates pointers as physical address
111  * instead of the virtual address. This is because secure and normal world
112  * have completely independent memory mapping. Normal world can even have a
113  * hypervisor which need to translate the guest physical address (AKA IPA
114  * in ARM documentation) to a real physical address before passing the
115  * structure to secure world.
116  */
117 struct optee_msg_param_tmem {
118 	u64 buf_ptr;
119 	u64 size;
120 	u64 shm_ref;
121 };
122 
123 /**
124  * struct optee_msg_param_rmem - registered memory reference parameter
125  * @offs:	offset into shared memory reference
126  * @size:	size of the buffer
127  * @shm_ref:	shared memory reference, pointer to a struct tee_shm
128  */
129 struct optee_msg_param_rmem {
130 	u64 offs;
131 	u64 size;
132 	u64 shm_ref;
133 };
134 
135 /**
136  * struct optee_msg_param_fmem - FF-A memory reference parameter
137  * @offs_low:		lower bits of offset into shared memory reference
138  * @offs_high:		higher bits of offset into shared memory reference
139  * @internal_offs:	internal offset into the first page of shared memory
140  *			reference
141  * @size:		size of the buffer
142  * @global_id:		global identifier of the shared memory
143  */
144 struct optee_msg_param_fmem {
145 	u32 offs_low;
146 	u16 offs_high;
147 	u16 internal_offs;
148 	u64 size;
149 	u64 global_id;
150 };
151 
152 /**
153  * struct optee_msg_param_value - opaque value parameter
154  * @a: first opaque value
155  * @b: second opaque value
156  * @c: third opaque value
157  *
158  * Value parameters are passed unchecked between normal and secure world.
159  */
160 struct optee_msg_param_value {
161 	u64 a;
162 	u64 b;
163 	u64 c;
164 };
165 
166 /**
167  * struct optee_msg_param - parameter used together with struct optee_msg_arg
168  * @attr:	attributes
169  * @tmem:	parameter by temporary memory reference
170  * @rmem:	parameter by registered memory reference
171  * @fmem:	parameter by FF-A registered memory reference
172  * @value:	parameter by opaque value
173  * @octets:	parameter by octet string
174  * @u:		union holding OP-TEE msg parameter
175  *
176  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
177  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,
178  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
179  * OPTEE_MSG_ATTR_TYPE_RMEM_* or the alias PTEE_MSG_ATTR_TYPE_FMEM_* indicates
180  * @rmem or @fmem depending on the conduit.
181  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
182  */
183 struct optee_msg_param {
184 	u64 attr;
185 	union {
186 		struct optee_msg_param_tmem tmem;
187 		struct optee_msg_param_rmem rmem;
188 		struct optee_msg_param_fmem fmem;
189 		struct optee_msg_param_value value;
190 		u8 octets[24];
191 	} u;
192 };
193 
194 /**
195  * struct optee_msg_arg - call argument
196  * @cmd:	command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
197  * @func:	Trusted Application function, specific to the Trusted
198  *		Application, used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
199  * @session:	in parameter for all OPTEE_MSG_CMD_* except
200  *		OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter
201  *		instead
202  * @cancel_id:	cancellation id, a unique value to identify this request
203  * @pad:	padding for alignment
204  * @ret:	return value
205  * @ret_origin:	origin of the return value
206  * @num_params:	number of parameters supplied to the OS Command
207  * @params:	the parameters supplied to the OS Command
208  *
209  * All normal calls to Trusted OS uses this struct. If cmd requires further
210  * information than what these fields hold it can be passed as a parameter
211  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
212  * attrs field). All parameters tagged as meta have to come first.
213  */
214 struct optee_msg_arg {
215 	u32 cmd;
216 	u32 func;
217 	u32 session;
218 	u32 cancel_id;
219 	u32 pad;
220 	u32 ret;
221 	u32 ret_origin;
222 	u32 num_params;
223 
224 	/* num_params tells the actual number of element in params */
225 	struct optee_msg_param params[];
226 };
227 
228 /**
229  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
230  *
231  * @num_params: Number of parameters embedded in the struct optee_msg_arg
232  *
233  * Returns the size of the struct optee_msg_arg together with the number
234  * of embedded parameters.
235  */
236 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
237 	(sizeof(struct optee_msg_arg) + \
238 	 sizeof(struct optee_msg_param) * (num_params))
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  * In the case where the OP-TEE image is loaded by the kernel, this will
252  * initially return an alternate UID to reflect that we are communicating with
253  * the TF-A image loading service at that time instead of OP-TEE. That UID is:
254  * a3fbeab1-1246-315d-c7c4-06b9c03cbea4.
255  * Represented in 4 32-bit words in OPTEE_MSG_IMAGE_LOAD_UID_0,
256  * OPTEE_MSG_IMAGE_LOAD_UID_1, OPTEE_MSG_IMAGE_LOAD_UID_2,
257  * OPTEE_MSG_IMAGE_LOAD_UID_3.
258  */
259 #define OPTEE_MSG_UID_0			0x384fb3e0
260 #define OPTEE_MSG_UID_1			0xe7f811e3
261 #define OPTEE_MSG_UID_2			0xaf630002
262 #define OPTEE_MSG_UID_3			0xa5d5c51b
263 #define OPTEE_MSG_IMAGE_LOAD_UID_0	0xa3fbeab1
264 #define OPTEE_MSG_IMAGE_LOAD_UID_1	0x1246315d
265 #define OPTEE_MSG_IMAGE_LOAD_UID_2	0xc7c406b9
266 #define OPTEE_MSG_IMAGE_LOAD_UID_3	0xc03cbea4
267 #define OPTEE_MSG_FUNCID_CALLS_UID	0xFF01
268 
269 /*
270  * Returns 2.0 if using API specified in this file without further
271  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
272  * and OPTEE_MSG_REVISION_MINOR
273  */
274 #define OPTEE_MSG_REVISION_MAJOR	2
275 #define OPTEE_MSG_REVISION_MINOR	0
276 #define OPTEE_MSG_FUNCID_CALLS_REVISION	0xFF03
277 
278 /*
279  * Get UUID of Trusted OS.
280  *
281  * Used by non-secure world to figure out which Trusted OS is installed.
282  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
283  *
284  * Returns UUID in 4 32-bit words in the same way as
285  * OPTEE_MSG_FUNCID_CALLS_UID described above.
286  */
287 #define OPTEE_MSG_OS_OPTEE_UUID_0	0x486178e0
288 #define OPTEE_MSG_OS_OPTEE_UUID_1	0xe7f811e3
289 #define OPTEE_MSG_OS_OPTEE_UUID_2	0xbc5e0002
290 #define OPTEE_MSG_OS_OPTEE_UUID_3	0xa5d5c51b
291 #define OPTEE_MSG_FUNCID_GET_OS_UUID	0x0000
292 
293 /*
294  * Get revision of Trusted OS.
295  *
296  * Used by non-secure world to figure out which version of the Trusted OS
297  * is installed. Note that the returned revision is the revision of the
298  * Trusted OS, not of the API.
299  *
300  * Returns revision in 2 32-bit words in the same way as
301  * OPTEE_MSG_CALLS_REVISION described above.
302  */
303 #define OPTEE_MSG_FUNCID_GET_OS_REVISION	0x0001
304 
305 /*
306  * Values used in OPTEE_MSG_CMD_LEND_PROTMEM below
307  * OPTEE_MSG_PROTMEM_RESERVED		Reserved
308  * OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY	Secure Video Playback
309  * OPTEE_MSG_PROTMEM_TRUSTED_UI		Trused UI
310  * OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD	Secure Video Recording
311  */
312 #define OPTEE_MSG_PROTMEM_RESERVED		0
313 #define OPTEE_MSG_PROTMEM_SECURE_VIDEO_PLAY	1
314 #define OPTEE_MSG_PROTMEM_TRUSTED_UI		2
315 #define OPTEE_MSG_PROTMEM_SECURE_VIDEO_RECORD	3
316 
317 /*
318  * Do a secure call with struct optee_msg_arg as argument
319  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
320  *
321  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
322  * The first two parameters are tagged as meta, holding two value
323  * parameters to pass the following information:
324  * param[0].u.value.a-b uuid of Trusted Application
325  * param[1].u.value.a-b uuid of Client
326  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
327  *
328  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
329  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
330  * Application function, specific to the Trusted Application.
331  *
332  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
333  * Trusted Application.
334  *
335  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
336  *
337  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
338  * information is passed as:
339  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
340  *					[| OPTEE_MSG_ATTR_NONCONTIG]
341  * [in] param[0].u.tmem.buf_ptr		physical address (of first fragment)
342  * [in] param[0].u.tmem.size		size (of first fragment)
343  * [in] param[0].u.tmem.shm_ref		holds shared memory reference
344  *
345  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared
346  * memory reference. The information is passed as:
347  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
348  * [in] param[0].u.rmem.shm_ref		holds shared memory reference
349  * [in] param[0].u.rmem.offs		0
350  * [in] param[0].u.rmem.size		0
351  *
352  * OPTEE_MSG_CMD_DO_BOTTOM_HALF does the scheduled bottom half processing
353  * of a driver.
354  *
355  * OPTEE_MSG_CMD_STOP_ASYNC_NOTIF informs secure world that from now is
356  * normal world unable to process asynchronous notifications. Typically
357  * used when the driver is shut down.
358  *
359  * OPTEE_MSG_CMD_LEND_PROTMEM lends protected memory. The passed normal
360  * physical memory is protected from normal world access. The memory
361  * should be unmapped prior to this call since it becomes inaccessible
362  * during the request.
363  * Parameters are passed as:
364  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
365  * [in] param[0].u.value.a		OPTEE_MSG_PROTMEM_* defined above
366  * [in] param[1].attr			OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
367  * [in] param[1].u.tmem.buf_ptr		physical address
368  * [in] param[1].u.tmem.size		size
369  * [in] param[1].u.tmem.shm_ref		holds protected memory reference
370  *
371  * OPTEE_MSG_CMD_RECLAIM_PROTMEM reclaims a previously lent protected
372  * memory reference. The physical memory is accessible by the normal world
373  * after this function has return and can be mapped again. The information
374  * is passed as:
375  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
376  * [in] param[0].u.value.a		holds protected memory cookie
377  *
378  * OPTEE_MSG_CMD_GET_PROTMEM_CONFIG get configuration for a specific
379  * protected memory use case. Parameters are passed as:
380  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_VALUE_INOUT
381  * [in] param[0].value.a		OPTEE_MSG_PROTMEM_*
382  * [in] param[1].attr			OPTEE_MSG_ATTR_TYPE_{R,F}MEM_OUTPUT
383  * [in] param[1].u.{r,f}mem		Buffer or NULL
384  * [in] param[1].u.{r,f}mem.size	Provided size of buffer or 0 for query
385  * output for the protected use case:
386  * [out] param[0].value.a		Minimal size of protected memory
387  * [out] param[0].value.b		Required alignment of size and start of
388  *					protected memory
389  * [out] param[0].value.c               PA width, max 64
390  * [out] param[1].{r,f}mem.size		Size of output data
391  * [out] param[1].{r,f}mem		If non-NULL, contains an array of
392  *					uint32_t memory attributes that must be
393  *					included when lending memory for this
394  *					use case
395  *
396  * OPTEE_MSG_CMD_ASSIGN_PROTMEM assigns use-case to protected memory
397  * previously lent using the FFA_LEND framework ABI. Parameters are passed
398  * as:
399  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_VALUE_INPUT
400  * [in] param[0].u.value.a		holds protected memory cookie
401  * [in] param[0].u.value.b		OPTEE_MSG_PROTMEM_* defined above
402  */
403 #define OPTEE_MSG_CMD_OPEN_SESSION		0
404 #define OPTEE_MSG_CMD_INVOKE_COMMAND		1
405 #define OPTEE_MSG_CMD_CLOSE_SESSION		2
406 #define OPTEE_MSG_CMD_CANCEL			3
407 #define OPTEE_MSG_CMD_REGISTER_SHM		4
408 #define OPTEE_MSG_CMD_UNREGISTER_SHM		5
409 #define OPTEE_MSG_CMD_DO_BOTTOM_HALF		6
410 #define OPTEE_MSG_CMD_STOP_ASYNC_NOTIF		7
411 #define OPTEE_MSG_CMD_LEND_PROTMEM		8
412 #define OPTEE_MSG_CMD_RECLAIM_PROTMEM		9
413 #define OPTEE_MSG_CMD_GET_PROTMEM_CONFIG	10
414 #define OPTEE_MSG_CMD_ASSIGN_PROTMEM		11
415 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG		0x0004
416 
417 #endif /* _OPTEE_MSG_H */
418