blob: 25cb081e20ef6c7e5f7d207abf48354f97b9846f [file] [log] [blame]
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 The Hafnium Authors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01007 */
8
9#pragma once
10
11#include "hf/types.h"
12
Karl Meakin0e617d92024-04-05 12:55:22 +010013/**
14 * The version number of a Firmware Framework implementation is a 31-bit
15 * unsigned integer, with the upper 15 bits denoting the major revision,
16 * and the lower 16 bits denoting the minor revision.
17 *
18 * See FF-A specification v1.2 ALP1, section 13.2.1.
19 */
20enum ffa_version {
21 FFA_VERSION_1_0 = 0x10000,
22 FFA_VERSION_1_1 = 0x10001,
23 FFA_VERSION_1_2 = 0x10002,
24 FFA_VERSION_COMPILED = FFA_VERSION_1_2,
25};
Daniel Boulby6e32c612021-02-17 15:09:41 +000026
Karl Meakin0e617d92024-04-05 12:55:22 +010027#define FFA_VERSION_MBZ_BIT (1U << 31U)
28#define FFA_VERSION_MAJOR_SHIFT (16U)
29#define FFA_VERSION_MAJOR_MASK (0x7FFFU)
30#define FFA_VERSION_MINOR_SHIFT (0U)
31#define FFA_VERSION_MINOR_MASK (0xFFFFU)
32
33/** Return true if the version is valid (i.e. bit 31 is 0). */
34static inline bool ffa_version_is_valid(uint32_t version)
35{
36 return (version & FFA_VERSION_MBZ_BIT) == 0;
37}
38
39/** Construct a version from a pair of major and minor components. */
40static inline enum ffa_version make_ffa_version(uint16_t major, uint16_t minor)
41{
42 return (enum ffa_version)((major << FFA_VERSION_MAJOR_SHIFT) |
43 (minor << FFA_VERSION_MINOR_SHIFT));
44}
45
46/** Get the major component of the version. */
47static inline uint16_t ffa_version_get_major(enum ffa_version version)
48{
49 return (version >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
50}
51
52/** Get the minor component of the version. */
53static inline uint16_t ffa_version_get_minor(enum ffa_version version)
54{
55 return (version >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK;
56}
Olivier Deprez62d99e32020-01-09 15:58:07 +010057
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010058/**
59 * Check major versions are equal and the minor version of the caller is
60 * less than or equal to the minor version of the callee.
61 */
Karl Meakin0e617d92024-04-05 12:55:22 +010062static inline bool ffa_versions_are_compatible(enum ffa_version caller,
63 enum ffa_version callee)
64{
65 return ffa_version_get_major(caller) == ffa_version_get_major(callee) &&
66 ffa_version_get_minor(caller) <= ffa_version_get_minor(callee);
67}
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010068
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010069/* clang-format off */
70
71#define FFA_LOW_32_ID 0x84000060
72#define FFA_HIGH_32_ID 0x8400007F
73#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010074#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010075
Karl Meakinf51a35f2023-08-07 17:53:52 +010076/**
77 * FF-A function identifiers.
78 * Don't forget to update `ffa_func_name` if you add a new one.
79 */
J-Alves3829fc02021-03-18 12:49:18 +000080#define FFA_ERROR_32 0x84000060
81#define FFA_SUCCESS_32 0x84000061
82#define FFA_SUCCESS_64 0xC4000061
83#define FFA_INTERRUPT_32 0x84000062
84#define FFA_VERSION_32 0x84000063
85#define FFA_FEATURES_32 0x84000064
86#define FFA_RX_RELEASE_32 0x84000065
87#define FFA_RXTX_MAP_32 0x84000066
88#define FFA_RXTX_MAP_64 0xC4000066
89#define FFA_RXTX_UNMAP_32 0x84000067
90#define FFA_PARTITION_INFO_GET_32 0x84000068
91#define FFA_ID_GET_32 0x84000069
92#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
93#define FFA_MSG_WAIT_32 0x8400006B
94#define FFA_YIELD_32 0x8400006C
95#define FFA_RUN_32 0x8400006D
96#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
97#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
98#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
99#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
100#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
101#define FFA_MEM_DONATE_32 0x84000071
J-Alves95fbb312024-03-20 15:19:16 +0000102#define FFA_MEM_DONATE_64 0xC4000071
J-Alves3829fc02021-03-18 12:49:18 +0000103#define FFA_MEM_LEND_32 0x84000072
J-Alves95fbb312024-03-20 15:19:16 +0000104#define FFA_MEM_LEND_64 0xC4000072
J-Alves3829fc02021-03-18 12:49:18 +0000105#define FFA_MEM_SHARE_32 0x84000073
J-Alves95fbb312024-03-20 15:19:16 +0000106#define FFA_MEM_SHARE_64 0xC4000073
J-Alves3829fc02021-03-18 12:49:18 +0000107#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
J-Alves95fbb312024-03-20 15:19:16 +0000108#define FFA_MEM_RETRIEVE_REQ_64 0xC4000074
J-Alves3829fc02021-03-18 12:49:18 +0000109#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
110#define FFA_MEM_RELINQUISH_32 0x84000076
111#define FFA_MEM_RECLAIM_32 0x84000077
112#define FFA_MEM_FRAG_RX_32 0x8400007A
113#define FFA_MEM_FRAG_TX_32 0x8400007B
114#define FFA_NORMAL_WORLD_RESUME 0x8400007C
115
116/* FF-A v1.1 */
117#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
118#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
119#define FFA_NOTIFICATION_BIND_32 0x8400007F
120#define FFA_NOTIFICATION_UNBIND_32 0x84000080
121#define FFA_NOTIFICATION_SET_32 0x84000081
122#define FFA_NOTIFICATION_GET_32 0x84000082
123#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
124#define FFA_RX_ACQUIRE_32 0x84000084
125#define FFA_SPM_ID_GET_32 0x84000085
126#define FFA_MSG_SEND2_32 0x84000086
127#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
128#define FFA_MEM_PERM_GET_32 0x84000088
129#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700130#define FFA_MEM_PERM_GET_64 0xC4000088
131#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100132
Kathleen Capellae4fe2962023-09-01 17:08:47 -0400133/* FF-A v1.2 */
Maksims Svecovs71b76702022-05-20 15:32:58 +0100134#define FFA_CONSOLE_LOG_32 0x8400008A
135#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800136#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -0600137#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Kathleen Capella41fea932023-06-23 17:39:28 -0400138#define FFA_MSG_SEND_DIRECT_REQ2_64 0xC400008D
Kathleen Capella087e5022023-09-07 18:04:15 -0400139#define FFA_MSG_SEND_DIRECT_RESP2_64 0xC400008E
Maksims Svecovs71b76702022-05-20 15:32:58 +0100140
Karl Meakinf51a35f2023-08-07 17:53:52 +0100141/**
142 * FF-A error codes.
143 * Don't forget to update `ffa_error_name` if you add a new one.
144 */
Karl Meakindc759f52024-05-07 16:08:02 +0100145enum ffa_error {
146 FFA_NOT_SUPPORTED = -1,
147 FFA_INVALID_PARAMETERS = -2,
148 FFA_NO_MEMORY = -3,
149 FFA_BUSY = -4,
150 FFA_INTERRUPTED = -5,
151 FFA_DENIED = -6,
152 FFA_RETRY = -7,
153 FFA_ABORTED = -8,
154 FFA_NO_DATA = -9,
155 FFA_NOT_READY = -10,
156};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100157
158/* clang-format on */
159
Karl Meakinf51a35f2023-08-07 17:53:52 +0100160/* Return the name of the function identifier. */
161static inline const char *ffa_func_name(uint32_t func)
162{
163 switch (func) {
164 case FFA_ERROR_32:
165 return "FFA_ERROR_32";
166 case FFA_SUCCESS_32:
167 return "FFA_SUCCESS_32";
168 case FFA_SUCCESS_64:
169 return "FFA_SUCCESS_64";
170 case FFA_INTERRUPT_32:
171 return "FFA_INTERRUPT_32";
172 case FFA_VERSION_32:
173 return "FFA_VERSION_32";
174 case FFA_FEATURES_32:
175 return "FFA_FEATURES_32";
176 case FFA_RX_RELEASE_32:
177 return "FFA_RX_RELEASE_32";
178 case FFA_RXTX_MAP_32:
179 return "FFA_RXTX_MAP_32";
180 case FFA_RXTX_MAP_64:
181 return "FFA_RXTX_MAP_64";
182 case FFA_RXTX_UNMAP_32:
183 return "FFA_RXTX_UNMAP_32";
184 case FFA_PARTITION_INFO_GET_32:
185 return "FFA_PARTITION_INFO_GET_32";
186 case FFA_ID_GET_32:
187 return "FFA_ID_GET_32";
188 case FFA_MSG_POLL_32:
189 return "FFA_MSG_POLL_32";
190 case FFA_MSG_WAIT_32:
191 return "FFA_MSG_WAIT_32";
192 case FFA_YIELD_32:
193 return "FFA_YIELD_32";
194 case FFA_RUN_32:
195 return "FFA_RUN_32";
196 case FFA_MSG_SEND_32:
197 return "FFA_MSG_SEND_32";
198 case FFA_MSG_SEND_DIRECT_REQ_32:
199 return "FFA_MSG_SEND_DIRECT_REQ_32";
200 case FFA_MSG_SEND_DIRECT_REQ_64:
201 return "FFA_MSG_SEND_DIRECT_REQ_64";
202 case FFA_MSG_SEND_DIRECT_RESP_32:
203 return "FFA_MSG_SEND_DIRECT_RESP_32";
204 case FFA_MSG_SEND_DIRECT_RESP_64:
205 return "FFA_MSG_SEND_DIRECT_RESP_64";
206 case FFA_MEM_DONATE_32:
207 return "FFA_MEM_DONATE_32";
208 case FFA_MEM_LEND_32:
209 return "FFA_MEM_LEND_32";
210 case FFA_MEM_SHARE_32:
211 return "FFA_MEM_SHARE_32";
212 case FFA_MEM_RETRIEVE_REQ_32:
213 return "FFA_MEM_RETRIEVE_REQ_32";
J-Alves95fbb312024-03-20 15:19:16 +0000214 case FFA_MEM_DONATE_64:
215 return "FFA_MEM_DONATE_64";
216 case FFA_MEM_LEND_64:
217 return "FFA_MEM_LEND_64";
218 case FFA_MEM_SHARE_64:
219 return "FFA_MEM_SHARE_64";
220 case FFA_MEM_RETRIEVE_REQ_64:
221 return "FFA_MEM_RETRIEVE_REQ_64";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100222 case FFA_MEM_RETRIEVE_RESP_32:
223 return "FFA_MEM_RETRIEVE_RESP_32";
224 case FFA_MEM_RELINQUISH_32:
225 return "FFA_MEM_RELINQUISH_32";
226 case FFA_MEM_RECLAIM_32:
227 return "FFA_MEM_RECLAIM_32";
228 case FFA_MEM_FRAG_RX_32:
229 return "FFA_MEM_FRAG_RX_32";
230 case FFA_MEM_FRAG_TX_32:
231 return "FFA_MEM_FRAG_TX_32";
232 case FFA_NORMAL_WORLD_RESUME:
233 return "FFA_NORMAL_WORLD_RESUME";
234
235 /* FF-A v1.1 */
236 case FFA_NOTIFICATION_BITMAP_CREATE_32:
237 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
238 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
239 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
240 case FFA_NOTIFICATION_BIND_32:
241 return "FFA_NOTIFICATION_BIND_32";
242 case FFA_NOTIFICATION_UNBIND_32:
243 return "FFA_NOTIFICATION_UNBIND_32";
244 case FFA_NOTIFICATION_SET_32:
245 return "FFA_NOTIFICATION_SET_32";
246 case FFA_NOTIFICATION_GET_32:
247 return "FFA_NOTIFICATION_GET_32";
248 case FFA_NOTIFICATION_INFO_GET_64:
249 return "FFA_NOTIFICATION_INFO_GET_64";
250 case FFA_RX_ACQUIRE_32:
251 return "FFA_RX_ACQUIRE_32";
252 case FFA_SPM_ID_GET_32:
253 return "FFA_SPM_ID_GET_32";
254 case FFA_MSG_SEND2_32:
255 return "FFA_MSG_SEND2_32";
256 case FFA_SECONDARY_EP_REGISTER_64:
257 return "FFA_SECONDARY_EP_REGISTER_64";
258 case FFA_MEM_PERM_GET_32:
259 return "FFA_MEM_PERM_GET_32";
260 case FFA_MEM_PERM_SET_32:
261 return "FFA_MEM_PERM_SET_32";
262 case FFA_MEM_PERM_GET_64:
263 return "FFA_MEM_PERM_GET_64";
264 case FFA_MEM_PERM_SET_64:
265 return "FFA_MEM_PERM_SET_64";
266
267 /* Implementation-defined ABIs. */
268 case FFA_CONSOLE_LOG_32:
269 return "FFA_CONSOLE_LOG_32";
270 case FFA_CONSOLE_LOG_64:
271 return "FFA_CONSOLE_LOG_64";
272 case FFA_PARTITION_INFO_GET_REGS_64:
273 return "FFA_PARTITION_INFO_GET_REGS_64";
274 case FFA_EL3_INTR_HANDLE_32:
275 return "FFA_EL3_INTR_HANDLE_32";
276
277 default:
278 return "UNKNOWN";
279 }
280}
281
282/* Return the name of the error code. */
Karl Meakindc759f52024-05-07 16:08:02 +0100283static inline const char *ffa_error_name(enum ffa_error error)
Karl Meakinf51a35f2023-08-07 17:53:52 +0100284{
285 switch (error) {
286 case FFA_NOT_SUPPORTED:
287 return "FFA_NOT_SUPPORTED";
288 case FFA_INVALID_PARAMETERS:
289 return "FFA_INVALID_PARAMETERS";
290 case FFA_NO_MEMORY:
291 return "FFA_NO_MEMORY";
292 case FFA_BUSY:
293 return "FFA_BUSY";
294 case FFA_INTERRUPTED:
295 return "FFA_INTERRUPTED";
296 case FFA_DENIED:
297 return "FFA_DENIED";
298 case FFA_RETRY:
299 return "FFA_RETRY";
300 case FFA_ABORTED:
301 return "FFA_ABORTED";
302 case FFA_NO_DATA:
303 return "FFA_NO_DATA";
Karl Meakindc759f52024-05-07 16:08:02 +0100304 case FFA_NOT_READY:
305 return "FFA_NOT_READY";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100306 }
Karl Meakindc759f52024-05-07 16:08:02 +0100307 return "UNKNOWN";
Karl Meakinf51a35f2023-08-07 17:53:52 +0100308}
309
J-Alves6f72ca82021-11-01 12:34:58 +0000310/**
Karl Meakin49ec1e42024-05-10 13:08:24 +0100311 * Defined in Table 3.1 in the FF-A v.1.2 memory management supplement.
312 * Input properties:
313 * - Bits[31:2] and Bit[0] are reserved (SBZ).
314 * Output properties:
315 * - Bit[0]: dynamically allocated buffer support.
316 * - Bit[1]: NS bit handling.
317 * - Bit[2]: support for retrieval by hypervisor.
318 * - Bits[31:3] are reserved (MBZ).
J-Alves6f72ca82021-11-01 12:34:58 +0000319 */
Karl Meakin49ec1e42024-05-10 13:08:24 +0100320#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT (0U << 0U)
321#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (1U << 1U)
322#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (1U << 2U)
J-Alves6f72ca82021-11-01 12:34:58 +0000323
Karl Meakin49ec1e42024-05-10 13:08:24 +0100324#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_HI_BIT (31U)
325#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_LO_BIT (2U)
326#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_BIT (0U)
J-Alves6f72ca82021-11-01 12:34:58 +0000327
Karl Meakin49ec1e42024-05-10 13:08:24 +0100328enum ffa_feature_id {
329 /* Query interrupt ID of Notification Pending Interrupt. */
330 FFA_FEATURE_NPI = 1,
Karl Meakin34b8ae92023-01-13 13:33:07 +0000331
Karl Meakin49ec1e42024-05-10 13:08:24 +0100332 /* Query interrupt ID of Schedule Receiver Interrupt. */
333 FFA_FEATURE_SRI = 2,
J-Alves6f72ca82021-11-01 12:34:58 +0000334
Karl Meakin49ec1e42024-05-10 13:08:24 +0100335 /* Query interrupt ID of the Managed Exit Interrupt. */
336 FFA_FEATURE_MEI = 3,
337};
J-Alves6f72ca82021-11-01 12:34:58 +0000338
Karl Meakin49ec1e42024-05-10 13:08:24 +0100339/** Constants for bitmasks used in FFA_FEATURES. */
340#define FFA_FEATURES_FEATURE_BIT (31U)
341#define FFA_FEATURES_FEATURE_MBZ_HI_BIT (30U)
342#define FFA_FEATURES_FEATURE_MBZ_LO_BIT (8U)
343
344#define FFA_FEATURES_NS_SUPPORT_BIT (1U)
J-Alves6f72ca82021-11-01 12:34:58 +0000345
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100346/* FF-A function specific constants. */
347#define FFA_MSG_RECV_BLOCK 0x1
348#define FFA_MSG_RECV_BLOCK_MASK 0x1
349
350#define FFA_MSG_SEND_NOTIFY 0x1
351#define FFA_MSG_SEND_NOTIFY_MASK 0x1
352
353#define FFA_MEM_RECLAIM_CLEAR 0x1
354
355#define FFA_SLEEP_INDEFINITE 0
356
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700357#define FFA_MEM_PERM_RO UINT32_C(0x7)
358#define FFA_MEM_PERM_RW UINT32_C(0x5)
359#define FFA_MEM_PERM_RX UINT32_C(0x3)
360
Kathleen Capella7253bd52024-08-14 17:36:09 -0400361#define FFA_MSG_WAIT_FLAG_RETAIN_RX UINT32_C(0x1)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000362/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100363 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000364 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
365 * if partition info descriptors should be returned or just the count.
366 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100367#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
368#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000369
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100370/**
371 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
372 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
373 * configured to use.
374 */
J-Alves715d6232023-02-16 16:33:28 +0000375#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376
Federico Recanatifc0295e2022-02-08 19:37:39 +0100377/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100378typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100379
380/**
J-Alves661e1b72023-08-02 13:39:40 +0100381 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
382 * parts:
383 * - Bit15 -> partition type identifier.
384 * - b'0 -> ID relates to a VM ID.
385 * - b'1 -> ID relates to an SP ID.
386 */
387#define FFA_ID_MASK ((ffa_id_t)0x8000)
388#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
389
390/**
391 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
392 */
393static inline bool ffa_is_vm_id(ffa_id_t id)
394{
395 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
396}
397
398/**
Federico Recanatifc0295e2022-02-08 19:37:39 +0100399 * Partition message header as specified by table 6.2 from FF-A v1.1 EAC0
400 * specification.
401 */
402struct ffa_partition_rxtx_header {
403 uint32_t flags; /* MBZ */
404 uint32_t reserved;
405 /* Offset from the beginning of the buffer to the message payload. */
406 uint32_t offset;
Karl Meakin9ca05512024-11-29 17:02:32 +0000407 /* Receiver endpoint ID. */
408 ffa_id_t receiver;
409 /* Sender endpoint ID. */
410 ffa_id_t sender;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100411 /* Size of message in buffer. */
412 uint32_t size;
413};
414
415#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
J-Alves70079932022-12-07 17:32:20 +0000416#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100417
Karl Meakin895007c2025-01-13 15:48:07 +0000418/**
419 * Initialize a partition message header, with the default values for `flags`
420 * and `offset`.
421 */
Federico Recanatifc0295e2022-02-08 19:37:39 +0100422static inline void ffa_rxtx_header_init(
Karl Meakin895007c2025-01-13 15:48:07 +0000423 struct ffa_partition_rxtx_header *header, ffa_id_t sender,
424 ffa_id_t receiver, uint32_t payload_size)
Federico Recanatifc0295e2022-02-08 19:37:39 +0100425{
426 header->flags = 0;
427 header->reserved = 0;
428 header->offset = FFA_RXTX_HEADER_SIZE;
Karl Meakin9ca05512024-11-29 17:02:32 +0000429 header->sender = sender;
430 header->receiver = receiver;
Karl Meakin895007c2025-01-13 15:48:07 +0000431 header->size = payload_size;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100432}
433
Federico Recanatifc0295e2022-02-08 19:37:39 +0100434/* The maximum length possible for a single message. */
435#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
436
437struct ffa_partition_msg {
438 struct ffa_partition_rxtx_header header;
439 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
440};
441
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100442/* The maximum length possible for a single message. */
443#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
444
445enum ffa_data_access {
446 FFA_DATA_ACCESS_NOT_SPECIFIED,
447 FFA_DATA_ACCESS_RO,
448 FFA_DATA_ACCESS_RW,
449 FFA_DATA_ACCESS_RESERVED,
450};
451
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100452static inline const char *ffa_data_access_name(enum ffa_data_access data_access)
453{
454 switch (data_access) {
455 case FFA_DATA_ACCESS_NOT_SPECIFIED:
456 return "FFA_DATA_ACCESS_NOT_SPECIFIED";
457 case FFA_DATA_ACCESS_RO:
458 return "FFA_DATA_ACCESS_RO";
459 case FFA_DATA_ACCESS_RW:
460 return "FFA_DATA_ACCESS_RW";
461 case FFA_DATA_ACCESS_RESERVED:
462 return "FFA_DATA_ACCESS_RESERVED";
463 }
464}
465
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100466enum ffa_instruction_access {
467 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
468 FFA_INSTRUCTION_ACCESS_NX,
469 FFA_INSTRUCTION_ACCESS_X,
470 FFA_INSTRUCTION_ACCESS_RESERVED,
471};
472
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100473static inline const char *ffa_instruction_access_name(
474 enum ffa_instruction_access instruction_access)
475{
476 switch (instruction_access) {
477 case FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED:
478 return "FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED";
479 case FFA_INSTRUCTION_ACCESS_NX:
480 return "FFA_INSTRUCTION_ACCESS_NX";
481 case FFA_INSTRUCTION_ACCESS_X:
482 return "FFA_INSTRUCTION_ACCESS_X";
483 case FFA_INSTRUCTION_ACCESS_RESERVED:
484 return "FFA_INSTRUCTION_ACCESS_RESERVED";
485 }
486}
487
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100488enum ffa_memory_type {
489 FFA_MEMORY_NOT_SPECIFIED_MEM,
490 FFA_MEMORY_DEVICE_MEM,
491 FFA_MEMORY_NORMAL_MEM,
492};
493
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100494static inline const char *ffa_memory_type_name(enum ffa_memory_type type)
495{
496 switch (type) {
497 case FFA_MEMORY_NOT_SPECIFIED_MEM:
498 return "FFA_MEMORY_NOT_SPECIFIED_MEM";
499 case FFA_MEMORY_DEVICE_MEM:
500 return "FFA_MEMORY_DEVICE_MEM";
501 case FFA_MEMORY_NORMAL_MEM:
502 return "FFA_MEMORY_NORMAL_MEM";
503 }
504}
505
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100506enum ffa_memory_cacheability {
507 FFA_MEMORY_CACHE_RESERVED = 0x0,
508 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
509 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
510 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
511 FFA_MEMORY_DEV_NGNRNE = 0x0,
512 FFA_MEMORY_DEV_NGNRE = 0x1,
513 FFA_MEMORY_DEV_NGRE = 0x2,
514 FFA_MEMORY_DEV_GRE = 0x3,
515};
516
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100517static inline const char *ffa_memory_cacheability_name(
518 enum ffa_memory_cacheability cacheability)
519{
520 switch (cacheability) {
521 case FFA_MEMORY_CACHE_RESERVED:
522 return "FFA_MEMORY_CACHE_RESERVED";
523 case FFA_MEMORY_CACHE_NON_CACHEABLE:
524 return "FFA_MEMORY_CACHE_NON_CACHEABLE";
525 case FFA_MEMORY_CACHE_RESERVED_1:
526 return "FFA_MEMORY_CACHE_RESERVED_1";
527 case FFA_MEMORY_CACHE_WRITE_BACK:
528 return "FFA_MEMORY_CACHE_WRITE_BACK";
529 }
530}
531
Daniel Boulby9764ff62024-01-30 17:47:39 +0000532static inline const char *ffa_device_memory_cacheability_name(
533 enum ffa_memory_cacheability cacheability)
534{
535 switch (cacheability) {
536 case FFA_MEMORY_DEV_NGNRNE:
537 return "FFA_MEMORY_DEV_NGNRNE";
538 case FFA_MEMORY_DEV_NGNRE:
539 return "FFA_MEMORY_DEV_NGNRE";
540 case FFA_MEMORY_DEV_NGRE:
541 return "FFA_MEMORY_DEV_NGRE";
542 case FFA_MEMORY_DEV_GRE:
543 return "FFA_MEMORY_DEV_GRE";
544 }
545}
546
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100547enum ffa_memory_shareability {
548 FFA_MEMORY_SHARE_NON_SHAREABLE,
549 FFA_MEMORY_SHARE_RESERVED,
550 FFA_MEMORY_OUTER_SHAREABLE,
551 FFA_MEMORY_INNER_SHAREABLE,
552};
553
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100554static inline const char *ffa_memory_shareability_name(
555 enum ffa_memory_shareability shareability)
556{
557 switch (shareability) {
558 case FFA_MEMORY_SHARE_NON_SHAREABLE:
559 return "FFA_MEMORY_SHARE_NON_SHAREABLE";
560 case FFA_MEMORY_SHARE_RESERVED:
561 return "FFA_MEMORY_SHARE_RESERVED";
562 case FFA_MEMORY_OUTER_SHAREABLE:
563 return "FFA_MEMORY_OUTER_SHAREABLE";
564 case FFA_MEMORY_INNER_SHAREABLE:
565 return "FFA_MEMORY_INNER_SHAREABLE";
566 }
567}
568
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100569/**
570 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
571 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
572 * and FFA_MEM_RETRIEVE_REQUEST.
573 */
574enum ffa_memory_security {
575 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
576 FFA_MEMORY_SECURITY_SECURE = 0,
577 FFA_MEMORY_SECURITY_NON_SECURE,
578};
579
Karl Meakinf98b2aa2023-10-12 16:09:59 +0100580static inline const char *ffa_memory_security_name(
581 enum ffa_memory_security security)
582{
583 switch (security) {
584 case FFA_MEMORY_SECURITY_UNSPECIFIED:
585 return "FFA_MEMORY_SECURITY_UNSPECIFIED";
586 case FFA_MEMORY_SECURITY_NON_SECURE:
587 return "FFA_MEMORY_SECURITY_NON_SECURE";
588 }
589}
590
Karl Meakin84710f32023-10-12 15:14:49 +0100591typedef struct {
592 uint8_t data_access : 2;
593 uint8_t instruction_access : 2;
594} ffa_memory_access_permissions_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100595
596/**
J-Alves0b6653d2022-04-22 13:17:38 +0100597 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100598 * region attributes descriptor".
599 */
Karl Meakin84710f32023-10-12 15:14:49 +0100600typedef struct {
601 uint8_t shareability : 2;
602 uint8_t cacheability : 2;
603 uint8_t type : 2;
604 uint8_t security : 2;
605 uint8_t : 8;
606} ffa_memory_attributes_t;
J-Alves0b6653d2022-04-22 13:17:38 +0100607
608/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
609#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100610
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100611/**
612 * A globally-unique ID assigned by the hypervisor for a region of memory being
613 * sent between VMs.
614 */
615typedef uint64_t ffa_memory_handle_t;
616
Karl Meakin1a760e72024-07-25 18:58:37 +0100617enum ffa_memory_handle_allocator {
618 FFA_MEMORY_HANDLE_ALLOCATOR_SPMC = 0,
619 FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR = 1,
620};
J-Alves917d2f22020-10-30 18:39:30 +0000621
Karl Meakin1a760e72024-07-25 18:58:37 +0100622#define FFA_MEMORY_HANDLE_ALLOCATOR_BIT UINT64_C(63)
623#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
624 (UINT64_C(1) << FFA_MEMORY_HANDLE_ALLOCATOR_BIT)
J-Alves917d2f22020-10-30 18:39:30 +0000625#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
626
Karl Meakin1a760e72024-07-25 18:58:37 +0100627static inline ffa_memory_handle_t ffa_memory_handle_make(
628 uint64_t index, enum ffa_memory_handle_allocator allocator)
629{
630 return index | ((uint64_t)allocator << FFA_MEMORY_HANDLE_ALLOCATOR_BIT);
631}
632
633static inline uint64_t ffa_memory_handle_index(ffa_memory_handle_t handle)
634{
635 return handle & ~FFA_MEMORY_HANDLE_ALLOCATOR_MASK;
636}
637
638static inline enum ffa_memory_handle_allocator ffa_memory_handle_allocator(
639 ffa_memory_handle_t handle)
640{
641 return ((handle & FFA_MEMORY_HANDLE_ALLOCATOR_MASK) != 0)
642 ? FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR
643 : FFA_MEMORY_HANDLE_ALLOCATOR_SPMC;
644}
645
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646/**
647 * A count of VMs. This has the same range as the VM IDs but we give it a
648 * different name to make the different semantics clear.
649 */
J-Alves19e20cf2023-08-02 12:48:55 +0100650typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100651
652/** The index of a vCPU within a particular VM. */
653typedef uint16_t ffa_vcpu_index_t;
654
655/**
656 * A count of vCPUs. This has the same range as the vCPU indices but we give it
657 * a different name to make the different semantics clear.
658 */
659typedef ffa_vcpu_index_t ffa_vcpu_count_t;
660
661/** Parameter and return type of FF-A functions. */
662struct ffa_value {
663 uint64_t func;
664 uint64_t arg1;
665 uint64_t arg2;
666 uint64_t arg3;
667 uint64_t arg4;
668 uint64_t arg5;
669 uint64_t arg6;
670 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800671
672 struct {
673 uint64_t arg8;
674 uint64_t arg9;
675 uint64_t arg10;
676 uint64_t arg11;
677 uint64_t arg12;
678 uint64_t arg13;
679 uint64_t arg14;
680 uint64_t arg15;
681 uint64_t arg16;
682 uint64_t arg17;
683 bool valid;
684 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100685};
686
Olivier Depreze562e542020-06-11 17:31:54 +0200687static inline uint32_t ffa_func_id(struct ffa_value args)
688{
689 return args.func;
690}
691
Karl Meakindc759f52024-05-07 16:08:02 +0100692static inline enum ffa_error ffa_error_code(struct ffa_value val)
J-Alves13318e32021-02-22 17:21:00 +0000693{
Karl Meakin66a38bd2024-05-28 16:00:56 +0100694 /* NOLINTNEXTLINE(EnumCastOutOfRange) */
Karl Meakindc759f52024-05-07 16:08:02 +0100695 return (enum ffa_error)val.arg2;
J-Alves13318e32021-02-22 17:21:00 +0000696}
697
J-Alves19e20cf2023-08-02 12:48:55 +0100698static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100699{
700 return (args.arg1 >> 16) & 0xffff;
701}
702
J-Alves19e20cf2023-08-02 12:48:55 +0100703static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100704{
705 return args.arg1 & 0xffff;
706}
707
708static inline uint32_t ffa_msg_send_size(struct ffa_value args)
709{
710 return args.arg3;
711}
712
Federico Recanati25053ee2022-03-14 15:01:53 +0100713static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
714{
715 return args.arg2;
716}
717
Olivier Depreze562e542020-06-11 17:31:54 +0200718static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
719{
720 return args.arg2;
721}
722
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800723static inline uint16_t ffa_partition_info_regs_get_last_idx(
724 struct ffa_value args)
725{
726 return args.arg2 & 0xFFFF;
727}
728
729static inline uint16_t ffa_partition_info_regs_get_curr_idx(
730 struct ffa_value args)
731{
732 return (args.arg2 >> 16) & 0xFFFF;
733}
734
735static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
736{
737 return (args.arg2 >> 32) & 0xFFFF;
738}
739
740static inline uint16_t ffa_partition_info_regs_get_desc_size(
741 struct ffa_value args)
742{
743 return (args.arg2 >> 48);
744}
745
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100746static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
747{
748 return (uint64_t)a1 | (uint64_t)a2 << 32;
749}
750
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100751static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
752{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100753 return ffa_assemble_handle(args.arg2, args.arg3);
754}
755
Andrew Walbranca808b12020-05-15 17:22:28 +0100756static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
757{
758 return ffa_assemble_handle(args.arg1, args.arg2);
759}
760
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100761static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
762{
763 return (struct ffa_value){.func = FFA_SUCCESS_32,
764 .arg2 = (uint32_t)handle,
765 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100766}
767
J-Alves19e20cf2023-08-02 12:48:55 +0100768static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100769{
770 return (args.arg1 >> 16) & 0xffff;
771}
772
773static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
774{
775 return args.arg1 & 0xffff;
776}
777
J-Alves19e20cf2023-08-02 12:48:55 +0100778static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100779{
780 return ((uint32_t)vm_id << 16) | vcpu_index;
781}
782
J-Alves19e20cf2023-08-02 12:48:55 +0100783static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100784{
785 return (args.arg4 >> 16) & 0xffff;
786}
787
J-Alves6f72ca82021-11-01 12:34:58 +0000788static inline uint32_t ffa_feature_intid(struct ffa_value args)
789{
790 return (uint32_t)args.arg2;
791}
792
Karl Meakind0356f82024-09-04 13:34:31 +0100793#define FFA_FRAMEWORK_MSG_BIT (UINT64_C(1) << 31)
794#define FFA_FRAMEWORK_MSG_FUNC_MASK UINT64_C(0xFF)
795
796/**
797 * Identifies the VM availability message. See section 18.3 of v1.2 FF-A
798 * specification.
799 */
800enum ffa_framework_msg_func {
801 FFA_FRAMEWORK_MSG_VM_CREATION_REQ = 4,
802 FFA_FRAMEWORK_MSG_VM_CREATION_RESP = 5,
803
804 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_REQ = 6,
805 FFA_FRAMEWORK_MSG_VM_DESTRUCTION_RESP = 7,
806};
807
Karl Meakin06e8b732024-09-20 18:26:49 +0100808#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_LO 16
809#define FFA_VM_AVAILABILITY_MESSAGE_SBZ_HI 31
810
Karl Meakind0356f82024-09-04 13:34:31 +0100811/** Get the `flags` field of a framework message */
812static inline uint32_t ffa_framework_msg_flags(struct ffa_value args)
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000813{
814 return (uint32_t)args.arg2;
815}
816
Karl Meakind0356f82024-09-04 13:34:31 +0100817/** Is `args` a framework message? */
818static inline bool ffa_is_framework_msg(struct ffa_value args)
819{
Karl Meakin06e8b732024-09-20 18:26:49 +0100820 return (args.func != FFA_MSG_SEND_DIRECT_REQ2_64) &&
821 (args.func != FFA_MSG_SEND_DIRECT_RESP2_64) &&
822 ((ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_BIT) != 0);
Karl Meakind0356f82024-09-04 13:34:31 +0100823}
824
Karl Meakina1a02352024-09-20 18:27:18 +0100825/**
826 * Get the ID of the VM that has been created/destroyed from VM availability
827 * message
828 */
829static inline ffa_id_t ffa_vm_availability_message_vm_id(struct ffa_value args)
830{
831 return args.arg5 & 0xFFFF;
832}
833
Karl Meakind0356f82024-09-04 13:34:31 +0100834/** Get the function ID from a framework message */
835static inline uint32_t ffa_framework_msg_func(struct ffa_value args)
836{
837 return ffa_framework_msg_flags(args) & FFA_FRAMEWORK_MSG_FUNC_MASK;
838}
839
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100840/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100841 * Holds the UUID in a struct that is mappable directly to the SMCC calling
842 * convention, which is used for FF-A calls.
843 *
844 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
845 * of the SMCC Spec 1.2.
846 */
847struct ffa_uuid {
848 uint32_t uuid[4];
849};
850
851static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
852 uint32_t w3, struct ffa_uuid *uuid)
853{
854 uuid->uuid[0] = w0;
855 uuid->uuid[1] = w1;
856 uuid->uuid[2] = w2;
857 uuid->uuid[3] = w3;
858}
859
860static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
861 const struct ffa_uuid *uuid2)
862{
863 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
864 (uuid1->uuid[1] == uuid2->uuid[1]) &&
865 (uuid1->uuid[2] == uuid2->uuid[2]) &&
866 (uuid1->uuid[3] == uuid2->uuid[3]);
867}
868
869static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
870{
Karl Meakin60532972024-08-02 16:11:21 +0100871 struct ffa_uuid null = {0};
872
873 return ffa_uuid_equal(uuid, &null);
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100874}
875
Karl Meakin9478e322024-09-23 17:47:09 +0100876static inline void ffa_uuid_from_u64x2(uint64_t uuid_lo, uint64_t uuid_hi,
877 struct ffa_uuid *uuid)
Kathleen Capella41fea932023-06-23 17:39:28 -0400878{
879 ffa_uuid_init((uint32_t)(uuid_lo & 0xFFFFFFFFU),
880 (uint32_t)(uuid_lo >> 32),
881 (uint32_t)(uuid_hi & 0xFFFFFFFFU),
882 (uint32_t)(uuid_hi >> 32), uuid);
883}
Karl Meakin9478e322024-09-23 17:47:09 +0100884
885/**
886 * Split `uuid` into two u64s.
887 * This function writes to pointer parameters because C does not allow returning
888 * arrays from functions.
889 */
890static inline void ffa_uuid_to_u64x2(uint64_t *lo, uint64_t *hi,
891 const struct ffa_uuid *uuid)
892{
893 *lo = (uint64_t)uuid->uuid[1] << 32 | uuid->uuid[0];
894 *hi = (uint64_t)uuid->uuid[3] << 32 | uuid->uuid[2];
895}
896
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100897/**
898 * Flags to determine the partition properties, as required by
899 * FFA_PARTITION_INFO_GET.
900 *
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400901 * 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 +0100902 * specification, "Partition information descriptor, partition properties".
903 */
904typedef uint32_t ffa_partition_properties_t;
905
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400906/**
907 * Partition property: partition supports receipt of direct requests via the
908 * FFA_MSG_SEND_DIRECT_REQ ABI.
909 */
Kathleen Capella402fa852022-11-09 16:16:51 -0500910#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100911
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400912/**
913 * Partition property: partition can send direct requests via the
914 * FFA_MSG_SEND_DIRECT_REQ ABI.
915 */
Kathleen Capella402fa852022-11-09 16:16:51 -0500916#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100917
918/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500919#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100920
J-Alves09ff9d82021-11-02 11:55:20 +0000921/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500922#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
923
Karl Meakina603e082024-08-02 17:57:27 +0100924/**
925 * Partition property: partition must be informed about each VM that is created
926 * by the Hypervisor.
927 */
928#define FFA_PARTITION_VM_CREATED (UINT32_C(1) << 6)
929
930/**
931 * Partition property: partition must be informed about each VM that is
932 * destroyed by the Hypervisor.
933 */
934#define FFA_PARTITION_VM_DESTROYED (UINT32_C(1) << 7)
935
Kathleen Capella402fa852022-11-09 16:16:51 -0500936/** Partition property: partition runs in the AArch64 execution state. */
937#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +0000938
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100939/**
Kathleen Capellaf71dee42023-08-08 16:24:14 -0400940 * Partition property: partition supports receipt of direct requests via the
941 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
942 */
943#define FFA_PARTITION_DIRECT_REQ2_RECV (UINT32_C(1) << 9)
944
945/**
946 * Partition property: partition can send direct requests via the
947 * FFA_MSG_SEND_DIRECT_REQ2 ABI.
948 */
949#define FFA_PARTITION_DIRECT_REQ2_SEND (UINT32_C(1) << 10)
950
951/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100952 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
953 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -0500954 * This corresponds to table 13.37 "Partition information descriptor"
955 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000956 */
957struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +0100958 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000959 ffa_vcpu_count_t vcpu_count;
960 ffa_partition_properties_t properties;
961 struct ffa_uuid uuid;
962};
963
J-Alvesdd1ad572022-01-25 17:58:26 +0000964/** Length in bytes of the name in boot information descriptor. */
965#define FFA_BOOT_INFO_NAME_LEN 16
966
J-Alves240d84c2022-04-22 12:19:34 +0100967/**
968 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
969 * the FF-A v1.1 EAC0 specification.
970 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000971struct ffa_boot_info_desc {
972 char name[FFA_BOOT_INFO_NAME_LEN];
973 uint8_t type;
974 uint8_t reserved;
975 uint16_t flags;
976 uint32_t size;
977 uint64_t content;
978};
979
980/** FF-A boot information type mask. */
981#define FFA_BOOT_INFO_TYPE_SHIFT 7
982#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
983#define FFA_BOOT_INFO_TYPE_STD 0U
984#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
985
986/** Standard boot info type IDs. */
987#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
988#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
989#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
990
991/** FF-A Boot Info descriptors flags. */
992#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
993
994/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
995#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
996#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
997 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
998#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
999#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
1000
1001/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
1002#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
1003#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
1004 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
1005#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
1006#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
1007
1008static inline uint16_t ffa_boot_info_content_format(
1009 struct ffa_boot_info_desc *desc)
1010{
1011 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
1012 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
1013}
1014
1015static inline uint16_t ffa_boot_info_name_format(
1016 struct ffa_boot_info_desc *desc)
1017{
1018 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
1019 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
1020}
1021
1022static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
1023{
1024 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
1025}
1026
1027static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
1028{
1029 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
1030 FFA_BOOT_INFO_TYPE_SHIFT;
1031}
1032
1033/** Length in bytes of the signature in the boot descriptor. */
1034#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
1035
J-Alves240d84c2022-04-22 12:19:34 +01001036/**
1037 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
1038 * of the FF-A v1.1 EAC0 specification.
1039 */
J-Alvesdd1ad572022-01-25 17:58:26 +00001040struct ffa_boot_info_header {
1041 uint32_t signature;
1042 uint32_t version;
1043 uint32_t info_blob_size;
1044 uint32_t desc_size;
1045 uint32_t desc_count;
1046 uint32_t desc_offset;
1047 uint64_t reserved;
1048 struct ffa_boot_info_desc boot_info[];
1049};
1050
Fuad Tabbae4efcc32020-07-16 15:37:27 +01001051/**
J-Alves980d1992021-03-18 12:49:18 +00001052 * FF-A v1.1 specification restricts the number of notifications to a maximum
1053 * of 64. Following all possible bitmaps.
1054 */
Karl Meakin2ad6b662024-07-29 20:45:40 +01001055#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << (ID))
J-Alves980d1992021-03-18 12:49:18 +00001056
1057typedef uint64_t ffa_notifications_bitmap_t;
1058
1059#define MAX_FFA_NOTIFICATIONS 64U
1060
1061/**
J-Alvesc003a7a2021-03-18 13:06:53 +00001062 * Flag for notification bind and set, to specify call is about per-vCPU
1063 * notifications.
1064 */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001065#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +00001066
Federico Recanatie73d2832022-04-20 11:10:52 +02001067#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
1068#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
1069
1070/**
1071 * Helper functions to check for buffer full notification.
1072 */
1073static inline bool is_ffa_hyp_buffer_full_notification(
1074 ffa_notifications_bitmap_t framework)
1075{
1076 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
1077}
1078
1079static inline bool is_ffa_spm_buffer_full_notification(
1080 ffa_notifications_bitmap_t framework)
1081{
1082 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
1083}
1084
J-Alvesc003a7a2021-03-18 13:06:53 +00001085/**
J-Alves980d1992021-03-18 12:49:18 +00001086 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
1087 * and hi.
1088 * Helpful as FF-A specification defines that the notifications interfaces
1089 * arguments are 32-bit registers.
1090 */
1091static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
1092 uint32_t hi)
1093{
1094 return (ffa_notifications_bitmap_t)hi << 32U | lo;
1095}
1096
J-Alves98ff9562021-09-09 14:39:41 +01001097static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
1098 struct ffa_value val)
1099{
1100 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
1101}
1102
1103static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
1104 struct ffa_value val)
1105{
1106 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
1107}
1108
Federico Recanatie73d2832022-04-20 11:10:52 +02001109static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
1110 struct ffa_value val)
1111{
1112 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
1113}
1114
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001115typedef uint32_t ffa_notification_flags_t;
1116
1117/** Flags used in calls to FFA_NOTIFICATION_BIND interface. */
1118#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
1119
1120/** Flags used in calls to FFA_NOTIFICATION_GET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001121#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
1122#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
1123#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
1124#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +01001125
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001126/** Flags used in calls to FFA_NOTIFICATION_SET interface. */
Olivier Deprezb76307d2022-06-09 17:17:45 +02001127#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001128#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
Olivier Deprezb76307d2022-06-09 17:17:45 +02001129#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
1130 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001131#define FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(flags) \
1132 ((ffa_vcpu_index_t)((flags) >> 16))
J-Alves13394022021-06-30 13:48:49 +01001133
J-Alvesbe6e3032021-11-30 14:54:12 +00001134static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +01001135{
Karl Meakinf9c73ce2024-07-30 17:37:13 +01001136 return FFA_NOTIFICATIONS_FLAGS_GET_VCPU_ID(args.arg1);
J-Alvesaa79c012021-07-09 14:29:45 +01001137}
1138
1139/**
J-Alvesc8e8a222021-06-08 17:33:52 +01001140 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
1141 */
1142#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
1143
1144/**
1145 * Number of registers to use in successfull return of interface
1146 * FFA_NOTIFICATION_INFO_GET.
1147 */
1148#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
1149
1150#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
1151
1152/**
1153 * Helper macros for return parameter encoding as described in section 17.7.1
1154 * of the FF-A v1.1 Beta0 specification.
1155 */
1156#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
1157#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
Karl Meakin2ad6b662024-07-29 20:45:40 +01001158#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * ((l) - 1) + 12)
J-Alvesc8e8a222021-06-08 17:33:52 +01001159#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
Daniel Boulby1308a632024-09-11 15:19:16 +01001160#define FFA_NOTIFICATIONS_LIST_MAX_SIZE 0x4U
J-Alvesc8e8a222021-06-08 17:33:52 +01001161
1162static inline uint32_t ffa_notification_info_get_lists_count(
1163 struct ffa_value args)
1164{
1165 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
1166 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
1167}
1168
1169static inline uint32_t ffa_notification_info_get_list_size(
1170 struct ffa_value args, unsigned int list_idx)
1171{
1172 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
1173 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
1174}
1175
1176static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
1177{
1178 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
1179}
1180
Daniel Boulby1308a632024-09-11 15:19:16 +01001181void ffa_notification_info_get_and_check(
1182 const uint32_t expected_lists_count,
1183 const uint32_t *const expected_lists_sizes,
1184 const uint16_t *const expected_ids);
1185
J-Alvesc8e8a222021-06-08 17:33:52 +01001186/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001187 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +01001188 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
1189 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001190 */
1191struct ffa_memory_region_constituent {
1192 /**
1193 * The base IPA of the constituent memory region, aligned to 4 kiB page
1194 * size granularity.
1195 */
1196 uint64_t address;
1197 /** The number of 4 kiB pages in the constituent memory region. */
1198 uint32_t page_count;
1199 /** Reserved field, must be 0. */
1200 uint32_t reserved;
1201};
1202
1203/**
J-Alves0b6653d2022-04-22 13:17:38 +01001204 * A set of pages comprising a memory region. This corresponds to table 10.13 of
1205 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001206 */
1207struct ffa_composite_memory_region {
1208 /**
1209 * The total number of 4 kiB pages included in this memory region. This
1210 * must be equal to the sum of page counts specified in each
1211 * `ffa_memory_region_constituent`.
1212 */
1213 uint32_t page_count;
1214 /**
1215 * The number of constituents (`ffa_memory_region_constituent`)
1216 * included in this memory region range.
1217 */
1218 uint32_t constituent_count;
1219 /** Reserved field, must be 0. */
1220 uint64_t reserved_0;
1221 /** An array of `constituent_count` memory region constituents. */
1222 struct ffa_memory_region_constituent constituents[];
1223};
1224
1225/** Flags to indicate properties of receivers during memory region retrieval. */
1226typedef uint8_t ffa_memory_receiver_flags_t;
1227
1228/**
J-Alves0b6653d2022-04-22 13:17:38 +01001229 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001230 * access permissions descriptor".
1231 */
1232struct ffa_memory_region_attributes {
1233 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001234 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001235 /**
1236 * The permissions with which the memory region should be mapped in the
1237 * receiver's page table.
1238 */
1239 ffa_memory_access_permissions_t permissions;
1240 /**
1241 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1242 * for memory regions with multiple borrowers.
1243 */
1244 ffa_memory_receiver_flags_t flags;
1245};
1246
1247/** Flags to control the behaviour of a memory sharing transaction. */
1248typedef uint32_t ffa_memory_region_flags_t;
1249
1250/**
1251 * Clear memory region contents after unmapping it from the sender and before
1252 * mapping it for any receiver.
1253 */
1254#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1255
1256/**
1257 * Whether the hypervisor may time slice the memory sharing or retrieval
1258 * operation.
1259 */
1260#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1261
1262/**
1263 * Whether the hypervisor should clear the memory region after the receiver
1264 * relinquishes it or is aborted.
1265 */
1266#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1267
J-Alves3456e032023-07-20 12:20:05 +01001268/**
1269 * On retrieve request, bypass the multi-borrower check.
1270 */
1271#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1272
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001273#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1274#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1275#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1276#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1277#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1278
Federico Recanati85090c42021-12-15 13:17:54 +01001279#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1280#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1281
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001282/**
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001283 * Struct to store the impdef value seen in Table 11.16 of the
1284 * FF-A v1.2 ALP0 specification "Endpoint memory access descriptor".
1285 */
1286struct ffa_memory_access_impdef {
1287 uint64_t val[2];
1288};
1289
Daniel Boulbye0ca9a02024-03-05 18:40:59 +00001290static inline struct ffa_memory_access_impdef ffa_memory_access_impdef_init(
1291 uint64_t impdef_hi, uint64_t impdef_lo)
1292{
1293 return (struct ffa_memory_access_impdef){{impdef_hi, impdef_lo}};
1294}
1295
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001296/**
J-Alves0b6653d2022-04-22 13:17:38 +01001297 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1298 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001299 */
1300struct ffa_memory_access {
1301 struct ffa_memory_region_attributes receiver_permissions;
1302 /**
1303 * Offset in bytes from the start of the outer `ffa_memory_region` to
1304 * an `ffa_composite_memory_region` struct.
1305 */
1306 uint32_t composite_memory_region_offset;
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001307 struct ffa_memory_access_impdef impdef;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001308 uint64_t reserved_0;
1309};
1310
J-Alves363f5722022-04-25 17:37:37 +01001311/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001312#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001313
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001314/**
1315 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001316 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1317 * memory transaction descriptor". Note that it is also used for retrieve
1318 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001319 */
1320struct ffa_memory_region {
1321 /**
1322 * The ID of the VM which originally sent the memory region, i.e. the
1323 * owner.
1324 */
J-Alves19e20cf2023-08-02 12:48:55 +01001325 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001326 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001327 /** Flags to control behaviour of the transaction. */
1328 ffa_memory_region_flags_t flags;
1329 ffa_memory_handle_t handle;
1330 /**
1331 * An implementation defined value associated with the receiver and the
1332 * memory region.
1333 */
1334 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001335 /* Size of the memory access descriptor. */
1336 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001337 /**
1338 * The number of `ffa_memory_access` entries included in this
1339 * transaction.
1340 */
1341 uint32_t receiver_count;
1342 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001343 * Offset of the 'receivers' field, which relates to the memory access
1344 * descriptors.
1345 */
1346 uint32_t receivers_offset;
1347 /** Reserved field (12 bytes) must be 0. */
1348 uint32_t reserved[3];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001349};
1350
1351/**
1352 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001353 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001354 * region".
1355 */
1356struct ffa_mem_relinquish {
1357 ffa_memory_handle_t handle;
1358 ffa_memory_region_flags_t flags;
1359 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001360 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001361};
1362
1363/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001364 * Returns the first FF-A version that matches the memory access descriptor
1365 * size.
Daniel Boulbyde974ca2023-12-12 13:53:31 +00001366 */
Karl Meakin0e617d92024-04-05 12:55:22 +01001367enum ffa_version ffa_version_from_memory_access_desc_size(
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01001368 uint32_t memory_access_desc_size);
1369
1370/**
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001371 * To maintain forwards compatability we can't make assumptions about the size
1372 * of the endpoint memory access descriptor so provide a helper function
1373 * to get a receiver from the receiver array using the memory access descriptor
1374 * size field from the memory region descriptor struct.
1375 * Returns NULL if we cannot return the receiver.
1376 */
1377static inline struct ffa_memory_access *ffa_memory_region_get_receiver(
1378 struct ffa_memory_region *memory_region, uint32_t receiver_index)
1379{
1380 uint32_t memory_access_desc_size =
1381 memory_region->memory_access_desc_size;
1382
1383 if (receiver_index >= memory_region->receiver_count) {
1384 return NULL;
1385 }
1386
1387 /*
1388 * Memory access descriptor size cannot be greater than the size of
1389 * the memory access descriptor defined by the current FF-A version.
1390 */
1391 if (memory_access_desc_size > sizeof(struct ffa_memory_access)) {
1392 return NULL;
1393 }
1394
Daniel Boulby41ef8ba2023-10-13 17:01:22 +01001395 /* Check we cannot use receivers offset to cause overflow. */
1396 if (memory_region->receivers_offset !=
1397 sizeof(struct ffa_memory_region)) {
1398 return NULL;
1399 }
1400
Karl Meakin2ad6b662024-07-29 20:45:40 +01001401 return (struct ffa_memory_access
1402 *)((uint8_t *)memory_region +
1403 (size_t)memory_region->receivers_offset +
1404 (size_t)(receiver_index * memory_access_desc_size));
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001405}
1406
1407/**
Daniel Boulby59ffee92023-11-02 18:26:26 +00001408 * Gets the receiver's access permissions from 'struct ffa_memory_region' and
1409 * returns its index in the receiver's array. If receiver's ID doesn't exist
1410 * in the array, return the region's 'receivers_count'.
1411 */
1412static inline uint32_t ffa_memory_region_get_receiver_index(
1413 struct ffa_memory_region *memory_region, ffa_id_t receiver_id)
1414{
1415 uint32_t i;
1416
1417 for (i = 0U; i < memory_region->receiver_count; i++) {
1418 struct ffa_memory_access *receiver =
1419 ffa_memory_region_get_receiver(memory_region, i);
1420 if (receiver->receiver_permissions.receiver == receiver_id) {
1421 break;
1422 }
1423 }
1424
1425 return i;
1426}
1427
1428/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001429 * Gets the `ffa_composite_memory_region` for the given receiver from an
1430 * `ffa_memory_region`, or NULL if it is not valid.
1431 */
1432static inline struct ffa_composite_memory_region *
1433ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1434 uint32_t receiver_index)
1435{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001436 struct ffa_memory_access *receiver =
1437 ffa_memory_region_get_receiver(memory_region, receiver_index);
1438 uint32_t offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001439
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001440 if (receiver == NULL) {
1441 return NULL;
1442 }
1443
1444 offset = receiver->composite_memory_region_offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001445 if (offset == 0) {
1446 return NULL;
1447 }
1448
1449 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1450 offset);
1451}
1452
1453static inline uint32_t ffa_mem_relinquish_init(
1454 struct ffa_mem_relinquish *relinquish_request,
1455 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001456 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001457{
1458 relinquish_request->handle = handle;
1459 relinquish_request->flags = flags;
1460 relinquish_request->endpoint_count = 1;
1461 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001462 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001463}
1464
J-Alves126ab502022-09-29 11:37:33 +01001465void ffa_copy_memory_region_constituents(
1466 struct ffa_memory_region_constituent *dest,
1467 const struct ffa_memory_region_constituent *src);
1468
Karl Meakin0fd67292024-02-06 17:33:05 +00001469struct ffa_features_rxtx_map_params {
1470 /*
1471 * Bit[0:1]:
1472 * Minimum buffer size and alignment boundary:
1473 * 0b00: 4K
1474 * 0b01: 64K
1475 * 0b10: 16K
1476 * 0b11: Reserved
1477 */
1478 uint8_t min_buf_size : 2;
1479 /*
1480 * Bit[2:15]:
1481 * Reserved (MBZ)
1482 */
1483 uint16_t mbz : 14;
1484 /*
1485 * Bit[16:32]:
1486 * Maximum buffer size in number of pages
1487 * Only present on version 1.2 or later
1488 */
1489 uint16_t max_buf_size : 16;
1490};
1491
Karl Meakin49ec1e42024-05-10 13:08:24 +01001492enum ffa_features_rxtx_map_buf_size {
1493 FFA_RXTX_MAP_MIN_BUF_4K = 0,
1494 FFA_RXTX_MAP_MAX_BUF_PAGE_COUNT = 1,
1495};
Karl Meakin0fd67292024-02-06 17:33:05 +00001496
Karl Meakinf7861302024-02-20 14:39:33 +00001497static inline struct ffa_features_rxtx_map_params ffa_features_rxtx_map_params(
1498 struct ffa_value args)
1499{
1500 struct ffa_features_rxtx_map_params params;
1501 uint32_t arg2 = args.arg2;
1502
1503 params = *(struct ffa_features_rxtx_map_params *)(&arg2);
1504
1505 return params;
1506}
1507
Federico Recanati392be392022-02-08 20:53:03 +01001508/**
1509 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1510 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1511 * to the SPMC, in order to allow indirect messaging.
1512 */
1513struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001514 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001515 uint16_t reserved;
1516
1517 /*
1518 * 8-byte aligned offset from the base address of this descriptor to the
1519 * `ffa_composite_memory_region` describing the RX buffer.
1520 */
1521 uint32_t rx_offset;
1522
1523 /*
1524 * 8-byte aligned offset from the base address of this descriptor to the
1525 * `ffa_composite_memory_region` describing the TX buffer.
1526 */
1527 uint32_t tx_offset;
1528
1529 /* Pad to align on 16-byte boundary. */
1530 uint32_t pad;
1531};
1532
1533static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001534ffa_endpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001535{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001536 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001537 desc->rx_offset);
1538}
1539
1540static inline struct ffa_composite_memory_region *
Karl Meakinb9705e22024-04-05 13:58:28 +01001541ffa_endpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
Federico Recanati392be392022-02-08 20:53:03 +01001542{
Karl Meakin2ad6b662024-07-29 20:45:40 +01001543 return (struct ffa_composite_memory_region *)((char *)desc +
Federico Recanati392be392022-02-08 20:53:03 +01001544 desc->tx_offset);
1545}
1546
J-Alves2d8457f2022-10-05 11:06:41 +01001547void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001548 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001549 ffa_memory_attributes_t attributes,
1550 ffa_memory_region_flags_t flags,
1551 ffa_memory_handle_t handle, uint32_t tag,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001552 uint32_t receiver_count,
1553 uint32_t receiver_desc_size);
Daniel Boulby59ffee92023-11-02 18:26:26 +00001554void ffa_memory_access_init(struct ffa_memory_access *receiver,
1555 ffa_id_t receiver_id,
1556 enum ffa_data_access data_access,
1557 enum ffa_instruction_access instruction_access,
1558 ffa_memory_receiver_flags_t flags,
1559 struct ffa_memory_access_impdef *impdef_val);
J-Alves45085432022-04-22 16:19:20 +01001560uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001561 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001562 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001563 const struct ffa_memory_region_constituent constituents[],
1564 uint32_t constituent_count, uint32_t tag,
1565 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1566 enum ffa_instruction_access instruction_access,
1567 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001568 enum ffa_memory_shareability shareability,
1569 struct ffa_memory_access_impdef *impdef_val, uint32_t *fragment_length,
Andrew Walbranca808b12020-05-15 17:22:28 +01001570 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001571uint32_t ffa_memory_region_init(
1572 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001573 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001574 uint32_t receiver_count, uint32_t receiver_desc_size,
J-Alvesf4eecf72022-07-20 16:05:34 +01001575 const struct ffa_memory_region_constituent constituents[],
1576 uint32_t constituent_count, uint32_t tag,
1577 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1578 enum ffa_memory_cacheability cacheability,
1579 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1580 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001581uint32_t ffa_memory_retrieve_request_init(
1582 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001583 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001584 uint32_t receiver_count, uint32_t receiver_desc_size, uint32_t tag,
1585 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1586 enum ffa_memory_cacheability cacheability,
J-Alves9b24ed82022-08-04 13:12:45 +01001587 enum ffa_memory_shareability shareability);
1588uint32_t ffa_memory_retrieve_request_init_single_receiver(
1589 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001590 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001591 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1592 enum ffa_instruction_access instruction_access,
1593 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Daniel Boulby59ffee92023-11-02 18:26:26 +00001594 enum ffa_memory_shareability shareability,
1595 struct ffa_memory_access_impdef *impdef_val);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001596uint32_t ffa_memory_lender_retrieve_request_init(
1597 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001598 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001599uint32_t ffa_memory_fragment_init(
1600 struct ffa_memory_region_constituent *fragment,
1601 size_t fragment_max_size,
1602 const struct ffa_memory_region_constituent constituents[],
1603 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001604void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001605 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001606 uint64_t rx_address, uint64_t tx_address);