blob: 02c07778bc9337680394d0581d31222da0e309d1 [file] [log] [blame]
Andrew Walbranb5ab43c2020-04-30 11:32:54 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
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
14#define FFA_VERSION_MINOR 0x0
15
16#define FFA_VERSION_MAJOR_OFFSET 16
17
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010018/* clang-format off */
19
20#define FFA_LOW_32_ID 0x84000060
21#define FFA_HIGH_32_ID 0x8400007F
22#define FFA_LOW_64_ID 0xC4000060
23#define FFA_HIGH_32_ID 0x8400007F
24
25/* FF-A function identifiers. */
26#define FFA_ERROR_32 0x84000060
27#define FFA_SUCCESS_32 0x84000061
28#define FFA_INTERRUPT_32 0x84000062
29#define FFA_VERSION_32 0x84000063
30#define FFA_FEATURES_32 0x84000064
31#define FFA_RX_RELEASE_32 0x84000065
32#define FFA_RXTX_MAP_32 0x84000066
33#define FFA_RXTX_MAP_64 0xC4000066
34#define FFA_RXTX_UNMAP_32 0x84000067
35#define FFA_PARTITION_INFO_GET_32 0x84000068
36#define FFA_ID_GET_32 0x84000069
37#define FFA_MSG_POLL_32 0x8400006A
38#define FFA_MSG_WAIT_32 0x8400006B
39#define FFA_YIELD_32 0x8400006C
40#define FFA_RUN_32 0x8400006D
41#define FFA_MSG_SEND_32 0x8400006E
42#define FFA_MSG_SEND_DIRECT_REQ_32 0x8400006F
43#define FFA_MSG_SEND_DIRECT_RESP_32 0x84000070
44#define FFA_MEM_DONATE_32 0x84000071
45#define FFA_MEM_LEND_32 0x84000072
46#define FFA_MEM_SHARE_32 0x84000073
47#define FFA_MEM_RETRIEVE_REQ_32 0x84000074
48#define FFA_MEM_RETRIEVE_RESP_32 0x84000075
49#define FFA_MEM_RELINQUISH_32 0x84000076
50#define FFA_MEM_RECLAIM_32 0x84000077
Andrew Walbranca808b12020-05-15 17:22:28 +010051#define FFA_MEM_FRAG_RX_32 0x8400007A
52#define FFA_MEM_FRAG_TX_32 0x8400007B
Andrew Walbranb5ab43c2020-04-30 11:32:54 +010053
54/* FF-A error codes. */
55#define FFA_NOT_SUPPORTED INT32_C(-1)
56#define FFA_INVALID_PARAMETERS INT32_C(-2)
57#define FFA_NO_MEMORY INT32_C(-3)
58#define FFA_BUSY INT32_C(-4)
59#define FFA_INTERRUPTED INT32_C(-5)
60#define FFA_DENIED INT32_C(-6)
61#define FFA_RETRY INT32_C(-7)
62#define FFA_ABORTED INT32_C(-8)
63
64/* clang-format on */
65
66/* FF-A function specific constants. */
67#define FFA_MSG_RECV_BLOCK 0x1
68#define FFA_MSG_RECV_BLOCK_MASK 0x1
69
70#define FFA_MSG_SEND_NOTIFY 0x1
71#define FFA_MSG_SEND_NOTIFY_MASK 0x1
72
73#define FFA_MEM_RECLAIM_CLEAR 0x1
74
75#define FFA_SLEEP_INDEFINITE 0
76
77/**
78 * For use where the FF-A specification refers explicitly to '4K pages'. Not to
79 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
80 * configured to use.
81 */
82#define FFA_PAGE_SIZE 4096
83
84/* The maximum length possible for a single message. */
85#define FFA_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
86
87enum ffa_data_access {
88 FFA_DATA_ACCESS_NOT_SPECIFIED,
89 FFA_DATA_ACCESS_RO,
90 FFA_DATA_ACCESS_RW,
91 FFA_DATA_ACCESS_RESERVED,
92};
93
94enum ffa_instruction_access {
95 FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED,
96 FFA_INSTRUCTION_ACCESS_NX,
97 FFA_INSTRUCTION_ACCESS_X,
98 FFA_INSTRUCTION_ACCESS_RESERVED,
99};
100
101enum ffa_memory_type {
102 FFA_MEMORY_NOT_SPECIFIED_MEM,
103 FFA_MEMORY_DEVICE_MEM,
104 FFA_MEMORY_NORMAL_MEM,
105};
106
107enum ffa_memory_cacheability {
108 FFA_MEMORY_CACHE_RESERVED = 0x0,
109 FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1,
110 FFA_MEMORY_CACHE_RESERVED_1 = 0x2,
111 FFA_MEMORY_CACHE_WRITE_BACK = 0x3,
112 FFA_MEMORY_DEV_NGNRNE = 0x0,
113 FFA_MEMORY_DEV_NGNRE = 0x1,
114 FFA_MEMORY_DEV_NGRE = 0x2,
115 FFA_MEMORY_DEV_GRE = 0x3,
116};
117
118enum ffa_memory_shareability {
119 FFA_MEMORY_SHARE_NON_SHAREABLE,
120 FFA_MEMORY_SHARE_RESERVED,
121 FFA_MEMORY_OUTER_SHAREABLE,
122 FFA_MEMORY_INNER_SHAREABLE,
123};
124
125typedef uint8_t ffa_memory_access_permissions_t;
126
127/**
128 * This corresponds to table 44 of the FF-A 1.0 EAC specification, "Memory
129 * region attributes descriptor".
130 */
131typedef uint8_t ffa_memory_attributes_t;
132
133#define FFA_DATA_ACCESS_OFFSET (0x0U)
134#define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET)
135
136#define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U)
137#define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET)
138
139#define FFA_MEMORY_TYPE_OFFSET (0x4U)
140#define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET)
141
142#define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U)
143#define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET)
144
145#define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U)
146#define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET)
147
148#define ATTR_FUNCTION_SET(name, container_type, offset, mask) \
149 static inline void ffa_set_##name##_attr(container_type *attr, \
150 const enum ffa_##name perm) \
151 { \
152 *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \
153 }
154
155#define ATTR_FUNCTION_GET(name, container_type, offset, mask) \
156 static inline enum ffa_##name ffa_get_##name##_attr( \
157 container_type attr) \
158 { \
159 return (enum ffa_##name)((attr & mask) >> offset); \
160 }
161
162ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t,
163 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
164ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t,
165 FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK)
166
167ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t,
168 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
169ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t,
170 FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK)
171
172ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
173 FFA_MEMORY_TYPE_MASK)
174ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET,
175 FFA_MEMORY_TYPE_MASK)
176
177ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t,
178 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
179ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t,
180 FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK)
181
182ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t,
183 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
184ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t,
185 FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK)
186
187#define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \
188 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
189#define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \
190 ((ffa_memory_handle_t)(UINT64_C(1) << 63))
Andrew Walbranca808b12020-05-15 17:22:28 +0100191#define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0))
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100192
193/** The ID of a VM. These are assigned sequentially starting with an offset. */
194typedef uint16_t ffa_vm_id_t;
195
196/**
197 * A globally-unique ID assigned by the hypervisor for a region of memory being
198 * sent between VMs.
199 */
200typedef uint64_t ffa_memory_handle_t;
201
202/**
203 * A count of VMs. This has the same range as the VM IDs but we give it a
204 * different name to make the different semantics clear.
205 */
206typedef ffa_vm_id_t ffa_vm_count_t;
207
208/** The index of a vCPU within a particular VM. */
209typedef uint16_t ffa_vcpu_index_t;
210
211/**
212 * A count of vCPUs. This has the same range as the vCPU indices but we give it
213 * a different name to make the different semantics clear.
214 */
215typedef ffa_vcpu_index_t ffa_vcpu_count_t;
216
217/** Parameter and return type of FF-A functions. */
218struct ffa_value {
219 uint64_t func;
220 uint64_t arg1;
221 uint64_t arg2;
222 uint64_t arg3;
223 uint64_t arg4;
224 uint64_t arg5;
225 uint64_t arg6;
226 uint64_t arg7;
227};
228
229static inline ffa_vm_id_t ffa_msg_send_sender(struct ffa_value args)
230{
231 return (args.arg1 >> 16) & 0xffff;
232}
233
234static inline ffa_vm_id_t ffa_msg_send_receiver(struct ffa_value args)
235{
236 return args.arg1 & 0xffff;
237}
238
239static inline uint32_t ffa_msg_send_size(struct ffa_value args)
240{
241 return args.arg3;
242}
243
244static inline uint32_t ffa_msg_send_attributes(struct ffa_value args)
245{
246 return args.arg4;
247}
248
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100249static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t a1, uint32_t a2)
250{
251 return (uint64_t)a1 | (uint64_t)a2 << 32;
252}
253
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100254static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
255{
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100256 return ffa_assemble_handle(args.arg2, args.arg3);
257}
258
Andrew Walbranca808b12020-05-15 17:22:28 +0100259static inline ffa_memory_handle_t ffa_frag_handle(struct ffa_value args)
260{
261 return ffa_assemble_handle(args.arg1, args.arg2);
262}
263
Andrew Walbran1bbe9402020-04-30 16:47:13 +0100264static inline struct ffa_value ffa_mem_success(ffa_memory_handle_t handle)
265{
266 return (struct ffa_value){.func = FFA_SUCCESS_32,
267 .arg2 = (uint32_t)handle,
268 .arg3 = (uint32_t)(handle >> 32)};
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100269}
270
271static inline ffa_vm_id_t ffa_vm_id(struct ffa_value args)
272{
273 return (args.arg1 >> 16) & 0xffff;
274}
275
276static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
277{
278 return args.arg1 & 0xffff;
279}
280
281static inline uint64_t ffa_vm_vcpu(ffa_vm_id_t vm_id,
282 ffa_vcpu_index_t vcpu_index)
283{
284 return ((uint32_t)vm_id << 16) | vcpu_index;
285}
286
Andrew Walbranca808b12020-05-15 17:22:28 +0100287static inline ffa_vm_id_t ffa_frag_sender(struct ffa_value args)
288{
289 return (args.arg4 >> 16) & 0xffff;
290}
291
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100292/**
293 * A set of contiguous pages which is part of a memory region. This corresponds
294 * to table 40 of the FF-A 1.0 EAC specification, "Constituent memory region
295 * descriptor".
296 */
297struct ffa_memory_region_constituent {
298 /**
299 * The base IPA of the constituent memory region, aligned to 4 kiB page
300 * size granularity.
301 */
302 uint64_t address;
303 /** The number of 4 kiB pages in the constituent memory region. */
304 uint32_t page_count;
305 /** Reserved field, must be 0. */
306 uint32_t reserved;
307};
308
309/**
310 * A set of pages comprising a memory region. This corresponds to table 39 of
311 * the FF-A 1.0 EAC specification, "Composite memory region descriptor".
312 */
313struct ffa_composite_memory_region {
314 /**
315 * The total number of 4 kiB pages included in this memory region. This
316 * must be equal to the sum of page counts specified in each
317 * `ffa_memory_region_constituent`.
318 */
319 uint32_t page_count;
320 /**
321 * The number of constituents (`ffa_memory_region_constituent`)
322 * included in this memory region range.
323 */
324 uint32_t constituent_count;
325 /** Reserved field, must be 0. */
326 uint64_t reserved_0;
327 /** An array of `constituent_count` memory region constituents. */
328 struct ffa_memory_region_constituent constituents[];
329};
330
331/** Flags to indicate properties of receivers during memory region retrieval. */
332typedef uint8_t ffa_memory_receiver_flags_t;
333
334/**
335 * This corresponds to table 41 of the FF-A 1.0 EAC specification, "Memory
336 * access permissions descriptor".
337 */
338struct ffa_memory_region_attributes {
339 /** The ID of the VM to which the memory is being given or shared. */
340 ffa_vm_id_t receiver;
341 /**
342 * The permissions with which the memory region should be mapped in the
343 * receiver's page table.
344 */
345 ffa_memory_access_permissions_t permissions;
346 /**
347 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
348 * for memory regions with multiple borrowers.
349 */
350 ffa_memory_receiver_flags_t flags;
351};
352
353/** Flags to control the behaviour of a memory sharing transaction. */
354typedef uint32_t ffa_memory_region_flags_t;
355
356/**
357 * Clear memory region contents after unmapping it from the sender and before
358 * mapping it for any receiver.
359 */
360#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
361
362/**
363 * Whether the hypervisor may time slice the memory sharing or retrieval
364 * operation.
365 */
366#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
367
368/**
369 * Whether the hypervisor should clear the memory region after the receiver
370 * relinquishes it or is aborted.
371 */
372#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
373
374#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
375#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
376#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
377#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
378#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
379
380/**
381 * This corresponds to table 42 of the FF-A 1.0 EAC specification, "Endpoint
382 * memory access descriptor".
383 */
384struct ffa_memory_access {
385 struct ffa_memory_region_attributes receiver_permissions;
386 /**
387 * Offset in bytes from the start of the outer `ffa_memory_region` to
388 * an `ffa_composite_memory_region` struct.
389 */
390 uint32_t composite_memory_region_offset;
391 uint64_t reserved_0;
392};
393
394/**
395 * Information about a set of pages which are being shared. This corresponds to
396 * table 45 of the FF-A 1.0 EAC specification, "Lend, donate or share memory
397 * transaction descriptor". Note that it is also used for retrieve requests and
398 * responses.
399 */
400struct ffa_memory_region {
401 /**
402 * The ID of the VM which originally sent the memory region, i.e. the
403 * owner.
404 */
405 ffa_vm_id_t sender;
406 ffa_memory_attributes_t attributes;
407 /** Reserved field, must be 0. */
408 uint8_t reserved_0;
409 /** Flags to control behaviour of the transaction. */
410 ffa_memory_region_flags_t flags;
411 ffa_memory_handle_t handle;
412 /**
413 * An implementation defined value associated with the receiver and the
414 * memory region.
415 */
416 uint64_t tag;
417 /** Reserved field, must be 0. */
418 uint32_t reserved_1;
419 /**
420 * The number of `ffa_memory_access` entries included in this
421 * transaction.
422 */
423 uint32_t receiver_count;
424 /**
425 * An array of `attribute_count` endpoint memory access descriptors.
426 * Each one specifies a memory region offset, an endpoint and the
427 * attributes with which this memory region should be mapped in that
428 * endpoint's page table.
429 */
430 struct ffa_memory_access receivers[];
431};
432
433/**
434 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
435 * 150 of the FF-A 1.0 EAC specification, "Descriptor to relinquish a memory
436 * region".
437 */
438struct ffa_mem_relinquish {
439 ffa_memory_handle_t handle;
440 ffa_memory_region_flags_t flags;
441 uint32_t endpoint_count;
442 ffa_vm_id_t endpoints[];
443};
444
445/**
446 * Gets the `ffa_composite_memory_region` for the given receiver from an
447 * `ffa_memory_region`, or NULL if it is not valid.
448 */
449static inline struct ffa_composite_memory_region *
450ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
451 uint32_t receiver_index)
452{
453 uint32_t offset = memory_region->receivers[receiver_index]
454 .composite_memory_region_offset;
455
456 if (offset == 0) {
457 return NULL;
458 }
459
460 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
461 offset);
462}
463
464static inline uint32_t ffa_mem_relinquish_init(
465 struct ffa_mem_relinquish *relinquish_request,
466 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
467 ffa_vm_id_t sender)
468{
469 relinquish_request->handle = handle;
470 relinquish_request->flags = flags;
471 relinquish_request->endpoint_count = 1;
472 relinquish_request->endpoints[0] = sender;
473 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t);
474}
475
476uint32_t ffa_memory_region_init(
Andrew Walbranca808b12020-05-15 17:22:28 +0100477 struct ffa_memory_region *memory_region, size_t memory_region_max_size,
478 ffa_vm_id_t sender, ffa_vm_id_t receiver,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100479 const struct ffa_memory_region_constituent constituents[],
480 uint32_t constituent_count, uint32_t tag,
481 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
482 enum ffa_instruction_access instruction_access,
483 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
Andrew Walbranca808b12020-05-15 17:22:28 +0100484 enum ffa_memory_shareability shareability, uint32_t *fragment_length,
485 uint32_t *total_length);
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100486uint32_t ffa_memory_retrieve_request_init(
487 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
488 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
489 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
490 enum ffa_instruction_access instruction_access,
491 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
492 enum ffa_memory_shareability shareability);
493uint32_t ffa_memory_lender_retrieve_request_init(
494 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
495 ffa_vm_id_t sender);
Andrew Walbranca808b12020-05-15 17:22:28 +0100496bool ffa_retrieved_memory_region_init(
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100497 struct ffa_memory_region *response, size_t response_max_size,
498 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
499 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
500 ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions,
Andrew Walbranca808b12020-05-15 17:22:28 +0100501 uint32_t page_count, uint32_t total_constituent_count,
Andrew Walbranb5ab43c2020-04-30 11:32:54 +0100502 const struct ffa_memory_region_constituent constituents[],
Andrew Walbranca808b12020-05-15 17:22:28 +0100503 uint32_t fragment_constituent_count, uint32_t *total_length,
504 uint32_t *fragment_length);
505uint32_t ffa_memory_fragment_init(
506 struct ffa_memory_region_constituent *fragment,
507 size_t fragment_max_size,
508 const struct ffa_memory_region_constituent constituents[],
509 uint32_t constituent_count, uint32_t *fragment_length);