blob: 8b61fbed998617a5b38fb62c9513dff4128f3cdb [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
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010026/* clang-format off */
27
28#define FFA_LOW_32_ID 0x84000060
29#define FFA_HIGH_32_ID 0x8400007F
30#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010031#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032
Karl Meakinf51a35f2023-08-07 17:53:52 +010033/**
34 * FF-A function identifiers.
35 * Don't forget to update `ffa_func_name` if you add a new one.
36 */
J-Alves3829fc02021-03-18 12:49:18 +000037#define FFA_ERROR_32 0x84000060
38#define FFA_SUCCESS_32 0x84000061
39#define FFA_SUCCESS_64 0xC4000061
40#define FFA_INTERRUPT_32 0x84000062
41#define FFA_VERSION_32 0x84000063
42#define FFA_FEATURES_32 0x84000064
43#define FFA_RX_RELEASE_32 0x84000065
44#define FFA_RXTX_MAP_32 0x84000066
45#define FFA_RXTX_MAP_64 0xC4000066
46#define FFA_RXTX_UNMAP_32 0x84000067
47#define FFA_PARTITION_INFO_GET_32 0x84000068
48#define FFA_ID_GET_32 0x84000069
49#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
50#define FFA_MSG_WAIT_32 0x8400006B
51#define FFA_YIELD_32 0x8400006C
52#define FFA_RUN_32 0x8400006D
53#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
54#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
55#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
56#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
57#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
58#define FFA_MEM_DONATE_32 0x84000071
59#define FFA_MEM_LEND_32 0x84000072
60#define FFA_MEM_SHARE_32 0x84000073
61#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
62#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
63#define FFA_MEM_RELINQUISH_32 0x84000076
64#define FFA_MEM_RECLAIM_32 0x84000077
65#define FFA_MEM_FRAG_RX_32 0x8400007A
66#define FFA_MEM_FRAG_TX_32 0x8400007B
67#define FFA_NORMAL_WORLD_RESUME 0x8400007C
68
69/* FF-A v1.1 */
70#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
71#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
72#define FFA_NOTIFICATION_BIND_32 0x8400007F
73#define FFA_NOTIFICATION_UNBIND_32 0x84000080
74#define FFA_NOTIFICATION_SET_32 0x84000081
75#define FFA_NOTIFICATION_GET_32 0x84000082
76#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
77#define FFA_RX_ACQUIRE_32 0x84000084
78#define FFA_SPM_ID_GET_32 0x84000085
79#define FFA_MSG_SEND2_32 0x84000086
80#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
81#define FFA_MEM_PERM_GET_32 0x84000088
82#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -070083#define FFA_MEM_PERM_GET_64 0xC4000088
84#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010085
Kathleen Capellae4fe2962023-09-01 17:08:47 -040086/* FF-A v1.2 */
Maksims Svecovs71b76702022-05-20 15:32:58 +010087#define FFA_CONSOLE_LOG_32 0x8400008A
88#define FFA_CONSOLE_LOG_64 0xC400008A
Raghu Krishnamurthy7592bcb2022-12-25 13:09:00 -080089#define FFA_PARTITION_INFO_GET_REGS_64 0xC400008B
Madhukar Pappireddy77d3bcd2023-03-01 17:26:22 -060090#define FFA_EL3_INTR_HANDLE_32 0x8400008C
Maksims Svecovs71b76702022-05-20 15:32:58 +010091
Karl Meakinf51a35f2023-08-07 17:53:52 +010092/**
93 * FF-A error codes.
94 * Don't forget to update `ffa_error_name` if you add a new one.
95 */
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010096#define FFA_NOT_SUPPORTED INT32_C(-1)
97#define FFA_INVALID_PARAMETERS INT32_C(-2)
98#define FFA_NO_MEMORY INT32_C(-3)
99#define FFA_BUSY INT32_C(-4)
100#define FFA_INTERRUPTED INT32_C(-5)
101#define FFA_DENIED INT32_C(-6)
102#define FFA_RETRY INT32_C(-7)
103#define FFA_ABORTED INT32_C(-8)
J-Alvesc8e8a222021-06-08 17:33:52 +0100104#define FFA_NO_DATA INT32_C(-9)
Kathleen Capellae4fe2962023-09-01 17:08:47 -0400105#define FFA_NOT_READY INT32_C(-10)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100106
107/* clang-format on */
108
Karl Meakinf51a35f2023-08-07 17:53:52 +0100109/* Return the name of the function identifier. */
110static inline const char *ffa_func_name(uint32_t func)
111{
112 switch (func) {
113 case FFA_ERROR_32:
114 return "FFA_ERROR_32";
115 case FFA_SUCCESS_32:
116 return "FFA_SUCCESS_32";
117 case FFA_SUCCESS_64:
118 return "FFA_SUCCESS_64";
119 case FFA_INTERRUPT_32:
120 return "FFA_INTERRUPT_32";
121 case FFA_VERSION_32:
122 return "FFA_VERSION_32";
123 case FFA_FEATURES_32:
124 return "FFA_FEATURES_32";
125 case FFA_RX_RELEASE_32:
126 return "FFA_RX_RELEASE_32";
127 case FFA_RXTX_MAP_32:
128 return "FFA_RXTX_MAP_32";
129 case FFA_RXTX_MAP_64:
130 return "FFA_RXTX_MAP_64";
131 case FFA_RXTX_UNMAP_32:
132 return "FFA_RXTX_UNMAP_32";
133 case FFA_PARTITION_INFO_GET_32:
134 return "FFA_PARTITION_INFO_GET_32";
135 case FFA_ID_GET_32:
136 return "FFA_ID_GET_32";
137 case FFA_MSG_POLL_32:
138 return "FFA_MSG_POLL_32";
139 case FFA_MSG_WAIT_32:
140 return "FFA_MSG_WAIT_32";
141 case FFA_YIELD_32:
142 return "FFA_YIELD_32";
143 case FFA_RUN_32:
144 return "FFA_RUN_32";
145 case FFA_MSG_SEND_32:
146 return "FFA_MSG_SEND_32";
147 case FFA_MSG_SEND_DIRECT_REQ_32:
148 return "FFA_MSG_SEND_DIRECT_REQ_32";
149 case FFA_MSG_SEND_DIRECT_REQ_64:
150 return "FFA_MSG_SEND_DIRECT_REQ_64";
151 case FFA_MSG_SEND_DIRECT_RESP_32:
152 return "FFA_MSG_SEND_DIRECT_RESP_32";
153 case FFA_MSG_SEND_DIRECT_RESP_64:
154 return "FFA_MSG_SEND_DIRECT_RESP_64";
155 case FFA_MEM_DONATE_32:
156 return "FFA_MEM_DONATE_32";
157 case FFA_MEM_LEND_32:
158 return "FFA_MEM_LEND_32";
159 case FFA_MEM_SHARE_32:
160 return "FFA_MEM_SHARE_32";
161 case FFA_MEM_RETRIEVE_REQ_32:
162 return "FFA_MEM_RETRIEVE_REQ_32";
163 case FFA_MEM_RETRIEVE_RESP_32:
164 return "FFA_MEM_RETRIEVE_RESP_32";
165 case FFA_MEM_RELINQUISH_32:
166 return "FFA_MEM_RELINQUISH_32";
167 case FFA_MEM_RECLAIM_32:
168 return "FFA_MEM_RECLAIM_32";
169 case FFA_MEM_FRAG_RX_32:
170 return "FFA_MEM_FRAG_RX_32";
171 case FFA_MEM_FRAG_TX_32:
172 return "FFA_MEM_FRAG_TX_32";
173 case FFA_NORMAL_WORLD_RESUME:
174 return "FFA_NORMAL_WORLD_RESUME";
175
176 /* FF-A v1.1 */
177 case FFA_NOTIFICATION_BITMAP_CREATE_32:
178 return "FFA_NOTIFICATION_BITMAP_CREATE_32";
179 case FFA_NOTIFICATION_BITMAP_DESTROY_32:
180 return "FFA_NOTIFICATION_BITMAP_DESTROY_32";
181 case FFA_NOTIFICATION_BIND_32:
182 return "FFA_NOTIFICATION_BIND_32";
183 case FFA_NOTIFICATION_UNBIND_32:
184 return "FFA_NOTIFICATION_UNBIND_32";
185 case FFA_NOTIFICATION_SET_32:
186 return "FFA_NOTIFICATION_SET_32";
187 case FFA_NOTIFICATION_GET_32:
188 return "FFA_NOTIFICATION_GET_32";
189 case FFA_NOTIFICATION_INFO_GET_64:
190 return "FFA_NOTIFICATION_INFO_GET_64";
191 case FFA_RX_ACQUIRE_32:
192 return "FFA_RX_ACQUIRE_32";
193 case FFA_SPM_ID_GET_32:
194 return "FFA_SPM_ID_GET_32";
195 case FFA_MSG_SEND2_32:
196 return "FFA_MSG_SEND2_32";
197 case FFA_SECONDARY_EP_REGISTER_64:
198 return "FFA_SECONDARY_EP_REGISTER_64";
199 case FFA_MEM_PERM_GET_32:
200 return "FFA_MEM_PERM_GET_32";
201 case FFA_MEM_PERM_SET_32:
202 return "FFA_MEM_PERM_SET_32";
203 case FFA_MEM_PERM_GET_64:
204 return "FFA_MEM_PERM_GET_64";
205 case FFA_MEM_PERM_SET_64:
206 return "FFA_MEM_PERM_SET_64";
207
208 /* Implementation-defined ABIs. */
209 case FFA_CONSOLE_LOG_32:
210 return "FFA_CONSOLE_LOG_32";
211 case FFA_CONSOLE_LOG_64:
212 return "FFA_CONSOLE_LOG_64";
213 case FFA_PARTITION_INFO_GET_REGS_64:
214 return "FFA_PARTITION_INFO_GET_REGS_64";
215 case FFA_EL3_INTR_HANDLE_32:
216 return "FFA_EL3_INTR_HANDLE_32";
217
218 default:
219 return "UNKNOWN";
220 }
221}
222
223/* Return the name of the error code. */
224static inline const char *ffa_error_name(int32_t error)
225{
226 switch (error) {
227 case FFA_NOT_SUPPORTED:
228 return "FFA_NOT_SUPPORTED";
229 case FFA_INVALID_PARAMETERS:
230 return "FFA_INVALID_PARAMETERS";
231 case FFA_NO_MEMORY:
232 return "FFA_NO_MEMORY";
233 case FFA_BUSY:
234 return "FFA_BUSY";
235 case FFA_INTERRUPTED:
236 return "FFA_INTERRUPTED";
237 case FFA_DENIED:
238 return "FFA_DENIED";
239 case FFA_RETRY:
240 return "FFA_RETRY";
241 case FFA_ABORTED:
242 return "FFA_ABORTED";
243 case FFA_NO_DATA:
244 return "FFA_NO_DATA";
245 default:
246 return "UNKNOWN";
247 }
248}
249
J-Alves6f72ca82021-11-01 12:34:58 +0000250/**
251 * FF-A Feature ID, to be used with interface FFA_FEATURES.
252 * As defined in the FF-A v1.1 Beta specification, table 13.10, in section
253 * 13.2.
254 */
255
Olivier Deprezb76307d2022-06-09 17:17:45 +0200256#define FFA_FEATURES_FUNC_ID_MASK (UINT32_C(1) << 31)
J-Alves6f72ca82021-11-01 12:34:58 +0000257#define FFA_FEATURES_FEATURE_ID_MASK UINT32_C(0x7F)
258
Karl Meakin34b8ae92023-01-13 13:33:07 +0000259/**
260 * Defined in Table 13.14 in the FF-A v.1.1 REL0 specification.
261 * Bits[31:2] and Bit[0] of input are reserved (must be zero).
262 * Bit[0]: dynamically allocated buffer support.
263 * Bit[1]: NS bit handling.
264 * Bit[2]: support for retrieval by hypervisor.
265 */
266#define FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT 0
267#define FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT (UINT32_C(1) << 1)
268#define FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT (UINT32_C(1) << 2)
269#define FFA_FEATURES_MEM_RETRIEVE_REQ_MBZ_MASK \
270 (~(FFA_FEATURES_MEM_RETRIEVE_REQ_BUFFER_SUPPORT | \
271 FFA_FEATURES_MEM_RETRIEVE_REQ_NS_SUPPORT | \
272 FFA_FEATURES_MEM_RETRIEVE_REQ_HYPERVISOR_SUPPORT))
273
J-Alves6f72ca82021-11-01 12:34:58 +0000274/* Query interrupt ID of Notification Pending Interrupt. */
275#define FFA_FEATURE_NPI 0x1U
276
277/* Query interrupt ID of Schedule Receiver Interrupt. */
278#define FFA_FEATURE_SRI 0x2U
279
280/* Query interrupt ID of the Managed Exit Interrupt. */
281#define FFA_FEATURE_MEI 0x3U
282
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100283/* FF-A function specific constants. */
284#define FFA_MSG_RECV_BLOCK 0x1
285#define FFA_MSG_RECV_BLOCK_MASK 0x1
286
287#define FFA_MSG_SEND_NOTIFY 0x1
288#define FFA_MSG_SEND_NOTIFY_MASK 0x1
289
290#define FFA_MEM_RECLAIM_CLEAR 0x1
291
292#define FFA_SLEEP_INDEFINITE 0
293
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700294#define FFA_MEM_PERM_RO UINT32_C(0x7)
295#define FFA_MEM_PERM_RW UINT32_C(0x5)
296#define FFA_MEM_PERM_RX UINT32_C(0x3)
297
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000298/*
Olivier Deprez013f4d62022-11-21 15:46:20 +0100299 * Defined in Table 13.34 in the FF-A v1.1 EAC0 specification.
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000300 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
301 * if partition info descriptors should be returned or just the count.
302 */
Olivier Deprez013f4d62022-11-21 15:46:20 +0100303#define FFA_PARTITION_COUNT_FLAG UINT32_C(0x1)
304#define FFA_PARTITION_COUNT_FLAG_MASK (UINT32_C(0x1) << 0)
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000305
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100306/**
307 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
308 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
309 * configured to use.
310 */
J-Alves715d6232023-02-16 16:33:28 +0000311#define FFA_PAGE_SIZE ((size_t)4096)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100312
Federico Recanatifc0295e2022-02-08 19:37:39 +0100313/** The ID of a VM. These are assigned sequentially starting with an offset. */
J-Alves19e20cf2023-08-02 12:48:55 +0100314typedef uint16_t ffa_id_t;
Federico Recanatifc0295e2022-02-08 19:37:39 +0100315
316/**
J-Alves661e1b72023-08-02 13:39:40 +0100317 * The FF-A v1.2 ALP0, section 6.1 defines that partition IDs are split into two
318 * parts:
319 * - Bit15 -> partition type identifier.
320 * - b'0 -> ID relates to a VM ID.
321 * - b'1 -> ID relates to an SP ID.
322 */
323#define FFA_ID_MASK ((ffa_id_t)0x8000)
324#define FFA_VM_ID_MASK ((ffa_id_t)0x0000)
325
326/**
327 * Helper to check if FF-A ID is a VM ID, managed by the hypervisor.
328 */
329static inline bool ffa_is_vm_id(ffa_id_t id)
330{
331 return (FFA_ID_MASK & id) == FFA_VM_ID_MASK;
332}
333
334/**
Federico Recanatifc0295e2022-02-08 19:37:39 +0100335 * Partition message header as specified by table 6.2 from FF-A v1.1 EAC0
336 * specification.
337 */
338struct ffa_partition_rxtx_header {
339 uint32_t flags; /* MBZ */
340 uint32_t reserved;
341 /* Offset from the beginning of the buffer to the message payload. */
342 uint32_t offset;
343 /* Sender(Bits[31:16]) and Receiver(Bits[15:0]) endpoint IDs. */
344 uint32_t sender_receiver;
345 /* Size of message in buffer. */
346 uint32_t size;
347};
348
349#define FFA_RXTX_HEADER_SIZE sizeof(struct ffa_partition_rxtx_header)
350#define FFA_RXTX_SENDER_SHIFT (0x10U)
J-Alves70079932022-12-07 17:32:20 +0000351#define FFA_RXTX_ALLOCATOR_SHIFT 16
Federico Recanatifc0295e2022-02-08 19:37:39 +0100352
353static inline void ffa_rxtx_header_init(
J-Alves19e20cf2023-08-02 12:48:55 +0100354 ffa_id_t sender, ffa_id_t receiver, uint32_t size,
Federico Recanatifc0295e2022-02-08 19:37:39 +0100355 struct ffa_partition_rxtx_header *header)
356{
357 header->flags = 0;
358 header->reserved = 0;
359 header->offset = FFA_RXTX_HEADER_SIZE;
360 header->sender_receiver =
361 (uint32_t)(receiver | (sender << FFA_RXTX_SENDER_SHIFT));
362 header->size = size;
363}
364
J-Alves19e20cf2023-08-02 12:48:55 +0100365static inline ffa_id_t ffa_rxtx_header_sender(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100366 const struct ffa_partition_rxtx_header *h)
367{
J-Alves19e20cf2023-08-02 12:48:55 +0100368 return (ffa_id_t)(h->sender_receiver >> FFA_RXTX_SENDER_SHIFT);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100369}
370
J-Alves19e20cf2023-08-02 12:48:55 +0100371static inline ffa_id_t ffa_rxtx_header_receiver(
Federico Recanatifc0295e2022-02-08 19:37:39 +0100372 const struct ffa_partition_rxtx_header *h)
373{
J-Alves19e20cf2023-08-02 12:48:55 +0100374 return (ffa_id_t)(h->sender_receiver);
Federico Recanatifc0295e2022-02-08 19:37:39 +0100375}
376
377/* The maximum length possible for a single message. */
378#define FFA_PARTITION_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - FFA_RXTX_HEADER_SIZE)
379
380struct ffa_partition_msg {
381 struct ffa_partition_rxtx_header header;
382 char payload[FFA_PARTITION_MSG_PAYLOAD_MAX];
383};
384
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100385/* The maximum length possible for a single message. */
386#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
387
388enum ffa_data_access {
389 FFA_DATA_ACCESS_NOT_SPECIFIED,
390 FFA_DATA_ACCESS_RO,
391 FFA_DATA_ACCESS_RW,
392 FFA_DATA_ACCESS_RESERVED,
393};
394
395enum ffa_instruction_access {
396 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
397 FFA_INSTRUCTION_ACCESS_NX,
398 FFA_INSTRUCTION_ACCESS_X,
399 FFA_INSTRUCTION_ACCESS_RESERVED,
400};
401
402enum ffa_memory_type {
403 FFA_MEMORY_NOT_SPECIFIED_MEM,
404 FFA_MEMORY_DEVICE_MEM,
405 FFA_MEMORY_NORMAL_MEM,
406};
407
408enum ffa_memory_cacheability {
409 FFA_MEMORY_CACHE_RESERVED = 0x0,
410 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
411 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
412 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
413 FFA_MEMORY_DEV_NGNRNE = 0x0,
414 FFA_MEMORY_DEV_NGNRE = 0x1,
415 FFA_MEMORY_DEV_NGRE = 0x2,
416 FFA_MEMORY_DEV_GRE = 0x3,
417};
418
419enum ffa_memory_shareability {
420 FFA_MEMORY_SHARE_NON_SHAREABLE,
421 FFA_MEMORY_SHARE_RESERVED,
422 FFA_MEMORY_OUTER_SHAREABLE,
423 FFA_MEMORY_INNER_SHAREABLE,
424};
425
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100426/**
427 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
428 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
429 * and FFA_MEM_RETRIEVE_REQUEST.
430 */
431enum ffa_memory_security {
432 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
433 FFA_MEMORY_SECURITY_SECURE = 0,
434 FFA_MEMORY_SECURITY_NON_SECURE,
435};
436
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100437typedef uint8_t ffa_memory_access_permissions_t;
438
439/**
J-Alves0b6653d2022-04-22 13:17:38 +0100440 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100441 * region attributes descriptor".
442 */
J-Alves0b6653d2022-04-22 13:17:38 +0100443typedef uint16_t ffa_memory_attributes_t;
444
445/* FF-A v1.1 EAC0 states bit [15:7] Must Be Zero. */
446#define FFA_MEMORY_ATTRIBUTES_MBZ_MASK 0xFF80U
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100447
448#define FFA_DATA_ACCESS_OFFSET (0x0U)
449#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
450
451#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
452#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
453
454#define FFA_MEMORY_TYPE_OFFSET (0x4U)
455#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
456
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100457#define FFA_MEMORY_SECURITY_OFFSET (0x6U)
458#define FFA_MEMORY_SECURITY_MASK ((0x1U) << FFA_MEMORY_SECURITY_OFFSET)
459
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100460#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
461#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
462
463#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
464#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
465
466#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
467 static inline void ffa_set_##name##_attr(container_type *attr, \
468 const enum ffa_##name perm) \
469 { \
470 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
471 }
472
473#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
474 static inline enum ffa_##name ffa_get_##name##_attr( \
475 container_type attr) \
476 { \
477 return (enum ffa_##name)((attr & mask) >> offset); \
478 }
479
480ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
481 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
482ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
483 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
484
485ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
486 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
487ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
488 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
489
490ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
491 FFA_MEMORY_TYPE_MASK)
492ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
493 FFA_MEMORY_TYPE_MASK)
494
495ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
496 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
497ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
498 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
499
500ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
501 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
502ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
503 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
504
Olivier Deprez4342a3c2022-02-28 09:37:25 +0100505ATTR_FUNCTION_SET(memory_security, ffa_memory_attributes_t,
506 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
507ATTR_FUNCTION_GET(memory_security, ffa_memory_attributes_t,
508 FFA_MEMORY_SECURITY_OFFSET, FFA_MEMORY_SECURITY_MASK)
509
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100510/**
511 * A globally-unique ID assigned by the hypervisor for a region of memory being
512 * sent between VMs.
513 */
514typedef uint64_t ffa_memory_handle_t;
515
J-Alves917d2f22020-10-30 18:39:30 +0000516#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
517 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
518#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
519 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
520
521#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
522#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
523
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100524/**
525 * A count of VMs. This has the same range as the VM IDs but we give it a
526 * different name to make the different semantics clear.
527 */
J-Alves19e20cf2023-08-02 12:48:55 +0100528typedef ffa_id_t ffa_vm_count_t;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100529
530/** The index of a vCPU within a particular VM. */
531typedef uint16_t ffa_vcpu_index_t;
532
533/**
534 * A count of vCPUs. This has the same range as the vCPU indices but we give it
535 * a different name to make the different semantics clear.
536 */
537typedef ffa_vcpu_index_t ffa_vcpu_count_t;
538
539/** Parameter and return type of FF-A functions. */
540struct ffa_value {
541 uint64_t func;
542 uint64_t arg1;
543 uint64_t arg2;
544 uint64_t arg3;
545 uint64_t arg4;
546 uint64_t arg5;
547 uint64_t arg6;
548 uint64_t arg7;
Raghu Krishnamurthy567068e2022-12-26 07:46:38 -0800549
550 struct {
551 uint64_t arg8;
552 uint64_t arg9;
553 uint64_t arg10;
554 uint64_t arg11;
555 uint64_t arg12;
556 uint64_t arg13;
557 uint64_t arg14;
558 uint64_t arg15;
559 uint64_t arg16;
560 uint64_t arg17;
561 bool valid;
562 } extended_val;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100563};
564
Olivier Depreze562e542020-06-11 17:31:54 +0200565static inline uint32_t ffa_func_id(struct ffa_value args)
566{
567 return args.func;
568}
569
J-Alves13318e32021-02-22 17:21:00 +0000570static inline int32_t ffa_error_code(struct ffa_value val)
571{
572 return (int32_t)val.arg2;
573}
574
J-Alves19e20cf2023-08-02 12:48:55 +0100575static inline ffa_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100576{
577 return (args.arg1 >> 16) & 0xffff;
578}
579
J-Alves19e20cf2023-08-02 12:48:55 +0100580static inline ffa_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100581{
582 return args.arg1 & 0xffff;
583}
584
585static inline uint32_t ffa_msg_send_size(struct ffa_value args)
586{
587 return args.arg3;
588}
589
Federico Recanati25053ee2022-03-14 15:01:53 +0100590static inline uint32_t ffa_msg_send2_flags(struct ffa_value args)
591{
592 return args.arg2;
593}
594
Olivier Depreze562e542020-06-11 17:31:54 +0200595static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
596{
597 return args.arg2;
598}
599
Raghu Krishnamurthy2957b922022-12-27 10:29:12 -0800600static inline uint16_t ffa_partition_info_regs_get_last_idx(
601 struct ffa_value args)
602{
603 return args.arg2 & 0xFFFF;
604}
605
606static inline uint16_t ffa_partition_info_regs_get_curr_idx(
607 struct ffa_value args)
608{
609 return (args.arg2 >> 16) & 0xFFFF;
610}
611
612static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
613{
614 return (args.arg2 >> 32) & 0xFFFF;
615}
616
617static inline uint16_t ffa_partition_info_regs_get_desc_size(
618 struct ffa_value args)
619{
620 return (args.arg2 >> 48);
621}
622
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100623static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
624{
625 return (uint64_t)a1 | (uint64_t)a2 << 32;
626}
627
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100628static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
629{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100630 return ffa_assemble_handle(args.arg2, args.arg3);
631}
632
Andrew Walbranca808b12020-05-15 17:22:28 +0100633static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
634{
635 return ffa_assemble_handle(args.arg1, args.arg2);
636}
637
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100638static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
639{
640 return (struct ffa_value){.func = FFA_SUCCESS_32,
641 .arg2 = (uint32_t)handle,
642 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100643}
644
J-Alves19e20cf2023-08-02 12:48:55 +0100645static inline ffa_id_t ffa_vm_id(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100646{
647 return (args.arg1 >> 16) & 0xffff;
648}
649
650static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
651{
652 return args.arg1 & 0xffff;
653}
654
J-Alves19e20cf2023-08-02 12:48:55 +0100655static inline uint64_t ffa_vm_vcpu(ffa_id_t vm_id, ffa_vcpu_index_t vcpu_index)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100656{
657 return ((uint32_t)vm_id << 16) | vcpu_index;
658}
659
J-Alves19e20cf2023-08-02 12:48:55 +0100660static inline ffa_id_t ffa_frag_sender(struct ffa_value args)
Andrew Walbranca808b12020-05-15 17:22:28 +0100661{
662 return (args.arg4 >> 16) & 0xffff;
663}
664
J-Alves6f72ca82021-11-01 12:34:58 +0000665static inline uint32_t ffa_feature_intid(struct ffa_value args)
666{
667 return (uint32_t)args.arg2;
668}
669
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000670static inline uint32_t ffa_fwk_msg(struct ffa_value args)
671{
672 return (uint32_t)args.arg2;
673}
674
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100675/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100676 * Holds the UUID in a struct that is mappable directly to the SMCC calling
677 * convention, which is used for FF-A calls.
678 *
679 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
680 * of the SMCC Spec 1.2.
681 */
682struct ffa_uuid {
683 uint32_t uuid[4];
684};
685
686static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
687 uint32_t w3, struct ffa_uuid *uuid)
688{
689 uuid->uuid[0] = w0;
690 uuid->uuid[1] = w1;
691 uuid->uuid[2] = w2;
692 uuid->uuid[3] = w3;
693}
694
695static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
696 const struct ffa_uuid *uuid2)
697{
698 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
699 (uuid1->uuid[1] == uuid2->uuid[1]) &&
700 (uuid1->uuid[2] == uuid2->uuid[2]) &&
701 (uuid1->uuid[3] == uuid2->uuid[3]);
702}
703
704static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
705{
706 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
707 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
708}
709
710/**
711 * Flags to determine the partition properties, as required by
712 * FFA_PARTITION_INFO_GET.
713 *
Kathleen Capella402fa852022-11-09 16:16:51 -0500714 * 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 +0100715 * specification, "Partition information descriptor, partition properties".
716 */
717typedef uint32_t ffa_partition_properties_t;
718
719/** Partition property: partition supports receipt of direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500720#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100721
722/** Partition property: partition can send direct requests. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500723#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100724
725/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500726#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100727
J-Alves09ff9d82021-11-02 11:55:20 +0000728/** Partition property: partition can receive notifications. */
Kathleen Capella402fa852022-11-09 16:16:51 -0500729#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
730
731/** Partition property: partition runs in the AArch64 execution state. */
732#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
J-Alves09ff9d82021-11-02 11:55:20 +0000733
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100734/**
735 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
736 * interface.
Kathleen Capella402fa852022-11-09 16:16:51 -0500737 * This corresponds to table 13.37 "Partition information descriptor"
738 * in FF-A 1.1 EAC0 specification.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000739 */
740struct ffa_partition_info {
J-Alves19e20cf2023-08-02 12:48:55 +0100741 ffa_id_t vm_id;
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000742 ffa_vcpu_count_t vcpu_count;
743 ffa_partition_properties_t properties;
744 struct ffa_uuid uuid;
745};
746
J-Alvesdd1ad572022-01-25 17:58:26 +0000747/** Length in bytes of the name in boot information descriptor. */
748#define FFA_BOOT_INFO_NAME_LEN 16
749
J-Alves240d84c2022-04-22 12:19:34 +0100750/**
751 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
752 * the FF-A v1.1 EAC0 specification.
753 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000754struct ffa_boot_info_desc {
755 char name[FFA_BOOT_INFO_NAME_LEN];
756 uint8_t type;
757 uint8_t reserved;
758 uint16_t flags;
759 uint32_t size;
760 uint64_t content;
761};
762
763/** FF-A boot information type mask. */
764#define FFA_BOOT_INFO_TYPE_SHIFT 7
765#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
766#define FFA_BOOT_INFO_TYPE_STD 0U
767#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
768
769/** Standard boot info type IDs. */
770#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
771#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
772#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
773
774/** FF-A Boot Info descriptors flags. */
775#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
776
777/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
778#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
779#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
780 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
781#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
782#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
783
784/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
785#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
786#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
787 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
788#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
789#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
790
791static inline uint16_t ffa_boot_info_content_format(
792 struct ffa_boot_info_desc *desc)
793{
794 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
795 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
796}
797
798static inline uint16_t ffa_boot_info_name_format(
799 struct ffa_boot_info_desc *desc)
800{
801 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
802 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
803}
804
805static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
806{
807 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
808}
809
810static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
811{
812 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
813 FFA_BOOT_INFO_TYPE_SHIFT;
814}
815
816/** Length in bytes of the signature in the boot descriptor. */
817#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
818
J-Alves240d84c2022-04-22 12:19:34 +0100819/**
820 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
821 * of the FF-A v1.1 EAC0 specification.
822 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000823struct ffa_boot_info_header {
824 uint32_t signature;
825 uint32_t version;
826 uint32_t info_blob_size;
827 uint32_t desc_size;
828 uint32_t desc_count;
829 uint32_t desc_offset;
830 uint64_t reserved;
831 struct ffa_boot_info_desc boot_info[];
832};
833
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100834/**
J-Alves980d1992021-03-18 12:49:18 +0000835 * FF-A v1.1 specification restricts the number of notifications to a maximum
836 * of 64. Following all possible bitmaps.
837 */
838#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
839
840typedef uint64_t ffa_notifications_bitmap_t;
841
842#define MAX_FFA_NOTIFICATIONS 64U
843
844/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000845 * Flag for notification bind and set, to specify call is about per-vCPU
846 * notifications.
847 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200848#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +0000849
Federico Recanatie73d2832022-04-20 11:10:52 +0200850#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
851#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
852
853/**
854 * Helper functions to check for buffer full notification.
855 */
856static inline bool is_ffa_hyp_buffer_full_notification(
857 ffa_notifications_bitmap_t framework)
858{
859 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
860}
861
862static inline bool is_ffa_spm_buffer_full_notification(
863 ffa_notifications_bitmap_t framework)
864{
865 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
866}
867
J-Alvesc003a7a2021-03-18 13:06:53 +0000868/**
J-Alves980d1992021-03-18 12:49:18 +0000869 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
870 * and hi.
871 * Helpful as FF-A specification defines that the notifications interfaces
872 * arguments are 32-bit registers.
873 */
874static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
875 uint32_t hi)
876{
877 return (ffa_notifications_bitmap_t)hi << 32U | lo;
878}
879
J-Alves98ff9562021-09-09 14:39:41 +0100880static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
881 struct ffa_value val)
882{
883 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
884}
885
886static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
887 struct ffa_value val)
888{
889 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
890}
891
Federico Recanatie73d2832022-04-20 11:10:52 +0200892static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
893 struct ffa_value val)
894{
895 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
896}
897
J-Alves980d1992021-03-18 12:49:18 +0000898/**
J-Alvesaa79c012021-07-09 14:29:45 +0100899 * Flags used in calls to FFA_NOTIFICATION_GET interface.
900 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200901#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
902#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
903#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
904#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +0100905
J-Alvesa3755072021-11-26 16:01:08 +0000906/* Flag to configure notification as being per vCPU. */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200907#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesa3755072021-11-26 16:01:08 +0000908
J-Alves13394022021-06-30 13:48:49 +0100909/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200910#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
911
912#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
913 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
J-Alves13394022021-06-30 13:48:49 +0100914
J-Alvesbe6e3032021-11-30 14:54:12 +0000915static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100916{
J-Alvesbe6e3032021-11-30 14:54:12 +0000917 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100918}
919
920/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100921 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
922 */
923#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
924
925/**
926 * Number of registers to use in successfull return of interface
927 * FFA_NOTIFICATION_INFO_GET.
928 */
929#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
930
931#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
932
933/**
934 * Helper macros for return parameter encoding as described in section 17.7.1
935 * of the FF-A v1.1 Beta0 specification.
936 */
937#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
938#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
939#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
940#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
941
942static inline uint32_t ffa_notification_info_get_lists_count(
943 struct ffa_value args)
944{
945 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
946 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
947}
948
949static inline uint32_t ffa_notification_info_get_list_size(
950 struct ffa_value args, unsigned int list_idx)
951{
952 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
953 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
954}
955
956static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
957{
958 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
959}
960
961/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100962 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alves0b6653d2022-04-22 13:17:38 +0100963 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
964 * region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100965 */
966struct ffa_memory_region_constituent {
967 /**
968 * The base IPA of the constituent memory region, aligned to 4 kiB page
969 * size granularity.
970 */
971 uint64_t address;
972 /** The number of 4 kiB pages in the constituent memory region. */
973 uint32_t page_count;
974 /** Reserved field, must be 0. */
975 uint32_t reserved;
976};
977
978/**
J-Alves0b6653d2022-04-22 13:17:38 +0100979 * A set of pages comprising a memory region. This corresponds to table 10.13 of
980 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100981 */
982struct ffa_composite_memory_region {
983 /**
984 * The total number of 4 kiB pages included in this memory region. This
985 * must be equal to the sum of page counts specified in each
986 * `ffa_memory_region_constituent`.
987 */
988 uint32_t page_count;
989 /**
990 * The number of constituents (`ffa_memory_region_constituent`)
991 * included in this memory region range.
992 */
993 uint32_t constituent_count;
994 /** Reserved field, must be 0. */
995 uint64_t reserved_0;
996 /** An array of `constituent_count` memory region constituents. */
997 struct ffa_memory_region_constituent constituents[];
998};
999
1000/** Flags to indicate properties of receivers during memory region retrieval. */
1001typedef uint8_t ffa_memory_receiver_flags_t;
1002
1003/**
J-Alves0b6653d2022-04-22 13:17:38 +01001004 * This corresponds to table 10.15 of the FF-A v1.1 EAC0 specification, "Memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001005 * access permissions descriptor".
1006 */
1007struct ffa_memory_region_attributes {
1008 /** The ID of the VM to which the memory is being given or shared. */
J-Alves19e20cf2023-08-02 12:48:55 +01001009 ffa_id_t receiver;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001010 /**
1011 * The permissions with which the memory region should be mapped in the
1012 * receiver's page table.
1013 */
1014 ffa_memory_access_permissions_t permissions;
1015 /**
1016 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
1017 * for memory regions with multiple borrowers.
1018 */
1019 ffa_memory_receiver_flags_t flags;
1020};
1021
1022/** Flags to control the behaviour of a memory sharing transaction. */
1023typedef uint32_t ffa_memory_region_flags_t;
1024
1025/**
1026 * Clear memory region contents after unmapping it from the sender and before
1027 * mapping it for any receiver.
1028 */
1029#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
1030
1031/**
1032 * Whether the hypervisor may time slice the memory sharing or retrieval
1033 * operation.
1034 */
1035#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
1036
1037/**
1038 * Whether the hypervisor should clear the memory region after the receiver
1039 * relinquishes it or is aborted.
1040 */
1041#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
1042
J-Alves3456e032023-07-20 12:20:05 +01001043/**
1044 * On retrieve request, bypass the multi-borrower check.
1045 */
1046#define FFA_MEMORY_REGION_FLAG_BYPASS_BORROWERS_CHECK (0x1U << 10)
1047
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001048#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
1049#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
1050#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
1051#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
1052#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
1053
Federico Recanati85090c42021-12-15 13:17:54 +01001054#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
1055#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
1056
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001057/**
J-Alves0b6653d2022-04-22 13:17:38 +01001058 * This corresponds to table 10.16 of the FF-A v1.1 EAC0 specification,
1059 * "Endpoint memory access descriptor".
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001060 */
1061struct ffa_memory_access {
1062 struct ffa_memory_region_attributes receiver_permissions;
1063 /**
1064 * Offset in bytes from the start of the outer `ffa_memory_region` to
1065 * an `ffa_composite_memory_region` struct.
1066 */
1067 uint32_t composite_memory_region_offset;
1068 uint64_t reserved_0;
1069};
1070
J-Alves363f5722022-04-25 17:37:37 +01001071/** The maximum number of recipients a memory region may be sent to. */
J-Alvesba0e6172022-04-25 17:41:40 +01001072#define MAX_MEM_SHARE_RECIPIENTS UINT32_C(2)
J-Alves363f5722022-04-25 17:37:37 +01001073
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001074/**
1075 * Information about a set of pages which are being shared. This corresponds to
J-Alves0b6653d2022-04-22 13:17:38 +01001076 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
1077 * memory transaction descriptor". Note that it is also used for retrieve
1078 * requests and responses.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001079 */
1080struct ffa_memory_region {
1081 /**
1082 * The ID of the VM which originally sent the memory region, i.e. the
1083 * owner.
1084 */
J-Alves19e20cf2023-08-02 12:48:55 +01001085 ffa_id_t sender;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001086 ffa_memory_attributes_t attributes;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001087 /** Flags to control behaviour of the transaction. */
1088 ffa_memory_region_flags_t flags;
1089 ffa_memory_handle_t handle;
1090 /**
1091 * An implementation defined value associated with the receiver and the
1092 * memory region.
1093 */
1094 uint64_t tag;
J-Alves0b6653d2022-04-22 13:17:38 +01001095 /* Size of the memory access descriptor. */
1096 uint32_t memory_access_desc_size;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001097 /**
1098 * The number of `ffa_memory_access` entries included in this
1099 * transaction.
1100 */
1101 uint32_t receiver_count;
1102 /**
J-Alves0b6653d2022-04-22 13:17:38 +01001103 * Offset of the 'receivers' field, which relates to the memory access
1104 * descriptors.
1105 */
1106 uint32_t receivers_offset;
1107 /** Reserved field (12 bytes) must be 0. */
1108 uint32_t reserved[3];
1109 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +01001110 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001111 * Each one specifies a memory region offset, an endpoint and the
1112 * attributes with which this memory region should be mapped in that
1113 * endpoint's page table.
1114 */
1115 struct ffa_memory_access receivers[];
1116};
1117
1118/**
1119 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alves0b6653d2022-04-22 13:17:38 +01001120 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001121 * region".
1122 */
1123struct ffa_mem_relinquish {
1124 ffa_memory_handle_t handle;
1125 ffa_memory_region_flags_t flags;
1126 uint32_t endpoint_count;
J-Alves19e20cf2023-08-02 12:48:55 +01001127 ffa_id_t endpoints[];
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001128};
1129
1130/**
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001131 * To maintain forwards compatability we can't make assumptions about the size
1132 * of the endpoint memory access descriptor so provide a helper function
1133 * to get a receiver from the receiver array using the memory access descriptor
1134 * size field from the memory region descriptor struct.
1135 * Returns NULL if we cannot return the receiver.
1136 */
1137static inline struct ffa_memory_access *ffa_memory_region_get_receiver(
1138 struct ffa_memory_region *memory_region, uint32_t receiver_index)
1139{
1140 uint32_t memory_access_desc_size =
1141 memory_region->memory_access_desc_size;
1142
1143 if (receiver_index >= memory_region->receiver_count) {
1144 return NULL;
1145 }
1146
1147 /*
1148 * Memory access descriptor size cannot be greater than the size of
1149 * the memory access descriptor defined by the current FF-A version.
1150 */
1151 if (memory_access_desc_size > sizeof(struct ffa_memory_access)) {
1152 return NULL;
1153 }
1154
1155 return (struct ffa_memory_access *)((uint8_t *)
1156 memory_region->receivers +
1157 (receiver_index *
1158 memory_access_desc_size));
1159}
1160
1161/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001162 * Gets the `ffa_composite_memory_region` for the given receiver from an
1163 * `ffa_memory_region`, or NULL if it is not valid.
1164 */
1165static inline struct ffa_composite_memory_region *
1166ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
1167 uint32_t receiver_index)
1168{
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001169 struct ffa_memory_access *receiver =
1170 ffa_memory_region_get_receiver(memory_region, receiver_index);
1171 uint32_t offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001172
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001173 if (receiver == NULL) {
1174 return NULL;
1175 }
1176
1177 offset = receiver->composite_memory_region_offset;
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001178 if (offset == 0) {
1179 return NULL;
1180 }
1181
1182 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
1183 offset);
1184}
1185
1186static inline uint32_t ffa_mem_relinquish_init(
1187 struct ffa_mem_relinquish *relinquish_request,
1188 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
J-Alves19e20cf2023-08-02 12:48:55 +01001189 ffa_id_t sender)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001190{
1191 relinquish_request->handle = handle;
1192 relinquish_request->flags = flags;
1193 relinquish_request->endpoint_count = 1;
1194 relinquish_request->endpoints[0] = sender;
J-Alves19e20cf2023-08-02 12:48:55 +01001195 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001196}
1197
J-Alves126ab502022-09-29 11:37:33 +01001198void ffa_copy_memory_region_constituents(
1199 struct ffa_memory_region_constituent *dest,
1200 const struct ffa_memory_region_constituent *src);
1201
Federico Recanati392be392022-02-08 20:53:03 +01001202/**
1203 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
1204 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
1205 * to the SPMC, in order to allow indirect messaging.
1206 */
1207struct ffa_endpoint_rx_tx_descriptor {
J-Alves19e20cf2023-08-02 12:48:55 +01001208 ffa_id_t endpoint_id;
Federico Recanati392be392022-02-08 20:53:03 +01001209 uint16_t reserved;
1210
1211 /*
1212 * 8-byte aligned offset from the base address of this descriptor to the
1213 * `ffa_composite_memory_region` describing the RX buffer.
1214 */
1215 uint32_t rx_offset;
1216
1217 /*
1218 * 8-byte aligned offset from the base address of this descriptor to the
1219 * `ffa_composite_memory_region` describing the TX buffer.
1220 */
1221 uint32_t tx_offset;
1222
1223 /* Pad to align on 16-byte boundary. */
1224 uint32_t pad;
1225};
1226
1227static inline struct ffa_composite_memory_region *
1228ffa_enpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1229{
1230 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1231 desc->rx_offset);
1232}
1233
1234static inline struct ffa_composite_memory_region *
1235ffa_enpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
1236{
1237 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
1238 desc->tx_offset);
1239}
1240
J-Alves2d8457f2022-10-05 11:06:41 +01001241void ffa_memory_region_init_header(struct ffa_memory_region *memory_region,
J-Alves19e20cf2023-08-02 12:48:55 +01001242 ffa_id_t sender,
J-Alves2d8457f2022-10-05 11:06:41 +01001243 ffa_memory_attributes_t attributes,
1244 ffa_memory_region_flags_t flags,
1245 ffa_memory_handle_t handle, uint32_t tag,
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001246 uint32_t receiver_count,
1247 uint32_t receiver_desc_size);
J-Alvesb44bb7d2022-04-25 15:43:46 +01001248void ffa_memory_access_init_permissions(
J-Alves19e20cf2023-08-02 12:48:55 +01001249 struct ffa_memory_access *receiver, ffa_id_t receiver_id,
J-Alvesb44bb7d2022-04-25 15:43:46 +01001250 enum ffa_data_access data_access,
1251 enum ffa_instruction_access instruction_access,
1252 ffa_memory_receiver_flags_t flags);
J-Alves45085432022-04-22 16:19:20 +01001253uint32_t ffa_memory_region_init_single_receiver(
Andrew Walbranca808b12020-05-15 17:22:28 +01001254 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001255 ffa_id_t sender, ffa_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001256 const struct ffa_memory_region_constituent constituents[],
1257 uint32_t constituent_count, uint32_t tag,
1258 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1259 enum ffa_instruction_access instruction_access,
1260 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +01001261 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1262 uint32_t *total_length);
J-Alvesf4eecf72022-07-20 16:05:34 +01001263uint32_t ffa_memory_region_init(
1264 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
J-Alves19e20cf2023-08-02 12:48:55 +01001265 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001266 uint32_t receiver_count, uint32_t receiver_desc_size,
J-Alvesf4eecf72022-07-20 16:05:34 +01001267 const struct ffa_memory_region_constituent constituents[],
1268 uint32_t constituent_count, uint32_t tag,
1269 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1270 enum ffa_memory_cacheability cacheability,
1271 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
1272 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001273uint32_t ffa_memory_retrieve_request_init(
1274 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001275 ffa_id_t sender, struct ffa_memory_access receivers[],
Daniel Boulbyd5ae44b2023-12-12 12:18:11 +00001276 uint32_t receiver_count, uint32_t receiver_desc_size, uint32_t tag,
1277 ffa_memory_region_flags_t flags, enum ffa_memory_type type,
1278 enum ffa_memory_cacheability cacheability,
J-Alves9b24ed82022-08-04 13:12:45 +01001279 enum ffa_memory_shareability shareability);
1280uint32_t ffa_memory_retrieve_request_init_single_receiver(
1281 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001282 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001283 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
1284 enum ffa_instruction_access instruction_access,
1285 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
1286 enum ffa_memory_shareability shareability);
1287uint32_t ffa_memory_lender_retrieve_request_init(
1288 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +01001289 ffa_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +01001290uint32_t ffa_memory_fragment_init(
1291 struct ffa_memory_region_constituent *fragment,
1292 size_t fragment_max_size,
1293 const struct ffa_memory_region_constituent constituents[],
1294 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +01001295void ffa_endpoint_rx_tx_descriptor_init(
J-Alves19e20cf2023-08-02 12:48:55 +01001296 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_id_t endpoint_id,
Federico Recanati392be392022-02-08 20:53:03 +01001297 uint64_t rx_address, uint64_t tx_address);