blob: 3fb2b27166c754111484df68b8170eb4fbce5a11 [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
Karl Meakinbf580c22024-12-12 14:31:06 +000011#include "hf/static_assert.h"
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010012#include "hf/types.h"
13
Karl Meakin0e617d92024-04-05 12:55:22 +010014/**
15 * The version number of a Firmware Framework implementation is a 31-bit
16 * unsigned integer, with the upper 15 bits denoting the major revision,
17 * and the lower 16 bits denoting the minor revision.
18 *
19 * See FF-A specification v1.2 ALP1, section 13.2.1.
20 */
21enum ffa_version {
22 FFA_VERSION_1_0 = 0x10000,
23 FFA_VERSION_1_1 = 0x10001,
24 FFA_VERSION_1_2 = 0x10002,
Karl Meakin59352b32025-02-25 12:19:45 +000025/*
26 * Use the value of `FFA_VERSION` passed by the build system, otherwise default
27 * to latest FF-A version.
28 */
29#ifdef FFA_VERSION
30 FFA_VERSION_COMPILED = FFA_VERSION,
31#else
Karl Meakin0e617d92024-04-05 12:55:22 +010032 FFA_VERSION_COMPILED = FFA_VERSION_1_2,
Karl Meakin59352b32025-02-25 12:19:45 +000033#endif
Karl Meakin0e617d92024-04-05 12:55:22 +010034};
Daniel Boulby6e32c612021-02-17 15:09:41 +000035
Karl Meakin59352b32025-02-25 12:19:45 +000036static_assert((FFA_VERSION_1_0 <= FFA_VERSION_COMPILED) &&
37 (FFA_VERSION_1_2 >= FFA_VERSION_COMPILED),
38 "FFA_VERSION_COMPILED must be between v1.0 and v1.2");
39
Karl Meakin0e617d92024-04-05 12:55:22 +010040#define FFA_VERSION_MBZ_BIT (1U << 31U)
41#define FFA_VERSION_MAJOR_SHIFT (16U)
42#define FFA_VERSION_MAJOR_MASK (0x7FFFU)
43#define FFA_VERSION_MINOR_SHIFT (0U)
44#define FFA_VERSION_MINOR_MASK (0xFFFFU)
45
46/** Return true if the version is valid (i.e. bit 31 is 0). */
47static inline bool ffa_version_is_valid(uint32_t version)
48{
49 return (version & FFA_VERSION_MBZ_BIT) == 0;
50}
51
52/** Construct a version from a pair of major and minor components. */
53static inline enum ffa_version make_ffa_version(uint16_t major, uint16_t minor)
54{
55 return (enum ffa_version)((major << FFA_VERSION_MAJOR_SHIFT) |
56 (minor << FFA_VERSION_MINOR_SHIFT));
57}
58
59/** Get the major component of the version. */
60static inline uint16_t ffa_version_get_major(enum ffa_version version)
61{
62 return (version >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
63}
64
65/** Get the minor component of the version. */
66static inline uint16_t ffa_version_get_minor(enum ffa_version version)
67{
68 return (version >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK;
69}
Olivier Deprez62d99e32020-01-09 15:58:07 +010070
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010071/**
72 * Check major versions are equal and the minor version of the caller is
73 * less than or equal to the minor version of the callee.
74 */
Karl Meakin0e617d92024-04-05 12:55:22 +010075static inline bool ffa_versions_are_compatible(enum ffa_version caller,
76 enum ffa_version callee)
77{
78 return ffa_version_get_major(caller) == ffa_version_get_major(callee) &&
79 ffa_version_get_minor(caller) <= ffa_version_get_minor(callee);
80}
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010081
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082/* clang-format off */
83
84#define FFA_LOW_32_ID 0x84000060
85#define FFA_HIGH_32_ID 0x8400007F
86#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010087#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010088
Karl Meakinf51a35f2023-08-07 17:53:52 +010089/**
90 * FF-A function identifiers.
91 * Don't forget to update `ffa_func_name` if you add a new one.
92 */
J-Alves3829fc02021-03-18 12:49:18 +000093#define FFA_ERROR_32 0x84000060
94#define FFA_SUCCESS_32 0x84000061
95#define FFA_SUCCESS_64 0xC4000061
96#define FFA_INTERRUPT_32 0x84000062
97#define FFA_VERSION_32 0x84000063
98#define FFA_FEATURES_32 0x84000064
99#define FFA_RX_RELEASE_32 0x84000065
100#define FFA_RXTX_MAP_32 0x84000066
101#define FFA_RXTX_MAP_64 0xC4000066
102#define FFA_RXTX_UNMAP_32 0x84000067
103#define FFA_PARTITION_INFO_GET_32 0x84000068
104#define FFA_ID_GET_32 0x84000069
105#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
106#define FFA_MSG_WAIT_32 0x8400006B
107#define FFA_YIELD_32 0x8400006C
108#define FFA_RUN_32 0x8400006D
109#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
110#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
111#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
112#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
113#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
114#define FFA_MEM_DONATE_32 0x84000071
J-Alves95fbb312024-03-20 15:19:16 +0000115#define FFA_MEM_DONATE_64 0xC4000071
J-Alves3829fc02021-03-18 12:49:18 +0000116#define FFA_MEM_LEND_32 0x84000072
J-Alves95fbb312024-03-20 15:19:16 +0000117#define FFA_MEM_LEND_64 0xC4000072
J-Alves3829fc02021-03-18 12:49:18 +0000118#define FFA_MEM_SHARE_32 0x84000073
J-Alves95fbb312024-03-20 15:19:16 +0000119#define FFA_MEM_SHARE_64 0xC4000073
J-Alves3829fc02021-03-18 12:49:18 +0000120#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
J-Alves95fbb312024-03-20 15:19:16 +0000121#define FFA_MEM_RETRIEVE_REQ_64 0xC4000074
J-Alves3829fc02021-03-18 12:49:18 +0000122#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
123#define FFA_MEM_RELINQUISH_32 0x84000076
124#define FFA_MEM_RECLAIM_32 0x84000077
125#define FFA_MEM_FRAG_RX_32 0x8400007A
126#define FFA_MEM_FRAG_TX_32 0x8400007B
127#define FFA_NORMAL_WORLD_RESUME 0x8400007C
128
129/* FF-A v1.1 */
130#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
131#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
132#define FFA_NOTIFICATION_BIND_32 0x8400007F
133#define FFA_NOTIFICATION_UNBIND_32 0x84000080
134#define FFA_NOTIFICATION_SET_32 0x84000081
135#define FFA_NOTIFICATION_GET_32 0x84000082
136#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
137#define FFA_RX_ACQUIRE_32 0x84000084
138#define FFA_SPM_ID_GET_32 0x84000085
139#define FFA_MSG_SEND2_32 0x84000086
140#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
141#define FFA_MEM_PERM_GET_32 0x84000088
142#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700143#define FFA_MEM_PERM_GET_64 0xC4000088
144#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100145
Kathleen Capellae4fe2962023-09-01 17:08:47 -0400146/* FF-A v1.2 */
Maksims Svecovs71b76702022-05-20 15:32:58 +0100147#define FFA_CONSOLE_LOG_32 0x8400008A
148#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800149#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -0600150#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Kathleen Capella41fea932023-06-23 17:39:28 -0400151#define FFA_MSG_SEND_DIRECT_REQ2_64 0xC400008D
Kathleen Capella087e5022023-09-07 18:04:15 -0400152#define FFA_MSG_SEND_DIRECT_RESP2_64 0xC400008E
Maksims Svecovs71b76702022-05-20 15:32:58 +0100153
Karl Meakinf51a35f2023-08-07 17:53:52 +0100154/**
155 * FF-A error codes.
156 * Don't forget to update `ffa_error_name` if you add a new one.
157 */
Karl Meakindc759f52024-05-07 16:08:02 +0100158enum ffa_error {
159 FFA_NOT_SUPPORTED = -1,
160 FFA_INVALID_PARAMETERS = -2,
161 FFA_NO_MEMORY = -3,
162 FFA_BUSY = -4,
163 FFA_INTERRUPTED = -5,
164 FFA_DENIED = -6,
165 FFA_RETRY = -7,
166 FFA_ABORTED = -8,
167 FFA_NO_DATA = -9,
168 FFA_NOT_READY = -10,
169};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100170
171/* clang-format on */
172
Karl Meakinf51a35f2023-08-07 17:53:52 +0100173/* Return the name of the function identifier. */
174static inline const char *ffa_func_name(uint32_t func)
175{
176 switch (func) {
177 case FFA_ERROR_32:
178 return "FFA_ERROR_32";
179 case FFA_SUCCESS_32:
180 return "FFA_SUCCESS_32";
181 case FFA_SUCCESS_64:
182 return "FFA_SUCCESS_64";
183 case FFA_INTERRUPT_32:
184 return "FFA_INTERRUPT_32";
185 case FFA_VERSION_32:
186 return "FFA_VERSION_32";
187 case FFA_FEATURES_32:
188 return "FFA_FEATURES_32";
189 case FFA_RX_RELEASE_32:
190 return "FFA_RX_RELEASE_32";
191 case FFA_RXTX_MAP_32:
192 return "FFA_RXTX_MAP_32";
193 case FFA_RXTX_MAP_64:
194 return "FFA_RXTX_MAP_64";
195 case FFA_RXTX_UNMAP_32:
196 return "FFA_RXTX_UNMAP_32";
197 case FFA_PARTITION_INFO_GET_32:
198 return "FFA_PARTITION_INFO_GET_32";
199 case FFA_ID_GET_32:
200 return "FFA_ID_GET_32";
201 case FFA_MSG_POLL_32:
202 return "FFA_MSG_POLL_32";
203 case FFA_MSG_WAIT_32:
204 return "FFA_MSG_WAIT_32";
205 case FFA_YIELD_32:
206 return "FFA_YIELD_32";
207 case FFA_RUN_32:
208 return "FFA_RUN_32";
209 case FFA_MSG_SEND_32:
210 return "FFA_MSG_SEND_32";
211 case FFA_MSG_SEND_DIRECT_REQ_32:
212 return "FFA_MSG_SEND_DIRECT_REQ_32";
213 case FFA_MSG_SEND_DIRECT_REQ_64:
214 return "FFA_MSG_SEND_DIRECT_REQ_64";
215 case FFA_MSG_SEND_DIRECT_RESP_32:
216 return "FFA_MSG_SEND_DIRECT_RESP_32";
217 case FFA_MSG_SEND_DIRECT_RESP_64:
218 return "FFA_MSG_SEND_DIRECT_RESP_64";
219 case FFA_MEM_DONATE_32:
220 return "FFA_MEM_DONATE_32";
221 case FFA_MEM_LEND_32:
222 return "FFA_MEM_LEND_32";
223 case FFA_MEM_SHARE_32:
224 return "FFA_MEM_SHARE_32";
225 case FFA_MEM_RETRIEVE_REQ_32:
226 return "FFA_MEM_RETRIEVE_REQ_32";
J-Alves95fbb312024-03-20 15:19:16 +0000227 case FFA_MEM_DONATE_64:
228 return "FFA_MEM_DONATE_64";
229 case FFA_MEM_LEND_64:
230 return "FFA_MEM_LEND_64";
231 case FFA_MEM_SHARE_64:
232 return "FFA_MEM_SHARE_64";
233 case FFA_MEM_RETRIEVE_REQ_64:
234 return "FFA_MEM_RETRIEVE_REQ_64";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100235 case FFA_MEM_RETRIEVE_RESP_32:
236 return "FFA_MEM_RETRIEVE_RESP_32";
237 case FFA_MEM_RELINQUISH_32:
238 return "FFA_MEM_RELINQUISH_32";
239 case FFA_MEM_RECLAIM_32:
240 return "FFA_MEM_RECLAIM_32";
241 case FFA_MEM_FRAG_RX_32:
242 return "FFA_MEM_FRAG_RX_32";
243 case FFA_MEM_FRAG_TX_32:
244 return "FFA_MEM_FRAG_TX_32";
245 case FFA_NORMAL_WORLD_RESUME:
246 return "FFA_NORMAL_WORLD_RESUME";
247
248 /* FF-A v1.1 */
249 case FFA_NOTIFICATION_BITMAP_CREATE_32:
250 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
251 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
252 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
253 case FFA_NOTIFICATION_BIND_32:
254 return "FFA_NOTIFICATION_BIND_32";
255 case FFA_NOTIFICATION_UNBIND_32:
256 return "FFA_NOTIFICATION_UNBIND_32";
257 case FFA_NOTIFICATION_SET_32:
258 return "FFA_NOTIFICATION_SET_32";
259 case FFA_NOTIFICATION_GET_32:
260 return "FFA_NOTIFICATION_GET_32";
261 case FFA_NOTIFICATION_INFO_GET_64:
262 return "FFA_NOTIFICATION_INFO_GET_64";
263 case FFA_RX_ACQUIRE_32:
264 return "FFA_RX_ACQUIRE_32";
265 case FFA_SPM_ID_GET_32:
266 return "FFA_SPM_ID_GET_32";
267 case FFA_MSG_SEND2_32:
268 return "FFA_MSG_SEND2_32";
269 case FFA_SECONDARY_EP_REGISTER_64:
270 return "FFA_SECONDARY_EP_REGISTER_64";
271 case FFA_MEM_PERM_GET_32:
272 return "FFA_MEM_PERM_GET_32";
273 case FFA_MEM_PERM_SET_32:
274 return "FFA_MEM_PERM_SET_32";
275 case FFA_MEM_PERM_GET_64:
276 return "FFA_MEM_PERM_GET_64";
277 case FFA_MEM_PERM_SET_64:
278 return "FFA_MEM_PERM_SET_64";
279
280 /* Implementation-defined ABIs. */
281 case FFA_CONSOLE_LOG_32:
282 return "FFA_CONSOLE_LOG_32";
283 case FFA_CONSOLE_LOG_64:
284 return "FFA_CONSOLE_LOG_64";
285 case FFA_PARTITION_INFO_GET_REGS_64:
286 return "FFA_PARTITION_INFO_GET_REGS_64";
287 case FFA_EL3_INTR_HANDLE_32:
288 return "FFA_EL3_INTR_HANDLE_32";
289
290 default:
291 return "UNKNOWN";
292 }
293}
294
295/* Return the name of the error code. */
Karl Meakindc759f52024-05-07 16:08:02 +0100296static inline const char *ffa_error_name(enum ffa_error error)
Karl Meakinf51a35f2023-08-07 17:53:52 +0100297{
298 switch (error) {
299 case FFA_NOT_SUPPORTED:
300 return "FFA_NOT_SUPPORTED";
301 case FFA_INVALID_PARAMETERS:
302 return "FFA_INVALID_PARAMETERS";
303 case FFA_NO_MEMORY:
304 return "FFA_NO_MEMORY";
305 case FFA_BUSY:
306 return "FFA_BUSY";
307 case FFA_INTERRUPTED:
308 return "FFA_INTERRUPTED";
309 case FFA_DENIED:
310 return "FFA_DENIED";
311 case FFA_RETRY:
312 return "FFA_RETRY";
313 case FFA_ABORTED:
314 return "FFA_ABORTED";
315 case FFA_NO_DATA:
316 return "FFA_NO_DATA";
Karl Meakindc759f52024-05-07 16:08:02 +0100317 case FFA_NOT_READY:
318 return "FFA_NOT_READY";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100319 }
Karl Meakindc759f52024-05-07 16:08:02 +0100320 return "UNKNOWN";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100321}
322
J-Alves6f72ca82021-11-01 12:34:58 +0000323/**
Karl Meakin49ec1e42024-05-10 13:08:24 +0100324 * Defined in Table 3.1 in the FF-A v.1.2 memory management supplement.
325 * Input properties:
326 * - Bits[31:2] and Bit[0] are reserved (SBZ).
327 * Output properties:
328 * - Bit[0]: dynamically allocated buffer support.
329 * - Bit[1]: NS bit handling.
330 * - Bit[2]: support for retrieval by hypervisor.
331 * - Bits[31:3] are reserved (MBZ).
J-Alves6f72ca82021-11-01 12:34:58 +0000332 */
Karl Meakin49ec1e42024-05-10 13:08:24 +0100333#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT (0U << 0U)
334#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (1U << 1U)
335#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (1U << 2U)
J-Alves6f72ca82021-11-01 12:34:58 +0000336
Karl Meakin49ec1e42024-05-10 13:08:24 +0100337#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT (31U)
338#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT (2U)
339#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT (0U)
J-Alves6f72ca82021-11-01 12:34:58 +0000340
Karl Meakin49ec1e42024-05-10 13:08:24 +0100341enum ffa_feature_id {
342 /* Query interrupt ID of Notification Pending Interrupt. */
343 FFA_FEATURE_NPI = 1,
Karl Meakin34b8ae92023-01-13 13:33:07 +0000344
Karl Meakin49ec1e42024-05-10 13:08:24 +0100345 /* Query interrupt ID of Schedule Receiver Interrupt. */
346 FFA_FEATURE_SRI = 2,
J-Alves6f72ca82021-11-01 12:34:58 +0000347
Karl Meakin49ec1e42024-05-10 13:08:24 +0100348 /* Query interrupt ID of the Managed Exit Interrupt. */
349 FFA_FEATURE_MEI = 3,
350};
J-Alves6f72ca82021-11-01 12:34:58 +0000351
Karl Meakin49ec1e42024-05-10 13:08:24 +0100352/** Constants for bitmasks used in FFA_FEATURES. */
353#define FFA_FEATURES_FEATURE_BIT (31U)
354#define FFA_FEATURES_FEATURE_MBZ_HI_BIT (30U)
355#define FFA_FEATURES_FEATURE_MBZ_LO_BIT (8U)
356
357#define FFA_FEATURES_NS_SUPPORT_BIT (1U)
J-Alves6f72ca82021-11-01 12:34:58 +0000358
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100359/* FF-A function specific constants. */
360#define FFA_MSG_RECV_BLOCK 0x1
361#define FFA_MSG_RECV_BLOCK_MASK 0x1
362
363#define FFA_MSG_SEND_NOTIFY 0x1
364#define FFA_MSG_SEND_NOTIFY_MASK 0x1
365
366#define FFA_MEM_RECLAIM_CLEAR 0x1
367
368#define FFA_SLEEP_INDEFINITE 0
369
Karl Meakin80220052025-02-20 14:43:34 +0000370/*
371 * The type of memory permissions used by `FFA_MEM_PERM_GET` and
372 * `FFA_MEM_PERM_SET`.
373 */
374enum ffa_mem_perm {
375 FFA_MEM_PERM_RO = 0x7,
376 FFA_MEM_PERM_RW = 0x5,
377 FFA_MEM_PERM_RX = 0x3,
378};
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700379
Kathleen Capella7253bd52024-08-14 17:36:09 -0400380#define FFA_MSG_WAIT_FLAG_RETAIN_RX UINT32_C(0x1)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000381/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100382 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000383 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
384 * if partition info descriptors should be returned or just the count.
385 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100386#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
387#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000388
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100389/**
390 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
391 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
392 * configured to use.
393 */
J-Alves715d6232023-02-16 16:33:28 +0000394#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100395
Federico Recanatifc0295e2022-02-08 19:37:39 +0100396/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100397typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100398
399/**
J-Alves661e1b72023-08-02 13:39:40 +0100400 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
401 * parts:
402 * - Bit15 -> partition type identifier.
403 * - b'0 -> ID relates to a VM ID.
404 * - b'1 -> ID relates to an SP ID.
405 */
406#define FFA_ID_MASK ((ffa_id_t)0x8000)
407#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
408
409/**
410 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
411 */
412static inline bool ffa_is_vm_id(ffa_id_t id)
413{
414 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
415}
416
417/**
Karl Meakinc88c8e92024-11-29 16:13:55 +0000418 * Holds the UUID in a struct that is mappable directly to the SMCC calling
419 * convention, which is used for FF-A calls.
420 *
421 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
422 * of the SMCC Spec 1.2.
423 */
424struct ffa_uuid {
425 uint32_t uuid[4];
426};
427
428static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
429 uint32_t w3, struct ffa_uuid *uuid)
430{
431 uuid->uuid[0] = w0;
432 uuid->uuid[1] = w1;
433 uuid->uuid[2] = w2;
434 uuid->uuid[3] = w3;
435}
436
437static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
438 const struct ffa_uuid *uuid2)
439{
440 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
441 (uuid1->uuid[1] == uuid2->uuid[1]) &&
442 (uuid1->uuid[2] == uuid2->uuid[2]) &&
443 (uuid1->uuid[3] == uuid2->uuid[3]);
444}
445
446static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
447{
448 struct ffa_uuid null = {0};
449
450 return ffa_uuid_equal(uuid, &null);
451}
452
453static inline void ffa_uuid_from_u64x2(uint64_t uuid_lo, uint64_t uuid_hi,
454 struct ffa_uuid *uuid)
455{
456 ffa_uuid_init((uint32_t)(uuid_lo & 0xFFFFFFFFU),
457 (uint32_t)(uuid_lo >> 32),
458 (uint32_t)(uuid_hi & 0xFFFFFFFFU),
459 (uint32_t)(uuid_hi >> 32), uuid);
460}
461
462/**
463 * Split `uuid` into two u64s.
464 * This function writes to pointer parameters because C does not allow returning
465 * arrays from functions.
466 */
467static inline void ffa_uuid_to_u64x2(uint64_t *lo, uint64_t *hi,
468 const struct ffa_uuid *uuid)
469{
470 *lo = (uint64_t)uuid->uuid[1] << 32 | uuid->uuid[0];
471 *hi = (uint64_t)uuid->uuid[3] << 32 | uuid->uuid[2];
472}
473
474/**
475 * Partition message header as specified by table 7.1 from FF-A v1.3 ALP0
Federico Recanatifc0295e2022-02-08 19:37:39 +0100476 * specification.
477 */
478struct ffa_partition_rxtx_header {
Karl Meakinbf580c22024-12-12 14:31:06 +0000479 /* Reserved (SBZ). */
480 uint32_t flags;
481 /* Reserved (SBZ). */
482 uint32_t reserved_1;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100483 /* Offset from the beginning of the buffer to the message payload. */
484 uint32_t offset;
Karl Meakin9ca05512024-11-29 17:02:32 +0000485 /* Receiver endpoint ID. */
486 ffa_id_t receiver;
487 /* Sender endpoint ID. */
488 ffa_id_t sender;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100489 /* Size of message in buffer. */
490 uint32_t size;
Karl Meakinbf580c22024-12-12 14:31:06 +0000491 /* Reserved (SBZ). Added in v1.2. */
492 uint32_t reserved_2;
493 /* UUID identifying the communication protocol. Added in v1.2. */
494 struct ffa_uuid uuid;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100495};
496
Karl Meakinbf580c22024-12-12 14:31:06 +0000497#define FFA_RXTX_HEADER_SIZE_V1_1 \
498 offsetof(struct ffa_partition_rxtx_header, reserved_2)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100499#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
J-Alves70079932022-12-07 17:32:20 +0000500#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100501
Karl Meakin895007c2025-01-13 15:48:07 +0000502/**
Karl Meakinbf580c22024-12-12 14:31:06 +0000503 * Initialize a partition message header, with the default values for `flags`,
504 * `offset` and `uuid` and the v1.1 payload offset.
505 */
506static inline void ffa_rxtx_header_init_v1_1(
507 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
508 ffa_id_t receiver, uint32_t payload_size)
509{
510 header->flags = 0;
511 header->reserved_1 = 0;
512 header->offset = FFA_RXTX_HEADER_SIZE_V1_1;
513 header->sender = sender;
514 header->receiver = receiver;
515 header->size = payload_size;
516 header->reserved_2 = 0;
517 header->uuid = (struct ffa_uuid){0};
518}
519
520/**
521 * Initialize a partition message header, with the default values for `flags`,
522 * `offset` and `uuid`.
Karl Meakin895007c2025-01-13 15:48:07 +0000523 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100524static inline void ffa_rxtx_header_init(
Karl Meakin895007c2025-01-13 15:48:07 +0000525 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
526 ffa_id_t receiver, uint32_t payload_size)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100527{
528 header->flags = 0;
Karl Meakinbf580c22024-12-12 14:31:06 +0000529 header->reserved_1 = 0;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100530 header->offset = FFA_RXTX_HEADER_SIZE;
Karl Meakin9ca05512024-11-29 17:02:32 +0000531 header->sender = sender;
532 header->receiver = receiver;
Karl Meakin895007c2025-01-13 15:48:07 +0000533 header->size = payload_size;
Karl Meakinbf580c22024-12-12 14:31:06 +0000534 header->reserved_2 = 0;
535 header->uuid = (struct ffa_uuid){0};
536}
537
538/**
539 * Initialize a partition message header, with the default values for `flags`
540 * and `offset`.
541 */
542static inline void ffa_rxtx_header_init_with_uuid(
543 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
544 ffa_id_t receiver, uint32_t size, struct ffa_uuid uuid)
545{
546 header->flags = 0;
547 header->reserved_1 = 0;
548 header->offset = FFA_RXTX_HEADER_SIZE;
549 header->sender = sender;
550 header->receiver = receiver;
551 header->size = size;
552 header->reserved_2 = 0;
553 header->uuid = uuid;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100554}
555
Federico Recanatifc0295e2022-02-08 19:37:39 +0100556/* The maximum length possible for a single message. */
Karl Meakinbf580c22024-12-12 14:31:06 +0000557#define FFA_PARTITION_MSG_PAYLOAD_MAX_V1_1 \
558 (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE_V1_1)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100559#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
560
561struct ffa_partition_msg {
562 struct ffa_partition_rxtx_header header;
Karl Meakinbf580c22024-12-12 14:31:06 +0000563 /**
564 * Prefer using `ffa_partition_msg_payload` to accessing this field
565 * directly, because the offset does not necessarily correspond to the
566 * offset of this field.
567 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100568 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
569};
570
Karl Meakinbf580c22024-12-12 14:31:06 +0000571static_assert(sizeof(struct ffa_partition_msg) == HF_MAILBOX_SIZE,
572 "FF-A message size must match mailbox size");
573
Karl Meakin891eb882025-01-17 19:11:20 +0000574/**
575 * Get the partition message's payload, according to the header's `offset`
576 * field.
577 */
578static inline void *ffa_partition_msg_payload(struct ffa_partition_msg *msg)
579{
580 return (char *)msg + msg->header.offset;
581}
582
583static inline const void *ffa_partition_msg_payload_const(
584 const struct ffa_partition_msg *msg)
585{
586 return (const char *)msg + msg->header.offset;
587}
588
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100589/* The maximum length possible for a single message. */
590#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
591
592enum ffa_data_access {
593 FFA_DATA_ACCESS_NOT_SPECIFIED,
594 FFA_DATA_ACCESS_RO,
595 FFA_DATA_ACCESS_RW,
596 FFA_DATA_ACCESS_RESERVED,
597};
598
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100599static inline const char *ffa_data_access_name(enum ffa_data_access data_access)
600{
601 switch (data_access) {
602 case FFA_DATA_ACCESS_NOT_SPECIFIED:
603 return "FFA_DATA_ACCESS_NOT_SPECIFIED";
604 case FFA_DATA_ACCESS_RO:
605 return "FFA_DATA_ACCESS_RO";
606 case FFA_DATA_ACCESS_RW:
607 return "FFA_DATA_ACCESS_RW";
608 case FFA_DATA_ACCESS_RESERVED:
609 return "FFA_DATA_ACCESS_RESERVED";
610 }
611}
612
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100613enum ffa_instruction_access {
614 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
615 FFA_INSTRUCTION_ACCESS_NX,
616 FFA_INSTRUCTION_ACCESS_X,
617 FFA_INSTRUCTION_ACCESS_RESERVED,
618};
619
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100620static inline const char *ffa_instruction_access_name(
621 enum ffa_instruction_access instruction_access)
622{
623 switch (instruction_access) {
624 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
625 return "FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED";
626 case FFA_INSTRUCTION_ACCESS_NX:
627 return "FFA_INSTRUCTION_ACCESS_NX";
628 case FFA_INSTRUCTION_ACCESS_X:
629 return "FFA_INSTRUCTION_ACCESS_X";
630 case FFA_INSTRUCTION_ACCESS_RESERVED:
631 return "FFA_INSTRUCTION_ACCESS_RESERVED";
632 }
633}
634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100635enum ffa_memory_type {
636 FFA_MEMORY_NOT_SPECIFIED_MEM,
637 FFA_MEMORY_DEVICE_MEM,
638 FFA_MEMORY_NORMAL_MEM,
639};
640
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100641static inline const char *ffa_memory_type_name(enum ffa_memory_type type)
642{
643 switch (type) {
644 case FFA_MEMORY_NOT_SPECIFIED_MEM:
645 return "FFA_MEMORY_NOT_SPECIFIED_MEM";
646 case FFA_MEMORY_DEVICE_MEM:
647 return "FFA_MEMORY_DEVICE_MEM";
648 case FFA_MEMORY_NORMAL_MEM:
649 return "FFA_MEMORY_NORMAL_MEM";
650 }
651}
652
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100653enum ffa_memory_cacheability {
654 FFA_MEMORY_CACHE_RESERVED = 0x0,
655 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
656 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
657 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
658 FFA_MEMORY_DEV_NGNRNE = 0x0,
659 FFA_MEMORY_DEV_NGNRE = 0x1,
660 FFA_MEMORY_DEV_NGRE = 0x2,
661 FFA_MEMORY_DEV_GRE = 0x3,
662};
663
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100664static inline const char *ffa_memory_cacheability_name(
665 enum ffa_memory_cacheability cacheability)
666{
667 switch (cacheability) {
668 case FFA_MEMORY_CACHE_RESERVED:
669 return "FFA_MEMORY_CACHE_RESERVED";
670 case FFA_MEMORY_CACHE_NON_CACHEABLE:
671 return "FFA_MEMORY_CACHE_NON_CACHEABLE";
672 case FFA_MEMORY_CACHE_RESERVED_1:
673 return "FFA_MEMORY_CACHE_RESERVED_1";
674 case FFA_MEMORY_CACHE_WRITE_BACK:
675 return "FFA_MEMORY_CACHE_WRITE_BACK";
676 }
677}
678
Daniel Boulby9764ff62024-01-30 17:47:39 +0000679static inline const char *ffa_device_memory_cacheability_name(
680 enum ffa_memory_cacheability cacheability)
681{
682 switch (cacheability) {
683 case FFA_MEMORY_DEV_NGNRNE:
684 return "FFA_MEMORY_DEV_NGNRNE";
685 case FFA_MEMORY_DEV_NGNRE:
686 return "FFA_MEMORY_DEV_NGNRE";
687 case FFA_MEMORY_DEV_NGRE:
688 return "FFA_MEMORY_DEV_NGRE";
689 case FFA_MEMORY_DEV_GRE:
690 return "FFA_MEMORY_DEV_GRE";
691 }
692}
693
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100694enum ffa_memory_shareability {
695 FFA_MEMORY_SHARE_NON_SHAREABLE,
696 FFA_MEMORY_SHARE_RESERVED,
697 FFA_MEMORY_OUTER_SHAREABLE,
698 FFA_MEMORY_INNER_SHAREABLE,
699};
700
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100701static inline const char *ffa_memory_shareability_name(
702 enum ffa_memory_shareability shareability)
703{
704 switch (shareability) {
705 case FFA_MEMORY_SHARE_NON_SHAREABLE:
706 return "FFA_MEMORY_SHARE_NON_SHAREABLE";
707 case FFA_MEMORY_SHARE_RESERVED:
708 return "FFA_MEMORY_SHARE_RESERVED";
709 case FFA_MEMORY_OUTER_SHAREABLE:
710 return "FFA_MEMORY_OUTER_SHAREABLE";
711 case FFA_MEMORY_INNER_SHAREABLE:
712 return "FFA_MEMORY_INNER_SHAREABLE";
713 }
714}
715
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100716/**
717 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
718 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
719 * and FFA_MEM_RETRIEVE_REQUEST.
720 */
721enum ffa_memory_security {
722 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
723 FFA_MEMORY_SECURITY_SECURE = 0,
724 FFA_MEMORY_SECURITY_NON_SECURE,
725};
726
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100727static inline const char *ffa_memory_security_name(
728 enum ffa_memory_security security)
729{
730 switch (security) {
731 case FFA_MEMORY_SECURITY_UNSPECIFIED:
732 return "FFA_MEMORY_SECURITY_UNSPECIFIED";
733 case FFA_MEMORY_SECURITY_NON_SECURE:
734 return "FFA_MEMORY_SECURITY_NON_SECURE";
735 }
736}
737
Karl Meakin84710f32023-10-12 15:14:49 +0100738typedef struct {
739 uint8_t data_access : 2;
740 uint8_t instruction_access : 2;
741} ffa_memory_access_permissions_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100742
743/**
J-Alves0b6653d2022-04-22 13:17:38 +0100744 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100745 * region attributes descriptor".
746 */
Karl Meakin84710f32023-10-12 15:14:49 +0100747typedef struct {
748 uint8_t shareability : 2;
749 uint8_t cacheability : 2;
750 uint8_t type : 2;
751 uint8_t security : 2;
752 uint8_t : 8;
753} ffa_memory_attributes_t;
J-Alves0b6653d2022-04-22 13:17:38 +0100754
755/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
756#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100757
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100758/**
759 * A globally-unique ID assigned by the hypervisor for a region of memory being
760 * sent between VMs.
761 */
762typedef uint64_t ffa_memory_handle_t;
763
Karl Meakin1a760e72024-07-25 18:58:37 +0100764enum ffa_memory_handle_allocator {
765 FFA_MEMORY_HANDLE_ALLOCATOR_SPMC = 0,
766 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR = 1,
767};
J-Alves917d2f22020-10-30 18:39:30 +0000768
Karl Meakin1a760e72024-07-25 18:58:37 +0100769#define FFA_MEMORY_HANDLE_ALLOCATOR_BIT UINT64_C(63)
770#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
771 (UINT64_C(1) << FFA_MEMORY_HANDLE_ALLOCATOR_BIT)
J-Alves917d2f22020-10-30 18:39:30 +0000772#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
773
Karl Meakin1a760e72024-07-25 18:58:37 +0100774static inline ffa_memory_handle_t ffa_memory_handle_make(
775 uint64_t index, enum ffa_memory_handle_allocator allocator)
776{
777 return index | ((uint64_t)allocator << FFA_MEMORY_HANDLE_ALLOCATOR_BIT);
778}
779
780static inline uint64_t ffa_memory_handle_index(ffa_memory_handle_t handle)
781{
782 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
783}
784
785static inline enum ffa_memory_handle_allocator ffa_memory_handle_allocator(
786 ffa_memory_handle_t handle)
787{
788 return ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) != 0)
789 ? FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR
790 : FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
791}
792
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100793/**
794 * A count of VMs. This has the same range as the VM IDs but we give it a
795 * different name to make the different semantics clear.
796 */
J-Alves19e20cf2023-08-02 12:48:55 +0100797typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100798
799/** The index of a vCPU within a particular VM. */
800typedef uint16_t ffa_vcpu_index_t;
801
802/**
803 * A count of vCPUs. This has the same range as the vCPU indices but we give it
804 * a different name to make the different semantics clear.
805 */
806typedef ffa_vcpu_index_t ffa_vcpu_count_t;
807
808/** Parameter and return type of FF-A functions. */
809struct ffa_value {
810 uint64_t func;
811 uint64_t arg1;
812 uint64_t arg2;
813 uint64_t arg3;
814 uint64_t arg4;
815 uint64_t arg5;
816 uint64_t arg6;
817 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800818
819 struct {
820 uint64_t arg8;
821 uint64_t arg9;
822 uint64_t arg10;
823 uint64_t arg11;
824 uint64_t arg12;
825 uint64_t arg13;
826 uint64_t arg14;
827 uint64_t arg15;
828 uint64_t arg16;
829 uint64_t arg17;
830 bool valid;
831 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100832};
833
Olivier Depreze562e542020-06-11 17:31:54 +0200834static inline uint32_t ffa_func_id(struct ffa_value args)
835{
836 return args.func;
837}
838
Karl Meakindc759f52024-05-07 16:08:02 +0100839static inline enum ffa_error ffa_error_code(struct ffa_value val)
J-Alves13318e32021-02-22 17:21:00 +0000840{
Karl Meakin66a38bd2024-05-28 16:00:56 +0100841 /* NOLINTNEXTLINE(EnumCastOutOfRange) */
Karl Meakindc759f52024-05-07 16:08:02 +0100842 return (enum ffa_error)val.arg2;
J-Alves13318e32021-02-22 17:21:00 +0000843}
844
J-Alves19e20cf2023-08-02 12:48:55 +0100845static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100846{
847 return (args.arg1 >> 16) & 0xffff;
848}
849
J-Alves19e20cf2023-08-02 12:48:55 +0100850static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100851{
852 return args.arg1 & 0xffff;
853}
854
855static inline uint32_t ffa_msg_send_size(struct ffa_value args)
856{
857 return args.arg3;
858}
859
Federico Recanati25053ee2022-03-14 15:01:53 +0100860static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
861{
862 return args.arg2;
863}
864
Olivier Depreze562e542020-06-11 17:31:54 +0200865static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
866{
867 return args.arg2;
868}
869
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800870static inline uint16_t ffa_partition_info_regs_get_last_idx(
871 struct ffa_value args)
872{
873 return args.arg2 & 0xFFFF;
874}
875
876static inline uint16_t ffa_partition_info_regs_get_curr_idx(
877 struct ffa_value args)
878{
879 return (args.arg2 >> 16) & 0xFFFF;
880}
881
882static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
883{
884 return (args.arg2 >> 32) & 0xFFFF;
885}
886
887static inline uint16_t ffa_partition_info_regs_get_desc_size(
888 struct ffa_value args)
889{
890 return (args.arg2 >> 48);
891}
892
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100893static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
894{
895 return (uint64_t)a1 | (uint64_t)a2 << 32;
896}
897
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
899{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100900 return ffa_assemble_handle(args.arg2, args.arg3);
901}
902
Andrew Walbranca808b12020-05-15 17:22:28 +0100903static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
904{
905 return ffa_assemble_handle(args.arg1, args.arg2);
906}
907
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100908static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
909{
910 return (struct ffa_value){.func = FFA_SUCCESS_32,
911 .arg2 = (uint32_t)handle,
912 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100913}
914
J-Alves19e20cf2023-08-02 12:48:55 +0100915static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100916{
917 return (args.arg1 >> 16) & 0xffff;
918}
919
920static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
921{
922 return args.arg1 & 0xffff;
923}
924
J-Alves19e20cf2023-08-02 12:48:55 +0100925static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926{
927 return ((uint32_t)vm_id << 16) | vcpu_index;
928}
929
J-Alves19e20cf2023-08-02 12:48:55 +0100930static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100931{
932 return (args.arg4 >> 16) & 0xffff;
933}
934
J-Alves6f72ca82021-11-01 12:34:58 +0000935static inline uint32_t ffa_feature_intid(struct ffa_value args)
936{
937 return (uint32_t)args.arg2;
938}
939
Karl Meakind0356f82024-09-04 13:34:31 +0100940#define FFA_FRAMEWORK_MSG_BIT (UINT64_C(1) << 31)
941#define FFA_FRAMEWORK_MSG_FUNC_MASK UINT64_C(0xFF)
942
943/**
Madhukar Pappireddy8afab732025-02-10 09:39:36 -0600944 * Identifies FF-A framework messages. See sections 18.2 and 18.3 of v1.2 FF-A
Karl Meakind0356f82024-09-04 13:34:31 +0100945 * specification.
946 */
947enum ffa_framework_msg_func {
Madhukar Pappireddy8afab732025-02-10 09:39:36 -0600948 /* Power management framework messages. */
949 FFA_FRAMEWORK_MSG_PSCI_REQ = 0,
950 FFA_FRAMEWORK_MSG_PSCI_RESP = 2,
951
952 /* The VM availability messages. */
Karl Meakind0356f82024-09-04 13:34:31 +0100953 FFA_FRAMEWORK_MSG_VM_CREATION_REQ = 4,
954 FFA_FRAMEWORK_MSG_VM_CREATION_RESP = 5,
Karl Meakind0356f82024-09-04 13:34:31 +0100955 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_REQ = 6,
956 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_RESP = 7,
Madhukar Pappireddy8afab732025-02-10 09:39:36 -0600957
958 SPMD_FRAMEWORK_MSG_FFA_VERSION_REQ = 8,
959 SPMD_FRAMEWORK_MSG_FFA_VERSION_RESP = 9,
960
961 FFA_FRAMEWORK_MSG_INVALID = 0xFF,
Karl Meakind0356f82024-09-04 13:34:31 +0100962};
963
Karl Meakin06e8b732024-09-20 18:26:49 +0100964#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_LO 16
965#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_HI 31
966
Karl Meakind0356f82024-09-04 13:34:31 +0100967/** Get the `flags` field of a framework message */
968static inline uint32_t ffa_framework_msg_flags(struct ffa_value args)
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000969{
970 return (uint32_t)args.arg2;
971}
972
Karl Meakind0356f82024-09-04 13:34:31 +0100973/** Is `args` a framework message? */
974static inline bool ffa_is_framework_msg(struct ffa_value args)
975{
Karl Meakin06e8b732024-09-20 18:26:49 +0100976 return (args.func != FFA_MSG_SEND_DIRECT_REQ2_64) &&
977 (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) &&
978 ((ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_BIT) != 0);
Karl Meakind0356f82024-09-04 13:34:31 +0100979}
980
Karl Meakina1a02352024-09-20 18:27:18 +0100981/**
982 * Get the ID of the VM that has been created/destroyed from VM availability
983 * message
984 */
985static inline ffa_id_t ffa_vm_availability_message_vm_id(struct ffa_value args)
986{
987 return args.arg5 & 0xFFFF;
988}
989
Karl Meakind0356f82024-09-04 13:34:31 +0100990/** Get the function ID from a framework message */
Madhukar Pappireddy984e99a2025-02-10 09:55:27 -0600991static inline uint32_t ffa_framework_msg_get_func(struct ffa_value args)
Karl Meakind0356f82024-09-04 13:34:31 +0100992{
993 return ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_FUNC_MASK;
994}
995
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100996/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100997 * Flags to determine the partition properties, as required by
998 * FFA_PARTITION_INFO_GET.
999 *
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001000 * 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 +01001001 * specification, "Partition information descriptor, partition properties".
1002 */
1003typedef uint32_t ffa_partition_properties_t;
1004
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001005/**
1006 * Partition property: partition supports receipt of direct requests via the
1007 * FFA_MSG_SEND_DIRECT_REQ ABI.
1008 */
Kathleen Capella402fa852022-11-09 16:16:51 -05001009#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001010
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001011/**
1012 * Partition property: partition can send direct requests via the
1013 * FFA_MSG_SEND_DIRECT_REQ ABI.
1014 */
Kathleen Capella402fa852022-11-09 16:16:51 -05001015#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001016
1017/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -05001018#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001019
J-Alves09ff9d82021-11-02 11:55:20 +00001020/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -05001021#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
1022
Karl Meakina603e082024-08-02 17:57:27 +01001023/**
1024 * Partition property: partition must be informed about each VM that is created
1025 * by the Hypervisor.
1026 */
1027#define FFA_PARTITION_VM_CREATED (UINT32_C(1) << 6)
1028
1029/**
1030 * Partition property: partition must be informed about each VM that is
1031 * destroyed by the Hypervisor.
1032 */
1033#define FFA_PARTITION_VM_DESTROYED (UINT32_C(1) << 7)
1034
Kathleen Capella402fa852022-11-09 16:16:51 -05001035/** Partition property: partition runs in the AArch64 execution state. */
1036#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +00001037
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001038/**
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001039 * Partition property: partition supports receipt of direct requests via the
1040 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
1041 */
1042#define FFA_PARTITION_DIRECT_REQ2_RECV (UINT32_C(1) << 9)
1043
1044/**
1045 * Partition property: partition can send direct requests via the
1046 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
1047 */
1048#define FFA_PARTITION_DIRECT_REQ2_SEND (UINT32_C(1) << 10)
1049
1050/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001051 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
1052 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -05001053 * This corresponds to table 13.37 "Partition information descriptor"
1054 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +00001055 */
1056struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +01001057 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +00001058 ffa_vcpu_count_t vcpu_count;
1059 ffa_partition_properties_t properties;
1060 struct ffa_uuid uuid;
1061};
1062
J-Alvesdd1ad572022-01-25 17:58:26 +00001063/** Length in bytes of the name in boot information descriptor. */
1064#define FFA_BOOT_INFO_NAME_LEN 16
1065
J-Alves240d84c2022-04-22 12:19:34 +01001066/**
1067 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
1068 * the FF-A v1.1 EAC0 specification.
1069 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001070struct ffa_boot_info_desc {
1071 char name[FFA_BOOT_INFO_NAME_LEN];
1072 uint8_t type;
1073 uint8_t reserved;
1074 uint16_t flags;
1075 uint32_t size;
1076 uint64_t content;
1077};
1078
1079/** FF-A boot information type mask. */
1080#define FFA_BOOT_INFO_TYPE_SHIFT 7
1081#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
1082#define FFA_BOOT_INFO_TYPE_STD 0U
1083#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
1084
1085/** Standard boot info type IDs. */
1086#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
1087#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
1088#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
1089
1090/** FF-A Boot Info descriptors flags. */
1091#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
1092
1093/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
1094#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
1095#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
1096 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
1097#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
1098#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
1099
1100/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
1101#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
1102#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
1103 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
1104#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
1105#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
1106
1107static inline uint16_t ffa_boot_info_content_format(
1108 struct ffa_boot_info_desc *desc)
1109{
1110 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
1111 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
1112}
1113
1114static inline uint16_t ffa_boot_info_name_format(
1115 struct ffa_boot_info_desc *desc)
1116{
1117 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
1118 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
1119}
1120
1121static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
1122{
1123 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
1124}
1125
1126static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
1127{
1128 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
1129 FFA_BOOT_INFO_TYPE_SHIFT;
1130}
1131
1132/** Length in bytes of the signature in the boot descriptor. */
1133#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
1134
J-Alves240d84c2022-04-22 12:19:34 +01001135/**
1136 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
1137 * of the FF-A v1.1 EAC0 specification.
1138 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001139struct ffa_boot_info_header {
1140 uint32_t signature;
1141 uint32_t version;
1142 uint32_t info_blob_size;
1143 uint32_t desc_size;
1144 uint32_t desc_count;
1145 uint32_t desc_offset;
1146 uint64_t reserved;
1147 struct ffa_boot_info_desc boot_info[];
1148};
1149
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001150/**
J-Alves980d1992021-03-18 12:49:18 +00001151 * FF-A v1.1 specification restricts the number of notifications to a maximum
1152 * of 64. Following all possible bitmaps.
1153 */
Karl Meakin2ad6b662024-07-29 20:45:40 +01001154#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << (ID))
J-Alves980d1992021-03-18 12:49:18 +00001155
1156typedef uint64_t ffa_notifications_bitmap_t;
1157
1158#define MAX_FFA_NOTIFICATIONS 64U
1159
1160/**
J-Alvesc003a7a2021-03-18 13:06:53 +00001161 * Flag for notification bind and set, to specify call is about per-vCPU
1162 * notifications.
1163 */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001164#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +00001165
Federico Recanatie73d2832022-04-20 11:10:52 +02001166#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
1167#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
1168
1169/**
1170 * Helper functions to check for buffer full notification.
1171 */
1172static inline bool is_ffa_hyp_buffer_full_notification(
1173 ffa_notifications_bitmap_t framework)
1174{
1175 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
1176}
1177
1178static inline bool is_ffa_spm_buffer_full_notification(
1179 ffa_notifications_bitmap_t framework)
1180{
1181 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
1182}
1183
J-Alvesc003a7a2021-03-18 13:06:53 +00001184/**
J-Alves980d1992021-03-18 12:49:18 +00001185 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
1186 * and hi.
1187 * Helpful as FF-A specification defines that the notifications interfaces
1188 * arguments are 32-bit registers.
1189 */
1190static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
1191 uint32_t hi)
1192{
1193 return (ffa_notifications_bitmap_t)hi << 32U | lo;
1194}
1195
J-Alves98ff9562021-09-09 14:39:41 +01001196static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
1197 struct ffa_value val)
1198{
1199 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
1200}
1201
1202static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
1203 struct ffa_value val)
1204{
1205 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
1206}
1207
Federico Recanatie73d2832022-04-20 11:10:52 +02001208static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
1209 struct ffa_value val)
1210{
1211 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
1212}
1213
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001214typedef uint32_t ffa_notification_flags_t;
1215
1216/** Flags used in calls to FFA_NOTIFICATION_BIND interface. */
1217#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
1218
1219/** Flags used in calls to FFA_NOTIFICATION_GET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001220#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
1221#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
1222#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
1223#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +01001224
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001225/** Flags used in calls to FFA_NOTIFICATION_SET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001226#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001227#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001228#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
1229 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001230#define FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(flags) \
1231 ((ffa_vcpu_index_t)((flags) >> 16))
J-Alves13394022021-06-30 13:48:49 +01001232
J-Alvesbe6e3032021-11-30 14:54:12 +00001233static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +01001234{
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001235 return FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(args.arg1);
J-Alvesaa79c012021-07-09 14:29:45 +01001236}
1237
1238/**
J-Alvesc8e8a222021-06-08 17:33:52 +01001239 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
1240 */
1241#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
1242
1243/**
1244 * Number of registers to use in successfull return of interface
1245 * FFA_NOTIFICATION_INFO_GET.
1246 */
1247#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
1248
1249#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
1250
1251/**
1252 * Helper macros for return parameter encoding as described in section 17.7.1
1253 * of the FF-A v1.1 Beta0 specification.
1254 */
1255#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
1256#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
Karl Meakin2ad6b662024-07-29 20:45:40 +01001257#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * ((l) - 1) + 12)
J-Alvesc8e8a222021-06-08 17:33:52 +01001258#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
Daniel Boulby1308a632024-09-11 15:19:16 +01001259#define FFA_NOTIFICATIONS_LIST_MAX_SIZE 0x4U
Daniel Boulbyedfdf282025-01-02 18:51:19 +00001260#define FFA_NOTIFICATIONS_LIST_MAX_VCPU_IDS \
1261 (FFA_NOTIFICATIONS_LIST_MAX_SIZE - 1)
J-Alvesc8e8a222021-06-08 17:33:52 +01001262
1263static inline uint32_t ffa_notification_info_get_lists_count(
1264 struct ffa_value args)
1265{
1266 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
1267 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
1268}
1269
1270static inline uint32_t ffa_notification_info_get_list_size(
1271 struct ffa_value args, unsigned int list_idx)
1272{
1273 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
1274 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
1275}
1276
1277static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
1278{
1279 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
1280}
1281
Daniel Boulby1308a632024-09-11 15:19:16 +01001282void ffa_notification_info_get_and_check(
1283 const uint32_t expected_lists_count,
1284 const uint32_t *const expected_lists_sizes,
1285 const uint16_t *const expected_ids);
1286
J-Alvesc8e8a222021-06-08 17:33:52 +01001287/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001288 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +01001289 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
1290 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001291 */
1292struct ffa_memory_region_constituent {
1293 /**
1294 * The base IPA of the constituent memory region, aligned to 4 kiB page
1295 * size granularity.
1296 */
1297 uint64_t address;
1298 /** The number of 4 kiB pages in the constituent memory region. */
1299 uint32_t page_count;
1300 /** Reserved field, must be 0. */
1301 uint32_t reserved;
1302};
1303
1304/**
J-Alves0b6653d2022-04-22 13:17:38 +01001305 * A set of pages comprising a memory region. This corresponds to table 10.13 of
1306 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001307 */
1308struct ffa_composite_memory_region {
1309 /**
1310 * The total number of 4 kiB pages included in this memory region. This
1311 * must be equal to the sum of page counts specified in each
1312 * `ffa_memory_region_constituent`.
1313 */
1314 uint32_t page_count;
1315 /**
1316 * The number of constituents (`ffa_memory_region_constituent`)
1317 * included in this memory region range.
1318 */
1319 uint32_t constituent_count;
1320 /** Reserved field, must be 0. */
1321 uint64_t reserved_0;
1322 /** An array of `constituent_count` memory region constituents. */
1323 struct ffa_memory_region_constituent constituents[];
1324};
1325
1326/** Flags to indicate properties of receivers during memory region retrieval. */
1327typedef uint8_t ffa_memory_receiver_flags_t;
1328
1329/**
J-Alves0b6653d2022-04-22 13:17:38 +01001330 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001331 * access permissions descriptor".
1332 */
1333struct ffa_memory_region_attributes {
1334 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001335 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001336 /**
1337 * The permissions with which the memory region should be mapped in the
1338 * receiver's page table.
1339 */
1340 ffa_memory_access_permissions_t permissions;
1341 /**
1342 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1343 * for memory regions with multiple borrowers.
1344 */
1345 ffa_memory_receiver_flags_t flags;
1346};
1347
1348/** Flags to control the behaviour of a memory sharing transaction. */
1349typedef uint32_t ffa_memory_region_flags_t;
1350
1351/**
1352 * Clear memory region contents after unmapping it from the sender and before
1353 * mapping it for any receiver.
1354 */
1355#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1356
1357/**
1358 * Whether the hypervisor may time slice the memory sharing or retrieval
1359 * operation.
1360 */
1361#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1362
1363/**
1364 * Whether the hypervisor should clear the memory region after the receiver
1365 * relinquishes it or is aborted.
1366 */
1367#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1368
J-Alves3456e032023-07-20 12:20:05 +01001369/**
1370 * On retrieve request, bypass the multi-borrower check.
1371 */
1372#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1373
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001374#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1375#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1376#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1377#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1378#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1379
Federico Recanati85090c42021-12-15 13:17:54 +01001380#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1381#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1382
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001383/**
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001384 * Struct to store the impdef value seen in Table 11.16 of the
1385 * FF-A v1.2 ALP0 specification "Endpoint memory access descriptor".
1386 */
1387struct ffa_memory_access_impdef {
1388 uint64_t val[2];
1389};
1390
Daniel Boulbye0ca9a02024-03-05 18:40:59 +00001391static inline struct ffa_memory_access_impdef ffa_memory_access_impdef_init(
1392 uint64_t impdef_hi, uint64_t impdef_lo)
1393{
1394 return (struct ffa_memory_access_impdef){{impdef_hi, impdef_lo}};
1395}
1396
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001397/**
J-Alves0b6653d2022-04-22 13:17:38 +01001398 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1399 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001400 */
1401struct ffa_memory_access {
1402 struct ffa_memory_region_attributes receiver_permissions;
1403 /**
1404 * Offset in bytes from the start of the outer `ffa_memory_region` to
1405 * an `ffa_composite_memory_region` struct.
1406 */
1407 uint32_t composite_memory_region_offset;
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001408 struct ffa_memory_access_impdef impdef;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001409 uint64_t reserved_0;
1410};
1411
J-Alves363f5722022-04-25 17:37:37 +01001412/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001413#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001414
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001415/**
1416 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001417 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1418 * memory transaction descriptor". Note that it is also used for retrieve
1419 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001420 */
1421struct ffa_memory_region {
1422 /**
1423 * The ID of the VM which originally sent the memory region, i.e. the
1424 * owner.
1425 */
J-Alves19e20cf2023-08-02 12:48:55 +01001426 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001427 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001428 /** Flags to control behaviour of the transaction. */
1429 ffa_memory_region_flags_t flags;
1430 ffa_memory_handle_t handle;
1431 /**
1432 * An implementation defined value associated with the receiver and the
1433 * memory region.
1434 */
1435 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001436 /* Size of the memory access descriptor. */
1437 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001438 /**
1439 * The number of `ffa_memory_access` entries included in this
1440 * transaction.
1441 */
1442 uint32_t receiver_count;
1443 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001444 * Offset of the 'receivers' field, which relates to the memory access
1445 * descriptors.
1446 */
1447 uint32_t receivers_offset;
1448 /** Reserved field (12 bytes) must be 0. */
1449 uint32_t reserved[3];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001450};
1451
1452/**
1453 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001454 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001455 * region".
1456 */
1457struct ffa_mem_relinquish {
1458 ffa_memory_handle_t handle;
1459 ffa_memory_region_flags_t flags;
1460 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001461 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001462};
1463
1464/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001465 * Returns the first FF-A version that matches the memory access descriptor
1466 * size.
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001467 */
Karl Meakin0e617d92024-04-05 12:55:22 +01001468enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01001469 uint32_t memory_access_desc_size);
1470
1471/**
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001472 * To maintain forwards compatability we can't make assumptions about the size
1473 * of the endpoint memory access descriptor so provide a helper function
1474 * to get a receiver from the receiver array using the memory access descriptor
1475 * size field from the memory region descriptor struct.
1476 * Returns NULL if we cannot return the receiver.
1477 */
1478static inline struct ffa_memory_access *ffa_memory_region_get_receiver(
1479 struct ffa_memory_region *memory_region, uint32_t receiver_index)
1480{
1481 uint32_t memory_access_desc_size =
1482 memory_region->memory_access_desc_size;
1483
1484 if (receiver_index >= memory_region->receiver_count) {
1485 return NULL;
1486 }
1487
1488 /*
1489 * Memory access descriptor size cannot be greater than the size of
1490 * the memory access descriptor defined by the current FF-A version.
1491 */
1492 if (memory_access_desc_size > sizeof(struct ffa_memory_access)) {
1493 return NULL;
1494 }
1495
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001496 /* Check we cannot use receivers offset to cause overflow. */
1497 if (memory_region->receivers_offset !=
1498 sizeof(struct ffa_memory_region)) {
1499 return NULL;
1500 }
1501
Karl Meakin2ad6b662024-07-29 20:45:40 +01001502 return (struct ffa_memory_access
1503 *)((uint8_t *)memory_region +
1504 (size_t)memory_region->receivers_offset +
1505 (size_t)(receiver_index * memory_access_desc_size));
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001506}
1507
1508/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001509 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1510 * returns its index in the receiver's array. If receiver's ID doesn't exist
1511 * in the array, return the region's 'receivers_count'.
1512 */
1513static inline uint32_t ffa_memory_region_get_receiver_index(
1514 struct ffa_memory_region *memory_region, ffa_id_t receiver_id)
1515{
1516 uint32_t i;
1517
1518 for (i = 0U; i < memory_region->receiver_count; i++) {
1519 struct ffa_memory_access *receiver =
1520 ffa_memory_region_get_receiver(memory_region, i);
1521 if (receiver->receiver_permissions.receiver == receiver_id) {
1522 break;
1523 }
1524 }
1525
1526 return i;
1527}
1528
1529/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001530 * Gets the `ffa_composite_memory_region` for the given receiver from an
1531 * `ffa_memory_region`, or NULL if it is not valid.
1532 */
1533static inline struct ffa_composite_memory_region *
1534ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1535 uint32_t receiver_index)
1536{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001537 struct ffa_memory_access *receiver =
1538 ffa_memory_region_get_receiver(memory_region, receiver_index);
1539 uint32_t offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001540
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001541 if (receiver == NULL) {
1542 return NULL;
1543 }
1544
1545 offset = receiver->composite_memory_region_offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001546 if (offset == 0) {
1547 return NULL;
1548 }
1549
1550 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1551 offset);
1552}
1553
1554static inline uint32_t ffa_mem_relinquish_init(
1555 struct ffa_mem_relinquish *relinquish_request,
1556 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001557 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001558{
1559 relinquish_request->handle = handle;
1560 relinquish_request->flags = flags;
1561 relinquish_request->endpoint_count = 1;
1562 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001563 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001564}
1565
J-Alves126ab502022-09-29 11:37:33 +01001566void ffa_copy_memory_region_constituents(
1567 struct ffa_memory_region_constituent *dest,
1568 const struct ffa_memory_region_constituent *src);
1569
Karl Meakin0fd67292024-02-06 17:33:05 +00001570struct ffa_features_rxtx_map_params {
1571 /*
1572 * Bit[0:1]:
1573 * Minimum buffer size and alignment boundary:
1574 * 0b00: 4K
1575 * 0b01: 64K
1576 * 0b10: 16K
1577 * 0b11: Reserved
1578 */
1579 uint8_t min_buf_size : 2;
1580 /*
1581 * Bit[2:15]:
1582 * Reserved (MBZ)
1583 */
1584 uint16_t mbz : 14;
1585 /*
1586 * Bit[16:32]:
1587 * Maximum buffer size in number of pages
1588 * Only present on version 1.2 or later
1589 */
1590 uint16_t max_buf_size : 16;
1591};
1592
Karl Meakin49ec1e42024-05-10 13:08:24 +01001593enum ffa_features_rxtx_map_buf_size {
1594 FFA_RXTX_MAP_MIN_BUF_4K = 0,
1595 FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT = 1,
1596};
Karl Meakin0fd67292024-02-06 17:33:05 +00001597
Karl Meakinf7861302024-02-20 14:39:33 +00001598static inline struct ffa_features_rxtx_map_params ffa_features_rxtx_map_params(
1599 struct ffa_value args)
1600{
1601 struct ffa_features_rxtx_map_params params;
1602 uint32_t arg2 = args.arg2;
1603
1604 params = *(struct ffa_features_rxtx_map_params *)(&arg2);
1605
1606 return params;
1607}
1608
Federico Recanati392be392022-02-08 20:53:03 +01001609/**
1610 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1611 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1612 * to the SPMC, in order to allow indirect messaging.
1613 */
1614struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001615 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001616 uint16_t reserved;
1617
1618 /*
1619 * 8-byte aligned offset from the base address of this descriptor to the
1620 * `ffa_composite_memory_region` describing the RX buffer.
1621 */
1622 uint32_t rx_offset;
1623
1624 /*
1625 * 8-byte aligned offset from the base address of this descriptor to the
1626 * `ffa_composite_memory_region` describing the TX buffer.
1627 */
1628 uint32_t tx_offset;
1629
1630 /* Pad to align on 16-byte boundary. */
1631 uint32_t pad;
1632};
1633
1634static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001635ffa_endpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001636{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001637 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001638 desc->rx_offset);
1639}
1640
1641static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001642ffa_endpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001643{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001644 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001645 desc->tx_offset);
1646}
1647
J-Alves2d8457f2022-10-05 11:06:41 +01001648void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001649 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001650 ffa_memory_attributes_t attributes,
1651 ffa_memory_region_flags_t flags,
1652 ffa_memory_handle_t handle, uint32_t tag,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001653 uint32_t receiver_count,
1654 uint32_t receiver_desc_size);
Daniel Boulby59ffee92023-11-02 18:26:26 +00001655void ffa_memory_access_init(struct ffa_memory_access *receiver,
1656 ffa_id_t receiver_id,
1657 enum ffa_data_access data_access,
1658 enum ffa_instruction_access instruction_access,
1659 ffa_memory_receiver_flags_t flags,
1660 struct ffa_memory_access_impdef *impdef_val);
J-Alves45085432022-04-22 16:19:20 +01001661uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001662 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001663 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001664 const struct ffa_memory_region_constituent constituents[],
1665 uint32_t constituent_count, uint32_t tag,
1666 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1667 enum ffa_instruction_access instruction_access,
1668 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001669 enum ffa_memory_shareability shareability,
1670 struct ffa_memory_access_impdef *impdef_val, uint32_t *fragment_length,
Andrew Walbranca808b12020-05-15 17:22:28 +01001671 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001672uint32_t ffa_memory_region_init(
1673 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001674 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001675 uint32_t receiver_count, uint32_t receiver_desc_size,
J-Alvesf4eecf72022-07-20 16:05:34 +01001676 const struct ffa_memory_region_constituent constituents[],
1677 uint32_t constituent_count, uint32_t tag,
1678 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1679 enum ffa_memory_cacheability cacheability,
1680 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1681 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001682uint32_t ffa_memory_retrieve_request_init(
1683 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001684 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001685 uint32_t receiver_count, uint32_t receiver_desc_size, uint32_t tag,
1686 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1687 enum ffa_memory_cacheability cacheability,
J-Alves9b24ed82022-08-04 13:12:45 +01001688 enum ffa_memory_shareability shareability);
1689uint32_t ffa_memory_retrieve_request_init_single_receiver(
1690 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001691 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001692 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1693 enum ffa_instruction_access instruction_access,
1694 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001695 enum ffa_memory_shareability shareability,
1696 struct ffa_memory_access_impdef *impdef_val);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001697uint32_t ffa_memory_lender_retrieve_request_init(
1698 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001699 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001700uint32_t ffa_memory_fragment_init(
1701 struct ffa_memory_region_constituent *fragment,
1702 size_t fragment_max_size,
1703 const struct ffa_memory_region_constituent constituents[],
1704 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001705void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001706 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001707 uint64_t rx_address, uint64_t tx_address);