1bdd2596dSAchin Gupta /* 2*cdb49d47SOlivier Deprez * Copyright (c) 2020-2021, 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 /******************************************************************************* 4502d50bb0SOlivier Deprez * SPM Core context on CPU based on mpidr. 4602d50bb0SOlivier Deprez ******************************************************************************/ 4702d50bb0SOlivier Deprez spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr) 4802d50bb0SOlivier Deprez { 49f7fb0bf7SMax Shvetsov int core_idx = plat_core_pos_by_mpidr(mpidr); 50f7fb0bf7SMax Shvetsov 51f7fb0bf7SMax Shvetsov if (core_idx < 0) { 52f7fb0bf7SMax Shvetsov ERROR("Invalid mpidr: %llx, returned ID: %d\n", mpidr, core_idx); 53f7fb0bf7SMax Shvetsov panic(); 54f7fb0bf7SMax Shvetsov } 55f7fb0bf7SMax Shvetsov 56f7fb0bf7SMax Shvetsov return &spm_core_context[core_idx]; 5702d50bb0SOlivier Deprez } 5802d50bb0SOlivier Deprez 5902d50bb0SOlivier Deprez /******************************************************************************* 6052696946SOlivier Deprez * SPM Core context on current CPU get helper. 6152696946SOlivier Deprez ******************************************************************************/ 6252696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void) 6352696946SOlivier Deprez { 6402d50bb0SOlivier Deprez return spmd_get_context_by_mpidr(read_mpidr()); 6552696946SOlivier Deprez } 6652696946SOlivier Deprez 6752696946SOlivier Deprez /******************************************************************************* 68c0267cc9SOlivier Deprez * SPM Core entry point information get helper. 69c0267cc9SOlivier Deprez ******************************************************************************/ 70c0267cc9SOlivier Deprez entry_point_info_t *spmd_spmc_ep_info_get(void) 71c0267cc9SOlivier Deprez { 72c0267cc9SOlivier Deprez return spmc_ep_info; 73c0267cc9SOlivier Deprez } 74c0267cc9SOlivier Deprez 75c0267cc9SOlivier Deprez /******************************************************************************* 76a92bc73bSOlivier Deprez * SPM Core ID getter. 77a92bc73bSOlivier Deprez ******************************************************************************/ 78a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void) 79a92bc73bSOlivier Deprez { 80a92bc73bSOlivier Deprez return spmc_attrs.spmc_id; 81a92bc73bSOlivier Deprez } 82a92bc73bSOlivier Deprez 83a92bc73bSOlivier Deprez /******************************************************************************* 840f14d02fSMax Shvetsov * Static function declaration. 850f14d02fSMax Shvetsov ******************************************************************************/ 860f14d02fSMax Shvetsov static int32_t spmd_init(void); 8723d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr); 88662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, 8952696946SOlivier Deprez int error_code); 9052696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 9152696946SOlivier Deprez bool secure_origin, 9252696946SOlivier Deprez uint64_t x1, 9352696946SOlivier Deprez uint64_t x2, 9452696946SOlivier Deprez uint64_t x3, 9552696946SOlivier Deprez uint64_t x4, 9652696946SOlivier Deprez void *handle); 97bdd2596dSAchin Gupta 98bdd2596dSAchin Gupta /******************************************************************************* 9952696946SOlivier Deprez * This function takes an SPMC context pointer and performs a synchronous 10052696946SOlivier Deprez * SPMC entry. 101bdd2596dSAchin Gupta ******************************************************************************/ 102bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx) 103bdd2596dSAchin Gupta { 104bdd2596dSAchin Gupta uint64_t rc; 105bdd2596dSAchin Gupta 106bdd2596dSAchin Gupta assert(spmc_ctx != NULL); 107bdd2596dSAchin Gupta 108bdd2596dSAchin Gupta cm_set_context(&(spmc_ctx->cpu_ctx), SECURE); 109bdd2596dSAchin Gupta 110bdd2596dSAchin Gupta /* Restore the context assigned above */ 111bdd2596dSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 112033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 11328f39f02SMax Shvetsov cm_el2_sysregs_context_restore(SECURE); 114033039f8SMax Shvetsov #endif 115bdd2596dSAchin Gupta cm_set_next_eret_context(SECURE); 116bdd2596dSAchin Gupta 117033039f8SMax Shvetsov /* Enter SPMC */ 118bdd2596dSAchin Gupta rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx); 119bdd2596dSAchin Gupta 120bdd2596dSAchin Gupta /* Save secure state */ 121bdd2596dSAchin Gupta cm_el1_sysregs_context_save(SECURE); 122033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 12328f39f02SMax Shvetsov cm_el2_sysregs_context_save(SECURE); 124033039f8SMax Shvetsov #endif 125bdd2596dSAchin Gupta 126bdd2596dSAchin Gupta return rc; 127bdd2596dSAchin Gupta } 128bdd2596dSAchin Gupta 129bdd2596dSAchin Gupta /******************************************************************************* 13052696946SOlivier Deprez * This function returns to the place where spmd_spm_core_sync_entry() was 131bdd2596dSAchin Gupta * called originally. 132bdd2596dSAchin Gupta ******************************************************************************/ 133bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc) 134bdd2596dSAchin Gupta { 13552696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 136bdd2596dSAchin Gupta 13752696946SOlivier Deprez /* Get current CPU context from SPMC context */ 138bdd2596dSAchin Gupta assert(cm_get_context(SECURE) == &(ctx->cpu_ctx)); 139bdd2596dSAchin Gupta 140bdd2596dSAchin Gupta /* 141bdd2596dSAchin Gupta * The SPMD must have initiated the original request through a 142bdd2596dSAchin Gupta * synchronous entry into SPMC. Jump back to the original C runtime 143bdd2596dSAchin Gupta * context with the value of rc in x0; 144bdd2596dSAchin Gupta */ 145bdd2596dSAchin Gupta spmd_spm_core_exit(ctx->c_rt_ctx, rc); 146bdd2596dSAchin Gupta 147bdd2596dSAchin Gupta panic(); 148bdd2596dSAchin Gupta } 149bdd2596dSAchin Gupta 150bdd2596dSAchin Gupta /******************************************************************************* 15152696946SOlivier Deprez * Jump to the SPM Core for the first time. 152bdd2596dSAchin Gupta ******************************************************************************/ 153bdd2596dSAchin Gupta static int32_t spmd_init(void) 154bdd2596dSAchin Gupta { 15552696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 15652696946SOlivier Deprez uint64_t rc; 1579dcf63ddSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 1589dcf63ddSOlivier Deprez unsigned int core_id; 159bdd2596dSAchin Gupta 16052696946SOlivier Deprez VERBOSE("SPM Core init start.\n"); 1619dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 1629dcf63ddSOlivier Deprez 1639dcf63ddSOlivier Deprez /* Set the SPMC context state on other CPUs to OFF */ 164f0d743dbSOlivier Deprez for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) { 1659dcf63ddSOlivier Deprez if (core_id != linear_id) { 1669dcf63ddSOlivier Deprez spm_core_context[core_id].state = SPMC_STATE_OFF; 1679dcf63ddSOlivier Deprez } 1689dcf63ddSOlivier Deprez } 169bdd2596dSAchin Gupta 170bdd2596dSAchin Gupta rc = spmd_spm_core_sync_entry(ctx); 17152696946SOlivier Deprez if (rc != 0ULL) { 172bdd2596dSAchin Gupta ERROR("SPMC initialisation failed 0x%llx\n", rc); 17352696946SOlivier Deprez return 0; 174bdd2596dSAchin Gupta } 175bdd2596dSAchin Gupta 1769dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON; 1779dcf63ddSOlivier Deprez 17852696946SOlivier Deprez VERBOSE("SPM Core init end.\n"); 179bdd2596dSAchin Gupta 180bdd2596dSAchin Gupta return 1; 181bdd2596dSAchin Gupta } 182bdd2596dSAchin Gupta 183bdd2596dSAchin Gupta /******************************************************************************* 18452696946SOlivier Deprez * Loads SPMC manifest and inits SPMC. 1850f14d02fSMax Shvetsov ******************************************************************************/ 18623d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr) 1870f14d02fSMax Shvetsov { 18852696946SOlivier Deprez spmd_spm_core_context_t *spm_ctx = spmd_get_context(); 1890f14d02fSMax Shvetsov uint32_t ep_attr; 19052696946SOlivier Deprez int rc; 1910f14d02fSMax Shvetsov 19252696946SOlivier Deprez /* Load the SPM Core manifest */ 19323d5ba86SOlivier Deprez rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr); 1940f14d02fSMax Shvetsov if (rc != 0) { 19552696946SOlivier Deprez WARN("No or invalid SPM Core manifest image provided by BL2\n"); 19652696946SOlivier Deprez return rc; 1970f14d02fSMax Shvetsov } 1980f14d02fSMax Shvetsov 1990f14d02fSMax Shvetsov /* 20052696946SOlivier Deprez * Ensure that the SPM Core version is compatible with the SPM 20152696946SOlivier Deprez * Dispatcher version. 2020f14d02fSMax Shvetsov */ 203662af36dSJ-Alves if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) || 204662af36dSJ-Alves (spmc_attrs.minor_version > FFA_VERSION_MINOR)) { 205662af36dSJ-Alves WARN("Unsupported FFA version (%u.%u)\n", 2060f14d02fSMax Shvetsov spmc_attrs.major_version, spmc_attrs.minor_version); 20752696946SOlivier Deprez return -EINVAL; 2080f14d02fSMax Shvetsov } 2090f14d02fSMax Shvetsov 210662af36dSJ-Alves VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version, 2110f14d02fSMax Shvetsov spmc_attrs.minor_version); 2120f14d02fSMax Shvetsov 21352696946SOlivier Deprez VERBOSE("SPM Core run time EL%x.\n", 214033039f8SMax Shvetsov SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1); 2150f14d02fSMax Shvetsov 216ac03ac5eSMax Shvetsov /* Validate the SPMC ID, Ensure high bit is set */ 21752696946SOlivier Deprez if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) & 21852696946SOlivier Deprez SPMC_SECURE_ID_MASK) == 0U) { 21952696946SOlivier Deprez WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id); 22052696946SOlivier Deprez return -EINVAL; 221ac03ac5eSMax Shvetsov } 222ac03ac5eSMax Shvetsov 22352696946SOlivier Deprez /* Validate the SPM Core execution state */ 2240f14d02fSMax Shvetsov if ((spmc_attrs.exec_state != MODE_RW_64) && 2250f14d02fSMax Shvetsov (spmc_attrs.exec_state != MODE_RW_32)) { 22623d5ba86SOlivier Deprez WARN("Unsupported %s%x.\n", "SPM Core execution state 0x", 2270f14d02fSMax Shvetsov spmc_attrs.exec_state); 22852696946SOlivier Deprez return -EINVAL; 2290f14d02fSMax Shvetsov } 2300f14d02fSMax Shvetsov 23123d5ba86SOlivier Deprez VERBOSE("%s%x.\n", "SPM Core execution state 0x", 23223d5ba86SOlivier Deprez spmc_attrs.exec_state); 2330f14d02fSMax Shvetsov 234033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 235033039f8SMax Shvetsov /* Ensure manifest has not requested AArch32 state in S-EL2 */ 236033039f8SMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 237033039f8SMax Shvetsov WARN("AArch32 state at S-EL2 is not supported.\n"); 23852696946SOlivier Deprez return -EINVAL; 2390f14d02fSMax Shvetsov } 2400f14d02fSMax Shvetsov 2410f14d02fSMax Shvetsov /* 2420f14d02fSMax Shvetsov * Check if S-EL2 is supported on this system if S-EL2 2430f14d02fSMax Shvetsov * is required for SPM 2440f14d02fSMax Shvetsov */ 24552696946SOlivier Deprez if (!is_armv8_4_sel2_present()) { 24652696946SOlivier Deprez WARN("SPM Core run time S-EL2 is not supported.\n"); 24752696946SOlivier Deprez return -EINVAL; 2480f14d02fSMax Shvetsov } 249033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */ 2500f14d02fSMax Shvetsov 2510f14d02fSMax Shvetsov /* Initialise an entrypoint to set up the CPU context */ 2520f14d02fSMax Shvetsov ep_attr = SECURE | EP_ST_ENABLE; 25352696946SOlivier Deprez if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) { 2540f14d02fSMax Shvetsov ep_attr |= EP_EE_BIG; 2550f14d02fSMax Shvetsov } 2560f14d02fSMax Shvetsov 2570f14d02fSMax Shvetsov SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr); 2580f14d02fSMax Shvetsov 2590f14d02fSMax Shvetsov /* 26052696946SOlivier Deprez * Populate SPSR for SPM Core based upon validated parameters from the 26152696946SOlivier Deprez * manifest. 2620f14d02fSMax Shvetsov */ 2630f14d02fSMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 2640f14d02fSMax Shvetsov spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 2650f14d02fSMax Shvetsov SPSR_E_LITTLE, 2660f14d02fSMax Shvetsov DAIF_FIQ_BIT | 2670f14d02fSMax Shvetsov DAIF_IRQ_BIT | 2680f14d02fSMax Shvetsov DAIF_ABT_BIT); 2690f14d02fSMax Shvetsov } else { 270033039f8SMax Shvetsov 271033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 272033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL2; 273033039f8SMax Shvetsov #else 274033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL1; 275033039f8SMax Shvetsov #endif 276033039f8SMax Shvetsov spmc_ep_info->spsr = SPSR_64(runtime_el, 2770f14d02fSMax Shvetsov MODE_SP_ELX, 2780f14d02fSMax Shvetsov DISABLE_ALL_EXCEPTIONS); 2790f14d02fSMax Shvetsov } 2800f14d02fSMax Shvetsov 28152696946SOlivier Deprez /* Initialise SPM Core context with this entry point information */ 2820f14d02fSMax Shvetsov cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info); 2830f14d02fSMax Shvetsov 2840f14d02fSMax Shvetsov /* Reuse PSCI affinity states to mark this SPMC context as off */ 2850f14d02fSMax Shvetsov spm_ctx->state = AFF_STATE_OFF; 2860f14d02fSMax Shvetsov 28752696946SOlivier Deprez INFO("SPM Core setup done.\n"); 2880f14d02fSMax Shvetsov 289a334c4e6SOlivier Deprez /* Register power management hooks with PSCI */ 290a334c4e6SOlivier Deprez psci_register_spd_pm_hook(&spmd_pm); 291a334c4e6SOlivier Deprez 2920f14d02fSMax Shvetsov /* Register init function for deferred init. */ 2930f14d02fSMax Shvetsov bl31_register_bl32_init(&spmd_init); 2940f14d02fSMax Shvetsov 2950f14d02fSMax Shvetsov return 0; 2960f14d02fSMax Shvetsov } 2970f14d02fSMax Shvetsov 2980f14d02fSMax Shvetsov /******************************************************************************* 29952696946SOlivier Deprez * Initialize context of SPM Core. 300bdd2596dSAchin Gupta ******************************************************************************/ 3010f14d02fSMax Shvetsov int spmd_setup(void) 302bdd2596dSAchin Gupta { 30323d5ba86SOlivier Deprez void *spmc_manifest; 304bdd2596dSAchin Gupta int rc; 305bdd2596dSAchin Gupta 306bdd2596dSAchin Gupta spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 30752696946SOlivier Deprez if (spmc_ep_info == NULL) { 30852696946SOlivier Deprez WARN("No SPM Core image provided by BL2 boot loader.\n"); 30952696946SOlivier Deprez return -EINVAL; 310bdd2596dSAchin Gupta } 311bdd2596dSAchin Gupta 312bdd2596dSAchin Gupta /* Under no circumstances will this parameter be 0 */ 31352696946SOlivier Deprez assert(spmc_ep_info->pc != 0ULL); 314bdd2596dSAchin Gupta 315bdd2596dSAchin Gupta /* 316bdd2596dSAchin Gupta * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will 31752696946SOlivier Deprez * be used as a manifest for the SPM Core at the next lower EL/mode. 318bdd2596dSAchin Gupta */ 31923d5ba86SOlivier Deprez spmc_manifest = (void *)spmc_ep_info->args.arg0; 32023d5ba86SOlivier Deprez if (spmc_manifest == NULL) { 32123d5ba86SOlivier Deprez ERROR("Invalid or absent SPM Core manifest.\n"); 32223d5ba86SOlivier Deprez return -EINVAL; 323bdd2596dSAchin Gupta } 324bdd2596dSAchin Gupta 3250f14d02fSMax Shvetsov /* Load manifest, init SPMC */ 32623d5ba86SOlivier Deprez rc = spmd_spmc_init(spmc_manifest); 3270f14d02fSMax Shvetsov if (rc != 0) { 32852696946SOlivier Deprez WARN("Booting device without SPM initialization.\n"); 329bdd2596dSAchin Gupta } 330bdd2596dSAchin Gupta 3310f14d02fSMax Shvetsov return rc; 3320f14d02fSMax Shvetsov } 3330f14d02fSMax Shvetsov 3340f14d02fSMax Shvetsov /******************************************************************************* 3350f14d02fSMax Shvetsov * Forward SMC to the other security state 3360f14d02fSMax Shvetsov ******************************************************************************/ 33752696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 33852696946SOlivier Deprez bool secure_origin, 33952696946SOlivier Deprez uint64_t x1, 34052696946SOlivier Deprez uint64_t x2, 34152696946SOlivier Deprez uint64_t x3, 34252696946SOlivier Deprez uint64_t x4, 34352696946SOlivier Deprez void *handle) 3440f14d02fSMax Shvetsov { 345c2901419SOlivier Deprez unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE; 346c2901419SOlivier Deprez unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE; 34793ff138bSOlivier Deprez 3480f14d02fSMax Shvetsov /* Save incoming security state */ 34993ff138bSOlivier Deprez cm_el1_sysregs_context_save(secure_state_in); 350033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 35193ff138bSOlivier Deprez cm_el2_sysregs_context_save(secure_state_in); 352033039f8SMax Shvetsov #endif 3530f14d02fSMax Shvetsov 3540f14d02fSMax Shvetsov /* Restore outgoing security state */ 35593ff138bSOlivier Deprez cm_el1_sysregs_context_restore(secure_state_out); 356033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 35793ff138bSOlivier Deprez cm_el2_sysregs_context_restore(secure_state_out); 358033039f8SMax Shvetsov #endif 35993ff138bSOlivier Deprez cm_set_next_eret_context(secure_state_out); 3600f14d02fSMax Shvetsov 36193ff138bSOlivier Deprez SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4, 3620f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X5), 3630f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X6), 3640f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X7)); 3650f14d02fSMax Shvetsov } 3660f14d02fSMax Shvetsov 3670f14d02fSMax Shvetsov /******************************************************************************* 368662af36dSJ-Alves * Return FFA_ERROR with specified error code 3690f14d02fSMax Shvetsov ******************************************************************************/ 370662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code) 3710f14d02fSMax Shvetsov { 372662af36dSJ-Alves SMC_RET8(handle, FFA_ERROR, 373662af36dSJ-Alves FFA_TARGET_INFO_MBZ, error_code, 374662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 375662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 376bdd2596dSAchin Gupta } 377bdd2596dSAchin Gupta 378f0d743dbSOlivier Deprez /******************************************************************************* 379f0d743dbSOlivier Deprez * spmd_check_address_in_binary_image 380f0d743dbSOlivier Deprez ******************************************************************************/ 381f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address) 382f0d743dbSOlivier Deprez { 383f0d743dbSOlivier Deprez assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size)); 384f0d743dbSOlivier Deprez 385f0d743dbSOlivier Deprez return ((address >= spmc_attrs.load_address) && 386f0d743dbSOlivier Deprez (address < (spmc_attrs.load_address + spmc_attrs.binary_size))); 387f0d743dbSOlivier Deprez } 388f0d743dbSOlivier Deprez 389c2901419SOlivier Deprez /****************************************************************************** 390c2901419SOlivier Deprez * spmd_is_spmc_message 391c2901419SOlivier Deprez *****************************************************************************/ 392c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep) 393c2901419SOlivier Deprez { 394c2901419SOlivier Deprez return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID) 395c2901419SOlivier Deprez && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id)); 396c2901419SOlivier Deprez } 397c2901419SOlivier Deprez 398f0d743dbSOlivier Deprez /****************************************************************************** 399f0d743dbSOlivier Deprez * spmd_handle_spmc_message 400f0d743dbSOlivier Deprez *****************************************************************************/ 401a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg, 402a92bc73bSOlivier Deprez unsigned long long parm1, unsigned long long parm2, 403a92bc73bSOlivier Deprez unsigned long long parm3, unsigned long long parm4) 404f0d743dbSOlivier Deprez { 405f0d743dbSOlivier Deprez VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__, 406f0d743dbSOlivier Deprez msg, parm1, parm2, parm3, parm4); 407f0d743dbSOlivier Deprez 408f0d743dbSOlivier Deprez return -EINVAL; 409f0d743dbSOlivier Deprez } 410f0d743dbSOlivier Deprez 411bdd2596dSAchin Gupta /******************************************************************************* 412662af36dSJ-Alves * This function handles all SMCs in the range reserved for FFA. Each call is 413bdd2596dSAchin Gupta * either forwarded to the other security state or handled by the SPM dispatcher 414bdd2596dSAchin Gupta ******************************************************************************/ 41552696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid, 41652696946SOlivier Deprez uint64_t x1, 41752696946SOlivier Deprez uint64_t x2, 41852696946SOlivier Deprez uint64_t x3, 41952696946SOlivier Deprez uint64_t x4, 42052696946SOlivier Deprez void *cookie, 42152696946SOlivier Deprez void *handle, 422bdd2596dSAchin Gupta uint64_t flags) 423bdd2596dSAchin Gupta { 424*cdb49d47SOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 42552696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 42693ff138bSOlivier Deprez bool secure_origin; 42793ff138bSOlivier Deprez int32_t ret; 4284388f28fSJ-Alves uint32_t input_version; 429bdd2596dSAchin Gupta 430bdd2596dSAchin Gupta /* Determine which security state this SMC originated from */ 43193ff138bSOlivier Deprez secure_origin = is_caller_secure(flags); 432bdd2596dSAchin Gupta 433*cdb49d47SOlivier Deprez VERBOSE("SPM(%u): 0x%x 0x%llx 0x%llx 0x%llx 0x%llx " 434*cdb49d47SOlivier Deprez "0x%llx 0x%llx 0x%llx\n", 435*cdb49d47SOlivier Deprez linear_id, smc_fid, x1, x2, x3, x4, 436*cdb49d47SOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 437bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 438bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 439bdd2596dSAchin Gupta 440bdd2596dSAchin Gupta switch (smc_fid) { 441662af36dSJ-Alves case FFA_ERROR: 442bdd2596dSAchin Gupta /* 443bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 44452696946SOlivier Deprez * this CPU. If so, then indicate that the SPM Core initialised 445bdd2596dSAchin Gupta * unsuccessfully. 446bdd2596dSAchin Gupta */ 4479dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 448bdd2596dSAchin Gupta spmd_spm_core_sync_exit(x2); 4490f14d02fSMax Shvetsov } 450bdd2596dSAchin Gupta 45193ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4520f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 453bdd2596dSAchin Gupta break; /* not reached */ 454bdd2596dSAchin Gupta 455662af36dSJ-Alves case FFA_VERSION: 4564388f28fSJ-Alves input_version = (uint32_t)(0xFFFFFFFF & x1); 457bdd2596dSAchin Gupta /* 4584388f28fSJ-Alves * If caller is secure and SPMC was initialized, 4594388f28fSJ-Alves * return FFA_VERSION of SPMD. 4604388f28fSJ-Alves * If caller is non secure and SPMC was initialized, 4614388f28fSJ-Alves * return SPMC's version. 4624388f28fSJ-Alves * Sanity check to "input_version". 463bdd2596dSAchin Gupta */ 4644388f28fSJ-Alves if ((input_version & FFA_VERSION_BIT31_MASK) || 4654388f28fSJ-Alves (ctx->state == SPMC_STATE_RESET)) { 4664388f28fSJ-Alves ret = FFA_ERROR_NOT_SUPPORTED; 4674388f28fSJ-Alves } else if (!secure_origin) { 4684388f28fSJ-Alves ret = MAKE_FFA_VERSION(spmc_attrs.major_version, spmc_attrs.minor_version); 4694388f28fSJ-Alves } else { 4704388f28fSJ-Alves ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR); 4714388f28fSJ-Alves } 4724388f28fSJ-Alves 4734388f28fSJ-Alves SMC_RET8(handle, ret, FFA_TARGET_INFO_MBZ, FFA_TARGET_INFO_MBZ, 474662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 475662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 476bdd2596dSAchin Gupta break; /* not reached */ 477bdd2596dSAchin Gupta 478662af36dSJ-Alves case FFA_FEATURES: 479bdd2596dSAchin Gupta /* 480bdd2596dSAchin Gupta * This is an optional interface. Do the minimal checks and 48152696946SOlivier Deprez * forward to SPM Core which will handle it if implemented. 482bdd2596dSAchin Gupta */ 483bdd2596dSAchin Gupta 484bdd2596dSAchin Gupta /* 485662af36dSJ-Alves * Check if x1 holds a valid FFA fid. This is an 486bdd2596dSAchin Gupta * optimization. 487bdd2596dSAchin Gupta */ 488662af36dSJ-Alves if (!is_ffa_fid(x1)) { 489662af36dSJ-Alves return spmd_ffa_error_return(handle, 490662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 4910f14d02fSMax Shvetsov } 492bdd2596dSAchin Gupta 49352696946SOlivier Deprez /* Forward SMC from Normal world to the SPM Core */ 49493ff138bSOlivier Deprez if (!secure_origin) { 49593ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4960f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 49752696946SOlivier Deprez } 49852696946SOlivier Deprez 499bdd2596dSAchin Gupta /* 500bdd2596dSAchin Gupta * Return success if call was from secure world i.e. all 501662af36dSJ-Alves * FFA functions are supported. This is essentially a 502bdd2596dSAchin Gupta * nop. 503bdd2596dSAchin Gupta */ 504662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4, 505bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X5), 506bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 507bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 5080f14d02fSMax Shvetsov 509bdd2596dSAchin Gupta break; /* not reached */ 510bdd2596dSAchin Gupta 511662af36dSJ-Alves case FFA_ID_GET: 512ac03ac5eSMax Shvetsov /* 513662af36dSJ-Alves * Returns the ID of the calling FFA component. 514ac03ac5eSMax Shvetsov */ 515ac03ac5eSMax Shvetsov if (!secure_origin) { 516662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 517662af36dSJ-Alves FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID, 518662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 519662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 520662af36dSJ-Alves FFA_PARAM_MBZ); 52152696946SOlivier Deprez } 52252696946SOlivier Deprez 523662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 524662af36dSJ-Alves FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 525662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 526662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 527662af36dSJ-Alves FFA_PARAM_MBZ); 528ac03ac5eSMax Shvetsov 529ac03ac5eSMax Shvetsov break; /* not reached */ 530ac03ac5eSMax Shvetsov 531*cdb49d47SOlivier Deprez case FFA_SECONDARY_EP_REGISTER_SMC64: 532*cdb49d47SOlivier Deprez if (secure_origin) { 533*cdb49d47SOlivier Deprez ret = spmd_pm_secondary_ep_register(x1); 534*cdb49d47SOlivier Deprez 535*cdb49d47SOlivier Deprez if (ret < 0) { 536*cdb49d47SOlivier Deprez SMC_RET8(handle, FFA_ERROR_SMC64, 537*cdb49d47SOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 538*cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 539*cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 540*cdb49d47SOlivier Deprez FFA_PARAM_MBZ); 541*cdb49d47SOlivier Deprez } else { 542*cdb49d47SOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC64, 543*cdb49d47SOlivier Deprez FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, 544*cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 545*cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 546*cdb49d47SOlivier Deprez FFA_PARAM_MBZ); 547*cdb49d47SOlivier Deprez } 548*cdb49d47SOlivier Deprez } 549*cdb49d47SOlivier Deprez 550*cdb49d47SOlivier Deprez return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 551*cdb49d47SOlivier Deprez break; /* Not reached */ 552*cdb49d47SOlivier Deprez 553f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_REQ_SMC32: 554f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 555f0d743dbSOlivier Deprez ret = spmd_handle_spmc_message(x3, x4, 556f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 557f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X6), 558f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X7)); 559f0d743dbSOlivier Deprez 560f0d743dbSOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC32, 561f0d743dbSOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 562f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 563f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 564f0d743dbSOlivier Deprez FFA_PARAM_MBZ); 565f0d743dbSOlivier Deprez } else { 566f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 567f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 568f0d743dbSOlivier Deprez x1, x2, x3, x4, handle); 569f0d743dbSOlivier Deprez } 570f0d743dbSOlivier Deprez break; /* Not reached */ 571f0d743dbSOlivier Deprez 572f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_RESP_SMC32: 573f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 574f0d743dbSOlivier Deprez spmd_spm_core_sync_exit(0); 575f0d743dbSOlivier Deprez } else { 576f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 577f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 578f0d743dbSOlivier Deprez x1, x2, x3, x4, handle); 579f0d743dbSOlivier Deprez } 580f0d743dbSOlivier Deprez break; /* Not reached */ 581f0d743dbSOlivier Deprez 582662af36dSJ-Alves case FFA_RX_RELEASE: 583662af36dSJ-Alves case FFA_RXTX_MAP_SMC32: 584662af36dSJ-Alves case FFA_RXTX_MAP_SMC64: 585662af36dSJ-Alves case FFA_RXTX_UNMAP: 586545b8eb3SRuari Phipps case FFA_PARTITION_INFO_GET: 587545b8eb3SRuari Phipps /* 588545b8eb3SRuari Phipps * Should not be allowed to forward FFA_PARTITION_INFO_GET 589545b8eb3SRuari Phipps * from Secure world to Normal world 590545b8eb3SRuari Phipps * 591545b8eb3SRuari Phipps * Fall through to forward the call to the other world 592545b8eb3SRuari Phipps */ 593662af36dSJ-Alves case FFA_MSG_RUN: 594bdd2596dSAchin Gupta /* This interface must be invoked only by the Normal world */ 595545b8eb3SRuari Phipps 59693ff138bSOlivier Deprez if (secure_origin) { 597662af36dSJ-Alves return spmd_ffa_error_return(handle, 598662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 599bdd2596dSAchin Gupta } 600bdd2596dSAchin Gupta 601bdd2596dSAchin Gupta /* Fall through to forward the call to the other world */ 602662af36dSJ-Alves case FFA_MSG_SEND: 603662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_REQ_SMC64: 604662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_RESP_SMC64: 605662af36dSJ-Alves case FFA_MEM_DONATE_SMC32: 606662af36dSJ-Alves case FFA_MEM_DONATE_SMC64: 607662af36dSJ-Alves case FFA_MEM_LEND_SMC32: 608662af36dSJ-Alves case FFA_MEM_LEND_SMC64: 609662af36dSJ-Alves case FFA_MEM_SHARE_SMC32: 610662af36dSJ-Alves case FFA_MEM_SHARE_SMC64: 611662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC32: 612662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC64: 613662af36dSJ-Alves case FFA_MEM_RETRIEVE_RESP: 614662af36dSJ-Alves case FFA_MEM_RELINQUISH: 615662af36dSJ-Alves case FFA_MEM_RECLAIM: 616662af36dSJ-Alves case FFA_SUCCESS_SMC32: 617662af36dSJ-Alves case FFA_SUCCESS_SMC64: 618bdd2596dSAchin Gupta /* 619bdd2596dSAchin Gupta * TODO: Assume that no requests originate from EL3 at the 620bdd2596dSAchin Gupta * moment. This will change if a SP service is required in 621bdd2596dSAchin Gupta * response to secure interrupts targeted to EL3. Until then 622bdd2596dSAchin Gupta * simply forward the call to the Normal world. 623bdd2596dSAchin Gupta */ 624bdd2596dSAchin Gupta 62593ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 6260f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 627bdd2596dSAchin Gupta break; /* not reached */ 628bdd2596dSAchin Gupta 629662af36dSJ-Alves case FFA_MSG_WAIT: 630bdd2596dSAchin Gupta /* 631bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 632bdd2596dSAchin Gupta * this CPU from the Secure world. If so, then indicate that the 63352696946SOlivier Deprez * SPM Core initialised successfully. 634bdd2596dSAchin Gupta */ 6359dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 636bdd2596dSAchin Gupta spmd_spm_core_sync_exit(0); 637bdd2596dSAchin Gupta } 638bdd2596dSAchin Gupta 6390f14d02fSMax Shvetsov /* Fall through to forward the call to the other world */ 640bdd2596dSAchin Gupta 641662af36dSJ-Alves case FFA_MSG_YIELD: 642bdd2596dSAchin Gupta /* This interface must be invoked only by the Secure world */ 64393ff138bSOlivier Deprez if (!secure_origin) { 644662af36dSJ-Alves return spmd_ffa_error_return(handle, 645662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 646bdd2596dSAchin Gupta } 647bdd2596dSAchin Gupta 64893ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 6490f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 650bdd2596dSAchin Gupta break; /* not reached */ 651bdd2596dSAchin Gupta 652bdd2596dSAchin Gupta default: 653bdd2596dSAchin Gupta WARN("SPM: Unsupported call 0x%08x\n", smc_fid); 654662af36dSJ-Alves return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 655bdd2596dSAchin Gupta } 656bdd2596dSAchin Gupta } 657