1 /*
2  * Copyright (c) 2020-2022, 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 #define FFA_VERSION_MASK		U(0xFFFFFFFF)
42 
43 
44 #define MAKE_FFA_VERSION(major, minor) 	\
45 	((((major) & FFA_VERSION_MAJOR_MASK) <<  FFA_VERSION_MAJOR_SHIFT) | \
46 	 (((minor) & FFA_VERSION_MINOR_MASK) << FFA_VERSION_MINOR_SHIFT))
47 #define FFA_VERSION_COMPILED		MAKE_FFA_VERSION(FFA_VERSION_MAJOR, \
48 							  FFA_VERSION_MINOR)
49 
50 /* FFA_MSG_SEND helpers */
51 #define FFA_MSG_SEND_ATTRS_BLK_SHIFT	U(0)
52 #define FFA_MSG_SEND_ATTRS_BLK_MASK	U(0x1)
53 #define FFA_MSG_SEND_ATTRS_BLK		U(0)
54 #define FFA_MSG_SEND_ATTRS_BLK_NOT	U(1)
55 #define FFA_MSG_SEND_ATTRS(blk)		\
56 	(((blk) & FFA_MSG_SEND_ATTRS_BLK_MASK) \
57 	<< FFA_MSG_SEND_ATTRS_BLK_SHIFT)
58 
59 /* Defines for FF-A framework messages exchanged using direct messages. */
60 #define FFA_FWK_MSG_BIT		BIT(31)
61 #define FFA_FWK_MSG_MASK	0xFF
62 #define FFA_FWK_MSG_PSCI	U(0x0)
63 
64 /* Defines for FF-A power management messages framework messages. */
65 #define FFA_PM_MSG_WB_REQ	U(0x1) /* Warm boot request. */
66 #define FFA_PM_MSG_PM_RESP	U(0x2) /* Response to PSCI or warmboot req. */
67 
68 /* FF-A warm boot types. */
69 #define FFA_WB_TYPE_S2RAM	0x0
70 #define FFA_WB_TYPE_NOTS2RAM	0x1
71 
72 /* Get FFA fastcall std FID from function number */
73 #define FFA_FID(smc_cc, func_num)			\
74 		((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT) |	\
75 		 ((smc_cc) << FUNCID_CC_SHIFT) |	\
76 		 (OEN_STD_START << FUNCID_OEN_SHIFT) |	\
77 		 ((func_num) << FUNCID_NUM_SHIFT))
78 
79 /* FFA function numbers */
80 #define FFA_FNUM_ERROR				U(0x60)
81 #define FFA_FNUM_SUCCESS			U(0x61)
82 #define FFA_FNUM_INTERRUPT			U(0x62)
83 #define FFA_FNUM_VERSION			U(0x63)
84 #define FFA_FNUM_FEATURES			U(0x64)
85 #define FFA_FNUM_RX_RELEASE			U(0x65)
86 #define FFA_FNUM_RXTX_MAP			U(0x66)
87 #define FFA_FNUM_RXTX_UNMAP			U(0x67)
88 #define FFA_FNUM_PARTITION_INFO_GET		U(0x68)
89 #define FFA_FNUM_ID_GET				U(0x69)
90 #define FFA_FNUM_MSG_POLL			U(0x6A) /* Legacy FF-A v1.0 */
91 #define FFA_FNUM_MSG_WAIT			U(0x6B)
92 #define FFA_FNUM_MSG_YIELD			U(0x6C)
93 #define FFA_FNUM_MSG_RUN			U(0x6D)
94 #define FFA_FNUM_MSG_SEND			U(0x6E) /* Legacy FF-A v1.0 */
95 #define FFA_FNUM_MSG_SEND_DIRECT_REQ		U(0x6F)
96 #define FFA_FNUM_MSG_SEND_DIRECT_RESP		U(0x70)
97 #define FFA_FNUM_MEM_DONATE			U(0x71)
98 #define FFA_FNUM_MEM_LEND			U(0x72)
99 #define FFA_FNUM_MEM_SHARE			U(0x73)
100 #define FFA_FNUM_MEM_RETRIEVE_REQ		U(0x74)
101 #define FFA_FNUM_MEM_RETRIEVE_RESP		U(0x75)
102 #define FFA_FNUM_MEM_RELINQUISH			U(0x76)
103 #define FFA_FNUM_MEM_RECLAIM			U(0x77)
104 #define FFA_FNUM_MEM_FRAG_RX			U(0x7A)
105 #define FFA_FNUM_MEM_FRAG_TX			U(0x7B)
106 #define FFA_FNUM_NORMAL_WORLD_RESUME		U(0x7C)
107 
108 /* FF-A v1.1 */
109 #define FFA_FNUM_NOTIFICATION_BITMAP_CREATE	U(0x7D)
110 #define FFA_FNUM_NOTIFICATION_BITMAP_DESTROY	U(0x7E)
111 #define FFA_FNUM_NOTIFICATION_BIND		U(0x7F)
112 #define FFA_FNUM_NOTIFICATION_UNBIND		U(0x80)
113 #define FFA_FNUM_NOTIFICATION_SET		U(0x81)
114 #define FFA_FNUM_NOTIFICATION_GET		U(0x82)
115 #define FFA_FNUM_NOTIFICATION_INFO_GET		U(0x83)
116 #define FFA_FNUM_RX_ACQUIRE			U(0x84)
117 #define FFA_FNUM_SPM_ID_GET			U(0x85)
118 #define FFA_FNUM_MSG_SEND2			U(0x86)
119 #define FFA_FNUM_SECONDARY_EP_REGISTER		U(0x87)
120 
121 /* FFA SMC32 FIDs */
122 #define FFA_ERROR		FFA_FID(SMC_32, FFA_FNUM_ERROR)
123 #define FFA_SUCCESS_SMC32	FFA_FID(SMC_32, FFA_FNUM_SUCCESS)
124 #define FFA_INTERRUPT		FFA_FID(SMC_32, FFA_FNUM_INTERRUPT)
125 #define FFA_VERSION		FFA_FID(SMC_32, FFA_FNUM_VERSION)
126 #define FFA_FEATURES		FFA_FID(SMC_32, FFA_FNUM_FEATURES)
127 #define FFA_RX_RELEASE		FFA_FID(SMC_32, FFA_FNUM_RX_RELEASE)
128 #define FFA_RX_ACQUIRE		FFA_FID(SMC_32, FFA_FNUM_RX_ACQUIRE)
129 #define FFA_RXTX_MAP_SMC32	FFA_FID(SMC_32, FFA_FNUM_RXTX_MAP)
130 #define FFA_RXTX_UNMAP		FFA_FID(SMC_32, FFA_FNUM_RXTX_UNMAP)
131 #define FFA_PARTITION_INFO_GET	FFA_FID(SMC_32, FFA_FNUM_PARTITION_INFO_GET)
132 #define FFA_ID_GET		FFA_FID(SMC_32, FFA_FNUM_ID_GET)
133 #define FFA_MSG_POLL		FFA_FID(SMC_32, FFA_FNUM_MSG_POLL)
134 #define FFA_MSG_WAIT		FFA_FID(SMC_32, FFA_FNUM_MSG_WAIT)
135 #define FFA_MSG_YIELD		FFA_FID(SMC_32, FFA_FNUM_MSG_YIELD)
136 #define FFA_MSG_RUN		FFA_FID(SMC_32, FFA_FNUM_MSG_RUN)
137 #define FFA_MSG_SEND		FFA_FID(SMC_32, FFA_FNUM_MSG_SEND)
138 #define FFA_MSG_SEND2		FFA_FID(SMC_32, FFA_FNUM_MSG_SEND2)
139 #define FFA_MSG_SEND_DIRECT_REQ_SMC32 \
140 	FFA_FID(SMC_32, FFA_FNUM_MSG_SEND_DIRECT_REQ)
141 #define FFA_MSG_SEND_DIRECT_RESP_SMC32	\
142 	FFA_FID(SMC_32, FFA_FNUM_MSG_SEND_DIRECT_RESP)
143 #define FFA_MEM_DONATE_SMC32	FFA_FID(SMC_32, FFA_FNUM_MEM_DONATE)
144 #define FFA_MEM_LEND_SMC32	FFA_FID(SMC_32, FFA_FNUM_MEM_LEND)
145 #define FFA_MEM_SHARE_SMC32	FFA_FID(SMC_32, FFA_FNUM_MEM_SHARE)
146 #define FFA_MEM_RETRIEVE_REQ_SMC32 \
147 	FFA_FID(SMC_32, FFA_FNUM_MEM_RETRIEVE_REQ)
148 #define FFA_MEM_RETRIEVE_RESP	FFA_FID(SMC_32, FFA_FNUM_MEM_RETRIEVE_RESP)
149 #define FFA_MEM_RELINQUISH	FFA_FID(SMC_32, FFA_FNUM_MEM_RELINQUISH)
150 #define FFA_MEM_RECLAIM	FFA_FID(SMC_32, FFA_FNUM_MEM_RECLAIM)
151 #define FFA_NOTIFICATION_BITMAP_CREATE	\
152 	FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_BITMAP_CREATE)
153 #define FFA_NOTIFICATION_BITMAP_DESTROY	\
154 	FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_BITMAP_DESTROY)
155 #define FFA_NOTIFICATION_BIND	FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_BIND)
156 #define FFA_NOTIFICATION_UNBIND FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_UNBIND)
157 #define FFA_NOTIFICATION_SET 	FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_SET)
158 #define FFA_NOTIFICATION_GET 	FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_GET)
159 #define FFA_NOTIFICATION_INFO_GET \
160 	FFA_FID(SMC_32, FFA_FNUM_NOTIFICATION_INFO_GET)
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 #define FFA_NORMAL_WORLD_RESUME	FFA_FID(SMC_32, FFA_FNUM_NORMAL_WORLD_RESUME)
165 
166 /* FFA SMC64 FIDs */
167 #define FFA_ERROR_SMC64		FFA_FID(SMC_64, FFA_FNUM_ERROR)
168 #define FFA_SUCCESS_SMC64	FFA_FID(SMC_64, FFA_FNUM_SUCCESS)
169 #define FFA_RXTX_MAP_SMC64	FFA_FID(SMC_64, FFA_FNUM_RXTX_MAP)
170 #define FFA_MSG_SEND_DIRECT_REQ_SMC64 \
171 	FFA_FID(SMC_64, FFA_FNUM_MSG_SEND_DIRECT_REQ)
172 #define FFA_MSG_SEND_DIRECT_RESP_SMC64	\
173 	FFA_FID(SMC_64, FFA_FNUM_MSG_SEND_DIRECT_RESP)
174 #define FFA_MEM_DONATE_SMC64	FFA_FID(SMC_64, FFA_FNUM_MEM_DONATE)
175 #define FFA_MEM_LEND_SMC64	FFA_FID(SMC_64, FFA_FNUM_MEM_LEND)
176 #define FFA_MEM_SHARE_SMC64	FFA_FID(SMC_64, FFA_FNUM_MEM_SHARE)
177 #define FFA_MEM_RETRIEVE_REQ_SMC64 \
178 	FFA_FID(SMC_64, FFA_FNUM_MEM_RETRIEVE_REQ)
179 #define FFA_SECONDARY_EP_REGISTER_SMC64 \
180 	FFA_FID(SMC_64, FFA_FNUM_SECONDARY_EP_REGISTER)
181 #define FFA_NOTIFICATION_INFO_GET_SMC64 \
182 	FFA_FID(SMC_64, FFA_FNUM_NOTIFICATION_INFO_GET)
183 
184 /*
185  * FF-A partition properties values.
186  */
187 #define FFA_PARTITION_DIRECT_REQ_RECV	U(1 << 0)
188 #define FFA_PARTITION_DIRECT_REQ_SEND	U(1 << 1)
189 #define FFA_PARTITION_INDIRECT_MSG	U(1 << 2)
190 
191 /*
192  * Reserve a special value for traffic targeted to the Hypervisor or SPM.
193  */
194 #define FFA_TARGET_INFO_MBZ		U(0x0)
195 
196 /*
197  * Reserve a special value for MBZ parameters.
198  */
199 #define FFA_PARAM_MBZ			U(0x0)
200 
201 /*
202  * Maximum FF-A endpoint id value
203  */
204 #define FFA_ENDPOINT_ID_MAX			U(1 << 16)
205 
206 /*
207  * Reserve endpoint id for the SPMD.
208  */
209 #define SPMD_DIRECT_MSG_ENDPOINT_ID		U(FFA_ENDPOINT_ID_MAX - 1)
210 
211 /* Mask and shift to check valid secure FF-A Endpoint ID. */
212 #define SPMC_SECURE_ID_MASK			U(1)
213 #define SPMC_SECURE_ID_SHIFT			U(15)
214 
215 /*
216  * Partition Count Flag in FFA_PARTITION_INFO_GET.
217  */
218 #define FFA_PARTITION_INFO_GET_COUNT_FLAG_MASK U(1 << 0)
219 
220 /*
221  * Mask for source and destination endpoint id in
222  * a direct message request/response.
223  */
224 #define FFA_DIRECT_MSG_ENDPOINT_ID_MASK		U(0xffff)
225 
226 /*
227  * Bit shift for destination endpoint id in a direct message request/response.
228  */
229 #define FFA_DIRECT_MSG_DESTINATION_SHIFT	U(0)
230 
231 /*
232  * Bit shift for source endpoint id in a direct message request/response.
233  */
234 #define FFA_DIRECT_MSG_SOURCE_SHIFT		U(16)
235 
236 /******************************************************************************
237  * ffa_endpoint_destination
238  *****************************************************************************/
ffa_endpoint_destination(unsigned int ep)239 static inline uint16_t ffa_endpoint_destination(unsigned int ep)
240 {
241 	return (ep >> FFA_DIRECT_MSG_DESTINATION_SHIFT) &
242 		FFA_DIRECT_MSG_ENDPOINT_ID_MASK;
243 }
244 
245 /******************************************************************************
246  * ffa_endpoint_source
247  *****************************************************************************/
ffa_endpoint_source(unsigned int ep)248 static inline uint16_t ffa_endpoint_source(unsigned int ep)
249 {
250 	return (ep >> FFA_DIRECT_MSG_SOURCE_SHIFT) &
251 		FFA_DIRECT_MSG_ENDPOINT_ID_MASK;
252 }
253 
254 /******************************************************************************
255  * FF-A helper functions to determine partition ID world.
256  *****************************************************************************/
257 
258 /*
259  * Determine if provided ID is in the secure world.
260  */
ffa_is_secure_world_id(uint16_t id)261 static inline bool ffa_is_secure_world_id(uint16_t id)
262 {
263 	return ((id >> SPMC_SECURE_ID_SHIFT) & SPMC_SECURE_ID_MASK) == 1;
264 }
265 
266 /*
267  * Determine if provided ID is in the normal world.
268  */
ffa_is_normal_world_id(uint16_t id)269 static inline bool ffa_is_normal_world_id(uint16_t id)
270 {
271 	return !ffa_is_secure_world_id(id);
272 }
273 
274 
275 /******************************************************************************
276  * Boot information protocol as per the FF-A v1.1 spec.
277  *****************************************************************************/
278 #define FFA_INIT_DESC_SIGNATURE			0x00000FFA
279 
280 /* Boot information type. */
281 #define FFA_BOOT_INFO_TYPE_STD			U(0x0)
282 #define FFA_BOOT_INFO_TYPE_IMPL			U(0x1)
283 
284 #define FFA_BOOT_INFO_TYPE_MASK			U(0x1)
285 #define FFA_BOOT_INFO_TYPE_SHIFT		U(0x7)
286 #define FFA_BOOT_INFO_TYPE(type)		\
287 	(((type) & FFA_BOOT_INFO_TYPE_MASK)	\
288 	<< FFA_BOOT_INFO_TYPE_SHIFT)
289 
290 /* Boot information identifier. */
291 #define FFA_BOOT_INFO_TYPE_ID_FDT		U(0x0)
292 #define FFA_BOOT_INFO_TYPE_ID_HOB		U(0x1)
293 
294 #define FFA_BOOT_INFO_TYPE_ID_MASK		U(0x3F)
295 #define FFA_BOOT_INFO_TYPE_ID_SHIFT		U(0x0)
296 #define FFA_BOOT_INFO_TYPE_ID(type)		\
297 	(((type) & FFA_BOOT_INFO_TYPE_ID_MASK)	\
298 	<< FFA_BOOT_INFO_TYPE_ID_SHIFT)
299 
300 /* Format of Flags Name field. */
301 #define FFA_BOOT_INFO_FLAG_NAME_STRING		U(0x0)
302 #define FFA_BOOT_INFO_FLAG_NAME_UUID		U(0x1)
303 
304 #define FFA_BOOT_INFO_FLAG_NAME_MASK		U(0x3)
305 #define FFA_BOOT_INFO_FLAG_NAME_SHIFT		U(0x0)
306 #define FFA_BOOT_INFO_FLAG_NAME(type)		\
307 	(((type) & FFA_BOOT_INFO_FLAG_NAME_MASK)\
308 	<< FFA_BOOT_INFO_FLAG_NAME_SHIFT)
309 
310 /* Format of Flags Contents field. */
311 #define FFA_BOOT_INFO_FLAG_CONTENT_ADR		U(0x0)
312 #define FFA_BOOT_INFO_FLAG_CONTENT_VAL		U(0x1)
313 
314 #define FFA_BOOT_INFO_FLAG_CONTENT_MASK		U(0x1)
315 #define FFA_BOOT_INFO_FLAG_CONTENT_SHIFT	U(0x2)
316 #define FFA_BOOT_INFO_FLAG_CONTENT(content)		\
317 	(((content) & FFA_BOOT_INFO_FLAG_CONTENT_MASK)	\
318 	<< FFA_BOOT_INFO_FLAG_CONTENT_SHIFT)
319 
320 /* Boot information descriptor. */
321 struct ffa_boot_info_desc {
322 	uint8_t name[16];
323 	uint8_t type;
324 	uint8_t reserved;
325 	uint16_t flags;
326 	uint32_t size_boot_info;
327 	uint64_t content;
328 };
329 
330 /* Boot information header. */
331 struct ffa_boot_info_header {
332 	uint32_t signature; /* 0xFFA */
333 	uint32_t version;
334 	uint32_t size_boot_info_blob;
335 	uint32_t size_boot_info_desc;
336 	uint32_t count_boot_info_desc;
337 	uint32_t offset_boot_info_desc;
338 	uint64_t reserved;
339 };
340 
341 #endif /* FFA_SVC_H */
342