blob: 4ab3592920a1cc7959aeb3c4d8da28c496c1e898 [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
Kathleen Capellae4fe2962023-09-01 17:08:47 -040016#define FFA_VERSION_MINOR 0x2
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
Daniel Boulbyc7dc9322023-10-27 15:12:07 +010026/**
27 * Check major versions are equal and the minor version of the caller is
28 * less than or equal to the minor version of the callee.
29 */
30#define FFA_VERSIONS_ARE_COMPATIBLE(v_caller, v_callee) \
31 ((((v_caller >> FFA_VERSION_MAJOR_OFFSET) & FFA_VERSION_MAJOR_MASK) == \
32 ((v_callee >> FFA_VERSION_MAJOR_OFFSET) & \
33 FFA_VERSION_MAJOR_MASK)) && \
34 (((v_caller >> FFA_VERSION_MINOR_OFFSET) & FFA_VERSION_MINOR_MASK) <= \
35 ((v_callee >> FFA_VERSION_MINOR_OFFSET) & FFA_VERSION_MINOR_MASK)))
36
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010037/* clang-format off */
38
39#define FFA_LOW_32_ID 0x84000060
40#define FFA_HIGH_32_ID 0x8400007F
41#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010042#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010043
Karl Meakinf51a35f2023-08-07 17:53:52 +010044/**
45 * FF-A function identifiers.
46 * Don't forget to update `ffa_func_name` if you add a new one.
47 */
J-Alves3829fc02021-03-18 12:49:18 +000048#define FFA_ERROR_32 0x84000060
49#define FFA_SUCCESS_32 0x84000061
50#define FFA_SUCCESS_64 0xC4000061
51#define FFA_INTERRUPT_32 0x84000062
52#define FFA_VERSION_32 0x84000063
53#define FFA_FEATURES_32 0x84000064
54#define FFA_RX_RELEASE_32 0x84000065
55#define FFA_RXTX_MAP_32 0x84000066
56#define FFA_RXTX_MAP_64 0xC4000066
57#define FFA_RXTX_UNMAP_32 0x84000067
58#define FFA_PARTITION_INFO_GET_32 0x84000068
59#define FFA_ID_GET_32 0x84000069
60#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
61#define FFA_MSG_WAIT_32 0x8400006B
62#define FFA_YIELD_32 0x8400006C
63#define FFA_RUN_32 0x8400006D
64#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
65#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
66#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
67#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
68#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
69#define FFA_MEM_DONATE_32 0x84000071
70#define FFA_MEM_LEND_32 0x84000072
71#define FFA_MEM_SHARE_32 0x84000073
72#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
73#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
74#define FFA_MEM_RELINQUISH_32 0x84000076
75#define FFA_MEM_RECLAIM_32 0x84000077
76#define FFA_MEM_FRAG_RX_32 0x8400007A
77#define FFA_MEM_FRAG_TX_32 0x8400007B
78#define FFA_NORMAL_WORLD_RESUME 0x8400007C
79
80/* FF-A v1.1 */
81#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
82#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
83#define FFA_NOTIFICATION_BIND_32 0x8400007F
84#define FFA_NOTIFICATION_UNBIND_32 0x84000080
85#define FFA_NOTIFICATION_SET_32 0x84000081
86#define FFA_NOTIFICATION_GET_32 0x84000082
87#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
88#define FFA_RX_ACQUIRE_32 0x84000084
89#define FFA_SPM_ID_GET_32 0x84000085
90#define FFA_MSG_SEND2_32 0x84000086
91#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
92#define FFA_MEM_PERM_GET_32 0x84000088
93#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -070094#define FFA_MEM_PERM_GET_64 0xC4000088
95#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096
Kathleen Capellae4fe2962023-09-01 17:08:47 -040097/* FF-A v1.2 */
Maksims Svecovs71b76702022-05-20 15:32:58 +010098#define FFA_CONSOLE_LOG_32 0x8400008A
99#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -0800100#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -0600101#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Maksims Svecovs71b76702022-05-20 15:32:58 +0100102
Karl Meakinf51a35f2023-08-07 17:53:52 +0100103/**
104 * FF-A error codes.
105 * Don't forget to update `ffa_error_name` if you add a new one.
106 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100107#define FFA_NOT_SUPPORTED INT32_C(-1)
108#define FFA_INVALID_PARAMETERS INT32_C(-2)
109#define FFA_NO_MEMORY INT32_C(-3)
110#define FFA_BUSY INT32_C(-4)
111#define FFA_INTERRUPTED INT32_C(-5)
112#define FFA_DENIED INT32_C(-6)
113#define FFA_RETRY INT32_C(-7)
114#define FFA_ABORTED INT32_C(-8)
J-Alvesc8e8a222021-06-08 17:33:52 +0100115#define FFA_NO_DATA INT32_C(-9)
Kathleen Capellae4fe2962023-09-01 17:08:47 -0400116#define FFA_NOT_READY INT32_C(-10)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100117
118/* clang-format on */
119
Karl Meakinf51a35f2023-08-07 17:53:52 +0100120/* Return the name of the function identifier. */
121static inline const char *ffa_func_name(uint32_t func)
122{
123 switch (func) {
124 case FFA_ERROR_32:
125 return "FFA_ERROR_32";
126 case FFA_SUCCESS_32:
127 return "FFA_SUCCESS_32";
128 case FFA_SUCCESS_64:
129 return "FFA_SUCCESS_64";
130 case FFA_INTERRUPT_32:
131 return "FFA_INTERRUPT_32";
132 case FFA_VERSION_32:
133 return "FFA_VERSION_32";
134 case FFA_FEATURES_32:
135 return "FFA_FEATURES_32";
136 case FFA_RX_RELEASE_32:
137 return "FFA_RX_RELEASE_32";
138 case FFA_RXTX_MAP_32:
139 return "FFA_RXTX_MAP_32";
140 case FFA_RXTX_MAP_64:
141 return "FFA_RXTX_MAP_64";
142 case FFA_RXTX_UNMAP_32:
143 return "FFA_RXTX_UNMAP_32";
144 case FFA_PARTITION_INFO_GET_32:
145 return "FFA_PARTITION_INFO_GET_32";
146 case FFA_ID_GET_32:
147 return "FFA_ID_GET_32";
148 case FFA_MSG_POLL_32:
149 return "FFA_MSG_POLL_32";
150 case FFA_MSG_WAIT_32:
151 return "FFA_MSG_WAIT_32";
152 case FFA_YIELD_32:
153 return "FFA_YIELD_32";
154 case FFA_RUN_32:
155 return "FFA_RUN_32";
156 case FFA_MSG_SEND_32:
157 return "FFA_MSG_SEND_32";
158 case FFA_MSG_SEND_DIRECT_REQ_32:
159 return "FFA_MSG_SEND_DIRECT_REQ_32";
160 case FFA_MSG_SEND_DIRECT_REQ_64:
161 return "FFA_MSG_SEND_DIRECT_REQ_64";
162 case FFA_MSG_SEND_DIRECT_RESP_32:
163 return "FFA_MSG_SEND_DIRECT_RESP_32";
164 case FFA_MSG_SEND_DIRECT_RESP_64:
165 return "FFA_MSG_SEND_DIRECT_RESP_64";
166 case FFA_MEM_DONATE_32:
167 return "FFA_MEM_DONATE_32";
168 case FFA_MEM_LEND_32:
169 return "FFA_MEM_LEND_32";
170 case FFA_MEM_SHARE_32:
171 return "FFA_MEM_SHARE_32";
172 case FFA_MEM_RETRIEVE_REQ_32:
173 return "FFA_MEM_RETRIEVE_REQ_32";
174 case FFA_MEM_RETRIEVE_RESP_32:
175 return "FFA_MEM_RETRIEVE_RESP_32";
176 case FFA_MEM_RELINQUISH_32:
177 return "FFA_MEM_RELINQUISH_32";
178 case FFA_MEM_RECLAIM_32:
179 return "FFA_MEM_RECLAIM_32";
180 case FFA_MEM_FRAG_RX_32:
181 return "FFA_MEM_FRAG_RX_32";
182 case FFA_MEM_FRAG_TX_32:
183 return "FFA_MEM_FRAG_TX_32";
184 case FFA_NORMAL_WORLD_RESUME:
185 return "FFA_NORMAL_WORLD_RESUME";
186
187 /* FF-A v1.1 */
188 case FFA_NOTIFICATION_BITMAP_CREATE_32:
189 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
190 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
191 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
192 case FFA_NOTIFICATION_BIND_32:
193 return "FFA_NOTIFICATION_BIND_32";
194 case FFA_NOTIFICATION_UNBIND_32:
195 return "FFA_NOTIFICATION_UNBIND_32";
196 case FFA_NOTIFICATION_SET_32:
197 return "FFA_NOTIFICATION_SET_32";
198 case FFA_NOTIFICATION_GET_32:
199 return "FFA_NOTIFICATION_GET_32";
200 case FFA_NOTIFICATION_INFO_GET_64:
201 return "FFA_NOTIFICATION_INFO_GET_64";
202 case FFA_RX_ACQUIRE_32:
203 return "FFA_RX_ACQUIRE_32";
204 case FFA_SPM_ID_GET_32:
205 return "FFA_SPM_ID_GET_32";
206 case FFA_MSG_SEND2_32:
207 return "FFA_MSG_SEND2_32";
208 case FFA_SECONDARY_EP_REGISTER_64:
209 return "FFA_SECONDARY_EP_REGISTER_64";
210 case FFA_MEM_PERM_GET_32:
211 return "FFA_MEM_PERM_GET_32";
212 case FFA_MEM_PERM_SET_32:
213 return "FFA_MEM_PERM_SET_32";
214 case FFA_MEM_PERM_GET_64:
215 return "FFA_MEM_PERM_GET_64";
216 case FFA_MEM_PERM_SET_64:
217 return "FFA_MEM_PERM_SET_64";
218
219 /* Implementation-defined ABIs. */
220 case FFA_CONSOLE_LOG_32:
221 return "FFA_CONSOLE_LOG_32";
222 case FFA_CONSOLE_LOG_64:
223 return "FFA_CONSOLE_LOG_64";
224 case FFA_PARTITION_INFO_GET_REGS_64:
225 return "FFA_PARTITION_INFO_GET_REGS_64";
226 case FFA_EL3_INTR_HANDLE_32:
227 return "FFA_EL3_INTR_HANDLE_32";
228
229 default:
230 return "UNKNOWN";
231 }
232}
233
234/* Return the name of the error code. */
235static inline const char *ffa_error_name(int32_t error)
236{
237 switch (error) {
238 case FFA_NOT_SUPPORTED:
239 return "FFA_NOT_SUPPORTED";
240 case FFA_INVALID_PARAMETERS:
241 return "FFA_INVALID_PARAMETERS";
242 case FFA_NO_MEMORY:
243 return "FFA_NO_MEMORY";
244 case FFA_BUSY:
245 return "FFA_BUSY";
246 case FFA_INTERRUPTED:
247 return "FFA_INTERRUPTED";
248 case FFA_DENIED:
249 return "FFA_DENIED";
250 case FFA_RETRY:
251 return "FFA_RETRY";
252 case FFA_ABORTED:
253 return "FFA_ABORTED";
254 case FFA_NO_DATA:
255 return "FFA_NO_DATA";
256 default:
257 return "UNKNOWN";
258 }
259}
260
J-Alves6f72ca82021-11-01 12:34:58 +0000261/**
262 * FF-A Feature ID, to be used with interface FFA_FEATURES.
263 * As defined in the FF-A v1.1 Beta specification, table 13.10, in section
264 * 13.2.
265 */
266
Olivier Deprezb76307d2022-06-09 17:17:45 +0200267#define FFA_FEATURES_FUNC_ID_MASK (UINT32_C(1) << 31)
J-Alves6f72ca82021-11-01 12:34:58 +0000268#define FFA_FEATURES_FEATURE_ID_MASK UINT32_C(0x7F)
269
Karl Meakin34b8ae92023-01-13 13:33:07 +0000270/**
271 * Defined in Table 13.14 in the FF-A v.1.1 REL0 specification.
272 * Bits[31:2] and Bit[0] of input are reserved (must be zero).
273 * Bit[0]: dynamically allocated buffer support.
274 * Bit[1]: NS bit handling.
275 * Bit[2]: support for retrieval by hypervisor.
276 */
277#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT 0
278#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (UINT32_C(1) << 1)
279#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (UINT32_C(1) << 2)
280#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK \
281 (~(FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT | \
282 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT | \
283 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT))
284
J-Alves6f72ca82021-11-01 12:34:58 +0000285/* Query interrupt ID of Notification Pending Interrupt. */
286#define FFA_FEATURE_NPI 0x1U
287
288/* Query interrupt ID of Schedule Receiver Interrupt. */
289#define FFA_FEATURE_SRI 0x2U
290
291/* Query interrupt ID of the Managed Exit Interrupt. */
292#define FFA_FEATURE_MEI 0x3U
293
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100294/* FF-A function specific constants. */
295#define FFA_MSG_RECV_BLOCK 0x1
296#define FFA_MSG_RECV_BLOCK_MASK 0x1
297
298#define FFA_MSG_SEND_NOTIFY 0x1
299#define FFA_MSG_SEND_NOTIFY_MASK 0x1
300
301#define FFA_MEM_RECLAIM_CLEAR 0x1
302
303#define FFA_SLEEP_INDEFINITE 0
304
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700305#define FFA_MEM_PERM_RO UINT32_C(0x7)
306#define FFA_MEM_PERM_RW UINT32_C(0x5)
307#define FFA_MEM_PERM_RX UINT32_C(0x3)
308
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000309/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100310 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000311 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
312 * if partition info descriptors should be returned or just the count.
313 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100314#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
315#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000316
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100317/**
318 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
319 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
320 * configured to use.
321 */
J-Alves715d6232023-02-16 16:33:28 +0000322#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100323
Federico Recanatifc0295e2022-02-08 19:37:39 +0100324/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100325typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100326
327/**
J-Alves661e1b72023-08-02 13:39:40 +0100328 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
329 * parts:
330 * - Bit15 -> partition type identifier.
331 * - b'0 -> ID relates to a VM ID.
332 * - b'1 -> ID relates to an SP ID.
333 */
334#define FFA_ID_MASK ((ffa_id_t)0x8000)
335#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
336
337/**
338 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
339 */
340static inline bool ffa_is_vm_id(ffa_id_t id)
341{
342 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
343}
344
345/**
Federico Recanatifc0295e2022-02-08 19:37:39 +0100346 * Partition message header as specified by table 6.2 from FF-A v1.1 EAC0
347 * specification.
348 */
349struct ffa_partition_rxtx_header {
350 uint32_t flags; /* MBZ */
351 uint32_t reserved;
352 /* Offset from the beginning of the buffer to the message payload. */
353 uint32_t offset;
354 /* Sender(Bits[31:16]) and Receiver(Bits[15:0]) endpoint IDs. */
355 uint32_t sender_receiver;
356 /* Size of message in buffer. */
357 uint32_t size;
358};
359
360#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
361#define FFA_RXTX_SENDER_SHIFT (0x10U)
J-Alves70079932022-12-07 17:32:20 +0000362#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100363
364static inline void ffa_rxtx_header_init(
J-Alves19e20cf2023-08-02 12:48:55 +0100365 ffa_id_t sender, ffa_id_t receiver, uint32_t size,
Federico Recanatifc0295e2022-02-08 19:37:39 +0100366 struct ffa_partition_rxtx_header *header)
367{
368 header->flags = 0;
369 header->reserved = 0;
370 header->offset = FFA_RXTX_HEADER_SIZE;
371 header->sender_receiver =
372 (uint32_t)(receiver | (sender << FFA_RXTX_SENDER_SHIFT));
373 header->size = size;
374}
375
J-Alves19e20cf2023-08-02 12:48:55 +0100376static inline ffa_id_t ffa_rxtx_header_sender(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100377 const struct ffa_partition_rxtx_header *h)
378{
J-Alves19e20cf2023-08-02 12:48:55 +0100379 return (ffa_id_t)(h->sender_receiver >> FFA_RXTX_SENDER_SHIFT);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100380}
381
J-Alves19e20cf2023-08-02 12:48:55 +0100382static inline ffa_id_t ffa_rxtx_header_receiver(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100383 const struct ffa_partition_rxtx_header *h)
384{
J-Alves19e20cf2023-08-02 12:48:55 +0100385 return (ffa_id_t)(h->sender_receiver);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100386}
387
388/* The maximum length possible for a single message. */
389#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
390
391struct ffa_partition_msg {
392 struct ffa_partition_rxtx_header header;
393 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
394};
395
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100396/* The maximum length possible for a single message. */
397#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
398
399enum ffa_data_access {
400 FFA_DATA_ACCESS_NOT_SPECIFIED,
401 FFA_DATA_ACCESS_RO,
402 FFA_DATA_ACCESS_RW,
403 FFA_DATA_ACCESS_RESERVED,
404};
405
406enum ffa_instruction_access {
407 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
408 FFA_INSTRUCTION_ACCESS_NX,
409 FFA_INSTRUCTION_ACCESS_X,
410 FFA_INSTRUCTION_ACCESS_RESERVED,
411};
412
413enum ffa_memory_type {
414 FFA_MEMORY_NOT_SPECIFIED_MEM,
415 FFA_MEMORY_DEVICE_MEM,
416 FFA_MEMORY_NORMAL_MEM,
417};
418
419enum ffa_memory_cacheability {
420 FFA_MEMORY_CACHE_RESERVED = 0x0,
421 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
422 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
423 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
424 FFA_MEMORY_DEV_NGNRNE = 0x0,
425 FFA_MEMORY_DEV_NGNRE = 0x1,
426 FFA_MEMORY_DEV_NGRE = 0x2,
427 FFA_MEMORY_DEV_GRE = 0x3,
428};
429
430enum ffa_memory_shareability {
431 FFA_MEMORY_SHARE_NON_SHAREABLE,
432 FFA_MEMORY_SHARE_RESERVED,
433 FFA_MEMORY_OUTER_SHAREABLE,
434 FFA_MEMORY_INNER_SHAREABLE,
435};
436
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100437/**
438 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
439 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
440 * and FFA_MEM_RETRIEVE_REQUEST.
441 */
442enum ffa_memory_security {
443 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
444 FFA_MEMORY_SECURITY_SECURE = 0,
445 FFA_MEMORY_SECURITY_NON_SECURE,
446};
447
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100448typedef uint8_t ffa_memory_access_permissions_t;
449
450/**
J-Alves0b6653d2022-04-22 13:17:38 +0100451 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100452 * region attributes descriptor".
453 */
J-Alves0b6653d2022-04-22 13:17:38 +0100454typedef uint16_t ffa_memory_attributes_t;
455
456/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
457#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100458
459#define FFA_DATA_ACCESS_OFFSET (0x0U)
460#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
461
462#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
463#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
464
465#define FFA_MEMORY_TYPE_OFFSET (0x4U)
466#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
467
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100468#define FFA_MEMORY_SECURITY_OFFSET (0x6U)
469#define FFA_MEMORY_SECURITY_MASK ((0x1U) << FFA_MEMORY_SECURITY_OFFSET)
470
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100471#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
472#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
473
474#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
475#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
476
477#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
478 static inline void ffa_set_##name##_attr(container_type *attr, \
479 const enum ffa_##name perm) \
480 { \
481 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
482 }
483
484#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
485 static inline enum ffa_##name ffa_get_##name##_attr( \
486 container_type attr) \
487 { \
488 return (enum ffa_##name)((attr & mask) >> offset); \
489 }
490
491ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
492 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
493ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
494 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
495
496ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
497 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
498ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
499 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
500
501ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
502 FFA_MEMORY_TYPE_MASK)
503ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
504 FFA_MEMORY_TYPE_MASK)
505
506ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
507 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
508ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
509 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
510
511ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
512 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
513ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
514 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
515
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100516ATTR_FUNCTION_SET(memory_security, ffa_memory_attributes_t,
517 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
518ATTR_FUNCTION_GET(memory_security, ffa_memory_attributes_t,
519 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
520
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100521/**
522 * A globally-unique ID assigned by the hypervisor for a region of memory being
523 * sent between VMs.
524 */
525typedef uint64_t ffa_memory_handle_t;
526
J-Alves917d2f22020-10-30 18:39:30 +0000527#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
528 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
529#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
530 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
531
532#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
533#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
534
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100535/**
536 * A count of VMs. This has the same range as the VM IDs but we give it a
537 * different name to make the different semantics clear.
538 */
J-Alves19e20cf2023-08-02 12:48:55 +0100539typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100540
541/** The index of a vCPU within a particular VM. */
542typedef uint16_t ffa_vcpu_index_t;
543
544/**
545 * A count of vCPUs. This has the same range as the vCPU indices but we give it
546 * a different name to make the different semantics clear.
547 */
548typedef ffa_vcpu_index_t ffa_vcpu_count_t;
549
550/** Parameter and return type of FF-A functions. */
551struct ffa_value {
552 uint64_t func;
553 uint64_t arg1;
554 uint64_t arg2;
555 uint64_t arg3;
556 uint64_t arg4;
557 uint64_t arg5;
558 uint64_t arg6;
559 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800560
561 struct {
562 uint64_t arg8;
563 uint64_t arg9;
564 uint64_t arg10;
565 uint64_t arg11;
566 uint64_t arg12;
567 uint64_t arg13;
568 uint64_t arg14;
569 uint64_t arg15;
570 uint64_t arg16;
571 uint64_t arg17;
572 bool valid;
573 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100574};
575
Olivier Depreze562e542020-06-11 17:31:54 +0200576static inline uint32_t ffa_func_id(struct ffa_value args)
577{
578 return args.func;
579}
580
J-Alves13318e32021-02-22 17:21:00 +0000581static inline int32_t ffa_error_code(struct ffa_value val)
582{
583 return (int32_t)val.arg2;
584}
585
J-Alves19e20cf2023-08-02 12:48:55 +0100586static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100587{
588 return (args.arg1 >> 16) & 0xffff;
589}
590
J-Alves19e20cf2023-08-02 12:48:55 +0100591static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100592{
593 return args.arg1 & 0xffff;
594}
595
596static inline uint32_t ffa_msg_send_size(struct ffa_value args)
597{
598 return args.arg3;
599}
600
Federico Recanati25053ee2022-03-14 15:01:53 +0100601static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
602{
603 return args.arg2;
604}
605
Olivier Depreze562e542020-06-11 17:31:54 +0200606static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
607{
608 return args.arg2;
609}
610
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800611static inline uint16_t ffa_partition_info_regs_get_last_idx(
612 struct ffa_value args)
613{
614 return args.arg2 & 0xFFFF;
615}
616
617static inline uint16_t ffa_partition_info_regs_get_curr_idx(
618 struct ffa_value args)
619{
620 return (args.arg2 >> 16) & 0xFFFF;
621}
622
623static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
624{
625 return (args.arg2 >> 32) & 0xFFFF;
626}
627
628static inline uint16_t ffa_partition_info_regs_get_desc_size(
629 struct ffa_value args)
630{
631 return (args.arg2 >> 48);
632}
633
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100634static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
635{
636 return (uint64_t)a1 | (uint64_t)a2 << 32;
637}
638
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100639static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
640{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100641 return ffa_assemble_handle(args.arg2, args.arg3);
642}
643
Andrew Walbranca808b12020-05-15 17:22:28 +0100644static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
645{
646 return ffa_assemble_handle(args.arg1, args.arg2);
647}
648
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100649static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
650{
651 return (struct ffa_value){.func = FFA_SUCCESS_32,
652 .arg2 = (uint32_t)handle,
653 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100654}
655
J-Alves19e20cf2023-08-02 12:48:55 +0100656static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100657{
658 return (args.arg1 >> 16) & 0xffff;
659}
660
661static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
662{
663 return args.arg1 & 0xffff;
664}
665
J-Alves19e20cf2023-08-02 12:48:55 +0100666static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100667{
668 return ((uint32_t)vm_id << 16) | vcpu_index;
669}
670
J-Alves19e20cf2023-08-02 12:48:55 +0100671static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100672{
673 return (args.arg4 >> 16) & 0xffff;
674}
675
J-Alves6f72ca82021-11-01 12:34:58 +0000676static inline uint32_t ffa_feature_intid(struct ffa_value args)
677{
678 return (uint32_t)args.arg2;
679}
680
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000681static inline uint32_t ffa_fwk_msg(struct ffa_value args)
682{
683 return (uint32_t)args.arg2;
684}
685
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100686/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100687 * Holds the UUID in a struct that is mappable directly to the SMCC calling
688 * convention, which is used for FF-A calls.
689 *
690 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
691 * of the SMCC Spec 1.2.
692 */
693struct ffa_uuid {
694 uint32_t uuid[4];
695};
696
697static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
698 uint32_t w3, struct ffa_uuid *uuid)
699{
700 uuid->uuid[0] = w0;
701 uuid->uuid[1] = w1;
702 uuid->uuid[2] = w2;
703 uuid->uuid[3] = w3;
704}
705
706static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
707 const struct ffa_uuid *uuid2)
708{
709 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
710 (uuid1->uuid[1] == uuid2->uuid[1]) &&
711 (uuid1->uuid[2] == uuid2->uuid[2]) &&
712 (uuid1->uuid[3] == uuid2->uuid[3]);
713}
714
715static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
716{
717 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
718 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
719}
720
721/**
722 * Flags to determine the partition properties, as required by
723 * FFA_PARTITION_INFO_GET.
724 *
Kathleen Capella402fa852022-11-09 16:16:51 -0500725 * 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 +0100726 * specification, "Partition information descriptor, partition properties".
727 */
728typedef uint32_t ffa_partition_properties_t;
729
730/** Partition property: partition supports receipt of direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500731#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100732
733/** Partition property: partition can send direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500734#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100735
736/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500737#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100738
J-Alves09ff9d82021-11-02 11:55:20 +0000739/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500740#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
741
742/** Partition property: partition runs in the AArch64 execution state. */
743#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +0000744
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100745/**
746 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
747 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -0500748 * This corresponds to table 13.37 "Partition information descriptor"
749 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000750 */
751struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +0100752 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000753 ffa_vcpu_count_t vcpu_count;
754 ffa_partition_properties_t properties;
755 struct ffa_uuid uuid;
756};
757
J-Alvesdd1ad572022-01-25 17:58:26 +0000758/** Length in bytes of the name in boot information descriptor. */
759#define FFA_BOOT_INFO_NAME_LEN 16
760
J-Alves240d84c2022-04-22 12:19:34 +0100761/**
762 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
763 * the FF-A v1.1 EAC0 specification.
764 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000765struct ffa_boot_info_desc {
766 char name[FFA_BOOT_INFO_NAME_LEN];
767 uint8_t type;
768 uint8_t reserved;
769 uint16_t flags;
770 uint32_t size;
771 uint64_t content;
772};
773
774/** FF-A boot information type mask. */
775#define FFA_BOOT_INFO_TYPE_SHIFT 7
776#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
777#define FFA_BOOT_INFO_TYPE_STD 0U
778#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
779
780/** Standard boot info type IDs. */
781#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
782#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
783#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
784
785/** FF-A Boot Info descriptors flags. */
786#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
787
788/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
789#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
790#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
791 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
792#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
793#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
794
795/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
796#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
797#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
798 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
799#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
800#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
801
802static inline uint16_t ffa_boot_info_content_format(
803 struct ffa_boot_info_desc *desc)
804{
805 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
806 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
807}
808
809static inline uint16_t ffa_boot_info_name_format(
810 struct ffa_boot_info_desc *desc)
811{
812 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
813 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
814}
815
816static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
817{
818 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
819}
820
821static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
822{
823 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
824 FFA_BOOT_INFO_TYPE_SHIFT;
825}
826
827/** Length in bytes of the signature in the boot descriptor. */
828#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
829
J-Alves240d84c2022-04-22 12:19:34 +0100830/**
831 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
832 * of the FF-A v1.1 EAC0 specification.
833 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000834struct ffa_boot_info_header {
835 uint32_t signature;
836 uint32_t version;
837 uint32_t info_blob_size;
838 uint32_t desc_size;
839 uint32_t desc_count;
840 uint32_t desc_offset;
841 uint64_t reserved;
842 struct ffa_boot_info_desc boot_info[];
843};
844
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100845/**
J-Alves980d1992021-03-18 12:49:18 +0000846 * FF-A v1.1 specification restricts the number of notifications to a maximum
847 * of 64. Following all possible bitmaps.
848 */
849#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
850
851typedef uint64_t ffa_notifications_bitmap_t;
852
853#define MAX_FFA_NOTIFICATIONS 64U
854
855/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000856 * Flag for notification bind and set, to specify call is about per-vCPU
857 * notifications.
858 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200859#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +0000860
Federico Recanatie73d2832022-04-20 11:10:52 +0200861#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
862#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
863
864/**
865 * Helper functions to check for buffer full notification.
866 */
867static inline bool is_ffa_hyp_buffer_full_notification(
868 ffa_notifications_bitmap_t framework)
869{
870 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
871}
872
873static inline bool is_ffa_spm_buffer_full_notification(
874 ffa_notifications_bitmap_t framework)
875{
876 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
877}
878
J-Alvesc003a7a2021-03-18 13:06:53 +0000879/**
J-Alves980d1992021-03-18 12:49:18 +0000880 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
881 * and hi.
882 * Helpful as FF-A specification defines that the notifications interfaces
883 * arguments are 32-bit registers.
884 */
885static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
886 uint32_t hi)
887{
888 return (ffa_notifications_bitmap_t)hi << 32U | lo;
889}
890
J-Alves98ff9562021-09-09 14:39:41 +0100891static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
892 struct ffa_value val)
893{
894 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
895}
896
897static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
898 struct ffa_value val)
899{
900 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
901}
902
Federico Recanatie73d2832022-04-20 11:10:52 +0200903static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
904 struct ffa_value val)
905{
906 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
907}
908
J-Alves980d1992021-03-18 12:49:18 +0000909/**
J-Alvesaa79c012021-07-09 14:29:45 +0100910 * Flags used in calls to FFA_NOTIFICATION_GET interface.
911 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200912#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
913#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
914#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
915#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +0100916
J-Alvesa3755072021-11-26 16:01:08 +0000917/* Flag to configure notification as being per vCPU. */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200918#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesa3755072021-11-26 16:01:08 +0000919
J-Alves13394022021-06-30 13:48:49 +0100920/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200921#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
922
923#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
924 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
J-Alves13394022021-06-30 13:48:49 +0100925
J-Alvesbe6e3032021-11-30 14:54:12 +0000926static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100927{
J-Alvesbe6e3032021-11-30 14:54:12 +0000928 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100929}
930
931/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100932 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
933 */
934#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
935
936/**
937 * Number of registers to use in successfull return of interface
938 * FFA_NOTIFICATION_INFO_GET.
939 */
940#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
941
942#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
943
944/**
945 * Helper macros for return parameter encoding as described in section 17.7.1
946 * of the FF-A v1.1 Beta0 specification.
947 */
948#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
949#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
950#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
951#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
952
953static inline uint32_t ffa_notification_info_get_lists_count(
954 struct ffa_value args)
955{
956 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
957 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
958}
959
960static inline uint32_t ffa_notification_info_get_list_size(
961 struct ffa_value args, unsigned int list_idx)
962{
963 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
964 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
965}
966
967static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
968{
969 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
970}
971
972/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100973 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +0100974 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
975 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100976 */
977struct ffa_memory_region_constituent {
978 /**
979 * The base IPA of the constituent memory region, aligned to 4 kiB page
980 * size granularity.
981 */
982 uint64_t address;
983 /** The number of 4 kiB pages in the constituent memory region. */
984 uint32_t page_count;
985 /** Reserved field, must be 0. */
986 uint32_t reserved;
987};
988
989/**
J-Alves0b6653d2022-04-22 13:17:38 +0100990 * A set of pages comprising a memory region. This corresponds to table 10.13 of
991 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100992 */
993struct ffa_composite_memory_region {
994 /**
995 * The total number of 4 kiB pages included in this memory region. This
996 * must be equal to the sum of page counts specified in each
997 * `ffa_memory_region_constituent`.
998 */
999 uint32_t page_count;
1000 /**
1001 * The number of constituents (`ffa_memory_region_constituent`)
1002 * included in this memory region range.
1003 */
1004 uint32_t constituent_count;
1005 /** Reserved field, must be 0. */
1006 uint64_t reserved_0;
1007 /** An array of `constituent_count` memory region constituents. */
1008 struct ffa_memory_region_constituent constituents[];
1009};
1010
1011/** Flags to indicate properties of receivers during memory region retrieval. */
1012typedef uint8_t ffa_memory_receiver_flags_t;
1013
1014/**
J-Alves0b6653d2022-04-22 13:17:38 +01001015 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001016 * access permissions descriptor".
1017 */
1018struct ffa_memory_region_attributes {
1019 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001020 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001021 /**
1022 * The permissions with which the memory region should be mapped in the
1023 * receiver's page table.
1024 */
1025 ffa_memory_access_permissions_t permissions;
1026 /**
1027 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1028 * for memory regions with multiple borrowers.
1029 */
1030 ffa_memory_receiver_flags_t flags;
1031};
1032
1033/** Flags to control the behaviour of a memory sharing transaction. */
1034typedef uint32_t ffa_memory_region_flags_t;
1035
1036/**
1037 * Clear memory region contents after unmapping it from the sender and before
1038 * mapping it for any receiver.
1039 */
1040#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1041
1042/**
1043 * Whether the hypervisor may time slice the memory sharing or retrieval
1044 * operation.
1045 */
1046#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1047
1048/**
1049 * Whether the hypervisor should clear the memory region after the receiver
1050 * relinquishes it or is aborted.
1051 */
1052#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1053
J-Alves3456e032023-07-20 12:20:05 +01001054/**
1055 * On retrieve request, bypass the multi-borrower check.
1056 */
1057#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1058
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001059#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1060#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1061#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1062#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1063#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1064
Federico Recanati85090c42021-12-15 13:17:54 +01001065#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1066#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1067
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001068/**
J-Alves0b6653d2022-04-22 13:17:38 +01001069 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1070 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001071 */
1072struct ffa_memory_access {
1073 struct ffa_memory_region_attributes receiver_permissions;
1074 /**
1075 * Offset in bytes from the start of the outer `ffa_memory_region` to
1076 * an `ffa_composite_memory_region` struct.
1077 */
1078 uint32_t composite_memory_region_offset;
1079 uint64_t reserved_0;
1080};
1081
J-Alves363f5722022-04-25 17:37:37 +01001082/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001083#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001084
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001085/**
1086 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001087 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1088 * memory transaction descriptor". Note that it is also used for retrieve
1089 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001090 */
1091struct ffa_memory_region {
1092 /**
1093 * The ID of the VM which originally sent the memory region, i.e. the
1094 * owner.
1095 */
J-Alves19e20cf2023-08-02 12:48:55 +01001096 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001097 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001098 /** Flags to control behaviour of the transaction. */
1099 ffa_memory_region_flags_t flags;
1100 ffa_memory_handle_t handle;
1101 /**
1102 * An implementation defined value associated with the receiver and the
1103 * memory region.
1104 */
1105 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001106 /* Size of the memory access descriptor. */
1107 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001108 /**
1109 * The number of `ffa_memory_access` entries included in this
1110 * transaction.
1111 */
1112 uint32_t receiver_count;
1113 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001114 * Offset of the 'receivers' field, which relates to the memory access
1115 * descriptors.
1116 */
1117 uint32_t receivers_offset;
1118 /** Reserved field (12 bytes) must be 0. */
1119 uint32_t reserved[3];
1120 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +01001121 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001122 * Each one specifies a memory region offset, an endpoint and the
1123 * attributes with which this memory region should be mapped in that
1124 * endpoint's page table.
1125 */
1126 struct ffa_memory_access receivers[];
1127};
1128
1129/**
1130 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001131 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001132 * region".
1133 */
1134struct ffa_mem_relinquish {
1135 ffa_memory_handle_t handle;
1136 ffa_memory_region_flags_t flags;
1137 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001138 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001139};
1140
1141/**
Daniel Boulbyc7dc9322023-10-27 15:12:07 +01001142 * Returns the first FF-A version that matches the memory access descriptor
1143 * size.
1144 */
1145uint32_t ffa_version_from_memory_access_desc_size(
1146 uint32_t memory_access_desc_size);
1147
1148/**
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001149 * To maintain forwards compatability we can't make assumptions about the size
1150 * of the endpoint memory access descriptor so provide a helper function
1151 * to get a receiver from the receiver array using the memory access descriptor
1152 * size field from the memory region descriptor struct.
1153 * Returns NULL if we cannot return the receiver.
1154 */
1155static inline struct ffa_memory_access *ffa_memory_region_get_receiver(
1156 struct ffa_memory_region *memory_region, uint32_t receiver_index)
1157{
1158 uint32_t memory_access_desc_size =
1159 memory_region->memory_access_desc_size;
1160
1161 if (receiver_index >= memory_region->receiver_count) {
1162 return NULL;
1163 }
1164
1165 /*
1166 * Memory access descriptor size cannot be greater than the size of
1167 * the memory access descriptor defined by the current FF-A version.
1168 */
1169 if (memory_access_desc_size > sizeof(struct ffa_memory_access)) {
1170 return NULL;
1171 }
1172
1173 return (struct ffa_memory_access *)((uint8_t *)
1174 memory_region->receivers +
1175 (receiver_index *
1176 memory_access_desc_size));
1177}
1178
1179/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001180 * Gets the `ffa_composite_memory_region` for the given receiver from an
1181 * `ffa_memory_region`, or NULL if it is not valid.
1182 */
1183static inline struct ffa_composite_memory_region *
1184ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1185 uint32_t receiver_index)
1186{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001187 struct ffa_memory_access *receiver =
1188 ffa_memory_region_get_receiver(memory_region, receiver_index);
1189 uint32_t offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001190
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001191 if (receiver == NULL) {
1192 return NULL;
1193 }
1194
1195 offset = receiver->composite_memory_region_offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001196 if (offset == 0) {
1197 return NULL;
1198 }
1199
1200 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1201 offset);
1202}
1203
1204static inline uint32_t ffa_mem_relinquish_init(
1205 struct ffa_mem_relinquish *relinquish_request,
1206 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001207 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001208{
1209 relinquish_request->handle = handle;
1210 relinquish_request->flags = flags;
1211 relinquish_request->endpoint_count = 1;
1212 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001213 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001214}
1215
J-Alves126ab502022-09-29 11:37:33 +01001216void ffa_copy_memory_region_constituents(
1217 struct ffa_memory_region_constituent *dest,
1218 const struct ffa_memory_region_constituent *src);
1219
Federico Recanati392be392022-02-08 20:53:03 +01001220/**
1221 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1222 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1223 * to the SPMC, in order to allow indirect messaging.
1224 */
1225struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001226 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001227 uint16_t reserved;
1228
1229 /*
1230 * 8-byte aligned offset from the base address of this descriptor to the
1231 * `ffa_composite_memory_region` describing the RX buffer.
1232 */
1233 uint32_t rx_offset;
1234
1235 /*
1236 * 8-byte aligned offset from the base address of this descriptor to the
1237 * `ffa_composite_memory_region` describing the TX buffer.
1238 */
1239 uint32_t tx_offset;
1240
1241 /* Pad to align on 16-byte boundary. */
1242 uint32_t pad;
1243};
1244
1245static inline struct ffa_composite_memory_region *
1246ffa_enpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1247{
1248 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1249 desc->rx_offset);
1250}
1251
1252static inline struct ffa_composite_memory_region *
1253ffa_enpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1254{
1255 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1256 desc->tx_offset);
1257}
1258
J-Alves2d8457f2022-10-05 11:06:41 +01001259void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001260 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001261 ffa_memory_attributes_t attributes,
1262 ffa_memory_region_flags_t flags,
1263 ffa_memory_handle_t handle, uint32_t tag,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001264 uint32_t receiver_count,
1265 uint32_t receiver_desc_size);
J-Alvesb44bb7d2022-04-25 15:43:46 +01001266void ffa_memory_access_init_permissions(
J-Alves19e20cf2023-08-02 12:48:55 +01001267 struct ffa_memory_access *receiver, ffa_id_t receiver_id,
J-Alvesb44bb7d2022-04-25 15:43:46 +01001268 enum ffa_data_access data_access,
1269 enum ffa_instruction_access instruction_access,
1270 ffa_memory_receiver_flags_t flags);
J-Alves45085432022-04-22 16:19:20 +01001271uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001272 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001273 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001274 const struct ffa_memory_region_constituent constituents[],
1275 uint32_t constituent_count, uint32_t tag,
1276 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1277 enum ffa_instruction_access instruction_access,
1278 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +01001279 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1280 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001281uint32_t ffa_memory_region_init(
1282 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001283 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001284 uint32_t receiver_count, uint32_t receiver_desc_size,
J-Alvesf4eecf72022-07-20 16:05:34 +01001285 const struct ffa_memory_region_constituent constituents[],
1286 uint32_t constituent_count, uint32_t tag,
1287 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1288 enum ffa_memory_cacheability cacheability,
1289 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1290 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001291uint32_t ffa_memory_retrieve_request_init(
1292 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001293 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001294 uint32_t receiver_count, uint32_t receiver_desc_size, uint32_t tag,
1295 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1296 enum ffa_memory_cacheability cacheability,
J-Alves9b24ed82022-08-04 13:12:45 +01001297 enum ffa_memory_shareability shareability);
1298uint32_t ffa_memory_retrieve_request_init_single_receiver(
1299 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001300 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001301 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1302 enum ffa_instruction_access instruction_access,
1303 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1304 enum ffa_memory_shareability shareability);
1305uint32_t ffa_memory_lender_retrieve_request_init(
1306 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001307 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001308uint32_t ffa_memory_fragment_init(
1309 struct ffa_memory_region_constituent *fragment,
1310 size_t fragment_max_size,
1311 const struct ffa_memory_region_constituent constituents[],
1312 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001313void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001314 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001315 uint64_t rx_address, uint64_t tx_address);