blob: a4e4cbf8bb49b8cdb3ca00044f27d1392ec85f5e [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
Olivier Deprez62d99e32020-01-09 15:58:07 +010013#define FFA_VERSION_MAJOR 0x1
Olivier Deprez62d99e32020-01-09 15:58:07 +010014#define FFA_VERSION_MAJOR_OFFSET 16
Daniel Boulby6e32c612021-02-17 15:09:41 +000015#define FFA_VERSION_MAJOR_MASK 0x7FFF
J-Alves3829fc02021-03-18 12:49:18 +000016#define FFA_VERSION_MINOR 0x1
Daniel Boulby6e32c612021-02-17 15:09:41 +000017#define FFA_VERSION_MINOR_OFFSET 0
18#define FFA_VERSION_MINOR_MASK 0xFFFF
19
20#define MAKE_FFA_VERSION(major, minor) \
21 ((((major)&FFA_VERSION_MAJOR_MASK) << FFA_VERSION_MAJOR_OFFSET) | \
22 (((minor)&FFA_VERSION_MINOR_MASK) << FFA_VERSION_MINOR_OFFSET))
23#define FFA_VERSION_COMPILED \
24 MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR)
Olivier Deprez62d99e32020-01-09 15:58:07 +010025
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010026/* clang-format off */
27
28#define FFA_LOW_32_ID 0x84000060
29#define FFA_HIGH_32_ID 0x8400007F
30#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010031#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032
Karl Meakinf51a35f2023-08-07 17:53:52 +010033/**
34 * FF-A function identifiers.
35 * Don't forget to update `ffa_func_name` if you add a new one.
36 */
J-Alves3829fc02021-03-18 12:49:18 +000037#define FFA_ERROR_32 0x84000060
38#define FFA_SUCCESS_32 0x84000061
39#define FFA_SUCCESS_64 0xC4000061
40#define FFA_INTERRUPT_32 0x84000062
41#define FFA_VERSION_32 0x84000063
42#define FFA_FEATURES_32 0x84000064
43#define FFA_RX_RELEASE_32 0x84000065
44#define FFA_RXTX_MAP_32 0x84000066
45#define FFA_RXTX_MAP_64 0xC4000066
46#define FFA_RXTX_UNMAP_32 0x84000067
47#define FFA_PARTITION_INFO_GET_32 0x84000068
48#define FFA_ID_GET_32 0x84000069
49#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
50#define FFA_MSG_WAIT_32 0x8400006B
51#define FFA_YIELD_32 0x8400006C
52#define FFA_RUN_32 0x8400006D
53#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
54#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
55#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
56#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
57#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
58#define FFA_MEM_DONATE_32 0x84000071
59#define FFA_MEM_LEND_32 0x84000072
60#define FFA_MEM_SHARE_32 0x84000073
61#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
62#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
63#define FFA_MEM_RELINQUISH_32 0x84000076
64#define FFA_MEM_RECLAIM_32 0x84000077
65#define FFA_MEM_FRAG_RX_32 0x8400007A
66#define FFA_MEM_FRAG_TX_32 0x8400007B
67#define FFA_NORMAL_WORLD_RESUME 0x8400007C
68
69/* FF-A v1.1 */
70#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
71#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
72#define FFA_NOTIFICATION_BIND_32 0x8400007F
73#define FFA_NOTIFICATION_UNBIND_32 0x84000080
74#define FFA_NOTIFICATION_SET_32 0x84000081
75#define FFA_NOTIFICATION_GET_32 0x84000082
76#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
77#define FFA_RX_ACQUIRE_32 0x84000084
78#define FFA_SPM_ID_GET_32 0x84000085
79#define FFA_MSG_SEND2_32 0x84000086
80#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
81#define FFA_MEM_PERM_GET_32 0x84000088
82#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -070083#define FFA_MEM_PERM_GET_64 0xC4000088
84#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010085
Maksims Svecovs71b76702022-05-20 15:32:58 +010086/* Implementation-defined ABIs. */
87#define FFA_CONSOLE_LOG_32 0x8400008A
88#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -080089#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -060090#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Maksims Svecovs71b76702022-05-20 15:32:58 +010091
Karl Meakinf51a35f2023-08-07 17:53:52 +010092/**
93 * FF-A error codes.
94 * Don't forget to update `ffa_error_name` if you add a new one.
95 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096#define FFA_NOT_SUPPORTED INT32_C(-1)
97#define FFA_INVALID_PARAMETERS INT32_C(-2)
98#define FFA_NO_MEMORY INT32_C(-3)
99#define FFA_BUSY INT32_C(-4)
100#define FFA_INTERRUPTED INT32_C(-5)
101#define FFA_DENIED INT32_C(-6)
102#define FFA_RETRY INT32_C(-7)
103#define FFA_ABORTED INT32_C(-8)
J-Alvesc8e8a222021-06-08 17:33:52 +0100104#define FFA_NO_DATA INT32_C(-9)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100105
106/* clang-format on */
107
Karl Meakinf51a35f2023-08-07 17:53:52 +0100108/* Return the name of the function identifier. */
109static inline const char *ffa_func_name(uint32_t func)
110{
111 switch (func) {
112 case FFA_ERROR_32:
113 return "FFA_ERROR_32";
114 case FFA_SUCCESS_32:
115 return "FFA_SUCCESS_32";
116 case FFA_SUCCESS_64:
117 return "FFA_SUCCESS_64";
118 case FFA_INTERRUPT_32:
119 return "FFA_INTERRUPT_32";
120 case FFA_VERSION_32:
121 return "FFA_VERSION_32";
122 case FFA_FEATURES_32:
123 return "FFA_FEATURES_32";
124 case FFA_RX_RELEASE_32:
125 return "FFA_RX_RELEASE_32";
126 case FFA_RXTX_MAP_32:
127 return "FFA_RXTX_MAP_32";
128 case FFA_RXTX_MAP_64:
129 return "FFA_RXTX_MAP_64";
130 case FFA_RXTX_UNMAP_32:
131 return "FFA_RXTX_UNMAP_32";
132 case FFA_PARTITION_INFO_GET_32:
133 return "FFA_PARTITION_INFO_GET_32";
134 case FFA_ID_GET_32:
135 return "FFA_ID_GET_32";
136 case FFA_MSG_POLL_32:
137 return "FFA_MSG_POLL_32";
138 case FFA_MSG_WAIT_32:
139 return "FFA_MSG_WAIT_32";
140 case FFA_YIELD_32:
141 return "FFA_YIELD_32";
142 case FFA_RUN_32:
143 return "FFA_RUN_32";
144 case FFA_MSG_SEND_32:
145 return "FFA_MSG_SEND_32";
146 case FFA_MSG_SEND_DIRECT_REQ_32:
147 return "FFA_MSG_SEND_DIRECT_REQ_32";
148 case FFA_MSG_SEND_DIRECT_REQ_64:
149 return "FFA_MSG_SEND_DIRECT_REQ_64";
150 case FFA_MSG_SEND_DIRECT_RESP_32:
151 return "FFA_MSG_SEND_DIRECT_RESP_32";
152 case FFA_MSG_SEND_DIRECT_RESP_64:
153 return "FFA_MSG_SEND_DIRECT_RESP_64";
154 case FFA_MEM_DONATE_32:
155 return "FFA_MEM_DONATE_32";
156 case FFA_MEM_LEND_32:
157 return "FFA_MEM_LEND_32";
158 case FFA_MEM_SHARE_32:
159 return "FFA_MEM_SHARE_32";
160 case FFA_MEM_RETRIEVE_REQ_32:
161 return "FFA_MEM_RETRIEVE_REQ_32";
162 case FFA_MEM_RETRIEVE_RESP_32:
163 return "FFA_MEM_RETRIEVE_RESP_32";
164 case FFA_MEM_RELINQUISH_32:
165 return "FFA_MEM_RELINQUISH_32";
166 case FFA_MEM_RECLAIM_32:
167 return "FFA_MEM_RECLAIM_32";
168 case FFA_MEM_FRAG_RX_32:
169 return "FFA_MEM_FRAG_RX_32";
170 case FFA_MEM_FRAG_TX_32:
171 return "FFA_MEM_FRAG_TX_32";
172 case FFA_NORMAL_WORLD_RESUME:
173 return "FFA_NORMAL_WORLD_RESUME";
174
175 /* FF-A v1.1 */
176 case FFA_NOTIFICATION_BITMAP_CREATE_32:
177 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
178 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
179 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
180 case FFA_NOTIFICATION_BIND_32:
181 return "FFA_NOTIFICATION_BIND_32";
182 case FFA_NOTIFICATION_UNBIND_32:
183 return "FFA_NOTIFICATION_UNBIND_32";
184 case FFA_NOTIFICATION_SET_32:
185 return "FFA_NOTIFICATION_SET_32";
186 case FFA_NOTIFICATION_GET_32:
187 return "FFA_NOTIFICATION_GET_32";
188 case FFA_NOTIFICATION_INFO_GET_64:
189 return "FFA_NOTIFICATION_INFO_GET_64";
190 case FFA_RX_ACQUIRE_32:
191 return "FFA_RX_ACQUIRE_32";
192 case FFA_SPM_ID_GET_32:
193 return "FFA_SPM_ID_GET_32";
194 case FFA_MSG_SEND2_32:
195 return "FFA_MSG_SEND2_32";
196 case FFA_SECONDARY_EP_REGISTER_64:
197 return "FFA_SECONDARY_EP_REGISTER_64";
198 case FFA_MEM_PERM_GET_32:
199 return "FFA_MEM_PERM_GET_32";
200 case FFA_MEM_PERM_SET_32:
201 return "FFA_MEM_PERM_SET_32";
202 case FFA_MEM_PERM_GET_64:
203 return "FFA_MEM_PERM_GET_64";
204 case FFA_MEM_PERM_SET_64:
205 return "FFA_MEM_PERM_SET_64";
206
207 /* Implementation-defined ABIs. */
208 case FFA_CONSOLE_LOG_32:
209 return "FFA_CONSOLE_LOG_32";
210 case FFA_CONSOLE_LOG_64:
211 return "FFA_CONSOLE_LOG_64";
212 case FFA_PARTITION_INFO_GET_REGS_64:
213 return "FFA_PARTITION_INFO_GET_REGS_64";
214 case FFA_EL3_INTR_HANDLE_32:
215 return "FFA_EL3_INTR_HANDLE_32";
216
217 default:
218 return "UNKNOWN";
219 }
220}
221
222/* Return the name of the error code. */
223static inline const char *ffa_error_name(int32_t error)
224{
225 switch (error) {
226 case FFA_NOT_SUPPORTED:
227 return "FFA_NOT_SUPPORTED";
228 case FFA_INVALID_PARAMETERS:
229 return "FFA_INVALID_PARAMETERS";
230 case FFA_NO_MEMORY:
231 return "FFA_NO_MEMORY";
232 case FFA_BUSY:
233 return "FFA_BUSY";
234 case FFA_INTERRUPTED:
235 return "FFA_INTERRUPTED";
236 case FFA_DENIED:
237 return "FFA_DENIED";
238 case FFA_RETRY:
239 return "FFA_RETRY";
240 case FFA_ABORTED:
241 return "FFA_ABORTED";
242 case FFA_NO_DATA:
243 return "FFA_NO_DATA";
244 default:
245 return "UNKNOWN";
246 }
247}
248
J-Alves6f72ca82021-11-01 12:34:58 +0000249/**
250 * FF-A Feature ID, to be used with interface FFA_FEATURES.
251 * As defined in the FF-A v1.1 Beta specification, table 13.10, in section
252 * 13.2.
253 */
254
Olivier Deprezb76307d2022-06-09 17:17:45 +0200255#define FFA_FEATURES_FUNC_ID_MASK (UINT32_C(1) << 31)
J-Alves6f72ca82021-11-01 12:34:58 +0000256#define FFA_FEATURES_FEATURE_ID_MASK UINT32_C(0x7F)
257
Karl Meakin34b8ae92023-01-13 13:33:07 +0000258/**
259 * Defined in Table 13.14 in the FF-A v.1.1 REL0 specification.
260 * Bits[31:2] and Bit[0] of input are reserved (must be zero).
261 * Bit[0]: dynamically allocated buffer support.
262 * Bit[1]: NS bit handling.
263 * Bit[2]: support for retrieval by hypervisor.
264 */
265#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT 0
266#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (UINT32_C(1) << 1)
267#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (UINT32_C(1) << 2)
268#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK \
269 (~(FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT | \
270 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT | \
271 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT))
272
J-Alves6f72ca82021-11-01 12:34:58 +0000273/* Query interrupt ID of Notification Pending Interrupt. */
274#define FFA_FEATURE_NPI 0x1U
275
276/* Query interrupt ID of Schedule Receiver Interrupt. */
277#define FFA_FEATURE_SRI 0x2U
278
279/* Query interrupt ID of the Managed Exit Interrupt. */
280#define FFA_FEATURE_MEI 0x3U
281
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100282/* FF-A function specific constants. */
283#define FFA_MSG_RECV_BLOCK 0x1
284#define FFA_MSG_RECV_BLOCK_MASK 0x1
285
286#define FFA_MSG_SEND_NOTIFY 0x1
287#define FFA_MSG_SEND_NOTIFY_MASK 0x1
288
289#define FFA_MEM_RECLAIM_CLEAR 0x1
290
291#define FFA_SLEEP_INDEFINITE 0
292
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700293#define FFA_MEM_PERM_RO UINT32_C(0x7)
294#define FFA_MEM_PERM_RW UINT32_C(0x5)
295#define FFA_MEM_PERM_RX UINT32_C(0x3)
296
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000297/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100298 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000299 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
300 * if partition info descriptors should be returned or just the count.
301 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100302#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
303#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000304
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100305/**
306 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
307 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
308 * configured to use.
309 */
J-Alves715d6232023-02-16 16:33:28 +0000310#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100311
Federico Recanatifc0295e2022-02-08 19:37:39 +0100312/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100313typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100314
315/**
J-Alves661e1b72023-08-02 13:39:40 +0100316 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
317 * parts:
318 * - Bit15 -> partition type identifier.
319 * - b'0 -> ID relates to a VM ID.
320 * - b'1 -> ID relates to an SP ID.
321 */
322#define FFA_ID_MASK ((ffa_id_t)0x8000)
323#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
324
325/**
326 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
327 */
328static inline bool ffa_is_vm_id(ffa_id_t id)
329{
330 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
331}
332
333/**
Federico Recanatifc0295e2022-02-08 19:37:39 +0100334 * Partition message header as specified by table 6.2 from FF-A v1.1 EAC0
335 * specification.
336 */
337struct ffa_partition_rxtx_header {
338 uint32_t flags; /* MBZ */
339 uint32_t reserved;
340 /* Offset from the beginning of the buffer to the message payload. */
341 uint32_t offset;
342 /* Sender(Bits[31:16]) and Receiver(Bits[15:0]) endpoint IDs. */
343 uint32_t sender_receiver;
344 /* Size of message in buffer. */
345 uint32_t size;
346};
347
348#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
349#define FFA_RXTX_SENDER_SHIFT (0x10U)
J-Alves70079932022-12-07 17:32:20 +0000350#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100351
352static inline void ffa_rxtx_header_init(
J-Alves19e20cf2023-08-02 12:48:55 +0100353 ffa_id_t sender, ffa_id_t receiver, uint32_t size,
Federico Recanatifc0295e2022-02-08 19:37:39 +0100354 struct ffa_partition_rxtx_header *header)
355{
356 header->flags = 0;
357 header->reserved = 0;
358 header->offset = FFA_RXTX_HEADER_SIZE;
359 header->sender_receiver =
360 (uint32_t)(receiver | (sender << FFA_RXTX_SENDER_SHIFT));
361 header->size = size;
362}
363
J-Alves19e20cf2023-08-02 12:48:55 +0100364static inline ffa_id_t ffa_rxtx_header_sender(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100365 const struct ffa_partition_rxtx_header *h)
366{
J-Alves19e20cf2023-08-02 12:48:55 +0100367 return (ffa_id_t)(h->sender_receiver >> FFA_RXTX_SENDER_SHIFT);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100368}
369
J-Alves19e20cf2023-08-02 12:48:55 +0100370static inline ffa_id_t ffa_rxtx_header_receiver(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100371 const struct ffa_partition_rxtx_header *h)
372{
J-Alves19e20cf2023-08-02 12:48:55 +0100373 return (ffa_id_t)(h->sender_receiver);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100374}
375
376/* The maximum length possible for a single message. */
377#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
378
379struct ffa_partition_msg {
380 struct ffa_partition_rxtx_header header;
381 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
382};
383
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100384/* The maximum length possible for a single message. */
385#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
386
387enum ffa_data_access {
388 FFA_DATA_ACCESS_NOT_SPECIFIED,
389 FFA_DATA_ACCESS_RO,
390 FFA_DATA_ACCESS_RW,
391 FFA_DATA_ACCESS_RESERVED,
392};
393
394enum ffa_instruction_access {
395 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
396 FFA_INSTRUCTION_ACCESS_NX,
397 FFA_INSTRUCTION_ACCESS_X,
398 FFA_INSTRUCTION_ACCESS_RESERVED,
399};
400
401enum ffa_memory_type {
402 FFA_MEMORY_NOT_SPECIFIED_MEM,
403 FFA_MEMORY_DEVICE_MEM,
404 FFA_MEMORY_NORMAL_MEM,
405};
406
407enum ffa_memory_cacheability {
408 FFA_MEMORY_CACHE_RESERVED = 0x0,
409 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
410 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
411 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
412 FFA_MEMORY_DEV_NGNRNE = 0x0,
413 FFA_MEMORY_DEV_NGNRE = 0x1,
414 FFA_MEMORY_DEV_NGRE = 0x2,
415 FFA_MEMORY_DEV_GRE = 0x3,
416};
417
418enum ffa_memory_shareability {
419 FFA_MEMORY_SHARE_NON_SHAREABLE,
420 FFA_MEMORY_SHARE_RESERVED,
421 FFA_MEMORY_OUTER_SHAREABLE,
422 FFA_MEMORY_INNER_SHAREABLE,
423};
424
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100425/**
426 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
427 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
428 * and FFA_MEM_RETRIEVE_REQUEST.
429 */
430enum ffa_memory_security {
431 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
432 FFA_MEMORY_SECURITY_SECURE = 0,
433 FFA_MEMORY_SECURITY_NON_SECURE,
434};
435
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100436typedef uint8_t ffa_memory_access_permissions_t;
437
438/**
J-Alves0b6653d2022-04-22 13:17:38 +0100439 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100440 * region attributes descriptor".
441 */
J-Alves0b6653d2022-04-22 13:17:38 +0100442typedef uint16_t ffa_memory_attributes_t;
443
444/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
445#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100446
447#define FFA_DATA_ACCESS_OFFSET (0x0U)
448#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
449
450#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
451#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
452
453#define FFA_MEMORY_TYPE_OFFSET (0x4U)
454#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
455
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100456#define FFA_MEMORY_SECURITY_OFFSET (0x6U)
457#define FFA_MEMORY_SECURITY_MASK ((0x1U) << FFA_MEMORY_SECURITY_OFFSET)
458
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100459#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
460#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
461
462#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
463#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
464
465#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
466 static inline void ffa_set_##name##_attr(container_type *attr, \
467 const enum ffa_##name perm) \
468 { \
469 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
470 }
471
472#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
473 static inline enum ffa_##name ffa_get_##name##_attr( \
474 container_type attr) \
475 { \
476 return (enum ffa_##name)((attr & mask) >> offset); \
477 }
478
479ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
480 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
481ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
482 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
483
484ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
485 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
486ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
487 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
488
489ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
490 FFA_MEMORY_TYPE_MASK)
491ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
492 FFA_MEMORY_TYPE_MASK)
493
494ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
495 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
496ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
497 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
498
499ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
500 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
501ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
502 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
503
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100504ATTR_FUNCTION_SET(memory_security, ffa_memory_attributes_t,
505 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
506ATTR_FUNCTION_GET(memory_security, ffa_memory_attributes_t,
507 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
508
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100509/**
510 * A globally-unique ID assigned by the hypervisor for a region of memory being
511 * sent between VMs.
512 */
513typedef uint64_t ffa_memory_handle_t;
514
J-Alves917d2f22020-10-30 18:39:30 +0000515#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
516 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
517#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
518 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
519
520#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
521#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
522
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100523/**
524 * A count of VMs. This has the same range as the VM IDs but we give it a
525 * different name to make the different semantics clear.
526 */
J-Alves19e20cf2023-08-02 12:48:55 +0100527typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100528
529/** The index of a vCPU within a particular VM. */
530typedef uint16_t ffa_vcpu_index_t;
531
532/**
533 * A count of vCPUs. This has the same range as the vCPU indices but we give it
534 * a different name to make the different semantics clear.
535 */
536typedef ffa_vcpu_index_t ffa_vcpu_count_t;
537
538/** Parameter and return type of FF-A functions. */
539struct ffa_value {
540 uint64_t func;
541 uint64_t arg1;
542 uint64_t arg2;
543 uint64_t arg3;
544 uint64_t arg4;
545 uint64_t arg5;
546 uint64_t arg6;
547 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800548
549 struct {
550 uint64_t arg8;
551 uint64_t arg9;
552 uint64_t arg10;
553 uint64_t arg11;
554 uint64_t arg12;
555 uint64_t arg13;
556 uint64_t arg14;
557 uint64_t arg15;
558 uint64_t arg16;
559 uint64_t arg17;
560 bool valid;
561 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100562};
563
Olivier Depreze562e542020-06-11 17:31:54 +0200564static inline uint32_t ffa_func_id(struct ffa_value args)
565{
566 return args.func;
567}
568
J-Alves13318e32021-02-22 17:21:00 +0000569static inline int32_t ffa_error_code(struct ffa_value val)
570{
571 return (int32_t)val.arg2;
572}
573
J-Alves19e20cf2023-08-02 12:48:55 +0100574static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100575{
576 return (args.arg1 >> 16) & 0xffff;
577}
578
J-Alves19e20cf2023-08-02 12:48:55 +0100579static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100580{
581 return args.arg1 & 0xffff;
582}
583
584static inline uint32_t ffa_msg_send_size(struct ffa_value args)
585{
586 return args.arg3;
587}
588
Federico Recanati25053ee2022-03-14 15:01:53 +0100589static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
590{
591 return args.arg2;
592}
593
Olivier Depreze562e542020-06-11 17:31:54 +0200594static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
595{
596 return args.arg2;
597}
598
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800599static inline uint16_t ffa_partition_info_regs_get_last_idx(
600 struct ffa_value args)
601{
602 return args.arg2 & 0xFFFF;
603}
604
605static inline uint16_t ffa_partition_info_regs_get_curr_idx(
606 struct ffa_value args)
607{
608 return (args.arg2 >> 16) & 0xFFFF;
609}
610
611static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
612{
613 return (args.arg2 >> 32) & 0xFFFF;
614}
615
616static inline uint16_t ffa_partition_info_regs_get_desc_size(
617 struct ffa_value args)
618{
619 return (args.arg2 >> 48);
620}
621
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100622static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
623{
624 return (uint64_t)a1 | (uint64_t)a2 << 32;
625}
626
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100627static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
628{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100629 return ffa_assemble_handle(args.arg2, args.arg3);
630}
631
Andrew Walbranca808b12020-05-15 17:22:28 +0100632static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
633{
634 return ffa_assemble_handle(args.arg1, args.arg2);
635}
636
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100637static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
638{
639 return (struct ffa_value){.func = FFA_SUCCESS_32,
640 .arg2 = (uint32_t)handle,
641 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100642}
643
J-Alves19e20cf2023-08-02 12:48:55 +0100644static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100645{
646 return (args.arg1 >> 16) & 0xffff;
647}
648
649static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
650{
651 return args.arg1 & 0xffff;
652}
653
J-Alves19e20cf2023-08-02 12:48:55 +0100654static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100655{
656 return ((uint32_t)vm_id << 16) | vcpu_index;
657}
658
J-Alves19e20cf2023-08-02 12:48:55 +0100659static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100660{
661 return (args.arg4 >> 16) & 0xffff;
662}
663
J-Alves6f72ca82021-11-01 12:34:58 +0000664static inline uint32_t ffa_feature_intid(struct ffa_value args)
665{
666 return (uint32_t)args.arg2;
667}
668
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000669static inline uint32_t ffa_fwk_msg(struct ffa_value args)
670{
671 return (uint32_t)args.arg2;
672}
673
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100674/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100675 * Holds the UUID in a struct that is mappable directly to the SMCC calling
676 * convention, which is used for FF-A calls.
677 *
678 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
679 * of the SMCC Spec 1.2.
680 */
681struct ffa_uuid {
682 uint32_t uuid[4];
683};
684
685static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
686 uint32_t w3, struct ffa_uuid *uuid)
687{
688 uuid->uuid[0] = w0;
689 uuid->uuid[1] = w1;
690 uuid->uuid[2] = w2;
691 uuid->uuid[3] = w3;
692}
693
694static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
695 const struct ffa_uuid *uuid2)
696{
697 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
698 (uuid1->uuid[1] == uuid2->uuid[1]) &&
699 (uuid1->uuid[2] == uuid2->uuid[2]) &&
700 (uuid1->uuid[3] == uuid2->uuid[3]);
701}
702
703static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
704{
705 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
706 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
707}
708
709/**
710 * Flags to determine the partition properties, as required by
711 * FFA_PARTITION_INFO_GET.
712 *
Kathleen Capella402fa852022-11-09 16:16:51 -0500713 * The values of the flags are specified in table 8.25 of DEN0077A FF-A 1.0 REL
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100714 * specification, "Partition information descriptor, partition properties".
715 */
716typedef uint32_t ffa_partition_properties_t;
717
718/** Partition property: partition supports receipt of direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500719#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100720
721/** Partition property: partition can send direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500722#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100723
724/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500725#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100726
J-Alves09ff9d82021-11-02 11:55:20 +0000727/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500728#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
729
730/** Partition property: partition runs in the AArch64 execution state. */
731#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +0000732
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100733/**
734 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
735 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -0500736 * This corresponds to table 13.37 "Partition information descriptor"
737 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000738 */
739struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +0100740 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000741 ffa_vcpu_count_t vcpu_count;
742 ffa_partition_properties_t properties;
743 struct ffa_uuid uuid;
744};
745
J-Alvesdd1ad572022-01-25 17:58:26 +0000746/** Length in bytes of the name in boot information descriptor. */
747#define FFA_BOOT_INFO_NAME_LEN 16
748
J-Alves240d84c2022-04-22 12:19:34 +0100749/**
750 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
751 * the FF-A v1.1 EAC0 specification.
752 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000753struct ffa_boot_info_desc {
754 char name[FFA_BOOT_INFO_NAME_LEN];
755 uint8_t type;
756 uint8_t reserved;
757 uint16_t flags;
758 uint32_t size;
759 uint64_t content;
760};
761
762/** FF-A boot information type mask. */
763#define FFA_BOOT_INFO_TYPE_SHIFT 7
764#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
765#define FFA_BOOT_INFO_TYPE_STD 0U
766#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
767
768/** Standard boot info type IDs. */
769#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
770#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
771#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
772
773/** FF-A Boot Info descriptors flags. */
774#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
775
776/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
777#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
778#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
779 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
780#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
781#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
782
783/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
784#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
785#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
786 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
787#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
788#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
789
790static inline uint16_t ffa_boot_info_content_format(
791 struct ffa_boot_info_desc *desc)
792{
793 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
794 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
795}
796
797static inline uint16_t ffa_boot_info_name_format(
798 struct ffa_boot_info_desc *desc)
799{
800 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
801 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
802}
803
804static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
805{
806 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
807}
808
809static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
810{
811 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
812 FFA_BOOT_INFO_TYPE_SHIFT;
813}
814
815/** Length in bytes of the signature in the boot descriptor. */
816#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
817
J-Alves240d84c2022-04-22 12:19:34 +0100818/**
819 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
820 * of the FF-A v1.1 EAC0 specification.
821 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000822struct ffa_boot_info_header {
823 uint32_t signature;
824 uint32_t version;
825 uint32_t info_blob_size;
826 uint32_t desc_size;
827 uint32_t desc_count;
828 uint32_t desc_offset;
829 uint64_t reserved;
830 struct ffa_boot_info_desc boot_info[];
831};
832
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100833/**
J-Alves980d1992021-03-18 12:49:18 +0000834 * FF-A v1.1 specification restricts the number of notifications to a maximum
835 * of 64. Following all possible bitmaps.
836 */
837#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
838
839typedef uint64_t ffa_notifications_bitmap_t;
840
841#define MAX_FFA_NOTIFICATIONS 64U
842
843/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000844 * Flag for notification bind and set, to specify call is about per-vCPU
845 * notifications.
846 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200847#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +0000848
Federico Recanatie73d2832022-04-20 11:10:52 +0200849#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
850#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
851
852/**
853 * Helper functions to check for buffer full notification.
854 */
855static inline bool is_ffa_hyp_buffer_full_notification(
856 ffa_notifications_bitmap_t framework)
857{
858 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
859}
860
861static inline bool is_ffa_spm_buffer_full_notification(
862 ffa_notifications_bitmap_t framework)
863{
864 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
865}
866
J-Alvesc003a7a2021-03-18 13:06:53 +0000867/**
J-Alves980d1992021-03-18 12:49:18 +0000868 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
869 * and hi.
870 * Helpful as FF-A specification defines that the notifications interfaces
871 * arguments are 32-bit registers.
872 */
873static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
874 uint32_t hi)
875{
876 return (ffa_notifications_bitmap_t)hi << 32U | lo;
877}
878
J-Alves98ff9562021-09-09 14:39:41 +0100879static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
880 struct ffa_value val)
881{
882 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
883}
884
885static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
886 struct ffa_value val)
887{
888 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
889}
890
Federico Recanatie73d2832022-04-20 11:10:52 +0200891static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
892 struct ffa_value val)
893{
894 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
895}
896
J-Alves980d1992021-03-18 12:49:18 +0000897/**
J-Alvesaa79c012021-07-09 14:29:45 +0100898 * Flags used in calls to FFA_NOTIFICATION_GET interface.
899 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200900#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
901#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
902#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
903#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +0100904
J-Alvesa3755072021-11-26 16:01:08 +0000905/* Flag to configure notification as being per vCPU. */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200906#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesa3755072021-11-26 16:01:08 +0000907
J-Alves13394022021-06-30 13:48:49 +0100908/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200909#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
910
911#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
912 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
J-Alves13394022021-06-30 13:48:49 +0100913
J-Alvesbe6e3032021-11-30 14:54:12 +0000914static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100915{
J-Alvesbe6e3032021-11-30 14:54:12 +0000916 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100917}
918
919/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100920 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
921 */
922#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
923
924/**
925 * Number of registers to use in successfull return of interface
926 * FFA_NOTIFICATION_INFO_GET.
927 */
928#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
929
930#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
931
932/**
933 * Helper macros for return parameter encoding as described in section 17.7.1
934 * of the FF-A v1.1 Beta0 specification.
935 */
936#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
937#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
938#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
939#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
940
941static inline uint32_t ffa_notification_info_get_lists_count(
942 struct ffa_value args)
943{
944 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
945 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
946}
947
948static inline uint32_t ffa_notification_info_get_list_size(
949 struct ffa_value args, unsigned int list_idx)
950{
951 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
952 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
953}
954
955static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
956{
957 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
958}
959
960/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100961 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +0100962 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
963 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100964 */
965struct ffa_memory_region_constituent {
966 /**
967 * The base IPA of the constituent memory region, aligned to 4 kiB page
968 * size granularity.
969 */
970 uint64_t address;
971 /** The number of 4 kiB pages in the constituent memory region. */
972 uint32_t page_count;
973 /** Reserved field, must be 0. */
974 uint32_t reserved;
975};
976
977/**
J-Alves0b6653d2022-04-22 13:17:38 +0100978 * A set of pages comprising a memory region. This corresponds to table 10.13 of
979 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100980 */
981struct ffa_composite_memory_region {
982 /**
983 * The total number of 4 kiB pages included in this memory region. This
984 * must be equal to the sum of page counts specified in each
985 * `ffa_memory_region_constituent`.
986 */
987 uint32_t page_count;
988 /**
989 * The number of constituents (`ffa_memory_region_constituent`)
990 * included in this memory region range.
991 */
992 uint32_t constituent_count;
993 /** Reserved field, must be 0. */
994 uint64_t reserved_0;
995 /** An array of `constituent_count` memory region constituents. */
996 struct ffa_memory_region_constituent constituents[];
997};
998
999/** Flags to indicate properties of receivers during memory region retrieval. */
1000typedef uint8_t ffa_memory_receiver_flags_t;
1001
1002/**
J-Alves0b6653d2022-04-22 13:17:38 +01001003 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001004 * access permissions descriptor".
1005 */
1006struct ffa_memory_region_attributes {
1007 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001008 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001009 /**
1010 * The permissions with which the memory region should be mapped in the
1011 * receiver's page table.
1012 */
1013 ffa_memory_access_permissions_t permissions;
1014 /**
1015 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1016 * for memory regions with multiple borrowers.
1017 */
1018 ffa_memory_receiver_flags_t flags;
1019};
1020
1021/** Flags to control the behaviour of a memory sharing transaction. */
1022typedef uint32_t ffa_memory_region_flags_t;
1023
1024/**
1025 * Clear memory region contents after unmapping it from the sender and before
1026 * mapping it for any receiver.
1027 */
1028#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1029
1030/**
1031 * Whether the hypervisor may time slice the memory sharing or retrieval
1032 * operation.
1033 */
1034#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1035
1036/**
1037 * Whether the hypervisor should clear the memory region after the receiver
1038 * relinquishes it or is aborted.
1039 */
1040#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1041
J-Alves3456e032023-07-20 12:20:05 +01001042/**
1043 * On retrieve request, bypass the multi-borrower check.
1044 */
1045#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1046
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001047#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1048#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1049#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1050#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1051#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1052
Federico Recanati85090c42021-12-15 13:17:54 +01001053#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1054#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1055
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001056/**
J-Alves0b6653d2022-04-22 13:17:38 +01001057 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1058 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001059 */
1060struct ffa_memory_access {
1061 struct ffa_memory_region_attributes receiver_permissions;
1062 /**
1063 * Offset in bytes from the start of the outer `ffa_memory_region` to
1064 * an `ffa_composite_memory_region` struct.
1065 */
1066 uint32_t composite_memory_region_offset;
1067 uint64_t reserved_0;
1068};
1069
J-Alves363f5722022-04-25 17:37:37 +01001070/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001071#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001072
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001073/**
1074 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001075 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1076 * memory transaction descriptor". Note that it is also used for retrieve
1077 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 */
1079struct ffa_memory_region {
1080 /**
1081 * The ID of the VM which originally sent the memory region, i.e. the
1082 * owner.
1083 */
J-Alves19e20cf2023-08-02 12:48:55 +01001084 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001085 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 /** Flags to control behaviour of the transaction. */
1087 ffa_memory_region_flags_t flags;
1088 ffa_memory_handle_t handle;
1089 /**
1090 * An implementation defined value associated with the receiver and the
1091 * memory region.
1092 */
1093 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001094 /* Size of the memory access descriptor. */
1095 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001096 /**
1097 * The number of `ffa_memory_access` entries included in this
1098 * transaction.
1099 */
1100 uint32_t receiver_count;
1101 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001102 * Offset of the 'receivers' field, which relates to the memory access
1103 * descriptors.
1104 */
1105 uint32_t receivers_offset;
1106 /** Reserved field (12 bytes) must be 0. */
1107 uint32_t reserved[3];
1108 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +01001109 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001110 * Each one specifies a memory region offset, an endpoint and the
1111 * attributes with which this memory region should be mapped in that
1112 * endpoint's page table.
1113 */
1114 struct ffa_memory_access receivers[];
1115};
1116
1117/**
1118 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001119 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001120 * region".
1121 */
1122struct ffa_mem_relinquish {
1123 ffa_memory_handle_t handle;
1124 ffa_memory_region_flags_t flags;
1125 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001126 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001127};
1128
1129/**
1130 * Gets the `ffa_composite_memory_region` for the given receiver from an
1131 * `ffa_memory_region`, or NULL if it is not valid.
1132 */
1133static inline struct ffa_composite_memory_region *
1134ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1135 uint32_t receiver_index)
1136{
1137 uint32_t offset = memory_region->receivers[receiver_index]
1138 .composite_memory_region_offset;
1139
1140 if (offset == 0) {
1141 return NULL;
1142 }
1143
1144 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1145 offset);
1146}
1147
1148static inline uint32_t ffa_mem_relinquish_init(
1149 struct ffa_mem_relinquish *relinquish_request,
1150 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001151 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001152{
1153 relinquish_request->handle = handle;
1154 relinquish_request->flags = flags;
1155 relinquish_request->endpoint_count = 1;
1156 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001157 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001158}
1159
J-Alves126ab502022-09-29 11:37:33 +01001160void ffa_copy_memory_region_constituents(
1161 struct ffa_memory_region_constituent *dest,
1162 const struct ffa_memory_region_constituent *src);
1163
Federico Recanati392be392022-02-08 20:53:03 +01001164/**
1165 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1166 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1167 * to the SPMC, in order to allow indirect messaging.
1168 */
1169struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001170 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001171 uint16_t reserved;
1172
1173 /*
1174 * 8-byte aligned offset from the base address of this descriptor to the
1175 * `ffa_composite_memory_region` describing the RX buffer.
1176 */
1177 uint32_t rx_offset;
1178
1179 /*
1180 * 8-byte aligned offset from the base address of this descriptor to the
1181 * `ffa_composite_memory_region` describing the TX buffer.
1182 */
1183 uint32_t tx_offset;
1184
1185 /* Pad to align on 16-byte boundary. */
1186 uint32_t pad;
1187};
1188
1189static inline struct ffa_composite_memory_region *
1190ffa_enpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1191{
1192 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1193 desc->rx_offset);
1194}
1195
1196static inline struct ffa_composite_memory_region *
1197ffa_enpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1198{
1199 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1200 desc->tx_offset);
1201}
1202
J-Alves2d8457f2022-10-05 11:06:41 +01001203void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001204 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001205 ffa_memory_attributes_t attributes,
1206 ffa_memory_region_flags_t flags,
1207 ffa_memory_handle_t handle, uint32_t tag,
1208 uint32_t receiver_count);
J-Alvesb44bb7d2022-04-25 15:43:46 +01001209void ffa_memory_access_init_permissions(
J-Alves19e20cf2023-08-02 12:48:55 +01001210 struct ffa_memory_access *receiver, ffa_id_t receiver_id,
J-Alvesb44bb7d2022-04-25 15:43:46 +01001211 enum ffa_data_access data_access,
1212 enum ffa_instruction_access instruction_access,
1213 ffa_memory_receiver_flags_t flags);
J-Alves45085432022-04-22 16:19:20 +01001214uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001215 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001216 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001217 const struct ffa_memory_region_constituent constituents[],
1218 uint32_t constituent_count, uint32_t tag,
1219 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1220 enum ffa_instruction_access instruction_access,
1221 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +01001222 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1223 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001224uint32_t ffa_memory_region_init(
1225 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001226 ffa_id_t sender, struct ffa_memory_access receivers[],
J-Alvesf4eecf72022-07-20 16:05:34 +01001227 uint32_t receiver_count,
1228 const struct ffa_memory_region_constituent constituents[],
1229 uint32_t constituent_count, uint32_t tag,
1230 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1231 enum ffa_memory_cacheability cacheability,
1232 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1233 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001234uint32_t ffa_memory_retrieve_request_init(
1235 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001236 ffa_id_t sender, struct ffa_memory_access receivers[],
J-Alves9b24ed82022-08-04 13:12:45 +01001237 uint32_t receiver_count, uint32_t tag, ffa_memory_region_flags_t flags,
1238 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1239 enum ffa_memory_shareability shareability);
1240uint32_t ffa_memory_retrieve_request_init_single_receiver(
1241 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001242 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001243 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1244 enum ffa_instruction_access instruction_access,
1245 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1246 enum ffa_memory_shareability shareability);
1247uint32_t ffa_memory_lender_retrieve_request_init(
1248 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001249 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001250uint32_t ffa_memory_fragment_init(
1251 struct ffa_memory_region_constituent *fragment,
1252 size_t fragment_max_size,
1253 const struct ffa_memory_region_constituent constituents[],
1254 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001255void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001256 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001257 uint64_t rx_address, uint64_t tx_address);