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 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_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(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(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 /**
100  * struct optee_msg_param_tmem - temporary memory reference parameter
101  * @buf_ptr:	Address of the buffer
102  * @size:	Size of the buffer
103  * @shm_ref:	Temporary shared memory reference, pointer to a struct tee_shm
104  *
105  * Secure and normal world communicates pointers as physical address
106  * instead of the virtual address. This is because secure and normal world
107  * have completely independent memory mapping. Normal world can even have a
108  * hypervisor which need to translate the guest physical address (AKA IPA
109  * in ARM documentation) to a real physical address before passing the
110  * structure to secure world.
111  */
112 struct optee_msg_param_tmem {
113 	u64 buf_ptr;
114 	u64 size;
115 	u64 shm_ref;
116 };
117 
118 /**
119  * struct optee_msg_param_rmem - registered memory reference parameter
120  * @offs:	Offset into shared memory reference
121  * @size:	Size of the buffer
122  * @shm_ref:	Shared memory reference, pointer to a struct tee_shm
123  */
124 struct optee_msg_param_rmem {
125 	u64 offs;
126 	u64 size;
127 	u64 shm_ref;
128 };
129 
130 /**
131  * struct optee_msg_param_value - opaque value parameter
132  *
133  * Value parameters are passed unchecked between normal and secure world.
134  */
135 struct optee_msg_param_value {
136 	u64 a;
137 	u64 b;
138 	u64 c;
139 };
140 
141 /**
142  * struct optee_msg_param - parameter used together with struct optee_msg_arg
143  * @attr:	attributes
144  * @tmem:	parameter by temporary memory reference
145  * @rmem:	parameter by registered memory reference
146  * @value:	parameter by opaque value
147  *
148  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
149  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value,
150  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
151  * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates @rmem,
152  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
153  */
154 struct optee_msg_param {
155 	u64 attr;
156 	union {
157 		struct optee_msg_param_tmem tmem;
158 		struct optee_msg_param_rmem rmem;
159 		struct optee_msg_param_value value;
160 	} u;
161 };
162 
163 /**
164  * struct optee_msg_arg - call argument
165  * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
166  * @func: Trusted Application function, specific to the Trusted Application,
167  *	     used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
168  * @session: In parameter for all OPTEE_MSG_CMD_* except
169  *	     OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
170  * @cancel_id: Cancellation id, a unique value to identify this request
171  * @ret: return value
172  * @ret_origin: origin of the return value
173  * @num_params: number of parameters supplied to the OS Command
174  * @params: the parameters supplied to the OS Command
175  *
176  * All normal calls to Trusted OS uses this struct. If cmd requires further
177  * information than what these fields hold it can be passed as a parameter
178  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
179  * attrs field). All parameters tagged as meta have to come first.
180  */
181 struct optee_msg_arg {
182 	u32 cmd;
183 	u32 func;
184 	u32 session;
185 	u32 cancel_id;
186 	u32 pad;
187 	u32 ret;
188 	u32 ret_origin;
189 	u32 num_params;
190 
191 	/* num_params tells the actual number of element in params */
192 	struct optee_msg_param params[];
193 };
194 
195 /**
196  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
197  *
198  * @num_params: Number of parameters embedded in the struct optee_msg_arg
199  *
200  * Returns the size of the struct optee_msg_arg together with the number
201  * of embedded parameters.
202  */
203 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
204 	(sizeof(struct optee_msg_arg) + \
205 	 sizeof(struct optee_msg_param) * (num_params))
206 
207 /*****************************************************************************
208  * Part 2 - requests from normal world
209  *****************************************************************************/
210 
211 /*
212  * Return the following UID if using API specified in this file without
213  * further extensions:
214  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
215  * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
216  * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
217  */
218 #define OPTEE_MSG_UID_0			0x384fb3e0
219 #define OPTEE_MSG_UID_1			0xe7f811e3
220 #define OPTEE_MSG_UID_2			0xaf630002
221 #define OPTEE_MSG_UID_3			0xa5d5c51b
222 #define OPTEE_MSG_FUNCID_CALLS_UID	0xFF01
223 
224 /*
225  * Returns 2.0 if using API specified in this file without further
226  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
227  * and OPTEE_MSG_REVISION_MINOR
228  */
229 #define OPTEE_MSG_REVISION_MAJOR	2
230 #define OPTEE_MSG_REVISION_MINOR	0
231 #define OPTEE_MSG_FUNCID_CALLS_REVISION	0xFF03
232 
233 /*
234  * Get UUID of Trusted OS.
235  *
236  * Used by non-secure world to figure out which Trusted OS is installed.
237  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
238  *
239  * Returns UUID in 4 32-bit words in the same way as
240  * OPTEE_MSG_FUNCID_CALLS_UID described above.
241  */
242 #define OPTEE_MSG_OS_OPTEE_UUID_0	0x486178e0
243 #define OPTEE_MSG_OS_OPTEE_UUID_1	0xe7f811e3
244 #define OPTEE_MSG_OS_OPTEE_UUID_2	0xbc5e0002
245 #define OPTEE_MSG_OS_OPTEE_UUID_3	0xa5d5c51b
246 #define OPTEE_MSG_FUNCID_GET_OS_UUID	0x0000
247 
248 /*
249  * Get revision of Trusted OS.
250  *
251  * Used by non-secure world to figure out which version of the Trusted OS
252  * is installed. Note that the returned revision is the revision of the
253  * Trusted OS, not of the API.
254  *
255  * Returns revision in 2 32-bit words in the same way as
256  * OPTEE_MSG_CALLS_REVISION described above.
257  */
258 #define OPTEE_MSG_FUNCID_GET_OS_REVISION	0x0001
259 
260 /*
261  * Do a secure call with struct optee_msg_arg as argument
262  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
263  *
264  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
265  * The first two parameters are tagged as meta, holding two value
266  * parameters to pass the following information:
267  * param[0].u.value.a-b uuid of Trusted Application
268  * param[1].u.value.a-b uuid of Client
269  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
270  *
271  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
272  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
273  * Application function, specific to the Trusted Application.
274  *
275  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
276  * Trusted Application.
277  *
278  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
279  *
280  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
281  * information is passed as:
282  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
283  *					[| OPTEE_MSG_ATTR_NONCONTIG]
284  * [in] param[0].u.tmem.buf_ptr		physical address (of first fragment)
285  * [in] param[0].u.tmem.size		size (of first fragment)
286  * [in] param[0].u.tmem.shm_ref		holds shared memory reference
287  *
288  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared
289  * memory reference. The information is passed as:
290  * [in] param[0].attr			OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
291  * [in] param[0].u.rmem.shm_ref		holds shared memory reference
292  * [in] param[0].u.rmem.offs		0
293  * [in] param[0].u.rmem.size		0
294  */
295 #define OPTEE_MSG_CMD_OPEN_SESSION	0
296 #define OPTEE_MSG_CMD_INVOKE_COMMAND	1
297 #define OPTEE_MSG_CMD_CLOSE_SESSION	2
298 #define OPTEE_MSG_CMD_CANCEL		3
299 #define OPTEE_MSG_CMD_REGISTER_SHM	4
300 #define OPTEE_MSG_CMD_UNREGISTER_SHM	5
301 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG	0x0004
302 
303 #endif /* _OPTEE_MSG_H */
304