tee: define session login identifiers
[platform/kernel/u-boot.git] / include / tee.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (c) 2018 Linaro Limited
4  */
5
6 #ifndef __TEE_H
7 #define __TEE_H
8
9 #include <linux/bitops.h>
10 #include <linux/list.h>
11
12 #define TEE_UUID_LEN            16
13
14 #define TEE_GEN_CAP_GP          BIT(0)  /* GlobalPlatform compliant TEE */
15 #define TEE_GEN_CAP_REG_MEM     BIT(1)  /* Supports registering shared memory */
16
17 #define TEE_SHM_REGISTER        BIT(0)  /* In list of shared memory */
18 #define TEE_SHM_SEC_REGISTER    BIT(1)  /* TEE notified of this memory */
19 #define TEE_SHM_ALLOC           BIT(2)  /* The memory is malloced() and must */
20                                         /* be freed() */
21
22 #define TEE_PARAM_ATTR_TYPE_NONE                0       /* parameter not used */
23 #define TEE_PARAM_ATTR_TYPE_VALUE_INPUT         1
24 #define TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT        2
25 #define TEE_PARAM_ATTR_TYPE_VALUE_INOUT         3       /* input and output */
26 #define TEE_PARAM_ATTR_TYPE_MEMREF_INPUT        5
27 #define TEE_PARAM_ATTR_TYPE_MEMREF_OUTPUT       6
28 #define TEE_PARAM_ATTR_TYPE_MEMREF_INOUT        7       /* input and output */
29 #define TEE_PARAM_ATTR_TYPE_MASK                0xff
30 #define TEE_PARAM_ATTR_META                     0x100
31 #define TEE_PARAM_ATTR_MASK                     (TEE_PARAM_ATTR_TYPE_MASK | \
32                                                  TEE_PARAM_ATTR_META)
33
34 /*
35  * Global Platform login identifiers for tee_open_session_arg::clnt_login
36  */
37 #define TEE_LOGIN_PUBLIC                  0x00000000
38 #define TEE_LOGIN_USER                    0x00000001
39 #define TEE_LOGIN_GROUP                   0x00000002
40 #define TEE_LOGIN_APPLICATION             0x00000004
41 #define TEE_LOGIN_APPLICATION_USER        0x00000005
42 #define TEE_LOGIN_APPLICATION_GROUP       0x00000006
43 /*
44  * Reserve use of GP implementation specific login method range
45  * (0x80000000 - 0xBFFFFFFF). This range is rather being used
46  * for REE kernel clients or TEE implementation.
47  */
48 #define TEE_LOGIN_REE_KERNEL_MIN          0x80000000
49 #define TEE_LOGIN_REE_KERNEL_MAX          0xBFFFFFFF
50 /* Private login method for REE kernel/privileged clients */
51 #define TEE_LOGIN_REE_KERNEL              0x80000000
52
53 /*
54  * Some Global Platform error codes which has a meaning if the
55  * TEE_GEN_CAP_GP bit is returned by the driver in
56  * struct tee_version_data::gen_caps
57  */
58 #define TEE_SUCCESS                     0x00000000
59 #define TEE_ERROR_STORAGE_NOT_AVAILABLE 0xf0100003
60 #define TEE_ERROR_GENERIC               0xffff0000
61 #define TEE_ERROR_BAD_PARAMETERS        0xffff0006
62 #define TEE_ERROR_ITEM_NOT_FOUND        0xffff0008
63 #define TEE_ERROR_NOT_IMPLEMENTED       0xffff0009
64 #define TEE_ERROR_NOT_SUPPORTED         0xffff000a
65 #define TEE_ERROR_COMMUNICATION         0xffff000e
66 #define TEE_ERROR_SECURITY              0xffff000f
67 #define TEE_ERROR_OUT_OF_MEMORY         0xffff000c
68 #define TEE_ERROR_OVERFLOW              0xffff300f
69 #define TEE_ERROR_TARGET_DEAD           0xffff3024
70 #define TEE_ERROR_STORAGE_NO_SPACE      0xffff3041
71
72 #define TEE_ORIGIN_COMMS                0x00000002
73 #define TEE_ORIGIN_TEE                  0x00000003
74 #define TEE_ORIGIN_TRUSTED_APP          0x00000004
75
76 struct udevice;
77
78 /**
79  * struct tee_optee_ta_uuid - OP-TEE Trusted Application (TA) UUID format
80  *
81  * Used to identify an OP-TEE TA and define suitable to initialize structs
82  * of this format is distributed with the interface of the TA. The
83  * individual fields of this struct doesn't have any special meaning in
84  * OP-TEE. See RFC4122 for details on the format.
85  */
86 struct tee_optee_ta_uuid {
87         u32 time_low;
88         u16 time_mid;
89         u16 time_hi_and_version;
90         u8 clock_seq_and_node[8];
91 };
92
93 /**
94  * struct tee_shm - memory shared with the TEE
95  * @dev:        The TEE device
96  * @link:       List node in the list in struct struct tee_uclass_priv
97  * @addr:       Pointer to the shared memory
98  * @size:       Size of the the shared memory
99  * @flags:      TEE_SHM_* above
100  */
101 struct tee_shm {
102         struct udevice *dev;
103         struct list_head link;
104         void *addr;
105         ulong size;
106         u32 flags;
107 };
108
109 /**
110  * struct tee_param_memref - memory reference for a Trusted Application
111  * @shm_offs:   Offset in bytes into the shared memory object @shm
112  * @size:       Size in bytes of the memory reference
113  * @shm:        Pointer to a shared memory object for the buffer
114  *
115  * Used as a part of struct tee_param, see that for more information.
116  */
117 struct tee_param_memref {
118         ulong shm_offs;
119         ulong size;
120         struct tee_shm *shm;
121 };
122
123 /**
124  * struct tee_param_value - value parameter for a Trusted Application
125  * @a, @b, @c:  Parameters passed by value
126  *
127  * Used as a part of struct tee_param, see that for more information.
128  */
129 struct tee_param_value {
130         u64 a;
131         u64 b;
132         u64 c;
133 };
134
135 /**
136  * struct tee_param - invoke parameter for a Trusted Application
137  * @attr:       Attributes
138  * @u.memref:   Memref parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
139  *              TEE_PARAM_ATTR_TYPE_MEMREF_* above
140  * @u.value:    Value parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
141  *              TEE_PARAM_ATTR_TYPE_VALUE_* above
142  *
143  * Parameters to TA are passed using an array of this struct, for
144  * flexibility both value parameters and memory refereces can be used.
145  */
146 struct tee_param {
147         u64 attr;
148         union {
149                 struct tee_param_memref memref;
150                 struct tee_param_value value;
151         } u;
152 };
153
154 /**
155  * struct tee_open_session_arg - extra arguments for tee_open_session()
156  * @uuid:       [in] UUID of the Trusted Application
157  * @clnt_uuid:  [in] UUID of client, zeroes for PUBLIC/REE_KERNEL
158  * @clnt_login: [in] Class of client TEE_LOGIN_*
159  * @session:    [out] Session id
160  * @ret:        [out] return value
161  * @ret_origin: [out] origin of the return value
162  */
163 struct tee_open_session_arg {
164         u8 uuid[TEE_UUID_LEN];
165         u8 clnt_uuid[TEE_UUID_LEN];
166         u32 clnt_login;
167         u32 session;
168         u32 ret;
169         u32 ret_origin;
170 };
171
172 /**
173  * struct tee_invoke_arg - extra arguments for tee_invoke_func()
174  * @func:       [in] Trusted Application function, specific to the TA
175  * @session:    [in] Session id, from open session
176  * @ret:        [out] return value
177  * @ret_origin: [out] origin of the return value
178  */
179 struct tee_invoke_arg {
180         u32 func;
181         u32 session;
182         u32 ret;
183         u32 ret_origin;
184 };
185
186 /**
187  * struct tee_version_data - description of TEE
188  * @gen_caps:   Generic capabilities, TEE_GEN_CAP_* above
189  */
190 struct tee_version_data {
191         u32 gen_caps;
192 };
193
194 /**
195  * struct tee_driver_ops - TEE driver operations
196  * @get_version:        Query capabilities of TEE device,
197  * @open_session:       Opens a session to a Trusted Application in the TEE,
198  * @close_session:      Closes a session to Trusted Application,
199  * @invoke_func:        Invokes a function in a Trusted Application,
200  * @shm_register:       Registers memory shared with the TEE
201  * @shm_unregister:     Unregisters memory shared with the TEE
202  */
203 struct tee_driver_ops {
204         /**
205          * get_version() - Query capabilities of TEE device
206          * @dev:        The TEE device
207          * @vers:       Pointer to version data
208          */
209         void (*get_version)(struct udevice *dev, struct tee_version_data *vers);
210         /**
211          * open_session() - Open a session to a Trusted Application
212          * @dev:        The TEE device
213          * @arg:        Open session arguments
214          * @num_param:  Number of elements in @param
215          * @param:      Parameters for Trusted Application
216          *
217          * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
218          * TEE_SUCCESS the session identifier is available in @arg->session.
219          */
220         int (*open_session)(struct udevice *dev,
221                             struct tee_open_session_arg *arg, uint num_param,
222                             struct tee_param *param);
223         /**
224          * close_session() - Close a session to a Trusted Application
225          * @dev:        The TEE device
226          * @session:    Session id
227          *
228          * Return < 0 on error else 0, regardless the session will not be valid
229          * after this function has returned.
230          */
231         int (*close_session)(struct udevice *dev, u32 session);
232         /**
233          * tee_invoke_func() - Invoke a function in a Trusted Application
234          * @dev:        The TEE device
235          * @arg:        Invoke arguments
236          * @num_param:  Number of elements in @param
237          * @param:      Parameters for Trusted Application
238          *
239          * Returns < 0 on error else see @arg->ret for result.
240          */
241         int (*invoke_func)(struct udevice *dev, struct tee_invoke_arg *arg,
242                            uint num_param, struct tee_param *param);
243         /**
244          * shm_register() - Registers memory shared with the TEE
245          * @dev:        The TEE device
246          * @shm:        Pointer to a shared memory object
247          * Returns 0 on success or < 0 on failure.
248          */
249         int (*shm_register)(struct udevice *dev, struct tee_shm *shm);
250         /**
251          * shm_unregister() - Unregisters memory shared with the TEE
252          * @dev:        The TEE device
253          * @shm:        Pointer to a shared memory object
254          * Returns 0 on success or < 0 on failure.
255          */
256         int (*shm_unregister)(struct udevice *dev, struct tee_shm *shm);
257 };
258
259 /**
260  * __tee_shm_add() - Internal helper function to register shared memory
261  * @dev:        The TEE device
262  * @align:      Required alignment of allocated memory block if
263  *              (@flags & TEE_SHM_ALLOC)
264  * @addr:       Address of memory block, ignored if (@flags & TEE_SHM_ALLOC)
265  * @size:       Size of memory block
266  * @flags:      TEE_SHM_* above
267  * @shmp:       If the function return 0, this holds the allocated
268  *              struct tee_shm
269  *
270  * returns 0 on success or < 0 on failure.
271  */
272 int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
273                   u32 flags, struct tee_shm **shmp);
274
275 /**
276  * tee_shm_alloc() - Allocate shared memory
277  * @dev:        The TEE device
278  * @size:       Size of memory block
279  * @flags:      TEE_SHM_* above
280  * @shmp:       If the function return 0, this holds the allocated
281  *              struct tee_shm
282  *
283  * returns 0 on success or < 0 on failure.
284  */
285 int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
286                   struct tee_shm **shmp);
287
288 /**
289  * tee_shm_register() - Registers shared memory
290  * @dev:        The TEE device
291  * @addr:       Address of memory block
292  * @size:       Size of memory block
293  * @flags:      TEE_SHM_* above
294  * @shmp:       If the function return 0, this holds the allocated
295  *              struct tee_shm
296  *
297  * returns 0 on success or < 0 on failure.
298  */
299 int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
300                      struct tee_shm **shmp);
301
302 /**
303  * tee_shm_free() - Frees shared memory
304  * @shm:        Shared memory object
305  */
306 void tee_shm_free(struct tee_shm *shm);
307
308 /**
309  * tee_shm_is_registered() - Check register status of shared memory object
310  * @shm:        Pointer to shared memory object
311  * @dev:        The TEE device
312  *
313  * Returns true if the shared memory object is registered for the supplied
314  * TEE device
315  */
316 bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev);
317
318 /**
319  * tee_find_device() - Look up a TEE device
320  * @start:      if not NULL, continue search after this device
321  * @match:      function to check TEE device, returns != 0 if the device
322  *              matches
323  * @data:       data for match function
324  * @vers:       if not NULL, version data of TEE device of the device returned
325  *
326  * Returns a probed TEE device of the first TEE device matched by the
327  * match() callback or NULL.
328  */
329 #if CONFIG_IS_ENABLED(TEE)
330 struct udevice *tee_find_device(struct udevice *start,
331                                 int (*match)(struct tee_version_data *vers,
332                                              const void *data),
333                                 const void *data,
334                                 struct tee_version_data *vers);
335 #else
336 static inline struct udevice *tee_find_device(struct udevice *start,
337                                               int (*match)(struct tee_version_data *vers,
338                                                            const void *data),
339                                               const void *data,
340                                               struct tee_version_data *vers)
341 {
342         return NULL;
343 }
344 #endif
345
346 /**
347  * tee_get_version() - Query capabilities of TEE device
348  * @dev:        The TEE device
349  * @vers:       Pointer to version data
350  */
351 void tee_get_version(struct udevice *dev, struct tee_version_data *vers);
352
353 /**
354  * tee_open_session() - Open a session to a Trusted Application
355  * @dev:        The TEE device
356  * @arg:        Open session arguments
357  * @num_param:  Number of elements in @param
358  * @param:      Parameters for Trusted Application
359  *
360  * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
361  * TEE_SUCCESS the session identifier is available in @arg->session.
362  */
363 int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
364                      uint num_param, struct tee_param *param);
365
366 /**
367  * tee_close_session() - Close a session to a Trusted Application
368  * @dev:        The TEE device
369  * @session:    Session id
370  *
371  * Return < 0 on error else 0, regardless the session will not be valid
372  * after this function has returned.
373  */
374 int tee_close_session(struct udevice *dev, u32 session);
375
376 /**
377  * tee_invoke_func() - Invoke a function in a Trusted Application
378  * @dev:        The TEE device
379  * @arg:        Invoke arguments
380  * @num_param:  Number of elements in @param
381  * @param:      Parameters for Trusted Application
382  *
383  * Returns < 0 on error else see @arg->ret for result.
384  */
385 int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
386                     uint num_param, struct tee_param *param);
387
388 /**
389  * tee_optee_ta_uuid_from_octets() - Converts to struct tee_optee_ta_uuid
390  * @d:  Destination struct
391  * @s:  Source UUID octets
392  *
393  * Conversion to a struct tee_optee_ta_uuid represantion from binary octet
394  * representation.
395  */
396 void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
397                                    const u8 s[TEE_UUID_LEN]);
398
399 /**
400  * tee_optee_ta_uuid_to_octets() - Converts from struct tee_optee_ta_uuid
401  * @d:  Destination UUID octets
402  * @s:  Source struct
403  *
404  * Conversion from a struct tee_optee_ta_uuid represantion to binary octet
405  * representation.
406  */
407 void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
408                                  const struct tee_optee_ta_uuid *s);
409
410 /**
411  * tee_flush_all_shm_dcache() - Flush data cache for all shared memories
412  * @dev:        The TEE device
413  */
414 void tee_flush_all_shm_dcache(struct udevice *dev);
415
416 #endif /* __TEE_H */