blob: 3da5c18b04f8026de437de4e2c66bd48cebd348a [file] [log] [blame]
J-Alves7581c382020-05-07 18:34:20 +01001/*
Daniel Boulby8aa994c2022-01-05 19:44:30 +00002 * Copyright (c) 2018-2022, 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 {
25 const uint32_t uuid[4];
26};
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;
191};
192
193/* Function to make an SMC or SVC service call depending on the exception
194 * level of the SP.
195 */
196struct ffa_value ffa_service_call(struct ffa_value *args);
197
198/*
199 * Functions to trigger a service call.
200 *
201 * The arguments to pass through the service call must be stored in the
202 * ffa_value structure. The return values of the service call will be stored
203 * in the same structure (overriding the input arguments).
204 *
205 * Return the first return value. It is equivalent to args.fid but is also
206 * provided as the return value for convenience.
207 */
208u_register_t ffa_svc(struct ffa_value *args);
209u_register_t ffa_smc(struct ffa_value *args);
210
211static inline uint32_t ffa_func_id(struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100212{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100213 return (uint32_t)val.fid;
J-Alves6cb21d92021-01-07 15:18:12 +0000214}
215
Daniel Boulbyce386b12022-03-29 18:36:36 +0100216static inline int32_t ffa_error_code(struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100217{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100218 return (int32_t)val.arg2;
J-Alves6cb21d92021-01-07 15:18:12 +0000219}
220
Daniel Boulbyce386b12022-03-29 18:36:36 +0100221static inline ffa_id_t ffa_endpoint_id(struct ffa_value val) {
222 return (ffa_id_t)val.arg2 & 0xffff;
Daniel Boulby198deda2021-03-03 11:35:25 +0000223}
224
Daniel Boulbyce386b12022-03-29 18:36:36 +0100225static inline uint32_t ffa_partition_info_count(struct ffa_value val)
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000226{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100227 return (uint32_t)val.arg2;
Daniel Boulby2ac55f22022-01-21 12:08:08 +0000228}
229
Daniel Boulby07d751e2022-05-30 17:32:00 +0100230static inline uint32_t ffa_partition_info_desc_size(struct ffa_value val)
231{
232 return (uint32_t)val.arg3;
233}
234
Daniel Boulbyce386b12022-03-29 18:36:36 +0100235static inline uint32_t ffa_feature_intid(struct ffa_value val)
J-Alves4439ece2021-11-05 11:52:54 +0000236{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100237 return (uint32_t)val.arg2;
J-Alves4439ece2021-11-05 11:52:54 +0000238}
239
J-Alves18c28052021-03-09 09:58:53 +0000240typedef uint64_t ffa_notification_bitmap_t;
241
242#define FFA_NOTIFICATION(ID) (UINT64_C(1) << ID)
243
244#define MAX_FFA_NOTIFICATIONS UINT32_C(64)
245
246#define FFA_NOTIFICATIONS_FLAG_PER_VCPU UINT32_C(0x1 << 0)
247
J-Alvesb0cb5d02021-07-08 11:19:33 +0100248/** Flag to delay Schedule Receiver Interrupt. */
249#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI UINT32_C(0x1 << 1)
250
J-Alves18c28052021-03-09 09:58:53 +0000251#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) UINT32_C((id & 0xFFFF) << 16)
252
J-Alvesf156ae92021-10-08 12:10:05 +0100253#define FFA_NOTIFICATIONS_FLAG_BITMAP_SP UINT32_C(0x1 << 0)
254#define FFA_NOTIFICATIONS_FLAG_BITMAP_VM UINT32_C(0x1 << 1)
255#define FFA_NOTIFICATIONS_FLAG_BITMAP_SPM UINT32_C(0x1 << 2)
256#define FFA_NOTIFICATIONS_FLAG_BITMAP_HYP UINT32_C(0x1 << 3)
257
J-Alves269118a2021-09-22 09:46:11 +0100258/**
259 * The following is an SGI ID, that the SPMC configures as non-secure, as
260 * suggested by the FF-A v1.1 specification, in section 9.4.1.
261 */
262#define FFA_SCHEDULE_RECEIVER_INTERRUPT_ID 8
263
J-Alvesf156ae92021-10-08 12:10:05 +0100264#define FFA_NOTIFICATIONS_BITMAP(lo, hi) \
265 (ffa_notification_bitmap_t)(lo) | \
266 (((ffa_notification_bitmap_t)hi << 32) & 0xFFFFFFFF00000000ULL)
267
268#define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) UINT32_C((id & 0xFFFF) << 16)
269
Daniel Boulbyce386b12022-03-29 18:36:36 +0100270static inline ffa_notification_bitmap_t ffa_notifications_get_from_sp(
271 struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100272{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100273 return FFA_NOTIFICATIONS_BITMAP(val.arg2, val.arg3);
J-Alvesf156ae92021-10-08 12:10:05 +0100274}
275
Daniel Boulbyce386b12022-03-29 18:36:36 +0100276static inline ffa_notification_bitmap_t ffa_notifications_get_from_vm(
277 struct ffa_value val)
J-Alvesf156ae92021-10-08 12:10:05 +0100278{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100279 return FFA_NOTIFICATIONS_BITMAP(val.arg4, val.arg5);
J-Alvesf156ae92021-10-08 12:10:05 +0100280}
281
J-Alves5bce2502021-06-14 14:27:45 +0100282/*
283 * FFA_NOTIFICATION_INFO_GET is a SMC64 interface.
284 * The following macros are defined for SMC64 implementation.
285 */
286#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
287
288#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING UINT64_C(0x1)
289
290#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
291#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1FU
292#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
293#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
294
295static inline uint32_t ffa_notifications_info_get_lists_count(
Daniel Boulbyce386b12022-03-29 18:36:36 +0100296 struct ffa_value ret)
J-Alves5bce2502021-06-14 14:27:45 +0100297{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100298 return (uint32_t)(ret.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT)
J-Alves5bce2502021-06-14 14:27:45 +0100299 & FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
300}
301
302static inline uint32_t ffa_notifications_info_get_list_size(
Daniel Boulbyce386b12022-03-29 18:36:36 +0100303 struct ffa_value ret, uint32_t list)
J-Alves5bce2502021-06-14 14:27:45 +0100304{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100305 return (uint32_t)(ret.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list)) &
J-Alves5bce2502021-06-14 14:27:45 +0100306 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
307}
308
Daniel Boulbyce386b12022-03-29 18:36:36 +0100309static inline bool ffa_notifications_info_get_more_pending(struct ffa_value ret)
J-Alves5bce2502021-06-14 14:27:45 +0100310{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100311 return (ret.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
J-Alves5bce2502021-06-14 14:27:45 +0100312}
313
J-Alvesf3a393c2020-10-23 16:00:39 +0100314enum ffa_data_access {
315 FFA_DATA_ACCESS_NOT_SPECIFIED,
316 FFA_DATA_ACCESS_RO,
317 FFA_DATA_ACCESS_RW,
318 FFA_DATA_ACCESS_RESERVED,
319};
320
321enum ffa_instruction_access {
322 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
323 FFA_INSTRUCTION_ACCESS_NX,
324 FFA_INSTRUCTION_ACCESS_X,
325 FFA_INSTRUCTION_ACCESS_RESERVED,
326};
327
328enum ffa_memory_type {
329 FFA_MEMORY_NOT_SPECIFIED_MEM,
330 FFA_MEMORY_DEVICE_MEM,
331 FFA_MEMORY_NORMAL_MEM,
332};
333
334enum ffa_memory_cacheability {
335 FFA_MEMORY_CACHE_RESERVED = 0x0,
336 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
337 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
338 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
339 FFA_MEMORY_DEV_NGNRNE = 0x0,
340 FFA_MEMORY_DEV_NGNRE = 0x1,
341 FFA_MEMORY_DEV_NGRE = 0x2,
342 FFA_MEMORY_DEV_GRE = 0x3,
343};
344
345enum ffa_memory_shareability {
346 FFA_MEMORY_SHARE_NON_SHAREABLE,
347 FFA_MEMORY_SHARE_RESERVED,
348 FFA_MEMORY_OUTER_SHAREABLE,
349 FFA_MEMORY_INNER_SHAREABLE,
350};
351
352typedef uint8_t ffa_memory_access_permissions_t;
353
354/**
355 * This corresponds to table "Memory region attributes descriptor" of the FF-A
356 * 1.0 specification.
357 */
358typedef uint8_t ffa_memory_attributes_t;
359
360#define FFA_DATA_ACCESS_OFFSET (0x0U)
361#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
362
363#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
364#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
365
366#define FFA_MEMORY_TYPE_OFFSET (0x4U)
367#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
368
369#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
370#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
371
372#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
373#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
374
375#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
376 static inline void ffa_set_##name##_attr(container_type *attr, \
377 const enum ffa_##name perm) \
378 { \
379 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
380 }
381
382#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
383 static inline enum ffa_##name ffa_get_##name##_attr( \
384 container_type attr) \
385 { \
386 return (enum ffa_##name)((attr & mask) >> offset); \
387 }
388
389ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
390 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
391ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
392 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
393
394ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
395 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
396ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
397 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
398
399ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
400 FFA_MEMORY_TYPE_MASK)
401ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
402 FFA_MEMORY_TYPE_MASK)
403
404ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
405 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
406ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
407 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
408
409ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
410 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
411ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
412 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
413
414#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
415 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
416#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
417 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
418#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
419
420/**
421 * A set of contiguous pages which is part of a memory region. This corresponds
422 * to table "Constituent memory region descriptor" of the FFA 1.0 specification.
423 */
424struct ffa_memory_region_constituent {
425 /**
426 * The base IPA of the constituent memory region, aligned to 4 kiB page
427 * size granularity.
428 */
429 void *address;
430 /** The number of 4 kiB pages in the constituent memory region. */
431 uint32_t page_count;
432 /** Reserved field, must be 0. */
433 uint32_t reserved;
434};
435
436/**
437 * A set of pages comprising a memory region. This corresponds to table
438 * "Composite memory region descriptor" of the FFA 1.0 specification.
439 */
440struct ffa_composite_memory_region {
441 /**
442 * The total number of 4 kiB pages included in this memory region. This
443 * must be equal to the sum of page counts specified in each
444 * `ffa_memory_region_constituent`.
445 */
446 uint32_t page_count;
447 /**
448 * The number of constituents (`ffa_memory_region_constituent`)
449 * included in this memory region range.
450 */
451 uint32_t constituent_count;
452 /** Reserved field, must be 0. */
453 uint64_t reserved_0;
454 /** An array of `constituent_count` memory region constituents. */
455 struct ffa_memory_region_constituent constituents[];
456};
457
458/**
459 * This corresponds to table "Memory access permissions descriptor" of the FFA
460 * 1.0 specification.
461 */
462struct ffa_memory_region_attributes {
463 /** The ID of the VM to which the memory is being given or shared. */
Daniel Boulbye79d2072021-03-03 11:34:53 +0000464 ffa_id_t receiver;
J-Alvesf3a393c2020-10-23 16:00:39 +0100465 /**
466 * The permissions with which the memory region should be mapped in the
467 * receiver's page table.
468 */
469 ffa_memory_access_permissions_t permissions;
470 /**
471 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
472 * for memory regions with multiple borrowers.
473 */
474 ffa_memory_receiver_flags_t flags;
475};
476
477/** Flags to control the behaviour of a memory sharing transaction. */
478typedef uint32_t ffa_memory_region_flags_t;
479
480/**
481 * Clear memory region contents after unmapping it from the sender and before
482 * mapping it for any receiver.
483 */
484#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1U
485
486/**
487 * Whether the hypervisor may time slice the memory sharing or retrieval
488 * operation.
489 */
490#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2U
491
492/**
493 * Whether the hypervisor should clear the memory region after the receiver
494 * relinquishes it or is aborted.
495 */
496#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4U
497
498#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
499#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
500#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
501#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
502#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
503
J-Alves0435cae2020-11-06 10:49:56 +0000504/** The maximum number of recipients a memory region may be sent to. */
505#define MAX_MEM_SHARE_RECIPIENTS 1U
506
J-Alvesf3a393c2020-10-23 16:00:39 +0100507/**
508 * This corresponds to table "Endpoint memory access descriptor" of the FFA 1.0
509 * specification.
510 */
511struct ffa_memory_access {
512 struct ffa_memory_region_attributes receiver_permissions;
513 /**
514 * Offset in bytes from the start of the outer `ffa_memory_region` to
515 * an `ffa_composite_memory_region` struct.
516 */
517 uint32_t composite_memory_region_offset;
518 uint64_t reserved_0;
519};
520
521/**
522 * Information about a set of pages which are being shared. This corresponds to
523 * table "Lend, donate or share memory transaction descriptor" of the FFA
524 * 1.0 specification. Note that it is also used for retrieve requests and
525 * responses.
526 */
527struct ffa_memory_region {
528 /**
529 * The ID of the VM which originally sent the memory region, i.e. the
530 * owner.
531 */
Daniel Boulbye79d2072021-03-03 11:34:53 +0000532 ffa_id_t sender;
J-Alvesf3a393c2020-10-23 16:00:39 +0100533 ffa_memory_attributes_t attributes;
534 /** Reserved field, must be 0. */
535 uint8_t reserved_0;
536 /** Flags to control behaviour of the transaction. */
537 ffa_memory_region_flags_t flags;
538 ffa_memory_handle_t handle;
539 /**
540 * An implementation defined value associated with the receiver and the
541 * memory region.
542 */
543 uint64_t tag;
544 /** Reserved field, must be 0. */
545 uint32_t reserved_1;
546 /**
547 * The number of `ffa_memory_access` entries included in this
548 * transaction.
549 */
550 uint32_t receiver_count;
551 /**
552 * An array of `attribute_count` endpoint memory access descriptors.
553 * Each one specifies a memory region offset, an endpoint and the
554 * attributes with which this memory region should be mapped in that
555 * endpoint's page table.
556 */
557 struct ffa_memory_access receivers[];
558};
559
560/**
561 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
562 * "Descriptor to relinquish a memory region" of the FFA 1.0 specification.
563 */
564struct ffa_mem_relinquish {
565 ffa_memory_handle_t handle;
566 ffa_memory_region_flags_t flags;
567 uint32_t endpoint_count;
Daniel Boulbye79d2072021-03-03 11:34:53 +0000568 ffa_id_t endpoints[];
J-Alvesf3a393c2020-10-23 16:00:39 +0100569};
570
571static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t h1, uint32_t h2)
572{
J-Alves18c28052021-03-09 09:58:53 +0000573 return (ffa_notification_bitmap_t)h1 |
574 (ffa_notification_bitmap_t)h2 << 32;
J-Alvesf3a393c2020-10-23 16:00:39 +0100575}
576
Daniel Boulbyce386b12022-03-29 18:36:36 +0100577static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value r)
J-Alvesf3a393c2020-10-23 16:00:39 +0100578{
Daniel Boulbyce386b12022-03-29 18:36:36 +0100579 return ffa_assemble_handle(r.arg2, r.arg3);
J-Alvesf3a393c2020-10-23 16:00:39 +0100580}
581
582/**
583 * Gets the `ffa_composite_memory_region` for the given receiver from an
584 * `ffa_memory_region`, or NULL if it is not valid.
585 */
586static inline struct ffa_composite_memory_region *
587ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
588 uint32_t receiver_index)
589{
590 uint32_t offset = memory_region->receivers[receiver_index]
591 .composite_memory_region_offset;
592
593 if (offset == 0) {
594 return NULL;
595 }
596
597 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
598 offset);
599}
600
601static inline uint32_t ffa_mem_relinquish_init(
602 struct ffa_mem_relinquish *relinquish_request,
603 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
Daniel Boulbye79d2072021-03-03 11:34:53 +0000604 ffa_id_t sender)
J-Alvesf3a393c2020-10-23 16:00:39 +0100605{
606 relinquish_request->handle = handle;
607 relinquish_request->flags = flags;
608 relinquish_request->endpoint_count = 1;
609 relinquish_request->endpoints[0] = sender;
Daniel Boulbye79d2072021-03-03 11:34:53 +0000610 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t);
J-Alvesf3a393c2020-10-23 16:00:39 +0100611}
612
613uint32_t ffa_memory_retrieve_request_init(
614 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
Daniel Boulbye79d2072021-03-03 11:34:53 +0000615 ffa_id_t sender, ffa_id_t receiver, uint32_t tag,
J-Alvesf3a393c2020-10-23 16:00:39 +0100616 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
617 enum ffa_instruction_access instruction_access,
618 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
619 enum ffa_memory_shareability shareability);
620
621uint32_t ffa_memory_region_init(
622 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
Daniel Boulbye79d2072021-03-03 11:34:53 +0000623 ffa_id_t sender, ffa_id_t receiver,
J-Alvesf3a393c2020-10-23 16:00:39 +0100624 const struct ffa_memory_region_constituent constituents[],
625 uint32_t constituent_count, uint32_t tag,
626 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
627 enum ffa_instruction_access instruction_access,
628 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
629 enum ffa_memory_shareability shareability, uint32_t *total_length,
630 uint32_t *fragment_length);
631
Daniel Boulbyce386b12022-03-29 18:36:36 +0100632static inline ffa_id_t ffa_dir_msg_dest(struct ffa_value val) {
633 return (ffa_id_t)val.arg1 & U(0xFFFF);
J-Alves6cb21d92021-01-07 15:18:12 +0000634}
635
Daniel Boulbyce386b12022-03-29 18:36:36 +0100636static inline ffa_id_t ffa_dir_msg_source(struct ffa_value val) {
637 return (ffa_id_t)(val.arg1 >> 16U);
J-Alves6cb21d92021-01-07 15:18:12 +0000638}
639
Daniel Boulbyce386b12022-03-29 18:36:36 +0100640struct ffa_value ffa_msg_send_direct_req64(ffa_id_t source_id,
641 ffa_id_t dest_id, uint64_t arg0,
642 uint64_t arg1, uint64_t arg2,
643 uint64_t arg3, uint64_t arg4);
J-Alvesd1aae292020-10-08 17:16:58 +0100644
Daniel Boulbyce386b12022-03-29 18:36:36 +0100645struct ffa_value ffa_msg_send_direct_req32(ffa_id_t source_id,
646 ffa_id_t dest_id, uint32_t arg0,
647 uint32_t arg1, uint32_t arg2,
648 uint32_t arg3, uint32_t arg4);
J-Alvesecd30742021-02-19 18:31:06 +0000649
Daniel Boulbyce386b12022-03-29 18:36:36 +0100650struct ffa_value ffa_msg_send_direct_resp64(ffa_id_t source_id,
651 ffa_id_t dest_id, uint64_t arg0,
652 uint64_t arg1, uint64_t arg2,
653 uint64_t arg3, uint64_t arg4);
J-Alvesecd30742021-02-19 18:31:06 +0000654
Daniel Boulbyce386b12022-03-29 18:36:36 +0100655struct ffa_value ffa_msg_send_direct_resp32(ffa_id_t source_id,
656 ffa_id_t dest_id, uint32_t arg0,
657 uint32_t arg1, uint32_t arg2,
658 uint32_t arg3, uint32_t arg4);
J-Alves035b7d02021-02-11 10:40:35 +0000659
Daniel Boulbyce386b12022-03-29 18:36:36 +0100660struct ffa_value ffa_run(uint32_t dest_id, uint32_t vcpu_id);
661struct ffa_value ffa_version(uint32_t input_version);
662struct ffa_value ffa_id_get(void);
663struct ffa_value ffa_spm_id_get(void);
664struct ffa_value ffa_msg_wait(void);
665struct ffa_value ffa_error(int32_t error_code);
666struct ffa_value ffa_features(uint32_t feature);
667struct ffa_value ffa_partition_info_get(const struct ffa_uuid uuid);
668struct ffa_value ffa_rx_release(void);
669struct ffa_value ffa_rxtx_map(uintptr_t send, uintptr_t recv, uint32_t pages);
670struct ffa_value ffa_rxtx_unmap(void);
671struct ffa_value ffa_mem_donate(uint32_t descriptor_length,
672 uint32_t fragment_length);
673struct ffa_value ffa_mem_lend(uint32_t descriptor_length,
J-Alves3ea46d12020-09-09 11:13:05 +0100674 uint32_t fragment_length);
Daniel Boulbyce386b12022-03-29 18:36:36 +0100675struct ffa_value ffa_mem_share(uint32_t descriptor_length,
676 uint32_t fragment_length);
677struct ffa_value ffa_mem_retrieve_req(uint32_t descriptor_length,
678 uint32_t fragment_length);
679struct ffa_value ffa_mem_relinquish(void);
680struct ffa_value ffa_mem_reclaim(uint64_t handle, uint32_t flags);
681struct ffa_value ffa_notification_bitmap_create(ffa_id_t vm_id,
682 ffa_vcpu_count_t vcpu_count);
683struct ffa_value ffa_notification_bitmap_destroy(ffa_id_t vm_id);
684struct ffa_value ffa_notification_bind(ffa_id_t sender, ffa_id_t receiver,
685 uint32_t flags,
J-Alves18c28052021-03-09 09:58:53 +0000686 ffa_notification_bitmap_t notifications);
Daniel Boulbyce386b12022-03-29 18:36:36 +0100687struct ffa_value ffa_notification_unbind(ffa_id_t sender, ffa_id_t receiver,
688 ffa_notification_bitmap_t notifications);
689struct ffa_value ffa_notification_set(ffa_id_t sender, ffa_id_t receiver,
690 uint32_t flags,
691 ffa_notification_bitmap_t bitmap);
692struct ffa_value ffa_notification_get(ffa_id_t receiver, uint32_t vcpu_id,
693 uint32_t flags);
694struct ffa_value ffa_notification_info_get(void);
Maksims Svecovs0b452232022-05-24 11:30:34 +0100695
696struct ffa_value ffa_console_log(const char* message, size_t char_count);
697
J-Alves7581c382020-05-07 18:34:20 +0100698#endif /* __ASSEMBLY__ */
699
700#endif /* FFA_HELPERS_H */