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, 93bb01a673SMarc Bonnici void *cookie, 94bb01a673SMarc Bonnici void *handle, 95bb01a673SMarc 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 /******************************************************************************* 439bb01a673SMarc Bonnici * Forward FF-A SMCs to the other security state. 4400f14d02fSMax Shvetsov ******************************************************************************/ 441bb01a673SMarc 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 /******************************************************************************* 480bb01a673SMarc Bonnici * Forward SMCs to the other security state. 481bb01a673SMarc Bonnici ******************************************************************************/ 482bb01a673SMarc Bonnici static uint64_t spmd_smc_forward(uint32_t smc_fid, 483bb01a673SMarc Bonnici bool secure_origin, 484bb01a673SMarc Bonnici uint64_t x1, 485bb01a673SMarc Bonnici uint64_t x2, 486bb01a673SMarc Bonnici uint64_t x3, 487bb01a673SMarc Bonnici uint64_t x4, 488bb01a673SMarc Bonnici void *cookie, 489bb01a673SMarc Bonnici void *handle, 490bb01a673SMarc Bonnici uint64_t flags) 491bb01a673SMarc Bonnici { 492bb01a673SMarc Bonnici if (is_spmc_at_el3() && !secure_origin) { 493bb01a673SMarc Bonnici return spmc_smc_handler(smc_fid, secure_origin, x1, x2, x3, x4, 494bb01a673SMarc Bonnici cookie, handle, flags); 495bb01a673SMarc Bonnici } 496bb01a673SMarc Bonnici return spmd_smc_switch_state(smc_fid, secure_origin, x1, x2, x3, x4, 497bb01a673SMarc Bonnici handle); 498bb01a673SMarc Bonnici 499bb01a673SMarc Bonnici } 500bb01a673SMarc Bonnici 501bb01a673SMarc 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 { 528bb01a673SMarc Bonnici if (is_spmc_at_el3()) { 529bb01a673SMarc Bonnici return false; 530bb01a673SMarc Bonnici } 531bb01a673SMarc 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 /******************************************************************************* 550bb01a673SMarc Bonnici * This function forwards FF-A SMCs to either the main SPMD handler or the 551bb01a673SMarc Bonnici * SPMC at EL3, depending on the origin security state, if enabled. 552bb01a673SMarc Bonnici ******************************************************************************/ 553bb01a673SMarc Bonnici uint64_t spmd_ffa_smc_handler(uint32_t smc_fid, 554bb01a673SMarc Bonnici uint64_t x1, 555bb01a673SMarc Bonnici uint64_t x2, 556bb01a673SMarc Bonnici uint64_t x3, 557bb01a673SMarc Bonnici uint64_t x4, 558bb01a673SMarc Bonnici void *cookie, 559bb01a673SMarc Bonnici void *handle, 560bb01a673SMarc Bonnici uint64_t flags) 561bb01a673SMarc Bonnici { 562bb01a673SMarc Bonnici if (is_spmc_at_el3()) { 563bb01a673SMarc Bonnici /* 564bb01a673SMarc Bonnici * If we have an SPMC at EL3 allow handling of the SMC first. 565bb01a673SMarc Bonnici * The SPMC will call back through to SPMD handler if required. 566bb01a673SMarc Bonnici */ 567bb01a673SMarc Bonnici if (is_caller_secure(flags)) { 568bb01a673SMarc Bonnici return spmc_smc_handler(smc_fid, 569bb01a673SMarc Bonnici is_caller_secure(flags), 570bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 571bb01a673SMarc Bonnici handle, flags); 572bb01a673SMarc Bonnici } 573bb01a673SMarc Bonnici } 574bb01a673SMarc Bonnici return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 575bb01a673SMarc Bonnici handle, flags); 576bb01a673SMarc Bonnici } 577bb01a673SMarc Bonnici 578bb01a673SMarc 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, 619bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 620bb01a673SMarc 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, 6299576fa93SMarc Bonnici * forward to the EL3 SPMC if enabled, otherwise return 6309576fa93SMarc Bonnici * the SPMC version if implemented at a lower EL. 6314388f28fSJ-Alves * Sanity check to "input_version". 632bb01a673SMarc Bonnici * If the EL3 SPMC is enabled, ignore the SPMC state as 633bb01a673SMarc Bonnici * this is not used. 634bdd2596dSAchin Gupta */ 6354388f28fSJ-Alves if ((input_version & FFA_VERSION_BIT31_MASK) || 636bb01a673SMarc Bonnici (!is_spmc_at_el3() && (ctx->state == SPMC_STATE_RESET))) { 6374388f28fSJ-Alves ret = FFA_ERROR_NOT_SUPPORTED; 6384388f28fSJ-Alves } else if (!secure_origin) { 6399576fa93SMarc Bonnici if (is_spmc_at_el3()) { 6409576fa93SMarc Bonnici /* 6419576fa93SMarc Bonnici * Forward the call directly to the EL3 SPMC, if 6429576fa93SMarc Bonnici * enabled, as we don't need to wrap the call in 6439576fa93SMarc Bonnici * a direct request. 6449576fa93SMarc Bonnici */ 6459576fa93SMarc Bonnici return spmd_smc_forward(smc_fid, secure_origin, 6469576fa93SMarc Bonnici x1, x2, x3, x4, cookie, 6479576fa93SMarc Bonnici handle, flags); 6489576fa93SMarc Bonnici } 6499576fa93SMarc Bonnici 6509944f557SDaniel Boulby gp_regs_t *gpregs = get_gpregs_ctx(&ctx->cpu_ctx); 6519944f557SDaniel Boulby uint64_t rc; 6529944f557SDaniel Boulby 6539944f557SDaniel Boulby if (spmc_attrs.major_version == 1 && 6549944f557SDaniel Boulby spmc_attrs.minor_version == 0) { 655e46b2fd2SJ-Alves ret = MAKE_FFA_VERSION(spmc_attrs.major_version, 656e46b2fd2SJ-Alves spmc_attrs.minor_version); 6579944f557SDaniel Boulby SMC_RET8(handle, (uint32_t)ret, 6589944f557SDaniel Boulby FFA_TARGET_INFO_MBZ, 6599944f557SDaniel Boulby FFA_TARGET_INFO_MBZ, 6609944f557SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 6619944f557SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 6629944f557SDaniel Boulby FFA_PARAM_MBZ); 6639944f557SDaniel Boulby break; 6649944f557SDaniel Boulby } 6659944f557SDaniel Boulby /* Save non-secure system registers context */ 6669944f557SDaniel Boulby cm_el1_sysregs_context_save(NON_SECURE); 6679944f557SDaniel Boulby #if SPMD_SPM_AT_SEL2 6689944f557SDaniel Boulby cm_el2_sysregs_context_save(NON_SECURE); 6699944f557SDaniel Boulby #endif 6709944f557SDaniel Boulby 6719944f557SDaniel Boulby /* 6729944f557SDaniel Boulby * The incoming request has FFA_VERSION as X0 smc_fid 6739944f557SDaniel Boulby * and requested version in x1. Prepare a direct request 6749944f557SDaniel Boulby * from SPMD to SPMC with FFA_VERSION framework function 6759944f557SDaniel Boulby * identifier in X2 and requested version in X3. 6769944f557SDaniel Boulby */ 6779944f557SDaniel Boulby spmd_build_spmc_message(gpregs, 6789944f557SDaniel Boulby SPMD_FWK_MSG_FFA_VERSION_REQ, 6799944f557SDaniel Boulby input_version); 6809944f557SDaniel Boulby 6819944f557SDaniel Boulby rc = spmd_spm_core_sync_entry(ctx); 6829944f557SDaniel Boulby 6839944f557SDaniel Boulby if ((rc != 0ULL) || 6849944f557SDaniel Boulby (SMC_GET_GP(gpregs, CTX_GPREG_X0) != 6859944f557SDaniel Boulby FFA_MSG_SEND_DIRECT_RESP_SMC32) || 6869944f557SDaniel Boulby (SMC_GET_GP(gpregs, CTX_GPREG_X2) != 68759bd2ad8SMarc Bonnici (FFA_FWK_MSG_BIT | 6889944f557SDaniel Boulby SPMD_FWK_MSG_FFA_VERSION_RESP))) { 6899944f557SDaniel Boulby ERROR("Failed to forward FFA_VERSION\n"); 6909944f557SDaniel Boulby ret = FFA_ERROR_NOT_SUPPORTED; 6919944f557SDaniel Boulby } else { 6929944f557SDaniel Boulby ret = SMC_GET_GP(gpregs, CTX_GPREG_X3); 6939944f557SDaniel Boulby } 6949944f557SDaniel Boulby 6959944f557SDaniel Boulby /* 6969944f557SDaniel Boulby * Return here after SPMC has handled FFA_VERSION. 6979944f557SDaniel Boulby * The returned SPMC version is held in X3. 6989944f557SDaniel Boulby * Forward this version in X0 to the non-secure caller. 6999944f557SDaniel Boulby */ 7009944f557SDaniel Boulby return spmd_smc_forward(ret, true, FFA_PARAM_MBZ, 7019944f557SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 702bb01a673SMarc Bonnici FFA_PARAM_MBZ, cookie, gpregs, 703bb01a673SMarc Bonnici flags); 7044388f28fSJ-Alves } else { 705e46b2fd2SJ-Alves ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, 706e46b2fd2SJ-Alves FFA_VERSION_MINOR); 7074388f28fSJ-Alves } 7084388f28fSJ-Alves 709e46b2fd2SJ-Alves SMC_RET8(handle, (uint32_t)ret, FFA_TARGET_INFO_MBZ, 710e46b2fd2SJ-Alves FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 711e46b2fd2SJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ); 712bdd2596dSAchin Gupta break; /* not reached */ 713bdd2596dSAchin Gupta 714662af36dSJ-Alves case FFA_FEATURES: 715bdd2596dSAchin Gupta /* 716bdd2596dSAchin Gupta * This is an optional interface. Do the minimal checks and 71752696946SOlivier Deprez * forward to SPM Core which will handle it if implemented. 718bdd2596dSAchin Gupta */ 719bdd2596dSAchin Gupta 72052696946SOlivier Deprez /* Forward SMC from Normal world to the SPM Core */ 72193ff138bSOlivier Deprez if (!secure_origin) { 72293ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 723bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 724bb01a673SMarc Bonnici handle, flags); 72552696946SOlivier Deprez } 72652696946SOlivier Deprez 727bdd2596dSAchin Gupta /* 728bdd2596dSAchin Gupta * Return success if call was from secure world i.e. all 729662af36dSJ-Alves * FFA functions are supported. This is essentially a 730bdd2596dSAchin Gupta * nop. 731bdd2596dSAchin Gupta */ 732662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4, 733bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X5), 734bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 735bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 7360f14d02fSMax Shvetsov 737bdd2596dSAchin Gupta break; /* not reached */ 738bdd2596dSAchin Gupta 739662af36dSJ-Alves case FFA_ID_GET: 740ac03ac5eSMax Shvetsov /* 741662af36dSJ-Alves * Returns the ID of the calling FFA component. 742ac03ac5eSMax Shvetsov */ 743ac03ac5eSMax Shvetsov if (!secure_origin) { 744662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 745662af36dSJ-Alves FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID, 746662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 747662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 748662af36dSJ-Alves FFA_PARAM_MBZ); 74952696946SOlivier Deprez } 75052696946SOlivier Deprez 751662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 752662af36dSJ-Alves FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 753662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 754662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 755662af36dSJ-Alves FFA_PARAM_MBZ); 756ac03ac5eSMax Shvetsov 757ac03ac5eSMax Shvetsov break; /* not reached */ 758ac03ac5eSMax Shvetsov 759cdb49d47SOlivier Deprez case FFA_SECONDARY_EP_REGISTER_SMC64: 760cdb49d47SOlivier Deprez if (secure_origin) { 761cdb49d47SOlivier Deprez ret = spmd_pm_secondary_ep_register(x1); 762cdb49d47SOlivier Deprez 763cdb49d47SOlivier Deprez if (ret < 0) { 764cdb49d47SOlivier Deprez SMC_RET8(handle, FFA_ERROR_SMC64, 765cdb49d47SOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 766cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 767cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 768cdb49d47SOlivier Deprez FFA_PARAM_MBZ); 769cdb49d47SOlivier Deprez } else { 770cdb49d47SOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC64, 771cdb49d47SOlivier Deprez FFA_TARGET_INFO_MBZ, FFA_PARAM_MBZ, 772cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 773cdb49d47SOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 774cdb49d47SOlivier Deprez FFA_PARAM_MBZ); 775cdb49d47SOlivier Deprez } 776cdb49d47SOlivier Deprez } 777cdb49d47SOlivier Deprez 778cdb49d47SOlivier Deprez return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 779cdb49d47SOlivier Deprez break; /* Not reached */ 780cdb49d47SOlivier Deprez 78170c121a2SDaniel Boulby case FFA_SPM_ID_GET: 78270c121a2SDaniel Boulby if (MAKE_FFA_VERSION(1, 1) > FFA_VERSION_COMPILED) { 78370c121a2SDaniel Boulby return spmd_ffa_error_return(handle, 78470c121a2SDaniel Boulby FFA_ERROR_NOT_SUPPORTED); 78570c121a2SDaniel Boulby } 78670c121a2SDaniel Boulby /* 78770c121a2SDaniel Boulby * Returns the ID of the SPMC or SPMD depending on the FF-A 78870c121a2SDaniel Boulby * instance where this function is invoked 78970c121a2SDaniel Boulby */ 79070c121a2SDaniel Boulby if (!secure_origin) { 79170c121a2SDaniel Boulby SMC_RET8(handle, FFA_SUCCESS_SMC32, 79270c121a2SDaniel Boulby FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 79370c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 79470c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 79570c121a2SDaniel Boulby FFA_PARAM_MBZ); 79670c121a2SDaniel Boulby } 79770c121a2SDaniel Boulby SMC_RET8(handle, FFA_SUCCESS_SMC32, 79870c121a2SDaniel Boulby FFA_TARGET_INFO_MBZ, SPMD_DIRECT_MSG_ENDPOINT_ID, 79970c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 80070c121a2SDaniel Boulby FFA_PARAM_MBZ, FFA_PARAM_MBZ, 80170c121a2SDaniel Boulby FFA_PARAM_MBZ); 80270c121a2SDaniel Boulby 80370c121a2SDaniel Boulby break; /* not reached */ 80470c121a2SDaniel Boulby 805f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_REQ_SMC32: 806*5519f07cSShruti case FFA_MSG_SEND_DIRECT_REQ_SMC64: 807*5519f07cSShruti if (!secure_origin) { 808*5519f07cSShruti /* Validate source endpoint is non-secure for non-secure caller. */ 809*5519f07cSShruti if (ffa_is_secure_world_id(ffa_endpoint_source(x1))) { 810*5519f07cSShruti return spmd_ffa_error_return(handle, 811*5519f07cSShruti FFA_ERROR_INVALID_PARAMETER); 812*5519f07cSShruti } 813*5519f07cSShruti } 814f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 815f0d743dbSOlivier Deprez ret = spmd_handle_spmc_message(x3, x4, 816f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 817f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X6), 818f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X7)); 819f0d743dbSOlivier Deprez 820f0d743dbSOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC32, 821f0d743dbSOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 822f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 823f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 824f0d743dbSOlivier Deprez FFA_PARAM_MBZ); 825f0d743dbSOlivier Deprez } else { 826f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 827f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 828bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 829bb01a673SMarc Bonnici handle, flags); 830f0d743dbSOlivier Deprez } 831f0d743dbSOlivier Deprez break; /* Not reached */ 832f0d743dbSOlivier Deprez 833f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_RESP_SMC32: 834f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 8358cb99c3fSOlivier Deprez spmd_spm_core_sync_exit(0ULL); 836f0d743dbSOlivier Deprez } else { 837f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 838f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 839bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 840bb01a673SMarc Bonnici handle, flags); 841f0d743dbSOlivier Deprez } 842f0d743dbSOlivier Deprez break; /* Not reached */ 843f0d743dbSOlivier Deprez 844662af36dSJ-Alves case FFA_RX_RELEASE: 845662af36dSJ-Alves case FFA_RXTX_MAP_SMC32: 846662af36dSJ-Alves case FFA_RXTX_MAP_SMC64: 847662af36dSJ-Alves case FFA_RXTX_UNMAP: 848545b8eb3SRuari Phipps case FFA_PARTITION_INFO_GET: 849fc3f4800SJ-Alves #if MAKE_FFA_VERSION(1, 1) <= FFA_VERSION_COMPILED 850fc3f4800SJ-Alves case FFA_NOTIFICATION_BITMAP_CREATE: 851fc3f4800SJ-Alves case FFA_NOTIFICATION_BITMAP_DESTROY: 852fc3f4800SJ-Alves case FFA_NOTIFICATION_BIND: 853fc3f4800SJ-Alves case FFA_NOTIFICATION_UNBIND: 854fc3f4800SJ-Alves case FFA_NOTIFICATION_SET: 855fc3f4800SJ-Alves case FFA_NOTIFICATION_GET: 856fc3f4800SJ-Alves case FFA_NOTIFICATION_INFO_GET: 857fc3f4800SJ-Alves case FFA_NOTIFICATION_INFO_GET_SMC64: 858c2eba07cSFederico Recanati case FFA_MSG_SEND2: 859d555233fSFederico Recanati case FFA_RX_ACQUIRE: 860fc3f4800SJ-Alves #endif 861662af36dSJ-Alves case FFA_MSG_RUN: 862c2eba07cSFederico Recanati /* 863c2eba07cSFederico Recanati * Above calls should be invoked only by the Normal world and 864c2eba07cSFederico Recanati * must not be forwarded from Secure world to Normal world. 865c2eba07cSFederico Recanati */ 86693ff138bSOlivier Deprez if (secure_origin) { 867662af36dSJ-Alves return spmd_ffa_error_return(handle, 868662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 869bdd2596dSAchin Gupta } 870bdd2596dSAchin Gupta 871bdd2596dSAchin Gupta /* Fall through to forward the call to the other world */ 872662af36dSJ-Alves case FFA_MSG_SEND: 873662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_RESP_SMC64: 874662af36dSJ-Alves case FFA_MEM_DONATE_SMC32: 875662af36dSJ-Alves case FFA_MEM_DONATE_SMC64: 876662af36dSJ-Alves case FFA_MEM_LEND_SMC32: 877662af36dSJ-Alves case FFA_MEM_LEND_SMC64: 878662af36dSJ-Alves case FFA_MEM_SHARE_SMC32: 879662af36dSJ-Alves case FFA_MEM_SHARE_SMC64: 880662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC32: 881662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC64: 882662af36dSJ-Alves case FFA_MEM_RETRIEVE_RESP: 883662af36dSJ-Alves case FFA_MEM_RELINQUISH: 884662af36dSJ-Alves case FFA_MEM_RECLAIM: 885642db984SMarc Bonnici case FFA_MEM_FRAG_TX: 886642db984SMarc Bonnici case FFA_MEM_FRAG_RX: 887662af36dSJ-Alves case FFA_SUCCESS_SMC32: 888662af36dSJ-Alves case FFA_SUCCESS_SMC64: 889bdd2596dSAchin Gupta /* 890bdd2596dSAchin Gupta * TODO: Assume that no requests originate from EL3 at the 891bdd2596dSAchin Gupta * moment. This will change if a SP service is required in 892bdd2596dSAchin Gupta * response to secure interrupts targeted to EL3. Until then 893bdd2596dSAchin Gupta * simply forward the call to the Normal world. 894bdd2596dSAchin Gupta */ 895bdd2596dSAchin Gupta 89693ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 897bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 898bb01a673SMarc Bonnici handle, flags); 899bdd2596dSAchin Gupta break; /* not reached */ 900bdd2596dSAchin Gupta 901662af36dSJ-Alves case FFA_MSG_WAIT: 902bdd2596dSAchin Gupta /* 903bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 904bdd2596dSAchin Gupta * this CPU from the Secure world. If so, then indicate that the 90552696946SOlivier Deprez * SPM Core initialised successfully. 906bdd2596dSAchin Gupta */ 9079dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 9088cb99c3fSOlivier Deprez spmd_spm_core_sync_exit(0ULL); 909bdd2596dSAchin Gupta } 910bdd2596dSAchin Gupta 9110f14d02fSMax Shvetsov /* Fall through to forward the call to the other world */ 912386dc365SOlivier Deprez case FFA_INTERRUPT: 913662af36dSJ-Alves case FFA_MSG_YIELD: 914bdd2596dSAchin Gupta /* This interface must be invoked only by the Secure world */ 91593ff138bSOlivier Deprez if (!secure_origin) { 916662af36dSJ-Alves return spmd_ffa_error_return(handle, 917662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 918bdd2596dSAchin Gupta } 919bdd2596dSAchin Gupta 92093ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 921bb01a673SMarc Bonnici x1, x2, x3, x4, cookie, 922bb01a673SMarc Bonnici handle, flags); 923bdd2596dSAchin Gupta break; /* not reached */ 924bdd2596dSAchin Gupta 9258cb99c3fSOlivier Deprez case FFA_NORMAL_WORLD_RESUME: 9268cb99c3fSOlivier Deprez if (secure_origin && ctx->secure_interrupt_ongoing) { 9278cb99c3fSOlivier Deprez spmd_spm_core_sync_exit(0ULL); 9288cb99c3fSOlivier Deprez } else { 9298cb99c3fSOlivier Deprez return spmd_ffa_error_return(handle, FFA_ERROR_DENIED); 9308cb99c3fSOlivier Deprez } 9318cb99c3fSOlivier Deprez break; /* Not reached */ 9328cb99c3fSOlivier Deprez 933bdd2596dSAchin Gupta default: 934bdd2596dSAchin Gupta WARN("SPM: Unsupported call 0x%08x\n", smc_fid); 935662af36dSJ-Alves return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 936bdd2596dSAchin Gupta } 937bdd2596dSAchin Gupta } 938