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 
54 /*
55  * TEE Implementation ID
56  */
57 #define TEE_IMPL_ID_OPTEE	1
58 
59 /*
60  * OP-TEE specific capabilities
61  */
62 #define TEE_OPTEE_CAP_TZ	(1 << 0)
63 
64 /**
65  * struct tee_ioctl_version_data - TEE version
66  * @impl_id:	[out] TEE implementation id
67  * @impl_caps:	[out] Implementation specific capabilities
68  * @gen_caps:	[out] Generic capabilities, defined by TEE_GEN_CAPS_* above
69  *
70  * Identifies the TEE implementation, @impl_id is one of TEE_IMPL_ID_* above.
71  * @impl_caps is implementation specific, for example TEE_OPTEE_CAP_*
72  * is valid when @impl_id == TEE_IMPL_ID_OPTEE.
73  */
74 struct tee_ioctl_version_data {
75 	__u32 impl_id;
76 	__u32 impl_caps;
77 	__u32 gen_caps;
78 };
79 
80 /**
81  * TEE_IOC_VERSION - query version of TEE
82  *
83  * Takes a tee_ioctl_version_data struct and returns with the TEE version
84  * data filled in.
85  */
86 #define TEE_IOC_VERSION		_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 0, \
87 				     struct tee_ioctl_version_data)
88 
89 /**
90  * struct tee_ioctl_shm_alloc_data - Shared memory allocate argument
91  * @size:	[in/out] Size of shared memory to allocate
92  * @flags:	[in/out] Flags to/from allocation.
93  * @id:		[out] Identifier of the shared memory
94  *
95  * The flags field should currently be zero as input. Updated by the call
96  * with actual flags as defined by TEE_IOCTL_SHM_* above.
97  * This structure is used as argument for TEE_IOC_SHM_ALLOC below.
98  */
99 struct tee_ioctl_shm_alloc_data {
100 	__u64 size;
101 	__u32 flags;
102 	__s32 id;
103 };
104 
105 /**
106  * TEE_IOC_SHM_ALLOC - allocate shared memory
107  *
108  * Allocates shared memory between the user space process and secure OS.
109  *
110  * Returns a file descriptor on success or < 0 on failure
111  *
112  * The returned file descriptor is used to map the shared memory into user
113  * space. The shared memory is freed when the descriptor is closed and the
114  * memory is unmapped.
115  */
116 #define TEE_IOC_SHM_ALLOC	_IOWR(TEE_IOC_MAGIC, TEE_IOC_BASE + 1, \
117 				     struct tee_ioctl_shm_alloc_data)
118 
119 /**
120  * struct tee_ioctl_buf_data - Variable sized buffer
121  * @buf_ptr:	[in] A __user pointer to a buffer
122  * @buf_len:	[in] Length of the buffer above
123  *
124  * Used as argument for TEE_IOC_OPEN_SESSION, TEE_IOC_INVOKE,
125  * TEE_IOC_SUPPL_RECV, and TEE_IOC_SUPPL_SEND below.
126  */
127 struct tee_ioctl_buf_data {
128 	__u64 buf_ptr;
129 	__u64 buf_len;
130 };
131 
132 /*
133  * Attributes for struct tee_ioctl_param, selects field in the union
134  */
135 #define TEE_IOCTL_PARAM_ATTR_TYPE_NONE		0	/* parameter not used */
136 
137 /*
138  * These defines value parameters (struct tee_ioctl_param_value)
139  */
140 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT	1
141 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT	2
142 #define TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT	3	/* input and output */
143 
144 /*
145  * These defines shared memory reference parameters (struct
146  * tee_ioctl_param_memref)
147  */
148 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT	5
149 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT	6
150 #define TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT	7	/* input and output */
151 
152 /*
153  * Mask for the type part of the attribute, leaves room for more types
154  */
155 #define TEE_IOCTL_PARAM_ATTR_TYPE_MASK		0xff
156 
157 /*
158  * Matches TEEC_LOGIN_* in GP TEE Client API
159  * Are only defined for GP compliant TEEs
160  */
161 #define TEE_IOCTL_LOGIN_PUBLIC			0
162 #define TEE_IOCTL_LOGIN_USER			1
163 #define TEE_IOCTL_LOGIN_GROUP			2
164 #define TEE_IOCTL_LOGIN_APPLICATION		4
165 #define TEE_IOCTL_LOGIN_USER_APPLICATION	5
166 #define TEE_IOCTL_LOGIN_GROUP_APPLICATION	6
167 
168 /**
169  * struct tee_ioctl_param - parameter
170  * @attr: attributes
171  * @a: if a memref, offset into the shared memory object, else a value parameter
172  * @b: if a memref, size of the buffer, else a value parameter
173  * @c: if a memref, shared memory identifier, else a value parameter
174  *
175  * @attr & TEE_PARAM_ATTR_TYPE_MASK indicates if memref or value is used in
176  * the union. TEE_PARAM_ATTR_TYPE_VALUE_* indicates value and
177  * TEE_PARAM_ATTR_TYPE_MEMREF_* indicates memref. TEE_PARAM_ATTR_TYPE_NONE
178  * indicates that none of the members are used.
179  *
180  * Shared memory is allocated with TEE_IOC_SHM_ALLOC which returns an
181  * identifier representing the shared memory object. A memref can reference
182  * a part of a shared memory by specifying an offset (@a) and size (@b) of
183  * the object. To supply the entire shared memory object set the offset
184  * (@a) to 0 and size (@b) to the previously returned size of the object.
185  */
186 struct tee_ioctl_param {
187 	__u64 attr;
188 	__u64 a;
189 	__u64 b;
190 	__u64 c;
191 };
192 
193 #define TEE_IOCTL_UUID_LEN		16
194 
195 /**
196  * struct tee_ioctl_open_session_arg - Open session argument
197  * @uuid:	[in] UUID of the Trusted Application
198  * @clnt_uuid:	[in] UUID of client
199  * @clnt_login:	[in] Login class of client, TEE_IOCTL_LOGIN_* above
200  * @cancel_id:	[in] Cancellation id, a unique value to identify this request
201  * @session:	[out] Session id
202  * @ret:	[out] return value
203  * @ret_origin	[out] origin of the return value
204  * @num_params	[in] number of parameters following this struct
205  */
206 struct tee_ioctl_open_session_arg {
207 	__u8 uuid[TEE_IOCTL_UUID_LEN];
208 	__u8 clnt_uuid[TEE_IOCTL_UUID_LEN];
209 	__u32 clnt_login;
210 	__u32 cancel_id;
211 	__u32 session;
212 	__u32 ret;
213 	__u32 ret_origin;
214 	__u32 num_params;
215 	/* num_params tells the actual number of element in params */
216 	struct tee_ioctl_param params[];
217 };
218 
219 /**
220  * TEE_IOC_OPEN_SESSION - opens a session to a Trusted Application
221  *
222  * Takes a struct tee_ioctl_buf_data which contains a struct
223  * tee_ioctl_open_session_arg followed by any array of struct
224  * tee_ioctl_param
225  */
226 #define TEE_IOC_OPEN_SESSION	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 2, \
227 				     struct tee_ioctl_buf_data)
228 
229 /**
230  * struct tee_ioctl_invoke_func_arg - Invokes a function in a Trusted
231  * Application
232  * @func:	[in] Trusted Application function, specific to the TA
233  * @session:	[in] Session id
234  * @cancel_id:	[in] Cancellation id, a unique value to identify this request
235  * @ret:	[out] return value
236  * @ret_origin	[out] origin of the return value
237  * @num_params	[in] number of parameters following this struct
238  */
239 struct tee_ioctl_invoke_arg {
240 	__u32 func;
241 	__u32 session;
242 	__u32 cancel_id;
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_INVOKE - Invokes a function in a Trusted Application
252  *
253  * Takes a struct tee_ioctl_buf_data which contains a struct
254  * tee_invoke_func_arg followed by any array of struct tee_param
255  */
256 #define TEE_IOC_INVOKE		_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 3, \
257 				     struct tee_ioctl_buf_data)
258 
259 /**
260  * struct tee_ioctl_cancel_arg - Cancels an open session or invoke ioctl
261  * @cancel_id:	[in] Cancellation id, a unique value to identify this request
262  * @session:	[in] Session id, if the session is opened, else set to 0
263  */
264 struct tee_ioctl_cancel_arg {
265 	__u32 cancel_id;
266 	__u32 session;
267 };
268 
269 /**
270  * TEE_IOC_CANCEL - Cancels an open session or invoke
271  */
272 #define TEE_IOC_CANCEL		_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 4, \
273 				     struct tee_ioctl_cancel_arg)
274 
275 /**
276  * struct tee_ioctl_close_session_arg - Closes an open session
277  * @session:	[in] Session id
278  */
279 struct tee_ioctl_close_session_arg {
280 	__u32 session;
281 };
282 
283 /**
284  * TEE_IOC_CLOSE_SESSION - Closes a session
285  */
286 #define TEE_IOC_CLOSE_SESSION	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 5, \
287 				     struct tee_ioctl_close_session_arg)
288 
289 /**
290  * struct tee_iocl_supp_recv_arg - Receive a request for a supplicant function
291  * @func:	[in] supplicant function
292  * @num_params	[in/out] number of parameters following this struct
293  *
294  * @num_params is the number of params that tee-supplicant has room to
295  * receive when input, @num_params is the number of actual params
296  * tee-supplicant receives when output.
297  */
298 struct tee_iocl_supp_recv_arg {
299 	__u32 func;
300 	__u32 num_params;
301 	/* num_params tells the actual number of element in params */
302 	struct tee_ioctl_param params[];
303 };
304 
305 /**
306  * TEE_IOC_SUPPL_RECV - Receive a request for a supplicant function
307  *
308  * Takes a struct tee_ioctl_buf_data which contains a struct
309  * tee_iocl_supp_recv_arg followed by any array of struct tee_param
310  */
311 #define TEE_IOC_SUPPL_RECV	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 6, \
312 				     struct tee_ioctl_buf_data)
313 
314 /**
315  * struct tee_iocl_supp_send_arg - Send a response to a received request
316  * @ret:	[out] return value
317  * @num_params	[in] number of parameters following this struct
318  */
319 struct tee_iocl_supp_send_arg {
320 	__u32 ret;
321 	__u32 num_params;
322 	/* num_params tells the actual number of element in params */
323 	struct tee_ioctl_param params[];
324 };
325 
326 /**
327  * TEE_IOC_SUPPL_SEND - Receive a request for a supplicant function
328  *
329  * Takes a struct tee_ioctl_buf_data which contains a struct
330  * tee_iocl_supp_send_arg followed by any array of struct tee_param
331  */
332 #define TEE_IOC_SUPPL_SEND	_IOR(TEE_IOC_MAGIC, TEE_IOC_BASE + 7, \
333 				     struct tee_ioctl_buf_data)
334 
335 /*
336  * Five syscalls are used when communicating with the TEE driver.
337  * open(): opens the device associated with the driver
338  * ioctl(): as described above operating on the file descriptor from open()
339  * close(): two cases
340  *   - closes the device file descriptor
341  *   - closes a file descriptor connected to allocated shared memory
342  * mmap(): maps shared memory into user space using information from struct
343  *	   tee_ioctl_shm_alloc_data
344  * munmap(): unmaps previously shared memory
345  */
346 
347 #endif /*__TEE_H*/
348