1bdd2596dSAchin Gupta /* 2bdd2596dSAchin Gupta * Copyright (c) 2020, ARM Limited and Contributors. All rights reserved. 3bdd2596dSAchin Gupta * 4bdd2596dSAchin Gupta * SPDX-License-Identifier: BSD-3-Clause 5bdd2596dSAchin Gupta */ 6bdd2596dSAchin Gupta 7bdd2596dSAchin Gupta #include <assert.h> 8bdd2596dSAchin Gupta #include <errno.h> 9bdd2596dSAchin Gupta #include <string.h> 10bdd2596dSAchin Gupta 11bdd2596dSAchin Gupta #include <arch_helpers.h> 1252696946SOlivier Deprez #include <arch/aarch64/arch_features.h> 13bdd2596dSAchin Gupta #include <bl31/bl31.h> 14bdd2596dSAchin Gupta #include <common/debug.h> 15bdd2596dSAchin Gupta #include <common/runtime_svc.h> 16bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h> 17bdd2596dSAchin Gupta #include <lib/smccc.h> 18bdd2596dSAchin Gupta #include <lib/spinlock.h> 19bdd2596dSAchin Gupta #include <lib/utils.h> 20bdd2596dSAchin Gupta #include <plat/common/common_def.h> 21bdd2596dSAchin Gupta #include <plat/common/platform.h> 22bdd2596dSAchin Gupta #include <platform_def.h> 23662af36dSJ-Alves #include <services/ffa_svc.h> 24bdd2596dSAchin Gupta #include <services/spmd_svc.h> 25bdd2596dSAchin Gupta #include <smccc_helpers.h> 26bdd2596dSAchin Gupta #include "spmd_private.h" 27bdd2596dSAchin Gupta 28bdd2596dSAchin Gupta /******************************************************************************* 29bdd2596dSAchin Gupta * SPM Core context information. 30bdd2596dSAchin Gupta ******************************************************************************/ 3152696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT]; 32bdd2596dSAchin Gupta 33bdd2596dSAchin Gupta /******************************************************************************* 34bdd2596dSAchin Gupta * SPM Core attribute information read from its manifest. 35bdd2596dSAchin Gupta ******************************************************************************/ 3652696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs; 370f14d02fSMax Shvetsov 380f14d02fSMax Shvetsov /******************************************************************************* 390f14d02fSMax Shvetsov * SPM Core entry point information. Discovered on the primary core and reused 400f14d02fSMax Shvetsov * on secondary cores. 410f14d02fSMax Shvetsov ******************************************************************************/ 420f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info; 430f14d02fSMax Shvetsov 440f14d02fSMax Shvetsov /******************************************************************************* 4552696946SOlivier Deprez * SPM Core context on current CPU get helper. 4652696946SOlivier Deprez ******************************************************************************/ 4752696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void) 4852696946SOlivier Deprez { 4952696946SOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 5052696946SOlivier Deprez 5152696946SOlivier Deprez return &spm_core_context[linear_id]; 5252696946SOlivier Deprez } 5352696946SOlivier Deprez 5452696946SOlivier Deprez /******************************************************************************* 55c0267cc9SOlivier Deprez * SPM Core entry point information get helper. 56c0267cc9SOlivier Deprez ******************************************************************************/ 57c0267cc9SOlivier Deprez entry_point_info_t *spmd_spmc_ep_info_get(void) 58c0267cc9SOlivier Deprez { 59c0267cc9SOlivier Deprez return spmc_ep_info; 60c0267cc9SOlivier Deprez } 61c0267cc9SOlivier Deprez 62c0267cc9SOlivier Deprez /******************************************************************************* 630f14d02fSMax Shvetsov * Static function declaration. 640f14d02fSMax Shvetsov ******************************************************************************/ 650f14d02fSMax Shvetsov static int32_t spmd_init(void); 6623d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr); 67662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, 6852696946SOlivier Deprez int error_code); 6952696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 7052696946SOlivier Deprez bool secure_origin, 7152696946SOlivier Deprez uint64_t x1, 7252696946SOlivier Deprez uint64_t x2, 7352696946SOlivier Deprez uint64_t x3, 7452696946SOlivier Deprez uint64_t x4, 7552696946SOlivier Deprez void *handle); 76bdd2596dSAchin Gupta 77bdd2596dSAchin Gupta /******************************************************************************* 7852696946SOlivier Deprez * This function takes an SPMC context pointer and performs a synchronous 7952696946SOlivier Deprez * SPMC entry. 80bdd2596dSAchin Gupta ******************************************************************************/ 81bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx) 82bdd2596dSAchin Gupta { 83bdd2596dSAchin Gupta uint64_t rc; 84bdd2596dSAchin Gupta 85bdd2596dSAchin Gupta assert(spmc_ctx != NULL); 86bdd2596dSAchin Gupta 87bdd2596dSAchin Gupta cm_set_context(&(spmc_ctx->cpu_ctx), SECURE); 88bdd2596dSAchin Gupta 89bdd2596dSAchin Gupta /* Restore the context assigned above */ 90bdd2596dSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 91033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 9228f39f02SMax Shvetsov cm_el2_sysregs_context_restore(SECURE); 93033039f8SMax Shvetsov #endif 94bdd2596dSAchin Gupta cm_set_next_eret_context(SECURE); 95bdd2596dSAchin Gupta 96033039f8SMax Shvetsov /* Enter SPMC */ 97bdd2596dSAchin Gupta rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx); 98bdd2596dSAchin Gupta 99bdd2596dSAchin Gupta /* Save secure state */ 100bdd2596dSAchin Gupta cm_el1_sysregs_context_save(SECURE); 101033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 10228f39f02SMax Shvetsov cm_el2_sysregs_context_save(SECURE); 103033039f8SMax Shvetsov #endif 104bdd2596dSAchin Gupta 105bdd2596dSAchin Gupta return rc; 106bdd2596dSAchin Gupta } 107bdd2596dSAchin Gupta 108bdd2596dSAchin Gupta /******************************************************************************* 10952696946SOlivier Deprez * This function returns to the place where spmd_spm_core_sync_entry() was 110bdd2596dSAchin Gupta * called originally. 111bdd2596dSAchin Gupta ******************************************************************************/ 112bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc) 113bdd2596dSAchin Gupta { 11452696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 115bdd2596dSAchin Gupta 11652696946SOlivier Deprez /* Get current CPU context from SPMC context */ 117bdd2596dSAchin Gupta assert(cm_get_context(SECURE) == &(ctx->cpu_ctx)); 118bdd2596dSAchin Gupta 119bdd2596dSAchin Gupta /* 120bdd2596dSAchin Gupta * The SPMD must have initiated the original request through a 121bdd2596dSAchin Gupta * synchronous entry into SPMC. Jump back to the original C runtime 122bdd2596dSAchin Gupta * context with the value of rc in x0; 123bdd2596dSAchin Gupta */ 124bdd2596dSAchin Gupta spmd_spm_core_exit(ctx->c_rt_ctx, rc); 125bdd2596dSAchin Gupta 126bdd2596dSAchin Gupta panic(); 127bdd2596dSAchin Gupta } 128bdd2596dSAchin Gupta 129bdd2596dSAchin Gupta /******************************************************************************* 13052696946SOlivier Deprez * Jump to the SPM Core for the first time. 131bdd2596dSAchin Gupta ******************************************************************************/ 132bdd2596dSAchin Gupta static int32_t spmd_init(void) 133bdd2596dSAchin Gupta { 13452696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 13552696946SOlivier Deprez uint64_t rc; 1369dcf63ddSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 1379dcf63ddSOlivier Deprez unsigned int core_id; 138bdd2596dSAchin Gupta 13952696946SOlivier Deprez VERBOSE("SPM Core init start.\n"); 1409dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 1419dcf63ddSOlivier Deprez 1429dcf63ddSOlivier Deprez /* Set the SPMC context state on other CPUs to OFF */ 143*f0d743dbSOlivier Deprez for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) { 1449dcf63ddSOlivier Deprez if (core_id != linear_id) { 1459dcf63ddSOlivier Deprez spm_core_context[core_id].state = SPMC_STATE_OFF; 1469dcf63ddSOlivier Deprez } 1479dcf63ddSOlivier Deprez } 148bdd2596dSAchin Gupta 149bdd2596dSAchin Gupta rc = spmd_spm_core_sync_entry(ctx); 15052696946SOlivier Deprez if (rc != 0ULL) { 151bdd2596dSAchin Gupta ERROR("SPMC initialisation failed 0x%llx\n", rc); 15252696946SOlivier Deprez return 0; 153bdd2596dSAchin Gupta } 154bdd2596dSAchin Gupta 1559dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON; 1569dcf63ddSOlivier Deprez 15752696946SOlivier Deprez VERBOSE("SPM Core init end.\n"); 158bdd2596dSAchin Gupta 159bdd2596dSAchin Gupta return 1; 160bdd2596dSAchin Gupta } 161bdd2596dSAchin Gupta 162bdd2596dSAchin Gupta /******************************************************************************* 16352696946SOlivier Deprez * Loads SPMC manifest and inits SPMC. 1640f14d02fSMax Shvetsov ******************************************************************************/ 16523d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr) 1660f14d02fSMax Shvetsov { 16752696946SOlivier Deprez spmd_spm_core_context_t *spm_ctx = spmd_get_context(); 1680f14d02fSMax Shvetsov uint32_t ep_attr; 16952696946SOlivier Deprez int rc; 1700f14d02fSMax Shvetsov 17152696946SOlivier Deprez /* Load the SPM Core manifest */ 17223d5ba86SOlivier Deprez rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr); 1730f14d02fSMax Shvetsov if (rc != 0) { 17452696946SOlivier Deprez WARN("No or invalid SPM Core manifest image provided by BL2\n"); 17552696946SOlivier Deprez return rc; 1760f14d02fSMax Shvetsov } 1770f14d02fSMax Shvetsov 1780f14d02fSMax Shvetsov /* 17952696946SOlivier Deprez * Ensure that the SPM Core version is compatible with the SPM 18052696946SOlivier Deprez * Dispatcher version. 1810f14d02fSMax Shvetsov */ 182662af36dSJ-Alves if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) || 183662af36dSJ-Alves (spmc_attrs.minor_version > FFA_VERSION_MINOR)) { 184662af36dSJ-Alves WARN("Unsupported FFA version (%u.%u)\n", 1850f14d02fSMax Shvetsov spmc_attrs.major_version, spmc_attrs.minor_version); 18652696946SOlivier Deprez return -EINVAL; 1870f14d02fSMax Shvetsov } 1880f14d02fSMax Shvetsov 189662af36dSJ-Alves VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version, 1900f14d02fSMax Shvetsov spmc_attrs.minor_version); 1910f14d02fSMax Shvetsov 19252696946SOlivier Deprez VERBOSE("SPM Core run time EL%x.\n", 193033039f8SMax Shvetsov SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1); 1940f14d02fSMax Shvetsov 195ac03ac5eSMax Shvetsov /* Validate the SPMC ID, Ensure high bit is set */ 19652696946SOlivier Deprez if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) & 19752696946SOlivier Deprez SPMC_SECURE_ID_MASK) == 0U) { 19852696946SOlivier Deprez WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id); 19952696946SOlivier Deprez return -EINVAL; 200ac03ac5eSMax Shvetsov } 201ac03ac5eSMax Shvetsov 20252696946SOlivier Deprez /* Validate the SPM Core execution state */ 2030f14d02fSMax Shvetsov if ((spmc_attrs.exec_state != MODE_RW_64) && 2040f14d02fSMax Shvetsov (spmc_attrs.exec_state != MODE_RW_32)) { 20523d5ba86SOlivier Deprez WARN("Unsupported %s%x.\n", "SPM Core execution state 0x", 2060f14d02fSMax Shvetsov spmc_attrs.exec_state); 20752696946SOlivier Deprez return -EINVAL; 2080f14d02fSMax Shvetsov } 2090f14d02fSMax Shvetsov 21023d5ba86SOlivier Deprez VERBOSE("%s%x.\n", "SPM Core execution state 0x", 21123d5ba86SOlivier Deprez spmc_attrs.exec_state); 2120f14d02fSMax Shvetsov 213033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 214033039f8SMax Shvetsov /* Ensure manifest has not requested AArch32 state in S-EL2 */ 215033039f8SMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 216033039f8SMax Shvetsov WARN("AArch32 state at S-EL2 is not supported.\n"); 21752696946SOlivier Deprez return -EINVAL; 2180f14d02fSMax Shvetsov } 2190f14d02fSMax Shvetsov 2200f14d02fSMax Shvetsov /* 2210f14d02fSMax Shvetsov * Check if S-EL2 is supported on this system if S-EL2 2220f14d02fSMax Shvetsov * is required for SPM 2230f14d02fSMax Shvetsov */ 22452696946SOlivier Deprez if (!is_armv8_4_sel2_present()) { 22552696946SOlivier Deprez WARN("SPM Core run time S-EL2 is not supported.\n"); 22652696946SOlivier Deprez return -EINVAL; 2270f14d02fSMax Shvetsov } 228033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */ 2290f14d02fSMax Shvetsov 2300f14d02fSMax Shvetsov /* Initialise an entrypoint to set up the CPU context */ 2310f14d02fSMax Shvetsov ep_attr = SECURE | EP_ST_ENABLE; 23252696946SOlivier Deprez if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) { 2330f14d02fSMax Shvetsov ep_attr |= EP_EE_BIG; 2340f14d02fSMax Shvetsov } 2350f14d02fSMax Shvetsov 2360f14d02fSMax Shvetsov SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr); 2370f14d02fSMax Shvetsov assert(spmc_ep_info->pc == BL32_BASE); 2380f14d02fSMax Shvetsov 2390f14d02fSMax Shvetsov /* 24052696946SOlivier Deprez * Populate SPSR for SPM Core based upon validated parameters from the 24152696946SOlivier Deprez * manifest. 2420f14d02fSMax Shvetsov */ 2430f14d02fSMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 2440f14d02fSMax Shvetsov spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 2450f14d02fSMax Shvetsov SPSR_E_LITTLE, 2460f14d02fSMax Shvetsov DAIF_FIQ_BIT | 2470f14d02fSMax Shvetsov DAIF_IRQ_BIT | 2480f14d02fSMax Shvetsov DAIF_ABT_BIT); 2490f14d02fSMax Shvetsov } else { 250033039f8SMax Shvetsov 251033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 252033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL2; 253033039f8SMax Shvetsov #else 254033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL1; 255033039f8SMax Shvetsov #endif 256033039f8SMax Shvetsov spmc_ep_info->spsr = SPSR_64(runtime_el, 2570f14d02fSMax Shvetsov MODE_SP_ELX, 2580f14d02fSMax Shvetsov DISABLE_ALL_EXCEPTIONS); 2590f14d02fSMax Shvetsov } 2600f14d02fSMax Shvetsov 26152696946SOlivier Deprez /* Initialise SPM Core context with this entry point information */ 2620f14d02fSMax Shvetsov cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info); 2630f14d02fSMax Shvetsov 2640f14d02fSMax Shvetsov /* Reuse PSCI affinity states to mark this SPMC context as off */ 2650f14d02fSMax Shvetsov spm_ctx->state = AFF_STATE_OFF; 2660f14d02fSMax Shvetsov 26752696946SOlivier Deprez INFO("SPM Core setup done.\n"); 2680f14d02fSMax Shvetsov 269a334c4e6SOlivier Deprez /* Register power management hooks with PSCI */ 270a334c4e6SOlivier Deprez psci_register_spd_pm_hook(&spmd_pm); 271a334c4e6SOlivier Deprez 2720f14d02fSMax Shvetsov /* Register init function for deferred init. */ 2730f14d02fSMax Shvetsov bl31_register_bl32_init(&spmd_init); 2740f14d02fSMax Shvetsov 2750f14d02fSMax Shvetsov return 0; 2760f14d02fSMax Shvetsov } 2770f14d02fSMax Shvetsov 2780f14d02fSMax Shvetsov /******************************************************************************* 27952696946SOlivier Deprez * Initialize context of SPM Core. 280bdd2596dSAchin Gupta ******************************************************************************/ 2810f14d02fSMax Shvetsov int spmd_setup(void) 282bdd2596dSAchin Gupta { 28323d5ba86SOlivier Deprez void *spmc_manifest; 284bdd2596dSAchin Gupta int rc; 285bdd2596dSAchin Gupta 286bdd2596dSAchin Gupta spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 28752696946SOlivier Deprez if (spmc_ep_info == NULL) { 28852696946SOlivier Deprez WARN("No SPM Core image provided by BL2 boot loader.\n"); 28952696946SOlivier Deprez return -EINVAL; 290bdd2596dSAchin Gupta } 291bdd2596dSAchin Gupta 292bdd2596dSAchin Gupta /* Under no circumstances will this parameter be 0 */ 29352696946SOlivier Deprez assert(spmc_ep_info->pc != 0ULL); 294bdd2596dSAchin Gupta 295bdd2596dSAchin Gupta /* 296bdd2596dSAchin Gupta * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will 29752696946SOlivier Deprez * be used as a manifest for the SPM Core at the next lower EL/mode. 298bdd2596dSAchin Gupta */ 29923d5ba86SOlivier Deprez spmc_manifest = (void *)spmc_ep_info->args.arg0; 30023d5ba86SOlivier Deprez if (spmc_manifest == NULL) { 30123d5ba86SOlivier Deprez ERROR("Invalid or absent SPM Core manifest.\n"); 30223d5ba86SOlivier Deprez return -EINVAL; 303bdd2596dSAchin Gupta } 304bdd2596dSAchin Gupta 3050f14d02fSMax Shvetsov /* Load manifest, init SPMC */ 30623d5ba86SOlivier Deprez rc = spmd_spmc_init(spmc_manifest); 3070f14d02fSMax Shvetsov if (rc != 0) { 30852696946SOlivier Deprez WARN("Booting device without SPM initialization.\n"); 309bdd2596dSAchin Gupta } 310bdd2596dSAchin Gupta 3110f14d02fSMax Shvetsov return rc; 3120f14d02fSMax Shvetsov } 3130f14d02fSMax Shvetsov 3140f14d02fSMax Shvetsov /******************************************************************************* 3150f14d02fSMax Shvetsov * Forward SMC to the other security state 3160f14d02fSMax Shvetsov ******************************************************************************/ 31752696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 31852696946SOlivier Deprez bool secure_origin, 31952696946SOlivier Deprez uint64_t x1, 32052696946SOlivier Deprez uint64_t x2, 32152696946SOlivier Deprez uint64_t x3, 32252696946SOlivier Deprez uint64_t x4, 32352696946SOlivier Deprez void *handle) 3240f14d02fSMax Shvetsov { 325c2901419SOlivier Deprez unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE; 326c2901419SOlivier Deprez unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE; 32793ff138bSOlivier Deprez 3280f14d02fSMax Shvetsov /* Save incoming security state */ 32993ff138bSOlivier Deprez cm_el1_sysregs_context_save(secure_state_in); 330033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 33193ff138bSOlivier Deprez cm_el2_sysregs_context_save(secure_state_in); 332033039f8SMax Shvetsov #endif 3330f14d02fSMax Shvetsov 3340f14d02fSMax Shvetsov /* Restore outgoing security state */ 33593ff138bSOlivier Deprez cm_el1_sysregs_context_restore(secure_state_out); 336033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 33793ff138bSOlivier Deprez cm_el2_sysregs_context_restore(secure_state_out); 338033039f8SMax Shvetsov #endif 33993ff138bSOlivier Deprez cm_set_next_eret_context(secure_state_out); 3400f14d02fSMax Shvetsov 34193ff138bSOlivier Deprez SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4, 3420f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X5), 3430f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X6), 3440f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X7)); 3450f14d02fSMax Shvetsov } 3460f14d02fSMax Shvetsov 3470f14d02fSMax Shvetsov /******************************************************************************* 348662af36dSJ-Alves * Return FFA_ERROR with specified error code 3490f14d02fSMax Shvetsov ******************************************************************************/ 350662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code) 3510f14d02fSMax Shvetsov { 352662af36dSJ-Alves SMC_RET8(handle, FFA_ERROR, 353662af36dSJ-Alves FFA_TARGET_INFO_MBZ, error_code, 354662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 355662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 356bdd2596dSAchin Gupta } 357bdd2596dSAchin Gupta 358*f0d743dbSOlivier Deprez /******************************************************************************* 359*f0d743dbSOlivier Deprez * spmd_check_address_in_binary_image 360*f0d743dbSOlivier Deprez ******************************************************************************/ 361*f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address) 362*f0d743dbSOlivier Deprez { 363*f0d743dbSOlivier Deprez assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size)); 364*f0d743dbSOlivier Deprez 365*f0d743dbSOlivier Deprez return ((address >= spmc_attrs.load_address) && 366*f0d743dbSOlivier Deprez (address < (spmc_attrs.load_address + spmc_attrs.binary_size))); 367*f0d743dbSOlivier Deprez } 368*f0d743dbSOlivier Deprez 369c2901419SOlivier Deprez /****************************************************************************** 370c2901419SOlivier Deprez * spmd_is_spmc_message 371c2901419SOlivier Deprez *****************************************************************************/ 372c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep) 373c2901419SOlivier Deprez { 374c2901419SOlivier Deprez return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID) 375c2901419SOlivier Deprez && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id)); 376c2901419SOlivier Deprez } 377c2901419SOlivier Deprez 378*f0d743dbSOlivier Deprez /****************************************************************************** 379*f0d743dbSOlivier Deprez * spmd_handle_spmc_message 380*f0d743dbSOlivier Deprez *****************************************************************************/ 381*f0d743dbSOlivier Deprez static int32_t spmd_handle_spmc_message(uint64_t msg, uint64_t parm1, 382*f0d743dbSOlivier Deprez uint64_t parm2, uint64_t parm3, 383*f0d743dbSOlivier Deprez uint64_t parm4) 384*f0d743dbSOlivier Deprez { 385*f0d743dbSOlivier Deprez VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__, 386*f0d743dbSOlivier Deprez msg, parm1, parm2, parm3, parm4); 387*f0d743dbSOlivier Deprez 388*f0d743dbSOlivier Deprez switch (msg) { 389*f0d743dbSOlivier Deprez case SPMD_DIRECT_MSG_SET_ENTRY_POINT: 390*f0d743dbSOlivier Deprez return spmd_pm_secondary_core_set_ep(parm1, parm2, parm3); 391*f0d743dbSOlivier Deprez default: 392*f0d743dbSOlivier Deprez break; 393*f0d743dbSOlivier Deprez } 394*f0d743dbSOlivier Deprez 395*f0d743dbSOlivier Deprez return -EINVAL; 396*f0d743dbSOlivier Deprez } 397*f0d743dbSOlivier Deprez 398bdd2596dSAchin Gupta /******************************************************************************* 399662af36dSJ-Alves * This function handles all SMCs in the range reserved for FFA. Each call is 400bdd2596dSAchin Gupta * either forwarded to the other security state or handled by the SPM dispatcher 401bdd2596dSAchin Gupta ******************************************************************************/ 40252696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid, 40352696946SOlivier Deprez uint64_t x1, 40452696946SOlivier Deprez uint64_t x2, 40552696946SOlivier Deprez uint64_t x3, 40652696946SOlivier Deprez uint64_t x4, 40752696946SOlivier Deprez void *cookie, 40852696946SOlivier Deprez void *handle, 409bdd2596dSAchin Gupta uint64_t flags) 410bdd2596dSAchin Gupta { 41152696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 41293ff138bSOlivier Deprez bool secure_origin; 41393ff138bSOlivier Deprez int32_t ret; 4144388f28fSJ-Alves uint32_t input_version; 415bdd2596dSAchin Gupta 416bdd2596dSAchin Gupta /* Determine which security state this SMC originated from */ 41793ff138bSOlivier Deprez secure_origin = is_caller_secure(flags); 418bdd2596dSAchin Gupta 41952696946SOlivier Deprez INFO("SPM: 0x%x 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n", 420bdd2596dSAchin Gupta smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5), 421bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 422bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 423bdd2596dSAchin Gupta 424bdd2596dSAchin Gupta switch (smc_fid) { 425662af36dSJ-Alves case FFA_ERROR: 426bdd2596dSAchin Gupta /* 427bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 42852696946SOlivier Deprez * this CPU. If so, then indicate that the SPM Core initialised 429bdd2596dSAchin Gupta * unsuccessfully. 430bdd2596dSAchin Gupta */ 4319dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 432bdd2596dSAchin Gupta spmd_spm_core_sync_exit(x2); 4330f14d02fSMax Shvetsov } 434bdd2596dSAchin Gupta 43593ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4360f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 437bdd2596dSAchin Gupta break; /* not reached */ 438bdd2596dSAchin Gupta 439662af36dSJ-Alves case FFA_VERSION: 4404388f28fSJ-Alves input_version = (uint32_t)(0xFFFFFFFF & x1); 441bdd2596dSAchin Gupta /* 4424388f28fSJ-Alves * If caller is secure and SPMC was initialized, 4434388f28fSJ-Alves * return FFA_VERSION of SPMD. 4444388f28fSJ-Alves * If caller is non secure and SPMC was initialized, 4454388f28fSJ-Alves * return SPMC's version. 4464388f28fSJ-Alves * Sanity check to "input_version". 447bdd2596dSAchin Gupta */ 4484388f28fSJ-Alves if ((input_version & FFA_VERSION_BIT31_MASK) || 4494388f28fSJ-Alves (ctx->state == SPMC_STATE_RESET)) { 4504388f28fSJ-Alves ret = FFA_ERROR_NOT_SUPPORTED; 4514388f28fSJ-Alves } else if (!secure_origin) { 4524388f28fSJ-Alves ret = MAKE_FFA_VERSION(spmc_attrs.major_version, spmc_attrs.minor_version); 4534388f28fSJ-Alves } else { 4544388f28fSJ-Alves ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR); 4554388f28fSJ-Alves } 4564388f28fSJ-Alves 4574388f28fSJ-Alves SMC_RET8(handle, ret, FFA_TARGET_INFO_MBZ, FFA_TARGET_INFO_MBZ, 458662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 459662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 460bdd2596dSAchin Gupta break; /* not reached */ 461bdd2596dSAchin Gupta 462662af36dSJ-Alves case FFA_FEATURES: 463bdd2596dSAchin Gupta /* 464bdd2596dSAchin Gupta * This is an optional interface. Do the minimal checks and 46552696946SOlivier Deprez * forward to SPM Core which will handle it if implemented. 466bdd2596dSAchin Gupta */ 467bdd2596dSAchin Gupta 468bdd2596dSAchin Gupta /* 469662af36dSJ-Alves * Check if x1 holds a valid FFA fid. This is an 470bdd2596dSAchin Gupta * optimization. 471bdd2596dSAchin Gupta */ 472662af36dSJ-Alves if (!is_ffa_fid(x1)) { 473662af36dSJ-Alves return spmd_ffa_error_return(handle, 474662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 4750f14d02fSMax Shvetsov } 476bdd2596dSAchin Gupta 47752696946SOlivier Deprez /* Forward SMC from Normal world to the SPM Core */ 47893ff138bSOlivier Deprez if (!secure_origin) { 47993ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4800f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 48152696946SOlivier Deprez } 48252696946SOlivier Deprez 483bdd2596dSAchin Gupta /* 484bdd2596dSAchin Gupta * Return success if call was from secure world i.e. all 485662af36dSJ-Alves * FFA functions are supported. This is essentially a 486bdd2596dSAchin Gupta * nop. 487bdd2596dSAchin Gupta */ 488662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4, 489bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X5), 490bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 491bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 4920f14d02fSMax Shvetsov 493bdd2596dSAchin Gupta break; /* not reached */ 494bdd2596dSAchin Gupta 495662af36dSJ-Alves case FFA_ID_GET: 496ac03ac5eSMax Shvetsov /* 497662af36dSJ-Alves * Returns the ID of the calling FFA component. 498ac03ac5eSMax Shvetsov */ 499ac03ac5eSMax Shvetsov if (!secure_origin) { 500662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 501662af36dSJ-Alves FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID, 502662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 503662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 504662af36dSJ-Alves FFA_PARAM_MBZ); 50552696946SOlivier Deprez } 50652696946SOlivier Deprez 507662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 508662af36dSJ-Alves FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 509662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 510662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 511662af36dSJ-Alves FFA_PARAM_MBZ); 512ac03ac5eSMax Shvetsov 513ac03ac5eSMax Shvetsov break; /* not reached */ 514ac03ac5eSMax Shvetsov 515*f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_REQ_SMC32: 516*f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 517*f0d743dbSOlivier Deprez ret = spmd_handle_spmc_message(x3, x4, 518*f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 519*f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X6), 520*f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X7)); 521*f0d743dbSOlivier Deprez 522*f0d743dbSOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC32, 523*f0d743dbSOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 524*f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 525*f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 526*f0d743dbSOlivier Deprez FFA_PARAM_MBZ); 527*f0d743dbSOlivier Deprez } else { 528*f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 529*f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 530*f0d743dbSOlivier Deprez x1, x2, x3, x4, handle); 531*f0d743dbSOlivier Deprez } 532*f0d743dbSOlivier Deprez break; /* Not reached */ 533*f0d743dbSOlivier Deprez 534*f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_RESP_SMC32: 535*f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 536*f0d743dbSOlivier Deprez spmd_spm_core_sync_exit(0); 537*f0d743dbSOlivier Deprez } else { 538*f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 539*f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 540*f0d743dbSOlivier Deprez x1, x2, x3, x4, handle); 541*f0d743dbSOlivier Deprez } 542*f0d743dbSOlivier Deprez break; /* Not reached */ 543*f0d743dbSOlivier Deprez 544662af36dSJ-Alves case FFA_RX_RELEASE: 545662af36dSJ-Alves case FFA_RXTX_MAP_SMC32: 546662af36dSJ-Alves case FFA_RXTX_MAP_SMC64: 547662af36dSJ-Alves case FFA_RXTX_UNMAP: 548662af36dSJ-Alves case FFA_MSG_RUN: 549bdd2596dSAchin Gupta /* This interface must be invoked only by the Normal world */ 55093ff138bSOlivier Deprez if (secure_origin) { 551662af36dSJ-Alves return spmd_ffa_error_return(handle, 552662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 553bdd2596dSAchin Gupta } 554bdd2596dSAchin Gupta 555bdd2596dSAchin Gupta /* Fall through to forward the call to the other world */ 556bdd2596dSAchin Gupta 557662af36dSJ-Alves case FFA_PARTITION_INFO_GET: 558662af36dSJ-Alves case FFA_MSG_SEND: 559662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_REQ_SMC64: 560662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_RESP_SMC64: 561662af36dSJ-Alves case FFA_MEM_DONATE_SMC32: 562662af36dSJ-Alves case FFA_MEM_DONATE_SMC64: 563662af36dSJ-Alves case FFA_MEM_LEND_SMC32: 564662af36dSJ-Alves case FFA_MEM_LEND_SMC64: 565662af36dSJ-Alves case FFA_MEM_SHARE_SMC32: 566662af36dSJ-Alves case FFA_MEM_SHARE_SMC64: 567662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC32: 568662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC64: 569662af36dSJ-Alves case FFA_MEM_RETRIEVE_RESP: 570662af36dSJ-Alves case FFA_MEM_RELINQUISH: 571662af36dSJ-Alves case FFA_MEM_RECLAIM: 572662af36dSJ-Alves case FFA_SUCCESS_SMC32: 573662af36dSJ-Alves case FFA_SUCCESS_SMC64: 574bdd2596dSAchin Gupta /* 575bdd2596dSAchin Gupta * TODO: Assume that no requests originate from EL3 at the 576bdd2596dSAchin Gupta * moment. This will change if a SP service is required in 577bdd2596dSAchin Gupta * response to secure interrupts targeted to EL3. Until then 578bdd2596dSAchin Gupta * simply forward the call to the Normal world. 579bdd2596dSAchin Gupta */ 580bdd2596dSAchin Gupta 58193ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 5820f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 583bdd2596dSAchin Gupta break; /* not reached */ 584bdd2596dSAchin Gupta 585662af36dSJ-Alves case FFA_MSG_WAIT: 586bdd2596dSAchin Gupta /* 587bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 588bdd2596dSAchin Gupta * this CPU from the Secure world. If so, then indicate that the 58952696946SOlivier Deprez * SPM Core initialised successfully. 590bdd2596dSAchin Gupta */ 5919dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 592bdd2596dSAchin Gupta spmd_spm_core_sync_exit(0); 593bdd2596dSAchin Gupta } 594bdd2596dSAchin Gupta 5950f14d02fSMax Shvetsov /* Fall through to forward the call to the other world */ 596bdd2596dSAchin Gupta 597662af36dSJ-Alves case FFA_MSG_YIELD: 598bdd2596dSAchin Gupta /* This interface must be invoked only by the Secure world */ 59993ff138bSOlivier Deprez if (!secure_origin) { 600662af36dSJ-Alves return spmd_ffa_error_return(handle, 601662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 602bdd2596dSAchin Gupta } 603bdd2596dSAchin Gupta 60493ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 6050f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 606bdd2596dSAchin Gupta break; /* not reached */ 607bdd2596dSAchin Gupta 608bdd2596dSAchin Gupta default: 609bdd2596dSAchin Gupta WARN("SPM: Unsupported call 0x%08x\n", smc_fid); 610662af36dSJ-Alves return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 611bdd2596dSAchin Gupta } 612bdd2596dSAchin Gupta } 613