blob: 539b4892a4bc685e76eb8042a0fcf97d3ed19d51 [file] [log] [blame]
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001/*
J-Alves13318e32021-02-22 17:21:00 +00002 * Copyright 2021 The Hafnium Authors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01003 *
Andrew Walbrane959ec12020-06-17 15:01:09 +01004 * Use of this source code is governed by a BSD-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/BSD-3-Clause.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01007 */
8
9#pragma once
10
11#include "hf/types.h"
12
Olivier Deprez62d99e32020-01-09 15:58:07 +010013#define FFA_VERSION_MAJOR 0x1
Olivier Deprez62d99e32020-01-09 15:58:07 +010014#define FFA_VERSION_MAJOR_OFFSET 16
Daniel Boulby6e32c612021-02-17 15:09:41 +000015#define FFA_VERSION_MAJOR_MASK 0x7FFF
J-Alves3829fc02021-03-18 12:49:18 +000016#define FFA_VERSION_MINOR 0x1
Daniel Boulby6e32c612021-02-17 15:09:41 +000017#define FFA_VERSION_MINOR_OFFSET 0
18#define FFA_VERSION_MINOR_MASK 0xFFFF
19
20#define MAKE_FFA_VERSION(major, minor) \
21 ((((major)&FFA_VERSION_MAJOR_MASK) << FFA_VERSION_MAJOR_OFFSET) | \
22 (((minor)&FFA_VERSION_MINOR_MASK) << FFA_VERSION_MINOR_OFFSET))
23#define FFA_VERSION_COMPILED \
24 MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR)
Olivier Deprez62d99e32020-01-09 15:58:07 +010025
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010026/* clang-format off */
27
28#define FFA_LOW_32_ID 0x84000060
29#define FFA_HIGH_32_ID 0x8400007F
30#define FFA_LOW_64_ID 0xC4000060
Fuad Tabbada72d142020-07-30 09:17:05 +010031#define FFA_HIGH_64_ID 0xC400007F
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010032
33/* FF-A function identifiers. */
J-Alves3829fc02021-03-18 12:49:18 +000034#define FFA_ERROR_32 0x84000060
35#define FFA_SUCCESS_32 0x84000061
36#define FFA_SUCCESS_64 0xC4000061
37#define FFA_INTERRUPT_32 0x84000062
38#define FFA_VERSION_32 0x84000063
39#define FFA_FEATURES_32 0x84000064
40#define FFA_RX_RELEASE_32 0x84000065
41#define FFA_RXTX_MAP_32 0x84000066
42#define FFA_RXTX_MAP_64 0xC4000066
43#define FFA_RXTX_UNMAP_32 0x84000067
44#define FFA_PARTITION_INFO_GET_32 0x84000068
45#define FFA_ID_GET_32 0x84000069
46#define FFA_MSG_POLL_32 0x8400006A /* Legacy FF-A v1.0 */
47#define FFA_MSG_WAIT_32 0x8400006B
48#define FFA_YIELD_32 0x8400006C
49#define FFA_RUN_32 0x8400006D
50#define FFA_MSG_SEND_32 0x8400006E /* Legacy FF-A v1.0 */
51#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
52#define FFA_MSG_SEND_DIRECT_REQ_64 0xC400006F
53#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
54#define FFA_MSG_SEND_DIRECT_RESP_64 0xC4000070
55#define FFA_MEM_DONATE_32 0x84000071
56#define FFA_MEM_LEND_32 0x84000072
57#define FFA_MEM_SHARE_32 0x84000073
58#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
59#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
60#define FFA_MEM_RELINQUISH_32 0x84000076
61#define FFA_MEM_RECLAIM_32 0x84000077
62#define FFA_MEM_FRAG_RX_32 0x8400007A
63#define FFA_MEM_FRAG_TX_32 0x8400007B
64#define FFA_NORMAL_WORLD_RESUME 0x8400007C
65
66/* FF-A v1.1 */
67#define FFA_NOTIFICATION_BITMAP_CREATE_32 0x8400007D
68#define FFA_NOTIFICATION_BITMAP_DESTROY_32 0x8400007E
69#define FFA_NOTIFICATION_BIND_32 0x8400007F
70#define FFA_NOTIFICATION_UNBIND_32 0x84000080
71#define FFA_NOTIFICATION_SET_32 0x84000081
72#define FFA_NOTIFICATION_GET_32 0x84000082
73#define FFA_NOTIFICATION_INFO_GET_64 0xC4000083
74#define FFA_RX_ACQUIRE_32 0x84000084
75#define FFA_SPM_ID_GET_32 0x84000085
76#define FFA_MSG_SEND2_32 0x84000086
77#define FFA_SECONDARY_EP_REGISTER_64 0xC4000087
78#define FFA_MEM_PERM_GET_32 0x84000088
79#define FFA_MEM_PERM_SET_32 0x84000089
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -070080#define FFA_MEM_PERM_GET_64 0xC4000088
81#define FFA_MEM_PERM_SET_64 0xC4000089
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010082
Maksims Svecovs71b76702022-05-20 15:32:58 +010083/* Implementation-defined ABIs. */
84#define FFA_CONSOLE_LOG_32 0x8400008A
85#define FFA_CONSOLE_LOG_64 0xC400008A
86
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010087/* FF-A error codes. */
88#define FFA_NOT_SUPPORTED INT32_C(-1)
89#define FFA_INVALID_PARAMETERS INT32_C(-2)
90#define FFA_NO_MEMORY INT32_C(-3)
91#define FFA_BUSY INT32_C(-4)
92#define FFA_INTERRUPTED INT32_C(-5)
93#define FFA_DENIED INT32_C(-6)
94#define FFA_RETRY INT32_C(-7)
95#define FFA_ABORTED INT32_C(-8)
J-Alvesc8e8a222021-06-08 17:33:52 +010096#define FFA_NO_DATA INT32_C(-9)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010097
98/* clang-format on */
99
J-Alves6f72ca82021-11-01 12:34:58 +0000100/**
101 * FF-A Feature ID, to be used with interface FFA_FEATURES.
102 * As defined in the FF-A v1.1 Beta specification, table 13.10, in section
103 * 13.2.
104 */
105
Olivier Deprezb76307d2022-06-09 17:17:45 +0200106#define FFA_FEATURES_FUNC_ID_MASK (UINT32_C(1) << 31)
J-Alves6f72ca82021-11-01 12:34:58 +0000107#define FFA_FEATURES_FEATURE_ID_MASK UINT32_C(0x7F)
108
109/* Query interrupt ID of Notification Pending Interrupt. */
110#define FFA_FEATURE_NPI 0x1U
111
112/* Query interrupt ID of Schedule Receiver Interrupt. */
113#define FFA_FEATURE_SRI 0x2U
114
115/* Query interrupt ID of the Managed Exit Interrupt. */
116#define FFA_FEATURE_MEI 0x3U
117
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100118/* FF-A function specific constants. */
119#define FFA_MSG_RECV_BLOCK 0x1
120#define FFA_MSG_RECV_BLOCK_MASK 0x1
121
122#define FFA_MSG_SEND_NOTIFY 0x1
123#define FFA_MSG_SEND_NOTIFY_MASK 0x1
124
125#define FFA_MEM_RECLAIM_CLEAR 0x1
126
127#define FFA_SLEEP_INDEFINITE 0
128
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700129#define FFA_MEM_PERM_RO UINT32_C(0x7)
130#define FFA_MEM_PERM_RW UINT32_C(0x5)
131#define FFA_MEM_PERM_RX UINT32_C(0x3)
132
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000133/*
134 * Defined in Table 13.31 in the FF-A v1.1 BETA0 specification.
135 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
136 * if partition info descriptors should be returned or just the count.
137 */
138#define FFA_PARTITION_COUNT_FLAG 0x1
139#define FFA_PARTITION_COUNT_FLAG_MASK 0x1
140
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100141/**
142 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
143 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
144 * configured to use.
145 */
146#define FFA_PAGE_SIZE 4096
147
148/* The maximum length possible for a single message. */
149#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
150
151enum ffa_data_access {
152 FFA_DATA_ACCESS_NOT_SPECIFIED,
153 FFA_DATA_ACCESS_RO,
154 FFA_DATA_ACCESS_RW,
155 FFA_DATA_ACCESS_RESERVED,
156};
157
158enum ffa_instruction_access {
159 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
160 FFA_INSTRUCTION_ACCESS_NX,
161 FFA_INSTRUCTION_ACCESS_X,
162 FFA_INSTRUCTION_ACCESS_RESERVED,
163};
164
165enum ffa_memory_type {
166 FFA_MEMORY_NOT_SPECIFIED_MEM,
167 FFA_MEMORY_DEVICE_MEM,
168 FFA_MEMORY_NORMAL_MEM,
169};
170
171enum ffa_memory_cacheability {
172 FFA_MEMORY_CACHE_RESERVED = 0x0,
173 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
174 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
175 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
176 FFA_MEMORY_DEV_NGNRNE = 0x0,
177 FFA_MEMORY_DEV_NGNRE = 0x1,
178 FFA_MEMORY_DEV_NGRE = 0x2,
179 FFA_MEMORY_DEV_GRE = 0x3,
180};
181
182enum ffa_memory_shareability {
183 FFA_MEMORY_SHARE_NON_SHAREABLE,
184 FFA_MEMORY_SHARE_RESERVED,
185 FFA_MEMORY_OUTER_SHAREABLE,
186 FFA_MEMORY_INNER_SHAREABLE,
187};
188
189typedef uint8_t ffa_memory_access_permissions_t;
190
191/**
192 * This corresponds to table 44 of the FF-A 1.0 EAC specification, "Memory
193 * region attributes descriptor".
194 */
195typedef uint8_t ffa_memory_attributes_t;
196
197#define FFA_DATA_ACCESS_OFFSET (0x0U)
198#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
199
200#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
201#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
202
203#define FFA_MEMORY_TYPE_OFFSET (0x4U)
204#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
205
206#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
207#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
208
209#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
210#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
211
212#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
213 static inline void ffa_set_##name##_attr(container_type *attr, \
214 const enum ffa_##name perm) \
215 { \
216 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
217 }
218
219#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
220 static inline enum ffa_##name ffa_get_##name##_attr( \
221 container_type attr) \
222 { \
223 return (enum ffa_##name)((attr & mask) >> offset); \
224 }
225
226ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
227 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
228ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
229 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
230
231ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
232 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
233ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
234 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
235
236ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
237 FFA_MEMORY_TYPE_MASK)
238ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
239 FFA_MEMORY_TYPE_MASK)
240
241ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
242 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
243ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
244 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
245
246ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
247 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
248ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
249 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
250
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100251/**
252 * A globally-unique ID assigned by the hypervisor for a region of memory being
253 * sent between VMs.
254 */
255typedef uint64_t ffa_memory_handle_t;
256
J-Alves917d2f22020-10-30 18:39:30 +0000257#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
258 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
259#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
260 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
261
262#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
263#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
264
265/** The ID of a VM. These are assigned sequentially starting with an offset. */
266typedef uint16_t ffa_vm_id_t;
267
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100268/**
269 * A count of VMs. This has the same range as the VM IDs but we give it a
270 * different name to make the different semantics clear.
271 */
272typedef ffa_vm_id_t ffa_vm_count_t;
273
274/** The index of a vCPU within a particular VM. */
275typedef uint16_t ffa_vcpu_index_t;
276
277/**
278 * A count of vCPUs. This has the same range as the vCPU indices but we give it
279 * a different name to make the different semantics clear.
280 */
281typedef ffa_vcpu_index_t ffa_vcpu_count_t;
282
283/** Parameter and return type of FF-A functions. */
284struct ffa_value {
285 uint64_t func;
286 uint64_t arg1;
287 uint64_t arg2;
288 uint64_t arg3;
289 uint64_t arg4;
290 uint64_t arg5;
291 uint64_t arg6;
292 uint64_t arg7;
293};
294
Olivier Depreze562e542020-06-11 17:31:54 +0200295static inline uint32_t ffa_func_id(struct ffa_value args)
296{
297 return args.func;
298}
299
J-Alves13318e32021-02-22 17:21:00 +0000300static inline int32_t ffa_error_code(struct ffa_value val)
301{
302 return (int32_t)val.arg2;
303}
304
J-Alvesd6f4e142021-03-05 13:33:59 +0000305static inline ffa_vm_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100306{
307 return (args.arg1 >> 16) & 0xffff;
308}
309
J-Alvesd6f4e142021-03-05 13:33:59 +0000310static inline ffa_vm_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100311{
312 return args.arg1 & 0xffff;
313}
314
315static inline uint32_t ffa_msg_send_size(struct ffa_value args)
316{
317 return args.arg3;
318}
319
320static inline uint32_t ffa_msg_send_attributes(struct ffa_value args)
321{
322 return args.arg4;
323}
324
Olivier Depreze562e542020-06-11 17:31:54 +0200325static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
326{
327 return args.arg2;
328}
329
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100330static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
331{
332 return (uint64_t)a1 | (uint64_t)a2 << 32;
333}
334
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100335static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
336{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100337 return ffa_assemble_handle(args.arg2, args.arg3);
338}
339
Andrew Walbranca808b12020-05-15 17:22:28 +0100340static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
341{
342 return ffa_assemble_handle(args.arg1, args.arg2);
343}
344
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100345static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
346{
347 return (struct ffa_value){.func = FFA_SUCCESS_32,
348 .arg2 = (uint32_t)handle,
349 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100350}
351
352static inline ffa_vm_id_t ffa_vm_id(struct ffa_value args)
353{
354 return (args.arg1 >> 16) & 0xffff;
355}
356
357static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
358{
359 return args.arg1 & 0xffff;
360}
361
362static inline uint64_t ffa_vm_vcpu(ffa_vm_id_t vm_id,
363 ffa_vcpu_index_t vcpu_index)
364{
365 return ((uint32_t)vm_id << 16) | vcpu_index;
366}
367
Andrew Walbranca808b12020-05-15 17:22:28 +0100368static inline ffa_vm_id_t ffa_frag_sender(struct ffa_value args)
369{
370 return (args.arg4 >> 16) & 0xffff;
371}
372
J-Alves6f72ca82021-11-01 12:34:58 +0000373static inline uint32_t ffa_feature_intid(struct ffa_value args)
374{
375 return (uint32_t)args.arg2;
376}
377
Daniel Boulbyefa381f2022-01-18 14:49:40 +0000378static inline uint32_t ffa_fwk_msg(struct ffa_value args)
379{
380 return (uint32_t)args.arg2;
381}
382
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100383/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100384 * Holds the UUID in a struct that is mappable directly to the SMCC calling
385 * convention, which is used for FF-A calls.
386 *
387 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
388 * of the SMCC Spec 1.2.
389 */
390struct ffa_uuid {
391 uint32_t uuid[4];
392};
393
394static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
395 uint32_t w3, struct ffa_uuid *uuid)
396{
397 uuid->uuid[0] = w0;
398 uuid->uuid[1] = w1;
399 uuid->uuid[2] = w2;
400 uuid->uuid[3] = w3;
401}
402
403static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
404 const struct ffa_uuid *uuid2)
405{
406 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
407 (uuid1->uuid[1] == uuid2->uuid[1]) &&
408 (uuid1->uuid[2] == uuid2->uuid[2]) &&
409 (uuid1->uuid[3] == uuid2->uuid[3]);
410}
411
412static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
413{
414 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
415 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
416}
417
418/**
419 * Flags to determine the partition properties, as required by
420 * FFA_PARTITION_INFO_GET.
421 *
422 * The values of the flags are specified in table 82 of the FF-A 1.0 EAC
423 * specification, "Partition information descriptor, partition properties".
424 */
425typedef uint32_t ffa_partition_properties_t;
426
427/** Partition property: partition supports receipt of direct requests. */
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100428#define FFA_PARTITION_DIRECT_REQ_RECV 0x1
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100429
430/** Partition property: partition can send direct requests. */
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100431#define FFA_PARTITION_DIRECT_REQ_SEND 0x2
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100432
433/** Partition property: partition can send and receive indirect messages. */
434#define FFA_PARTITION_INDIRECT_MSG 0x4
435
J-Alves09ff9d82021-11-02 11:55:20 +0000436/** Partition property: partition can receive notifications. */
437#define FFA_PARTITION_NOTIFICATION 0x8
438
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100439/**
440 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
441 * interface.
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000442 * This corresponds to table 13.34 of the FF-A 1.1 BETA0 EAC specification,
443 * "Partition information descriptor".
444 */
445struct ffa_partition_info {
446 ffa_vm_id_t vm_id;
447 ffa_vcpu_count_t vcpu_count;
448 ffa_partition_properties_t properties;
449 struct ffa_uuid uuid;
450};
451
452/**
453 * Create a struct for the "Partition information descriptor" defined for v1.0
454 * which can be returned to v1.0 endpoints.
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100455 * This corresponds to table 82 of the FF-A 1.0 EAC specification, "Partition
456 * information descriptor".
457 */
Daniel Boulby1ddb3d72021-12-16 18:16:50 +0000458
459struct ffa_partition_info_v1_0 {
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100460 ffa_vm_id_t vm_id;
461 ffa_vcpu_count_t vcpu_count;
462 ffa_partition_properties_t properties;
463};
464
J-Alvesdd1ad572022-01-25 17:58:26 +0000465/** Length in bytes of the name in boot information descriptor. */
466#define FFA_BOOT_INFO_NAME_LEN 16
467
J-Alves240d84c2022-04-22 12:19:34 +0100468/**
469 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
470 * the FF-A v1.1 EAC0 specification.
471 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000472struct ffa_boot_info_desc {
473 char name[FFA_BOOT_INFO_NAME_LEN];
474 uint8_t type;
475 uint8_t reserved;
476 uint16_t flags;
477 uint32_t size;
478 uint64_t content;
479};
480
481/** FF-A boot information type mask. */
482#define FFA_BOOT_INFO_TYPE_SHIFT 7
483#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
484#define FFA_BOOT_INFO_TYPE_STD 0U
485#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
486
487/** Standard boot info type IDs. */
488#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
489#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
490#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
491
492/** FF-A Boot Info descriptors flags. */
493#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
494
495/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
496#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
497#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
498 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
499#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
500#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
501
502/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
503#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
504#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
505 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
506#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
507#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
508
509static inline uint16_t ffa_boot_info_content_format(
510 struct ffa_boot_info_desc *desc)
511{
512 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
513 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
514}
515
516static inline uint16_t ffa_boot_info_name_format(
517 struct ffa_boot_info_desc *desc)
518{
519 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
520 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
521}
522
523static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
524{
525 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
526}
527
528static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
529{
530 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
531 FFA_BOOT_INFO_TYPE_SHIFT;
532}
533
534/** Length in bytes of the signature in the boot descriptor. */
535#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
536
J-Alves240d84c2022-04-22 12:19:34 +0100537/**
538 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
539 * of the FF-A v1.1 EAC0 specification.
540 */
J-Alvesdd1ad572022-01-25 17:58:26 +0000541struct ffa_boot_info_header {
542 uint32_t signature;
543 uint32_t version;
544 uint32_t info_blob_size;
545 uint32_t desc_size;
546 uint32_t desc_count;
547 uint32_t desc_offset;
548 uint64_t reserved;
549 struct ffa_boot_info_desc boot_info[];
550};
551
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100552/**
J-Alves980d1992021-03-18 12:49:18 +0000553 * FF-A v1.1 specification restricts the number of notifications to a maximum
554 * of 64. Following all possible bitmaps.
555 */
556#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
557
558typedef uint64_t ffa_notifications_bitmap_t;
559
560#define MAX_FFA_NOTIFICATIONS 64U
561
562/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000563 * Flag for notification bind and set, to specify call is about per-vCPU
564 * notifications.
565 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200566#define FFA_NOTIFICATION_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesc003a7a2021-03-18 13:06:53 +0000567
Federico Recanatie73d2832022-04-20 11:10:52 +0200568#define FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(0)
569#define FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK FFA_NOTIFICATION_MASK(32)
570
571/**
572 * Helper functions to check for buffer full notification.
573 */
574static inline bool is_ffa_hyp_buffer_full_notification(
575 ffa_notifications_bitmap_t framework)
576{
577 return (framework & FFA_NOTIFICATION_HYP_BUFFER_FULL_MASK) != 0;
578}
579
580static inline bool is_ffa_spm_buffer_full_notification(
581 ffa_notifications_bitmap_t framework)
582{
583 return (framework & FFA_NOTIFICATION_SPM_BUFFER_FULL_MASK) != 0;
584}
585
J-Alvesc003a7a2021-03-18 13:06:53 +0000586/**
J-Alves980d1992021-03-18 12:49:18 +0000587 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
588 * and hi.
589 * Helpful as FF-A specification defines that the notifications interfaces
590 * arguments are 32-bit registers.
591 */
592static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
593 uint32_t hi)
594{
595 return (ffa_notifications_bitmap_t)hi << 32U | lo;
596}
597
J-Alves98ff9562021-09-09 14:39:41 +0100598static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
599 struct ffa_value val)
600{
601 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
602}
603
604static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
605 struct ffa_value val)
606{
607 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
608}
609
Federico Recanatie73d2832022-04-20 11:10:52 +0200610static inline ffa_notifications_bitmap_t ffa_notification_get_from_framework(
611 struct ffa_value val)
612{
613 return ffa_notifications_bitmap((uint32_t)val.arg6, (uint32_t)val.arg7);
614}
615
J-Alves980d1992021-03-18 12:49:18 +0000616/**
J-Alvesaa79c012021-07-09 14:29:45 +0100617 * Flags used in calls to FFA_NOTIFICATION_GET interface.
618 */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200619#define FFA_NOTIFICATION_FLAG_BITMAP_SP (UINT32_C(1) << 0)
620#define FFA_NOTIFICATION_FLAG_BITMAP_VM (UINT32_C(1) << 1)
621#define FFA_NOTIFICATION_FLAG_BITMAP_SPM (UINT32_C(1) << 2)
622#define FFA_NOTIFICATION_FLAG_BITMAP_HYP (UINT32_C(1) << 3)
J-Alvesaa79c012021-07-09 14:29:45 +0100623
J-Alvesa3755072021-11-26 16:01:08 +0000624/* Flag to configure notification as being per vCPU. */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200625#define FFA_NOTIFICATIONS_FLAG_PER_VCPU (UINT32_C(1) << 0)
J-Alvesa3755072021-11-26 16:01:08 +0000626
J-Alves13394022021-06-30 13:48:49 +0100627/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
Olivier Deprezb76307d2022-06-09 17:17:45 +0200628#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI (UINT32_C(1) << 1)
629
630#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) \
631 ((((uint32_t)(id)) & UINT32_C(0xffff)) << 16)
J-Alves13394022021-06-30 13:48:49 +0100632
J-Alvesbe6e3032021-11-30 14:54:12 +0000633static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100634{
J-Alvesbe6e3032021-11-30 14:54:12 +0000635 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100636}
637
638/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100639 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
640 */
641#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
642
643/**
644 * Number of registers to use in successfull return of interface
645 * FFA_NOTIFICATION_INFO_GET.
646 */
647#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
648
649#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
650
651/**
652 * Helper macros for return parameter encoding as described in section 17.7.1
653 * of the FF-A v1.1 Beta0 specification.
654 */
655#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
656#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
657#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
658#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
659
660static inline uint32_t ffa_notification_info_get_lists_count(
661 struct ffa_value args)
662{
663 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
664 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
665}
666
667static inline uint32_t ffa_notification_info_get_list_size(
668 struct ffa_value args, unsigned int list_idx)
669{
670 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
671 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
672}
673
674static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
675{
676 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
677}
678
679/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100680 * A set of contiguous pages which is part of a memory region. This corresponds
681 * to table 40 of the FF-A 1.0 EAC specification, "Constituent memory region
682 * descriptor".
683 */
684struct ffa_memory_region_constituent {
685 /**
686 * The base IPA of the constituent memory region, aligned to 4 kiB page
687 * size granularity.
688 */
689 uint64_t address;
690 /** The number of 4 kiB pages in the constituent memory region. */
691 uint32_t page_count;
692 /** Reserved field, must be 0. */
693 uint32_t reserved;
694};
695
696/**
697 * A set of pages comprising a memory region. This corresponds to table 39 of
698 * the FF-A 1.0 EAC specification, "Composite memory region descriptor".
699 */
700struct ffa_composite_memory_region {
701 /**
702 * The total number of 4 kiB pages included in this memory region. This
703 * must be equal to the sum of page counts specified in each
704 * `ffa_memory_region_constituent`.
705 */
706 uint32_t page_count;
707 /**
708 * The number of constituents (`ffa_memory_region_constituent`)
709 * included in this memory region range.
710 */
711 uint32_t constituent_count;
712 /** Reserved field, must be 0. */
713 uint64_t reserved_0;
714 /** An array of `constituent_count` memory region constituents. */
715 struct ffa_memory_region_constituent constituents[];
716};
717
718/** Flags to indicate properties of receivers during memory region retrieval. */
719typedef uint8_t ffa_memory_receiver_flags_t;
720
721/**
722 * This corresponds to table 41 of the FF-A 1.0 EAC specification, "Memory
723 * access permissions descriptor".
724 */
725struct ffa_memory_region_attributes {
726 /** The ID of the VM to which the memory is being given or shared. */
727 ffa_vm_id_t receiver;
728 /**
729 * The permissions with which the memory region should be mapped in the
730 * receiver's page table.
731 */
732 ffa_memory_access_permissions_t permissions;
733 /**
734 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
735 * for memory regions with multiple borrowers.
736 */
737 ffa_memory_receiver_flags_t flags;
738};
739
740/** Flags to control the behaviour of a memory sharing transaction. */
741typedef uint32_t ffa_memory_region_flags_t;
742
743/**
744 * Clear memory region contents after unmapping it from the sender and before
745 * mapping it for any receiver.
746 */
747#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
748
749/**
750 * Whether the hypervisor may time slice the memory sharing or retrieval
751 * operation.
752 */
753#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
754
755/**
756 * Whether the hypervisor should clear the memory region after the receiver
757 * relinquishes it or is aborted.
758 */
759#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
760
761#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
762#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
763#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
764#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
765#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
766
Federico Recanati85090c42021-12-15 13:17:54 +0100767#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
768#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
769
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100770/**
771 * This corresponds to table 42 of the FF-A 1.0 EAC specification, "Endpoint
772 * memory access descriptor".
773 */
774struct ffa_memory_access {
775 struct ffa_memory_region_attributes receiver_permissions;
776 /**
777 * Offset in bytes from the start of the outer `ffa_memory_region` to
778 * an `ffa_composite_memory_region` struct.
779 */
780 uint32_t composite_memory_region_offset;
781 uint64_t reserved_0;
782};
783
784/**
785 * Information about a set of pages which are being shared. This corresponds to
786 * table 45 of the FF-A 1.0 EAC specification, "Lend, donate or share memory
787 * transaction descriptor". Note that it is also used for retrieve requests and
788 * responses.
789 */
790struct ffa_memory_region {
791 /**
792 * The ID of the VM which originally sent the memory region, i.e. the
793 * owner.
794 */
795 ffa_vm_id_t sender;
796 ffa_memory_attributes_t attributes;
797 /** Reserved field, must be 0. */
798 uint8_t reserved_0;
799 /** Flags to control behaviour of the transaction. */
800 ffa_memory_region_flags_t flags;
801 ffa_memory_handle_t handle;
802 /**
803 * An implementation defined value associated with the receiver and the
804 * memory region.
805 */
806 uint64_t tag;
807 /** Reserved field, must be 0. */
808 uint32_t reserved_1;
809 /**
810 * The number of `ffa_memory_access` entries included in this
811 * transaction.
812 */
813 uint32_t receiver_count;
814 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +0100815 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100816 * Each one specifies a memory region offset, an endpoint and the
817 * attributes with which this memory region should be mapped in that
818 * endpoint's page table.
819 */
820 struct ffa_memory_access receivers[];
821};
822
823/**
824 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
825 * 150 of the FF-A 1.0 EAC specification, "Descriptor to relinquish a memory
826 * region".
827 */
828struct ffa_mem_relinquish {
829 ffa_memory_handle_t handle;
830 ffa_memory_region_flags_t flags;
831 uint32_t endpoint_count;
832 ffa_vm_id_t endpoints[];
833};
834
835/**
836 * Gets the `ffa_composite_memory_region` for the given receiver from an
837 * `ffa_memory_region`, or NULL if it is not valid.
838 */
839static inline struct ffa_composite_memory_region *
840ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
841 uint32_t receiver_index)
842{
843 uint32_t offset = memory_region->receivers[receiver_index]
844 .composite_memory_region_offset;
845
846 if (offset == 0) {
847 return NULL;
848 }
849
850 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
851 offset);
852}
853
854static inline uint32_t ffa_mem_relinquish_init(
855 struct ffa_mem_relinquish *relinquish_request,
856 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
857 ffa_vm_id_t sender)
858{
859 relinquish_request->handle = handle;
860 relinquish_request->flags = flags;
861 relinquish_request->endpoint_count = 1;
862 relinquish_request->endpoints[0] = sender;
863 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t);
864}
865
Federico Recanati392be392022-02-08 20:53:03 +0100866/**
867 * Endpoint RX/TX descriptor, as defined by Table 13.27 in FF-A v1.1 EAC0.
868 * It's used by the Hypervisor to describe the RX/TX buffers mapped by a VM
869 * to the SPMC, in order to allow indirect messaging.
870 */
871struct ffa_endpoint_rx_tx_descriptor {
872 ffa_vm_id_t endpoint_id;
873 uint16_t reserved;
874
875 /*
876 * 8-byte aligned offset from the base address of this descriptor to the
877 * `ffa_composite_memory_region` describing the RX buffer.
878 */
879 uint32_t rx_offset;
880
881 /*
882 * 8-byte aligned offset from the base address of this descriptor to the
883 * `ffa_composite_memory_region` describing the TX buffer.
884 */
885 uint32_t tx_offset;
886
887 /* Pad to align on 16-byte boundary. */
888 uint32_t pad;
889};
890
891static inline struct ffa_composite_memory_region *
892ffa_enpoint_get_rx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
893{
894 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
895 desc->rx_offset);
896}
897
898static inline struct ffa_composite_memory_region *
899ffa_enpoint_get_tx_memory_region(struct ffa_endpoint_rx_tx_descriptor *desc)
900{
901 return (struct ffa_composite_memory_region *)((uintptr_t)desc +
902 desc->tx_offset);
903}
904
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100905uint32_t ffa_memory_region_init(
Andrew Walbranca808b12020-05-15 17:22:28 +0100906 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
907 ffa_vm_id_t sender, ffa_vm_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100908 const struct ffa_memory_region_constituent constituents[],
909 uint32_t constituent_count, uint32_t tag,
910 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
911 enum ffa_instruction_access instruction_access,
912 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +0100913 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
914 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100915uint32_t ffa_memory_retrieve_request_init(
916 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
917 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
918 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
919 enum ffa_instruction_access instruction_access,
920 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
921 enum ffa_memory_shareability shareability);
922uint32_t ffa_memory_lender_retrieve_request_init(
923 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
924 ffa_vm_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +0100925bool ffa_retrieved_memory_region_init(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100926 struct ffa_memory_region *response, size_t response_max_size,
927 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
928 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
929 ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100930 uint32_t page_count, uint32_t total_constituent_count,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100931 const struct ffa_memory_region_constituent constituents[],
Andrew Walbranca808b12020-05-15 17:22:28 +0100932 uint32_t fragment_constituent_count, uint32_t *total_length,
933 uint32_t *fragment_length);
934uint32_t ffa_memory_fragment_init(
935 struct ffa_memory_region_constituent *fragment,
936 size_t fragment_max_size,
937 const struct ffa_memory_region_constituent constituents[],
938 uint32_t constituent_count, uint32_t *fragment_length);
Federico Recanati392be392022-02-08 20:53:03 +0100939void ffa_endpoint_rx_tx_descriptor_init(
940 struct ffa_endpoint_rx_tx_descriptor *desc, ffa_vm_id_t endpoint_id,
941 uint64_t rx_address, uint64_t tx_address);