1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2020-2023, Linaro Limited. 4 * Copyright (c) 2019-2024, Arm Limited. All rights reserved. 5 */ 6 7 #include <assert.h> 8 #include <ffa.h> 9 #include <initcall.h> 10 #include <io.h> 11 #include <kernel/interrupt.h> 12 #include <kernel/notif.h> 13 #include <kernel/panic.h> 14 #include <kernel/secure_partition.h> 15 #include <kernel/spinlock.h> 16 #include <kernel/spmc_sp_handler.h> 17 #include <kernel/tee_misc.h> 18 #include <kernel/thread.h> 19 #include <kernel/thread_private.h> 20 #include <kernel/thread_spmc.h> 21 #include <kernel/virtualization.h> 22 #include <mm/core_mmu.h> 23 #include <mm/mobj.h> 24 #include <optee_ffa.h> 25 #include <optee_msg.h> 26 #include <optee_rpc_cmd.h> 27 #include <sm/optee_smc.h> 28 #include <string.h> 29 #include <sys/queue.h> 30 #include <tee/entry_std.h> 31 #include <tee/uuid.h> 32 #include <util.h> 33 34 #if defined(CFG_CORE_SEL1_SPMC) 35 struct mem_share_state { 36 struct mobj_ffa *mf; 37 unsigned int page_count; 38 unsigned int region_count; 39 unsigned int current_page_idx; 40 }; 41 42 struct mem_frag_state { 43 struct mem_share_state share; 44 tee_mm_entry_t *mm; 45 unsigned int frag_offset; 46 SLIST_ENTRY(mem_frag_state) link; 47 }; 48 #endif 49 50 static unsigned int spmc_notif_lock = SPINLOCK_UNLOCK; 51 static int do_bottom_half_value = -1; 52 static uint16_t notif_vm_id; 53 static bool spmc_notif_is_ready; 54 55 /* Initialized in spmc_init() below */ 56 uint16_t optee_endpoint_id __nex_bss; 57 uint16_t spmc_id __nex_bss; 58 #ifdef CFG_CORE_SEL1_SPMC 59 uint16_t spmd_id __nex_bss; 60 static const uint32_t my_part_props = FFA_PART_PROP_DIRECT_REQ_RECV | 61 FFA_PART_PROP_DIRECT_REQ_SEND | 62 #ifdef CFG_NS_VIRTUALIZATION 63 FFA_PART_PROP_NOTIF_CREATED | 64 FFA_PART_PROP_NOTIF_DESTROYED | 65 #endif 66 #ifdef ARM64 67 FFA_PART_PROP_AARCH64_STATE | 68 #endif 69 FFA_PART_PROP_IS_PE_ID; 70 71 static uint32_t my_uuid_words[] = { 72 /* 73 * - if the SPMC is in S-EL2 this UUID describes OP-TEE as a S-EL1 74 * SP, or 75 * - if the SPMC is in S-EL1 then this UUID is for OP-TEE as a 76 * logical partition, residing in the same exception level as the 77 * SPMC 78 * UUID 486178e0-e7f8-11e3-bc5e-0002a5d5c51b 79 */ 80 0xe0786148, 0xe311f8e7, 0x02005ebc, 0x1bc5d5a5, 81 }; 82 83 /* 84 * If struct ffa_rxtx::size is 0 RX/TX buffers are not mapped or initialized. 85 * 86 * struct ffa_rxtx::spin_lock protects the variables below from concurrent 87 * access this includes the use of content of struct ffa_rxtx::rx and 88 * @frag_state_head. 89 * 90 * struct ffa_rxtx::tx_buf_is_mine is true when we may write to struct 91 * ffa_rxtx::tx and false when it is owned by normal world. 92 * 93 * Note that we can't prevent normal world from updating the content of 94 * these buffers so we must always be careful when reading. while we hold 95 * the lock. 96 */ 97 98 static struct ffa_rxtx my_rxtx __nex_bss; 99 100 static bool is_nw_buf(struct ffa_rxtx *rxtx) 101 { 102 return rxtx == &my_rxtx; 103 } 104 105 static SLIST_HEAD(mem_frag_state_head, mem_frag_state) frag_state_head = 106 SLIST_HEAD_INITIALIZER(&frag_state_head); 107 108 static uint64_t notif_pending_bitmap; 109 static uint64_t notif_bound_bitmap; 110 static bool notif_vm_id_valid; 111 static int notif_intid = -1; 112 #else 113 static uint8_t __rx_buf[SMALL_PAGE_SIZE] __aligned(SMALL_PAGE_SIZE); 114 static uint8_t __tx_buf[SMALL_PAGE_SIZE] __aligned(SMALL_PAGE_SIZE); 115 static struct ffa_rxtx my_rxtx = { 116 .rx = __rx_buf, 117 .tx = __tx_buf, 118 .size = sizeof(__rx_buf), 119 }; 120 #endif 121 122 static uint32_t swap_src_dst(uint32_t src_dst) 123 { 124 return (src_dst >> 16) | (src_dst << 16); 125 } 126 127 static uint16_t get_sender_id(uint32_t src_dst) 128 { 129 return src_dst >> 16; 130 } 131 132 void spmc_set_args(struct thread_smc_args *args, uint32_t fid, uint32_t src_dst, 133 uint32_t w2, uint32_t w3, uint32_t w4, uint32_t w5) 134 { 135 *args = (struct thread_smc_args){ .a0 = fid, 136 .a1 = src_dst, 137 .a2 = w2, 138 .a3 = w3, 139 .a4 = w4, 140 .a5 = w5, }; 141 } 142 143 static void set_simple_ret_val(struct thread_smc_args *args, int ffa_ret) 144 { 145 if (ffa_ret) 146 spmc_set_args(args, FFA_ERROR, 0, ffa_ret, 0, 0, 0); 147 else 148 spmc_set_args(args, FFA_SUCCESS_32, 0, 0, 0, 0, 0); 149 } 150 151 uint32_t spmc_exchange_version(uint32_t vers, struct ffa_rxtx *rxtx) 152 { 153 /* 154 * No locking, if the caller does concurrent calls to this it's 155 * only making a mess for itself. We must be able to renegotiate 156 * the FF-A version in order to support differing versions between 157 * the loader and the driver. 158 */ 159 if (vers < FFA_VERSION_1_1) 160 rxtx->ffa_vers = FFA_VERSION_1_0; 161 else 162 rxtx->ffa_vers = FFA_VERSION_1_1; 163 164 return rxtx->ffa_vers; 165 } 166 167 static bool is_ffa_success(uint32_t fid) 168 { 169 #ifdef ARM64 170 if (fid == FFA_SUCCESS_64) 171 return true; 172 #endif 173 return fid == FFA_SUCCESS_32; 174 } 175 176 static int32_t get_ffa_ret_code(const struct thread_smc_args *args) 177 { 178 if (is_ffa_success(args->a0)) 179 return FFA_OK; 180 if (args->a0 == FFA_ERROR && args->a2) 181 return args->a2; 182 return FFA_NOT_SUPPORTED; 183 } 184 185 static int ffa_simple_call(uint32_t fid, unsigned long a1, unsigned long a2, 186 unsigned long a3, unsigned long a4) 187 { 188 struct thread_smc_args args = { 189 .a0 = fid, 190 .a1 = a1, 191 .a2 = a2, 192 .a3 = a3, 193 .a4 = a4, 194 }; 195 196 thread_smccc(&args); 197 198 return get_ffa_ret_code(&args); 199 } 200 201 static int __maybe_unused ffa_features(uint32_t id) 202 { 203 return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0); 204 } 205 206 static int __maybe_unused ffa_set_notification(uint16_t dst, uint16_t src, 207 uint32_t flags, uint64_t bitmap) 208 { 209 return ffa_simple_call(FFA_NOTIFICATION_SET, 210 SHIFT_U32(src, 16) | dst, flags, 211 low32_from_64(bitmap), high32_from_64(bitmap)); 212 } 213 214 #if defined(CFG_CORE_SEL1_SPMC) 215 static void handle_features(struct thread_smc_args *args) 216 { 217 uint32_t ret_fid = FFA_ERROR; 218 uint32_t ret_w2 = FFA_NOT_SUPPORTED; 219 220 switch (args->a1) { 221 case FFA_FEATURE_SCHEDULE_RECV_INTR: 222 if (spmc_notif_is_ready) { 223 ret_fid = FFA_SUCCESS_32; 224 ret_w2 = notif_intid; 225 } 226 break; 227 228 #ifdef ARM64 229 case FFA_RXTX_MAP_64: 230 #endif 231 case FFA_RXTX_MAP_32: 232 ret_fid = FFA_SUCCESS_32; 233 ret_w2 = 0; /* 4kB Minimum buffer size and alignment boundary */ 234 break; 235 #ifdef ARM64 236 case FFA_MEM_SHARE_64: 237 #endif 238 case FFA_MEM_SHARE_32: 239 ret_fid = FFA_SUCCESS_32; 240 /* 241 * Partition manager supports transmission of a memory 242 * transaction descriptor in a buffer dynamically allocated 243 * by the endpoint. 244 */ 245 ret_w2 = BIT(0); 246 break; 247 248 case FFA_ERROR: 249 case FFA_VERSION: 250 case FFA_SUCCESS_32: 251 #ifdef ARM64 252 case FFA_SUCCESS_64: 253 #endif 254 case FFA_FEATURES: 255 case FFA_SPM_ID_GET: 256 case FFA_MEM_FRAG_TX: 257 case FFA_MEM_RECLAIM: 258 case FFA_MSG_SEND_DIRECT_REQ_64: 259 case FFA_MSG_SEND_DIRECT_REQ_32: 260 case FFA_INTERRUPT: 261 case FFA_PARTITION_INFO_GET: 262 case FFA_RXTX_UNMAP: 263 case FFA_RX_RELEASE: 264 case FFA_FEATURE_MANAGED_EXIT_INTR: 265 case FFA_NOTIFICATION_BITMAP_CREATE: 266 case FFA_NOTIFICATION_BITMAP_DESTROY: 267 case FFA_NOTIFICATION_BIND: 268 case FFA_NOTIFICATION_UNBIND: 269 case FFA_NOTIFICATION_SET: 270 case FFA_NOTIFICATION_GET: 271 case FFA_NOTIFICATION_INFO_GET_32: 272 #ifdef ARM64 273 case FFA_NOTIFICATION_INFO_GET_64: 274 #endif 275 ret_fid = FFA_SUCCESS_32; 276 ret_w2 = FFA_PARAM_MBZ; 277 break; 278 default: 279 break; 280 } 281 282 spmc_set_args(args, ret_fid, FFA_PARAM_MBZ, ret_w2, FFA_PARAM_MBZ, 283 FFA_PARAM_MBZ, FFA_PARAM_MBZ); 284 } 285 286 static int map_buf(paddr_t pa, unsigned int sz, void **va_ret) 287 { 288 tee_mm_entry_t *mm = NULL; 289 290 if (!core_pbuf_is(CORE_MEM_NON_SEC, pa, sz)) 291 return FFA_INVALID_PARAMETERS; 292 293 mm = tee_mm_alloc(&tee_mm_shm, sz); 294 if (!mm) 295 return FFA_NO_MEMORY; 296 297 if (core_mmu_map_contiguous_pages(tee_mm_get_smem(mm), pa, 298 sz / SMALL_PAGE_SIZE, 299 MEM_AREA_NSEC_SHM)) { 300 tee_mm_free(mm); 301 return FFA_INVALID_PARAMETERS; 302 } 303 304 *va_ret = (void *)tee_mm_get_smem(mm); 305 return 0; 306 } 307 308 void spmc_handle_spm_id_get(struct thread_smc_args *args) 309 { 310 spmc_set_args(args, FFA_SUCCESS_32, FFA_PARAM_MBZ, spmc_id, 311 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ); 312 } 313 314 static void unmap_buf(void *va, size_t sz) 315 { 316 tee_mm_entry_t *mm = tee_mm_find(&tee_mm_shm, (vaddr_t)va); 317 318 assert(mm); 319 core_mmu_unmap_pages(tee_mm_get_smem(mm), sz / SMALL_PAGE_SIZE); 320 tee_mm_free(mm); 321 } 322 323 void spmc_handle_rxtx_map(struct thread_smc_args *args, struct ffa_rxtx *rxtx) 324 { 325 int rc = 0; 326 unsigned int sz = 0; 327 paddr_t rx_pa = 0; 328 paddr_t tx_pa = 0; 329 void *rx = NULL; 330 void *tx = NULL; 331 332 cpu_spin_lock(&rxtx->spinlock); 333 334 if (args->a3 & GENMASK_64(63, 6)) { 335 rc = FFA_INVALID_PARAMETERS; 336 goto out; 337 } 338 339 sz = args->a3 * SMALL_PAGE_SIZE; 340 if (!sz) { 341 rc = FFA_INVALID_PARAMETERS; 342 goto out; 343 } 344 /* TX/RX are swapped compared to the caller */ 345 tx_pa = args->a2; 346 rx_pa = args->a1; 347 348 if (rxtx->size) { 349 rc = FFA_DENIED; 350 goto out; 351 } 352 353 /* 354 * If the buffer comes from a SP the address is virtual and already 355 * mapped. 356 */ 357 if (is_nw_buf(rxtx)) { 358 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 359 enum teecore_memtypes mt = MEM_AREA_NEX_NSEC_SHM; 360 bool tx_alloced = false; 361 362 /* 363 * With virtualization we establish this mapping in 364 * the nexus mapping which then is replicated to 365 * each partition. 366 * 367 * This means that this mapping must be done before 368 * any partition is created and then must not be 369 * changed. 370 */ 371 372 /* 373 * core_mmu_add_mapping() may reuse previous 374 * mappings. First check if there's any mappings to 375 * reuse so we know how to clean up in case of 376 * failure. 377 */ 378 tx = phys_to_virt(tx_pa, mt, sz); 379 rx = phys_to_virt(rx_pa, mt, sz); 380 if (!tx) { 381 tx = core_mmu_add_mapping(mt, tx_pa, sz); 382 if (!tx) { 383 rc = FFA_NO_MEMORY; 384 goto out; 385 } 386 tx_alloced = true; 387 } 388 if (!rx) 389 rx = core_mmu_add_mapping(mt, rx_pa, sz); 390 391 if (!rx) { 392 if (tx_alloced && tx) 393 core_mmu_remove_mapping(mt, tx, sz); 394 rc = FFA_NO_MEMORY; 395 goto out; 396 } 397 } else { 398 rc = map_buf(tx_pa, sz, &tx); 399 if (rc) 400 goto out; 401 rc = map_buf(rx_pa, sz, &rx); 402 if (rc) { 403 unmap_buf(tx, sz); 404 goto out; 405 } 406 } 407 rxtx->tx = tx; 408 rxtx->rx = rx; 409 } else { 410 if ((tx_pa & SMALL_PAGE_MASK) || (rx_pa & SMALL_PAGE_MASK)) { 411 rc = FFA_INVALID_PARAMETERS; 412 goto out; 413 } 414 415 if (!virt_to_phys((void *)tx_pa) || 416 !virt_to_phys((void *)rx_pa)) { 417 rc = FFA_INVALID_PARAMETERS; 418 goto out; 419 } 420 421 rxtx->tx = (void *)tx_pa; 422 rxtx->rx = (void *)rx_pa; 423 } 424 425 rxtx->size = sz; 426 rxtx->tx_is_mine = true; 427 DMSG("Mapped tx %#"PRIxPA" size %#x @ %p", tx_pa, sz, tx); 428 DMSG("Mapped rx %#"PRIxPA" size %#x @ %p", rx_pa, sz, rx); 429 out: 430 cpu_spin_unlock(&rxtx->spinlock); 431 set_simple_ret_val(args, rc); 432 } 433 434 void spmc_handle_rxtx_unmap(struct thread_smc_args *args, struct ffa_rxtx *rxtx) 435 { 436 int rc = FFA_INVALID_PARAMETERS; 437 438 cpu_spin_lock(&rxtx->spinlock); 439 440 if (!rxtx->size) 441 goto out; 442 443 /* We don't unmap the SP memory as the SP might still use it */ 444 if (is_nw_buf(rxtx)) { 445 unmap_buf(rxtx->rx, rxtx->size); 446 unmap_buf(rxtx->tx, rxtx->size); 447 } 448 rxtx->size = 0; 449 rxtx->rx = NULL; 450 rxtx->tx = NULL; 451 rc = 0; 452 out: 453 cpu_spin_unlock(&rxtx->spinlock); 454 set_simple_ret_val(args, rc); 455 } 456 457 void spmc_handle_rx_release(struct thread_smc_args *args, struct ffa_rxtx *rxtx) 458 { 459 int rc = 0; 460 461 cpu_spin_lock(&rxtx->spinlock); 462 /* The senders RX is our TX */ 463 if (!rxtx->size || rxtx->tx_is_mine) { 464 rc = FFA_DENIED; 465 } else { 466 rc = 0; 467 rxtx->tx_is_mine = true; 468 } 469 cpu_spin_unlock(&rxtx->spinlock); 470 471 set_simple_ret_val(args, rc); 472 } 473 474 static bool is_nil_uuid(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3) 475 { 476 return !w0 && !w1 && !w2 && !w3; 477 } 478 479 static bool is_my_uuid(uint32_t w0, uint32_t w1, uint32_t w2, uint32_t w3) 480 { 481 /* 482 * This depends on which UUID we have been assigned. 483 * TODO add a generic mechanism to obtain our UUID. 484 * 485 * The test below is for the hard coded UUID 486 * 486178e0-e7f8-11e3-bc5e-0002a5d5c51b 487 */ 488 return w0 == my_uuid_words[0] && w1 == my_uuid_words[1] && 489 w2 == my_uuid_words[2] && w3 == my_uuid_words[3]; 490 } 491 492 TEE_Result spmc_fill_partition_entry(uint32_t ffa_vers, void *buf, size_t blen, 493 size_t idx, uint16_t endpoint_id, 494 uint16_t execution_context, 495 uint32_t part_props, 496 const uint32_t uuid_words[4]) 497 { 498 struct ffa_partition_info_x *fpi = NULL; 499 size_t fpi_size = sizeof(*fpi); 500 501 if (ffa_vers >= FFA_VERSION_1_1) 502 fpi_size += FFA_UUID_SIZE; 503 504 if ((idx + 1) * fpi_size > blen) 505 return TEE_ERROR_OUT_OF_MEMORY; 506 507 fpi = (void *)((vaddr_t)buf + idx * fpi_size); 508 fpi->id = endpoint_id; 509 /* Number of execution contexts implemented by this partition */ 510 fpi->execution_context = execution_context; 511 512 fpi->partition_properties = part_props; 513 514 if (ffa_vers >= FFA_VERSION_1_1) { 515 if (uuid_words) 516 memcpy(fpi->uuid, uuid_words, FFA_UUID_SIZE); 517 else 518 memset(fpi->uuid, 0, FFA_UUID_SIZE); 519 } 520 521 return TEE_SUCCESS; 522 } 523 524 static int handle_partition_info_get_all(size_t *elem_count, 525 struct ffa_rxtx *rxtx, bool count_only) 526 { 527 if (!count_only) { 528 /* Add OP-TEE SP */ 529 if (spmc_fill_partition_entry(rxtx->ffa_vers, rxtx->tx, 530 rxtx->size, 0, optee_endpoint_id, 531 CFG_TEE_CORE_NB_CORE, 532 my_part_props, my_uuid_words)) 533 return FFA_NO_MEMORY; 534 } 535 *elem_count = 1; 536 537 if (IS_ENABLED(CFG_SECURE_PARTITION)) { 538 if (sp_partition_info_get(rxtx->ffa_vers, rxtx->tx, rxtx->size, 539 NULL, elem_count, count_only)) 540 return FFA_NO_MEMORY; 541 } 542 543 return FFA_OK; 544 } 545 546 void spmc_handle_partition_info_get(struct thread_smc_args *args, 547 struct ffa_rxtx *rxtx) 548 { 549 TEE_Result res = TEE_SUCCESS; 550 uint32_t ret_fid = FFA_ERROR; 551 uint32_t fpi_size = 0; 552 uint32_t rc = 0; 553 bool count_only = args->a5 & FFA_PARTITION_INFO_GET_COUNT_FLAG; 554 555 if (!count_only) { 556 cpu_spin_lock(&rxtx->spinlock); 557 558 if (!rxtx->size || !rxtx->tx_is_mine) { 559 rc = FFA_BUSY; 560 goto out; 561 } 562 } 563 564 if (is_nil_uuid(args->a1, args->a2, args->a3, args->a4)) { 565 size_t elem_count = 0; 566 567 ret_fid = handle_partition_info_get_all(&elem_count, rxtx, 568 count_only); 569 570 if (ret_fid) { 571 rc = ret_fid; 572 ret_fid = FFA_ERROR; 573 } else { 574 ret_fid = FFA_SUCCESS_32; 575 rc = elem_count; 576 } 577 578 goto out; 579 } 580 581 if (is_my_uuid(args->a1, args->a2, args->a3, args->a4)) { 582 if (!count_only) { 583 res = spmc_fill_partition_entry(rxtx->ffa_vers, 584 rxtx->tx, rxtx->size, 0, 585 optee_endpoint_id, 586 CFG_TEE_CORE_NB_CORE, 587 my_part_props, 588 my_uuid_words); 589 if (res) { 590 ret_fid = FFA_ERROR; 591 rc = FFA_INVALID_PARAMETERS; 592 goto out; 593 } 594 } 595 rc = 1; 596 } else if (IS_ENABLED(CFG_SECURE_PARTITION)) { 597 uint32_t uuid_array[4] = { 0 }; 598 TEE_UUID uuid = { }; 599 size_t count = 0; 600 601 uuid_array[0] = args->a1; 602 uuid_array[1] = args->a2; 603 uuid_array[2] = args->a3; 604 uuid_array[3] = args->a4; 605 tee_uuid_from_octets(&uuid, (uint8_t *)uuid_array); 606 607 res = sp_partition_info_get(rxtx->ffa_vers, rxtx->tx, 608 rxtx->size, &uuid, &count, 609 count_only); 610 if (res != TEE_SUCCESS) { 611 ret_fid = FFA_ERROR; 612 rc = FFA_INVALID_PARAMETERS; 613 goto out; 614 } 615 rc = count; 616 } else { 617 ret_fid = FFA_ERROR; 618 rc = FFA_INVALID_PARAMETERS; 619 goto out; 620 } 621 622 ret_fid = FFA_SUCCESS_32; 623 624 out: 625 if (ret_fid == FFA_SUCCESS_32 && !count_only && 626 rxtx->ffa_vers >= FFA_VERSION_1_1) 627 fpi_size = sizeof(struct ffa_partition_info_x) + FFA_UUID_SIZE; 628 629 spmc_set_args(args, ret_fid, FFA_PARAM_MBZ, rc, fpi_size, 630 FFA_PARAM_MBZ, FFA_PARAM_MBZ); 631 if (!count_only) { 632 rxtx->tx_is_mine = false; 633 cpu_spin_unlock(&rxtx->spinlock); 634 } 635 } 636 637 static void spmc_handle_run(struct thread_smc_args *args) 638 { 639 uint16_t endpoint = FFA_TARGET_INFO_GET_SP_ID(args->a1); 640 uint16_t thread_id = FFA_TARGET_INFO_GET_VCPU_ID(args->a1); 641 uint32_t rc = FFA_OK; 642 643 if (endpoint != optee_endpoint_id) { 644 /* 645 * The endpoint should be an SP, try to resume the SP from 646 * preempted into busy state. 647 */ 648 rc = spmc_sp_resume_from_preempted(endpoint); 649 if (rc) 650 goto out; 651 } 652 653 thread_resume_from_rpc(thread_id, 0, 0, 0, 0); 654 655 /* thread_resume_from_rpc return only of the thread_id is invalid */ 656 rc = FFA_INVALID_PARAMETERS; 657 658 out: 659 set_simple_ret_val(args, rc); 660 } 661 #endif /*CFG_CORE_SEL1_SPMC*/ 662 663 static uint32_t spmc_enable_async_notif(uint32_t bottom_half_value, 664 uint16_t vm_id) 665 { 666 uint32_t old_itr_status = 0; 667 668 if (!spmc_notif_is_ready) { 669 /* 670 * This should never happen, not if normal world respects the 671 * exchanged capabilities. 672 */ 673 EMSG("Asynchronous notifications are not ready"); 674 return TEE_ERROR_NOT_IMPLEMENTED; 675 } 676 677 if (bottom_half_value >= OPTEE_FFA_MAX_ASYNC_NOTIF_VALUE) { 678 EMSG("Invalid bottom half value %"PRIu32, bottom_half_value); 679 return TEE_ERROR_BAD_PARAMETERS; 680 } 681 682 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 683 do_bottom_half_value = bottom_half_value; 684 if (!IS_ENABLED(CFG_CORE_SEL1_SPMC)) 685 notif_vm_id = vm_id; 686 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 687 688 notif_deliver_atomic_event(NOTIF_EVENT_STARTED); 689 return TEE_SUCCESS; 690 } 691 692 static void handle_yielding_call(struct thread_smc_args *args, 693 uint32_t direct_resp_fid) 694 { 695 TEE_Result res = 0; 696 697 thread_check_canaries(); 698 699 #ifdef ARM64 700 /* Saving this for an eventual RPC */ 701 thread_get_core_local()->direct_resp_fid = direct_resp_fid; 702 #endif 703 704 if (args->a3 == OPTEE_FFA_YIELDING_CALL_RESUME) { 705 /* Note connection to struct thread_rpc_arg::ret */ 706 thread_resume_from_rpc(args->a7, args->a4, args->a5, args->a6, 707 0); 708 res = TEE_ERROR_BAD_PARAMETERS; 709 } else { 710 thread_alloc_and_run(args->a1, args->a3, args->a4, args->a5, 711 args->a6, args->a7); 712 res = TEE_ERROR_BUSY; 713 } 714 spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 715 0, res, 0, 0); 716 } 717 718 static uint32_t handle_unregister_shm(uint32_t a4, uint32_t a5) 719 { 720 uint64_t cookie = reg_pair_to_64(a5, a4); 721 uint32_t res = 0; 722 723 res = mobj_ffa_unregister_by_cookie(cookie); 724 switch (res) { 725 case TEE_SUCCESS: 726 case TEE_ERROR_ITEM_NOT_FOUND: 727 return 0; 728 case TEE_ERROR_BUSY: 729 EMSG("res %#"PRIx32, res); 730 return FFA_BUSY; 731 default: 732 EMSG("res %#"PRIx32, res); 733 return FFA_INVALID_PARAMETERS; 734 } 735 } 736 737 static void handle_blocking_call(struct thread_smc_args *args, 738 uint32_t direct_resp_fid) 739 { 740 uint32_t sec_caps = 0; 741 742 switch (args->a3) { 743 case OPTEE_FFA_GET_API_VERSION: 744 spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0, 745 OPTEE_FFA_VERSION_MAJOR, OPTEE_FFA_VERSION_MINOR, 746 0); 747 break; 748 case OPTEE_FFA_GET_OS_VERSION: 749 spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0, 750 CFG_OPTEE_REVISION_MAJOR, 751 CFG_OPTEE_REVISION_MINOR, 752 TEE_IMPL_GIT_SHA1 >> 32); 753 break; 754 case OPTEE_FFA_EXCHANGE_CAPABILITIES: 755 sec_caps = OPTEE_FFA_SEC_CAP_ARG_OFFSET; 756 if (spmc_notif_is_ready) 757 sec_caps |= OPTEE_FFA_SEC_CAP_ASYNC_NOTIF; 758 spmc_set_args(args, direct_resp_fid, 759 swap_src_dst(args->a1), 0, 0, 760 THREAD_RPC_MAX_NUM_PARAMS, sec_caps); 761 break; 762 case OPTEE_FFA_UNREGISTER_SHM: 763 spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0, 764 handle_unregister_shm(args->a4, args->a5), 0, 0); 765 break; 766 case OPTEE_FFA_ENABLE_ASYNC_NOTIF: 767 spmc_set_args(args, direct_resp_fid, 768 swap_src_dst(args->a1), 0, 769 spmc_enable_async_notif(args->a4, 770 FFA_SRC(args->a1)), 771 0, 0); 772 break; 773 default: 774 EMSG("Unhandled blocking service ID %#"PRIx32, 775 (uint32_t)args->a3); 776 spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0, 777 TEE_ERROR_BAD_PARAMETERS, 0, 0); 778 } 779 } 780 781 static void handle_framework_direct_request(struct thread_smc_args *args, 782 struct ffa_rxtx *rxtx, 783 uint32_t direct_resp_fid) 784 { 785 uint32_t w0 = FFA_ERROR; 786 uint32_t w1 = FFA_PARAM_MBZ; 787 uint32_t w2 = FFA_NOT_SUPPORTED; 788 uint32_t w3 = FFA_PARAM_MBZ; 789 790 switch (args->a2 & FFA_MSG_TYPE_MASK) { 791 case FFA_MSG_SEND_VM_CREATED: 792 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 793 uint16_t guest_id = args->a5; 794 TEE_Result res = virt_guest_created(guest_id); 795 796 w0 = direct_resp_fid; 797 w1 = swap_src_dst(args->a1); 798 w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_RESP_VM_CREATED; 799 if (res == TEE_SUCCESS) 800 w3 = FFA_OK; 801 else if (res == TEE_ERROR_OUT_OF_MEMORY) 802 w3 = FFA_DENIED; 803 else 804 w3 = FFA_INVALID_PARAMETERS; 805 } 806 break; 807 case FFA_MSG_SEND_VM_DESTROYED: 808 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 809 uint16_t guest_id = args->a5; 810 TEE_Result res = virt_guest_destroyed(guest_id); 811 812 w0 = direct_resp_fid; 813 w1 = swap_src_dst(args->a1); 814 w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_RESP_VM_DESTROYED; 815 if (res == TEE_SUCCESS) 816 w3 = FFA_OK; 817 else 818 w3 = FFA_INVALID_PARAMETERS; 819 } 820 break; 821 case FFA_MSG_VERSION_REQ: 822 w0 = direct_resp_fid; 823 w1 = swap_src_dst(args->a1); 824 w2 = FFA_MSG_FLAG_FRAMEWORK | FFA_MSG_VERSION_RESP; 825 w3 = spmc_exchange_version(args->a3, rxtx); 826 break; 827 default: 828 break; 829 } 830 spmc_set_args(args, w0, w1, w2, w3, FFA_PARAM_MBZ, FFA_PARAM_MBZ); 831 } 832 833 static void handle_direct_request(struct thread_smc_args *args, 834 struct ffa_rxtx *rxtx) 835 { 836 uint32_t direct_resp_fid = 0; 837 838 if (IS_ENABLED(CFG_SECURE_PARTITION) && 839 FFA_DST(args->a1) != spmc_id && 840 FFA_DST(args->a1) != optee_endpoint_id) { 841 spmc_sp_start_thread(args); 842 return; 843 } 844 845 if (OPTEE_SMC_IS_64(args->a0)) 846 direct_resp_fid = FFA_MSG_SEND_DIRECT_RESP_64; 847 else 848 direct_resp_fid = FFA_MSG_SEND_DIRECT_RESP_32; 849 850 if (args->a2 & FFA_MSG_FLAG_FRAMEWORK) { 851 handle_framework_direct_request(args, rxtx, direct_resp_fid); 852 return; 853 } 854 855 if (IS_ENABLED(CFG_NS_VIRTUALIZATION) && 856 virt_set_guest(get_sender_id(args->a1))) { 857 spmc_set_args(args, direct_resp_fid, swap_src_dst(args->a1), 0, 858 TEE_ERROR_ITEM_NOT_FOUND, 0, 0); 859 return; 860 } 861 862 if (args->a3 & BIT32(OPTEE_FFA_YIELDING_CALL_BIT)) 863 handle_yielding_call(args, direct_resp_fid); 864 else 865 handle_blocking_call(args, direct_resp_fid); 866 867 /* 868 * Note that handle_yielding_call() typically only returns if a 869 * thread cannot be allocated or found. virt_unset_guest() is also 870 * called from thread_state_suspend() and thread_state_free(). 871 */ 872 virt_unset_guest(); 873 } 874 875 int spmc_read_mem_transaction(uint32_t ffa_vers, void *buf, size_t blen, 876 struct ffa_mem_transaction_x *trans) 877 { 878 uint16_t mem_reg_attr = 0; 879 uint32_t flags = 0; 880 uint32_t count = 0; 881 uint32_t offs = 0; 882 uint32_t size = 0; 883 size_t n = 0; 884 885 if (!IS_ALIGNED_WITH_TYPE(buf, uint64_t)) 886 return FFA_INVALID_PARAMETERS; 887 888 if (ffa_vers >= FFA_VERSION_1_1) { 889 struct ffa_mem_transaction_1_1 *descr = NULL; 890 891 if (blen < sizeof(*descr)) 892 return FFA_INVALID_PARAMETERS; 893 894 descr = buf; 895 trans->sender_id = READ_ONCE(descr->sender_id); 896 mem_reg_attr = READ_ONCE(descr->mem_reg_attr); 897 flags = READ_ONCE(descr->flags); 898 trans->global_handle = READ_ONCE(descr->global_handle); 899 trans->tag = READ_ONCE(descr->tag); 900 901 count = READ_ONCE(descr->mem_access_count); 902 size = READ_ONCE(descr->mem_access_size); 903 offs = READ_ONCE(descr->mem_access_offs); 904 } else { 905 struct ffa_mem_transaction_1_0 *descr = NULL; 906 907 if (blen < sizeof(*descr)) 908 return FFA_INVALID_PARAMETERS; 909 910 descr = buf; 911 trans->sender_id = READ_ONCE(descr->sender_id); 912 mem_reg_attr = READ_ONCE(descr->mem_reg_attr); 913 flags = READ_ONCE(descr->flags); 914 trans->global_handle = READ_ONCE(descr->global_handle); 915 trans->tag = READ_ONCE(descr->tag); 916 917 count = READ_ONCE(descr->mem_access_count); 918 size = sizeof(struct ffa_mem_access); 919 offs = offsetof(struct ffa_mem_transaction_1_0, 920 mem_access_array); 921 } 922 923 if (mem_reg_attr > UINT8_MAX || flags > UINT8_MAX || 924 size > UINT8_MAX || count > UINT8_MAX || offs > UINT16_MAX) 925 return FFA_INVALID_PARAMETERS; 926 927 /* Check that the endpoint memory access descriptor array fits */ 928 if (MUL_OVERFLOW(size, count, &n) || ADD_OVERFLOW(offs, n, &n) || 929 n > blen) 930 return FFA_INVALID_PARAMETERS; 931 932 trans->mem_reg_attr = mem_reg_attr; 933 trans->flags = flags; 934 trans->mem_access_size = size; 935 trans->mem_access_count = count; 936 trans->mem_access_offs = offs; 937 return 0; 938 } 939 940 #if defined(CFG_CORE_SEL1_SPMC) 941 static int get_acc_perms(vaddr_t mem_acc_base, unsigned int mem_access_size, 942 unsigned int mem_access_count, uint8_t *acc_perms, 943 unsigned int *region_offs) 944 { 945 struct ffa_mem_access_perm *descr = NULL; 946 struct ffa_mem_access *mem_acc = NULL; 947 unsigned int n = 0; 948 949 for (n = 0; n < mem_access_count; n++) { 950 mem_acc = (void *)(mem_acc_base + mem_access_size * n); 951 descr = &mem_acc->access_perm; 952 if (READ_ONCE(descr->endpoint_id) == optee_endpoint_id) { 953 *acc_perms = READ_ONCE(descr->perm); 954 *region_offs = READ_ONCE(mem_acc[n].region_offs); 955 return 0; 956 } 957 } 958 959 return FFA_INVALID_PARAMETERS; 960 } 961 962 static int mem_share_init(struct ffa_mem_transaction_x *mem_trans, void *buf, 963 size_t blen, unsigned int *page_count, 964 unsigned int *region_count, size_t *addr_range_offs) 965 { 966 const uint16_t exp_mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR; 967 const uint8_t exp_mem_acc_perm = FFA_MEM_ACC_RW; 968 struct ffa_mem_region *region_descr = NULL; 969 unsigned int region_descr_offs = 0; 970 uint8_t mem_acc_perm = 0; 971 size_t n = 0; 972 973 if (mem_trans->mem_reg_attr != exp_mem_reg_attr) 974 return FFA_INVALID_PARAMETERS; 975 976 /* Check that the access permissions matches what's expected */ 977 if (get_acc_perms((vaddr_t)buf + mem_trans->mem_access_offs, 978 mem_trans->mem_access_size, 979 mem_trans->mem_access_count, 980 &mem_acc_perm, ®ion_descr_offs) || 981 mem_acc_perm != exp_mem_acc_perm) 982 return FFA_INVALID_PARAMETERS; 983 984 /* Check that the Composite memory region descriptor fits */ 985 if (ADD_OVERFLOW(region_descr_offs, sizeof(*region_descr), &n) || 986 n > blen) 987 return FFA_INVALID_PARAMETERS; 988 989 if (!IS_ALIGNED_WITH_TYPE((vaddr_t)buf + region_descr_offs, 990 struct ffa_mem_region)) 991 return FFA_INVALID_PARAMETERS; 992 993 region_descr = (struct ffa_mem_region *)((vaddr_t)buf + 994 region_descr_offs); 995 *page_count = READ_ONCE(region_descr->total_page_count); 996 *region_count = READ_ONCE(region_descr->address_range_count); 997 *addr_range_offs = n; 998 return 0; 999 } 1000 1001 static int add_mem_share_helper(struct mem_share_state *s, void *buf, 1002 size_t flen) 1003 { 1004 unsigned int region_count = flen / sizeof(struct ffa_address_range); 1005 struct ffa_address_range *arange = NULL; 1006 unsigned int n = 0; 1007 1008 if (region_count > s->region_count) 1009 region_count = s->region_count; 1010 1011 if (!IS_ALIGNED_WITH_TYPE(buf, struct ffa_address_range)) 1012 return FFA_INVALID_PARAMETERS; 1013 arange = buf; 1014 1015 for (n = 0; n < region_count; n++) { 1016 unsigned int page_count = READ_ONCE(arange[n].page_count); 1017 uint64_t addr = READ_ONCE(arange[n].address); 1018 1019 if (mobj_ffa_add_pages_at(s->mf, &s->current_page_idx, 1020 addr, page_count)) 1021 return FFA_INVALID_PARAMETERS; 1022 } 1023 1024 s->region_count -= region_count; 1025 if (s->region_count) 1026 return region_count * sizeof(*arange); 1027 1028 if (s->current_page_idx != s->page_count) 1029 return FFA_INVALID_PARAMETERS; 1030 1031 return 0; 1032 } 1033 1034 static int add_mem_share_frag(struct mem_frag_state *s, void *buf, size_t flen) 1035 { 1036 int rc = 0; 1037 1038 rc = add_mem_share_helper(&s->share, buf, flen); 1039 if (rc >= 0) { 1040 if (!ADD_OVERFLOW(s->frag_offset, rc, &s->frag_offset)) { 1041 /* We're not at the end of the descriptor yet */ 1042 if (s->share.region_count) 1043 return s->frag_offset; 1044 1045 /* We're done */ 1046 rc = 0; 1047 } else { 1048 rc = FFA_INVALID_PARAMETERS; 1049 } 1050 } 1051 1052 SLIST_REMOVE(&frag_state_head, s, mem_frag_state, link); 1053 if (rc < 0) 1054 mobj_ffa_sel1_spmc_delete(s->share.mf); 1055 else 1056 mobj_ffa_push_to_inactive(s->share.mf); 1057 free(s); 1058 1059 return rc; 1060 } 1061 1062 static bool is_sp_share(struct ffa_mem_transaction_x *mem_trans, 1063 void *buf) 1064 { 1065 struct ffa_mem_access_perm *perm = NULL; 1066 struct ffa_mem_access *mem_acc = NULL; 1067 1068 if (!IS_ENABLED(CFG_SECURE_PARTITION)) 1069 return false; 1070 1071 if (mem_trans->mem_access_count < 1) 1072 return false; 1073 1074 mem_acc = (void *)((vaddr_t)buf + mem_trans->mem_access_offs); 1075 perm = &mem_acc->access_perm; 1076 1077 /* 1078 * perm->endpoint_id is read here only to check if the endpoint is 1079 * OP-TEE. We do read it later on again, but there are some additional 1080 * checks there to make sure that the data is correct. 1081 */ 1082 return READ_ONCE(perm->endpoint_id) != optee_endpoint_id; 1083 } 1084 1085 static int add_mem_share(struct ffa_mem_transaction_x *mem_trans, 1086 tee_mm_entry_t *mm, void *buf, size_t blen, 1087 size_t flen, uint64_t *global_handle) 1088 { 1089 int rc = 0; 1090 struct mem_share_state share = { }; 1091 size_t addr_range_offs = 0; 1092 uint64_t cookie = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID; 1093 size_t n = 0; 1094 1095 rc = mem_share_init(mem_trans, buf, flen, &share.page_count, 1096 &share.region_count, &addr_range_offs); 1097 if (rc) 1098 return rc; 1099 1100 if (MUL_OVERFLOW(share.region_count, 1101 sizeof(struct ffa_address_range), &n) || 1102 ADD_OVERFLOW(n, addr_range_offs, &n) || n > blen) 1103 return FFA_INVALID_PARAMETERS; 1104 1105 if (mem_trans->global_handle) 1106 cookie = mem_trans->global_handle; 1107 share.mf = mobj_ffa_sel1_spmc_new(cookie, share.page_count); 1108 if (!share.mf) 1109 return FFA_NO_MEMORY; 1110 1111 if (flen != blen) { 1112 struct mem_frag_state *s = calloc(sizeof(*s), 1); 1113 1114 if (!s) { 1115 rc = FFA_NO_MEMORY; 1116 goto err; 1117 } 1118 s->share = share; 1119 s->mm = mm; 1120 s->frag_offset = addr_range_offs; 1121 1122 SLIST_INSERT_HEAD(&frag_state_head, s, link); 1123 rc = add_mem_share_frag(s, (char *)buf + addr_range_offs, 1124 flen - addr_range_offs); 1125 1126 if (rc >= 0) 1127 *global_handle = mobj_ffa_get_cookie(share.mf); 1128 1129 return rc; 1130 } 1131 1132 rc = add_mem_share_helper(&share, (char *)buf + addr_range_offs, 1133 flen - addr_range_offs); 1134 if (rc) { 1135 /* 1136 * Number of consumed bytes may be returned instead of 0 for 1137 * done. 1138 */ 1139 rc = FFA_INVALID_PARAMETERS; 1140 goto err; 1141 } 1142 1143 *global_handle = mobj_ffa_push_to_inactive(share.mf); 1144 1145 return 0; 1146 err: 1147 mobj_ffa_sel1_spmc_delete(share.mf); 1148 return rc; 1149 } 1150 1151 static int handle_mem_share_tmem(paddr_t pbuf, size_t blen, size_t flen, 1152 unsigned int page_count, 1153 uint64_t *global_handle, struct ffa_rxtx *rxtx) 1154 { 1155 struct ffa_mem_transaction_x mem_trans = { }; 1156 int rc = 0; 1157 size_t len = 0; 1158 void *buf = NULL; 1159 tee_mm_entry_t *mm = NULL; 1160 vaddr_t offs = pbuf & SMALL_PAGE_MASK; 1161 1162 if (MUL_OVERFLOW(page_count, SMALL_PAGE_SIZE, &len)) 1163 return FFA_INVALID_PARAMETERS; 1164 if (!core_pbuf_is(CORE_MEM_NON_SEC, pbuf, len)) 1165 return FFA_INVALID_PARAMETERS; 1166 1167 /* 1168 * Check that the length reported in flen is covered by len even 1169 * if the offset is taken into account. 1170 */ 1171 if (len < flen || len - offs < flen) 1172 return FFA_INVALID_PARAMETERS; 1173 1174 mm = tee_mm_alloc(&tee_mm_shm, len); 1175 if (!mm) 1176 return FFA_NO_MEMORY; 1177 1178 if (core_mmu_map_contiguous_pages(tee_mm_get_smem(mm), pbuf, 1179 page_count, MEM_AREA_NSEC_SHM)) { 1180 rc = FFA_INVALID_PARAMETERS; 1181 goto out; 1182 } 1183 buf = (void *)(tee_mm_get_smem(mm) + offs); 1184 1185 cpu_spin_lock(&rxtx->spinlock); 1186 rc = spmc_read_mem_transaction(rxtx->ffa_vers, buf, flen, &mem_trans); 1187 if (!rc && IS_ENABLED(CFG_NS_VIRTUALIZATION) && 1188 virt_set_guest(mem_trans.sender_id)) 1189 rc = FFA_DENIED; 1190 if (!rc) 1191 rc = add_mem_share(&mem_trans, mm, buf, blen, flen, 1192 global_handle); 1193 virt_unset_guest(); 1194 cpu_spin_unlock(&rxtx->spinlock); 1195 if (rc > 0) 1196 return rc; 1197 1198 core_mmu_unmap_pages(tee_mm_get_smem(mm), page_count); 1199 out: 1200 tee_mm_free(mm); 1201 return rc; 1202 } 1203 1204 static int handle_mem_share_rxbuf(size_t blen, size_t flen, 1205 uint64_t *global_handle, 1206 struct ffa_rxtx *rxtx) 1207 { 1208 struct ffa_mem_transaction_x mem_trans = { }; 1209 int rc = FFA_DENIED; 1210 1211 cpu_spin_lock(&rxtx->spinlock); 1212 1213 if (!rxtx->rx || flen > rxtx->size) 1214 goto out; 1215 1216 rc = spmc_read_mem_transaction(rxtx->ffa_vers, rxtx->rx, flen, 1217 &mem_trans); 1218 if (rc) 1219 goto out; 1220 if (is_sp_share(&mem_trans, rxtx->rx)) { 1221 rc = spmc_sp_add_share(&mem_trans, rxtx, blen, 1222 global_handle, NULL); 1223 goto out; 1224 } 1225 1226 if (IS_ENABLED(CFG_NS_VIRTUALIZATION) && 1227 virt_set_guest(mem_trans.sender_id)) 1228 goto out; 1229 1230 rc = add_mem_share(&mem_trans, NULL, rxtx->rx, blen, flen, 1231 global_handle); 1232 1233 virt_unset_guest(); 1234 1235 out: 1236 cpu_spin_unlock(&rxtx->spinlock); 1237 1238 return rc; 1239 } 1240 1241 static void handle_mem_share(struct thread_smc_args *args, 1242 struct ffa_rxtx *rxtx) 1243 { 1244 uint32_t tot_len = args->a1; 1245 uint32_t frag_len = args->a2; 1246 uint64_t addr = args->a3; 1247 uint32_t page_count = args->a4; 1248 uint32_t ret_w1 = 0; 1249 uint32_t ret_w2 = FFA_INVALID_PARAMETERS; 1250 uint32_t ret_w3 = 0; 1251 uint32_t ret_fid = FFA_ERROR; 1252 uint64_t global_handle = 0; 1253 int rc = 0; 1254 1255 /* Check that the MBZs are indeed 0 */ 1256 if (args->a5 || args->a6 || args->a7) 1257 goto out; 1258 1259 /* Check that fragment length doesn't exceed total length */ 1260 if (frag_len > tot_len) 1261 goto out; 1262 1263 /* Check for 32-bit calling convention */ 1264 if (args->a0 == FFA_MEM_SHARE_32) 1265 addr &= UINT32_MAX; 1266 1267 if (!addr) { 1268 /* 1269 * The memory transaction descriptor is passed via our rx 1270 * buffer. 1271 */ 1272 if (page_count) 1273 goto out; 1274 rc = handle_mem_share_rxbuf(tot_len, frag_len, &global_handle, 1275 rxtx); 1276 } else { 1277 rc = handle_mem_share_tmem(addr, tot_len, frag_len, page_count, 1278 &global_handle, rxtx); 1279 } 1280 if (rc < 0) { 1281 ret_w2 = rc; 1282 } else if (rc > 0) { 1283 ret_fid = FFA_MEM_FRAG_RX; 1284 ret_w3 = rc; 1285 reg_pair_from_64(global_handle, &ret_w2, &ret_w1); 1286 } else { 1287 ret_fid = FFA_SUCCESS_32; 1288 reg_pair_from_64(global_handle, &ret_w3, &ret_w2); 1289 } 1290 out: 1291 spmc_set_args(args, ret_fid, ret_w1, ret_w2, ret_w3, 0, 0); 1292 } 1293 1294 static struct mem_frag_state *get_frag_state(uint64_t global_handle) 1295 { 1296 struct mem_frag_state *s = NULL; 1297 1298 SLIST_FOREACH(s, &frag_state_head, link) 1299 if (mobj_ffa_get_cookie(s->share.mf) == global_handle) 1300 return s; 1301 1302 return NULL; 1303 } 1304 1305 static void handle_mem_frag_tx(struct thread_smc_args *args, 1306 struct ffa_rxtx *rxtx) 1307 { 1308 uint64_t global_handle = reg_pair_to_64(args->a2, args->a1); 1309 size_t flen = args->a3; 1310 uint32_t endpoint_id = args->a4; 1311 struct mem_frag_state *s = NULL; 1312 tee_mm_entry_t *mm = NULL; 1313 unsigned int page_count = 0; 1314 void *buf = NULL; 1315 uint32_t ret_w1 = 0; 1316 uint32_t ret_w2 = 0; 1317 uint32_t ret_w3 = 0; 1318 uint32_t ret_fid = 0; 1319 int rc = 0; 1320 1321 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 1322 uint16_t guest_id = endpoint_id >> 16; 1323 1324 if (!guest_id || virt_set_guest(guest_id)) { 1325 rc = FFA_INVALID_PARAMETERS; 1326 goto out_set_rc; 1327 } 1328 } 1329 1330 /* 1331 * Currently we're only doing this for fragmented FFA_MEM_SHARE_* 1332 * requests. 1333 */ 1334 1335 cpu_spin_lock(&rxtx->spinlock); 1336 1337 s = get_frag_state(global_handle); 1338 if (!s) { 1339 rc = FFA_INVALID_PARAMETERS; 1340 goto out; 1341 } 1342 1343 mm = s->mm; 1344 if (mm) { 1345 if (flen > tee_mm_get_bytes(mm)) { 1346 rc = FFA_INVALID_PARAMETERS; 1347 goto out; 1348 } 1349 page_count = s->share.page_count; 1350 buf = (void *)tee_mm_get_smem(mm); 1351 } else { 1352 if (flen > rxtx->size) { 1353 rc = FFA_INVALID_PARAMETERS; 1354 goto out; 1355 } 1356 buf = rxtx->rx; 1357 } 1358 1359 rc = add_mem_share_frag(s, buf, flen); 1360 out: 1361 virt_unset_guest(); 1362 cpu_spin_unlock(&rxtx->spinlock); 1363 1364 if (rc <= 0 && mm) { 1365 core_mmu_unmap_pages(tee_mm_get_smem(mm), page_count); 1366 tee_mm_free(mm); 1367 } 1368 1369 out_set_rc: 1370 if (rc < 0) { 1371 ret_fid = FFA_ERROR; 1372 ret_w2 = rc; 1373 } else if (rc > 0) { 1374 ret_fid = FFA_MEM_FRAG_RX; 1375 ret_w3 = rc; 1376 reg_pair_from_64(global_handle, &ret_w2, &ret_w1); 1377 } else { 1378 ret_fid = FFA_SUCCESS_32; 1379 reg_pair_from_64(global_handle, &ret_w3, &ret_w2); 1380 } 1381 1382 spmc_set_args(args, ret_fid, ret_w1, ret_w2, ret_w3, 0, 0); 1383 } 1384 1385 static void handle_mem_reclaim(struct thread_smc_args *args) 1386 { 1387 int rc = FFA_INVALID_PARAMETERS; 1388 uint64_t cookie = 0; 1389 1390 if (args->a3 || args->a4 || args->a5 || args->a6 || args->a7) 1391 goto out; 1392 1393 cookie = reg_pair_to_64(args->a2, args->a1); 1394 if (IS_ENABLED(CFG_NS_VIRTUALIZATION)) { 1395 uint16_t guest_id = 0; 1396 1397 if (cookie & FFA_MEMORY_HANDLE_HYPERVISOR_BIT) { 1398 guest_id = virt_find_guest_by_cookie(cookie); 1399 } else { 1400 guest_id = (cookie >> FFA_MEMORY_HANDLE_PRTN_SHIFT) & 1401 FFA_MEMORY_HANDLE_PRTN_MASK; 1402 } 1403 if (!guest_id) 1404 goto out; 1405 if (virt_set_guest(guest_id)) { 1406 if (!virt_reclaim_cookie_from_destroyed_guest(guest_id, 1407 cookie)) 1408 rc = FFA_OK; 1409 goto out; 1410 } 1411 } 1412 1413 switch (mobj_ffa_sel1_spmc_reclaim(cookie)) { 1414 case TEE_SUCCESS: 1415 rc = FFA_OK; 1416 break; 1417 case TEE_ERROR_ITEM_NOT_FOUND: 1418 DMSG("cookie %#"PRIx64" not found", cookie); 1419 rc = FFA_INVALID_PARAMETERS; 1420 break; 1421 default: 1422 DMSG("cookie %#"PRIx64" busy", cookie); 1423 rc = FFA_DENIED; 1424 break; 1425 } 1426 1427 virt_unset_guest(); 1428 1429 out: 1430 set_simple_ret_val(args, rc); 1431 } 1432 1433 static void handle_notification_bitmap_create(struct thread_smc_args *args) 1434 { 1435 uint32_t ret_val = FFA_INVALID_PARAMETERS; 1436 uint32_t ret_fid = FFA_ERROR; 1437 uint32_t old_itr_status = 0; 1438 1439 if (!FFA_TARGET_INFO_GET_SP_ID(args->a1) && !args->a3 && !args->a4 && 1440 !args->a5 && !args->a6 && !args->a7) { 1441 uint16_t vm_id = args->a1; 1442 1443 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 1444 1445 if (notif_vm_id_valid) { 1446 if (vm_id == notif_vm_id) 1447 ret_val = FFA_DENIED; 1448 else 1449 ret_val = FFA_NO_MEMORY; 1450 } else { 1451 notif_vm_id = vm_id; 1452 notif_vm_id_valid = true; 1453 ret_val = FFA_OK; 1454 ret_fid = FFA_SUCCESS_32; 1455 } 1456 1457 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 1458 } 1459 1460 spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0); 1461 } 1462 1463 static void handle_notification_bitmap_destroy(struct thread_smc_args *args) 1464 { 1465 uint32_t ret_val = FFA_INVALID_PARAMETERS; 1466 uint32_t ret_fid = FFA_ERROR; 1467 uint32_t old_itr_status = 0; 1468 1469 if (!FFA_TARGET_INFO_GET_SP_ID(args->a1) && !args->a3 && !args->a4 && 1470 !args->a5 && !args->a6 && !args->a7) { 1471 uint16_t vm_id = args->a1; 1472 1473 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 1474 1475 if (notif_vm_id_valid && vm_id == notif_vm_id) { 1476 if (notif_pending_bitmap || notif_bound_bitmap) { 1477 ret_val = FFA_DENIED; 1478 } else { 1479 notif_vm_id_valid = false; 1480 ret_val = FFA_OK; 1481 ret_fid = FFA_SUCCESS_32; 1482 } 1483 } 1484 1485 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 1486 } 1487 1488 spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0); 1489 } 1490 1491 static void handle_notification_bind(struct thread_smc_args *args) 1492 { 1493 uint32_t ret_val = FFA_INVALID_PARAMETERS; 1494 uint32_t ret_fid = FFA_ERROR; 1495 uint32_t old_itr_status = 0; 1496 uint64_t bitmap = 0; 1497 uint16_t vm_id = 0; 1498 1499 if (args->a5 || args->a6 || args->a7) 1500 goto out; 1501 if (args->a2) { 1502 /* We only deal with global notifications for now */ 1503 ret_val = FFA_NOT_SUPPORTED; 1504 goto out; 1505 } 1506 1507 /* The destination of the eventual notification */ 1508 vm_id = FFA_DST(args->a1); 1509 bitmap = reg_pair_to_64(args->a4, args->a3); 1510 1511 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 1512 1513 if (notif_vm_id_valid && vm_id == notif_vm_id) { 1514 if (bitmap & notif_bound_bitmap) { 1515 ret_val = FFA_DENIED; 1516 } else { 1517 notif_bound_bitmap |= bitmap; 1518 ret_val = FFA_OK; 1519 ret_fid = FFA_SUCCESS_32; 1520 } 1521 } 1522 1523 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 1524 out: 1525 spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0); 1526 } 1527 1528 static void handle_notification_unbind(struct thread_smc_args *args) 1529 { 1530 uint32_t ret_val = FFA_INVALID_PARAMETERS; 1531 uint32_t ret_fid = FFA_ERROR; 1532 uint32_t old_itr_status = 0; 1533 uint64_t bitmap = 0; 1534 uint16_t vm_id = 0; 1535 1536 if (args->a2 || args->a5 || args->a6 || args->a7) 1537 goto out; 1538 1539 /* The destination of the eventual notification */ 1540 vm_id = FFA_DST(args->a1); 1541 bitmap = reg_pair_to_64(args->a4, args->a3); 1542 1543 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 1544 1545 if (notif_vm_id_valid && vm_id == notif_vm_id) { 1546 /* 1547 * Spec says: 1548 * At least one notification is bound to another Sender or 1549 * is currently pending. 1550 * 1551 * Not sure what the intention is. 1552 */ 1553 if (bitmap & notif_pending_bitmap) { 1554 ret_val = FFA_DENIED; 1555 } else { 1556 notif_bound_bitmap &= ~bitmap; 1557 ret_val = FFA_OK; 1558 ret_fid = FFA_SUCCESS_32; 1559 } 1560 } 1561 1562 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 1563 out: 1564 spmc_set_args(args, ret_fid, 0, ret_val, 0, 0, 0); 1565 } 1566 1567 static void handle_notification_get(struct thread_smc_args *args) 1568 { 1569 uint32_t w2 = FFA_INVALID_PARAMETERS; 1570 uint32_t ret_fid = FFA_ERROR; 1571 uint32_t old_itr_status = 0; 1572 uint16_t vm_id = 0; 1573 uint32_t w3 = 0; 1574 1575 if (args->a5 || args->a6 || args->a7) 1576 goto out; 1577 if (!(args->a2 & 0x1)) { 1578 ret_fid = FFA_SUCCESS_32; 1579 w2 = 0; 1580 goto out; 1581 } 1582 vm_id = FFA_DST(args->a1); 1583 1584 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 1585 1586 if (notif_vm_id_valid && vm_id == notif_vm_id) { 1587 reg_pair_from_64(notif_pending_bitmap, &w3, &w2); 1588 notif_pending_bitmap = 0; 1589 ret_fid = FFA_SUCCESS_32; 1590 } 1591 1592 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 1593 out: 1594 spmc_set_args(args, ret_fid, 0, w2, w3, 0, 0); 1595 } 1596 1597 static void handle_notification_info_get(struct thread_smc_args *args) 1598 { 1599 uint32_t w2 = FFA_INVALID_PARAMETERS; 1600 uint32_t ret_fid = FFA_ERROR; 1601 1602 if (args->a1 || args->a2 || args->a3 || args->a4 || args->a5 || 1603 args->a6 || args->a7) 1604 goto out; 1605 1606 if (OPTEE_SMC_IS_64(args->a0)) 1607 ret_fid = FFA_SUCCESS_64; 1608 else 1609 ret_fid = FFA_SUCCESS_32; 1610 1611 /* 1612 * Note, we're only supporting physical OS kernel in normal world 1613 * with Global Notifications. 1614 * So one list of ID list registers (BIT[11:7]) 1615 * and one count of IDs (BIT[13:12] + 1) 1616 * and the VM is always 0. 1617 */ 1618 w2 = SHIFT_U32(1, 7); 1619 out: 1620 spmc_set_args(args, ret_fid, 0, w2, 0, 0, 0); 1621 } 1622 1623 void thread_spmc_set_async_notif_intid(int intid) 1624 { 1625 assert(interrupt_can_raise_sgi(interrupt_get_main_chip())); 1626 notif_intid = intid; 1627 spmc_notif_is_ready = true; 1628 DMSG("Asynchronous notifications are ready"); 1629 } 1630 1631 void notif_send_async(uint32_t value) 1632 { 1633 uint32_t old_itr_status = 0; 1634 1635 old_itr_status = cpu_spin_lock_xsave(&spmc_notif_lock); 1636 assert(value == NOTIF_VALUE_DO_BOTTOM_HALF && spmc_notif_is_ready && 1637 do_bottom_half_value >= 0 && notif_intid >= 0); 1638 notif_pending_bitmap |= BIT64(do_bottom_half_value); 1639 interrupt_raise_sgi(interrupt_get_main_chip(), notif_intid, 1640 ITR_CPU_MASK_TO_THIS_CPU); 1641 cpu_spin_unlock_xrestore(&spmc_notif_lock, old_itr_status); 1642 } 1643 #else 1644 void notif_send_async(uint32_t value) 1645 { 1646 /* global notification, delay notification interrupt */ 1647 uint32_t flags = BIT32(1); 1648 int res = 0; 1649 1650 assert(value == NOTIF_VALUE_DO_BOTTOM_HALF && spmc_notif_is_ready && 1651 do_bottom_half_value >= 0); 1652 res = ffa_set_notification(notif_vm_id, optee_endpoint_id, flags, 1653 BIT64(do_bottom_half_value)); 1654 if (res) { 1655 EMSG("notification set failed with error %d", res); 1656 panic(); 1657 } 1658 } 1659 #endif 1660 1661 /* Only called from assembly */ 1662 void thread_spmc_msg_recv(struct thread_smc_args *args); 1663 void thread_spmc_msg_recv(struct thread_smc_args *args) 1664 { 1665 assert((thread_get_exceptions() & THREAD_EXCP_ALL) == THREAD_EXCP_ALL); 1666 switch (args->a0) { 1667 #if defined(CFG_CORE_SEL1_SPMC) 1668 case FFA_FEATURES: 1669 handle_features(args); 1670 break; 1671 case FFA_SPM_ID_GET: 1672 spmc_handle_spm_id_get(args); 1673 break; 1674 #ifdef ARM64 1675 case FFA_RXTX_MAP_64: 1676 #endif 1677 case FFA_RXTX_MAP_32: 1678 spmc_handle_rxtx_map(args, &my_rxtx); 1679 break; 1680 case FFA_RXTX_UNMAP: 1681 spmc_handle_rxtx_unmap(args, &my_rxtx); 1682 break; 1683 case FFA_RX_RELEASE: 1684 spmc_handle_rx_release(args, &my_rxtx); 1685 break; 1686 case FFA_PARTITION_INFO_GET: 1687 spmc_handle_partition_info_get(args, &my_rxtx); 1688 break; 1689 case FFA_RUN: 1690 spmc_handle_run(args); 1691 break; 1692 #endif /*CFG_CORE_SEL1_SPMC*/ 1693 case FFA_INTERRUPT: 1694 if (IS_ENABLED(CFG_CORE_SEL1_SPMC)) 1695 spmc_set_args(args, FFA_NORMAL_WORLD_RESUME, 0, 0, 0, 1696 0, 0); 1697 else 1698 spmc_set_args(args, FFA_MSG_WAIT, 0, 0, 0, 0, 0); 1699 break; 1700 #ifdef ARM64 1701 case FFA_MSG_SEND_DIRECT_REQ_64: 1702 #endif 1703 case FFA_MSG_SEND_DIRECT_REQ_32: 1704 handle_direct_request(args, &my_rxtx); 1705 break; 1706 #if defined(CFG_CORE_SEL1_SPMC) 1707 #ifdef ARM64 1708 case FFA_MEM_SHARE_64: 1709 #endif 1710 case FFA_MEM_SHARE_32: 1711 handle_mem_share(args, &my_rxtx); 1712 break; 1713 case FFA_MEM_RECLAIM: 1714 if (!IS_ENABLED(CFG_SECURE_PARTITION) || 1715 !ffa_mem_reclaim(args, NULL)) 1716 handle_mem_reclaim(args); 1717 break; 1718 case FFA_MEM_FRAG_TX: 1719 handle_mem_frag_tx(args, &my_rxtx); 1720 break; 1721 case FFA_NOTIFICATION_BITMAP_CREATE: 1722 handle_notification_bitmap_create(args); 1723 break; 1724 case FFA_NOTIFICATION_BITMAP_DESTROY: 1725 handle_notification_bitmap_destroy(args); 1726 break; 1727 case FFA_NOTIFICATION_BIND: 1728 handle_notification_bind(args); 1729 break; 1730 case FFA_NOTIFICATION_UNBIND: 1731 handle_notification_unbind(args); 1732 break; 1733 case FFA_NOTIFICATION_GET: 1734 handle_notification_get(args); 1735 break; 1736 #ifdef ARM64 1737 case FFA_NOTIFICATION_INFO_GET_64: 1738 #endif 1739 case FFA_NOTIFICATION_INFO_GET_32: 1740 handle_notification_info_get(args); 1741 break; 1742 #endif /*CFG_CORE_SEL1_SPMC*/ 1743 case FFA_ERROR: 1744 EMSG("Cannot handle FFA_ERROR(%d)", (int)args->a2); 1745 if (!IS_ENABLED(CFG_CORE_SEL1_SPMC)) { 1746 /* 1747 * The SPMC will return an FFA_ERROR back so better 1748 * panic() now than flooding the log. 1749 */ 1750 panic("FFA_ERROR from SPMC is fatal"); 1751 } 1752 spmc_set_args(args, FFA_ERROR, FFA_PARAM_MBZ, FFA_NOT_SUPPORTED, 1753 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ); 1754 break; 1755 default: 1756 EMSG("Unhandled FFA function ID %#"PRIx32, (uint32_t)args->a0); 1757 set_simple_ret_val(args, FFA_NOT_SUPPORTED); 1758 } 1759 } 1760 1761 static TEE_Result yielding_call_with_arg(uint64_t cookie, uint32_t offset) 1762 { 1763 size_t sz_rpc = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS); 1764 struct thread_ctx *thr = threads + thread_get_id(); 1765 TEE_Result res = TEE_ERROR_BAD_PARAMETERS; 1766 struct optee_msg_arg *arg = NULL; 1767 struct mobj *mobj = NULL; 1768 uint32_t num_params = 0; 1769 size_t sz = 0; 1770 1771 mobj = mobj_ffa_get_by_cookie(cookie, 0); 1772 if (!mobj) { 1773 EMSG("Can't find cookie %#"PRIx64, cookie); 1774 return TEE_ERROR_BAD_PARAMETERS; 1775 } 1776 1777 res = mobj_inc_map(mobj); 1778 if (res) 1779 goto out_put_mobj; 1780 1781 res = TEE_ERROR_BAD_PARAMETERS; 1782 arg = mobj_get_va(mobj, offset, sizeof(*arg)); 1783 if (!arg) 1784 goto out_dec_map; 1785 1786 num_params = READ_ONCE(arg->num_params); 1787 if (num_params > OPTEE_MSG_MAX_NUM_PARAMS) 1788 goto out_dec_map; 1789 1790 sz = OPTEE_MSG_GET_ARG_SIZE(num_params); 1791 1792 thr->rpc_arg = mobj_get_va(mobj, offset + sz, sz_rpc); 1793 if (!thr->rpc_arg) 1794 goto out_dec_map; 1795 1796 virt_on_stdcall(); 1797 res = tee_entry_std(arg, num_params); 1798 1799 thread_rpc_shm_cache_clear(&thr->shm_cache); 1800 thr->rpc_arg = NULL; 1801 1802 out_dec_map: 1803 mobj_dec_map(mobj); 1804 out_put_mobj: 1805 mobj_put(mobj); 1806 return res; 1807 } 1808 1809 /* 1810 * Helper routine for the assembly function thread_std_smc_entry() 1811 * 1812 * Note: this function is weak just to make link_dummies_paged.c happy. 1813 */ 1814 uint32_t __weak __thread_std_smc_entry(uint32_t a0, uint32_t a1, 1815 uint32_t a2, uint32_t a3, 1816 uint32_t a4, uint32_t a5 __unused) 1817 { 1818 /* 1819 * Arguments are supplied from handle_yielding_call() as: 1820 * a0 <- w1 1821 * a1 <- w3 1822 * a2 <- w4 1823 * a3 <- w5 1824 * a4 <- w6 1825 * a5 <- w7 1826 */ 1827 thread_get_tsd()->rpc_target_info = swap_src_dst(a0); 1828 if (a1 == OPTEE_FFA_YIELDING_CALL_WITH_ARG) 1829 return yielding_call_with_arg(reg_pair_to_64(a3, a2), a4); 1830 return FFA_DENIED; 1831 } 1832 1833 static bool set_fmem(struct optee_msg_param *param, struct thread_param *tpm) 1834 { 1835 uint64_t offs = tpm->u.memref.offs; 1836 1837 param->attr = tpm->attr - THREAD_PARAM_ATTR_MEMREF_IN + 1838 OPTEE_MSG_ATTR_TYPE_FMEM_INPUT; 1839 1840 param->u.fmem.offs_low = offs; 1841 param->u.fmem.offs_high = offs >> 32; 1842 if (param->u.fmem.offs_high != offs >> 32) 1843 return false; 1844 1845 param->u.fmem.size = tpm->u.memref.size; 1846 if (tpm->u.memref.mobj) { 1847 uint64_t cookie = mobj_get_cookie(tpm->u.memref.mobj); 1848 1849 /* If a mobj is passed it better be one with a valid cookie. */ 1850 if (cookie == OPTEE_MSG_FMEM_INVALID_GLOBAL_ID) 1851 return false; 1852 param->u.fmem.global_id = cookie; 1853 } else { 1854 param->u.fmem.global_id = OPTEE_MSG_FMEM_INVALID_GLOBAL_ID; 1855 } 1856 1857 return true; 1858 } 1859 1860 static uint32_t get_rpc_arg(uint32_t cmd, size_t num_params, 1861 struct thread_param *params, 1862 struct optee_msg_arg **arg_ret) 1863 { 1864 size_t sz = OPTEE_MSG_GET_ARG_SIZE(THREAD_RPC_MAX_NUM_PARAMS); 1865 struct thread_ctx *thr = threads + thread_get_id(); 1866 struct optee_msg_arg *arg = thr->rpc_arg; 1867 1868 if (num_params > THREAD_RPC_MAX_NUM_PARAMS) 1869 return TEE_ERROR_BAD_PARAMETERS; 1870 1871 if (!arg) { 1872 EMSG("rpc_arg not set"); 1873 return TEE_ERROR_GENERIC; 1874 } 1875 1876 memset(arg, 0, sz); 1877 arg->cmd = cmd; 1878 arg->num_params = num_params; 1879 arg->ret = TEE_ERROR_GENERIC; /* in case value isn't updated */ 1880 1881 for (size_t n = 0; n < num_params; n++) { 1882 switch (params[n].attr) { 1883 case THREAD_PARAM_ATTR_NONE: 1884 arg->params[n].attr = OPTEE_MSG_ATTR_TYPE_NONE; 1885 break; 1886 case THREAD_PARAM_ATTR_VALUE_IN: 1887 case THREAD_PARAM_ATTR_VALUE_OUT: 1888 case THREAD_PARAM_ATTR_VALUE_INOUT: 1889 arg->params[n].attr = params[n].attr - 1890 THREAD_PARAM_ATTR_VALUE_IN + 1891 OPTEE_MSG_ATTR_TYPE_VALUE_INPUT; 1892 arg->params[n].u.value.a = params[n].u.value.a; 1893 arg->params[n].u.value.b = params[n].u.value.b; 1894 arg->params[n].u.value.c = params[n].u.value.c; 1895 break; 1896 case THREAD_PARAM_ATTR_MEMREF_IN: 1897 case THREAD_PARAM_ATTR_MEMREF_OUT: 1898 case THREAD_PARAM_ATTR_MEMREF_INOUT: 1899 if (!set_fmem(arg->params + n, params + n)) 1900 return TEE_ERROR_BAD_PARAMETERS; 1901 break; 1902 default: 1903 return TEE_ERROR_BAD_PARAMETERS; 1904 } 1905 } 1906 1907 if (arg_ret) 1908 *arg_ret = arg; 1909 1910 return TEE_SUCCESS; 1911 } 1912 1913 static uint32_t get_rpc_arg_res(struct optee_msg_arg *arg, size_t num_params, 1914 struct thread_param *params) 1915 { 1916 for (size_t n = 0; n < num_params; n++) { 1917 switch (params[n].attr) { 1918 case THREAD_PARAM_ATTR_VALUE_OUT: 1919 case THREAD_PARAM_ATTR_VALUE_INOUT: 1920 params[n].u.value.a = arg->params[n].u.value.a; 1921 params[n].u.value.b = arg->params[n].u.value.b; 1922 params[n].u.value.c = arg->params[n].u.value.c; 1923 break; 1924 case THREAD_PARAM_ATTR_MEMREF_OUT: 1925 case THREAD_PARAM_ATTR_MEMREF_INOUT: 1926 params[n].u.memref.size = arg->params[n].u.fmem.size; 1927 break; 1928 default: 1929 break; 1930 } 1931 } 1932 1933 return arg->ret; 1934 } 1935 1936 uint32_t thread_rpc_cmd(uint32_t cmd, size_t num_params, 1937 struct thread_param *params) 1938 { 1939 struct thread_rpc_arg rpc_arg = { .call = { 1940 .w1 = thread_get_tsd()->rpc_target_info, 1941 .w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD, 1942 }, 1943 }; 1944 struct optee_msg_arg *arg = NULL; 1945 uint32_t ret = 0; 1946 1947 ret = get_rpc_arg(cmd, num_params, params, &arg); 1948 if (ret) 1949 return ret; 1950 1951 thread_rpc(&rpc_arg); 1952 1953 return get_rpc_arg_res(arg, num_params, params); 1954 } 1955 1956 static void thread_rpc_free(unsigned int bt, uint64_t cookie, struct mobj *mobj) 1957 { 1958 struct thread_rpc_arg rpc_arg = { .call = { 1959 .w1 = thread_get_tsd()->rpc_target_info, 1960 .w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD, 1961 }, 1962 }; 1963 struct thread_param param = THREAD_PARAM_VALUE(IN, bt, cookie, 0); 1964 uint32_t res2 = 0; 1965 uint32_t res = 0; 1966 1967 DMSG("freeing cookie %#"PRIx64, cookie); 1968 1969 res = get_rpc_arg(OPTEE_RPC_CMD_SHM_FREE, 1, ¶m, NULL); 1970 1971 mobj_put(mobj); 1972 res2 = mobj_ffa_unregister_by_cookie(cookie); 1973 if (res2) 1974 DMSG("mobj_ffa_unregister_by_cookie(%#"PRIx64"): %#"PRIx32, 1975 cookie, res2); 1976 if (!res) 1977 thread_rpc(&rpc_arg); 1978 } 1979 1980 static struct mobj *thread_rpc_alloc(size_t size, size_t align, unsigned int bt) 1981 { 1982 struct thread_rpc_arg rpc_arg = { .call = { 1983 .w1 = thread_get_tsd()->rpc_target_info, 1984 .w4 = OPTEE_FFA_YIELDING_CALL_RETURN_RPC_CMD, 1985 }, 1986 }; 1987 struct thread_param param = THREAD_PARAM_VALUE(IN, bt, size, align); 1988 struct optee_msg_arg *arg = NULL; 1989 unsigned int internal_offset = 0; 1990 struct mobj *mobj = NULL; 1991 uint64_t cookie = 0; 1992 1993 if (get_rpc_arg(OPTEE_RPC_CMD_SHM_ALLOC, 1, ¶m, &arg)) 1994 return NULL; 1995 1996 thread_rpc(&rpc_arg); 1997 1998 if (arg->num_params != 1 || 1999 arg->params->attr != OPTEE_MSG_ATTR_TYPE_FMEM_OUTPUT) 2000 return NULL; 2001 2002 internal_offset = READ_ONCE(arg->params->u.fmem.internal_offs); 2003 cookie = READ_ONCE(arg->params->u.fmem.global_id); 2004 mobj = mobj_ffa_get_by_cookie(cookie, internal_offset); 2005 if (!mobj) { 2006 DMSG("mobj_ffa_get_by_cookie(%#"PRIx64", %#x): failed", 2007 cookie, internal_offset); 2008 return NULL; 2009 } 2010 2011 assert(mobj_is_nonsec(mobj)); 2012 2013 if (mobj->size < size) { 2014 DMSG("Mobj %#"PRIx64": wrong size", cookie); 2015 mobj_put(mobj); 2016 return NULL; 2017 } 2018 2019 if (mobj_inc_map(mobj)) { 2020 DMSG("mobj_inc_map(%#"PRIx64"): failed", cookie); 2021 mobj_put(mobj); 2022 return NULL; 2023 } 2024 2025 return mobj; 2026 } 2027 2028 struct mobj *thread_rpc_alloc_payload(size_t size) 2029 { 2030 return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_APPL); 2031 } 2032 2033 struct mobj *thread_rpc_alloc_kernel_payload(size_t size) 2034 { 2035 return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_KERNEL); 2036 } 2037 2038 void thread_rpc_free_kernel_payload(struct mobj *mobj) 2039 { 2040 if (mobj) 2041 thread_rpc_free(OPTEE_RPC_SHM_TYPE_KERNEL, 2042 mobj_get_cookie(mobj), mobj); 2043 } 2044 2045 void thread_rpc_free_payload(struct mobj *mobj) 2046 { 2047 if (mobj) 2048 thread_rpc_free(OPTEE_RPC_SHM_TYPE_APPL, mobj_get_cookie(mobj), 2049 mobj); 2050 } 2051 2052 struct mobj *thread_rpc_alloc_global_payload(size_t size) 2053 { 2054 return thread_rpc_alloc(size, 8, OPTEE_RPC_SHM_TYPE_GLOBAL); 2055 } 2056 2057 void thread_rpc_free_global_payload(struct mobj *mobj) 2058 { 2059 if (mobj) 2060 thread_rpc_free(OPTEE_RPC_SHM_TYPE_GLOBAL, 2061 mobj_get_cookie(mobj), mobj); 2062 } 2063 2064 void thread_spmc_register_secondary_ep(vaddr_t ep) 2065 { 2066 unsigned long ret = 0; 2067 2068 /* Let the SPM know the entry point for secondary CPUs */ 2069 ret = thread_smc(FFA_SECONDARY_EP_REGISTER_64, ep, 0, 0); 2070 2071 if (ret != FFA_SUCCESS_32 && ret != FFA_SUCCESS_64) 2072 EMSG("FFA_SECONDARY_EP_REGISTER_64 ret %#lx", ret); 2073 } 2074 2075 static uint16_t ffa_id_get(void) 2076 { 2077 /* 2078 * Ask the SPM component running at a higher EL to return our FF-A ID. 2079 * This can either be the SPMC ID (if the SPMC is enabled in OP-TEE) or 2080 * the partition ID (if not). 2081 */ 2082 struct thread_smc_args args = { 2083 .a0 = FFA_ID_GET, 2084 }; 2085 2086 thread_smccc(&args); 2087 if (!is_ffa_success(args.a0)) { 2088 if (args.a0 == FFA_ERROR) 2089 EMSG("Get id failed with error %ld", args.a2); 2090 else 2091 EMSG("Get id failed"); 2092 panic(); 2093 } 2094 2095 return args.a2; 2096 } 2097 2098 static uint16_t ffa_spm_id_get(void) 2099 { 2100 /* 2101 * Ask the SPM component running at a higher EL to return its ID. 2102 * If OP-TEE implements the S-EL1 SPMC, this will get the SPMD ID. 2103 * If not, the ID of the SPMC will be returned. 2104 */ 2105 struct thread_smc_args args = { 2106 .a0 = FFA_SPM_ID_GET, 2107 }; 2108 2109 thread_smccc(&args); 2110 if (!is_ffa_success(args.a0)) { 2111 if (args.a0 == FFA_ERROR) 2112 EMSG("Get spm id failed with error %ld", args.a2); 2113 else 2114 EMSG("Get spm id failed"); 2115 panic(); 2116 } 2117 2118 return args.a2; 2119 } 2120 2121 #if defined(CFG_CORE_SEL1_SPMC) 2122 static TEE_Result spmc_init(void) 2123 { 2124 spmd_id = ffa_spm_id_get(); 2125 DMSG("SPMD ID %#"PRIx16, spmd_id); 2126 2127 spmc_id = ffa_id_get(); 2128 DMSG("SPMC ID %#"PRIx16, spmc_id); 2129 2130 optee_endpoint_id = FFA_SWD_ID_MIN; 2131 while (optee_endpoint_id == spmd_id || optee_endpoint_id == spmc_id) 2132 optee_endpoint_id++; 2133 2134 DMSG("OP-TEE endpoint ID %#"PRIx16, optee_endpoint_id); 2135 2136 /* 2137 * If SPMD think we are version 1.0 it will report version 1.0 to 2138 * normal world regardless of what version we query the SPM with. 2139 * However, if SPMD think we are version 1.1 it will forward 2140 * queries from normal world to let us negotiate version. So by 2141 * setting version 1.0 here we should be compatible. 2142 * 2143 * Note that disagreement on negotiated version means that we'll 2144 * have communication problems with normal world. 2145 */ 2146 my_rxtx.ffa_vers = FFA_VERSION_1_0; 2147 2148 return TEE_SUCCESS; 2149 } 2150 #else /* !defined(CFG_CORE_SEL1_SPMC) */ 2151 static void spmc_rxtx_map(struct ffa_rxtx *rxtx) 2152 { 2153 struct thread_smc_args args = { 2154 #ifdef ARM64 2155 .a0 = FFA_RXTX_MAP_64, 2156 #else 2157 .a0 = FFA_RXTX_MAP_32, 2158 #endif 2159 .a1 = virt_to_phys(rxtx->tx), 2160 .a2 = virt_to_phys(rxtx->rx), 2161 .a3 = 1, 2162 }; 2163 2164 thread_smccc(&args); 2165 if (!is_ffa_success(args.a0)) { 2166 if (args.a0 == FFA_ERROR) 2167 EMSG("rxtx map failed with error %ld", args.a2); 2168 else 2169 EMSG("rxtx map failed"); 2170 panic(); 2171 } 2172 } 2173 2174 static uint32_t get_ffa_version(uint32_t my_version) 2175 { 2176 struct thread_smc_args args = { 2177 .a0 = FFA_VERSION, 2178 .a1 = my_version, 2179 }; 2180 2181 thread_smccc(&args); 2182 if (args.a0 & BIT(31)) { 2183 EMSG("FF-A version failed with error %ld", args.a0); 2184 panic(); 2185 } 2186 2187 return args.a0; 2188 } 2189 2190 static void *spmc_retrieve_req(uint64_t cookie, 2191 struct ffa_mem_transaction_x *trans) 2192 { 2193 struct ffa_mem_access *acc_descr_array = NULL; 2194 struct ffa_mem_access_perm *perm_descr = NULL; 2195 struct thread_smc_args args = { 2196 .a0 = FFA_MEM_RETRIEVE_REQ_32, 2197 .a3 = 0, /* Address, Using TX -> MBZ */ 2198 .a4 = 0, /* Using TX -> MBZ */ 2199 }; 2200 size_t size = 0; 2201 int rc = 0; 2202 2203 if (my_rxtx.ffa_vers == FFA_VERSION_1_0) { 2204 struct ffa_mem_transaction_1_0 *trans_descr = my_rxtx.tx; 2205 2206 size = sizeof(*trans_descr) + 1 * sizeof(struct ffa_mem_access); 2207 memset(trans_descr, 0, size); 2208 trans_descr->sender_id = thread_get_tsd()->rpc_target_info; 2209 trans_descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR; 2210 trans_descr->global_handle = cookie; 2211 trans_descr->flags = FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE | 2212 FFA_MEMORY_REGION_FLAG_ANY_ALIGNMENT; 2213 trans_descr->mem_access_count = 1; 2214 acc_descr_array = trans_descr->mem_access_array; 2215 } else { 2216 struct ffa_mem_transaction_1_1 *trans_descr = my_rxtx.tx; 2217 2218 size = sizeof(*trans_descr) + 1 * sizeof(struct ffa_mem_access); 2219 memset(trans_descr, 0, size); 2220 trans_descr->sender_id = thread_get_tsd()->rpc_target_info; 2221 trans_descr->mem_reg_attr = FFA_NORMAL_MEM_REG_ATTR; 2222 trans_descr->global_handle = cookie; 2223 trans_descr->flags = FFA_MEMORY_REGION_TRANSACTION_TYPE_SHARE | 2224 FFA_MEMORY_REGION_FLAG_ANY_ALIGNMENT; 2225 trans_descr->mem_access_count = 1; 2226 trans_descr->mem_access_offs = sizeof(*trans_descr); 2227 trans_descr->mem_access_size = sizeof(struct ffa_mem_access); 2228 acc_descr_array = (void *)((vaddr_t)my_rxtx.tx + 2229 sizeof(*trans_descr)); 2230 } 2231 acc_descr_array->region_offs = 0; 2232 acc_descr_array->reserved = 0; 2233 perm_descr = &acc_descr_array->access_perm; 2234 perm_descr->endpoint_id = optee_endpoint_id; 2235 perm_descr->perm = FFA_MEM_ACC_RW; 2236 perm_descr->flags = 0; 2237 2238 args.a1 = size; /* Total Length */ 2239 args.a2 = size; /* Frag Length == Total length */ 2240 thread_smccc(&args); 2241 if (args.a0 != FFA_MEM_RETRIEVE_RESP) { 2242 if (args.a0 == FFA_ERROR) 2243 EMSG("Failed to fetch cookie %#"PRIx64" error code %d", 2244 cookie, (int)args.a2); 2245 else 2246 EMSG("Failed to fetch cookie %#"PRIx64" a0 %#"PRIx64, 2247 cookie, args.a0); 2248 return NULL; 2249 } 2250 rc = spmc_read_mem_transaction(my_rxtx.ffa_vers, my_rxtx.rx, 2251 my_rxtx.size, trans); 2252 if (rc) { 2253 EMSG("Memory transaction failure for cookie %#"PRIx64" rc %d", 2254 cookie, rc); 2255 return NULL; 2256 } 2257 2258 return my_rxtx.rx; 2259 } 2260 2261 void thread_spmc_relinquish(uint64_t cookie) 2262 { 2263 struct ffa_mem_relinquish *relinquish_desc = my_rxtx.tx; 2264 struct thread_smc_args args = { 2265 .a0 = FFA_MEM_RELINQUISH, 2266 }; 2267 2268 memset(relinquish_desc, 0, sizeof(*relinquish_desc)); 2269 relinquish_desc->handle = cookie; 2270 relinquish_desc->flags = 0; 2271 relinquish_desc->endpoint_count = 1; 2272 relinquish_desc->endpoint_id_array[0] = optee_endpoint_id; 2273 thread_smccc(&args); 2274 if (!is_ffa_success(args.a0)) 2275 EMSG("Failed to relinquish cookie %#"PRIx64, cookie); 2276 } 2277 2278 static int set_pages(struct ffa_address_range *regions, 2279 unsigned int num_regions, unsigned int num_pages, 2280 struct mobj_ffa *mf) 2281 { 2282 unsigned int n = 0; 2283 unsigned int idx = 0; 2284 2285 for (n = 0; n < num_regions; n++) { 2286 unsigned int page_count = READ_ONCE(regions[n].page_count); 2287 uint64_t addr = READ_ONCE(regions[n].address); 2288 2289 if (mobj_ffa_add_pages_at(mf, &idx, addr, page_count)) 2290 return FFA_INVALID_PARAMETERS; 2291 } 2292 2293 if (idx != num_pages) 2294 return FFA_INVALID_PARAMETERS; 2295 2296 return 0; 2297 } 2298 2299 struct mobj_ffa *thread_spmc_populate_mobj_from_rx(uint64_t cookie) 2300 { 2301 struct mobj_ffa *ret = NULL; 2302 struct ffa_mem_transaction_x retrieve_desc = { }; 2303 struct ffa_mem_access *descr_array = NULL; 2304 struct ffa_mem_region *descr = NULL; 2305 struct mobj_ffa *mf = NULL; 2306 unsigned int num_pages = 0; 2307 unsigned int offs = 0; 2308 void *buf = NULL; 2309 struct thread_smc_args ffa_rx_release_args = { 2310 .a0 = FFA_RX_RELEASE 2311 }; 2312 2313 /* 2314 * OP-TEE is only supporting a single mem_region while the 2315 * specification allows for more than one. 2316 */ 2317 buf = spmc_retrieve_req(cookie, &retrieve_desc); 2318 if (!buf) { 2319 EMSG("Failed to retrieve cookie from rx buffer %#"PRIx64, 2320 cookie); 2321 return NULL; 2322 } 2323 2324 descr_array = (void *)((vaddr_t)buf + retrieve_desc.mem_access_offs); 2325 offs = READ_ONCE(descr_array->region_offs); 2326 descr = (struct ffa_mem_region *)((vaddr_t)buf + offs); 2327 2328 num_pages = READ_ONCE(descr->total_page_count); 2329 mf = mobj_ffa_spmc_new(cookie, num_pages); 2330 if (!mf) 2331 goto out; 2332 2333 if (set_pages(descr->address_range_array, 2334 READ_ONCE(descr->address_range_count), num_pages, mf)) { 2335 mobj_ffa_spmc_delete(mf); 2336 goto out; 2337 } 2338 2339 ret = mf; 2340 2341 out: 2342 /* Release RX buffer after the mem retrieve request. */ 2343 thread_smccc(&ffa_rx_release_args); 2344 2345 return ret; 2346 } 2347 2348 static TEE_Result spmc_init(void) 2349 { 2350 unsigned int major = 0; 2351 unsigned int minor __maybe_unused = 0; 2352 uint32_t my_vers = 0; 2353 uint32_t vers = 0; 2354 2355 my_vers = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR); 2356 vers = get_ffa_version(my_vers); 2357 major = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK; 2358 minor = (vers >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK; 2359 DMSG("SPMC reported version %u.%u", major, minor); 2360 if (major != FFA_VERSION_MAJOR) { 2361 EMSG("Incompatible major version %u, expected %u", 2362 major, FFA_VERSION_MAJOR); 2363 panic(); 2364 } 2365 if (vers < my_vers) 2366 my_vers = vers; 2367 DMSG("Using version %u.%u", 2368 (my_vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK, 2369 (my_vers >> FFA_VERSION_MINOR_SHIFT) & FFA_VERSION_MINOR_MASK); 2370 my_rxtx.ffa_vers = my_vers; 2371 2372 spmc_rxtx_map(&my_rxtx); 2373 2374 spmc_id = ffa_spm_id_get(); 2375 DMSG("SPMC ID %#"PRIx16, spmc_id); 2376 2377 optee_endpoint_id = ffa_id_get(); 2378 DMSG("OP-TEE endpoint ID %#"PRIx16, optee_endpoint_id); 2379 2380 if (!ffa_features(FFA_NOTIFICATION_SET)) { 2381 spmc_notif_is_ready = true; 2382 DMSG("Asynchronous notifications are ready"); 2383 } 2384 2385 return TEE_SUCCESS; 2386 } 2387 #endif /* !defined(CFG_CORE_SEL1_SPMC) */ 2388 2389 /* 2390 * boot_final() is always done before exiting at end of boot 2391 * initialization. In case of virtualization the init-calls are done only 2392 * once a OP-TEE partition has been created. So with virtualization we have 2393 * to initialize via boot_final() to make sure we have a value assigned 2394 * before it's used the first time. 2395 */ 2396 #ifdef CFG_NS_VIRTUALIZATION 2397 boot_final(spmc_init); 2398 #else 2399 service_init(spmc_init); 2400 #endif 2401