1532ed618SSoby Mathew /* 2b41b0824SJayanth Dodderi Chidanand * Copyright (c) 2013-2022, 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> 11532ed618SSoby Mathew #include <arch_helpers.h> 1209d40e0eSAntonio Nino Diaz #include <common/debug.h> 1309d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 1409d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 1509d40e0eSAntonio Nino Diaz #include <lib/smccc.h> 1609d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 1709d40e0eSAntonio Nino Diaz #include <services/arm_arch_svc.h> 1809d40e0eSAntonio Nino Diaz 19532ed618SSoby Mathew #include "psci_private.h" 20532ed618SSoby Mathew 21532ed618SSoby Mathew /******************************************************************************* 22532ed618SSoby Mathew * PSCI frontend api for servicing SMCs. Described in the PSCI spec. 23532ed618SSoby Mathew ******************************************************************************/ 24532ed618SSoby Mathew int psci_cpu_on(u_register_t target_cpu, 25532ed618SSoby Mathew uintptr_t entrypoint, 26532ed618SSoby Mathew u_register_t context_id) 27532ed618SSoby Mathew 28532ed618SSoby Mathew { 29532ed618SSoby Mathew int rc; 30532ed618SSoby Mathew entry_point_info_t ep; 31532ed618SSoby Mathew 32532ed618SSoby Mathew /* Determine if the cpu exists of not */ 33532ed618SSoby Mathew rc = psci_validate_mpidr(target_cpu); 34532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 35532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 36532ed618SSoby Mathew 37532ed618SSoby Mathew /* Validate the entry point and get the entry_point_info */ 38532ed618SSoby Mathew rc = psci_validate_entry_point(&ep, entrypoint, context_id); 39532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 40532ed618SSoby Mathew return rc; 41532ed618SSoby Mathew 42532ed618SSoby Mathew /* 43532ed618SSoby Mathew * To turn this cpu on, specify which power 44532ed618SSoby Mathew * levels need to be turned on 45532ed618SSoby Mathew */ 46532ed618SSoby Mathew return psci_cpu_on_start(target_cpu, &ep); 47532ed618SSoby Mathew } 48532ed618SSoby Mathew 49532ed618SSoby Mathew unsigned int psci_version(void) 50532ed618SSoby Mathew { 51532ed618SSoby Mathew return PSCI_MAJOR_VER | PSCI_MINOR_VER; 52532ed618SSoby Mathew } 53532ed618SSoby Mathew 54532ed618SSoby Mathew int psci_cpu_suspend(unsigned int power_state, 55532ed618SSoby Mathew uintptr_t entrypoint, 56532ed618SSoby Mathew u_register_t context_id) 57532ed618SSoby Mathew { 58532ed618SSoby Mathew int rc; 59532ed618SSoby Mathew unsigned int target_pwrlvl, is_power_down_state; 60532ed618SSoby Mathew entry_point_info_t ep; 61532ed618SSoby Mathew psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} }; 62532ed618SSoby Mathew plat_local_state_t cpu_pd_state; 63532ed618SSoby Mathew 64532ed618SSoby Mathew /* Validate the power_state parameter */ 65532ed618SSoby Mathew rc = psci_validate_power_state(power_state, &state_info); 66532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) { 67532ed618SSoby Mathew assert(rc == PSCI_E_INVALID_PARAMS); 68532ed618SSoby Mathew return rc; 69532ed618SSoby Mathew } 70532ed618SSoby Mathew 71532ed618SSoby Mathew /* 72532ed618SSoby Mathew * Get the value of the state type bit from the power state parameter. 73532ed618SSoby Mathew */ 74532ed618SSoby Mathew is_power_down_state = psci_get_pstate_type(power_state); 75532ed618SSoby Mathew 76532ed618SSoby Mathew /* Sanity check the requested suspend levels */ 77532ed618SSoby Mathew assert(psci_validate_suspend_req(&state_info, is_power_down_state) 78532ed618SSoby Mathew == PSCI_E_SUCCESS); 79532ed618SSoby Mathew 80532ed618SSoby Mathew target_pwrlvl = psci_find_target_suspend_lvl(&state_info); 81a1c3faa6SSandrine Bailleux if (target_pwrlvl == PSCI_INVALID_PWR_LVL) { 82a1c3faa6SSandrine Bailleux ERROR("Invalid target power level for suspend operation\n"); 83a1c3faa6SSandrine Bailleux panic(); 84a1c3faa6SSandrine Bailleux } 85532ed618SSoby Mathew 86532ed618SSoby Mathew /* Fast path for CPU standby.*/ 87362030bfSAntonio Nino Diaz if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) { 886b7b0f36SAntonio Nino Diaz if (psci_plat_pm_ops->cpu_standby == NULL) 89532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 90532ed618SSoby Mathew 91532ed618SSoby Mathew /* 92532ed618SSoby Mathew * Set the state of the CPU power domain to the platform 93532ed618SSoby Mathew * specific retention state and enter the standby state. 94532ed618SSoby Mathew */ 95532ed618SSoby Mathew cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL]; 96532ed618SSoby Mathew psci_set_cpu_local_state(cpu_pd_state); 97532ed618SSoby Mathew 98532ed618SSoby Mathew #if ENABLE_PSCI_STAT 9904c1db1eSdp-arm plat_psci_stat_accounting_start(&state_info); 100532ed618SSoby Mathew #endif 101532ed618SSoby Mathew 102872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 103872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 104872be88aSdp-arm RT_INSTR_ENTER_HW_LOW_PWR, 105872be88aSdp-arm PMF_NO_CACHE_MAINT); 106872be88aSdp-arm #endif 107872be88aSdp-arm 108532ed618SSoby Mathew psci_plat_pm_ops->cpu_standby(cpu_pd_state); 109532ed618SSoby Mathew 110532ed618SSoby Mathew /* Upon exit from standby, set the state back to RUN. */ 111532ed618SSoby Mathew psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN); 112532ed618SSoby Mathew 113872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 114872be88aSdp-arm PMF_CAPTURE_TIMESTAMP(rt_instr_svc, 115872be88aSdp-arm RT_INSTR_EXIT_HW_LOW_PWR, 116872be88aSdp-arm PMF_NO_CACHE_MAINT); 117872be88aSdp-arm #endif 118872be88aSdp-arm 119532ed618SSoby Mathew #if ENABLE_PSCI_STAT 12004c1db1eSdp-arm plat_psci_stat_accounting_stop(&state_info); 121532ed618SSoby Mathew 122532ed618SSoby Mathew /* Update PSCI stats */ 12304c1db1eSdp-arm psci_stats_update_pwr_up(PSCI_CPU_PWR_LVL, &state_info); 124532ed618SSoby Mathew #endif 125532ed618SSoby Mathew 126532ed618SSoby Mathew return PSCI_E_SUCCESS; 127532ed618SSoby Mathew } 128532ed618SSoby Mathew 129532ed618SSoby Mathew /* 130532ed618SSoby Mathew * If a power down state has been requested, we need to verify entry 131532ed618SSoby Mathew * point and program entry information. 132532ed618SSoby Mathew */ 1336b7b0f36SAntonio Nino Diaz if (is_power_down_state != 0U) { 134532ed618SSoby Mathew rc = psci_validate_entry_point(&ep, entrypoint, context_id); 135532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 136532ed618SSoby Mathew return rc; 137532ed618SSoby Mathew } 138532ed618SSoby Mathew 139532ed618SSoby Mathew /* 140532ed618SSoby Mathew * Do what is needed to enter the power down state. Upon success, 141532ed618SSoby Mathew * enter the final wfi which will power down this CPU. This function 142532ed618SSoby Mathew * might return if the power down was abandoned for any reason, e.g. 143532ed618SSoby Mathew * arrival of an interrupt 144532ed618SSoby Mathew */ 145532ed618SSoby Mathew psci_cpu_suspend_start(&ep, 146532ed618SSoby Mathew target_pwrlvl, 147532ed618SSoby Mathew &state_info, 148532ed618SSoby Mathew is_power_down_state); 149532ed618SSoby Mathew 150532ed618SSoby Mathew return PSCI_E_SUCCESS; 151532ed618SSoby Mathew } 152532ed618SSoby Mathew 153532ed618SSoby Mathew 154532ed618SSoby Mathew int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id) 155532ed618SSoby Mathew { 156532ed618SSoby Mathew int rc; 157532ed618SSoby Mathew psci_power_state_t state_info; 158532ed618SSoby Mathew entry_point_info_t ep; 159532ed618SSoby Mathew 160532ed618SSoby Mathew /* Check if the current CPU is the last ON CPU in the system */ 161b41b0824SJayanth Dodderi Chidanand if (!psci_is_last_on_cpu()) 162532ed618SSoby Mathew return PSCI_E_DENIED; 163532ed618SSoby Mathew 164532ed618SSoby Mathew /* Validate the entry point and get the entry_point_info */ 165532ed618SSoby Mathew rc = psci_validate_entry_point(&ep, entrypoint, context_id); 166532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 167532ed618SSoby Mathew return rc; 168532ed618SSoby Mathew 169532ed618SSoby Mathew /* Query the psci_power_state for system suspend */ 170532ed618SSoby Mathew psci_query_sys_suspend_pwrstate(&state_info); 171532ed618SSoby Mathew 172a4065abdSldts /* 173a4065abdSldts * Check if platform allows suspend to Highest power level 174a4065abdSldts * (System level) 175a4065abdSldts */ 176a4065abdSldts if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) 177a4065abdSldts return PSCI_E_DENIED; 178a4065abdSldts 179532ed618SSoby Mathew /* Ensure that the psci_power_state makes sense */ 180532ed618SSoby Mathew assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN) 181532ed618SSoby Mathew == PSCI_E_SUCCESS); 1826b7b0f36SAntonio Nino Diaz assert(is_local_state_off( 1836b7b0f36SAntonio Nino Diaz state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0); 184532ed618SSoby Mathew 185532ed618SSoby Mathew /* 186532ed618SSoby Mathew * Do what is needed to enter the system suspend state. This function 187532ed618SSoby Mathew * might return if the power down was abandoned for any reason, e.g. 188532ed618SSoby Mathew * arrival of an interrupt 189532ed618SSoby Mathew */ 190532ed618SSoby Mathew psci_cpu_suspend_start(&ep, 191532ed618SSoby Mathew PLAT_MAX_PWR_LVL, 192532ed618SSoby Mathew &state_info, 193532ed618SSoby Mathew PSTATE_TYPE_POWERDOWN); 194532ed618SSoby Mathew 195532ed618SSoby Mathew return PSCI_E_SUCCESS; 196532ed618SSoby Mathew } 197532ed618SSoby Mathew 198532ed618SSoby Mathew int psci_cpu_off(void) 199532ed618SSoby Mathew { 200532ed618SSoby Mathew int rc; 201532ed618SSoby Mathew unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL; 202532ed618SSoby Mathew 203532ed618SSoby Mathew /* 204532ed618SSoby Mathew * Do what is needed to power off this CPU and possible higher power 205532ed618SSoby Mathew * levels if it able to do so. Upon success, enter the final wfi 206532ed618SSoby Mathew * which will power down this CPU. 207532ed618SSoby Mathew */ 208532ed618SSoby Mathew rc = psci_do_cpu_off(target_pwrlvl); 209532ed618SSoby Mathew 210532ed618SSoby Mathew /* 211532ed618SSoby Mathew * The only error cpu_off can return is E_DENIED. So check if that's 212532ed618SSoby Mathew * indeed the case. 213532ed618SSoby Mathew */ 214532ed618SSoby Mathew assert(rc == PSCI_E_DENIED); 215532ed618SSoby Mathew 216532ed618SSoby Mathew return rc; 217532ed618SSoby Mathew } 218532ed618SSoby Mathew 219532ed618SSoby Mathew int psci_affinity_info(u_register_t target_affinity, 220532ed618SSoby Mathew unsigned int lowest_affinity_level) 221532ed618SSoby Mathew { 2225b33ad17SDeepika Bhavnani int ret; 2235b33ad17SDeepika Bhavnani unsigned int target_idx; 224532ed618SSoby Mathew 225532ed618SSoby Mathew /* We dont support level higher than PSCI_CPU_PWR_LVL */ 226532ed618SSoby Mathew if (lowest_affinity_level > PSCI_CPU_PWR_LVL) 227532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 228532ed618SSoby Mathew 229532ed618SSoby Mathew /* Calculate the cpu index of the target */ 2305b33ad17SDeepika Bhavnani ret = plat_core_pos_by_mpidr(target_affinity); 2315b33ad17SDeepika Bhavnani if (ret == -1) { 232532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 2335b33ad17SDeepika Bhavnani } 2345b33ad17SDeepika Bhavnani target_idx = (unsigned int)ret; 235532ed618SSoby Mathew 2368fd307ffSRoberto Vargas /* 2378fd307ffSRoberto Vargas * Generic management: 2388fd307ffSRoberto Vargas * Perform cache maintanence ahead of reading the target CPU state to 2398fd307ffSRoberto Vargas * ensure that the data is not stale. 2408fd307ffSRoberto Vargas * There is a theoretical edge case where the cache may contain stale 2418fd307ffSRoberto Vargas * data for the target CPU data - this can occur under the following 2428fd307ffSRoberto Vargas * conditions: 2438fd307ffSRoberto Vargas * - the target CPU is in another cluster from the current 2448fd307ffSRoberto Vargas * - the target CPU was the last CPU to shutdown on its cluster 2458fd307ffSRoberto Vargas * - the cluster was removed from coherency as part of the CPU shutdown 2468fd307ffSRoberto Vargas * 2478fd307ffSRoberto Vargas * In this case the cache maintenace that was performed as part of the 2488fd307ffSRoberto Vargas * target CPUs shutdown was not seen by the current CPU's cluster. And 2498fd307ffSRoberto Vargas * so the cache may contain stale data for the target CPU. 2508fd307ffSRoberto Vargas */ 2515b33ad17SDeepika Bhavnani flush_cpu_data_by_index(target_idx, 2526b7b0f36SAntonio Nino Diaz psci_svc_cpu_data.aff_info_state); 2538fd307ffSRoberto Vargas 254532ed618SSoby Mathew return psci_get_aff_info_state_by_idx(target_idx); 255532ed618SSoby Mathew } 256532ed618SSoby Mathew 257532ed618SSoby Mathew int psci_migrate(u_register_t target_cpu) 258532ed618SSoby Mathew { 259532ed618SSoby Mathew int rc; 260532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 261532ed618SSoby Mathew 262532ed618SSoby Mathew rc = psci_spd_migrate_info(&resident_cpu_mpidr); 263532ed618SSoby Mathew if (rc != PSCI_TOS_UP_MIG_CAP) 264532ed618SSoby Mathew return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ? 265532ed618SSoby Mathew PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED; 266532ed618SSoby Mathew 267532ed618SSoby Mathew /* 268532ed618SSoby Mathew * Migrate should only be invoked on the CPU where 269532ed618SSoby Mathew * the Secure OS is resident. 270532ed618SSoby Mathew */ 271532ed618SSoby Mathew if (resident_cpu_mpidr != read_mpidr_el1()) 272532ed618SSoby Mathew return PSCI_E_NOT_PRESENT; 273532ed618SSoby Mathew 274532ed618SSoby Mathew /* Check the validity of the specified target cpu */ 275532ed618SSoby Mathew rc = psci_validate_mpidr(target_cpu); 276532ed618SSoby Mathew if (rc != PSCI_E_SUCCESS) 277532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS; 278532ed618SSoby Mathew 2796b7b0f36SAntonio Nino Diaz assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL)); 280532ed618SSoby Mathew 281532ed618SSoby Mathew rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu); 2826b7b0f36SAntonio Nino Diaz assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL)); 283532ed618SSoby Mathew 284532ed618SSoby Mathew return rc; 285532ed618SSoby Mathew } 286532ed618SSoby Mathew 287532ed618SSoby Mathew int psci_migrate_info_type(void) 288532ed618SSoby Mathew { 289532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 290532ed618SSoby Mathew 291532ed618SSoby Mathew return psci_spd_migrate_info(&resident_cpu_mpidr); 292532ed618SSoby Mathew } 293532ed618SSoby Mathew 2946b7b0f36SAntonio Nino Diaz u_register_t psci_migrate_info_up_cpu(void) 295532ed618SSoby Mathew { 296532ed618SSoby Mathew u_register_t resident_cpu_mpidr; 297532ed618SSoby Mathew int rc; 298532ed618SSoby Mathew 299532ed618SSoby Mathew /* 300532ed618SSoby Mathew * Return value of this depends upon what 301532ed618SSoby Mathew * psci_spd_migrate_info() returns. 302532ed618SSoby Mathew */ 303532ed618SSoby Mathew rc = psci_spd_migrate_info(&resident_cpu_mpidr); 3046b7b0f36SAntonio Nino Diaz if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) 3056b7b0f36SAntonio Nino Diaz return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS; 306532ed618SSoby Mathew 307532ed618SSoby Mathew return resident_cpu_mpidr; 308532ed618SSoby Mathew } 309532ed618SSoby Mathew 31028d3d614SJeenu Viswambharan int psci_node_hw_state(u_register_t target_cpu, 31128d3d614SJeenu Viswambharan unsigned int power_level) 31228d3d614SJeenu Viswambharan { 31328d3d614SJeenu Viswambharan int rc; 31428d3d614SJeenu Viswambharan 31528d3d614SJeenu Viswambharan /* Validate target_cpu */ 31628d3d614SJeenu Viswambharan rc = psci_validate_mpidr(target_cpu); 31728d3d614SJeenu Viswambharan if (rc != PSCI_E_SUCCESS) 31828d3d614SJeenu Viswambharan return PSCI_E_INVALID_PARAMS; 31928d3d614SJeenu Viswambharan 32028d3d614SJeenu Viswambharan /* Validate power_level against PLAT_MAX_PWR_LVL */ 32128d3d614SJeenu Viswambharan if (power_level > PLAT_MAX_PWR_LVL) 32228d3d614SJeenu Viswambharan return PSCI_E_INVALID_PARAMS; 32328d3d614SJeenu Viswambharan 32428d3d614SJeenu Viswambharan /* 32528d3d614SJeenu Viswambharan * Dispatch this call to platform to query power controller, and pass on 32628d3d614SJeenu Viswambharan * to the caller what it returns 32728d3d614SJeenu Viswambharan */ 3286b7b0f36SAntonio Nino Diaz assert(psci_plat_pm_ops->get_node_hw_state != NULL); 32928d3d614SJeenu Viswambharan rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level); 3306b7b0f36SAntonio Nino Diaz assert(((rc >= HW_ON) && (rc <= HW_STANDBY)) 3316b7b0f36SAntonio Nino Diaz || (rc == PSCI_E_NOT_SUPPORTED) 3326b7b0f36SAntonio Nino Diaz || (rc == PSCI_E_INVALID_PARAMS)); 33328d3d614SJeenu Viswambharan return rc; 33428d3d614SJeenu Viswambharan } 33528d3d614SJeenu Viswambharan 336532ed618SSoby Mathew int psci_features(unsigned int psci_fid) 337532ed618SSoby Mathew { 338532ed618SSoby Mathew unsigned int local_caps = psci_caps; 339532ed618SSoby Mathew 3406eabbb07SDimitris Papastamos if (psci_fid == SMCCC_VERSION) 3416eabbb07SDimitris Papastamos return PSCI_E_SUCCESS; 3426eabbb07SDimitris Papastamos 343532ed618SSoby Mathew /* Check if it is a 64 bit function */ 344532ed618SSoby Mathew if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) 345532ed618SSoby Mathew local_caps &= PSCI_CAP_64BIT_MASK; 346532ed618SSoby Mathew 347532ed618SSoby Mathew /* Check for invalid fid */ 348532ed618SSoby Mathew if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid) 349532ed618SSoby Mathew && is_psci_fid(psci_fid))) 350532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED; 351532ed618SSoby Mathew 352532ed618SSoby Mathew 353532ed618SSoby Mathew /* Check if the psci fid is supported or not */ 3546b7b0f36SAntonio Nino Diaz if ((local_caps & define_psci_cap(psci_fid)) == 0U) 355532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED; 356532ed618SSoby Mathew 357532ed618SSoby Mathew /* Format the feature flags */ 3586b7b0f36SAntonio Nino Diaz if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) || 3596b7b0f36SAntonio Nino Diaz (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) { 360532ed618SSoby Mathew /* 361532ed618SSoby Mathew * The trusted firmware does not support OS Initiated Mode. 362532ed618SSoby Mathew */ 3636b7b0f36SAntonio Nino Diaz unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) | 3646b7b0f36SAntonio Nino Diaz (((FF_SUPPORTS_OS_INIT_MODE == 1U) ? 0U : 1U) 3656b7b0f36SAntonio Nino Diaz << FF_MODE_SUPPORT_SHIFT)); 3666b7b0f36SAntonio Nino Diaz return (int) ret; 367532ed618SSoby Mathew } 368532ed618SSoby Mathew 369532ed618SSoby Mathew /* Return 0 for all other fid's */ 370532ed618SSoby Mathew return PSCI_E_SUCCESS; 371532ed618SSoby Mathew } 372532ed618SSoby Mathew 373*b88a4416SWing Li #if PSCI_OS_INIT_MODE 374*b88a4416SWing Li int psci_set_suspend_mode(unsigned int mode) 375*b88a4416SWing Li { 376*b88a4416SWing Li if (psci_suspend_mode == mode) { 377*b88a4416SWing Li return PSCI_E_SUCCESS; 378*b88a4416SWing Li } 379*b88a4416SWing Li 380*b88a4416SWing Li if (mode == PLAT_COORD) { 381*b88a4416SWing Li /* Check if the current CPU is the last ON CPU in the system */ 382*b88a4416SWing Li if (!psci_is_last_on_cpu_safe()) { 383*b88a4416SWing Li return PSCI_E_DENIED; 384*b88a4416SWing Li } 385*b88a4416SWing Li } 386*b88a4416SWing Li 387*b88a4416SWing Li if (mode == OS_INIT) { 388*b88a4416SWing Li /* 389*b88a4416SWing Li * Check if all CPUs in the system are ON or if the current 390*b88a4416SWing Li * CPU is the last ON CPU in the system. 391*b88a4416SWing Li */ 392*b88a4416SWing Li if (!(psci_are_all_cpus_on_safe() || 393*b88a4416SWing Li psci_is_last_on_cpu_safe())) { 394*b88a4416SWing Li return PSCI_E_DENIED; 395*b88a4416SWing Li } 396*b88a4416SWing Li } 397*b88a4416SWing Li 398*b88a4416SWing Li psci_suspend_mode = mode; 399*b88a4416SWing Li psci_flush_dcache_range((uintptr_t)&psci_suspend_mode, 400*b88a4416SWing Li sizeof(psci_suspend_mode)); 401*b88a4416SWing Li 402*b88a4416SWing Li return PSCI_E_SUCCESS; 403*b88a4416SWing Li } 404*b88a4416SWing Li #endif 405*b88a4416SWing Li 406532ed618SSoby Mathew /******************************************************************************* 407532ed618SSoby Mathew * PSCI top level handler for servicing SMCs. 408532ed618SSoby Mathew ******************************************************************************/ 409cf0b1492SSoby Mathew u_register_t psci_smc_handler(uint32_t smc_fid, 410532ed618SSoby Mathew u_register_t x1, 411532ed618SSoby Mathew u_register_t x2, 412532ed618SSoby Mathew u_register_t x3, 413532ed618SSoby Mathew u_register_t x4, 414532ed618SSoby Mathew void *cookie, 415532ed618SSoby Mathew void *handle, 416532ed618SSoby Mathew u_register_t flags) 417532ed618SSoby Mathew { 4186b7b0f36SAntonio Nino Diaz u_register_t ret; 4196b7b0f36SAntonio Nino Diaz 420532ed618SSoby Mathew if (is_caller_secure(flags)) 4216b7b0f36SAntonio Nino Diaz return (u_register_t)SMC_UNK; 422532ed618SSoby Mathew 423532ed618SSoby Mathew /* Check the fid against the capabilities */ 4246b7b0f36SAntonio Nino Diaz if ((psci_caps & define_psci_cap(smc_fid)) == 0U) 4256b7b0f36SAntonio Nino Diaz return (u_register_t)SMC_UNK; 426532ed618SSoby Mathew 427532ed618SSoby Mathew if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) { 428532ed618SSoby Mathew /* 32-bit PSCI function, clear top parameter bits */ 429532ed618SSoby Mathew 4306b7b0f36SAntonio Nino Diaz uint32_t r1 = (uint32_t)x1; 4316b7b0f36SAntonio Nino Diaz uint32_t r2 = (uint32_t)x2; 4326b7b0f36SAntonio Nino Diaz uint32_t r3 = (uint32_t)x3; 433532ed618SSoby Mathew 434532ed618SSoby Mathew switch (smc_fid) { 435532ed618SSoby Mathew case PSCI_VERSION: 4366b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_version(); 4376b7b0f36SAntonio Nino Diaz break; 438532ed618SSoby Mathew 439532ed618SSoby Mathew case PSCI_CPU_OFF: 4406b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_off(); 4416b7b0f36SAntonio Nino Diaz break; 442532ed618SSoby Mathew 443532ed618SSoby Mathew case PSCI_CPU_SUSPEND_AARCH32: 4446b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_suspend(r1, r2, r3); 4456b7b0f36SAntonio Nino Diaz break; 446532ed618SSoby Mathew 447532ed618SSoby Mathew case PSCI_CPU_ON_AARCH32: 4486b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_on(r1, r2, r3); 4496b7b0f36SAntonio Nino Diaz break; 450532ed618SSoby Mathew 451532ed618SSoby Mathew case PSCI_AFFINITY_INFO_AARCH32: 4526b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_affinity_info(r1, r2); 4536b7b0f36SAntonio Nino Diaz break; 454532ed618SSoby Mathew 455532ed618SSoby Mathew case PSCI_MIG_AARCH32: 4566b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate(r1); 4576b7b0f36SAntonio Nino Diaz break; 458532ed618SSoby Mathew 459532ed618SSoby Mathew case PSCI_MIG_INFO_TYPE: 4606b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate_info_type(); 4616b7b0f36SAntonio Nino Diaz break; 462532ed618SSoby Mathew 463532ed618SSoby Mathew case PSCI_MIG_INFO_UP_CPU_AARCH32: 4646b7b0f36SAntonio Nino Diaz ret = psci_migrate_info_up_cpu(); 4656b7b0f36SAntonio Nino Diaz break; 466532ed618SSoby Mathew 46728d3d614SJeenu Viswambharan case PSCI_NODE_HW_STATE_AARCH32: 4686b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_node_hw_state(r1, r2); 4696b7b0f36SAntonio Nino Diaz break; 47028d3d614SJeenu Viswambharan 471532ed618SSoby Mathew case PSCI_SYSTEM_SUSPEND_AARCH32: 4726b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_system_suspend(r1, r2); 4736b7b0f36SAntonio Nino Diaz break; 474532ed618SSoby Mathew 475532ed618SSoby Mathew case PSCI_SYSTEM_OFF: 476532ed618SSoby Mathew psci_system_off(); 477532ed618SSoby Mathew /* We should never return from psci_system_off() */ 4783eacacc0SJonathan Wright break; 479532ed618SSoby Mathew 480532ed618SSoby Mathew case PSCI_SYSTEM_RESET: 481532ed618SSoby Mathew psci_system_reset(); 482532ed618SSoby Mathew /* We should never return from psci_system_reset() */ 4833eacacc0SJonathan Wright break; 484532ed618SSoby Mathew 485532ed618SSoby Mathew case PSCI_FEATURES: 4866b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_features(r1); 4876b7b0f36SAntonio Nino Diaz break; 488532ed618SSoby Mathew 489*b88a4416SWing Li #if PSCI_OS_INIT_MODE 490*b88a4416SWing Li case PSCI_SET_SUSPEND_MODE: 491*b88a4416SWing Li ret = (u_register_t)psci_set_suspend_mode(r1); 492*b88a4416SWing Li break; 493*b88a4416SWing Li #endif 494*b88a4416SWing Li 495532ed618SSoby Mathew #if ENABLE_PSCI_STAT 496532ed618SSoby Mathew case PSCI_STAT_RESIDENCY_AARCH32: 4976b7b0f36SAntonio Nino Diaz ret = psci_stat_residency(r1, r2); 4986b7b0f36SAntonio Nino Diaz break; 499532ed618SSoby Mathew 500532ed618SSoby Mathew case PSCI_STAT_COUNT_AARCH32: 5016b7b0f36SAntonio Nino Diaz ret = psci_stat_count(r1, r2); 5026b7b0f36SAntonio Nino Diaz break; 503532ed618SSoby Mathew #endif 504d4c596beSRoberto Vargas case PSCI_MEM_PROTECT: 5056b7b0f36SAntonio Nino Diaz ret = psci_mem_protect(r1); 5066b7b0f36SAntonio Nino Diaz break; 507d4c596beSRoberto Vargas 508d4c596beSRoberto Vargas case PSCI_MEM_CHK_RANGE_AARCH32: 5096b7b0f36SAntonio Nino Diaz ret = psci_mem_chk_range(r1, r2); 5106b7b0f36SAntonio Nino Diaz break; 511532ed618SSoby Mathew 51236a8f8fdSRoberto Vargas case PSCI_SYSTEM_RESET2_AARCH32: 51336a8f8fdSRoberto Vargas /* We should never return from psci_system_reset2() */ 5146b7b0f36SAntonio Nino Diaz ret = psci_system_reset2(r1, r2); 5156b7b0f36SAntonio Nino Diaz break; 51636a8f8fdSRoberto Vargas 517532ed618SSoby Mathew default: 5186b7b0f36SAntonio Nino Diaz WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 5196b7b0f36SAntonio Nino Diaz ret = (u_register_t)SMC_UNK; 520532ed618SSoby Mathew break; 521532ed618SSoby Mathew } 522532ed618SSoby Mathew } else { 523532ed618SSoby Mathew /* 64-bit PSCI function */ 524532ed618SSoby Mathew 525532ed618SSoby Mathew switch (smc_fid) { 526532ed618SSoby Mathew case PSCI_CPU_SUSPEND_AARCH64: 5276b7b0f36SAntonio Nino Diaz ret = (u_register_t) 5286b7b0f36SAntonio Nino Diaz psci_cpu_suspend((unsigned int)x1, x2, x3); 5296b7b0f36SAntonio Nino Diaz break; 530532ed618SSoby Mathew 531532ed618SSoby Mathew case PSCI_CPU_ON_AARCH64: 5326b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_cpu_on(x1, x2, x3); 5336b7b0f36SAntonio Nino Diaz break; 534532ed618SSoby Mathew 535532ed618SSoby Mathew case PSCI_AFFINITY_INFO_AARCH64: 5366b7b0f36SAntonio Nino Diaz ret = (u_register_t) 5376b7b0f36SAntonio Nino Diaz psci_affinity_info(x1, (unsigned int)x2); 5386b7b0f36SAntonio Nino Diaz break; 539532ed618SSoby Mathew 540532ed618SSoby Mathew case PSCI_MIG_AARCH64: 5416b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_migrate(x1); 5426b7b0f36SAntonio Nino Diaz break; 543532ed618SSoby Mathew 544532ed618SSoby Mathew case PSCI_MIG_INFO_UP_CPU_AARCH64: 5456b7b0f36SAntonio Nino Diaz ret = psci_migrate_info_up_cpu(); 5466b7b0f36SAntonio Nino Diaz break; 547532ed618SSoby Mathew 54828d3d614SJeenu Viswambharan case PSCI_NODE_HW_STATE_AARCH64: 5496b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_node_hw_state( 5506b7b0f36SAntonio Nino Diaz x1, (unsigned int) x2); 5516b7b0f36SAntonio Nino Diaz break; 55228d3d614SJeenu Viswambharan 553532ed618SSoby Mathew case PSCI_SYSTEM_SUSPEND_AARCH64: 5546b7b0f36SAntonio Nino Diaz ret = (u_register_t)psci_system_suspend(x1, x2); 5556b7b0f36SAntonio Nino Diaz break; 556532ed618SSoby Mathew 557*b88a4416SWing Li #if PSCI_OS_INIT_MODE 558*b88a4416SWing Li case PSCI_SET_SUSPEND_MODE: 559*b88a4416SWing Li ret = (u_register_t)psci_set_suspend_mode(x1); 560*b88a4416SWing Li break; 561*b88a4416SWing Li #endif 562*b88a4416SWing Li 563532ed618SSoby Mathew #if ENABLE_PSCI_STAT 564532ed618SSoby Mathew case PSCI_STAT_RESIDENCY_AARCH64: 5656b7b0f36SAntonio Nino Diaz ret = psci_stat_residency(x1, (unsigned int) x2); 5666b7b0f36SAntonio Nino Diaz break; 567532ed618SSoby Mathew 568532ed618SSoby Mathew case PSCI_STAT_COUNT_AARCH64: 5696b7b0f36SAntonio Nino Diaz ret = psci_stat_count(x1, (unsigned int) x2); 5706b7b0f36SAntonio Nino Diaz break; 571532ed618SSoby Mathew #endif 572532ed618SSoby Mathew 573d4c596beSRoberto Vargas case PSCI_MEM_CHK_RANGE_AARCH64: 5746b7b0f36SAntonio Nino Diaz ret = psci_mem_chk_range(x1, x2); 5756b7b0f36SAntonio Nino Diaz break; 576d4c596beSRoberto Vargas 57736a8f8fdSRoberto Vargas case PSCI_SYSTEM_RESET2_AARCH64: 57836a8f8fdSRoberto Vargas /* We should never return from psci_system_reset2() */ 5796b7b0f36SAntonio Nino Diaz ret = psci_system_reset2((uint32_t) x1, x2); 5806b7b0f36SAntonio Nino Diaz break; 581d4c596beSRoberto Vargas 582532ed618SSoby Mathew default: 5836b7b0f36SAntonio Nino Diaz WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid); 5846b7b0f36SAntonio Nino Diaz ret = (u_register_t)SMC_UNK; 585532ed618SSoby Mathew break; 586532ed618SSoby Mathew } 587532ed618SSoby Mathew } 588532ed618SSoby Mathew 5896b7b0f36SAntonio Nino Diaz return ret; 590532ed618SSoby Mathew } 591