164f6ea9bSJeenu Viswambharan /* 22a7b403dSAchin Gupta * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved. 364f6ea9bSJeenu Viswambharan * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 564f6ea9bSJeenu Viswambharan */ 664f6ea9bSJeenu Viswambharan 758e946aeSSoby Mathew #include <assert.h> 897043ac9SDan Handley #include <stdint.h> 909d40e0eSAntonio Nino Diaz 1009d40e0eSAntonio Nino Diaz #include <common/debug.h> 1109d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 1209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/cpu_data.h> 1309d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 1409d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 1509d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 1609d40e0eSAntonio Nino Diaz #include <services/sdei.h> 170bf9f567SPaul Beesley #include <services/spm_mm_svc.h> 182a7b403dSAchin Gupta #include <services/spmd_svc.h> 1909d40e0eSAntonio Nino Diaz #include <services/std_svc.h> 2009d40e0eSAntonio Nino Diaz #include <smccc_helpers.h> 2109d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 2264f6ea9bSJeenu Viswambharan 2364f6ea9bSJeenu Viswambharan /* Standard Service UUID */ 2403364865SRoberto Vargas static uuid_t arm_svc_uid = { 2503364865SRoberto Vargas {0x5b, 0x90, 0x8d, 0x10}, 2603364865SRoberto Vargas {0x63, 0xf8}, 2703364865SRoberto Vargas {0xe8, 0x47}, 2803364865SRoberto Vargas 0xae, 0x2d, 2903364865SRoberto Vargas {0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2} 3003364865SRoberto Vargas }; 3164f6ea9bSJeenu Viswambharan 3258e946aeSSoby Mathew /* Setup Standard Services */ 3358e946aeSSoby Mathew static int32_t std_svc_setup(void) 3458e946aeSSoby Mathew { 3558e946aeSSoby Mathew uintptr_t svc_arg; 362fccb228SAntonio Nino Diaz int ret = 0; 3758e946aeSSoby Mathew 3858e946aeSSoby Mathew svc_arg = get_arm_std_svc_args(PSCI_FID_MASK); 3958e946aeSSoby Mathew assert(svc_arg); 4058e946aeSSoby Mathew 4158e946aeSSoby Mathew /* 422fccb228SAntonio Nino Diaz * PSCI is one of the specifications implemented as a Standard Service. 4358e946aeSSoby Mathew * The `psci_setup()` also does EL3 architectural setup. 4458e946aeSSoby Mathew */ 452fccb228SAntonio Nino Diaz if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) { 462fccb228SAntonio Nino Diaz ret = 1; 472fccb228SAntonio Nino Diaz } 482fccb228SAntonio Nino Diaz 49538b0020SPaul Beesley #if SPM_MM 500bf9f567SPaul Beesley if (spm_mm_setup() != 0) { 512fccb228SAntonio Nino Diaz ret = 1; 522fccb228SAntonio Nino Diaz } 532fccb228SAntonio Nino Diaz #endif 542fccb228SAntonio Nino Diaz 552a7b403dSAchin Gupta #if defined(SPD_spmd) 562a7b403dSAchin Gupta if (spmd_setup() != 0) { 572a7b403dSAchin Gupta ret = 1; 582a7b403dSAchin Gupta } 592a7b403dSAchin Gupta #endif 602a7b403dSAchin Gupta 61b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT 62b7cb133eSJeenu Viswambharan /* SDEI initialisation */ 63b7cb133eSJeenu Viswambharan sdei_init(); 64b7cb133eSJeenu Viswambharan #endif 65b7cb133eSJeenu Viswambharan 662fccb228SAntonio Nino Diaz return ret; 6758e946aeSSoby Mathew } 6858e946aeSSoby Mathew 6964f6ea9bSJeenu Viswambharan /* 7064f6ea9bSJeenu Viswambharan * Top-level Standard Service SMC handler. This handler will in turn dispatch 7164f6ea9bSJeenu Viswambharan * calls to PSCI SMC handler 7264f6ea9bSJeenu Viswambharan */ 737fabe1a8SRoberto Vargas static uintptr_t std_svc_smc_handler(uint32_t smc_fid, 744c0d0390SSoby Mathew u_register_t x1, 754c0d0390SSoby Mathew u_register_t x2, 764c0d0390SSoby Mathew u_register_t x3, 774c0d0390SSoby Mathew u_register_t x4, 7864f6ea9bSJeenu Viswambharan void *cookie, 7964f6ea9bSJeenu Viswambharan void *handle, 804c0d0390SSoby Mathew u_register_t flags) 8164f6ea9bSJeenu Viswambharan { 8264f6ea9bSJeenu Viswambharan /* 8364f6ea9bSJeenu Viswambharan * Dispatch PSCI calls to PSCI SMC handler and return its return 8464f6ea9bSJeenu Viswambharan * value 8564f6ea9bSJeenu Viswambharan */ 8664f6ea9bSJeenu Viswambharan if (is_psci_fid(smc_fid)) { 87872be88aSdp-arm uint64_t ret; 88872be88aSdp-arm 89872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 90bfef6106Sdp-arm 91bfef6106Sdp-arm /* 92bfef6106Sdp-arm * Flush cache line so that even if CPU power down happens 93bfef6106Sdp-arm * the timestamp update is reflected in memory. 94bfef6106Sdp-arm */ 95872be88aSdp-arm PMF_WRITE_TIMESTAMP(rt_instr_svc, 96872be88aSdp-arm RT_INSTR_ENTER_PSCI, 97bfef6106Sdp-arm PMF_CACHE_MAINT, 98872be88aSdp-arm get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX])); 99872be88aSdp-arm #endif 100872be88aSdp-arm 101872be88aSdp-arm ret = psci_smc_handler(smc_fid, x1, x2, x3, x4, 102872be88aSdp-arm cookie, handle, flags); 103872be88aSdp-arm 104872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 105872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 106872be88aSdp-arm RT_INSTR_EXIT_PSCI, 107872be88aSdp-arm PMF_NO_CACHE_MAINT); 108872be88aSdp-arm #endif 109872be88aSdp-arm 110872be88aSdp-arm SMC_RET1(handle, ret); 11164f6ea9bSJeenu Viswambharan } 11264f6ea9bSJeenu Viswambharan 1133f3c341aSPaul Beesley #if SPM_MM 1142fccb228SAntonio Nino Diaz /* 1152fccb228SAntonio Nino Diaz * Dispatch SPM calls to SPM SMC handler and return its return 1162fccb228SAntonio Nino Diaz * value 1172fccb228SAntonio Nino Diaz */ 1180bf9f567SPaul Beesley if (is_spm_mm_fid(smc_fid)) { 1190bf9f567SPaul Beesley return spm_mm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 1202fccb228SAntonio Nino Diaz handle, flags); 1212fccb228SAntonio Nino Diaz } 1222fccb228SAntonio Nino Diaz #endif 1232fccb228SAntonio Nino Diaz 1242a7b403dSAchin Gupta #if defined(SPD_spmd) 1252a7b403dSAchin Gupta /* 126*662af36dSJ-Alves * Dispatch FFA calls to the FFA SMC handler implemented by the SPM 1272a7b403dSAchin Gupta * dispatcher and return its return value 1282a7b403dSAchin Gupta */ 129*662af36dSJ-Alves if (is_ffa_fid(smc_fid)) { 1302a7b403dSAchin Gupta return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 1312a7b403dSAchin Gupta handle, flags); 1322a7b403dSAchin Gupta } 1332a7b403dSAchin Gupta #endif 1342a7b403dSAchin Gupta 135b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT 136b7cb133eSJeenu Viswambharan if (is_sdei_fid(smc_fid)) { 137b7cb133eSJeenu Viswambharan return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 138b7cb133eSJeenu Viswambharan flags); 139b7cb133eSJeenu Viswambharan } 140b7cb133eSJeenu Viswambharan #endif 141b7cb133eSJeenu Viswambharan 14264f6ea9bSJeenu Viswambharan switch (smc_fid) { 14364f6ea9bSJeenu Viswambharan case ARM_STD_SVC_CALL_COUNT: 14464f6ea9bSJeenu Viswambharan /* 14564f6ea9bSJeenu Viswambharan * Return the number of Standard Service Calls. PSCI is the only 14664f6ea9bSJeenu Viswambharan * standard service implemented; so return number of PSCI calls 14764f6ea9bSJeenu Viswambharan */ 14864f6ea9bSJeenu Viswambharan SMC_RET1(handle, PSCI_NUM_CALLS); 14964f6ea9bSJeenu Viswambharan 15064f6ea9bSJeenu Viswambharan case ARM_STD_SVC_UID: 15164f6ea9bSJeenu Viswambharan /* Return UID to the caller */ 15264f6ea9bSJeenu Viswambharan SMC_UUID_RET(handle, arm_svc_uid); 15364f6ea9bSJeenu Viswambharan 15464f6ea9bSJeenu Viswambharan case ARM_STD_SVC_VERSION: 15564f6ea9bSJeenu Viswambharan /* Return the version of current implementation */ 15664f6ea9bSJeenu Viswambharan SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR); 15764f6ea9bSJeenu Viswambharan 15864f6ea9bSJeenu Viswambharan default: 15964f6ea9bSJeenu Viswambharan WARN("Unimplemented Standard Service Call: 0x%x \n", smc_fid); 16064f6ea9bSJeenu Viswambharan SMC_RET1(handle, SMC_UNK); 16164f6ea9bSJeenu Viswambharan } 16264f6ea9bSJeenu Viswambharan } 16364f6ea9bSJeenu Viswambharan 16464f6ea9bSJeenu Viswambharan /* Register Standard Service Calls as runtime service */ 16564f6ea9bSJeenu Viswambharan DECLARE_RT_SVC( 16664f6ea9bSJeenu Viswambharan std_svc, 16764f6ea9bSJeenu Viswambharan 16864f6ea9bSJeenu Viswambharan OEN_STD_START, 16964f6ea9bSJeenu Viswambharan OEN_STD_END, 17064f6ea9bSJeenu Viswambharan SMC_TYPE_FAST, 17158e946aeSSoby Mathew std_svc_setup, 17264f6ea9bSJeenu Viswambharan std_svc_smc_handler 17364f6ea9bSJeenu Viswambharan ); 174