1bdd2596dSAchin Gupta /* 29944f557SDaniel Boulby * Copyright (c) 2020-2022, 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> 94ce3e99aSScott Branden #include <inttypes.h> 104ce3e99aSScott Branden #include <stdint.h> 11bdd2596dSAchin Gupta #include <string.h> 12bdd2596dSAchin Gupta 13bdd2596dSAchin Gupta #include <arch_helpers.h> 1452696946SOlivier Deprez #include <arch/aarch64/arch_features.h> 15bdd2596dSAchin Gupta #include <bl31/bl31.h> 168cb99c3fSOlivier Deprez #include <bl31/interrupt_mgmt.h> 17bdd2596dSAchin Gupta #include <common/debug.h> 18bdd2596dSAchin Gupta #include <common/runtime_svc.h> 19bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h> 20bdd2596dSAchin Gupta #include <lib/smccc.h> 21bdd2596dSAchin Gupta #include <lib/spinlock.h> 22bdd2596dSAchin Gupta #include <lib/utils.h> 23bdd2596dSAchin Gupta #include <plat/common/common_def.h> 24bdd2596dSAchin Gupta #include <plat/common/platform.h> 25bdd2596dSAchin Gupta #include <platform_def.h> 26662af36dSJ-Alves #include <services/ffa_svc.h> 276da76075SMarc Bonnici #include <services/spmc_svc.h> 28bdd2596dSAchin Gupta #include <services/spmd_svc.h> 29bdd2596dSAchin Gupta #include <smccc_helpers.h> 30bdd2596dSAchin Gupta #include "spmd_private.h" 31bdd2596dSAchin Gupta 32bdd2596dSAchin Gupta /******************************************************************************* 33bdd2596dSAchin Gupta * SPM Core context information. 34bdd2596dSAchin Gupta ******************************************************************************/ 3552696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT]; 36bdd2596dSAchin Gupta 37bdd2596dSAchin Gupta /******************************************************************************* 386da76075SMarc Bonnici * SPM Core attribute information is read from its manifest if the SPMC is not 396da76075SMarc Bonnici * at EL3. Else, it is populated from the SPMC directly. 40bdd2596dSAchin Gupta ******************************************************************************/ 4152696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs; 420f14d02fSMax Shvetsov 430f14d02fSMax Shvetsov /******************************************************************************* 440f14d02fSMax Shvetsov * SPM Core entry point information. Discovered on the primary core and reused 450f14d02fSMax Shvetsov * on secondary cores. 460f14d02fSMax Shvetsov ******************************************************************************/ 470f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info; 480f14d02fSMax Shvetsov 490f14d02fSMax Shvetsov /******************************************************************************* 5002d50bb0SOlivier Deprez * SPM Core context on CPU based on mpidr. 5102d50bb0SOlivier Deprez ******************************************************************************/ 5202d50bb0SOlivier Deprez spmd_spm_core_context_t *spmd_get_context_by_mpidr(uint64_t mpidr) 5302d50bb0SOlivier Deprez { 54f7fb0bf7SMax Shvetsov int core_idx = plat_core_pos_by_mpidr(mpidr); 55f7fb0bf7SMax Shvetsov 56f7fb0bf7SMax Shvetsov if (core_idx < 0) { 574ce3e99aSScott Branden ERROR("Invalid mpidr: %" PRIx64 ", returned ID: %d\n", mpidr, core_idx); 58f7fb0bf7SMax Shvetsov panic(); 59f7fb0bf7SMax Shvetsov } 60f7fb0bf7SMax Shvetsov 61f7fb0bf7SMax Shvetsov return &spm_core_context[core_idx]; 6202d50bb0SOlivier Deprez } 6302d50bb0SOlivier Deprez 6402d50bb0SOlivier Deprez /******************************************************************************* 6552696946SOlivier Deprez * SPM Core context on current CPU get helper. 6652696946SOlivier Deprez ******************************************************************************/ 6752696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void) 6852696946SOlivier Deprez { 6902d50bb0SOlivier Deprez return spmd_get_context_by_mpidr(read_mpidr()); 7052696946SOlivier Deprez } 7152696946SOlivier Deprez 7252696946SOlivier Deprez /******************************************************************************* 73a92bc73bSOlivier Deprez * SPM Core ID getter. 74a92bc73bSOlivier Deprez ******************************************************************************/ 75a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void) 76a92bc73bSOlivier Deprez { 77a92bc73bSOlivier Deprez return spmc_attrs.spmc_id; 78a92bc73bSOlivier Deprez } 79a92bc73bSOlivier Deprez 80a92bc73bSOlivier Deprez /******************************************************************************* 810f14d02fSMax Shvetsov * Static function declaration. 820f14d02fSMax Shvetsov ******************************************************************************/ 830f14d02fSMax Shvetsov static int32_t spmd_init(void); 8423d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr); 85662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, 8652696946SOlivier Deprez int error_code); 8752696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 8852696946SOlivier Deprez bool secure_origin, 8952696946SOlivier Deprez uint64_t x1, 9052696946SOlivier Deprez uint64_t x2, 9152696946SOlivier Deprez uint64_t x3, 9252696946SOlivier Deprez uint64_t x4, 93*bb01a673SMarc Bonnici void *cookie, 94*bb01a673SMarc Bonnici void *handle, 95*bb01a673SMarc Bonnici uint64_t flags); 96bdd2596dSAchin Gupta 979944f557SDaniel Boulby /****************************************************************************** 989944f557SDaniel Boulby * Builds an SPMD to SPMC direct message request. 999944f557SDaniel Boulby *****************************************************************************/ 1009944f557SDaniel Boulby void spmd_build_spmc_message(gp_regs_t *gpregs, uint8_t target_func, 1019944f557SDaniel Boulby unsigned long long message) 1029944f557SDaniel Boulby { 1039944f557SDaniel Boulby write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_MSG_SEND_DIRECT_REQ_SMC32); 1049944f557SDaniel Boulby write_ctx_reg(gpregs, CTX_GPREG_X1, 1059944f557SDaniel Boulby (SPMD_DIRECT_MSG_ENDPOINT_ID << FFA_DIRECT_MSG_SOURCE_SHIFT) | 1069944f557SDaniel Boulby spmd_spmc_id_get()); 1079944f557SDaniel Boulby write_ctx_reg(gpregs, CTX_GPREG_X2, BIT(31) | target_func); 1089944f557SDaniel Boulby write_ctx_reg(gpregs, CTX_GPREG_X3, message); 1099944f557SDaniel Boulby } 1109944f557SDaniel Boulby 1119944f557SDaniel Boulby 112bdd2596dSAchin Gupta /******************************************************************************* 11352696946SOlivier Deprez * This function takes an SPMC context pointer and performs a synchronous 11452696946SOlivier Deprez * SPMC entry. 115bdd2596dSAchin Gupta ******************************************************************************/ 116bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx) 117bdd2596dSAchin Gupta { 118bdd2596dSAchin Gupta uint64_t rc; 119bdd2596dSAchin Gupta 120bdd2596dSAchin Gupta assert(spmc_ctx != NULL); 121bdd2596dSAchin Gupta 122bdd2596dSAchin Gupta cm_set_context(&(spmc_ctx->cpu_ctx), SECURE); 123bdd2596dSAchin Gupta 124bdd2596dSAchin Gupta /* Restore the context assigned above */ 125033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 12628f39f02SMax Shvetsov cm_el2_sysregs_context_restore(SECURE); 127678ce223SOlivier Deprez #else 128678ce223SOlivier Deprez cm_el1_sysregs_context_restore(SECURE); 129033039f8SMax Shvetsov #endif 130bdd2596dSAchin Gupta cm_set_next_eret_context(SECURE); 131bdd2596dSAchin Gupta 132033039f8SMax Shvetsov /* Enter SPMC */ 133bdd2596dSAchin Gupta rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx); 134bdd2596dSAchin Gupta 135bdd2596dSAchin Gupta /* Save secure state */ 136033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 13728f39f02SMax Shvetsov cm_el2_sysregs_context_save(SECURE); 138678ce223SOlivier Deprez #else 139678ce223SOlivier Deprez cm_el1_sysregs_context_save(SECURE); 140033039f8SMax Shvetsov #endif 141bdd2596dSAchin Gupta 142bdd2596dSAchin Gupta return rc; 143bdd2596dSAchin Gupta } 144bdd2596dSAchin Gupta 145bdd2596dSAchin Gupta /******************************************************************************* 14652696946SOlivier Deprez * This function returns to the place where spmd_spm_core_sync_entry() was 147bdd2596dSAchin Gupta * called originally. 148bdd2596dSAchin Gupta ******************************************************************************/ 149bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc) 150bdd2596dSAchin Gupta { 15152696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 152bdd2596dSAchin Gupta 15352696946SOlivier Deprez /* Get current CPU context from SPMC context */ 154bdd2596dSAchin Gupta assert(cm_get_context(SECURE) == &(ctx->cpu_ctx)); 155bdd2596dSAchin Gupta 156bdd2596dSAchin Gupta /* 157bdd2596dSAchin Gupta * The SPMD must have initiated the original request through a 158bdd2596dSAchin Gupta * synchronous entry into SPMC. Jump back to the original C runtime 159bdd2596dSAchin Gupta * context with the value of rc in x0; 160bdd2596dSAchin Gupta */ 161bdd2596dSAchin Gupta spmd_spm_core_exit(ctx->c_rt_ctx, rc); 162bdd2596dSAchin Gupta 163bdd2596dSAchin Gupta panic(); 164bdd2596dSAchin Gupta } 165bdd2596dSAchin Gupta 166bdd2596dSAchin Gupta /******************************************************************************* 16752696946SOlivier Deprez * Jump to the SPM Core for the first time. 168bdd2596dSAchin Gupta ******************************************************************************/ 169bdd2596dSAchin Gupta static int32_t spmd_init(void) 170bdd2596dSAchin Gupta { 17152696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 17252696946SOlivier Deprez uint64_t rc; 173bdd2596dSAchin Gupta 17452696946SOlivier Deprez VERBOSE("SPM Core init start.\n"); 1759dcf63ddSOlivier Deprez 176f2dcf418SOlivier Deprez /* Primary boot core enters the SPMC for initialization. */ 177f2dcf418SOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 178bdd2596dSAchin Gupta 179bdd2596dSAchin Gupta rc = spmd_spm_core_sync_entry(ctx); 18052696946SOlivier Deprez if (rc != 0ULL) { 1814ce3e99aSScott Branden ERROR("SPMC initialisation failed 0x%" PRIx64 "\n", rc); 18252696946SOlivier Deprez return 0; 183bdd2596dSAchin Gupta } 184bdd2596dSAchin Gupta 1859dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON; 1869dcf63ddSOlivier Deprez 18752696946SOlivier Deprez VERBOSE("SPM Core init end.\n"); 188bdd2596dSAchin Gupta 189bdd2596dSAchin Gupta return 1; 190bdd2596dSAchin Gupta } 191bdd2596dSAchin Gupta 192bdd2596dSAchin Gupta /******************************************************************************* 1938cb99c3fSOlivier Deprez * spmd_secure_interrupt_handler 1948cb99c3fSOlivier Deprez * Enter the SPMC for further handling of the secure interrupt by the SPMC 1958cb99c3fSOlivier Deprez * itself or a Secure Partition. 1968cb99c3fSOlivier Deprez ******************************************************************************/ 1978cb99c3fSOlivier Deprez static uint64_t spmd_secure_interrupt_handler(uint32_t id, 1988cb99c3fSOlivier Deprez uint32_t flags, 1998cb99c3fSOlivier Deprez void *handle, 2008cb99c3fSOlivier Deprez void *cookie) 2018cb99c3fSOlivier Deprez { 2028cb99c3fSOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 2038cb99c3fSOlivier Deprez gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx); 2048cb99c3fSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 2058cb99c3fSOlivier Deprez int64_t rc; 2068cb99c3fSOlivier Deprez 2078cb99c3fSOlivier Deprez /* Sanity check the security state when the exception was generated */ 2088cb99c3fSOlivier Deprez assert(get_interrupt_src_ss(flags) == NON_SECURE); 2098cb99c3fSOlivier Deprez 2108cb99c3fSOlivier Deprez /* Sanity check the pointer to this cpu's context */ 2118cb99c3fSOlivier Deprez assert(handle == cm_get_context(NON_SECURE)); 2128cb99c3fSOlivier Deprez 2138cb99c3fSOlivier Deprez /* Save the non-secure context before entering SPMC */ 2148cb99c3fSOlivier Deprez cm_el1_sysregs_context_save(NON_SECURE); 2158cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2 2168cb99c3fSOlivier Deprez cm_el2_sysregs_context_save(NON_SECURE); 2178cb99c3fSOlivier Deprez #endif 2188cb99c3fSOlivier Deprez 2198cb99c3fSOlivier Deprez /* Convey the event to the SPMC through the FFA_INTERRUPT interface. */ 2208cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X0, FFA_INTERRUPT); 2218cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X1, 0); 2228cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X2, 0); 2238cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X3, 0); 2248cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X4, 0); 2258cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X5, 0); 2268cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X6, 0); 2278cb99c3fSOlivier Deprez write_ctx_reg(gpregs, CTX_GPREG_X7, 0); 2288cb99c3fSOlivier Deprez 2298cb99c3fSOlivier Deprez /* Mark current core as handling a secure interrupt. */ 2308cb99c3fSOlivier Deprez ctx->secure_interrupt_ongoing = true; 2318cb99c3fSOlivier Deprez 2328cb99c3fSOlivier Deprez rc = spmd_spm_core_sync_entry(ctx); 2338cb99c3fSOlivier Deprez if (rc != 0ULL) { 2340c23e6f4SOlivier Deprez ERROR("%s failed (%" PRId64 ") on CPU%u\n", __func__, rc, linear_id); 2358cb99c3fSOlivier Deprez } 2368cb99c3fSOlivier Deprez 2378cb99c3fSOlivier Deprez ctx->secure_interrupt_ongoing = false; 2388cb99c3fSOlivier Deprez 2398cb99c3fSOlivier Deprez cm_el1_sysregs_context_restore(NON_SECURE); 2408cb99c3fSOlivier Deprez #if SPMD_SPM_AT_SEL2 2418cb99c3fSOlivier Deprez cm_el2_sysregs_context_restore(NON_SECURE); 2428cb99c3fSOlivier Deprez #endif 2438cb99c3fSOlivier Deprez cm_set_next_eret_context(NON_SECURE); 2448cb99c3fSOlivier Deprez 2458cb99c3fSOlivier Deprez SMC_RET0(&ctx->cpu_ctx); 2468cb99c3fSOlivier Deprez } 2478cb99c3fSOlivier Deprez 2488cb99c3fSOlivier Deprez /******************************************************************************* 24952696946SOlivier Deprez * Loads SPMC manifest and inits SPMC. 2500f14d02fSMax Shvetsov ******************************************************************************/ 25123d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr) 2520f14d02fSMax Shvetsov { 253f2dcf418SOlivier Deprez cpu_context_t *cpu_ctx; 254f2dcf418SOlivier Deprez unsigned int core_id; 2558cb99c3fSOlivier Deprez uint32_t ep_attr, flags; 25652696946SOlivier Deprez int rc; 2570f14d02fSMax Shvetsov 25852696946SOlivier Deprez /* Load the SPM Core manifest */ 25923d5ba86SOlivier Deprez rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr); 2600f14d02fSMax Shvetsov if (rc != 0) { 26152696946SOlivier Deprez WARN("No or invalid SPM Core manifest image provided by BL2\n"); 26252696946SOlivier Deprez return rc; 2630f14d02fSMax Shvetsov } 2640f14d02fSMax Shvetsov 2650f14d02fSMax Shvetsov /* 26652696946SOlivier Deprez * Ensure that the SPM Core version is compatible with the SPM 26752696946SOlivier Deprez * Dispatcher version. 2680f14d02fSMax Shvetsov */ 269662af36dSJ-Alves if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) || 270662af36dSJ-Alves (spmc_attrs.minor_version > FFA_VERSION_MINOR)) { 271662af36dSJ-Alves WARN("Unsupported FFA version (%u.%u)\n", 2720f14d02fSMax Shvetsov spmc_attrs.major_version, spmc_attrs.minor_version); 27352696946SOlivier Deprez return -EINVAL; 2740f14d02fSMax Shvetsov } 2750f14d02fSMax Shvetsov 276662af36dSJ-Alves VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version, 2770f14d02fSMax Shvetsov spmc_attrs.minor_version); 2780f14d02fSMax Shvetsov 27952696946SOlivier Deprez VERBOSE("SPM Core run time EL%x.\n", 280033039f8SMax Shvetsov SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1); 2810f14d02fSMax Shvetsov 282ac03ac5eSMax Shvetsov /* Validate the SPMC ID, Ensure high bit is set */ 28352696946SOlivier Deprez if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) & 28452696946SOlivier Deprez SPMC_SECURE_ID_MASK) == 0U) { 28552696946SOlivier Deprez WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id); 28652696946SOlivier Deprez return -EINVAL; 287ac03ac5eSMax Shvetsov } 288ac03ac5eSMax Shvetsov 28952696946SOlivier Deprez /* Validate the SPM Core execution state */ 2900f14d02fSMax Shvetsov if ((spmc_attrs.exec_state != MODE_RW_64) && 2910f14d02fSMax Shvetsov (spmc_attrs.exec_state != MODE_RW_32)) { 29223d5ba86SOlivier Deprez WARN("Unsupported %s%x.\n", "SPM Core execution state 0x", 2930f14d02fSMax Shvetsov spmc_attrs.exec_state); 29452696946SOlivier Deprez return -EINVAL; 2950f14d02fSMax Shvetsov } 2960f14d02fSMax Shvetsov 29723d5ba86SOlivier Deprez VERBOSE("%s%x.\n", "SPM Core execution state 0x", 29823d5ba86SOlivier Deprez spmc_attrs.exec_state); 2990f14d02fSMax Shvetsov 300033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 301033039f8SMax Shvetsov /* Ensure manifest has not requested AArch32 state in S-EL2 */ 302033039f8SMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 303033039f8SMax Shvetsov WARN("AArch32 state at S-EL2 is not supported.\n"); 30452696946SOlivier Deprez return -EINVAL; 3050f14d02fSMax Shvetsov } 3060f14d02fSMax Shvetsov 3070f14d02fSMax Shvetsov /* 3080f14d02fSMax Shvetsov * Check if S-EL2 is supported on this system if S-EL2 3090f14d02fSMax Shvetsov * is required for SPM 3100f14d02fSMax Shvetsov */ 31152696946SOlivier Deprez if (!is_armv8_4_sel2_present()) { 31252696946SOlivier Deprez WARN("SPM Core run time S-EL2 is not supported.\n"); 31352696946SOlivier Deprez return -EINVAL; 3140f14d02fSMax Shvetsov } 315033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */ 3160f14d02fSMax Shvetsov 3170f14d02fSMax Shvetsov /* Initialise an entrypoint to set up the CPU context */ 3180f14d02fSMax Shvetsov ep_attr = SECURE | EP_ST_ENABLE; 31952696946SOlivier Deprez if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) { 3200f14d02fSMax Shvetsov ep_attr |= EP_EE_BIG; 3210f14d02fSMax Shvetsov } 3220f14d02fSMax Shvetsov 3230f14d02fSMax Shvetsov SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr); 3240f14d02fSMax Shvetsov 3250f14d02fSMax Shvetsov /* 32652696946SOlivier Deprez * Populate SPSR for SPM Core based upon validated parameters from the 32752696946SOlivier Deprez * manifest. 3280f14d02fSMax Shvetsov */ 3290f14d02fSMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 3300f14d02fSMax Shvetsov spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 3310f14d02fSMax Shvetsov SPSR_E_LITTLE, 3320f14d02fSMax Shvetsov DAIF_FIQ_BIT | 3330f14d02fSMax Shvetsov DAIF_IRQ_BIT | 3340f14d02fSMax Shvetsov DAIF_ABT_BIT); 3350f14d02fSMax Shvetsov } else { 336033039f8SMax Shvetsov 337033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 338033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL2; 339033039f8SMax Shvetsov #else 340033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL1; 341033039f8SMax Shvetsov #endif 342033039f8SMax Shvetsov spmc_ep_info->spsr = SPSR_64(runtime_el, 3430f14d02fSMax Shvetsov MODE_SP_ELX, 3440f14d02fSMax Shvetsov DISABLE_ALL_EXCEPTIONS); 3450f14d02fSMax Shvetsov } 3460f14d02fSMax Shvetsov 347f2dcf418SOlivier Deprez /* Set an initial SPMC context state for all cores. */ 348f2dcf418SOlivier Deprez for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) { 349f2dcf418SOlivier Deprez spm_core_context[core_id].state = SPMC_STATE_OFF; 3500f14d02fSMax Shvetsov 351f2dcf418SOlivier Deprez /* Setup an initial cpu context for the SPMC. */ 352f2dcf418SOlivier Deprez cpu_ctx = &spm_core_context[core_id].cpu_ctx; 353f2dcf418SOlivier Deprez cm_setup_context(cpu_ctx, spmc_ep_info); 3540f14d02fSMax Shvetsov 355f2dcf418SOlivier Deprez /* 356f2dcf418SOlivier Deprez * Pass the core linear ID to the SPMC through x4. 357f2dcf418SOlivier Deprez * (TF-A implementation defined behavior helping 358f2dcf418SOlivier Deprez * a legacy TOS migration to adopt FF-A). 359f2dcf418SOlivier Deprez */ 360f2dcf418SOlivier Deprez write_ctx_reg(get_gpregs_ctx(cpu_ctx), CTX_GPREG_X4, core_id); 361f2dcf418SOlivier Deprez } 3620f14d02fSMax Shvetsov 363a334c4e6SOlivier Deprez /* Register power management hooks with PSCI */ 364a334c4e6SOlivier Deprez psci_register_spd_pm_hook(&spmd_pm); 365a334c4e6SOlivier Deprez 3660f14d02fSMax Shvetsov /* Register init function for deferred init. */ 3670f14d02fSMax Shvetsov bl31_register_bl32_init(&spmd_init); 3680f14d02fSMax Shvetsov 369f2dcf418SOlivier Deprez INFO("SPM Core setup done.\n"); 370f2dcf418SOlivier Deprez 3718cb99c3fSOlivier Deprez /* 3728cb99c3fSOlivier Deprez * Register an interrupt handler routing secure interrupts to SPMD 3738cb99c3fSOlivier Deprez * while the NWd is running. 3748cb99c3fSOlivier Deprez */ 3758cb99c3fSOlivier Deprez flags = 0; 3768cb99c3fSOlivier Deprez set_interrupt_rm_flag(flags, NON_SECURE); 3778cb99c3fSOlivier Deprez rc = register_interrupt_type_handler(INTR_TYPE_S_EL1, 3788cb99c3fSOlivier Deprez spmd_secure_interrupt_handler, 3798cb99c3fSOlivier Deprez flags); 3808cb99c3fSOlivier Deprez if (rc != 0) { 3818cb99c3fSOlivier Deprez panic(); 3828cb99c3fSOlivier Deprez } 3838cb99c3fSOlivier Deprez 3840f14d02fSMax Shvetsov return 0; 3850f14d02fSMax Shvetsov } 3860f14d02fSMax Shvetsov 3870f14d02fSMax Shvetsov /******************************************************************************* 38852696946SOlivier Deprez * Initialize context of SPM Core. 389bdd2596dSAchin Gupta ******************************************************************************/ 3900f14d02fSMax Shvetsov int spmd_setup(void) 391bdd2596dSAchin Gupta { 392bdd2596dSAchin Gupta int rc; 3936da76075SMarc Bonnici void *spmc_manifest; 3946da76075SMarc Bonnici 3956da76075SMarc Bonnici /* 3966da76075SMarc Bonnici * If the SPMC is at EL3, then just initialise it directly. The 3976da76075SMarc Bonnici * shenanigans of when it is at a lower EL are not needed. 3986da76075SMarc Bonnici */ 3996da76075SMarc Bonnici if (is_spmc_at_el3()) { 4006da76075SMarc Bonnici /* Allow the SPMC to populate its attributes directly. */ 4016da76075SMarc Bonnici spmc_populate_attrs(&spmc_attrs); 4026da76075SMarc Bonnici 4036da76075SMarc Bonnici rc = spmc_setup(); 4046da76075SMarc Bonnici if (rc != 0) { 4056da76075SMarc Bonnici ERROR("SPMC initialisation failed 0x%x.\n", rc); 4066da76075SMarc Bonnici } 4076da76075SMarc Bonnici return rc; 4086da76075SMarc Bonnici } 409bdd2596dSAchin Gupta 410bdd2596dSAchin Gupta spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 41152696946SOlivier Deprez if (spmc_ep_info == NULL) { 41252696946SOlivier Deprez WARN("No SPM Core image provided by BL2 boot loader.\n"); 41352696946SOlivier Deprez return -EINVAL; 414bdd2596dSAchin Gupta } 415bdd2596dSAchin Gupta 416bdd2596dSAchin Gupta /* Under no circumstances will this parameter be 0 */ 41752696946SOlivier Deprez assert(spmc_ep_info->pc != 0ULL); 418bdd2596dSAchin Gupta 419bdd2596dSAchin Gupta /* 420bdd2596dSAchin Gupta * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will 42152696946SOlivier Deprez * be used as a manifest for the SPM Core at the next lower EL/mode. 422bdd2596dSAchin Gupta */ 42323d5ba86SOlivier Deprez spmc_manifest = (void *)spmc_ep_info->args.arg0; 42423d5ba86SOlivier Deprez if (spmc_manifest == NULL) { 42523d5ba86SOlivier Deprez ERROR("Invalid or absent SPM Core manifest.\n"); 42623d5ba86SOlivier Deprez return -EINVAL; 427bdd2596dSAchin Gupta } 428bdd2596dSAchin Gupta 4290f14d02fSMax Shvetsov /* Load manifest, init SPMC */ 43023d5ba86SOlivier Deprez rc = spmd_spmc_init(spmc_manifest); 4310f14d02fSMax Shvetsov if (rc != 0) { 43252696946SOlivier Deprez WARN("Booting device without SPM initialization.\n"); 433bdd2596dSAchin Gupta } 434bdd2596dSAchin Gupta 4350f14d02fSMax Shvetsov return rc; 4360f14d02fSMax Shvetsov } 4370f14d02fSMax Shvetsov 4380f14d02fSMax Shvetsov /******************************************************************************* 439*bb01a673SMarc Bonnici * Forward FF-A SMCs to the other security state. 4400f14d02fSMax Shvetsov ******************************************************************************/ 441*bb01a673SMarc Bonnici uint64_t spmd_smc_switch_state(uint32_t smc_fid, 44252696946SOlivier Deprez bool secure_origin, 44352696946SOlivier Deprez uint64_t x1, 44452696946SOlivier Deprez uint64_t x2, 44552696946SOlivier Deprez uint64_t x3, 44652696946SOlivier Deprez uint64_t x4, 44752696946SOlivier Deprez void *handle) 4480f14d02fSMax Shvetsov { 449c2901419SOlivier Deprez unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE; 450c2901419SOlivier Deprez unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE; 45193ff138bSOlivier Deprez 4520f14d02fSMax Shvetsov /* Save incoming security state */ 453033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 454678ce223SOlivier Deprez if (secure_state_in == NON_SECURE) { 455678ce223SOlivier Deprez cm_el1_sysregs_context_save(secure_state_in); 456678ce223SOlivier Deprez } 45793ff138bSOlivier Deprez cm_el2_sysregs_context_save(secure_state_in); 458678ce223SOlivier Deprez #else 459678ce223SOlivier Deprez cm_el1_sysregs_context_save(secure_state_in); 460033039f8SMax Shvetsov #endif 4610f14d02fSMax Shvetsov 4620f14d02fSMax Shvetsov /* Restore outgoing security state */ 463033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 464678ce223SOlivier Deprez if (secure_state_out == NON_SECURE) { 465678ce223SOlivier Deprez cm_el1_sysregs_context_restore(secure_state_out); 466678ce223SOlivier Deprez } 46793ff138bSOlivier Deprez cm_el2_sysregs_context_restore(secure_state_out); 468678ce223SOlivier Deprez #else 469678ce223SOlivier Deprez cm_el1_sysregs_context_restore(secure_state_out); 470033039f8SMax Shvetsov #endif 47193ff138bSOlivier Deprez cm_set_next_eret_context(secure_state_out); 4720f14d02fSMax Shvetsov 47393ff138bSOlivier Deprez SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4, 4740f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X5), 4750f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X6), 4760f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X7)); 4770f14d02fSMax Shvetsov } 4780f14d02fSMax Shvetsov 4790f14d02fSMax Shvetsov /******************************************************************************* 480*bb01a673SMarc Bonnici * Forward SMCs to the other security state. 481*bb01a673SMarc Bonnici ******************************************************************************/ 482*bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid, 483*bb01a673SMarc Bonnici bool secure_origin, 484*bb01a673SMarc Bonnici uint64_t x1, 485*bb01a673SMarc Bonnici uint64_t x2, 486*bb01a673SMarc Bonnici uint64_t x3, 487*bb01a673SMarc Bonnici uint64_t x4, 488*bb01a673SMarc Bonnici void *cookie, 489*bb01a673SMarc Bonnici void *handle, 490*bb01a673SMarc Bonnici uint64_t flags) 491*bb01a673SMarc Bonnici { 492*bb01a673SMarc Bonnici if (is_spmc_at_el3() && !secure_origin) { 493*bb01a673SMarc Bonnici return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4, 494*bb01a673SMarc Bonnici cookie, handle, flags); 495*bb01a673SMarc Bonnici } 496*bb01a673SMarc Bonnici return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4, 497*bb01a673SMarc Bonnici handle); 498*bb01a673SMarc Bonnici 499*bb01a673SMarc Bonnici } 500*bb01a673SMarc Bonnici 501*bb01a673SMarc Bonnici /******************************************************************************* 502662af36dSJ-Alves * Return FFA_ERROR with specified error code 5030f14d02fSMax Shvetsov ******************************************************************************/ 504662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code) 5050f14d02fSMax Shvetsov { 506e46b2fd2SJ-Alves SMC_RET8(handle, (uint32_t) FFA_ERROR, 507e46b2fd2SJ-Alves FFA_TARGET_INFO_MBZ, (uint32_t)error_code, 508662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 509662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 510bdd2596dSAchin Gupta } 511bdd2596dSAchin Gupta 512f0d743dbSOlivier Deprez /******************************************************************************* 513f0d743dbSOlivier Deprez * spmd_check_address_in_binary_image 514f0d743dbSOlivier Deprez ******************************************************************************/ 515f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address) 516f0d743dbSOlivier Deprez { 517f0d743dbSOlivier Deprez assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size)); 518f0d743dbSOlivier Deprez 519f0d743dbSOlivier Deprez return ((address >= spmc_attrs.load_address) && 520f0d743dbSOlivier Deprez (address < (spmc_attrs.load_address + spmc_attrs.binary_size))); 521f0d743dbSOlivier Deprez } 522f0d743dbSOlivier Deprez 523c2901419SOlivier Deprez /****************************************************************************** 524c2901419SOlivier Deprez * spmd_is_spmc_message 525c2901419SOlivier Deprez *****************************************************************************/ 526c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep) 527c2901419SOlivier Deprez { 528*bb01a673SMarc Bonnici if (is_spmc_at_el3()) { 529*bb01a673SMarc Bonnici return false; 530*bb01a673SMarc Bonnici } 531*bb01a673SMarc Bonnici 532c2901419SOlivier Deprez return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID) 533c2901419SOlivier Deprez && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id)); 534c2901419SOlivier Deprez } 535c2901419SOlivier Deprez 536f0d743dbSOlivier Deprez /****************************************************************************** 537f0d743dbSOlivier Deprez * spmd_handle_spmc_message 538f0d743dbSOlivier Deprez *****************************************************************************/ 539a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg, 540a92bc73bSOlivier Deprez unsigned long long parm1, unsigned long long parm2, 541a92bc73bSOlivier Deprez unsigned long long parm3, unsigned long long parm4) 542f0d743dbSOlivier Deprez { 543f0d743dbSOlivier Deprez VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__, 544f0d743dbSOlivier Deprez msg, parm1, parm2, parm3, parm4); 545f0d743dbSOlivier Deprez 546f0d743dbSOlivier Deprez return -EINVAL; 547f0d743dbSOlivier Deprez } 548f0d743dbSOlivier Deprez 549bdd2596dSAchin Gupta /******************************************************************************* 550*bb01a673SMarc Bonnici * This function forwards FF-A SMCs to either the main SPMD handler or the 551*bb01a673SMarc Bonnici * SPMC at EL3, depending on the origin security state, if enabled. 552*bb01a673SMarc Bonnici ******************************************************************************/ 553*bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid, 554*bb01a673SMarc Bonnici uint64_t x1, 555*bb01a673SMarc Bonnici uint64_t x2, 556*bb01a673SMarc Bonnici uint64_t x3, 557*bb01a673SMarc Bonnici uint64_t x4, 558*bb01a673SMarc Bonnici void *cookie, 559*bb01a673SMarc Bonnici void *handle, 560*bb01a673SMarc Bonnici uint64_t flags) 561*bb01a673SMarc Bonnici { 562*bb01a673SMarc Bonnici if (is_spmc_at_el3()) { 563*bb01a673SMarc Bonnici /* 564*bb01a673SMarc Bonnici * If we have an SPMC at EL3 allow handling of the SMC first. 565*bb01a673SMarc Bonnici * The SPMC will call back through to SPMD handler if required. 566*bb01a673SMarc Bonnici */ 567*bb01a673SMarc Bonnici if (is_caller_secure(flags)) { 568*bb01a673SMarc Bonnici return spmc_smc_handler(smc_fid, 569*bb01a673SMarc Bonnici is_caller_secure(flags), 570*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 571*bb01a673SMarc Bonnici handle, flags); 572*bb01a673SMarc Bonnici } 573*bb01a673SMarc Bonnici } 574*bb01a673SMarc Bonnici return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 575*bb01a673SMarc Bonnici handle, flags); 576*bb01a673SMarc Bonnici } 577*bb01a673SMarc Bonnici 578*bb01a673SMarc Bonnici /******************************************************************************* 579662af36dSJ-Alves * This function handles all SMCs in the range reserved for FFA. Each call is 580bdd2596dSAchin Gupta * either forwarded to the other security state or handled by the SPM dispatcher 581bdd2596dSAchin Gupta ******************************************************************************/ 58252696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid, 58352696946SOlivier Deprez uint64_t x1, 58452696946SOlivier Deprez uint64_t x2, 58552696946SOlivier Deprez uint64_t x3, 58652696946SOlivier Deprez uint64_t x4, 58752696946SOlivier Deprez void *cookie, 58852696946SOlivier Deprez void *handle, 589bdd2596dSAchin Gupta uint64_t flags) 590bdd2596dSAchin Gupta { 591cdb49d47SOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 59252696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 59393ff138bSOlivier Deprez bool secure_origin; 59493ff138bSOlivier Deprez int32_t ret; 5954388f28fSJ-Alves uint32_t input_version; 596bdd2596dSAchin Gupta 597bdd2596dSAchin Gupta /* Determine which security state this SMC originated from */ 59893ff138bSOlivier Deprez secure_origin = is_caller_secure(flags); 599bdd2596dSAchin Gupta 6004ce3e99aSScott Branden VERBOSE("SPM(%u): 0x%x 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 6014ce3e99aSScott Branden " 0x%" PRIx64 " 0x%" PRIx64 " 0x%" PRIx64 "\n", 602cdb49d47SOlivier Deprez linear_id, smc_fid, x1, x2, x3, x4, 603cdb49d47SOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 604bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 605bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 606bdd2596dSAchin Gupta 607bdd2596dSAchin Gupta switch (smc_fid) { 608662af36dSJ-Alves case FFA_ERROR: 609bdd2596dSAchin Gupta /* 610bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 61152696946SOlivier Deprez * this CPU. If so, then indicate that the SPM Core initialised 612bdd2596dSAchin Gupta * unsuccessfully. 613bdd2596dSAchin Gupta */ 6149dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 615bdd2596dSAchin Gupta spmd_spm_core_sync_exit(x2); 6160f14d02fSMax Shvetsov } 617bdd2596dSAchin Gupta 61893ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 619*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 620*bb01a673SMarc Bonnici handle, flags); 621bdd2596dSAchin Gupta break; /* not reached */ 622bdd2596dSAchin Gupta 623662af36dSJ-Alves case FFA_VERSION: 6244388f28fSJ-Alves input_version = (uint32_t)(0xFFFFFFFF & x1); 625bdd2596dSAchin Gupta /* 6264388f28fSJ-Alves * If caller is secure and SPMC was initialized, 6274388f28fSJ-Alves * return FFA_VERSION of SPMD. 6284388f28fSJ-Alves * If caller is non secure and SPMC was initialized, 6294388f28fSJ-Alves * return SPMC's version. 6304388f28fSJ-Alves * Sanity check to "input_version". 631*bb01a673SMarc Bonnici * If the EL3 SPMC is enabled, ignore the SPMC state as 632*bb01a673SMarc Bonnici * this is not used. 633bdd2596dSAchin Gupta */ 6344388f28fSJ-Alves if ((input_version & FFA_VERSION_BIT31_MASK) || 635*bb01a673SMarc Bonnici (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) { 6364388f28fSJ-Alves ret = FFA_ERROR_NOT_SUPPORTED; 6374388f28fSJ-Alves } else if (!secure_origin) { 6389944f557SDaniel Boulby gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx); 6399944f557SDaniel Boulby uint64_t rc; 6409944f557SDaniel Boulby 6419944f557SDaniel Boulby if (spmc_attrs.major_version == 1 && 6429944f557SDaniel Boulby spmc_attrs.minor_version == 0) { 643e46b2fd2SJ-Alves ret = MAKE_FFA_VERSION(spmc_attrs.major_version, 644e46b2fd2SJ-Alves spmc_attrs.minor_version); 6459944f557SDaniel Boulby SMC_RET8(handle, (uint32_t)ret, 6469944f557SDaniel Boulby FFA_TARGET_INFO_MBZ, 6479944f557SDaniel Boulby FFA_TARGET_INFO_MBZ, 6489944f557SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 6499944f557SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 6509944f557SDaniel Boulby FFA_PARAM_MBZ); 6519944f557SDaniel Boulby break; 6529944f557SDaniel Boulby } 6539944f557SDaniel Boulby /* Save non-secure system registers context */ 6549944f557SDaniel Boulby cm_el1_sysregs_context_save(NON_SECURE); 6559944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2 6569944f557SDaniel Boulby cm_el2_sysregs_context_save(NON_SECURE); 6579944f557SDaniel Boulby #endif 6589944f557SDaniel Boulby 6599944f557SDaniel Boulby /* 6609944f557SDaniel Boulby * The incoming request has FFA_VERSION as X0 smc_fid 6619944f557SDaniel Boulby * and requested version in x1. Prepare a direct request 6629944f557SDaniel Boulby * from SPMD to SPMC with FFA_VERSION framework function 6639944f557SDaniel Boulby * identifier in X2 and requested version in X3. 6649944f557SDaniel Boulby */ 6659944f557SDaniel Boulby spmd_build_spmc_message(gpregs, 6669944f557SDaniel Boulby SPMD_FWK_MSG_FFA_VERSION_REQ, 6679944f557SDaniel Boulby input_version); 6689944f557SDaniel Boulby 6699944f557SDaniel Boulby rc = spmd_spm_core_sync_entry(ctx); 6709944f557SDaniel Boulby 6719944f557SDaniel Boulby if ((rc != 0ULL) || 6729944f557SDaniel Boulby (SMC_GET_GP(gpregs, CTX_GPREG_X0) != 6739944f557SDaniel Boulby FFA_MSG_SEND_DIRECT_RESP_SMC32) || 6749944f557SDaniel Boulby (SMC_GET_GP(gpregs, CTX_GPREG_X2) != 6759944f557SDaniel Boulby (SPMD_FWK_MSG_BIT | 6769944f557SDaniel Boulby SPMD_FWK_MSG_FFA_VERSION_RESP))) { 6779944f557SDaniel Boulby ERROR("Failed to forward FFA_VERSION\n"); 6789944f557SDaniel Boulby ret = FFA_ERROR_NOT_SUPPORTED; 6799944f557SDaniel Boulby } else { 6809944f557SDaniel Boulby ret = SMC_GET_GP(gpregs, CTX_GPREG_X3); 6819944f557SDaniel Boulby } 6829944f557SDaniel Boulby 6839944f557SDaniel Boulby /* 6849944f557SDaniel Boulby * Return here after SPMC has handled FFA_VERSION. 6859944f557SDaniel Boulby * The returned SPMC version is held in X3. 6869944f557SDaniel Boulby * Forward this version in X0 to the non-secure caller. 6879944f557SDaniel Boulby */ 6889944f557SDaniel Boulby return spmd_smc_forward(ret, true, FFA_PARAM_MBZ, 6899944f557SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 690*bb01a673SMarc Bonnici FFA_PARAM_MBZ, cookie, gpregs, 691*bb01a673SMarc Bonnici flags); 6924388f28fSJ-Alves } else { 693e46b2fd2SJ-Alves ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, 694e46b2fd2SJ-Alves FFA_VERSION_MINOR); 6954388f28fSJ-Alves } 6964388f28fSJ-Alves 697e46b2fd2SJ-Alves SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ, 698e46b2fd2SJ-Alves FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 699e46b2fd2SJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ); 700bdd2596dSAchin Gupta break; /* not reached */ 701bdd2596dSAchin Gupta 702662af36dSJ-Alves case FFA_FEATURES: 703bdd2596dSAchin Gupta /* 704bdd2596dSAchin Gupta * This is an optional interface. Do the minimal checks and 70552696946SOlivier Deprez * forward to SPM Core which will handle it if implemented. 706bdd2596dSAchin Gupta */ 707bdd2596dSAchin Gupta 70852696946SOlivier Deprez /* Forward SMC from Normal world to the SPM Core */ 70993ff138bSOlivier Deprez if (!secure_origin) { 71093ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 711*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 712*bb01a673SMarc Bonnici handle, flags); 71352696946SOlivier Deprez } 71452696946SOlivier Deprez 715bdd2596dSAchin Gupta /* 716bdd2596dSAchin Gupta * Return success if call was from secure world i.e. all 717662af36dSJ-Alves * FFA functions are supported. This is essentially a 718bdd2596dSAchin Gupta * nop. 719bdd2596dSAchin Gupta */ 720662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4, 721bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X5), 722bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 723bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 7240f14d02fSMax Shvetsov 725bdd2596dSAchin Gupta break; /* not reached */ 726bdd2596dSAchin Gupta 727662af36dSJ-Alves case FFA_ID_GET: 728ac03ac5eSMax Shvetsov /* 729662af36dSJ-Alves * Returns the ID of the calling FFA component. 730ac03ac5eSMax Shvetsov */ 731ac03ac5eSMax Shvetsov if (!secure_origin) { 732662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 733662af36dSJ-Alves FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID, 734662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 735662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 736662af36dSJ-Alves FFA_PARAM_MBZ); 73752696946SOlivier Deprez } 73852696946SOlivier Deprez 739662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 740662af36dSJ-Alves FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 741662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 742662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 743662af36dSJ-Alves FFA_PARAM_MBZ); 744ac03ac5eSMax Shvetsov 745ac03ac5eSMax Shvetsov break; /* not reached */ 746ac03ac5eSMax Shvetsov 747cdb49d47SOlivier Deprez case FFA_SECONDARY_EP_REGISTER_SMC64: 748cdb49d47SOlivier Deprez if (secure_origin) { 749cdb49d47SOlivier Deprez ret = spmd_pm_secondary_ep_register(x1); 750cdb49d47SOlivier Deprez 751cdb49d47SOlivier Deprez if (ret < 0) { 752cdb49d47SOlivier Deprez SMC_RET8(handle, FFA_ERROR_SMC64, 753cdb49d47SOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 754cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 755cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 756cdb49d47SOlivier Deprez FFA_PARAM_MBZ); 757cdb49d47SOlivier Deprez } else { 758cdb49d47SOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC64, 759cdb49d47SOlivier Deprez FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, 760cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 761cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 762cdb49d47SOlivier Deprez FFA_PARAM_MBZ); 763cdb49d47SOlivier Deprez } 764cdb49d47SOlivier Deprez } 765cdb49d47SOlivier Deprez 766cdb49d47SOlivier Deprez return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 767cdb49d47SOlivier Deprez break; /* Not reached */ 768cdb49d47SOlivier Deprez 76970c121a2SDaniel Boulby case FFA_SPM_ID_GET: 77070c121a2SDaniel Boulby if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) { 77170c121a2SDaniel Boulby return spmd_ffa_error_return(handle, 77270c121a2SDaniel Boulby FFA_ERROR_NOT_SUPPORTED); 77370c121a2SDaniel Boulby } 77470c121a2SDaniel Boulby /* 77570c121a2SDaniel Boulby * Returns the ID of the SPMC or SPMD depending on the FF-A 77670c121a2SDaniel Boulby * instance where this function is invoked 77770c121a2SDaniel Boulby */ 77870c121a2SDaniel Boulby if (!secure_origin) { 77970c121a2SDaniel Boulby SMC_RET8(handle, FFA_SUCCESS_SMC32, 78070c121a2SDaniel Boulby FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 78170c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 78270c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 78370c121a2SDaniel Boulby FFA_PARAM_MBZ); 78470c121a2SDaniel Boulby } 78570c121a2SDaniel Boulby SMC_RET8(handle, FFA_SUCCESS_SMC32, 78670c121a2SDaniel Boulby FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID, 78770c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 78870c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 78970c121a2SDaniel Boulby FFA_PARAM_MBZ); 79070c121a2SDaniel Boulby 79170c121a2SDaniel Boulby break; /* not reached */ 79270c121a2SDaniel Boulby 793f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_REQ_SMC32: 794f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 795f0d743dbSOlivier Deprez ret = spmd_handle_spmc_message(x3, x4, 796f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 797f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X6), 798f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X7)); 799f0d743dbSOlivier Deprez 800f0d743dbSOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC32, 801f0d743dbSOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 802f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 803f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 804f0d743dbSOlivier Deprez FFA_PARAM_MBZ); 805f0d743dbSOlivier Deprez } else { 806f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 807f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 808*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 809*bb01a673SMarc Bonnici handle, flags); 810f0d743dbSOlivier Deprez } 811f0d743dbSOlivier Deprez break; /* Not reached */ 812f0d743dbSOlivier Deprez 813f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_RESP_SMC32: 814f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 8158cb99c3fSOlivier Deprez spmd_spm_core_sync_exit(0ULL); 816f0d743dbSOlivier Deprez } else { 817f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 818f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 819*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 820*bb01a673SMarc Bonnici handle, flags); 821f0d743dbSOlivier Deprez } 822f0d743dbSOlivier Deprez break; /* Not reached */ 823f0d743dbSOlivier Deprez 824662af36dSJ-Alves case FFA_RX_RELEASE: 825662af36dSJ-Alves case FFA_RXTX_MAP_SMC32: 826662af36dSJ-Alves case FFA_RXTX_MAP_SMC64: 827662af36dSJ-Alves case FFA_RXTX_UNMAP: 828545b8eb3SRuari Phipps case FFA_PARTITION_INFO_GET: 829fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED 830fc3f4800SJ-Alves case FFA_NOTIFICATION_BITMAP_CREATE: 831fc3f4800SJ-Alves case FFA_NOTIFICATION_BITMAP_DESTROY: 832fc3f4800SJ-Alves case FFA_NOTIFICATION_BIND: 833fc3f4800SJ-Alves case FFA_NOTIFICATION_UNBIND: 834fc3f4800SJ-Alves case FFA_NOTIFICATION_SET: 835fc3f4800SJ-Alves case FFA_NOTIFICATION_GET: 836fc3f4800SJ-Alves case FFA_NOTIFICATION_INFO_GET: 837fc3f4800SJ-Alves case FFA_NOTIFICATION_INFO_GET_SMC64: 838c2eba07cSFederico Recanati case FFA_MSG_SEND2: 839fc3f4800SJ-Alves #endif 840662af36dSJ-Alves case FFA_MSG_RUN: 841c2eba07cSFederico Recanati /* 842c2eba07cSFederico Recanati * Above calls should be invoked only by the Normal world and 843c2eba07cSFederico Recanati * must not be forwarded from Secure world to Normal world. 844c2eba07cSFederico Recanati */ 84593ff138bSOlivier Deprez if (secure_origin) { 846662af36dSJ-Alves return spmd_ffa_error_return(handle, 847662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 848bdd2596dSAchin Gupta } 849bdd2596dSAchin Gupta 850bdd2596dSAchin Gupta /* Fall through to forward the call to the other world */ 851662af36dSJ-Alves case FFA_MSG_SEND: 852662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_REQ_SMC64: 853662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_RESP_SMC64: 854662af36dSJ-Alves case FFA_MEM_DONATE_SMC32: 855662af36dSJ-Alves case FFA_MEM_DONATE_SMC64: 856662af36dSJ-Alves case FFA_MEM_LEND_SMC32: 857662af36dSJ-Alves case FFA_MEM_LEND_SMC64: 858662af36dSJ-Alves case FFA_MEM_SHARE_SMC32: 859662af36dSJ-Alves case FFA_MEM_SHARE_SMC64: 860662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC32: 861662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC64: 862662af36dSJ-Alves case FFA_MEM_RETRIEVE_RESP: 863662af36dSJ-Alves case FFA_MEM_RELINQUISH: 864662af36dSJ-Alves case FFA_MEM_RECLAIM: 865662af36dSJ-Alves case FFA_SUCCESS_SMC32: 866662af36dSJ-Alves case FFA_SUCCESS_SMC64: 867bdd2596dSAchin Gupta /* 868bdd2596dSAchin Gupta * TODO: Assume that no requests originate from EL3 at the 869bdd2596dSAchin Gupta * moment. This will change if a SP service is required in 870bdd2596dSAchin Gupta * response to secure interrupts targeted to EL3. Until then 871bdd2596dSAchin Gupta * simply forward the call to the Normal world. 872bdd2596dSAchin Gupta */ 873bdd2596dSAchin Gupta 87493ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 875*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 876*bb01a673SMarc Bonnici handle, flags); 877bdd2596dSAchin Gupta break; /* not reached */ 878bdd2596dSAchin Gupta 879662af36dSJ-Alves case FFA_MSG_WAIT: 880bdd2596dSAchin Gupta /* 881bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 882bdd2596dSAchin Gupta * this CPU from the Secure world. If so, then indicate that the 88352696946SOlivier Deprez * SPM Core initialised successfully. 884bdd2596dSAchin Gupta */ 8859dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 8868cb99c3fSOlivier Deprez spmd_spm_core_sync_exit(0ULL); 887bdd2596dSAchin Gupta } 888bdd2596dSAchin Gupta 8890f14d02fSMax Shvetsov /* Fall through to forward the call to the other world */ 890386dc365SOlivier Deprez case FFA_INTERRUPT: 891662af36dSJ-Alves case FFA_MSG_YIELD: 892bdd2596dSAchin Gupta /* This interface must be invoked only by the Secure world */ 89393ff138bSOlivier Deprez if (!secure_origin) { 894662af36dSJ-Alves return spmd_ffa_error_return(handle, 895662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 896bdd2596dSAchin Gupta } 897bdd2596dSAchin Gupta 89893ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 899*bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 900*bb01a673SMarc Bonnici handle, flags); 901bdd2596dSAchin Gupta break; /* not reached */ 902bdd2596dSAchin Gupta 9038cb99c3fSOlivier Deprez case FFA_NORMAL_WORLD_RESUME: 9048cb99c3fSOlivier Deprez if (secure_origin && ctx->secure_interrupt_ongoing) { 9058cb99c3fSOlivier Deprez spmd_spm_core_sync_exit(0ULL); 9068cb99c3fSOlivier Deprez } else { 9078cb99c3fSOlivier Deprez return spmd_ffa_error_return(handle, FFA_ERROR_DENIED); 9088cb99c3fSOlivier Deprez } 9098cb99c3fSOlivier Deprez break; /* Not reached */ 9108cb99c3fSOlivier Deprez 911bdd2596dSAchin Gupta default: 912bdd2596dSAchin Gupta WARN("SPM: Unsupported call 0x%08x\n", smc_fid); 913662af36dSJ-Alves return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 914bdd2596dSAchin Gupta } 915bdd2596dSAchin Gupta } 916