1 /* 2 * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 #include <assert.h> 7 #include <errno.h> 8 9 #include <common/debug.h> 10 #include <common/runtime_svc.h> 11 #include <lib/object_pool.h> 12 #include <lib/spinlock.h> 13 #include <lib/xlat_tables/xlat_tables_v2.h> 14 #include <services/ffa_svc.h> 15 #include "spmc.h" 16 #include "spmc_shared_mem.h" 17 18 #include <platform_def.h> 19 20 /** 21 * struct spmc_shmem_obj - Shared memory object. 22 * @desc_size: Size of @desc. 23 * @desc_filled: Size of @desc already received. 24 * @in_use: Number of clients that have called ffa_mem_retrieve_req 25 * without a matching ffa_mem_relinquish call. 26 * @desc: FF-A memory region descriptor passed in ffa_mem_share. 27 */ 28 struct spmc_shmem_obj { 29 size_t desc_size; 30 size_t desc_filled; 31 size_t in_use; 32 struct ffa_mtd desc; 33 }; 34 35 /* 36 * Declare our data structure to store the metadata of memory share requests. 37 * The main datastore is allocated on a per platform basis to ensure enough 38 * storage can be made available. 39 * The address of the data store will be populated by the SPMC during its 40 * initialization. 41 */ 42 43 struct spmc_shmem_obj_state spmc_shmem_obj_state = { 44 /* Set start value for handle so top 32 bits are needed quickly. */ 45 .next_handle = 0xffffffc0U, 46 }; 47 48 /** 49 * spmc_shmem_obj_size - Convert from descriptor size to object size. 50 * @desc_size: Size of struct ffa_memory_region_descriptor object. 51 * 52 * Return: Size of struct spmc_shmem_obj object. 53 */ 54 static size_t spmc_shmem_obj_size(size_t desc_size) 55 { 56 return desc_size + offsetof(struct spmc_shmem_obj, desc); 57 } 58 59 /** 60 * spmc_shmem_obj_alloc - Allocate struct spmc_shmem_obj. 61 * @state: Global state. 62 * @desc_size: Size of struct ffa_memory_region_descriptor object that 63 * allocated object will hold. 64 * 65 * Return: Pointer to newly allocated object, or %NULL if there not enough space 66 * left. The returned pointer is only valid while @state is locked, to 67 * used it again after unlocking @state, spmc_shmem_obj_lookup must be 68 * called. 69 */ 70 static struct spmc_shmem_obj * 71 spmc_shmem_obj_alloc(struct spmc_shmem_obj_state *state, size_t desc_size) 72 { 73 struct spmc_shmem_obj *obj; 74 size_t free = state->data_size - state->allocated; 75 76 if (state->data == NULL) { 77 ERROR("Missing shmem datastore!\n"); 78 return NULL; 79 } 80 81 if (spmc_shmem_obj_size(desc_size) > free) { 82 WARN("%s(0x%zx) failed, free 0x%zx\n", 83 __func__, desc_size, free); 84 return NULL; 85 } 86 obj = (struct spmc_shmem_obj *)(state->data + state->allocated); 87 obj->desc = (struct ffa_mtd) {0}; 88 obj->desc_size = desc_size; 89 obj->desc_filled = 0; 90 obj->in_use = 0; 91 state->allocated += spmc_shmem_obj_size(desc_size); 92 return obj; 93 } 94 95 /** 96 * spmc_shmem_obj_free - Free struct spmc_shmem_obj. 97 * @state: Global state. 98 * @obj: Object to free. 99 * 100 * Release memory used by @obj. Other objects may move, so on return all 101 * pointers to struct spmc_shmem_obj object should be considered invalid, not 102 * just @obj. 103 * 104 * The current implementation always compacts the remaining objects to simplify 105 * the allocator and to avoid fragmentation. 106 */ 107 108 static void spmc_shmem_obj_free(struct spmc_shmem_obj_state *state, 109 struct spmc_shmem_obj *obj) 110 { 111 size_t free_size = spmc_shmem_obj_size(obj->desc_size); 112 uint8_t *shift_dest = (uint8_t *)obj; 113 uint8_t *shift_src = shift_dest + free_size; 114 size_t shift_size = state->allocated - (shift_src - state->data); 115 116 if (shift_size != 0U) { 117 memmove(shift_dest, shift_src, shift_size); 118 } 119 state->allocated -= free_size; 120 } 121 122 /** 123 * spmc_shmem_obj_lookup - Lookup struct spmc_shmem_obj by handle. 124 * @state: Global state. 125 * @handle: Unique handle of object to return. 126 * 127 * Return: struct spmc_shmem_obj_state object with handle matching @handle. 128 * %NULL, if not object in @state->data has a matching handle. 129 */ 130 static struct spmc_shmem_obj * 131 spmc_shmem_obj_lookup(struct spmc_shmem_obj_state *state, uint64_t handle) 132 { 133 uint8_t *curr = state->data; 134 135 while (curr - state->data < state->allocated) { 136 struct spmc_shmem_obj *obj = (struct spmc_shmem_obj *)curr; 137 138 if (obj->desc.handle == handle) { 139 return obj; 140 } 141 curr += spmc_shmem_obj_size(obj->desc_size); 142 } 143 return NULL; 144 } 145 146 /** 147 * spmc_shmem_obj_get_next - Get the next memory object from an offset. 148 * @offset: Offset used to track which objects have previously been 149 * returned. 150 * 151 * Return: the next struct spmc_shmem_obj_state object from the provided 152 * offset. 153 * %NULL, if there are no more objects. 154 */ 155 static struct spmc_shmem_obj * 156 spmc_shmem_obj_get_next(struct spmc_shmem_obj_state *state, size_t *offset) 157 { 158 uint8_t *curr = state->data + *offset; 159 160 if (curr - state->data < state->allocated) { 161 struct spmc_shmem_obj *obj = (struct spmc_shmem_obj *)curr; 162 163 *offset += spmc_shmem_obj_size(obj->desc_size); 164 165 return obj; 166 } 167 return NULL; 168 } 169 170 /******************************************************************************* 171 * FF-A memory descriptor helper functions. 172 ******************************************************************************/ 173 /** 174 * spmc_shmem_obj_get_emad - Get the emad from a given index depending on the 175 * clients FF-A version. 176 * @desc: The memory transaction descriptor. 177 * @index: The index of the emad element to be accessed. 178 * @ffa_version: FF-A version of the provided structure. 179 * @emad_size: Will be populated with the size of the returned emad 180 * descriptor. 181 * Return: A pointer to the requested emad structure. 182 */ 183 static void * 184 spmc_shmem_obj_get_emad(const struct ffa_mtd *desc, uint32_t index, 185 uint32_t ffa_version, size_t *emad_size) 186 { 187 uint8_t *emad; 188 /* 189 * If the caller is using FF-A v1.0 interpret the descriptor as a v1.0 190 * format, otherwise assume it is a v1.1 format. 191 */ 192 if (ffa_version == MAKE_FFA_VERSION(1, 0)) { 193 /* Cast our descriptor to the v1.0 format. */ 194 struct ffa_mtd_v1_0 *mtd_v1_0 = 195 (struct ffa_mtd_v1_0 *) desc; 196 emad = (uint8_t *) &(mtd_v1_0->emad); 197 *emad_size = sizeof(struct ffa_emad_v1_0); 198 } else { 199 if (!is_aligned(desc->emad_offset, 16)) { 200 WARN("Emad offset is not aligned.\n"); 201 return NULL; 202 } 203 emad = ((uint8_t *) desc + desc->emad_offset); 204 *emad_size = desc->emad_size; 205 } 206 return (emad + (*emad_size * index)); 207 } 208 209 /** 210 * spmc_shmem_obj_get_comp_mrd - Get comp_mrd from a mtd struct based on the 211 * FF-A version of the descriptor. 212 * @obj: Object containing ffa_memory_region_descriptor. 213 * 214 * Return: struct ffa_comp_mrd object corresponding to the composite memory 215 * region descriptor. 216 */ 217 static struct ffa_comp_mrd * 218 spmc_shmem_obj_get_comp_mrd(struct spmc_shmem_obj *obj, uint32_t ffa_version) 219 { 220 size_t emad_size; 221 /* 222 * The comp_mrd_offset field of the emad descriptor remains consistent 223 * between FF-A versions therefore we can use the v1.0 descriptor here 224 * in all cases. 225 */ 226 struct ffa_emad_v1_0 *emad = spmc_shmem_obj_get_emad(&obj->desc, 0, 227 ffa_version, 228 &emad_size); 229 /* Ensure the emad array was found. */ 230 if (emad == NULL) { 231 return NULL; 232 } 233 234 /* Ensure the composite descriptor offset is aligned. */ 235 if (!is_aligned(emad->comp_mrd_offset, 8)) { 236 WARN("Unaligned composite memory region descriptor offset.\n"); 237 return NULL; 238 } 239 240 return (struct ffa_comp_mrd *) 241 ((uint8_t *)(&obj->desc) + emad->comp_mrd_offset); 242 } 243 244 /** 245 * spmc_shmem_obj_ffa_constituent_size - Calculate variable size part of obj. 246 * @obj: Object containing ffa_memory_region_descriptor. 247 * 248 * Return: Size of ffa_constituent_memory_region_descriptors in @obj. 249 */ 250 static size_t 251 spmc_shmem_obj_ffa_constituent_size(struct spmc_shmem_obj *obj, 252 uint32_t ffa_version) 253 { 254 struct ffa_comp_mrd *comp_mrd; 255 256 comp_mrd = spmc_shmem_obj_get_comp_mrd(obj, ffa_version); 257 if (comp_mrd == NULL) { 258 return 0; 259 } 260 return comp_mrd->address_range_count * sizeof(struct ffa_cons_mrd); 261 } 262 263 /* 264 * Compare two memory regions to determine if any range overlaps with another 265 * ongoing memory transaction. 266 */ 267 static bool 268 overlapping_memory_regions(struct ffa_comp_mrd *region1, 269 struct ffa_comp_mrd *region2) 270 { 271 uint64_t region1_start; 272 uint64_t region1_size; 273 uint64_t region1_end; 274 uint64_t region2_start; 275 uint64_t region2_size; 276 uint64_t region2_end; 277 278 assert(region1 != NULL); 279 assert(region2 != NULL); 280 281 if (region1 == region2) { 282 return true; 283 } 284 285 /* 286 * Check each memory region in the request against existing 287 * transactions. 288 */ 289 for (size_t i = 0; i < region1->address_range_count; i++) { 290 291 region1_start = region1->address_range_array[i].address; 292 region1_size = 293 region1->address_range_array[i].page_count * 294 PAGE_SIZE_4KB; 295 region1_end = region1_start + region1_size; 296 297 for (size_t j = 0; j < region2->address_range_count; j++) { 298 299 region2_start = region2->address_range_array[j].address; 300 region2_size = 301 region2->address_range_array[j].page_count * 302 PAGE_SIZE_4KB; 303 region2_end = region2_start + region2_size; 304 305 if ((region1_start >= region2_start && 306 region1_start < region2_end) || 307 (region1_end >= region2_start 308 && region1_end < region2_end)) { 309 WARN("Overlapping mem regions 0x%lx-0x%lx & 0x%lx-0x%lx\n", 310 region1_start, region1_end, 311 region2_start, region2_end); 312 return true; 313 } 314 } 315 } 316 return false; 317 } 318 319 /******************************************************************************* 320 * FF-A v1.0 Memory Descriptor Conversion Helpers. 321 ******************************************************************************/ 322 /** 323 * spmc_shm_get_v1_1_descriptor_size - Calculate the required size for a v1.1 324 * converted descriptor. 325 * @orig: The original v1.0 memory transaction descriptor. 326 * @desc_size: The size of the original v1.0 memory transaction descriptor. 327 * 328 * Return: the size required to store the descriptor store in the v1.1 format. 329 */ 330 static size_t 331 spmc_shm_get_v1_1_descriptor_size(struct ffa_mtd_v1_0 *orig, size_t desc_size) 332 { 333 size_t size = 0; 334 struct ffa_comp_mrd *mrd; 335 struct ffa_emad_v1_0 *emad_array = orig->emad; 336 337 /* Get the size of the v1.1 descriptor. */ 338 size += sizeof(struct ffa_mtd); 339 340 /* Add the size of the emad descriptors. */ 341 size += orig->emad_count * sizeof(struct ffa_emad_v1_0); 342 343 /* Add the size of the composite mrds. */ 344 size += sizeof(struct ffa_comp_mrd); 345 346 /* Add the size of the constituent mrds. */ 347 mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig + 348 emad_array[0].comp_mrd_offset); 349 350 /* Check the calculated address is within the memory descriptor. */ 351 if ((uintptr_t) mrd >= (uintptr_t)((uint8_t *) orig + desc_size)) { 352 return 0; 353 } 354 size += mrd->address_range_count * sizeof(struct ffa_cons_mrd); 355 356 return size; 357 } 358 359 /** 360 * spmc_shm_get_v1_0_descriptor_size - Calculate the required size for a v1.0 361 * converted descriptor. 362 * @orig: The original v1.1 memory transaction descriptor. 363 * @desc_size: The size of the original v1.1 memory transaction descriptor. 364 * 365 * Return: the size required to store the descriptor store in the v1.0 format. 366 */ 367 static size_t 368 spmc_shm_get_v1_0_descriptor_size(struct ffa_mtd *orig, size_t desc_size) 369 { 370 size_t size = 0; 371 struct ffa_comp_mrd *mrd; 372 struct ffa_emad_v1_0 *emad_array = (struct ffa_emad_v1_0 *) 373 ((uint8_t *) orig + 374 orig->emad_offset); 375 376 /* Get the size of the v1.0 descriptor. */ 377 size += sizeof(struct ffa_mtd_v1_0); 378 379 /* Add the size of the v1.0 emad descriptors. */ 380 size += orig->emad_count * sizeof(struct ffa_emad_v1_0); 381 382 /* Add the size of the composite mrds. */ 383 size += sizeof(struct ffa_comp_mrd); 384 385 /* Add the size of the constituent mrds. */ 386 mrd = (struct ffa_comp_mrd *) ((uint8_t *) orig + 387 emad_array[0].comp_mrd_offset); 388 389 /* Check the calculated address is within the memory descriptor. */ 390 if ((uintptr_t) mrd >= (uintptr_t)((uint8_t *) orig + desc_size)) { 391 return 0; 392 } 393 size += mrd->address_range_count * sizeof(struct ffa_cons_mrd); 394 395 return size; 396 } 397 398 /** 399 * spmc_shm_convert_shmem_obj_from_v1_0 - Converts a given v1.0 memory object. 400 * @out_obj: The shared memory object to populate the converted descriptor. 401 * @orig: The shared memory object containing the v1.0 descriptor. 402 * 403 * Return: true if the conversion is successful else false. 404 */ 405 static bool 406 spmc_shm_convert_shmem_obj_from_v1_0(struct spmc_shmem_obj *out_obj, 407 struct spmc_shmem_obj *orig) 408 { 409 struct ffa_mtd_v1_0 *mtd_orig = (struct ffa_mtd_v1_0 *) &orig->desc; 410 struct ffa_mtd *out = &out_obj->desc; 411 struct ffa_emad_v1_0 *emad_array_in; 412 struct ffa_emad_v1_0 *emad_array_out; 413 struct ffa_comp_mrd *mrd_in; 414 struct ffa_comp_mrd *mrd_out; 415 416 size_t mrd_in_offset; 417 size_t mrd_out_offset; 418 size_t mrd_size = 0; 419 420 /* Populate the new descriptor format from the v1.0 struct. */ 421 out->sender_id = mtd_orig->sender_id; 422 out->memory_region_attributes = mtd_orig->memory_region_attributes; 423 out->flags = mtd_orig->flags; 424 out->handle = mtd_orig->handle; 425 out->tag = mtd_orig->tag; 426 out->emad_count = mtd_orig->emad_count; 427 out->emad_size = sizeof(struct ffa_emad_v1_0); 428 429 /* 430 * We will locate the emad descriptors directly after the ffa_mtd 431 * struct. This will be 8-byte aligned. 432 */ 433 out->emad_offset = sizeof(struct ffa_mtd); 434 435 emad_array_in = mtd_orig->emad; 436 emad_array_out = (struct ffa_emad_v1_0 *) 437 ((uint8_t *) out + out->emad_offset); 438 439 /* Copy across the emad structs. */ 440 for (unsigned int i = 0U; i < out->emad_count; i++) { 441 memcpy(&emad_array_out[i], &emad_array_in[i], 442 sizeof(struct ffa_emad_v1_0)); 443 } 444 445 /* Place the mrd descriptors after the end of the emad descriptors.*/ 446 mrd_in_offset = emad_array_in->comp_mrd_offset; 447 mrd_out_offset = out->emad_offset + (out->emad_size * out->emad_count); 448 mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset); 449 450 /* Add the size of the composite memory region descriptor. */ 451 mrd_size += sizeof(struct ffa_comp_mrd); 452 453 /* Find the mrd descriptor. */ 454 mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset); 455 456 /* Add the size of the constituent memory region descriptors. */ 457 mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd); 458 459 /* 460 * Update the offset in the emads by the delta between the input and 461 * output addresses. 462 */ 463 for (unsigned int i = 0U; i < out->emad_count; i++) { 464 emad_array_out[i].comp_mrd_offset = 465 emad_array_in[i].comp_mrd_offset + 466 (mrd_out_offset - mrd_in_offset); 467 } 468 469 /* Verify that we stay within bound of the memory descriptors. */ 470 if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) > 471 (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) || 472 ((uintptr_t)((uint8_t *) mrd_out + mrd_size) > 473 (uintptr_t)((uint8_t *) out + out_obj->desc_size))) { 474 ERROR("%s: Invalid mrd structure.\n", __func__); 475 return false; 476 } 477 478 /* Copy the mrd descriptors directly. */ 479 memcpy(mrd_out, mrd_in, mrd_size); 480 481 return true; 482 } 483 484 /** 485 * spmc_shm_convert_mtd_to_v1_0 - Converts a given v1.1 memory object to 486 * v1.0 memory object. 487 * @out_obj: The shared memory object to populate the v1.0 descriptor. 488 * @orig: The shared memory object containing the v1.1 descriptor. 489 * 490 * Return: true if the conversion is successful else false. 491 */ 492 static bool 493 spmc_shm_convert_mtd_to_v1_0(struct spmc_shmem_obj *out_obj, 494 struct spmc_shmem_obj *orig) 495 { 496 struct ffa_mtd *mtd_orig = &orig->desc; 497 struct ffa_mtd_v1_0 *out = (struct ffa_mtd_v1_0 *) &out_obj->desc; 498 struct ffa_emad_v1_0 *emad_in; 499 struct ffa_emad_v1_0 *emad_array_in; 500 struct ffa_emad_v1_0 *emad_array_out; 501 struct ffa_comp_mrd *mrd_in; 502 struct ffa_comp_mrd *mrd_out; 503 504 size_t mrd_in_offset; 505 size_t mrd_out_offset; 506 size_t emad_out_array_size; 507 size_t mrd_size = 0; 508 509 /* Populate the v1.0 descriptor format from the v1.1 struct. */ 510 out->sender_id = mtd_orig->sender_id; 511 out->memory_region_attributes = mtd_orig->memory_region_attributes; 512 out->flags = mtd_orig->flags; 513 out->handle = mtd_orig->handle; 514 out->tag = mtd_orig->tag; 515 out->emad_count = mtd_orig->emad_count; 516 517 /* Determine the location of the emad array in both descriptors. */ 518 emad_array_in = (struct ffa_emad_v1_0 *) 519 ((uint8_t *) mtd_orig + mtd_orig->emad_offset); 520 emad_array_out = out->emad; 521 522 /* Copy across the emad structs. */ 523 emad_in = emad_array_in; 524 for (unsigned int i = 0U; i < out->emad_count; i++) { 525 memcpy(&emad_array_out[i], emad_in, 526 sizeof(struct ffa_emad_v1_0)); 527 528 emad_in += mtd_orig->emad_size; 529 } 530 531 /* Place the mrd descriptors after the end of the emad descriptors. */ 532 emad_out_array_size = sizeof(struct ffa_emad_v1_0) * out->emad_count; 533 534 mrd_out_offset = (uint8_t *) out->emad - (uint8_t *) out + 535 emad_out_array_size; 536 537 mrd_out = (struct ffa_comp_mrd *) ((uint8_t *) out + mrd_out_offset); 538 539 mrd_in_offset = mtd_orig->emad_offset + 540 (mtd_orig->emad_size * mtd_orig->emad_count); 541 542 /* Add the size of the composite memory region descriptor. */ 543 mrd_size += sizeof(struct ffa_comp_mrd); 544 545 /* Find the mrd descriptor. */ 546 mrd_in = (struct ffa_comp_mrd *) ((uint8_t *) mtd_orig + mrd_in_offset); 547 548 /* Add the size of the constituent memory region descriptors. */ 549 mrd_size += mrd_in->address_range_count * sizeof(struct ffa_cons_mrd); 550 551 /* 552 * Update the offset in the emads by the delta between the input and 553 * output addresses. 554 */ 555 emad_in = emad_array_in; 556 557 for (unsigned int i = 0U; i < out->emad_count; i++) { 558 emad_array_out[i].comp_mrd_offset = emad_in->comp_mrd_offset + 559 (mrd_out_offset - 560 mrd_in_offset); 561 emad_in += mtd_orig->emad_size; 562 } 563 564 /* Verify that we stay within bound of the memory descriptors. */ 565 if ((uintptr_t)((uint8_t *) mrd_in + mrd_size) > 566 (uintptr_t)((uint8_t *) mtd_orig + orig->desc_size) || 567 ((uintptr_t)((uint8_t *) mrd_out + mrd_size) > 568 (uintptr_t)((uint8_t *) out + out_obj->desc_size))) { 569 ERROR("%s: Invalid mrd structure.\n", __func__); 570 return false; 571 } 572 573 /* Copy the mrd descriptors directly. */ 574 memcpy(mrd_out, mrd_in, mrd_size); 575 576 return true; 577 } 578 579 /** 580 * spmc_populate_ffa_v1_0_descriptor - Converts a given v1.1 memory object to 581 * the v1.0 format and populates the 582 * provided buffer. 583 * @dst: Buffer to populate v1.0 ffa_memory_region_descriptor. 584 * @orig_obj: Object containing v1.1 ffa_memory_region_descriptor. 585 * @buf_size: Size of the buffer to populate. 586 * @offset: The offset of the converted descriptor to copy. 587 * @copy_size: Will be populated with the number of bytes copied. 588 * @out_desc_size: Will be populated with the total size of the v1.0 589 * descriptor. 590 * 591 * Return: 0 if conversion and population succeeded. 592 * Note: This function invalidates the reference to @orig therefore 593 * `spmc_shmem_obj_lookup` must be called if further usage is required. 594 */ 595 static uint32_t 596 spmc_populate_ffa_v1_0_descriptor(void *dst, struct spmc_shmem_obj *orig_obj, 597 size_t buf_size, size_t offset, 598 size_t *copy_size, size_t *v1_0_desc_size) 599 { 600 struct spmc_shmem_obj *v1_0_obj; 601 602 /* Calculate the size that the v1.0 descriptor will require. */ 603 *v1_0_desc_size = spmc_shm_get_v1_0_descriptor_size( 604 &orig_obj->desc, orig_obj->desc_size); 605 606 if (*v1_0_desc_size == 0) { 607 ERROR("%s: cannot determine size of descriptor.\n", 608 __func__); 609 return FFA_ERROR_INVALID_PARAMETER; 610 } 611 612 /* Get a new obj to store the v1.0 descriptor. */ 613 v1_0_obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state, 614 *v1_0_desc_size); 615 616 if (!v1_0_obj) { 617 return FFA_ERROR_NO_MEMORY; 618 } 619 620 /* Perform the conversion from v1.1 to v1.0. */ 621 if (!spmc_shm_convert_mtd_to_v1_0(v1_0_obj, orig_obj)) { 622 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj); 623 return FFA_ERROR_INVALID_PARAMETER; 624 } 625 626 *copy_size = MIN(v1_0_obj->desc_size - offset, buf_size); 627 memcpy(dst, (uint8_t *) &v1_0_obj->desc + offset, *copy_size); 628 629 /* 630 * We're finished with the v1.0 descriptor for now so free it. 631 * Note that this will invalidate any references to the v1.1 632 * descriptor. 633 */ 634 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_0_obj); 635 636 return 0; 637 } 638 639 /** 640 * spmc_shmem_check_obj - Check that counts in descriptor match overall size. 641 * @obj: Object containing ffa_memory_region_descriptor. 642 * @ffa_version: FF-A version of the provided descriptor. 643 * 644 * Return: 0 if object is valid, -EINVAL if constituent_memory_region_descriptor 645 * offset or count is invalid. 646 */ 647 static int spmc_shmem_check_obj(struct spmc_shmem_obj *obj, 648 uint32_t ffa_version) 649 { 650 uint32_t comp_mrd_offset = 0; 651 652 if (obj->desc.emad_count == 0U) { 653 WARN("%s: unsupported attribute desc count %u.\n", 654 __func__, obj->desc.emad_count); 655 return -EINVAL; 656 } 657 658 for (size_t emad_num = 0; emad_num < obj->desc.emad_count; emad_num++) { 659 size_t size; 660 size_t count; 661 size_t expected_size; 662 size_t total_page_count; 663 size_t emad_size; 664 size_t desc_size; 665 size_t header_emad_size; 666 uint32_t offset; 667 struct ffa_comp_mrd *comp; 668 struct ffa_emad_v1_0 *emad; 669 670 emad = spmc_shmem_obj_get_emad(&obj->desc, emad_num, 671 ffa_version, &emad_size); 672 if (emad == NULL) { 673 WARN("%s: invalid emad structure.\n", __func__); 674 return -EINVAL; 675 } 676 677 /* 678 * Validate the calculated emad address resides within the 679 * descriptor. 680 */ 681 if ((uintptr_t) emad >= 682 (uintptr_t)((uint8_t *) &obj->desc + obj->desc_size)) { 683 WARN("Invalid emad access.\n"); 684 return -EINVAL; 685 } 686 687 offset = emad->comp_mrd_offset; 688 689 if (ffa_version == MAKE_FFA_VERSION(1, 0)) { 690 desc_size = sizeof(struct ffa_mtd_v1_0); 691 } else { 692 desc_size = sizeof(struct ffa_mtd); 693 } 694 695 header_emad_size = desc_size + 696 (obj->desc.emad_count * emad_size); 697 698 if (offset < header_emad_size) { 699 WARN("%s: invalid object, offset %u < header + emad %zu\n", 700 __func__, offset, header_emad_size); 701 return -EINVAL; 702 } 703 704 size = obj->desc_size; 705 706 if (offset > size) { 707 WARN("%s: invalid object, offset %u > total size %zu\n", 708 __func__, offset, obj->desc_size); 709 return -EINVAL; 710 } 711 size -= offset; 712 713 if (size < sizeof(struct ffa_comp_mrd)) { 714 WARN("%s: invalid object, offset %u, total size %zu, no header space.\n", 715 __func__, offset, obj->desc_size); 716 return -EINVAL; 717 } 718 size -= sizeof(struct ffa_comp_mrd); 719 720 count = size / sizeof(struct ffa_cons_mrd); 721 722 comp = spmc_shmem_obj_get_comp_mrd(obj, ffa_version); 723 724 if (comp == NULL) { 725 WARN("%s: invalid comp_mrd offset\n", __func__); 726 return -EINVAL; 727 } 728 729 if (comp->address_range_count != count) { 730 WARN("%s: invalid object, desc count %u != %zu\n", 731 __func__, comp->address_range_count, count); 732 return -EINVAL; 733 } 734 735 expected_size = offset + sizeof(*comp) + 736 spmc_shmem_obj_ffa_constituent_size(obj, 737 ffa_version); 738 739 if (expected_size != obj->desc_size) { 740 WARN("%s: invalid object, computed size %zu != size %zu\n", 741 __func__, expected_size, obj->desc_size); 742 return -EINVAL; 743 } 744 745 if (obj->desc_filled < obj->desc_size) { 746 /* 747 * The whole descriptor has not yet been received. 748 * Skip final checks. 749 */ 750 return 0; 751 } 752 753 /* 754 * The offset provided to the composite memory region descriptor 755 * should be consistent across endpoint descriptors. Store the 756 * first entry and compare against subsequent entries. 757 */ 758 if (comp_mrd_offset == 0) { 759 comp_mrd_offset = offset; 760 } else { 761 if (comp_mrd_offset != offset) { 762 ERROR("%s: mismatching offsets provided, %u != %u\n", 763 __func__, offset, comp_mrd_offset); 764 return -EINVAL; 765 } 766 } 767 768 total_page_count = 0; 769 770 for (size_t i = 0; i < count; i++) { 771 total_page_count += 772 comp->address_range_array[i].page_count; 773 } 774 if (comp->total_page_count != total_page_count) { 775 WARN("%s: invalid object, desc total_page_count %u != %zu\n", 776 __func__, comp->total_page_count, 777 total_page_count); 778 return -EINVAL; 779 } 780 } 781 return 0; 782 } 783 784 /** 785 * spmc_shmem_check_state_obj - Check if the descriptor describes memory 786 * regions that are currently involved with an 787 * existing memory transactions. This implies that 788 * the memory is not in a valid state for lending. 789 * @obj: Object containing ffa_memory_region_descriptor. 790 * 791 * Return: 0 if object is valid, -EINVAL if invalid memory state. 792 */ 793 static int spmc_shmem_check_state_obj(struct spmc_shmem_obj *obj, 794 uint32_t ffa_version) 795 { 796 size_t obj_offset = 0; 797 struct spmc_shmem_obj *inflight_obj; 798 799 struct ffa_comp_mrd *other_mrd; 800 struct ffa_comp_mrd *requested_mrd = spmc_shmem_obj_get_comp_mrd(obj, 801 ffa_version); 802 803 if (requested_mrd == NULL) { 804 return -EINVAL; 805 } 806 807 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state, 808 &obj_offset); 809 810 while (inflight_obj != NULL) { 811 /* 812 * Don't compare the transaction to itself or to partially 813 * transmitted descriptors. 814 */ 815 if ((obj->desc.handle != inflight_obj->desc.handle) && 816 (obj->desc_size == obj->desc_filled)) { 817 other_mrd = spmc_shmem_obj_get_comp_mrd(inflight_obj, 818 ffa_version); 819 if (other_mrd == NULL) { 820 return -EINVAL; 821 } 822 if (overlapping_memory_regions(requested_mrd, 823 other_mrd)) { 824 return -EINVAL; 825 } 826 } 827 828 inflight_obj = spmc_shmem_obj_get_next(&spmc_shmem_obj_state, 829 &obj_offset); 830 } 831 return 0; 832 } 833 834 static long spmc_ffa_fill_desc(struct mailbox *mbox, 835 struct spmc_shmem_obj *obj, 836 uint32_t fragment_length, 837 ffa_mtd_flag32_t mtd_flag, 838 uint32_t ffa_version, 839 void *smc_handle) 840 { 841 int ret; 842 size_t emad_size; 843 uint32_t handle_low; 844 uint32_t handle_high; 845 struct ffa_emad_v1_0 *emad; 846 struct ffa_emad_v1_0 *other_emad; 847 848 if (mbox->rxtx_page_count == 0U) { 849 WARN("%s: buffer pair not registered.\n", __func__); 850 ret = FFA_ERROR_INVALID_PARAMETER; 851 goto err_arg; 852 } 853 854 if (fragment_length > mbox->rxtx_page_count * PAGE_SIZE_4KB) { 855 WARN("%s: bad fragment size %u > %u buffer size\n", __func__, 856 fragment_length, mbox->rxtx_page_count * PAGE_SIZE_4KB); 857 ret = FFA_ERROR_INVALID_PARAMETER; 858 goto err_arg; 859 } 860 861 memcpy((uint8_t *)&obj->desc + obj->desc_filled, 862 (uint8_t *) mbox->tx_buffer, fragment_length); 863 864 if (fragment_length > obj->desc_size - obj->desc_filled) { 865 WARN("%s: bad fragment size %u > %zu remaining\n", __func__, 866 fragment_length, obj->desc_size - obj->desc_filled); 867 ret = FFA_ERROR_INVALID_PARAMETER; 868 goto err_arg; 869 } 870 871 /* Ensure that the sender ID resides in the normal world. */ 872 if (ffa_is_secure_world_id(obj->desc.sender_id)) { 873 WARN("%s: Invalid sender ID 0x%x.\n", 874 __func__, obj->desc.sender_id); 875 ret = FFA_ERROR_DENIED; 876 goto err_arg; 877 } 878 879 /* 880 * We don't currently support any optional flags so ensure none are 881 * requested. 882 */ 883 if (obj->desc.flags != 0U && mtd_flag != 0U && 884 (obj->desc.flags != mtd_flag)) { 885 WARN("%s: invalid memory transaction flags %u != %u\n", 886 __func__, obj->desc.flags, mtd_flag); 887 ret = FFA_ERROR_INVALID_PARAMETER; 888 goto err_arg; 889 } 890 891 if (obj->desc_filled == 0U) { 892 /* First fragment, descriptor header has been copied */ 893 obj->desc.handle = spmc_shmem_obj_state.next_handle++; 894 obj->desc.flags |= mtd_flag; 895 } 896 897 obj->desc_filled += fragment_length; 898 ret = spmc_shmem_check_obj(obj, ffa_version); 899 if (ret != 0) { 900 ret = FFA_ERROR_INVALID_PARAMETER; 901 goto err_bad_desc; 902 } 903 904 handle_low = (uint32_t)obj->desc.handle; 905 handle_high = obj->desc.handle >> 32; 906 907 if (obj->desc_filled != obj->desc_size) { 908 SMC_RET8(smc_handle, FFA_MEM_FRAG_RX, handle_low, 909 handle_high, obj->desc_filled, 910 (uint32_t)obj->desc.sender_id << 16, 0, 0, 0); 911 } 912 913 /* The full descriptor has been received, perform any final checks. */ 914 915 /* 916 * If a partition ID resides in the secure world validate that the 917 * partition ID is for a known partition. Ignore any partition ID 918 * belonging to the normal world as it is assumed the Hypervisor will 919 * have validated these. 920 */ 921 for (size_t i = 0; i < obj->desc.emad_count; i++) { 922 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version, 923 &emad_size); 924 if (emad == NULL) { 925 ret = FFA_ERROR_INVALID_PARAMETER; 926 goto err_bad_desc; 927 } 928 929 ffa_endpoint_id16_t ep_id = emad->mapd.endpoint_id; 930 931 if (ffa_is_secure_world_id(ep_id)) { 932 if (spmc_get_sp_ctx(ep_id) == NULL) { 933 WARN("%s: Invalid receiver id 0x%x\n", 934 __func__, ep_id); 935 ret = FFA_ERROR_INVALID_PARAMETER; 936 goto err_bad_desc; 937 } 938 } 939 } 940 941 /* Ensure partition IDs are not duplicated. */ 942 for (size_t i = 0; i < obj->desc.emad_count; i++) { 943 emad = spmc_shmem_obj_get_emad(&obj->desc, i, ffa_version, 944 &emad_size); 945 if (emad == NULL) { 946 ret = FFA_ERROR_INVALID_PARAMETER; 947 goto err_bad_desc; 948 } 949 for (size_t j = i + 1; j < obj->desc.emad_count; j++) { 950 other_emad = spmc_shmem_obj_get_emad(&obj->desc, j, 951 ffa_version, 952 &emad_size); 953 if (other_emad == NULL) { 954 ret = FFA_ERROR_INVALID_PARAMETER; 955 goto err_bad_desc; 956 } 957 958 if (emad->mapd.endpoint_id == 959 other_emad->mapd.endpoint_id) { 960 WARN("%s: Duplicated endpoint id 0x%x\n", 961 __func__, emad->mapd.endpoint_id); 962 ret = FFA_ERROR_INVALID_PARAMETER; 963 goto err_bad_desc; 964 } 965 } 966 } 967 968 ret = spmc_shmem_check_state_obj(obj, ffa_version); 969 if (ret) { 970 ERROR("%s: invalid memory region descriptor.\n", __func__); 971 ret = FFA_ERROR_INVALID_PARAMETER; 972 goto err_bad_desc; 973 } 974 975 /* 976 * Everything checks out, if the sender was using FF-A v1.0, convert 977 * the descriptor format to use the v1.1 structures. 978 */ 979 if (ffa_version == MAKE_FFA_VERSION(1, 0)) { 980 struct spmc_shmem_obj *v1_1_obj; 981 uint64_t mem_handle; 982 983 /* Calculate the size that the v1.1 descriptor will required. */ 984 size_t v1_1_desc_size = 985 spmc_shm_get_v1_1_descriptor_size((void *) &obj->desc, 986 fragment_length); 987 988 if (v1_1_desc_size == 0U) { 989 ERROR("%s: cannot determine size of descriptor.\n", 990 __func__); 991 goto err_arg; 992 } 993 994 /* Get a new obj to store the v1.1 descriptor. */ 995 v1_1_obj = 996 spmc_shmem_obj_alloc(&spmc_shmem_obj_state, v1_1_desc_size); 997 998 if (!obj) { 999 ret = FFA_ERROR_NO_MEMORY; 1000 goto err_arg; 1001 } 1002 1003 /* Perform the conversion from v1.0 to v1.1. */ 1004 v1_1_obj->desc_size = v1_1_desc_size; 1005 v1_1_obj->desc_filled = v1_1_desc_size; 1006 if (!spmc_shm_convert_shmem_obj_from_v1_0(v1_1_obj, obj)) { 1007 ERROR("%s: Could not convert mtd!\n", __func__); 1008 spmc_shmem_obj_free(&spmc_shmem_obj_state, v1_1_obj); 1009 goto err_arg; 1010 } 1011 1012 /* 1013 * We're finished with the v1.0 descriptor so free it 1014 * and continue our checks with the new v1.1 descriptor. 1015 */ 1016 mem_handle = obj->desc.handle; 1017 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj); 1018 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle); 1019 if (obj == NULL) { 1020 ERROR("%s: Failed to find converted descriptor.\n", 1021 __func__); 1022 ret = FFA_ERROR_INVALID_PARAMETER; 1023 return spmc_ffa_error_return(smc_handle, ret); 1024 } 1025 } 1026 1027 SMC_RET8(smc_handle, FFA_SUCCESS_SMC32, 0, handle_low, handle_high, 0, 1028 0, 0, 0); 1029 1030 err_bad_desc: 1031 err_arg: 1032 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj); 1033 return spmc_ffa_error_return(smc_handle, ret); 1034 } 1035 1036 /** 1037 * spmc_ffa_mem_send - FFA_MEM_SHARE/LEND implementation. 1038 * @client: Client state. 1039 * @total_length: Total length of shared memory descriptor. 1040 * @fragment_length: Length of fragment of shared memory descriptor passed in 1041 * this call. 1042 * @address: Not supported, must be 0. 1043 * @page_count: Not supported, must be 0. 1044 * @smc_handle: Handle passed to smc call. Used to return 1045 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS. 1046 * 1047 * Implements a subset of the FF-A FFA_MEM_SHARE and FFA_MEM_LEND calls needed 1048 * to share or lend memory from non-secure os to secure os (with no stream 1049 * endpoints). 1050 * 1051 * Return: 0 on success, error code on failure. 1052 */ 1053 long spmc_ffa_mem_send(uint32_t smc_fid, 1054 bool secure_origin, 1055 uint64_t total_length, 1056 uint32_t fragment_length, 1057 uint64_t address, 1058 uint32_t page_count, 1059 void *cookie, 1060 void *handle, 1061 uint64_t flags) 1062 1063 { 1064 long ret; 1065 struct spmc_shmem_obj *obj; 1066 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin); 1067 ffa_mtd_flag32_t mtd_flag; 1068 uint32_t ffa_version = get_partition_ffa_version(secure_origin); 1069 1070 if (address != 0U || page_count != 0U) { 1071 WARN("%s: custom memory region for message not supported.\n", 1072 __func__); 1073 return spmc_ffa_error_return(handle, 1074 FFA_ERROR_INVALID_PARAMETER); 1075 } 1076 1077 if (secure_origin) { 1078 WARN("%s: unsupported share direction.\n", __func__); 1079 return spmc_ffa_error_return(handle, 1080 FFA_ERROR_INVALID_PARAMETER); 1081 } 1082 1083 /* 1084 * Check if the descriptor is smaller than the v1.0 descriptor. The 1085 * descriptor cannot be smaller than this structure. 1086 */ 1087 if (fragment_length < sizeof(struct ffa_mtd_v1_0)) { 1088 WARN("%s: bad first fragment size %u < %zu\n", 1089 __func__, fragment_length, sizeof(struct ffa_mtd_v1_0)); 1090 return spmc_ffa_error_return(handle, 1091 FFA_ERROR_INVALID_PARAMETER); 1092 } 1093 1094 if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_SHARE) { 1095 mtd_flag = FFA_MTD_FLAG_TYPE_SHARE_MEMORY; 1096 } else if ((smc_fid & FUNCID_NUM_MASK) == FFA_FNUM_MEM_LEND) { 1097 mtd_flag = FFA_MTD_FLAG_TYPE_LEND_MEMORY; 1098 } else { 1099 WARN("%s: invalid memory management operation.\n", __func__); 1100 return spmc_ffa_error_return(handle, 1101 FFA_ERROR_INVALID_PARAMETER); 1102 } 1103 1104 spin_lock(&spmc_shmem_obj_state.lock); 1105 obj = spmc_shmem_obj_alloc(&spmc_shmem_obj_state, total_length); 1106 if (obj == NULL) { 1107 ret = FFA_ERROR_NO_MEMORY; 1108 goto err_unlock; 1109 } 1110 1111 spin_lock(&mbox->lock); 1112 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, mtd_flag, 1113 ffa_version, handle); 1114 spin_unlock(&mbox->lock); 1115 1116 spin_unlock(&spmc_shmem_obj_state.lock); 1117 return ret; 1118 1119 err_unlock: 1120 spin_unlock(&spmc_shmem_obj_state.lock); 1121 return spmc_ffa_error_return(handle, ret); 1122 } 1123 1124 /** 1125 * spmc_ffa_mem_frag_tx - FFA_MEM_FRAG_TX implementation. 1126 * @client: Client state. 1127 * @handle_low: Handle_low value returned from FFA_MEM_FRAG_RX. 1128 * @handle_high: Handle_high value returned from FFA_MEM_FRAG_RX. 1129 * @fragment_length: Length of fragments transmitted. 1130 * @sender_id: Vmid of sender in bits [31:16] 1131 * @smc_handle: Handle passed to smc call. Used to return 1132 * FFA_MEM_FRAG_RX or SMC_FC_FFA_SUCCESS. 1133 * 1134 * Return: @smc_handle on success, error code on failure. 1135 */ 1136 long spmc_ffa_mem_frag_tx(uint32_t smc_fid, 1137 bool secure_origin, 1138 uint64_t handle_low, 1139 uint64_t handle_high, 1140 uint32_t fragment_length, 1141 uint32_t sender_id, 1142 void *cookie, 1143 void *handle, 1144 uint64_t flags) 1145 { 1146 long ret; 1147 uint32_t desc_sender_id; 1148 uint32_t ffa_version = get_partition_ffa_version(secure_origin); 1149 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin); 1150 1151 struct spmc_shmem_obj *obj; 1152 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32); 1153 1154 spin_lock(&spmc_shmem_obj_state.lock); 1155 1156 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle); 1157 if (obj == NULL) { 1158 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n", 1159 __func__, mem_handle); 1160 ret = FFA_ERROR_INVALID_PARAMETER; 1161 goto err_unlock; 1162 } 1163 1164 desc_sender_id = (uint32_t)obj->desc.sender_id << 16; 1165 if (sender_id != desc_sender_id) { 1166 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__, 1167 sender_id, desc_sender_id); 1168 ret = FFA_ERROR_INVALID_PARAMETER; 1169 goto err_unlock; 1170 } 1171 1172 if (obj->desc_filled == obj->desc_size) { 1173 WARN("%s: object desc already filled, %zu\n", __func__, 1174 obj->desc_filled); 1175 ret = FFA_ERROR_INVALID_PARAMETER; 1176 goto err_unlock; 1177 } 1178 1179 spin_lock(&mbox->lock); 1180 ret = spmc_ffa_fill_desc(mbox, obj, fragment_length, 0, ffa_version, 1181 handle); 1182 spin_unlock(&mbox->lock); 1183 1184 spin_unlock(&spmc_shmem_obj_state.lock); 1185 return ret; 1186 1187 err_unlock: 1188 spin_unlock(&spmc_shmem_obj_state.lock); 1189 return spmc_ffa_error_return(handle, ret); 1190 } 1191 1192 /** 1193 * spmc_ffa_mem_retrieve_req - FFA_MEM_RETRIEVE_REQ implementation. 1194 * @smc_fid: FID of SMC 1195 * @total_length: Total length of retrieve request descriptor if this is 1196 * the first call. Otherwise (unsupported) must be 0. 1197 * @fragment_length: Length of fragment of retrieve request descriptor passed 1198 * in this call. Only @fragment_length == @length is 1199 * supported by this implementation. 1200 * @address: Not supported, must be 0. 1201 * @page_count: Not supported, must be 0. 1202 * @smc_handle: Handle passed to smc call. Used to return 1203 * FFA_MEM_RETRIEVE_RESP. 1204 * 1205 * Implements a subset of the FF-A FFA_MEM_RETRIEVE_REQ call. 1206 * Used by secure os to retrieve memory already shared by non-secure os. 1207 * If the data does not fit in a single FFA_MEM_RETRIEVE_RESP message, 1208 * the client must call FFA_MEM_FRAG_RX until the full response has been 1209 * received. 1210 * 1211 * Return: @handle on success, error code on failure. 1212 */ 1213 long 1214 spmc_ffa_mem_retrieve_req(uint32_t smc_fid, 1215 bool secure_origin, 1216 uint32_t total_length, 1217 uint32_t fragment_length, 1218 uint64_t address, 1219 uint32_t page_count, 1220 void *cookie, 1221 void *handle, 1222 uint64_t flags) 1223 { 1224 int ret; 1225 size_t buf_size; 1226 size_t copy_size = 0; 1227 size_t min_desc_size; 1228 size_t out_desc_size = 0; 1229 1230 /* 1231 * Currently we are only accessing fields that are the same in both the 1232 * v1.0 and v1.1 mtd struct therefore we can use a v1.1 struct directly 1233 * here. We only need validate against the appropriate struct size. 1234 */ 1235 struct ffa_mtd *resp; 1236 const struct ffa_mtd *req; 1237 struct spmc_shmem_obj *obj = NULL; 1238 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin); 1239 uint32_t ffa_version = get_partition_ffa_version(secure_origin); 1240 1241 if (!secure_origin) { 1242 WARN("%s: unsupported retrieve req direction.\n", __func__); 1243 return spmc_ffa_error_return(handle, 1244 FFA_ERROR_INVALID_PARAMETER); 1245 } 1246 1247 if (address != 0U || page_count != 0U) { 1248 WARN("%s: custom memory region not supported.\n", __func__); 1249 return spmc_ffa_error_return(handle, 1250 FFA_ERROR_INVALID_PARAMETER); 1251 } 1252 1253 spin_lock(&mbox->lock); 1254 1255 req = mbox->tx_buffer; 1256 resp = mbox->rx_buffer; 1257 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE; 1258 1259 if (mbox->rxtx_page_count == 0U) { 1260 WARN("%s: buffer pair not registered.\n", __func__); 1261 ret = FFA_ERROR_INVALID_PARAMETER; 1262 goto err_unlock_mailbox; 1263 } 1264 1265 if (mbox->state != MAILBOX_STATE_EMPTY) { 1266 WARN("%s: RX Buffer is full! %d\n", __func__, mbox->state); 1267 ret = FFA_ERROR_DENIED; 1268 goto err_unlock_mailbox; 1269 } 1270 1271 if (fragment_length != total_length) { 1272 WARN("%s: fragmented retrieve request not supported.\n", 1273 __func__); 1274 ret = FFA_ERROR_INVALID_PARAMETER; 1275 goto err_unlock_mailbox; 1276 } 1277 1278 if (req->emad_count == 0U) { 1279 WARN("%s: unsupported attribute desc count %u.\n", 1280 __func__, obj->desc.emad_count); 1281 return -EINVAL; 1282 } 1283 1284 /* Determine the appropriate minimum descriptor size. */ 1285 if (ffa_version == MAKE_FFA_VERSION(1, 0)) { 1286 min_desc_size = sizeof(struct ffa_mtd_v1_0); 1287 } else { 1288 min_desc_size = sizeof(struct ffa_mtd); 1289 } 1290 if (total_length < min_desc_size) { 1291 WARN("%s: invalid length %u < %zu\n", __func__, total_length, 1292 min_desc_size); 1293 ret = FFA_ERROR_INVALID_PARAMETER; 1294 goto err_unlock_mailbox; 1295 } 1296 1297 spin_lock(&spmc_shmem_obj_state.lock); 1298 1299 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle); 1300 if (obj == NULL) { 1301 ret = FFA_ERROR_INVALID_PARAMETER; 1302 goto err_unlock_all; 1303 } 1304 1305 if (obj->desc_filled != obj->desc_size) { 1306 WARN("%s: incomplete object desc filled %zu < size %zu\n", 1307 __func__, obj->desc_filled, obj->desc_size); 1308 ret = FFA_ERROR_INVALID_PARAMETER; 1309 goto err_unlock_all; 1310 } 1311 1312 if (req->emad_count != 0U && req->sender_id != obj->desc.sender_id) { 1313 WARN("%s: wrong sender id 0x%x != 0x%x\n", 1314 __func__, req->sender_id, obj->desc.sender_id); 1315 ret = FFA_ERROR_INVALID_PARAMETER; 1316 goto err_unlock_all; 1317 } 1318 1319 if (req->emad_count != 0U && req->tag != obj->desc.tag) { 1320 WARN("%s: wrong tag 0x%lx != 0x%lx\n", 1321 __func__, req->tag, obj->desc.tag); 1322 ret = FFA_ERROR_INVALID_PARAMETER; 1323 goto err_unlock_all; 1324 } 1325 1326 if (req->emad_count != 0U && req->emad_count != obj->desc.emad_count) { 1327 WARN("%s: mistmatch of endpoint counts %u != %u\n", 1328 __func__, req->emad_count, obj->desc.emad_count); 1329 ret = FFA_ERROR_INVALID_PARAMETER; 1330 goto err_unlock_all; 1331 } 1332 1333 if (req->flags != 0U) { 1334 if ((req->flags & FFA_MTD_FLAG_TYPE_MASK) != 1335 (obj->desc.flags & FFA_MTD_FLAG_TYPE_MASK)) { 1336 /* 1337 * If the retrieve request specifies the memory 1338 * transaction ensure it matches what we expect. 1339 */ 1340 WARN("%s: wrong mem transaction flags %x != %x\n", 1341 __func__, req->flags, obj->desc.flags); 1342 ret = FFA_ERROR_INVALID_PARAMETER; 1343 goto err_unlock_all; 1344 } 1345 1346 if (req->flags != FFA_MTD_FLAG_TYPE_SHARE_MEMORY && 1347 req->flags != FFA_MTD_FLAG_TYPE_LEND_MEMORY) { 1348 /* 1349 * Current implementation does not support donate and 1350 * it supports no other flags. 1351 */ 1352 WARN("%s: invalid flags 0x%x\n", __func__, req->flags); 1353 ret = FFA_ERROR_INVALID_PARAMETER; 1354 goto err_unlock_all; 1355 } 1356 } 1357 1358 /* Validate that the provided emad offset and structure is valid.*/ 1359 for (size_t i = 0; i < req->emad_count; i++) { 1360 size_t emad_size; 1361 struct ffa_emad_v1_0 *emad; 1362 1363 emad = spmc_shmem_obj_get_emad(req, i, ffa_version, 1364 &emad_size); 1365 if (emad == NULL) { 1366 WARN("%s: invalid emad structure.\n", __func__); 1367 ret = FFA_ERROR_INVALID_PARAMETER; 1368 goto err_unlock_all; 1369 } 1370 1371 if ((uintptr_t) emad >= (uintptr_t) 1372 ((uint8_t *) req + total_length)) { 1373 WARN("Invalid emad access.\n"); 1374 ret = FFA_ERROR_INVALID_PARAMETER; 1375 goto err_unlock_all; 1376 } 1377 } 1378 1379 /* 1380 * Validate all the endpoints match in the case of multiple 1381 * borrowers. We don't mandate that the order of the borrowers 1382 * must match in the descriptors therefore check to see if the 1383 * endpoints match in any order. 1384 */ 1385 for (size_t i = 0; i < req->emad_count; i++) { 1386 bool found = false; 1387 size_t emad_size; 1388 struct ffa_emad_v1_0 *emad; 1389 struct ffa_emad_v1_0 *other_emad; 1390 1391 emad = spmc_shmem_obj_get_emad(req, i, ffa_version, 1392 &emad_size); 1393 if (emad == NULL) { 1394 ret = FFA_ERROR_INVALID_PARAMETER; 1395 goto err_unlock_all; 1396 } 1397 1398 for (size_t j = 0; j < obj->desc.emad_count; j++) { 1399 other_emad = spmc_shmem_obj_get_emad( 1400 &obj->desc, j, MAKE_FFA_VERSION(1, 1), 1401 &emad_size); 1402 1403 if (other_emad == NULL) { 1404 ret = FFA_ERROR_INVALID_PARAMETER; 1405 goto err_unlock_all; 1406 } 1407 1408 if (req->emad_count && 1409 emad->mapd.endpoint_id == 1410 other_emad->mapd.endpoint_id) { 1411 found = true; 1412 break; 1413 } 1414 } 1415 1416 if (!found) { 1417 WARN("%s: invalid receiver id (0x%x).\n", 1418 __func__, emad->mapd.endpoint_id); 1419 ret = FFA_ERROR_INVALID_PARAMETER; 1420 goto err_unlock_all; 1421 } 1422 } 1423 1424 mbox->state = MAILBOX_STATE_FULL; 1425 1426 if (req->emad_count != 0U) { 1427 obj->in_use++; 1428 } 1429 1430 /* 1431 * If the caller is v1.0 convert the descriptor, otherwise copy 1432 * directly. 1433 */ 1434 if (ffa_version == MAKE_FFA_VERSION(1, 0)) { 1435 ret = spmc_populate_ffa_v1_0_descriptor(resp, obj, buf_size, 0, 1436 ©_size, 1437 &out_desc_size); 1438 if (ret != 0U) { 1439 ERROR("%s: Failed to process descriptor.\n", __func__); 1440 goto err_unlock_all; 1441 } 1442 } else { 1443 copy_size = MIN(obj->desc_size, buf_size); 1444 out_desc_size = obj->desc_size; 1445 1446 memcpy(resp, &obj->desc, copy_size); 1447 } 1448 1449 spin_unlock(&spmc_shmem_obj_state.lock); 1450 spin_unlock(&mbox->lock); 1451 1452 SMC_RET8(handle, FFA_MEM_RETRIEVE_RESP, out_desc_size, 1453 copy_size, 0, 0, 0, 0, 0); 1454 1455 err_unlock_all: 1456 spin_unlock(&spmc_shmem_obj_state.lock); 1457 err_unlock_mailbox: 1458 spin_unlock(&mbox->lock); 1459 return spmc_ffa_error_return(handle, ret); 1460 } 1461 1462 /** 1463 * spmc_ffa_mem_frag_rx - FFA_MEM_FRAG_RX implementation. 1464 * @client: Client state. 1465 * @handle_low: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[31:0]. 1466 * @handle_high: Handle passed to &FFA_MEM_RETRIEVE_REQ. Bit[63:32]. 1467 * @fragment_offset: Byte offset in descriptor to resume at. 1468 * @sender_id: Bit[31:16]: Endpoint id of sender if client is a 1469 * hypervisor. 0 otherwise. 1470 * @smc_handle: Handle passed to smc call. Used to return 1471 * FFA_MEM_FRAG_TX. 1472 * 1473 * Return: @smc_handle on success, error code on failure. 1474 */ 1475 long spmc_ffa_mem_frag_rx(uint32_t smc_fid, 1476 bool secure_origin, 1477 uint32_t handle_low, 1478 uint32_t handle_high, 1479 uint32_t fragment_offset, 1480 uint32_t sender_id, 1481 void *cookie, 1482 void *handle, 1483 uint64_t flags) 1484 { 1485 int ret; 1486 void *src; 1487 size_t buf_size; 1488 size_t copy_size; 1489 size_t full_copy_size; 1490 uint32_t desc_sender_id; 1491 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin); 1492 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32); 1493 struct spmc_shmem_obj *obj; 1494 uint32_t ffa_version = get_partition_ffa_version(secure_origin); 1495 1496 if (!secure_origin) { 1497 WARN("%s: can only be called from swld.\n", 1498 __func__); 1499 return spmc_ffa_error_return(handle, 1500 FFA_ERROR_INVALID_PARAMETER); 1501 } 1502 1503 spin_lock(&spmc_shmem_obj_state.lock); 1504 1505 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle); 1506 if (obj == NULL) { 1507 WARN("%s: invalid handle, 0x%lx, not a valid handle.\n", 1508 __func__, mem_handle); 1509 ret = FFA_ERROR_INVALID_PARAMETER; 1510 goto err_unlock_shmem; 1511 } 1512 1513 desc_sender_id = (uint32_t)obj->desc.sender_id << 16; 1514 if (sender_id != 0U && sender_id != desc_sender_id) { 1515 WARN("%s: invalid sender_id 0x%x != 0x%x\n", __func__, 1516 sender_id, desc_sender_id); 1517 ret = FFA_ERROR_INVALID_PARAMETER; 1518 goto err_unlock_shmem; 1519 } 1520 1521 if (fragment_offset >= obj->desc_size) { 1522 WARN("%s: invalid fragment_offset 0x%x >= 0x%zx\n", 1523 __func__, fragment_offset, obj->desc_size); 1524 ret = FFA_ERROR_INVALID_PARAMETER; 1525 goto err_unlock_shmem; 1526 } 1527 1528 spin_lock(&mbox->lock); 1529 1530 if (mbox->rxtx_page_count == 0U) { 1531 WARN("%s: buffer pair not registered.\n", __func__); 1532 ret = FFA_ERROR_INVALID_PARAMETER; 1533 goto err_unlock_all; 1534 } 1535 1536 if (mbox->state != MAILBOX_STATE_EMPTY) { 1537 WARN("%s: RX Buffer is full!\n", __func__); 1538 ret = FFA_ERROR_DENIED; 1539 goto err_unlock_all; 1540 } 1541 1542 buf_size = mbox->rxtx_page_count * FFA_PAGE_SIZE; 1543 1544 mbox->state = MAILBOX_STATE_FULL; 1545 1546 /* 1547 * If the caller is v1.0 convert the descriptor, otherwise copy 1548 * directly. 1549 */ 1550 if (ffa_version == MAKE_FFA_VERSION(1, 0)) { 1551 size_t out_desc_size; 1552 1553 ret = spmc_populate_ffa_v1_0_descriptor(mbox->rx_buffer, obj, 1554 buf_size, 1555 fragment_offset, 1556 ©_size, 1557 &out_desc_size); 1558 if (ret != 0U) { 1559 ERROR("%s: Failed to process descriptor.\n", __func__); 1560 goto err_unlock_all; 1561 } 1562 } else { 1563 full_copy_size = obj->desc_size - fragment_offset; 1564 copy_size = MIN(full_copy_size, buf_size); 1565 1566 src = &obj->desc; 1567 1568 memcpy(mbox->rx_buffer, src + fragment_offset, copy_size); 1569 } 1570 1571 spin_unlock(&mbox->lock); 1572 spin_unlock(&spmc_shmem_obj_state.lock); 1573 1574 SMC_RET8(handle, FFA_MEM_FRAG_TX, handle_low, handle_high, 1575 copy_size, sender_id, 0, 0, 0); 1576 1577 err_unlock_all: 1578 spin_unlock(&mbox->lock); 1579 err_unlock_shmem: 1580 spin_unlock(&spmc_shmem_obj_state.lock); 1581 return spmc_ffa_error_return(handle, ret); 1582 } 1583 1584 /** 1585 * spmc_ffa_mem_relinquish - FFA_MEM_RELINQUISH implementation. 1586 * @client: Client state. 1587 * 1588 * Implements a subset of the FF-A FFA_MEM_RELINQUISH call. 1589 * Used by secure os release previously shared memory to non-secure os. 1590 * 1591 * The handle to release must be in the client's (secure os's) transmit buffer. 1592 * 1593 * Return: 0 on success, error code on failure. 1594 */ 1595 int spmc_ffa_mem_relinquish(uint32_t smc_fid, 1596 bool secure_origin, 1597 uint32_t handle_low, 1598 uint32_t handle_high, 1599 uint32_t fragment_offset, 1600 uint32_t sender_id, 1601 void *cookie, 1602 void *handle, 1603 uint64_t flags) 1604 { 1605 int ret; 1606 struct mailbox *mbox = spmc_get_mbox_desc(secure_origin); 1607 struct spmc_shmem_obj *obj; 1608 const struct ffa_mem_relinquish_descriptor *req; 1609 1610 if (!secure_origin) { 1611 WARN("%s: unsupported relinquish direction.\n", __func__); 1612 return spmc_ffa_error_return(handle, 1613 FFA_ERROR_INVALID_PARAMETER); 1614 } 1615 1616 spin_lock(&mbox->lock); 1617 1618 if (mbox->rxtx_page_count == 0U) { 1619 WARN("%s: buffer pair not registered.\n", __func__); 1620 ret = FFA_ERROR_INVALID_PARAMETER; 1621 goto err_unlock_mailbox; 1622 } 1623 1624 req = mbox->tx_buffer; 1625 1626 if (req->flags != 0U) { 1627 WARN("%s: unsupported flags 0x%x\n", __func__, req->flags); 1628 ret = FFA_ERROR_INVALID_PARAMETER; 1629 goto err_unlock_mailbox; 1630 } 1631 1632 if (req->endpoint_count == 0) { 1633 WARN("%s: endpoint count cannot be 0.\n", __func__); 1634 ret = FFA_ERROR_INVALID_PARAMETER; 1635 goto err_unlock_mailbox; 1636 } 1637 1638 spin_lock(&spmc_shmem_obj_state.lock); 1639 1640 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, req->handle); 1641 if (obj == NULL) { 1642 ret = FFA_ERROR_INVALID_PARAMETER; 1643 goto err_unlock_all; 1644 } 1645 1646 if (obj->desc.emad_count != req->endpoint_count) { 1647 WARN("%s: mismatch of endpoint count %u != %u\n", __func__, 1648 obj->desc.emad_count, req->endpoint_count); 1649 ret = FFA_ERROR_INVALID_PARAMETER; 1650 goto err_unlock_all; 1651 } 1652 1653 /* Validate requested endpoint IDs match descriptor. */ 1654 for (size_t i = 0; i < req->endpoint_count; i++) { 1655 bool found = false; 1656 size_t emad_size; 1657 struct ffa_emad_v1_0 *emad; 1658 1659 for (unsigned int j = 0; j < obj->desc.emad_count; j++) { 1660 emad = spmc_shmem_obj_get_emad(&obj->desc, j, 1661 MAKE_FFA_VERSION(1, 1), 1662 &emad_size); 1663 if (req->endpoint_array[i] == 1664 emad->mapd.endpoint_id) { 1665 found = true; 1666 break; 1667 } 1668 } 1669 1670 if (!found) { 1671 WARN("%s: Invalid endpoint ID (0x%x).\n", 1672 __func__, req->endpoint_array[i]); 1673 ret = FFA_ERROR_INVALID_PARAMETER; 1674 goto err_unlock_all; 1675 } 1676 } 1677 1678 if (obj->in_use == 0U) { 1679 ret = FFA_ERROR_INVALID_PARAMETER; 1680 goto err_unlock_all; 1681 } 1682 obj->in_use--; 1683 1684 spin_unlock(&spmc_shmem_obj_state.lock); 1685 spin_unlock(&mbox->lock); 1686 1687 SMC_RET1(handle, FFA_SUCCESS_SMC32); 1688 1689 err_unlock_all: 1690 spin_unlock(&spmc_shmem_obj_state.lock); 1691 err_unlock_mailbox: 1692 spin_unlock(&mbox->lock); 1693 return spmc_ffa_error_return(handle, ret); 1694 } 1695 1696 /** 1697 * spmc_ffa_mem_reclaim - FFA_MEM_RECLAIM implementation. 1698 * @client: Client state. 1699 * @handle_low: Unique handle of shared memory object to reclaim. Bit[31:0]. 1700 * @handle_high: Unique handle of shared memory object to reclaim. 1701 * Bit[63:32]. 1702 * @flags: Unsupported, ignored. 1703 * 1704 * Implements a subset of the FF-A FFA_MEM_RECLAIM call. 1705 * Used by non-secure os reclaim memory previously shared with secure os. 1706 * 1707 * Return: 0 on success, error code on failure. 1708 */ 1709 int spmc_ffa_mem_reclaim(uint32_t smc_fid, 1710 bool secure_origin, 1711 uint32_t handle_low, 1712 uint32_t handle_high, 1713 uint32_t mem_flags, 1714 uint64_t x4, 1715 void *cookie, 1716 void *handle, 1717 uint64_t flags) 1718 { 1719 int ret; 1720 struct spmc_shmem_obj *obj; 1721 uint64_t mem_handle = handle_low | (((uint64_t)handle_high) << 32); 1722 1723 if (secure_origin) { 1724 WARN("%s: unsupported reclaim direction.\n", __func__); 1725 return spmc_ffa_error_return(handle, 1726 FFA_ERROR_INVALID_PARAMETER); 1727 } 1728 1729 if (mem_flags != 0U) { 1730 WARN("%s: unsupported flags 0x%x\n", __func__, mem_flags); 1731 return spmc_ffa_error_return(handle, 1732 FFA_ERROR_INVALID_PARAMETER); 1733 } 1734 1735 spin_lock(&spmc_shmem_obj_state.lock); 1736 1737 obj = spmc_shmem_obj_lookup(&spmc_shmem_obj_state, mem_handle); 1738 if (obj == NULL) { 1739 ret = FFA_ERROR_INVALID_PARAMETER; 1740 goto err_unlock; 1741 } 1742 if (obj->in_use != 0U) { 1743 ret = FFA_ERROR_DENIED; 1744 goto err_unlock; 1745 } 1746 spmc_shmem_obj_free(&spmc_shmem_obj_state, obj); 1747 spin_unlock(&spmc_shmem_obj_state.lock); 1748 1749 SMC_RET1(handle, FFA_SUCCESS_SMC32); 1750 1751 err_unlock: 1752 spin_unlock(&spmc_shmem_obj_state.lock); 1753 return spmc_ffa_error_return(handle, ret); 1754 } 1755