blob: b4879917c92230df47e2bae4bd99ee40a42f1b78 [file] [log] [blame]
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 The Hafnium Authors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01007 */
8
9#pragma once
10
11#include "hf/types.h"
12
Karl Meakin0e617d92024-04-05 12:55:22 +010013/**
14 * The version number of a Firmware Framework implementation is a 31-bit
15 * unsigned integer, with the upper 15 bits denoting the major revision,
16 * and the lower 16 bits denoting the minor revision.
17 *
18 * See FF-A specification v1.2 ALP1, section 13.2.1.
19 */
20enum ffa_version {
21 FFA_VERSION_1_0 = 0x10000,
22 FFA_VERSION_1_1 = 0x10001,
23 FFA_VERSION_1_2 = 0x10002,
Karl Meakin59352b32025-02-25 12:19:45 +000024/*
25 * Use the value of `FFA_VERSION` passed by the build system, otherwise default
26 * to latest FF-A version.
27 */
28#ifdef FFA_VERSION
29 FFA_VERSION_COMPILED = FFA_VERSION,
30#else
Karl Meakin0e617d92024-04-05 12:55:22 +010031 FFA_VERSION_COMPILED = FFA_VERSION_1_2,
Karl Meakin59352b32025-02-25 12:19:45 +000032#endif
Karl Meakin0e617d92024-04-05 12:55:22 +010033};
Daniel Boulby6e32c612021-02-17 15:09:41 +000034
Karl Meakin59352b32025-02-25 12:19:45 +000035static_assert((FFA_VERSION_1_0 <= FFA_VERSION_COMPILED) &&
36 (FFA_VERSION_1_2 >= FFA_VERSION_COMPILED),
37 "FFA_VERSION_COMPILED must be between v1.0 and v1.2");
38
Karl Meakin0e617d92024-04-05 12:55:22 +010039#define FFA_VERSION_MBZ_BIT (1U << 31U)
40#define FFA_VERSION_MAJOR_SHIFT (16U)
41#define FFA_VERSION_MAJOR_MASK (0x7FFFU)
42#define FFA_VERSION_MINOR_SHIFT (0U)
43#define FFA_VERSION_MINOR_MASK (0xFFFFU)
44
45/** Return true if the version is valid (i.e. bit 31 is 0). */
46static inline bool ffa_version_is_valid(uint32_t version)
47{
48 return (version & FFA_VERSION_MBZ_BIT) == 0;
49}
50
51/** Construct a version from a pair of major and minor components. */
52static inline enum ffa_version make_ffa_version(uint16_t major, uint16_t minor)
53{
54 return (enum ffa_version)((major << FFA_VERSION_MAJOR_SHIFT) |
55 (minor << FFA_VERSION_MINOR_SHIFT));
56}
57
58/** Get the major component of the version. */
59static inline uint16_t ffa_version_get_major(enum ffa_version version)
60{
61 return (version >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
62}
63
64/** Get the minor component of the version. */
65static inline uint16_t ffa_version_get_minor(enum ffa_version version)
66{
67 return (version >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK;
68}
Olivier Deprez62d99e32020-01-09 15:58:07 +010069
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010070/**
71 * Check major versions are equal and the minor version of the caller is
72 * less than or equal to the minor version of the callee.
73 */
Karl Meakin0e617d92024-04-05 12:55:22 +010074static inline bool ffa_versions_are_compatible(enum ffa_version caller,
75 enum ffa_version callee)
76{
77 return ffa_version_get_major(caller) == ffa_version_get_major(callee) &&
78 ffa_version_get_minor(caller) <= ffa_version_get_minor(callee);
79}
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010080
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010081/* clang-format off */
82
83#define FFA_LOW_32_ID 0x84000060
84#define FFA_HIGH_32_ID 0x8400007F
85#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010086#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010087
Karl Meakinf51a35f2023-08-07 17:53:52 +010088/**
89 * FF-A function identifiers.
90 * Don't forget to update `ffa_func_name` if you add a new one.
91 */
J-Alves3829fc02021-03-18 12:49:18 +000092#define FFA_ERROR_32 0x84000060
93#define FFA_SUCCESS_32 0x84000061
94#define FFA_SUCCESS_64 0xC4000061
95#define FFA_INTERRUPT_32 0x84000062
96#define FFA_VERSION_32 0x84000063
97#define FFA_FEATURES_32 0x84000064
98#define FFA_RX_RELEASE_32 0x84000065
99#define FFA_RXTX_MAP_32 0x84000066
100#define FFA_RXTX_MAP_64 0xC4000066
101#define FFA_RXTX_UNMAP_32 0x84000067
102#define FFA_PARTITION_INFO_GET_32 0x84000068
103#define FFA_ID_GET_32 0x84000069
104#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
105#define FFA_MSG_WAIT_32 0x8400006B
106#define FFA_YIELD_32 0x8400006C
107#define FFA_RUN_32 0x8400006D
108#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
109#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
110#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
111#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
112#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
113#define FFA_MEM_DONATE_32 0x84000071
J-Alves95fbb312024-03-20 15:19:16 +0000114#define FFA_MEM_DONATE_64 0xC4000071
J-Alves3829fc02021-03-18 12:49:18 +0000115#define FFA_MEM_LEND_32 0x84000072
J-Alves95fbb312024-03-20 15:19:16 +0000116#define FFA_MEM_LEND_64 0xC4000072
J-Alves3829fc02021-03-18 12:49:18 +0000117#define FFA_MEM_SHARE_32 0x84000073
J-Alves95fbb312024-03-20 15:19:16 +0000118#define FFA_MEM_SHARE_64 0xC4000073
J-Alves3829fc02021-03-18 12:49:18 +0000119#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
J-Alves95fbb312024-03-20 15:19:16 +0000120#define FFA_MEM_RETRIEVE_REQ_64 0xC4000074
J-Alves3829fc02021-03-18 12:49:18 +0000121#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
122#define FFA_MEM_RELINQUISH_32 0x84000076
123#define FFA_MEM_RECLAIM_32 0x84000077
124#define FFA_MEM_FRAG_RX_32 0x8400007A
125#define FFA_MEM_FRAG_TX_32 0x8400007B
126#define FFA_NORMAL_WORLD_RESUME 0x8400007C
127
128/* FF-A v1.1 */
129#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
130#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
131#define FFA_NOTIFICATION_BIND_32 0x8400007F
132#define FFA_NOTIFICATION_UNBIND_32 0x84000080
133#define FFA_NOTIFICATION_SET_32 0x84000081
134#define FFA_NOTIFICATION_GET_32 0x84000082
135#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
136#define FFA_RX_ACQUIRE_32 0x84000084
137#define FFA_SPM_ID_GET_32 0x84000085
138#define FFA_MSG_SEND2_32 0x84000086
139#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
140#define FFA_MEM_PERM_GET_32 0x84000088
141#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700142#define FFA_MEM_PERM_GET_64 0xC4000088
143#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100144
Kathleen Capellae4fe2962023-09-01 17:08:47 -0400145/* FF-A v1.2 */
Maksims Svecovs71b76702022-05-20 15:32:58 +0100146#define FFA_CONSOLE_LOG_32 0x8400008A
147#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800148#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -0600149#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Kathleen Capella41fea932023-06-23 17:39:28 -0400150#define FFA_MSG_SEND_DIRECT_REQ2_64 0xC400008D
Kathleen Capella087e5022023-09-07 18:04:15 -0400151#define FFA_MSG_SEND_DIRECT_RESP2_64 0xC400008E
Maksims Svecovs71b76702022-05-20 15:32:58 +0100152
Karl Meakinf51a35f2023-08-07 17:53:52 +0100153/**
154 * FF-A error codes.
155 * Don't forget to update `ffa_error_name` if you add a new one.
156 */
Karl Meakindc759f52024-05-07 16:08:02 +0100157enum ffa_error {
158 FFA_NOT_SUPPORTED = -1,
159 FFA_INVALID_PARAMETERS = -2,
160 FFA_NO_MEMORY = -3,
161 FFA_BUSY = -4,
162 FFA_INTERRUPTED = -5,
163 FFA_DENIED = -6,
164 FFA_RETRY = -7,
165 FFA_ABORTED = -8,
166 FFA_NO_DATA = -9,
167 FFA_NOT_READY = -10,
168};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100169
170/* clang-format on */
171
Karl Meakinf51a35f2023-08-07 17:53:52 +0100172/* Return the name of the function identifier. */
173static inline const char *ffa_func_name(uint32_t func)
174{
175 switch (func) {
176 case FFA_ERROR_32:
177 return "FFA_ERROR_32";
178 case FFA_SUCCESS_32:
179 return "FFA_SUCCESS_32";
180 case FFA_SUCCESS_64:
181 return "FFA_SUCCESS_64";
182 case FFA_INTERRUPT_32:
183 return "FFA_INTERRUPT_32";
184 case FFA_VERSION_32:
185 return "FFA_VERSION_32";
186 case FFA_FEATURES_32:
187 return "FFA_FEATURES_32";
188 case FFA_RX_RELEASE_32:
189 return "FFA_RX_RELEASE_32";
190 case FFA_RXTX_MAP_32:
191 return "FFA_RXTX_MAP_32";
192 case FFA_RXTX_MAP_64:
193 return "FFA_RXTX_MAP_64";
194 case FFA_RXTX_UNMAP_32:
195 return "FFA_RXTX_UNMAP_32";
196 case FFA_PARTITION_INFO_GET_32:
197 return "FFA_PARTITION_INFO_GET_32";
198 case FFA_ID_GET_32:
199 return "FFA_ID_GET_32";
200 case FFA_MSG_POLL_32:
201 return "FFA_MSG_POLL_32";
202 case FFA_MSG_WAIT_32:
203 return "FFA_MSG_WAIT_32";
204 case FFA_YIELD_32:
205 return "FFA_YIELD_32";
206 case FFA_RUN_32:
207 return "FFA_RUN_32";
208 case FFA_MSG_SEND_32:
209 return "FFA_MSG_SEND_32";
210 case FFA_MSG_SEND_DIRECT_REQ_32:
211 return "FFA_MSG_SEND_DIRECT_REQ_32";
212 case FFA_MSG_SEND_DIRECT_REQ_64:
213 return "FFA_MSG_SEND_DIRECT_REQ_64";
214 case FFA_MSG_SEND_DIRECT_RESP_32:
215 return "FFA_MSG_SEND_DIRECT_RESP_32";
216 case FFA_MSG_SEND_DIRECT_RESP_64:
217 return "FFA_MSG_SEND_DIRECT_RESP_64";
218 case FFA_MEM_DONATE_32:
219 return "FFA_MEM_DONATE_32";
220 case FFA_MEM_LEND_32:
221 return "FFA_MEM_LEND_32";
222 case FFA_MEM_SHARE_32:
223 return "FFA_MEM_SHARE_32";
224 case FFA_MEM_RETRIEVE_REQ_32:
225 return "FFA_MEM_RETRIEVE_REQ_32";
J-Alves95fbb312024-03-20 15:19:16 +0000226 case FFA_MEM_DONATE_64:
227 return "FFA_MEM_DONATE_64";
228 case FFA_MEM_LEND_64:
229 return "FFA_MEM_LEND_64";
230 case FFA_MEM_SHARE_64:
231 return "FFA_MEM_SHARE_64";
232 case FFA_MEM_RETRIEVE_REQ_64:
233 return "FFA_MEM_RETRIEVE_REQ_64";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100234 case FFA_MEM_RETRIEVE_RESP_32:
235 return "FFA_MEM_RETRIEVE_RESP_32";
236 case FFA_MEM_RELINQUISH_32:
237 return "FFA_MEM_RELINQUISH_32";
238 case FFA_MEM_RECLAIM_32:
239 return "FFA_MEM_RECLAIM_32";
240 case FFA_MEM_FRAG_RX_32:
241 return "FFA_MEM_FRAG_RX_32";
242 case FFA_MEM_FRAG_TX_32:
243 return "FFA_MEM_FRAG_TX_32";
244 case FFA_NORMAL_WORLD_RESUME:
245 return "FFA_NORMAL_WORLD_RESUME";
246
247 /* FF-A v1.1 */
248 case FFA_NOTIFICATION_BITMAP_CREATE_32:
249 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
250 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
251 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
252 case FFA_NOTIFICATION_BIND_32:
253 return "FFA_NOTIFICATION_BIND_32";
254 case FFA_NOTIFICATION_UNBIND_32:
255 return "FFA_NOTIFICATION_UNBIND_32";
256 case FFA_NOTIFICATION_SET_32:
257 return "FFA_NOTIFICATION_SET_32";
258 case FFA_NOTIFICATION_GET_32:
259 return "FFA_NOTIFICATION_GET_32";
260 case FFA_NOTIFICATION_INFO_GET_64:
261 return "FFA_NOTIFICATION_INFO_GET_64";
262 case FFA_RX_ACQUIRE_32:
263 return "FFA_RX_ACQUIRE_32";
264 case FFA_SPM_ID_GET_32:
265 return "FFA_SPM_ID_GET_32";
266 case FFA_MSG_SEND2_32:
267 return "FFA_MSG_SEND2_32";
268 case FFA_SECONDARY_EP_REGISTER_64:
269 return "FFA_SECONDARY_EP_REGISTER_64";
270 case FFA_MEM_PERM_GET_32:
271 return "FFA_MEM_PERM_GET_32";
272 case FFA_MEM_PERM_SET_32:
273 return "FFA_MEM_PERM_SET_32";
274 case FFA_MEM_PERM_GET_64:
275 return "FFA_MEM_PERM_GET_64";
276 case FFA_MEM_PERM_SET_64:
277 return "FFA_MEM_PERM_SET_64";
278
279 /* Implementation-defined ABIs. */
280 case FFA_CONSOLE_LOG_32:
281 return "FFA_CONSOLE_LOG_32";
282 case FFA_CONSOLE_LOG_64:
283 return "FFA_CONSOLE_LOG_64";
284 case FFA_PARTITION_INFO_GET_REGS_64:
285 return "FFA_PARTITION_INFO_GET_REGS_64";
286 case FFA_EL3_INTR_HANDLE_32:
287 return "FFA_EL3_INTR_HANDLE_32";
288
289 default:
290 return "UNKNOWN";
291 }
292}
293
294/* Return the name of the error code. */
Karl Meakindc759f52024-05-07 16:08:02 +0100295static inline const char *ffa_error_name(enum ffa_error error)
Karl Meakinf51a35f2023-08-07 17:53:52 +0100296{
297 switch (error) {
298 case FFA_NOT_SUPPORTED:
299 return "FFA_NOT_SUPPORTED";
300 case FFA_INVALID_PARAMETERS:
301 return "FFA_INVALID_PARAMETERS";
302 case FFA_NO_MEMORY:
303 return "FFA_NO_MEMORY";
304 case FFA_BUSY:
305 return "FFA_BUSY";
306 case FFA_INTERRUPTED:
307 return "FFA_INTERRUPTED";
308 case FFA_DENIED:
309 return "FFA_DENIED";
310 case FFA_RETRY:
311 return "FFA_RETRY";
312 case FFA_ABORTED:
313 return "FFA_ABORTED";
314 case FFA_NO_DATA:
315 return "FFA_NO_DATA";
Karl Meakindc759f52024-05-07 16:08:02 +0100316 case FFA_NOT_READY:
317 return "FFA_NOT_READY";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100318 }
Karl Meakindc759f52024-05-07 16:08:02 +0100319 return "UNKNOWN";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100320}
321
J-Alves6f72ca82021-11-01 12:34:58 +0000322/**
Karl Meakin49ec1e42024-05-10 13:08:24 +0100323 * Defined in Table 3.1 in the FF-A v.1.2 memory management supplement.
324 * Input properties:
325 * - Bits[31:2] and Bit[0] are reserved (SBZ).
326 * Output properties:
327 * - Bit[0]: dynamically allocated buffer support.
328 * - Bit[1]: NS bit handling.
329 * - Bit[2]: support for retrieval by hypervisor.
330 * - Bits[31:3] are reserved (MBZ).
J-Alves6f72ca82021-11-01 12:34:58 +0000331 */
Karl Meakin49ec1e42024-05-10 13:08:24 +0100332#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT (0U << 0U)
333#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (1U << 1U)
334#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (1U << 2U)
J-Alves6f72ca82021-11-01 12:34:58 +0000335
Karl Meakin49ec1e42024-05-10 13:08:24 +0100336#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT (31U)
337#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT (2U)
338#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT (0U)
J-Alves6f72ca82021-11-01 12:34:58 +0000339
Karl Meakin49ec1e42024-05-10 13:08:24 +0100340enum ffa_feature_id {
341 /* Query interrupt ID of Notification Pending Interrupt. */
342 FFA_FEATURE_NPI = 1,
Karl Meakin34b8ae92023-01-13 13:33:07 +0000343
Karl Meakin49ec1e42024-05-10 13:08:24 +0100344 /* Query interrupt ID of Schedule Receiver Interrupt. */
345 FFA_FEATURE_SRI = 2,
J-Alves6f72ca82021-11-01 12:34:58 +0000346
Karl Meakin49ec1e42024-05-10 13:08:24 +0100347 /* Query interrupt ID of the Managed Exit Interrupt. */
348 FFA_FEATURE_MEI = 3,
349};
J-Alves6f72ca82021-11-01 12:34:58 +0000350
Karl Meakin49ec1e42024-05-10 13:08:24 +0100351/** Constants for bitmasks used in FFA_FEATURES. */
352#define FFA_FEATURES_FEATURE_BIT (31U)
353#define FFA_FEATURES_FEATURE_MBZ_HI_BIT (30U)
354#define FFA_FEATURES_FEATURE_MBZ_LO_BIT (8U)
355
356#define FFA_FEATURES_NS_SUPPORT_BIT (1U)
J-Alves6f72ca82021-11-01 12:34:58 +0000357
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358/* FF-A function specific constants. */
359#define FFA_MSG_RECV_BLOCK 0x1
360#define FFA_MSG_RECV_BLOCK_MASK 0x1
361
362#define FFA_MSG_SEND_NOTIFY 0x1
363#define FFA_MSG_SEND_NOTIFY_MASK 0x1
364
365#define FFA_MEM_RECLAIM_CLEAR 0x1
366
367#define FFA_SLEEP_INDEFINITE 0
368
Karl Meakin80220052025-02-20 14:43:34 +0000369/*
370 * The type of memory permissions used by `FFA_MEM_PERM_GET` and
371 * `FFA_MEM_PERM_SET`.
372 */
373enum ffa_mem_perm {
374 FFA_MEM_PERM_RO = 0x7,
375 FFA_MEM_PERM_RW = 0x5,
376 FFA_MEM_PERM_RX = 0x3,
377};
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700378
Kathleen Capella7253bd52024-08-14 17:36:09 -0400379#define FFA_MSG_WAIT_FLAG_RETAIN_RX UINT32_C(0x1)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000380/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100381 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000382 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
383 * if partition info descriptors should be returned or just the count.
384 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100385#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
386#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000387
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100388/**
389 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
390 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
391 * configured to use.
392 */
J-Alves715d6232023-02-16 16:33:28 +0000393#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100394
Federico Recanatifc0295e2022-02-08 19:37:39 +0100395/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100396typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100397
398/**
J-Alves661e1b72023-08-02 13:39:40 +0100399 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
400 * parts:
401 * - Bit15 -> partition type identifier.
402 * - b'0 -> ID relates to a VM ID.
403 * - b'1 -> ID relates to an SP ID.
404 */
405#define FFA_ID_MASK ((ffa_id_t)0x8000)
406#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
407
408/**
409 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
410 */
411static inline bool ffa_is_vm_id(ffa_id_t id)
412{
413 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
414}
415
416/**
Karl Meakinc88c8e92024-11-29 16:13:55 +0000417 * Holds the UUID in a struct that is mappable directly to the SMCC calling
418 * convention, which is used for FF-A calls.
419 *
420 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
421 * of the SMCC Spec 1.2.
422 */
423struct ffa_uuid {
424 uint32_t uuid[4];
425};
426
427static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
428 uint32_t w3, struct ffa_uuid *uuid)
429{
430 uuid->uuid[0] = w0;
431 uuid->uuid[1] = w1;
432 uuid->uuid[2] = w2;
433 uuid->uuid[3] = w3;
434}
435
436static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
437 const struct ffa_uuid *uuid2)
438{
439 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
440 (uuid1->uuid[1] == uuid2->uuid[1]) &&
441 (uuid1->uuid[2] == uuid2->uuid[2]) &&
442 (uuid1->uuid[3] == uuid2->uuid[3]);
443}
444
445static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
446{
447 struct ffa_uuid null = {0};
448
449 return ffa_uuid_equal(uuid, &null);
450}
451
452static inline void ffa_uuid_from_u64x2(uint64_t uuid_lo, uint64_t uuid_hi,
453 struct ffa_uuid *uuid)
454{
455 ffa_uuid_init((uint32_t)(uuid_lo & 0xFFFFFFFFU),
456 (uint32_t)(uuid_lo >> 32),
457 (uint32_t)(uuid_hi & 0xFFFFFFFFU),
458 (uint32_t)(uuid_hi >> 32), uuid);
459}
460
461/**
462 * Split `uuid` into two u64s.
463 * This function writes to pointer parameters because C does not allow returning
464 * arrays from functions.
465 */
466static inline void ffa_uuid_to_u64x2(uint64_t *lo, uint64_t *hi,
467 const struct ffa_uuid *uuid)
468{
469 *lo = (uint64_t)uuid->uuid[1] << 32 | uuid->uuid[0];
470 *hi = (uint64_t)uuid->uuid[3] << 32 | uuid->uuid[2];
471}
472
473/**
474 * Partition message header as specified by table 7.1 from FF-A v1.3 ALP0
Federico Recanatifc0295e2022-02-08 19:37:39 +0100475 * specification.
476 */
477struct ffa_partition_rxtx_header {
Karl Meakinbf580c22024-12-12 14:31:06 +0000478 /* Reserved (SBZ). */
479 uint32_t flags;
480 /* Reserved (SBZ). */
481 uint32_t reserved_1;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100482 /* Offset from the beginning of the buffer to the message payload. */
483 uint32_t offset;
Karl Meakin9ca05512024-11-29 17:02:32 +0000484 /* Receiver endpoint ID. */
485 ffa_id_t receiver;
486 /* Sender endpoint ID. */
487 ffa_id_t sender;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100488 /* Size of message in buffer. */
489 uint32_t size;
Karl Meakinbf580c22024-12-12 14:31:06 +0000490 /* Reserved (SBZ). Added in v1.2. */
491 uint32_t reserved_2;
492 /* UUID identifying the communication protocol. Added in v1.2. */
493 struct ffa_uuid uuid;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100494};
495
Karl Meakinbf580c22024-12-12 14:31:06 +0000496#define FFA_RXTX_HEADER_SIZE_V1_1 \
497 offsetof(struct ffa_partition_rxtx_header, reserved_2)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100498#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
J-Alves70079932022-12-07 17:32:20 +0000499#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100500
Karl Meakin895007c2025-01-13 15:48:07 +0000501/**
Karl Meakinbf580c22024-12-12 14:31:06 +0000502 * Initialize a partition message header, with the default values for `flags`,
503 * `offset` and `uuid` and the v1.1 payload offset.
504 */
505static inline void ffa_rxtx_header_init_v1_1(
506 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
507 ffa_id_t receiver, uint32_t payload_size)
508{
509 header->flags = 0;
510 header->reserved_1 = 0;
511 header->offset = FFA_RXTX_HEADER_SIZE_V1_1;
512 header->sender = sender;
513 header->receiver = receiver;
514 header->size = payload_size;
515 header->reserved_2 = 0;
516 header->uuid = (struct ffa_uuid){0};
517}
518
519/**
520 * Initialize a partition message header, with the default values for `flags`,
521 * `offset` and `uuid`.
Karl Meakin895007c2025-01-13 15:48:07 +0000522 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100523static inline void ffa_rxtx_header_init(
Karl Meakin895007c2025-01-13 15:48:07 +0000524 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
525 ffa_id_t receiver, uint32_t payload_size)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100526{
527 header->flags = 0;
Karl Meakinbf580c22024-12-12 14:31:06 +0000528 header->reserved_1 = 0;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100529 header->offset = FFA_RXTX_HEADER_SIZE;
Karl Meakin9ca05512024-11-29 17:02:32 +0000530 header->sender = sender;
531 header->receiver = receiver;
Karl Meakin895007c2025-01-13 15:48:07 +0000532 header->size = payload_size;
Karl Meakinbf580c22024-12-12 14:31:06 +0000533 header->reserved_2 = 0;
534 header->uuid = (struct ffa_uuid){0};
535}
536
537/**
538 * Initialize a partition message header, with the default values for `flags`
539 * and `offset`.
540 */
541static inline void ffa_rxtx_header_init_with_uuid(
542 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
543 ffa_id_t receiver, uint32_t size, struct ffa_uuid uuid)
544{
545 header->flags = 0;
546 header->reserved_1 = 0;
547 header->offset = FFA_RXTX_HEADER_SIZE;
548 header->sender = sender;
549 header->receiver = receiver;
550 header->size = size;
551 header->reserved_2 = 0;
552 header->uuid = uuid;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100553}
554
Federico Recanatifc0295e2022-02-08 19:37:39 +0100555/* The maximum length possible for a single message. */
Karl Meakinbf580c22024-12-12 14:31:06 +0000556#define FFA_PARTITION_MSG_PAYLOAD_MAX_V1_1 \
557 (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE_V1_1)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100558#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
559
560struct ffa_partition_msg {
561 struct ffa_partition_rxtx_header header;
Karl Meakinbf580c22024-12-12 14:31:06 +0000562 /**
563 * Prefer using `ffa_partition_msg_payload` to accessing this field
564 * directly, because the offset does not necessarily correspond to the
565 * offset of this field.
566 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100567 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
568};
569
Karl Meakinbf580c22024-12-12 14:31:06 +0000570static_assert(sizeof(struct ffa_partition_msg) == HF_MAILBOX_SIZE,
571 "FF-A message size must match mailbox size");
572
Karl Meakin891eb882025-01-17 19:11:20 +0000573/**
574 * Get the partition message's payload, according to the header's `offset`
575 * field.
576 */
577static inline void *ffa_partition_msg_payload(struct ffa_partition_msg *msg)
578{
579 return (char *)msg + msg->header.offset;
580}
581
582static inline const void *ffa_partition_msg_payload_const(
583 const struct ffa_partition_msg *msg)
584{
585 return (const char *)msg + msg->header.offset;
586}
587
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100588/* The maximum length possible for a single message. */
589#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
590
591enum ffa_data_access {
592 FFA_DATA_ACCESS_NOT_SPECIFIED,
593 FFA_DATA_ACCESS_RO,
594 FFA_DATA_ACCESS_RW,
595 FFA_DATA_ACCESS_RESERVED,
596};
597
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100598static inline const char *ffa_data_access_name(enum ffa_data_access data_access)
599{
600 switch (data_access) {
601 case FFA_DATA_ACCESS_NOT_SPECIFIED:
602 return "FFA_DATA_ACCESS_NOT_SPECIFIED";
603 case FFA_DATA_ACCESS_RO:
604 return "FFA_DATA_ACCESS_RO";
605 case FFA_DATA_ACCESS_RW:
606 return "FFA_DATA_ACCESS_RW";
607 case FFA_DATA_ACCESS_RESERVED:
608 return "FFA_DATA_ACCESS_RESERVED";
609 }
610}
611
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100612enum ffa_instruction_access {
613 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
614 FFA_INSTRUCTION_ACCESS_NX,
615 FFA_INSTRUCTION_ACCESS_X,
616 FFA_INSTRUCTION_ACCESS_RESERVED,
617};
618
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100619static inline const char *ffa_instruction_access_name(
620 enum ffa_instruction_access instruction_access)
621{
622 switch (instruction_access) {
623 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
624 return "FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED";
625 case FFA_INSTRUCTION_ACCESS_NX:
626 return "FFA_INSTRUCTION_ACCESS_NX";
627 case FFA_INSTRUCTION_ACCESS_X:
628 return "FFA_INSTRUCTION_ACCESS_X";
629 case FFA_INSTRUCTION_ACCESS_RESERVED:
630 return "FFA_INSTRUCTION_ACCESS_RESERVED";
631 }
632}
633
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100634enum ffa_memory_type {
635 FFA_MEMORY_NOT_SPECIFIED_MEM,
636 FFA_MEMORY_DEVICE_MEM,
637 FFA_MEMORY_NORMAL_MEM,
638};
639
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100640static inline const char *ffa_memory_type_name(enum ffa_memory_type type)
641{
642 switch (type) {
643 case FFA_MEMORY_NOT_SPECIFIED_MEM:
644 return "FFA_MEMORY_NOT_SPECIFIED_MEM";
645 case FFA_MEMORY_DEVICE_MEM:
646 return "FFA_MEMORY_DEVICE_MEM";
647 case FFA_MEMORY_NORMAL_MEM:
648 return "FFA_MEMORY_NORMAL_MEM";
649 }
650}
651
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100652enum ffa_memory_cacheability {
653 FFA_MEMORY_CACHE_RESERVED = 0x0,
654 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
655 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
656 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
657 FFA_MEMORY_DEV_NGNRNE = 0x0,
658 FFA_MEMORY_DEV_NGNRE = 0x1,
659 FFA_MEMORY_DEV_NGRE = 0x2,
660 FFA_MEMORY_DEV_GRE = 0x3,
661};
662
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100663static inline const char *ffa_memory_cacheability_name(
664 enum ffa_memory_cacheability cacheability)
665{
666 switch (cacheability) {
667 case FFA_MEMORY_CACHE_RESERVED:
668 return "FFA_MEMORY_CACHE_RESERVED";
669 case FFA_MEMORY_CACHE_NON_CACHEABLE:
670 return "FFA_MEMORY_CACHE_NON_CACHEABLE";
671 case FFA_MEMORY_CACHE_RESERVED_1:
672 return "FFA_MEMORY_CACHE_RESERVED_1";
673 case FFA_MEMORY_CACHE_WRITE_BACK:
674 return "FFA_MEMORY_CACHE_WRITE_BACK";
675 }
676}
677
Daniel Boulby9764ff62024-01-30 17:47:39 +0000678static inline const char *ffa_device_memory_cacheability_name(
679 enum ffa_memory_cacheability cacheability)
680{
681 switch (cacheability) {
682 case FFA_MEMORY_DEV_NGNRNE:
683 return "FFA_MEMORY_DEV_NGNRNE";
684 case FFA_MEMORY_DEV_NGNRE:
685 return "FFA_MEMORY_DEV_NGNRE";
686 case FFA_MEMORY_DEV_NGRE:
687 return "FFA_MEMORY_DEV_NGRE";
688 case FFA_MEMORY_DEV_GRE:
689 return "FFA_MEMORY_DEV_GRE";
690 }
691}
692
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100693enum ffa_memory_shareability {
694 FFA_MEMORY_SHARE_NON_SHAREABLE,
695 FFA_MEMORY_SHARE_RESERVED,
696 FFA_MEMORY_OUTER_SHAREABLE,
697 FFA_MEMORY_INNER_SHAREABLE,
698};
699
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100700static inline const char *ffa_memory_shareability_name(
701 enum ffa_memory_shareability shareability)
702{
703 switch (shareability) {
704 case FFA_MEMORY_SHARE_NON_SHAREABLE:
705 return "FFA_MEMORY_SHARE_NON_SHAREABLE";
706 case FFA_MEMORY_SHARE_RESERVED:
707 return "FFA_MEMORY_SHARE_RESERVED";
708 case FFA_MEMORY_OUTER_SHAREABLE:
709 return "FFA_MEMORY_OUTER_SHAREABLE";
710 case FFA_MEMORY_INNER_SHAREABLE:
711 return "FFA_MEMORY_INNER_SHAREABLE";
712 }
713}
714
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100715/**
716 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
717 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
718 * and FFA_MEM_RETRIEVE_REQUEST.
719 */
720enum ffa_memory_security {
721 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
722 FFA_MEMORY_SECURITY_SECURE = 0,
723 FFA_MEMORY_SECURITY_NON_SECURE,
724};
725
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100726static inline const char *ffa_memory_security_name(
727 enum ffa_memory_security security)
728{
729 switch (security) {
730 case FFA_MEMORY_SECURITY_UNSPECIFIED:
731 return "FFA_MEMORY_SECURITY_UNSPECIFIED";
732 case FFA_MEMORY_SECURITY_NON_SECURE:
733 return "FFA_MEMORY_SECURITY_NON_SECURE";
734 }
735}
736
Karl Meakin84710f32023-10-12 15:14:49 +0100737typedef struct {
738 uint8_t data_access : 2;
739 uint8_t instruction_access : 2;
740} ffa_memory_access_permissions_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100741
742/**
J-Alves0b6653d2022-04-22 13:17:38 +0100743 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100744 * region attributes descriptor".
745 */
Karl Meakin84710f32023-10-12 15:14:49 +0100746typedef struct {
747 uint8_t shareability : 2;
748 uint8_t cacheability : 2;
749 uint8_t type : 2;
750 uint8_t security : 2;
751 uint8_t : 8;
752} ffa_memory_attributes_t;
J-Alves0b6653d2022-04-22 13:17:38 +0100753
754/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
755#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100756
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100757/**
758 * A globally-unique ID assigned by the hypervisor for a region of memory being
759 * sent between VMs.
760 */
761typedef uint64_t ffa_memory_handle_t;
762
Karl Meakin1a760e72024-07-25 18:58:37 +0100763enum ffa_memory_handle_allocator {
764 FFA_MEMORY_HANDLE_ALLOCATOR_SPMC = 0,
765 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR = 1,
766};
J-Alves917d2f22020-10-30 18:39:30 +0000767
Karl Meakin1a760e72024-07-25 18:58:37 +0100768#define FFA_MEMORY_HANDLE_ALLOCATOR_BIT UINT64_C(63)
769#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
770 (UINT64_C(1) << FFA_MEMORY_HANDLE_ALLOCATOR_BIT)
J-Alves917d2f22020-10-30 18:39:30 +0000771#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
772
Karl Meakin1a760e72024-07-25 18:58:37 +0100773static inline ffa_memory_handle_t ffa_memory_handle_make(
774 uint64_t index, enum ffa_memory_handle_allocator allocator)
775{
776 return index | ((uint64_t)allocator << FFA_MEMORY_HANDLE_ALLOCATOR_BIT);
777}
778
779static inline uint64_t ffa_memory_handle_index(ffa_memory_handle_t handle)
780{
781 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
782}
783
784static inline enum ffa_memory_handle_allocator ffa_memory_handle_allocator(
785 ffa_memory_handle_t handle)
786{
787 return ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) != 0)
788 ? FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR
789 : FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
790}
791
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100792/**
793 * A count of VMs. This has the same range as the VM IDs but we give it a
794 * different name to make the different semantics clear.
795 */
J-Alves19e20cf2023-08-02 12:48:55 +0100796typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100797
798/** The index of a vCPU within a particular VM. */
799typedef uint16_t ffa_vcpu_index_t;
800
801/**
802 * A count of vCPUs. This has the same range as the vCPU indices but we give it
803 * a different name to make the different semantics clear.
804 */
805typedef ffa_vcpu_index_t ffa_vcpu_count_t;
806
807/** Parameter and return type of FF-A functions. */
808struct ffa_value {
809 uint64_t func;
810 uint64_t arg1;
811 uint64_t arg2;
812 uint64_t arg3;
813 uint64_t arg4;
814 uint64_t arg5;
815 uint64_t arg6;
816 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800817
818 struct {
819 uint64_t arg8;
820 uint64_t arg9;
821 uint64_t arg10;
822 uint64_t arg11;
823 uint64_t arg12;
824 uint64_t arg13;
825 uint64_t arg14;
826 uint64_t arg15;
827 uint64_t arg16;
828 uint64_t arg17;
829 bool valid;
830 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100831};
832
Olivier Depreze562e542020-06-11 17:31:54 +0200833static inline uint32_t ffa_func_id(struct ffa_value args)
834{
835 return args.func;
836}
837
Karl Meakindc759f52024-05-07 16:08:02 +0100838static inline enum ffa_error ffa_error_code(struct ffa_value val)
J-Alves13318e32021-02-22 17:21:00 +0000839{
Karl Meakin66a38bd2024-05-28 16:00:56 +0100840 /* NOLINTNEXTLINE(EnumCastOutOfRange) */
Karl Meakindc759f52024-05-07 16:08:02 +0100841 return (enum ffa_error)val.arg2;
J-Alves13318e32021-02-22 17:21:00 +0000842}
843
J-Alves19e20cf2023-08-02 12:48:55 +0100844static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100845{
846 return (args.arg1 >> 16) & 0xffff;
847}
848
J-Alves19e20cf2023-08-02 12:48:55 +0100849static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100850{
851 return args.arg1 & 0xffff;
852}
853
854static inline uint32_t ffa_msg_send_size(struct ffa_value args)
855{
856 return args.arg3;
857}
858
Federico Recanati25053ee2022-03-14 15:01:53 +0100859static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
860{
861 return args.arg2;
862}
863
Olivier Depreze562e542020-06-11 17:31:54 +0200864static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
865{
866 return args.arg2;
867}
868
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800869static inline uint16_t ffa_partition_info_regs_get_last_idx(
870 struct ffa_value args)
871{
872 return args.arg2 & 0xFFFF;
873}
874
875static inline uint16_t ffa_partition_info_regs_get_curr_idx(
876 struct ffa_value args)
877{
878 return (args.arg2 >> 16) & 0xFFFF;
879}
880
881static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
882{
883 return (args.arg2 >> 32) & 0xFFFF;
884}
885
886static inline uint16_t ffa_partition_info_regs_get_desc_size(
887 struct ffa_value args)
888{
889 return (args.arg2 >> 48);
890}
891
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100892static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
893{
894 return (uint64_t)a1 | (uint64_t)a2 << 32;
895}
896
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100897static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
898{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100899 return ffa_assemble_handle(args.arg2, args.arg3);
900}
901
Andrew Walbranca808b12020-05-15 17:22:28 +0100902static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
903{
904 return ffa_assemble_handle(args.arg1, args.arg2);
905}
906
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100907static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
908{
909 return (struct ffa_value){.func = FFA_SUCCESS_32,
910 .arg2 = (uint32_t)handle,
911 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912}
913
J-Alves19e20cf2023-08-02 12:48:55 +0100914static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100915{
916 return (args.arg1 >> 16) & 0xffff;
917}
918
919static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
920{
921 return args.arg1 & 0xffff;
922}
923
J-Alves19e20cf2023-08-02 12:48:55 +0100924static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100925{
926 return ((uint32_t)vm_id << 16) | vcpu_index;
927}
928
J-Alves19e20cf2023-08-02 12:48:55 +0100929static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100930{
931 return (args.arg4 >> 16) & 0xffff;
932}
933
J-Alves6f72ca82021-11-01 12:34:58 +0000934static inline uint32_t ffa_feature_intid(struct ffa_value args)
935{
936 return (uint32_t)args.arg2;
937}
938
Karl Meakind0356f82024-09-04 13:34:31 +0100939#define FFA_FRAMEWORK_MSG_BIT (UINT64_C(1) << 31)
940#define FFA_FRAMEWORK_MSG_FUNC_MASK UINT64_C(0xFF)
941
942/**
Madhukar Pappireddy8afab732025-02-10 09:39:36 -0600943 * Identifies FF-A framework messages. See sections 18.2 and 18.3 of v1.2 FF-A
Karl Meakind0356f82024-09-04 13:34:31 +0100944 * specification.
945 */
946enum ffa_framework_msg_func {
Madhukar Pappireddy8afab732025-02-10 09:39:36 -0600947 /* Power management framework messages. */
948 FFA_FRAMEWORK_MSG_PSCI_REQ = 0,
949 FFA_FRAMEWORK_MSG_PSCI_RESP = 2,
950
951 /* The VM availability messages. */
Karl Meakind0356f82024-09-04 13:34:31 +0100952 FFA_FRAMEWORK_MSG_VM_CREATION_REQ = 4,
953 FFA_FRAMEWORK_MSG_VM_CREATION_RESP = 5,
Karl Meakind0356f82024-09-04 13:34:31 +0100954 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_REQ = 6,
955 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_RESP = 7,
Madhukar Pappireddy8afab732025-02-10 09:39:36 -0600956
957 SPMD_FRAMEWORK_MSG_FFA_VERSION_REQ = 8,
958 SPMD_FRAMEWORK_MSG_FFA_VERSION_RESP = 9,
959
960 FFA_FRAMEWORK_MSG_INVALID = 0xFF,
Karl Meakind0356f82024-09-04 13:34:31 +0100961};
962
Karl Meakin06e8b732024-09-20 18:26:49 +0100963#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_LO 16
964#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_HI 31
965
Karl Meakind0356f82024-09-04 13:34:31 +0100966/** Get the `flags` field of a framework message */
967static inline uint32_t ffa_framework_msg_flags(struct ffa_value args)
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000968{
969 return (uint32_t)args.arg2;
970}
971
Karl Meakind0356f82024-09-04 13:34:31 +0100972/** Is `args` a framework message? */
973static inline bool ffa_is_framework_msg(struct ffa_value args)
974{
Karl Meakin06e8b732024-09-20 18:26:49 +0100975 return (args.func != FFA_MSG_SEND_DIRECT_REQ2_64) &&
976 (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) &&
977 ((ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_BIT) != 0);
Karl Meakind0356f82024-09-04 13:34:31 +0100978}
979
Karl Meakina1a02352024-09-20 18:27:18 +0100980/**
981 * Get the ID of the VM that has been created/destroyed from VM availability
982 * message
983 */
984static inline ffa_id_t ffa_vm_availability_message_vm_id(struct ffa_value args)
985{
986 return args.arg5 & 0xFFFF;
987}
988
Karl Meakind0356f82024-09-04 13:34:31 +0100989/** Get the function ID from a framework message */
Madhukar Pappireddy984e99a2025-02-10 09:55:27 -0600990static inline uint32_t ffa_framework_msg_get_func(struct ffa_value args)
Karl Meakind0356f82024-09-04 13:34:31 +0100991{
992 return ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_FUNC_MASK;
993}
994
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100995/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100996 * Flags to determine the partition properties, as required by
997 * FFA_PARTITION_INFO_GET.
998 *
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400999 * The values of the flags are specified in table 6.2 of DEN0077A FF-A 1.2 ALP0
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001000 * specification, "Partition information descriptor, partition properties".
1001 */
1002typedef uint32_t ffa_partition_properties_t;
1003
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001004/**
1005 * Partition property: partition supports receipt of direct requests via the
1006 * FFA_MSG_SEND_DIRECT_REQ ABI.
1007 */
Kathleen Capella402fa852022-11-09 16:16:51 -05001008#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001009
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001010/**
1011 * Partition property: partition can send direct requests via the
1012 * FFA_MSG_SEND_DIRECT_REQ ABI.
1013 */
Kathleen Capella402fa852022-11-09 16:16:51 -05001014#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001015
1016/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -05001017#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001018
J-Alves09ff9d82021-11-02 11:55:20 +00001019/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -05001020#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
1021
Karl Meakina603e082024-08-02 17:57:27 +01001022/**
1023 * Partition property: partition must be informed about each VM that is created
1024 * by the Hypervisor.
1025 */
1026#define FFA_PARTITION_VM_CREATED (UINT32_C(1) << 6)
1027
1028/**
1029 * Partition property: partition must be informed about each VM that is
1030 * destroyed by the Hypervisor.
1031 */
1032#define FFA_PARTITION_VM_DESTROYED (UINT32_C(1) << 7)
1033
Kathleen Capella402fa852022-11-09 16:16:51 -05001034/** Partition property: partition runs in the AArch64 execution state. */
1035#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +00001036
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001037/**
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001038 * Partition property: partition supports receipt of direct requests via the
1039 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
1040 */
1041#define FFA_PARTITION_DIRECT_REQ2_RECV (UINT32_C(1) << 9)
1042
1043/**
1044 * Partition property: partition can send direct requests via the
1045 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
1046 */
1047#define FFA_PARTITION_DIRECT_REQ2_SEND (UINT32_C(1) << 10)
1048
1049/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001050 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
1051 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -05001052 * This corresponds to table 13.37 "Partition information descriptor"
1053 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +00001054 */
1055struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +01001056 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +00001057 ffa_vcpu_count_t vcpu_count;
1058 ffa_partition_properties_t properties;
1059 struct ffa_uuid uuid;
1060};
1061
J-Alvesdd1ad572022-01-25 17:58:26 +00001062/** Length in bytes of the name in boot information descriptor. */
1063#define FFA_BOOT_INFO_NAME_LEN 16
1064
J-Alves240d84c2022-04-22 12:19:34 +01001065/**
1066 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
1067 * the FF-A v1.1 EAC0 specification.
1068 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001069struct ffa_boot_info_desc {
1070 char name[FFA_BOOT_INFO_NAME_LEN];
1071 uint8_t type;
1072 uint8_t reserved;
1073 uint16_t flags;
1074 uint32_t size;
1075 uint64_t content;
1076};
1077
1078/** FF-A boot information type mask. */
1079#define FFA_BOOT_INFO_TYPE_SHIFT 7
1080#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
1081#define FFA_BOOT_INFO_TYPE_STD 0U
1082#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
1083
1084/** Standard boot info type IDs. */
1085#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
1086#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
1087#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
1088
1089/** FF-A Boot Info descriptors flags. */
1090#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
1091
1092/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
1093#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
1094#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
1095 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
1096#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
1097#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
1098
1099/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
1100#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
1101#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
1102 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
1103#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
1104#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
1105
1106static inline uint16_t ffa_boot_info_content_format(
1107 struct ffa_boot_info_desc *desc)
1108{
1109 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
1110 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
1111}
1112
1113static inline uint16_t ffa_boot_info_name_format(
1114 struct ffa_boot_info_desc *desc)
1115{
1116 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
1117 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
1118}
1119
1120static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
1121{
1122 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
1123}
1124
1125static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
1126{
1127 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
1128 FFA_BOOT_INFO_TYPE_SHIFT;
1129}
1130
1131/** Length in bytes of the signature in the boot descriptor. */
1132#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
1133
J-Alves240d84c2022-04-22 12:19:34 +01001134/**
1135 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
1136 * of the FF-A v1.1 EAC0 specification.
1137 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001138struct ffa_boot_info_header {
1139 uint32_t signature;
1140 uint32_t version;
1141 uint32_t info_blob_size;
1142 uint32_t desc_size;
1143 uint32_t desc_count;
1144 uint32_t desc_offset;
1145 uint64_t reserved;
1146 struct ffa_boot_info_desc boot_info[];
1147};
1148
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001149/**
J-Alves980d1992021-03-18 12:49:18 +00001150 * FF-A v1.1 specification restricts the number of notifications to a maximum
1151 * of 64. Following all possible bitmaps.
1152 */
Karl Meakin2ad6b662024-07-29 20:45:40 +01001153#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << (ID))
J-Alves980d1992021-03-18 12:49:18 +00001154
1155typedef uint64_t ffa_notifications_bitmap_t;
1156
1157#define MAX_FFA_NOTIFICATIONS 64U
1158
1159/**
J-Alvesc003a7a2021-03-18 13:06:53 +00001160 * Flag for notification bind and set, to specify call is about per-vCPU
1161 * notifications.
1162 */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001163#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +00001164
Federico Recanatie73d2832022-04-20 11:10:52 +02001165#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
1166#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
1167
1168/**
1169 * Helper functions to check for buffer full notification.
1170 */
1171static inline bool is_ffa_hyp_buffer_full_notification(
1172 ffa_notifications_bitmap_t framework)
1173{
1174 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
1175}
1176
1177static inline bool is_ffa_spm_buffer_full_notification(
1178 ffa_notifications_bitmap_t framework)
1179{
1180 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
1181}
1182
J-Alvesc003a7a2021-03-18 13:06:53 +00001183/**
J-Alves980d1992021-03-18 12:49:18 +00001184 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
1185 * and hi.
1186 * Helpful as FF-A specification defines that the notifications interfaces
1187 * arguments are 32-bit registers.
1188 */
1189static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
1190 uint32_t hi)
1191{
1192 return (ffa_notifications_bitmap_t)hi << 32U | lo;
1193}
1194
J-Alves98ff9562021-09-09 14:39:41 +01001195static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
1196 struct ffa_value val)
1197{
1198 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
1199}
1200
1201static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
1202 struct ffa_value val)
1203{
1204 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
1205}
1206
Federico Recanatie73d2832022-04-20 11:10:52 +02001207static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
1208 struct ffa_value val)
1209{
1210 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
1211}
1212
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001213typedef uint32_t ffa_notification_flags_t;
1214
1215/** Flags used in calls to FFA_NOTIFICATION_BIND interface. */
1216#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
1217
1218/** Flags used in calls to FFA_NOTIFICATION_GET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001219#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
1220#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
1221#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
1222#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +01001223
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001224/** Flags used in calls to FFA_NOTIFICATION_SET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001225#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001226#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001227#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
1228 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001229#define FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(flags) \
1230 ((ffa_vcpu_index_t)((flags) >> 16))
J-Alves13394022021-06-30 13:48:49 +01001231
J-Alvesbe6e3032021-11-30 14:54:12 +00001232static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +01001233{
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001234 return FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(args.arg1);
J-Alvesaa79c012021-07-09 14:29:45 +01001235}
1236
1237/**
J-Alvesc8e8a222021-06-08 17:33:52 +01001238 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
1239 */
1240#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
1241
1242/**
1243 * Number of registers to use in successfull return of interface
1244 * FFA_NOTIFICATION_INFO_GET.
1245 */
1246#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
1247
1248#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
1249
1250/**
1251 * Helper macros for return parameter encoding as described in section 17.7.1
1252 * of the FF-A v1.1 Beta0 specification.
1253 */
1254#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
1255#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
Karl Meakin2ad6b662024-07-29 20:45:40 +01001256#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * ((l) - 1) + 12)
J-Alvesc8e8a222021-06-08 17:33:52 +01001257#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
Daniel Boulby1308a632024-09-11 15:19:16 +01001258#define FFA_NOTIFICATIONS_LIST_MAX_SIZE 0x4U
Daniel Boulbyedfdf282025-01-02 18:51:19 +00001259#define FFA_NOTIFICATIONS_LIST_MAX_VCPU_IDS \
1260 (FFA_NOTIFICATIONS_LIST_MAX_SIZE - 1)
J-Alvesc8e8a222021-06-08 17:33:52 +01001261
1262static inline uint32_t ffa_notification_info_get_lists_count(
1263 struct ffa_value args)
1264{
1265 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
1266 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
1267}
1268
1269static inline uint32_t ffa_notification_info_get_list_size(
1270 struct ffa_value args, unsigned int list_idx)
1271{
1272 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
1273 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
1274}
1275
1276static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
1277{
1278 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
1279}
1280
Daniel Boulby1308a632024-09-11 15:19:16 +01001281void ffa_notification_info_get_and_check(
1282 const uint32_t expected_lists_count,
1283 const uint32_t *const expected_lists_sizes,
1284 const uint16_t *const expected_ids);
1285
J-Alvesc8e8a222021-06-08 17:33:52 +01001286/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001287 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +01001288 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
1289 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001290 */
1291struct ffa_memory_region_constituent {
1292 /**
1293 * The base IPA of the constituent memory region, aligned to 4 kiB page
1294 * size granularity.
1295 */
1296 uint64_t address;
1297 /** The number of 4 kiB pages in the constituent memory region. */
1298 uint32_t page_count;
1299 /** Reserved field, must be 0. */
1300 uint32_t reserved;
1301};
1302
1303/**
J-Alves0b6653d2022-04-22 13:17:38 +01001304 * A set of pages comprising a memory region. This corresponds to table 10.13 of
1305 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001306 */
1307struct ffa_composite_memory_region {
1308 /**
1309 * The total number of 4 kiB pages included in this memory region. This
1310 * must be equal to the sum of page counts specified in each
1311 * `ffa_memory_region_constituent`.
1312 */
1313 uint32_t page_count;
1314 /**
1315 * The number of constituents (`ffa_memory_region_constituent`)
1316 * included in this memory region range.
1317 */
1318 uint32_t constituent_count;
1319 /** Reserved field, must be 0. */
1320 uint64_t reserved_0;
1321 /** An array of `constituent_count` memory region constituents. */
1322 struct ffa_memory_region_constituent constituents[];
1323};
1324
1325/** Flags to indicate properties of receivers during memory region retrieval. */
1326typedef uint8_t ffa_memory_receiver_flags_t;
1327
1328/**
J-Alves0b6653d2022-04-22 13:17:38 +01001329 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001330 * access permissions descriptor".
1331 */
1332struct ffa_memory_region_attributes {
1333 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001334 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001335 /**
1336 * The permissions with which the memory region should be mapped in the
1337 * receiver's page table.
1338 */
1339 ffa_memory_access_permissions_t permissions;
1340 /**
1341 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1342 * for memory regions with multiple borrowers.
1343 */
1344 ffa_memory_receiver_flags_t flags;
1345};
1346
1347/** Flags to control the behaviour of a memory sharing transaction. */
1348typedef uint32_t ffa_memory_region_flags_t;
1349
1350/**
1351 * Clear memory region contents after unmapping it from the sender and before
1352 * mapping it for any receiver.
1353 */
1354#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1355
1356/**
1357 * Whether the hypervisor may time slice the memory sharing or retrieval
1358 * operation.
1359 */
1360#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1361
1362/**
1363 * Whether the hypervisor should clear the memory region after the receiver
1364 * relinquishes it or is aborted.
1365 */
1366#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1367
J-Alves3456e032023-07-20 12:20:05 +01001368/**
1369 * On retrieve request, bypass the multi-borrower check.
1370 */
1371#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1372
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1374#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1375#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1376#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1377#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1378
Federico Recanati85090c42021-12-15 13:17:54 +01001379#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1380#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1381
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001382/**
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001383 * Struct to store the impdef value seen in Table 11.16 of the
1384 * FF-A v1.2 ALP0 specification "Endpoint memory access descriptor".
1385 */
1386struct ffa_memory_access_impdef {
1387 uint64_t val[2];
1388};
1389
Daniel Boulbye0ca9a02024-03-05 18:40:59 +00001390static inline struct ffa_memory_access_impdef ffa_memory_access_impdef_init(
1391 uint64_t impdef_hi, uint64_t impdef_lo)
1392{
1393 return (struct ffa_memory_access_impdef){{impdef_hi, impdef_lo}};
1394}
1395
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001396/**
J-Alves0b6653d2022-04-22 13:17:38 +01001397 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1398 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001399 */
1400struct ffa_memory_access {
1401 struct ffa_memory_region_attributes receiver_permissions;
1402 /**
1403 * Offset in bytes from the start of the outer `ffa_memory_region` to
1404 * an `ffa_composite_memory_region` struct.
1405 */
1406 uint32_t composite_memory_region_offset;
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001407 struct ffa_memory_access_impdef impdef;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001408 uint64_t reserved_0;
1409};
1410
J-Alves363f5722022-04-25 17:37:37 +01001411/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001412#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001413
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001414/**
1415 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001416 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1417 * memory transaction descriptor". Note that it is also used for retrieve
1418 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001419 */
1420struct ffa_memory_region {
1421 /**
1422 * The ID of the VM which originally sent the memory region, i.e. the
1423 * owner.
1424 */
J-Alves19e20cf2023-08-02 12:48:55 +01001425 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001426 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 /** Flags to control behaviour of the transaction. */
1428 ffa_memory_region_flags_t flags;
1429 ffa_memory_handle_t handle;
1430 /**
1431 * An implementation defined value associated with the receiver and the
1432 * memory region.
1433 */
1434 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001435 /* Size of the memory access descriptor. */
1436 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001437 /**
1438 * The number of `ffa_memory_access` entries included in this
1439 * transaction.
1440 */
1441 uint32_t receiver_count;
1442 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001443 * Offset of the 'receivers' field, which relates to the memory access
1444 * descriptors.
1445 */
1446 uint32_t receivers_offset;
1447 /** Reserved field (12 bytes) must be 0. */
1448 uint32_t reserved[3];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001449};
1450
1451/**
1452 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001453 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001454 * region".
1455 */
1456struct ffa_mem_relinquish {
1457 ffa_memory_handle_t handle;
1458 ffa_memory_region_flags_t flags;
1459 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001460 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001461};
1462
1463/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001464 * Returns the first FF-A version that matches the memory access descriptor
1465 * size.
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001466 */
Karl Meakin0e617d92024-04-05 12:55:22 +01001467enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01001468 uint32_t memory_access_desc_size);
1469
1470/**
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001471 * To maintain forwards compatability we can't make assumptions about the size
1472 * of the endpoint memory access descriptor so provide a helper function
1473 * to get a receiver from the receiver array using the memory access descriptor
1474 * size field from the memory region descriptor struct.
1475 * Returns NULL if we cannot return the receiver.
1476 */
1477static inline struct ffa_memory_access *ffa_memory_region_get_receiver(
1478 struct ffa_memory_region *memory_region, uint32_t receiver_index)
1479{
1480 uint32_t memory_access_desc_size =
1481 memory_region->memory_access_desc_size;
1482
1483 if (receiver_index >= memory_region->receiver_count) {
1484 return NULL;
1485 }
1486
1487 /*
1488 * Memory access descriptor size cannot be greater than the size of
1489 * the memory access descriptor defined by the current FF-A version.
1490 */
1491 if (memory_access_desc_size > sizeof(struct ffa_memory_access)) {
1492 return NULL;
1493 }
1494
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001495 /* Check we cannot use receivers offset to cause overflow. */
1496 if (memory_region->receivers_offset !=
1497 sizeof(struct ffa_memory_region)) {
1498 return NULL;
1499 }
1500
Karl Meakin2ad6b662024-07-29 20:45:40 +01001501 return (struct ffa_memory_access
1502 *)((uint8_t *)memory_region +
1503 (size_t)memory_region->receivers_offset +
1504 (size_t)(receiver_index * memory_access_desc_size));
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001505}
1506
1507/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001508 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1509 * returns its index in the receiver's array. If receiver's ID doesn't exist
1510 * in the array, return the region's 'receivers_count'.
1511 */
1512static inline uint32_t ffa_memory_region_get_receiver_index(
1513 struct ffa_memory_region *memory_region, ffa_id_t receiver_id)
1514{
1515 uint32_t i;
1516
1517 for (i = 0U; i < memory_region->receiver_count; i++) {
1518 struct ffa_memory_access *receiver =
1519 ffa_memory_region_get_receiver(memory_region, i);
1520 if (receiver->receiver_permissions.receiver == receiver_id) {
1521 break;
1522 }
1523 }
1524
1525 return i;
1526}
1527
1528/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001529 * Gets the `ffa_composite_memory_region` for the given receiver from an
1530 * `ffa_memory_region`, or NULL if it is not valid.
1531 */
1532static inline struct ffa_composite_memory_region *
1533ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1534 uint32_t receiver_index)
1535{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001536 struct ffa_memory_access *receiver =
1537 ffa_memory_region_get_receiver(memory_region, receiver_index);
1538 uint32_t offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001539
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001540 if (receiver == NULL) {
1541 return NULL;
1542 }
1543
1544 offset = receiver->composite_memory_region_offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001545 if (offset == 0) {
1546 return NULL;
1547 }
1548
1549 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1550 offset);
1551}
1552
1553static inline uint32_t ffa_mem_relinquish_init(
1554 struct ffa_mem_relinquish *relinquish_request,
1555 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001556 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001557{
1558 relinquish_request->handle = handle;
1559 relinquish_request->flags = flags;
1560 relinquish_request->endpoint_count = 1;
1561 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001562 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001563}
1564
J-Alves126ab502022-09-29 11:37:33 +01001565void ffa_copy_memory_region_constituents(
1566 struct ffa_memory_region_constituent *dest,
1567 const struct ffa_memory_region_constituent *src);
1568
Karl Meakin0fd67292024-02-06 17:33:05 +00001569struct ffa_features_rxtx_map_params {
1570 /*
1571 * Bit[0:1]:
1572 * Minimum buffer size and alignment boundary:
1573 * 0b00: 4K
1574 * 0b01: 64K
1575 * 0b10: 16K
1576 * 0b11: Reserved
1577 */
1578 uint8_t min_buf_size : 2;
1579 /*
1580 * Bit[2:15]:
1581 * Reserved (MBZ)
1582 */
1583 uint16_t mbz : 14;
1584 /*
1585 * Bit[16:32]:
1586 * Maximum buffer size in number of pages
1587 * Only present on version 1.2 or later
1588 */
1589 uint16_t max_buf_size : 16;
1590};
1591
Karl Meakin49ec1e42024-05-10 13:08:24 +01001592enum ffa_features_rxtx_map_buf_size {
1593 FFA_RXTX_MAP_MIN_BUF_4K = 0,
1594 FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT = 1,
1595};
Karl Meakin0fd67292024-02-06 17:33:05 +00001596
Karl Meakinf7861302024-02-20 14:39:33 +00001597static inline struct ffa_features_rxtx_map_params ffa_features_rxtx_map_params(
1598 struct ffa_value args)
1599{
1600 struct ffa_features_rxtx_map_params params;
1601 uint32_t arg2 = args.arg2;
1602
1603 params = *(struct ffa_features_rxtx_map_params *)(&arg2);
1604
1605 return params;
1606}
1607
Federico Recanati392be392022-02-08 20:53:03 +01001608/**
1609 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1610 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1611 * to the SPMC, in order to allow indirect messaging.
1612 */
1613struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001614 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001615 uint16_t reserved;
1616
1617 /*
1618 * 8-byte aligned offset from the base address of this descriptor to the
1619 * `ffa_composite_memory_region` describing the RX buffer.
1620 */
1621 uint32_t rx_offset;
1622
1623 /*
1624 * 8-byte aligned offset from the base address of this descriptor to the
1625 * `ffa_composite_memory_region` describing the TX buffer.
1626 */
1627 uint32_t tx_offset;
1628
1629 /* Pad to align on 16-byte boundary. */
1630 uint32_t pad;
1631};
1632
1633static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001634ffa_endpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001635{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001636 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001637 desc->rx_offset);
1638}
1639
1640static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001641ffa_endpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001642{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001643 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001644 desc->tx_offset);
1645}
1646
J-Alves2d8457f2022-10-05 11:06:41 +01001647void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001648 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001649 ffa_memory_attributes_t attributes,
1650 ffa_memory_region_flags_t flags,
1651 ffa_memory_handle_t handle, uint32_t tag,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001652 uint32_t receiver_count,
1653 uint32_t receiver_desc_size);
Daniel Boulby59ffee92023-11-02 18:26:26 +00001654void ffa_memory_access_init(struct ffa_memory_access *receiver,
1655 ffa_id_t receiver_id,
1656 enum ffa_data_access data_access,
1657 enum ffa_instruction_access instruction_access,
1658 ffa_memory_receiver_flags_t flags,
1659 struct ffa_memory_access_impdef *impdef_val);
J-Alves45085432022-04-22 16:19:20 +01001660uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001661 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001662 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001663 const struct ffa_memory_region_constituent constituents[],
1664 uint32_t constituent_count, uint32_t tag,
1665 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1666 enum ffa_instruction_access instruction_access,
1667 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001668 enum ffa_memory_shareability shareability,
1669 struct ffa_memory_access_impdef *impdef_val, uint32_t *fragment_length,
Andrew Walbranca808b12020-05-15 17:22:28 +01001670 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001671uint32_t ffa_memory_region_init(
1672 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001673 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001674 uint32_t receiver_count, uint32_t receiver_desc_size,
J-Alvesf4eecf72022-07-20 16:05:34 +01001675 const struct ffa_memory_region_constituent constituents[],
1676 uint32_t constituent_count, uint32_t tag,
1677 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1678 enum ffa_memory_cacheability cacheability,
1679 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1680 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001681uint32_t ffa_memory_retrieve_request_init(
1682 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001683 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001684 uint32_t receiver_count, uint32_t receiver_desc_size, uint32_t tag,
1685 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1686 enum ffa_memory_cacheability cacheability,
J-Alves9b24ed82022-08-04 13:12:45 +01001687 enum ffa_memory_shareability shareability);
1688uint32_t ffa_memory_retrieve_request_init_single_receiver(
1689 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001690 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001691 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1692 enum ffa_instruction_access instruction_access,
1693 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001694 enum ffa_memory_shareability shareability,
1695 struct ffa_memory_access_impdef *impdef_val);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001696uint32_t ffa_memory_lender_retrieve_request_init(
1697 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001698 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001699uint32_t ffa_memory_fragment_init(
1700 struct ffa_memory_region_constituent *fragment,
1701 size_t fragment_max_size,
1702 const struct ffa_memory_region_constituent constituents[],
1703 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001704void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001705 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001706 uint64_t rx_address, uint64_t tx_address);