blob: 90bb30f60db755bc3cfb78c3e9e1e307f61eabb6 [file] [log] [blame]
J-Alves7581c382020-05-07 18:34:20 +01001/*
Karl Meakin92aa7702023-10-11 18:48:01 +01002 * Copyright (c) 2018-2023, Arm Limited. All rights reserved.
J-Alves7581c382020-05-07 18:34:20 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef FFA_HELPERS_H
8#define FFA_HELPERS_H
9
J-Alves8f08a052020-05-26 17:14:40 +010010#include <ffa_svc.h>
J-Alves7581c382020-05-07 18:34:20 +010011#include <tftf_lib.h>
12#include <utils_def.h>
13
14/* This error code must be different to the ones used by FFA */
15#define FFA_TFTF_ERROR -42
16
Daniel Boulbye79d2072021-03-03 11:34:53 +000017typedef unsigned short ffa_id_t;
J-Alves5aecd982020-06-11 10:25:33 +010018typedef unsigned short ffa_vm_count_t;
19typedef unsigned short ffa_vcpu_count_t;
J-Alvesf3a393c2020-10-23 16:00:39 +010020typedef uint64_t ffa_memory_handle_t;
21/** Flags to indicate properties of receivers during memory region retrieval. */
22typedef uint8_t ffa_memory_receiver_flags_t;
J-Alves5aecd982020-06-11 10:25:33 +010023
J-Alvesd708c032020-11-19 12:14:21 +000024struct ffa_uuid {
Raghu Krishnamurthyab5321a2023-04-23 16:14:28 -070025 uint32_t uuid[4];
J-Alvesd708c032020-11-19 12:14:21 +000026};
27
J-Alvesda3e6d42022-01-25 10:19:56 +000028/** Length in bytes of the name in boot information descriptor. */
29#define FFA_BOOT_INFO_NAME_LEN 16
30
31/**
32 * The FF-A boot info descriptor, as defined in table 5.8 of section 5.4.1, of
33 * the FF-A v1.1 EAC0 specification.
34 */
35struct ffa_boot_info_desc {
36 char name[FFA_BOOT_INFO_NAME_LEN];
37 uint8_t type;
38 uint8_t reserved;
39 uint16_t flags;
40 uint32_t size;
41 uint64_t content;
42};
43
44/** FF-A boot information type mask. */
45#define FFA_BOOT_INFO_TYPE_SHIFT 7
46#define FFA_BOOT_INFO_TYPE_MASK (0x1U << FFA_BOOT_INFO_TYPE_SHIFT)
47#define FFA_BOOT_INFO_TYPE_STD 0U
48#define FFA_BOOT_INFO_TYPE_IMPDEF 1U
49
50/** Standard boot info type IDs. */
51#define FFA_BOOT_INFO_TYPE_ID_MASK 0x7FU
52#define FFA_BOOT_INFO_TYPE_ID_FDT 0U
53#define FFA_BOOT_INFO_TYPE_ID_HOB 1U
54
55/** FF-A Boot Info descriptors flags. */
56#define FFA_BOOT_INFO_FLAG_MBZ_MASK 0xFFF0U
57
58/** Bits [1:0] encode the format of the name field in ffa_boot_info_desc. */
59#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT 0U
60#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK \
61 (0x3U << FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT)
62#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_STRING 0x0U
63#define FFA_BOOT_INFO_FLAG_NAME_FORMAT_UUID 0x1U
64
65/** Bits [3:2] encode the format of the content field in ffa_boot_info_desc. */
66#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT 2
67#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK \
68 (0x3U << FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT)
69#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_VALUE 0x1U
70#define FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_ADDR 0x0U
71
72static inline uint16_t ffa_boot_info_content_format(
73 struct ffa_boot_info_desc *desc)
74{
75 return (desc->flags & FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_MASK) >>
76 FFA_BOOT_INFO_FLAG_CONTENT_FORMAT_SHIFT;
77}
78
79static inline uint16_t ffa_boot_info_name_format(
80 struct ffa_boot_info_desc *desc)
81{
82 return (desc->flags & FFA_BOOT_INFO_FLAG_NAME_FORMAT_MASK) >>
83 FFA_BOOT_INFO_FLAG_NAME_FORMAT_SHIFT;
84}
85
86static inline uint8_t ffa_boot_info_type_id(struct ffa_boot_info_desc *desc)
87{
88 return desc->type & FFA_BOOT_INFO_TYPE_ID_MASK;
89}
90
91static inline uint8_t ffa_boot_info_type(struct ffa_boot_info_desc *desc)
92{
93 return (desc->type & FFA_BOOT_INFO_TYPE_MASK) >>
94 FFA_BOOT_INFO_TYPE_SHIFT;
95}
96
97/** Length in bytes of the signature in the boot descriptor. */
98#define FFA_BOOT_INFO_HEADER_SIGNATURE_LEN 4
99
100/**
101 * The FF-A boot information header, as defined in table 5.9 of section 5.4.2,
102 * of the FF-A v1.1 EAC0 specification.
103 */
104struct ffa_boot_info_header {
105 uint32_t signature;
106 uint32_t version;
107 uint32_t info_blob_size;
108 uint32_t desc_size;
109 uint32_t desc_count;
110 uint32_t desc_offset;
111 uint64_t reserved;
112 struct ffa_boot_info_desc boot_info[];
113};
114
J-Alves7581c382020-05-07 18:34:20 +0100115#ifndef __ASSEMBLY__
116
J-Alves18c28052021-03-09 09:58:53 +0000117#include <cassert.h>
J-Alves7581c382020-05-07 18:34:20 +0100118#include <stdint.h>
119
J-Alves4439ece2021-11-05 11:52:54 +0000120/**
121 * FF-A Feature ID, to be used with interface FFA_FEATURES.
122 * As defined in the FF-A v1.1 Beta specification, table 13.10, in section
123 * 13.2.
124 */
125
126/** Query interrupt ID of Notification Pending Interrupt. */
127#define FFA_FEATURE_NPI 0x1U
128
129/** Query interrupt ID of Schedule Receiver Interrupt. */
130#define FFA_FEATURE_SRI 0x2U
131
132/** Query interrupt ID of the Managed Exit Interrupt. */
133#define FFA_FEATURE_MEI 0x3U
134
Max Shvetsov0b7d25f2021-03-05 13:46:42 +0000135/** Partition property: partition supports receipt of direct requests. */
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500136#define FFA_PARTITION_DIRECT_REQ_RECV (UINT32_C(1) << 0)
Max Shvetsov0b7d25f2021-03-05 13:46:42 +0000137
138/** Partition property: partition can send direct requests. */
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500139#define FFA_PARTITION_DIRECT_REQ_SEND (UINT32_C(1) << 1)
Max Shvetsov0b7d25f2021-03-05 13:46:42 +0000140
141/** Partition property: partition can send and receive indirect messages. */
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500142#define FFA_PARTITION_INDIRECT_MSG (UINT32_C(1) << 2)
Max Shvetsov0b7d25f2021-03-05 13:46:42 +0000143
J-Alves4d05dec2021-11-02 11:52:27 +0000144/** Partition property: partition can receive notifications. */
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500145#define FFA_PARTITION_NOTIFICATION (UINT32_C(1) << 3)
J-Alves4d05dec2021-11-02 11:52:27 +0000146
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500147/** Partition property: partition runs in the AArch64 execution state. */
148#define FFA_PARTITION_AARCH64_EXEC (UINT32_C(1) << 8)
149
150/** Partition info descriptor as defined in FF-A v1.1 EAC0 Table 13.37 */
Max Shvetsovc32f4782020-06-23 09:41:15 +0100151struct ffa_partition_info {
152 /** The ID of the VM the information is about */
Daniel Boulbye79d2072021-03-03 11:34:53 +0000153 ffa_id_t id;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100154 /** The number of execution contexts implemented by the partition */
155 uint16_t exec_context;
156 /** The Partition's properties, e.g. supported messaging methods */
157 uint32_t properties;
Daniel Boulby8aa994c2022-01-05 19:44:30 +0000158 /** The uuid of the partition */
159 struct ffa_uuid uuid;
Max Shvetsovc32f4782020-06-23 09:41:15 +0100160};
161
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000162/**
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500163 * Bits[31:3] of partition properties must be zero for FF-A v1.0.
164 * This corresponds to table 8.25 "Partition information descriptor"
165 * in DEN0077A FF-A 1.0 REL specification.
166 */
167#define FFA_PARTITION_v1_0_RES_MASK (~(UINT32_C(0x7)))
168
169/**
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000170 * Partition info descriptor as defined in Table 8.25 of the v1.0
Kathleen Capella7774d6e2022-11-23 19:06:21 -0500171 * FF-A Specification (DEN0077A).
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000172 */
173struct ffa_partition_info_v1_0 {
174 /** The ID of the VM the information is about */
175 ffa_id_t id;
176 /** The number of execution contexts implemented by the partition */
177 uint16_t exec_context;
178 /** The Partition's properties, e.g. supported messaging methods */
179 uint32_t properties;
180};
181
Daniel Boulbyce386b12022-03-29 18:36:36 +0100182struct ffa_value {
183 u_register_t fid;
184 u_register_t arg1;
185 u_register_t arg2;
186 u_register_t arg3;
187 u_register_t arg4;
188 u_register_t arg5;
189 u_register_t arg6;
190 u_register_t arg7;
Raghu Krishnamurthyab5321a2023-04-23 16:14:28 -0700191 u_register_t arg8;
192 u_register_t arg9;
193 u_register_t arg10;
194 u_register_t arg11;
195 u_register_t arg12;
196 u_register_t arg13;
197 u_register_t arg14;
198 u_register_t arg15;
199 u_register_t arg16;
200 u_register_t arg17;
Daniel Boulbyce386b12022-03-29 18:36:36 +0100201};
202
203/* Function to make an SMC or SVC service call depending on the exception
204 * level of the SP.
205 */
206struct ffa_value ffa_service_call(struct ffa_value *args);
207
208/*
209 * Functions to trigger a service call.
210 *
211 * The arguments to pass through the service call must be stored in the
212 * ffa_value structure. The return values of the service call will be stored
213 * in the same structure (overriding the input arguments).
214 *
215 * Return the first return value. It is equivalent to args.fid but is also
216 * provided as the return value for convenience.
217 */
218u_register_t ffa_svc(struct ffa_value *args);
219u_register_t ffa_smc(struct ffa_value *args);
220
221static inline uint32_t ffa_func_id(struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100222{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100223 return (uint32_t)val.fid;
J-Alves6cb21d92021-01-07 15:18:12 +0000224}
225
Daniel Boulbyce386b12022-03-29 18:36:36 +0100226static inline int32_t ffa_error_code(struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100227{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100228 return (int32_t)val.arg2;
J-Alves6cb21d92021-01-07 15:18:12 +0000229}
230
Daniel Boulbyce386b12022-03-29 18:36:36 +0100231static inline ffa_id_t ffa_endpoint_id(struct ffa_value val) {
232 return (ffa_id_t)val.arg2 & 0xffff;
Daniel Boulby198deda2021-03-03 11:35:25 +0000233}
234
Daniel Boulbyce386b12022-03-29 18:36:36 +0100235static inline uint32_t ffa_partition_info_count(struct ffa_value val)
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000236{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100237 return (uint32_t)val.arg2;
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000238}
239
Daniel Boulby07d751e2022-05-30 17:32:00 +0100240static inline uint32_t ffa_partition_info_desc_size(struct ffa_value val)
241{
242 return (uint32_t)val.arg3;
243}
244
Daniel Boulbyce386b12022-03-29 18:36:36 +0100245static inline uint32_t ffa_feature_intid(struct ffa_value val)
J-Alves4439ece2021-11-05 11:52:54 +0000246{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100247 return (uint32_t)val.arg2;
J-Alves4439ece2021-11-05 11:52:54 +0000248}
249
Raghu Krishnamurthyab5321a2023-04-23 16:14:28 -0700250static inline uint16_t ffa_partition_info_regs_get_last_idx(
251 struct ffa_value args)
252{
253 return args.arg2 & 0xFFFF;
254}
255
256static inline uint16_t ffa_partition_info_regs_get_curr_idx(
257 struct ffa_value args)
258{
259 return (args.arg2 >> 16) & 0xFFFF;
260}
261
262static inline uint16_t ffa_partition_info_regs_get_tag(struct ffa_value args)
263{
264 return (args.arg2 >> 32) & 0xFFFF;
265}
266
267static inline uint16_t ffa_partition_info_regs_get_desc_size(
268 struct ffa_value args)
269{
270 return (args.arg2 >> 48);
271}
272
273static inline uint32_t ffa_partition_info_regs_partition_count(
274 struct ffa_value args)
275{
276 return ffa_partition_info_regs_get_last_idx(args) + 1;
277}
278
279static inline uint32_t ffa_partition_info_regs_entry_count(
280 struct ffa_value args, uint16_t start_idx)
281{
282 return (ffa_partition_info_regs_get_curr_idx(args) - start_idx + 1);
283}
284
285static inline uint16_t ffa_partition_info_regs_entry_size(
286 struct ffa_value args)
287{
288 return (args.arg2 >> 48) & 0xFFFFU;
289}
290
J-Alves18c28052021-03-09 09:58:53 +0000291typedef uint64_t ffa_notification_bitmap_t;
292
293#define FFA_NOTIFICATION(ID) (UINT64_C(1) << ID)
294
295#define MAX_FFA_NOTIFICATIONS UINT32_C(64)
296
297#define FFA_NOTIFICATIONS_FLAG_PER_VCPU UINT32_C(0x1 << 0)
298
J-Alvesb0cb5d02021-07-08 11:19:33 +0100299/** Flag to delay Schedule Receiver Interrupt. */
300#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI UINT32_C(0x1 << 1)
301
J-Alves18c28052021-03-09 09:58:53 +0000302#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) UINT32_C((id & 0xFFFF) << 16)
303
J-Alvesf156ae92021-10-08 12:10:05 +0100304#define FFA_NOTIFICATIONS_FLAG_BITMAP_SP UINT32_C(0x1 << 0)
305#define FFA_NOTIFICATIONS_FLAG_BITMAP_VM UINT32_C(0x1 << 1)
306#define FFA_NOTIFICATIONS_FLAG_BITMAP_SPM UINT32_C(0x1 << 2)
307#define FFA_NOTIFICATIONS_FLAG_BITMAP_HYP UINT32_C(0x1 << 3)
308
J-Alves269118a2021-09-22 09:46:11 +0100309/**
310 * The following is an SGI ID, that the SPMC configures as non-secure, as
311 * suggested by the FF-A v1.1 specification, in section 9.4.1.
312 */
313#define FFA_SCHEDULE_RECEIVER_INTERRUPT_ID 8
314
J-Alvesf156ae92021-10-08 12:10:05 +0100315#define FFA_NOTIFICATIONS_BITMAP(lo, hi) \
316 (ffa_notification_bitmap_t)(lo) | \
317 (((ffa_notification_bitmap_t)hi << 32) & 0xFFFFFFFF00000000ULL)
318
319#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) UINT32_C((id & 0xFFFF) << 16)
320
Daniel Boulbyce386b12022-03-29 18:36:36 +0100321static inline ffa_notification_bitmap_t ffa_notifications_get_from_sp(
322 struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100323{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100324 return FFA_NOTIFICATIONS_BITMAP(val.arg2, val.arg3);
J-Alvesf156ae92021-10-08 12:10:05 +0100325}
326
Daniel Boulbyce386b12022-03-29 18:36:36 +0100327static inline ffa_notification_bitmap_t ffa_notifications_get_from_vm(
328 struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100329{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100330 return FFA_NOTIFICATIONS_BITMAP(val.arg4, val.arg5);
J-Alvesf156ae92021-10-08 12:10:05 +0100331}
332
J-Alves5bce2502021-06-14 14:27:45 +0100333/*
334 * FFA_NOTIFICATION_INFO_GET is a SMC64 interface.
335 * The following macros are defined for SMC64 implementation.
336 */
337#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
338
339#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING UINT64_C(0x1)
340
341#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
342#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1FU
343#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
344#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
345
346static inline uint32_t ffa_notifications_info_get_lists_count(
Daniel Boulbyce386b12022-03-29 18:36:36 +0100347 struct ffa_value ret)
J-Alves5bce2502021-06-14 14:27:45 +0100348{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100349 return (uint32_t)(ret.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT)
J-Alves5bce2502021-06-14 14:27:45 +0100350 & FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
351}
352
353static inline uint32_t ffa_notifications_info_get_list_size(
Daniel Boulbyce386b12022-03-29 18:36:36 +0100354 struct ffa_value ret, uint32_t list)
J-Alves5bce2502021-06-14 14:27:45 +0100355{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100356 return (uint32_t)(ret.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list)) &
J-Alves5bce2502021-06-14 14:27:45 +0100357 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
358}
359
Daniel Boulbyce386b12022-03-29 18:36:36 +0100360static inline bool ffa_notifications_info_get_more_pending(struct ffa_value ret)
J-Alves5bce2502021-06-14 14:27:45 +0100361{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100362 return (ret.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
J-Alves5bce2502021-06-14 14:27:45 +0100363}
364
J-Alvesf3a393c2020-10-23 16:00:39 +0100365enum ffa_data_access {
366 FFA_DATA_ACCESS_NOT_SPECIFIED,
367 FFA_DATA_ACCESS_RO,
368 FFA_DATA_ACCESS_RW,
369 FFA_DATA_ACCESS_RESERVED,
370};
371
372enum ffa_instruction_access {
373 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
374 FFA_INSTRUCTION_ACCESS_NX,
375 FFA_INSTRUCTION_ACCESS_X,
376 FFA_INSTRUCTION_ACCESS_RESERVED,
377};
378
379enum ffa_memory_type {
380 FFA_MEMORY_NOT_SPECIFIED_MEM,
381 FFA_MEMORY_DEVICE_MEM,
382 FFA_MEMORY_NORMAL_MEM,
383};
384
385enum ffa_memory_cacheability {
386 FFA_MEMORY_CACHE_RESERVED = 0x0,
387 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
388 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
389 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
390 FFA_MEMORY_DEV_NGNRNE = 0x0,
391 FFA_MEMORY_DEV_NGNRE = 0x1,
392 FFA_MEMORY_DEV_NGRE = 0x2,
393 FFA_MEMORY_DEV_GRE = 0x3,
394};
395
396enum ffa_memory_shareability {
397 FFA_MEMORY_SHARE_NON_SHAREABLE,
398 FFA_MEMORY_SHARE_RESERVED,
399 FFA_MEMORY_OUTER_SHAREABLE,
400 FFA_MEMORY_INNER_SHAREABLE,
401};
402
Karl Meakin92aa7702023-10-11 18:48:01 +0100403typedef struct {
404 uint8_t data_access : 2;
405 uint8_t instruction_access : 2;
406} ffa_memory_access_permissions_t;
407
408_Static_assert(sizeof(ffa_memory_access_permissions_t) == sizeof(uint8_t),
409 "ffa_memory_access_permissions_t must be 1 byte wide");
J-Alvesf3a393c2020-10-23 16:00:39 +0100410
411/**
J-Alvesd13d7602023-05-05 14:19:03 +0100412 * FF-A v1.1 REL0 Table 10.18 memory region attributes descriptor NS Bit 6.
413 * Per section 10.10.4.1, NS bit is reserved for FFA_MEM_DONATE/LEND/SHARE
414 * and FFA_MEM_RETRIEVE_REQUEST.
415 */
416enum ffa_memory_security {
417 FFA_MEMORY_SECURITY_UNSPECIFIED = 0,
418 FFA_MEMORY_SECURITY_SECURE = 0,
419 FFA_MEMORY_SECURITY_NON_SECURE,
420};
421
422/**
J-Alvesb42d17f2022-07-04 12:42:13 +0100423 * This corresponds to table 10.18 of the FF-A v1.1 EAC0 specification, "Memory
424 * region attributes descriptor".
J-Alvesf3a393c2020-10-23 16:00:39 +0100425 */
Karl Meakin92aa7702023-10-11 18:48:01 +0100426typedef struct {
427 uint16_t shareability : 2;
428 uint16_t cacheability : 2;
429 uint16_t type : 2;
430 uint16_t security : 1;
431} ffa_memory_attributes_t;
J-Alvesf3a393c2020-10-23 16:00:39 +0100432
Karl Meakin92aa7702023-10-11 18:48:01 +0100433_Static_assert(sizeof(ffa_memory_attributes_t) == sizeof(uint16_t),
434 "ffa_memory_attributes_t must be 2 bytes wide");
J-Alvesf3a393c2020-10-23 16:00:39 +0100435
436#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
437 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
438#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
439 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
440#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
441
442/**
443 * A set of contiguous pages which is part of a memory region. This corresponds
J-Alvesb42d17f2022-07-04 12:42:13 +0100444 * to table 10.14 of the FF-A v1.1 EAC0 specification, "Constituent memory
445 * region descriptor".
J-Alvesf3a393c2020-10-23 16:00:39 +0100446 */
447struct ffa_memory_region_constituent {
448 /**
449 * The base IPA of the constituent memory region, aligned to 4 kiB page
450 * size granularity.
451 */
452 void *address;
453 /** The number of 4 kiB pages in the constituent memory region. */
454 uint32_t page_count;
455 /** Reserved field, must be 0. */
456 uint32_t reserved;
457};
458
459/**
J-Alvesb42d17f2022-07-04 12:42:13 +0100460 * A set of pages comprising a memory region. This corresponds to table 10.13 of
461 * the FF-A v1.1 EAC0 specification, "Composite memory region descriptor".
J-Alvesf3a393c2020-10-23 16:00:39 +0100462 */
463struct ffa_composite_memory_region {
464 /**
465 * The total number of 4 kiB pages included in this memory region. This
466 * must be equal to the sum of page counts specified in each
467 * `ffa_memory_region_constituent`.
468 */
469 uint32_t page_count;
470 /**
471 * The number of constituents (`ffa_memory_region_constituent`)
472 * included in this memory region range.
473 */
474 uint32_t constituent_count;
475 /** Reserved field, must be 0. */
476 uint64_t reserved_0;
477 /** An array of `constituent_count` memory region constituents. */
478 struct ffa_memory_region_constituent constituents[];
479};
480
481/**
482 * This corresponds to table "Memory access permissions descriptor" of the FFA
483 * 1.0 specification.
484 */
485struct ffa_memory_region_attributes {
486 /** The ID of the VM to which the memory is being given or shared. */
Daniel Boulbye79d2072021-03-03 11:34:53 +0000487 ffa_id_t receiver;
J-Alvesf3a393c2020-10-23 16:00:39 +0100488 /**
489 * The permissions with which the memory region should be mapped in the
490 * receiver's page table.
491 */
492 ffa_memory_access_permissions_t permissions;
493 /**
494 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
495 * for memory regions with multiple borrowers.
496 */
497 ffa_memory_receiver_flags_t flags;
498};
499
500/** Flags to control the behaviour of a memory sharing transaction. */
501typedef uint32_t ffa_memory_region_flags_t;
502
503/**
504 * Clear memory region contents after unmapping it from the sender and before
505 * mapping it for any receiver.
506 */
507#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1U
508
509/**
510 * Whether the hypervisor may time slice the memory sharing or retrieval
511 * operation.
512 */
513#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2U
514
515/**
516 * Whether the hypervisor should clear the memory region after the receiver
517 * relinquishes it or is aborted.
518 */
519#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4U
520
521#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
522#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
523#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
524#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
525#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
526
J-Alves0435cae2020-11-06 10:49:56 +0000527/** The maximum number of recipients a memory region may be sent to. */
528#define MAX_MEM_SHARE_RECIPIENTS 1U
529
J-Alvesf3a393c2020-10-23 16:00:39 +0100530/**
531 * This corresponds to table "Endpoint memory access descriptor" of the FFA 1.0
532 * specification.
533 */
534struct ffa_memory_access {
535 struct ffa_memory_region_attributes receiver_permissions;
536 /**
537 * Offset in bytes from the start of the outer `ffa_memory_region` to
538 * an `ffa_composite_memory_region` struct.
539 */
540 uint32_t composite_memory_region_offset;
541 uint64_t reserved_0;
542};
543
544/**
545 * Information about a set of pages which are being shared. This corresponds to
J-Alvesb42d17f2022-07-04 12:42:13 +0100546 * table 10.20 of the FF-A v1.1 EAC0 specification, "Lend, donate or share
547 * memory transaction descriptor". Note that it is also used for retrieve
548 * requests and responses.
J-Alvesf3a393c2020-10-23 16:00:39 +0100549 */
550struct ffa_memory_region {
551 /**
552 * The ID of the VM which originally sent the memory region, i.e. the
553 * owner.
554 */
Daniel Boulbye79d2072021-03-03 11:34:53 +0000555 ffa_id_t sender;
J-Alvesf3a393c2020-10-23 16:00:39 +0100556 ffa_memory_attributes_t attributes;
J-Alvesf3a393c2020-10-23 16:00:39 +0100557 /** Flags to control behaviour of the transaction. */
558 ffa_memory_region_flags_t flags;
559 ffa_memory_handle_t handle;
560 /**
561 * An implementation defined value associated with the receiver and the
562 * memory region.
563 */
564 uint64_t tag;
J-Alvesb42d17f2022-07-04 12:42:13 +0100565 /** Size of the memory access descriptor. */
566 uint32_t memory_access_desc_size;
J-Alvesf3a393c2020-10-23 16:00:39 +0100567 /**
568 * The number of `ffa_memory_access` entries included in this
569 * transaction.
570 */
571 uint32_t receiver_count;
572 /**
J-Alvesb42d17f2022-07-04 12:42:13 +0100573 * Offset of the 'receivers' field, which relates to the memory access
574 * descriptors.
575 */
576 uint32_t receivers_offset;
577 /** Reserved field (12 bytes) must be 0. */
578 uint32_t reserved[3];
579 /**
580 * An array of `receiver_count` endpoint memory access descriptors.
J-Alvesf3a393c2020-10-23 16:00:39 +0100581 * Each one specifies a memory region offset, an endpoint and the
582 * attributes with which this memory region should be mapped in that
583 * endpoint's page table.
584 */
585 struct ffa_memory_access receivers[];
586};
587
588/**
589 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
J-Alvesb42d17f2022-07-04 12:42:13 +0100590 * 16.25 of the FF-A v1.1 EAC0 specification, "Descriptor to relinquish a memory
591 * region".
J-Alvesf3a393c2020-10-23 16:00:39 +0100592 */
593struct ffa_mem_relinquish {
594 ffa_memory_handle_t handle;
595 ffa_memory_region_flags_t flags;
596 uint32_t endpoint_count;
Daniel Boulbye79d2072021-03-03 11:34:53 +0000597 ffa_id_t endpoints[];
J-Alvesf3a393c2020-10-23 16:00:39 +0100598};
599
600static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t h1, uint32_t h2)
601{
J-Alves18c28052021-03-09 09:58:53 +0000602 return (ffa_notification_bitmap_t)h1 |
603 (ffa_notification_bitmap_t)h2 << 32;
J-Alvesf3a393c2020-10-23 16:00:39 +0100604}
605
Daniel Boulbyce386b12022-03-29 18:36:36 +0100606static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value r)
J-Alvesf3a393c2020-10-23 16:00:39 +0100607{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100608 return ffa_assemble_handle(r.arg2, r.arg3);
J-Alvesf3a393c2020-10-23 16:00:39 +0100609}
610
611/**
612 * Gets the `ffa_composite_memory_region` for the given receiver from an
613 * `ffa_memory_region`, or NULL if it is not valid.
614 */
615static inline struct ffa_composite_memory_region *
616ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
617 uint32_t receiver_index)
618{
619 uint32_t offset = memory_region->receivers[receiver_index]
620 .composite_memory_region_offset;
621
622 if (offset == 0) {
623 return NULL;
624 }
625
626 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
627 offset);
628}
629
630static inline uint32_t ffa_mem_relinquish_init(
631 struct ffa_mem_relinquish *relinquish_request,
632 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
Daniel Boulbye79d2072021-03-03 11:34:53 +0000633 ffa_id_t sender)
J-Alvesf3a393c2020-10-23 16:00:39 +0100634{
635 relinquish_request->handle = handle;
636 relinquish_request->flags = flags;
637 relinquish_request->endpoint_count = 1;
638 relinquish_request->endpoints[0] = sender;
Daniel Boulbye79d2072021-03-03 11:34:53 +0000639 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
J-Alvesf3a393c2020-10-23 16:00:39 +0100640}
641
642uint32_t ffa_memory_retrieve_request_init(
643 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
Daniel Boulbye79d2072021-03-03 11:34:53 +0000644 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
J-Alvesf3a393c2020-10-23 16:00:39 +0100645 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
646 enum ffa_instruction_access instruction_access,
647 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
648 enum ffa_memory_shareability shareability);
649
Karl Meakin3d879b82023-06-16 10:32:08 +0100650void ffa_hypervisor_retrieve_request_init(struct ffa_memory_region *region,
651 ffa_memory_handle_t handle);
652
J-Alvesf3a393c2020-10-23 16:00:39 +0100653uint32_t ffa_memory_region_init(
654 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
Daniel Boulbye79d2072021-03-03 11:34:53 +0000655 ffa_id_t sender, ffa_id_t receiver,
J-Alvesf3a393c2020-10-23 16:00:39 +0100656 const struct ffa_memory_region_constituent constituents[],
657 uint32_t constituent_count, uint32_t tag,
658 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
659 enum ffa_instruction_access instruction_access,
660 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
661 enum ffa_memory_shareability shareability, uint32_t *total_length,
662 uint32_t *fragment_length);
663
Daniel Boulbyce386b12022-03-29 18:36:36 +0100664static inline ffa_id_t ffa_dir_msg_dest(struct ffa_value val) {
665 return (ffa_id_t)val.arg1 & U(0xFFFF);
J-Alves6cb21d92021-01-07 15:18:12 +0000666}
667
Daniel Boulbyce386b12022-03-29 18:36:36 +0100668static inline ffa_id_t ffa_dir_msg_source(struct ffa_value val) {
669 return (ffa_id_t)(val.arg1 >> 16U);
J-Alves6cb21d92021-01-07 15:18:12 +0000670}
671
Daniel Boulbyce386b12022-03-29 18:36:36 +0100672struct ffa_value ffa_msg_send_direct_req64(ffa_id_t source_id,
673 ffa_id_t dest_id, uint64_t arg0,
674 uint64_t arg1, uint64_t arg2,
675 uint64_t arg3, uint64_t arg4);
J-Alvesd1aae292020-10-08 17:16:58 +0100676
Daniel Boulbyce386b12022-03-29 18:36:36 +0100677struct ffa_value ffa_msg_send_direct_req32(ffa_id_t source_id,
678 ffa_id_t dest_id, uint32_t arg0,
679 uint32_t arg1, uint32_t arg2,
680 uint32_t arg3, uint32_t arg4);
J-Alvesecd30742021-02-19 18:31:06 +0000681
Daniel Boulbyce386b12022-03-29 18:36:36 +0100682struct ffa_value ffa_msg_send_direct_resp64(ffa_id_t source_id,
683 ffa_id_t dest_id, uint64_t arg0,
684 uint64_t arg1, uint64_t arg2,
685 uint64_t arg3, uint64_t arg4);
J-Alvesecd30742021-02-19 18:31:06 +0000686
Daniel Boulbyce386b12022-03-29 18:36:36 +0100687struct ffa_value ffa_msg_send_direct_resp32(ffa_id_t source_id,
688 ffa_id_t dest_id, uint32_t arg0,
689 uint32_t arg1, uint32_t arg2,
690 uint32_t arg3, uint32_t arg4);
J-Alves035b7d02021-02-11 10:40:35 +0000691
Daniel Boulbyce386b12022-03-29 18:36:36 +0100692struct ffa_value ffa_run(uint32_t dest_id, uint32_t vcpu_id);
693struct ffa_value ffa_version(uint32_t input_version);
694struct ffa_value ffa_id_get(void);
695struct ffa_value ffa_spm_id_get(void);
696struct ffa_value ffa_msg_wait(void);
697struct ffa_value ffa_error(int32_t error_code);
698struct ffa_value ffa_features(uint32_t feature);
Karl Meakin31b81772023-03-14 15:38:17 +0000699struct ffa_value ffa_features_with_input_property(uint32_t feature,
700 uint32_t param);
Daniel Boulbyce386b12022-03-29 18:36:36 +0100701struct ffa_value ffa_partition_info_get(const struct ffa_uuid uuid);
702struct ffa_value ffa_rx_release(void);
703struct ffa_value ffa_rxtx_map(uintptr_t send, uintptr_t recv, uint32_t pages);
704struct ffa_value ffa_rxtx_unmap(void);
705struct ffa_value ffa_mem_donate(uint32_t descriptor_length,
706 uint32_t fragment_length);
707struct ffa_value ffa_mem_lend(uint32_t descriptor_length,
J-Alves3ea46d12020-09-09 11:13:05 +0100708 uint32_t fragment_length);
Daniel Boulbyce386b12022-03-29 18:36:36 +0100709struct ffa_value ffa_mem_share(uint32_t descriptor_length,
710 uint32_t fragment_length);
711struct ffa_value ffa_mem_retrieve_req(uint32_t descriptor_length,
712 uint32_t fragment_length);
713struct ffa_value ffa_mem_relinquish(void);
714struct ffa_value ffa_mem_reclaim(uint64_t handle, uint32_t flags);
715struct ffa_value ffa_notification_bitmap_create(ffa_id_t vm_id,
716 ffa_vcpu_count_t vcpu_count);
717struct ffa_value ffa_notification_bitmap_destroy(ffa_id_t vm_id);
718struct ffa_value ffa_notification_bind(ffa_id_t sender, ffa_id_t receiver,
719 uint32_t flags,
J-Alves18c28052021-03-09 09:58:53 +0000720 ffa_notification_bitmap_t notifications);
Daniel Boulbyce386b12022-03-29 18:36:36 +0100721struct ffa_value ffa_notification_unbind(ffa_id_t sender, ffa_id_t receiver,
722 ffa_notification_bitmap_t notifications);
723struct ffa_value ffa_notification_set(ffa_id_t sender, ffa_id_t receiver,
724 uint32_t flags,
725 ffa_notification_bitmap_t bitmap);
726struct ffa_value ffa_notification_get(ffa_id_t receiver, uint32_t vcpu_id,
727 uint32_t flags);
728struct ffa_value ffa_notification_info_get(void);
Maksims Svecovs0b452232022-05-24 11:30:34 +0100729
730struct ffa_value ffa_console_log(const char* message, size_t char_count);
Raghu Krishnamurthyab5321a2023-04-23 16:14:28 -0700731struct ffa_value ffa_partition_info_get_regs(const struct ffa_uuid uuid,
732 const uint16_t start_index,
733 const uint16_t tag);
Karl Meakin367ff542023-11-01 15:05:37 +0000734
735struct ffa_memory_access ffa_memory_access_init_permissions(
736 ffa_id_t receiver_id, enum ffa_data_access data_access,
737 enum ffa_instruction_access instruction_access,
738 ffa_memory_receiver_flags_t flags);
739
J-Alves7581c382020-05-07 18:34:20 +0100740#endif /* __ASSEMBLY__ */
741
742#endif /* FFA_HELPERS_H */