1 /* 2 * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <errno.h> 9 10 #include <arch_helpers.h> 11 #include <bl31/bl31.h> 12 #include <bl31/ehf.h> 13 #include <common/debug.h> 14 #include <common/fdt_wrappers.h> 15 #include <common/runtime_svc.h> 16 #include <lib/el3_runtime/context_mgmt.h> 17 #include <lib/smccc.h> 18 #include <lib/utils.h> 19 #include <lib/xlat_tables/xlat_tables_v2.h> 20 #include <libfdt.h> 21 #include <plat/common/platform.h> 22 #include <services/el3_spmc_logical_sp.h> 23 #include <services/ffa_svc.h> 24 #include <services/spmc_svc.h> 25 #include <services/spmd_svc.h> 26 #include "spmc.h" 27 28 #include <platform_def.h> 29 30 /* 31 * Allocate a secure partition descriptor to describe each SP in the system that 32 * does not reside at EL3. 33 */ 34 static struct secure_partition_desc sp_desc[SECURE_PARTITION_COUNT]; 35 36 /* 37 * Allocate an NS endpoint descriptor to describe each VM and the Hypervisor in 38 * the system that interacts with a SP. It is used to track the Hypervisor 39 * buffer pair, version and ID for now. It could be extended to track VM 40 * properties when the SPMC supports indirect messaging. 41 */ 42 static struct ns_endpoint_desc ns_ep_desc[NS_PARTITION_COUNT]; 43 44 /* 45 * Helper function to obtain the array storing the EL3 46 * Logical Partition descriptors. 47 */ 48 struct el3_lp_desc *get_el3_lp_array(void) 49 { 50 return (struct el3_lp_desc *) EL3_LP_DESCS_START; 51 } 52 53 /* 54 * Helper function to obtain the descriptor of the last SP to whom control was 55 * handed to on this physical cpu. Currently, we assume there is only one SP. 56 * TODO: Expand to track multiple partitions when required. 57 */ 58 struct secure_partition_desc *spmc_get_current_sp_ctx(void) 59 { 60 return &(sp_desc[ACTIVE_SP_DESC_INDEX]); 61 } 62 63 /* 64 * Helper function to obtain the execution context of an SP on the 65 * current physical cpu. 66 */ 67 struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp) 68 { 69 return &(sp->ec[get_ec_index(sp)]); 70 } 71 72 /* Helper function to get pointer to SP context from its ID. */ 73 struct secure_partition_desc *spmc_get_sp_ctx(uint16_t id) 74 { 75 /* Check for SWd Partitions. */ 76 for (unsigned int i = 0U; i < SECURE_PARTITION_COUNT; i++) { 77 if (sp_desc[i].sp_id == id) { 78 return &(sp_desc[i]); 79 } 80 } 81 return NULL; 82 } 83 84 /****************************************************************************** 85 * This function returns to the place where spmc_sp_synchronous_entry() was 86 * called originally. 87 ******************************************************************************/ 88 __dead2 void spmc_sp_synchronous_exit(struct sp_exec_ctx *ec, uint64_t rc) 89 { 90 /* 91 * The SPM must have initiated the original request through a 92 * synchronous entry into the secure partition. Jump back to the 93 * original C runtime context with the value of rc in x0; 94 */ 95 spm_secure_partition_exit(ec->c_rt_ctx, rc); 96 97 panic(); 98 } 99 100 /******************************************************************************* 101 * Return FFA_ERROR with specified error code. 102 ******************************************************************************/ 103 uint64_t spmc_ffa_error_return(void *handle, int error_code) 104 { 105 SMC_RET8(handle, FFA_ERROR, 106 FFA_TARGET_INFO_MBZ, error_code, 107 FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 108 FFA_PARAM_MBZ, FFA_PARAM_MBZ); 109 } 110 111 /****************************************************************************** 112 * Helper function to validate a secure partition ID to ensure it does not 113 * conflict with any other FF-A component and follows the convention to 114 * indicate it resides within the secure world. 115 ******************************************************************************/ 116 bool is_ffa_secure_id_valid(uint16_t partition_id) 117 { 118 struct el3_lp_desc *el3_lp_descs = get_el3_lp_array(); 119 120 /* Ensure the ID is not the invalid partition ID. */ 121 if (partition_id == INV_SP_ID) { 122 return false; 123 } 124 125 /* Ensure the ID is not the SPMD ID. */ 126 if (partition_id == SPMD_DIRECT_MSG_ENDPOINT_ID) { 127 return false; 128 } 129 130 /* 131 * Ensure the ID follows the convention to indicate it resides 132 * in the secure world. 133 */ 134 if (!ffa_is_secure_world_id(partition_id)) { 135 return false; 136 } 137 138 /* Ensure we don't conflict with the SPMC partition ID. */ 139 if (partition_id == FFA_SPMC_ID) { 140 return false; 141 } 142 143 /* Ensure we do not already have an SP context with this ID. */ 144 if (spmc_get_sp_ctx(partition_id)) { 145 return false; 146 } 147 148 /* Ensure we don't clash with any Logical SP's. */ 149 for (unsigned int i = 0U; i < EL3_LP_DESCS_COUNT; i++) { 150 if (el3_lp_descs[i].sp_id == partition_id) { 151 return false; 152 } 153 } 154 155 return true; 156 } 157 158 /******************************************************************************* 159 * This function either forwards the request to the other world or returns 160 * with an ERET depending on the source of the call. 161 * We can assume that the destination is for an entity at a lower exception 162 * level as any messages destined for a logical SP resident in EL3 will have 163 * already been taken care of by the SPMC before entering this function. 164 ******************************************************************************/ 165 static uint64_t spmc_smc_return(uint32_t smc_fid, 166 bool secure_origin, 167 uint64_t x1, 168 uint64_t x2, 169 uint64_t x3, 170 uint64_t x4, 171 void *handle, 172 void *cookie, 173 uint64_t flags, 174 uint16_t dst_id) 175 { 176 /* If the destination is in the normal world always go via the SPMD. */ 177 if (ffa_is_normal_world_id(dst_id)) { 178 return spmd_smc_handler(smc_fid, x1, x2, x3, x4, 179 cookie, handle, flags); 180 } 181 /* 182 * If the caller is secure and we want to return to the secure world, 183 * ERET directly. 184 */ 185 else if (secure_origin && ffa_is_secure_world_id(dst_id)) { 186 SMC_RET5(handle, smc_fid, x1, x2, x3, x4); 187 } 188 /* If we originated in the normal world then switch contexts. */ 189 else if (!secure_origin && ffa_is_secure_world_id(dst_id)) { 190 return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, 191 x3, x4, handle); 192 } else { 193 /* Unknown State. */ 194 panic(); 195 } 196 197 /* Shouldn't be Reached. */ 198 return 0; 199 } 200 201 /******************************************************************************* 202 * FF-A ABI Handlers. 203 ******************************************************************************/ 204 205 /******************************************************************************* 206 * Helper function to validate arg2 as part of a direct message. 207 ******************************************************************************/ 208 static inline bool direct_msg_validate_arg2(uint64_t x2) 209 { 210 /* 211 * We currently only support partition messages, therefore ensure x2 is 212 * not set. 213 */ 214 if (x2 != (uint64_t) 0) { 215 VERBOSE("Arg2 MBZ for partition messages (0x%lx).\n", x2); 216 return false; 217 } 218 return true; 219 } 220 221 /******************************************************************************* 222 * Handle direct request messages and route to the appropriate destination. 223 ******************************************************************************/ 224 static uint64_t direct_req_smc_handler(uint32_t smc_fid, 225 bool secure_origin, 226 uint64_t x1, 227 uint64_t x2, 228 uint64_t x3, 229 uint64_t x4, 230 void *cookie, 231 void *handle, 232 uint64_t flags) 233 { 234 uint16_t dst_id = ffa_endpoint_destination(x1); 235 struct el3_lp_desc *el3_lp_descs; 236 struct secure_partition_desc *sp; 237 unsigned int idx; 238 239 /* Check if arg2 has been populated correctly based on message type. */ 240 if (!direct_msg_validate_arg2(x2)) { 241 return spmc_ffa_error_return(handle, 242 FFA_ERROR_INVALID_PARAMETER); 243 } 244 245 el3_lp_descs = get_el3_lp_array(); 246 247 /* Check if the request is destined for a Logical Partition. */ 248 for (unsigned int i = 0U; i < MAX_EL3_LP_DESCS_COUNT; i++) { 249 if (el3_lp_descs[i].sp_id == dst_id) { 250 return el3_lp_descs[i].direct_req( 251 smc_fid, secure_origin, x1, x2, x3, x4, 252 cookie, handle, flags); 253 } 254 } 255 256 /* 257 * If the request was not targeted to a LSP and from the secure world 258 * then it is invalid since a SP cannot call into the Normal world and 259 * there is no other SP to call into. If there are other SPs in future 260 * then the partition runtime model would need to be validated as well. 261 */ 262 if (secure_origin) { 263 VERBOSE("Direct request not supported to the Normal World.\n"); 264 return spmc_ffa_error_return(handle, 265 FFA_ERROR_INVALID_PARAMETER); 266 } 267 268 /* Check if the SP ID is valid. */ 269 sp = spmc_get_sp_ctx(dst_id); 270 if (sp == NULL) { 271 VERBOSE("Direct request to unknown partition ID (0x%x).\n", 272 dst_id); 273 return spmc_ffa_error_return(handle, 274 FFA_ERROR_INVALID_PARAMETER); 275 } 276 277 /* 278 * Check that the target execution context is in a waiting state before 279 * forwarding the direct request to it. 280 */ 281 idx = get_ec_index(sp); 282 if (sp->ec[idx].rt_state != RT_STATE_WAITING) { 283 VERBOSE("SP context on core%u is not waiting (%u).\n", 284 idx, sp->ec[idx].rt_model); 285 return spmc_ffa_error_return(handle, FFA_ERROR_BUSY); 286 } 287 288 /* 289 * Everything checks out so forward the request to the SP after updating 290 * its state and runtime model. 291 */ 292 sp->ec[idx].rt_state = RT_STATE_RUNNING; 293 sp->ec[idx].rt_model = RT_MODEL_DIR_REQ; 294 return spmc_smc_return(smc_fid, secure_origin, x1, x2, x3, x4, 295 handle, cookie, flags, dst_id); 296 } 297 298 /******************************************************************************* 299 * Handle direct response messages and route to the appropriate destination. 300 ******************************************************************************/ 301 static uint64_t direct_resp_smc_handler(uint32_t smc_fid, 302 bool secure_origin, 303 uint64_t x1, 304 uint64_t x2, 305 uint64_t x3, 306 uint64_t x4, 307 void *cookie, 308 void *handle, 309 uint64_t flags) 310 { 311 uint16_t dst_id = ffa_endpoint_destination(x1); 312 struct secure_partition_desc *sp; 313 unsigned int idx; 314 315 /* Check if arg2 has been populated correctly based on message type. */ 316 if (!direct_msg_validate_arg2(x2)) { 317 return spmc_ffa_error_return(handle, 318 FFA_ERROR_INVALID_PARAMETER); 319 } 320 321 /* Check that the response did not originate from the Normal world. */ 322 if (!secure_origin) { 323 VERBOSE("Direct Response not supported from Normal World.\n"); 324 return spmc_ffa_error_return(handle, 325 FFA_ERROR_INVALID_PARAMETER); 326 } 327 328 /* 329 * Check that the response is either targeted to the Normal world or the 330 * SPMC e.g. a PM response. 331 */ 332 if ((dst_id != FFA_SPMC_ID) && ffa_is_secure_world_id(dst_id)) { 333 VERBOSE("Direct response to invalid partition ID (0x%x).\n", 334 dst_id); 335 return spmc_ffa_error_return(handle, 336 FFA_ERROR_INVALID_PARAMETER); 337 } 338 339 /* Obtain the SP descriptor and update its runtime state. */ 340 sp = spmc_get_sp_ctx(ffa_endpoint_source(x1)); 341 if (sp == NULL) { 342 VERBOSE("Direct response to unknown partition ID (0x%x).\n", 343 dst_id); 344 return spmc_ffa_error_return(handle, 345 FFA_ERROR_INVALID_PARAMETER); 346 } 347 348 /* Sanity check state is being tracked correctly in the SPMC. */ 349 idx = get_ec_index(sp); 350 assert(sp->ec[idx].rt_state == RT_STATE_RUNNING); 351 352 /* Ensure SP execution context was in the right runtime model. */ 353 if (sp->ec[idx].rt_model != RT_MODEL_DIR_REQ) { 354 VERBOSE("SP context on core%u not handling direct req (%u).\n", 355 idx, sp->ec[idx].rt_model); 356 return spmc_ffa_error_return(handle, FFA_ERROR_DENIED); 357 } 358 359 /* Update the state of the SP execution context. */ 360 sp->ec[idx].rt_state = RT_STATE_WAITING; 361 362 /* 363 * If the receiver is not the SPMC then forward the response to the 364 * Normal world. 365 */ 366 if (dst_id == FFA_SPMC_ID) { 367 spmc_sp_synchronous_exit(&sp->ec[idx], x4); 368 /* Should not get here. */ 369 panic(); 370 } 371 372 return spmc_smc_return(smc_fid, secure_origin, x1, x2, x3, x4, 373 handle, cookie, flags, dst_id); 374 } 375 376 /******************************************************************************* 377 * This function handles the FFA_MSG_WAIT SMC to allow an SP to relinquish its 378 * cycles. 379 ******************************************************************************/ 380 static uint64_t msg_wait_handler(uint32_t smc_fid, 381 bool secure_origin, 382 uint64_t x1, 383 uint64_t x2, 384 uint64_t x3, 385 uint64_t x4, 386 void *cookie, 387 void *handle, 388 uint64_t flags) 389 { 390 struct secure_partition_desc *sp; 391 unsigned int idx; 392 393 /* 394 * Check that the response did not originate from the Normal world as 395 * only the secure world can call this ABI. 396 */ 397 if (!secure_origin) { 398 VERBOSE("Normal world cannot call FFA_MSG_WAIT.\n"); 399 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 400 } 401 402 /* Get the descriptor of the SP that invoked FFA_MSG_WAIT. */ 403 sp = spmc_get_current_sp_ctx(); 404 if (sp == NULL) { 405 return spmc_ffa_error_return(handle, 406 FFA_ERROR_INVALID_PARAMETER); 407 } 408 409 /* 410 * Get the execution context of the SP that invoked FFA_MSG_WAIT. 411 */ 412 idx = get_ec_index(sp); 413 414 /* Ensure SP execution context was in the right runtime model. */ 415 if (sp->ec[idx].rt_model == RT_MODEL_DIR_REQ) { 416 return spmc_ffa_error_return(handle, FFA_ERROR_DENIED); 417 } 418 419 /* Sanity check the state is being tracked correctly in the SPMC. */ 420 assert(sp->ec[idx].rt_state == RT_STATE_RUNNING); 421 422 /* 423 * Perform a synchronous exit if the partition was initialising. The 424 * state is updated after the exit. 425 */ 426 if (sp->ec[idx].rt_model == RT_MODEL_INIT) { 427 spmc_sp_synchronous_exit(&sp->ec[idx], x4); 428 /* Should not get here */ 429 panic(); 430 } 431 432 /* Update the state of the SP execution context. */ 433 sp->ec[idx].rt_state = RT_STATE_WAITING; 434 435 /* Resume normal world if a secure interrupt was handled. */ 436 if (sp->ec[idx].rt_model == RT_MODEL_INTR) { 437 /* FFA_MSG_WAIT can only be called from the secure world. */ 438 unsigned int secure_state_in = SECURE; 439 unsigned int secure_state_out = NON_SECURE; 440 441 cm_el1_sysregs_context_save(secure_state_in); 442 cm_el1_sysregs_context_restore(secure_state_out); 443 cm_set_next_eret_context(secure_state_out); 444 SMC_RET0(cm_get_context(secure_state_out)); 445 } 446 447 /* Forward the response to the Normal world. */ 448 return spmc_smc_return(smc_fid, secure_origin, x1, x2, x3, x4, 449 handle, cookie, flags, FFA_NWD_ID); 450 } 451 452 static uint64_t ffa_error_handler(uint32_t smc_fid, 453 bool secure_origin, 454 uint64_t x1, 455 uint64_t x2, 456 uint64_t x3, 457 uint64_t x4, 458 void *cookie, 459 void *handle, 460 uint64_t flags) 461 { 462 struct secure_partition_desc *sp; 463 unsigned int idx; 464 465 /* Check that the response did not originate from the Normal world. */ 466 if (!secure_origin) { 467 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 468 } 469 470 /* Get the descriptor of the SP that invoked FFA_ERROR. */ 471 sp = spmc_get_current_sp_ctx(); 472 if (sp == NULL) { 473 return spmc_ffa_error_return(handle, 474 FFA_ERROR_INVALID_PARAMETER); 475 } 476 477 /* Get the execution context of the SP that invoked FFA_ERROR. */ 478 idx = get_ec_index(sp); 479 480 /* 481 * We only expect FFA_ERROR to be received during SP initialisation 482 * otherwise this is an invalid call. 483 */ 484 if (sp->ec[idx].rt_model == RT_MODEL_INIT) { 485 ERROR("SP 0x%x failed to initialize.\n", sp->sp_id); 486 spmc_sp_synchronous_exit(&sp->ec[idx], x2); 487 /* Should not get here. */ 488 panic(); 489 } 490 491 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 492 } 493 494 /******************************************************************************* 495 * This function will parse the Secure Partition Manifest. From manifest, it 496 * will fetch details for preparing Secure partition image context and secure 497 * partition image boot arguments if any. 498 ******************************************************************************/ 499 static int sp_manifest_parse(void *sp_manifest, int offset, 500 struct secure_partition_desc *sp, 501 entry_point_info_t *ep_info) 502 { 503 int32_t ret, node; 504 uint32_t config_32; 505 506 /* 507 * Look for the mandatory fields that are expected to be present in 508 * the SP manifests. 509 */ 510 node = fdt_path_offset(sp_manifest, "/"); 511 if (node < 0) { 512 ERROR("Did not find root node.\n"); 513 return node; 514 } 515 516 ret = fdt_read_uint32_array(sp_manifest, node, "uuid", 517 ARRAY_SIZE(sp->uuid), sp->uuid); 518 if (ret != 0) { 519 ERROR("Missing Secure Partition UUID.\n"); 520 return ret; 521 } 522 523 ret = fdt_read_uint32(sp_manifest, node, "exception-level", &config_32); 524 if (ret != 0) { 525 ERROR("Missing SP Exception Level information.\n"); 526 return ret; 527 } 528 529 sp->runtime_el = config_32; 530 531 ret = fdt_read_uint32(sp_manifest, node, "ffa-version", &config_32); 532 if (ret != 0) { 533 ERROR("Missing Secure Partition FF-A Version.\n"); 534 return ret; 535 } 536 537 sp->ffa_version = config_32; 538 539 ret = fdt_read_uint32(sp_manifest, node, "execution-state", &config_32); 540 if (ret != 0) { 541 ERROR("Missing Secure Partition Execution State.\n"); 542 return ret; 543 } 544 545 sp->execution_state = config_32; 546 547 /* 548 * Look for the optional fields that are expected to be present in 549 * an SP manifest. 550 */ 551 ret = fdt_read_uint32(sp_manifest, node, "id", &config_32); 552 if (ret != 0) { 553 WARN("Missing Secure Partition ID.\n"); 554 } else { 555 if (!is_ffa_secure_id_valid(config_32)) { 556 ERROR("Invalid Secure Partition ID (0x%x).\n", 557 config_32); 558 return -EINVAL; 559 } 560 sp->sp_id = config_32; 561 } 562 563 return 0; 564 } 565 566 /******************************************************************************* 567 * This function gets the Secure Partition Manifest base and maps the manifest 568 * region. 569 * Currently only one Secure Partition manifest is considered which is used to 570 * prepare the context for the single Secure Partition. 571 ******************************************************************************/ 572 static int find_and_prepare_sp_context(void) 573 { 574 void *sp_manifest; 575 uintptr_t manifest_base; 576 uintptr_t manifest_base_align; 577 entry_point_info_t *next_image_ep_info; 578 int32_t ret; 579 struct secure_partition_desc *sp; 580 581 next_image_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 582 if (next_image_ep_info == NULL) { 583 WARN("No Secure Partition image provided by BL2.\n"); 584 return -ENOENT; 585 } 586 587 sp_manifest = (void *)next_image_ep_info->args.arg0; 588 if (sp_manifest == NULL) { 589 WARN("Secure Partition manifest absent.\n"); 590 return -ENOENT; 591 } 592 593 manifest_base = (uintptr_t)sp_manifest; 594 manifest_base_align = page_align(manifest_base, DOWN); 595 596 /* 597 * Map the secure partition manifest region in the EL3 translation 598 * regime. 599 * Map an area equal to (2 * PAGE_SIZE) for now. During manifest base 600 * alignment the region of 1 PAGE_SIZE from manifest align base may 601 * not completely accommodate the secure partition manifest region. 602 */ 603 ret = mmap_add_dynamic_region((unsigned long long)manifest_base_align, 604 manifest_base_align, 605 PAGE_SIZE * 2, 606 MT_RO_DATA); 607 if (ret != 0) { 608 ERROR("Error while mapping SP manifest (%d).\n", ret); 609 return ret; 610 } 611 612 ret = fdt_node_offset_by_compatible(sp_manifest, -1, 613 "arm,ffa-manifest-1.0"); 614 if (ret < 0) { 615 ERROR("Error happened in SP manifest reading.\n"); 616 return -EINVAL; 617 } 618 619 /* 620 * Store the size of the manifest so that it can be used later to pass 621 * the manifest as boot information later. 622 */ 623 next_image_ep_info->args.arg1 = fdt_totalsize(sp_manifest); 624 INFO("Manifest size = %lu bytes.\n", next_image_ep_info->args.arg1); 625 626 /* 627 * Select an SP descriptor for initialising the partition's execution 628 * context on the primary CPU. 629 */ 630 sp = spmc_get_current_sp_ctx(); 631 632 /* Initialize entry point information for the SP */ 633 SET_PARAM_HEAD(next_image_ep_info, PARAM_EP, VERSION_1, 634 SECURE | EP_ST_ENABLE); 635 636 /* Parse the SP manifest. */ 637 ret = sp_manifest_parse(sp_manifest, ret, sp, next_image_ep_info); 638 if (ret != 0) { 639 ERROR("Error in Secure Partition manifest parsing.\n"); 640 return ret; 641 } 642 643 /* Check that the runtime EL in the manifest was correct. */ 644 if (sp->runtime_el != S_EL1) { 645 ERROR("Unexpected runtime EL: %d\n", sp->runtime_el); 646 return -EINVAL; 647 } 648 649 /* Perform any common initialisation. */ 650 spmc_sp_common_setup(sp, next_image_ep_info); 651 652 /* Perform any initialisation specific to S-EL1 SPs. */ 653 spmc_el1_sp_setup(sp, next_image_ep_info); 654 655 /* Initialize the SP context with the required ep info. */ 656 spmc_sp_common_ep_commit(sp, next_image_ep_info); 657 658 return 0; 659 } 660 661 /******************************************************************************* 662 * This function takes an SP context pointer and performs a synchronous entry 663 * into it. 664 ******************************************************************************/ 665 static int32_t logical_sp_init(void) 666 { 667 int32_t rc = 0; 668 struct el3_lp_desc *el3_lp_descs; 669 670 /* Perform initial validation of the Logical Partitions. */ 671 rc = el3_sp_desc_validate(); 672 if (rc != 0) { 673 ERROR("Logical Partition validation failed!\n"); 674 return rc; 675 } 676 677 el3_lp_descs = get_el3_lp_array(); 678 679 INFO("Logical Secure Partition init start.\n"); 680 for (unsigned int i = 0U; i < EL3_LP_DESCS_COUNT; i++) { 681 rc = el3_lp_descs[i].init(); 682 if (rc != 0) { 683 ERROR("Logical SP (0x%x) Failed to Initialize\n", 684 el3_lp_descs[i].sp_id); 685 return rc; 686 } 687 VERBOSE("Logical SP (0x%x) Initialized\n", 688 el3_lp_descs[i].sp_id); 689 } 690 691 INFO("Logical Secure Partition init completed.\n"); 692 693 return rc; 694 } 695 696 uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec) 697 { 698 uint64_t rc; 699 700 assert(ec != NULL); 701 702 /* Assign the context of the SP to this CPU */ 703 cm_set_context(&(ec->cpu_ctx), SECURE); 704 705 /* Restore the context assigned above */ 706 cm_el1_sysregs_context_restore(SECURE); 707 cm_set_next_eret_context(SECURE); 708 709 /* Invalidate TLBs at EL1. */ 710 tlbivmalle1(); 711 dsbish(); 712 713 /* Enter Secure Partition */ 714 rc = spm_secure_partition_enter(&ec->c_rt_ctx); 715 716 /* Save secure state */ 717 cm_el1_sysregs_context_save(SECURE); 718 719 return rc; 720 } 721 722 /******************************************************************************* 723 * SPMC Helper Functions. 724 ******************************************************************************/ 725 static int32_t sp_init(void) 726 { 727 uint64_t rc; 728 struct secure_partition_desc *sp; 729 struct sp_exec_ctx *ec; 730 731 sp = spmc_get_current_sp_ctx(); 732 ec = spmc_get_sp_ec(sp); 733 ec->rt_model = RT_MODEL_INIT; 734 ec->rt_state = RT_STATE_RUNNING; 735 736 INFO("Secure Partition (0x%x) init start.\n", sp->sp_id); 737 738 rc = spmc_sp_synchronous_entry(ec); 739 if (rc != 0) { 740 /* Indicate SP init was not successful. */ 741 ERROR("SP (0x%x) failed to initialize (%lu).\n", 742 sp->sp_id, rc); 743 return 0; 744 } 745 746 ec->rt_state = RT_STATE_WAITING; 747 INFO("Secure Partition initialized.\n"); 748 749 return 1; 750 } 751 752 static void initalize_sp_descs(void) 753 { 754 struct secure_partition_desc *sp; 755 756 for (unsigned int i = 0U; i < SECURE_PARTITION_COUNT; i++) { 757 sp = &sp_desc[i]; 758 sp->sp_id = INV_SP_ID; 759 sp->mailbox.rx_buffer = NULL; 760 sp->mailbox.tx_buffer = NULL; 761 sp->mailbox.state = MAILBOX_STATE_EMPTY; 762 sp->secondary_ep = 0; 763 } 764 } 765 766 static void initalize_ns_ep_descs(void) 767 { 768 struct ns_endpoint_desc *ns_ep; 769 770 for (unsigned int i = 0U; i < NS_PARTITION_COUNT; i++) { 771 ns_ep = &ns_ep_desc[i]; 772 /* 773 * Clashes with the Hypervisor ID but will not be a 774 * problem in practice. 775 */ 776 ns_ep->ns_ep_id = 0; 777 ns_ep->ffa_version = 0; 778 ns_ep->mailbox.rx_buffer = NULL; 779 ns_ep->mailbox.tx_buffer = NULL; 780 ns_ep->mailbox.state = MAILBOX_STATE_EMPTY; 781 } 782 } 783 784 /******************************************************************************* 785 * Initialize SPMC attributes for the SPMD. 786 ******************************************************************************/ 787 void spmc_populate_attrs(spmc_manifest_attribute_t *spmc_attrs) 788 { 789 spmc_attrs->major_version = FFA_VERSION_MAJOR; 790 spmc_attrs->minor_version = FFA_VERSION_MINOR; 791 spmc_attrs->exec_state = MODE_RW_64; 792 spmc_attrs->spmc_id = FFA_SPMC_ID; 793 } 794 795 /******************************************************************************* 796 * Initialize contexts of all Secure Partitions. 797 ******************************************************************************/ 798 int32_t spmc_setup(void) 799 { 800 int32_t ret; 801 802 /* Initialize endpoint descriptors */ 803 initalize_sp_descs(); 804 initalize_ns_ep_descs(); 805 806 /* Setup logical SPs. */ 807 ret = logical_sp_init(); 808 if (ret != 0) { 809 ERROR("Failed to initialize Logical Partitions.\n"); 810 return ret; 811 } 812 813 /* Perform physical SP setup. */ 814 815 /* Disable MMU at EL1 (initialized by BL2) */ 816 disable_mmu_icache_el1(); 817 818 /* Initialize context of the SP */ 819 INFO("Secure Partition context setup start.\n"); 820 821 ret = find_and_prepare_sp_context(); 822 if (ret != 0) { 823 ERROR("Error in SP finding and context preparation.\n"); 824 return ret; 825 } 826 827 /* Register init function for deferred init. */ 828 bl31_register_bl32_init(&sp_init); 829 830 INFO("Secure Partition setup done.\n"); 831 832 return 0; 833 } 834 835 /******************************************************************************* 836 * Secure Partition Manager SMC handler. 837 ******************************************************************************/ 838 uint64_t spmc_smc_handler(uint32_t smc_fid, 839 bool secure_origin, 840 uint64_t x1, 841 uint64_t x2, 842 uint64_t x3, 843 uint64_t x4, 844 void *cookie, 845 void *handle, 846 uint64_t flags) 847 { 848 switch (smc_fid) { 849 850 case FFA_MSG_SEND_DIRECT_REQ_SMC32: 851 case FFA_MSG_SEND_DIRECT_REQ_SMC64: 852 return direct_req_smc_handler(smc_fid, secure_origin, x1, x2, 853 x3, x4, cookie, handle, flags); 854 855 case FFA_MSG_SEND_DIRECT_RESP_SMC32: 856 case FFA_MSG_SEND_DIRECT_RESP_SMC64: 857 return direct_resp_smc_handler(smc_fid, secure_origin, x1, x2, 858 x3, x4, cookie, handle, flags); 859 860 case FFA_MSG_WAIT: 861 return msg_wait_handler(smc_fid, secure_origin, x1, x2, x3, x4, 862 cookie, handle, flags); 863 864 case FFA_ERROR: 865 return ffa_error_handler(smc_fid, secure_origin, x1, x2, x3, x4, 866 cookie, handle, flags); 867 868 default: 869 WARN("Unsupported FF-A call 0x%08x.\n", smc_fid); 870 break; 871 } 872 return spmc_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 873 } 874