1 /*
2  * Copyright (c) 2015-2016, Linaro Limited
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
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.
14  *
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.
26  */
27 
28 #ifndef __TEE_H
29 #define __TEE_H
30 
31 #include <linux/ioctl.h>
32 #include <linux/types.h>
33 
34 /*
35  * This file describes the API provided by a TEE driver to user space.
36  *
37  * Each TEE driver defines a TEE specific protocol which is used for the
38  * data passed back and forth using TEE_IOC_CMD.
39  */
40 
41 /* Helpers to make the ioctl defines */
42 #define TEE_IOC_MAGIC	0xa4
43 #define TEE_IOC_BASE	0
44 
45 /* Flags relating to shared memory */
46 #define TEE_IOCTL_SHM_MAPPED	0x1	/* memory mapped in normal world */
47 #define TEE_IOCTL_SHM_DMA_BUF	0x2	/* dma-buf handle on shared memory */
48 
49 #define TEE_MAX_ARG_SIZE	1024
50 
51 #define TEE_GEN_CAP_GP		(1 << 0)/* GlobalPlatform compliant TEE */
52 #define TEE_GEN_CAP_PRIVILEGED	(1 << 1)/* Privileged device (for supplicant) */
53 #define TEE_GEN_CAP_REG_MEM	(1 << 2)/* Supports registering shared memory */
54 #define TEE_GEN_CAP_MEMREF_NULL	(1 << 3)/* NULL MemRef support */
55 
56 #define TEE_MEMREF_NULL		(__u64)(-1) /* NULL MemRef Buffer */
57 
58 /*
59  * TEE Implementation ID
60  */
61 #define TEE_IMPL_ID_OPTEE	1
62 #define TEE_IMPL_ID_AMDTEE	2
63 
64 /*
65  * OP-TEE specific capabilities
66  */
67 #define TEE_OPTEE_CAP_TZ	(1 << 0)
68 
69 /**
70  * struct tee_ioctl_version_data - TEE version
71  * @impl_id:	[out] TEE implementation id
72  * @impl_caps:	[out] Implementation specific capabilities
73  * @gen_caps:	[out] Generic capabilities, defined by TEE_GEN_CAPS_* above
74  *
75  * Identifies the TEE implementation, @impl_id is one of TEE_IMPL_ID_* above.
76  * @impl_caps is implementation specific, for example TEE_OPTEE_CAP_*
77  * is valid when @impl_id == TEE_IMPL_ID_OPTEE.
78  */
79 struct tee_ioctl_version_data {
80 	__u32 impl_id;
81 	__u32 impl_caps;
82 	__u32 gen_caps;
83 };
84 
85 /**
86  * TEE_IOC_VERSION - query version of TEE
87  *
88  * Takes a tee_ioctl_version_data struct and returns with the TEE version
89  * data filled in.
90  */
91 #define TEE_IOC_VERSION		_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 0, \
92 				     struct tee_ioctl_version_data)
93 
94 /**
95  * struct tee_ioctl_shm_alloc_data - Shared memory allocate argument
96  * @size:	[in/out] Size of shared memory to allocate
97  * @flags:	[in/out] Flags to/from allocation.
98  * @id:		[out] Identifier of the shared memory
99  *
100  * The flags field should currently be zero as input. Updated by the call
101  * with actual flags as defined by TEE_IOCTL_SHM_* above.
102  * This structure is used as argument for TEE_IOC_SHM_ALLOC below.
103  */
104 struct tee_ioctl_shm_alloc_data {
105 	__u64 size;
106 	__u32 flags;
107 	__s32 id;
108 };
109 
110 /**
111  * TEE_IOC_SHM_ALLOC - allocate shared memory
112  *
113  * Allocates shared memory between the user space process and secure OS.
114  *
115  * Returns a file descriptor on success or < 0 on failure
116  *
117  * The returned file descriptor is used to map the shared memory into user
118  * space. The shared memory is freed when the descriptor is closed and the
119  * memory is unmapped.
120  */
121 #define TEE_IOC_SHM_ALLOC	_IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 1, \
122 				     struct tee_ioctl_shm_alloc_data)
123 
124 /**
125  * struct tee_ioctl_buf_data - Variable sized buffer
126  * @buf_ptr:	[in] A __user pointer to a buffer
127  * @buf_len:	[in] Length of the buffer above
128  *
129  * Used as argument for TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE,
130  * TEE_IOC_SUPPL_RECV, and TEE_IOC_SUPPL_SEND below.
131  */
132 struct tee_ioctl_buf_data {
133 	__u64 buf_ptr;
134 	__u64 buf_len;
135 };
136 
137 /*
138  * Attributes for struct tee_ioctl_param, selects field in the union
139  */
140 #define TEE_IOCTL_PARAM_ATTR_TYPE_NONE		0	/* parameter not used */
141 
142 /*
143  * These defines value parameters (struct tee_ioctl_param_value)
144  */
145 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT	1
146 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT	2
147 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT	3	/* input and output */
148 
149 /*
150  * These defines shared memory reference parameters (struct
151  * tee_ioctl_param_memref)
152  */
153 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT	5
154 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT	6
155 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT	7	/* input and output */
156 
157 /*
158  * Mask for the type part of the attribute, leaves room for more types
159  */
160 #define TEE_IOCTL_PARAM_ATTR_TYPE_MASK		0xff
161 
162 /* Meta parameter carrying extra information about the message. */
163 #define TEE_IOCTL_PARAM_ATTR_META		0x100
164 
165 /* Mask of all known attr bits */
166 #define TEE_IOCTL_PARAM_ATTR_MASK \
167 	(TEE_IOCTL_PARAM_ATTR_TYPE_MASK | TEE_IOCTL_PARAM_ATTR_META)
168 
169 /*
170  * Matches TEEC_LOGIN_* in GP TEE Client API
171  * Are only defined for GP compliant TEEs
172  */
173 #define TEE_IOCTL_LOGIN_PUBLIC			0
174 #define TEE_IOCTL_LOGIN_USER			1
175 #define TEE_IOCTL_LOGIN_GROUP			2
176 #define TEE_IOCTL_LOGIN_APPLICATION		4
177 #define TEE_IOCTL_LOGIN_USER_APPLICATION	5
178 #define TEE_IOCTL_LOGIN_GROUP_APPLICATION	6
179 /*
180  * Disallow user-space to use GP implementation specific login
181  * method range (0x80000000 - 0xBFFFFFFF). This range is rather
182  * being reserved for REE kernel clients or TEE implementation.
183  */
184 #define TEE_IOCTL_LOGIN_REE_KERNEL_MIN		0x80000000
185 #define TEE_IOCTL_LOGIN_REE_KERNEL_MAX		0xBFFFFFFF
186 /* Private login method for REE kernel clients */
187 #define TEE_IOCTL_LOGIN_REE_KERNEL		0x80000000
188 
189 /**
190  * struct tee_ioctl_param - parameter
191  * @attr: attributes
192  * @a: if a memref, offset into the shared memory object, else a value parameter
193  * @b: if a memref, size of the buffer, else a value parameter
194  * @c: if a memref, shared memory identifier, else a value parameter
195  *
196  * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref or value is used in
197  * the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value and
198  * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref. TEE_PARAM_ATTR_TYPE_NONE
199  * indicates that none of the members are used.
200  *
201  * Shared memory is allocated with TEE_IOC_SHM_ALLOC which returns an
202  * identifier representing the shared memory object. A memref can reference
203  * a part of a shared memory by specifying an offset (@a) and size (@b) of
204  * the object. To supply the entire shared memory object set the offset
205  * (@a) to 0 and size (@b) to the previously returned size of the object.
206  *
207  * A client may need to present a NULL pointer in the argument
208  * passed to a trusted application in the TEE.
209  * This is also a requirement in GlobalPlatform Client API v1.0c
210  * (section 3.2.5 memory references), which can be found at
211  * http://www.globalplatform.org/specificationsdevice.asp
212  *
213  * If a NULL pointer is passed to a TA in the TEE, the (@c)
214  * IOCTL parameters value must be set to TEE_MEMREF_NULL indicating a NULL
215  * memory reference.
216  */
217 struct tee_ioctl_param {
218 	__u64 attr;
219 	__u64 a;
220 	__u64 b;
221 	__u64 c;
222 };
223 
224 #define TEE_IOCTL_UUID_LEN		16
225 
226 /**
227  * struct tee_ioctl_open_session_arg - Open session argument
228  * @uuid:	[in] UUID of the Trusted Application
229  * @clnt_uuid:	[in] UUID of client
230  * @clnt_login:	[in] Login class of client, TEE_IOCTL_LOGIN_* above
231  * @cancel_id:	[in] Cancellation id, a unique value to identify this request
232  * @session:	[out] Session id
233  * @ret:	[out] return value
234  * @ret_origin	[out] origin of the return value
235  * @num_params	[in] number of parameters following this struct
236  */
237 struct tee_ioctl_open_session_arg {
238 	__u8 uuid[TEE_IOCTL_UUID_LEN];
239 	__u8 clnt_uuid[TEE_IOCTL_UUID_LEN];
240 	__u32 clnt_login;
241 	__u32 cancel_id;
242 	__u32 session;
243 	__u32 ret;
244 	__u32 ret_origin;
245 	__u32 num_params;
246 	/* num_params tells the actual number of element in params */
247 	struct tee_ioctl_param params[];
248 };
249 
250 /**
251  * TEE_IOC_OPEN_SESSION - opens a session to a Trusted Application
252  *
253  * Takes a struct tee_ioctl_buf_data which contains a struct
254  * tee_ioctl_open_session_arg followed by any array of struct
255  * tee_ioctl_param
256  */
257 #define TEE_IOC_OPEN_SESSION	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 2, \
258 				     struct tee_ioctl_buf_data)
259 
260 /**
261  * struct tee_ioctl_invoke_func_arg - Invokes a function in a Trusted
262  * Application
263  * @func:	[in] Trusted Application function, specific to the TA
264  * @session:	[in] Session id
265  * @cancel_id:	[in] Cancellation id, a unique value to identify this request
266  * @ret:	[out] return value
267  * @ret_origin	[out] origin of the return value
268  * @num_params	[in] number of parameters following this struct
269  */
270 struct tee_ioctl_invoke_arg {
271 	__u32 func;
272 	__u32 session;
273 	__u32 cancel_id;
274 	__u32 ret;
275 	__u32 ret_origin;
276 	__u32 num_params;
277 	/* num_params tells the actual number of element in params */
278 	struct tee_ioctl_param params[];
279 };
280 
281 /**
282  * TEE_IOC_INVOKE - Invokes a function in a Trusted Application
283  *
284  * Takes a struct tee_ioctl_buf_data which contains a struct
285  * tee_invoke_func_arg followed by any array of struct tee_param
286  */
287 #define TEE_IOC_INVOKE		_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 3, \
288 				     struct tee_ioctl_buf_data)
289 
290 /**
291  * struct tee_ioctl_cancel_arg - Cancels an open session or invoke ioctl
292  * @cancel_id:	[in] Cancellation id, a unique value to identify this request
293  * @session:	[in] Session id, if the session is opened, else set to 0
294  */
295 struct tee_ioctl_cancel_arg {
296 	__u32 cancel_id;
297 	__u32 session;
298 };
299 
300 /**
301  * TEE_IOC_CANCEL - Cancels an open session or invoke
302  */
303 #define TEE_IOC_CANCEL		_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 4, \
304 				     struct tee_ioctl_cancel_arg)
305 
306 /**
307  * struct tee_ioctl_close_session_arg - Closes an open session
308  * @session:	[in] Session id
309  */
310 struct tee_ioctl_close_session_arg {
311 	__u32 session;
312 };
313 
314 /**
315  * TEE_IOC_CLOSE_SESSION - Closes a session
316  */
317 #define TEE_IOC_CLOSE_SESSION	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 5, \
318 				     struct tee_ioctl_close_session_arg)
319 
320 /**
321  * struct tee_iocl_supp_recv_arg - Receive a request for a supplicant function
322  * @func:	[in] supplicant function
323  * @num_params	[in/out] number of parameters following this struct
324  *
325  * @num_params is the number of params that tee-supplicant has room to
326  * receive when input, @num_params is the number of actual params
327  * tee-supplicant receives when output.
328  */
329 struct tee_iocl_supp_recv_arg {
330 	__u32 func;
331 	__u32 num_params;
332 	/* num_params tells the actual number of element in params */
333 	struct tee_ioctl_param params[];
334 };
335 
336 /**
337  * TEE_IOC_SUPPL_RECV - Receive a request for a supplicant function
338  *
339  * Takes a struct tee_ioctl_buf_data which contains a struct
340  * tee_iocl_supp_recv_arg followed by any array of struct tee_param
341  */
342 #define TEE_IOC_SUPPL_RECV	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 6, \
343 				     struct tee_ioctl_buf_data)
344 
345 /**
346  * struct tee_iocl_supp_send_arg - Send a response to a received request
347  * @ret:	[out] return value
348  * @num_params	[in] number of parameters following this struct
349  */
350 struct tee_iocl_supp_send_arg {
351 	__u32 ret;
352 	__u32 num_params;
353 	/* num_params tells the actual number of element in params */
354 	struct tee_ioctl_param params[];
355 };
356 
357 /**
358  * TEE_IOC_SUPPL_SEND - Send a response to a received request
359  *
360  * Takes a struct tee_ioctl_buf_data which contains a struct
361  * tee_iocl_supp_send_arg followed by any array of struct tee_param
362  */
363 #define TEE_IOC_SUPPL_SEND	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 7, \
364 				     struct tee_ioctl_buf_data)
365 
366 /**
367  * struct tee_ioctl_shm_register_data - Shared memory register argument
368  * @addr:      [in] Start address of shared memory to register
369  * @length:    [in/out] Length of shared memory to register
370  * @flags:     [in/out] Flags to/from registration.
371  * @id:                [out] Identifier of the shared memory
372  *
373  * The flags field should currently be zero as input. Updated by the call
374  * with actual flags as defined by TEE_IOCTL_SHM_* above.
375  * This structure is used as argument for TEE_IOC_SHM_REGISTER below.
376  */
377 struct tee_ioctl_shm_register_data {
378 	__u64 addr;
379 	__u64 length;
380 	__u32 flags;
381 	__s32 id;
382 };
383 
384 /**
385  * TEE_IOC_SHM_REGISTER - Register shared memory argument
386  *
387  * Registers shared memory between the user space process and secure OS.
388  *
389  * Returns a file descriptor on success or < 0 on failure
390  *
391  * The shared memory is unregisterred when the descriptor is closed.
392  */
393 #define TEE_IOC_SHM_REGISTER   _IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 9, \
394 				     struct tee_ioctl_shm_register_data)
395 /*
396  * Five syscalls are used when communicating with the TEE driver.
397  * open(): opens the device associated with the driver
398  * ioctl(): as described above operating on the file descriptor from open()
399  * close(): two cases
400  *   - closes the device file descriptor
401  *   - closes a file descriptor connected to allocated shared memory
402  * mmap(): maps shared memory into user space using information from struct
403  *	   tee_ioctl_shm_alloc_data
404  * munmap(): unmaps previously shared memory
405  */
406 
407 #endif /*__TEE_H*/
408