J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 1 | /* |
Daniel Boulby | 8aa994c | 2022-01-05 19:44:30 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef FFA_HELPERS_H |
| 8 | #define FFA_HELPERS_H |
| 9 | |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 10 | #include <ffa_svc.h> |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 11 | #include <tftf_lib.h> |
| 12 | #include <utils_def.h> |
| 13 | |
| 14 | /* This error code must be different to the ones used by FFA */ |
| 15 | #define FFA_TFTF_ERROR -42 |
| 16 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 17 | typedef unsigned short ffa_id_t; |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame] | 18 | typedef unsigned short ffa_vm_count_t; |
| 19 | typedef unsigned short ffa_vcpu_count_t; |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 20 | typedef uint64_t ffa_memory_handle_t; |
| 21 | /** Flags to indicate properties of receivers during memory region retrieval. */ |
| 22 | typedef uint8_t ffa_memory_receiver_flags_t; |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame] | 23 | |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 24 | struct ffa_uuid { |
| 25 | const uint32_t uuid[4]; |
| 26 | }; |
| 27 | |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 28 | #ifndef __ASSEMBLY__ |
| 29 | |
J-Alves | 18c2805 | 2021-03-09 09:58:53 +0000 | [diff] [blame] | 30 | #include <cassert.h> |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 31 | #include <stdint.h> |
| 32 | |
J-Alves | 4439ece | 2021-11-05 11:52:54 +0000 | [diff] [blame] | 33 | /** |
| 34 | * FF-A Feature ID, to be used with interface FFA_FEATURES. |
| 35 | * As defined in the FF-A v1.1 Beta specification, table 13.10, in section |
| 36 | * 13.2. |
| 37 | */ |
| 38 | |
| 39 | /** Query interrupt ID of Notification Pending Interrupt. */ |
| 40 | #define FFA_FEATURE_NPI 0x1U |
| 41 | |
| 42 | /** Query interrupt ID of Schedule Receiver Interrupt. */ |
| 43 | #define FFA_FEATURE_SRI 0x2U |
| 44 | |
| 45 | /** Query interrupt ID of the Managed Exit Interrupt. */ |
| 46 | #define FFA_FEATURE_MEI 0x3U |
| 47 | |
Max Shvetsov | 0b7d25f | 2021-03-05 13:46:42 +0000 | [diff] [blame] | 48 | /** Partition property: partition supports receipt of direct requests. */ |
| 49 | #define FFA_PARTITION_DIRECT_REQ_RECV 0x1 |
| 50 | |
| 51 | /** Partition property: partition can send direct requests. */ |
| 52 | #define FFA_PARTITION_DIRECT_REQ_SEND 0x2 |
| 53 | |
| 54 | /** Partition property: partition can send and receive indirect messages. */ |
| 55 | #define FFA_PARTITION_INDIRECT_MSG 0x4 |
| 56 | |
J-Alves | 4d05dec | 2021-11-02 11:52:27 +0000 | [diff] [blame] | 57 | /** Partition property: partition can receive notifications. */ |
| 58 | #define FFA_PARTITION_NOTIFICATION 0x8 |
| 59 | |
Daniel Boulby | 8aa994c | 2022-01-05 19:44:30 +0000 | [diff] [blame] | 60 | /** |
| 61 | * Partition info descriptor as defined in Table 13.34 of the v1.1 BETA0 |
| 62 | * FF-A Specification |
| 63 | */ |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 64 | struct ffa_partition_info { |
| 65 | /** The ID of the VM the information is about */ |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 66 | ffa_id_t id; |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 67 | /** The number of execution contexts implemented by the partition */ |
| 68 | uint16_t exec_context; |
| 69 | /** The Partition's properties, e.g. supported messaging methods */ |
| 70 | uint32_t properties; |
Daniel Boulby | 8aa994c | 2022-01-05 19:44:30 +0000 | [diff] [blame] | 71 | /** The uuid of the partition */ |
| 72 | struct ffa_uuid uuid; |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Daniel Boulby | 2ac55f2 | 2022-01-21 12:08:08 +0000 | [diff] [blame^] | 75 | /** |
| 76 | * Partition info descriptor as defined in Table 8.25 of the v1.0 |
| 77 | * FF-A Specification |
| 78 | */ |
| 79 | struct ffa_partition_info_v1_0 { |
| 80 | /** The ID of the VM the information is about */ |
| 81 | ffa_id_t id; |
| 82 | /** The number of execution contexts implemented by the partition */ |
| 83 | uint16_t exec_context; |
| 84 | /** The Partition's properties, e.g. supported messaging methods */ |
| 85 | uint32_t properties; |
| 86 | }; |
| 87 | |
J-Alves | f156ae9 | 2021-10-08 12:10:05 +0100 | [diff] [blame] | 88 | static inline uint32_t ffa_func_id(smc_ret_values val) |
| 89 | { |
J-Alves | 6cb21d9 | 2021-01-07 15:18:12 +0000 | [diff] [blame] | 90 | return (uint32_t) val.ret0; |
| 91 | } |
| 92 | |
J-Alves | f156ae9 | 2021-10-08 12:10:05 +0100 | [diff] [blame] | 93 | static inline int32_t ffa_error_code(smc_ret_values val) |
| 94 | { |
J-Alves | 6cb21d9 | 2021-01-07 15:18:12 +0000 | [diff] [blame] | 95 | return (int32_t) val.ret2; |
| 96 | } |
| 97 | |
Daniel Boulby | 198deda | 2021-03-03 11:35:25 +0000 | [diff] [blame] | 98 | static inline ffa_id_t ffa_endpoint_id(smc_ret_values val) { |
| 99 | return (ffa_id_t) val.ret2 & 0xffff; |
| 100 | } |
| 101 | |
Daniel Boulby | 2ac55f2 | 2022-01-21 12:08:08 +0000 | [diff] [blame^] | 102 | static inline uint32_t ffa_partition_info_count(smc_ret_values val) |
| 103 | { |
| 104 | return (uint32_t) val.ret2; |
| 105 | } |
| 106 | |
J-Alves | 4439ece | 2021-11-05 11:52:54 +0000 | [diff] [blame] | 107 | static inline uint32_t ffa_feature_intid(smc_ret_values val) |
| 108 | { |
| 109 | return (uint32_t)val.ret2; |
| 110 | } |
| 111 | |
J-Alves | 18c2805 | 2021-03-09 09:58:53 +0000 | [diff] [blame] | 112 | typedef uint64_t ffa_notification_bitmap_t; |
| 113 | |
| 114 | #define FFA_NOTIFICATION(ID) (UINT64_C(1) << ID) |
| 115 | |
| 116 | #define MAX_FFA_NOTIFICATIONS UINT32_C(64) |
| 117 | |
| 118 | #define FFA_NOTIFICATIONS_FLAG_PER_VCPU UINT32_C(0x1 << 0) |
| 119 | |
J-Alves | b0cb5d0 | 2021-07-08 11:19:33 +0100 | [diff] [blame] | 120 | /** Flag to delay Schedule Receiver Interrupt. */ |
| 121 | #define FFA_NOTIFICATIONS_FLAG_DELAY_SRI UINT32_C(0x1 << 1) |
| 122 | |
J-Alves | 18c2805 | 2021-03-09 09:58:53 +0000 | [diff] [blame] | 123 | #define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) UINT32_C((id & 0xFFFF) << 16) |
| 124 | |
J-Alves | f156ae9 | 2021-10-08 12:10:05 +0100 | [diff] [blame] | 125 | #define FFA_NOTIFICATIONS_FLAG_BITMAP_SP UINT32_C(0x1 << 0) |
| 126 | #define FFA_NOTIFICATIONS_FLAG_BITMAP_VM UINT32_C(0x1 << 1) |
| 127 | #define FFA_NOTIFICATIONS_FLAG_BITMAP_SPM UINT32_C(0x1 << 2) |
| 128 | #define FFA_NOTIFICATIONS_FLAG_BITMAP_HYP UINT32_C(0x1 << 3) |
| 129 | |
J-Alves | 269118a | 2021-09-22 09:46:11 +0100 | [diff] [blame] | 130 | /** |
| 131 | * The following is an SGI ID, that the SPMC configures as non-secure, as |
| 132 | * suggested by the FF-A v1.1 specification, in section 9.4.1. |
| 133 | */ |
| 134 | #define FFA_SCHEDULE_RECEIVER_INTERRUPT_ID 8 |
| 135 | |
J-Alves | f156ae9 | 2021-10-08 12:10:05 +0100 | [diff] [blame] | 136 | #define FFA_NOTIFICATIONS_BITMAP(lo, hi) \ |
| 137 | (ffa_notification_bitmap_t)(lo) | \ |
| 138 | (((ffa_notification_bitmap_t)hi << 32) & 0xFFFFFFFF00000000ULL) |
| 139 | |
| 140 | #define FFA_NOTIFICATIONS_FLAGS_VCPU_ID(id) UINT32_C((id & 0xFFFF) << 16) |
| 141 | |
| 142 | static inline ffa_notification_bitmap_t ffa_notifications_get_from_sp(smc_ret_values val) |
| 143 | { |
| 144 | return FFA_NOTIFICATIONS_BITMAP(val.ret2, val.ret3); |
| 145 | } |
| 146 | |
| 147 | static inline ffa_notification_bitmap_t ffa_notifications_get_from_vm(smc_ret_values val) |
| 148 | { |
| 149 | return FFA_NOTIFICATIONS_BITMAP(val.ret4, val.ret5); |
| 150 | } |
| 151 | |
J-Alves | 5bce250 | 2021-06-14 14:27:45 +0100 | [diff] [blame] | 152 | /* |
| 153 | * FFA_NOTIFICATION_INFO_GET is a SMC64 interface. |
| 154 | * The following macros are defined for SMC64 implementation. |
| 155 | */ |
| 156 | #define FFA_NOTIFICATIONS_INFO_GET_MAX_IDS 20U |
| 157 | |
| 158 | #define FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING UINT64_C(0x1) |
| 159 | |
| 160 | #define FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT 0x7U |
| 161 | #define FFA_NOTIFICATIONS_LISTS_COUNT_MASK 0x1FU |
| 162 | #define FFA_NOTIFICATIONS_LIST_SHIFT(l) (2 * (l - 1) + 12) |
| 163 | #define FFA_NOTIFICATIONS_LIST_SIZE_MASK 0x3U |
| 164 | |
| 165 | static inline uint32_t ffa_notifications_info_get_lists_count( |
| 166 | smc_ret_values ret) |
| 167 | { |
| 168 | return (uint32_t)(ret.ret2 >> FFA_NOTIFICATIONS_LISTS_COUNT_SHIFT) |
| 169 | & FFA_NOTIFICATIONS_LISTS_COUNT_MASK; |
| 170 | } |
| 171 | |
| 172 | static inline uint32_t ffa_notifications_info_get_list_size( |
| 173 | smc_ret_values ret, uint32_t list) |
| 174 | { |
| 175 | return (uint32_t)(ret.ret2 >> FFA_NOTIFICATIONS_LIST_SHIFT(list)) & |
| 176 | FFA_NOTIFICATIONS_LIST_SIZE_MASK; |
| 177 | } |
| 178 | |
| 179 | static inline bool ffa_notifications_info_get_more_pending(smc_ret_values ret) |
| 180 | { |
| 181 | return (ret.ret2 & FFA_NOTIFICATIONS_INFO_GET_FLAG_MORE_PENDING) != 0U; |
| 182 | } |
| 183 | |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 184 | enum ffa_data_access { |
| 185 | FFA_DATA_ACCESS_NOT_SPECIFIED, |
| 186 | FFA_DATA_ACCESS_RO, |
| 187 | FFA_DATA_ACCESS_RW, |
| 188 | FFA_DATA_ACCESS_RESERVED, |
| 189 | }; |
| 190 | |
| 191 | enum ffa_instruction_access { |
| 192 | FFA_INSTRUCTION_ACCESS_NOT_SPECIFIED, |
| 193 | FFA_INSTRUCTION_ACCESS_NX, |
| 194 | FFA_INSTRUCTION_ACCESS_X, |
| 195 | FFA_INSTRUCTION_ACCESS_RESERVED, |
| 196 | }; |
| 197 | |
| 198 | enum ffa_memory_type { |
| 199 | FFA_MEMORY_NOT_SPECIFIED_MEM, |
| 200 | FFA_MEMORY_DEVICE_MEM, |
| 201 | FFA_MEMORY_NORMAL_MEM, |
| 202 | }; |
| 203 | |
| 204 | enum ffa_memory_cacheability { |
| 205 | FFA_MEMORY_CACHE_RESERVED = 0x0, |
| 206 | FFA_MEMORY_CACHE_NON_CACHEABLE = 0x1, |
| 207 | FFA_MEMORY_CACHE_RESERVED_1 = 0x2, |
| 208 | FFA_MEMORY_CACHE_WRITE_BACK = 0x3, |
| 209 | FFA_MEMORY_DEV_NGNRNE = 0x0, |
| 210 | FFA_MEMORY_DEV_NGNRE = 0x1, |
| 211 | FFA_MEMORY_DEV_NGRE = 0x2, |
| 212 | FFA_MEMORY_DEV_GRE = 0x3, |
| 213 | }; |
| 214 | |
| 215 | enum ffa_memory_shareability { |
| 216 | FFA_MEMORY_SHARE_NON_SHAREABLE, |
| 217 | FFA_MEMORY_SHARE_RESERVED, |
| 218 | FFA_MEMORY_OUTER_SHAREABLE, |
| 219 | FFA_MEMORY_INNER_SHAREABLE, |
| 220 | }; |
| 221 | |
| 222 | typedef uint8_t ffa_memory_access_permissions_t; |
| 223 | |
| 224 | /** |
| 225 | * This corresponds to table "Memory region attributes descriptor" of the FF-A |
| 226 | * 1.0 specification. |
| 227 | */ |
| 228 | typedef uint8_t ffa_memory_attributes_t; |
| 229 | |
| 230 | #define FFA_DATA_ACCESS_OFFSET (0x0U) |
| 231 | #define FFA_DATA_ACCESS_MASK ((0x3U) << FFA_DATA_ACCESS_OFFSET) |
| 232 | |
| 233 | #define FFA_INSTRUCTION_ACCESS_OFFSET (0x2U) |
| 234 | #define FFA_INSTRUCTION_ACCESS_MASK ((0x3U) << FFA_INSTRUCTION_ACCESS_OFFSET) |
| 235 | |
| 236 | #define FFA_MEMORY_TYPE_OFFSET (0x4U) |
| 237 | #define FFA_MEMORY_TYPE_MASK ((0x3U) << FFA_MEMORY_TYPE_OFFSET) |
| 238 | |
| 239 | #define FFA_MEMORY_CACHEABILITY_OFFSET (0x2U) |
| 240 | #define FFA_MEMORY_CACHEABILITY_MASK ((0x3U) << FFA_MEMORY_CACHEABILITY_OFFSET) |
| 241 | |
| 242 | #define FFA_MEMORY_SHAREABILITY_OFFSET (0x0U) |
| 243 | #define FFA_MEMORY_SHAREABILITY_MASK ((0x3U) << FFA_MEMORY_SHAREABILITY_OFFSET) |
| 244 | |
| 245 | #define ATTR_FUNCTION_SET(name, container_type, offset, mask) \ |
| 246 | static inline void ffa_set_##name##_attr(container_type *attr, \ |
| 247 | const enum ffa_##name perm) \ |
| 248 | { \ |
| 249 | *attr = (*attr & ~(mask)) | ((perm << offset) & mask); \ |
| 250 | } |
| 251 | |
| 252 | #define ATTR_FUNCTION_GET(name, container_type, offset, mask) \ |
| 253 | static inline enum ffa_##name ffa_get_##name##_attr( \ |
| 254 | container_type attr) \ |
| 255 | { \ |
| 256 | return (enum ffa_##name)((attr & mask) >> offset); \ |
| 257 | } |
| 258 | |
| 259 | ATTR_FUNCTION_SET(data_access, ffa_memory_access_permissions_t, |
| 260 | FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK) |
| 261 | ATTR_FUNCTION_GET(data_access, ffa_memory_access_permissions_t, |
| 262 | FFA_DATA_ACCESS_OFFSET, FFA_DATA_ACCESS_MASK) |
| 263 | |
| 264 | ATTR_FUNCTION_SET(instruction_access, ffa_memory_access_permissions_t, |
| 265 | FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK) |
| 266 | ATTR_FUNCTION_GET(instruction_access, ffa_memory_access_permissions_t, |
| 267 | FFA_INSTRUCTION_ACCESS_OFFSET, FFA_INSTRUCTION_ACCESS_MASK) |
| 268 | |
| 269 | ATTR_FUNCTION_SET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET, |
| 270 | FFA_MEMORY_TYPE_MASK) |
| 271 | ATTR_FUNCTION_GET(memory_type, ffa_memory_attributes_t, FFA_MEMORY_TYPE_OFFSET, |
| 272 | FFA_MEMORY_TYPE_MASK) |
| 273 | |
| 274 | ATTR_FUNCTION_SET(memory_cacheability, ffa_memory_attributes_t, |
| 275 | FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK) |
| 276 | ATTR_FUNCTION_GET(memory_cacheability, ffa_memory_attributes_t, |
| 277 | FFA_MEMORY_CACHEABILITY_OFFSET, FFA_MEMORY_CACHEABILITY_MASK) |
| 278 | |
| 279 | ATTR_FUNCTION_SET(memory_shareability, ffa_memory_attributes_t, |
| 280 | FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK) |
| 281 | ATTR_FUNCTION_GET(memory_shareability, ffa_memory_attributes_t, |
| 282 | FFA_MEMORY_SHAREABILITY_OFFSET, FFA_MEMORY_SHAREABILITY_MASK) |
| 283 | |
| 284 | #define FFA_MEMORY_HANDLE_ALLOCATOR_MASK \ |
| 285 | ((ffa_memory_handle_t)(UINT64_C(1) << 63)) |
| 286 | #define FFA_MEMORY_HANDLE_ALLOCATOR_HYPERVISOR \ |
| 287 | ((ffa_memory_handle_t)(UINT64_C(1) << 63)) |
| 288 | #define FFA_MEMORY_HANDLE_INVALID (~UINT64_C(0)) |
| 289 | |
| 290 | /** |
| 291 | * A set of contiguous pages which is part of a memory region. This corresponds |
| 292 | * to table "Constituent memory region descriptor" of the FFA 1.0 specification. |
| 293 | */ |
| 294 | struct ffa_memory_region_constituent { |
| 295 | /** |
| 296 | * The base IPA of the constituent memory region, aligned to 4 kiB page |
| 297 | * size granularity. |
| 298 | */ |
| 299 | void *address; |
| 300 | /** The number of 4 kiB pages in the constituent memory region. */ |
| 301 | uint32_t page_count; |
| 302 | /** Reserved field, must be 0. */ |
| 303 | uint32_t reserved; |
| 304 | }; |
| 305 | |
| 306 | /** |
| 307 | * A set of pages comprising a memory region. This corresponds to table |
| 308 | * "Composite memory region descriptor" of the FFA 1.0 specification. |
| 309 | */ |
| 310 | struct ffa_composite_memory_region { |
| 311 | /** |
| 312 | * The total number of 4 kiB pages included in this memory region. This |
| 313 | * must be equal to the sum of page counts specified in each |
| 314 | * `ffa_memory_region_constituent`. |
| 315 | */ |
| 316 | uint32_t page_count; |
| 317 | /** |
| 318 | * The number of constituents (`ffa_memory_region_constituent`) |
| 319 | * included in this memory region range. |
| 320 | */ |
| 321 | uint32_t constituent_count; |
| 322 | /** Reserved field, must be 0. */ |
| 323 | uint64_t reserved_0; |
| 324 | /** An array of `constituent_count` memory region constituents. */ |
| 325 | struct ffa_memory_region_constituent constituents[]; |
| 326 | }; |
| 327 | |
| 328 | /** |
| 329 | * This corresponds to table "Memory access permissions descriptor" of the FFA |
| 330 | * 1.0 specification. |
| 331 | */ |
| 332 | struct ffa_memory_region_attributes { |
| 333 | /** The ID of the VM to which the memory is being given or shared. */ |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 334 | ffa_id_t receiver; |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 335 | /** |
| 336 | * The permissions with which the memory region should be mapped in the |
| 337 | * receiver's page table. |
| 338 | */ |
| 339 | ffa_memory_access_permissions_t permissions; |
| 340 | /** |
| 341 | * Flags used during FFA_MEM_RETRIEVE_REQ and FFA_MEM_RETRIEVE_RESP |
| 342 | * for memory regions with multiple borrowers. |
| 343 | */ |
| 344 | ffa_memory_receiver_flags_t flags; |
| 345 | }; |
| 346 | |
| 347 | /** Flags to control the behaviour of a memory sharing transaction. */ |
| 348 | typedef uint32_t ffa_memory_region_flags_t; |
| 349 | |
| 350 | /** |
| 351 | * Clear memory region contents after unmapping it from the sender and before |
| 352 | * mapping it for any receiver. |
| 353 | */ |
| 354 | #define FFA_MEMORY_REGION_FLAG_CLEAR 0x1U |
| 355 | |
| 356 | /** |
| 357 | * Whether the hypervisor may time slice the memory sharing or retrieval |
| 358 | * operation. |
| 359 | */ |
| 360 | #define FFA_MEMORY_REGION_FLAG_TIME_SLICE 0x2U |
| 361 | |
| 362 | /** |
| 363 | * Whether the hypervisor should clear the memory region after the receiver |
| 364 | * relinquishes it or is aborted. |
| 365 | */ |
| 366 | #define FFA_MEMORY_REGION_FLAG_CLEAR_RELINQUISH 0x4U |
| 367 | |
| 368 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_MASK ((0x3U) << 3) |
| 369 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_UNSPECIFIED ((0x0U) << 3) |
| 370 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE ((0x1U) << 3) |
| 371 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_LEND ((0x2U) << 3) |
| 372 | #define FFA_MEMORY_REGION_TRANSACTION_TYPE_DONATE ((0x3U) << 3) |
| 373 | |
J-Alves | 0435cae | 2020-11-06 10:49:56 +0000 | [diff] [blame] | 374 | /** The maximum number of recipients a memory region may be sent to. */ |
| 375 | #define MAX_MEM_SHARE_RECIPIENTS 1U |
| 376 | |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 377 | /** |
| 378 | * This corresponds to table "Endpoint memory access descriptor" of the FFA 1.0 |
| 379 | * specification. |
| 380 | */ |
| 381 | struct ffa_memory_access { |
| 382 | struct ffa_memory_region_attributes receiver_permissions; |
| 383 | /** |
| 384 | * Offset in bytes from the start of the outer `ffa_memory_region` to |
| 385 | * an `ffa_composite_memory_region` struct. |
| 386 | */ |
| 387 | uint32_t composite_memory_region_offset; |
| 388 | uint64_t reserved_0; |
| 389 | }; |
| 390 | |
| 391 | /** |
| 392 | * Information about a set of pages which are being shared. This corresponds to |
| 393 | * table "Lend, donate or share memory transaction descriptor" of the FFA |
| 394 | * 1.0 specification. Note that it is also used for retrieve requests and |
| 395 | * responses. |
| 396 | */ |
| 397 | struct ffa_memory_region { |
| 398 | /** |
| 399 | * The ID of the VM which originally sent the memory region, i.e. the |
| 400 | * owner. |
| 401 | */ |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 402 | ffa_id_t sender; |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 403 | ffa_memory_attributes_t attributes; |
| 404 | /** Reserved field, must be 0. */ |
| 405 | uint8_t reserved_0; |
| 406 | /** Flags to control behaviour of the transaction. */ |
| 407 | ffa_memory_region_flags_t flags; |
| 408 | ffa_memory_handle_t handle; |
| 409 | /** |
| 410 | * An implementation defined value associated with the receiver and the |
| 411 | * memory region. |
| 412 | */ |
| 413 | uint64_t tag; |
| 414 | /** Reserved field, must be 0. */ |
| 415 | uint32_t reserved_1; |
| 416 | /** |
| 417 | * The number of `ffa_memory_access` entries included in this |
| 418 | * transaction. |
| 419 | */ |
| 420 | uint32_t receiver_count; |
| 421 | /** |
| 422 | * An array of `attribute_count` endpoint memory access descriptors. |
| 423 | * Each one specifies a memory region offset, an endpoint and the |
| 424 | * attributes with which this memory region should be mapped in that |
| 425 | * endpoint's page table. |
| 426 | */ |
| 427 | struct ffa_memory_access receivers[]; |
| 428 | }; |
| 429 | |
| 430 | /** |
| 431 | * Descriptor used for FFA_MEM_RELINQUISH requests. This corresponds to table |
| 432 | * "Descriptor to relinquish a memory region" of the FFA 1.0 specification. |
| 433 | */ |
| 434 | struct ffa_mem_relinquish { |
| 435 | ffa_memory_handle_t handle; |
| 436 | ffa_memory_region_flags_t flags; |
| 437 | uint32_t endpoint_count; |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 438 | ffa_id_t endpoints[]; |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | static inline ffa_memory_handle_t ffa_assemble_handle(uint32_t h1, uint32_t h2) |
| 442 | { |
J-Alves | 18c2805 | 2021-03-09 09:58:53 +0000 | [diff] [blame] | 443 | return (ffa_notification_bitmap_t)h1 | |
| 444 | (ffa_notification_bitmap_t)h2 << 32; |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static inline ffa_memory_handle_t ffa_mem_success_handle(smc_ret_values r) |
| 448 | { |
| 449 | return ffa_assemble_handle(r.ret2, r.ret3); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Gets the `ffa_composite_memory_region` for the given receiver from an |
| 454 | * `ffa_memory_region`, or NULL if it is not valid. |
| 455 | */ |
| 456 | static inline struct ffa_composite_memory_region * |
| 457 | ffa_memory_region_get_composite(struct ffa_memory_region *memory_region, |
| 458 | uint32_t receiver_index) |
| 459 | { |
| 460 | uint32_t offset = memory_region->receivers[receiver_index] |
| 461 | .composite_memory_region_offset; |
| 462 | |
| 463 | if (offset == 0) { |
| 464 | return NULL; |
| 465 | } |
| 466 | |
| 467 | return (struct ffa_composite_memory_region *)((uint8_t *)memory_region + |
| 468 | offset); |
| 469 | } |
| 470 | |
| 471 | static inline uint32_t ffa_mem_relinquish_init( |
| 472 | struct ffa_mem_relinquish *relinquish_request, |
| 473 | ffa_memory_handle_t handle, ffa_memory_region_flags_t flags, |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 474 | ffa_id_t sender) |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 475 | { |
| 476 | relinquish_request->handle = handle; |
| 477 | relinquish_request->flags = flags; |
| 478 | relinquish_request->endpoint_count = 1; |
| 479 | relinquish_request->endpoints[0] = sender; |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 480 | return sizeof(struct ffa_mem_relinquish) + sizeof(ffa_id_t); |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | uint32_t ffa_memory_retrieve_request_init( |
| 484 | struct ffa_memory_region *memory_region, ffa_memory_handle_t handle, |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 485 | ffa_id_t sender, ffa_id_t receiver, uint32_t tag, |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 486 | ffa_memory_region_flags_t flags, enum ffa_data_access data_access, |
| 487 | enum ffa_instruction_access instruction_access, |
| 488 | enum ffa_memory_type type, enum ffa_memory_cacheability cacheability, |
| 489 | enum ffa_memory_shareability shareability); |
| 490 | |
| 491 | uint32_t ffa_memory_region_init( |
| 492 | struct ffa_memory_region *memory_region, size_t memory_region_max_size, |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 493 | ffa_id_t sender, ffa_id_t receiver, |
J-Alves | f3a393c | 2020-10-23 16:00:39 +0100 | [diff] [blame] | 494 | const struct ffa_memory_region_constituent constituents[], |
| 495 | uint32_t constituent_count, uint32_t tag, |
| 496 | ffa_memory_region_flags_t flags, enum ffa_data_access data_access, |
| 497 | enum ffa_instruction_access instruction_access, |
| 498 | enum ffa_memory_type type, enum ffa_memory_cacheability cacheability, |
| 499 | enum ffa_memory_shareability shareability, uint32_t *total_length, |
| 500 | uint32_t *fragment_length); |
| 501 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 502 | static inline ffa_id_t ffa_dir_msg_dest(smc_ret_values val) { |
| 503 | return (ffa_id_t)val.ret1 & U(0xFFFF); |
J-Alves | 6cb21d9 | 2021-01-07 15:18:12 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 506 | static inline ffa_id_t ffa_dir_msg_source(smc_ret_values val) { |
| 507 | return (ffa_id_t)(val.ret1 >> 16U); |
J-Alves | 6cb21d9 | 2021-01-07 15:18:12 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 510 | smc_ret_values ffa_msg_send_direct_req64(ffa_id_t source_id, |
| 511 | ffa_id_t dest_id, uint64_t arg0, |
J-Alves | ecd3074 | 2021-02-19 18:31:06 +0000 | [diff] [blame] | 512 | uint64_t arg1, uint64_t arg2, |
| 513 | uint64_t arg3, uint64_t arg4); |
J-Alves | d1aae29 | 2020-10-08 17:16:58 +0100 | [diff] [blame] | 514 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 515 | smc_ret_values ffa_msg_send_direct_req32(ffa_id_t source_id, |
| 516 | ffa_id_t dest_id, uint32_t arg0, |
J-Alves | ecd3074 | 2021-02-19 18:31:06 +0000 | [diff] [blame] | 517 | uint32_t arg1, uint32_t arg2, |
| 518 | uint32_t arg3, uint32_t arg4); |
| 519 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 520 | smc_ret_values ffa_msg_send_direct_resp64(ffa_id_t source_id, |
| 521 | ffa_id_t dest_id, uint64_t arg0, |
J-Alves | ecd3074 | 2021-02-19 18:31:06 +0000 | [diff] [blame] | 522 | uint64_t arg1, uint64_t arg2, |
| 523 | uint64_t arg3, uint64_t arg4); |
| 524 | |
Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 525 | smc_ret_values ffa_msg_send_direct_resp32(ffa_id_t source_id, |
| 526 | ffa_id_t dest_id, uint32_t arg0, |
J-Alves | ecd3074 | 2021-02-19 18:31:06 +0000 | [diff] [blame] | 527 | uint32_t arg1, uint32_t arg2, |
| 528 | uint32_t arg3, uint32_t arg4); |
J-Alves | 035b7d0 | 2021-02-11 10:40:35 +0000 | [diff] [blame] | 529 | |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 530 | smc_ret_values ffa_run(uint32_t dest_id, uint32_t vcpu_id); |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 531 | smc_ret_values ffa_version(uint32_t input_version); |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame] | 532 | smc_ret_values ffa_id_get(void); |
Daniel Boulby | 198deda | 2021-03-03 11:35:25 +0000 | [diff] [blame] | 533 | smc_ret_values ffa_spm_id_get(void); |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame] | 534 | smc_ret_values ffa_msg_wait(void); |
J-Alves | 5aecd98 | 2020-06-11 10:25:33 +0100 | [diff] [blame] | 535 | smc_ret_values ffa_error(int32_t error_code); |
Max Shvetsov | c17c1d3 | 2020-06-11 15:03:01 +0100 | [diff] [blame] | 536 | smc_ret_values ffa_features(uint32_t feature); |
Max Shvetsov | 0b7d25f | 2021-03-05 13:46:42 +0000 | [diff] [blame] | 537 | smc_ret_values ffa_partition_info_get(const struct ffa_uuid uuid); |
Max Shvetsov | c32f478 | 2020-06-23 09:41:15 +0100 | [diff] [blame] | 538 | smc_ret_values ffa_rx_release(void); |
Ruari Phipps | bd0a7e4 | 2020-07-17 16:42:21 +0100 | [diff] [blame] | 539 | smc_ret_values ffa_rxtx_map(uintptr_t send, uintptr_t recv, uint32_t pages); |
Daniel Boulby | e060290 | 2021-07-07 11:14:39 +0100 | [diff] [blame] | 540 | smc_ret_values ffa_rxtx_unmap(void); |
J-Alves | 3ea46d1 | 2020-09-09 11:13:05 +0100 | [diff] [blame] | 541 | smc_ret_values ffa_mem_donate(uint32_t descriptor_length, |
| 542 | uint32_t fragment_length); |
| 543 | smc_ret_values ffa_mem_lend(uint32_t descriptor_length, |
| 544 | uint32_t fragment_length); |
| 545 | smc_ret_values ffa_mem_share(uint32_t descriptor_length, |
| 546 | uint32_t fragment_length); |
| 547 | smc_ret_values ffa_mem_retrieve_req(uint32_t descriptor_length, |
| 548 | uint32_t fragment_length); |
| 549 | smc_ret_values ffa_mem_relinquish(void); |
| 550 | smc_ret_values ffa_mem_reclaim(uint64_t handle, uint32_t flags); |
J-Alves | bcb1f97 | 2021-03-11 14:03:54 +0000 | [diff] [blame] | 551 | smc_ret_values ffa_notification_bitmap_create(ffa_id_t vm_id, |
| 552 | ffa_vcpu_count_t vcpu_count); |
| 553 | smc_ret_values ffa_notification_bitmap_destroy(ffa_id_t vm_id); |
J-Alves | 18c2805 | 2021-03-09 09:58:53 +0000 | [diff] [blame] | 554 | smc_ret_values ffa_notification_bind(ffa_id_t sender, ffa_id_t receiver, |
| 555 | uint32_t flags, |
| 556 | ffa_notification_bitmap_t notifications); |
| 557 | smc_ret_values ffa_notification_unbind(ffa_id_t sender, ffa_id_t receiver, |
| 558 | ffa_notification_bitmap_t notifications); |
J-Alves | f156ae9 | 2021-10-08 12:10:05 +0100 | [diff] [blame] | 559 | smc_ret_values ffa_notification_set(ffa_id_t sender, ffa_id_t receiver, |
| 560 | uint32_t flags, |
| 561 | ffa_notification_bitmap_t bitmap); |
| 562 | smc_ret_values ffa_notification_get(ffa_id_t receiver, uint32_t vcpu_id, |
| 563 | uint32_t flags); |
J-Alves | 5bce250 | 2021-06-14 14:27:45 +0100 | [diff] [blame] | 564 | smc_ret_values ffa_notification_info_get(void); |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 565 | #endif /* __ASSEMBLY__ */ |
| 566 | |
| 567 | #endif /* FFA_HELPERS_H */ |