blob: 718777a1e717fd3ac374bcc1d005899b85355eee [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
33/* FF-A function identifiers. */
J-Alves3829fc02021-03-18 12:49:18 +000034#define FFA_ERROR_32 0x84000060
35#define FFA_SUCCESS_32 0x84000061
36#define FFA_SUCCESS_64 0xC4000061
37#define FFA_INTERRUPT_32 0x84000062
38#define FFA_VERSION_32 0x84000063
39#define FFA_FEATURES_32 0x84000064
40#define FFA_RX_RELEASE_32 0x84000065
41#define FFA_RXTX_MAP_32 0x84000066
42#define FFA_RXTX_MAP_64 0xC4000066
43#define FFA_RXTX_UNMAP_32 0x84000067
44#define FFA_PARTITION_INFO_GET_32 0x84000068
45#define FFA_ID_GET_32 0x84000069
46#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
47#define FFA_MSG_WAIT_32 0x8400006B
48#define FFA_YIELD_32 0x8400006C
49#define FFA_RUN_32 0x8400006D
50#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
51#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
52#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
53#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
54#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
55#define FFA_MEM_DONATE_32 0x84000071
56#define FFA_MEM_LEND_32 0x84000072
57#define FFA_MEM_SHARE_32 0x84000073
58#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
59#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
60#define FFA_MEM_RELINQUISH_32 0x84000076
61#define FFA_MEM_RECLAIM_32 0x84000077
62#define FFA_MEM_FRAG_RX_32 0x8400007A
63#define FFA_MEM_FRAG_TX_32 0x8400007B
64#define FFA_NORMAL_WORLD_RESUME 0x8400007C
65
66/* FF-A v1.1 */
67#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
68#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
69#define FFA_NOTIFICATION_BIND_32 0x8400007F
70#define FFA_NOTIFICATION_UNBIND_32 0x84000080
71#define FFA_NOTIFICATION_SET_32 0x84000081
72#define FFA_NOTIFICATION_GET_32 0x84000082
73#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
74#define FFA_RX_ACQUIRE_32 0x84000084
75#define FFA_SPM_ID_GET_32 0x84000085
76#define FFA_MSG_SEND2_32 0x84000086
77#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
78#define FFA_MEM_PERM_GET_32 0x84000088
79#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -070080#define FFA_MEM_PERM_GET_64 0xC4000088
81#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082
Maksims Svecovs71b76702022-05-20 15:32:58 +010083/* Implementation-defined ABIs. */
84#define FFA_CONSOLE_LOG_32 0x8400008A
85#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -080086#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -060087#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Maksims Svecovs71b76702022-05-20 15:32:58 +010088
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010089/* FF-A error codes. */
90#define FFA_NOT_SUPPORTED INT32_C(-1)
91#define FFA_INVALID_PARAMETERS INT32_C(-2)
92#define FFA_NO_MEMORY INT32_C(-3)
93#define FFA_BUSY INT32_C(-4)
94#define FFA_INTERRUPTED INT32_C(-5)
95#define FFA_DENIED INT32_C(-6)
96#define FFA_RETRY INT32_C(-7)
97#define FFA_ABORTED INT32_C(-8)
J-Alvesc8e8a222021-06-08 17:33:52 +010098#define FFA_NO_DATA INT32_C(-9)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010099
100/* clang-format on */
101
J-Alves6f72ca82021-11-01 12:34:58 +0000102/**
103 * FF-A Feature ID, to be used with interface FFA_FEATURES.
104 * As defined in the FF-A v1.1 Beta specification, table 13.10, in section
105 * 13.2.
106 */
107
Olivier Deprezb76307d2022-06-09 17:17:45 +0200108#define FFA_FEATURES_FUNC_ID_MASK (UINT32_C(1) << 31)
J-Alves6f72ca82021-11-01 12:34:58 +0000109#define FFA_FEATURES_FEATURE_ID_MASK UINT32_C(0x7F)
110
Karl Meakin34b8ae92023-01-13 13:33:07 +0000111/**
112 * Defined in Table 13.14 in the FF-A v.1.1 REL0 specification.
113 * Bits[31:2] and Bit[0] of input are reserved (must be zero).
114 * Bit[0]: dynamically allocated buffer support.
115 * Bit[1]: NS bit handling.
116 * Bit[2]: support for retrieval by hypervisor.
117 */
118#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT 0
119#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (UINT32_C(1) << 1)
120#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (UINT32_C(1) << 2)
121#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK \
122 (~(FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT | \
123 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT | \
124 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT))
125
J-Alves6f72ca82021-11-01 12:34:58 +0000126/* Query interrupt ID of Notification Pending Interrupt. */
127#define FFA_FEATURE_NPI 0x1U
128
129/* Query interrupt ID of Schedule Receiver Interrupt. */
130#define FFA_FEATURE_SRI 0x2U
131
132/* Query interrupt ID of the Managed Exit Interrupt. */
133#define FFA_FEATURE_MEI 0x3U
134
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100135/* FF-A function specific constants. */
136#define FFA_MSG_RECV_BLOCK 0x1
137#define FFA_MSG_RECV_BLOCK_MASK 0x1
138
139#define FFA_MSG_SEND_NOTIFY 0x1
140#define FFA_MSG_SEND_NOTIFY_MASK 0x1
141
142#define FFA_MEM_RECLAIM_CLEAR 0x1
143
144#define FFA_SLEEP_INDEFINITE 0
145
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700146#define FFA_MEM_PERM_RO UINT32_C(0x7)
147#define FFA_MEM_PERM_RW UINT32_C(0x5)
148#define FFA_MEM_PERM_RX UINT32_C(0x3)
149
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000150/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100151 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000152 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
153 * if partition info descriptors should be returned or just the count.
154 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100155#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
156#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000157
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100158/**
159 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
160 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
161 * configured to use.
162 */
J-Alves715d6232023-02-16 16:33:28 +0000163#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100164
Federico Recanatifc0295e2022-02-08 19:37:39 +0100165/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100166typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100167
168/**
J-Alves661e1b72023-08-02 13:39:40 +0100169 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
170 * parts:
171 * - Bit15 -> partition type identifier.
172 * - b'0 -> ID relates to a VM ID.
173 * - b'1 -> ID relates to an SP ID.
174 */
175#define FFA_ID_MASK ((ffa_id_t)0x8000)
176#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
177
178/**
179 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
180 */
181static inline bool ffa_is_vm_id(ffa_id_t id)
182{
183 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
184}
185
186/**
Federico Recanatifc0295e2022-02-08 19:37:39 +0100187 * Partition message header as specified by table 6.2 from FF-A v1.1 EAC0
188 * specification.
189 */
190struct ffa_partition_rxtx_header {
191 uint32_t flags; /* MBZ */
192 uint32_t reserved;
193 /* Offset from the beginning of the buffer to the message payload. */
194 uint32_t offset;
195 /* Sender(Bits[31:16]) and Receiver(Bits[15:0]) endpoint IDs. */
196 uint32_t sender_receiver;
197 /* Size of message in buffer. */
198 uint32_t size;
199};
200
201#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
202#define FFA_RXTX_SENDER_SHIFT (0x10U)
J-Alves70079932022-12-07 17:32:20 +0000203#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100204
205static inline void ffa_rxtx_header_init(
J-Alves19e20cf2023-08-02 12:48:55 +0100206 ffa_id_t sender, ffa_id_t receiver, uint32_t size,
Federico Recanatifc0295e2022-02-08 19:37:39 +0100207 struct ffa_partition_rxtx_header *header)
208{
209 header->flags = 0;
210 header->reserved = 0;
211 header->offset = FFA_RXTX_HEADER_SIZE;
212 header->sender_receiver =
213 (uint32_t)(receiver | (sender << FFA_RXTX_SENDER_SHIFT));
214 header->size = size;
215}
216
J-Alves19e20cf2023-08-02 12:48:55 +0100217static inline ffa_id_t ffa_rxtx_header_sender(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100218 const struct ffa_partition_rxtx_header *h)
219{
J-Alves19e20cf2023-08-02 12:48:55 +0100220 return (ffa_id_t)(h->sender_receiver >> FFA_RXTX_SENDER_SHIFT);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100221}
222
J-Alves19e20cf2023-08-02 12:48:55 +0100223static inline ffa_id_t ffa_rxtx_header_receiver(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100224 const struct ffa_partition_rxtx_header *h)
225{
J-Alves19e20cf2023-08-02 12:48:55 +0100226 return (ffa_id_t)(h->sender_receiver);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100227}
228
229/* The maximum length possible for a single message. */
230#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
231
232struct ffa_partition_msg {
233 struct ffa_partition_rxtx_header header;
234 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
235};
236
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100237/* The maximum length possible for a single message. */
238#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
239
240enum ffa_data_access {
241 FFA_DATA_ACCESS_NOT_SPECIFIED,
242 FFA_DATA_ACCESS_RO,
243 FFA_DATA_ACCESS_RW,
244 FFA_DATA_ACCESS_RESERVED,
245};
246
247enum ffa_instruction_access {
248 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
249 FFA_INSTRUCTION_ACCESS_NX,
250 FFA_INSTRUCTION_ACCESS_X,
251 FFA_INSTRUCTION_ACCESS_RESERVED,
252};
253
254enum ffa_memory_type {
255 FFA_MEMORY_NOT_SPECIFIED_MEM,
256 FFA_MEMORY_DEVICE_MEM,
257 FFA_MEMORY_NORMAL_MEM,
258};
259
260enum ffa_memory_cacheability {
261 FFA_MEMORY_CACHE_RESERVED = 0x0,
262 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
263 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
264 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
265 FFA_MEMORY_DEV_NGNRNE = 0x0,
266 FFA_MEMORY_DEV_NGNRE = 0x1,
267 FFA_MEMORY_DEV_NGRE = 0x2,
268 FFA_MEMORY_DEV_GRE = 0x3,
269};
270
271enum ffa_memory_shareability {
272 FFA_MEMORY_SHARE_NON_SHAREABLE,
273 FFA_MEMORY_SHARE_RESERVED,
274 FFA_MEMORY_OUTER_SHAREABLE,
275 FFA_MEMORY_INNER_SHAREABLE,
276};
277
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100278/**
279 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
280 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
281 * and FFA_MEM_RETRIEVE_REQUEST.
282 */
283enum ffa_memory_security {
284 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
285 FFA_MEMORY_SECURITY_SECURE = 0,
286 FFA_MEMORY_SECURITY_NON_SECURE,
287};
288
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100289typedef uint8_t ffa_memory_access_permissions_t;
290
291/**
J-Alves0b6653d2022-04-22 13:17:38 +0100292 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100293 * region attributes descriptor".
294 */
J-Alves0b6653d2022-04-22 13:17:38 +0100295typedef uint16_t ffa_memory_attributes_t;
296
297/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
298#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100299
300#define FFA_DATA_ACCESS_OFFSET (0x0U)
301#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
302
303#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
304#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
305
306#define FFA_MEMORY_TYPE_OFFSET (0x4U)
307#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
308
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100309#define FFA_MEMORY_SECURITY_OFFSET (0x6U)
310#define FFA_MEMORY_SECURITY_MASK ((0x1U) << FFA_MEMORY_SECURITY_OFFSET)
311
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100312#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
313#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
314
315#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
316#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
317
318#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
319 static inline void ffa_set_##name##_attr(container_type *attr, \
320 const enum ffa_##name perm) \
321 { \
322 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
323 }
324
325#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
326 static inline enum ffa_##name ffa_get_##name##_attr( \
327 container_type attr) \
328 { \
329 return (enum ffa_##name)((attr & mask) >> offset); \
330 }
331
332ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
333 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
334ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
335 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
336
337ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
338 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
339ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
340 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
341
342ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
343 FFA_MEMORY_TYPE_MASK)
344ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
345 FFA_MEMORY_TYPE_MASK)
346
347ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
348 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
349ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
350 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
351
352ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
353 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
354ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
355 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
356
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100357ATTR_FUNCTION_SET(memory_security, ffa_memory_attributes_t,
358 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
359ATTR_FUNCTION_GET(memory_security, ffa_memory_attributes_t,
360 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
361
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100362/**
363 * A globally-unique ID assigned by the hypervisor for a region of memory being
364 * sent between VMs.
365 */
366typedef uint64_t ffa_memory_handle_t;
367
J-Alves917d2f22020-10-30 18:39:30 +0000368#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
369 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
370#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
371 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
372
373#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
374#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
375
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100376/**
377 * A count of VMs. This has the same range as the VM IDs but we give it a
378 * different name to make the different semantics clear.
379 */
J-Alves19e20cf2023-08-02 12:48:55 +0100380typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100381
382/** The index of a vCPU within a particular VM. */
383typedef uint16_t ffa_vcpu_index_t;
384
385/**
386 * A count of vCPUs. This has the same range as the vCPU indices but we give it
387 * a different name to make the different semantics clear.
388 */
389typedef ffa_vcpu_index_t ffa_vcpu_count_t;
390
391/** Parameter and return type of FF-A functions. */
392struct ffa_value {
393 uint64_t func;
394 uint64_t arg1;
395 uint64_t arg2;
396 uint64_t arg3;
397 uint64_t arg4;
398 uint64_t arg5;
399 uint64_t arg6;
400 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800401
402 struct {
403 uint64_t arg8;
404 uint64_t arg9;
405 uint64_t arg10;
406 uint64_t arg11;
407 uint64_t arg12;
408 uint64_t arg13;
409 uint64_t arg14;
410 uint64_t arg15;
411 uint64_t arg16;
412 uint64_t arg17;
413 bool valid;
414 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415};
416
Olivier Depreze562e542020-06-11 17:31:54 +0200417static inline uint32_t ffa_func_id(struct ffa_value args)
418{
419 return args.func;
420}
421
J-Alves13318e32021-02-22 17:21:00 +0000422static inline int32_t ffa_error_code(struct ffa_value val)
423{
424 return (int32_t)val.arg2;
425}
426
J-Alves19e20cf2023-08-02 12:48:55 +0100427static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100428{
429 return (args.arg1 >> 16) & 0xffff;
430}
431
J-Alves19e20cf2023-08-02 12:48:55 +0100432static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100433{
434 return args.arg1 & 0xffff;
435}
436
437static inline uint32_t ffa_msg_send_size(struct ffa_value args)
438{
439 return args.arg3;
440}
441
Federico Recanati25053ee2022-03-14 15:01:53 +0100442static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
443{
444 return args.arg2;
445}
446
Olivier Depreze562e542020-06-11 17:31:54 +0200447static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
448{
449 return args.arg2;
450}
451
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800452static inline uint16_t ffa_partition_info_regs_get_last_idx(
453 struct ffa_value args)
454{
455 return args.arg2 & 0xFFFF;
456}
457
458static inline uint16_t ffa_partition_info_regs_get_curr_idx(
459 struct ffa_value args)
460{
461 return (args.arg2 >> 16) & 0xFFFF;
462}
463
464static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
465{
466 return (args.arg2 >> 32) & 0xFFFF;
467}
468
469static inline uint16_t ffa_partition_info_regs_get_desc_size(
470 struct ffa_value args)
471{
472 return (args.arg2 >> 48);
473}
474
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100475static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
476{
477 return (uint64_t)a1 | (uint64_t)a2 << 32;
478}
479
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100480static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
481{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100482 return ffa_assemble_handle(args.arg2, args.arg3);
483}
484
Andrew Walbranca808b12020-05-15 17:22:28 +0100485static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
486{
487 return ffa_assemble_handle(args.arg1, args.arg2);
488}
489
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100490static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
491{
492 return (struct ffa_value){.func = FFA_SUCCESS_32,
493 .arg2 = (uint32_t)handle,
494 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100495}
496
J-Alves19e20cf2023-08-02 12:48:55 +0100497static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100498{
499 return (args.arg1 >> 16) & 0xffff;
500}
501
502static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
503{
504 return args.arg1 & 0xffff;
505}
506
J-Alves19e20cf2023-08-02 12:48:55 +0100507static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100508{
509 return ((uint32_t)vm_id << 16) | vcpu_index;
510}
511
J-Alves19e20cf2023-08-02 12:48:55 +0100512static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100513{
514 return (args.arg4 >> 16) & 0xffff;
515}
516
J-Alves6f72ca82021-11-01 12:34:58 +0000517static inline uint32_t ffa_feature_intid(struct ffa_value args)
518{
519 return (uint32_t)args.arg2;
520}
521
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000522static inline uint32_t ffa_fwk_msg(struct ffa_value args)
523{
524 return (uint32_t)args.arg2;
525}
526
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100527/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100528 * Holds the UUID in a struct that is mappable directly to the SMCC calling
529 * convention, which is used for FF-A calls.
530 *
531 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
532 * of the SMCC Spec 1.2.
533 */
534struct ffa_uuid {
535 uint32_t uuid[4];
536};
537
538static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
539 uint32_t w3, struct ffa_uuid *uuid)
540{
541 uuid->uuid[0] = w0;
542 uuid->uuid[1] = w1;
543 uuid->uuid[2] = w2;
544 uuid->uuid[3] = w3;
545}
546
547static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
548 const struct ffa_uuid *uuid2)
549{
550 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
551 (uuid1->uuid[1] == uuid2->uuid[1]) &&
552 (uuid1->uuid[2] == uuid2->uuid[2]) &&
553 (uuid1->uuid[3] == uuid2->uuid[3]);
554}
555
556static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
557{
558 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
559 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
560}
561
562/**
563 * Flags to determine the partition properties, as required by
564 * FFA_PARTITION_INFO_GET.
565 *
Kathleen Capella402fa852022-11-09 16:16:51 -0500566 * 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 +0100567 * specification, "Partition information descriptor, partition properties".
568 */
569typedef uint32_t ffa_partition_properties_t;
570
571/** Partition property: partition supports receipt of direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500572#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100573
574/** Partition property: partition can send direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500575#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100576
577/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500578#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100579
J-Alves09ff9d82021-11-02 11:55:20 +0000580/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500581#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
582
583/** Partition property: partition runs in the AArch64 execution state. */
584#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +0000585
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100586/**
587 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
588 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -0500589 * This corresponds to table 13.37 "Partition information descriptor"
590 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000591 */
592struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +0100593 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000594 ffa_vcpu_count_t vcpu_count;
595 ffa_partition_properties_t properties;
596 struct ffa_uuid uuid;
597};
598
J-Alvesdd1ad572022-01-25 17:58:26 +0000599/** Length in bytes of the name in boot information descriptor. */
600#define FFA_BOOT_INFO_NAME_LEN 16
601
J-Alves240d84c2022-04-22 12:19:34 +0100602/**
603 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
604 * the FF-A v1.1 EAC0 specification.
605 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000606struct ffa_boot_info_desc {
607 char name[FFA_BOOT_INFO_NAME_LEN];
608 uint8_t type;
609 uint8_t reserved;
610 uint16_t flags;
611 uint32_t size;
612 uint64_t content;
613};
614
615/** FF-A boot information type mask. */
616#define FFA_BOOT_INFO_TYPE_SHIFT 7
617#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
618#define FFA_BOOT_INFO_TYPE_STD 0U
619#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
620
621/** Standard boot info type IDs. */
622#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
623#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
624#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
625
626/** FF-A Boot Info descriptors flags. */
627#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
628
629/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
630#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
631#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
632 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
633#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
634#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
635
636/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
637#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
638#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
639 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
640#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
641#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
642
643static inline uint16_t ffa_boot_info_content_format(
644 struct ffa_boot_info_desc *desc)
645{
646 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
647 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
648}
649
650static inline uint16_t ffa_boot_info_name_format(
651 struct ffa_boot_info_desc *desc)
652{
653 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
654 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
655}
656
657static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
658{
659 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
660}
661
662static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
663{
664 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
665 FFA_BOOT_INFO_TYPE_SHIFT;
666}
667
668/** Length in bytes of the signature in the boot descriptor. */
669#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
670
J-Alves240d84c2022-04-22 12:19:34 +0100671/**
672 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
673 * of the FF-A v1.1 EAC0 specification.
674 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000675struct ffa_boot_info_header {
676 uint32_t signature;
677 uint32_t version;
678 uint32_t info_blob_size;
679 uint32_t desc_size;
680 uint32_t desc_count;
681 uint32_t desc_offset;
682 uint64_t reserved;
683 struct ffa_boot_info_desc boot_info[];
684};
685
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100686/**
J-Alves980d1992021-03-18 12:49:18 +0000687 * FF-A v1.1 specification restricts the number of notifications to a maximum
688 * of 64. Following all possible bitmaps.
689 */
690#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
691
692typedef uint64_t ffa_notifications_bitmap_t;
693
694#define MAX_FFA_NOTIFICATIONS 64U
695
696/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000697 * Flag for notification bind and set, to specify call is about per-vCPU
698 * notifications.
699 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200700#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +0000701
Federico Recanatie73d2832022-04-20 11:10:52 +0200702#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
703#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
704
705/**
706 * Helper functions to check for buffer full notification.
707 */
708static inline bool is_ffa_hyp_buffer_full_notification(
709 ffa_notifications_bitmap_t framework)
710{
711 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
712}
713
714static inline bool is_ffa_spm_buffer_full_notification(
715 ffa_notifications_bitmap_t framework)
716{
717 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
718}
719
J-Alvesc003a7a2021-03-18 13:06:53 +0000720/**
J-Alves980d1992021-03-18 12:49:18 +0000721 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
722 * and hi.
723 * Helpful as FF-A specification defines that the notifications interfaces
724 * arguments are 32-bit registers.
725 */
726static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
727 uint32_t hi)
728{
729 return (ffa_notifications_bitmap_t)hi << 32U | lo;
730}
731
J-Alves98ff9562021-09-09 14:39:41 +0100732static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
733 struct ffa_value val)
734{
735 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
736}
737
738static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
739 struct ffa_value val)
740{
741 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
742}
743
Federico Recanatie73d2832022-04-20 11:10:52 +0200744static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
745 struct ffa_value val)
746{
747 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
748}
749
J-Alves980d1992021-03-18 12:49:18 +0000750/**
J-Alvesaa79c012021-07-09 14:29:45 +0100751 * Flags used in calls to FFA_NOTIFICATION_GET interface.
752 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200753#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
754#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
755#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
756#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +0100757
J-Alvesa3755072021-11-26 16:01:08 +0000758/* Flag to configure notification as being per vCPU. */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200759#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesa3755072021-11-26 16:01:08 +0000760
J-Alves13394022021-06-30 13:48:49 +0100761/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200762#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
763
764#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
765 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
J-Alves13394022021-06-30 13:48:49 +0100766
J-Alvesbe6e3032021-11-30 14:54:12 +0000767static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100768{
J-Alvesbe6e3032021-11-30 14:54:12 +0000769 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100770}
771
772/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100773 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
774 */
775#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
776
777/**
778 * Number of registers to use in successfull return of interface
779 * FFA_NOTIFICATION_INFO_GET.
780 */
781#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
782
783#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
784
785/**
786 * Helper macros for return parameter encoding as described in section 17.7.1
787 * of the FF-A v1.1 Beta0 specification.
788 */
789#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
790#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
791#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
792#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
793
794static inline uint32_t ffa_notification_info_get_lists_count(
795 struct ffa_value args)
796{
797 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
798 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
799}
800
801static inline uint32_t ffa_notification_info_get_list_size(
802 struct ffa_value args, unsigned int list_idx)
803{
804 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
805 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
806}
807
808static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
809{
810 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
811}
812
813/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100814 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +0100815 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
816 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100817 */
818struct ffa_memory_region_constituent {
819 /**
820 * The base IPA of the constituent memory region, aligned to 4 kiB page
821 * size granularity.
822 */
823 uint64_t address;
824 /** The number of 4 kiB pages in the constituent memory region. */
825 uint32_t page_count;
826 /** Reserved field, must be 0. */
827 uint32_t reserved;
828};
829
830/**
J-Alves0b6653d2022-04-22 13:17:38 +0100831 * A set of pages comprising a memory region. This corresponds to table 10.13 of
832 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100833 */
834struct ffa_composite_memory_region {
835 /**
836 * The total number of 4 kiB pages included in this memory region. This
837 * must be equal to the sum of page counts specified in each
838 * `ffa_memory_region_constituent`.
839 */
840 uint32_t page_count;
841 /**
842 * The number of constituents (`ffa_memory_region_constituent`)
843 * included in this memory region range.
844 */
845 uint32_t constituent_count;
846 /** Reserved field, must be 0. */
847 uint64_t reserved_0;
848 /** An array of `constituent_count` memory region constituents. */
849 struct ffa_memory_region_constituent constituents[];
850};
851
852/** Flags to indicate properties of receivers during memory region retrieval. */
853typedef uint8_t ffa_memory_receiver_flags_t;
854
855/**
J-Alves0b6653d2022-04-22 13:17:38 +0100856 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100857 * access permissions descriptor".
858 */
859struct ffa_memory_region_attributes {
860 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +0100861 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100862 /**
863 * The permissions with which the memory region should be mapped in the
864 * receiver's page table.
865 */
866 ffa_memory_access_permissions_t permissions;
867 /**
868 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
869 * for memory regions with multiple borrowers.
870 */
871 ffa_memory_receiver_flags_t flags;
872};
873
874/** Flags to control the behaviour of a memory sharing transaction. */
875typedef uint32_t ffa_memory_region_flags_t;
876
877/**
878 * Clear memory region contents after unmapping it from the sender and before
879 * mapping it for any receiver.
880 */
881#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
882
883/**
884 * Whether the hypervisor may time slice the memory sharing or retrieval
885 * operation.
886 */
887#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
888
889/**
890 * Whether the hypervisor should clear the memory region after the receiver
891 * relinquishes it or is aborted.
892 */
893#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
894
J-Alves3456e032023-07-20 12:20:05 +0100895/**
896 * On retrieve request, bypass the multi-borrower check.
897 */
898#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
899
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100900#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
901#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
902#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
903#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
904#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
905
Federico Recanati85090c42021-12-15 13:17:54 +0100906#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
907#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
908
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100909/**
J-Alves0b6653d2022-04-22 13:17:38 +0100910 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
911 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100912 */
913struct ffa_memory_access {
914 struct ffa_memory_region_attributes receiver_permissions;
915 /**
916 * Offset in bytes from the start of the outer `ffa_memory_region` to
917 * an `ffa_composite_memory_region` struct.
918 */
919 uint32_t composite_memory_region_offset;
920 uint64_t reserved_0;
921};
922
J-Alves363f5722022-04-25 17:37:37 +0100923/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +0100924#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +0100925
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926/**
927 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +0100928 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
929 * memory transaction descriptor". Note that it is also used for retrieve
930 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 */
932struct ffa_memory_region {
933 /**
934 * The ID of the VM which originally sent the memory region, i.e. the
935 * owner.
936 */
J-Alves19e20cf2023-08-02 12:48:55 +0100937 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100938 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100939 /** Flags to control behaviour of the transaction. */
940 ffa_memory_region_flags_t flags;
941 ffa_memory_handle_t handle;
942 /**
943 * An implementation defined value associated with the receiver and the
944 * memory region.
945 */
946 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +0100947 /* Size of the memory access descriptor. */
948 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100949 /**
950 * The number of `ffa_memory_access` entries included in this
951 * transaction.
952 */
953 uint32_t receiver_count;
954 /**
J-Alves0b6653d2022-04-22 13:17:38 +0100955 * Offset of the 'receivers' field, which relates to the memory access
956 * descriptors.
957 */
958 uint32_t receivers_offset;
959 /** Reserved field (12 bytes) must be 0. */
960 uint32_t reserved[3];
961 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +0100962 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100963 * Each one specifies a memory region offset, an endpoint and the
964 * attributes with which this memory region should be mapped in that
965 * endpoint's page table.
966 */
967 struct ffa_memory_access receivers[];
968};
969
970/**
971 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +0100972 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100973 * region".
974 */
975struct ffa_mem_relinquish {
976 ffa_memory_handle_t handle;
977 ffa_memory_region_flags_t flags;
978 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +0100979 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100980};
981
982/**
983 * Gets the `ffa_composite_memory_region` for the given receiver from an
984 * `ffa_memory_region`, or NULL if it is not valid.
985 */
986static inline struct ffa_composite_memory_region *
987ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
988 uint32_t receiver_index)
989{
990 uint32_t offset = memory_region->receivers[receiver_index]
991 .composite_memory_region_offset;
992
993 if (offset == 0) {
994 return NULL;
995 }
996
997 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
998 offset);
999}
1000
1001static inline uint32_t ffa_mem_relinquish_init(
1002 struct ffa_mem_relinquish *relinquish_request,
1003 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001004 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001005{
1006 relinquish_request->handle = handle;
1007 relinquish_request->flags = flags;
1008 relinquish_request->endpoint_count = 1;
1009 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001010 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001011}
1012
J-Alves126ab502022-09-29 11:37:33 +01001013void ffa_copy_memory_region_constituents(
1014 struct ffa_memory_region_constituent *dest,
1015 const struct ffa_memory_region_constituent *src);
1016
Federico Recanati392be392022-02-08 20:53:03 +01001017/**
1018 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1019 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1020 * to the SPMC, in order to allow indirect messaging.
1021 */
1022struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001023 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001024 uint16_t reserved;
1025
1026 /*
1027 * 8-byte aligned offset from the base address of this descriptor to the
1028 * `ffa_composite_memory_region` describing the RX buffer.
1029 */
1030 uint32_t rx_offset;
1031
1032 /*
1033 * 8-byte aligned offset from the base address of this descriptor to the
1034 * `ffa_composite_memory_region` describing the TX buffer.
1035 */
1036 uint32_t tx_offset;
1037
1038 /* Pad to align on 16-byte boundary. */
1039 uint32_t pad;
1040};
1041
1042static inline struct ffa_composite_memory_region *
1043ffa_enpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1044{
1045 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1046 desc->rx_offset);
1047}
1048
1049static inline struct ffa_composite_memory_region *
1050ffa_enpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1051{
1052 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1053 desc->tx_offset);
1054}
1055
J-Alves2d8457f2022-10-05 11:06:41 +01001056void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001057 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001058 ffa_memory_attributes_t attributes,
1059 ffa_memory_region_flags_t flags,
1060 ffa_memory_handle_t handle, uint32_t tag,
1061 uint32_t receiver_count);
J-Alvesb44bb7d2022-04-25 15:43:46 +01001062void ffa_memory_access_init_permissions(
J-Alves19e20cf2023-08-02 12:48:55 +01001063 struct ffa_memory_access *receiver, ffa_id_t receiver_id,
J-Alvesb44bb7d2022-04-25 15:43:46 +01001064 enum ffa_data_access data_access,
1065 enum ffa_instruction_access instruction_access,
1066 ffa_memory_receiver_flags_t flags);
J-Alves45085432022-04-22 16:19:20 +01001067uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001068 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001069 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001070 const struct ffa_memory_region_constituent constituents[],
1071 uint32_t constituent_count, uint32_t tag,
1072 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1073 enum ffa_instruction_access instruction_access,
1074 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +01001075 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1076 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001077uint32_t ffa_memory_region_init(
1078 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001079 ffa_id_t sender, struct ffa_memory_access receivers[],
J-Alvesf4eecf72022-07-20 16:05:34 +01001080 uint32_t receiver_count,
1081 const struct ffa_memory_region_constituent constituents[],
1082 uint32_t constituent_count, uint32_t tag,
1083 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1084 enum ffa_memory_cacheability cacheability,
1085 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1086 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001087uint32_t ffa_memory_retrieve_request_init(
1088 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001089 ffa_id_t sender, struct ffa_memory_access receivers[],
J-Alves9b24ed82022-08-04 13:12:45 +01001090 uint32_t receiver_count, uint32_t tag, ffa_memory_region_flags_t flags,
1091 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1092 enum ffa_memory_shareability shareability);
1093uint32_t ffa_memory_retrieve_request_init_single_receiver(
1094 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001095 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001096 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1097 enum ffa_instruction_access instruction_access,
1098 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1099 enum ffa_memory_shareability shareability);
1100uint32_t ffa_memory_lender_retrieve_request_init(
1101 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001102 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001103uint32_t ffa_memory_fragment_init(
1104 struct ffa_memory_region_constituent *fragment,
1105 size_t fragment_max_size,
1106 const struct ffa_memory_region_constituent constituents[],
1107 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001108void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001109 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001110 uint64_t rx_address, uint64_t tx_address);