Merge remote-tracking branch 'torvalds/master' into perf/core
[linux-2.6-microblaze.git] / drivers / tee / optee / optee_msg.h
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_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  * @octets:     parameter by octet string
148  *
149  * @attr & OPTEE_MSG_ATTR_TYPE_MASK indicates if tmem, rmem or value is used in
150  * the union. OPTEE_MSG_ATTR_TYPE_VALUE_* indicates value or octets,
151  * OPTEE_MSG_ATTR_TYPE_TMEM_* indicates @tmem and
152  * OPTEE_MSG_ATTR_TYPE_RMEM_* indicates @rmem,
153  * OPTEE_MSG_ATTR_TYPE_NONE indicates that none of the members are used.
154  */
155 struct optee_msg_param {
156         u64 attr;
157         union {
158                 struct optee_msg_param_tmem tmem;
159                 struct optee_msg_param_rmem rmem;
160                 struct optee_msg_param_value value;
161                 u8 octets[24];
162         } u;
163 };
164
165 /**
166  * struct optee_msg_arg - call argument
167  * @cmd: Command, one of OPTEE_MSG_CMD_* or OPTEE_MSG_RPC_CMD_*
168  * @func: Trusted Application function, specific to the Trusted Application,
169  *           used if cmd == OPTEE_MSG_CMD_INVOKE_COMMAND
170  * @session: In parameter for all OPTEE_MSG_CMD_* except
171  *           OPTEE_MSG_CMD_OPEN_SESSION where it's an output parameter instead
172  * @cancel_id: Cancellation id, a unique value to identify this request
173  * @ret: return value
174  * @ret_origin: origin of the return value
175  * @num_params: number of parameters supplied to the OS Command
176  * @params: the parameters supplied to the OS Command
177  *
178  * All normal calls to Trusted OS uses this struct. If cmd requires further
179  * information than what these fields hold it can be passed as a parameter
180  * tagged as meta (setting the OPTEE_MSG_ATTR_META bit in corresponding
181  * attrs field). All parameters tagged as meta have to come first.
182  */
183 struct optee_msg_arg {
184         u32 cmd;
185         u32 func;
186         u32 session;
187         u32 cancel_id;
188         u32 pad;
189         u32 ret;
190         u32 ret_origin;
191         u32 num_params;
192
193         /* num_params tells the actual number of element in params */
194         struct optee_msg_param params[];
195 };
196
197 /**
198  * OPTEE_MSG_GET_ARG_SIZE - return size of struct optee_msg_arg
199  *
200  * @num_params: Number of parameters embedded in the struct optee_msg_arg
201  *
202  * Returns the size of the struct optee_msg_arg together with the number
203  * of embedded parameters.
204  */
205 #define OPTEE_MSG_GET_ARG_SIZE(num_params) \
206         (sizeof(struct optee_msg_arg) + \
207          sizeof(struct optee_msg_param) * (num_params))
208
209 /*****************************************************************************
210  * Part 2 - requests from normal world
211  *****************************************************************************/
212
213 /*
214  * Return the following UID if using API specified in this file without
215  * further extensions:
216  * 384fb3e0-e7f8-11e3-af63-0002a5d5c51b.
217  * Represented in 4 32-bit words in OPTEE_MSG_UID_0, OPTEE_MSG_UID_1,
218  * OPTEE_MSG_UID_2, OPTEE_MSG_UID_3.
219  */
220 #define OPTEE_MSG_UID_0                 0x384fb3e0
221 #define OPTEE_MSG_UID_1                 0xe7f811e3
222 #define OPTEE_MSG_UID_2                 0xaf630002
223 #define OPTEE_MSG_UID_3                 0xa5d5c51b
224 #define OPTEE_MSG_FUNCID_CALLS_UID      0xFF01
225
226 /*
227  * Returns 2.0 if using API specified in this file without further
228  * extensions. Represented in 2 32-bit words in OPTEE_MSG_REVISION_MAJOR
229  * and OPTEE_MSG_REVISION_MINOR
230  */
231 #define OPTEE_MSG_REVISION_MAJOR        2
232 #define OPTEE_MSG_REVISION_MINOR        0
233 #define OPTEE_MSG_FUNCID_CALLS_REVISION 0xFF03
234
235 /*
236  * Get UUID of Trusted OS.
237  *
238  * Used by non-secure world to figure out which Trusted OS is installed.
239  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
240  *
241  * Returns UUID in 4 32-bit words in the same way as
242  * OPTEE_MSG_FUNCID_CALLS_UID described above.
243  */
244 #define OPTEE_MSG_OS_OPTEE_UUID_0       0x486178e0
245 #define OPTEE_MSG_OS_OPTEE_UUID_1       0xe7f811e3
246 #define OPTEE_MSG_OS_OPTEE_UUID_2       0xbc5e0002
247 #define OPTEE_MSG_OS_OPTEE_UUID_3       0xa5d5c51b
248 #define OPTEE_MSG_FUNCID_GET_OS_UUID    0x0000
249
250 /*
251  * Get revision of Trusted OS.
252  *
253  * Used by non-secure world to figure out which version of the Trusted OS
254  * is installed. Note that the returned revision is the revision of the
255  * Trusted OS, not of the API.
256  *
257  * Returns revision in 2 32-bit words in the same way as
258  * OPTEE_MSG_CALLS_REVISION described above.
259  */
260 #define OPTEE_MSG_FUNCID_GET_OS_REVISION        0x0001
261
262 /*
263  * Do a secure call with struct optee_msg_arg as argument
264  * The OPTEE_MSG_CMD_* below defines what goes in struct optee_msg_arg::cmd
265  *
266  * OPTEE_MSG_CMD_OPEN_SESSION opens a session to a Trusted Application.
267  * The first two parameters are tagged as meta, holding two value
268  * parameters to pass the following information:
269  * param[0].u.value.a-b uuid of Trusted Application
270  * param[1].u.value.a-b uuid of Client
271  * param[1].u.value.c Login class of client OPTEE_MSG_LOGIN_*
272  *
273  * OPTEE_MSG_CMD_INVOKE_COMMAND invokes a command a previously opened
274  * session to a Trusted Application.  struct optee_msg_arg::func is Trusted
275  * Application function, specific to the Trusted Application.
276  *
277  * OPTEE_MSG_CMD_CLOSE_SESSION closes a previously opened session to
278  * Trusted Application.
279  *
280  * OPTEE_MSG_CMD_CANCEL cancels a currently invoked command.
281  *
282  * OPTEE_MSG_CMD_REGISTER_SHM registers a shared memory reference. The
283  * information is passed as:
284  * [in] param[0].attr                   OPTEE_MSG_ATTR_TYPE_TMEM_INPUT
285  *                                      [| OPTEE_MSG_ATTR_NONCONTIG]
286  * [in] param[0].u.tmem.buf_ptr         physical address (of first fragment)
287  * [in] param[0].u.tmem.size            size (of first fragment)
288  * [in] param[0].u.tmem.shm_ref         holds shared memory reference
289  *
290  * OPTEE_MSG_CMD_UNREGISTER_SHM unregisters a previously registered shared
291  * memory reference. The information is passed as:
292  * [in] param[0].attr                   OPTEE_MSG_ATTR_TYPE_RMEM_INPUT
293  * [in] param[0].u.rmem.shm_ref         holds shared memory reference
294  * [in] param[0].u.rmem.offs            0
295  * [in] param[0].u.rmem.size            0
296  */
297 #define OPTEE_MSG_CMD_OPEN_SESSION      0
298 #define OPTEE_MSG_CMD_INVOKE_COMMAND    1
299 #define OPTEE_MSG_CMD_CLOSE_SESSION     2
300 #define OPTEE_MSG_CMD_CANCEL            3
301 #define OPTEE_MSG_CMD_REGISTER_SHM      4
302 #define OPTEE_MSG_CMD_UNREGISTER_SHM    5
303 #define OPTEE_MSG_FUNCID_CALL_WITH_ARG  0x0004
304
305 #endif /* _OPTEE_MSG_H */