6727db4713d22e14a52f461237de46391fb22804
[firefly-linux-kernel-4.4.55.git] / security / optee_linuxdriver / include / arm_common / teesmc.h
1 /*
2  * Copyright (c) 2014, Linaro Limited
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License Version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  */
13 #ifndef TEESMC_H
14 #define TEESMC_H
15
16 #ifndef ASM
17 /*
18  * This section depends on uint64_t, uint32_t uint8_t already being
19  * defined. Since this file is used in several different environments
20  * (secure world OS and normal world Linux kernel to start with) where
21  * stdint.h may not be available it's the responsibility of the one
22  * including this file to provide those types.
23  */
24
25 /*
26  * Trusted OS SMC interface.
27  *
28  * The SMC interface follows SMC Calling Convention
29  * (ARM_DEN0028A_SMC_Calling_Convention).
30  *
31  * The primary objective of this API is to provide a transport layer on
32  * which a Global Platform compliant TEE interfaces can be deployed. But the
33  * interface can also be used for other implementations.
34  *
35  * This file is divided in two parts.
36  * Part 1 deals with passing parameters to Trusted Applications running in
37  * a trusted OS in secure world.
38  * Part 2 deals with the lower level handling of the SMC.
39  */
40
41 /*
42  *******************************************************************************
43  * Part 1 - passing parameters to Trusted Applications
44  *******************************************************************************
45  */
46
47 /*
48  * Same values as TEE_PARAM_* from TEE Internal API
49  */
50 #define TEESMC_ATTR_TYPE_NONE           0
51 #define TEESMC_ATTR_TYPE_VALUE_INPUT    1
52 #define TEESMC_ATTR_TYPE_VALUE_OUTPUT   2
53 #define TEESMC_ATTR_TYPE_VALUE_INOUT    3
54 #define TEESMC_ATTR_TYPE_MEMREF_INPUT   5
55 #define TEESMC_ATTR_TYPE_MEMREF_OUTPUT  6
56 #define TEESMC_ATTR_TYPE_MEMREF_INOUT   7
57
58 #define TEESMC_ATTR_TYPE_MASK           0x7
59
60 /*
61  * Meta parameter to be absorbed by the Secure OS and not passed
62  * to the Trusted Application.
63  *
64  * One example of this is a struct teesmc_meta_open_session which
65  * is added to TEESMC{32,64}_CMD_OPEN_SESSION.
66  */
67 #define TEESMC_ATTR_META                0x8
68
69 /*
70  * Used as an indication from normal world of compatible cache usage.
71  * 'I' stands for inner cache and 'O' for outer cache.
72  */
73 #define TEESMC_ATTR_CACHE_I_NONCACHE    0x0
74 #define TEESMC_ATTR_CACHE_I_WRITE_THR   0x1
75 #define TEESMC_ATTR_CACHE_I_WRITE_BACK  0x2
76 #define TEESMC_ATTR_CACHE_O_NONCACHE    0x0
77 #define TEESMC_ATTR_CACHE_O_WRITE_THR   0x4
78 #define TEESMC_ATTR_CACHE_O_WRITE_BACK  0x8
79
80 #define TEESMC_ATTR_CACHE_NONCACHE      (TEESMC_ATTR_CACHE_I_NONCACHE | \
81                                          TEESMC_ATTR_CACHE_O_NONCACHE)
82 #define TEESMC_ATTR_CACHE_DEFAULT       (TEESMC_ATTR_CACHE_I_WRITE_BACK | \
83                                          TEESMC_ATTR_CACHE_O_WRITE_BACK)
84
85 #define TEESMC_ATTR_CACHE_SHIFT         4
86 #define TEESMC_ATTR_CACHE_MASK          0xf
87
88 #define TEESMC_CMD_OPEN_SESSION         0
89 #define TEESMC_CMD_INVOKE_COMMAND       1
90 #define TEESMC_CMD_CLOSE_SESSION        2
91 #define TEESMC_CMD_CANCEL               3
92
93 /**
94  * struct teesmc32_param_memref - memory reference
95  * @buf_ptr: Address of the buffer
96  * @size: Size of the buffer
97  *
98  * Secure and normal world communicates pointer via physical address instead of
99  * the virtual address with is usually used for pointers. This is because
100  * Secure and normal world has completely independant memory mapping. Normal
101  * world can even have a hypervisor which need to translate the guest
102  * physical address (AKA IPA in ARM lingo) to a real physical address
103  * before passing the structure to secure world.
104  */
105 struct teesmc32_param_memref {
106         uint32_t buf_ptr;
107         uint32_t size;
108 };
109
110 /**
111  * struct teesmc32_param_memref - memory reference
112  * @buf_ptr: Address of the buffer
113  * @size: Size of the buffer
114  *
115  * See description of struct teesmc32_param_memref.
116  */
117 struct teesmc64_param_memref {
118         uint64_t buf_ptr;
119         uint64_t size;
120 };
121
122 /**
123  * struct teesmc32_param_value - values
124  * @a: first value
125  * @b: second value
126  */
127 struct teesmc32_param_value {
128         uint32_t a;
129         uint32_t b;
130 };
131
132 /**
133  * struct teesmc64_param_value - values
134  * @a: first value
135  * @b: second value
136  */
137 struct teesmc64_param_value {
138         uint64_t a;
139         uint64_t b;
140 };
141
142 /**
143  * struct teesmc32_param - parameter
144  * @attr: attributes
145  * @memref: a memory reference
146  * @value: a value
147  *
148  * attr & TEESMC_ATTR_TYPE_MASK indicates if memref or value is used in the
149  * union. TEESMC_ATTR_TYPE_VALUE_* indicates value and
150  * TEESMC_ATTR_TYPE_MEMREF_* indicates memref. TEESMC_ATTR_TYPE_NONE
151  * indicates that none of the members are used.
152  */
153 struct teesmc32_param {
154         uint32_t attr;
155         union {
156                 struct teesmc32_param_memref memref;
157                 struct teesmc32_param_value value;
158         } u;
159 };
160
161 /**
162  * struct teesmc64_param - parameter
163  * @attr: attributes
164  * @memref: a memory reference
165  * @value: a value
166  *
167  * See description of union teesmc32_param.
168  */
169 struct teesmc64_param {
170         uint64_t attr;
171         union {
172                 struct teesmc64_param_memref memref;
173                 struct teesmc64_param_value value;
174         } u;
175 };
176
177 /**
178  * struct teesmc32_arg - SMC argument for Trusted OS
179  * @cmd: Command, one of TEESMC_CMD_*
180  * @ta_func: Trusted Application function, specific to the Trusted Application,
181  *           used if cmd == TEESMC_CMD_INVOKE_COMMAND
182  * @session: In parameter for all TEESMC_CMD_* except
183  *           TEESMC_CMD_OPEN_SESSION where it's an output paramter instead
184  * @ret: return value
185  * @ret_origin: origin of the return value
186  * @num_params: number of parameters supplied to the OS Command
187  * @params: the parameters supplied to the OS Command
188  *
189  * All normal SMC calls to Trusted OS uses this struct. If cmd requires
190  * further information than what these field holds it can be passed as a
191  * parameter tagged as meta (setting the TEESMC_ATTR_META bit in
192  * corresponding param_attrs). This is used for TEESMC_CMD_OPEN_SESSION
193  * to pass a struct teesmc32_meta_open_session which is needed find the
194  * Trusted Application and to indicate the credentials of the client.
195  */
196 struct teesmc32_arg {
197         uint32_t cmd;
198         uint32_t ta_func;
199         uint32_t session;
200         uint32_t ret;
201         uint32_t ret_origin;
202         uint32_t num_params;
203         /*
204          * Commented out elements used to visualize the layout dynamic part
205          * of the struct. Note that these fields are not available at all
206          * if num_params == 0.
207          *
208          * params is accessed through the macro TEESMC32_GET_PARAMS
209          */
210
211         /* struct teesmc32_param params[num_params]; */
212 };
213
214 /**
215  * TEESMC32_GET_PARAMS - return pointer to union teesmc32_param *
216  *
217  * @x: Pointer to a struct teesmc32_arg
218  *
219  * Returns a pointer to the params[] inside a struct teesmc32_arg.
220  */
221 #define TEESMC32_GET_PARAMS(x) \
222         (struct teesmc32_param *)(((struct teesmc32_arg *)(x)) + 1)
223
224 /**
225  * TEESMC32_GET_ARG_SIZE - return size of struct teesmc32_arg
226  *
227  * @num_params: Number of parameters embedded in the struct teesmc32_arg
228  *
229  * Returns the size of the struct teesmc32_arg together with the number
230  * of embedded paramters.
231  */
232 #define TEESMC32_GET_ARG_SIZE(num_params) \
233         (sizeof(struct teesmc32_arg) + \
234          sizeof(struct teesmc32_param) * (num_params))
235
236 /**
237  * struct teesmc64_arg - SMC argument for Trusted OS
238  * @cmd: OS Command, one of TEESMC_CMD_*
239  * @ta_func: Trusted Application function, specific to the Trusted Application
240  * @session: In parameter for all TEESMC_CMD_* but
241  *           TEESMC_CMD_OPEN_SESSION
242  * @ret: return value
243  * @ret_origin: origin of the return value
244  * @num_params: number of parameters supplied to the OS Command
245  * @params: the parameters supplied to the OS Command
246  *
247  * See description of struct teesmc32_arg.
248  */
249 struct teesmc64_arg {
250         uint64_t cmd;
251         uint64_t ta_func;
252         uint64_t session;
253         uint64_t ret;
254         uint64_t ret_origin;
255         uint64_t num_params;
256         /*
257          * Commented out elements used to visualize the layout dynamic part
258          * of the struct. Note that these fields are not available at all
259          * if num_params == 0.
260          *
261          * params is accessed through the macro TEESMC64_GET_PARAMS
262          */
263
264         /* struct teesmc64_param params[num_params]; */
265 };
266
267 /**
268  * TEESMC64_GET_PARAMS - return pointer to union teesmc64_param *
269  *
270  * @x: Pointer to a struct teesmc64_arg
271  *
272  * Returns a pointer to the params[] inside a struct teesmc64_arg.
273  */
274 #define TEESMC64_GET_PARAMS(x) \
275         (struct teesmc64_param *)(((struct teesmc64_arg *)(x)) + 1)
276
277 /**
278  * TEESMC64_GET_ARG_SIZE - return size of struct teesmc64_arg
279  *
280  * @num_params: Number of parameters embedded in the struct teesmc64_arg
281  *
282  * Returns the size of the struct teesmc64_arg together with the number
283  * of embedded paramters.
284  */
285 #define TEESMC64_GET_ARG_SIZE(num_params) \
286         (sizeof(struct teesmc64_arg) + \
287          sizeof(union teesmc64_param) * (num_params))
288
289 #define TEESMC_UUID_LEN 16
290
291 /**
292  * struct teesmc_meta_open_session - additional parameters for
293  *                                   TEESMC32_CMD_OPEN_SESSION and
294  *                                   TEESMC64_CMD_OPEN_SESSION
295  * @uuid: UUID of the Trusted Application
296  * @clnt_uuid: UUID of client
297  * @clnt_login: Login class of client, TEE_LOGIN_* if being Global Platform
298  *              compliant
299  *
300  * This struct is passed in the first parameter as an input memref tagged
301  * as meta on an TEESMC{32,64}_CMD_OPEN_SESSION cmd. It's important
302  * that it really is the first parameter to make it easy for an eventual
303  * hypervisor to inspect and possibly update clnt_* values.
304  */
305 struct teesmc_meta_open_session {
306         uint8_t uuid[TEESMC_UUID_LEN];
307         uint8_t clnt_uuid[TEESMC_UUID_LEN];
308         uint32_t clnt_login;
309 };
310
311
312 #endif /*!ASM*/
313
314 /*
315  *******************************************************************************
316  * Part 2 - low level SMC interaction
317  *******************************************************************************
318  */
319
320 #define TEESMC_32                       0
321 #define TEESMC_64                       0x40000000
322 #define TEESMC_FAST_CALL                0x80000000
323 #define TEESMC_STD_CALL                 0
324
325 #define TEESMC_OWNER_MASK               0x3F
326 #define TEESMC_OWNER_SHIFT              24
327
328 #define TEESMC_FUNC_MASK                0xFFFF
329
330 #define TEESMC_IS_FAST_CALL(smc_val)    ((smc_val) & TEESMC_FAST_CALL)
331 #define TEESMC_IS_64(smc_val)           ((smc_val) & TEESMC_64)
332 #define TEESMC_FUNC_NUM(smc_val)        ((smc_val) & TEESMC_FUNC_MASK)
333 #define TEESMC_OWNER_NUM(smc_val)       (((smc_val) >> TEESMC_OWNER_SHIFT) & \
334                                          TEESMC_OWNER_MASK)
335
336 #define TEESMC_CALL_VAL(type, calling_convention, owner, func_num) \
337                         ((type) | (calling_convention) | \
338                         (((owner) & TEESMC_OWNER_MASK) << TEESMC_OWNER_SHIFT) |\
339                         ((func_num) & TEESMC_FUNC_MASK))
340
341 #define TEESMC_OWNER_ARCH               0
342 #define TEESMC_OWNER_CPU                1
343 #define TEESMC_OWNER_SIP                2
344 #define TEESMC_OWNER_OEM                3
345 #define TEESMC_OWNER_STANDARD           4
346 #define TEESMC_OWNER_TRUSTED_APP        48
347 #define TEESMC_OWNER_TRUSTED_OS         50
348
349 #define TEESMC_OWNER_TRUSTED_OS_API     63
350
351 /*
352  * Function specified by SMC Calling convention.
353  */
354 #define TEESMC32_FUNCID_CALLS_COUNT     0xFF00
355 #define TEESMC32_CALLS_COUNT \
356         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, \
357                         TEESMC_OWNER_TRUSTED_OS_API, \
358                         TEESMC32_FUNCID_CALLS_COUNT)
359
360 /*
361  * Function specified by SMC Calling convention
362  *
363  * Return one of the following UIDs if using API specified in this file
364  * without further extentions:
365  * 65cb6b93-af0c-4617-8ed6-644a8d1140f8 : Only 32 bit calls are supported
366  * 65cb6b93-af0c-4617-8ed6-644a8d1140f9 : Both 32 and 64 bit calls are supported
367  */
368 #define TEESMC_UID_R0                   0x65cb6b93
369 #define TEESMC_UID_R1                   0xaf0c4617
370 #define TEESMC_UID_R2                   0x8ed6644a
371 #define TEESMC_UID32_R3                 0x8d1140f8
372 #define TEESMC_UID64_R3                 0x8d1140f9
373 #define TEESMC32_FUNCID_CALLS_UID       0xFF01
374 #define TEESMC32_CALLS_UID \
375         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, \
376                         TEESMC_OWNER_TRUSTED_OS_API, \
377                         TEESMC32_FUNCID_CALLS_UID)
378
379 /*
380  * Function specified by SMC Calling convention
381  *
382  * Returns 1.0 if using API specified in this file without further extentions.
383  */
384 #define TEESMC_REVISION_MAJOR   1
385 #define TEESMC_REVISION_MINOR   0
386 #define TEESMC32_FUNCID_CALLS_REVISION  0xFF03
387 #define TEESMC32_CALLS_REVISION \
388         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, \
389                         TEESMC_OWNER_TRUSTED_OS_API, \
390                         TEESMC32_FUNCID_CALLS_REVISION)
391
392 /*
393  * Get UUID of Trusted OS.
394  *
395  * Used by non-secure world to figure out which Trusted OS is installed.
396  * Note that returned UUID is the UUID of the Trusted OS, not of the API.
397  *
398  * Returns UUID in r0-4/w0-4 in the same way as TEESMC32_CALLS_UID
399  * described above.
400  */
401 #define TEESMC_FUNCID_GET_OS_UUID       0
402 #define TEESMC32_CALL_GET_OS_UUID \
403         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
404                         TEESMC_FUNCID_GET_OS_UUID)
405
406 /*
407  * Get revision of Trusted OS.
408  *
409  * Used by non-secure world to figure out which version of the Trusted OS
410  * is installed. Note that the returned revision is the revision of the
411  * Trusted OS, not of the API.
412  *
413  * Returns revision in r0-1/w0-1 in the same way as TEESMC32_CALLS_REVISION
414  * described above.
415  */
416 #define TEESMC_FUNCID_GET_OS_REVISION   1
417 #define TEESMC32_CALL_GET_OS_REVISION \
418         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
419                         TEESMC_FUNCID_GET_OS_REVISION)
420
421
422
423 /*
424  * Call with struct teesmc32_arg as argument
425  *
426  * Call register usage:
427  * r0/x0        SMC Function ID, TEESMC32_CALL_WITH_ARG
428  * r1/x1        Physical pointer to a struct teesmc32_arg
429  * r2-6/x2-6    Not used
430  * r7/x7        Hypervisor Client ID register
431  *
432  * Normal return register usage:
433  * r0/x0        Return value, TEESMC_RETURN_*
434  * r1-3/x1-3    Not used
435  * r4-7/x4-7    Preserved
436  *
437  * Ebusy return register usage:
438  * r0/x0        Return value, TEESMC_RETURN_EBUSY
439  * r1-3/x1-3    Preserved
440  * r4-7/x4-7    Preserved
441  *
442  * RPC return register usage:
443  * r0/x0        Return value, TEESMC_RETURN_IS_RPC(val)
444  * r1-2/x1-2    RPC parameters
445  * r3-7/x3-7    Resume information, must be preserved
446  *
447  * Possible return values:
448  * TEESMC_RETURN_UNKNOWN_FUNCTION       Trusted OS does not recognize this
449  *                                      function.
450  * TEESMC_RETURN_OK                     Call completed, result updated in
451  *                                      the previously supplied struct
452  *                                      teesmc32_arg.
453  * TEESMC_RETURN_EBUSY                  Trusted OS busy, try again later.
454  * TEESMC_RETURN_EBADADDR               Bad physcial pointer to struct
455  *                                      teesmc32_arg.
456  * TEESMC_RETURN_EBADCMD                Bad/unknown cmd in struct teesmc32_arg
457  * TEESMC_RETURN_IS_RPC()               Call suspended by RPC call to normal
458  *                                      world.
459  */
460 #define TEESMC_FUNCID_CALL_WITH_ARG     2
461 #define TEESMC32_CALL_WITH_ARG \
462         TEESMC_CALL_VAL(TEESMC_32, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
463         TEESMC_FUNCID_CALL_WITH_ARG)
464 /* Same as TEESMC32_CALL_WITH_ARG but a "fast call". */
465 #define TEESMC32_FASTCALL_WITH_ARG \
466         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
467         TEESMC_FUNCID_CALL_WITH_ARG)
468
469 /*
470  * Call with struct teesmc64_arg as argument
471  *
472  * See description of TEESMC32_CALL_WITH_ARG above, uses struct
473  * teesmc64_arg in x1 instead.
474  */
475 #define TEESMC64_CALL_WITH_ARG \
476         TEESMC_CALL_VAL(TEESMC_64, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
477         TEESMC_FUNCID_CALL_WITH_ARG)
478 /* Same as TEESMC64_CALL_WITH_ARG but a "fast call". */
479 #define TEESMC64_FASTCALL_WITH_ARG \
480         TEESMC_CALL_VAL(TEESMC_64, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
481         TEESMC_FUNCID_CALL_WITH_ARG)
482
483 /*
484  * Resume from RPC (for example after processing an IRQ)
485  *
486  * Call register usage:
487  * r0/x0        SMC Function ID,
488  *              TEESMC32_CALL_RETURN_FROM_RPC or
489  *              TEESMC32_FASTCALL_RETURN_FROM_RPC
490  * r1-3/x1-3    Value of r1-3/x1-3 when TEESMC32_CALL_WITH_ARG returned
491  *              TEESMC_RETURN_RPC in r0/x0
492  *
493  * Return register usage is the same as for TEESMC32_CALL_WITH_ARG above.
494  *
495  * Possible return values
496  * TEESMC_RETURN_UNKNOWN_FUNCTION       Trusted OS does not recognize this
497  *                                      function.
498  * TEESMC_RETURN_OK                     Original call completed, result
499  *                                      updated in the previously supplied.
500  *                                      struct teesmc32_arg
501  * TEESMC_RETURN_RPC                    Call suspended by RPC call to normal
502  *                                      world.
503  * TEESMC_RETURN_EBUSY                  Trusted OS busy, try again later.
504  * TEESMC_RETURN_ERESUME                Resume failed, the opaque resume
505  *                                      information was corrupt.
506  */
507 #define TEESMC_FUNCID_RETURN_FROM_RPC   3
508 #define TEESMC32_CALL_RETURN_FROM_RPC \
509         TEESMC_CALL_VAL(TEESMC_32, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
510                         TEESMC_FUNCID_RETURN_FROM_RPC)
511 /* Same as TEESMC32_CALL_RETURN_FROM_RPC but a "fast call". */
512 #define TEESMC32_FASTCALL_RETURN_FROM_RPC \
513         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
514                         TEESMC_FUNCID_RETURN_FROM_RPC)
515
516 /*
517  * Resume from RPC (for example after processing an IRQ)
518  *
519  * See description of TEESMC32_CALL_RETURN_FROM_RPC above, used when
520  * it's a 64bit call that has returned.
521  */
522 #define TEESMC64_CALL_RETURN_FROM_RPC \
523         TEESMC_CALL_VAL(TEESMC_64, TEESMC_STD_CALL, TEESMC_OWNER_TRUSTED_OS, \
524                         TEESMC_FUNCID_RETURN_FROM_RPC)
525 /* Same as TEESMC64_CALL_RETURN_FROM_RPC but a "fast call". */
526 #define TEESMC64_FASTCALL_RETURN_FROM_RPC \
527         TEESMC_CALL_VAL(TEESMC_64, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
528                         TEESMC_FUNCID_RETURN_FROM_RPC)
529
530 /*
531  * From secure monitor to Trusted OS, handle FIQ
532  *
533  * A virtual call which is injected by the Secure Monitor when an FIQ is
534  * raised while in normal world (SCR_NS is set). The monitor restores
535  * secure architecture registers and secure EL_SP1 and jumps to previous
536  * secure EL3_ELR. Trusted OS should preserve all general purpose
537  * registers.
538  *
539  * Call register usage:
540  * r0/x0        SMC Function ID, TEESMC32_CALL_HANDLE_FIQ
541  * r1-7/x1-7    Not used, but must be preserved
542  *
543  * Return register usage:
544  * Note used
545  */
546 #define TEESMC_FUNCID_CALL_HANDLE_FIQ   0xf000
547 #define TEESMC32_CALL_HANDLE_FIQ \
548         TEESMC_CALL_VAL(TEESMC_32, TEESMC_FAST_CALL, TEESMC_OWNER_TRUSTED_OS, \
549                         TEESMC_FUNCID_CALL_HANDLE_FIQ)
550
551 #define TEESMC_RETURN_RPC_PREFIX_MASK   0xFFFF0000
552 #define TEESMC_RETURN_RPC_PREFIX        0xFFFF0000
553 #define TEESMC_RETURN_RPC_FUNC_MASK     0x0000FFFF
554
555 #define TEESMC_RETURN_GET_RPC_FUNC(ret) ((ret) & TEESMC_RETURN_RPC_FUNC_MASK)
556
557 #define TEESMC_RPC_VAL(func)            ((func) | TEESMC_RETURN_RPC_PREFIX)
558
559 /*
560  * Allocate argument memory for RPC parameter passing.
561  * Argument memory is used to hold a struct teesmc32_arg.
562  *
563  * "Call" register usage:
564  * r0/x0        This value, TEESMC_RETURN_RPC_ALLOC
565  * r1/x1        Size in bytes of required argument memory
566  * r2-7/x2-7    Resume information, must be preserved
567  *
568  * "Return" register usage:
569  * r0/x0        SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
570  *              AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
571  *              AArch64 SMC return
572  * r1/x1        Physical pointer to allocated argument memory, 0 if size
573  *              was 0 or if memory can't be allocated
574  * r2-7/x2-7    Preserved
575  */
576 #define TEESMC_RPC_FUNC_ALLOC_ARG       0
577 #define TEESMC_RETURN_RPC_ALLOC_ARG     \
578         TEESMC_RPC_VAL(TEESMC_RPC_FUNC_ALLOC_ARG)
579
580 /*
581  * Allocate payload memory for RPC parameter passing.
582  * Payload memory is used to hold the memory referred to by struct
583  * teesmc32_param_memref.
584  *
585  * "Call" register usage:
586  * r0/x0        This value, TEESMC_RETURN_RPC_ALLOC
587  * r1/x1        Size in bytes of required payload memory
588  * r2-7/x2-7    Resume information, must be preserved
589  *
590  * "Return" register usage:
591  * r0/x0        SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
592  *              AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
593  *              AArch64 SMC return
594  * r1/x1        Physical pointer to allocated payload memory, 0 if size
595  *              was 0 or if memory can't be allocated
596  * r2-7/x2-7    Preserved
597  */
598 #define TEESMC_RPC_FUNC_ALLOC_PAYLOAD   1
599 #define TEESMC_RETURN_RPC_ALLOC_PAYLOAD \
600         TEESMC_RPC_VAL(TEESMC_RPC_FUNC_ALLOC_PAYLOAD)
601
602 /*
603  * Free memory previously allocated by TEESMC_RETURN_RPC_ALLOC_ARG.
604  *
605  * "Call" register usage:
606  * r0/x0        This value, TEESMC_RETURN_RPC_FREE
607  * r1/x1        Physical pointer to previously allocated argument memory
608  * r2-7/x2-7    Resume information, must be preserved
609  *
610  * "Return" register usage:
611  * r0/x0        SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
612  *              AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
613  *              AArch64 SMC return
614  * r1/x1        Not used
615  * r2-7/x2-7    Preserved
616  */
617 #define TEESMC_RPC_FUNC_FREE_ARG        2
618 #define TEESMC_RETURN_RPC_FREE_ARG      TEESMC_RPC_VAL(TEESMC_RPC_FUNC_FREE_ARG)
619
620 /*
621  * Free memory previously allocated by TEESMC_RETURN_RPC_ALLOC_PAYLOAD.
622  *
623  * "Call" register usage:
624  * r0/x0        This value, TEESMC_RETURN_RPC_FREE
625  * r1/x1        Physical pointer to previously allocated payload memory
626  * r3-7/x3-7    Resume information, must be preserved
627  *
628  * "Return" register usage:
629  * r0/x0        SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
630  *              AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
631  *              AArch64 SMC return
632  * r1-2/x1-2    Not used
633  * r3-7/x3-7    Preserved
634  */
635 #define TEESMC_RPC_FUNC_FREE_PAYLOAD    3
636 #define TEESMC_RETURN_RPC_FREE_PAYLOAD  \
637         TEESMC_RPC_VAL(TEESMC_RPC_FUNC_FREE_PAYLOAD)
638
639 /*
640  * Deliver an IRQ in normal world.
641  *
642  * "Call" register usage:
643  * r0/x0        TEESMC_RETURN_RPC_IRQ
644  * r1-7/x1-7    Resume information, must be preserved
645  *
646  * "Return" register usage:
647  * r0/x0        SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
648  *              AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
649  *              AArch64 SMC return
650  * r1-7/x1-7    Preserved
651  */
652 #define TEESMC_RPC_FUNC_IRQ             4
653 #define TEESMC_RETURN_RPC_IRQ           TEESMC_RPC_VAL(TEESMC_RPC_FUNC_IRQ)
654
655 /*
656  * Do an RPC request. The supplied struct teesmc{32,64}_arg tells which
657  * request to do and the paramters for the request. The following fields
658  * are used (the rest are unused):
659  * - cmd                the Request ID
660  * - ret                return value of the request, filled in by normal world
661  * - num_params         number of parameters for the request
662  * - params             the parameters
663  * - param_attrs        attributes of the parameters
664  *
665  * "Call" register usage:
666  * r0/x0        TEESMC_RETURN_RPC_CMD
667  * r1/x1        Physical pointer to a struct teesmc32_arg if returning from
668  *              a AArch32 SMC or a struct teesmc64_arg if returning from a
669  *              AArch64 SMC, must be preserved, only the data should
670  *              be updated
671  * r2-7/x2-7    Resume information, must be preserved
672  *
673  * "Return" register usage:
674  * r0/x0        SMC Function ID, TEESMC32_CALL_RETURN_FROM_RPC if it was an
675  *              AArch32 SMC return or TEESMC64_CALL_RETURN_FROM_RPC for
676  *              AArch64 SMC return
677  * r1-7/x1-7    Preserved
678  */
679 #define TEESMC_RPC_FUNC_CMD             5
680 #define TEESMC_RETURN_RPC_CMD           TEESMC_RPC_VAL(TEESMC_RPC_FUNC_CMD)
681
682
683 /* Returned in r0 */
684 #define TEESMC_RETURN_UNKNOWN_FUNCTION  0xFFFFFFFF
685
686 /* Returned in r0 only from Trusted OS functions */
687 #define TEESMC_RETURN_OK                0x0
688 #define TEESMC_RETURN_EBUSY             0x1
689 #define TEESMC_RETURN_ERESUME           0x2
690 #define TEESMC_RETURN_EBADADDR          0x3
691 #define TEESMC_RETURN_EBADCMD           0x4
692 #define TEESMC_RETURN_IS_RPC(ret) \
693         (((ret) & TEESMC_RETURN_RPC_PREFIX_MASK) == TEESMC_RETURN_RPC_PREFIX)
694
695 /*
696  * Returned in r1 by Trusted OS functions if r0 = TEESMC_RETURN_RPC
697  */
698 #define TEESMC_RPC_REQUEST_IRQ          0x0
699
700 #endif /* TEESMC_H */