blob: f3d9d15f54652746f68d45218279eac5dc3a178e [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
83/* FF-A error codes. */
84#define FFA_NOT_SUPPORTED INT32_C(-1)
85#define FFA_INVALID_PARAMETERS INT32_C(-2)
86#define FFA_NO_MEMORY INT32_C(-3)
87#define FFA_BUSY INT32_C(-4)
88#define FFA_INTERRUPTED INT32_C(-5)
89#define FFA_DENIED INT32_C(-6)
90#define FFA_RETRY INT32_C(-7)
91#define FFA_ABORTED INT32_C(-8)
J-Alvesc8e8a222021-06-08 17:33:52 +010092#define FFA_NO_DATA INT32_C(-9)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010093
94/* clang-format on */
95
96/* FF-A function specific constants. */
97#define FFA_MSG_RECV_BLOCK 0x1
98#define FFA_MSG_RECV_BLOCK_MASK 0x1
99
100#define FFA_MSG_SEND_NOTIFY 0x1
101#define FFA_MSG_SEND_NOTIFY_MASK 0x1
102
103#define FFA_MEM_RECLAIM_CLEAR 0x1
104
105#define FFA_SLEEP_INDEFINITE 0
106
Raghu Krishnamurthyea6d25f2021-09-14 15:27:06 -0700107#define FFA_MEM_PERM_RO UINT32_C(0x7)
108#define FFA_MEM_PERM_RW UINT32_C(0x5)
109#define FFA_MEM_PERM_RX UINT32_C(0x3)
110
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100111/**
112 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
113 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
114 * configured to use.
115 */
116#define FFA_PAGE_SIZE 4096
117
118/* The maximum length possible for a single message. */
119#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
120
121enum ffa_data_access {
122 FFA_DATA_ACCESS_NOT_SPECIFIED,
123 FFA_DATA_ACCESS_RO,
124 FFA_DATA_ACCESS_RW,
125 FFA_DATA_ACCESS_RESERVED,
126};
127
128enum ffa_instruction_access {
129 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
130 FFA_INSTRUCTION_ACCESS_NX,
131 FFA_INSTRUCTION_ACCESS_X,
132 FFA_INSTRUCTION_ACCESS_RESERVED,
133};
134
135enum ffa_memory_type {
136 FFA_MEMORY_NOT_SPECIFIED_MEM,
137 FFA_MEMORY_DEVICE_MEM,
138 FFA_MEMORY_NORMAL_MEM,
139};
140
141enum ffa_memory_cacheability {
142 FFA_MEMORY_CACHE_RESERVED = 0x0,
143 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
144 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
145 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
146 FFA_MEMORY_DEV_NGNRNE = 0x0,
147 FFA_MEMORY_DEV_NGNRE = 0x1,
148 FFA_MEMORY_DEV_NGRE = 0x2,
149 FFA_MEMORY_DEV_GRE = 0x3,
150};
151
152enum ffa_memory_shareability {
153 FFA_MEMORY_SHARE_NON_SHAREABLE,
154 FFA_MEMORY_SHARE_RESERVED,
155 FFA_MEMORY_OUTER_SHAREABLE,
156 FFA_MEMORY_INNER_SHAREABLE,
157};
158
159typedef uint8_t ffa_memory_access_permissions_t;
160
161/**
162 * This corresponds to table 44 of the FF-A 1.0 EAC specification, "Memory
163 * region attributes descriptor".
164 */
165typedef uint8_t ffa_memory_attributes_t;
166
167#define FFA_DATA_ACCESS_OFFSET (0x0U)
168#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
169
170#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
171#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
172
173#define FFA_MEMORY_TYPE_OFFSET (0x4U)
174#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
175
176#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
177#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
178
179#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
180#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
181
182#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
183 static inline void ffa_set_##name##_attr(container_type *attr, \
184 const enum ffa_##name perm) \
185 { \
186 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
187 }
188
189#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
190 static inline enum ffa_##name ffa_get_##name##_attr( \
191 container_type attr) \
192 { \
193 return (enum ffa_##name)((attr & mask) >> offset); \
194 }
195
196ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
197 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
198ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
199 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
200
201ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
202 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
203ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
204 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
205
206ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
207 FFA_MEMORY_TYPE_MASK)
208ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
209 FFA_MEMORY_TYPE_MASK)
210
211ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
212 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
213ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
214 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
215
216ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
217 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
218ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
219 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
220
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100221/**
222 * A globally-unique ID assigned by the hypervisor for a region of memory being
223 * sent between VMs.
224 */
225typedef uint64_t ffa_memory_handle_t;
226
J-Alves917d2f22020-10-30 18:39:30 +0000227#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
228 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
229#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
230 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
231
232#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
233#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
234
235/** The ID of a VM. These are assigned sequentially starting with an offset. */
236typedef uint16_t ffa_vm_id_t;
237
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100238/**
239 * A count of VMs. This has the same range as the VM IDs but we give it a
240 * different name to make the different semantics clear.
241 */
242typedef ffa_vm_id_t ffa_vm_count_t;
243
244/** The index of a vCPU within a particular VM. */
245typedef uint16_t ffa_vcpu_index_t;
246
247/**
248 * A count of vCPUs. This has the same range as the vCPU indices but we give it
249 * a different name to make the different semantics clear.
250 */
251typedef ffa_vcpu_index_t ffa_vcpu_count_t;
252
253/** Parameter and return type of FF-A functions. */
254struct ffa_value {
255 uint64_t func;
256 uint64_t arg1;
257 uint64_t arg2;
258 uint64_t arg3;
259 uint64_t arg4;
260 uint64_t arg5;
261 uint64_t arg6;
262 uint64_t arg7;
263};
264
J-Alves13318e32021-02-22 17:21:00 +0000265static inline int32_t ffa_error_code(struct ffa_value val)
266{
267 return (int32_t)val.arg2;
268}
269
J-Alvesd6f4e142021-03-05 13:33:59 +0000270static inline ffa_vm_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100271{
272 return (args.arg1 >> 16) & 0xffff;
273}
274
J-Alvesd6f4e142021-03-05 13:33:59 +0000275static inline ffa_vm_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100276{
277 return args.arg1 & 0xffff;
278}
279
280static inline uint32_t ffa_msg_send_size(struct ffa_value args)
281{
282 return args.arg3;
283}
284
285static inline uint32_t ffa_msg_send_attributes(struct ffa_value args)
286{
287 return args.arg4;
288}
289
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100290static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
291{
292 return (uint64_t)a1 | (uint64_t)a2 << 32;
293}
294
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100295static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
296{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100297 return ffa_assemble_handle(args.arg2, args.arg3);
298}
299
Andrew Walbranca808b12020-05-15 17:22:28 +0100300static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
301{
302 return ffa_assemble_handle(args.arg1, args.arg2);
303}
304
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100305static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
306{
307 return (struct ffa_value){.func = FFA_SUCCESS_32,
308 .arg2 = (uint32_t)handle,
309 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100310}
311
312static inline ffa_vm_id_t ffa_vm_id(struct ffa_value args)
313{
314 return (args.arg1 >> 16) & 0xffff;
315}
316
317static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
318{
319 return args.arg1 & 0xffff;
320}
321
322static inline uint64_t ffa_vm_vcpu(ffa_vm_id_t vm_id,
323 ffa_vcpu_index_t vcpu_index)
324{
325 return ((uint32_t)vm_id << 16) | vcpu_index;
326}
327
Andrew Walbranca808b12020-05-15 17:22:28 +0100328static inline ffa_vm_id_t ffa_frag_sender(struct ffa_value args)
329{
330 return (args.arg4 >> 16) & 0xffff;
331}
332
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100333/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100334 * Holds the UUID in a struct that is mappable directly to the SMCC calling
335 * convention, which is used for FF-A calls.
336 *
337 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
338 * of the SMCC Spec 1.2.
339 */
340struct ffa_uuid {
341 uint32_t uuid[4];
342};
343
344static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
345 uint32_t w3, struct ffa_uuid *uuid)
346{
347 uuid->uuid[0] = w0;
348 uuid->uuid[1] = w1;
349 uuid->uuid[2] = w2;
350 uuid->uuid[3] = w3;
351}
352
353static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
354 const struct ffa_uuid *uuid2)
355{
356 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
357 (uuid1->uuid[1] == uuid2->uuid[1]) &&
358 (uuid1->uuid[2] == uuid2->uuid[2]) &&
359 (uuid1->uuid[3] == uuid2->uuid[3]);
360}
361
362static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
363{
364 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
365 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
366}
367
368/**
369 * Flags to determine the partition properties, as required by
370 * FFA_PARTITION_INFO_GET.
371 *
372 * The values of the flags are specified in table 82 of the FF-A 1.0 EAC
373 * specification, "Partition information descriptor, partition properties".
374 */
375typedef uint32_t ffa_partition_properties_t;
376
377/** Partition property: partition supports receipt of direct requests. */
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100378#define FFA_PARTITION_DIRECT_REQ_RECV 0x1
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100379
380/** Partition property: partition can send direct requests. */
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100381#define FFA_PARTITION_DIRECT_REQ_SEND 0x2
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100382
383/** Partition property: partition can send and receive indirect messages. */
384#define FFA_PARTITION_INDIRECT_MSG 0x4
385
386/**
387 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
388 * interface.
389 * This corresponds to table 82 of the FF-A 1.0 EAC specification, "Partition
390 * information descriptor".
391 */
392struct ffa_partition_info {
393 ffa_vm_id_t vm_id;
394 ffa_vcpu_count_t vcpu_count;
395 ffa_partition_properties_t properties;
396};
397
398/**
J-Alves980d1992021-03-18 12:49:18 +0000399 * FF-A v1.1 specification restricts the number of notifications to a maximum
400 * of 64. Following all possible bitmaps.
401 */
402#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
403
404typedef uint64_t ffa_notifications_bitmap_t;
405
406#define MAX_FFA_NOTIFICATIONS 64U
407
J-Alves13394022021-06-30 13:48:49 +0100408#define FFA_SCHEDULE_RECEIVER_INTERRUPT_ID 8
409
J-Alves980d1992021-03-18 12:49:18 +0000410/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000411 * Flag for notification bind and set, to specify call is about per-vCPU
412 * notifications.
413 */
414#define FFA_NOTIFICATION_FLAG_PER_VCPU UINT32_C(1 << 0)
415
416/**
J-Alves980d1992021-03-18 12:49:18 +0000417 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
418 * and hi.
419 * Helpful as FF-A specification defines that the notifications interfaces
420 * arguments are 32-bit registers.
421 */
422static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
423 uint32_t hi)
424{
425 return (ffa_notifications_bitmap_t)hi << 32U | lo;
426}
427
J-Alves98ff9562021-09-09 14:39:41 +0100428static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
429 struct ffa_value val)
430{
431 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
432}
433
434static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
435 struct ffa_value val)
436{
437 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
438}
439
J-Alves980d1992021-03-18 12:49:18 +0000440/**
J-Alvesaa79c012021-07-09 14:29:45 +0100441 * Flags used in calls to FFA_NOTIFICATION_GET interface.
442 */
443#define FFA_NOTIFICATION_FLAG_BITMAP_SP UINT32_C(0x1 << 0)
444#define FFA_NOTIFICATION_FLAG_BITMAP_VM UINT32_C(0x1 << 1)
445#define FFA_NOTIFICATION_FLAG_BITMAP_SPM UINT32_C(0x1 << 2)
446#define FFA_NOTIFICATION_FLAG_BITMAP_HYP UINT32_C(0x1 << 3)
447
J-Alves13394022021-06-30 13:48:49 +0100448/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
449#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI UINT32_C(0x1 << 1)
450
J-Alvesaa79c012021-07-09 14:29:45 +0100451static inline ffa_vm_id_t ffa_notifications_get_receiver(struct ffa_value args)
452{
453 return (args.arg1 >> 16) & 0xffffU;
454}
455
456static inline ffa_vm_id_t ffa_notifications_get_vcpu(struct ffa_value args)
457{
458 return args.arg1 & 0xffffU;
459}
460
461/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100462 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
463 */
464#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
465
466/**
467 * Number of registers to use in successfull return of interface
468 * FFA_NOTIFICATION_INFO_GET.
469 */
470#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
471
472#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
473
474/**
475 * Helper macros for return parameter encoding as described in section 17.7.1
476 * of the FF-A v1.1 Beta0 specification.
477 */
478#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
479#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
480#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
481#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
482
483static inline uint32_t ffa_notification_info_get_lists_count(
484 struct ffa_value args)
485{
486 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
487 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
488}
489
490static inline uint32_t ffa_notification_info_get_list_size(
491 struct ffa_value args, unsigned int list_idx)
492{
493 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
494 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
495}
496
497static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
498{
499 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
500}
501
502/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100503 * A set of contiguous pages which is part of a memory region. This corresponds
504 * to table 40 of the FF-A 1.0 EAC specification, "Constituent memory region
505 * descriptor".
506 */
507struct ffa_memory_region_constituent {
508 /**
509 * The base IPA of the constituent memory region, aligned to 4 kiB page
510 * size granularity.
511 */
512 uint64_t address;
513 /** The number of 4 kiB pages in the constituent memory region. */
514 uint32_t page_count;
515 /** Reserved field, must be 0. */
516 uint32_t reserved;
517};
518
519/**
520 * A set of pages comprising a memory region. This corresponds to table 39 of
521 * the FF-A 1.0 EAC specification, "Composite memory region descriptor".
522 */
523struct ffa_composite_memory_region {
524 /**
525 * The total number of 4 kiB pages included in this memory region. This
526 * must be equal to the sum of page counts specified in each
527 * `ffa_memory_region_constituent`.
528 */
529 uint32_t page_count;
530 /**
531 * The number of constituents (`ffa_memory_region_constituent`)
532 * included in this memory region range.
533 */
534 uint32_t constituent_count;
535 /** Reserved field, must be 0. */
536 uint64_t reserved_0;
537 /** An array of `constituent_count` memory region constituents. */
538 struct ffa_memory_region_constituent constituents[];
539};
540
541/** Flags to indicate properties of receivers during memory region retrieval. */
542typedef uint8_t ffa_memory_receiver_flags_t;
543
544/**
545 * This corresponds to table 41 of the FF-A 1.0 EAC specification, "Memory
546 * access permissions descriptor".
547 */
548struct ffa_memory_region_attributes {
549 /** The ID of the VM to which the memory is being given or shared. */
550 ffa_vm_id_t receiver;
551 /**
552 * The permissions with which the memory region should be mapped in the
553 * receiver's page table.
554 */
555 ffa_memory_access_permissions_t permissions;
556 /**
557 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
558 * for memory regions with multiple borrowers.
559 */
560 ffa_memory_receiver_flags_t flags;
561};
562
563/** Flags to control the behaviour of a memory sharing transaction. */
564typedef uint32_t ffa_memory_region_flags_t;
565
566/**
567 * Clear memory region contents after unmapping it from the sender and before
568 * mapping it for any receiver.
569 */
570#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
571
572/**
573 * Whether the hypervisor may time slice the memory sharing or retrieval
574 * operation.
575 */
576#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
577
578/**
579 * Whether the hypervisor should clear the memory region after the receiver
580 * relinquishes it or is aborted.
581 */
582#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
583
584#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
585#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
586#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
587#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
588#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
589
590/**
591 * This corresponds to table 42 of the FF-A 1.0 EAC specification, "Endpoint
592 * memory access descriptor".
593 */
594struct ffa_memory_access {
595 struct ffa_memory_region_attributes receiver_permissions;
596 /**
597 * Offset in bytes from the start of the outer `ffa_memory_region` to
598 * an `ffa_composite_memory_region` struct.
599 */
600 uint32_t composite_memory_region_offset;
601 uint64_t reserved_0;
602};
603
604/**
605 * Information about a set of pages which are being shared. This corresponds to
606 * table 45 of the FF-A 1.0 EAC specification, "Lend, donate or share memory
607 * transaction descriptor". Note that it is also used for retrieve requests and
608 * responses.
609 */
610struct ffa_memory_region {
611 /**
612 * The ID of the VM which originally sent the memory region, i.e. the
613 * owner.
614 */
615 ffa_vm_id_t sender;
616 ffa_memory_attributes_t attributes;
617 /** Reserved field, must be 0. */
618 uint8_t reserved_0;
619 /** Flags to control behaviour of the transaction. */
620 ffa_memory_region_flags_t flags;
621 ffa_memory_handle_t handle;
622 /**
623 * An implementation defined value associated with the receiver and the
624 * memory region.
625 */
626 uint64_t tag;
627 /** Reserved field, must be 0. */
628 uint32_t reserved_1;
629 /**
630 * The number of `ffa_memory_access` entries included in this
631 * transaction.
632 */
633 uint32_t receiver_count;
634 /**
635 * An array of `attribute_count` endpoint memory access descriptors.
636 * Each one specifies a memory region offset, an endpoint and the
637 * attributes with which this memory region should be mapped in that
638 * endpoint's page table.
639 */
640 struct ffa_memory_access receivers[];
641};
642
643/**
644 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
645 * 150 of the FF-A 1.0 EAC specification, "Descriptor to relinquish a memory
646 * region".
647 */
648struct ffa_mem_relinquish {
649 ffa_memory_handle_t handle;
650 ffa_memory_region_flags_t flags;
651 uint32_t endpoint_count;
652 ffa_vm_id_t endpoints[];
653};
654
655/**
656 * Gets the `ffa_composite_memory_region` for the given receiver from an
657 * `ffa_memory_region`, or NULL if it is not valid.
658 */
659static inline struct ffa_composite_memory_region *
660ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
661 uint32_t receiver_index)
662{
663 uint32_t offset = memory_region->receivers[receiver_index]
664 .composite_memory_region_offset;
665
666 if (offset == 0) {
667 return NULL;
668 }
669
670 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
671 offset);
672}
673
674static inline uint32_t ffa_mem_relinquish_init(
675 struct ffa_mem_relinquish *relinquish_request,
676 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
677 ffa_vm_id_t sender)
678{
679 relinquish_request->handle = handle;
680 relinquish_request->flags = flags;
681 relinquish_request->endpoint_count = 1;
682 relinquish_request->endpoints[0] = sender;
683 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t);
684}
685
686uint32_t ffa_memory_region_init(
Andrew Walbranca808b12020-05-15 17:22:28 +0100687 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
688 ffa_vm_id_t sender, ffa_vm_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100689 const struct ffa_memory_region_constituent constituents[],
690 uint32_t constituent_count, uint32_t tag,
691 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
692 enum ffa_instruction_access instruction_access,
693 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +0100694 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
695 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100696uint32_t ffa_memory_retrieve_request_init(
697 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
698 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
699 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
700 enum ffa_instruction_access instruction_access,
701 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
702 enum ffa_memory_shareability shareability);
703uint32_t ffa_memory_lender_retrieve_request_init(
704 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
705 ffa_vm_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +0100706bool ffa_retrieved_memory_region_init(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100707 struct ffa_memory_region *response, size_t response_max_size,
708 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
709 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
710 ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100711 uint32_t page_count, uint32_t total_constituent_count,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100712 const struct ffa_memory_region_constituent constituents[],
Andrew Walbranca808b12020-05-15 17:22:28 +0100713 uint32_t fragment_constituent_count, uint32_t *total_length,
714 uint32_t *fragment_length);
715uint32_t ffa_memory_fragment_init(
716 struct ffa_memory_region_constituent *fragment,
717 size_t fragment_max_size,
718 const struct ffa_memory_region_constituent constituents[],
719 uint32_t constituent_count, uint32_t *fragment_length);