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> 12bdd2596dSAchin Gupta #include <bl31/bl31.h> 13bdd2596dSAchin Gupta #include <common/debug.h> 14bdd2596dSAchin Gupta #include <common/runtime_svc.h> 15bdd2596dSAchin Gupta #include <lib/el3_runtime/context_mgmt.h> 16bdd2596dSAchin Gupta #include <lib/smccc.h> 17bdd2596dSAchin Gupta #include <lib/spinlock.h> 18bdd2596dSAchin Gupta #include <lib/utils.h> 19bdd2596dSAchin Gupta #include <lib/xlat_tables/xlat_tables_v2.h> 20bdd2596dSAchin Gupta #include <plat/common/common_def.h> 21bdd2596dSAchin Gupta #include <plat/common/platform.h> 22bdd2596dSAchin Gupta #include <platform_def.h> 23bdd2596dSAchin Gupta #include <services/spci_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 ******************************************************************************/ 31bdd2596dSAchin Gupta 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 ******************************************************************************/ 360f14d02fSMax Shvetsov static spmc_manifest_sect_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 /******************************************************************************* 450f14d02fSMax Shvetsov * Static function declaration. 460f14d02fSMax Shvetsov ******************************************************************************/ 470f14d02fSMax Shvetsov static int32_t spmd_init(void); 480f14d02fSMax Shvetsov static int spmd_spmc_init(void *rd_base, size_t rd_size); 490f14d02fSMax Shvetsov static uint64_t spmd_spci_error_return(void *handle, int error_code); 50*93ff138bSOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, bool secure_origin, 51*93ff138bSOlivier Deprez uint64_t x1, uint64_t x2, uint64_t x3, 52*93ff138bSOlivier Deprez uint64_t x4, void *handle); 53bdd2596dSAchin Gupta 54bdd2596dSAchin Gupta /******************************************************************************* 55bdd2596dSAchin Gupta * This function takes an SP context pointer and performs a synchronous entry 56bdd2596dSAchin Gupta * into it. 57bdd2596dSAchin Gupta ******************************************************************************/ 58bdd2596dSAchin Gupta uint64_t spmd_spm_core_sync_entry(spmd_spm_core_context_t *spmc_ctx) 59bdd2596dSAchin Gupta { 60bdd2596dSAchin Gupta uint64_t rc; 61bdd2596dSAchin Gupta 62bdd2596dSAchin Gupta assert(spmc_ctx != NULL); 63bdd2596dSAchin Gupta 64bdd2596dSAchin Gupta cm_set_context(&(spmc_ctx->cpu_ctx), SECURE); 65bdd2596dSAchin Gupta 66bdd2596dSAchin Gupta /* Restore the context assigned above */ 67bdd2596dSAchin Gupta cm_el1_sysregs_context_restore(SECURE); 6828f39f02SMax Shvetsov cm_el2_sysregs_context_restore(SECURE); 69bdd2596dSAchin Gupta cm_set_next_eret_context(SECURE); 70bdd2596dSAchin Gupta 71bdd2596dSAchin Gupta /* Invalidate TLBs at EL1. */ 72bdd2596dSAchin Gupta tlbivmalle1(); 73bdd2596dSAchin Gupta dsbish(); 74bdd2596dSAchin Gupta 75bdd2596dSAchin Gupta /* Enter Secure Partition */ 76bdd2596dSAchin Gupta rc = spmd_spm_core_enter(&spmc_ctx->c_rt_ctx); 77bdd2596dSAchin Gupta 78bdd2596dSAchin Gupta /* Save secure state */ 79bdd2596dSAchin Gupta cm_el1_sysregs_context_save(SECURE); 8028f39f02SMax Shvetsov cm_el2_sysregs_context_save(SECURE); 81bdd2596dSAchin Gupta 82bdd2596dSAchin Gupta return rc; 83bdd2596dSAchin Gupta } 84bdd2596dSAchin Gupta 85bdd2596dSAchin Gupta /******************************************************************************* 86bdd2596dSAchin Gupta * This function returns to the place where spm_sp_synchronous_entry() was 87bdd2596dSAchin Gupta * called originally. 88bdd2596dSAchin Gupta ******************************************************************************/ 89bdd2596dSAchin Gupta __dead2 void spmd_spm_core_sync_exit(uint64_t rc) 90bdd2596dSAchin Gupta { 91bdd2596dSAchin Gupta spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()]; 92bdd2596dSAchin Gupta 93bdd2596dSAchin Gupta /* Get context of the SP in use by this CPU. */ 94bdd2596dSAchin Gupta assert(cm_get_context(SECURE) == &(ctx->cpu_ctx)); 95bdd2596dSAchin Gupta 96bdd2596dSAchin Gupta /* 97bdd2596dSAchin Gupta * The SPMD must have initiated the original request through a 98bdd2596dSAchin Gupta * synchronous entry into SPMC. Jump back to the original C runtime 99bdd2596dSAchin Gupta * context with the value of rc in x0; 100bdd2596dSAchin Gupta */ 101bdd2596dSAchin Gupta spmd_spm_core_exit(ctx->c_rt_ctx, rc); 102bdd2596dSAchin Gupta 103bdd2596dSAchin Gupta panic(); 104bdd2596dSAchin Gupta } 105bdd2596dSAchin Gupta 106bdd2596dSAchin Gupta /******************************************************************************* 107bdd2596dSAchin Gupta * Jump to the SPM core for the first time. 108bdd2596dSAchin Gupta ******************************************************************************/ 109bdd2596dSAchin Gupta static int32_t spmd_init(void) 110bdd2596dSAchin Gupta { 111bdd2596dSAchin Gupta uint64_t rc = 0; 112bdd2596dSAchin Gupta spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()]; 113bdd2596dSAchin Gupta 114bdd2596dSAchin Gupta INFO("SPM Core init start.\n"); 115bdd2596dSAchin Gupta ctx->state = SPMC_STATE_RESET; 116bdd2596dSAchin Gupta 117bdd2596dSAchin Gupta rc = spmd_spm_core_sync_entry(ctx); 118bdd2596dSAchin Gupta if (rc) { 119bdd2596dSAchin Gupta ERROR("SPMC initialisation failed 0x%llx\n", rc); 120bdd2596dSAchin Gupta panic(); 121bdd2596dSAchin Gupta } 122bdd2596dSAchin Gupta 123bdd2596dSAchin Gupta ctx->state = SPMC_STATE_IDLE; 124bdd2596dSAchin Gupta INFO("SPM Core init end.\n"); 125bdd2596dSAchin Gupta 126bdd2596dSAchin Gupta return 1; 127bdd2596dSAchin Gupta } 128bdd2596dSAchin Gupta 129bdd2596dSAchin Gupta /******************************************************************************* 1300f14d02fSMax Shvetsov * Load SPMC manifest, init SPMC. 1310f14d02fSMax Shvetsov ******************************************************************************/ 1320f14d02fSMax Shvetsov static int spmd_spmc_init(void *rd_base, size_t rd_size) 1330f14d02fSMax Shvetsov { 1340f14d02fSMax Shvetsov int rc; 1350f14d02fSMax Shvetsov uint32_t ep_attr; 1360f14d02fSMax Shvetsov unsigned int linear_id = plat_my_core_pos(); 1370f14d02fSMax Shvetsov spmd_spm_core_context_t *spm_ctx = &spm_core_context[linear_id]; 1380f14d02fSMax Shvetsov 1390f14d02fSMax Shvetsov /* Load the SPM core manifest */ 1400f14d02fSMax Shvetsov rc = plat_spm_core_manifest_load(&spmc_attrs, rd_base, rd_size); 1410f14d02fSMax Shvetsov if (rc != 0) { 1420f14d02fSMax Shvetsov WARN("No or invalid SPM core manifest image provided by BL2 " 1430f14d02fSMax Shvetsov "boot loader. "); 1440f14d02fSMax Shvetsov return 1; 1450f14d02fSMax Shvetsov } 1460f14d02fSMax Shvetsov 1470f14d02fSMax Shvetsov /* 1480f14d02fSMax Shvetsov * Ensure that the SPM core version is compatible with the SPM 1490f14d02fSMax Shvetsov * dispatcher version 1500f14d02fSMax Shvetsov */ 1510f14d02fSMax Shvetsov if ((spmc_attrs.major_version != SPCI_VERSION_MAJOR) || 1520f14d02fSMax Shvetsov (spmc_attrs.minor_version > SPCI_VERSION_MINOR)) { 1530f14d02fSMax Shvetsov WARN("Unsupported SPCI version (%x.%x) specified in SPM core " 1540f14d02fSMax Shvetsov "manifest image provided by BL2 boot loader.\n", 1550f14d02fSMax Shvetsov spmc_attrs.major_version, spmc_attrs.minor_version); 1560f14d02fSMax Shvetsov return 1; 1570f14d02fSMax Shvetsov } 1580f14d02fSMax Shvetsov 1590f14d02fSMax Shvetsov INFO("SPCI version (%x.%x).\n", spmc_attrs.major_version, 1600f14d02fSMax Shvetsov spmc_attrs.minor_version); 1610f14d02fSMax Shvetsov 1620f14d02fSMax Shvetsov /* Validate the SPM core runtime EL */ 1630f14d02fSMax Shvetsov if ((spmc_attrs.runtime_el != MODE_EL1) && 1640f14d02fSMax Shvetsov (spmc_attrs.runtime_el != MODE_EL2)) { 1650f14d02fSMax Shvetsov WARN("Unsupported SPM core run time EL%x specified in " 1660f14d02fSMax Shvetsov "manifest image provided by BL2 boot loader.\n", 1670f14d02fSMax Shvetsov spmc_attrs.runtime_el); 1680f14d02fSMax Shvetsov return 1; 1690f14d02fSMax Shvetsov } 1700f14d02fSMax Shvetsov 1710f14d02fSMax Shvetsov INFO("SPM core run time EL%x.\n", spmc_attrs.runtime_el); 1720f14d02fSMax Shvetsov 1730f14d02fSMax Shvetsov /* Validate the SPM core execution state */ 1740f14d02fSMax Shvetsov if ((spmc_attrs.exec_state != MODE_RW_64) && 1750f14d02fSMax Shvetsov (spmc_attrs.exec_state != MODE_RW_32)) { 1760f14d02fSMax Shvetsov WARN("Unsupported SPM core execution state %x specified in " 1770f14d02fSMax Shvetsov "manifest image provided by BL2 boot loader.\n", 1780f14d02fSMax Shvetsov spmc_attrs.exec_state); 1790f14d02fSMax Shvetsov return 1; 1800f14d02fSMax Shvetsov } 1810f14d02fSMax Shvetsov 1820f14d02fSMax Shvetsov INFO("SPM core execution state %x.\n", spmc_attrs.exec_state); 1830f14d02fSMax Shvetsov 1840f14d02fSMax Shvetsov /* Ensure manifest has not requested S-EL2 in AArch32 state */ 1850f14d02fSMax Shvetsov if ((spmc_attrs.exec_state == MODE_RW_32) && 1860f14d02fSMax Shvetsov (spmc_attrs.runtime_el == MODE_EL2)) { 1870f14d02fSMax Shvetsov WARN("Invalid combination of SPM core execution state (%x) " 1880f14d02fSMax Shvetsov "and run time EL (%x).\n", spmc_attrs.exec_state, 1890f14d02fSMax Shvetsov spmc_attrs.runtime_el); 1900f14d02fSMax Shvetsov return 1; 1910f14d02fSMax Shvetsov } 1920f14d02fSMax Shvetsov 1930f14d02fSMax Shvetsov /* 1940f14d02fSMax Shvetsov * Check if S-EL2 is supported on this system if S-EL2 1950f14d02fSMax Shvetsov * is required for SPM 1960f14d02fSMax Shvetsov */ 1970f14d02fSMax Shvetsov if (spmc_attrs.runtime_el == MODE_EL2) { 1980f14d02fSMax Shvetsov uint64_t sel2 = read_id_aa64pfr0_el1(); 1990f14d02fSMax Shvetsov 2000f14d02fSMax Shvetsov sel2 >>= ID_AA64PFR0_SEL2_SHIFT; 2010f14d02fSMax Shvetsov sel2 &= ID_AA64PFR0_SEL2_MASK; 2020f14d02fSMax Shvetsov 2030f14d02fSMax Shvetsov if (!sel2) { 2040f14d02fSMax Shvetsov WARN("SPM core run time EL: S-EL%x is not supported " 2050f14d02fSMax Shvetsov "but specified in manifest image provided by " 2060f14d02fSMax Shvetsov "BL2 boot loader.\n", spmc_attrs.runtime_el); 2070f14d02fSMax Shvetsov return 1; 2080f14d02fSMax Shvetsov } 2090f14d02fSMax Shvetsov } 2100f14d02fSMax Shvetsov 2110f14d02fSMax Shvetsov /* Initialise an entrypoint to set up the CPU context */ 2120f14d02fSMax Shvetsov ep_attr = SECURE | EP_ST_ENABLE; 2130f14d02fSMax Shvetsov if (read_sctlr_el3() & SCTLR_EE_BIT) { 2140f14d02fSMax Shvetsov ep_attr |= EP_EE_BIG; 2150f14d02fSMax Shvetsov } 2160f14d02fSMax Shvetsov 2170f14d02fSMax Shvetsov SET_PARAM_HEAD(spmc_ep_info, PARAM_EP, VERSION_1, ep_attr); 2180f14d02fSMax Shvetsov assert(spmc_ep_info->pc == BL32_BASE); 2190f14d02fSMax Shvetsov 2200f14d02fSMax Shvetsov /* 2210f14d02fSMax Shvetsov * Populate SPSR for SPM core based upon validated parameters from the 2220f14d02fSMax Shvetsov * manifest 2230f14d02fSMax Shvetsov */ 2240f14d02fSMax Shvetsov if (spmc_attrs.exec_state == MODE_RW_32) { 2250f14d02fSMax Shvetsov spmc_ep_info->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, 2260f14d02fSMax Shvetsov SPSR_E_LITTLE, 2270f14d02fSMax Shvetsov DAIF_FIQ_BIT | 2280f14d02fSMax Shvetsov DAIF_IRQ_BIT | 2290f14d02fSMax Shvetsov DAIF_ABT_BIT); 2300f14d02fSMax Shvetsov } else { 2310f14d02fSMax Shvetsov spmc_ep_info->spsr = SPSR_64(spmc_attrs.runtime_el, 2320f14d02fSMax Shvetsov MODE_SP_ELX, 2330f14d02fSMax Shvetsov DISABLE_ALL_EXCEPTIONS); 2340f14d02fSMax Shvetsov } 2350f14d02fSMax Shvetsov 2360f14d02fSMax Shvetsov /* Initialise SPM core context with this entry point information */ 2370f14d02fSMax Shvetsov cm_setup_context(&spm_ctx->cpu_ctx, spmc_ep_info); 2380f14d02fSMax Shvetsov 2390f14d02fSMax Shvetsov /* Reuse PSCI affinity states to mark this SPMC context as off */ 2400f14d02fSMax Shvetsov spm_ctx->state = AFF_STATE_OFF; 2410f14d02fSMax Shvetsov 2420f14d02fSMax Shvetsov INFO("SPM core setup done.\n"); 2430f14d02fSMax Shvetsov 2440f14d02fSMax Shvetsov /* Register init function for deferred init. */ 2450f14d02fSMax Shvetsov bl31_register_bl32_init(&spmd_init); 2460f14d02fSMax Shvetsov 2470f14d02fSMax Shvetsov return 0; 2480f14d02fSMax Shvetsov } 2490f14d02fSMax Shvetsov 2500f14d02fSMax Shvetsov /******************************************************************************* 251bdd2596dSAchin Gupta * Initialize context of SPM core. 252bdd2596dSAchin Gupta ******************************************************************************/ 2530f14d02fSMax Shvetsov int spmd_setup(void) 254bdd2596dSAchin Gupta { 255bdd2596dSAchin Gupta int rc; 256bdd2596dSAchin Gupta void *rd_base; 257bdd2596dSAchin Gupta size_t rd_size; 258bdd2596dSAchin Gupta uintptr_t rd_base_align; 259bdd2596dSAchin Gupta uintptr_t rd_size_align; 260bdd2596dSAchin Gupta 261bdd2596dSAchin Gupta spmc_ep_info = bl31_plat_get_next_image_ep_info(SECURE); 262bdd2596dSAchin Gupta if (!spmc_ep_info) { 263bdd2596dSAchin Gupta WARN("No SPM core image provided by BL2 boot loader, Booting " 264bdd2596dSAchin Gupta "device without SP initialization. SMC`s destined for SPM " 265bdd2596dSAchin Gupta "core will return SMC_UNK\n"); 266bdd2596dSAchin Gupta return 1; 267bdd2596dSAchin Gupta } 268bdd2596dSAchin Gupta 269bdd2596dSAchin Gupta /* Under no circumstances will this parameter be 0 */ 270bdd2596dSAchin Gupta assert(spmc_ep_info->pc != 0U); 271bdd2596dSAchin Gupta 272bdd2596dSAchin Gupta /* 273bdd2596dSAchin Gupta * Check if BL32 ep_info has a reference to 'tos_fw_config'. This will 274bdd2596dSAchin Gupta * be used as a manifest for the SPM core at the next lower EL/mode. 275bdd2596dSAchin Gupta */ 276bdd2596dSAchin Gupta if (spmc_ep_info->args.arg0 == 0U || spmc_ep_info->args.arg2 == 0U) { 277bdd2596dSAchin Gupta ERROR("Invalid or absent SPM core manifest\n"); 278bdd2596dSAchin Gupta panic(); 279bdd2596dSAchin Gupta } 280bdd2596dSAchin Gupta 281bdd2596dSAchin Gupta /* Obtain whereabouts of SPM core manifest */ 282bdd2596dSAchin Gupta rd_base = (void *) spmc_ep_info->args.arg0; 283bdd2596dSAchin Gupta rd_size = spmc_ep_info->args.arg2; 284bdd2596dSAchin Gupta 285bdd2596dSAchin Gupta rd_base_align = page_align((uintptr_t) rd_base, DOWN); 286bdd2596dSAchin Gupta rd_size_align = page_align((uintptr_t) rd_size, UP); 287bdd2596dSAchin Gupta 288bdd2596dSAchin Gupta /* Map the manifest in the SPMD translation regime first */ 289bdd2596dSAchin Gupta VERBOSE("SPM core manifest base : 0x%lx\n", rd_base_align); 290bdd2596dSAchin Gupta VERBOSE("SPM core manifest size : 0x%lx\n", rd_size_align); 291bdd2596dSAchin Gupta rc = mmap_add_dynamic_region((unsigned long long) rd_base_align, 292bdd2596dSAchin Gupta (uintptr_t) rd_base_align, 293bdd2596dSAchin Gupta rd_size_align, 294bdd2596dSAchin Gupta MT_RO_DATA); 2950f14d02fSMax Shvetsov if (rc != 0) { 296bdd2596dSAchin Gupta ERROR("Error while mapping SPM core manifest (%d).\n", rc); 297bdd2596dSAchin Gupta panic(); 298bdd2596dSAchin Gupta } 299bdd2596dSAchin Gupta 3000f14d02fSMax Shvetsov /* Load manifest, init SPMC */ 3010f14d02fSMax Shvetsov rc = spmd_spmc_init(rd_base, rd_size); 3020f14d02fSMax Shvetsov if (rc != 0) { 3030f14d02fSMax Shvetsov int mmap_rc; 304bdd2596dSAchin Gupta 305bdd2596dSAchin Gupta WARN("Booting device without SPM initialization. " 306bdd2596dSAchin Gupta "SPCI SMCs destined for SPM core will return " 307bdd2596dSAchin Gupta "ENOTSUPPORTED\n"); 308bdd2596dSAchin Gupta 3090f14d02fSMax Shvetsov mmap_rc = mmap_remove_dynamic_region(rd_base_align, 3100f14d02fSMax Shvetsov rd_size_align); 3110f14d02fSMax Shvetsov if (mmap_rc != 0) { 312bdd2596dSAchin Gupta ERROR("Error while unmapping SPM core manifest (%d).\n", 3130f14d02fSMax Shvetsov mmap_rc); 314bdd2596dSAchin Gupta panic(); 315bdd2596dSAchin Gupta } 316bdd2596dSAchin Gupta 3170f14d02fSMax Shvetsov return rc; 3180f14d02fSMax Shvetsov } 3190f14d02fSMax Shvetsov 3200f14d02fSMax Shvetsov return 0; 3210f14d02fSMax Shvetsov } 3220f14d02fSMax Shvetsov 3230f14d02fSMax Shvetsov /******************************************************************************* 3240f14d02fSMax Shvetsov * Forward SMC to the other security state 3250f14d02fSMax Shvetsov ******************************************************************************/ 326*93ff138bSOlivier Deprez static uint64_t spmd_smc_forward(uint32_t smc_fid, bool secure_origin, 327*93ff138bSOlivier Deprez uint64_t x1, uint64_t x2, uint64_t x3, 328*93ff138bSOlivier Deprez uint64_t x4, void *handle) 3290f14d02fSMax Shvetsov { 330*93ff138bSOlivier Deprez uint32_t secure_state_in = (secure_origin) ? SECURE : NON_SECURE; 331*93ff138bSOlivier Deprez uint32_t secure_state_out = (!secure_origin) ? SECURE : NON_SECURE; 332*93ff138bSOlivier Deprez 3330f14d02fSMax Shvetsov /* Save incoming security state */ 334*93ff138bSOlivier Deprez cm_el1_sysregs_context_save(secure_state_in); 335*93ff138bSOlivier Deprez cm_el2_sysregs_context_save(secure_state_in); 3360f14d02fSMax Shvetsov 3370f14d02fSMax Shvetsov /* Restore outgoing security state */ 338*93ff138bSOlivier Deprez cm_el1_sysregs_context_restore(secure_state_out); 339*93ff138bSOlivier Deprez cm_el2_sysregs_context_restore(secure_state_out); 340*93ff138bSOlivier Deprez cm_set_next_eret_context(secure_state_out); 3410f14d02fSMax Shvetsov 342*93ff138bSOlivier Deprez SMC_RET8(cm_get_context(secure_state_out), smc_fid, x1, x2, x3, x4, 3430f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X5), 3440f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X6), 3450f14d02fSMax Shvetsov SMC_GET_GP(handle, CTX_GPREG_X7)); 3460f14d02fSMax Shvetsov } 3470f14d02fSMax Shvetsov 3480f14d02fSMax Shvetsov /******************************************************************************* 3490f14d02fSMax Shvetsov * Return SPCI_ERROR with specified error code 3500f14d02fSMax Shvetsov ******************************************************************************/ 3510f14d02fSMax Shvetsov static uint64_t spmd_spci_error_return(void *handle, int error_code) 3520f14d02fSMax Shvetsov { 3530f14d02fSMax Shvetsov SMC_RET8(handle, SPCI_ERROR, 3540f14d02fSMax Shvetsov SPCI_TARGET_INFO_MBZ, error_code, 3550f14d02fSMax Shvetsov SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, 3560f14d02fSMax Shvetsov SPCI_PARAM_MBZ, SPCI_PARAM_MBZ); 357bdd2596dSAchin Gupta } 358bdd2596dSAchin Gupta 359bdd2596dSAchin Gupta /******************************************************************************* 360bdd2596dSAchin Gupta * This function handles all SMCs in the range reserved for SPCI. Each call is 361bdd2596dSAchin Gupta * either forwarded to the other security state or handled by the SPM dispatcher 362bdd2596dSAchin Gupta ******************************************************************************/ 363bdd2596dSAchin Gupta uint64_t spmd_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, 364bdd2596dSAchin Gupta uint64_t x3, uint64_t x4, void *cookie, void *handle, 365bdd2596dSAchin Gupta uint64_t flags) 366bdd2596dSAchin Gupta { 367bdd2596dSAchin Gupta spmd_spm_core_context_t *ctx = &spm_core_context[plat_my_core_pos()]; 368*93ff138bSOlivier Deprez bool secure_origin; 369*93ff138bSOlivier Deprez int32_t ret; 370bdd2596dSAchin Gupta 371bdd2596dSAchin Gupta /* Determine which security state this SMC originated from */ 372*93ff138bSOlivier Deprez secure_origin = is_caller_secure(flags); 373bdd2596dSAchin Gupta 374bdd2596dSAchin Gupta INFO("SPM: 0x%x, 0x%llx, 0x%llx, 0x%llx, 0x%llx, " 375bdd2596dSAchin Gupta "0x%llx, 0x%llx, 0x%llx\n", 376bdd2596dSAchin Gupta smc_fid, x1, x2, x3, x4, SMC_GET_GP(handle, CTX_GPREG_X5), 377bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 378bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 379bdd2596dSAchin Gupta 380bdd2596dSAchin Gupta switch (smc_fid) { 381bdd2596dSAchin Gupta case SPCI_ERROR: 382bdd2596dSAchin Gupta /* 383bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 384bdd2596dSAchin Gupta * this CPU. If so, then indicate that the SPM core initialised 385bdd2596dSAchin Gupta * unsuccessfully. 386bdd2596dSAchin Gupta */ 387*93ff138bSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_RESET)) { 388bdd2596dSAchin Gupta spmd_spm_core_sync_exit(x2); 3890f14d02fSMax Shvetsov } 390bdd2596dSAchin Gupta 391*93ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 3920f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 393bdd2596dSAchin Gupta break; /* not reached */ 394bdd2596dSAchin Gupta 395bdd2596dSAchin Gupta case SPCI_VERSION: 396bdd2596dSAchin Gupta /* 397bdd2596dSAchin Gupta * TODO: This is an optimization that the version information 398bdd2596dSAchin Gupta * provided by the SPM core manifest is returned by the SPM 399bdd2596dSAchin Gupta * dispatcher. It might be a better idea to simply forward this 400bdd2596dSAchin Gupta * call to the SPM core and wash our hands completely. 401bdd2596dSAchin Gupta */ 402bdd2596dSAchin Gupta ret = MAKE_SPCI_VERSION(spmc_attrs.major_version, 403bdd2596dSAchin Gupta spmc_attrs.minor_version); 404bdd2596dSAchin Gupta SMC_RET8(handle, SPCI_SUCCESS_SMC32, SPCI_TARGET_INFO_MBZ, ret, 405bdd2596dSAchin Gupta SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, SPCI_PARAM_MBZ, 406bdd2596dSAchin Gupta SPCI_PARAM_MBZ, SPCI_PARAM_MBZ); 407bdd2596dSAchin Gupta break; /* not reached */ 408bdd2596dSAchin Gupta 409bdd2596dSAchin Gupta case SPCI_FEATURES: 410bdd2596dSAchin Gupta /* 411bdd2596dSAchin Gupta * This is an optional interface. Do the minimal checks and 412bdd2596dSAchin Gupta * forward to SPM core which will handle it if implemented. 413bdd2596dSAchin Gupta */ 414bdd2596dSAchin Gupta 415bdd2596dSAchin Gupta /* 4160f14d02fSMax Shvetsov * Check if x1 holds a valid SPCI fid. This is an 417bdd2596dSAchin Gupta * optimization. 418bdd2596dSAchin Gupta */ 4190f14d02fSMax Shvetsov if (!is_spci_fid(x1)) { 4200f14d02fSMax Shvetsov return spmd_spci_error_return(handle, 4210f14d02fSMax Shvetsov SPCI_ERROR_NOT_SUPPORTED); 4220f14d02fSMax Shvetsov } 423bdd2596dSAchin Gupta 424bdd2596dSAchin Gupta /* Forward SMC from Normal world to the SPM core */ 425*93ff138bSOlivier Deprez if (!secure_origin) { 426*93ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4270f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 428bdd2596dSAchin Gupta } else { 429bdd2596dSAchin Gupta /* 430bdd2596dSAchin Gupta * Return success if call was from secure world i.e. all 431bdd2596dSAchin Gupta * SPCI functions are supported. This is essentially a 432bdd2596dSAchin Gupta * nop. 433bdd2596dSAchin Gupta */ 434bdd2596dSAchin Gupta SMC_RET8(handle, SPCI_SUCCESS_SMC32, x1, x2, x3, x4, 435bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X5), 436bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X6), 437bdd2596dSAchin Gupta SMC_GET_GP(handle, CTX_GPREG_X7)); 438bdd2596dSAchin Gupta } 4390f14d02fSMax Shvetsov 440bdd2596dSAchin Gupta break; /* not reached */ 441bdd2596dSAchin Gupta 442bdd2596dSAchin Gupta case SPCI_RX_RELEASE: 443bdd2596dSAchin Gupta case SPCI_RXTX_MAP_SMC32: 444bdd2596dSAchin Gupta case SPCI_RXTX_MAP_SMC64: 445bdd2596dSAchin Gupta case SPCI_RXTX_UNMAP: 446bdd2596dSAchin Gupta case SPCI_MSG_RUN: 447bdd2596dSAchin Gupta /* This interface must be invoked only by the Normal world */ 448*93ff138bSOlivier Deprez if (secure_origin) { 4490f14d02fSMax Shvetsov return spmd_spci_error_return(handle, 4500f14d02fSMax Shvetsov SPCI_ERROR_NOT_SUPPORTED); 451bdd2596dSAchin Gupta } 452bdd2596dSAchin Gupta 453bdd2596dSAchin Gupta /* Fall through to forward the call to the other world */ 454bdd2596dSAchin Gupta 455bdd2596dSAchin Gupta case SPCI_PARTITION_INFO_GET: 456bdd2596dSAchin Gupta case SPCI_MSG_SEND: 457bdd2596dSAchin Gupta case SPCI_MSG_SEND_DIRECT_REQ_SMC32: 458bdd2596dSAchin Gupta case SPCI_MSG_SEND_DIRECT_REQ_SMC64: 459bdd2596dSAchin Gupta case SPCI_MSG_SEND_DIRECT_RESP_SMC32: 460bdd2596dSAchin Gupta case SPCI_MSG_SEND_DIRECT_RESP_SMC64: 461bdd2596dSAchin Gupta case SPCI_MEM_DONATE_SMC32: 462bdd2596dSAchin Gupta case SPCI_MEM_DONATE_SMC64: 463bdd2596dSAchin Gupta case SPCI_MEM_LEND_SMC32: 464bdd2596dSAchin Gupta case SPCI_MEM_LEND_SMC64: 465bdd2596dSAchin Gupta case SPCI_MEM_SHARE_SMC32: 466bdd2596dSAchin Gupta case SPCI_MEM_SHARE_SMC64: 467bdd2596dSAchin Gupta case SPCI_MEM_RETRIEVE_REQ_SMC32: 468bdd2596dSAchin Gupta case SPCI_MEM_RETRIEVE_REQ_SMC64: 469bdd2596dSAchin Gupta case SPCI_MEM_RETRIEVE_RESP: 470bdd2596dSAchin Gupta case SPCI_MEM_RELINQUISH: 471bdd2596dSAchin Gupta case SPCI_MEM_RECLAIM: 472bdd2596dSAchin Gupta case SPCI_SUCCESS_SMC32: 473bdd2596dSAchin Gupta case SPCI_SUCCESS_SMC64: 474bdd2596dSAchin Gupta /* 475bdd2596dSAchin Gupta * TODO: Assume that no requests originate from EL3 at the 476bdd2596dSAchin Gupta * moment. This will change if a SP service is required in 477bdd2596dSAchin Gupta * response to secure interrupts targeted to EL3. Until then 478bdd2596dSAchin Gupta * simply forward the call to the Normal world. 479bdd2596dSAchin Gupta */ 480bdd2596dSAchin Gupta 481*93ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 4820f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 483bdd2596dSAchin Gupta break; /* not reached */ 484bdd2596dSAchin Gupta 485bdd2596dSAchin Gupta case SPCI_MSG_WAIT: 486bdd2596dSAchin Gupta /* 487bdd2596dSAchin Gupta * Check if this is the first invocation of this interface on 488bdd2596dSAchin Gupta * this CPU from the Secure world. If so, then indicate that the 489bdd2596dSAchin Gupta * SPM core initialised successfully. 490bdd2596dSAchin Gupta */ 491*93ff138bSOlivier Deprez if (secure_origin && (ctx->state == SPMC_STATE_RESET)) { 492bdd2596dSAchin Gupta spmd_spm_core_sync_exit(0); 493bdd2596dSAchin Gupta } 494bdd2596dSAchin Gupta 4950f14d02fSMax Shvetsov /* Fall through to forward the call to the other world */ 496bdd2596dSAchin Gupta 497bdd2596dSAchin Gupta case SPCI_MSG_YIELD: 498bdd2596dSAchin Gupta /* This interface must be invoked only by the Secure world */ 499*93ff138bSOlivier Deprez if (!secure_origin) { 5000f14d02fSMax Shvetsov return spmd_spci_error_return(handle, 5010f14d02fSMax Shvetsov SPCI_ERROR_NOT_SUPPORTED); 502bdd2596dSAchin Gupta } 503bdd2596dSAchin Gupta 504*93ff138bSOlivier Deprez return spmd_smc_forward(smc_fid, secure_origin, 5050f14d02fSMax Shvetsov x1, x2, x3, x4, handle); 506bdd2596dSAchin Gupta break; /* not reached */ 507bdd2596dSAchin Gupta 508bdd2596dSAchin Gupta default: 509bdd2596dSAchin Gupta WARN("SPM: Unsupported call 0x%08x\n", smc_fid); 5100f14d02fSMax Shvetsov return spmd_spci_error_return(handle, SPCI_ERROR_NOT_SUPPORTED); 511bdd2596dSAchin Gupta } 512bdd2596dSAchin Gupta } 513