Merge tag 'ti-v2021.07-rc1' of https://source.denx.de/u-boot/custodians/u-boot-ti
[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  * Some Global Platform error codes which has a meaning if the
36  * TEE_GEN_CAP_GP bit is returned by the driver in
37  * struct tee_version_data::gen_caps
38  */
39 #define TEE_SUCCESS                     0x00000000
40 #define TEE_ERROR_STORAGE_NOT_AVAILABLE 0xf0100003
41 #define TEE_ERROR_GENERIC               0xffff0000
42 #define TEE_ERROR_BAD_PARAMETERS        0xffff0006
43 #define TEE_ERROR_ITEM_NOT_FOUND        0xffff0008
44 #define TEE_ERROR_NOT_IMPLEMENTED       0xffff0009
45 #define TEE_ERROR_NOT_SUPPORTED         0xffff000a
46 #define TEE_ERROR_COMMUNICATION         0xffff000e
47 #define TEE_ERROR_SECURITY              0xffff000f
48 #define TEE_ERROR_OUT_OF_MEMORY         0xffff000c
49 #define TEE_ERROR_OVERFLOW              0xffff300f
50 #define TEE_ERROR_TARGET_DEAD           0xffff3024
51 #define TEE_ERROR_STORAGE_NO_SPACE      0xffff3041
52
53 #define TEE_ORIGIN_COMMS                0x00000002
54 #define TEE_ORIGIN_TEE                  0x00000003
55 #define TEE_ORIGIN_TRUSTED_APP          0x00000004
56
57 struct udevice;
58
59 /**
60  * struct tee_optee_ta_uuid - OP-TEE Trusted Application (TA) UUID format
61  *
62  * Used to identify an OP-TEE TA and define suitable to initialize structs
63  * of this format is distributed with the interface of the TA. The
64  * individual fields of this struct doesn't have any special meaning in
65  * OP-TEE. See RFC4122 for details on the format.
66  */
67 struct tee_optee_ta_uuid {
68         u32 time_low;
69         u16 time_mid;
70         u16 time_hi_and_version;
71         u8 clock_seq_and_node[8];
72 };
73
74 /**
75  * struct tee_shm - memory shared with the TEE
76  * @dev:        The TEE device
77  * @link:       List node in the list in struct struct tee_uclass_priv
78  * @addr:       Pointer to the shared memory
79  * @size:       Size of the the shared memory
80  * @flags:      TEE_SHM_* above
81  */
82 struct tee_shm {
83         struct udevice *dev;
84         struct list_head link;
85         void *addr;
86         ulong size;
87         u32 flags;
88 };
89
90 /**
91  * struct tee_param_memref - memory reference for a Trusted Application
92  * @shm_offs:   Offset in bytes into the shared memory object @shm
93  * @size:       Size in bytes of the memory reference
94  * @shm:        Pointer to a shared memory object for the buffer
95  *
96  * Used as a part of struct tee_param, see that for more information.
97  */
98 struct tee_param_memref {
99         ulong shm_offs;
100         ulong size;
101         struct tee_shm *shm;
102 };
103
104 /**
105  * struct tee_param_value - value parameter for a Trusted Application
106  * @a, @b, @c:  Parameters passed by value
107  *
108  * Used as a part of struct tee_param, see that for more information.
109  */
110 struct tee_param_value {
111         u64 a;
112         u64 b;
113         u64 c;
114 };
115
116 /**
117  * struct tee_param - invoke parameter for a Trusted Application
118  * @attr:       Attributes
119  * @u.memref:   Memref parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
120  *              TEE_PARAM_ATTR_TYPE_MEMREF_* above
121  * @u.value:    Value parameter if (@attr & TEE_PARAM_ATTR_MASK) is one of
122  *              TEE_PARAM_ATTR_TYPE_VALUE_* above
123  *
124  * Parameters to TA are passed using an array of this struct, for
125  * flexibility both value parameters and memory refereces can be used.
126  */
127 struct tee_param {
128         u64 attr;
129         union {
130                 struct tee_param_memref memref;
131                 struct tee_param_value value;
132         } u;
133 };
134
135 /**
136  * struct tee_open_session_arg - extra arguments for tee_open_session()
137  * @uuid:       [in] UUID of the Trusted Application
138  * @clnt_uuid:  [in] Normally zeroes
139  * @clnt_login: [in] Normally 0
140  * @session:    [out] Session id
141  * @ret:        [out] return value
142  * @ret_origin: [out] origin of the return value
143  */
144 struct tee_open_session_arg {
145         u8 uuid[TEE_UUID_LEN];
146         u8 clnt_uuid[TEE_UUID_LEN];
147         u32 clnt_login;
148         u32 session;
149         u32 ret;
150         u32 ret_origin;
151 };
152
153 /**
154  * struct tee_invoke_arg - extra arguments for tee_invoke_func()
155  * @func:       [in] Trusted Application function, specific to the TA
156  * @session:    [in] Session id, from open session
157  * @ret:        [out] return value
158  * @ret_origin: [out] origin of the return value
159  */
160 struct tee_invoke_arg {
161         u32 func;
162         u32 session;
163         u32 ret;
164         u32 ret_origin;
165 };
166
167 /**
168  * struct tee_version_data - description of TEE
169  * @gen_caps:   Generic capabilities, TEE_GEN_CAP_* above
170  */
171 struct tee_version_data {
172         u32 gen_caps;
173 };
174
175 /**
176  * struct tee_driver_ops - TEE driver operations
177  * @get_version:        Query capabilities of TEE device,
178  * @open_session:       Opens a session to a Trusted Application in the TEE,
179  * @close_session:      Closes a session to Trusted Application,
180  * @invoke_func:        Invokes a function in a Trusted Application,
181  * @shm_register:       Registers memory shared with the TEE
182  * @shm_unregister:     Unregisters memory shared with the TEE
183  */
184 struct tee_driver_ops {
185         /**
186          * get_version() - Query capabilities of TEE device
187          * @dev:        The TEE device
188          * @vers:       Pointer to version data
189          */
190         void (*get_version)(struct udevice *dev, struct tee_version_data *vers);
191         /**
192          * open_session() - Open a session to a Trusted Application
193          * @dev:        The TEE device
194          * @arg:        Open session arguments
195          * @num_param:  Number of elements in @param
196          * @param:      Parameters for Trusted Application
197          *
198          * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
199          * TEE_SUCCESS the session identifier is available in @arg->session.
200          */
201         int (*open_session)(struct udevice *dev,
202                             struct tee_open_session_arg *arg, uint num_param,
203                             struct tee_param *param);
204         /**
205          * close_session() - Close a session to a Trusted Application
206          * @dev:        The TEE device
207          * @session:    Session id
208          *
209          * Return < 0 on error else 0, regardless the session will not be valid
210          * after this function has returned.
211          */
212         int (*close_session)(struct udevice *dev, u32 session);
213         /**
214          * tee_invoke_func() - Invoke a function in a Trusted Application
215          * @dev:        The TEE device
216          * @arg:        Invoke arguments
217          * @num_param:  Number of elements in @param
218          * @param:      Parameters for Trusted Application
219          *
220          * Returns < 0 on error else see @arg->ret for result.
221          */
222         int (*invoke_func)(struct udevice *dev, struct tee_invoke_arg *arg,
223                            uint num_param, struct tee_param *param);
224         /**
225          * shm_register() - Registers memory shared with the TEE
226          * @dev:        The TEE device
227          * @shm:        Pointer to a shared memory object
228          * Returns 0 on success or < 0 on failure.
229          */
230         int (*shm_register)(struct udevice *dev, struct tee_shm *shm);
231         /**
232          * shm_unregister() - Unregisters memory shared with the TEE
233          * @dev:        The TEE device
234          * @shm:        Pointer to a shared memory object
235          * Returns 0 on success or < 0 on failure.
236          */
237         int (*shm_unregister)(struct udevice *dev, struct tee_shm *shm);
238 };
239
240 /**
241  * __tee_shm_add() - Internal helper function to register shared memory
242  * @dev:        The TEE device
243  * @align:      Required alignment of allocated memory block if
244  *              (@flags & TEE_SHM_ALLOC)
245  * @addr:       Address of memory block, ignored if (@flags & TEE_SHM_ALLOC)
246  * @size:       Size of memory block
247  * @flags:      TEE_SHM_* above
248  * @shmp:       If the function return 0, this holds the allocated
249  *              struct tee_shm
250  *
251  * returns 0 on success or < 0 on failure.
252  */
253 int __tee_shm_add(struct udevice *dev, ulong align, void *addr, ulong size,
254                   u32 flags, struct tee_shm **shmp);
255
256 /**
257  * tee_shm_alloc() - Allocate shared memory
258  * @dev:        The TEE device
259  * @size:       Size of memory block
260  * @flags:      TEE_SHM_* above
261  * @shmp:       If the function return 0, this holds the allocated
262  *              struct tee_shm
263  *
264  * returns 0 on success or < 0 on failure.
265  */
266 int tee_shm_alloc(struct udevice *dev, ulong size, u32 flags,
267                   struct tee_shm **shmp);
268
269 /**
270  * tee_shm_register() - Registers shared memory
271  * @dev:        The TEE device
272  * @addr:       Address of memory block
273  * @size:       Size of memory block
274  * @flags:      TEE_SHM_* above
275  * @shmp:       If the function return 0, this holds the allocated
276  *              struct tee_shm
277  *
278  * returns 0 on success or < 0 on failure.
279  */
280 int tee_shm_register(struct udevice *dev, void *addr, ulong size, u32 flags,
281                      struct tee_shm **shmp);
282
283 /**
284  * tee_shm_free() - Frees shared memory
285  * @shm:        Shared memory object
286  */
287 void tee_shm_free(struct tee_shm *shm);
288
289 /**
290  * tee_shm_is_registered() - Check register status of shared memory object
291  * @shm:        Pointer to shared memory object
292  * @dev:        The TEE device
293  *
294  * Returns true if the shared memory object is registered for the supplied
295  * TEE device
296  */
297 bool tee_shm_is_registered(struct tee_shm *shm, struct udevice *dev);
298
299 /**
300  * tee_find_device() - Look up a TEE device
301  * @start:      if not NULL, continue search after this device
302  * @match:      function to check TEE device, returns != 0 if the device
303  *              matches
304  * @data:       data for match function
305  * @vers:       if not NULL, version data of TEE device of the device returned
306  *
307  * Returns a probed TEE device of the first TEE device matched by the
308  * match() callback or NULL.
309  */
310 struct udevice *tee_find_device(struct udevice *start,
311                                 int (*match)(struct tee_version_data *vers,
312                                              const void *data),
313                                 const void *data,
314                                 struct tee_version_data *vers);
315
316 /**
317  * tee_get_version() - Query capabilities of TEE device
318  * @dev:        The TEE device
319  * @vers:       Pointer to version data
320  */
321 void tee_get_version(struct udevice *dev, struct tee_version_data *vers);
322
323 /**
324  * tee_open_session() - Open a session to a Trusted Application
325  * @dev:        The TEE device
326  * @arg:        Open session arguments
327  * @num_param:  Number of elements in @param
328  * @param:      Parameters for Trusted Application
329  *
330  * Returns < 0 on error else see @arg->ret for result. If @arg->ret is
331  * TEE_SUCCESS the session identifier is available in @arg->session.
332  */
333 int tee_open_session(struct udevice *dev, struct tee_open_session_arg *arg,
334                      uint num_param, struct tee_param *param);
335
336 /**
337  * tee_close_session() - Close a session to a Trusted Application
338  * @dev:        The TEE device
339  * @session:    Session id
340  *
341  * Return < 0 on error else 0, regardless the session will not be valid
342  * after this function has returned.
343  */
344 int tee_close_session(struct udevice *dev, u32 session);
345
346 /**
347  * tee_invoke_func() - Invoke a function in a Trusted Application
348  * @dev:        The TEE device
349  * @arg:        Invoke arguments
350  * @num_param:  Number of elements in @param
351  * @param:      Parameters for Trusted Application
352  *
353  * Returns < 0 on error else see @arg->ret for result.
354  */
355 int tee_invoke_func(struct udevice *dev, struct tee_invoke_arg *arg,
356                     uint num_param, struct tee_param *param);
357
358 /**
359  * tee_optee_ta_uuid_from_octets() - Converts to struct tee_optee_ta_uuid
360  * @d:  Destination struct
361  * @s:  Source UUID octets
362  *
363  * Conversion to a struct tee_optee_ta_uuid represantion from binary octet
364  * representation.
365  */
366 void tee_optee_ta_uuid_from_octets(struct tee_optee_ta_uuid *d,
367                                    const u8 s[TEE_UUID_LEN]);
368
369 /**
370  * tee_optee_ta_uuid_to_octets() - Converts from struct tee_optee_ta_uuid
371  * @d:  Destination UUID octets
372  * @s:  Source struct
373  *
374  * Conversion from a struct tee_optee_ta_uuid represantion to binary octet
375  * representation.
376  */
377 void tee_optee_ta_uuid_to_octets(u8 d[TEE_UUID_LEN],
378                                  const struct tee_optee_ta_uuid *s);
379
380 #endif /* __TEE_H */