1532ed618SSoby Mathew /* 2382ba743SBoyan Karatotev * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved. 3532ed618SSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5532ed618SSoby Mathew */ 6532ed618SSoby Mathew 709d40e0eSAntonio Nino Diaz #include <assert.h> 809d40e0eSAntonio Nino Diaz #include <string.h> 909d40e0eSAntonio Nino Diaz 10532ed618SSoby Mathew #include <arch.h> 1145c7328cSBoyan Karatotev #include <arch_features.h> 12532ed618SSoby Mathew #include <arch_helpers.h> 1309d40e0eSAntonio Nino Diaz #include <common/debug.h> 1409d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 1509d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 1609d40e0eSAntonio Nino Diaz #include <lib/smccc.h> 1709d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 1809d40e0eSAntonio Nino Diaz #include <services/arm_arch_svc.h> 1909d40e0eSAntonio Nino Diaz 20532ed618SSoby Mathew #include "psci_private.h" 21532ed618SSoby Mathew 22532ed618SSoby Mathew /******************************************************************************* 23532ed618SSoby Mathew * PSCI frontend api for servicing SMCs. Described in the PSCI spec. 24532ed618SSoby Mathew ******************************************************************************/ 25532ed618SSoby Mathew int psci_cpu_on(u_register_t target_cpu, 26532ed618SSoby Mathew uintptr_t entrypoint, 27532ed618SSoby Mathew u_register_t context_id) 28532ed618SSoby Mathew 29532ed618SSoby Mathew { 30532ed618SSoby Mathew int rc; 31ef738d19SManish Pandey entry_point_info_t *ep; 32ef738d19SManish Pandey unsigned int target_idx = (unsigned int)plat_core_pos_by_mpidr(target_cpu); 33532ed618SSoby Mathew 34e60c1847SManish Pandey /* Validate the target CPU */ 35c7b0a28dSMaheedhar Bollapalli if (!is_valid_mpidr(target_cpu)) { 36532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 37c7b0a28dSMaheedhar Bollapalli } 38532ed618SSoby Mathew 39ef738d19SManish Pandey ep = get_cpu_data_by_index(target_idx, warmboot_ep_info); 40ef738d19SManish Pandey /* Validate the lower EL entry point and put it in the entry_point_info */ 41ef738d19SManish Pandey rc = psci_validate_entry_point(ep, entrypoint, context_id); 42c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_E_SUCCESS) { 43532ed618SSoby Mathew return rc; 44c7b0a28dSMaheedhar Bollapalli } 45532ed618SSoby Mathew 46532ed618SSoby Mathew /* 47532ed618SSoby Mathew * To turn this cpu on, specify which power 48532ed618SSoby Mathew * levels need to be turned on 49532ed618SSoby Mathew */ 50ef738d19SManish Pandey return psci_cpu_on_start(target_cpu, ep); 51532ed618SSoby Mathew } 52532ed618SSoby Mathew 53532ed618SSoby Mathew unsigned int psci_version(void) 54532ed618SSoby Mathew { 55532ed618SSoby Mathew return PSCI_MAJOR_VER | PSCI_MINOR_VER; 56532ed618SSoby Mathew } 57532ed618SSoby Mathew 58532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state, 59532ed618SSoby Mathew uintptr_t entrypoint, 60532ed618SSoby Mathew u_register_t context_id) 61532ed618SSoby Mathew { 62532ed618SSoby Mathew int rc; 63532ed618SSoby Mathew unsigned int target_pwrlvl, is_power_down_state; 64532ed618SSoby Mathew psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} }; 65532ed618SSoby Mathew plat_local_state_t cpu_pd_state; 66606b7430SWing Li unsigned int cpu_idx = plat_my_core_pos(); 67532ed618SSoby Mathew 6845c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN 6945c7328cSBoyan Karatotev /* 7045c7328cSBoyan Karatotev * If SME isn't off, attempting a real power down will only end up being 7145c7328cSBoyan Karatotev * rejected. If we got called with SME on, fall back to a normal 7245c7328cSBoyan Karatotev * suspend. We can't force SME off as in the event the power down is 7345c7328cSBoyan Karatotev * rejected for another reason (eg GIC) we'd lose the SME context. 7445c7328cSBoyan Karatotev */ 7545c7328cSBoyan Karatotev if (is_feat_sme_supported() && read_svcr() != 0) { 7645c7328cSBoyan Karatotev power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT); 7745c7328cSBoyan Karatotev power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT); 7845c7328cSBoyan Karatotev } 7945c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */ 8045c7328cSBoyan Karatotev 81532ed618SSoby Mathew /* Validate the power_state parameter */ 82532ed618SSoby Mathew rc = psci_validate_power_state(power_state, &state_info); 83532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) { 84532ed618SSoby Mathew assert(rc == PSCI_E_INVALID_PARAMS); 85532ed618SSoby Mathew return rc; 86532ed618SSoby Mathew } 87532ed618SSoby Mathew 88532ed618SSoby Mathew /* 89532ed618SSoby Mathew * Get the value of the state type bit from the power state parameter. 90532ed618SSoby Mathew */ 91532ed618SSoby Mathew is_power_down_state = psci_get_pstate_type(power_state); 92532ed618SSoby Mathew 93532ed618SSoby Mathew /* Sanity check the requested suspend levels */ 94532ed618SSoby Mathew assert(psci_validate_suspend_req(&state_info, is_power_down_state) 95532ed618SSoby Mathew == PSCI_E_SUCCESS); 96532ed618SSoby Mathew 97532ed618SSoby Mathew target_pwrlvl = psci_find_target_suspend_lvl(&state_info); 98a1c3faa6SSandrine Bailleux if (target_pwrlvl == PSCI_INVALID_PWR_LVL) { 99a1c3faa6SSandrine Bailleux ERROR("Invalid target power level for suspend operation\n"); 100a1c3faa6SSandrine Bailleux panic(); 101a1c3faa6SSandrine Bailleux } 102532ed618SSoby Mathew 103*b34be5dfSBoyan Karatotev /* Fast path for local CPU standby, won't interact with higher power levels. */ 104362030bfSAntonio Nino Diaz if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) { 105c7b0a28dSMaheedhar Bollapalli if (psci_plat_pm_ops->cpu_standby == NULL) { 106532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 107c7b0a28dSMaheedhar Bollapalli } 108532ed618SSoby Mathew 109532ed618SSoby Mathew /* 110532ed618SSoby Mathew * Set the state of the CPU power domain to the platform 111532ed618SSoby Mathew * specific retention state and enter the standby state. 112532ed618SSoby Mathew */ 113532ed618SSoby Mathew cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL]; 114532ed618SSoby Mathew psci_set_cpu_local_state(cpu_pd_state); 115532ed618SSoby Mathew 116532ed618SSoby Mathew #if ENABLE_PSCI_STAT 11704c1db1eSdp-arm plat_psci_stat_accounting_start(&state_info); 118532ed618SSoby Mathew #endif 119532ed618SSoby Mathew 120872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 121872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 122872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 123872be88aSdp-arm PMF_NO_CACHE_MAINT); 124872be88aSdp-arm #endif 125872be88aSdp-arm 126532ed618SSoby Mathew psci_plat_pm_ops->cpu_standby(cpu_pd_state); 127532ed618SSoby Mathew 128532ed618SSoby Mathew /* Upon exit from standby, set the state back to RUN. */ 129532ed618SSoby Mathew psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN); 130532ed618SSoby Mathew 131872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 132872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 133872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 134872be88aSdp-arm PMF_NO_CACHE_MAINT); 135872be88aSdp-arm #endif 136872be88aSdp-arm 137532ed618SSoby Mathew #if ENABLE_PSCI_STAT 13804c1db1eSdp-arm plat_psci_stat_accounting_stop(&state_info); 139532ed618SSoby Mathew 140532ed618SSoby Mathew /* Update PSCI stats */ 1413b802105SBoyan Karatotev psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info); 142532ed618SSoby Mathew #endif 143532ed618SSoby Mathew 144532ed618SSoby Mathew return PSCI_E_SUCCESS; 145532ed618SSoby Mathew } 146532ed618SSoby Mathew 147532ed618SSoby Mathew /* 148532ed618SSoby Mathew * If a power down state has been requested, we need to verify entry 149532ed618SSoby Mathew * point and program entry information. 150532ed618SSoby Mathew */ 1516b7b0f36SAntonio Nino Diaz if (is_power_down_state != 0U) { 152ef738d19SManish Pandey entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info); 153ef738d19SManish Pandey 154ef738d19SManish Pandey rc = psci_validate_entry_point(ep, entrypoint, context_id); 155c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_E_SUCCESS) { 156532ed618SSoby Mathew return rc; 157532ed618SSoby Mathew } 158c7b0a28dSMaheedhar Bollapalli } 159532ed618SSoby Mathew 160532ed618SSoby Mathew /* 161532ed618SSoby Mathew * Do what is needed to enter the power down state. Upon success, 162532ed618SSoby Mathew * enter the final wfi which will power down this CPU. This function 163532ed618SSoby Mathew * might return if the power down was abandoned for any reason, e.g. 164532ed618SSoby Mathew * arrival of an interrupt 165532ed618SSoby Mathew */ 1663b802105SBoyan Karatotev rc = psci_cpu_suspend_start(cpu_idx, 167532ed618SSoby Mathew target_pwrlvl, 168532ed618SSoby Mathew &state_info, 169532ed618SSoby Mathew is_power_down_state); 170532ed618SSoby Mathew 171606b7430SWing Li return rc; 172532ed618SSoby Mathew } 173532ed618SSoby Mathew 174532ed618SSoby Mathew 175532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id) 176532ed618SSoby Mathew { 177532ed618SSoby Mathew int rc; 178532ed618SSoby Mathew psci_power_state_t state_info; 1793b802105SBoyan Karatotev unsigned int cpu_idx = plat_my_core_pos(); 180ef738d19SManish Pandey entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info); 181532ed618SSoby Mathew 182532ed618SSoby Mathew /* Check if the current CPU is the last ON CPU in the system */ 183c7b0a28dSMaheedhar Bollapalli if (!psci_is_last_on_cpu(cpu_idx)) { 184532ed618SSoby Mathew return PSCI_E_DENIED; 185c7b0a28dSMaheedhar Bollapalli } 186532ed618SSoby Mathew 187532ed618SSoby Mathew /* Validate the entry point and get the entry_point_info */ 188ef738d19SManish Pandey rc = psci_validate_entry_point(ep, entrypoint, context_id); 189c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_E_SUCCESS) { 190532ed618SSoby Mathew return rc; 191c7b0a28dSMaheedhar Bollapalli } 192532ed618SSoby Mathew 193532ed618SSoby Mathew /* Query the psci_power_state for system suspend */ 194532ed618SSoby Mathew psci_query_sys_suspend_pwrstate(&state_info); 195532ed618SSoby Mathew 196a4065abdSldts /* 197a4065abdSldts * Check if platform allows suspend to Highest power level 198a4065abdSldts * (System level) 199a4065abdSldts */ 200c7b0a28dSMaheedhar Bollapalli if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) { 201a4065abdSldts return PSCI_E_DENIED; 202c7b0a28dSMaheedhar Bollapalli } 203532ed618SSoby Mathew /* Ensure that the psci_power_state makes sense */ 204532ed618SSoby Mathew assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN) 205532ed618SSoby Mathew == PSCI_E_SUCCESS); 2066b7b0f36SAntonio Nino Diaz assert(is_local_state_off( 2076b7b0f36SAntonio Nino Diaz state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0); 208532ed618SSoby Mathew 209532ed618SSoby Mathew /* 210532ed618SSoby Mathew * Do what is needed to enter the system suspend state. This function 211532ed618SSoby Mathew * might return if the power down was abandoned for any reason, e.g. 212532ed618SSoby Mathew * arrival of an interrupt 213532ed618SSoby Mathew */ 2143b802105SBoyan Karatotev rc = psci_cpu_suspend_start(cpu_idx, 215532ed618SSoby Mathew PLAT_MAX_PWR_LVL, 216532ed618SSoby Mathew &state_info, 217532ed618SSoby Mathew PSTATE_TYPE_POWERDOWN); 218532ed618SSoby Mathew 219606b7430SWing Li return rc; 220532ed618SSoby Mathew } 221532ed618SSoby Mathew 222532ed618SSoby Mathew int psci_cpu_off(void) 223532ed618SSoby Mathew { 224532ed618SSoby Mathew int rc; 225532ed618SSoby Mathew unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL; 226532ed618SSoby Mathew 227532ed618SSoby Mathew /* 228532ed618SSoby Mathew * Do what is needed to power off this CPU and possible higher power 229532ed618SSoby Mathew * levels if it able to do so. Upon success, enter the final wfi 230532ed618SSoby Mathew * which will power down this CPU. 231532ed618SSoby Mathew */ 232532ed618SSoby Mathew rc = psci_do_cpu_off(target_pwrlvl); 233532ed618SSoby Mathew 234532ed618SSoby Mathew /* 235532ed618SSoby Mathew * The only error cpu_off can return is E_DENIED. So check if that's 236532ed618SSoby Mathew * indeed the case. 237532ed618SSoby Mathew */ 238532ed618SSoby Mathew assert(rc == PSCI_E_DENIED); 239532ed618SSoby Mathew 240532ed618SSoby Mathew return rc; 241532ed618SSoby Mathew } 242532ed618SSoby Mathew 243532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity, 244532ed618SSoby Mathew unsigned int lowest_affinity_level) 245532ed618SSoby Mathew { 2465b33ad17SDeepika Bhavnani unsigned int target_idx; 247532ed618SSoby Mathew 248e60c1847SManish Pandey /* Validate the target affinity */ 249c7b0a28dSMaheedhar Bollapalli if (!is_valid_mpidr(target_affinity)) { 250e60c1847SManish Pandey return PSCI_E_INVALID_PARAMS; 251c7b0a28dSMaheedhar Bollapalli } 252e60c1847SManish Pandey 253532ed618SSoby Mathew /* We dont support level higher than PSCI_CPU_PWR_LVL */ 254c7b0a28dSMaheedhar Bollapalli if (lowest_affinity_level > PSCI_CPU_PWR_LVL) { 255532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 256c7b0a28dSMaheedhar Bollapalli } 257532ed618SSoby Mathew /* Calculate the cpu index of the target */ 258e60c1847SManish Pandey target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity); 259532ed618SSoby Mathew 2608fd307ffSRoberto Vargas /* 2618fd307ffSRoberto Vargas * Generic management: 2628fd307ffSRoberto Vargas * Perform cache maintanence ahead of reading the target CPU state to 2638fd307ffSRoberto Vargas * ensure that the data is not stale. 2648fd307ffSRoberto Vargas * There is a theoretical edge case where the cache may contain stale 2658fd307ffSRoberto Vargas * data for the target CPU data - this can occur under the following 2668fd307ffSRoberto Vargas * conditions: 2678fd307ffSRoberto Vargas * - the target CPU is in another cluster from the current 2688fd307ffSRoberto Vargas * - the target CPU was the last CPU to shutdown on its cluster 2698fd307ffSRoberto Vargas * - the cluster was removed from coherency as part of the CPU shutdown 2708fd307ffSRoberto Vargas * 2718fd307ffSRoberto Vargas * In this case the cache maintenace that was performed as part of the 2728fd307ffSRoberto Vargas * target CPUs shutdown was not seen by the current CPU's cluster. And 2738fd307ffSRoberto Vargas * so the cache may contain stale data for the target CPU. 2748fd307ffSRoberto Vargas */ 2755b33ad17SDeepika Bhavnani flush_cpu_data_by_index(target_idx, 2766b7b0f36SAntonio Nino Diaz psci_svc_cpu_data.aff_info_state); 2778fd307ffSRoberto Vargas 278f6166f7fSMaheedhar Bollapalli return (int)psci_get_aff_info_state_by_idx(target_idx); 279532ed618SSoby Mathew } 280532ed618SSoby Mathew 281532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu) 282532ed618SSoby Mathew { 283532ed618SSoby Mathew int rc; 284382ba743SBoyan Karatotev u_register_t resident_cpu_mpidr = 0; 285532ed618SSoby Mathew 286e60c1847SManish Pandey /* Validate the target cpu */ 287e60c1847SManish Pandey if (!is_valid_mpidr(target_cpu)) 288e60c1847SManish Pandey return PSCI_E_INVALID_PARAMS; 289e60c1847SManish Pandey 290532ed618SSoby Mathew rc = psci_spd_migrate_info(&resident_cpu_mpidr); 291c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_TOS_UP_MIG_CAP) { 292532ed618SSoby Mathew return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ? 293532ed618SSoby Mathew PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED; 294c7b0a28dSMaheedhar Bollapalli } 295532ed618SSoby Mathew 296532ed618SSoby Mathew /* 297532ed618SSoby Mathew * Migrate should only be invoked on the CPU where 298532ed618SSoby Mathew * the Secure OS is resident. 299532ed618SSoby Mathew */ 300c7b0a28dSMaheedhar Bollapalli if (resident_cpu_mpidr != read_mpidr_el1()) { 301532ed618SSoby Mathew return PSCI_E_NOT_PRESENT; 302c7b0a28dSMaheedhar Bollapalli } 303532ed618SSoby Mathew 304532ed618SSoby Mathew /* Check the validity of the specified target cpu */ 305c7b0a28dSMaheedhar Bollapalli if (!is_valid_mpidr(target_cpu)) { 306532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 307c7b0a28dSMaheedhar Bollapalli } 308532ed618SSoby Mathew 3096b7b0f36SAntonio Nino Diaz assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL)); 310532ed618SSoby Mathew 311532ed618SSoby Mathew rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu); 3126b7b0f36SAntonio Nino Diaz assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL)); 313532ed618SSoby Mathew 314532ed618SSoby Mathew return rc; 315532ed618SSoby Mathew } 316532ed618SSoby Mathew 317532ed618SSoby Mathew int psci_migrate_info_type(void) 318532ed618SSoby Mathew { 319532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 320532ed618SSoby Mathew 321532ed618SSoby Mathew return psci_spd_migrate_info(&resident_cpu_mpidr); 322532ed618SSoby Mathew } 323532ed618SSoby Mathew 3246b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void) 325532ed618SSoby Mathew { 326382ba743SBoyan Karatotev u_register_t resident_cpu_mpidr = 0; 327532ed618SSoby Mathew int rc; 328532ed618SSoby Mathew 329532ed618SSoby Mathew /* 330532ed618SSoby Mathew * Return value of this depends upon what 331532ed618SSoby Mathew * psci_spd_migrate_info() returns. 332532ed618SSoby Mathew */ 333532ed618SSoby Mathew rc = psci_spd_migrate_info(&resident_cpu_mpidr); 3346b7b0f36SAntonio Nino Diaz if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) 3356b7b0f36SAntonio Nino Diaz return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS; 336532ed618SSoby Mathew 337532ed618SSoby Mathew return resident_cpu_mpidr; 338532ed618SSoby Mathew } 339532ed618SSoby Mathew 34028d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu, 34128d3d614SJeenu Viswambharan unsigned int power_level) 34228d3d614SJeenu Viswambharan { 34328d3d614SJeenu Viswambharan int rc; 34428d3d614SJeenu Viswambharan 34528d3d614SJeenu Viswambharan /* Validate target_cpu */ 346e60c1847SManish Pandey if (!is_valid_mpidr(target_cpu)) 34728d3d614SJeenu Viswambharan return PSCI_E_INVALID_PARAMS; 34828d3d614SJeenu Viswambharan 34928d3d614SJeenu Viswambharan /* Validate power_level against PLAT_MAX_PWR_LVL */ 35028d3d614SJeenu Viswambharan if (power_level > PLAT_MAX_PWR_LVL) 35128d3d614SJeenu Viswambharan return PSCI_E_INVALID_PARAMS; 35228d3d614SJeenu Viswambharan 35328d3d614SJeenu Viswambharan /* 35428d3d614SJeenu Viswambharan * Dispatch this call to platform to query power controller, and pass on 35528d3d614SJeenu Viswambharan * to the caller what it returns 35628d3d614SJeenu Viswambharan */ 3576b7b0f36SAntonio Nino Diaz assert(psci_plat_pm_ops->get_node_hw_state != NULL); 35828d3d614SJeenu Viswambharan rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level); 3596b7b0f36SAntonio Nino Diaz assert(((rc >= HW_ON) && (rc <= HW_STANDBY)) 3606b7b0f36SAntonio Nino Diaz || (rc == PSCI_E_NOT_SUPPORTED) 3616b7b0f36SAntonio Nino Diaz || (rc == PSCI_E_INVALID_PARAMS)); 36228d3d614SJeenu Viswambharan return rc; 36328d3d614SJeenu Viswambharan } 36428d3d614SJeenu Viswambharan 365532ed618SSoby Mathew int psci_features(unsigned int psci_fid) 366532ed618SSoby Mathew { 367532ed618SSoby Mathew unsigned int local_caps = psci_caps; 368532ed618SSoby Mathew 369c7b0a28dSMaheedhar Bollapalli if (psci_fid == SMCCC_VERSION) { 3706eabbb07SDimitris Papastamos return PSCI_E_SUCCESS; 371c7b0a28dSMaheedhar Bollapalli } 372532ed618SSoby Mathew /* Check if it is a 64 bit function */ 373c7b0a28dSMaheedhar Bollapalli if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) { 374532ed618SSoby Mathew local_caps &= PSCI_CAP_64BIT_MASK; 375c7b0a28dSMaheedhar Bollapalli } 376532ed618SSoby Mathew /* Check for invalid fid */ 377532ed618SSoby Mathew if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid) 378c7b0a28dSMaheedhar Bollapalli && is_psci_fid(psci_fid))) { 379532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED; 380c7b0a28dSMaheedhar Bollapalli } 381532ed618SSoby Mathew 382532ed618SSoby Mathew /* Check if the psci fid is supported or not */ 383c7b0a28dSMaheedhar Bollapalli if ((local_caps & define_psci_cap(psci_fid)) == 0U) { 384532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED; 385c7b0a28dSMaheedhar Bollapalli } 386532ed618SSoby Mathew /* Format the feature flags */ 3876b7b0f36SAntonio Nino Diaz if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) || 3886b7b0f36SAntonio Nino Diaz (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) { 3896b7b0f36SAntonio Nino Diaz unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) | 3909a70e69eSWing Li (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT)); 3916b7b0f36SAntonio Nino Diaz return (int)ret; 392532ed618SSoby Mathew } 393532ed618SSoby Mathew 394532ed618SSoby Mathew /* Return 0 for all other fid's */ 395532ed618SSoby Mathew return PSCI_E_SUCCESS; 396532ed618SSoby Mathew } 397532ed618SSoby Mathew 398b88a4416SWing Li #if PSCI_OS_INIT_MODE 399b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode) 400b88a4416SWing Li { 401b88a4416SWing Li if (psci_suspend_mode == mode) { 402b88a4416SWing Li return PSCI_E_SUCCESS; 403b88a4416SWing Li } 404b88a4416SWing Li 4053b802105SBoyan Karatotev unsigned int this_core = plat_my_core_pos(); 4063b802105SBoyan Karatotev 407b88a4416SWing Li if (mode == PLAT_COORD) { 408b88a4416SWing Li /* Check if the current CPU is the last ON CPU in the system */ 4093b802105SBoyan Karatotev if (!psci_is_last_on_cpu_safe(this_core)) { 410b88a4416SWing Li return PSCI_E_DENIED; 411b88a4416SWing Li } 412b88a4416SWing Li } 413b88a4416SWing Li 414b88a4416SWing Li if (mode == OS_INIT) { 415b88a4416SWing Li /* 416b88a4416SWing Li * Check if all CPUs in the system are ON or if the current 417b88a4416SWing Li * CPU is the last ON CPU in the system. 418b88a4416SWing Li */ 4193b802105SBoyan Karatotev if (!(psci_are_all_cpus_on_safe(this_core) || 4203b802105SBoyan Karatotev psci_is_last_on_cpu_safe(this_core))) { 421b88a4416SWing Li return PSCI_E_DENIED; 422b88a4416SWing Li } 423b88a4416SWing Li } 424b88a4416SWing Li 425b88a4416SWing Li psci_suspend_mode = mode; 426b88a4416SWing Li psci_flush_dcache_range((uintptr_t)&psci_suspend_mode, 427b88a4416SWing Li sizeof(psci_suspend_mode)); 428b88a4416SWing Li 429b88a4416SWing Li return PSCI_E_SUCCESS; 430b88a4416SWing Li } 431b88a4416SWing Li #endif 432b88a4416SWing Li 433532ed618SSoby Mathew /******************************************************************************* 434532ed618SSoby Mathew * PSCI top level handler for servicing SMCs. 435532ed618SSoby Mathew ******************************************************************************/ 436cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid, 437532ed618SSoby Mathew u_register_t x1, 438532ed618SSoby Mathew u_register_t x2, 439532ed618SSoby Mathew u_register_t x3, 440532ed618SSoby Mathew u_register_t x4, 441532ed618SSoby Mathew void *cookie, 442532ed618SSoby Mathew void *handle, 443532ed618SSoby Mathew u_register_t flags) 444532ed618SSoby Mathew { 4456b7b0f36SAntonio Nino Diaz u_register_t ret; 4466b7b0f36SAntonio Nino Diaz 447c7b0a28dSMaheedhar Bollapalli if (is_caller_secure(flags)) { 4486b7b0f36SAntonio Nino Diaz return (u_register_t)SMC_UNK; 449c7b0a28dSMaheedhar Bollapalli } 450532ed618SSoby Mathew 451532ed618SSoby Mathew /* Check the fid against the capabilities */ 452c7b0a28dSMaheedhar Bollapalli if ((psci_caps & define_psci_cap(smc_fid)) == 0U) { 4536b7b0f36SAntonio Nino Diaz return (u_register_t)SMC_UNK; 454c7b0a28dSMaheedhar Bollapalli } 455532ed618SSoby Mathew 456532ed618SSoby Mathew if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { 457532ed618SSoby Mathew /* 32-bit PSCI function, clear top parameter bits */ 458532ed618SSoby Mathew 4596b7b0f36SAntonio Nino Diaz uint32_t r1 = (uint32_t)x1; 4606b7b0f36SAntonio Nino Diaz uint32_t r2 = (uint32_t)x2; 4616b7b0f36SAntonio Nino Diaz uint32_t r3 = (uint32_t)x3; 462532ed618SSoby Mathew 463532ed618SSoby Mathew switch (smc_fid) { 464532ed618SSoby Mathew case PSCI_VERSION: 4656b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_version(); 4666b7b0f36SAntonio Nino Diaz break; 467532ed618SSoby Mathew 468532ed618SSoby Mathew case PSCI_CPU_OFF: 4696b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_off(); 4706b7b0f36SAntonio Nino Diaz break; 471532ed618SSoby Mathew 472532ed618SSoby Mathew case PSCI_CPU_SUSPEND_AARCH32: 4736b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_suspend(r1, r2, r3); 4746b7b0f36SAntonio Nino Diaz break; 475532ed618SSoby Mathew 476532ed618SSoby Mathew case PSCI_CPU_ON_AARCH32: 4776b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_on(r1, r2, r3); 4786b7b0f36SAntonio Nino Diaz break; 479532ed618SSoby Mathew 480532ed618SSoby Mathew case PSCI_AFFINITY_INFO_AARCH32: 4816b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_affinity_info(r1, r2); 4826b7b0f36SAntonio Nino Diaz break; 483532ed618SSoby Mathew 484532ed618SSoby Mathew case PSCI_MIG_AARCH32: 4856b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate(r1); 4866b7b0f36SAntonio Nino Diaz break; 487532ed618SSoby Mathew 488532ed618SSoby Mathew case PSCI_MIG_INFO_TYPE: 4896b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate_info_type(); 4906b7b0f36SAntonio Nino Diaz break; 491532ed618SSoby Mathew 492532ed618SSoby Mathew case PSCI_MIG_INFO_UP_CPU_AARCH32: 4936b7b0f36SAntonio Nino Diaz ret = psci_migrate_info_up_cpu(); 4946b7b0f36SAntonio Nino Diaz break; 495532ed618SSoby Mathew 49628d3d614SJeenu Viswambharan case PSCI_NODE_HW_STATE_AARCH32: 4976b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_node_hw_state(r1, r2); 4986b7b0f36SAntonio Nino Diaz break; 49928d3d614SJeenu Viswambharan 500532ed618SSoby Mathew case PSCI_SYSTEM_SUSPEND_AARCH32: 5016b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_system_suspend(r1, r2); 5026b7b0f36SAntonio Nino Diaz break; 503532ed618SSoby Mathew 504532ed618SSoby Mathew case PSCI_SYSTEM_OFF: 505532ed618SSoby Mathew psci_system_off(); 506532ed618SSoby Mathew /* We should never return from psci_system_off() */ 5073eacacc0SJonathan Wright break; 508532ed618SSoby Mathew 509532ed618SSoby Mathew case PSCI_SYSTEM_RESET: 510532ed618SSoby Mathew psci_system_reset(); 511532ed618SSoby Mathew /* We should never return from psci_system_reset() */ 5123eacacc0SJonathan Wright break; 513532ed618SSoby Mathew 514532ed618SSoby Mathew case PSCI_FEATURES: 5156b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_features(r1); 5166b7b0f36SAntonio Nino Diaz break; 517532ed618SSoby Mathew 518b88a4416SWing Li #if PSCI_OS_INIT_MODE 519b88a4416SWing Li case PSCI_SET_SUSPEND_MODE: 520b88a4416SWing Li ret = (u_register_t)psci_set_suspend_mode(r1); 521b88a4416SWing Li break; 522b88a4416SWing Li #endif 523b88a4416SWing Li 524532ed618SSoby Mathew #if ENABLE_PSCI_STAT 525532ed618SSoby Mathew case PSCI_STAT_RESIDENCY_AARCH32: 5266b7b0f36SAntonio Nino Diaz ret = psci_stat_residency(r1, r2); 5276b7b0f36SAntonio Nino Diaz break; 528532ed618SSoby Mathew 529532ed618SSoby Mathew case PSCI_STAT_COUNT_AARCH32: 5306b7b0f36SAntonio Nino Diaz ret = psci_stat_count(r1, r2); 5316b7b0f36SAntonio Nino Diaz break; 532532ed618SSoby Mathew #endif 533d4c596beSRoberto Vargas case PSCI_MEM_PROTECT: 5346b7b0f36SAntonio Nino Diaz ret = psci_mem_protect(r1); 5356b7b0f36SAntonio Nino Diaz break; 536d4c596beSRoberto Vargas 537d4c596beSRoberto Vargas case PSCI_MEM_CHK_RANGE_AARCH32: 5386b7b0f36SAntonio Nino Diaz ret = psci_mem_chk_range(r1, r2); 5396b7b0f36SAntonio Nino Diaz break; 540532ed618SSoby Mathew 54136a8f8fdSRoberto Vargas case PSCI_SYSTEM_RESET2_AARCH32: 54236a8f8fdSRoberto Vargas /* We should never return from psci_system_reset2() */ 5436b7b0f36SAntonio Nino Diaz ret = psci_system_reset2(r1, r2); 5446b7b0f36SAntonio Nino Diaz break; 54536a8f8fdSRoberto Vargas 546532ed618SSoby Mathew default: 5476b7b0f36SAntonio Nino Diaz WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 5486b7b0f36SAntonio Nino Diaz ret = (u_register_t)SMC_UNK; 549532ed618SSoby Mathew break; 550532ed618SSoby Mathew } 551532ed618SSoby Mathew } else { 552532ed618SSoby Mathew /* 64-bit PSCI function */ 553532ed618SSoby Mathew 554532ed618SSoby Mathew switch (smc_fid) { 555532ed618SSoby Mathew case PSCI_CPU_SUSPEND_AARCH64: 5566b7b0f36SAntonio Nino Diaz ret = (u_register_t) 5576b7b0f36SAntonio Nino Diaz psci_cpu_suspend((unsigned int)x1, x2, x3); 5586b7b0f36SAntonio Nino Diaz break; 559532ed618SSoby Mathew 560532ed618SSoby Mathew case PSCI_CPU_ON_AARCH64: 5616b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_on(x1, x2, x3); 5626b7b0f36SAntonio Nino Diaz break; 563532ed618SSoby Mathew 564532ed618SSoby Mathew case PSCI_AFFINITY_INFO_AARCH64: 5656b7b0f36SAntonio Nino Diaz ret = (u_register_t) 5666b7b0f36SAntonio Nino Diaz psci_affinity_info(x1, (unsigned int)x2); 5676b7b0f36SAntonio Nino Diaz break; 568532ed618SSoby Mathew 569532ed618SSoby Mathew case PSCI_MIG_AARCH64: 5706b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate(x1); 5716b7b0f36SAntonio Nino Diaz break; 572532ed618SSoby Mathew 573532ed618SSoby Mathew case PSCI_MIG_INFO_UP_CPU_AARCH64: 5746b7b0f36SAntonio Nino Diaz ret = psci_migrate_info_up_cpu(); 5756b7b0f36SAntonio Nino Diaz break; 576532ed618SSoby Mathew 57728d3d614SJeenu Viswambharan case PSCI_NODE_HW_STATE_AARCH64: 5786b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_node_hw_state( 5796b7b0f36SAntonio Nino Diaz x1, (unsigned int) x2); 5806b7b0f36SAntonio Nino Diaz break; 58128d3d614SJeenu Viswambharan 582532ed618SSoby Mathew case PSCI_SYSTEM_SUSPEND_AARCH64: 5836b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_system_suspend(x1, x2); 5846b7b0f36SAntonio Nino Diaz break; 585532ed618SSoby Mathew 586532ed618SSoby Mathew #if ENABLE_PSCI_STAT 587532ed618SSoby Mathew case PSCI_STAT_RESIDENCY_AARCH64: 5886b7b0f36SAntonio Nino Diaz ret = psci_stat_residency(x1, (unsigned int) x2); 5896b7b0f36SAntonio Nino Diaz break; 590532ed618SSoby Mathew 591532ed618SSoby Mathew case PSCI_STAT_COUNT_AARCH64: 5926b7b0f36SAntonio Nino Diaz ret = psci_stat_count(x1, (unsigned int) x2); 5936b7b0f36SAntonio Nino Diaz break; 594532ed618SSoby Mathew #endif 595532ed618SSoby Mathew 596d4c596beSRoberto Vargas case PSCI_MEM_CHK_RANGE_AARCH64: 5976b7b0f36SAntonio Nino Diaz ret = psci_mem_chk_range(x1, x2); 5986b7b0f36SAntonio Nino Diaz break; 599d4c596beSRoberto Vargas 60036a8f8fdSRoberto Vargas case PSCI_SYSTEM_RESET2_AARCH64: 60136a8f8fdSRoberto Vargas /* We should never return from psci_system_reset2() */ 6026b7b0f36SAntonio Nino Diaz ret = psci_system_reset2((uint32_t) x1, x2); 6036b7b0f36SAntonio Nino Diaz break; 604d4c596beSRoberto Vargas 605532ed618SSoby Mathew default: 6066b7b0f36SAntonio Nino Diaz WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 6076b7b0f36SAntonio Nino Diaz ret = (u_register_t)SMC_UNK; 608532ed618SSoby Mathew break; 609532ed618SSoby Mathew } 610532ed618SSoby Mathew } 611532ed618SSoby Mathew 6126b7b0f36SAntonio Nino Diaz return ret; 613532ed618SSoby Mathew } 614