2 * Copyright (c) 2015-2016, Linaro Limited
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
30 #include <linux/bitops.h>
31 #include <linux/types.h>
34 * This file defines the OP-TEE message protocol used to communicate
35 * with an instance of OP-TEE running in secure world.
37 * This file is divided into three sections.
38 * 1. Formatting of messages.
39 * 2. Requests from normal world
40 * 3. Requests from secure world, Remote Procedure Call (RPC), handled by
44 /*****************************************************************************
45 * Part 1 - formatting of messages
46 *****************************************************************************/
48 #define OPTEE_MSG_ATTR_TYPE_NONE 0x0
49 #define OPTEE_MSG_ATTR_TYPE_VALUE_INPUT 0x1
50 #define OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT 0x2
51 #define OPTEE_MSG_ATTR_TYPE_VALUE_INOUT 0x3
52 #define OPTEE_MSG_ATTR_TYPE_RMEM_INPUT 0x5
53 #define OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT 0x6
54 #define OPTEE_MSG_ATTR_TYPE_RMEM_INOUT 0x7
55 #define OPTEE_MSG_ATTR_TYPE_TMEM_INPUT 0x9
56 #define OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT 0xa
57 #define OPTEE_MSG_ATTR_TYPE_TMEM_INOUT 0xb
59 #define OPTEE_MSG_ATTR_TYPE_MASK GENMASK(7, 0)
62 * Meta parameter to be absorbed by the Secure OS and not passed
63 * to the Trusted Application.
65 * Currently only used with OPTEE_MSG_CMD_OPEN_SESSION.
67 #define OPTEE_MSG_ATTR_META BIT(8)
70 * Pointer to a list of pages used to register user-defined SHM buffer.
71 * Used with OPTEE_MSG_ATTR_TYPE_TMEM_*.
72 * buf_ptr should point to the beginning of the buffer. Buffer will contain
73 * list of page addresses. OP-TEE core can reconstruct contiguous buffer from
74 * that page addresses list. Page addresses are stored as 64 bit values.
75 * Last entry on a page should point to the next page of buffer.
76 * Every entry in buffer should point to a 4k page beginning (12 least
77 * significant bits must be equal to zero).
79 * 12 least significant bints of optee_msg_param.u.tmem.buf_ptr should hold page
80 * offset of the user buffer.
82 * So, entries should be placed like members of this structure:
85 * uint64_t pages_array[OPTEE_MSG_NONCONTIG_PAGE_SIZE/sizeof(uint64_t) - 1];
86 * uint64_t next_page_data;
89 * Structure is designed to exactly fit into the page size
90 * OPTEE_MSG_NONCONTIG_PAGE_SIZE which is a standard 4KB page.
92 * The size of 4KB is chosen because this is the smallest page size for ARM
93 * architectures. If REE uses larger pages, it should divide them to 4KB ones.
95 #define OPTEE_MSG_ATTR_NONCONTIG BIT(9)
98 * Memory attributes for caching passed with temp memrefs. The actual value
99 * used is defined outside the message protocol with the exception of
100 * OPTEE_MSG_ATTR_CACHE_PREDEFINED which means the attributes already
101 * defined for the memory range should be used. If optee_smc.h is used as
102 * bearer of this protocol OPTEE_SMC_SHM_* is used for values.
104 #define OPTEE_MSG_ATTR_CACHE_SHIFT 16
105 #define OPTEE_MSG_ATTR_CACHE_MASK GENMASK(2, 0)
106 #define OPTEE_MSG_ATTR_CACHE_PREDEFINED 0
109 * Same values as TEE_LOGIN_* from TEE Internal API
111 #define OPTEE_MSG_LOGIN_PUBLIC 0x00000000
112 #define OPTEE_MSG_LOGIN_USER 0x00000001
113 #define OPTEE_MSG_LOGIN_GROUP 0x00000002
114 #define OPTEE_MSG_LOGIN_APPLICATION 0x00000004
115 #define OPTEE_MSG_LOGIN_APPLICATION_USER 0x00000005
116 #define OPTEE_MSG_LOGIN_APPLICATION_GROUP 0x00000006
119 * Page size used in non-contiguous buffer entries
121 #define OPTEE_MSG_NONCONTIG_PAGE_SIZE 4096
124 * struct optee_msg_param_tmem - temporary memory reference parameter
125 * @buf_ptr: Address of the buffer
126 * @size: Size of the buffer
127 * @shm_ref: Temporary shared memory reference, pointer to a struct tee_shm
129 * Secure and normal world communicates pointers as physical address
130 * instead of the virtual address. This is because secure and normal world
131 * have completely independent memory mapping. Normal world can even have a
132 * hypervisor which need to translate the guest physical address (AKA IPA
133 * in ARM documentation) to a real physical address before passing the
134 * structure to secure world.
136 struct optee_msg_param_tmem
{
143 * struct optee_msg_param_rmem - registered memory reference parameter
144 * @offs: Offset into shared memory reference
145 * @size: Size of the buffer
146 * @shm_ref: Shared memory reference, pointer to a struct tee_shm
148 struct optee_msg_param_rmem
{
155 * struct optee_msg_param_value - opaque value parameter
157 * Value parameters are passed unchecked between normal and secure world.
159 struct optee_msg_param_value
{
166 * struct optee_msg_param - parameter used together with struct optee_msg_arg
168 * @tmem: parameter by temporary memory reference
169 * @rmem: parameter by registered memory reference
170 * @value: parameter by opaque value
172 * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
173 * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value,
174 * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
175 * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates @rmem,
176 * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
178 struct optee_msg_param
{
181 struct optee_msg_param_tmem tmem
;
182 struct optee_msg_param_rmem rmem
;
183 struct optee_msg_param_value value
;
188 * struct optee_msg_arg - call argument
189 * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
190 * @func: Trusted Application function, specific to the Trusted Application,
191 * used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
192 * @session: In parameter for all OPTEE_MSG_CMD_* except
193 * OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
194 * @cancel_id: Cancellation id, a unique value to identify this request
196 * @ret_origin: origin of the return value
197 * @num_params: number of parameters supplied to the OS Command
198 * @params: the parameters supplied to the OS Command
200 * All normal calls to Trusted OS uses this struct. If cmd requires further
201 * information than what these field holds it can be passed as a parameter
202 * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
203 * attrs field). All parameters tagged as meta has to come first.
205 * Temp memref parameters can be fragmented if supported by the Trusted OS
206 * (when optee_smc.h is bearer of this protocol this is indicated with
207 * OPTEE_SMC_SEC_CAP_UNREGISTERED_SHM). If a logical memref parameter is
208 * fragmented then has all but the last fragment the
209 * OPTEE_MSG_ATTR_FRAGMENT bit set in attrs. Even if a memref is fragmented
210 * it will still be presented as a single logical memref to the Trusted
213 struct optee_msg_arg
{
223 /* num_params tells the actual number of element in params */
224 struct optee_msg_param params
[0];
228 * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
230 * @num_params: Number of parameters embedded in the struct optee_msg_arg
232 * Returns the size of the struct optee_msg_arg together with the number
233 * of embedded parameters.
235 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
236 (sizeof(struct optee_msg_arg) + \
237 sizeof(struct optee_msg_param) * (num_params))
239 /*****************************************************************************
240 * Part 2 - requests from normal world
241 *****************************************************************************/
244 * Return the following UID if using API specified in this file without
245 * further extensions:
246 * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
247 * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
248 * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
250 #define OPTEE_MSG_UID_0 0x384fb3e0
251 #define OPTEE_MSG_UID_1 0xe7f811e3
252 #define OPTEE_MSG_UID_2 0xaf630002
253 #define OPTEE_MSG_UID_3 0xa5d5c51b
254 #define OPTEE_MSG_FUNCID_CALLS_UID 0xFF01
257 * Returns 2.0 if using API specified in this file without further
258 * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
259 * and OPTEE_MSG_REVISION_MINOR
261 #define OPTEE_MSG_REVISION_MAJOR 2
262 #define OPTEE_MSG_REVISION_MINOR 0
263 #define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03
266 * Get UUID of Trusted OS.
268 * Used by non-secure world to figure out which Trusted OS is installed.
269 * Note that returned UUID is the UUID of the Trusted OS, not of the API.
271 * Returns UUID in 4 32-bit words in the same way as
272 * OPTEE_MSG_FUNCID_CALLS_UID described above.
274 #define OPTEE_MSG_OS_OPTEE_UUID_0 0x486178e0
275 #define OPTEE_MSG_OS_OPTEE_UUID_1 0xe7f811e3
276 #define OPTEE_MSG_OS_OPTEE_UUID_2 0xbc5e0002
277 #define OPTEE_MSG_OS_OPTEE_UUID_3 0xa5d5c51b
278 #define OPTEE_MSG_FUNCID_GET_OS_UUID 0x0000
281 * Get revision of Trusted OS.
283 * Used by non-secure world to figure out which version of the Trusted OS
284 * is installed. Note that the returned revision is the revision of the
285 * Trusted OS, not of the API.
287 * Returns revision in 2 32-bit words in the same way as
288 * OPTEE_MSG_CALLS_REVISION described above.
290 #define OPTEE_MSG_FUNCID_GET_OS_REVISION 0x0001
293 * Do a secure call with struct optee_msg_arg as argument
294 * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
296 * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
297 * The first two parameters are tagged as meta, holding two value
298 * parameters to pass the following information:
299 * param[0].u.value.a-b uuid of Trusted Application
300 * param[1].u.value.a-b uuid of Client
301 * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
303 * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
304 * session to a Trusted Application. struct optee_msg_arg::func is Trusted
305 * Application function, specific to the Trusted Application.
307 * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
308 * Trusted Application.
310 * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
312 * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
313 * information is passed as:
314 * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
315 * [| OPTEE_MSG_ATTR_FRAGMENT]
316 * [in] param[0].u.tmem.buf_ptr physical address (of first fragment)
317 * [in] param[0].u.tmem.size size (of first fragment)
318 * [in] param[0].u.tmem.shm_ref holds shared memory reference
320 * The shared memory can optionally be fragmented, temp memrefs can follow
321 * each other with all but the last with the OPTEE_MSG_ATTR_FRAGMENT bit set.
323 * OPTEE_MSG_CMD_UNREGISTER_SHM unregisteres a previously registered shared
324 * memory reference. The information is passed as:
325 * [in] param[0].attr OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
326 * [in] param[0].u.rmem.shm_ref holds shared memory reference
327 * [in] param[0].u.rmem.offs 0
328 * [in] param[0].u.rmem.size 0
330 #define OPTEE_MSG_CMD_OPEN_SESSION 0
331 #define OPTEE_MSG_CMD_INVOKE_COMMAND 1
332 #define OPTEE_MSG_CMD_CLOSE_SESSION 2
333 #define OPTEE_MSG_CMD_CANCEL 3
334 #define OPTEE_MSG_CMD_REGISTER_SHM 4
335 #define OPTEE_MSG_CMD_UNREGISTER_SHM 5
336 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG 0x0004
338 /*****************************************************************************
339 * Part 3 - Requests from secure world, RPC
340 *****************************************************************************/
343 * All RPC is done with a struct optee_msg_arg as bearer of information,
344 * struct optee_msg_arg::arg holds values defined by OPTEE_MSG_RPC_CMD_* below
346 * RPC communication with tee-supplicant is reversed compared to normal
347 * client communication desribed above. The supplicant receives requests
348 * and sends responses.
352 * Load a TA into memory, defined in tee-supplicant
354 #define OPTEE_MSG_RPC_CMD_LOAD_TA 0
359 #define OPTEE_MSG_RPC_CMD_RPMB 1
362 * File system access, defined in tee-supplicant
364 #define OPTEE_MSG_RPC_CMD_FS 2
369 * Returns number of seconds and nano seconds since the Epoch,
370 * 1970-01-01 00:00:00 +0000 (UTC).
372 * [out] param[0].u.value.a Number of seconds
373 * [out] param[0].u.value.b Number of nano seconds.
375 #define OPTEE_MSG_RPC_CMD_GET_TIME 3
378 * Wait queue primitive, helper for secure world to implement a wait queue.
380 * If secure world need to wait for a secure world mutex it issues a sleep
381 * request instead of spinning in secure world. Conversely is a wakeup
382 * request issued when a secure world mutex with a thread waiting thread is
386 * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP
387 * [in] param[0].u.value.b wait key
390 * [in] param[0].u.value.a OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP
391 * [in] param[0].u.value.b wakeup key
393 #define OPTEE_MSG_RPC_CMD_WAIT_QUEUE 4
394 #define OPTEE_MSG_RPC_WAIT_QUEUE_SLEEP 0
395 #define OPTEE_MSG_RPC_WAIT_QUEUE_WAKEUP 1
400 * [in] param[0].value .a number of milliseconds to suspend
402 #define OPTEE_MSG_RPC_CMD_SUSPEND 5
405 * Allocate a piece of shared memory
407 * Shared memory can optionally be fragmented, to support that additional
408 * spare param entries are allocated to make room for eventual fragments.
409 * The spare param entries has .attr = OPTEE_MSG_ATTR_TYPE_NONE when
410 * unused. All returned temp memrefs except the last should have the
411 * OPTEE_MSG_ATTR_FRAGMENT bit set in the attr field.
413 * [in] param[0].u.value.a type of memory one of
414 * OPTEE_MSG_RPC_SHM_TYPE_* below
415 * [in] param[0].u.value.b requested size
416 * [in] param[0].u.value.c required alignment
418 * [out] param[0].u.tmem.buf_ptr physical address (of first fragment)
419 * [out] param[0].u.tmem.size size (of first fragment)
420 * [out] param[0].u.tmem.shm_ref shared memory reference
422 * [out] param[n].u.tmem.buf_ptr physical address
423 * [out] param[n].u.tmem.size size
424 * [out] param[n].u.tmem.shm_ref shared memory reference (same value
425 * as in param[n-1].u.tmem.shm_ref)
427 #define OPTEE_MSG_RPC_CMD_SHM_ALLOC 6
428 /* Memory that can be shared with a non-secure user space application */
429 #define OPTEE_MSG_RPC_SHM_TYPE_APPL 0
430 /* Memory only shared with non-secure kernel */
431 #define OPTEE_MSG_RPC_SHM_TYPE_KERNEL 1
434 * Free shared memory previously allocated with OPTEE_MSG_RPC_CMD_SHM_ALLOC
436 * [in] param[0].u.value.a type of memory one of
437 * OPTEE_MSG_RPC_SHM_TYPE_* above
438 * [in] param[0].u.value.b value of shared memory reference
439 * returned in param[0].u.tmem.shm_ref
442 #define OPTEE_MSG_RPC_CMD_SHM_FREE 7
444 #endif /* _OPTEE_MSG_H */