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