Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * 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 Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | #include "hf/types.h" |
| 12 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 13 | #define FFA_VERSION_MAJOR 0x1 |
| 14 | #define FFA_VERSION_MINOR 0x0 |
| 15 | |
| 16 | #define FFA_VERSION_MAJOR_OFFSET 16 |
| 17 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 18 | /* 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 |
Fuad Tabba | da72d14 | 2020-07-30 09:17:05 +0100 | [diff] [blame] | 23 | #define FFA_HIGH_64_ID 0xC400007F |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 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 Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 51 | #define FFA_MEM_FRAG_RX_32 0x8400007A |
| 52 | #define FFA_MEM_FRAG_TX_32 0x8400007B |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 53 | |
| 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 | |
| 87 | enum 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 | |
| 94 | enum 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 | |
| 101 | enum ffa_memory_type { |
| 102 | FFA_MEMORY_NOT_SPECIFIED_MEM, |
| 103 | FFA_MEMORY_DEVICE_MEM, |
| 104 | FFA_MEMORY_NORMAL_MEM, |
| 105 | }; |
| 106 | |
| 107 | enum 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 | |
| 118 | enum 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 | |
| 125 | typedef 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 | */ |
| 131 | typedef 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 | |
| 162 | ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t, |
| 163 | FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK) |
| 164 | ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t, |
| 165 | FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK) |
| 166 | |
| 167 | ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t, |
| 168 | FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK) |
| 169 | ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t, |
| 170 | FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK) |
| 171 | |
| 172 | ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET, |
| 173 | FFA_MEMORY_TYPE_MASK) |
| 174 | ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET, |
| 175 | FFA_MEMORY_TYPE_MASK) |
| 176 | |
| 177 | ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t, |
| 178 | FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK) |
| 179 | ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t, |
| 180 | FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK) |
| 181 | |
| 182 | ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t, |
| 183 | FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK) |
| 184 | ATTR_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 Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 191 | #define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0)) |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 192 | |
| 193 | /** The ID of a VM. These are assigned sequentially starting with an offset. */ |
| 194 | typedef 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 | */ |
| 200 | typedef 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 | */ |
| 206 | typedef ffa_vm_id_t ffa_vm_count_t; |
| 207 | |
| 208 | /** The index of a vCPU within a particular VM. */ |
| 209 | typedef 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 | */ |
| 215 | typedef ffa_vcpu_index_t ffa_vcpu_count_t; |
| 216 | |
| 217 | /** Parameter and return type of FF-A functions. */ |
| 218 | struct 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 | |
| 229 | static inline ffa_vm_id_t ffa_msg_send_sender(struct ffa_value args) |
| 230 | { |
| 231 | return (args.arg1 >> 16) & 0xffff; |
| 232 | } |
| 233 | |
| 234 | static inline ffa_vm_id_t ffa_msg_send_receiver(struct ffa_value args) |
| 235 | { |
| 236 | return args.arg1 & 0xffff; |
| 237 | } |
| 238 | |
| 239 | static inline uint32_t ffa_msg_send_size(struct ffa_value args) |
| 240 | { |
| 241 | return args.arg3; |
| 242 | } |
| 243 | |
| 244 | static inline uint32_t ffa_msg_send_attributes(struct ffa_value args) |
| 245 | { |
| 246 | return args.arg4; |
| 247 | } |
| 248 | |
Andrew Walbran | 1bbe940 | 2020-04-30 16:47:13 +0100 | [diff] [blame] | 249 | static 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 Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 254 | static inline ffa_memory_handle_t ffa_mem_success_handle(struct ffa_value args) |
| 255 | { |
Andrew Walbran | 1bbe940 | 2020-04-30 16:47:13 +0100 | [diff] [blame] | 256 | return ffa_assemble_handle(args.arg2, args.arg3); |
| 257 | } |
| 258 | |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 259 | static 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 Walbran | 1bbe940 | 2020-04-30 16:47:13 +0100 | [diff] [blame] | 264 | static 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 Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static inline ffa_vm_id_t ffa_vm_id(struct ffa_value args) |
| 272 | { |
| 273 | return (args.arg1 >> 16) & 0xffff; |
| 274 | } |
| 275 | |
| 276 | static inline ffa_vcpu_index_t ffa_vcpu_index(struct ffa_value args) |
| 277 | { |
| 278 | return args.arg1 & 0xffff; |
| 279 | } |
| 280 | |
| 281 | static 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 Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 287 | static inline ffa_vm_id_t ffa_frag_sender(struct ffa_value args) |
| 288 | { |
| 289 | return (args.arg4 >> 16) & 0xffff; |
| 290 | } |
| 291 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 292 | /** |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 293 | * Holds the UUID in a struct that is mappable directly to the SMCC calling |
| 294 | * convention, which is used for FF-A calls. |
| 295 | * |
| 296 | * Refer to table 84 of the FF-A 1.0 EAC specification as well as section 5.3 |
| 297 | * of the SMCC Spec 1.2. |
| 298 | */ |
| 299 | struct ffa_uuid { |
| 300 | uint32_t uuid[4]; |
| 301 | }; |
| 302 | |
| 303 | static inline void ffa_uuid_init(uint32_t w0, uint32_t w1, uint32_t w2, |
| 304 | uint32_t w3, struct ffa_uuid *uuid) |
| 305 | { |
| 306 | uuid->uuid[0] = w0; |
| 307 | uuid->uuid[1] = w1; |
| 308 | uuid->uuid[2] = w2; |
| 309 | uuid->uuid[3] = w3; |
| 310 | } |
| 311 | |
| 312 | static inline bool ffa_uuid_equal(const struct ffa_uuid *uuid1, |
| 313 | const struct ffa_uuid *uuid2) |
| 314 | { |
| 315 | return (uuid1->uuid[0] == uuid2->uuid[0]) && |
| 316 | (uuid1->uuid[1] == uuid2->uuid[1]) && |
| 317 | (uuid1->uuid[2] == uuid2->uuid[2]) && |
| 318 | (uuid1->uuid[3] == uuid2->uuid[3]); |
| 319 | } |
| 320 | |
| 321 | static inline bool ffa_uuid_is_null(const struct ffa_uuid *uuid) |
| 322 | { |
| 323 | return (uuid->uuid[0] == 0) && (uuid->uuid[1] == 0) && |
| 324 | (uuid->uuid[2] == 0) && (uuid->uuid[3] == 0); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Flags to determine the partition properties, as required by |
| 329 | * FFA_PARTITION_INFO_GET. |
| 330 | * |
| 331 | * The values of the flags are specified in table 82 of the FF-A 1.0 EAC |
| 332 | * specification, "Partition information descriptor, partition properties". |
| 333 | */ |
| 334 | typedef uint32_t ffa_partition_properties_t; |
| 335 | |
| 336 | /** Partition property: partition supports receipt of direct requests. */ |
| 337 | #define FFA_PARTITION_DIRECT_RECV 0x1 |
| 338 | |
| 339 | /** Partition property: partition can send direct requests. */ |
| 340 | #define FFA_PARTITION_DIRECT_SEND 0x2 |
| 341 | |
| 342 | /** Partition property: partition can send and receive indirect messages. */ |
| 343 | #define FFA_PARTITION_INDIRECT_MSG 0x4 |
| 344 | |
| 345 | /** |
| 346 | * Holds information returned for each partition by the FFA_PARTITION_INFO_GET |
| 347 | * interface. |
| 348 | * This corresponds to table 82 of the FF-A 1.0 EAC specification, "Partition |
| 349 | * information descriptor". |
| 350 | */ |
| 351 | struct ffa_partition_info { |
| 352 | ffa_vm_id_t vm_id; |
| 353 | ffa_vcpu_count_t vcpu_count; |
| 354 | ffa_partition_properties_t properties; |
| 355 | }; |
| 356 | |
| 357 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 358 | * A set of contiguous pages which is part of a memory region. This corresponds |
| 359 | * to table 40 of the FF-A 1.0 EAC specification, "Constituent memory region |
| 360 | * descriptor". |
| 361 | */ |
| 362 | struct ffa_memory_region_constituent { |
| 363 | /** |
| 364 | * The base IPA of the constituent memory region, aligned to 4 kiB page |
| 365 | * size granularity. |
| 366 | */ |
| 367 | uint64_t address; |
| 368 | /** The number of 4 kiB pages in the constituent memory region. */ |
| 369 | uint32_t page_count; |
| 370 | /** Reserved field, must be 0. */ |
| 371 | uint32_t reserved; |
| 372 | }; |
| 373 | |
| 374 | /** |
| 375 | * A set of pages comprising a memory region. This corresponds to table 39 of |
| 376 | * the FF-A 1.0 EAC specification, "Composite memory region descriptor". |
| 377 | */ |
| 378 | struct ffa_composite_memory_region { |
| 379 | /** |
| 380 | * The total number of 4 kiB pages included in this memory region. This |
| 381 | * must be equal to the sum of page counts specified in each |
| 382 | * `ffa_memory_region_constituent`. |
| 383 | */ |
| 384 | uint32_t page_count; |
| 385 | /** |
| 386 | * The number of constituents (`ffa_memory_region_constituent`) |
| 387 | * included in this memory region range. |
| 388 | */ |
| 389 | uint32_t constituent_count; |
| 390 | /** Reserved field, must be 0. */ |
| 391 | uint64_t reserved_0; |
| 392 | /** An array of `constituent_count` memory region constituents. */ |
| 393 | struct ffa_memory_region_constituent constituents[]; |
| 394 | }; |
| 395 | |
| 396 | /** Flags to indicate properties of receivers during memory region retrieval. */ |
| 397 | typedef uint8_t ffa_memory_receiver_flags_t; |
| 398 | |
| 399 | /** |
| 400 | * This corresponds to table 41 of the FF-A 1.0 EAC specification, "Memory |
| 401 | * access permissions descriptor". |
| 402 | */ |
| 403 | struct ffa_memory_region_attributes { |
| 404 | /** The ID of the VM to which the memory is being given or shared. */ |
| 405 | ffa_vm_id_t receiver; |
| 406 | /** |
| 407 | * The permissions with which the memory region should be mapped in the |
| 408 | * receiver's page table. |
| 409 | */ |
| 410 | ffa_memory_access_permissions_t permissions; |
| 411 | /** |
| 412 | * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP |
| 413 | * for memory regions with multiple borrowers. |
| 414 | */ |
| 415 | ffa_memory_receiver_flags_t flags; |
| 416 | }; |
| 417 | |
| 418 | /** Flags to control the behaviour of a memory sharing transaction. */ |
| 419 | typedef uint32_t ffa_memory_region_flags_t; |
| 420 | |
| 421 | /** |
| 422 | * Clear memory region contents after unmapping it from the sender and before |
| 423 | * mapping it for any receiver. |
| 424 | */ |
| 425 | #define FFA_MEMORY_REGION_FLAG_CLEAR 0x1 |
| 426 | |
| 427 | /** |
| 428 | * Whether the hypervisor may time slice the memory sharing or retrieval |
| 429 | * operation. |
| 430 | */ |
| 431 | #define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2 |
| 432 | |
| 433 | /** |
| 434 | * Whether the hypervisor should clear the memory region after the receiver |
| 435 | * relinquishes it or is aborted. |
| 436 | */ |
| 437 | #define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4 |
| 438 | |
| 439 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3) |
| 440 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3) |
| 441 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3) |
| 442 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3) |
| 443 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3) |
| 444 | |
| 445 | /** |
| 446 | * This corresponds to table 42 of the FF-A 1.0 EAC specification, "Endpoint |
| 447 | * memory access descriptor". |
| 448 | */ |
| 449 | struct ffa_memory_access { |
| 450 | struct ffa_memory_region_attributes receiver_permissions; |
| 451 | /** |
| 452 | * Offset in bytes from the start of the outer `ffa_memory_region` to |
| 453 | * an `ffa_composite_memory_region` struct. |
| 454 | */ |
| 455 | uint32_t composite_memory_region_offset; |
| 456 | uint64_t reserved_0; |
| 457 | }; |
| 458 | |
| 459 | /** |
| 460 | * Information about a set of pages which are being shared. This corresponds to |
| 461 | * table 45 of the FF-A 1.0 EAC specification, "Lend, donate or share memory |
| 462 | * transaction descriptor". Note that it is also used for retrieve requests and |
| 463 | * responses. |
| 464 | */ |
| 465 | struct ffa_memory_region { |
| 466 | /** |
| 467 | * The ID of the VM which originally sent the memory region, i.e. the |
| 468 | * owner. |
| 469 | */ |
| 470 | ffa_vm_id_t sender; |
| 471 | ffa_memory_attributes_t attributes; |
| 472 | /** Reserved field, must be 0. */ |
| 473 | uint8_t reserved_0; |
| 474 | /** Flags to control behaviour of the transaction. */ |
| 475 | ffa_memory_region_flags_t flags; |
| 476 | ffa_memory_handle_t handle; |
| 477 | /** |
| 478 | * An implementation defined value associated with the receiver and the |
| 479 | * memory region. |
| 480 | */ |
| 481 | uint64_t tag; |
| 482 | /** Reserved field, must be 0. */ |
| 483 | uint32_t reserved_1; |
| 484 | /** |
| 485 | * The number of `ffa_memory_access` entries included in this |
| 486 | * transaction. |
| 487 | */ |
| 488 | uint32_t receiver_count; |
| 489 | /** |
| 490 | * An array of `attribute_count` endpoint memory access descriptors. |
| 491 | * Each one specifies a memory region offset, an endpoint and the |
| 492 | * attributes with which this memory region should be mapped in that |
| 493 | * endpoint's page table. |
| 494 | */ |
| 495 | struct ffa_memory_access receivers[]; |
| 496 | }; |
| 497 | |
| 498 | /** |
| 499 | * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table |
| 500 | * 150 of the FF-A 1.0 EAC specification, "Descriptor to relinquish a memory |
| 501 | * region". |
| 502 | */ |
| 503 | struct ffa_mem_relinquish { |
| 504 | ffa_memory_handle_t handle; |
| 505 | ffa_memory_region_flags_t flags; |
| 506 | uint32_t endpoint_count; |
| 507 | ffa_vm_id_t endpoints[]; |
| 508 | }; |
| 509 | |
| 510 | /** |
| 511 | * Gets the `ffa_composite_memory_region` for the given receiver from an |
| 512 | * `ffa_memory_region`, or NULL if it is not valid. |
| 513 | */ |
| 514 | static inline struct ffa_composite_memory_region * |
| 515 | ffa_memory_region_get_composite(struct ffa_memory_region *memory_region, |
| 516 | uint32_t receiver_index) |
| 517 | { |
| 518 | uint32_t offset = memory_region->receivers[receiver_index] |
| 519 | .composite_memory_region_offset; |
| 520 | |
| 521 | if (offset == 0) { |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | return (struct ffa_composite_memory_region *)((uint8_t *)memory_region + |
| 526 | offset); |
| 527 | } |
| 528 | |
| 529 | static inline uint32_t ffa_mem_relinquish_init( |
| 530 | struct ffa_mem_relinquish *relinquish_request, |
| 531 | ffa_memory_handle_t handle, ffa_memory_region_flags_t flags, |
| 532 | ffa_vm_id_t sender) |
| 533 | { |
| 534 | relinquish_request->handle = handle; |
| 535 | relinquish_request->flags = flags; |
| 536 | relinquish_request->endpoint_count = 1; |
| 537 | relinquish_request->endpoints[0] = sender; |
| 538 | return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_vm_id_t); |
| 539 | } |
| 540 | |
| 541 | uint32_t ffa_memory_region_init( |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 542 | struct ffa_memory_region *memory_region, size_t memory_region_max_size, |
| 543 | ffa_vm_id_t sender, ffa_vm_id_t receiver, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 544 | const struct ffa_memory_region_constituent constituents[], |
| 545 | uint32_t constituent_count, uint32_t tag, |
| 546 | ffa_memory_region_flags_t flags, enum ffa_data_access data_access, |
| 547 | enum ffa_instruction_access instruction_access, |
| 548 | enum ffa_memory_type type, enum ffa_memory_cacheability cacheability, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 549 | enum ffa_memory_shareability shareability, uint32_t *fragment_length, |
| 550 | uint32_t *total_length); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 551 | uint32_t ffa_memory_retrieve_request_init( |
| 552 | struct ffa_memory_region *memory_region, ffa_memory_handle_t handle, |
| 553 | ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag, |
| 554 | ffa_memory_region_flags_t flags, enum ffa_data_access data_access, |
| 555 | enum ffa_instruction_access instruction_access, |
| 556 | enum ffa_memory_type type, enum ffa_memory_cacheability cacheability, |
| 557 | enum ffa_memory_shareability shareability); |
| 558 | uint32_t ffa_memory_lender_retrieve_request_init( |
| 559 | struct ffa_memory_region *memory_region, ffa_memory_handle_t handle, |
| 560 | ffa_vm_id_t sender); |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 561 | bool ffa_retrieved_memory_region_init( |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 562 | struct ffa_memory_region *response, size_t response_max_size, |
| 563 | ffa_vm_id_t sender, ffa_memory_attributes_t attributes, |
| 564 | ffa_memory_region_flags_t flags, ffa_memory_handle_t handle, |
| 565 | ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions, |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 566 | uint32_t page_count, uint32_t total_constituent_count, |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 567 | const struct ffa_memory_region_constituent constituents[], |
Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 568 | uint32_t fragment_constituent_count, uint32_t *total_length, |
| 569 | uint32_t *fragment_length); |
| 570 | uint32_t ffa_memory_fragment_init( |
| 571 | struct ffa_memory_region_constituent *fragment, |
| 572 | size_t fragment_max_size, |
| 573 | const struct ffa_memory_region_constituent constituents[], |
| 574 | uint32_t constituent_count, uint32_t *fragment_length); |