blob: dc902b6054672af8fcd84e33c1c6d1d61105c953 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Manish Pandeyef738d12024-06-22 00:00:18 +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();
Boyan Karatotev3b802102024-11-06 16:26:15 +000067#if PSCI_OS_INIT_MODE
Wing Li606b7432022-09-14 13:18:17 -070068 plat_local_state_t prev[PLAT_MAX_PWR_LVL];
69#endif
Achin Gupta4f6ad662013-10-25 09:08:21 +010070
Boyan Karatotev45c73282024-09-20 13:37:51 +010071#if ERRATA_SME_POWER_DOWN
72 /*
73 * If SME isn't off, attempting a real power down will only end up being
74 * rejected. If we got called with SME on, fall back to a normal
75 * suspend. We can't force SME off as in the event the power down is
76 * rejected for another reason (eg GIC) we'd lose the SME context.
77 */
78 if (is_feat_sme_supported() && read_svcr() != 0) {
79 power_state &= ~(PSTATE_TYPE_MASK << PSTATE_TYPE_SHIFT);
80 power_state &= ~(PSTATE_PWR_LVL_MASK << PSTATE_PWR_LVL_SHIFT);
81 }
82#endif /* ERRATA_SME_POWER_DOWN */
83
Soby Mathew67487842015-07-13 14:10:57 +010084 /* Validate the power_state parameter */
85 rc = psci_validate_power_state(power_state, &state_info);
86 if (rc != PSCI_E_SUCCESS) {
87 assert(rc == PSCI_E_INVALID_PARAMS);
88 return rc;
Soby Mathew539dced2014-10-02 16:56:51 +010089 }
90
Achin Gupta317ba092014-05-09 19:32:25 +010091 /*
Soby Mathew67487842015-07-13 14:10:57 +010092 * Get the value of the state type bit from the power state parameter.
Achin Gupta317ba092014-05-09 19:32:25 +010093 */
Soby Mathew67487842015-07-13 14:10:57 +010094 is_power_down_state = psci_get_pstate_type(power_state);
95
96 /* Sanity check the requested suspend levels */
Soby Mathewda554d72016-05-03 17:11:42 +010097 assert(psci_validate_suspend_req(&state_info, is_power_down_state)
Soby Mathew67487842015-07-13 14:10:57 +010098 == PSCI_E_SUCCESS);
99
100 target_pwrlvl = psci_find_target_suspend_lvl(&state_info);
Sandrine Bailleuxa1c3faa2016-06-22 16:35:01 +0100101 if (target_pwrlvl == PSCI_INVALID_PWR_LVL) {
102 ERROR("Invalid target power level for suspend operation\n");
103 panic();
104 }
Soby Mathew67487842015-07-13 14:10:57 +0100105
106 /* Fast path for CPU standby.*/
Antonio Nino Diaz362030b2018-08-01 16:42:10 +0100107 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530108 if (psci_plat_pm_ops->cpu_standby == NULL) {
Vikram Kanigirid118f9f2014-03-21 11:57:10 +0000109 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530110 }
Achin Gupta317ba092014-05-09 19:32:25 +0100111
Soby Mathew67487842015-07-13 14:10:57 +0100112 /*
113 * Set the state of the CPU power domain to the platform
114 * specific retention state and enter the standby state.
115 */
116 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
117 psci_set_cpu_local_state(cpu_pd_state);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100118
Wing Li606b7432022-09-14 13:18:17 -0700119#if PSCI_OS_INIT_MODE
120 /*
121 * If in OS-initiated mode, save a copy of the previous
122 * requested local power states and update the new requested
123 * local power states for this CPU.
124 */
125 if (psci_suspend_mode == OS_INIT) {
126 psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
127 &state_info, prev);
128 }
129#endif
130
Yatharth Kochar170fb932016-05-09 18:26:35 +0100131#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000132 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100133#endif
134
dp-arm872be882016-09-19 11:18:44 +0100135#if ENABLE_RUNTIME_INSTRUMENTATION
136 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
137 RT_INSTR_ENTER_HW_LOW_PWR,
138 PMF_NO_CACHE_MAINT);
139#endif
140
Soby Mathew67487842015-07-13 14:10:57 +0100141 psci_plat_pm_ops->cpu_standby(cpu_pd_state);
142
143 /* Upon exit from standby, set the state back to RUN. */
144 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
145
Wing Li606b7432022-09-14 13:18:17 -0700146#if PSCI_OS_INIT_MODE
147 /*
148 * If in OS-initiated mode, restore the previous requested
149 * local power states for this CPU.
150 */
151 if (psci_suspend_mode == OS_INIT) {
152 psci_restore_req_local_pwr_states(cpu_idx, prev);
153 }
154#endif
155
dp-arm872be882016-09-19 11:18:44 +0100156#if ENABLE_RUNTIME_INSTRUMENTATION
157 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
158 RT_INSTR_EXIT_HW_LOW_PWR,
159 PMF_NO_CACHE_MAINT);
160#endif
161
Yatharth Kochar170fb932016-05-09 18:26:35 +0100162#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000163 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100164
165 /* Update PSCI stats */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000166 psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100167#endif
168
Soby Mathew539dced2014-10-02 16:56:51 +0100169 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100170 }
171
Achin Gupta317ba092014-05-09 19:32:25 +0100172 /*
Soby Mathew67487842015-07-13 14:10:57 +0100173 * If a power down state has been requested, we need to verify entry
174 * point and program entry information.
Soby Mathew78879b92015-01-06 15:36:38 +0000175 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100176 if (is_power_down_state != 0U) {
Manish Pandeyef738d12024-06-22 00:00:18 +0100177 entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
178
179 rc = psci_validate_entry_point(ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530180 if (rc != PSCI_E_SUCCESS) {
Soby Mathew67487842015-07-13 14:10:57 +0100181 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530182 }
Soby Mathew67487842015-07-13 14:10:57 +0100183 }
Soby Mathew31244d72014-09-30 11:19:51 +0100184
Soby Mathew78879b92015-01-06 15:36:38 +0000185 /*
Achin Gupta317ba092014-05-09 19:32:25 +0100186 * Do what is needed to enter the power down state. Upon success,
Soby Mathew67487842015-07-13 14:10:57 +0100187 * enter the final wfi which will power down this CPU. This function
188 * might return if the power down was abandoned for any reason, e.g.
189 * arrival of an interrupt
Achin Gupta317ba092014-05-09 19:32:25 +0100190 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000191 rc = psci_cpu_suspend_start(cpu_idx,
Wing Li606b7432022-09-14 13:18:17 -0700192 target_pwrlvl,
193 &state_info,
194 is_power_down_state);
Soby Mathew539dced2014-10-02 16:56:51 +0100195
Wing Li606b7432022-09-14 13:18:17 -0700196 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100197}
198
Soby Mathew9d070b92015-07-29 17:05:03 +0100199
200int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000201{
202 int rc;
Soby Mathew67487842015-07-13 14:10:57 +0100203 psci_power_state_t state_info;
Boyan Karatotev3b802102024-11-06 16:26:15 +0000204 unsigned int cpu_idx = plat_my_core_pos();
Manish Pandeyef738d12024-06-22 00:00:18 +0100205 entry_point_info_t *ep = get_cpu_data_by_index(cpu_idx, warmboot_ep_info);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000206
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000207 /* Check if the current CPU is the last ON CPU in the system */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530208 if (!psci_is_last_on_cpu(cpu_idx)) {
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000209 return PSCI_E_DENIED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530210 }
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000211
Soby Mathew617540d2015-07-15 12:13:26 +0100212 /* Validate the entry point and get the entry_point_info */
Manish Pandeyef738d12024-06-22 00:00:18 +0100213 rc = psci_validate_entry_point(ep, entrypoint, context_id);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530214 if (rc != PSCI_E_SUCCESS) {
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000215 return rc;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530216 }
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000217
Soby Mathew67487842015-07-13 14:10:57 +0100218 /* Query the psci_power_state for system suspend */
219 psci_query_sys_suspend_pwrstate(&state_info);
220
ldtsa4065ab2018-10-11 08:40:32 +0200221 /*
222 * Check if platform allows suspend to Highest power level
223 * (System level)
224 */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530225 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL) {
ldtsa4065ab2018-10-11 08:40:32 +0200226 return PSCI_E_DENIED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530227 }
Soby Mathew67487842015-07-13 14:10:57 +0100228 /* Ensure that the psci_power_state makes sense */
Soby Mathew67487842015-07-13 14:10:57 +0100229 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
230 == PSCI_E_SUCCESS);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100231 assert(is_local_state_off(
232 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000233
234 /*
Soby Mathew67487842015-07-13 14:10:57 +0100235 * Do what is needed to enter the system suspend state. This function
236 * might return if the power down was abandoned for any reason, e.g.
237 * arrival of an interrupt
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000238 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000239 rc = psci_cpu_suspend_start(cpu_idx,
Wing Li606b7432022-09-14 13:18:17 -0700240 PLAT_MAX_PWR_LVL,
241 &state_info,
242 PSTATE_TYPE_POWERDOWN);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000243
Wing Li606b7432022-09-14 13:18:17 -0700244 return rc;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000245}
246
Achin Gupta4f6ad662013-10-25 09:08:21 +0100247int psci_cpu_off(void)
248{
249 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100250 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100251
Achin Gupta4f6ad662013-10-25 09:08:21 +0100252 /*
Soby Mathew67487842015-07-13 14:10:57 +0100253 * Do what is needed to power off this CPU and possible higher power
254 * levels if it able to do so. Upon success, enter the final wfi
255 * which will power down this CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100256 */
Soby Mathew67487842015-07-13 14:10:57 +0100257 rc = psci_do_cpu_off(target_pwrlvl);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100258
Achin Gupta3140a9e2013-12-02 16:23:12 +0000259 /*
260 * The only error cpu_off can return is E_DENIED. So check if that's
261 * indeed the case.
262 */
Soby Mathewda554d72016-05-03 17:11:42 +0100263 assert(rc == PSCI_E_DENIED);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100264
265 return rc;
266}
267
Soby Mathew9d070b92015-07-29 17:05:03 +0100268int psci_affinity_info(u_register_t target_affinity,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100269 unsigned int lowest_affinity_level)
270{
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600271 unsigned int target_idx;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100272
Manish Pandeye60c1842023-10-27 11:45:44 +0100273 /* Validate the target affinity */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530274 if (!is_valid_mpidr(target_affinity)) {
Manish Pandeye60c1842023-10-27 11:45:44 +0100275 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530276 }
Manish Pandeye60c1842023-10-27 11:45:44 +0100277
Soby Mathew67487842015-07-13 14:10:57 +0100278 /* We dont support level higher than PSCI_CPU_PWR_LVL */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530279 if (lowest_affinity_level > PSCI_CPU_PWR_LVL) {
Soby Mathew67487842015-07-13 14:10:57 +0100280 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530281 }
Soby Mathew67487842015-07-13 14:10:57 +0100282 /* Calculate the cpu index of the target */
Manish Pandeye60c1842023-10-27 11:45:44 +0100283 target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
Achin Gupta75f73672013-12-05 16:33:10 +0000284
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000285 /*
286 * Generic management:
287 * Perform cache maintanence ahead of reading the target CPU state to
288 * ensure that the data is not stale.
289 * There is a theoretical edge case where the cache may contain stale
290 * data for the target CPU data - this can occur under the following
291 * conditions:
292 * - the target CPU is in another cluster from the current
293 * - the target CPU was the last CPU to shutdown on its cluster
294 * - the cluster was removed from coherency as part of the CPU shutdown
295 *
296 * In this case the cache maintenace that was performed as part of the
297 * target CPUs shutdown was not seen by the current CPU's cluster. And
298 * so the cache may contain stale data for the target CPU.
299 */
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600300 flush_cpu_data_by_index(target_idx,
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100301 psci_svc_cpu_data.aff_info_state);
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000302
Soby Mathew67487842015-07-13 14:10:57 +0100303 return psci_get_aff_info_state_by_idx(target_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100304}
305
Soby Mathew9d070b92015-07-29 17:05:03 +0100306int psci_migrate(u_register_t target_cpu)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100307{
Soby Mathew8991eed2014-10-23 10:35:34 +0100308 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100309 u_register_t resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100310
Manish Pandeye60c1842023-10-27 11:45:44 +0100311 /* Validate the target cpu */
312 if (!is_valid_mpidr(target_cpu))
313 return PSCI_E_INVALID_PARAMS;
314
Soby Mathew8991eed2014-10-23 10:35:34 +0100315 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530316 if (rc != PSCI_TOS_UP_MIG_CAP) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100317 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
318 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530319 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100320
Achin Gupta4f6ad662013-10-25 09:08:21 +0100321 /*
Soby Mathew8991eed2014-10-23 10:35:34 +0100322 * Migrate should only be invoked on the CPU where
323 * the Secure OS is resident.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100324 */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530325 if (resident_cpu_mpidr != read_mpidr_el1()) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100326 return PSCI_E_NOT_PRESENT;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530327 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100328
329 /* Check the validity of the specified target cpu */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530330 if (!is_valid_mpidr(target_cpu)) {
Soby Mathew8991eed2014-10-23 10:35:34 +0100331 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530332 }
Soby Mathew8991eed2014-10-23 10:35:34 +0100333
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100334 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100335
336 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100337 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100338
339 return rc;
340}
341
342int psci_migrate_info_type(void)
343{
Soby Mathew9d070b92015-07-29 17:05:03 +0100344 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100345
346 return psci_spd_migrate_info(&resident_cpu_mpidr);
347}
348
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100349u_register_t psci_migrate_info_up_cpu(void)
Soby Mathew8991eed2014-10-23 10:35:34 +0100350{
Soby Mathew9d070b92015-07-29 17:05:03 +0100351 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100352 int rc;
353
354 /*
355 * Return value of this depends upon what
356 * psci_spd_migrate_info() returns.
357 */
358 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100359 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
360 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
Soby Mathew8991eed2014-10-23 10:35:34 +0100361
362 return resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100363}
364
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100365int psci_node_hw_state(u_register_t target_cpu,
366 unsigned int power_level)
367{
368 int rc;
369
370 /* Validate target_cpu */
Manish Pandeye60c1842023-10-27 11:45:44 +0100371 if (!is_valid_mpidr(target_cpu))
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100372 return PSCI_E_INVALID_PARAMS;
373
374 /* Validate power_level against PLAT_MAX_PWR_LVL */
375 if (power_level > PLAT_MAX_PWR_LVL)
376 return PSCI_E_INVALID_PARAMS;
377
378 /*
379 * Dispatch this call to platform to query power controller, and pass on
380 * to the caller what it returns
381 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100382 assert(psci_plat_pm_ops->get_node_hw_state != NULL);
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100383 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100384 assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
385 || (rc == PSCI_E_NOT_SUPPORTED)
386 || (rc == PSCI_E_INVALID_PARAMS));
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100387 return rc;
388}
389
Soby Mathew90e82582015-01-07 11:10:22 +0000390int psci_features(unsigned int psci_fid)
391{
Soby Mathew9d070b92015-07-29 17:05:03 +0100392 unsigned int local_caps = psci_caps;
Soby Mathew90e82582015-01-07 11:10:22 +0000393
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530394 if (psci_fid == SMCCC_VERSION) {
Dimitris Papastamos6eabbb02018-01-22 12:58:52 +0000395 return PSCI_E_SUCCESS;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530396 }
Soby Mathew90e82582015-01-07 11:10:22 +0000397 /* Check if it is a 64 bit function */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530398 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64) {
Soby Mathew90e82582015-01-07 11:10:22 +0000399 local_caps &= PSCI_CAP_64BIT_MASK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530400 }
Soby Mathew90e82582015-01-07 11:10:22 +0000401 /* Check for invalid fid */
402 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530403 && is_psci_fid(psci_fid))) {
Soby Mathew90e82582015-01-07 11:10:22 +0000404 return PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530405 }
Soby Mathew90e82582015-01-07 11:10:22 +0000406
407 /* Check if the psci fid is supported or not */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530408 if ((local_caps & define_psci_cap(psci_fid)) == 0U) {
Soby Mathew90e82582015-01-07 11:10:22 +0000409 return PSCI_E_NOT_SUPPORTED;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530410 }
Soby Mathew90e82582015-01-07 11:10:22 +0000411 /* Format the feature flags */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100412 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
413 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100414 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
Wing Li9a70e692022-09-14 13:18:19 -0700415 (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
416 return (int)ret;
Soby Mathew90e82582015-01-07 11:10:22 +0000417 }
418
419 /* Return 0 for all other fid's */
420 return PSCI_E_SUCCESS;
421}
422
Wing Lib88a4412022-09-14 13:18:15 -0700423#if PSCI_OS_INIT_MODE
424int psci_set_suspend_mode(unsigned int mode)
425{
426 if (psci_suspend_mode == mode) {
427 return PSCI_E_SUCCESS;
428 }
429
Boyan Karatotev3b802102024-11-06 16:26:15 +0000430 unsigned int this_core = plat_my_core_pos();
431
Wing Lib88a4412022-09-14 13:18:15 -0700432 if (mode == PLAT_COORD) {
433 /* Check if the current CPU is the last ON CPU in the system */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000434 if (!psci_is_last_on_cpu_safe(this_core)) {
Wing Lib88a4412022-09-14 13:18:15 -0700435 return PSCI_E_DENIED;
436 }
437 }
438
439 if (mode == OS_INIT) {
440 /*
441 * Check if all CPUs in the system are ON or if the current
442 * CPU is the last ON CPU in the system.
443 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000444 if (!(psci_are_all_cpus_on_safe(this_core) ||
445 psci_is_last_on_cpu_safe(this_core))) {
Wing Lib88a4412022-09-14 13:18:15 -0700446 return PSCI_E_DENIED;
447 }
448 }
449
450 psci_suspend_mode = mode;
451 psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
452 sizeof(psci_suspend_mode));
453
454 return PSCI_E_SUCCESS;
455}
456#endif
457
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000458/*******************************************************************************
459 * PSCI top level handler for servicing SMCs.
460 ******************************************************************************/
Soby Mathewcf0b1492016-04-29 19:01:30 +0100461u_register_t psci_smc_handler(uint32_t smc_fid,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100462 u_register_t x1,
463 u_register_t x2,
464 u_register_t x3,
465 u_register_t x4,
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000466 void *cookie,
467 void *handle,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100468 u_register_t flags)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000469{
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100470 u_register_t ret;
471
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530472 if (is_caller_secure(flags)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100473 return (u_register_t)SMC_UNK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530474 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000475
Soby Mathewb234b2c2015-01-15 11:49:49 +0000476 /* Check the fid against the capabilities */
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530477 if ((psci_caps & define_psci_cap(smc_fid)) == 0U) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100478 return (u_register_t)SMC_UNK;
Maheedhar Bollapallic7b0a282024-04-25 11:47:27 +0530479 }
Soby Mathewb234b2c2015-01-15 11:49:49 +0000480
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100481 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
482 /* 32-bit PSCI function, clear top parameter bits */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000483
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100484 uint32_t r1 = (uint32_t)x1;
485 uint32_t r2 = (uint32_t)x2;
486 uint32_t r3 = (uint32_t)x3;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000487
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100488 switch (smc_fid) {
489 case PSCI_VERSION:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100490 ret = (u_register_t)psci_version();
491 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000492
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100493 case PSCI_CPU_OFF:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100494 ret = (u_register_t)psci_cpu_off();
495 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000496
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100497 case PSCI_CPU_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100498 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
499 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000500
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100501 case PSCI_CPU_ON_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100502 ret = (u_register_t)psci_cpu_on(r1, r2, r3);
503 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000504
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100505 case PSCI_AFFINITY_INFO_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100506 ret = (u_register_t)psci_affinity_info(r1, r2);
507 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000508
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100509 case PSCI_MIG_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100510 ret = (u_register_t)psci_migrate(r1);
511 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000512
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100513 case PSCI_MIG_INFO_TYPE:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100514 ret = (u_register_t)psci_migrate_info_type();
515 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100516
517 case PSCI_MIG_INFO_UP_CPU_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100518 ret = psci_migrate_info_up_cpu();
519 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100520
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100521 case PSCI_NODE_HW_STATE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100522 ret = (u_register_t)psci_node_hw_state(r1, r2);
523 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100524
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000525 case PSCI_SYSTEM_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100526 ret = (u_register_t)psci_system_suspend(r1, r2);
527 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000528
Juan Castillod5f13092014-08-12 11:17:06 +0100529 case PSCI_SYSTEM_OFF:
530 psci_system_off();
531 /* We should never return from psci_system_off() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000532 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100533
534 case PSCI_SYSTEM_RESET:
535 psci_system_reset();
536 /* We should never return from psci_system_reset() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000537 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100538
Soby Mathew90e82582015-01-07 11:10:22 +0000539 case PSCI_FEATURES:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100540 ret = (u_register_t)psci_features(r1);
541 break;
Soby Mathew90e82582015-01-07 11:10:22 +0000542
Wing Lib88a4412022-09-14 13:18:15 -0700543#if PSCI_OS_INIT_MODE
544 case PSCI_SET_SUSPEND_MODE:
545 ret = (u_register_t)psci_set_suspend_mode(r1);
546 break;
547#endif
548
Yatharth Kochar170fb932016-05-09 18:26:35 +0100549#if ENABLE_PSCI_STAT
550 case PSCI_STAT_RESIDENCY_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100551 ret = psci_stat_residency(r1, r2);
552 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100553
554 case PSCI_STAT_COUNT_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100555 ret = psci_stat_count(r1, r2);
556 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100557#endif
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100558 case PSCI_MEM_PROTECT:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100559 ret = psci_mem_protect(r1);
560 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100561
562 case PSCI_MEM_CHK_RANGE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100563 ret = psci_mem_chk_range(r1, r2);
564 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100565
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100566 case PSCI_SYSTEM_RESET2_AARCH32:
567 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100568 ret = psci_system_reset2(r1, r2);
569 break;
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100570
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100571 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100572 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
573 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100574 break;
575 }
576 } else {
577 /* 64-bit PSCI function */
578
579 switch (smc_fid) {
580 case PSCI_CPU_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100581 ret = (u_register_t)
582 psci_cpu_suspend((unsigned int)x1, x2, x3);
583 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100584
585 case PSCI_CPU_ON_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100586 ret = (u_register_t)psci_cpu_on(x1, x2, x3);
587 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100588
589 case PSCI_AFFINITY_INFO_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100590 ret = (u_register_t)
591 psci_affinity_info(x1, (unsigned int)x2);
592 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100593
594 case PSCI_MIG_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100595 ret = (u_register_t)psci_migrate(x1);
596 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100597
598 case PSCI_MIG_INFO_UP_CPU_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100599 ret = psci_migrate_info_up_cpu();
600 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100601
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100602 case PSCI_NODE_HW_STATE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100603 ret = (u_register_t)psci_node_hw_state(
604 x1, (unsigned int) x2);
605 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100606
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000607 case PSCI_SYSTEM_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100608 ret = (u_register_t)psci_system_suspend(x1, x2);
609 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000610
Yatharth Kochar170fb932016-05-09 18:26:35 +0100611#if ENABLE_PSCI_STAT
612 case PSCI_STAT_RESIDENCY_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100613 ret = psci_stat_residency(x1, (unsigned int) x2);
614 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100615
616 case PSCI_STAT_COUNT_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100617 ret = psci_stat_count(x1, (unsigned int) x2);
618 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100619#endif
620
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100621 case PSCI_MEM_CHK_RANGE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100622 ret = psci_mem_chk_range(x1, x2);
623 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100624
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100625 case PSCI_SYSTEM_RESET2_AARCH64:
626 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100627 ret = psci_system_reset2((uint32_t) x1, x2);
628 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100629
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100630 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100631 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
632 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100633 break;
634 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000635 }
636
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100637 return ret;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000638}