blob: 308c6f927c7799fab86facb983227af77fff49a2 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Boyan Karatotev382ba742025-04-07 15:46:39 +01002 * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00007#include <assert.h>
8#include <string.h>
9
Dan Handley97043ac2014-04-09 13:14:54 +010010#include <arch.h>
Boyan Karatotev45c73282024-09-20 13:37:51 +010011#include <arch_features.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010012#include <arch_helpers.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000013#include <common/debug.h>
14#include <lib/pmf/pmf.h>
15#include <lib/runtime_instr.h>
16#include <lib/smccc.h>
17#include <plat/common/platform.h>
18#include <services/arm_arch_svc.h>
19
Dan Handley35e98e52014-04-09 13:13:04 +010020#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010021
22/*******************************************************************************
23 * PSCI frontend api for servicing SMCs. Described in the PSCI spec.
24 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +010025int psci_cpu_on(u_register_t target_cpu,
26 uintptr_t entrypoint,
27 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010028
29{
30 int rc;
Manish Pandeyef738d12024-06-22 00:00:18 +010031 entry_point_info_t *ep;
32 unsigned int target_idx = (unsigned int)plat_core_pos_by_mpidr(target_cpu);
Achin Gupta4f6ad662013-10-25 09:08:21 +010033
Manish Pandeye60c1842023-10-27 11:45:44 +010034 /* Validate the target CPU */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053035 if (!is_valid_mpidr(target_cpu)) {
Soby Mathew539dced2014-10-02 16:56:51 +010036 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053037 }
Soby Mathew539dced2014-10-02 16:56:51 +010038
Manish Pandeyef738d12024-06-22 00:00:18 +010039 ep = get_cpu_data_by_index(target_idx, warmboot_ep_info);
40 /* Validate the lower EL entry point and put it in the entry_point_info */
41 rc = psci_validate_entry_point(ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053042 if (rc != PSCI_E_SUCCESS) {
Soby Mathew78879b92015-01-06 15:36:38 +000043 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053044 }
Soby Mathew78879b92015-01-06 15:36:38 +000045
Soby Mathew78879b92015-01-06 15:36:38 +000046 /*
Soby Mathew67487842015-07-13 14:10:57 +010047 * To turn this cpu on, specify which power
Achin Gupta0959db52013-12-02 17:33:04 +000048 * levels need to be turned on
49 */
Manish Pandeyef738d12024-06-22 00:00:18 +010050 return psci_cpu_on_start(target_cpu, ep);
Achin Gupta4f6ad662013-10-25 09:08:21 +010051}
52
53unsigned int psci_version(void)
54{
55 return PSCI_MAJOR_VER | PSCI_MINOR_VER;
56}
57
58int psci_cpu_suspend(unsigned int power_state,
Soby Mathew9d070b92015-07-29 17:05:03 +010059 uintptr_t entrypoint,
60 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010061{
62 int rc;
Soby Mathew67487842015-07-13 14:10:57 +010063 unsigned int target_pwrlvl, is_power_down_state;
Soby Mathew67487842015-07-13 14:10:57 +010064 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
65 plat_local_state_t cpu_pd_state;
Wing Li606b7432022-09-14 13:18:17 -070066 unsigned int cpu_idx = plat_my_core_pos();
Achin Gupta4f6ad662013-10-25 09:08:21 +010067
Boyan Karatotev45c73282024-09-20 13:37:51 +010068#if ERRATA_SME_POWER_DOWN
69 /*
70 * If SME isn't off, attempting a real power down will only end up being
71 * rejected. If we got called with SME on, fall back to a normal
72 * suspend. We can't force SME off as in the event the power down is
73 * rejected for another reason (eg GIC) we'd lose the SME context.
74 */
75 if (is_feat_sme_supported() && read_svcr() != 0) {
76 power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
77 power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
78 }
79#endif /* ERRATA_SME_POWER_DOWN */
80
Soby Mathew67487842015-07-13 14:10:57 +010081 /* Validate the power_state parameter */
82 rc = psci_validate_power_state(power_state, &state_info);
83 if (rc != PSCI_E_SUCCESS) {
84 assert(rc == PSCI_E_INVALID_PARAMS);
85 return rc;
Soby Mathew539dced2014-10-02 16:56:51 +010086 }
87
Achin Gupta317ba092014-05-09 19:32:25 +010088 /*
Soby Mathew67487842015-07-13 14:10:57 +010089 * Get the value of the state type bit from the power state parameter.
Achin Gupta317ba092014-05-09 19:32:25 +010090 */
Soby Mathew67487842015-07-13 14:10:57 +010091 is_power_down_state = psci_get_pstate_type(power_state);
92
93 /* Sanity check the requested suspend levels */
Soby Mathewda554d72016-05-03 17:11:42 +010094 assert(psci_validate_suspend_req(&state_info, is_power_down_state)
Soby Mathew67487842015-07-13 14:10:57 +010095 == PSCI_E_SUCCESS);
96
97 target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
Sandrine Bailleuxa1c3faa2016-06-22 16:35:01 +010098 if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
99 ERROR("Invalid target power level for suspend operation\n");
100 panic();
101 }
Soby Mathew67487842015-07-13 14:10:57 +0100102
Boyan Karatotevb34be5d2025-04-10 10:24:47 +0100103 /* Fast path for local CPU standby, won't interact with higher power levels. */
Antonio Nino Diaz362030b2018-08-01 16:42:10 +0100104 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530105 if (psci_plat_pm_ops->cpu_standby == NULL) {
Vikram Kanigirid118f9f2014-03-21 11:57:10 +0000106 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530107 }
Achin Gupta317ba092014-05-09 19:32:25 +0100108
Soby Mathew67487842015-07-13 14:10:57 +0100109 /*
110 * Set the state of the CPU power domain to the platform
111 * specific retention state and enter the standby state.
112 */
113 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
114 psci_set_cpu_local_state(cpu_pd_state);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100115
116#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000117 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100118#endif
119
dp-arm872be882016-09-19 11:18:44 +0100120#if ENABLE_RUNTIME_INSTRUMENTATION
121 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
122 RT_INSTR_ENTER_HW_LOW_PWR,
123 PMF_NO_CACHE_MAINT);
124#endif
125
Soby Mathew67487842015-07-13 14:10:57 +0100126 psci_plat_pm_ops->cpu_standby(cpu_pd_state);
127
128 /* Upon exit from standby, set the state back to RUN. */
129 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
130
dp-arm872be882016-09-19 11:18:44 +0100131#if ENABLE_RUNTIME_INSTRUMENTATION
132 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
133 RT_INSTR_EXIT_HW_LOW_PWR,
134 PMF_NO_CACHE_MAINT);
135#endif
136
Yatharth Kochar170fb932016-05-09 18:26:35 +0100137#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000138 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100139
140 /* Update PSCI stats */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000141 psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100142#endif
143
Soby Mathew539dced2014-10-02 16:56:51 +0100144 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100145 }
146
Achin Gupta317ba092014-05-09 19:32:25 +0100147 /*
Soby Mathew67487842015-07-13 14:10:57 +0100148 * If a power down state has been requested, we need to verify entry
149 * point and program entry information.
Soby Mathew78879b92015-01-06 15:36:38 +0000150 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100151 if (is_power_down_state != 0U) {
Manish Pandeyef738d12024-06-22 00:00:18 +0100152 entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
153
154 rc = psci_validate_entry_point(ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530155 if (rc != PSCI_E_SUCCESS) {
Soby Mathew67487842015-07-13 14:10:57 +0100156 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530157 }
Soby Mathew67487842015-07-13 14:10:57 +0100158 }
Soby Mathew31244d72014-09-30 11:19:51 +0100159
Soby Mathew78879b92015-01-06 15:36:38 +0000160 /*
Achin Gupta317ba092014-05-09 19:32:25 +0100161 * Do what is needed to enter the power down state. Upon success,
Soby Mathew67487842015-07-13 14:10:57 +0100162 * enter the final wfi which will power down this CPU. This function
163 * might return if the power down was abandoned for any reason, e.g.
164 * arrival of an interrupt
Achin Gupta317ba092014-05-09 19:32:25 +0100165 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000166 rc = psci_cpu_suspend_start(cpu_idx,
Wing Li606b7432022-09-14 13:18:17 -0700167 target_pwrlvl,
168 &state_info,
169 is_power_down_state);
Soby Mathew539dced2014-10-02 16:56:51 +0100170
Wing Li606b7432022-09-14 13:18:17 -0700171 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100172}
173
Soby Mathew9d070b92015-07-29 17:05:03 +0100174
175int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000176{
177 int rc;
Soby Mathew67487842015-07-13 14:10:57 +0100178 psci_power_state_t state_info;
Boyan Karatotev3b802102024-11-06 16:26:15 +0000179 unsigned int cpu_idx = plat_my_core_pos();
Manish Pandeyef738d12024-06-22 00:00:18 +0100180 entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000181
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000182 /* Check if the current CPU is the last ON CPU in the system */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530183 if (!psci_is_last_on_cpu(cpu_idx)) {
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000184 return PSCI_E_DENIED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530185 }
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000186
Soby Mathew617540d2015-07-15 12:13:26 +0100187 /* Validate the entry point and get the entry_point_info */
Manish Pandeyef738d12024-06-22 00:00:18 +0100188 rc = psci_validate_entry_point(ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530189 if (rc != PSCI_E_SUCCESS) {
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000190 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530191 }
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000192
Soby Mathew67487842015-07-13 14:10:57 +0100193 /* Query the psci_power_state for system suspend */
194 psci_query_sys_suspend_pwrstate(&state_info);
195
ldtsa4065ab2018-10-11 08:40:32 +0200196 /*
197 * Check if platform allows suspend to Highest power level
198 * (System level)
199 */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530200 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) {
ldtsa4065ab2018-10-11 08:40:32 +0200201 return PSCI_E_DENIED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530202 }
Soby Mathew67487842015-07-13 14:10:57 +0100203 /* Ensure that the psci_power_state makes sense */
Soby Mathew67487842015-07-13 14:10:57 +0100204 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
205 == PSCI_E_SUCCESS);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100206 assert(is_local_state_off(
207 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000208
209 /*
Soby Mathew67487842015-07-13 14:10:57 +0100210 * Do what is needed to enter the system suspend state. This function
211 * might return if the power down was abandoned for any reason, e.g.
212 * arrival of an interrupt
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000213 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000214 rc = psci_cpu_suspend_start(cpu_idx,
Wing Li606b7432022-09-14 13:18:17 -0700215 PLAT_MAX_PWR_LVL,
216 &state_info,
217 PSTATE_TYPE_POWERDOWN);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000218
Wing Li606b7432022-09-14 13:18:17 -0700219 return rc;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000220}
221
Achin Gupta4f6ad662013-10-25 09:08:21 +0100222int psci_cpu_off(void)
223{
224 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100225 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100226
Achin Gupta4f6ad662013-10-25 09:08:21 +0100227 /*
Soby Mathew67487842015-07-13 14:10:57 +0100228 * Do what is needed to power off this CPU and possible higher power
229 * levels if it able to do so. Upon success, enter the final wfi
230 * which will power down this CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100231 */
Soby Mathew67487842015-07-13 14:10:57 +0100232 rc = psci_do_cpu_off(target_pwrlvl);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100233
Achin Gupta3140a9e2013-12-02 16:23:12 +0000234 /*
235 * The only error cpu_off can return is E_DENIED. So check if that's
236 * indeed the case.
237 */
Soby Mathewda554d72016-05-03 17:11:42 +0100238 assert(rc == PSCI_E_DENIED);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100239
240 return rc;
241}
242
Soby Mathew9d070b92015-07-29 17:05:03 +0100243int psci_affinity_info(u_register_t target_affinity,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100244 unsigned int lowest_affinity_level)
245{
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600246 unsigned int target_idx;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100247
Manish Pandeye60c1842023-10-27 11:45:44 +0100248 /* Validate the target affinity */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530249 if (!is_valid_mpidr(target_affinity)) {
Manish Pandeye60c1842023-10-27 11:45:44 +0100250 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530251 }
Manish Pandeye60c1842023-10-27 11:45:44 +0100252
Soby Mathew67487842015-07-13 14:10:57 +0100253 /* We dont support level higher than PSCI_CPU_PWR_LVL */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530254 if (lowest_affinity_level > PSCI_CPU_PWR_LVL) {
Soby Mathew67487842015-07-13 14:10:57 +0100255 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530256 }
Soby Mathew67487842015-07-13 14:10:57 +0100257 /* Calculate the cpu index of the target */
Manish Pandeye60c1842023-10-27 11:45:44 +0100258 target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
Achin Gupta75f73672013-12-05 16:33:10 +0000259
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000260 /*
261 * Generic management:
262 * Perform cache maintanence ahead of reading the target CPU state to
263 * ensure that the data is not stale.
264 * There is a theoretical edge case where the cache may contain stale
265 * data for the target CPU data - this can occur under the following
266 * conditions:
267 * - the target CPU is in another cluster from the current
268 * - the target CPU was the last CPU to shutdown on its cluster
269 * - the cluster was removed from coherency as part of the CPU shutdown
270 *
271 * In this case the cache maintenace that was performed as part of the
272 * target CPUs shutdown was not seen by the current CPU's cluster. And
273 * so the cache may contain stale data for the target CPU.
274 */
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600275 flush_cpu_data_by_index(target_idx,
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100276 psci_svc_cpu_data.aff_info_state);
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000277
Maheedhar Bollapallif6166f72024-04-24 19:04:11 +0530278 return (int)psci_get_aff_info_state_by_idx(target_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100279}
280
Soby Mathew9d070b92015-07-29 17:05:03 +0100281int psci_migrate(u_register_t target_cpu)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100282{
Soby Mathew8991eed2014-10-23 10:35:34 +0100283 int rc;
Boyan Karatotev382ba742025-04-07 15:46:39 +0100284 u_register_t resident_cpu_mpidr = 0;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100285
Manish Pandeye60c1842023-10-27 11:45:44 +0100286 /* Validate the target cpu */
287 if (!is_valid_mpidr(target_cpu))
288 return PSCI_E_INVALID_PARAMS;
289
Soby Mathew8991eed2014-10-23 10:35:34 +0100290 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530291 if (rc != PSCI_TOS_UP_MIG_CAP) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100292 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
293 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530294 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100295
Achin Gupta4f6ad662013-10-25 09:08:21 +0100296 /*
Soby Mathew8991eed2014-10-23 10:35:34 +0100297 * Migrate should only be invoked on the CPU where
298 * the Secure OS is resident.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100299 */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530300 if (resident_cpu_mpidr != read_mpidr_el1()) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100301 return PSCI_E_NOT_PRESENT;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530302 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100303
304 /* Check the validity of the specified target cpu */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530305 if (!is_valid_mpidr(target_cpu)) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100306 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530307 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100308
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100309 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100310
311 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100312 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100313
314 return rc;
315}
316
317int psci_migrate_info_type(void)
318{
Soby Mathew9d070b92015-07-29 17:05:03 +0100319 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100320
321 return psci_spd_migrate_info(&resident_cpu_mpidr);
322}
323
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100324u_register_t psci_migrate_info_up_cpu(void)
Soby Mathew8991eed2014-10-23 10:35:34 +0100325{
Boyan Karatotev382ba742025-04-07 15:46:39 +0100326 u_register_t resident_cpu_mpidr = 0;
Soby Mathew8991eed2014-10-23 10:35:34 +0100327 int rc;
328
329 /*
330 * Return value of this depends upon what
331 * psci_spd_migrate_info() returns.
332 */
333 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Prasad Kummarif3d9e222025-04-22 20:50:01 +0530334 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100335 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
Prasad Kummarif3d9e222025-04-22 20:50:01 +0530336 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100337
338 return resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100339}
340
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100341int psci_node_hw_state(u_register_t target_cpu,
342 unsigned int power_level)
343{
344 int rc;
345
346 /* Validate target_cpu */
Prasad Kummarif3d9e222025-04-22 20:50:01 +0530347 if (!is_valid_mpidr(target_cpu)) {
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100348 return PSCI_E_INVALID_PARAMS;
Prasad Kummarif3d9e222025-04-22 20:50:01 +0530349 }
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100350
351 /* Validate power_level against PLAT_MAX_PWR_LVL */
Prasad Kummarif3d9e222025-04-22 20:50:01 +0530352 if (power_level > PLAT_MAX_PWR_LVL) {
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100353 return PSCI_E_INVALID_PARAMS;
Prasad Kummarif3d9e222025-04-22 20:50:01 +0530354 }
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100355
356 /*
357 * Dispatch this call to platform to query power controller, and pass on
358 * to the caller what it returns
359 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100360 assert(psci_plat_pm_ops->get_node_hw_state != NULL);
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100361 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100362 assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
363 || (rc == PSCI_E_NOT_SUPPORTED)
364 || (rc == PSCI_E_INVALID_PARAMS));
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100365 return rc;
366}
367
Soby Mathew90e82582015-01-07 11:10:22 +0000368int psci_features(unsigned int psci_fid)
369{
Soby Mathew9d070b92015-07-29 17:05:03 +0100370 unsigned int local_caps = psci_caps;
Soby Mathew90e82582015-01-07 11:10:22 +0000371
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530372 if (psci_fid == SMCCC_VERSION) {
Dimitris Papastamos6eabbb02018-01-22 12:58:52 +0000373 return PSCI_E_SUCCESS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530374 }
Soby Mathew90e82582015-01-07 11:10:22 +0000375 /* Check if it is a 64 bit function */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530376 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) {
Soby Mathew90e82582015-01-07 11:10:22 +0000377 local_caps &= PSCI_CAP_64BIT_MASK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530378 }
Soby Mathew90e82582015-01-07 11:10:22 +0000379 /* Check for invalid fid */
380 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530381 && is_psci_fid(psci_fid))) {
Soby Mathew90e82582015-01-07 11:10:22 +0000382 return PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530383 }
Soby Mathew90e82582015-01-07 11:10:22 +0000384
385 /* Check if the psci fid is supported or not */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530386 if ((local_caps & define_psci_cap(psci_fid)) == 0U) {
Soby Mathew90e82582015-01-07 11:10:22 +0000387 return PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530388 }
Soby Mathew90e82582015-01-07 11:10:22 +0000389 /* Format the feature flags */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100390 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
391 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100392 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
Wing Li9a70e692022-09-14 13:18:19 -0700393 (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
394 return (int)ret;
Soby Mathew90e82582015-01-07 11:10:22 +0000395 }
396
397 /* Return 0 for all other fid's */
398 return PSCI_E_SUCCESS;
399}
400
Wing Lib88a4412022-09-14 13:18:15 -0700401#if PSCI_OS_INIT_MODE
402int psci_set_suspend_mode(unsigned int mode)
403{
404 if (psci_suspend_mode == mode) {
405 return PSCI_E_SUCCESS;
406 }
407
Boyan Karatotev3b802102024-11-06 16:26:15 +0000408 unsigned int this_core = plat_my_core_pos();
409
Wing Lib88a4412022-09-14 13:18:15 -0700410 if (mode == PLAT_COORD) {
411 /* Check if the current CPU is the last ON CPU in the system */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000412 if (!psci_is_last_on_cpu_safe(this_core)) {
Wing Lib88a4412022-09-14 13:18:15 -0700413 return PSCI_E_DENIED;
414 }
415 }
416
417 if (mode == OS_INIT) {
418 /*
419 * Check if all CPUs in the system are ON or if the current
420 * CPU is the last ON CPU in the system.
421 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000422 if (!(psci_are_all_cpus_on_safe(this_core) ||
423 psci_is_last_on_cpu_safe(this_core))) {
Wing Lib88a4412022-09-14 13:18:15 -0700424 return PSCI_E_DENIED;
425 }
426 }
427
428 psci_suspend_mode = mode;
429 psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
430 sizeof(psci_suspend_mode));
431
432 return PSCI_E_SUCCESS;
433}
434#endif
435
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000436/*******************************************************************************
437 * PSCI top level handler for servicing SMCs.
438 ******************************************************************************/
Soby Mathewcf0b1492016-04-29 19:01:30 +0100439u_register_t psci_smc_handler(uint32_t smc_fid,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100440 u_register_t x1,
441 u_register_t x2,
442 u_register_t x3,
443 u_register_t x4,
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000444 void *cookie,
445 void *handle,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100446 u_register_t flags)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000447{
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100448 u_register_t ret;
449
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530450 if (is_caller_secure(flags)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100451 return (u_register_t)SMC_UNK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530452 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000453
Soby Mathewb234b2c2015-01-15 11:49:49 +0000454 /* Check the fid against the capabilities */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530455 if ((psci_caps & define_psci_cap(smc_fid)) == 0U) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100456 return (u_register_t)SMC_UNK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530457 }
Soby Mathewb234b2c2015-01-15 11:49:49 +0000458
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100459 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
460 /* 32-bit PSCI function, clear top parameter bits */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000461
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100462 uint32_t r1 = (uint32_t)x1;
463 uint32_t r2 = (uint32_t)x2;
464 uint32_t r3 = (uint32_t)x3;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000465
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100466 switch (smc_fid) {
467 case PSCI_VERSION:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100468 ret = (u_register_t)psci_version();
469 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000470
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100471 case PSCI_CPU_OFF:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100472 ret = (u_register_t)psci_cpu_off();
473 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000474
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100475 case PSCI_CPU_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100476 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
477 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000478
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100479 case PSCI_CPU_ON_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100480 ret = (u_register_t)psci_cpu_on(r1, r2, r3);
481 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000482
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100483 case PSCI_AFFINITY_INFO_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100484 ret = (u_register_t)psci_affinity_info(r1, r2);
485 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000486
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100487 case PSCI_MIG_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100488 ret = (u_register_t)psci_migrate(r1);
489 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000490
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100491 case PSCI_MIG_INFO_TYPE:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100492 ret = (u_register_t)psci_migrate_info_type();
493 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100494
495 case PSCI_MIG_INFO_UP_CPU_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100496 ret = psci_migrate_info_up_cpu();
497 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100498
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100499 case PSCI_NODE_HW_STATE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100500 ret = (u_register_t)psci_node_hw_state(r1, r2);
501 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100502
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000503 case PSCI_SYSTEM_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100504 ret = (u_register_t)psci_system_suspend(r1, r2);
505 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000506
Juan Castillod5f13092014-08-12 11:17:06 +0100507 case PSCI_SYSTEM_OFF:
508 psci_system_off();
509 /* We should never return from psci_system_off() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000510 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100511
512 case PSCI_SYSTEM_RESET:
513 psci_system_reset();
514 /* We should never return from psci_system_reset() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000515 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100516
Soby Mathew90e82582015-01-07 11:10:22 +0000517 case PSCI_FEATURES:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100518 ret = (u_register_t)psci_features(r1);
519 break;
Soby Mathew90e82582015-01-07 11:10:22 +0000520
Wing Lib88a4412022-09-14 13:18:15 -0700521#if PSCI_OS_INIT_MODE
522 case PSCI_SET_SUSPEND_MODE:
523 ret = (u_register_t)psci_set_suspend_mode(r1);
524 break;
525#endif
526
Yatharth Kochar170fb932016-05-09 18:26:35 +0100527#if ENABLE_PSCI_STAT
528 case PSCI_STAT_RESIDENCY_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100529 ret = psci_stat_residency(r1, r2);
530 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100531
532 case PSCI_STAT_COUNT_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100533 ret = psci_stat_count(r1, r2);
534 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100535#endif
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100536 case PSCI_MEM_PROTECT:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100537 ret = psci_mem_protect(r1);
538 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100539
540 case PSCI_MEM_CHK_RANGE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100541 ret = psci_mem_chk_range(r1, r2);
542 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100543
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100544 case PSCI_SYSTEM_RESET2_AARCH32:
545 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100546 ret = psci_system_reset2(r1, r2);
547 break;
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100548
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100549 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100550 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
551 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100552 break;
553 }
554 } else {
555 /* 64-bit PSCI function */
556
557 switch (smc_fid) {
558 case PSCI_CPU_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100559 ret = (u_register_t)
560 psci_cpu_suspend((unsigned int)x1, x2, x3);
561 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100562
563 case PSCI_CPU_ON_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100564 ret = (u_register_t)psci_cpu_on(x1, x2, x3);
565 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100566
567 case PSCI_AFFINITY_INFO_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100568 ret = (u_register_t)
569 psci_affinity_info(x1, (unsigned int)x2);
570 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100571
572 case PSCI_MIG_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100573 ret = (u_register_t)psci_migrate(x1);
574 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100575
576 case PSCI_MIG_INFO_UP_CPU_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100577 ret = psci_migrate_info_up_cpu();
578 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100579
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100580 case PSCI_NODE_HW_STATE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100581 ret = (u_register_t)psci_node_hw_state(
582 x1, (unsigned int) x2);
583 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100584
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000585 case PSCI_SYSTEM_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100586 ret = (u_register_t)psci_system_suspend(x1, x2);
587 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000588
Yatharth Kochar170fb932016-05-09 18:26:35 +0100589#if ENABLE_PSCI_STAT
590 case PSCI_STAT_RESIDENCY_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100591 ret = psci_stat_residency(x1, (unsigned int) x2);
592 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100593
594 case PSCI_STAT_COUNT_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100595 ret = psci_stat_count(x1, (unsigned int) x2);
596 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100597#endif
598
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100599 case PSCI_MEM_CHK_RANGE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100600 ret = psci_mem_chk_range(x1, x2);
601 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100602
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100603 case PSCI_SYSTEM_RESET2_AARCH64:
604 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100605 ret = psci_system_reset2((uint32_t) x1, x2);
606 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100607
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100608 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100609 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
610 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100611 break;
612 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000613 }
614
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100615 return ret;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000616}