164f6ea9bSJeenu Viswambharan /* 2319fb084SSoby Mathew * Copyright (c) 2014-2022, 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> 16e62748e3SManish V Badarkhe #include <services/drtm_svc.h> 171cdf1eb8SJeremy Linton #include <services/pci_svc.h> 1877c27753SZelalem Aweke #include <services/rmmd_svc.h> 1909d40e0eSAntonio Nino Diaz #include <services/sdei.h> 200bf9f567SPaul Beesley #include <services/spm_mm_svc.h> 21bb01a673SMarc Bonnici #include <services/spmc_svc.h> 222a7b403dSAchin Gupta #include <services/spmd_svc.h> 2309d40e0eSAntonio Nino Diaz #include <services/std_svc.h> 247dfb9911SJimmy Brisson #include <services/trng_svc.h> 2509d40e0eSAntonio Nino Diaz #include <smccc_helpers.h> 2609d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 2764f6ea9bSJeenu Viswambharan 2864f6ea9bSJeenu Viswambharan /* Standard Service UUID */ 2903364865SRoberto Vargas static uuid_t arm_svc_uid = { 3003364865SRoberto Vargas {0x5b, 0x90, 0x8d, 0x10}, 3103364865SRoberto Vargas {0x63, 0xf8}, 3203364865SRoberto Vargas {0xe8, 0x47}, 3303364865SRoberto Vargas 0xae, 0x2d, 3403364865SRoberto Vargas {0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2} 3503364865SRoberto Vargas }; 3664f6ea9bSJeenu Viswambharan 3758e946aeSSoby Mathew /* Setup Standard Services */ 3858e946aeSSoby Mathew static int32_t std_svc_setup(void) 3958e946aeSSoby Mathew { 4058e946aeSSoby Mathew uintptr_t svc_arg; 412fccb228SAntonio Nino Diaz int ret = 0; 4258e946aeSSoby Mathew 4358e946aeSSoby Mathew svc_arg = get_arm_std_svc_args(PSCI_FID_MASK); 4458e946aeSSoby Mathew assert(svc_arg); 4558e946aeSSoby Mathew 4658e946aeSSoby Mathew /* 472fccb228SAntonio Nino Diaz * PSCI is one of the specifications implemented as a Standard Service. 4858e946aeSSoby Mathew * The `psci_setup()` also does EL3 architectural setup. 4958e946aeSSoby Mathew */ 502fccb228SAntonio Nino Diaz if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) { 512fccb228SAntonio Nino Diaz ret = 1; 522fccb228SAntonio Nino Diaz } 532fccb228SAntonio Nino Diaz 54538b0020SPaul Beesley #if SPM_MM 550bf9f567SPaul Beesley if (spm_mm_setup() != 0) { 562fccb228SAntonio Nino Diaz ret = 1; 572fccb228SAntonio Nino Diaz } 582fccb228SAntonio Nino Diaz #endif 592fccb228SAntonio Nino Diaz 602a7b403dSAchin Gupta #if defined(SPD_spmd) 612a7b403dSAchin Gupta if (spmd_setup() != 0) { 622a7b403dSAchin Gupta ret = 1; 632a7b403dSAchin Gupta } 642a7b403dSAchin Gupta #endif 652a7b403dSAchin Gupta 66b9fd2d3cSSubhasish Ghosh #if ENABLE_RME 67b9fd2d3cSSubhasish Ghosh if (rmmd_setup() != 0) { 68b9fd2d3cSSubhasish Ghosh ret = 1; 69b9fd2d3cSSubhasish Ghosh } 70b9fd2d3cSSubhasish Ghosh #endif 71b9fd2d3cSSubhasish Ghosh 72b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT 73b7cb133eSJeenu Viswambharan /* SDEI initialisation */ 74b7cb133eSJeenu Viswambharan sdei_init(); 75b7cb133eSJeenu Viswambharan #endif 76b7cb133eSJeenu Viswambharan 77*0b22e591SJayanth Dodderi Chidanand #if TRNG_SUPPORT 78*0b22e591SJayanth Dodderi Chidanand /* TRNG initialisation */ 797dfb9911SJimmy Brisson trng_setup(); 80*0b22e591SJayanth Dodderi Chidanand #endif /* TRNG_SUPPORT */ 817dfb9911SJimmy Brisson 82e62748e3SManish V Badarkhe #if DRTM_SUPPORT 83e62748e3SManish V Badarkhe if (drtm_setup() != 0) { 84e62748e3SManish V Badarkhe ret = 1; 85e62748e3SManish V Badarkhe } 86e62748e3SManish V Badarkhe #endif /* DRTM_SUPPORT */ 87e62748e3SManish V Badarkhe 882fccb228SAntonio Nino Diaz return ret; 8958e946aeSSoby Mathew } 9058e946aeSSoby Mathew 9164f6ea9bSJeenu Viswambharan /* 9264f6ea9bSJeenu Viswambharan * Top-level Standard Service SMC handler. This handler will in turn dispatch 9364f6ea9bSJeenu Viswambharan * calls to PSCI SMC handler 9464f6ea9bSJeenu Viswambharan */ 957fabe1a8SRoberto Vargas static uintptr_t std_svc_smc_handler(uint32_t smc_fid, 964c0d0390SSoby Mathew u_register_t x1, 974c0d0390SSoby Mathew u_register_t x2, 984c0d0390SSoby Mathew u_register_t x3, 994c0d0390SSoby Mathew u_register_t x4, 10064f6ea9bSJeenu Viswambharan void *cookie, 10164f6ea9bSJeenu Viswambharan void *handle, 1024c0d0390SSoby Mathew u_register_t flags) 10364f6ea9bSJeenu Viswambharan { 104475333c8SJeremy Linton if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { 105475333c8SJeremy Linton /* 32-bit SMC function, clear top parameter bits */ 106475333c8SJeremy Linton 107475333c8SJeremy Linton x1 &= UINT32_MAX; 108475333c8SJeremy Linton x2 &= UINT32_MAX; 109475333c8SJeremy Linton x3 &= UINT32_MAX; 110475333c8SJeremy Linton x4 &= UINT32_MAX; 111475333c8SJeremy Linton } 112475333c8SJeremy Linton 11364f6ea9bSJeenu Viswambharan /* 11464f6ea9bSJeenu Viswambharan * Dispatch PSCI calls to PSCI SMC handler and return its return 11564f6ea9bSJeenu Viswambharan * value 11664f6ea9bSJeenu Viswambharan */ 11764f6ea9bSJeenu Viswambharan if (is_psci_fid(smc_fid)) { 118872be88aSdp-arm uint64_t ret; 119872be88aSdp-arm 120872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 121bfef6106Sdp-arm 122bfef6106Sdp-arm /* 123bfef6106Sdp-arm * Flush cache line so that even if CPU power down happens 124bfef6106Sdp-arm * the timestamp update is reflected in memory. 125bfef6106Sdp-arm */ 126872be88aSdp-arm PMF_WRITE_TIMESTAMP(rt_instr_svc, 127872be88aSdp-arm RT_INSTR_ENTER_PSCI, 128bfef6106Sdp-arm PMF_CACHE_MAINT, 129872be88aSdp-arm get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX])); 130872be88aSdp-arm #endif 131872be88aSdp-arm 132872be88aSdp-arm ret = psci_smc_handler(smc_fid, x1, x2, x3, x4, 133872be88aSdp-arm cookie, handle, flags); 134872be88aSdp-arm 135872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 136872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 137872be88aSdp-arm RT_INSTR_EXIT_PSCI, 138872be88aSdp-arm PMF_NO_CACHE_MAINT); 139872be88aSdp-arm #endif 140872be88aSdp-arm 141872be88aSdp-arm SMC_RET1(handle, ret); 14264f6ea9bSJeenu Viswambharan } 14364f6ea9bSJeenu Viswambharan 1443f3c341aSPaul Beesley #if SPM_MM 1452fccb228SAntonio Nino Diaz /* 1462fccb228SAntonio Nino Diaz * Dispatch SPM calls to SPM SMC handler and return its return 1472fccb228SAntonio Nino Diaz * value 1482fccb228SAntonio Nino Diaz */ 1490bf9f567SPaul Beesley if (is_spm_mm_fid(smc_fid)) { 1500bf9f567SPaul Beesley return spm_mm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 1512fccb228SAntonio Nino Diaz handle, flags); 1522fccb228SAntonio Nino Diaz } 1532fccb228SAntonio Nino Diaz #endif 1542fccb228SAntonio Nino Diaz 1552a7b403dSAchin Gupta #if defined(SPD_spmd) 1562a7b403dSAchin Gupta /* 157662af36dSJ-Alves * Dispatch FFA calls to the FFA SMC handler implemented by the SPM 1582a7b403dSAchin Gupta * dispatcher and return its return value 1592a7b403dSAchin Gupta */ 160662af36dSJ-Alves if (is_ffa_fid(smc_fid)) { 161bb01a673SMarc Bonnici return spmd_ffa_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 1622a7b403dSAchin Gupta handle, flags); 1632a7b403dSAchin Gupta } 1642a7b403dSAchin Gupta #endif 1652a7b403dSAchin Gupta 166b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT 167b7cb133eSJeenu Viswambharan if (is_sdei_fid(smc_fid)) { 168b7cb133eSJeenu Viswambharan return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 169b7cb133eSJeenu Viswambharan flags); 170b7cb133eSJeenu Viswambharan } 171b7cb133eSJeenu Viswambharan #endif 172b7cb133eSJeenu Viswambharan 173323b6c63SAndre Przywara #if TRNG_SUPPORT 1747dfb9911SJimmy Brisson if (is_trng_fid(smc_fid)) { 1757dfb9911SJimmy Brisson return trng_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 1767dfb9911SJimmy Brisson flags); 1777dfb9911SJimmy Brisson } 178*0b22e591SJayanth Dodderi Chidanand #endif /* TRNG_SUPPORT */ 179*0b22e591SJayanth Dodderi Chidanand 18077c27753SZelalem Aweke #if ENABLE_RME 181319fb084SSoby Mathew 182319fb084SSoby Mathew if (is_rmmd_el3_fid(smc_fid)) { 183319fb084SSoby Mathew return rmmd_rmm_el3_handler(smc_fid, x1, x2, x3, x4, cookie, 18477c27753SZelalem Aweke handle, flags); 18577c27753SZelalem Aweke } 186b9fd2d3cSSubhasish Ghosh 187b9fd2d3cSSubhasish Ghosh if (is_rmi_fid(smc_fid)) { 188b9fd2d3cSSubhasish Ghosh return rmmd_rmi_handler(smc_fid, x1, x2, x3, x4, cookie, 189b9fd2d3cSSubhasish Ghosh handle, flags); 190b9fd2d3cSSubhasish Ghosh } 19177c27753SZelalem Aweke #endif 1927dfb9911SJimmy Brisson 1931cdf1eb8SJeremy Linton #if SMC_PCI_SUPPORT 1941cdf1eb8SJeremy Linton if (is_pci_fid(smc_fid)) { 1951cdf1eb8SJeremy Linton return pci_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 1961cdf1eb8SJeremy Linton flags); 1971cdf1eb8SJeremy Linton } 1981cdf1eb8SJeremy Linton #endif 1991cdf1eb8SJeremy Linton 200e62748e3SManish V Badarkhe #if DRTM_SUPPORT 201e62748e3SManish V Badarkhe if (is_drtm_fid(smc_fid)) { 202e62748e3SManish V Badarkhe return drtm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 203e62748e3SManish V Badarkhe flags); 204e62748e3SManish V Badarkhe } 205e62748e3SManish V Badarkhe #endif /* DRTM_SUPPORT */ 206e62748e3SManish V Badarkhe 20764f6ea9bSJeenu Viswambharan switch (smc_fid) { 20864f6ea9bSJeenu Viswambharan case ARM_STD_SVC_CALL_COUNT: 20964f6ea9bSJeenu Viswambharan /* 21064f6ea9bSJeenu Viswambharan * Return the number of Standard Service Calls. PSCI is the only 21164f6ea9bSJeenu Viswambharan * standard service implemented; so return number of PSCI calls 21264f6ea9bSJeenu Viswambharan */ 21364f6ea9bSJeenu Viswambharan SMC_RET1(handle, PSCI_NUM_CALLS); 21464f6ea9bSJeenu Viswambharan 21564f6ea9bSJeenu Viswambharan case ARM_STD_SVC_UID: 21664f6ea9bSJeenu Viswambharan /* Return UID to the caller */ 21764f6ea9bSJeenu Viswambharan SMC_UUID_RET(handle, arm_svc_uid); 21864f6ea9bSJeenu Viswambharan 21964f6ea9bSJeenu Viswambharan case ARM_STD_SVC_VERSION: 22064f6ea9bSJeenu Viswambharan /* Return the version of current implementation */ 22164f6ea9bSJeenu Viswambharan SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR); 22264f6ea9bSJeenu Viswambharan 22364f6ea9bSJeenu Viswambharan default: 22467fad514SAndre Przywara VERBOSE("Unimplemented Standard Service Call: 0x%x \n", smc_fid); 22564f6ea9bSJeenu Viswambharan SMC_RET1(handle, SMC_UNK); 22664f6ea9bSJeenu Viswambharan } 22764f6ea9bSJeenu Viswambharan } 22864f6ea9bSJeenu Viswambharan 22964f6ea9bSJeenu Viswambharan /* Register Standard Service Calls as runtime service */ 23064f6ea9bSJeenu Viswambharan DECLARE_RT_SVC( 23164f6ea9bSJeenu Viswambharan std_svc, 23264f6ea9bSJeenu Viswambharan 23364f6ea9bSJeenu Viswambharan OEN_STD_START, 23464f6ea9bSJeenu Viswambharan OEN_STD_END, 23564f6ea9bSJeenu Viswambharan SMC_TYPE_FAST, 23658e946aeSSoby Mathew std_svc_setup, 23764f6ea9bSJeenu Viswambharan std_svc_smc_handler 23864f6ea9bSJeenu Viswambharan ); 239