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