blob: bd93f0cef5e450797cf92c01829aa3d323a3a6a9 [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
249static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args)
250{
251 return args.arg2;
252}
253
254static inline ffa_vm_id_t ffa_vm_id(struct ffa_value args)
255{
256 return (args.arg1 >> 16) & 0xffff;
257}
258
259static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args)
260{
261 return args.arg1 & 0xffff;
262}
263
264static inline uint64_t ffa_vm_vcpu(ffa_vm_id_t vm_id,
265 ffa_vcpu_index_t vcpu_index)
266{
267 return ((uint32_t)vm_id << 16) | vcpu_index;
268}
269
270/**
271 * A set of contiguous pages which is part of a memory region. This corresponds
272 * to table 40 of the FF-A 1.0 EAC specification, "Constituent memory region
273 * descriptor".
274 */
275struct ffa_memory_region_constituent {
276 /**
277 * The base IPA of the constituent memory region, aligned to 4 kiB page
278 * size granularity.
279 */
280 uint64_t address;
281 /** The number of 4 kiB pages in the constituent memory region. */
282 uint32_t page_count;
283 /** Reserved field, must be 0. */
284 uint32_t reserved;
285};
286
287/**
288 * A set of pages comprising a memory region. This corresponds to table 39 of
289 * the FF-A 1.0 EAC specification, "Composite memory region descriptor".
290 */
291struct ffa_composite_memory_region {
292 /**
293 * The total number of 4 kiB pages included in this memory region. This
294 * must be equal to the sum of page counts specified in each
295 * `ffa_memory_region_constituent`.
296 */
297 uint32_t page_count;
298 /**
299 * The number of constituents (`ffa_memory_region_constituent`)
300 * included in this memory region range.
301 */
302 uint32_t constituent_count;
303 /** Reserved field, must be 0. */
304 uint64_t reserved_0;
305 /** An array of `constituent_count` memory region constituents. */
306 struct ffa_memory_region_constituent constituents[];
307};
308
309/** Flags to indicate properties of receivers during memory region retrieval. */
310typedef uint8_t ffa_memory_receiver_flags_t;
311
312/**
313 * This corresponds to table 41 of the FF-A 1.0 EAC specification, "Memory
314 * access permissions descriptor".
315 */
316struct ffa_memory_region_attributes {
317 /** The ID of the VM to which the memory is being given or shared. */
318 ffa_vm_id_t receiver;
319 /**
320 * The permissions with which the memory region should be mapped in the
321 * receiver's page table.
322 */
323 ffa_memory_access_permissions_t permissions;
324 /**
325 * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP
326 * for memory regions with multiple borrowers.
327 */
328 ffa_memory_receiver_flags_t flags;
329};
330
331/** Flags to control the behaviour of a memory sharing transaction. */
332typedef uint32_t ffa_memory_region_flags_t;
333
334/**
335 * Clear memory region contents after unmapping it from the sender and before
336 * mapping it for any receiver.
337 */
338#define FFA_MEMORY_REGION_FLAG_CLEAR 0x1
339
340/**
341 * Whether the hypervisor may time slice the memory sharing or retrieval
342 * operation.
343 */
344#define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2
345
346/**
347 * Whether the hypervisor should clear the memory region after the receiver
348 * relinquishes it or is aborted.
349 */
350#define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4
351
352#define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3)
353#define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3)
354#define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3)
355#define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3)
356#define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3)
357
358/**
359 * This corresponds to table 42 of the FF-A 1.0 EAC specification, "Endpoint
360 * memory access descriptor".
361 */
362struct ffa_memory_access {
363 struct ffa_memory_region_attributes receiver_permissions;
364 /**
365 * Offset in bytes from the start of the outer `ffa_memory_region` to
366 * an `ffa_composite_memory_region` struct.
367 */
368 uint32_t composite_memory_region_offset;
369 uint64_t reserved_0;
370};
371
372/**
373 * Information about a set of pages which are being shared. This corresponds to
374 * table 45 of the FF-A 1.0 EAC specification, "Lend, donate or share memory
375 * transaction descriptor". Note that it is also used for retrieve requests and
376 * responses.
377 */
378struct ffa_memory_region {
379 /**
380 * The ID of the VM which originally sent the memory region, i.e. the
381 * owner.
382 */
383 ffa_vm_id_t sender;
384 ffa_memory_attributes_t attributes;
385 /** Reserved field, must be 0. */
386 uint8_t reserved_0;
387 /** Flags to control behaviour of the transaction. */
388 ffa_memory_region_flags_t flags;
389 ffa_memory_handle_t handle;
390 /**
391 * An implementation defined value associated with the receiver and the
392 * memory region.
393 */
394 uint64_t tag;
395 /** Reserved field, must be 0. */
396 uint32_t reserved_1;
397 /**
398 * The number of `ffa_memory_access` entries included in this
399 * transaction.
400 */
401 uint32_t receiver_count;
402 /**
403 * An array of `attribute_count` endpoint memory access descriptors.
404 * Each one specifies a memory region offset, an endpoint and the
405 * attributes with which this memory region should be mapped in that
406 * endpoint's page table.
407 */
408 struct ffa_memory_access receivers[];
409};
410
411/**
412 * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table
413 * 150 of the FF-A 1.0 EAC specification, "Descriptor to relinquish a memory
414 * region".
415 */
416struct ffa_mem_relinquish {
417 ffa_memory_handle_t handle;
418 ffa_memory_region_flags_t flags;
419 uint32_t endpoint_count;
420 ffa_vm_id_t endpoints[];
421};
422
423/**
424 * Gets the `ffa_composite_memory_region` for the given receiver from an
425 * `ffa_memory_region`, or NULL if it is not valid.
426 */
427static inline struct ffa_composite_memory_region *
428ffa_memory_region_get_composite(struct ffa_memory_region *memory_region,
429 uint32_t receiver_index)
430{
431 uint32_t offset = memory_region->receivers[receiver_index]
432 .composite_memory_region_offset;
433
434 if (offset == 0) {
435 return NULL;
436 }
437
438 return (struct ffa_composite_memory_region *)((uint8_t *)memory_region +
439 offset);
440}
441
442static inline uint32_t ffa_mem_relinquish_init(
443 struct ffa_mem_relinquish *relinquish_request,
444 ffa_memory_handle_t handle, ffa_memory_region_flags_t flags,
445 ffa_vm_id_t sender)
446{
447 relinquish_request->handle = handle;
448 relinquish_request->flags = flags;
449 relinquish_request->endpoint_count = 1;
450 relinquish_request->endpoints[0] = sender;
451 return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t);
452}
453
454uint32_t ffa_memory_region_init(
455 struct ffa_memory_region *memory_region, ffa_vm_id_t sender,
456 ffa_vm_id_t receiver,
457 const struct ffa_memory_region_constituent constituents[],
458 uint32_t constituent_count, uint32_t tag,
459 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
460 enum ffa_instruction_access instruction_access,
461 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
462 enum ffa_memory_shareability shareability);
463uint32_t ffa_memory_retrieve_request_init(
464 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
465 ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag,
466 ffa_memory_region_flags_t flags, enum ffa_data_access data_access,
467 enum ffa_instruction_access instruction_access,
468 enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
469 enum ffa_memory_shareability shareability);
470uint32_t ffa_memory_lender_retrieve_request_init(
471 struct ffa_memory_region *memory_region, ffa_memory_handle_t handle,
472 ffa_vm_id_t sender);
473uint32_t ffa_retrieved_memory_region_init(
474 struct ffa_memory_region *response, size_t response_max_size,
475 ffa_vm_id_t sender, ffa_memory_attributes_t attributes,
476 ffa_memory_region_flags_t flags, ffa_memory_handle_t handle,
477 ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions,
478 const struct ffa_memory_region_constituent constituents[],
479 uint32_t constituent_count);