blob: 7668b9677082c2374f6ff16b7512a451a661e149 [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
Daniel Boulbyb46cad12021-12-13 17:47:21 +0000129/*
130 * Defined in Table 13.31 in the FF-A v1.1 BETA0 specification.
131 * The Partition count flag is used by FFA_PARTITION_INFO_GET to specify
132 * if partition info descriptors should be returned or just the count.
133 */
134#define FFA_PARTITION_COUNT_FLAG 0x1
135#define FFA_PARTITION_COUNT_FLAG_MASK 0x1
136
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100137/**
138 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
139 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
140 * configured to use.
141 */
142#define FFA_PAGE_SIZE 4096
143
144/* The maximum length possible for a single message. */
145#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
146
147enum ffa_data_access {
148 FFA_DATA_ACCESS_NOT_SPECIFIED,
149 FFA_DATA_ACCESS_RO,
150 FFA_DATA_ACCESS_RW,
151 FFA_DATA_ACCESS_RESERVED,
152};
153
154enum ffa_instruction_access {
155 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
156 FFA_INSTRUCTION_ACCESS_NX,
157 FFA_INSTRUCTION_ACCESS_X,
158 FFA_INSTRUCTION_ACCESS_RESERVED,
159};
160
161enum ffa_memory_type {
162 FFA_MEMORY_NOT_SPECIFIED_MEM,
163 FFA_MEMORY_DEVICE_MEM,
164 FFA_MEMORY_NORMAL_MEM,
165};
166
167enum ffa_memory_cacheability {
168 FFA_MEMORY_CACHE_RESERVED = 0x0,
169 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
170 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
171 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
172 FFA_MEMORY_DEV_NGNRNE = 0x0,
173 FFA_MEMORY_DEV_NGNRE = 0x1,
174 FFA_MEMORY_DEV_NGRE = 0x2,
175 FFA_MEMORY_DEV_GRE = 0x3,
176};
177
178enum ffa_memory_shareability {
179 FFA_MEMORY_SHARE_NON_SHAREABLE,
180 FFA_MEMORY_SHARE_RESERVED,
181 FFA_MEMORY_OUTER_SHAREABLE,
182 FFA_MEMORY_INNER_SHAREABLE,
183};
184
185typedef uint8_t ffa_memory_access_permissions_t;
186
187/**
188 * This corresponds to table 44 of the FF-A 1.0 EAC specification, "Memory
189 * region attributes descriptor".
190 */
191typedef uint8_t ffa_memory_attributes_t;
192
193#define FFA_DATA_ACCESS_OFFSET (0x0U)
194#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
195
196#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
197#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
198
199#define FFA_MEMORY_TYPE_OFFSET (0x4U)
200#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
201
202#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
203#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
204
205#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
206#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
207
208#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
209 static inline void ffa_set_##name##_attr(container_type *attr, \
210 const enum ffa_##name perm) \
211 { \
212 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
213 }
214
215#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
216 static inline enum ffa_##name ffa_get_##name##_attr( \
217 container_type attr) \
218 { \
219 return (enum ffa_##name)((attr & mask) >> offset); \
220 }
221
222ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
223 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
224ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
225 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
226
227ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
228 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
229ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
230 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
231
232ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
233 FFA_MEMORY_TYPE_MASK)
234ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
235 FFA_MEMORY_TYPE_MASK)
236
237ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
238 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
239ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
240 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
241
242ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
243 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
244ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
245 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
246
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100247/**
248 * A globally-unique ID assigned by the hypervisor for a region of memory being
249 * sent between VMs.
250 */
251typedef uint64_t ffa_memory_handle_t;
252
J-Alves917d2f22020-10-30 18:39:30 +0000253#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
254 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
255#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
256 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
257
258#define FFA_MEMORY_HANDLE_ALLOCATOR_SPMC (UINT64_C(0) << 63)
259#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
260
261/** The ID of a VM. These are assigned sequentially starting with an offset. */
262typedef uint16_t ffa_vm_id_t;
263
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100264/**
265 * A count of VMs. This has the same range as the VM IDs but we give it a
266 * different name to make the different semantics clear.
267 */
268typedef ffa_vm_id_t ffa_vm_count_t;
269
270/** The index of a vCPU within a particular VM. */
271typedef uint16_t ffa_vcpu_index_t;
272
273/**
274 * A count of vCPUs. This has the same range as the vCPU indices but we give it
275 * a different name to make the different semantics clear.
276 */
277typedef ffa_vcpu_index_t ffa_vcpu_count_t;
278
279/** Parameter and return type of FF-A functions. */
280struct ffa_value {
281 uint64_t func;
282 uint64_t arg1;
283 uint64_t arg2;
284 uint64_t arg3;
285 uint64_t arg4;
286 uint64_t arg5;
287 uint64_t arg6;
288 uint64_t arg7;
289};
290
Olivier Depreze562e542020-06-11 17:31:54 +0200291static inline uint32_t ffa_func_id(struct ffa_value args)
292{
293 return args.func;
294}
295
J-Alves13318e32021-02-22 17:21:00 +0000296static inline int32_t ffa_error_code(struct ffa_value val)
297{
298 return (int32_t)val.arg2;
299}
300
J-Alvesd6f4e142021-03-05 13:33:59 +0000301static inline ffa_vm_id_t ffa_sender(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100302{
303 return (args.arg1 >> 16) & 0xffff;
304}
305
J-Alvesd6f4e142021-03-05 13:33:59 +0000306static inline ffa_vm_id_t ffa_receiver(struct ffa_value args)
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100307{
308 return args.arg1 & 0xffff;
309}
310
311static inline uint32_t ffa_msg_send_size(struct ffa_value args)
312{
313 return args.arg3;
314}
315
316static inline uint32_t ffa_msg_send_attributes(struct ffa_value args)
317{
318 return args.arg4;
319}
320
Olivier Depreze562e542020-06-11 17:31:54 +0200321static inline uint32_t ffa_partition_info_get_count(struct ffa_value args)
322{
323 return args.arg2;
324}
325
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100326static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
327{
328 return (uint64_t)a1 | (uint64_t)a2 << 32;
329}
330
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100331static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
332{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100333 return ffa_assemble_handle(args.arg2, args.arg3);
334}
335
Andrew Walbranca808b12020-05-15 17:22:28 +0100336static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
337{
338 return ffa_assemble_handle(args.arg1, args.arg2);
339}
340
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100341static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
342{
343 return (struct ffa_value){.func = FFA_SUCCESS_32,
344 .arg2 = (uint32_t)handle,
345 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100346}
347
348static inline ffa_vm_id_t ffa_vm_id(struct ffa_value args)
349{
350 return (args.arg1 >> 16) & 0xffff;
351}
352
353static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
354{
355 return args.arg1 & 0xffff;
356}
357
358static inline uint64_t ffa_vm_vcpu(ffa_vm_id_t vm_id,
359 ffa_vcpu_index_t vcpu_index)
360{
361 return ((uint32_t)vm_id << 16) | vcpu_index;
362}
363
Andrew Walbranca808b12020-05-15 17:22:28 +0100364static inline ffa_vm_id_t ffa_frag_sender(struct ffa_value args)
365{
366 return (args.arg4 >> 16) & 0xffff;
367}
368
J-Alves6f72ca82021-11-01 12:34:58 +0000369static inline uint32_t ffa_feature_intid(struct ffa_value args)
370{
371 return (uint32_t)args.arg2;
372}
373
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100374/**
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100375 * Holds the UUID in a struct that is mappable directly to the SMCC calling
376 * convention, which is used for FF-A calls.
377 *
378 * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3
379 * of the SMCC Spec 1.2.
380 */
381struct ffa_uuid {
382 uint32_t uuid[4];
383};
384
385static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2,
386 uint32_t w3, struct ffa_uuid *uuid)
387{
388 uuid->uuid[0] = w0;
389 uuid->uuid[1] = w1;
390 uuid->uuid[2] = w2;
391 uuid->uuid[3] = w3;
392}
393
394static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1,
395 const struct ffa_uuid *uuid2)
396{
397 return (uuid1->uuid[0] == uuid2->uuid[0]) &&
398 (uuid1->uuid[1] == uuid2->uuid[1]) &&
399 (uuid1->uuid[2] == uuid2->uuid[2]) &&
400 (uuid1->uuid[3] == uuid2->uuid[3]);
401}
402
403static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid)
404{
405 return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) &&
406 (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0);
407}
408
409/**
410 * Flags to determine the partition properties, as required by
411 * FFA_PARTITION_INFO_GET.
412 *
413 * The values of the flags are specified in table 82 of the FF-A 1.0 EAC
414 * specification, "Partition information descriptor, partition properties".
415 */
416typedef uint32_t ffa_partition_properties_t;
417
418/** Partition property: partition supports receipt of direct requests. */
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100419#define FFA_PARTITION_DIRECT_REQ_RECV 0x1
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100420
421/** Partition property: partition can send direct requests. */
Maksims Svecovsb596eab2021-04-27 00:52:27 +0100422#define FFA_PARTITION_DIRECT_REQ_SEND 0x2
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100423
424/** Partition property: partition can send and receive indirect messages. */
425#define FFA_PARTITION_INDIRECT_MSG 0x4
426
J-Alves09ff9d82021-11-02 11:55:20 +0000427/** Partition property: partition can receive notifications. */
428#define FFA_PARTITION_NOTIFICATION 0x8
429
Fuad Tabbae4efcc32020-07-16 15:37:27 +0100430/**
431 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET
432 * interface.
433 * This corresponds to table 82 of the FF-A 1.0 EAC specification, "Partition
434 * information descriptor".
435 */
436struct ffa_partition_info {
437 ffa_vm_id_t vm_id;
438 ffa_vcpu_count_t vcpu_count;
439 ffa_partition_properties_t properties;
440};
441
442/**
J-Alves980d1992021-03-18 12:49:18 +0000443 * FF-A v1.1 specification restricts the number of notifications to a maximum
444 * of 64. Following all possible bitmaps.
445 */
446#define FFA_NOTIFICATION_MASK(ID) (UINT64_C(1) << ID)
447
448typedef uint64_t ffa_notifications_bitmap_t;
449
450#define MAX_FFA_NOTIFICATIONS 64U
451
452/**
J-Alvesc003a7a2021-03-18 13:06:53 +0000453 * Flag for notification bind and set, to specify call is about per-vCPU
454 * notifications.
455 */
456#define FFA_NOTIFICATION_FLAG_PER_VCPU UINT32_C(1 << 0)
457
458/**
J-Alves980d1992021-03-18 12:49:18 +0000459 * Helper function to assemble a 64-bit sized bitmap, from the 32-bit sized lo
460 * and hi.
461 * Helpful as FF-A specification defines that the notifications interfaces
462 * arguments are 32-bit registers.
463 */
464static inline ffa_notifications_bitmap_t ffa_notifications_bitmap(uint32_t lo,
465 uint32_t hi)
466{
467 return (ffa_notifications_bitmap_t)hi << 32U | lo;
468}
469
J-Alves98ff9562021-09-09 14:39:41 +0100470static inline ffa_notifications_bitmap_t ffa_notification_get_from_sp(
471 struct ffa_value val)
472{
473 return ffa_notifications_bitmap((uint32_t)val.arg2, (uint32_t)val.arg3);
474}
475
476static inline ffa_notifications_bitmap_t ffa_notification_get_from_vm(
477 struct ffa_value val)
478{
479 return ffa_notifications_bitmap((uint32_t)val.arg4, (uint32_t)val.arg5);
480}
481
J-Alves980d1992021-03-18 12:49:18 +0000482/**
J-Alvesaa79c012021-07-09 14:29:45 +0100483 * Flags used in calls to FFA_NOTIFICATION_GET interface.
484 */
485#define FFA_NOTIFICATION_FLAG_BITMAP_SP UINT32_C(0x1 << 0)
486#define FFA_NOTIFICATION_FLAG_BITMAP_VM UINT32_C(0x1 << 1)
487#define FFA_NOTIFICATION_FLAG_BITMAP_SPM UINT32_C(0x1 << 2)
488#define FFA_NOTIFICATION_FLAG_BITMAP_HYP UINT32_C(0x1 << 3)
489
J-Alves13394022021-06-30 13:48:49 +0100490/** Flag for FFA_NOTIFICATION_SET to delay Schedule Receiver Interrupt */
491#define FFA_NOTIFICATIONS_FLAG_DELAY_SRI UINT32_C(0x1 << 1)
492
J-Alvesbe6e3032021-11-30 14:54:12 +0000493static inline ffa_vcpu_index_t ffa_notifications_get_vcpu(struct ffa_value args)
J-Alvesaa79c012021-07-09 14:29:45 +0100494{
J-Alvesbe6e3032021-11-30 14:54:12 +0000495 return (ffa_vcpu_index_t)(args.arg1 >> 16 & 0xffffU);
J-Alvesaa79c012021-07-09 14:29:45 +0100496}
497
498/**
J-Alvesc8e8a222021-06-08 17:33:52 +0100499 * The max number of IDs for return of FFA_NOTIFICATION_INFO_GET.
500 */
501#define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U
502
503/**
504 * Number of registers to use in successfull return of interface
505 * FFA_NOTIFICATION_INFO_GET.
506 */
507#define FFA_NOTIFICATIONS_INFO_GET_REGS_RET 5U
508
509#define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING 0x1U
510
511/**
512 * Helper macros for return parameter encoding as described in section 17.7.1
513 * of the FF-A v1.1 Beta0 specification.
514 */
515#define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U
516#define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1fU
517#define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12)
518#define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U
519
520static inline uint32_t ffa_notification_info_get_lists_count(
521 struct ffa_value args)
522{
523 return (uint32_t)(args.arg2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) &
524 FFA_NOTIFICATIONS_LISTS_COUNT_MASK;
525}
526
527static inline uint32_t ffa_notification_info_get_list_size(
528 struct ffa_value args, unsigned int list_idx)
529{
530 return ((uint32_t)args.arg2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list_idx)) &
531 FFA_NOTIFICATIONS_LIST_SIZE_MASK;
532}
533
534static inline bool ffa_notification_info_get_more_pending(struct ffa_value args)
535{
536 return (args.arg2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U;
537}
538
539/**
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100540 * A set of contiguous pages which is part of a memory region. This corresponds
541 * to table 40 of the FF-A 1.0 EAC specification, "Constituent memory region
542 * descriptor".
543 */
544struct ffa_memory_region_constituent {
545 /**
546 * The base IPA of the constituent memory region, aligned to 4 kiB page
547 * size granularity.
548 */
549 uint64_t address;
550 /** The number of 4 kiB pages in the constituent memory region. */
551 uint32_t page_count;
552 /** Reserved field, must be 0. */
553 uint32_t reserved;
554};
555
556/**
557 * A set of pages comprising a memory region. This corresponds to table 39 of
558 * the FF-A 1.0 EAC specification, "Composite memory region descriptor".
559 */
560struct ffa_composite_memory_region {
561 /**
562 * The total number of 4 kiB pages included in this memory region. This
563 * must be equal to the sum of page counts specified in each
564 * `ffa_memory_region_constituent`.
565 */
566 uint32_t page_count;
567 /**
568 * The number of constituents (`ffa_memory_region_constituent`)
569 * included in this memory region range.
570 */
571 uint32_t constituent_count;
572 /** Reserved field, must be 0. */
573 uint64_t reserved_0;
574 /** An array of `constituent_count` memory region constituents. */
575 struct ffa_memory_region_constituent constituents[];
576};
577
578/** Flags to indicate properties of receivers during memory region retrieval. */
579typedef uint8_t ffa_memory_receiver_flags_t;
580
581/**
582 * This corresponds to table 41 of the FF-A 1.0 EAC specification, "Memory
583 * access permissions descriptor".
584 */
585struct ffa_memory_region_attributes {
586 /** The ID of the VM to which the memory is being given or shared. */
587 ffa_vm_id_t receiver;
588 /**
589 * The permissions with which the memory region should be mapped in the
590 * receiver's page table.
591 */
592 ffa_memory_access_permissions_t permissions;
593 /**
594 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
595 * for memory regions with multiple borrowers.
596 */
597 ffa_memory_receiver_flags_t flags;
598};
599
600/** Flags to control the behaviour of a memory sharing transaction. */
601typedef uint32_t ffa_memory_region_flags_t;
602
603/**
604 * Clear memory region contents after unmapping it from the sender and before
605 * mapping it for any receiver.
606 */
607#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
608
609/**
610 * Whether the hypervisor may time slice the memory sharing or retrieval
611 * operation.
612 */
613#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
614
615/**
616 * Whether the hypervisor should clear the memory region after the receiver
617 * relinquishes it or is aborted.
618 */
619#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
620
621#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
622#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
623#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
624#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
625#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
626
Federico Recanati85090c42021-12-15 13:17:54 +0100627#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_VALID ((0x1U) << 9)
628#define FFA_MEMORY_REGION_ADDRESS_RANGE_HINT_MASK ((0xFU) << 5)
629
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100630/**
631 * This corresponds to table 42 of the FF-A 1.0 EAC specification, "Endpoint
632 * memory access descriptor".
633 */
634struct ffa_memory_access {
635 struct ffa_memory_region_attributes receiver_permissions;
636 /**
637 * Offset in bytes from the start of the outer `ffa_memory_region` to
638 * an `ffa_composite_memory_region` struct.
639 */
640 uint32_t composite_memory_region_offset;
641 uint64_t reserved_0;
642};
643
644/**
645 * Information about a set of pages which are being shared. This corresponds to
646 * table 45 of the FF-A 1.0 EAC specification, "Lend, donate or share memory
647 * transaction descriptor". Note that it is also used for retrieve requests and
648 * responses.
649 */
650struct ffa_memory_region {
651 /**
652 * The ID of the VM which originally sent the memory region, i.e. the
653 * owner.
654 */
655 ffa_vm_id_t sender;
656 ffa_memory_attributes_t attributes;
657 /** Reserved field, must be 0. */
658 uint8_t reserved_0;
659 /** Flags to control behaviour of the transaction. */
660 ffa_memory_region_flags_t flags;
661 ffa_memory_handle_t handle;
662 /**
663 * An implementation defined value associated with the receiver and the
664 * memory region.
665 */
666 uint64_t tag;
667 /** Reserved field, must be 0. */
668 uint32_t reserved_1;
669 /**
670 * The number of `ffa_memory_access` entries included in this
671 * transaction.
672 */
673 uint32_t receiver_count;
674 /**
Federico Recanati3ddf28e2021-12-20 09:40:26 +0100675 * An array of `receiver_count` endpoint memory access descriptors.
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100676 * Each one specifies a memory region offset, an endpoint and the
677 * attributes with which this memory region should be mapped in that
678 * endpoint's page table.
679 */
680 struct ffa_memory_access receivers[];
681};
682
683/**
684 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
685 * 150 of the FF-A 1.0 EAC specification, "Descriptor to relinquish a memory
686 * region".
687 */
688struct ffa_mem_relinquish {
689 ffa_memory_handle_t handle;
690 ffa_memory_region_flags_t flags;
691 uint32_t endpoint_count;
692 ffa_vm_id_t endpoints[];
693};
694
695/**
696 * Gets the `ffa_composite_memory_region` for the given receiver from an
697 * `ffa_memory_region`, or NULL if it is not valid.
698 */
699static inline struct ffa_composite_memory_region *
700ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
701 uint32_t receiver_index)
702{
703 uint32_t offset = memory_region->receivers[receiver_index]
704 .composite_memory_region_offset;
705
706 if (offset == 0) {
707 return NULL;
708 }
709
710 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
711 offset);
712}
713
714static inline uint32_t ffa_mem_relinquish_init(
715 struct ffa_mem_relinquish *relinquish_request,
716 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
717 ffa_vm_id_t sender)
718{
719 relinquish_request->handle = handle;
720 relinquish_request->flags = flags;
721 relinquish_request->endpoint_count = 1;
722 relinquish_request->endpoints[0] = sender;
723 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t);
724}
725
726uint32_t ffa_memory_region_init(
Andrew Walbranca808b12020-05-15 17:22:28 +0100727 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
728 ffa_vm_id_t sender, ffa_vm_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100729 const struct ffa_memory_region_constituent constituents[],
730 uint32_t constituent_count, uint32_t tag,
731 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
732 enum ffa_instruction_access instruction_access,
733 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +0100734 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
735 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100736uint32_t ffa_memory_retrieve_request_init(
737 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
738 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
739 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
740 enum ffa_instruction_access instruction_access,
741 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
742 enum ffa_memory_shareability shareability);
743uint32_t ffa_memory_lender_retrieve_request_init(
744 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
745 ffa_vm_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +0100746bool ffa_retrieved_memory_region_init(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100747 struct ffa_memory_region *response, size_t response_max_size,
748 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
749 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
750 ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100751 uint32_t page_count, uint32_t total_constituent_count,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100752 const struct ffa_memory_region_constituent constituents[],
Andrew Walbranca808b12020-05-15 17:22:28 +0100753 uint32_t fragment_constituent_count, uint32_t *total_length,
754 uint32_t *fragment_length);
755uint32_t ffa_memory_fragment_init(
756 struct ffa_memory_region_constituent *fragment,
757 size_t fragment_max_size,
758 const struct ffa_memory_region_constituent constituents[],
759 uint32_t constituent_count, uint32_t *fragment_length);