blob: 34668ea8d261ad01767d9e3584d4961dcab1b253 [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;
Soby Mathew78879b92015-01-06 15:36:38 +000031 entry_point_info_t ep;
Achin Gupta4f6ad662013-10-25 09:08:21 +010032
Manish Pandeye60c1842023-10-27 11:45:44 +010033 /* Validate the target CPU */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053034 if (!is_valid_mpidr(target_cpu)) {
Soby Mathew539dced2014-10-02 16:56:51 +010035 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053036 }
Soby Mathew539dced2014-10-02 16:56:51 +010037
Soby Mathew617540d2015-07-15 12:13:26 +010038 /* Validate the entry point and get the entry_point_info */
39 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053040 if (rc != PSCI_E_SUCCESS) {
Soby Mathew78879b92015-01-06 15:36:38 +000041 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +053042 }
Soby Mathew78879b92015-01-06 15:36:38 +000043
Soby Mathew78879b92015-01-06 15:36:38 +000044 /*
Soby Mathew67487842015-07-13 14:10:57 +010045 * To turn this cpu on, specify which power
Achin Gupta0959db52013-12-02 17:33:04 +000046 * levels need to be turned on
47 */
Sandrine Bailleux22b09c12016-04-25 09:28:43 +010048 return psci_cpu_on_start(target_cpu, &ep);
Achin Gupta4f6ad662013-10-25 09:08:21 +010049}
50
51unsigned int psci_version(void)
52{
53 return PSCI_MAJOR_VER | PSCI_MINOR_VER;
54}
55
56int psci_cpu_suspend(unsigned int power_state,
Soby Mathew9d070b92015-07-29 17:05:03 +010057 uintptr_t entrypoint,
58 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010059{
60 int rc;
Soby Mathew67487842015-07-13 14:10:57 +010061 unsigned int target_pwrlvl, is_power_down_state;
Soby Mathew78879b92015-01-06 15:36:38 +000062 entry_point_info_t ep;
Soby Mathew67487842015-07-13 14:10:57 +010063 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
64 plat_local_state_t cpu_pd_state;
Wing Li606b7432022-09-14 13:18:17 -070065 unsigned int cpu_idx = plat_my_core_pos();
Boyan Karatotev3b802102024-11-06 16:26:15 +000066#if PSCI_OS_INIT_MODE
Wing Li606b7432022-09-14 13:18:17 -070067 plat_local_state_t prev[PLAT_MAX_PWR_LVL];
68#endif
Achin Gupta4f6ad662013-10-25 09:08:21 +010069
Boyan Karatotev45c73282024-09-20 13:37:51 +010070#if ERRATA_SME_POWER_DOWN
71 /*
72 * If SME isn't off, attempting a real power down will only end up being
73 * rejected. If we got called with SME on, fall back to a normal
74 * suspend. We can't force SME off as in the event the power down is
75 * rejected for another reason (eg GIC) we'd lose the SME context.
76 */
77 if (is_feat_sme_supported() && read_svcr() != 0) {
78 power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
79 power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
80 }
81#endif /* ERRATA_SME_POWER_DOWN */
82
Soby Mathew67487842015-07-13 14:10:57 +010083 /* Validate the power_state parameter */
84 rc = psci_validate_power_state(power_state, &state_info);
85 if (rc != PSCI_E_SUCCESS) {
86 assert(rc == PSCI_E_INVALID_PARAMS);
87 return rc;
Soby Mathew539dced2014-10-02 16:56:51 +010088 }
89
Achin Gupta317ba092014-05-09 19:32:25 +010090 /*
Soby Mathew67487842015-07-13 14:10:57 +010091 * Get the value of the state type bit from the power state parameter.
Achin Gupta317ba092014-05-09 19:32:25 +010092 */
Soby Mathew67487842015-07-13 14:10:57 +010093 is_power_down_state = psci_get_pstate_type(power_state);
94
95 /* Sanity check the requested suspend levels */
Soby Mathewda554d72016-05-03 17:11:42 +010096 assert(psci_validate_suspend_req(&state_info, is_power_down_state)
Soby Mathew67487842015-07-13 14:10:57 +010097 == PSCI_E_SUCCESS);
98
99 target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
Sandrine Bailleuxa1c3faa2016-06-22 16:35:01 +0100100 if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
101 ERROR("Invalid target power level for suspend operation\n");
102 panic();
103 }
Soby Mathew67487842015-07-13 14:10:57 +0100104
105 /* Fast path for CPU standby.*/
Antonio Nino Diaz362030b2018-08-01 16:42:10 +0100106 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530107 if (psci_plat_pm_ops->cpu_standby == NULL) {
Vikram Kanigirid118f9f2014-03-21 11:57:10 +0000108 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530109 }
Achin Gupta317ba092014-05-09 19:32:25 +0100110
Soby Mathew67487842015-07-13 14:10:57 +0100111 /*
112 * Set the state of the CPU power domain to the platform
113 * specific retention state and enter the standby state.
114 */
115 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
116 psci_set_cpu_local_state(cpu_pd_state);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100117
Wing Li606b7432022-09-14 13:18:17 -0700118#if PSCI_OS_INIT_MODE
119 /*
120 * If in OS-initiated mode, save a copy of the previous
121 * requested local power states and update the new requested
122 * local power states for this CPU.
123 */
124 if (psci_suspend_mode == OS_INIT) {
125 psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
126 &state_info, prev);
127 }
128#endif
129
Yatharth Kochar170fb932016-05-09 18:26:35 +0100130#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000131 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100132#endif
133
dp-arm872be882016-09-19 11:18:44 +0100134#if ENABLE_RUNTIME_INSTRUMENTATION
135 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
136 RT_INSTR_ENTER_HW_LOW_PWR,
137 PMF_NO_CACHE_MAINT);
138#endif
139
Soby Mathew67487842015-07-13 14:10:57 +0100140 psci_plat_pm_ops->cpu_standby(cpu_pd_state);
141
142 /* Upon exit from standby, set the state back to RUN. */
143 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
144
Wing Li606b7432022-09-14 13:18:17 -0700145#if PSCI_OS_INIT_MODE
146 /*
147 * If in OS-initiated mode, restore the previous requested
148 * local power states for this CPU.
149 */
150 if (psci_suspend_mode == OS_INIT) {
151 psci_restore_req_local_pwr_states(cpu_idx, prev);
152 }
153#endif
154
dp-arm872be882016-09-19 11:18:44 +0100155#if ENABLE_RUNTIME_INSTRUMENTATION
156 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
157 RT_INSTR_EXIT_HW_LOW_PWR,
158 PMF_NO_CACHE_MAINT);
159#endif
160
Yatharth Kochar170fb932016-05-09 18:26:35 +0100161#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000162 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100163
164 /* Update PSCI stats */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000165 psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100166#endif
167
Soby Mathew539dced2014-10-02 16:56:51 +0100168 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100169 }
170
Achin Gupta317ba092014-05-09 19:32:25 +0100171 /*
Soby Mathew67487842015-07-13 14:10:57 +0100172 * If a power down state has been requested, we need to verify entry
173 * point and program entry information.
Soby Mathew78879b92015-01-06 15:36:38 +0000174 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100175 if (is_power_down_state != 0U) {
Soby Mathew617540d2015-07-15 12:13:26 +0100176 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530177 if (rc != PSCI_E_SUCCESS) {
Soby Mathew67487842015-07-13 14:10:57 +0100178 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530179 }
Soby Mathew67487842015-07-13 14:10:57 +0100180 }
Soby Mathew31244d72014-09-30 11:19:51 +0100181
Soby Mathew78879b92015-01-06 15:36:38 +0000182 /*
Achin Gupta317ba092014-05-09 19:32:25 +0100183 * Do what is needed to enter the power down state. Upon success,
Soby Mathew67487842015-07-13 14:10:57 +0100184 * enter the final wfi which will power down this CPU. This function
185 * might return if the power down was abandoned for any reason, e.g.
186 * arrival of an interrupt
Achin Gupta317ba092014-05-09 19:32:25 +0100187 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000188 rc = psci_cpu_suspend_start(cpu_idx,
189 &ep,
Wing Li606b7432022-09-14 13:18:17 -0700190 target_pwrlvl,
191 &state_info,
192 is_power_down_state);
Soby Mathew539dced2014-10-02 16:56:51 +0100193
Wing Li606b7432022-09-14 13:18:17 -0700194 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100195}
196
Soby Mathew9d070b92015-07-29 17:05:03 +0100197
198int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000199{
200 int rc;
Soby Mathew67487842015-07-13 14:10:57 +0100201 psci_power_state_t state_info;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000202 entry_point_info_t ep;
Boyan Karatotev3b802102024-11-06 16:26:15 +0000203 unsigned int cpu_idx = plat_my_core_pos();
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000204
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000205 /* Check if the current CPU is the last ON CPU in the system */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530206 if (!psci_is_last_on_cpu(cpu_idx)) {
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000207 return PSCI_E_DENIED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530208 }
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000209
Soby Mathew617540d2015-07-15 12:13:26 +0100210 /* Validate the entry point and get the entry_point_info */
211 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530212 if (rc != PSCI_E_SUCCESS) {
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000213 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530214 }
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000215
Soby Mathew67487842015-07-13 14:10:57 +0100216 /* Query the psci_power_state for system suspend */
217 psci_query_sys_suspend_pwrstate(&state_info);
218
ldtsa4065ab2018-10-11 08:40:32 +0200219 /*
220 * Check if platform allows suspend to Highest power level
221 * (System level)
222 */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530223 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) {
ldtsa4065ab2018-10-11 08:40:32 +0200224 return PSCI_E_DENIED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530225 }
Soby Mathew67487842015-07-13 14:10:57 +0100226 /* Ensure that the psci_power_state makes sense */
Soby Mathew67487842015-07-13 14:10:57 +0100227 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
228 == PSCI_E_SUCCESS);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100229 assert(is_local_state_off(
230 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000231
232 /*
Soby Mathew67487842015-07-13 14:10:57 +0100233 * Do what is needed to enter the system suspend state. This function
234 * might return if the power down was abandoned for any reason, e.g.
235 * arrival of an interrupt
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000236 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000237 rc = psci_cpu_suspend_start(cpu_idx,
238 &ep,
Wing Li606b7432022-09-14 13:18:17 -0700239 PLAT_MAX_PWR_LVL,
240 &state_info,
241 PSTATE_TYPE_POWERDOWN);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000242
Wing Li606b7432022-09-14 13:18:17 -0700243 return rc;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000244}
245
Achin Gupta4f6ad662013-10-25 09:08:21 +0100246int psci_cpu_off(void)
247{
248 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100249 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100250
Achin Gupta4f6ad662013-10-25 09:08:21 +0100251 /*
Soby Mathew67487842015-07-13 14:10:57 +0100252 * Do what is needed to power off this CPU and possible higher power
253 * levels if it able to do so. Upon success, enter the final wfi
254 * which will power down this CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100255 */
Soby Mathew67487842015-07-13 14:10:57 +0100256 rc = psci_do_cpu_off(target_pwrlvl);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100257
Achin Gupta3140a9e2013-12-02 16:23:12 +0000258 /*
259 * The only error cpu_off can return is E_DENIED. So check if that's
260 * indeed the case.
261 */
Soby Mathewda554d72016-05-03 17:11:42 +0100262 assert(rc == PSCI_E_DENIED);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100263
264 return rc;
265}
266
Soby Mathew9d070b92015-07-29 17:05:03 +0100267int psci_affinity_info(u_register_t target_affinity,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100268 unsigned int lowest_affinity_level)
269{
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600270 unsigned int target_idx;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100271
Manish Pandeye60c1842023-10-27 11:45:44 +0100272 /* Validate the target affinity */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530273 if (!is_valid_mpidr(target_affinity)) {
Manish Pandeye60c1842023-10-27 11:45:44 +0100274 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530275 }
Manish Pandeye60c1842023-10-27 11:45:44 +0100276
Soby Mathew67487842015-07-13 14:10:57 +0100277 /* We dont support level higher than PSCI_CPU_PWR_LVL */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530278 if (lowest_affinity_level > PSCI_CPU_PWR_LVL) {
Soby Mathew67487842015-07-13 14:10:57 +0100279 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530280 }
Soby Mathew67487842015-07-13 14:10:57 +0100281 /* Calculate the cpu index of the target */
Manish Pandeye60c1842023-10-27 11:45:44 +0100282 target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
Achin Gupta75f73672013-12-05 16:33:10 +0000283
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000284 /*
285 * Generic management:
286 * Perform cache maintanence ahead of reading the target CPU state to
287 * ensure that the data is not stale.
288 * There is a theoretical edge case where the cache may contain stale
289 * data for the target CPU data - this can occur under the following
290 * conditions:
291 * - the target CPU is in another cluster from the current
292 * - the target CPU was the last CPU to shutdown on its cluster
293 * - the cluster was removed from coherency as part of the CPU shutdown
294 *
295 * In this case the cache maintenace that was performed as part of the
296 * target CPUs shutdown was not seen by the current CPU's cluster. And
297 * so the cache may contain stale data for the target CPU.
298 */
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600299 flush_cpu_data_by_index(target_idx,
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100300 psci_svc_cpu_data.aff_info_state);
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000301
Soby Mathew67487842015-07-13 14:10:57 +0100302 return psci_get_aff_info_state_by_idx(target_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100303}
304
Soby Mathew9d070b92015-07-29 17:05:03 +0100305int psci_migrate(u_register_t target_cpu)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100306{
Soby Mathew8991eed2014-10-23 10:35:34 +0100307 int rc;
Boyan Karatotev382ba742025-04-07 15:46:39 +0100308 u_register_t resident_cpu_mpidr = 0;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100309
Manish Pandeye60c1842023-10-27 11:45:44 +0100310 /* Validate the target cpu */
311 if (!is_valid_mpidr(target_cpu))
312 return PSCI_E_INVALID_PARAMS;
313
Soby Mathew8991eed2014-10-23 10:35:34 +0100314 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530315 if (rc != PSCI_TOS_UP_MIG_CAP) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100316 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
317 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530318 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100319
Achin Gupta4f6ad662013-10-25 09:08:21 +0100320 /*
Soby Mathew8991eed2014-10-23 10:35:34 +0100321 * Migrate should only be invoked on the CPU where
322 * the Secure OS is resident.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100323 */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530324 if (resident_cpu_mpidr != read_mpidr_el1()) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100325 return PSCI_E_NOT_PRESENT;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530326 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100327
328 /* Check the validity of the specified target cpu */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530329 if (!is_valid_mpidr(target_cpu)) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100330 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530331 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100332
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100333 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100334
335 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100336 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100337
338 return rc;
339}
340
341int psci_migrate_info_type(void)
342{
Soby Mathew9d070b92015-07-29 17:05:03 +0100343 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100344
345 return psci_spd_migrate_info(&resident_cpu_mpidr);
346}
347
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100348u_register_t psci_migrate_info_up_cpu(void)
Soby Mathew8991eed2014-10-23 10:35:34 +0100349{
Boyan Karatotev382ba742025-04-07 15:46:39 +0100350 u_register_t resident_cpu_mpidr = 0;
Soby Mathew8991eed2014-10-23 10:35:34 +0100351 int rc;
352
353 /*
354 * Return value of this depends upon what
355 * psci_spd_migrate_info() returns.
356 */
357 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100358 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
359 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
Soby Mathew8991eed2014-10-23 10:35:34 +0100360
361 return resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100362}
363
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100364int psci_node_hw_state(u_register_t target_cpu,
365 unsigned int power_level)
366{
367 int rc;
368
369 /* Validate target_cpu */
Manish Pandeye60c1842023-10-27 11:45:44 +0100370 if (!is_valid_mpidr(target_cpu))
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100371 return PSCI_E_INVALID_PARAMS;
372
373 /* Validate power_level against PLAT_MAX_PWR_LVL */
374 if (power_level > PLAT_MAX_PWR_LVL)
375 return PSCI_E_INVALID_PARAMS;
376
377 /*
378 * Dispatch this call to platform to query power controller, and pass on
379 * to the caller what it returns
380 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100381 assert(psci_plat_pm_ops->get_node_hw_state != NULL);
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100382 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100383 assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
384 || (rc == PSCI_E_NOT_SUPPORTED)
385 || (rc == PSCI_E_INVALID_PARAMS));
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100386 return rc;
387}
388
Soby Mathew90e82582015-01-07 11:10:22 +0000389int psci_features(unsigned int psci_fid)
390{
Soby Mathew9d070b92015-07-29 17:05:03 +0100391 unsigned int local_caps = psci_caps;
Soby Mathew90e82582015-01-07 11:10:22 +0000392
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530393 if (psci_fid == SMCCC_VERSION) {
Dimitris Papastamos6eabbb02018-01-22 12:58:52 +0000394 return PSCI_E_SUCCESS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530395 }
Soby Mathew90e82582015-01-07 11:10:22 +0000396 /* Check if it is a 64 bit function */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530397 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) {
Soby Mathew90e82582015-01-07 11:10:22 +0000398 local_caps &= PSCI_CAP_64BIT_MASK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530399 }
Soby Mathew90e82582015-01-07 11:10:22 +0000400 /* Check for invalid fid */
401 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530402 && is_psci_fid(psci_fid))) {
Soby Mathew90e82582015-01-07 11:10:22 +0000403 return PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530404 }
Soby Mathew90e82582015-01-07 11:10:22 +0000405
406 /* Check if the psci fid is supported or not */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530407 if ((local_caps & define_psci_cap(psci_fid)) == 0U) {
Soby Mathew90e82582015-01-07 11:10:22 +0000408 return PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530409 }
Soby Mathew90e82582015-01-07 11:10:22 +0000410 /* Format the feature flags */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100411 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
412 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100413 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
Wing Li9a70e692022-09-14 13:18:19 -0700414 (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
415 return (int)ret;
Soby Mathew90e82582015-01-07 11:10:22 +0000416 }
417
418 /* Return 0 for all other fid's */
419 return PSCI_E_SUCCESS;
420}
421
Wing Lib88a4412022-09-14 13:18:15 -0700422#if PSCI_OS_INIT_MODE
423int psci_set_suspend_mode(unsigned int mode)
424{
425 if (psci_suspend_mode == mode) {
426 return PSCI_E_SUCCESS;
427 }
428
Boyan Karatotev3b802102024-11-06 16:26:15 +0000429 unsigned int this_core = plat_my_core_pos();
430
Wing Lib88a4412022-09-14 13:18:15 -0700431 if (mode == PLAT_COORD) {
432 /* Check if the current CPU is the last ON CPU in the system */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000433 if (!psci_is_last_on_cpu_safe(this_core)) {
Wing Lib88a4412022-09-14 13:18:15 -0700434 return PSCI_E_DENIED;
435 }
436 }
437
438 if (mode == OS_INIT) {
439 /*
440 * Check if all CPUs in the system are ON or if the current
441 * CPU is the last ON CPU in the system.
442 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000443 if (!(psci_are_all_cpus_on_safe(this_core) ||
444 psci_is_last_on_cpu_safe(this_core))) {
Wing Lib88a4412022-09-14 13:18:15 -0700445 return PSCI_E_DENIED;
446 }
447 }
448
449 psci_suspend_mode = mode;
450 psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
451 sizeof(psci_suspend_mode));
452
453 return PSCI_E_SUCCESS;
454}
455#endif
456
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000457/*******************************************************************************
458 * PSCI top level handler for servicing SMCs.
459 ******************************************************************************/
Soby Mathewcf0b1492016-04-29 19:01:30 +0100460u_register_t psci_smc_handler(uint32_t smc_fid,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100461 u_register_t x1,
462 u_register_t x2,
463 u_register_t x3,
464 u_register_t x4,
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000465 void *cookie,
466 void *handle,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100467 u_register_t flags)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000468{
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100469 u_register_t ret;
470
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530471 if (is_caller_secure(flags)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100472 return (u_register_t)SMC_UNK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530473 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000474
Soby Mathewb234b2c2015-01-15 11:49:49 +0000475 /* Check the fid against the capabilities */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530476 if ((psci_caps & define_psci_cap(smc_fid)) == 0U) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100477 return (u_register_t)SMC_UNK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530478 }
Soby Mathewb234b2c2015-01-15 11:49:49 +0000479
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100480 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
481 /* 32-bit PSCI function, clear top parameter bits */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000482
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100483 uint32_t r1 = (uint32_t)x1;
484 uint32_t r2 = (uint32_t)x2;
485 uint32_t r3 = (uint32_t)x3;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000486
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100487 switch (smc_fid) {
488 case PSCI_VERSION:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100489 ret = (u_register_t)psci_version();
490 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000491
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100492 case PSCI_CPU_OFF:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100493 ret = (u_register_t)psci_cpu_off();
494 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000495
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100496 case PSCI_CPU_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100497 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
498 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000499
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100500 case PSCI_CPU_ON_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100501 ret = (u_register_t)psci_cpu_on(r1, r2, r3);
502 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000503
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100504 case PSCI_AFFINITY_INFO_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100505 ret = (u_register_t)psci_affinity_info(r1, r2);
506 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000507
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100508 case PSCI_MIG_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100509 ret = (u_register_t)psci_migrate(r1);
510 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000511
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100512 case PSCI_MIG_INFO_TYPE:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100513 ret = (u_register_t)psci_migrate_info_type();
514 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100515
516 case PSCI_MIG_INFO_UP_CPU_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100517 ret = psci_migrate_info_up_cpu();
518 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100519
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100520 case PSCI_NODE_HW_STATE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100521 ret = (u_register_t)psci_node_hw_state(r1, r2);
522 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100523
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000524 case PSCI_SYSTEM_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100525 ret = (u_register_t)psci_system_suspend(r1, r2);
526 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000527
Juan Castillod5f13092014-08-12 11:17:06 +0100528 case PSCI_SYSTEM_OFF:
529 psci_system_off();
530 /* We should never return from psci_system_off() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000531 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100532
533 case PSCI_SYSTEM_RESET:
534 psci_system_reset();
535 /* We should never return from psci_system_reset() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000536 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100537
Soby Mathew90e82582015-01-07 11:10:22 +0000538 case PSCI_FEATURES:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100539 ret = (u_register_t)psci_features(r1);
540 break;
Soby Mathew90e82582015-01-07 11:10:22 +0000541
Wing Lib88a4412022-09-14 13:18:15 -0700542#if PSCI_OS_INIT_MODE
543 case PSCI_SET_SUSPEND_MODE:
544 ret = (u_register_t)psci_set_suspend_mode(r1);
545 break;
546#endif
547
Yatharth Kochar170fb932016-05-09 18:26:35 +0100548#if ENABLE_PSCI_STAT
549 case PSCI_STAT_RESIDENCY_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100550 ret = psci_stat_residency(r1, r2);
551 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100552
553 case PSCI_STAT_COUNT_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100554 ret = psci_stat_count(r1, r2);
555 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100556#endif
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100557 case PSCI_MEM_PROTECT:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100558 ret = psci_mem_protect(r1);
559 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100560
561 case PSCI_MEM_CHK_RANGE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100562 ret = psci_mem_chk_range(r1, r2);
563 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100564
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100565 case PSCI_SYSTEM_RESET2_AARCH32:
566 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100567 ret = psci_system_reset2(r1, r2);
568 break;
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100569
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100570 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100571 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
572 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100573 break;
574 }
575 } else {
576 /* 64-bit PSCI function */
577
578 switch (smc_fid) {
579 case PSCI_CPU_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100580 ret = (u_register_t)
581 psci_cpu_suspend((unsigned int)x1, x2, x3);
582 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100583
584 case PSCI_CPU_ON_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100585 ret = (u_register_t)psci_cpu_on(x1, x2, x3);
586 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100587
588 case PSCI_AFFINITY_INFO_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100589 ret = (u_register_t)
590 psci_affinity_info(x1, (unsigned int)x2);
591 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100592
593 case PSCI_MIG_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100594 ret = (u_register_t)psci_migrate(x1);
595 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100596
597 case PSCI_MIG_INFO_UP_CPU_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100598 ret = psci_migrate_info_up_cpu();
599 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100600
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100601 case PSCI_NODE_HW_STATE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100602 ret = (u_register_t)psci_node_hw_state(
603 x1, (unsigned int) x2);
604 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100605
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000606 case PSCI_SYSTEM_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100607 ret = (u_register_t)psci_system_suspend(x1, x2);
608 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000609
Yatharth Kochar170fb932016-05-09 18:26:35 +0100610#if ENABLE_PSCI_STAT
611 case PSCI_STAT_RESIDENCY_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100612 ret = psci_stat_residency(x1, (unsigned int) x2);
613 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100614
615 case PSCI_STAT_COUNT_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100616 ret = psci_stat_count(x1, (unsigned int) x2);
617 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100618#endif
619
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100620 case PSCI_MEM_CHK_RANGE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100621 ret = psci_mem_chk_range(x1, x2);
622 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100623
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100624 case PSCI_SYSTEM_RESET2_AARCH64:
625 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100626 ret = psci_system_reset2((uint32_t) x1, x2);
627 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100628
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100629 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100630 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
631 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100632 break;
633 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000634 }
635
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100636 return ret;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000637}