blob: 45be63a24a849ec867e3f48113fa752e8e894031 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Boyan Karatotev45c73282024-09-20 13:37:51 +01002 * Copyright (c) 2013-2024, 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 */
34 if (!is_valid_mpidr(target_cpu))
Soby Mathew539dced2014-10-02 16:56:51 +010035 return PSCI_E_INVALID_PARAMS;
Soby Mathew539dced2014-10-02 16:56:51 +010036
Soby Mathew617540d2015-07-15 12:13:26 +010037 /* Validate the entry point and get the entry_point_info */
38 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew78879b92015-01-06 15:36:38 +000039 if (rc != PSCI_E_SUCCESS)
40 return rc;
41
Soby Mathew78879b92015-01-06 15:36:38 +000042 /*
Soby Mathew67487842015-07-13 14:10:57 +010043 * To turn this cpu on, specify which power
Achin Gupta0959db52013-12-02 17:33:04 +000044 * levels need to be turned on
45 */
Sandrine Bailleux22b09c12016-04-25 09:28:43 +010046 return psci_cpu_on_start(target_cpu, &ep);
Achin Gupta4f6ad662013-10-25 09:08:21 +010047}
48
49unsigned int psci_version(void)
50{
51 return PSCI_MAJOR_VER | PSCI_MINOR_VER;
52}
53
54int psci_cpu_suspend(unsigned int power_state,
Soby Mathew9d070b92015-07-29 17:05:03 +010055 uintptr_t entrypoint,
56 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +010057{
58 int rc;
Soby Mathew67487842015-07-13 14:10:57 +010059 unsigned int target_pwrlvl, is_power_down_state;
Soby Mathew78879b92015-01-06 15:36:38 +000060 entry_point_info_t ep;
Soby Mathew67487842015-07-13 14:10:57 +010061 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
62 plat_local_state_t cpu_pd_state;
Wing Li606b7432022-09-14 13:18:17 -070063 unsigned int cpu_idx = plat_my_core_pos();
Boyan Karatotev3b802102024-11-06 16:26:15 +000064#if PSCI_OS_INIT_MODE
Wing Li606b7432022-09-14 13:18:17 -070065 plat_local_state_t prev[PLAT_MAX_PWR_LVL];
66#endif
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
103 /* Fast path for CPU standby.*/
Antonio Nino Diaz362030b2018-08-01 16:42:10 +0100104 if (is_cpu_standby_req(is_power_down_state, target_pwrlvl)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100105 if (psci_plat_pm_ops->cpu_standby == NULL)
Vikram Kanigirid118f9f2014-03-21 11:57:10 +0000106 return PSCI_E_INVALID_PARAMS;
Achin Gupta317ba092014-05-09 19:32:25 +0100107
Soby Mathew67487842015-07-13 14:10:57 +0100108 /*
109 * Set the state of the CPU power domain to the platform
110 * specific retention state and enter the standby state.
111 */
112 cpu_pd_state = state_info.pwr_domain_state[PSCI_CPU_PWR_LVL];
113 psci_set_cpu_local_state(cpu_pd_state);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100114
Wing Li606b7432022-09-14 13:18:17 -0700115#if PSCI_OS_INIT_MODE
116 /*
117 * If in OS-initiated mode, save a copy of the previous
118 * requested local power states and update the new requested
119 * local power states for this CPU.
120 */
121 if (psci_suspend_mode == OS_INIT) {
122 psci_update_req_local_pwr_states(target_pwrlvl, cpu_idx,
123 &state_info, prev);
124 }
125#endif
126
Yatharth Kochar170fb932016-05-09 18:26:35 +0100127#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000128 plat_psci_stat_accounting_start(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100129#endif
130
dp-arm872be882016-09-19 11:18:44 +0100131#if ENABLE_RUNTIME_INSTRUMENTATION
132 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
133 RT_INSTR_ENTER_HW_LOW_PWR,
134 PMF_NO_CACHE_MAINT);
135#endif
136
Soby Mathew67487842015-07-13 14:10:57 +0100137 psci_plat_pm_ops->cpu_standby(cpu_pd_state);
138
139 /* Upon exit from standby, set the state back to RUN. */
140 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
141
Wing Li606b7432022-09-14 13:18:17 -0700142#if PSCI_OS_INIT_MODE
143 /*
144 * If in OS-initiated mode, restore the previous requested
145 * local power states for this CPU.
146 */
147 if (psci_suspend_mode == OS_INIT) {
148 psci_restore_req_local_pwr_states(cpu_idx, prev);
149 }
150#endif
151
dp-arm872be882016-09-19 11:18:44 +0100152#if ENABLE_RUNTIME_INSTRUMENTATION
153 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
154 RT_INSTR_EXIT_HW_LOW_PWR,
155 PMF_NO_CACHE_MAINT);
156#endif
157
Yatharth Kochar170fb932016-05-09 18:26:35 +0100158#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000159 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100160
161 /* Update PSCI stats */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000162 psci_stats_update_pwr_up(cpu_idx, PSCI_CPU_PWR_LVL, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100163#endif
164
Soby Mathew539dced2014-10-02 16:56:51 +0100165 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100166 }
167
Achin Gupta317ba092014-05-09 19:32:25 +0100168 /*
Soby Mathew67487842015-07-13 14:10:57 +0100169 * If a power down state has been requested, we need to verify entry
170 * point and program entry information.
Soby Mathew78879b92015-01-06 15:36:38 +0000171 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100172 if (is_power_down_state != 0U) {
Soby Mathew617540d2015-07-15 12:13:26 +0100173 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathew67487842015-07-13 14:10:57 +0100174 if (rc != PSCI_E_SUCCESS)
175 return rc;
176 }
Soby Mathew31244d72014-09-30 11:19:51 +0100177
Soby Mathew78879b92015-01-06 15:36:38 +0000178 /*
Achin Gupta317ba092014-05-09 19:32:25 +0100179 * Do what is needed to enter the power down state. Upon success,
Soby Mathew67487842015-07-13 14:10:57 +0100180 * enter the final wfi which will power down this CPU. This function
181 * might return if the power down was abandoned for any reason, e.g.
182 * arrival of an interrupt
Achin Gupta317ba092014-05-09 19:32:25 +0100183 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000184 rc = psci_cpu_suspend_start(cpu_idx,
185 &ep,
Wing Li606b7432022-09-14 13:18:17 -0700186 target_pwrlvl,
187 &state_info,
188 is_power_down_state);
Soby Mathew539dced2014-10-02 16:56:51 +0100189
Wing Li606b7432022-09-14 13:18:17 -0700190 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100191}
192
Soby Mathew9d070b92015-07-29 17:05:03 +0100193
194int psci_system_suspend(uintptr_t entrypoint, u_register_t context_id)
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000195{
196 int rc;
Soby Mathew67487842015-07-13 14:10:57 +0100197 psci_power_state_t state_info;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000198 entry_point_info_t ep;
Boyan Karatotev3b802102024-11-06 16:26:15 +0000199 unsigned int cpu_idx = plat_my_core_pos();
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000200
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000201 /* Check if the current CPU is the last ON CPU in the system */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000202 if (!psci_is_last_on_cpu(cpu_idx))
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000203 return PSCI_E_DENIED;
204
Soby Mathew617540d2015-07-15 12:13:26 +0100205 /* Validate the entry point and get the entry_point_info */
206 rc = psci_validate_entry_point(&ep, entrypoint, context_id);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000207 if (rc != PSCI_E_SUCCESS)
208 return rc;
209
Soby Mathew67487842015-07-13 14:10:57 +0100210 /* Query the psci_power_state for system suspend */
211 psci_query_sys_suspend_pwrstate(&state_info);
212
ldtsa4065ab2018-10-11 08:40:32 +0200213 /*
214 * Check if platform allows suspend to Highest power level
215 * (System level)
216 */
217 if (psci_find_target_suspend_lvl(&state_info) < PLAT_MAX_PWR_LVL)
218 return PSCI_E_DENIED;
219
Soby Mathew67487842015-07-13 14:10:57 +0100220 /* Ensure that the psci_power_state makes sense */
Soby Mathew67487842015-07-13 14:10:57 +0100221 assert(psci_validate_suspend_req(&state_info, PSTATE_TYPE_POWERDOWN)
222 == PSCI_E_SUCCESS);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100223 assert(is_local_state_off(
224 state_info.pwr_domain_state[PLAT_MAX_PWR_LVL]) != 0);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000225
226 /*
Soby Mathew67487842015-07-13 14:10:57 +0100227 * Do what is needed to enter the system suspend state. This function
228 * might return if the power down was abandoned for any reason, e.g.
229 * arrival of an interrupt
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000230 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000231 rc = psci_cpu_suspend_start(cpu_idx,
232 &ep,
Wing Li606b7432022-09-14 13:18:17 -0700233 PLAT_MAX_PWR_LVL,
234 &state_info,
235 PSTATE_TYPE_POWERDOWN);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000236
Wing Li606b7432022-09-14 13:18:17 -0700237 return rc;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000238}
239
Achin Gupta4f6ad662013-10-25 09:08:21 +0100240int psci_cpu_off(void)
241{
242 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100243 unsigned int target_pwrlvl = PLAT_MAX_PWR_LVL;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100244
Achin Gupta4f6ad662013-10-25 09:08:21 +0100245 /*
Soby Mathew67487842015-07-13 14:10:57 +0100246 * Do what is needed to power off this CPU and possible higher power
247 * levels if it able to do so. Upon success, enter the final wfi
248 * which will power down this CPU.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100249 */
Soby Mathew67487842015-07-13 14:10:57 +0100250 rc = psci_do_cpu_off(target_pwrlvl);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100251
Achin Gupta3140a9e2013-12-02 16:23:12 +0000252 /*
253 * The only error cpu_off can return is E_DENIED. So check if that's
254 * indeed the case.
255 */
Soby Mathewda554d72016-05-03 17:11:42 +0100256 assert(rc == PSCI_E_DENIED);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100257
258 return rc;
259}
260
Soby Mathew9d070b92015-07-29 17:05:03 +0100261int psci_affinity_info(u_register_t target_affinity,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100262 unsigned int lowest_affinity_level)
263{
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600264 unsigned int target_idx;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100265
Manish Pandeye60c1842023-10-27 11:45:44 +0100266 /* Validate the target affinity */
267 if (!is_valid_mpidr(target_affinity))
268 return PSCI_E_INVALID_PARAMS;
269
Soby Mathew67487842015-07-13 14:10:57 +0100270 /* We dont support level higher than PSCI_CPU_PWR_LVL */
271 if (lowest_affinity_level > PSCI_CPU_PWR_LVL)
272 return PSCI_E_INVALID_PARAMS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100273
Soby Mathew67487842015-07-13 14:10:57 +0100274 /* Calculate the cpu index of the target */
Manish Pandeye60c1842023-10-27 11:45:44 +0100275 target_idx = (unsigned int) plat_core_pos_by_mpidr(target_affinity);
Achin Gupta75f73672013-12-05 16:33:10 +0000276
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000277 /*
278 * Generic management:
279 * Perform cache maintanence ahead of reading the target CPU state to
280 * ensure that the data is not stale.
281 * There is a theoretical edge case where the cache may contain stale
282 * data for the target CPU data - this can occur under the following
283 * conditions:
284 * - the target CPU is in another cluster from the current
285 * - the target CPU was the last CPU to shutdown on its cluster
286 * - the cluster was removed from coherency as part of the CPU shutdown
287 *
288 * In this case the cache maintenace that was performed as part of the
289 * target CPUs shutdown was not seen by the current CPU's cluster. And
290 * so the cache may contain stale data for the target CPU.
291 */
Deepika Bhavnani5b33ad12019-12-13 10:23:18 -0600292 flush_cpu_data_by_index(target_idx,
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100293 psci_svc_cpu_data.aff_info_state);
Roberto Vargas8fd307f2017-11-13 08:24:07 +0000294
Soby Mathew67487842015-07-13 14:10:57 +0100295 return psci_get_aff_info_state_by_idx(target_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100296}
297
Soby Mathew9d070b92015-07-29 17:05:03 +0100298int psci_migrate(u_register_t target_cpu)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100299{
Soby Mathew8991eed2014-10-23 10:35:34 +0100300 int rc;
Soby Mathew9d070b92015-07-29 17:05:03 +0100301 u_register_t resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100302
Manish Pandeye60c1842023-10-27 11:45:44 +0100303 /* Validate the target cpu */
304 if (!is_valid_mpidr(target_cpu))
305 return PSCI_E_INVALID_PARAMS;
306
Soby Mathew8991eed2014-10-23 10:35:34 +0100307 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
308 if (rc != PSCI_TOS_UP_MIG_CAP)
309 return (rc == PSCI_TOS_NOT_UP_MIG_CAP) ?
310 PSCI_E_DENIED : PSCI_E_NOT_SUPPORTED;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100311
Achin Gupta4f6ad662013-10-25 09:08:21 +0100312 /*
Soby Mathew8991eed2014-10-23 10:35:34 +0100313 * Migrate should only be invoked on the CPU where
314 * the Secure OS is resident.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100315 */
Soby Mathew8991eed2014-10-23 10:35:34 +0100316 if (resident_cpu_mpidr != read_mpidr_el1())
317 return PSCI_E_NOT_PRESENT;
318
319 /* Check the validity of the specified target cpu */
Manish Pandeye60c1842023-10-27 11:45:44 +0100320 if (!is_valid_mpidr(target_cpu))
Soby Mathew8991eed2014-10-23 10:35:34 +0100321 return PSCI_E_INVALID_PARAMS;
322
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100323 assert((psci_spd_pm != NULL) && (psci_spd_pm->svc_migrate != NULL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100324
325 rc = psci_spd_pm->svc_migrate(read_mpidr_el1(), target_cpu);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100326 assert((rc == PSCI_E_SUCCESS) || (rc == PSCI_E_INTERN_FAIL));
Soby Mathew8991eed2014-10-23 10:35:34 +0100327
328 return rc;
329}
330
331int psci_migrate_info_type(void)
332{
Soby Mathew9d070b92015-07-29 17:05:03 +0100333 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100334
335 return psci_spd_migrate_info(&resident_cpu_mpidr);
336}
337
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100338u_register_t psci_migrate_info_up_cpu(void)
Soby Mathew8991eed2014-10-23 10:35:34 +0100339{
Soby Mathew9d070b92015-07-29 17:05:03 +0100340 u_register_t resident_cpu_mpidr;
Soby Mathew8991eed2014-10-23 10:35:34 +0100341 int rc;
342
343 /*
344 * Return value of this depends upon what
345 * psci_spd_migrate_info() returns.
346 */
347 rc = psci_spd_migrate_info(&resident_cpu_mpidr);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100348 if ((rc != PSCI_TOS_NOT_UP_MIG_CAP) && (rc != PSCI_TOS_UP_MIG_CAP))
349 return (u_register_t)(register_t) PSCI_E_INVALID_PARAMS;
Soby Mathew8991eed2014-10-23 10:35:34 +0100350
351 return resident_cpu_mpidr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100352}
353
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100354int psci_node_hw_state(u_register_t target_cpu,
355 unsigned int power_level)
356{
357 int rc;
358
359 /* Validate target_cpu */
Manish Pandeye60c1842023-10-27 11:45:44 +0100360 if (!is_valid_mpidr(target_cpu))
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100361 return PSCI_E_INVALID_PARAMS;
362
363 /* Validate power_level against PLAT_MAX_PWR_LVL */
364 if (power_level > PLAT_MAX_PWR_LVL)
365 return PSCI_E_INVALID_PARAMS;
366
367 /*
368 * Dispatch this call to platform to query power controller, and pass on
369 * to the caller what it returns
370 */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100371 assert(psci_plat_pm_ops->get_node_hw_state != NULL);
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100372 rc = psci_plat_pm_ops->get_node_hw_state(target_cpu, power_level);
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100373 assert(((rc >= HW_ON) && (rc <= HW_STANDBY))
374 || (rc == PSCI_E_NOT_SUPPORTED)
375 || (rc == PSCI_E_INVALID_PARAMS));
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100376 return rc;
377}
378
Soby Mathew90e82582015-01-07 11:10:22 +0000379int psci_features(unsigned int psci_fid)
380{
Soby Mathew9d070b92015-07-29 17:05:03 +0100381 unsigned int local_caps = psci_caps;
Soby Mathew90e82582015-01-07 11:10:22 +0000382
Dimitris Papastamos6eabbb02018-01-22 12:58:52 +0000383 if (psci_fid == SMCCC_VERSION)
384 return PSCI_E_SUCCESS;
385
Soby Mathew90e82582015-01-07 11:10:22 +0000386 /* Check if it is a 64 bit function */
387 if (((psci_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_64)
388 local_caps &= PSCI_CAP_64BIT_MASK;
389
390 /* Check for invalid fid */
391 if (!(is_std_svc_call(psci_fid) && is_valid_fast_smc(psci_fid)
392 && is_psci_fid(psci_fid)))
393 return PSCI_E_NOT_SUPPORTED;
394
395
396 /* Check if the psci fid is supported or not */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100397 if ((local_caps & define_psci_cap(psci_fid)) == 0U)
Soby Mathew90e82582015-01-07 11:10:22 +0000398 return PSCI_E_NOT_SUPPORTED;
399
400 /* Format the feature flags */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100401 if ((psci_fid == PSCI_CPU_SUSPEND_AARCH32) ||
402 (psci_fid == PSCI_CPU_SUSPEND_AARCH64)) {
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100403 unsigned int ret = ((FF_PSTATE << FF_PSTATE_SHIFT) |
Wing Li9a70e692022-09-14 13:18:19 -0700404 (FF_SUPPORTS_OS_INIT_MODE << FF_MODE_SUPPORT_SHIFT));
405 return (int)ret;
Soby Mathew90e82582015-01-07 11:10:22 +0000406 }
407
408 /* Return 0 for all other fid's */
409 return PSCI_E_SUCCESS;
410}
411
Wing Lib88a4412022-09-14 13:18:15 -0700412#if PSCI_OS_INIT_MODE
413int psci_set_suspend_mode(unsigned int mode)
414{
415 if (psci_suspend_mode == mode) {
416 return PSCI_E_SUCCESS;
417 }
418
Boyan Karatotev3b802102024-11-06 16:26:15 +0000419 unsigned int this_core = plat_my_core_pos();
420
Wing Lib88a4412022-09-14 13:18:15 -0700421 if (mode == PLAT_COORD) {
422 /* Check if the current CPU is the last ON CPU in the system */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000423 if (!psci_is_last_on_cpu_safe(this_core)) {
Wing Lib88a4412022-09-14 13:18:15 -0700424 return PSCI_E_DENIED;
425 }
426 }
427
428 if (mode == OS_INIT) {
429 /*
430 * Check if all CPUs in the system are ON or if the current
431 * CPU is the last ON CPU in the system.
432 */
Boyan Karatotev3b802102024-11-06 16:26:15 +0000433 if (!(psci_are_all_cpus_on_safe(this_core) ||
434 psci_is_last_on_cpu_safe(this_core))) {
Wing Lib88a4412022-09-14 13:18:15 -0700435 return PSCI_E_DENIED;
436 }
437 }
438
439 psci_suspend_mode = mode;
440 psci_flush_dcache_range((uintptr_t)&psci_suspend_mode,
441 sizeof(psci_suspend_mode));
442
443 return PSCI_E_SUCCESS;
444}
445#endif
446
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000447/*******************************************************************************
448 * PSCI top level handler for servicing SMCs.
449 ******************************************************************************/
Soby Mathewcf0b1492016-04-29 19:01:30 +0100450u_register_t psci_smc_handler(uint32_t smc_fid,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100451 u_register_t x1,
452 u_register_t x2,
453 u_register_t x3,
454 u_register_t x4,
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000455 void *cookie,
456 void *handle,
Soby Mathew4c0d0392016-06-16 14:52:04 +0100457 u_register_t flags)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000458{
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100459 u_register_t ret;
460
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100461 if (is_caller_secure(flags))
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100462 return (u_register_t)SMC_UNK;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000463
Soby Mathewb234b2c2015-01-15 11:49:49 +0000464 /* Check the fid against the capabilities */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100465 if ((psci_caps & define_psci_cap(smc_fid)) == 0U)
466 return (u_register_t)SMC_UNK;
Soby Mathewb234b2c2015-01-15 11:49:49 +0000467
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100468 if (((smc_fid >> FUNCID_CC_SHIFT) & FUNCID_CC_MASK) == SMC_32) {
469 /* 32-bit PSCI function, clear top parameter bits */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000470
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100471 uint32_t r1 = (uint32_t)x1;
472 uint32_t r2 = (uint32_t)x2;
473 uint32_t r3 = (uint32_t)x3;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000474
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100475 switch (smc_fid) {
476 case PSCI_VERSION:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100477 ret = (u_register_t)psci_version();
478 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000479
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100480 case PSCI_CPU_OFF:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100481 ret = (u_register_t)psci_cpu_off();
482 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000483
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100484 case PSCI_CPU_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100485 ret = (u_register_t)psci_cpu_suspend(r1, r2, r3);
486 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000487
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100488 case PSCI_CPU_ON_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100489 ret = (u_register_t)psci_cpu_on(r1, r2, r3);
490 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000491
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100492 case PSCI_AFFINITY_INFO_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100493 ret = (u_register_t)psci_affinity_info(r1, r2);
494 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000495
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100496 case PSCI_MIG_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100497 ret = (u_register_t)psci_migrate(r1);
498 break;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000499
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100500 case PSCI_MIG_INFO_TYPE:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100501 ret = (u_register_t)psci_migrate_info_type();
502 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100503
504 case PSCI_MIG_INFO_UP_CPU_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100505 ret = psci_migrate_info_up_cpu();
506 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100507
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100508 case PSCI_NODE_HW_STATE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100509 ret = (u_register_t)psci_node_hw_state(r1, r2);
510 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100511
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000512 case PSCI_SYSTEM_SUSPEND_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100513 ret = (u_register_t)psci_system_suspend(r1, r2);
514 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000515
Juan Castillod5f13092014-08-12 11:17:06 +0100516 case PSCI_SYSTEM_OFF:
517 psci_system_off();
518 /* We should never return from psci_system_off() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000519 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100520
521 case PSCI_SYSTEM_RESET:
522 psci_system_reset();
523 /* We should never return from psci_system_reset() */
Jonathan Wright3eacacc2018-03-13 17:45:42 +0000524 break;
Juan Castillod5f13092014-08-12 11:17:06 +0100525
Soby Mathew90e82582015-01-07 11:10:22 +0000526 case PSCI_FEATURES:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100527 ret = (u_register_t)psci_features(r1);
528 break;
Soby Mathew90e82582015-01-07 11:10:22 +0000529
Wing Lib88a4412022-09-14 13:18:15 -0700530#if PSCI_OS_INIT_MODE
531 case PSCI_SET_SUSPEND_MODE:
532 ret = (u_register_t)psci_set_suspend_mode(r1);
533 break;
534#endif
535
Yatharth Kochar170fb932016-05-09 18:26:35 +0100536#if ENABLE_PSCI_STAT
537 case PSCI_STAT_RESIDENCY_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100538 ret = psci_stat_residency(r1, r2);
539 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100540
541 case PSCI_STAT_COUNT_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100542 ret = psci_stat_count(r1, r2);
543 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100544#endif
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100545 case PSCI_MEM_PROTECT:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100546 ret = psci_mem_protect(r1);
547 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100548
549 case PSCI_MEM_CHK_RANGE_AARCH32:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100550 ret = psci_mem_chk_range(r1, r2);
551 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100552
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100553 case PSCI_SYSTEM_RESET2_AARCH32:
554 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100555 ret = psci_system_reset2(r1, r2);
556 break;
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100557
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100558 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100559 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
560 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100561 break;
562 }
563 } else {
564 /* 64-bit PSCI function */
565
566 switch (smc_fid) {
567 case PSCI_CPU_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100568 ret = (u_register_t)
569 psci_cpu_suspend((unsigned int)x1, x2, x3);
570 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100571
572 case PSCI_CPU_ON_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100573 ret = (u_register_t)psci_cpu_on(x1, x2, x3);
574 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100575
576 case PSCI_AFFINITY_INFO_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100577 ret = (u_register_t)
578 psci_affinity_info(x1, (unsigned int)x2);
579 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100580
581 case PSCI_MIG_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100582 ret = (u_register_t)psci_migrate(x1);
583 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100584
585 case PSCI_MIG_INFO_UP_CPU_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100586 ret = psci_migrate_info_up_cpu();
587 break;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100588
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100589 case PSCI_NODE_HW_STATE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100590 ret = (u_register_t)psci_node_hw_state(
591 x1, (unsigned int) x2);
592 break;
Jeenu Viswambharan28d3d612016-08-03 15:54:50 +0100593
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000594 case PSCI_SYSTEM_SUSPEND_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100595 ret = (u_register_t)psci_system_suspend(x1, x2);
596 break;
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000597
Yatharth Kochar170fb932016-05-09 18:26:35 +0100598#if ENABLE_PSCI_STAT
599 case PSCI_STAT_RESIDENCY_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100600 ret = psci_stat_residency(x1, (unsigned int) x2);
601 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100602
603 case PSCI_STAT_COUNT_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100604 ret = psci_stat_count(x1, (unsigned int) x2);
605 break;
Yatharth Kochar170fb932016-05-09 18:26:35 +0100606#endif
607
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100608 case PSCI_MEM_CHK_RANGE_AARCH64:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100609 ret = psci_mem_chk_range(x1, x2);
610 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100611
Roberto Vargas36a8f8f2017-07-26 09:23:09 +0100612 case PSCI_SYSTEM_RESET2_AARCH64:
613 /* We should never return from psci_system_reset2() */
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100614 ret = psci_system_reset2((uint32_t) x1, x2);
615 break;
Roberto Vargasd4c596b2017-08-03 08:16:16 +0100616
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100617 default:
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100618 WARN("Unimplemented PSCI Call: 0x%x\n", smc_fid);
619 ret = (u_register_t)SMC_UNK;
Andrew Thoelke5003eca2014-06-10 16:37:37 +0100620 break;
621 }
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000622 }
623
Antonio Nino Diaz6b7b0f32018-07-17 15:10:08 +0100624 return ret;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +0000625}