| 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 | #include "hf/ffa.h" |
| 10 | |
| 11 | #include <stddef.h> |
| 12 | |
| 13 | #include "hf/types.h" |
| 14 | |
| 15 | #if defined(__linux__) && defined(__KERNEL__) |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/string.h> |
| 18 | |
| 19 | #else |
| Federico Recanati | 392be39 | 2022-02-08 20:53:03 +0100 | [diff] [blame] | 20 | #include "hf/static_assert.h" |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 21 | #include "hf/std.h" |
| 22 | #endif |
| 23 | |
| Federico Recanati | 392be39 | 2022-02-08 20:53:03 +0100 | [diff] [blame] | 24 | static_assert(sizeof(struct ffa_endpoint_rx_tx_descriptor) % 16 == 0, |
| 25 | "struct ffa_endpoint_rx_tx_descriptor must be a multiple of 16 " |
| 26 | "bytes long."); |
| 27 | |
| Raghu Krishnamurthy | 03f21d2 | 2021-11-02 18:39:23 -0700 | [diff] [blame] | 28 | static void ffa_copy_memory_region_constituents( |
| 29 | struct ffa_memory_region_constituent *dest, |
| 30 | const struct ffa_memory_region_constituent *src) |
| 31 | { |
| 32 | dest->address = src->address; |
| 33 | dest->page_count = src->page_count; |
| 34 | dest->reserved = 0; |
| 35 | } |
| 36 | |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 37 | /** |
| J-Alves | b44bb7d | 2022-04-25 15:43:46 +0100 | [diff] [blame] | 38 | * Initializes receiver permissions, in a memory transaction descriptor. |
| 39 | */ |
| 40 | void ffa_memory_access_init_permissions( |
| 41 | struct ffa_memory_access *receiver, ffa_vm_id_t receiver_id, |
| 42 | enum ffa_data_access data_access, |
| 43 | enum ffa_instruction_access instruction_access, |
| 44 | ffa_memory_receiver_flags_t flags) |
| 45 | { |
| 46 | ffa_memory_access_permissions_t permissions = 0; |
| 47 | |
| 48 | /* Set memory region's permissions. */ |
| 49 | ffa_set_data_access_attr(&permissions, data_access); |
| 50 | ffa_set_instruction_access_attr(&permissions, instruction_access); |
| 51 | |
| 52 | receiver->receiver_permissions.receiver = receiver_id; |
| 53 | receiver->receiver_permissions.permissions = permissions; |
| 54 | receiver->receiver_permissions.flags = flags; |
| 55 | |
| 56 | receiver->reserved_0 = 0ULL; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Initialises the header of the given `ffa_memory_region`, not |
| 61 | * including the composite memory region offset. |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 62 | */ |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 63 | static void ffa_memory_region_init_header( |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 64 | struct ffa_memory_region *memory_region, ffa_vm_id_t sender, |
| 65 | ffa_memory_attributes_t attributes, ffa_memory_region_flags_t flags, |
| J-Alves | b44bb7d | 2022-04-25 15:43:46 +0100 | [diff] [blame] | 66 | ffa_memory_handle_t handle, uint32_t tag, uint32_t receiver_count) |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 67 | { |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 68 | memory_region->sender = sender; |
| 69 | memory_region->attributes = attributes; |
| 70 | memory_region->reserved_0 = 0; |
| 71 | memory_region->flags = flags; |
| 72 | memory_region->handle = handle; |
| 73 | memory_region->tag = tag; |
| 74 | memory_region->reserved_1 = 0; |
| J-Alves | b44bb7d | 2022-04-25 15:43:46 +0100 | [diff] [blame] | 75 | memory_region->receiver_count = receiver_count; |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 79 | * Copies as many as possible of the given constituents to the respective |
| 80 | * memory region and sets the respective offset. |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 81 | * |
| 82 | * Returns the number of constituents remaining which wouldn't fit, and (via |
| 83 | * return parameters) the size in bytes of the first fragment of data copied to |
| 84 | * `memory_region` (attributes, constituents and memory region header size), and |
| 85 | * the total size of the memory sharing message including all constituents. |
| 86 | */ |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 87 | static uint32_t ffa_memory_region_init_constituents( |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 88 | struct ffa_memory_region *memory_region, size_t memory_region_max_size, |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 89 | const struct ffa_memory_region_constituent constituents[], |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 90 | uint32_t constituent_count, uint32_t *total_length, |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 91 | uint32_t *fragment_length) |
| 92 | { |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 93 | struct ffa_composite_memory_region *composite_memory_region; |
| 94 | uint32_t fragment_max_constituents; |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 95 | uint32_t constituents_offset; |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 96 | uint32_t count_to_copy; |
| 97 | uint32_t i; |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 98 | |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 99 | /* |
| 100 | * Note that `sizeof(struct_ffa_memory_region)` and `sizeof(struct |
| 101 | * ffa_memory_access)` must both be multiples of 16 (as verified by the |
| 102 | * asserts in `ffa_memory.c`, so it is guaranteed that the offset we |
| 103 | * calculate here is aligned to a 64-bit boundary and so 64-bit values |
| 104 | * can be copied without alignment faults. |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 105 | * If there are multiple receiver endpoints, their respective access |
| 106 | * structure should point to the same offset value. |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 107 | */ |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 108 | for (i = 0U; i < memory_region->receiver_count; i++) { |
| 109 | memory_region->receivers[i].composite_memory_region_offset = |
| 110 | sizeof(struct ffa_memory_region) + |
| 111 | memory_region->receiver_count * |
| 112 | sizeof(struct ffa_memory_access); |
| 113 | } |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 114 | |
| 115 | composite_memory_region = |
| 116 | ffa_memory_region_get_composite(memory_region, 0); |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 117 | composite_memory_region->page_count = 0; |
| 118 | composite_memory_region->constituent_count = constituent_count; |
| 119 | composite_memory_region->reserved_0 = 0; |
| 120 | |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 121 | constituents_offset = |
| 122 | memory_region->receivers[0].composite_memory_region_offset + |
| 123 | sizeof(struct ffa_composite_memory_region); |
| 124 | fragment_max_constituents = |
| 125 | (memory_region_max_size - constituents_offset) / |
| 126 | sizeof(struct ffa_memory_region_constituent); |
| 127 | |
| 128 | count_to_copy = constituent_count; |
| 129 | if (count_to_copy > fragment_max_constituents) { |
| 130 | count_to_copy = fragment_max_constituents; |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 133 | for (i = 0U; i < constituent_count; i++) { |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 134 | if (i < count_to_copy) { |
| Raghu Krishnamurthy | 03f21d2 | 2021-11-02 18:39:23 -0700 | [diff] [blame] | 135 | ffa_copy_memory_region_constituents( |
| 136 | &composite_memory_region->constituents[i], |
| 137 | &constituents[i]); |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 138 | } |
| 139 | composite_memory_region->page_count += |
| 140 | constituents[i].page_count; |
| 141 | } |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 142 | |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 143 | if (total_length != NULL) { |
| 144 | *total_length = |
| 145 | constituents_offset + |
| 146 | composite_memory_region->constituent_count * |
| 147 | sizeof(struct ffa_memory_region_constituent); |
| 148 | } |
| 149 | if (fragment_length != NULL) { |
| 150 | *fragment_length = |
| 151 | constituents_offset + |
| 152 | count_to_copy * |
| 153 | sizeof(struct ffa_memory_region_constituent); |
| 154 | } |
| 155 | |
| 156 | return composite_memory_region->constituent_count - count_to_copy; |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 160 | * Initialises the given `ffa_memory_region` and copies as many as possible of |
| 161 | * the given constituents to it. |
| 162 | * |
| 163 | * Returns the number of constituents remaining which wouldn't fit, and (via |
| 164 | * return parameters) the size in bytes of the first fragment of data copied to |
| 165 | * `memory_region` (attributes, constituents and memory region header size), and |
| 166 | * the total size of the memory sharing message including all constituents. |
| 167 | */ |
| 168 | uint32_t ffa_memory_region_init_single_receiver( |
| 169 | struct ffa_memory_region *memory_region, size_t memory_region_max_size, |
| 170 | ffa_vm_id_t sender, ffa_vm_id_t receiver, |
| 171 | const struct ffa_memory_region_constituent constituents[], |
| 172 | uint32_t constituent_count, uint32_t tag, |
| 173 | ffa_memory_region_flags_t flags, enum ffa_data_access data_access, |
| 174 | enum ffa_instruction_access instruction_access, |
| 175 | enum ffa_memory_type type, enum ffa_memory_cacheability cacheability, |
| 176 | enum ffa_memory_shareability shareability, uint32_t *total_length, |
| 177 | uint32_t *fragment_length) |
| 178 | { |
| J-Alves | f4eecf7 | 2022-07-20 16:05:34 +0100 | [diff] [blame^] | 179 | struct ffa_memory_access receiver_access; |
| 180 | |
| 181 | ffa_memory_access_init_permissions(&receiver_access, receiver, |
| 182 | data_access, instruction_access, 0); |
| 183 | |
| 184 | return ffa_memory_region_init( |
| 185 | memory_region, memory_region_max_size, sender, &receiver_access, |
| 186 | 1, constituents, constituent_count, tag, flags, type, |
| 187 | cacheability, shareability, total_length, fragment_length); |
| 188 | } |
| 189 | |
| 190 | uint32_t ffa_memory_region_init( |
| 191 | struct ffa_memory_region *memory_region, size_t memory_region_max_size, |
| 192 | ffa_vm_id_t sender, struct ffa_memory_access receivers[], |
| 193 | uint32_t receiver_count, |
| 194 | const struct ffa_memory_region_constituent constituents[], |
| 195 | uint32_t constituent_count, uint32_t tag, |
| 196 | ffa_memory_region_flags_t flags, enum ffa_memory_type type, |
| 197 | enum ffa_memory_cacheability cacheability, |
| 198 | enum ffa_memory_shareability shareability, uint32_t *total_length, |
| 199 | uint32_t *fragment_length) |
| 200 | { |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 201 | ffa_memory_attributes_t attributes = 0; |
| 202 | |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 203 | /* Set memory region's page attributes. */ |
| 204 | ffa_set_memory_type_attr(&attributes, type); |
| 205 | ffa_set_memory_cacheability_attr(&attributes, cacheability); |
| 206 | ffa_set_memory_shareability_attr(&attributes, shareability); |
| 207 | |
| 208 | ffa_memory_region_init_header(memory_region, sender, attributes, flags, |
| J-Alves | f4eecf7 | 2022-07-20 16:05:34 +0100 | [diff] [blame^] | 209 | 0, tag, receiver_count); |
| 210 | |
| 211 | #if defined(__linux__) && defined(__KERNEL__) |
| 212 | memcpy(memory_region->receivers, receivers, |
| 213 | receiver_count * sizeof(struct ffa_memory_access)); |
| 214 | #else |
| 215 | memcpy_s(memory_region->receivers, |
| 216 | MAX_MEM_SHARE_RECIPIENTS * sizeof(struct ffa_memory_access), |
| 217 | receivers, receiver_count * sizeof(struct ffa_memory_access)); |
| 218 | #endif |
| J-Alves | f55620e | 2022-04-25 14:03:02 +0100 | [diff] [blame] | 219 | |
| 220 | return ffa_memory_region_init_constituents( |
| 221 | memory_region, memory_region_max_size, constituents, |
| 222 | constituent_count, total_length, fragment_length); |
| 223 | } |
| 224 | |
| 225 | /** |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 226 | * Initialises the given `ffa_memory_region` to be used for an |
| 227 | * `FFA_MEM_RETRIEVE_REQ` by the receiver of a memory transaction. |
| 228 | * |
| 229 | * Returns the size of the message written. |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 230 | */ |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 231 | uint32_t ffa_memory_retrieve_request_init( |
| 232 | struct ffa_memory_region *memory_region, ffa_memory_handle_t handle, |
| 233 | ffa_vm_id_t sender, ffa_vm_id_t receiver, uint32_t tag, |
| 234 | ffa_memory_region_flags_t flags, enum ffa_data_access data_access, |
| 235 | enum ffa_instruction_access instruction_access, |
| 236 | enum ffa_memory_type type, enum ffa_memory_cacheability cacheability, |
| 237 | enum ffa_memory_shareability shareability) |
| 238 | { |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 239 | ffa_memory_attributes_t attributes = 0; |
| 240 | |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 241 | /* Set memory region's page attributes. */ |
| 242 | ffa_set_memory_type_attr(&attributes, type); |
| 243 | ffa_set_memory_cacheability_attr(&attributes, cacheability); |
| 244 | ffa_set_memory_shareability_attr(&attributes, shareability); |
| 245 | |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 246 | ffa_memory_region_init_header(memory_region, sender, attributes, flags, |
| J-Alves | b44bb7d | 2022-04-25 15:43:46 +0100 | [diff] [blame] | 247 | handle, tag, 1); |
| 248 | ffa_memory_access_init_permissions(&memory_region->receivers[0], |
| 249 | receiver, data_access, |
| 250 | instruction_access, 0); |
| 251 | |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 252 | /* |
| 253 | * Offset 0 in this case means that the hypervisor should allocate the |
| 254 | * address ranges. This is the only configuration supported by Hafnium, |
| 255 | * as it enforces 1:1 mappings in the stage 2 page tables. |
| 256 | */ |
| 257 | memory_region->receivers[0].composite_memory_region_offset = 0; |
| 258 | memory_region->receivers[0].reserved_0 = 0; |
| 259 | |
| 260 | return sizeof(struct ffa_memory_region) + |
| 261 | memory_region->receiver_count * sizeof(struct ffa_memory_access); |
| 262 | } |
| 263 | |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 264 | /** |
| 265 | * Initialises the given `ffa_memory_region` to be used for an |
| 266 | * `FFA_MEM_RETRIEVE_REQ` from the hypervisor to the TEE. |
| 267 | * |
| 268 | * Returns the size of the message written. |
| 269 | */ |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 270 | uint32_t ffa_memory_lender_retrieve_request_init( |
| 271 | struct ffa_memory_region *memory_region, ffa_memory_handle_t handle, |
| 272 | ffa_vm_id_t sender) |
| 273 | { |
| 274 | memory_region->sender = sender; |
| 275 | memory_region->attributes = 0; |
| 276 | memory_region->reserved_0 = 0; |
| 277 | memory_region->flags = 0; |
| 278 | memory_region->reserved_1 = 0; |
| 279 | memory_region->handle = handle; |
| 280 | memory_region->tag = 0; |
| 281 | memory_region->receiver_count = 0; |
| 282 | |
| 283 | return sizeof(struct ffa_memory_region); |
| 284 | } |
| 285 | |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 286 | /** |
| 287 | * Initialises the given `ffa_memory_region` to be used for an |
| 288 | * `FFA_MEM_RETRIEVE_RESP`, including the given constituents for the first |
| 289 | * fragment. |
| 290 | * |
| 291 | * Returns true on success, or false if the given constituents won't all fit in |
| 292 | * the first fragment. |
| 293 | */ |
| 294 | bool ffa_retrieved_memory_region_init( |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 295 | struct ffa_memory_region *response, size_t response_max_size, |
| 296 | ffa_vm_id_t sender, ffa_memory_attributes_t attributes, |
| 297 | ffa_memory_region_flags_t flags, ffa_memory_handle_t handle, |
| 298 | ffa_vm_id_t receiver, ffa_memory_access_permissions_t permissions, |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 299 | uint32_t page_count, uint32_t total_constituent_count, |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 300 | const struct ffa_memory_region_constituent constituents[], |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 301 | uint32_t fragment_constituent_count, uint32_t *total_length, |
| 302 | uint32_t *fragment_length) |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 303 | { |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 304 | struct ffa_composite_memory_region *composite_memory_region; |
| 305 | uint32_t i; |
| 306 | uint32_t constituents_offset; |
| 307 | |
| 308 | ffa_memory_region_init_header(response, sender, attributes, flags, |
| J-Alves | b44bb7d | 2022-04-25 15:43:46 +0100 | [diff] [blame] | 309 | handle, 0, 1); |
| 310 | /* |
| 311 | * Initialized here as in memory retrieve responses we currently expect |
| 312 | * one borrower to be specified. |
| 313 | */ |
| 314 | ffa_memory_access_init_permissions(&response->receivers[0], receiver, 0, |
| J-Alves | d9a13cd | 2022-06-15 18:38:11 +0100 | [diff] [blame] | 315 | 0, flags); |
| 316 | response->receivers[0].receiver_permissions.permissions = permissions; |
| J-Alves | b44bb7d | 2022-04-25 15:43:46 +0100 | [diff] [blame] | 317 | |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 318 | /* |
| 319 | * Note that `sizeof(struct_ffa_memory_region)` and `sizeof(struct |
| 320 | * ffa_memory_access)` must both be multiples of 16 (as verified by the |
| 321 | * asserts in `ffa_memory.c`, so it is guaranteed that the offset we |
| 322 | * calculate here is aligned to a 64-bit boundary and so 64-bit values |
| 323 | * can be copied without alignment faults. |
| 324 | */ |
| 325 | response->receivers[0].composite_memory_region_offset = |
| 326 | sizeof(struct ffa_memory_region) + |
| 327 | response->receiver_count * sizeof(struct ffa_memory_access); |
| 328 | |
| 329 | composite_memory_region = ffa_memory_region_get_composite(response, 0); |
| 330 | composite_memory_region->page_count = page_count; |
| 331 | composite_memory_region->constituent_count = total_constituent_count; |
| 332 | composite_memory_region->reserved_0 = 0; |
| 333 | |
| 334 | constituents_offset = |
| 335 | response->receivers[0].composite_memory_region_offset + |
| 336 | sizeof(struct ffa_composite_memory_region); |
| 337 | if (constituents_offset + |
| 338 | fragment_constituent_count * |
| 339 | sizeof(struct ffa_memory_region_constituent) > |
| 340 | response_max_size) { |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | for (i = 0; i < fragment_constituent_count; ++i) { |
| 345 | composite_memory_region->constituents[i] = constituents[i]; |
| 346 | } |
| 347 | |
| 348 | if (total_length != NULL) { |
| 349 | *total_length = |
| 350 | constituents_offset + |
| 351 | composite_memory_region->constituent_count * |
| 352 | sizeof(struct ffa_memory_region_constituent); |
| 353 | } |
| 354 | if (fragment_length != NULL) { |
| 355 | *fragment_length = |
| 356 | constituents_offset + |
| 357 | fragment_constituent_count * |
| 358 | sizeof(struct ffa_memory_region_constituent); |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | uint32_t ffa_memory_fragment_init( |
| 365 | struct ffa_memory_region_constituent *fragment, |
| 366 | size_t fragment_max_size, |
| 367 | const struct ffa_memory_region_constituent constituents[], |
| 368 | uint32_t constituent_count, uint32_t *fragment_length) |
| 369 | { |
| 370 | uint32_t fragment_max_constituents = |
| 371 | fragment_max_size / |
| 372 | sizeof(struct ffa_memory_region_constituent); |
| 373 | uint32_t count_to_copy = constituent_count; |
| 374 | uint32_t i; |
| 375 | |
| 376 | if (count_to_copy > fragment_max_constituents) { |
| 377 | count_to_copy = fragment_max_constituents; |
| 378 | } |
| 379 | |
| 380 | for (i = 0; i < count_to_copy; ++i) { |
| Raghu Krishnamurthy | 03f21d2 | 2021-11-02 18:39:23 -0700 | [diff] [blame] | 381 | ffa_copy_memory_region_constituents(&fragment[i], |
| 382 | &constituents[i]); |
| Andrew Walbran | ca808b1 | 2020-05-15 17:22:28 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | if (fragment_length != NULL) { |
| 386 | *fragment_length = count_to_copy * |
| 387 | sizeof(struct ffa_memory_region_constituent); |
| 388 | } |
| 389 | |
| 390 | return constituent_count - count_to_copy; |
| Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 391 | } |
| Federico Recanati | 392be39 | 2022-02-08 20:53:03 +0100 | [diff] [blame] | 392 | |
| 393 | static void ffa_composite_memory_region_init( |
| 394 | struct ffa_composite_memory_region *composite, uint64_t address, |
| 395 | uint32_t page_count) |
| 396 | { |
| 397 | composite->page_count = page_count; |
| 398 | composite->constituent_count = 1; |
| 399 | composite->reserved_0 = 0; |
| 400 | |
| 401 | composite->constituents[0].page_count = page_count; |
| 402 | composite->constituents[0].address = address; |
| 403 | composite->constituents[0].reserved = 0; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Initialises the given `ffa_endpoint_rx_tx_descriptor` to be used for an |
| 408 | * `FFA_RXTX_MAP` forwarding. |
| 409 | * Each buffer is described by an `ffa_composite_memory_region` containing |
| 410 | * one `ffa_memory_region_constituent`. |
| 411 | */ |
| 412 | void ffa_endpoint_rx_tx_descriptor_init( |
| 413 | struct ffa_endpoint_rx_tx_descriptor *desc, ffa_vm_id_t endpoint_id, |
| 414 | uint64_t rx_address, uint64_t tx_address) |
| 415 | { |
| 416 | desc->endpoint_id = endpoint_id; |
| 417 | desc->reserved = 0; |
| 418 | desc->pad = 0; |
| 419 | |
| 420 | /* |
| 421 | * RX's composite descriptor is allocated after the enpoint descriptor. |
| 422 | * `sizeof(struct ffa_endpoint_rx_tx_descriptor)` is guaranteed to be |
| 423 | * 16-byte aligned. |
| 424 | */ |
| 425 | desc->rx_offset = sizeof(struct ffa_endpoint_rx_tx_descriptor); |
| 426 | |
| 427 | ffa_composite_memory_region_init( |
| 428 | (struct ffa_composite_memory_region *)((uintptr_t)desc + |
| 429 | desc->rx_offset), |
| 430 | rx_address, HF_MAILBOX_SIZE / FFA_PAGE_SIZE); |
| 431 | |
| 432 | /* |
| 433 | * TX's composite descriptor is allocated after the RX descriptor. |
| 434 | * `sizeof(struct ffa_composite_memory_region)` and |
| 435 | * `sizeof(struct ffa_memory_region_constituent)` are guaranteed to be |
| 436 | * 16-byte aligned in ffa_memory.c. |
| 437 | */ |
| 438 | desc->tx_offset = desc->rx_offset + |
| 439 | sizeof(struct ffa_composite_memory_region) + |
| 440 | sizeof(struct ffa_memory_region_constituent); |
| 441 | |
| 442 | ffa_composite_memory_region_init( |
| 443 | (struct ffa_composite_memory_region *)((uintptr_t)desc + |
| 444 | desc->tx_offset), |
| 445 | tx_address, HF_MAILBOX_SIZE / FFA_PAGE_SIZE); |
| 446 | } |