164f6ea9bSJeenu Viswambharan /* 24c0d0390SSoby Mathew * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. 364f6ea9bSJeenu Viswambharan * 4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 564f6ea9bSJeenu Viswambharan */ 664f6ea9bSJeenu Viswambharan 758e946aeSSoby Mathew #include <assert.h> 8872be88aSdp-arm #include <cpu_data.h> 997043ac9SDan Handley #include <debug.h> 10872be88aSdp-arm #include <pmf.h> 1197043ac9SDan Handley #include <psci.h> 12872be88aSdp-arm #include <runtime_instr.h> 1364f6ea9bSJeenu Viswambharan #include <runtime_svc.h> 14cf0b1492SSoby Mathew #include <smcc_helpers.h> 1564f6ea9bSJeenu Viswambharan #include <std_svc.h> 1697043ac9SDan Handley #include <stdint.h> 1797043ac9SDan Handley #include <uuid.h> 1864f6ea9bSJeenu Viswambharan 1964f6ea9bSJeenu Viswambharan /* Standard Service UUID */ 2064f6ea9bSJeenu Viswambharan DEFINE_SVC_UUID(arm_svc_uid, 2164f6ea9bSJeenu Viswambharan 0x108d905b, 0xf863, 0x47e8, 0xae, 0x2d, 2264f6ea9bSJeenu Viswambharan 0xc0, 0xfb, 0x56, 0x41, 0xf6, 0xe2); 2364f6ea9bSJeenu Viswambharan 2458e946aeSSoby Mathew /* Setup Standard Services */ 2558e946aeSSoby Mathew static int32_t std_svc_setup(void) 2658e946aeSSoby Mathew { 2758e946aeSSoby Mathew uintptr_t svc_arg; 2858e946aeSSoby Mathew 2958e946aeSSoby Mathew svc_arg = get_arm_std_svc_args(PSCI_FID_MASK); 3058e946aeSSoby Mathew assert(svc_arg); 3158e946aeSSoby Mathew 3258e946aeSSoby Mathew /* 3358e946aeSSoby Mathew * PSCI is the only specification implemented as a Standard Service. 3458e946aeSSoby Mathew * The `psci_setup()` also does EL3 architectural setup. 3558e946aeSSoby Mathew */ 3658e946aeSSoby Mathew return psci_setup((const psci_lib_args_t *)svc_arg); 3758e946aeSSoby Mathew } 3858e946aeSSoby Mathew 3964f6ea9bSJeenu Viswambharan /* 4064f6ea9bSJeenu Viswambharan * Top-level Standard Service SMC handler. This handler will in turn dispatch 4164f6ea9bSJeenu Viswambharan * calls to PSCI SMC handler 4264f6ea9bSJeenu Viswambharan */ 434c0d0390SSoby Mathew uintptr_t std_svc_smc_handler(uint32_t smc_fid, 444c0d0390SSoby Mathew u_register_t x1, 454c0d0390SSoby Mathew u_register_t x2, 464c0d0390SSoby Mathew u_register_t x3, 474c0d0390SSoby Mathew u_register_t x4, 4864f6ea9bSJeenu Viswambharan void *cookie, 4964f6ea9bSJeenu Viswambharan void *handle, 504c0d0390SSoby Mathew u_register_t flags) 5164f6ea9bSJeenu Viswambharan { 5264f6ea9bSJeenu Viswambharan /* 5364f6ea9bSJeenu Viswambharan * Dispatch PSCI calls to PSCI SMC handler and return its return 5464f6ea9bSJeenu Viswambharan * value 5564f6ea9bSJeenu Viswambharan */ 5664f6ea9bSJeenu Viswambharan if (is_psci_fid(smc_fid)) { 57872be88aSdp-arm uint64_t ret; 58872be88aSdp-arm 59872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 60bfef6106Sdp-arm 61bfef6106Sdp-arm /* 62bfef6106Sdp-arm * Flush cache line so that even if CPU power down happens 63bfef6106Sdp-arm * the timestamp update is reflected in memory. 64bfef6106Sdp-arm */ 65872be88aSdp-arm PMF_WRITE_TIMESTAMP(rt_instr_svc, 66872be88aSdp-arm RT_INSTR_ENTER_PSCI, 67bfef6106Sdp-arm PMF_CACHE_MAINT, 68872be88aSdp-arm get_cpu_data(cpu_data_pmf_ts[CPU_DATA_PMF_TS0_IDX])); 69872be88aSdp-arm #endif 70872be88aSdp-arm 71872be88aSdp-arm ret = psci_smc_handler(smc_fid, x1, x2, x3, x4, 72872be88aSdp-arm cookie, handle, flags); 73872be88aSdp-arm 74872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 75872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 76872be88aSdp-arm RT_INSTR_EXIT_PSCI, 77872be88aSdp-arm PMF_NO_CACHE_MAINT); 78872be88aSdp-arm #endif 79872be88aSdp-arm 80872be88aSdp-arm SMC_RET1(handle, ret); 8164f6ea9bSJeenu Viswambharan } 8264f6ea9bSJeenu Viswambharan 8364f6ea9bSJeenu Viswambharan switch (smc_fid) { 8464f6ea9bSJeenu Viswambharan case ARM_STD_SVC_CALL_COUNT: 8564f6ea9bSJeenu Viswambharan /* 8664f6ea9bSJeenu Viswambharan * Return the number of Standard Service Calls. PSCI is the only 8764f6ea9bSJeenu Viswambharan * standard service implemented; so return number of PSCI calls 8864f6ea9bSJeenu Viswambharan */ 8964f6ea9bSJeenu Viswambharan SMC_RET1(handle, PSCI_NUM_CALLS); 9064f6ea9bSJeenu Viswambharan 9164f6ea9bSJeenu Viswambharan case ARM_STD_SVC_UID: 9264f6ea9bSJeenu Viswambharan /* Return UID to the caller */ 9364f6ea9bSJeenu Viswambharan SMC_UUID_RET(handle, arm_svc_uid); 9464f6ea9bSJeenu Viswambharan 9564f6ea9bSJeenu Viswambharan case ARM_STD_SVC_VERSION: 9664f6ea9bSJeenu Viswambharan /* Return the version of current implementation */ 9764f6ea9bSJeenu Viswambharan SMC_RET2(handle, STD_SVC_VERSION_MAJOR, STD_SVC_VERSION_MINOR); 9864f6ea9bSJeenu Viswambharan 9964f6ea9bSJeenu Viswambharan default: 10064f6ea9bSJeenu Viswambharan WARN("Unimplemented Standard Service Call: 0x%x \n", smc_fid); 10164f6ea9bSJeenu Viswambharan SMC_RET1(handle, SMC_UNK); 10264f6ea9bSJeenu Viswambharan } 10364f6ea9bSJeenu Viswambharan } 10464f6ea9bSJeenu Viswambharan 10564f6ea9bSJeenu Viswambharan /* Register Standard Service Calls as runtime service */ 10664f6ea9bSJeenu Viswambharan DECLARE_RT_SVC( 10764f6ea9bSJeenu Viswambharan std_svc, 10864f6ea9bSJeenu Viswambharan 10964f6ea9bSJeenu Viswambharan OEN_STD_START, 11064f6ea9bSJeenu Viswambharan OEN_STD_END, 11164f6ea9bSJeenu Viswambharan SMC_TYPE_FAST, 11258e946aeSSoby Mathew std_svc_setup, 11364f6ea9bSJeenu Viswambharan std_svc_smc_handler 11464f6ea9bSJeenu Viswambharan ); 115