1bdd2596dSAchin Gupta /* 2bdd2596dSAchin Gupta * Copyright (c) 2020, ARM Limited and Contributors. All rights reserved. 3bdd2596dSAchin Gupta * 4bdd2596dSAchin Gupta * SPDX-License-Identifier: BSD-3-Clause 5bdd2596dSAchin Gupta */ 6bdd2596dSAchin Gupta 7bdd2596dSAchin Gupta #include <assert.h> 8bdd2596dSAchin Gupta #include <errno.h> 9bdd2596dSAchin Gupta #include <string.h> 10bdd2596dSAchin Gupta 11bdd2596dSAchin Gupta #include <arch_helpers.h> 1252696946SOlivier Deprez #include <arch/aarch64/arch_features.h> 13bdd2596dSAchin Gupta #include <bl31/bl31.h> 14bdd2596dSAchin Gupta #include <common/debug.h> 15bdd2596dSAchin Gupta #include <common/runtime_svc.h> 16bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h> 17bdd2596dSAchin Gupta #include <lib/smccc.h> 18bdd2596dSAchin Gupta #include <lib/spinlock.h> 19bdd2596dSAchin Gupta #include <lib/utils.h> 20bdd2596dSAchin Gupta #include <plat/common/common_def.h> 21bdd2596dSAchin Gupta #include <plat/common/platform.h> 22bdd2596dSAchin Gupta #include <platform_def.h> 23662af36dSJ-Alves #include <services/ffa_svc.h> 24bdd2596dSAchin Gupta #include <services/spmd_svc.h> 25bdd2596dSAchin Gupta #include <smccc_helpers.h> 26bdd2596dSAchin Gupta #include "spmd_private.h" 27bdd2596dSAchin Gupta 28bdd2596dSAchin Gupta /******************************************************************************* 29bdd2596dSAchin Gupta * SPM Core context information. 30bdd2596dSAchin Gupta ******************************************************************************/ 3152696946SOlivier Deprez static spmd_spm_core_context_t spm_core_context[PLATFORM_CORE_COUNT]; 32bdd2596dSAchin Gupta 33bdd2596dSAchin Gupta /******************************************************************************* 34bdd2596dSAchin Gupta * SPM Core attribute information read from its manifest. 35bdd2596dSAchin Gupta ******************************************************************************/ 3652696946SOlivier Deprez static spmc_manifest_attribute_t spmc_attrs; 370f14d02fSMax Shvetsov 380f14d02fSMax Shvetsov /******************************************************************************* 390f14d02fSMax Shvetsov * SPM Core entry point information. Discovered on the primary core and reused 400f14d02fSMax Shvetsov * on secondary cores. 410f14d02fSMax Shvetsov ******************************************************************************/ 420f14d02fSMax Shvetsov static entry_point_info_t *spmc_ep_info; 430f14d02fSMax Shvetsov 440f14d02fSMax Shvetsov /******************************************************************************* 4552696946SOlivier Deprez * SPM Core context on current CPU get helper. 4652696946SOlivier Deprez ******************************************************************************/ 4752696946SOlivier Deprez spmd_spm_core_context_t *spmd_get_context(void) 4852696946SOlivier Deprez { 4952696946SOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 5052696946SOlivier Deprez 5152696946SOlivier Deprez return &spm_core_context[linear_id]; 5252696946SOlivier Deprez } 5352696946SOlivier Deprez 5452696946SOlivier Deprez /******************************************************************************* 55c0267cc9SOlivier Deprez * SPM Core entry point information get helper. 56c0267cc9SOlivier Deprez ******************************************************************************/ 57c0267cc9SOlivier Deprez entry_point_info_t *spmd_spmc_ep_info_get(void) 58c0267cc9SOlivier Deprez { 59c0267cc9SOlivier Deprez return spmc_ep_info; 60c0267cc9SOlivier Deprez } 61c0267cc9SOlivier Deprez 62c0267cc9SOlivier Deprez /******************************************************************************* 63*a92bc73bSOlivier Deprez * SPM Core ID getter. 64*a92bc73bSOlivier Deprez ******************************************************************************/ 65*a92bc73bSOlivier Deprez uint16_t spmd_spmc_id_get(void) 66*a92bc73bSOlivier Deprez { 67*a92bc73bSOlivier Deprez return spmc_attrs.spmc_id; 68*a92bc73bSOlivier Deprez } 69*a92bc73bSOlivier Deprez 70*a92bc73bSOlivier Deprez /******************************************************************************* 710f14d02fSMax Shvetsov * Static function declaration. 720f14d02fSMax Shvetsov ******************************************************************************/ 730f14d02fSMax Shvetsov static int32_t spmd_init(void); 7423d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr); 75662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, 7652696946SOlivier Deprez int error_code); 7752696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 7852696946SOlivier Deprez bool secure_origin, 7952696946SOlivier Deprez uint64_t x1, 8052696946SOlivier Deprez uint64_t x2, 8152696946SOlivier Deprez uint64_t x3, 8252696946SOlivier Deprez uint64_t x4, 8352696946SOlivier Deprez void *handle); 84bdd2596dSAchin Gupta 85bdd2596dSAchin Gupta /******************************************************************************* 8652696946SOlivier Deprez * This function takes an SPMC context pointer and performs a synchronous 8752696946SOlivier Deprez * SPMC entry. 88bdd2596dSAchin Gupta ******************************************************************************/ 89bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx) 90bdd2596dSAchin Gupta { 91bdd2596dSAchin Gupta uint64_t rc; 92bdd2596dSAchin Gupta 93bdd2596dSAchin Gupta assert(spmc_ctx != NULL); 94bdd2596dSAchin Gupta 95bdd2596dSAchin Gupta cm_set_context(&(spmc_ctx->cpu_ctx), SECURE); 96bdd2596dSAchin Gupta 97bdd2596dSAchin Gupta /* Restore the context assigned above */ 98bdd2596dSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 99033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 10028f39f02SMax Shvetsov cm_el2_sysregs_context_restore(SECURE); 101033039f8SMax Shvetsov #endif 102bdd2596dSAchin Gupta cm_set_next_eret_context(SECURE); 103bdd2596dSAchin Gupta 104033039f8SMax Shvetsov /* Enter SPMC */ 105bdd2596dSAchin Gupta rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx); 106bdd2596dSAchin Gupta 107bdd2596dSAchin Gupta /* Save secure state */ 108bdd2596dSAchin Gupta cm_el1_sysregs_context_save(SECURE); 109033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 11028f39f02SMax Shvetsov cm_el2_sysregs_context_save(SECURE); 111033039f8SMax Shvetsov #endif 112bdd2596dSAchin Gupta 113bdd2596dSAchin Gupta return rc; 114bdd2596dSAchin Gupta } 115bdd2596dSAchin Gupta 116bdd2596dSAchin Gupta /******************************************************************************* 11752696946SOlivier Deprez * This function returns to the place where spmd_spm_core_sync_entry() was 118bdd2596dSAchin Gupta * called originally. 119bdd2596dSAchin Gupta ******************************************************************************/ 120bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc) 121bdd2596dSAchin Gupta { 12252696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 123bdd2596dSAchin Gupta 12452696946SOlivier Deprez /* Get current CPU context from SPMC context */ 125bdd2596dSAchin Gupta assert(cm_get_context(SECURE) == &(ctx->cpu_ctx)); 126bdd2596dSAchin Gupta 127bdd2596dSAchin Gupta /* 128bdd2596dSAchin Gupta * The SPMD must have initiated the original request through a 129bdd2596dSAchin Gupta * synchronous entry into SPMC. Jump back to the original C runtime 130bdd2596dSAchin Gupta * context with the value of rc in x0; 131bdd2596dSAchin Gupta */ 132bdd2596dSAchin Gupta spmd_spm_core_exit(ctx->c_rt_ctx, rc); 133bdd2596dSAchin Gupta 134bdd2596dSAchin Gupta panic(); 135bdd2596dSAchin Gupta } 136bdd2596dSAchin Gupta 137bdd2596dSAchin Gupta /******************************************************************************* 13852696946SOlivier Deprez * Jump to the SPM Core for the first time. 139bdd2596dSAchin Gupta ******************************************************************************/ 140bdd2596dSAchin Gupta static int32_t spmd_init(void) 141bdd2596dSAchin Gupta { 14252696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 14352696946SOlivier Deprez uint64_t rc; 1449dcf63ddSOlivier Deprez unsigned int linear_id = plat_my_core_pos(); 1459dcf63ddSOlivier Deprez unsigned int core_id; 146bdd2596dSAchin Gupta 14752696946SOlivier Deprez VERBOSE("SPM Core init start.\n"); 1489dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON_PENDING; 1499dcf63ddSOlivier Deprez 1509dcf63ddSOlivier Deprez /* Set the SPMC context state on other CPUs to OFF */ 151f0d743dbSOlivier Deprez for (core_id = 0U; core_id < PLATFORM_CORE_COUNT; core_id++) { 1529dcf63ddSOlivier Deprez if (core_id != linear_id) { 1539dcf63ddSOlivier Deprez spm_core_context[core_id].state = SPMC_STATE_OFF; 1549dcf63ddSOlivier Deprez } 1559dcf63ddSOlivier Deprez } 156bdd2596dSAchin Gupta 157bdd2596dSAchin Gupta rc = spmd_spm_core_sync_entry(ctx); 15852696946SOlivier Deprez if (rc != 0ULL) { 159bdd2596dSAchin Gupta ERROR("SPMC initialisation failed 0x%llx\n", rc); 16052696946SOlivier Deprez return 0; 161bdd2596dSAchin Gupta } 162bdd2596dSAchin Gupta 1639dcf63ddSOlivier Deprez ctx->state = SPMC_STATE_ON; 1649dcf63ddSOlivier Deprez 16552696946SOlivier Deprez VERBOSE("SPM Core init end.\n"); 166bdd2596dSAchin Gupta 167bdd2596dSAchin Gupta return 1; 168bdd2596dSAchin Gupta } 169bdd2596dSAchin Gupta 170bdd2596dSAchin Gupta /******************************************************************************* 17152696946SOlivier Deprez * Loads SPMC manifest and inits SPMC. 1720f14d02fSMax Shvetsov ******************************************************************************/ 17323d5ba86SOlivier Deprez static int spmd_spmc_init(void *pm_addr) 1740f14d02fSMax Shvetsov { 17552696946SOlivier Deprez spmd_spm_core_context_t *spm_ctx = spmd_get_context(); 1760f14d02fSMax Shvetsov uint32_t ep_attr; 17752696946SOlivier Deprez int rc; 1780f14d02fSMax Shvetsov 17952696946SOlivier Deprez /* Load the SPM Core manifest */ 18023d5ba86SOlivier Deprez rc = plat_spm_core_manifest_load(&spmc_attrs, pm_addr); 1810f14d02fSMax Shvetsov if (rc != 0) { 18252696946SOlivier Deprez WARN("No or invalid SPM Core manifest image provided by BL2\n"); 18352696946SOlivier Deprez return rc; 1840f14d02fSMax Shvetsov } 1850f14d02fSMax Shvetsov 1860f14d02fSMax Shvetsov /* 18752696946SOlivier Deprez * Ensure that the SPM Core version is compatible with the SPM 18852696946SOlivier Deprez * Dispatcher version. 1890f14d02fSMax Shvetsov */ 190662af36dSJ-Alves if ((spmc_attrs.major_version != FFA_VERSION_MAJOR) || 191662af36dSJ-Alves (spmc_attrs.minor_version > FFA_VERSION_MINOR)) { 192662af36dSJ-Alves WARN("Unsupported FFA version (%u.%u)\n", 1930f14d02fSMax Shvetsov spmc_attrs.major_version, spmc_attrs.minor_version); 19452696946SOlivier Deprez return -EINVAL; 1950f14d02fSMax Shvetsov } 1960f14d02fSMax Shvetsov 197662af36dSJ-Alves VERBOSE("FFA version (%u.%u)\n", spmc_attrs.major_version, 1980f14d02fSMax Shvetsov spmc_attrs.minor_version); 1990f14d02fSMax Shvetsov 20052696946SOlivier Deprez VERBOSE("SPM Core run time EL%x.\n", 201033039f8SMax Shvetsov SPMD_SPM_AT_SEL2 ? MODE_EL2 : MODE_EL1); 2020f14d02fSMax Shvetsov 203ac03ac5eSMax Shvetsov /* Validate the SPMC ID, Ensure high bit is set */ 20452696946SOlivier Deprez if (((spmc_attrs.spmc_id >> SPMC_SECURE_ID_SHIFT) & 20552696946SOlivier Deprez SPMC_SECURE_ID_MASK) == 0U) { 20652696946SOlivier Deprez WARN("Invalid ID (0x%x) for SPMC.\n", spmc_attrs.spmc_id); 20752696946SOlivier Deprez return -EINVAL; 208ac03ac5eSMax Shvetsov } 209ac03ac5eSMax Shvetsov 21052696946SOlivier Deprez /* Validate the SPM Core execution state */ 2110f14d02fSMax Shvetsov if ((spmc_attrs.exec_state != MODE_RW_64) && 2120f14d02fSMax Shvetsov (spmc_attrs.exec_state != MODE_RW_32)) { 21323d5ba86SOlivier Deprez WARN("Unsupported %s%x.\n", "SPM Core execution state 0x", 2140f14d02fSMax Shvetsov spmc_attrs.exec_state); 21552696946SOlivier Deprez return -EINVAL; 2160f14d02fSMax Shvetsov } 2170f14d02fSMax Shvetsov 21823d5ba86SOlivier Deprez VERBOSE("%s%x.\n", "SPM Core execution state 0x", 21923d5ba86SOlivier Deprez spmc_attrs.exec_state); 2200f14d02fSMax Shvetsov 221033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 222033039f8SMax Shvetsov /* Ensure manifest has not requested AArch32 state in S-EL2 */ 223033039f8SMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 224033039f8SMax Shvetsov WARN("AArch32 state at S-EL2 is not supported.\n"); 22552696946SOlivier Deprez return -EINVAL; 2260f14d02fSMax Shvetsov } 2270f14d02fSMax Shvetsov 2280f14d02fSMax Shvetsov /* 2290f14d02fSMax Shvetsov * Check if S-EL2 is supported on this system if S-EL2 2300f14d02fSMax Shvetsov * is required for SPM 2310f14d02fSMax Shvetsov */ 23252696946SOlivier Deprez if (!is_armv8_4_sel2_present()) { 23352696946SOlivier Deprez WARN("SPM Core run time S-EL2 is not supported.\n"); 23452696946SOlivier Deprez return -EINVAL; 2350f14d02fSMax Shvetsov } 236033039f8SMax Shvetsov #endif /* SPMD_SPM_AT_SEL2 */ 2370f14d02fSMax Shvetsov 2380f14d02fSMax Shvetsov /* Initialise an entrypoint to set up the CPU context */ 2390f14d02fSMax Shvetsov ep_attr = SECURE | EP_ST_ENABLE; 24052696946SOlivier Deprez if ((read_sctlr_el3() & SCTLR_EE_BIT) != 0ULL) { 2410f14d02fSMax Shvetsov ep_attr |= EP_EE_BIG; 2420f14d02fSMax Shvetsov } 2430f14d02fSMax Shvetsov 2440f14d02fSMax Shvetsov SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr); 2450f14d02fSMax Shvetsov assert(spmc_ep_info->pc == BL32_BASE); 2460f14d02fSMax Shvetsov 2470f14d02fSMax Shvetsov /* 24852696946SOlivier Deprez * Populate SPSR for SPM Core based upon validated parameters from the 24952696946SOlivier Deprez * manifest. 2500f14d02fSMax Shvetsov */ 2510f14d02fSMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 2520f14d02fSMax Shvetsov spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 2530f14d02fSMax Shvetsov SPSR_E_LITTLE, 2540f14d02fSMax Shvetsov DAIF_FIQ_BIT | 2550f14d02fSMax Shvetsov DAIF_IRQ_BIT | 2560f14d02fSMax Shvetsov DAIF_ABT_BIT); 2570f14d02fSMax Shvetsov } else { 258033039f8SMax Shvetsov 259033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 260033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL2; 261033039f8SMax Shvetsov #else 262033039f8SMax Shvetsov static const uint32_t runtime_el = MODE_EL1; 263033039f8SMax Shvetsov #endif 264033039f8SMax Shvetsov spmc_ep_info->spsr = SPSR_64(runtime_el, 2650f14d02fSMax Shvetsov MODE_SP_ELX, 2660f14d02fSMax Shvetsov DISABLE_ALL_EXCEPTIONS); 2670f14d02fSMax Shvetsov } 2680f14d02fSMax Shvetsov 26952696946SOlivier Deprez /* Initialise SPM Core context with this entry point information */ 2700f14d02fSMax Shvetsov cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info); 2710f14d02fSMax Shvetsov 2720f14d02fSMax Shvetsov /* Reuse PSCI affinity states to mark this SPMC context as off */ 2730f14d02fSMax Shvetsov spm_ctx->state = AFF_STATE_OFF; 2740f14d02fSMax Shvetsov 27552696946SOlivier Deprez INFO("SPM Core setup done.\n"); 2760f14d02fSMax Shvetsov 277a334c4e6SOlivier Deprez /* Register power management hooks with PSCI */ 278a334c4e6SOlivier Deprez psci_register_spd_pm_hook(&spmd_pm); 279a334c4e6SOlivier Deprez 2800f14d02fSMax Shvetsov /* Register init function for deferred init. */ 2810f14d02fSMax Shvetsov bl31_register_bl32_init(&spmd_init); 2820f14d02fSMax Shvetsov 2830f14d02fSMax Shvetsov return 0; 2840f14d02fSMax Shvetsov } 2850f14d02fSMax Shvetsov 2860f14d02fSMax Shvetsov /******************************************************************************* 28752696946SOlivier Deprez * Initialize context of SPM Core. 288bdd2596dSAchin Gupta ******************************************************************************/ 2890f14d02fSMax Shvetsov int spmd_setup(void) 290bdd2596dSAchin Gupta { 29123d5ba86SOlivier Deprez void *spmc_manifest; 292bdd2596dSAchin Gupta int rc; 293bdd2596dSAchin Gupta 294bdd2596dSAchin Gupta spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 29552696946SOlivier Deprez if (spmc_ep_info == NULL) { 29652696946SOlivier Deprez WARN("No SPM Core image provided by BL2 boot loader.\n"); 29752696946SOlivier Deprez return -EINVAL; 298bdd2596dSAchin Gupta } 299bdd2596dSAchin Gupta 300bdd2596dSAchin Gupta /* Under no circumstances will this parameter be 0 */ 30152696946SOlivier Deprez assert(spmc_ep_info->pc != 0ULL); 302bdd2596dSAchin Gupta 303bdd2596dSAchin Gupta /* 304bdd2596dSAchin Gupta * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will 30552696946SOlivier Deprez * be used as a manifest for the SPM Core at the next lower EL/mode. 306bdd2596dSAchin Gupta */ 30723d5ba86SOlivier Deprez spmc_manifest = (void *)spmc_ep_info->args.arg0; 30823d5ba86SOlivier Deprez if (spmc_manifest == NULL) { 30923d5ba86SOlivier Deprez ERROR("Invalid or absent SPM Core manifest.\n"); 31023d5ba86SOlivier Deprez return -EINVAL; 311bdd2596dSAchin Gupta } 312bdd2596dSAchin Gupta 3130f14d02fSMax Shvetsov /* Load manifest, init SPMC */ 31423d5ba86SOlivier Deprez rc = spmd_spmc_init(spmc_manifest); 3150f14d02fSMax Shvetsov if (rc != 0) { 31652696946SOlivier Deprez WARN("Booting device without SPM initialization.\n"); 317bdd2596dSAchin Gupta } 318bdd2596dSAchin Gupta 3190f14d02fSMax Shvetsov return rc; 3200f14d02fSMax Shvetsov } 3210f14d02fSMax Shvetsov 3220f14d02fSMax Shvetsov /******************************************************************************* 3230f14d02fSMax Shvetsov * Forward SMC to the other security state 3240f14d02fSMax Shvetsov ******************************************************************************/ 32552696946SOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, 32652696946SOlivier Deprez bool secure_origin, 32752696946SOlivier Deprez uint64_t x1, 32852696946SOlivier Deprez uint64_t x2, 32952696946SOlivier Deprez uint64_t x3, 33052696946SOlivier Deprez uint64_t x4, 33152696946SOlivier Deprez void *handle) 3320f14d02fSMax Shvetsov { 333c2901419SOlivier Deprez unsigned int secure_state_in = (secure_origin) ? SECURE : NON_SECURE; 334c2901419SOlivier Deprez unsigned int secure_state_out = (!secure_origin) ? SECURE : NON_SECURE; 33593ff138bSOlivier Deprez 3360f14d02fSMax Shvetsov /* Save incoming security state */ 33793ff138bSOlivier Deprez cm_el1_sysregs_context_save(secure_state_in); 338033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 33993ff138bSOlivier Deprez cm_el2_sysregs_context_save(secure_state_in); 340033039f8SMax Shvetsov #endif 3410f14d02fSMax Shvetsov 3420f14d02fSMax Shvetsov /* Restore outgoing security state */ 34393ff138bSOlivier Deprez cm_el1_sysregs_context_restore(secure_state_out); 344033039f8SMax Shvetsov #if SPMD_SPM_AT_SEL2 34593ff138bSOlivier Deprez cm_el2_sysregs_context_restore(secure_state_out); 346033039f8SMax Shvetsov #endif 34793ff138bSOlivier Deprez cm_set_next_eret_context(secure_state_out); 3480f14d02fSMax Shvetsov 34993ff138bSOlivier Deprez SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4, 3500f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X5), 3510f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X6), 3520f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X7)); 3530f14d02fSMax Shvetsov } 3540f14d02fSMax Shvetsov 3550f14d02fSMax Shvetsov /******************************************************************************* 356662af36dSJ-Alves * Return FFA_ERROR with specified error code 3570f14d02fSMax Shvetsov ******************************************************************************/ 358662af36dSJ-Alves static uint64_t spmd_ffa_error_return(void *handle, int error_code) 3590f14d02fSMax Shvetsov { 360662af36dSJ-Alves SMC_RET8(handle, FFA_ERROR, 361662af36dSJ-Alves FFA_TARGET_INFO_MBZ, error_code, 362662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 363662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 364bdd2596dSAchin Gupta } 365bdd2596dSAchin Gupta 366f0d743dbSOlivier Deprez /******************************************************************************* 367f0d743dbSOlivier Deprez * spmd_check_address_in_binary_image 368f0d743dbSOlivier Deprez ******************************************************************************/ 369f0d743dbSOlivier Deprez bool spmd_check_address_in_binary_image(uint64_t address) 370f0d743dbSOlivier Deprez { 371f0d743dbSOlivier Deprez assert(!check_uptr_overflow(spmc_attrs.load_address, spmc_attrs.binary_size)); 372f0d743dbSOlivier Deprez 373f0d743dbSOlivier Deprez return ((address >= spmc_attrs.load_address) && 374f0d743dbSOlivier Deprez (address < (spmc_attrs.load_address + spmc_attrs.binary_size))); 375f0d743dbSOlivier Deprez } 376f0d743dbSOlivier Deprez 377c2901419SOlivier Deprez /****************************************************************************** 378c2901419SOlivier Deprez * spmd_is_spmc_message 379c2901419SOlivier Deprez *****************************************************************************/ 380c2901419SOlivier Deprez static bool spmd_is_spmc_message(unsigned int ep) 381c2901419SOlivier Deprez { 382c2901419SOlivier Deprez return ((ffa_endpoint_destination(ep) == SPMD_DIRECT_MSG_ENDPOINT_ID) 383c2901419SOlivier Deprez && (ffa_endpoint_source(ep) == spmc_attrs.spmc_id)); 384c2901419SOlivier Deprez } 385c2901419SOlivier Deprez 386f0d743dbSOlivier Deprez /****************************************************************************** 387f0d743dbSOlivier Deprez * spmd_handle_spmc_message 388f0d743dbSOlivier Deprez *****************************************************************************/ 389*a92bc73bSOlivier Deprez static int spmd_handle_spmc_message(unsigned long long msg, 390*a92bc73bSOlivier Deprez unsigned long long parm1, unsigned long long parm2, 391*a92bc73bSOlivier Deprez unsigned long long parm3, unsigned long long parm4) 392f0d743dbSOlivier Deprez { 393f0d743dbSOlivier Deprez VERBOSE("%s %llx %llx %llx %llx %llx\n", __func__, 394f0d743dbSOlivier Deprez msg, parm1, parm2, parm3, parm4); 395f0d743dbSOlivier Deprez 396f0d743dbSOlivier Deprez switch (msg) { 397f0d743dbSOlivier Deprez case SPMD_DIRECT_MSG_SET_ENTRY_POINT: 398f0d743dbSOlivier Deprez return spmd_pm_secondary_core_set_ep(parm1, parm2, parm3); 399f0d743dbSOlivier Deprez default: 400f0d743dbSOlivier Deprez break; 401f0d743dbSOlivier Deprez } 402f0d743dbSOlivier Deprez 403f0d743dbSOlivier Deprez return -EINVAL; 404f0d743dbSOlivier Deprez } 405f0d743dbSOlivier Deprez 406bdd2596dSAchin Gupta /******************************************************************************* 407662af36dSJ-Alves * This function handles all SMCs in the range reserved for FFA. Each call is 408bdd2596dSAchin Gupta * either forwarded to the other security state or handled by the SPM dispatcher 409bdd2596dSAchin Gupta ******************************************************************************/ 41052696946SOlivier Deprez uint64_t spmd_smc_handler(uint32_t smc_fid, 41152696946SOlivier Deprez uint64_t x1, 41252696946SOlivier Deprez uint64_t x2, 41352696946SOlivier Deprez uint64_t x3, 41452696946SOlivier Deprez uint64_t x4, 41552696946SOlivier Deprez void *cookie, 41652696946SOlivier Deprez void *handle, 417bdd2596dSAchin Gupta uint64_t flags) 418bdd2596dSAchin Gupta { 41952696946SOlivier Deprez spmd_spm_core_context_t *ctx = spmd_get_context(); 42093ff138bSOlivier Deprez bool secure_origin; 42193ff138bSOlivier Deprez int32_t ret; 4224388f28fSJ-Alves uint32_t input_version; 423bdd2596dSAchin Gupta 424bdd2596dSAchin Gupta /* Determine which security state this SMC originated from */ 42593ff138bSOlivier Deprez secure_origin = is_caller_secure(flags); 426bdd2596dSAchin Gupta 42752696946SOlivier Deprez INFO("SPM: 0x%x 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n", 428bdd2596dSAchin Gupta smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5), 429bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 430bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 431bdd2596dSAchin Gupta 432bdd2596dSAchin Gupta switch (smc_fid) { 433662af36dSJ-Alves case FFA_ERROR: 434bdd2596dSAchin Gupta /* 435bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 43652696946SOlivier Deprez * this CPU. If so, then indicate that the SPM Core initialised 437bdd2596dSAchin Gupta * unsuccessfully. 438bdd2596dSAchin Gupta */ 4399dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 440bdd2596dSAchin Gupta spmd_spm_core_sync_exit(x2); 4410f14d02fSMax Shvetsov } 442bdd2596dSAchin Gupta 44393ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4440f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 445bdd2596dSAchin Gupta break; /* not reached */ 446bdd2596dSAchin Gupta 447662af36dSJ-Alves case FFA_VERSION: 4484388f28fSJ-Alves input_version = (uint32_t)(0xFFFFFFFF & x1); 449bdd2596dSAchin Gupta /* 4504388f28fSJ-Alves * If caller is secure and SPMC was initialized, 4514388f28fSJ-Alves * return FFA_VERSION of SPMD. 4524388f28fSJ-Alves * If caller is non secure and SPMC was initialized, 4534388f28fSJ-Alves * return SPMC's version. 4544388f28fSJ-Alves * Sanity check to "input_version". 455bdd2596dSAchin Gupta */ 4564388f28fSJ-Alves if ((input_version & FFA_VERSION_BIT31_MASK) || 4574388f28fSJ-Alves (ctx->state == SPMC_STATE_RESET)) { 4584388f28fSJ-Alves ret = FFA_ERROR_NOT_SUPPORTED; 4594388f28fSJ-Alves } else if (!secure_origin) { 4604388f28fSJ-Alves ret = MAKE_FFA_VERSION(spmc_attrs.major_version, spmc_attrs.minor_version); 4614388f28fSJ-Alves } else { 4624388f28fSJ-Alves ret = MAKE_FFA_VERSION(FFA_VERSION_MAJOR, FFA_VERSION_MINOR); 4634388f28fSJ-Alves } 4644388f28fSJ-Alves 4654388f28fSJ-Alves SMC_RET8(handle, ret, FFA_TARGET_INFO_MBZ, FFA_TARGET_INFO_MBZ, 466662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, FFA_PARAM_MBZ, 467662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ); 468bdd2596dSAchin Gupta break; /* not reached */ 469bdd2596dSAchin Gupta 470662af36dSJ-Alves case FFA_FEATURES: 471bdd2596dSAchin Gupta /* 472bdd2596dSAchin Gupta * This is an optional interface. Do the minimal checks and 47352696946SOlivier Deprez * forward to SPM Core which will handle it if implemented. 474bdd2596dSAchin Gupta */ 475bdd2596dSAchin Gupta 476bdd2596dSAchin Gupta /* 477662af36dSJ-Alves * Check if x1 holds a valid FFA fid. This is an 478bdd2596dSAchin Gupta * optimization. 479bdd2596dSAchin Gupta */ 480662af36dSJ-Alves if (!is_ffa_fid(x1)) { 481662af36dSJ-Alves return spmd_ffa_error_return(handle, 482662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 4830f14d02fSMax Shvetsov } 484bdd2596dSAchin Gupta 48552696946SOlivier Deprez /* Forward SMC from Normal world to the SPM Core */ 48693ff138bSOlivier Deprez if (!secure_origin) { 48793ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4880f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 48952696946SOlivier Deprez } 49052696946SOlivier Deprez 491bdd2596dSAchin Gupta /* 492bdd2596dSAchin Gupta * Return success if call was from secure world i.e. all 493662af36dSJ-Alves * FFA functions are supported. This is essentially a 494bdd2596dSAchin Gupta * nop. 495bdd2596dSAchin Gupta */ 496662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, x1, x2, x3, x4, 497bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X5), 498bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 499bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 5000f14d02fSMax Shvetsov 501bdd2596dSAchin Gupta break; /* not reached */ 502bdd2596dSAchin Gupta 503662af36dSJ-Alves case FFA_ID_GET: 504ac03ac5eSMax Shvetsov /* 505662af36dSJ-Alves * Returns the ID of the calling FFA component. 506ac03ac5eSMax Shvetsov */ 507ac03ac5eSMax Shvetsov if (!secure_origin) { 508662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 509662af36dSJ-Alves FFA_TARGET_INFO_MBZ, FFA_NS_ENDPOINT_ID, 510662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 511662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 512662af36dSJ-Alves FFA_PARAM_MBZ); 51352696946SOlivier Deprez } 51452696946SOlivier Deprez 515662af36dSJ-Alves SMC_RET8(handle, FFA_SUCCESS_SMC32, 516662af36dSJ-Alves FFA_TARGET_INFO_MBZ, spmc_attrs.spmc_id, 517662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 518662af36dSJ-Alves FFA_PARAM_MBZ, FFA_PARAM_MBZ, 519662af36dSJ-Alves FFA_PARAM_MBZ); 520ac03ac5eSMax Shvetsov 521ac03ac5eSMax Shvetsov break; /* not reached */ 522ac03ac5eSMax Shvetsov 523f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_REQ_SMC32: 524f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 525f0d743dbSOlivier Deprez ret = spmd_handle_spmc_message(x3, x4, 526f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X5), 527f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X6), 528f0d743dbSOlivier Deprez SMC_GET_GP(handle, CTX_GPREG_X7)); 529f0d743dbSOlivier Deprez 530f0d743dbSOlivier Deprez SMC_RET8(handle, FFA_SUCCESS_SMC32, 531f0d743dbSOlivier Deprez FFA_TARGET_INFO_MBZ, ret, 532f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 533f0d743dbSOlivier Deprez FFA_PARAM_MBZ, FFA_PARAM_MBZ, 534f0d743dbSOlivier Deprez FFA_PARAM_MBZ); 535f0d743dbSOlivier Deprez } else { 536f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 537f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 538f0d743dbSOlivier Deprez x1, x2, x3, x4, handle); 539f0d743dbSOlivier Deprez } 540f0d743dbSOlivier Deprez break; /* Not reached */ 541f0d743dbSOlivier Deprez 542f0d743dbSOlivier Deprez case FFA_MSG_SEND_DIRECT_RESP_SMC32: 543f0d743dbSOlivier Deprez if (secure_origin && spmd_is_spmc_message(x1)) { 544f0d743dbSOlivier Deprez spmd_spm_core_sync_exit(0); 545f0d743dbSOlivier Deprez } else { 546f0d743dbSOlivier Deprez /* Forward direct message to the other world */ 547f0d743dbSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 548f0d743dbSOlivier Deprez x1, x2, x3, x4, handle); 549f0d743dbSOlivier Deprez } 550f0d743dbSOlivier Deprez break; /* Not reached */ 551f0d743dbSOlivier Deprez 552662af36dSJ-Alves case FFA_RX_RELEASE: 553662af36dSJ-Alves case FFA_RXTX_MAP_SMC32: 554662af36dSJ-Alves case FFA_RXTX_MAP_SMC64: 555662af36dSJ-Alves case FFA_RXTX_UNMAP: 556662af36dSJ-Alves case FFA_MSG_RUN: 557bdd2596dSAchin Gupta /* This interface must be invoked only by the Normal world */ 55893ff138bSOlivier Deprez if (secure_origin) { 559662af36dSJ-Alves return spmd_ffa_error_return(handle, 560662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 561bdd2596dSAchin Gupta } 562bdd2596dSAchin Gupta 563bdd2596dSAchin Gupta /* Fall through to forward the call to the other world */ 564bdd2596dSAchin Gupta 565662af36dSJ-Alves case FFA_PARTITION_INFO_GET: 566662af36dSJ-Alves case FFA_MSG_SEND: 567662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_REQ_SMC64: 568662af36dSJ-Alves case FFA_MSG_SEND_DIRECT_RESP_SMC64: 569662af36dSJ-Alves case FFA_MEM_DONATE_SMC32: 570662af36dSJ-Alves case FFA_MEM_DONATE_SMC64: 571662af36dSJ-Alves case FFA_MEM_LEND_SMC32: 572662af36dSJ-Alves case FFA_MEM_LEND_SMC64: 573662af36dSJ-Alves case FFA_MEM_SHARE_SMC32: 574662af36dSJ-Alves case FFA_MEM_SHARE_SMC64: 575662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC32: 576662af36dSJ-Alves case FFA_MEM_RETRIEVE_REQ_SMC64: 577662af36dSJ-Alves case FFA_MEM_RETRIEVE_RESP: 578662af36dSJ-Alves case FFA_MEM_RELINQUISH: 579662af36dSJ-Alves case FFA_MEM_RECLAIM: 580662af36dSJ-Alves case FFA_SUCCESS_SMC32: 581662af36dSJ-Alves case FFA_SUCCESS_SMC64: 582bdd2596dSAchin Gupta /* 583bdd2596dSAchin Gupta * TODO: Assume that no requests originate from EL3 at the 584bdd2596dSAchin Gupta * moment. This will change if a SP service is required in 585bdd2596dSAchin Gupta * response to secure interrupts targeted to EL3. Until then 586bdd2596dSAchin Gupta * simply forward the call to the Normal world. 587bdd2596dSAchin Gupta */ 588bdd2596dSAchin Gupta 58993ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 5900f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 591bdd2596dSAchin Gupta break; /* not reached */ 592bdd2596dSAchin Gupta 593662af36dSJ-Alves case FFA_MSG_WAIT: 594bdd2596dSAchin Gupta /* 595bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 596bdd2596dSAchin Gupta * this CPU from the Secure world. If so, then indicate that the 59752696946SOlivier Deprez * SPM Core initialised successfully. 598bdd2596dSAchin Gupta */ 5999dcf63ddSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_ON_PENDING)) { 600bdd2596dSAchin Gupta spmd_spm_core_sync_exit(0); 601bdd2596dSAchin Gupta } 602bdd2596dSAchin Gupta 6030f14d02fSMax Shvetsov /* Fall through to forward the call to the other world */ 604bdd2596dSAchin Gupta 605662af36dSJ-Alves case FFA_MSG_YIELD: 606bdd2596dSAchin Gupta /* This interface must be invoked only by the Secure world */ 60793ff138bSOlivier Deprez if (!secure_origin) { 608662af36dSJ-Alves return spmd_ffa_error_return(handle, 609662af36dSJ-Alves FFA_ERROR_NOT_SUPPORTED); 610bdd2596dSAchin Gupta } 611bdd2596dSAchin Gupta 61293ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 6130f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 614bdd2596dSAchin Gupta break; /* not reached */ 615bdd2596dSAchin Gupta 616bdd2596dSAchin Gupta default: 617bdd2596dSAchin Gupta WARN("SPM: Unsupported call 0x%08x\n", smc_fid); 618662af36dSJ-Alves return spmd_ffa_error_return(handle, FFA_ERROR_NOT_SUPPORTED); 619bdd2596dSAchin Gupta } 620bdd2596dSAchin Gupta } 621