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