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