1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Decription: declarations for ffa functions and useful macros
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  */
14 #ifndef FFA_ABI_H
15 #define FFA_ABI_H
16 
17 #include <linux/arm_ffa.h>
18 #include "smc_smp.h"
19 #include "smc_call.h"
20 /*
21  * Normal world sends requests with FFA_MSG_SEND_DIRECT_REQ and
22  * responses are returned with FFA_MSG_SEND_DIRECT_RESP for normal
23  * messages.
24  *
25  * All requests with FFA_MSG_SEND_DIRECT_REQ and FFA_MSG_SEND_DIRECT_RESP
26  * are using the AArch32 SMC calling convention with register usage as
27  * defined in FF-A specification:
28  * w0:	Function ID (0x8400006F or 0x84000070)
29  * w1:	Source/Destination IDs
30  * w2:	Reserved (MBZ)
31  * w3-w7: Implementation defined, free to be used below
32  */
33 
34 #define TZ_FFA_VERSION_MAJOR 1
35 #define TZ_FFA_VERSION_MINOR 0
36 
37 #define TZ_FFA_BLOCKING_CALL(id)	(id)
38 #define TZ_FFA_YIELDING_CALL_BIT	31
39 #define TZ_FFA_YIELDING_CALL(id)	((id) | BIT(TZ_FFA_YIELDING_CALL_BIT))
40 
41 /*
42  * Returns the API version implemented, currently follows the FF-A version.
43  * Call register usage:
44  * w3:	Service ID, TZ_FFA_GET_API_VERSION
45  * w4-w7: Not used (MBZ)
46  *
47  * Return register usage:
48  * w3:	TZ_FFA_VERSION_MAJOR
49  * w4:	TZ_FFA_VERSION_MINOR
50  * w5-w7: Not used (MBZ)
51  */
52 #define TZ_FFA_GET_API_VERSION TZ_FFA_BLOCKING_CALL(0)
53 
54 /*
55  * Returns the revision of iTrustee
56  *
57  * Used by non-secure world to figure out which version of the Trusted OS
58  * is installed. Note that the returned revision is the revision of the
59  * Trusted OS, not of the API.
60  *
61  * Call register usage:
62  * w3:	Service ID, TZ_FFA_GET_OS_VERSION
63  * w4-w7: Unused (MBZ)
64  *
65  * Return register usage:
66  * w3:	CFG_TZ_REVISION_MAJOR
67  * w4:	CFG_TZ_REVISION_MINOR
68  * w5:	TEE_IMPL_GIT_SHA1 (or zero if not supported)
69  */
70 #define TZ_FFA_GET_OS_VERSION	TZ_FFA_BLOCKING_CALL(1)
71 
72 /*
73  * Exchange capabilities between normal world and secure world.
74  *
75  * Currently, there are no defined capabilities. When features are added new
76  * capabilities may be added.
77  *
78  * Call register usage:
79  * w3:	Service ID, TZ_FFA_EXCHANGE_CAPABILITIES
80  * w4-w7: Not used (MBZ)
81  *
82  * Return register usage:
83  * w3:	Error code, 0 on success
84  * w4:	Bit[7:0]:  Number of parameters needed for RPC to be supplied
85  *				   as the second MSG arg struct for
86  *				   TZ_FFA_YIELDING_CALL_WITH_ARG.
87  *		Bit[31:8]: Reserved (MBZ)
88  * w5-w7: Not used (MBZ)
89  */
90 #define TZ_FFA_EXCHANGE_CAPABILITIES TZ_FFA_BLOCKING_CALL(2)
91 
92 /*
93  * Unregister shared memory
94  *
95  * Call register usage:
96  * w3:	Service ID, TZ_FFA_YIELDING_CALL_UNREGISTER_SHM
97  * w4:	Shared memory handle, lower bits
98  * w5:	Shared memory handle, higher bits
99  * w6-w7: Not used (MBZ)
100  *
101  * Return register usage:
102  * w3:	Error code, 0 on success
103  * w4-w7: Not used (MBZ)
104  */
105 #define TZ_FFA_UNREGISTER_SHM	TZ_FFA_BLOCKING_CALL(3)
106 
107 /*
108  * Call with struct TZ_msg_arg as argument in the supplied shared memory
109  * with a zero internal offset and normal cached memory attributes
110  * Register usage:
111  * w3:	Service ID, TZ_FFA_YIELDING_CALL_WITH_ARG
112  * w4:	Lower 32 bits of a 64-bit Shared memory handle
113  * w5:	Upper 32 bits of a 64-bit Shared memory handle
114  * w6:	Offset into shared memory pointing to a struct TZ_msg_arg
115  *		right after the parameters of this struct (at offset
116  *		TZ_MSG_GET_ARG_SIZE(num_params) follows a struct TZ_msg_arg
117  *		for RPC, this struct has reserved space for the number of RPC
118  *		parameters as returned by TZ_FFA_EXCHANGE_CAPABILITIES.
119  * w7:	Not used (MBZ)
120  * Resume from RPC. Register usage:
121  * w3:	Service ID, TZ_FFA_YIELDING_CALL_RESUME
122  * w4-w6: Not used (MBZ)
123  * w7:	Resume info
124  *
125  * Normal return (yielding call is completed). Register usage:
126  * w3:	Error code, 0 on success
127  * w4:	TZ_FFA_YIELDING_CALL_RETURN_DONE
128  * w5-w7: Not used (MBZ)
129  *
130  * RPC interrupt return (RPC from secure world). Register usage:
131  * w3:	Error code == 0
132  * w4:	Any defined RPC code but TZ_FFA_YIELDING_CALL_RETURN_DONE
133  * w5-w6: Not used (MBZ)
134  * w7:	Resume info
135  *
136  * Possible error codes in register w3:
137  * 0:					  Success
138  * FFA_DENIED:			  w4 isn't one of TZ_FFA_YIELDING_CALL_START
139  *						  TZ_FFA_YIELDING_CALL_RESUME
140  *
141  * Possible error codes for TZ_FFA_YIELDING_CALL_START
142  * FFA_BUSY:			  Number of OP-TEE OS threads exceeded,
143  *						  try again later
144  * FFA_DENIED:			  RPC shared memory object not found
145  * FFA_INVALID_PARAMETER: Bad shared memory handle or offset into the memory
146  *
147  * Possible error codes for TZ_FFA_YIELDING_CALL_RESUME
148  * FFA_INVALID_PARAMETER: Bad resume info
149  */
150 #define TZ_FFA_YIELDING_CALL_WITH_ARG TZ_FFA_YIELDING_CALL(0)
151 #define TZ_FFA_YIELDING_CALL_RESUME TZ_FFA_YIELDING_CALL(1)
152 
153 #define TZ_FFA_YIELDING_CALL_RETURN_DONE 0
154 #define TZ_FFA_YIELDING_CALL_RETURN_RPC_CMD 1
155 #define TZ_FFA_YIELDING_CALL_RETURN_INTERRUPT 2
156 
157 int ffa_abi_register(void);
158 void ffa_abi_unregister(void);
159 int ffa_forward_call(struct smc_in_params *in_param, struct smc_out_params *out_param, uint8_t wait);
160 
161 #endif