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