1 /*
2  * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef FFA_SVC_H
8 #define FFA_SVC_H
9 
10 #include <stdbool.h>
11 
12 #include <lib/smccc.h>
13 #include <lib/utils_def.h>
14 #include <tools_share/uuid.h>
15 
16 /* FFA error codes. */
17 #define FFA_ERROR_NOT_SUPPORTED		-1
18 #define FFA_ERROR_INVALID_PARAMETER	-2
19 #define FFA_ERROR_NO_MEMORY		-3
20 #define FFA_ERROR_BUSY			-4
21 #define FFA_ERROR_INTERRUPTED		-5
22 #define FFA_ERROR_DENIED		-6
23 #define FFA_ERROR_RETRY			-7
24 
25 /* The macros below are used to identify FFA calls from the SMC function ID */
26 #define FFA_FNUM_MIN_VALUE	U(0x60)
27 #define FFA_FNUM_MAX_VALUE	U(0x87)
28 #define is_ffa_fid(fid) __extension__ ({		\
29 	__typeof__(fid) _fid = (fid);			\
30 	((GET_SMC_NUM(_fid) >= FFA_FNUM_MIN_VALUE) &&	\
31 	 (GET_SMC_NUM(_fid) <= FFA_FNUM_MAX_VALUE)); })
32 
33 /* FFA_VERSION helpers */
34 #define FFA_VERSION_MAJOR		U(1)
35 #define FFA_VERSION_MAJOR_SHIFT		16
36 #define FFA_VERSION_MAJOR_MASK		U(0x7FFF)
37 #define FFA_VERSION_MINOR		U(1)
38 #define FFA_VERSION_MINOR_SHIFT		0
39 #define FFA_VERSION_MINOR_MASK		U(0xFFFF)
40 #define FFA_VERSION_BIT31_MASK 		U(0x1u << 31)
41 
42 
43 #define MAKE_FFA_VERSION(major, minor) 	\
44 	((((major) & FFA_VERSION_MAJOR_MASK) <<  FFA_VERSION_MAJOR_SHIFT) | \
45 	 (((minor) & FFA_VERSION_MINOR_MASK) << FFA_VERSION_MINOR_SHIFT))
46 #define FFA_VERSION_COMPILED		MAKE_FFA_VERSION(FFA_VERSION_MAJOR, \
47 							  FFA_VERSION_MINOR)
48 
49 /* FFA_MSG_SEND helpers */
50 #define FFA_MSG_SEND_ATTRS_BLK_SHIFT	U(0)
51 #define FFA_MSG_SEND_ATTRS_BLK_MASK	U(0x1)
52 #define FFA_MSG_SEND_ATTRS_BLK		U(0)
53 #define FFA_MSG_SEND_ATTRS_BLK_NOT	U(1)
54 #define FFA_MSG_SEND_ATTRS(blk)		\
55 	(((blk) & FFA_MSG_SEND_ATTRS_BLK_MASK) \
56 	<< FFA_MSG_SEND_ATTRS_BLK_SHIFT)
57 
58 
59 /* FFA_RELATED helper macros */
60 #define FFA_PARTITION_ID_MASK 0xFFFF
61 #define FFA_SENDER(src_dst_ids) \
62 		((src_dst_ids >> 16) & FFA_PARTITION_ID_MASK)
63 #define FFA_RECEIVER(src_dst_ids) \
64 		(src_dst_ids & FFA_PARTITION_ID_MASK)
65 #define FFA_RUN_TARGET(src_dst_ids) \
66 		((src_dst_ids >> 16) & FFA_PARTITION_ID_MASK)
67 
68 
69 /* Get FFA fastcall std FID from function number */
70 #define FFA_FID(smc_cc, func_num)			\
71 		((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT) |	\
72 		 ((smc_cc) << FUNCID_CC_SHIFT) |	\
73 		 (OEN_STD_START << FUNCID_OEN_SHIFT) |	\
74 		 ((func_num) << FUNCID_NUM_SHIFT))
75 
76 /*
77  * Defines for power management framework messages exchanged using direct
78  * messages between the SPMC and SP.
79  */
80 #define FFA_DIRECT_FRAMEWORK_MSG_SHIFT	31
81 #define FFA_DIRECT_FRAMEWORK_MSG_MASK	(1UL << FFA_DIRECT_FRAMEWORK_MSG_SHIFT)
82 
83 #define FFA_PM_MSG_MASK			0xFF
84 #define FFA_PM_MSG_PSCI_REQ		0x0
85 #define FFA_PM_MSG_WB_REQ		0x1		/* Warm boot request */
86 #define FFA_PM_MSG_PM_RESP		0x2		/* Response to a PSCI or warmboot request */
87 
88 #define FFA_WB_TYPE_S2RAM		0
89 #define FFA_WB_TYPE_NOTS2RAM		1
90 
91 /* FFA function numbers */
92 #define FFA_FNUM_ERROR				U(0x60)
93 #define FFA_FNUM_SUCCESS			U(0x61)
94 #define FFA_FNUM_INTERRUPT			U(0x62)
95 #define FFA_FNUM_VERSION			U(0x63)
96 #define FFA_FNUM_FEATURES			U(0x64)
97 #define FFA_FNUM_RX_RELEASE			U(0x65)
98 #define FFA_FNUM_RXTX_MAP			U(0x66)
99 #define FFA_FNUM_RXTX_UNMAP			U(0x67)
100 #define FFA_FNUM_PARTITION_INFO_GET		U(0x68)
101 #define FFA_FNUM_ID_GET				U(0x69)
102 #define FFA_FNUM_MSG_POLL			U(0x6A) /* Legacy FF-A v1.0 */
103 #define FFA_FNUM_MSG_WAIT			U(0x6B)
104 #define FFA_FNUM_MSG_YIELD			U(0x6C)
105 #define FFA_FNUM_MSG_RUN			U(0x6D)
106 #define FFA_FNUM_MSG_SEND			U(0x6E) /* Legacy FF-A v1.0 */
107 #define FFA_FNUM_MSG_SEND_DIRECT_REQ		U(0x6F)
108 #define FFA_FNUM_MSG_SEND_DIRECT_RESP		U(0x70)
109 #define FFA_FNUM_MEM_DONATE			U(0x71)
110 #define FFA_FNUM_MEM_LEND			U(0x72)
111 #define FFA_FNUM_MEM_SHARE			U(0x73)
112 #define FFA_FNUM_MEM_RETRIEVE_REQ		U(0x74)
113 #define FFA_FNUM_MEM_RETRIEVE_RESP		U(0x75)
114 #define FFA_FNUM_MEM_RELINQUISH			U(0x76)
115 #define FFA_FNUM_MEM_RECLAIM			U(0x77)
116 #define FFA_FNUM_MEM_FRAG_RX			U(0x7A)
117 #define FFA_FNUM_MEM_FRAG_TX			U(0x7B)
118 #define FFA_FNUM_NORMAL_WORLD_RESUME		U(0x7C)
119 
120 /* FF-A v1.1 */
121 #define FFA_FNUM_NOTIFICATION_BITMAP_CREATE	U(0x7D)
122 #define FFA_FNUM_NOTIFICATION_BITMAP_DESTROY	U(0x7E)
123 #define FFA_FNUM_NOTIFICATION_BIND		U(0x7F)
124 #define FFA_FNUM_NOTIFICATION_UNBIND		U(0x80)
125 #define FFA_FNUM_NOTIFICATION_SET		U(0x81)
126 #define FFA_FNUM_NOTIFICATION_GET		U(0x82)
127 #define FFA_FNUM_NOTIFICATION_INFO_GET		U(0x83)
128 #define FFA_FNUM_RX_ACQUIRE			U(0x84)
129 #define FFA_FNUM_SPM_ID_GET			U(0x85)
130 #define FFA_FNUM_MSG_SEND2			U(0x86)
131 #define FFA_FNUM_SECONDARY_EP_REGISTER		U(0x87)
132 
133 /* FFA SMC32 FIDs */
134 #define FFA_ERROR		FFA_FID(SMC_32, FFA_FNUM_ERROR)
135 #define FFA_SUCCESS_SMC32	FFA_FID(SMC_32, FFA_FNUM_SUCCESS)
136 #define FFA_INTERRUPT		FFA_FID(SMC_32, FFA_FNUM_INTERRUPT)
137 #define FFA_VERSION		FFA_FID(SMC_32, FFA_FNUM_VERSION)
138 #define FFA_FEATURES		FFA_FID(SMC_32, FFA_FNUM_FEATURES)
139 #define FFA_RX_RELEASE		FFA_FID(SMC_32, FFA_FNUM_RX_RELEASE)
140 #define FFA_RXTX_MAP_SMC32	FFA_FID(SMC_32, FFA_FNUM_RXTX_MAP)
141 #define FFA_RXTX_UNMAP		FFA_FID(SMC_32, FFA_FNUM_RXTX_UNMAP)
142 #define FFA_PARTITION_INFO_GET	FFA_FID(SMC_32, FFA_FNUM_PARTITION_INFO_GET)
143 #define FFA_ID_GET		FFA_FID(SMC_32, FFA_FNUM_ID_GET)
144 #define FFA_MSG_POLL		FFA_FID(SMC_32, FFA_FNUM_MSG_POLL)
145 #define FFA_MSG_WAIT		FFA_FID(SMC_32, FFA_FNUM_MSG_WAIT)
146 #define FFA_MSG_YIELD		FFA_FID(SMC_32, FFA_FNUM_MSG_YIELD)
147 #define FFA_MSG_RUN		FFA_FID(SMC_32, FFA_FNUM_MSG_RUN)
148 #define FFA_MSG_SEND		FFA_FID(SMC_32, FFA_FNUM_MSG_SEND)
149 #define FFA_MSG_SEND_DIRECT_REQ_SMC32 \
150 	FFA_FID(SMC_32, FFA_FNUM_MSG_SEND_DIRECT_REQ)
151 #define FFA_MSG_SEND_DIRECT_RESP_SMC32	\
152 	FFA_FID(SMC_32, FFA_FNUM_MSG_SEND_DIRECT_RESP)
153 #define FFA_MEM_DONATE_SMC32	FFA_FID(SMC_32, FFA_FNUM_MEM_DONATE)
154 #define FFA_MEM_LEND_SMC32	FFA_FID(SMC_32, FFA_FNUM_MEM_LEND)
155 #define FFA_MEM_SHARE_SMC32	FFA_FID(SMC_32, FFA_FNUM_MEM_SHARE)
156 #define FFA_MEM_RETRIEVE_REQ_SMC32 \
157 	FFA_FID(SMC_32, FFA_FNUM_MEM_RETRIEVE_REQ)
158 #define FFA_MEM_RETRIEVE_RESP	FFA_FID(SMC_32, FFA_FNUM_MEM_RETRIEVE_RESP)
159 #define FFA_MEM_RELINQUISH	FFA_FID(SMC_32, FFA_FNUM_MEM_RELINQUISH)
160 #define FFA_MEM_RECLAIM	FFA_FID(SMC_32, FFA_FNUM_MEM_RECLAIM)
161 #define FFA_MEM_FRAG_RX	FFA_FID(SMC_32, FFA_FNUM_MEM_FRAG_RX)
162 #define FFA_MEM_FRAG_TX	FFA_FID(SMC_32, FFA_FNUM_MEM_FRAG_TX)
163 #define FFA_SPM_ID_GET		FFA_FID(SMC_32, FFA_FNUM_SPM_ID_GET)
164 
165 /* FFA SMC64 FIDs */
166 #define FFA_ERROR_SMC64		FFA_FID(SMC_64, FFA_FNUM_ERROR)
167 #define FFA_SUCCESS_SMC64	FFA_FID(SMC_64, FFA_FNUM_SUCCESS)
168 #define FFA_RXTX_MAP_SMC64	FFA_FID(SMC_64, FFA_FNUM_RXTX_MAP)
169 #define FFA_MSG_SEND_DIRECT_REQ_SMC64 \
170 	FFA_FID(SMC_64, FFA_FNUM_MSG_SEND_DIRECT_REQ)
171 #define FFA_MSG_SEND_DIRECT_RESP_SMC64	\
172 	FFA_FID(SMC_64, FFA_FNUM_MSG_SEND_DIRECT_RESP)
173 #define FFA_MEM_DONATE_SMC64	FFA_FID(SMC_64, FFA_FNUM_MEM_DONATE)
174 #define FFA_MEM_LEND_SMC64	FFA_FID(SMC_64, FFA_FNUM_MEM_LEND)
175 #define FFA_MEM_SHARE_SMC64	FFA_FID(SMC_64, FFA_FNUM_MEM_SHARE)
176 #define FFA_MEM_RETRIEVE_REQ_SMC64 \
177 	FFA_FID(SMC_64, FFA_FNUM_MEM_RETRIEVE_REQ)
178 #define FFA_SECONDARY_EP_REGISTER_SMC64 \
179 	FFA_FID(SMC_64, FFA_FNUM_SECONDARY_EP_REGISTER)
180 
181 /*
182  * Reserve a special value for traffic targeted to the Hypervisor or SPM.
183  */
184 #define FFA_TARGET_INFO_MBZ		U(0x0)
185 
186 /*
187  * Reserve a special value for MBZ parameters.
188  */
189 #define FFA_PARAM_MBZ			U(0x0)
190 
191 /*
192  * Maximum FF-A endpoint id value
193  */
194 #define FFA_ENDPOINT_ID_MAX			U(1 << 16)
195 
196 /*
197  * FFA Mask for the normal world.
198  */
199 #define FFA_SECURE_WORLD_ID_MASK	U(0x8000)
200 
201 /*
202  * Mask for source and destination endpoint id in
203  * a direct message request/response.
204  */
205 #define FFA_DIRECT_MSG_ENDPOINT_ID_MASK		U(0xffff)
206 
207 /*
208  * Bit shift for destination endpoint id in a direct message request/response.
209  */
210 #define FFA_DIRECT_MSG_DESTINATION_SHIFT	U(0)
211 
212 /*
213  * Bit shift for source endpoint id in a direct message request/response.
214  */
215 #define FFA_DIRECT_MSG_SOURCE_SHIFT		U(16)
216 
217 /******************************************************************************
218  * ffa_endpoint_destination
219  *****************************************************************************/
ffa_endpoint_destination(unsigned int ep)220 static inline uint16_t ffa_endpoint_destination(unsigned int ep)
221 {
222 	return (ep >> FFA_DIRECT_MSG_DESTINATION_SHIFT) &
223 		FFA_DIRECT_MSG_ENDPOINT_ID_MASK;
224 }
225 
226 /******************************************************************************
227  * ffa_endpoint_source
228  *****************************************************************************/
ffa_endpoint_source(unsigned int ep)229 static inline uint16_t ffa_endpoint_source(unsigned int ep)
230 {
231 	return (ep >> FFA_DIRECT_MSG_SOURCE_SHIFT) &
232 		FFA_DIRECT_MSG_ENDPOINT_ID_MASK;
233 }
234 
235 /******************************************************************************
236  * ffa helper functions to determine partition ID world
237  *****************************************************************************/
238 
239 /*
240  * Determine if provided ID is in the secure world.
241  */
ffa_is_secure_world_id(uint16_t id)242 static inline bool ffa_is_secure_world_id(uint16_t id) {
243 	return id & FFA_SECURE_WORLD_ID_MASK;
244 }
245 
246 /*
247  * Determine if provided ID is in the normal world.
248  */
ffa_is_normal_world_id(uint16_t id)249 static inline bool ffa_is_normal_world_id(uint16_t id) {
250 	return !ffa_is_secure_world_id(id);
251 }
252 
253 #endif /* FFA_SVC_H */
254