blob: 875bd9479bfe00e799d3716c46c50115b6b884d9 [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,
25 FFA_VERSION_COMPILED = FFA_VERSION_1_2,
26};
Daniel Boulby6e32c612021-02-17 15:09:41 +000027
Karl Meakin0e617d92024-04-05 12:55:22 +010028#define FFA_VERSION_MBZ_BIT (1U << 31U)
29#define FFA_VERSION_MAJOR_SHIFT (16U)
30#define FFA_VERSION_MAJOR_MASK (0x7FFFU)
31#define FFA_VERSION_MINOR_SHIFT (0U)
32#define FFA_VERSION_MINOR_MASK (0xFFFFU)
33
34/** Return true if the version is valid (i.e. bit 31 is 0). */
35static inline bool ffa_version_is_valid(uint32_t version)
36{
37 return (version & FFA_VERSION_MBZ_BIT) == 0;
38}
39
40/** Construct a version from a pair of major and minor components. */
41static inline enum ffa_version make_ffa_version(uint16_t major, uint16_t minor)
42{
43 return (enum ffa_version)((major << FFA_VERSION_MAJOR_SHIFT) |
44 (minor << FFA_VERSION_MINOR_SHIFT));
45}
46
47/** Get the major component of the version. */
48static inline uint16_t ffa_version_get_major(enum ffa_version version)
49{
50 return (version >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
51}
52
53/** Get the minor component of the version. */
54static inline uint16_t ffa_version_get_minor(enum ffa_version version)
55{
56 return (version >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK;
57}
Olivier Deprez62d99e32020-01-09 15:58:07 +010058
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010059/**
60 * Check major versions are equal and the minor version of the caller is
61 * less than or equal to the minor version of the callee.
62 */
Karl Meakin0e617d92024-04-05 12:55:22 +010063static inline bool ffa_versions_are_compatible(enum ffa_version caller,
64 enum ffa_version callee)
65{
66 return ffa_version_get_major(caller) == ffa_version_get_major(callee) &&
67 ffa_version_get_minor(caller) <= ffa_version_get_minor(callee);
68}
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010069
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010070/* clang-format off */
71
72#define FFA_LOW_32_ID 0x84000060
73#define FFA_HIGH_32_ID 0x8400007F
74#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010075#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010076
Karl Meakinf51a35f2023-08-07 17:53:52 +010077/**
78 * FF-A function identifiers.
79 * Don't forget to update `ffa_func_name` if you add a new one.
80 */
J-Alves3829fc02021-03-18 12:49:18 +000081#define FFA_ERROR_32 0x84000060
82#define FFA_SUCCESS_32 0x84000061
83#define FFA_SUCCESS_64 0xC4000061
84#define FFA_INTERRUPT_32 0x84000062
85#define FFA_VERSION_32 0x84000063
86#define FFA_FEATURES_32 0x84000064
87#define FFA_RX_RELEASE_32 0x84000065
88#define FFA_RXTX_MAP_32 0x84000066
89#define FFA_RXTX_MAP_64 0xC4000066
90#define FFA_RXTX_UNMAP_32 0x84000067
91#define FFA_PARTITION_INFO_GET_32 0x84000068
92#define FFA_ID_GET_32 0x84000069
93#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
94#define FFA_MSG_WAIT_32 0x8400006B
95#define FFA_YIELD_32 0x8400006C
96#define FFA_RUN_32 0x8400006D
97#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
98#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
99#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
100#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
101#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
102#define FFA_MEM_DONATE_32 0x84000071
J-Alves95fbb312024-03-20 15:19:16 +0000103#define FFA_MEM_DONATE_64 0xC4000071
J-Alves3829fc02021-03-18 12:49:18 +0000104#define FFA_MEM_LEND_32 0x84000072
J-Alves95fbb312024-03-20 15:19:16 +0000105#define FFA_MEM_LEND_64 0xC4000072
J-Alves3829fc02021-03-18 12:49:18 +0000106#define FFA_MEM_SHARE_32 0x84000073
J-Alves95fbb312024-03-20 15:19:16 +0000107#define FFA_MEM_SHARE_64 0xC4000073
J-Alves3829fc02021-03-18 12:49:18 +0000108#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
J-Alves95fbb312024-03-20 15:19:16 +0000109#define FFA_MEM_RETRIEVE_REQ_64 0xC4000074
J-Alves3829fc02021-03-18 12:49:18 +0000110#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
111#define FFA_MEM_RELINQUISH_32 0x84000076
112#define FFA_MEM_RECLAIM_32 0x84000077
113#define FFA_MEM_FRAG_RX_32 0x8400007A
114#define FFA_MEM_FRAG_TX_32 0x8400007B
115#define FFA_NORMAL_WORLD_RESUME 0x8400007C
116
117/* FF-A v1.1 */
118#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
119#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
120#define FFA_NOTIFICATION_BIND_32 0x8400007F
121#define FFA_NOTIFICATION_UNBIND_32 0x84000080
122#define FFA_NOTIFICATION_SET_32 0x84000081
123#define FFA_NOTIFICATION_GET_32 0x84000082
124#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
125#define FFA_RX_ACQUIRE_32 0x84000084
126#define FFA_SPM_ID_GET_32 0x84000085
127#define FFA_MSG_SEND2_32 0x84000086
128#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
129#define FFA_MEM_PERM_GET_32 0x84000088
130#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700131#define FFA_MEM_PERM_GET_64 0xC4000088
132#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100133
Kathleen Capellae4fe2962023-09-01 17:08:47 -0400134/* FF-A v1.2 */
Maksims Svecovs71b76702022-05-20 15:32:58 +0100135#define FFA_CONSOLE_LOG_32 0x8400008A
136#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800137#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -0600138#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Kathleen Capella41fea932023-06-23 17:39:28 -0400139#define FFA_MSG_SEND_DIRECT_REQ2_64 0xC400008D
Kathleen Capella087e5022023-09-07 18:04:15 -0400140#define FFA_MSG_SEND_DIRECT_RESP2_64 0xC400008E
Maksims Svecovs71b76702022-05-20 15:32:58 +0100141
Karl Meakinf51a35f2023-08-07 17:53:52 +0100142/**
143 * FF-A error codes.
144 * Don't forget to update `ffa_error_name` if you add a new one.
145 */
Karl Meakindc759f52024-05-07 16:08:02 +0100146enum ffa_error {
147 FFA_NOT_SUPPORTED = -1,
148 FFA_INVALID_PARAMETERS = -2,
149 FFA_NO_MEMORY = -3,
150 FFA_BUSY = -4,
151 FFA_INTERRUPTED = -5,
152 FFA_DENIED = -6,
153 FFA_RETRY = -7,
154 FFA_ABORTED = -8,
155 FFA_NO_DATA = -9,
156 FFA_NOT_READY = -10,
157};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158
159/* clang-format on */
160
Karl Meakinf51a35f2023-08-07 17:53:52 +0100161/* Return the name of the function identifier. */
162static inline const char *ffa_func_name(uint32_t func)
163{
164 switch (func) {
165 case FFA_ERROR_32:
166 return "FFA_ERROR_32";
167 case FFA_SUCCESS_32:
168 return "FFA_SUCCESS_32";
169 case FFA_SUCCESS_64:
170 return "FFA_SUCCESS_64";
171 case FFA_INTERRUPT_32:
172 return "FFA_INTERRUPT_32";
173 case FFA_VERSION_32:
174 return "FFA_VERSION_32";
175 case FFA_FEATURES_32:
176 return "FFA_FEATURES_32";
177 case FFA_RX_RELEASE_32:
178 return "FFA_RX_RELEASE_32";
179 case FFA_RXTX_MAP_32:
180 return "FFA_RXTX_MAP_32";
181 case FFA_RXTX_MAP_64:
182 return "FFA_RXTX_MAP_64";
183 case FFA_RXTX_UNMAP_32:
184 return "FFA_RXTX_UNMAP_32";
185 case FFA_PARTITION_INFO_GET_32:
186 return "FFA_PARTITION_INFO_GET_32";
187 case FFA_ID_GET_32:
188 return "FFA_ID_GET_32";
189 case FFA_MSG_POLL_32:
190 return "FFA_MSG_POLL_32";
191 case FFA_MSG_WAIT_32:
192 return "FFA_MSG_WAIT_32";
193 case FFA_YIELD_32:
194 return "FFA_YIELD_32";
195 case FFA_RUN_32:
196 return "FFA_RUN_32";
197 case FFA_MSG_SEND_32:
198 return "FFA_MSG_SEND_32";
199 case FFA_MSG_SEND_DIRECT_REQ_32:
200 return "FFA_MSG_SEND_DIRECT_REQ_32";
201 case FFA_MSG_SEND_DIRECT_REQ_64:
202 return "FFA_MSG_SEND_DIRECT_REQ_64";
203 case FFA_MSG_SEND_DIRECT_RESP_32:
204 return "FFA_MSG_SEND_DIRECT_RESP_32";
205 case FFA_MSG_SEND_DIRECT_RESP_64:
206 return "FFA_MSG_SEND_DIRECT_RESP_64";
207 case FFA_MEM_DONATE_32:
208 return "FFA_MEM_DONATE_32";
209 case FFA_MEM_LEND_32:
210 return "FFA_MEM_LEND_32";
211 case FFA_MEM_SHARE_32:
212 return "FFA_MEM_SHARE_32";
213 case FFA_MEM_RETRIEVE_REQ_32:
214 return "FFA_MEM_RETRIEVE_REQ_32";
J-Alves95fbb312024-03-20 15:19:16 +0000215 case FFA_MEM_DONATE_64:
216 return "FFA_MEM_DONATE_64";
217 case FFA_MEM_LEND_64:
218 return "FFA_MEM_LEND_64";
219 case FFA_MEM_SHARE_64:
220 return "FFA_MEM_SHARE_64";
221 case FFA_MEM_RETRIEVE_REQ_64:
222 return "FFA_MEM_RETRIEVE_REQ_64";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100223 case FFA_MEM_RETRIEVE_RESP_32:
224 return "FFA_MEM_RETRIEVE_RESP_32";
225 case FFA_MEM_RELINQUISH_32:
226 return "FFA_MEM_RELINQUISH_32";
227 case FFA_MEM_RECLAIM_32:
228 return "FFA_MEM_RECLAIM_32";
229 case FFA_MEM_FRAG_RX_32:
230 return "FFA_MEM_FRAG_RX_32";
231 case FFA_MEM_FRAG_TX_32:
232 return "FFA_MEM_FRAG_TX_32";
233 case FFA_NORMAL_WORLD_RESUME:
234 return "FFA_NORMAL_WORLD_RESUME";
235
236 /* FF-A v1.1 */
237 case FFA_NOTIFICATION_BITMAP_CREATE_32:
238 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
239 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
240 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
241 case FFA_NOTIFICATION_BIND_32:
242 return "FFA_NOTIFICATION_BIND_32";
243 case FFA_NOTIFICATION_UNBIND_32:
244 return "FFA_NOTIFICATION_UNBIND_32";
245 case FFA_NOTIFICATION_SET_32:
246 return "FFA_NOTIFICATION_SET_32";
247 case FFA_NOTIFICATION_GET_32:
248 return "FFA_NOTIFICATION_GET_32";
249 case FFA_NOTIFICATION_INFO_GET_64:
250 return "FFA_NOTIFICATION_INFO_GET_64";
251 case FFA_RX_ACQUIRE_32:
252 return "FFA_RX_ACQUIRE_32";
253 case FFA_SPM_ID_GET_32:
254 return "FFA_SPM_ID_GET_32";
255 case FFA_MSG_SEND2_32:
256 return "FFA_MSG_SEND2_32";
257 case FFA_SECONDARY_EP_REGISTER_64:
258 return "FFA_SECONDARY_EP_REGISTER_64";
259 case FFA_MEM_PERM_GET_32:
260 return "FFA_MEM_PERM_GET_32";
261 case FFA_MEM_PERM_SET_32:
262 return "FFA_MEM_PERM_SET_32";
263 case FFA_MEM_PERM_GET_64:
264 return "FFA_MEM_PERM_GET_64";
265 case FFA_MEM_PERM_SET_64:
266 return "FFA_MEM_PERM_SET_64";
267
268 /* Implementation-defined ABIs. */
269 case FFA_CONSOLE_LOG_32:
270 return "FFA_CONSOLE_LOG_32";
271 case FFA_CONSOLE_LOG_64:
272 return "FFA_CONSOLE_LOG_64";
273 case FFA_PARTITION_INFO_GET_REGS_64:
274 return "FFA_PARTITION_INFO_GET_REGS_64";
275 case FFA_EL3_INTR_HANDLE_32:
276 return "FFA_EL3_INTR_HANDLE_32";
277
278 default:
279 return "UNKNOWN";
280 }
281}
282
283/* Return the name of the error code. */
Karl Meakindc759f52024-05-07 16:08:02 +0100284static inline const char *ffa_error_name(enum ffa_error error)
Karl Meakinf51a35f2023-08-07 17:53:52 +0100285{
286 switch (error) {
287 case FFA_NOT_SUPPORTED:
288 return "FFA_NOT_SUPPORTED";
289 case FFA_INVALID_PARAMETERS:
290 return "FFA_INVALID_PARAMETERS";
291 case FFA_NO_MEMORY:
292 return "FFA_NO_MEMORY";
293 case FFA_BUSY:
294 return "FFA_BUSY";
295 case FFA_INTERRUPTED:
296 return "FFA_INTERRUPTED";
297 case FFA_DENIED:
298 return "FFA_DENIED";
299 case FFA_RETRY:
300 return "FFA_RETRY";
301 case FFA_ABORTED:
302 return "FFA_ABORTED";
303 case FFA_NO_DATA:
304 return "FFA_NO_DATA";
Karl Meakindc759f52024-05-07 16:08:02 +0100305 case FFA_NOT_READY:
306 return "FFA_NOT_READY";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100307 }
Karl Meakindc759f52024-05-07 16:08:02 +0100308 return "UNKNOWN";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100309}
310
J-Alves6f72ca82021-11-01 12:34:58 +0000311/**
Karl Meakin49ec1e42024-05-10 13:08:24 +0100312 * Defined in Table 3.1 in the FF-A v.1.2 memory management supplement.
313 * Input properties:
314 * - Bits[31:2] and Bit[0] are reserved (SBZ).
315 * Output properties:
316 * - Bit[0]: dynamically allocated buffer support.
317 * - Bit[1]: NS bit handling.
318 * - Bit[2]: support for retrieval by hypervisor.
319 * - Bits[31:3] are reserved (MBZ).
J-Alves6f72ca82021-11-01 12:34:58 +0000320 */
Karl Meakin49ec1e42024-05-10 13:08:24 +0100321#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT (0U << 0U)
322#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (1U << 1U)
323#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (1U << 2U)
J-Alves6f72ca82021-11-01 12:34:58 +0000324
Karl Meakin49ec1e42024-05-10 13:08:24 +0100325#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT (31U)
326#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT (2U)
327#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT (0U)
J-Alves6f72ca82021-11-01 12:34:58 +0000328
Karl Meakin49ec1e42024-05-10 13:08:24 +0100329enum ffa_feature_id {
330 /* Query interrupt ID of Notification Pending Interrupt. */
331 FFA_FEATURE_NPI = 1,
Karl Meakin34b8ae92023-01-13 13:33:07 +0000332
Karl Meakin49ec1e42024-05-10 13:08:24 +0100333 /* Query interrupt ID of Schedule Receiver Interrupt. */
334 FFA_FEATURE_SRI = 2,
J-Alves6f72ca82021-11-01 12:34:58 +0000335
Karl Meakin49ec1e42024-05-10 13:08:24 +0100336 /* Query interrupt ID of the Managed Exit Interrupt. */
337 FFA_FEATURE_MEI = 3,
338};
J-Alves6f72ca82021-11-01 12:34:58 +0000339
Karl Meakin49ec1e42024-05-10 13:08:24 +0100340/** Constants for bitmasks used in FFA_FEATURES. */
341#define FFA_FEATURES_FEATURE_BIT (31U)
342#define FFA_FEATURES_FEATURE_MBZ_HI_BIT (30U)
343#define FFA_FEATURES_FEATURE_MBZ_LO_BIT (8U)
344
345#define FFA_FEATURES_NS_SUPPORT_BIT (1U)
J-Alves6f72ca82021-11-01 12:34:58 +0000346
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100347/* FF-A function specific constants. */
348#define FFA_MSG_RECV_BLOCK 0x1
349#define FFA_MSG_RECV_BLOCK_MASK 0x1
350
351#define FFA_MSG_SEND_NOTIFY 0x1
352#define FFA_MSG_SEND_NOTIFY_MASK 0x1
353
354#define FFA_MEM_RECLAIM_CLEAR 0x1
355
356#define FFA_SLEEP_INDEFINITE 0
357
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700358#define FFA_MEM_PERM_RO UINT32_C(0x7)
359#define FFA_MEM_PERM_RW UINT32_C(0x5)
360#define FFA_MEM_PERM_RX UINT32_C(0x3)
361
Kathleen Capella7253bd52024-08-14 17:36:09 -0400362#define FFA_MSG_WAIT_FLAG_RETAIN_RX UINT32_C(0x1)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000363/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100364 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000365 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
366 * if partition info descriptors should be returned or just the count.
367 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100368#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
369#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000370
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100371/**
372 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
373 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
374 * configured to use.
375 */
J-Alves715d6232023-02-16 16:33:28 +0000376#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100377
Federico Recanatifc0295e2022-02-08 19:37:39 +0100378/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100379typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100380
381/**
J-Alves661e1b72023-08-02 13:39:40 +0100382 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
383 * parts:
384 * - Bit15 -> partition type identifier.
385 * - b'0 -> ID relates to a VM ID.
386 * - b'1 -> ID relates to an SP ID.
387 */
388#define FFA_ID_MASK ((ffa_id_t)0x8000)
389#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
390
391/**
392 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
393 */
394static inline bool ffa_is_vm_id(ffa_id_t id)
395{
396 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
397}
398
399/**
Karl Meakinc88c8e92024-11-29 16:13:55 +0000400 * Holds the UUID in a struct that is mappable directly to the SMCC calling
401 * convention, which is used for FF-A calls.
402 *
403 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
404 * of the SMCC Spec 1.2.
405 */
406struct ffa_uuid {
407 uint32_t uuid[4];
408};
409
410static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
411 uint32_t w3, struct ffa_uuid *uuid)
412{
413 uuid->uuid[0] = w0;
414 uuid->uuid[1] = w1;
415 uuid->uuid[2] = w2;
416 uuid->uuid[3] = w3;
417}
418
419static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
420 const struct ffa_uuid *uuid2)
421{
422 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
423 (uuid1->uuid[1] == uuid2->uuid[1]) &&
424 (uuid1->uuid[2] == uuid2->uuid[2]) &&
425 (uuid1->uuid[3] == uuid2->uuid[3]);
426}
427
428static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
429{
430 struct ffa_uuid null = {0};
431
432 return ffa_uuid_equal(uuid, &null);
433}
434
435static inline void ffa_uuid_from_u64x2(uint64_t uuid_lo, uint64_t uuid_hi,
436 struct ffa_uuid *uuid)
437{
438 ffa_uuid_init((uint32_t)(uuid_lo & 0xFFFFFFFFU),
439 (uint32_t)(uuid_lo >> 32),
440 (uint32_t)(uuid_hi & 0xFFFFFFFFU),
441 (uint32_t)(uuid_hi >> 32), uuid);
442}
443
444/**
445 * Split `uuid` into two u64s.
446 * This function writes to pointer parameters because C does not allow returning
447 * arrays from functions.
448 */
449static inline void ffa_uuid_to_u64x2(uint64_t *lo, uint64_t *hi,
450 const struct ffa_uuid *uuid)
451{
452 *lo = (uint64_t)uuid->uuid[1] << 32 | uuid->uuid[0];
453 *hi = (uint64_t)uuid->uuid[3] << 32 | uuid->uuid[2];
454}
455
456/**
457 * Partition message header as specified by table 7.1 from FF-A v1.3 ALP0
Federico Recanatifc0295e2022-02-08 19:37:39 +0100458 * specification.
459 */
460struct ffa_partition_rxtx_header {
Karl Meakinbf580c22024-12-12 14:31:06 +0000461 /* Reserved (SBZ). */
462 uint32_t flags;
463 /* Reserved (SBZ). */
464 uint32_t reserved_1;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100465 /* Offset from the beginning of the buffer to the message payload. */
466 uint32_t offset;
Karl Meakin9ca05512024-11-29 17:02:32 +0000467 /* Receiver endpoint ID. */
468 ffa_id_t receiver;
469 /* Sender endpoint ID. */
470 ffa_id_t sender;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100471 /* Size of message in buffer. */
472 uint32_t size;
Karl Meakinbf580c22024-12-12 14:31:06 +0000473 /* Reserved (SBZ). Added in v1.2. */
474 uint32_t reserved_2;
475 /* UUID identifying the communication protocol. Added in v1.2. */
476 struct ffa_uuid uuid;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100477};
478
Karl Meakinbf580c22024-12-12 14:31:06 +0000479#define FFA_RXTX_HEADER_SIZE_V1_1 \
480 offsetof(struct ffa_partition_rxtx_header, reserved_2)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100481#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
J-Alves70079932022-12-07 17:32:20 +0000482#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100483
Karl Meakin895007c2025-01-13 15:48:07 +0000484/**
Karl Meakinbf580c22024-12-12 14:31:06 +0000485 * Initialize a partition message header, with the default values for `flags`,
486 * `offset` and `uuid` and the v1.1 payload offset.
487 */
488static inline void ffa_rxtx_header_init_v1_1(
489 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
490 ffa_id_t receiver, uint32_t payload_size)
491{
492 header->flags = 0;
493 header->reserved_1 = 0;
494 header->offset = FFA_RXTX_HEADER_SIZE_V1_1;
495 header->sender = sender;
496 header->receiver = receiver;
497 header->size = payload_size;
498 header->reserved_2 = 0;
499 header->uuid = (struct ffa_uuid){0};
500}
501
502/**
503 * Initialize a partition message header, with the default values for `flags`,
504 * `offset` and `uuid`.
Karl Meakin895007c2025-01-13 15:48:07 +0000505 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100506static inline void ffa_rxtx_header_init(
Karl Meakin895007c2025-01-13 15:48:07 +0000507 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
508 ffa_id_t receiver, uint32_t payload_size)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100509{
510 header->flags = 0;
Karl Meakinbf580c22024-12-12 14:31:06 +0000511 header->reserved_1 = 0;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100512 header->offset = FFA_RXTX_HEADER_SIZE;
Karl Meakin9ca05512024-11-29 17:02:32 +0000513 header->sender = sender;
514 header->receiver = receiver;
Karl Meakin895007c2025-01-13 15:48:07 +0000515 header->size = payload_size;
Karl Meakinbf580c22024-12-12 14:31:06 +0000516 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 * and `offset`.
523 */
524static inline void ffa_rxtx_header_init_with_uuid(
525 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
526 ffa_id_t receiver, uint32_t size, struct ffa_uuid uuid)
527{
528 header->flags = 0;
529 header->reserved_1 = 0;
530 header->offset = FFA_RXTX_HEADER_SIZE;
531 header->sender = sender;
532 header->receiver = receiver;
533 header->size = size;
534 header->reserved_2 = 0;
535 header->uuid = uuid;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100536}
537
Federico Recanatifc0295e2022-02-08 19:37:39 +0100538/* The maximum length possible for a single message. */
Karl Meakinbf580c22024-12-12 14:31:06 +0000539#define FFA_PARTITION_MSG_PAYLOAD_MAX_V1_1 \
540 (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE_V1_1)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100541#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
542
543struct ffa_partition_msg {
544 struct ffa_partition_rxtx_header header;
Karl Meakinbf580c22024-12-12 14:31:06 +0000545 /**
546 * Prefer using `ffa_partition_msg_payload` to accessing this field
547 * directly, because the offset does not necessarily correspond to the
548 * offset of this field.
549 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100550 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
551};
552
Karl Meakinbf580c22024-12-12 14:31:06 +0000553static_assert(sizeof(struct ffa_partition_msg) == HF_MAILBOX_SIZE,
554 "FF-A message size must match mailbox size");
555
Karl Meakin891eb882025-01-17 19:11:20 +0000556/**
557 * Get the partition message's payload, according to the header's `offset`
558 * field.
559 */
560static inline void *ffa_partition_msg_payload(struct ffa_partition_msg *msg)
561{
562 return (char *)msg + msg->header.offset;
563}
564
565static inline const void *ffa_partition_msg_payload_const(
566 const struct ffa_partition_msg *msg)
567{
568 return (const char *)msg + msg->header.offset;
569}
570
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100571/* The maximum length possible for a single message. */
572#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
573
574enum ffa_data_access {
575 FFA_DATA_ACCESS_NOT_SPECIFIED,
576 FFA_DATA_ACCESS_RO,
577 FFA_DATA_ACCESS_RW,
578 FFA_DATA_ACCESS_RESERVED,
579};
580
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100581static inline const char *ffa_data_access_name(enum ffa_data_access data_access)
582{
583 switch (data_access) {
584 case FFA_DATA_ACCESS_NOT_SPECIFIED:
585 return "FFA_DATA_ACCESS_NOT_SPECIFIED";
586 case FFA_DATA_ACCESS_RO:
587 return "FFA_DATA_ACCESS_RO";
588 case FFA_DATA_ACCESS_RW:
589 return "FFA_DATA_ACCESS_RW";
590 case FFA_DATA_ACCESS_RESERVED:
591 return "FFA_DATA_ACCESS_RESERVED";
592 }
593}
594
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100595enum ffa_instruction_access {
596 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
597 FFA_INSTRUCTION_ACCESS_NX,
598 FFA_INSTRUCTION_ACCESS_X,
599 FFA_INSTRUCTION_ACCESS_RESERVED,
600};
601
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100602static inline const char *ffa_instruction_access_name(
603 enum ffa_instruction_access instruction_access)
604{
605 switch (instruction_access) {
606 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
607 return "FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED";
608 case FFA_INSTRUCTION_ACCESS_NX:
609 return "FFA_INSTRUCTION_ACCESS_NX";
610 case FFA_INSTRUCTION_ACCESS_X:
611 return "FFA_INSTRUCTION_ACCESS_X";
612 case FFA_INSTRUCTION_ACCESS_RESERVED:
613 return "FFA_INSTRUCTION_ACCESS_RESERVED";
614 }
615}
616
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100617enum ffa_memory_type {
618 FFA_MEMORY_NOT_SPECIFIED_MEM,
619 FFA_MEMORY_DEVICE_MEM,
620 FFA_MEMORY_NORMAL_MEM,
621};
622
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100623static inline const char *ffa_memory_type_name(enum ffa_memory_type type)
624{
625 switch (type) {
626 case FFA_MEMORY_NOT_SPECIFIED_MEM:
627 return "FFA_MEMORY_NOT_SPECIFIED_MEM";
628 case FFA_MEMORY_DEVICE_MEM:
629 return "FFA_MEMORY_DEVICE_MEM";
630 case FFA_MEMORY_NORMAL_MEM:
631 return "FFA_MEMORY_NORMAL_MEM";
632 }
633}
634
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100635enum ffa_memory_cacheability {
636 FFA_MEMORY_CACHE_RESERVED = 0x0,
637 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
638 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
639 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
640 FFA_MEMORY_DEV_NGNRNE = 0x0,
641 FFA_MEMORY_DEV_NGNRE = 0x1,
642 FFA_MEMORY_DEV_NGRE = 0x2,
643 FFA_MEMORY_DEV_GRE = 0x3,
644};
645
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100646static inline const char *ffa_memory_cacheability_name(
647 enum ffa_memory_cacheability cacheability)
648{
649 switch (cacheability) {
650 case FFA_MEMORY_CACHE_RESERVED:
651 return "FFA_MEMORY_CACHE_RESERVED";
652 case FFA_MEMORY_CACHE_NON_CACHEABLE:
653 return "FFA_MEMORY_CACHE_NON_CACHEABLE";
654 case FFA_MEMORY_CACHE_RESERVED_1:
655 return "FFA_MEMORY_CACHE_RESERVED_1";
656 case FFA_MEMORY_CACHE_WRITE_BACK:
657 return "FFA_MEMORY_CACHE_WRITE_BACK";
658 }
659}
660
Daniel Boulby9764ff62024-01-30 17:47:39 +0000661static inline const char *ffa_device_memory_cacheability_name(
662 enum ffa_memory_cacheability cacheability)
663{
664 switch (cacheability) {
665 case FFA_MEMORY_DEV_NGNRNE:
666 return "FFA_MEMORY_DEV_NGNRNE";
667 case FFA_MEMORY_DEV_NGNRE:
668 return "FFA_MEMORY_DEV_NGNRE";
669 case FFA_MEMORY_DEV_NGRE:
670 return "FFA_MEMORY_DEV_NGRE";
671 case FFA_MEMORY_DEV_GRE:
672 return "FFA_MEMORY_DEV_GRE";
673 }
674}
675
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676enum ffa_memory_shareability {
677 FFA_MEMORY_SHARE_NON_SHAREABLE,
678 FFA_MEMORY_SHARE_RESERVED,
679 FFA_MEMORY_OUTER_SHAREABLE,
680 FFA_MEMORY_INNER_SHAREABLE,
681};
682
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100683static inline const char *ffa_memory_shareability_name(
684 enum ffa_memory_shareability shareability)
685{
686 switch (shareability) {
687 case FFA_MEMORY_SHARE_NON_SHAREABLE:
688 return "FFA_MEMORY_SHARE_NON_SHAREABLE";
689 case FFA_MEMORY_SHARE_RESERVED:
690 return "FFA_MEMORY_SHARE_RESERVED";
691 case FFA_MEMORY_OUTER_SHAREABLE:
692 return "FFA_MEMORY_OUTER_SHAREABLE";
693 case FFA_MEMORY_INNER_SHAREABLE:
694 return "FFA_MEMORY_INNER_SHAREABLE";
695 }
696}
697
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100698/**
699 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
700 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
701 * and FFA_MEM_RETRIEVE_REQUEST.
702 */
703enum ffa_memory_security {
704 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
705 FFA_MEMORY_SECURITY_SECURE = 0,
706 FFA_MEMORY_SECURITY_NON_SECURE,
707};
708
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100709static inline const char *ffa_memory_security_name(
710 enum ffa_memory_security security)
711{
712 switch (security) {
713 case FFA_MEMORY_SECURITY_UNSPECIFIED:
714 return "FFA_MEMORY_SECURITY_UNSPECIFIED";
715 case FFA_MEMORY_SECURITY_NON_SECURE:
716 return "FFA_MEMORY_SECURITY_NON_SECURE";
717 }
718}
719
Karl Meakin84710f32023-10-12 15:14:49 +0100720typedef struct {
721 uint8_t data_access : 2;
722 uint8_t instruction_access : 2;
723} ffa_memory_access_permissions_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100724
725/**
J-Alves0b6653d2022-04-22 13:17:38 +0100726 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100727 * region attributes descriptor".
728 */
Karl Meakin84710f32023-10-12 15:14:49 +0100729typedef struct {
730 uint8_t shareability : 2;
731 uint8_t cacheability : 2;
732 uint8_t type : 2;
733 uint8_t security : 2;
734 uint8_t : 8;
735} ffa_memory_attributes_t;
J-Alves0b6653d2022-04-22 13:17:38 +0100736
737/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
738#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100739
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100740/**
741 * A globally-unique ID assigned by the hypervisor for a region of memory being
742 * sent between VMs.
743 */
744typedef uint64_t ffa_memory_handle_t;
745
Karl Meakin1a760e72024-07-25 18:58:37 +0100746enum ffa_memory_handle_allocator {
747 FFA_MEMORY_HANDLE_ALLOCATOR_SPMC = 0,
748 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR = 1,
749};
J-Alves917d2f22020-10-30 18:39:30 +0000750
Karl Meakin1a760e72024-07-25 18:58:37 +0100751#define FFA_MEMORY_HANDLE_ALLOCATOR_BIT UINT64_C(63)
752#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
753 (UINT64_C(1) << FFA_MEMORY_HANDLE_ALLOCATOR_BIT)
J-Alves917d2f22020-10-30 18:39:30 +0000754#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
755
Karl Meakin1a760e72024-07-25 18:58:37 +0100756static inline ffa_memory_handle_t ffa_memory_handle_make(
757 uint64_t index, enum ffa_memory_handle_allocator allocator)
758{
759 return index | ((uint64_t)allocator << FFA_MEMORY_HANDLE_ALLOCATOR_BIT);
760}
761
762static inline uint64_t ffa_memory_handle_index(ffa_memory_handle_t handle)
763{
764 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
765}
766
767static inline enum ffa_memory_handle_allocator ffa_memory_handle_allocator(
768 ffa_memory_handle_t handle)
769{
770 return ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) != 0)
771 ? FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR
772 : FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
773}
774
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100775/**
776 * A count of VMs. This has the same range as the VM IDs but we give it a
777 * different name to make the different semantics clear.
778 */
J-Alves19e20cf2023-08-02 12:48:55 +0100779typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100780
781/** The index of a vCPU within a particular VM. */
782typedef uint16_t ffa_vcpu_index_t;
783
784/**
785 * A count of vCPUs. This has the same range as the vCPU indices but we give it
786 * a different name to make the different semantics clear.
787 */
788typedef ffa_vcpu_index_t ffa_vcpu_count_t;
789
790/** Parameter and return type of FF-A functions. */
791struct ffa_value {
792 uint64_t func;
793 uint64_t arg1;
794 uint64_t arg2;
795 uint64_t arg3;
796 uint64_t arg4;
797 uint64_t arg5;
798 uint64_t arg6;
799 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800800
801 struct {
802 uint64_t arg8;
803 uint64_t arg9;
804 uint64_t arg10;
805 uint64_t arg11;
806 uint64_t arg12;
807 uint64_t arg13;
808 uint64_t arg14;
809 uint64_t arg15;
810 uint64_t arg16;
811 uint64_t arg17;
812 bool valid;
813 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814};
815
Olivier Depreze562e542020-06-11 17:31:54 +0200816static inline uint32_t ffa_func_id(struct ffa_value args)
817{
818 return args.func;
819}
820
Karl Meakindc759f52024-05-07 16:08:02 +0100821static inline enum ffa_error ffa_error_code(struct ffa_value val)
J-Alves13318e32021-02-22 17:21:00 +0000822{
Karl Meakin66a38bd2024-05-28 16:00:56 +0100823 /* NOLINTNEXTLINE(EnumCastOutOfRange) */
Karl Meakindc759f52024-05-07 16:08:02 +0100824 return (enum ffa_error)val.arg2;
J-Alves13318e32021-02-22 17:21:00 +0000825}
826
J-Alves19e20cf2023-08-02 12:48:55 +0100827static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100828{
829 return (args.arg1 >> 16) & 0xffff;
830}
831
J-Alves19e20cf2023-08-02 12:48:55 +0100832static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100833{
834 return args.arg1 & 0xffff;
835}
836
837static inline uint32_t ffa_msg_send_size(struct ffa_value args)
838{
839 return args.arg3;
840}
841
Federico Recanati25053ee2022-03-14 15:01:53 +0100842static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
843{
844 return args.arg2;
845}
846
Olivier Depreze562e542020-06-11 17:31:54 +0200847static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
848{
849 return args.arg2;
850}
851
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800852static inline uint16_t ffa_partition_info_regs_get_last_idx(
853 struct ffa_value args)
854{
855 return args.arg2 & 0xFFFF;
856}
857
858static inline uint16_t ffa_partition_info_regs_get_curr_idx(
859 struct ffa_value args)
860{
861 return (args.arg2 >> 16) & 0xFFFF;
862}
863
864static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
865{
866 return (args.arg2 >> 32) & 0xFFFF;
867}
868
869static inline uint16_t ffa_partition_info_regs_get_desc_size(
870 struct ffa_value args)
871{
872 return (args.arg2 >> 48);
873}
874
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100875static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
876{
877 return (uint64_t)a1 | (uint64_t)a2 << 32;
878}
879
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100880static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
881{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100882 return ffa_assemble_handle(args.arg2, args.arg3);
883}
884
Andrew Walbranca808b12020-05-15 17:22:28 +0100885static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
886{
887 return ffa_assemble_handle(args.arg1, args.arg2);
888}
889
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100890static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
891{
892 return (struct ffa_value){.func = FFA_SUCCESS_32,
893 .arg2 = (uint32_t)handle,
894 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100895}
896
J-Alves19e20cf2023-08-02 12:48:55 +0100897static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100898{
899 return (args.arg1 >> 16) & 0xffff;
900}
901
902static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
903{
904 return args.arg1 & 0xffff;
905}
906
J-Alves19e20cf2023-08-02 12:48:55 +0100907static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908{
909 return ((uint32_t)vm_id << 16) | vcpu_index;
910}
911
J-Alves19e20cf2023-08-02 12:48:55 +0100912static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100913{
914 return (args.arg4 >> 16) & 0xffff;
915}
916
J-Alves6f72ca82021-11-01 12:34:58 +0000917static inline uint32_t ffa_feature_intid(struct ffa_value args)
918{
919 return (uint32_t)args.arg2;
920}
921
Karl Meakind0356f82024-09-04 13:34:31 +0100922#define FFA_FRAMEWORK_MSG_BIT (UINT64_C(1) << 31)
923#define FFA_FRAMEWORK_MSG_FUNC_MASK UINT64_C(0xFF)
924
925/**
926 * Identifies the VM availability message. See section 18.3 of v1.2 FF-A
927 * specification.
928 */
929enum ffa_framework_msg_func {
930 FFA_FRAMEWORK_MSG_VM_CREATION_REQ = 4,
931 FFA_FRAMEWORK_MSG_VM_CREATION_RESP = 5,
932
933 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_REQ = 6,
934 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_RESP = 7,
935};
936
Karl Meakin06e8b732024-09-20 18:26:49 +0100937#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_LO 16
938#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_HI 31
939
Karl Meakind0356f82024-09-04 13:34:31 +0100940/** Get the `flags` field of a framework message */
941static inline uint32_t ffa_framework_msg_flags(struct ffa_value args)
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000942{
943 return (uint32_t)args.arg2;
944}
945
Karl Meakind0356f82024-09-04 13:34:31 +0100946/** Is `args` a framework message? */
947static inline bool ffa_is_framework_msg(struct ffa_value args)
948{
Karl Meakin06e8b732024-09-20 18:26:49 +0100949 return (args.func != FFA_MSG_SEND_DIRECT_REQ2_64) &&
950 (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) &&
951 ((ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_BIT) != 0);
Karl Meakind0356f82024-09-04 13:34:31 +0100952}
953
Karl Meakina1a02352024-09-20 18:27:18 +0100954/**
955 * Get the ID of the VM that has been created/destroyed from VM availability
956 * message
957 */
958static inline ffa_id_t ffa_vm_availability_message_vm_id(struct ffa_value args)
959{
960 return args.arg5 & 0xFFFF;
961}
962
Karl Meakind0356f82024-09-04 13:34:31 +0100963/** Get the function ID from a framework message */
964static inline uint32_t ffa_framework_msg_func(struct ffa_value args)
965{
966 return ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_FUNC_MASK;
967}
968
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100969/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100970 * Flags to determine the partition properties, as required by
971 * FFA_PARTITION_INFO_GET.
972 *
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400973 * 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 +0100974 * specification, "Partition information descriptor, partition properties".
975 */
976typedef uint32_t ffa_partition_properties_t;
977
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400978/**
979 * Partition property: partition supports receipt of direct requests via the
980 * FFA_MSG_SEND_DIRECT_REQ ABI.
981 */
Kathleen Capella402fa852022-11-09 16:16:51 -0500982#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100983
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400984/**
985 * Partition property: partition can send direct requests via the
986 * FFA_MSG_SEND_DIRECT_REQ ABI.
987 */
Kathleen Capella402fa852022-11-09 16:16:51 -0500988#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100989
990/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500991#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100992
J-Alves09ff9d82021-11-02 11:55:20 +0000993/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500994#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
995
Karl Meakina603e082024-08-02 17:57:27 +0100996/**
997 * Partition property: partition must be informed about each VM that is created
998 * by the Hypervisor.
999 */
1000#define FFA_PARTITION_VM_CREATED (UINT32_C(1) << 6)
1001
1002/**
1003 * Partition property: partition must be informed about each VM that is
1004 * destroyed by the Hypervisor.
1005 */
1006#define FFA_PARTITION_VM_DESTROYED (UINT32_C(1) << 7)
1007
Kathleen Capella402fa852022-11-09 16:16:51 -05001008/** Partition property: partition runs in the AArch64 execution state. */
1009#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +00001010
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001011/**
Kathleen Capellaf71dee42023-08-08 16:24:14 -04001012 * Partition property: partition supports receipt of direct requests via the
1013 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
1014 */
1015#define FFA_PARTITION_DIRECT_REQ2_RECV (UINT32_C(1) << 9)
1016
1017/**
1018 * Partition property: partition can send direct requests via the
1019 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
1020 */
1021#define FFA_PARTITION_DIRECT_REQ2_SEND (UINT32_C(1) << 10)
1022
1023/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001024 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
1025 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -05001026 * This corresponds to table 13.37 "Partition information descriptor"
1027 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +00001028 */
1029struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +01001030 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +00001031 ffa_vcpu_count_t vcpu_count;
1032 ffa_partition_properties_t properties;
1033 struct ffa_uuid uuid;
1034};
1035
J-Alvesdd1ad572022-01-25 17:58:26 +00001036/** Length in bytes of the name in boot information descriptor. */
1037#define FFA_BOOT_INFO_NAME_LEN 16
1038
J-Alves240d84c2022-04-22 12:19:34 +01001039/**
1040 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
1041 * the FF-A v1.1 EAC0 specification.
1042 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001043struct ffa_boot_info_desc {
1044 char name[FFA_BOOT_INFO_NAME_LEN];
1045 uint8_t type;
1046 uint8_t reserved;
1047 uint16_t flags;
1048 uint32_t size;
1049 uint64_t content;
1050};
1051
1052/** FF-A boot information type mask. */
1053#define FFA_BOOT_INFO_TYPE_SHIFT 7
1054#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
1055#define FFA_BOOT_INFO_TYPE_STD 0U
1056#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
1057
1058/** Standard boot info type IDs. */
1059#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
1060#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
1061#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
1062
1063/** FF-A Boot Info descriptors flags. */
1064#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
1065
1066/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
1067#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
1068#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
1069 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
1070#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
1071#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
1072
1073/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
1074#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
1075#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
1076 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
1077#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
1078#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
1079
1080static inline uint16_t ffa_boot_info_content_format(
1081 struct ffa_boot_info_desc *desc)
1082{
1083 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
1084 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
1085}
1086
1087static inline uint16_t ffa_boot_info_name_format(
1088 struct ffa_boot_info_desc *desc)
1089{
1090 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
1091 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
1092}
1093
1094static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
1095{
1096 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
1097}
1098
1099static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
1100{
1101 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
1102 FFA_BOOT_INFO_TYPE_SHIFT;
1103}
1104
1105/** Length in bytes of the signature in the boot descriptor. */
1106#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
1107
J-Alves240d84c2022-04-22 12:19:34 +01001108/**
1109 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
1110 * of the FF-A v1.1 EAC0 specification.
1111 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001112struct ffa_boot_info_header {
1113 uint32_t signature;
1114 uint32_t version;
1115 uint32_t info_blob_size;
1116 uint32_t desc_size;
1117 uint32_t desc_count;
1118 uint32_t desc_offset;
1119 uint64_t reserved;
1120 struct ffa_boot_info_desc boot_info[];
1121};
1122
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001123/**
J-Alves980d1992021-03-18 12:49:18 +00001124 * FF-A v1.1 specification restricts the number of notifications to a maximum
1125 * of 64. Following all possible bitmaps.
1126 */
Karl Meakin2ad6b662024-07-29 20:45:40 +01001127#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << (ID))
J-Alves980d1992021-03-18 12:49:18 +00001128
1129typedef uint64_t ffa_notifications_bitmap_t;
1130
1131#define MAX_FFA_NOTIFICATIONS 64U
1132
1133/**
J-Alvesc003a7a2021-03-18 13:06:53 +00001134 * Flag for notification bind and set, to specify call is about per-vCPU
1135 * notifications.
1136 */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001137#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +00001138
Federico Recanatie73d2832022-04-20 11:10:52 +02001139#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
1140#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
1141
1142/**
1143 * Helper functions to check for buffer full notification.
1144 */
1145static inline bool is_ffa_hyp_buffer_full_notification(
1146 ffa_notifications_bitmap_t framework)
1147{
1148 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
1149}
1150
1151static inline bool is_ffa_spm_buffer_full_notification(
1152 ffa_notifications_bitmap_t framework)
1153{
1154 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
1155}
1156
J-Alvesc003a7a2021-03-18 13:06:53 +00001157/**
J-Alves980d1992021-03-18 12:49:18 +00001158 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
1159 * and hi.
1160 * Helpful as FF-A specification defines that the notifications interfaces
1161 * arguments are 32-bit registers.
1162 */
1163static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
1164 uint32_t hi)
1165{
1166 return (ffa_notifications_bitmap_t)hi << 32U | lo;
1167}
1168
J-Alves98ff9562021-09-09 14:39:41 +01001169static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
1170 struct ffa_value val)
1171{
1172 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
1173}
1174
1175static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
1176 struct ffa_value val)
1177{
1178 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
1179}
1180
Federico Recanatie73d2832022-04-20 11:10:52 +02001181static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
1182 struct ffa_value val)
1183{
1184 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
1185}
1186
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001187typedef uint32_t ffa_notification_flags_t;
1188
1189/** Flags used in calls to FFA_NOTIFICATION_BIND interface. */
1190#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
1191
1192/** Flags used in calls to FFA_NOTIFICATION_GET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001193#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
1194#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
1195#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
1196#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +01001197
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001198/** Flags used in calls to FFA_NOTIFICATION_SET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001199#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001200#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001201#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
1202 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001203#define FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(flags) \
1204 ((ffa_vcpu_index_t)((flags) >> 16))
J-Alves13394022021-06-30 13:48:49 +01001205
J-Alvesbe6e3032021-11-30 14:54:12 +00001206static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +01001207{
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001208 return FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(args.arg1);
J-Alvesaa79c012021-07-09 14:29:45 +01001209}
1210
1211/**
J-Alvesc8e8a222021-06-08 17:33:52 +01001212 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
1213 */
1214#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
1215
1216/**
1217 * Number of registers to use in successfull return of interface
1218 * FFA_NOTIFICATION_INFO_GET.
1219 */
1220#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
1221
1222#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
1223
1224/**
1225 * Helper macros for return parameter encoding as described in section 17.7.1
1226 * of the FF-A v1.1 Beta0 specification.
1227 */
1228#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
1229#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
Karl Meakin2ad6b662024-07-29 20:45:40 +01001230#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * ((l) - 1) + 12)
J-Alvesc8e8a222021-06-08 17:33:52 +01001231#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
Daniel Boulby1308a632024-09-11 15:19:16 +01001232#define FFA_NOTIFICATIONS_LIST_MAX_SIZE 0x4U
Daniel Boulbyedfdf282025-01-02 18:51:19 +00001233#define FFA_NOTIFICATIONS_LIST_MAX_VCPU_IDS \
1234 (FFA_NOTIFICATIONS_LIST_MAX_SIZE - 1)
J-Alvesc8e8a222021-06-08 17:33:52 +01001235
1236static inline uint32_t ffa_notification_info_get_lists_count(
1237 struct ffa_value args)
1238{
1239 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
1240 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
1241}
1242
1243static inline uint32_t ffa_notification_info_get_list_size(
1244 struct ffa_value args, unsigned int list_idx)
1245{
1246 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
1247 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
1248}
1249
1250static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
1251{
1252 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
1253}
1254
Daniel Boulby1308a632024-09-11 15:19:16 +01001255void ffa_notification_info_get_and_check(
1256 const uint32_t expected_lists_count,
1257 const uint32_t *const expected_lists_sizes,
1258 const uint16_t *const expected_ids);
1259
J-Alvesc8e8a222021-06-08 17:33:52 +01001260/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001261 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +01001262 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
1263 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001264 */
1265struct ffa_memory_region_constituent {
1266 /**
1267 * The base IPA of the constituent memory region, aligned to 4 kiB page
1268 * size granularity.
1269 */
1270 uint64_t address;
1271 /** The number of 4 kiB pages in the constituent memory region. */
1272 uint32_t page_count;
1273 /** Reserved field, must be 0. */
1274 uint32_t reserved;
1275};
1276
1277/**
J-Alves0b6653d2022-04-22 13:17:38 +01001278 * A set of pages comprising a memory region. This corresponds to table 10.13 of
1279 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001280 */
1281struct ffa_composite_memory_region {
1282 /**
1283 * The total number of 4 kiB pages included in this memory region. This
1284 * must be equal to the sum of page counts specified in each
1285 * `ffa_memory_region_constituent`.
1286 */
1287 uint32_t page_count;
1288 /**
1289 * The number of constituents (`ffa_memory_region_constituent`)
1290 * included in this memory region range.
1291 */
1292 uint32_t constituent_count;
1293 /** Reserved field, must be 0. */
1294 uint64_t reserved_0;
1295 /** An array of `constituent_count` memory region constituents. */
1296 struct ffa_memory_region_constituent constituents[];
1297};
1298
1299/** Flags to indicate properties of receivers during memory region retrieval. */
1300typedef uint8_t ffa_memory_receiver_flags_t;
1301
1302/**
J-Alves0b6653d2022-04-22 13:17:38 +01001303 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001304 * access permissions descriptor".
1305 */
1306struct ffa_memory_region_attributes {
1307 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001308 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001309 /**
1310 * The permissions with which the memory region should be mapped in the
1311 * receiver's page table.
1312 */
1313 ffa_memory_access_permissions_t permissions;
1314 /**
1315 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1316 * for memory regions with multiple borrowers.
1317 */
1318 ffa_memory_receiver_flags_t flags;
1319};
1320
1321/** Flags to control the behaviour of a memory sharing transaction. */
1322typedef uint32_t ffa_memory_region_flags_t;
1323
1324/**
1325 * Clear memory region contents after unmapping it from the sender and before
1326 * mapping it for any receiver.
1327 */
1328#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1329
1330/**
1331 * Whether the hypervisor may time slice the memory sharing or retrieval
1332 * operation.
1333 */
1334#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1335
1336/**
1337 * Whether the hypervisor should clear the memory region after the receiver
1338 * relinquishes it or is aborted.
1339 */
1340#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1341
J-Alves3456e032023-07-20 12:20:05 +01001342/**
1343 * On retrieve request, bypass the multi-borrower check.
1344 */
1345#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1346
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001347#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1348#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1349#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1350#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1351#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1352
Federico Recanati85090c42021-12-15 13:17:54 +01001353#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1354#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1355
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001356/**
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001357 * Struct to store the impdef value seen in Table 11.16 of the
1358 * FF-A v1.2 ALP0 specification "Endpoint memory access descriptor".
1359 */
1360struct ffa_memory_access_impdef {
1361 uint64_t val[2];
1362};
1363
Daniel Boulbye0ca9a02024-03-05 18:40:59 +00001364static inline struct ffa_memory_access_impdef ffa_memory_access_impdef_init(
1365 uint64_t impdef_hi, uint64_t impdef_lo)
1366{
1367 return (struct ffa_memory_access_impdef){{impdef_hi, impdef_lo}};
1368}
1369
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001370/**
J-Alves0b6653d2022-04-22 13:17:38 +01001371 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1372 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001373 */
1374struct ffa_memory_access {
1375 struct ffa_memory_region_attributes receiver_permissions;
1376 /**
1377 * Offset in bytes from the start of the outer `ffa_memory_region` to
1378 * an `ffa_composite_memory_region` struct.
1379 */
1380 uint32_t composite_memory_region_offset;
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001381 struct ffa_memory_access_impdef impdef;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001382 uint64_t reserved_0;
1383};
1384
J-Alves363f5722022-04-25 17:37:37 +01001385/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001386#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001387
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001388/**
1389 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001390 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1391 * memory transaction descriptor". Note that it is also used for retrieve
1392 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001393 */
1394struct ffa_memory_region {
1395 /**
1396 * The ID of the VM which originally sent the memory region, i.e. the
1397 * owner.
1398 */
J-Alves19e20cf2023-08-02 12:48:55 +01001399 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001400 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001401 /** Flags to control behaviour of the transaction. */
1402 ffa_memory_region_flags_t flags;
1403 ffa_memory_handle_t handle;
1404 /**
1405 * An implementation defined value associated with the receiver and the
1406 * memory region.
1407 */
1408 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001409 /* Size of the memory access descriptor. */
1410 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001411 /**
1412 * The number of `ffa_memory_access` entries included in this
1413 * transaction.
1414 */
1415 uint32_t receiver_count;
1416 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001417 * Offset of the 'receivers' field, which relates to the memory access
1418 * descriptors.
1419 */
1420 uint32_t receivers_offset;
1421 /** Reserved field (12 bytes) must be 0. */
1422 uint32_t reserved[3];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001423};
1424
1425/**
1426 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001427 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001428 * region".
1429 */
1430struct ffa_mem_relinquish {
1431 ffa_memory_handle_t handle;
1432 ffa_memory_region_flags_t flags;
1433 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001434 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001435};
1436
1437/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001438 * Returns the first FF-A version that matches the memory access descriptor
1439 * size.
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001440 */
Karl Meakin0e617d92024-04-05 12:55:22 +01001441enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01001442 uint32_t memory_access_desc_size);
1443
1444/**
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001445 * To maintain forwards compatability we can't make assumptions about the size
1446 * of the endpoint memory access descriptor so provide a helper function
1447 * to get a receiver from the receiver array using the memory access descriptor
1448 * size field from the memory region descriptor struct.
1449 * Returns NULL if we cannot return the receiver.
1450 */
1451static inline struct ffa_memory_access *ffa_memory_region_get_receiver(
1452 struct ffa_memory_region *memory_region, uint32_t receiver_index)
1453{
1454 uint32_t memory_access_desc_size =
1455 memory_region->memory_access_desc_size;
1456
1457 if (receiver_index >= memory_region->receiver_count) {
1458 return NULL;
1459 }
1460
1461 /*
1462 * Memory access descriptor size cannot be greater than the size of
1463 * the memory access descriptor defined by the current FF-A version.
1464 */
1465 if (memory_access_desc_size > sizeof(struct ffa_memory_access)) {
1466 return NULL;
1467 }
1468
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001469 /* Check we cannot use receivers offset to cause overflow. */
1470 if (memory_region->receivers_offset !=
1471 sizeof(struct ffa_memory_region)) {
1472 return NULL;
1473 }
1474
Karl Meakin2ad6b662024-07-29 20:45:40 +01001475 return (struct ffa_memory_access
1476 *)((uint8_t *)memory_region +
1477 (size_t)memory_region->receivers_offset +
1478 (size_t)(receiver_index * memory_access_desc_size));
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001479}
1480
1481/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001482 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1483 * returns its index in the receiver's array. If receiver's ID doesn't exist
1484 * in the array, return the region's 'receivers_count'.
1485 */
1486static inline uint32_t ffa_memory_region_get_receiver_index(
1487 struct ffa_memory_region *memory_region, ffa_id_t receiver_id)
1488{
1489 uint32_t i;
1490
1491 for (i = 0U; i < memory_region->receiver_count; i++) {
1492 struct ffa_memory_access *receiver =
1493 ffa_memory_region_get_receiver(memory_region, i);
1494 if (receiver->receiver_permissions.receiver == receiver_id) {
1495 break;
1496 }
1497 }
1498
1499 return i;
1500}
1501
1502/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001503 * Gets the `ffa_composite_memory_region` for the given receiver from an
1504 * `ffa_memory_region`, or NULL if it is not valid.
1505 */
1506static inline struct ffa_composite_memory_region *
1507ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1508 uint32_t receiver_index)
1509{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001510 struct ffa_memory_access *receiver =
1511 ffa_memory_region_get_receiver(memory_region, receiver_index);
1512 uint32_t offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001513
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001514 if (receiver == NULL) {
1515 return NULL;
1516 }
1517
1518 offset = receiver->composite_memory_region_offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001519 if (offset == 0) {
1520 return NULL;
1521 }
1522
1523 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1524 offset);
1525}
1526
1527static inline uint32_t ffa_mem_relinquish_init(
1528 struct ffa_mem_relinquish *relinquish_request,
1529 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001530 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001531{
1532 relinquish_request->handle = handle;
1533 relinquish_request->flags = flags;
1534 relinquish_request->endpoint_count = 1;
1535 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001536 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001537}
1538
J-Alves126ab502022-09-29 11:37:33 +01001539void ffa_copy_memory_region_constituents(
1540 struct ffa_memory_region_constituent *dest,
1541 const struct ffa_memory_region_constituent *src);
1542
Karl Meakin0fd67292024-02-06 17:33:05 +00001543struct ffa_features_rxtx_map_params {
1544 /*
1545 * Bit[0:1]:
1546 * Minimum buffer size and alignment boundary:
1547 * 0b00: 4K
1548 * 0b01: 64K
1549 * 0b10: 16K
1550 * 0b11: Reserved
1551 */
1552 uint8_t min_buf_size : 2;
1553 /*
1554 * Bit[2:15]:
1555 * Reserved (MBZ)
1556 */
1557 uint16_t mbz : 14;
1558 /*
1559 * Bit[16:32]:
1560 * Maximum buffer size in number of pages
1561 * Only present on version 1.2 or later
1562 */
1563 uint16_t max_buf_size : 16;
1564};
1565
Karl Meakin49ec1e42024-05-10 13:08:24 +01001566enum ffa_features_rxtx_map_buf_size {
1567 FFA_RXTX_MAP_MIN_BUF_4K = 0,
1568 FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT = 1,
1569};
Karl Meakin0fd67292024-02-06 17:33:05 +00001570
Karl Meakinf7861302024-02-20 14:39:33 +00001571static inline struct ffa_features_rxtx_map_params ffa_features_rxtx_map_params(
1572 struct ffa_value args)
1573{
1574 struct ffa_features_rxtx_map_params params;
1575 uint32_t arg2 = args.arg2;
1576
1577 params = *(struct ffa_features_rxtx_map_params *)(&arg2);
1578
1579 return params;
1580}
1581
Federico Recanati392be392022-02-08 20:53:03 +01001582/**
1583 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1584 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1585 * to the SPMC, in order to allow indirect messaging.
1586 */
1587struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001588 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001589 uint16_t reserved;
1590
1591 /*
1592 * 8-byte aligned offset from the base address of this descriptor to the
1593 * `ffa_composite_memory_region` describing the RX buffer.
1594 */
1595 uint32_t rx_offset;
1596
1597 /*
1598 * 8-byte aligned offset from the base address of this descriptor to the
1599 * `ffa_composite_memory_region` describing the TX buffer.
1600 */
1601 uint32_t tx_offset;
1602
1603 /* Pad to align on 16-byte boundary. */
1604 uint32_t pad;
1605};
1606
1607static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001608ffa_endpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001609{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001610 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001611 desc->rx_offset);
1612}
1613
1614static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001615ffa_endpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001616{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001617 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001618 desc->tx_offset);
1619}
1620
J-Alves2d8457f2022-10-05 11:06:41 +01001621void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001622 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001623 ffa_memory_attributes_t attributes,
1624 ffa_memory_region_flags_t flags,
1625 ffa_memory_handle_t handle, uint32_t tag,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001626 uint32_t receiver_count,
1627 uint32_t receiver_desc_size);
Daniel Boulby59ffee92023-11-02 18:26:26 +00001628void ffa_memory_access_init(struct ffa_memory_access *receiver,
1629 ffa_id_t receiver_id,
1630 enum ffa_data_access data_access,
1631 enum ffa_instruction_access instruction_access,
1632 ffa_memory_receiver_flags_t flags,
1633 struct ffa_memory_access_impdef *impdef_val);
J-Alves45085432022-04-22 16:19:20 +01001634uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001635 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001636 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001637 const struct ffa_memory_region_constituent constituents[],
1638 uint32_t constituent_count, uint32_t tag,
1639 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1640 enum ffa_instruction_access instruction_access,
1641 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001642 enum ffa_memory_shareability shareability,
1643 struct ffa_memory_access_impdef *impdef_val, uint32_t *fragment_length,
Andrew Walbranca808b12020-05-15 17:22:28 +01001644 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001645uint32_t ffa_memory_region_init(
1646 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001647 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001648 uint32_t receiver_count, uint32_t receiver_desc_size,
J-Alvesf4eecf72022-07-20 16:05:34 +01001649 const struct ffa_memory_region_constituent constituents[],
1650 uint32_t constituent_count, uint32_t tag,
1651 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1652 enum ffa_memory_cacheability cacheability,
1653 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1654 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001655uint32_t ffa_memory_retrieve_request_init(
1656 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001657 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001658 uint32_t receiver_count, uint32_t receiver_desc_size, uint32_t tag,
1659 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1660 enum ffa_memory_cacheability cacheability,
J-Alves9b24ed82022-08-04 13:12:45 +01001661 enum ffa_memory_shareability shareability);
1662uint32_t ffa_memory_retrieve_request_init_single_receiver(
1663 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001664 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001665 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);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001670uint32_t ffa_memory_lender_retrieve_request_init(
1671 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001672 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001673uint32_t ffa_memory_fragment_init(
1674 struct ffa_memory_region_constituent *fragment,
1675 size_t fragment_max_size,
1676 const struct ffa_memory_region_constituent constituents[],
1677 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001678void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001679 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001680 uint64_t rx_address, uint64_t tx_address);