164f6ea9bSJeenu Viswambharan /* 27dfb9911SJimmy Brisson * Copyright (c) 2014-2021, 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> 16*77c27753SZelalem Aweke #include <services/gtsi_svc.h> 171cdf1eb8SJeremy Linton #include <services/pci_svc.h> 18*77c27753SZelalem Aweke #include <services/rmmd_svc.h> 1909d40e0eSAntonio Nino Diaz #include <services/sdei.h> 200bf9f567SPaul Beesley #include <services/spm_mm_svc.h> 212a7b403dSAchin Gupta #include <services/spmd_svc.h> 2209d40e0eSAntonio Nino Diaz #include <services/std_svc.h> 237dfb9911SJimmy Brisson #include <services/trng_svc.h> 2409d40e0eSAntonio Nino Diaz #include <smccc_helpers.h> 2509d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 2664f6ea9bSJeenu Viswambharan 2764f6ea9bSJeenu Viswambharan /* Standard Service UUID */ 2803364865SRoberto Vargas static uuid_t arm_svc_uid = { 2903364865SRoberto Vargas {0x5b, 0x90, 0x8d, 0x10}, 3003364865SRoberto Vargas {0x63, 0xf8}, 3103364865SRoberto Vargas {0xe8, 0x47}, 3203364865SRoberto Vargas 0xae, 0x2d, 3303364865SRoberto Vargas {0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2} 3403364865SRoberto Vargas }; 3564f6ea9bSJeenu Viswambharan 3658e946aeSSoby Mathew /* Setup Standard Services */ 3758e946aeSSoby Mathew static int32_t std_svc_setup(void) 3858e946aeSSoby Mathew { 3958e946aeSSoby Mathew uintptr_t svc_arg; 402fccb228SAntonio Nino Diaz int ret = 0; 4158e946aeSSoby Mathew 4258e946aeSSoby Mathew svc_arg = get_arm_std_svc_args(PSCI_FID_MASK); 4358e946aeSSoby Mathew assert(svc_arg); 4458e946aeSSoby Mathew 4558e946aeSSoby Mathew /* 462fccb228SAntonio Nino Diaz * PSCI is one of the specifications implemented as a Standard Service. 4758e946aeSSoby Mathew * The `psci_setup()` also does EL3 architectural setup. 4858e946aeSSoby Mathew */ 492fccb228SAntonio Nino Diaz if (psci_setup((const psci_lib_args_t *)svc_arg) != PSCI_E_SUCCESS) { 502fccb228SAntonio Nino Diaz ret = 1; 512fccb228SAntonio Nino Diaz } 522fccb228SAntonio Nino Diaz 53538b0020SPaul Beesley #if SPM_MM 540bf9f567SPaul Beesley if (spm_mm_setup() != 0) { 552fccb228SAntonio Nino Diaz ret = 1; 562fccb228SAntonio Nino Diaz } 572fccb228SAntonio Nino Diaz #endif 582fccb228SAntonio Nino Diaz 592a7b403dSAchin Gupta #if defined(SPD_spmd) 602a7b403dSAchin Gupta if (spmd_setup() != 0) { 612a7b403dSAchin Gupta ret = 1; 622a7b403dSAchin Gupta } 632a7b403dSAchin Gupta #endif 642a7b403dSAchin Gupta 65b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT 66b7cb133eSJeenu Viswambharan /* SDEI initialisation */ 67b7cb133eSJeenu Viswambharan sdei_init(); 68b7cb133eSJeenu Viswambharan #endif 69b7cb133eSJeenu Viswambharan 707dfb9911SJimmy Brisson trng_setup(); 717dfb9911SJimmy Brisson 722fccb228SAntonio Nino Diaz return ret; 7358e946aeSSoby Mathew } 7458e946aeSSoby Mathew 7564f6ea9bSJeenu Viswambharan /* 7664f6ea9bSJeenu Viswambharan * Top-level Standard Service SMC handler. This handler will in turn dispatch 7764f6ea9bSJeenu Viswambharan * calls to PSCI SMC handler 7864f6ea9bSJeenu Viswambharan */ 797fabe1a8SRoberto Vargas static uintptr_t std_svc_smc_handler(uint32_t smc_fid, 804c0d0390SSoby Mathew u_register_t x1, 814c0d0390SSoby Mathew u_register_t x2, 824c0d0390SSoby Mathew u_register_t x3, 834c0d0390SSoby Mathew u_register_t x4, 8464f6ea9bSJeenu Viswambharan void *cookie, 8564f6ea9bSJeenu Viswambharan void *handle, 864c0d0390SSoby Mathew u_register_t flags) 8764f6ea9bSJeenu Viswambharan { 88475333c8SJeremy Linton if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { 89475333c8SJeremy Linton /* 32-bit SMC function, clear top parameter bits */ 90475333c8SJeremy Linton 91475333c8SJeremy Linton x1 &= UINT32_MAX; 92475333c8SJeremy Linton x2 &= UINT32_MAX; 93475333c8SJeremy Linton x3 &= UINT32_MAX; 94475333c8SJeremy Linton x4 &= UINT32_MAX; 95475333c8SJeremy Linton } 96475333c8SJeremy Linton 9764f6ea9bSJeenu Viswambharan /* 9864f6ea9bSJeenu Viswambharan * Dispatch PSCI calls to PSCI SMC handler and return its return 9964f6ea9bSJeenu Viswambharan * value 10064f6ea9bSJeenu Viswambharan */ 10164f6ea9bSJeenu Viswambharan if (is_psci_fid(smc_fid)) { 102872be88aSdp-arm uint64_t ret; 103872be88aSdp-arm 104872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 105bfef6106Sdp-arm 106bfef6106Sdp-arm /* 107bfef6106Sdp-arm * Flush cache line so that even if CPU power down happens 108bfef6106Sdp-arm * the timestamp update is reflected in memory. 109bfef6106Sdp-arm */ 110872be88aSdp-arm PMF_WRITE_TIMESTAMP(rt_instr_svc, 111872be88aSdp-arm RT_INSTR_ENTER_PSCI, 112bfef6106Sdp-arm PMF_CACHE_MAINT, 113872be88aSdp-arm get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX])); 114872be88aSdp-arm #endif 115872be88aSdp-arm 116872be88aSdp-arm ret = psci_smc_handler(smc_fid, x1, x2, x3, x4, 117872be88aSdp-arm cookie, handle, flags); 118872be88aSdp-arm 119872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 120872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 121872be88aSdp-arm RT_INSTR_EXIT_PSCI, 122872be88aSdp-arm PMF_NO_CACHE_MAINT); 123872be88aSdp-arm #endif 124872be88aSdp-arm 125872be88aSdp-arm SMC_RET1(handle, ret); 12664f6ea9bSJeenu Viswambharan } 12764f6ea9bSJeenu Viswambharan 1283f3c341aSPaul Beesley #if SPM_MM 1292fccb228SAntonio Nino Diaz /* 1302fccb228SAntonio Nino Diaz * Dispatch SPM calls to SPM SMC handler and return its return 1312fccb228SAntonio Nino Diaz * value 1322fccb228SAntonio Nino Diaz */ 1330bf9f567SPaul Beesley if (is_spm_mm_fid(smc_fid)) { 1340bf9f567SPaul Beesley return spm_mm_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 1352fccb228SAntonio Nino Diaz handle, flags); 1362fccb228SAntonio Nino Diaz } 1372fccb228SAntonio Nino Diaz #endif 1382fccb228SAntonio Nino Diaz 1392a7b403dSAchin Gupta #if defined(SPD_spmd) 1402a7b403dSAchin Gupta /* 141662af36dSJ-Alves * Dispatch FFA calls to the FFA SMC handler implemented by the SPM 1422a7b403dSAchin Gupta * dispatcher and return its return value 1432a7b403dSAchin Gupta */ 144662af36dSJ-Alves if (is_ffa_fid(smc_fid)) { 1452a7b403dSAchin Gupta return spmd_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 1462a7b403dSAchin Gupta handle, flags); 1472a7b403dSAchin Gupta } 1482a7b403dSAchin Gupta #endif 1492a7b403dSAchin Gupta 150b7cb133eSJeenu Viswambharan #if SDEI_SUPPORT 151b7cb133eSJeenu Viswambharan if (is_sdei_fid(smc_fid)) { 152b7cb133eSJeenu Viswambharan return sdei_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 153b7cb133eSJeenu Viswambharan flags); 154b7cb133eSJeenu Viswambharan } 155b7cb133eSJeenu Viswambharan #endif 156b7cb133eSJeenu Viswambharan 157323b6c63SAndre Przywara #if TRNG_SUPPORT 1587dfb9911SJimmy Brisson if (is_trng_fid(smc_fid)) { 1597dfb9911SJimmy Brisson return trng_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 1607dfb9911SJimmy Brisson flags); 1617dfb9911SJimmy Brisson } 162323b6c63SAndre Przywara #endif 163*77c27753SZelalem Aweke #if ENABLE_RME 164*77c27753SZelalem Aweke /* 165*77c27753SZelalem Aweke * Granule transition service interface functions (GTSI) are allocated 166*77c27753SZelalem Aweke * from the Std service range. Call the RMM dispatcher to handle calls. 167*77c27753SZelalem Aweke */ 168*77c27753SZelalem Aweke if (is_gtsi_fid(smc_fid)) { 169*77c27753SZelalem Aweke return rmmd_gtsi_handler(smc_fid, x1, x2, x3, x4, cookie, 170*77c27753SZelalem Aweke handle, flags); 171*77c27753SZelalem Aweke } 172*77c27753SZelalem Aweke #endif 1737dfb9911SJimmy Brisson 1741cdf1eb8SJeremy Linton #if SMC_PCI_SUPPORT 1751cdf1eb8SJeremy Linton if (is_pci_fid(smc_fid)) { 1761cdf1eb8SJeremy Linton return pci_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, 1771cdf1eb8SJeremy Linton flags); 1781cdf1eb8SJeremy Linton } 1791cdf1eb8SJeremy Linton #endif 1801cdf1eb8SJeremy Linton 18164f6ea9bSJeenu Viswambharan switch (smc_fid) { 18264f6ea9bSJeenu Viswambharan case ARM_STD_SVC_CALL_COUNT: 18364f6ea9bSJeenu Viswambharan /* 18464f6ea9bSJeenu Viswambharan * Return the number of Standard Service Calls. PSCI is the only 18564f6ea9bSJeenu Viswambharan * standard service implemented; so return number of PSCI calls 18664f6ea9bSJeenu Viswambharan */ 18764f6ea9bSJeenu Viswambharan SMC_RET1(handle, PSCI_NUM_CALLS); 18864f6ea9bSJeenu Viswambharan 18964f6ea9bSJeenu Viswambharan case ARM_STD_SVC_UID: 19064f6ea9bSJeenu Viswambharan /* Return UID to the caller */ 19164f6ea9bSJeenu Viswambharan SMC_UUID_RET(handle, arm_svc_uid); 19264f6ea9bSJeenu Viswambharan 19364f6ea9bSJeenu Viswambharan case ARM_STD_SVC_VERSION: 19464f6ea9bSJeenu Viswambharan /* Return the version of current implementation */ 19564f6ea9bSJeenu Viswambharan SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR); 19664f6ea9bSJeenu Viswambharan 19764f6ea9bSJeenu Viswambharan default: 19867fad514SAndre Przywara VERBOSE("Unimplemented Standard Service Call: 0x%x \n", smc_fid); 19964f6ea9bSJeenu Viswambharan SMC_RET1(handle, SMC_UNK); 20064f6ea9bSJeenu Viswambharan } 20164f6ea9bSJeenu Viswambharan } 20264f6ea9bSJeenu Viswambharan 20364f6ea9bSJeenu Viswambharan /* Register Standard Service Calls as runtime service */ 20464f6ea9bSJeenu Viswambharan DECLARE_RT_SVC( 20564f6ea9bSJeenu Viswambharan std_svc, 20664f6ea9bSJeenu Viswambharan 20764f6ea9bSJeenu Viswambharan OEN_STD_START, 20864f6ea9bSJeenu Viswambharan OEN_STD_END, 20964f6ea9bSJeenu Viswambharan SMC_TYPE_FAST, 21058e946aeSSoby Mathew std_svc_setup, 21164f6ea9bSJeenu Viswambharan std_svc_smc_handler 21264f6ea9bSJeenu Viswambharan ); 213