1532ed618SSoby Mathew /* 2*ef738d19SManish Pandey * 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; 31*ef738d19SManish Pandey entry_point_info_t *ep; 32*ef738d19SManish 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 39*ef738d19SManish Pandey ep = get_cpu_data_by_index(target_idx, warmboot_ep_info); 40*ef738d19SManish Pandey /* Validate the lower EL entry point and put it in the entry_point_info */ 41*ef738d19SManish 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 */ 50*ef738d19SManish 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(); 673b802105SBoyan Karatotev #if PSCI_OS_INIT_MODE 68606b7430SWing Li plat_local_state_t prev[PLAT_MAX_PWR_LVL]; 69606b7430SWing Li #endif 70532ed618SSoby Mathew 7145c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN 7245c7328cSBoyan Karatotev /* 7345c7328cSBoyan Karatotev * If SME isn't off, attempting a real power down will only end up being 7445c7328cSBoyan Karatotev * rejected. If we got called with SME on, fall back to a normal 7545c7328cSBoyan Karatotev * suspend. We can't force SME off as in the event the power down is 7645c7328cSBoyan Karatotev * rejected for another reason (eg GIC) we'd lose the SME context. 7745c7328cSBoyan Karatotev */ 7845c7328cSBoyan Karatotev if (is_feat_sme_supported() && read_svcr() != 0) { 7945c7328cSBoyan Karatotev power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT); 8045c7328cSBoyan Karatotev power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT); 8145c7328cSBoyan Karatotev } 8245c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */ 8345c7328cSBoyan Karatotev 84532ed618SSoby Mathew /* Validate the power_state parameter */ 85532ed618SSoby Mathew rc = psci_validate_power_state(power_state, &state_info); 86532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) { 87532ed618SSoby Mathew assert(rc == PSCI_E_INVALID_PARAMS); 88532ed618SSoby Mathew return rc; 89532ed618SSoby Mathew } 90532ed618SSoby Mathew 91532ed618SSoby Mathew /* 92532ed618SSoby Mathew * Get the value of the state type bit from the power state parameter. 93532ed618SSoby Mathew */ 94532ed618SSoby Mathew is_power_down_state = psci_get_pstate_type(power_state); 95532ed618SSoby Mathew 96532ed618SSoby Mathew /* Sanity check the requested suspend levels */ 97532ed618SSoby Mathew assert(psci_validate_suspend_req(&state_info, is_power_down_state) 98532ed618SSoby Mathew == PSCI_E_SUCCESS); 99532ed618SSoby Mathew 100532ed618SSoby Mathew target_pwrlvl = psci_find_target_suspend_lvl(&state_info); 101a1c3faa6SSandrine Bailleux if (target_pwrlvl == PSCI_INVALID_PWR_LVL) { 102a1c3faa6SSandrine Bailleux ERROR("Invalid target power level for suspend operation\n"); 103a1c3faa6SSandrine Bailleux panic(); 104a1c3faa6SSandrine Bailleux } 105532ed618SSoby Mathew 106532ed618SSoby Mathew /* Fast path for CPU standby.*/ 107362030bfSAntonio Nino Diaz if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) { 108c7b0a28dSMaheedhar Bollapalli if (psci_plat_pm_ops->cpu_standby == NULL) { 109532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 110c7b0a28dSMaheedhar Bollapalli } 111532ed618SSoby Mathew 112532ed618SSoby Mathew /* 113532ed618SSoby Mathew * Set the state of the CPU power domain to the platform 114532ed618SSoby Mathew * specific retention state and enter the standby state. 115532ed618SSoby Mathew */ 116532ed618SSoby Mathew cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL]; 117532ed618SSoby Mathew psci_set_cpu_local_state(cpu_pd_state); 118532ed618SSoby Mathew 119606b7430SWing Li #if PSCI_OS_INIT_MODE 120606b7430SWing Li /* 121606b7430SWing Li * If in OS-initiated mode, save a copy of the previous 122606b7430SWing Li * requested local power states and update the new requested 123606b7430SWing Li * local power states for this CPU. 124606b7430SWing Li */ 125606b7430SWing Li if (psci_suspend_mode == OS_INIT) { 126606b7430SWing Li psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx, 127606b7430SWing Li &state_info, prev); 128606b7430SWing Li } 129606b7430SWing Li #endif 130606b7430SWing Li 131532ed618SSoby Mathew #if ENABLE_PSCI_STAT 13204c1db1eSdp-arm plat_psci_stat_accounting_start(&state_info); 133532ed618SSoby Mathew #endif 134532ed618SSoby Mathew 135872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 136872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 137872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 138872be88aSdp-arm PMF_NO_CACHE_MAINT); 139872be88aSdp-arm #endif 140872be88aSdp-arm 141532ed618SSoby Mathew psci_plat_pm_ops->cpu_standby(cpu_pd_state); 142532ed618SSoby Mathew 143532ed618SSoby Mathew /* Upon exit from standby, set the state back to RUN. */ 144532ed618SSoby Mathew psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN); 145532ed618SSoby Mathew 146606b7430SWing Li #if PSCI_OS_INIT_MODE 147606b7430SWing Li /* 148606b7430SWing Li * If in OS-initiated mode, restore the previous requested 149606b7430SWing Li * local power states for this CPU. 150606b7430SWing Li */ 151606b7430SWing Li if (psci_suspend_mode == OS_INIT) { 152606b7430SWing Li psci_restore_req_local_pwr_states(cpu_idx, prev); 153606b7430SWing Li } 154606b7430SWing Li #endif 155606b7430SWing Li 156872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 157872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 158872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 159872be88aSdp-arm PMF_NO_CACHE_MAINT); 160872be88aSdp-arm #endif 161872be88aSdp-arm 162532ed618SSoby Mathew #if ENABLE_PSCI_STAT 16304c1db1eSdp-arm plat_psci_stat_accounting_stop(&state_info); 164532ed618SSoby Mathew 165532ed618SSoby Mathew /* Update PSCI stats */ 1663b802105SBoyan Karatotev psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info); 167532ed618SSoby Mathew #endif 168532ed618SSoby Mathew 169532ed618SSoby Mathew return PSCI_E_SUCCESS; 170532ed618SSoby Mathew } 171532ed618SSoby Mathew 172532ed618SSoby Mathew /* 173532ed618SSoby Mathew * If a power down state has been requested, we need to verify entry 174532ed618SSoby Mathew * point and program entry information. 175532ed618SSoby Mathew */ 1766b7b0f36SAntonio Nino Diaz if (is_power_down_state != 0U) { 177*ef738d19SManish Pandey entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info); 178*ef738d19SManish Pandey 179*ef738d19SManish Pandey rc = psci_validate_entry_point(ep, entrypoint, context_id); 180c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_E_SUCCESS) { 181532ed618SSoby Mathew return rc; 182532ed618SSoby Mathew } 183c7b0a28dSMaheedhar Bollapalli } 184532ed618SSoby Mathew 185532ed618SSoby Mathew /* 186532ed618SSoby Mathew * Do what is needed to enter the power down state. Upon success, 187532ed618SSoby Mathew * enter the final wfi which will power down this CPU. This function 188532ed618SSoby Mathew * might return if the power down was abandoned for any reason, e.g. 189532ed618SSoby Mathew * arrival of an interrupt 190532ed618SSoby Mathew */ 1913b802105SBoyan Karatotev rc = psci_cpu_suspend_start(cpu_idx, 192532ed618SSoby Mathew target_pwrlvl, 193532ed618SSoby Mathew &state_info, 194532ed618SSoby Mathew is_power_down_state); 195532ed618SSoby Mathew 196606b7430SWing Li return rc; 197532ed618SSoby Mathew } 198532ed618SSoby Mathew 199532ed618SSoby Mathew 200532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id) 201532ed618SSoby Mathew { 202532ed618SSoby Mathew int rc; 203532ed618SSoby Mathew psci_power_state_t state_info; 2043b802105SBoyan Karatotev unsigned int cpu_idx = plat_my_core_pos(); 205*ef738d19SManish Pandey entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info); 206532ed618SSoby Mathew 207532ed618SSoby Mathew /* Check if the current CPU is the last ON CPU in the system */ 208c7b0a28dSMaheedhar Bollapalli if (!psci_is_last_on_cpu(cpu_idx)) { 209532ed618SSoby Mathew return PSCI_E_DENIED; 210c7b0a28dSMaheedhar Bollapalli } 211532ed618SSoby Mathew 212532ed618SSoby Mathew /* Validate the entry point and get the entry_point_info */ 213*ef738d19SManish Pandey rc = psci_validate_entry_point(ep, entrypoint, context_id); 214c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_E_SUCCESS) { 215532ed618SSoby Mathew return rc; 216c7b0a28dSMaheedhar Bollapalli } 217532ed618SSoby Mathew 218532ed618SSoby Mathew /* Query the psci_power_state for system suspend */ 219532ed618SSoby Mathew psci_query_sys_suspend_pwrstate(&state_info); 220532ed618SSoby Mathew 221a4065abdSldts /* 222a4065abdSldts * Check if platform allows suspend to Highest power level 223a4065abdSldts * (System level) 224a4065abdSldts */ 225c7b0a28dSMaheedhar Bollapalli if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) { 226a4065abdSldts return PSCI_E_DENIED; 227c7b0a28dSMaheedhar Bollapalli } 228532ed618SSoby Mathew /* Ensure that the psci_power_state makes sense */ 229532ed618SSoby Mathew assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN) 230532ed618SSoby Mathew == PSCI_E_SUCCESS); 2316b7b0f36SAntonio Nino Diaz assert(is_local_state_off( 2326b7b0f36SAntonio Nino Diaz state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0); 233532ed618SSoby Mathew 234532ed618SSoby Mathew /* 235532ed618SSoby Mathew * Do what is needed to enter the system suspend state. This function 236532ed618SSoby Mathew * might return if the power down was abandoned for any reason, e.g. 237532ed618SSoby Mathew * arrival of an interrupt 238532ed618SSoby Mathew */ 2393b802105SBoyan Karatotev rc = psci_cpu_suspend_start(cpu_idx, 240532ed618SSoby Mathew PLAT_MAX_PWR_LVL, 241532ed618SSoby Mathew &state_info, 242532ed618SSoby Mathew PSTATE_TYPE_POWERDOWN); 243532ed618SSoby Mathew 244606b7430SWing Li return rc; 245532ed618SSoby Mathew } 246532ed618SSoby Mathew 247532ed618SSoby Mathew int psci_cpu_off(void) 248532ed618SSoby Mathew { 249532ed618SSoby Mathew int rc; 250532ed618SSoby Mathew unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL; 251532ed618SSoby Mathew 252532ed618SSoby Mathew /* 253532ed618SSoby Mathew * Do what is needed to power off this CPU and possible higher power 254532ed618SSoby Mathew * levels if it able to do so. Upon success, enter the final wfi 255532ed618SSoby Mathew * which will power down this CPU. 256532ed618SSoby Mathew */ 257532ed618SSoby Mathew rc = psci_do_cpu_off(target_pwrlvl); 258532ed618SSoby Mathew 259532ed618SSoby Mathew /* 260532ed618SSoby Mathew * The only error cpu_off can return is E_DENIED. So check if that's 261532ed618SSoby Mathew * indeed the case. 262532ed618SSoby Mathew */ 263532ed618SSoby Mathew assert(rc == PSCI_E_DENIED); 264532ed618SSoby Mathew 265532ed618SSoby Mathew return rc; 266532ed618SSoby Mathew } 267532ed618SSoby Mathew 268532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity, 269532ed618SSoby Mathew unsigned int lowest_affinity_level) 270532ed618SSoby Mathew { 2715b33ad17SDeepika Bhavnani unsigned int target_idx; 272532ed618SSoby Mathew 273e60c1847SManish Pandey /* Validate the target affinity */ 274c7b0a28dSMaheedhar Bollapalli if (!is_valid_mpidr(target_affinity)) { 275e60c1847SManish Pandey return PSCI_E_INVALID_PARAMS; 276c7b0a28dSMaheedhar Bollapalli } 277e60c1847SManish Pandey 278532ed618SSoby Mathew /* We dont support level higher than PSCI_CPU_PWR_LVL */ 279c7b0a28dSMaheedhar Bollapalli if (lowest_affinity_level > PSCI_CPU_PWR_LVL) { 280532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 281c7b0a28dSMaheedhar Bollapalli } 282532ed618SSoby Mathew /* Calculate the cpu index of the target */ 283e60c1847SManish Pandey target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity); 284532ed618SSoby Mathew 2858fd307ffSRoberto Vargas /* 2868fd307ffSRoberto Vargas * Generic management: 2878fd307ffSRoberto Vargas * Perform cache maintanence ahead of reading the target CPU state to 2888fd307ffSRoberto Vargas * ensure that the data is not stale. 2898fd307ffSRoberto Vargas * There is a theoretical edge case where the cache may contain stale 2908fd307ffSRoberto Vargas * data for the target CPU data - this can occur under the following 2918fd307ffSRoberto Vargas * conditions: 2928fd307ffSRoberto Vargas * - the target CPU is in another cluster from the current 2938fd307ffSRoberto Vargas * - the target CPU was the last CPU to shutdown on its cluster 2948fd307ffSRoberto Vargas * - the cluster was removed from coherency as part of the CPU shutdown 2958fd307ffSRoberto Vargas * 2968fd307ffSRoberto Vargas * In this case the cache maintenace that was performed as part of the 2978fd307ffSRoberto Vargas * target CPUs shutdown was not seen by the current CPU's cluster. And 2988fd307ffSRoberto Vargas * so the cache may contain stale data for the target CPU. 2998fd307ffSRoberto Vargas */ 3005b33ad17SDeepika Bhavnani flush_cpu_data_by_index(target_idx, 3016b7b0f36SAntonio Nino Diaz psci_svc_cpu_data.aff_info_state); 3028fd307ffSRoberto Vargas 303532ed618SSoby Mathew return psci_get_aff_info_state_by_idx(target_idx); 304532ed618SSoby Mathew } 305532ed618SSoby Mathew 306532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu) 307532ed618SSoby Mathew { 308532ed618SSoby Mathew int rc; 309532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 310532ed618SSoby Mathew 311e60c1847SManish Pandey /* Validate the target cpu */ 312e60c1847SManish Pandey if (!is_valid_mpidr(target_cpu)) 313e60c1847SManish Pandey return PSCI_E_INVALID_PARAMS; 314e60c1847SManish Pandey 315532ed618SSoby Mathew rc = psci_spd_migrate_info(&resident_cpu_mpidr); 316c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_TOS_UP_MIG_CAP) { 317532ed618SSoby Mathew return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ? 318532ed618SSoby Mathew PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED; 319c7b0a28dSMaheedhar Bollapalli } 320532ed618SSoby Mathew 321532ed618SSoby Mathew /* 322532ed618SSoby Mathew * Migrate should only be invoked on the CPU where 323532ed618SSoby Mathew * the Secure OS is resident. 324532ed618SSoby Mathew */ 325c7b0a28dSMaheedhar Bollapalli if (resident_cpu_mpidr != read_mpidr_el1()) { 326532ed618SSoby Mathew return PSCI_E_NOT_PRESENT; 327c7b0a28dSMaheedhar Bollapalli } 328532ed618SSoby Mathew 329532ed618SSoby Mathew /* Check the validity of the specified target cpu */ 330c7b0a28dSMaheedhar Bollapalli if (!is_valid_mpidr(target_cpu)) { 331532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 332c7b0a28dSMaheedhar Bollapalli } 333532ed618SSoby Mathew 3346b7b0f36SAntonio Nino Diaz assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL)); 335532ed618SSoby Mathew 336532ed618SSoby Mathew rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu); 3376b7b0f36SAntonio Nino Diaz assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL)); 338532ed618SSoby Mathew 339532ed618SSoby Mathew return rc; 340532ed618SSoby Mathew } 341532ed618SSoby Mathew 342532ed618SSoby Mathew int psci_migrate_info_type(void) 343532ed618SSoby Mathew { 344532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 345532ed618SSoby Mathew 346532ed618SSoby Mathew return psci_spd_migrate_info(&resident_cpu_mpidr); 347532ed618SSoby Mathew } 348532ed618SSoby Mathew 3496b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void) 350532ed618SSoby Mathew { 351532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 352532ed618SSoby Mathew int rc; 353532ed618SSoby Mathew 354532ed618SSoby Mathew /* 355532ed618SSoby Mathew * Return value of this depends upon what 356532ed618SSoby Mathew * psci_spd_migrate_info() returns. 357532ed618SSoby Mathew */ 358532ed618SSoby Mathew rc = psci_spd_migrate_info(&resident_cpu_mpidr); 3596b7b0f36SAntonio Nino Diaz if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) 3606b7b0f36SAntonio Nino Diaz return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS; 361532ed618SSoby Mathew 362532ed618SSoby Mathew return resident_cpu_mpidr; 363532ed618SSoby Mathew } 364532ed618SSoby Mathew 36528d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu, 36628d3d614SJeenu Viswambharan unsigned int power_level) 36728d3d614SJeenu Viswambharan { 36828d3d614SJeenu Viswambharan int rc; 36928d3d614SJeenu Viswambharan 37028d3d614SJeenu Viswambharan /* Validate target_cpu */ 371e60c1847SManish Pandey if (!is_valid_mpidr(target_cpu)) 37228d3d614SJeenu Viswambharan return PSCI_E_INVALID_PARAMS; 37328d3d614SJeenu Viswambharan 37428d3d614SJeenu Viswambharan /* Validate power_level against PLAT_MAX_PWR_LVL */ 37528d3d614SJeenu Viswambharan if (power_level > PLAT_MAX_PWR_LVL) 37628d3d614SJeenu Viswambharan return PSCI_E_INVALID_PARAMS; 37728d3d614SJeenu Viswambharan 37828d3d614SJeenu Viswambharan /* 37928d3d614SJeenu Viswambharan * Dispatch this call to platform to query power controller, and pass on 38028d3d614SJeenu Viswambharan * to the caller what it returns 38128d3d614SJeenu Viswambharan */ 3826b7b0f36SAntonio Nino Diaz assert(psci_plat_pm_ops->get_node_hw_state != NULL); 38328d3d614SJeenu Viswambharan rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level); 3846b7b0f36SAntonio Nino Diaz assert(((rc >= HW_ON) && (rc <= HW_STANDBY)) 3856b7b0f36SAntonio Nino Diaz || (rc == PSCI_E_NOT_SUPPORTED) 3866b7b0f36SAntonio Nino Diaz || (rc == PSCI_E_INVALID_PARAMS)); 38728d3d614SJeenu Viswambharan return rc; 38828d3d614SJeenu Viswambharan } 38928d3d614SJeenu Viswambharan 390532ed618SSoby Mathew int psci_features(unsigned int psci_fid) 391532ed618SSoby Mathew { 392532ed618SSoby Mathew unsigned int local_caps = psci_caps; 393532ed618SSoby Mathew 394c7b0a28dSMaheedhar Bollapalli if (psci_fid == SMCCC_VERSION) { 3956eabbb07SDimitris Papastamos return PSCI_E_SUCCESS; 396c7b0a28dSMaheedhar Bollapalli } 397532ed618SSoby Mathew /* Check if it is a 64 bit function */ 398c7b0a28dSMaheedhar Bollapalli if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) { 399532ed618SSoby Mathew local_caps &= PSCI_CAP_64BIT_MASK; 400c7b0a28dSMaheedhar Bollapalli } 401532ed618SSoby Mathew /* Check for invalid fid */ 402532ed618SSoby Mathew if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid) 403c7b0a28dSMaheedhar Bollapalli && is_psci_fid(psci_fid))) { 404532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED; 405c7b0a28dSMaheedhar Bollapalli } 406532ed618SSoby Mathew 407532ed618SSoby Mathew /* Check if the psci fid is supported or not */ 408c7b0a28dSMaheedhar Bollapalli if ((local_caps & define_psci_cap(psci_fid)) == 0U) { 409532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED; 410c7b0a28dSMaheedhar Bollapalli } 411532ed618SSoby Mathew /* Format the feature flags */ 4126b7b0f36SAntonio Nino Diaz if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) || 4136b7b0f36SAntonio Nino Diaz (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) { 4146b7b0f36SAntonio Nino Diaz unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) | 4159a70e69eSWing Li (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT)); 4166b7b0f36SAntonio Nino Diaz return (int)ret; 417532ed618SSoby Mathew } 418532ed618SSoby Mathew 419532ed618SSoby Mathew /* Return 0 for all other fid's */ 420532ed618SSoby Mathew return PSCI_E_SUCCESS; 421532ed618SSoby Mathew } 422532ed618SSoby Mathew 423b88a4416SWing Li #if PSCI_OS_INIT_MODE 424b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode) 425b88a4416SWing Li { 426b88a4416SWing Li if (psci_suspend_mode == mode) { 427b88a4416SWing Li return PSCI_E_SUCCESS; 428b88a4416SWing Li } 429b88a4416SWing Li 4303b802105SBoyan Karatotev unsigned int this_core = plat_my_core_pos(); 4313b802105SBoyan Karatotev 432b88a4416SWing Li if (mode == PLAT_COORD) { 433b88a4416SWing Li /* Check if the current CPU is the last ON CPU in the system */ 4343b802105SBoyan Karatotev if (!psci_is_last_on_cpu_safe(this_core)) { 435b88a4416SWing Li return PSCI_E_DENIED; 436b88a4416SWing Li } 437b88a4416SWing Li } 438b88a4416SWing Li 439b88a4416SWing Li if (mode == OS_INIT) { 440b88a4416SWing Li /* 441b88a4416SWing Li * Check if all CPUs in the system are ON or if the current 442b88a4416SWing Li * CPU is the last ON CPU in the system. 443b88a4416SWing Li */ 4443b802105SBoyan Karatotev if (!(psci_are_all_cpus_on_safe(this_core) || 4453b802105SBoyan Karatotev psci_is_last_on_cpu_safe(this_core))) { 446b88a4416SWing Li return PSCI_E_DENIED; 447b88a4416SWing Li } 448b88a4416SWing Li } 449b88a4416SWing Li 450b88a4416SWing Li psci_suspend_mode = mode; 451b88a4416SWing Li psci_flush_dcache_range((uintptr_t)&psci_suspend_mode, 452b88a4416SWing Li sizeof(psci_suspend_mode)); 453b88a4416SWing Li 454b88a4416SWing Li return PSCI_E_SUCCESS; 455b88a4416SWing Li } 456b88a4416SWing Li #endif 457b88a4416SWing Li 458532ed618SSoby Mathew /******************************************************************************* 459532ed618SSoby Mathew * PSCI top level handler for servicing SMCs. 460532ed618SSoby Mathew ******************************************************************************/ 461cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid, 462532ed618SSoby Mathew u_register_t x1, 463532ed618SSoby Mathew u_register_t x2, 464532ed618SSoby Mathew u_register_t x3, 465532ed618SSoby Mathew u_register_t x4, 466532ed618SSoby Mathew void *cookie, 467532ed618SSoby Mathew void *handle, 468532ed618SSoby Mathew u_register_t flags) 469532ed618SSoby Mathew { 4706b7b0f36SAntonio Nino Diaz u_register_t ret; 4716b7b0f36SAntonio Nino Diaz 472c7b0a28dSMaheedhar Bollapalli if (is_caller_secure(flags)) { 4736b7b0f36SAntonio Nino Diaz return (u_register_t)SMC_UNK; 474c7b0a28dSMaheedhar Bollapalli } 475532ed618SSoby Mathew 476532ed618SSoby Mathew /* Check the fid against the capabilities */ 477c7b0a28dSMaheedhar Bollapalli if ((psci_caps & define_psci_cap(smc_fid)) == 0U) { 4786b7b0f36SAntonio Nino Diaz return (u_register_t)SMC_UNK; 479c7b0a28dSMaheedhar Bollapalli } 480532ed618SSoby Mathew 481532ed618SSoby Mathew if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { 482532ed618SSoby Mathew /* 32-bit PSCI function, clear top parameter bits */ 483532ed618SSoby Mathew 4846b7b0f36SAntonio Nino Diaz uint32_t r1 = (uint32_t)x1; 4856b7b0f36SAntonio Nino Diaz uint32_t r2 = (uint32_t)x2; 4866b7b0f36SAntonio Nino Diaz uint32_t r3 = (uint32_t)x3; 487532ed618SSoby Mathew 488532ed618SSoby Mathew switch (smc_fid) { 489532ed618SSoby Mathew case PSCI_VERSION: 4906b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_version(); 4916b7b0f36SAntonio Nino Diaz break; 492532ed618SSoby Mathew 493532ed618SSoby Mathew case PSCI_CPU_OFF: 4946b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_off(); 4956b7b0f36SAntonio Nino Diaz break; 496532ed618SSoby Mathew 497532ed618SSoby Mathew case PSCI_CPU_SUSPEND_AARCH32: 4986b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_suspend(r1, r2, r3); 4996b7b0f36SAntonio Nino Diaz break; 500532ed618SSoby Mathew 501532ed618SSoby Mathew case PSCI_CPU_ON_AARCH32: 5026b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_on(r1, r2, r3); 5036b7b0f36SAntonio Nino Diaz break; 504532ed618SSoby Mathew 505532ed618SSoby Mathew case PSCI_AFFINITY_INFO_AARCH32: 5066b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_affinity_info(r1, r2); 5076b7b0f36SAntonio Nino Diaz break; 508532ed618SSoby Mathew 509532ed618SSoby Mathew case PSCI_MIG_AARCH32: 5106b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate(r1); 5116b7b0f36SAntonio Nino Diaz break; 512532ed618SSoby Mathew 513532ed618SSoby Mathew case PSCI_MIG_INFO_TYPE: 5146b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate_info_type(); 5156b7b0f36SAntonio Nino Diaz break; 516532ed618SSoby Mathew 517532ed618SSoby Mathew case PSCI_MIG_INFO_UP_CPU_AARCH32: 5186b7b0f36SAntonio Nino Diaz ret = psci_migrate_info_up_cpu(); 5196b7b0f36SAntonio Nino Diaz break; 520532ed618SSoby Mathew 52128d3d614SJeenu Viswambharan case PSCI_NODE_HW_STATE_AARCH32: 5226b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_node_hw_state(r1, r2); 5236b7b0f36SAntonio Nino Diaz break; 52428d3d614SJeenu Viswambharan 525532ed618SSoby Mathew case PSCI_SYSTEM_SUSPEND_AARCH32: 5266b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_system_suspend(r1, r2); 5276b7b0f36SAntonio Nino Diaz break; 528532ed618SSoby Mathew 529532ed618SSoby Mathew case PSCI_SYSTEM_OFF: 530532ed618SSoby Mathew psci_system_off(); 531532ed618SSoby Mathew /* We should never return from psci_system_off() */ 5323eacacc0SJonathan Wright break; 533532ed618SSoby Mathew 534532ed618SSoby Mathew case PSCI_SYSTEM_RESET: 535532ed618SSoby Mathew psci_system_reset(); 536532ed618SSoby Mathew /* We should never return from psci_system_reset() */ 5373eacacc0SJonathan Wright break; 538532ed618SSoby Mathew 539532ed618SSoby Mathew case PSCI_FEATURES: 5406b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_features(r1); 5416b7b0f36SAntonio Nino Diaz break; 542532ed618SSoby Mathew 543b88a4416SWing Li #if PSCI_OS_INIT_MODE 544b88a4416SWing Li case PSCI_SET_SUSPEND_MODE: 545b88a4416SWing Li ret = (u_register_t)psci_set_suspend_mode(r1); 546b88a4416SWing Li break; 547b88a4416SWing Li #endif 548b88a4416SWing Li 549532ed618SSoby Mathew #if ENABLE_PSCI_STAT 550532ed618SSoby Mathew case PSCI_STAT_RESIDENCY_AARCH32: 5516b7b0f36SAntonio Nino Diaz ret = psci_stat_residency(r1, r2); 5526b7b0f36SAntonio Nino Diaz break; 553532ed618SSoby Mathew 554532ed618SSoby Mathew case PSCI_STAT_COUNT_AARCH32: 5556b7b0f36SAntonio Nino Diaz ret = psci_stat_count(r1, r2); 5566b7b0f36SAntonio Nino Diaz break; 557532ed618SSoby Mathew #endif 558d4c596beSRoberto Vargas case PSCI_MEM_PROTECT: 5596b7b0f36SAntonio Nino Diaz ret = psci_mem_protect(r1); 5606b7b0f36SAntonio Nino Diaz break; 561d4c596beSRoberto Vargas 562d4c596beSRoberto Vargas case PSCI_MEM_CHK_RANGE_AARCH32: 5636b7b0f36SAntonio Nino Diaz ret = psci_mem_chk_range(r1, r2); 5646b7b0f36SAntonio Nino Diaz break; 565532ed618SSoby Mathew 56636a8f8fdSRoberto Vargas case PSCI_SYSTEM_RESET2_AARCH32: 56736a8f8fdSRoberto Vargas /* We should never return from psci_system_reset2() */ 5686b7b0f36SAntonio Nino Diaz ret = psci_system_reset2(r1, r2); 5696b7b0f36SAntonio Nino Diaz break; 57036a8f8fdSRoberto Vargas 571532ed618SSoby Mathew default: 5726b7b0f36SAntonio Nino Diaz WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 5736b7b0f36SAntonio Nino Diaz ret = (u_register_t)SMC_UNK; 574532ed618SSoby Mathew break; 575532ed618SSoby Mathew } 576532ed618SSoby Mathew } else { 577532ed618SSoby Mathew /* 64-bit PSCI function */ 578532ed618SSoby Mathew 579532ed618SSoby Mathew switch (smc_fid) { 580532ed618SSoby Mathew case PSCI_CPU_SUSPEND_AARCH64: 5816b7b0f36SAntonio Nino Diaz ret = (u_register_t) 5826b7b0f36SAntonio Nino Diaz psci_cpu_suspend((unsigned int)x1, x2, x3); 5836b7b0f36SAntonio Nino Diaz break; 584532ed618SSoby Mathew 585532ed618SSoby Mathew case PSCI_CPU_ON_AARCH64: 5866b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_on(x1, x2, x3); 5876b7b0f36SAntonio Nino Diaz break; 588532ed618SSoby Mathew 589532ed618SSoby Mathew case PSCI_AFFINITY_INFO_AARCH64: 5906b7b0f36SAntonio Nino Diaz ret = (u_register_t) 5916b7b0f36SAntonio Nino Diaz psci_affinity_info(x1, (unsigned int)x2); 5926b7b0f36SAntonio Nino Diaz break; 593532ed618SSoby Mathew 594532ed618SSoby Mathew case PSCI_MIG_AARCH64: 5956b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate(x1); 5966b7b0f36SAntonio Nino Diaz break; 597532ed618SSoby Mathew 598532ed618SSoby Mathew case PSCI_MIG_INFO_UP_CPU_AARCH64: 5996b7b0f36SAntonio Nino Diaz ret = psci_migrate_info_up_cpu(); 6006b7b0f36SAntonio Nino Diaz break; 601532ed618SSoby Mathew 60228d3d614SJeenu Viswambharan case PSCI_NODE_HW_STATE_AARCH64: 6036b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_node_hw_state( 6046b7b0f36SAntonio Nino Diaz x1, (unsigned int) x2); 6056b7b0f36SAntonio Nino Diaz break; 60628d3d614SJeenu Viswambharan 607532ed618SSoby Mathew case PSCI_SYSTEM_SUSPEND_AARCH64: 6086b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_system_suspend(x1, x2); 6096b7b0f36SAntonio Nino Diaz break; 610532ed618SSoby Mathew 611532ed618SSoby Mathew #if ENABLE_PSCI_STAT 612532ed618SSoby Mathew case PSCI_STAT_RESIDENCY_AARCH64: 6136b7b0f36SAntonio Nino Diaz ret = psci_stat_residency(x1, (unsigned int) x2); 6146b7b0f36SAntonio Nino Diaz break; 615532ed618SSoby Mathew 616532ed618SSoby Mathew case PSCI_STAT_COUNT_AARCH64: 6176b7b0f36SAntonio Nino Diaz ret = psci_stat_count(x1, (unsigned int) x2); 6186b7b0f36SAntonio Nino Diaz break; 619532ed618SSoby Mathew #endif 620532ed618SSoby Mathew 621d4c596beSRoberto Vargas case PSCI_MEM_CHK_RANGE_AARCH64: 6226b7b0f36SAntonio Nino Diaz ret = psci_mem_chk_range(x1, x2); 6236b7b0f36SAntonio Nino Diaz break; 624d4c596beSRoberto Vargas 62536a8f8fdSRoberto Vargas case PSCI_SYSTEM_RESET2_AARCH64: 62636a8f8fdSRoberto Vargas /* We should never return from psci_system_reset2() */ 6276b7b0f36SAntonio Nino Diaz ret = psci_system_reset2((uint32_t) x1, x2); 6286b7b0f36SAntonio Nino Diaz break; 629d4c596beSRoberto Vargas 630532ed618SSoby Mathew default: 6316b7b0f36SAntonio Nino Diaz WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 6326b7b0f36SAntonio Nino Diaz ret = (u_register_t)SMC_UNK; 633532ed618SSoby Mathew break; 634532ed618SSoby Mathew } 635532ed618SSoby Mathew } 636532ed618SSoby Mathew 6376b7b0f36SAntonio Nino Diaz return ret; 638532ed618SSoby Mathew } 639