blob: 26e777a806e8756c54af2e0cc9bd1ca2a4c73e3d [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/**
169 * Partition message header as specified by table 6.2 from FF-A v1.1 EAC0
170 * specification.
171 */
172struct ffa_partition_rxtx_header {
173 uint32_t flags; /* MBZ */
174 uint32_t reserved;
175 /* Offset from the beginning of the buffer to the message payload. */
176 uint32_t offset;
177 /* Sender(Bits[31:16]) and Receiver(Bits[15:0]) endpoint IDs. */
178 uint32_t sender_receiver;
179 /* Size of message in buffer. */
180 uint32_t size;
181};
182
183#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
184#define FFA_RXTX_SENDER_SHIFT (0x10U)
J-Alves70079932022-12-07 17:32:20 +0000185#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100186
187static inline void ffa_rxtx_header_init(
J-Alves19e20cf2023-08-02 12:48:55 +0100188 ffa_id_t sender, ffa_id_t receiver, uint32_t size,
Federico Recanatifc0295e2022-02-08 19:37:39 +0100189 struct ffa_partition_rxtx_header *header)
190{
191 header->flags = 0;
192 header->reserved = 0;
193 header->offset = FFA_RXTX_HEADER_SIZE;
194 header->sender_receiver =
195 (uint32_t)(receiver | (sender << FFA_RXTX_SENDER_SHIFT));
196 header->size = size;
197}
198
J-Alves19e20cf2023-08-02 12:48:55 +0100199static inline ffa_id_t ffa_rxtx_header_sender(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100200 const struct ffa_partition_rxtx_header *h)
201{
J-Alves19e20cf2023-08-02 12:48:55 +0100202 return (ffa_id_t)(h->sender_receiver >> FFA_RXTX_SENDER_SHIFT);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100203}
204
J-Alves19e20cf2023-08-02 12:48:55 +0100205static inline ffa_id_t ffa_rxtx_header_receiver(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100206 const struct ffa_partition_rxtx_header *h)
207{
J-Alves19e20cf2023-08-02 12:48:55 +0100208 return (ffa_id_t)(h->sender_receiver);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100209}
210
211/* The maximum length possible for a single message. */
212#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
213
214struct ffa_partition_msg {
215 struct ffa_partition_rxtx_header header;
216 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
217};
218
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100219/* The maximum length possible for a single message. */
220#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
221
222enum ffa_data_access {
223 FFA_DATA_ACCESS_NOT_SPECIFIED,
224 FFA_DATA_ACCESS_RO,
225 FFA_DATA_ACCESS_RW,
226 FFA_DATA_ACCESS_RESERVED,
227};
228
229enum ffa_instruction_access {
230 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
231 FFA_INSTRUCTION_ACCESS_NX,
232 FFA_INSTRUCTION_ACCESS_X,
233 FFA_INSTRUCTION_ACCESS_RESERVED,
234};
235
236enum ffa_memory_type {
237 FFA_MEMORY_NOT_SPECIFIED_MEM,
238 FFA_MEMORY_DEVICE_MEM,
239 FFA_MEMORY_NORMAL_MEM,
240};
241
242enum ffa_memory_cacheability {
243 FFA_MEMORY_CACHE_RESERVED = 0x0,
244 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
245 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
246 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
247 FFA_MEMORY_DEV_NGNRNE = 0x0,
248 FFA_MEMORY_DEV_NGNRE = 0x1,
249 FFA_MEMORY_DEV_NGRE = 0x2,
250 FFA_MEMORY_DEV_GRE = 0x3,
251};
252
253enum ffa_memory_shareability {
254 FFA_MEMORY_SHARE_NON_SHAREABLE,
255 FFA_MEMORY_SHARE_RESERVED,
256 FFA_MEMORY_OUTER_SHAREABLE,
257 FFA_MEMORY_INNER_SHAREABLE,
258};
259
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100260/**
261 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
262 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
263 * and FFA_MEM_RETRIEVE_REQUEST.
264 */
265enum ffa_memory_security {
266 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
267 FFA_MEMORY_SECURITY_SECURE = 0,
268 FFA_MEMORY_SECURITY_NON_SECURE,
269};
270
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100271typedef uint8_t ffa_memory_access_permissions_t;
272
273/**
J-Alves0b6653d2022-04-22 13:17:38 +0100274 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100275 * region attributes descriptor".
276 */
J-Alves0b6653d2022-04-22 13:17:38 +0100277typedef uint16_t ffa_memory_attributes_t;
278
279/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
280#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100281
282#define FFA_DATA_ACCESS_OFFSET (0x0U)
283#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
284
285#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
286#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
287
288#define FFA_MEMORY_TYPE_OFFSET (0x4U)
289#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
290
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100291#define FFA_MEMORY_SECURITY_OFFSET (0x6U)
292#define FFA_MEMORY_SECURITY_MASK ((0x1U) << FFA_MEMORY_SECURITY_OFFSET)
293
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100294#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
295#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
296
297#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
298#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
299
300#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
301 static inline void ffa_set_##name##_attr(container_type *attr, \
302 const enum ffa_##name perm) \
303 { \
304 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
305 }
306
307#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
308 static inline enum ffa_##name ffa_get_##name##_attr( \
309 container_type attr) \
310 { \
311 return (enum ffa_##name)((attr & mask) >> offset); \
312 }
313
314ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
315 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
316ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
317 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
318
319ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
320 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
321ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
322 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
323
324ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
325 FFA_MEMORY_TYPE_MASK)
326ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
327 FFA_MEMORY_TYPE_MASK)
328
329ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
330 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
331ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
332 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
333
334ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
335 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
336ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
337 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
338
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100339ATTR_FUNCTION_SET(memory_security, ffa_memory_attributes_t,
340 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
341ATTR_FUNCTION_GET(memory_security, ffa_memory_attributes_t,
342 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
343
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100344/**
345 * A globally-unique ID assigned by the hypervisor for a region of memory being
346 * sent between VMs.
347 */
348typedef uint64_t ffa_memory_handle_t;
349
J-Alves917d2f22020-10-30 18:39:30 +0000350#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
351 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
352#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
353 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
354
355#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
356#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
357
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100358/**
359 * A count of VMs. This has the same range as the VM IDs but we give it a
360 * different name to make the different semantics clear.
361 */
J-Alves19e20cf2023-08-02 12:48:55 +0100362typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100363
364/** The index of a vCPU within a particular VM. */
365typedef uint16_t ffa_vcpu_index_t;
366
367/**
368 * A count of vCPUs. This has the same range as the vCPU indices but we give it
369 * a different name to make the different semantics clear.
370 */
371typedef ffa_vcpu_index_t ffa_vcpu_count_t;
372
373/** Parameter and return type of FF-A functions. */
374struct ffa_value {
375 uint64_t func;
376 uint64_t arg1;
377 uint64_t arg2;
378 uint64_t arg3;
379 uint64_t arg4;
380 uint64_t arg5;
381 uint64_t arg6;
382 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800383
384 struct {
385 uint64_t arg8;
386 uint64_t arg9;
387 uint64_t arg10;
388 uint64_t arg11;
389 uint64_t arg12;
390 uint64_t arg13;
391 uint64_t arg14;
392 uint64_t arg15;
393 uint64_t arg16;
394 uint64_t arg17;
395 bool valid;
396 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100397};
398
Olivier Depreze562e542020-06-11 17:31:54 +0200399static inline uint32_t ffa_func_id(struct ffa_value args)
400{
401 return args.func;
402}
403
J-Alves13318e32021-02-22 17:21:00 +0000404static inline int32_t ffa_error_code(struct ffa_value val)
405{
406 return (int32_t)val.arg2;
407}
408
J-Alves19e20cf2023-08-02 12:48:55 +0100409static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100410{
411 return (args.arg1 >> 16) & 0xffff;
412}
413
J-Alves19e20cf2023-08-02 12:48:55 +0100414static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100415{
416 return args.arg1 & 0xffff;
417}
418
419static inline uint32_t ffa_msg_send_size(struct ffa_value args)
420{
421 return args.arg3;
422}
423
Federico Recanati25053ee2022-03-14 15:01:53 +0100424static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
425{
426 return args.arg2;
427}
428
Olivier Depreze562e542020-06-11 17:31:54 +0200429static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
430{
431 return args.arg2;
432}
433
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800434static inline uint16_t ffa_partition_info_regs_get_last_idx(
435 struct ffa_value args)
436{
437 return args.arg2 & 0xFFFF;
438}
439
440static inline uint16_t ffa_partition_info_regs_get_curr_idx(
441 struct ffa_value args)
442{
443 return (args.arg2 >> 16) & 0xFFFF;
444}
445
446static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
447{
448 return (args.arg2 >> 32) & 0xFFFF;
449}
450
451static inline uint16_t ffa_partition_info_regs_get_desc_size(
452 struct ffa_value args)
453{
454 return (args.arg2 >> 48);
455}
456
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100457static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
458{
459 return (uint64_t)a1 | (uint64_t)a2 << 32;
460}
461
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100462static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
463{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100464 return ffa_assemble_handle(args.arg2, args.arg3);
465}
466
Andrew Walbranca808b12020-05-15 17:22:28 +0100467static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
468{
469 return ffa_assemble_handle(args.arg1, args.arg2);
470}
471
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100472static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
473{
474 return (struct ffa_value){.func = FFA_SUCCESS_32,
475 .arg2 = (uint32_t)handle,
476 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100477}
478
J-Alves19e20cf2023-08-02 12:48:55 +0100479static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100480{
481 return (args.arg1 >> 16) & 0xffff;
482}
483
484static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
485{
486 return args.arg1 & 0xffff;
487}
488
J-Alves19e20cf2023-08-02 12:48:55 +0100489static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100490{
491 return ((uint32_t)vm_id << 16) | vcpu_index;
492}
493
J-Alves19e20cf2023-08-02 12:48:55 +0100494static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100495{
496 return (args.arg4 >> 16) & 0xffff;
497}
498
J-Alves6f72ca82021-11-01 12:34:58 +0000499static inline uint32_t ffa_feature_intid(struct ffa_value args)
500{
501 return (uint32_t)args.arg2;
502}
503
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000504static inline uint32_t ffa_fwk_msg(struct ffa_value args)
505{
506 return (uint32_t)args.arg2;
507}
508
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100509/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100510 * Holds the UUID in a struct that is mappable directly to the SMCC calling
511 * convention, which is used for FF-A calls.
512 *
513 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
514 * of the SMCC Spec 1.2.
515 */
516struct ffa_uuid {
517 uint32_t uuid[4];
518};
519
520static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
521 uint32_t w3, struct ffa_uuid *uuid)
522{
523 uuid->uuid[0] = w0;
524 uuid->uuid[1] = w1;
525 uuid->uuid[2] = w2;
526 uuid->uuid[3] = w3;
527}
528
529static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
530 const struct ffa_uuid *uuid2)
531{
532 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
533 (uuid1->uuid[1] == uuid2->uuid[1]) &&
534 (uuid1->uuid[2] == uuid2->uuid[2]) &&
535 (uuid1->uuid[3] == uuid2->uuid[3]);
536}
537
538static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
539{
540 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
541 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
542}
543
544/**
545 * Flags to determine the partition properties, as required by
546 * FFA_PARTITION_INFO_GET.
547 *
Kathleen Capella402fa852022-11-09 16:16:51 -0500548 * 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 +0100549 * specification, "Partition information descriptor, partition properties".
550 */
551typedef uint32_t ffa_partition_properties_t;
552
553/** Partition property: partition supports receipt of direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500554#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100555
556/** Partition property: partition can send direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500557#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100558
559/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500560#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100561
J-Alves09ff9d82021-11-02 11:55:20 +0000562/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500563#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
564
565/** Partition property: partition runs in the AArch64 execution state. */
566#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +0000567
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100568/**
569 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
570 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -0500571 * This corresponds to table 13.37 "Partition information descriptor"
572 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000573 */
574struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +0100575 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000576 ffa_vcpu_count_t vcpu_count;
577 ffa_partition_properties_t properties;
578 struct ffa_uuid uuid;
579};
580
J-Alvesdd1ad572022-01-25 17:58:26 +0000581/** Length in bytes of the name in boot information descriptor. */
582#define FFA_BOOT_INFO_NAME_LEN 16
583
J-Alves240d84c2022-04-22 12:19:34 +0100584/**
585 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
586 * the FF-A v1.1 EAC0 specification.
587 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000588struct ffa_boot_info_desc {
589 char name[FFA_BOOT_INFO_NAME_LEN];
590 uint8_t type;
591 uint8_t reserved;
592 uint16_t flags;
593 uint32_t size;
594 uint64_t content;
595};
596
597/** FF-A boot information type mask. */
598#define FFA_BOOT_INFO_TYPE_SHIFT 7
599#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
600#define FFA_BOOT_INFO_TYPE_STD 0U
601#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
602
603/** Standard boot info type IDs. */
604#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
605#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
606#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
607
608/** FF-A Boot Info descriptors flags. */
609#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
610
611/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
612#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
613#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
614 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
615#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
616#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
617
618/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
619#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
620#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
621 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
622#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
623#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
624
625static inline uint16_t ffa_boot_info_content_format(
626 struct ffa_boot_info_desc *desc)
627{
628 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
629 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
630}
631
632static inline uint16_t ffa_boot_info_name_format(
633 struct ffa_boot_info_desc *desc)
634{
635 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
636 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
637}
638
639static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
640{
641 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
642}
643
644static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
645{
646 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
647 FFA_BOOT_INFO_TYPE_SHIFT;
648}
649
650/** Length in bytes of the signature in the boot descriptor. */
651#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
652
J-Alves240d84c2022-04-22 12:19:34 +0100653/**
654 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
655 * of the FF-A v1.1 EAC0 specification.
656 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000657struct ffa_boot_info_header {
658 uint32_t signature;
659 uint32_t version;
660 uint32_t info_blob_size;
661 uint32_t desc_size;
662 uint32_t desc_count;
663 uint32_t desc_offset;
664 uint64_t reserved;
665 struct ffa_boot_info_desc boot_info[];
666};
667
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100668/**
J-Alves980d1992021-03-18 12:49:18 +0000669 * FF-A v1.1 specification restricts the number of notifications to a maximum
670 * of 64. Following all possible bitmaps.
671 */
672#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
673
674typedef uint64_t ffa_notifications_bitmap_t;
675
676#define MAX_FFA_NOTIFICATIONS 64U
677
678/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000679 * Flag for notification bind and set, to specify call is about per-vCPU
680 * notifications.
681 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200682#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +0000683
Federico Recanatie73d2832022-04-20 11:10:52 +0200684#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
685#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
686
687/**
688 * Helper functions to check for buffer full notification.
689 */
690static inline bool is_ffa_hyp_buffer_full_notification(
691 ffa_notifications_bitmap_t framework)
692{
693 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
694}
695
696static inline bool is_ffa_spm_buffer_full_notification(
697 ffa_notifications_bitmap_t framework)
698{
699 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
700}
701
J-Alvesc003a7a2021-03-18 13:06:53 +0000702/**
J-Alves980d1992021-03-18 12:49:18 +0000703 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
704 * and hi.
705 * Helpful as FF-A specification defines that the notifications interfaces
706 * arguments are 32-bit registers.
707 */
708static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
709 uint32_t hi)
710{
711 return (ffa_notifications_bitmap_t)hi << 32U | lo;
712}
713
J-Alves98ff9562021-09-09 14:39:41 +0100714static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
715 struct ffa_value val)
716{
717 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
718}
719
720static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
721 struct ffa_value val)
722{
723 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
724}
725
Federico Recanatie73d2832022-04-20 11:10:52 +0200726static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
727 struct ffa_value val)
728{
729 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
730}
731
J-Alves980d1992021-03-18 12:49:18 +0000732/**
J-Alvesaa79c012021-07-09 14:29:45 +0100733 * Flags used in calls to FFA_NOTIFICATION_GET interface.
734 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200735#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
736#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
737#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
738#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +0100739
J-Alvesa3755072021-11-26 16:01:08 +0000740/* Flag to configure notification as being per vCPU. */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200741#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesa3755072021-11-26 16:01:08 +0000742
J-Alves13394022021-06-30 13:48:49 +0100743/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200744#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
745
746#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
747 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
J-Alves13394022021-06-30 13:48:49 +0100748
J-Alvesbe6e3032021-11-30 14:54:12 +0000749static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100750{
J-Alvesbe6e3032021-11-30 14:54:12 +0000751 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100752}
753
754/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100755 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
756 */
757#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
758
759/**
760 * Number of registers to use in successfull return of interface
761 * FFA_NOTIFICATION_INFO_GET.
762 */
763#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
764
765#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
766
767/**
768 * Helper macros for return parameter encoding as described in section 17.7.1
769 * of the FF-A v1.1 Beta0 specification.
770 */
771#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
772#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
773#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
774#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
775
776static inline uint32_t ffa_notification_info_get_lists_count(
777 struct ffa_value args)
778{
779 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
780 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
781}
782
783static inline uint32_t ffa_notification_info_get_list_size(
784 struct ffa_value args, unsigned int list_idx)
785{
786 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
787 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
788}
789
790static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
791{
792 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
793}
794
795/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100796 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +0100797 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
798 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100799 */
800struct ffa_memory_region_constituent {
801 /**
802 * The base IPA of the constituent memory region, aligned to 4 kiB page
803 * size granularity.
804 */
805 uint64_t address;
806 /** The number of 4 kiB pages in the constituent memory region. */
807 uint32_t page_count;
808 /** Reserved field, must be 0. */
809 uint32_t reserved;
810};
811
812/**
J-Alves0b6653d2022-04-22 13:17:38 +0100813 * A set of pages comprising a memory region. This corresponds to table 10.13 of
814 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100815 */
816struct ffa_composite_memory_region {
817 /**
818 * The total number of 4 kiB pages included in this memory region. This
819 * must be equal to the sum of page counts specified in each
820 * `ffa_memory_region_constituent`.
821 */
822 uint32_t page_count;
823 /**
824 * The number of constituents (`ffa_memory_region_constituent`)
825 * included in this memory region range.
826 */
827 uint32_t constituent_count;
828 /** Reserved field, must be 0. */
829 uint64_t reserved_0;
830 /** An array of `constituent_count` memory region constituents. */
831 struct ffa_memory_region_constituent constituents[];
832};
833
834/** Flags to indicate properties of receivers during memory region retrieval. */
835typedef uint8_t ffa_memory_receiver_flags_t;
836
837/**
J-Alves0b6653d2022-04-22 13:17:38 +0100838 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100839 * access permissions descriptor".
840 */
841struct ffa_memory_region_attributes {
842 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +0100843 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100844 /**
845 * The permissions with which the memory region should be mapped in the
846 * receiver's page table.
847 */
848 ffa_memory_access_permissions_t permissions;
849 /**
850 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
851 * for memory regions with multiple borrowers.
852 */
853 ffa_memory_receiver_flags_t flags;
854};
855
856/** Flags to control the behaviour of a memory sharing transaction. */
857typedef uint32_t ffa_memory_region_flags_t;
858
859/**
860 * Clear memory region contents after unmapping it from the sender and before
861 * mapping it for any receiver.
862 */
863#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
864
865/**
866 * Whether the hypervisor may time slice the memory sharing or retrieval
867 * operation.
868 */
869#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
870
871/**
872 * Whether the hypervisor should clear the memory region after the receiver
873 * relinquishes it or is aborted.
874 */
875#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
876
J-Alves3456e032023-07-20 12:20:05 +0100877/**
878 * On retrieve request, bypass the multi-borrower check.
879 */
880#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
881
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100882#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
883#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
884#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
885#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
886#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
887
Federico Recanati85090c42021-12-15 13:17:54 +0100888#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
889#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
890
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100891/**
J-Alves0b6653d2022-04-22 13:17:38 +0100892 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
893 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100894 */
895struct ffa_memory_access {
896 struct ffa_memory_region_attributes receiver_permissions;
897 /**
898 * Offset in bytes from the start of the outer `ffa_memory_region` to
899 * an `ffa_composite_memory_region` struct.
900 */
901 uint32_t composite_memory_region_offset;
902 uint64_t reserved_0;
903};
904
J-Alves363f5722022-04-25 17:37:37 +0100905/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +0100906#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +0100907
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908/**
909 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +0100910 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
911 * memory transaction descriptor". Note that it is also used for retrieve
912 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100913 */
914struct ffa_memory_region {
915 /**
916 * The ID of the VM which originally sent the memory region, i.e. the
917 * owner.
918 */
J-Alves19e20cf2023-08-02 12:48:55 +0100919 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100920 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100921 /** Flags to control behaviour of the transaction. */
922 ffa_memory_region_flags_t flags;
923 ffa_memory_handle_t handle;
924 /**
925 * An implementation defined value associated with the receiver and the
926 * memory region.
927 */
928 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +0100929 /* Size of the memory access descriptor. */
930 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 /**
932 * The number of `ffa_memory_access` entries included in this
933 * transaction.
934 */
935 uint32_t receiver_count;
936 /**
J-Alves0b6653d2022-04-22 13:17:38 +0100937 * Offset of the 'receivers' field, which relates to the memory access
938 * descriptors.
939 */
940 uint32_t receivers_offset;
941 /** Reserved field (12 bytes) must be 0. */
942 uint32_t reserved[3];
943 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +0100944 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100945 * Each one specifies a memory region offset, an endpoint and the
946 * attributes with which this memory region should be mapped in that
947 * endpoint's page table.
948 */
949 struct ffa_memory_access receivers[];
950};
951
952/**
953 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +0100954 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100955 * region".
956 */
957struct ffa_mem_relinquish {
958 ffa_memory_handle_t handle;
959 ffa_memory_region_flags_t flags;
960 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +0100961 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962};
963
964/**
965 * Gets the `ffa_composite_memory_region` for the given receiver from an
966 * `ffa_memory_region`, or NULL if it is not valid.
967 */
968static inline struct ffa_composite_memory_region *
969ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
970 uint32_t receiver_index)
971{
972 uint32_t offset = memory_region->receivers[receiver_index]
973 .composite_memory_region_offset;
974
975 if (offset == 0) {
976 return NULL;
977 }
978
979 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
980 offset);
981}
982
983static inline uint32_t ffa_mem_relinquish_init(
984 struct ffa_mem_relinquish *relinquish_request,
985 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +0100986 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100987{
988 relinquish_request->handle = handle;
989 relinquish_request->flags = flags;
990 relinquish_request->endpoint_count = 1;
991 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +0100992 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100993}
994
J-Alves126ab502022-09-29 11:37:33 +0100995void ffa_copy_memory_region_constituents(
996 struct ffa_memory_region_constituent *dest,
997 const struct ffa_memory_region_constituent *src);
998
Federico Recanati392be392022-02-08 20:53:03 +0100999/**
1000 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1001 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1002 * to the SPMC, in order to allow indirect messaging.
1003 */
1004struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001005 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001006 uint16_t reserved;
1007
1008 /*
1009 * 8-byte aligned offset from the base address of this descriptor to the
1010 * `ffa_composite_memory_region` describing the RX buffer.
1011 */
1012 uint32_t rx_offset;
1013
1014 /*
1015 * 8-byte aligned offset from the base address of this descriptor to the
1016 * `ffa_composite_memory_region` describing the TX buffer.
1017 */
1018 uint32_t tx_offset;
1019
1020 /* Pad to align on 16-byte boundary. */
1021 uint32_t pad;
1022};
1023
1024static inline struct ffa_composite_memory_region *
1025ffa_enpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1026{
1027 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1028 desc->rx_offset);
1029}
1030
1031static inline struct ffa_composite_memory_region *
1032ffa_enpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1033{
1034 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1035 desc->tx_offset);
1036}
1037
J-Alves2d8457f2022-10-05 11:06:41 +01001038void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001039 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001040 ffa_memory_attributes_t attributes,
1041 ffa_memory_region_flags_t flags,
1042 ffa_memory_handle_t handle, uint32_t tag,
1043 uint32_t receiver_count);
J-Alvesb44bb7d2022-04-25 15:43:46 +01001044void ffa_memory_access_init_permissions(
J-Alves19e20cf2023-08-02 12:48:55 +01001045 struct ffa_memory_access *receiver, ffa_id_t receiver_id,
J-Alvesb44bb7d2022-04-25 15:43:46 +01001046 enum ffa_data_access data_access,
1047 enum ffa_instruction_access instruction_access,
1048 ffa_memory_receiver_flags_t flags);
J-Alves45085432022-04-22 16:19:20 +01001049uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001050 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001051 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001052 const struct ffa_memory_region_constituent constituents[],
1053 uint32_t constituent_count, uint32_t tag,
1054 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1055 enum ffa_instruction_access instruction_access,
1056 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +01001057 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1058 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001059uint32_t ffa_memory_region_init(
1060 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001061 ffa_id_t sender, struct ffa_memory_access receivers[],
J-Alvesf4eecf72022-07-20 16:05:34 +01001062 uint32_t receiver_count,
1063 const struct ffa_memory_region_constituent constituents[],
1064 uint32_t constituent_count, uint32_t tag,
1065 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1066 enum ffa_memory_cacheability cacheability,
1067 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1068 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001069uint32_t ffa_memory_retrieve_request_init(
1070 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001071 ffa_id_t sender, struct ffa_memory_access receivers[],
J-Alves9b24ed82022-08-04 13:12:45 +01001072 uint32_t receiver_count, uint32_t tag, ffa_memory_region_flags_t flags,
1073 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1074 enum ffa_memory_shareability shareability);
1075uint32_t ffa_memory_retrieve_request_init_single_receiver(
1076 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001077 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001078 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1079 enum ffa_instruction_access instruction_access,
1080 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1081 enum ffa_memory_shareability shareability);
1082uint32_t ffa_memory_lender_retrieve_request_init(
1083 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001084 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001085uint32_t ffa_memory_fragment_init(
1086 struct ffa_memory_region_constituent *fragment,
1087 size_t fragment_max_size,
1088 const struct ffa_memory_region_constituent constituents[],
1089 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001090void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001091 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001092 uint64_t rx_address, uint64_t tx_address);