blob: 1021ef6ae9010db50dbb93ce294cdb8c2fdf39db [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00002 * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <assert.h>
8#include <string.h>
9
Dan Handley2bd4ef22014-04-09 13:14:54 +010010#include <arch.h>
Jayanth Dodderi Chidanand18d93792023-07-18 14:48:09 +010011#include <arch_features.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010012#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <common/bl_common.h>
14#include <common/debug.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010015#include <context.h>
Sandeep Tripathy12030042020-08-17 20:22:13 +053016#include <drivers/delay_timer.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <lib/el3_runtime/context_mgmt.h>
Jayanth Dodderi Chidanand18d93792023-07-18 14:48:09 +010018#include <lib/extensions/spe.h>
Boyan Karatotevb33206f2024-10-10 08:11:09 +010019#include <lib/pmf/pmf.h>
20#include <lib/runtime_instr.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <lib/utils.h>
22#include <plat/common/platform.h>
23
Dan Handley714a0d22014-04-09 13:13:04 +010024#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010025
Achin Gupta607084e2014-02-09 18:24:19 +000026/*
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000027 * SPD power management operations, expected to be supplied by the registered
28 * SPD on successful SP initialization
Achin Gupta607084e2014-02-09 18:24:19 +000029 */
Dan Handleye2712bc2014-04-10 15:37:22 +010030const spd_pm_ops_t *psci_spd_pm;
Achin Gupta607084e2014-02-09 18:24:19 +000031
Soby Mathew981487a2015-07-13 14:10:57 +010032/*
33 * PSCI requested local power state map. This array is used to store the local
34 * power states requested by a CPU for power levels from level 1 to
35 * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
36 * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
37 * CPU are the same.
38 *
39 * During state coordination, the platform is passed an array containing the
40 * local states requested for a particular non cpu power domain by each cpu
41 * within the domain.
42 *
43 * TODO: Dense packing of the requested states will cause cache thrashing
44 * when multiple power domains write to it. If we allocate the requested
45 * states at each power level in a cache-line aligned per-domain memory,
46 * the cache thrashing can be avoided.
47 */
48static plat_local_state_t
49 psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
50
Pankaj Gupta02c35682019-10-15 15:44:45 +053051unsigned int psci_plat_core_count;
Soby Mathew981487a2015-07-13 14:10:57 +010052
Achin Gupta4f6ad662013-10-25 09:08:21 +010053/*******************************************************************************
Soby Mathew981487a2015-07-13 14:10:57 +010054 * Arrays that hold the platform's power domain tree information for state
55 * management of power domains.
56 * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
57 * which is an ancestor of a CPU power domain.
58 * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
Achin Gupta4f6ad662013-10-25 09:08:21 +010059 ******************************************************************************/
Soby Mathew981487a2015-07-13 14:10:57 +010060non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
Soby Mathew2ae20432015-01-08 18:02:44 +000061#if USE_COHERENT_MEM
Chris Kay33bfc5e2023-02-14 11:30:04 +000062__section(".tzfw_coherent_mem")
Soby Mathew2ae20432015-01-08 18:02:44 +000063#endif
64;
Achin Gupta4f6ad662013-10-25 09:08:21 +010065
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +000066/* Lock for PSCI state coordination */
67DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
Andrew Thoelkee466c9f2015-09-10 11:39:36 +010068
Soby Mathew981487a2015-07-13 14:10:57 +010069cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
70
Achin Gupta4f6ad662013-10-25 09:08:21 +010071/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +010072 * Pointer to functions exported by the platform to complete power mgmt. ops
73 ******************************************************************************/
Soby Mathew981487a2015-07-13 14:10:57 +010074const plat_psci_ops_t *psci_plat_pm_ops;
Achin Gupta4f6ad662013-10-25 09:08:21 +010075
Soby Mathew981487a2015-07-13 14:10:57 +010076/******************************************************************************
77 * Check that the maximum power level supported by the platform makes sense
78 *****************************************************************************/
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +010079CASSERT((PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL) &&
80 (PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL),
81 assert_platform_max_pwrlvl_check);
Soby Mathew2b7de2b2015-02-12 14:45:02 +000082
Wing Li71f69df2022-09-14 13:18:15 -070083#if PSCI_OS_INIT_MODE
84/*******************************************************************************
85 * The power state coordination mode used in CPU_SUSPEND.
86 * Defaults to platform-coordinated mode.
87 ******************************************************************************/
88suspend_mode_t psci_suspend_mode = PLAT_COORD;
89#endif
90
Soby Mathew981487a2015-07-13 14:10:57 +010091/*
92 * The plat_local_state used by the platform is one of these types: RUN,
93 * RETENTION and OFF. The platform can define further sub-states for each type
94 * apart from RUN. This categorization is done to verify the sanity of the
95 * psci_power_state passed by the platform and to print debug information. The
96 * categorization is done on the basis of the following conditions:
97 *
98 * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
99 *
100 * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
101 * STATE_TYPE_RETN.
102 *
103 * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
104 * STATE_TYPE_OFF.
105 */
106typedef enum plat_local_state_type {
107 STATE_TYPE_RUN = 0,
108 STATE_TYPE_RETN,
109 STATE_TYPE_OFF
110} plat_local_state_type_t;
111
Antonio Nino Diaz5a42b682018-07-18 11:57:21 +0100112/* Function used to categorize plat_local_state. */
113static plat_local_state_type_t find_local_state_type(plat_local_state_t state)
114{
115 if (state != 0U) {
116 if (state > PLAT_MAX_RET_STATE) {
117 return STATE_TYPE_OFF;
118 } else {
119 return STATE_TYPE_RETN;
120 }
121 } else {
122 return STATE_TYPE_RUN;
123 }
124}
Soby Mathew981487a2015-07-13 14:10:57 +0100125
126/******************************************************************************
127 * Check that the maximum retention level supported by the platform is less
128 * than the maximum off level.
129 *****************************************************************************/
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100130CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE,
Soby Mathew981487a2015-07-13 14:10:57 +0100131 assert_platform_max_off_and_retn_state_check);
132
133/******************************************************************************
134 * This function ensures that the power state parameter in a CPU_SUSPEND request
135 * is valid. If so, it returns the requested states for each power level.
136 *****************************************************************************/
137int psci_validate_power_state(unsigned int power_state,
138 psci_power_state_t *state_info)
Achin Guptaf6b9e992014-07-31 11:19:11 +0100139{
Soby Mathew981487a2015-07-13 14:10:57 +0100140 /* Check SBZ bits in power state are zero */
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530141 if (psci_check_power_state(power_state) != 0U) {
Soby Mathew981487a2015-07-13 14:10:57 +0100142 return PSCI_E_INVALID_PARAMS;
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530143 }
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100144 assert(psci_plat_pm_ops->validate_power_state != NULL);
Achin Guptaf6b9e992014-07-31 11:19:11 +0100145
Soby Mathew981487a2015-07-13 14:10:57 +0100146 /* Validate the power_state using platform pm_ops */
147 return psci_plat_pm_ops->validate_power_state(power_state, state_info);
148}
Achin Guptaf6b9e992014-07-31 11:19:11 +0100149
Soby Mathew981487a2015-07-13 14:10:57 +0100150/******************************************************************************
151 * This function retrieves the `psci_power_state_t` for system suspend from
152 * the platform.
153 *****************************************************************************/
154void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
155{
156 /*
157 * Assert that the required pm_ops hook is implemented to ensure that
158 * the capability detected during psci_setup() is valid.
159 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100160 assert(psci_plat_pm_ops->get_sys_suspend_power_state != NULL);
Soby Mathew981487a2015-07-13 14:10:57 +0100161
162 /*
163 * Query the platform for the power_state required for system suspend
164 */
165 psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
Achin Guptaf6b9e992014-07-31 11:19:11 +0100166}
167
Wing Li2c556f32022-09-14 13:18:17 -0700168#if PSCI_OS_INIT_MODE
169/*******************************************************************************
170 * This function verifies that all the other cores at the 'end_pwrlvl' have been
171 * idled and the current CPU is the last running CPU at the 'end_pwrlvl'.
172 * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
173 * otherwise.
174 ******************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000175static bool psci_is_last_cpu_to_idle_at_pwrlvl(unsigned int my_idx, unsigned int end_pwrlvl)
Wing Li2c556f32022-09-14 13:18:17 -0700176{
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000177 unsigned int lvl;
Mark Dykese1594ec2024-04-08 13:38:01 -0500178 unsigned int parent_idx = 0;
Wing Li2c556f32022-09-14 13:18:17 -0700179 unsigned int cpu_start_idx, ncpus, cpu_idx;
180 plat_local_state_t local_state;
181
182 if (end_pwrlvl == PSCI_CPU_PWR_LVL) {
183 return true;
184 }
185
Charlie Bareham9c801042023-10-17 20:17:58 +0200186 parent_idx = psci_cpu_pd_nodes[my_idx].parent_node;
187 for (lvl = PSCI_CPU_PWR_LVL + U(1); lvl < end_pwrlvl; lvl++) {
188 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
Wing Li2c556f32022-09-14 13:18:17 -0700189 }
190
191 cpu_start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
192 ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
193
194 for (cpu_idx = cpu_start_idx; cpu_idx < cpu_start_idx + ncpus;
195 cpu_idx++) {
196 local_state = psci_get_cpu_local_state_by_idx(cpu_idx);
197 if (cpu_idx == my_idx) {
198 assert(is_local_state_run(local_state) != 0);
199 continue;
200 }
201
202 if (is_local_state_run(local_state) != 0) {
203 return false;
204 }
205 }
206
207 return true;
208}
209#endif
210
Achin Guptaf6b9e992014-07-31 11:19:11 +0100211/*******************************************************************************
Wing Li71f69df2022-09-14 13:18:15 -0700212 * This function verifies that all the other cores in the system have been
Soby Mathew96168382014-12-17 14:47:57 +0000213 * turned OFF and the current CPU is the last running CPU in the system.
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +0100214 * Returns true, if the current CPU is the last ON CPU or false otherwise.
Soby Mathew96168382014-12-17 14:47:57 +0000215 ******************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000216bool psci_is_last_on_cpu(unsigned int my_idx)
Soby Mathew96168382014-12-17 14:47:57 +0000217{
Manish V Badarkhed55c23f2025-05-29 14:55:39 +0100218 for (unsigned int cpu_idx = 0U; cpu_idx < psci_plat_core_count; cpu_idx++) {
Soby Mathew981487a2015-07-13 14:10:57 +0100219 if (cpu_idx == my_idx) {
220 assert(psci_get_aff_info_state() == AFF_STATE_ON);
Soby Mathew96168382014-12-17 14:47:57 +0000221 continue;
222 }
223
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +0100224 if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF) {
225 VERBOSE("core=%u other than current core=%u %s\n",
226 cpu_idx, my_idx, "running in the system");
227 return false;
228 }
Soby Mathew96168382014-12-17 14:47:57 +0000229 }
230
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +0100231 return true;
Soby Mathew96168382014-12-17 14:47:57 +0000232}
233
234/*******************************************************************************
Wing Li71f69df2022-09-14 13:18:15 -0700235 * This function verifies that all cores in the system have been turned ON.
236 * Returns true, if all CPUs are ON or false otherwise.
237 ******************************************************************************/
238static bool psci_are_all_cpus_on(void)
239{
240 unsigned int cpu_idx;
241
Manish V Badarkhed55c23f2025-05-29 14:55:39 +0100242 for (cpu_idx = 0U; cpu_idx < psci_plat_core_count; cpu_idx++) {
Wing Li71f69df2022-09-14 13:18:15 -0700243 if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_OFF) {
244 return false;
245 }
246 }
247
248 return true;
249}
250
251/*******************************************************************************
Manish V Badarkhed55c23f2025-05-29 14:55:39 +0100252 * Counts the number of CPUs in the system that are currently in the ON or
253 * ON_PENDING state.
254 *
255 * @note This function does not acquire any power domain locks. It must only be
256 * called in contexts where it is guaranteed that PSCI state transitions
257 * are not concurrently happening, or where locks are already held.
258 *
259 * @return The number of CPUs currently in AFF_STATE_ON or AFF_STATE_ON_PENDING.
260 ******************************************************************************/
261static unsigned int psci_num_cpus_running(void)
262{
263 unsigned int cpu_idx;
264 unsigned int no_of_cpus = 0U;
265 aff_info_state_t aff_state;
266
267 for (cpu_idx = 0U; cpu_idx < psci_plat_core_count; cpu_idx++) {
268 aff_state = psci_get_aff_info_state_by_idx(cpu_idx);
269 if (aff_state == AFF_STATE_ON ||
270 aff_state == AFF_STATE_ON_PENDING) {
271 no_of_cpus++;
272 }
273 }
274
275 return no_of_cpus;
276}
277
278/*******************************************************************************
Soby Mathew981487a2015-07-13 14:10:57 +0100279 * Routine to return the maximum power level to traverse to after a cpu has
280 * been physically powered up. It is expected to be called immediately after
281 * reset from assembler code.
Achin Guptaf6b9e992014-07-31 11:19:11 +0100282 ******************************************************************************/
Soby Mathew011ca182015-07-29 17:05:03 +0100283static unsigned int get_power_on_target_pwrlvl(void)
Achin Guptaf6b9e992014-07-31 11:19:11 +0100284{
Soby Mathew011ca182015-07-29 17:05:03 +0100285 unsigned int pwrlvl;
Achin Guptaf6b9e992014-07-31 11:19:11 +0100286
287 /*
Soby Mathew981487a2015-07-13 14:10:57 +0100288 * Assume that this cpu was suspended and retrieve its target power
Boyan Karatotevd8dfe512024-09-30 11:31:55 +0100289 * level. If it wasn't, the cpu is off so this will be PLAT_MAX_PWR_LVL.
Achin Guptaf6b9e992014-07-31 11:19:11 +0100290 */
Soby Mathew981487a2015-07-13 14:10:57 +0100291 pwrlvl = psci_get_suspend_pwrlvl();
Deepika Bhavnani523024c2019-08-17 01:10:02 +0300292 assert(pwrlvl < PSCI_INVALID_PWR_LVL);
Soby Mathew981487a2015-07-13 14:10:57 +0100293 return pwrlvl;
Achin Guptaf6b9e992014-07-31 11:19:11 +0100294}
295
Soby Mathew981487a2015-07-13 14:10:57 +0100296/******************************************************************************
297 * Helper function to update the requested local power state array. This array
298 * does not store the requested state for the CPU power level. Hence an
Deepika Bhavnani6bd46662019-08-15 00:56:46 +0300299 * assertion is added to prevent us from accessing the CPU power level.
Soby Mathew981487a2015-07-13 14:10:57 +0100300 *****************************************************************************/
301static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
302 unsigned int cpu_idx,
303 plat_local_state_t req_pwr_state)
Achin Guptaf6b9e992014-07-31 11:19:11 +0100304{
Soby Mathew981487a2015-07-13 14:10:57 +0100305 assert(pwrlvl > PSCI_CPU_PWR_LVL);
Deepika Bhavnani6bd46662019-08-15 00:56:46 +0300306 if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
Pankaj Gupta02c35682019-10-15 15:44:45 +0530307 (cpu_idx < psci_plat_core_count)) {
Deepika Bhavnani6bd46662019-08-15 00:56:46 +0300308 psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx] = req_pwr_state;
309 }
Achin Guptaf6b9e992014-07-31 11:19:11 +0100310}
311
Soby Mathew981487a2015-07-13 14:10:57 +0100312/******************************************************************************
313 * This function initializes the psci_req_local_pwr_states.
314 *****************************************************************************/
Daniel Boulby5753e492018-09-20 14:12:46 +0100315void __init psci_init_req_local_pwr_states(void)
Achin Guptaa45e3972013-12-05 15:10:48 +0000316{
Soby Mathew981487a2015-07-13 14:10:57 +0100317 /* Initialize the requested state of all non CPU power domains as OFF */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100318 unsigned int pwrlvl;
Pankaj Gupta02c35682019-10-15 15:44:45 +0530319 unsigned int core;
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100320
321 for (pwrlvl = 0U; pwrlvl < PLAT_MAX_PWR_LVL; pwrlvl++) {
Pankaj Gupta02c35682019-10-15 15:44:45 +0530322 for (core = 0; core < psci_plat_core_count; core++) {
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100323 psci_req_local_pwr_states[pwrlvl][core] =
324 PLAT_MAX_OFF_STATE;
325 }
326 }
Soby Mathew981487a2015-07-13 14:10:57 +0100327}
Achin Guptaa45e3972013-12-05 15:10:48 +0000328
Soby Mathew981487a2015-07-13 14:10:57 +0100329/******************************************************************************
330 * Helper function to return a reference to an array containing the local power
331 * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
332 * array will be the number of cpu power domains of which this power domain is
333 * an ancestor. These requested states will be used to determine a suitable
334 * target state for this power domain during psci state coordination. An
335 * assertion is added to prevent us from accessing the CPU power level.
336 *****************************************************************************/
Soby Mathew011ca182015-07-29 17:05:03 +0100337static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
Deepika Bhavnani79ffab52019-08-27 00:32:24 +0300338 unsigned int cpu_idx)
Soby Mathew981487a2015-07-13 14:10:57 +0100339{
340 assert(pwrlvl > PSCI_CPU_PWR_LVL);
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100341
Deepika Bhavnani6bd46662019-08-15 00:56:46 +0300342 if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
Pankaj Gupta02c35682019-10-15 15:44:45 +0530343 (cpu_idx < psci_plat_core_count)) {
Deepika Bhavnani6bd46662019-08-15 00:56:46 +0300344 return &psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx];
345 } else
346 return NULL;
Soby Mathew981487a2015-07-13 14:10:57 +0100347}
Achin Guptaa45e3972013-12-05 15:10:48 +0000348
Wing Li2c556f32022-09-14 13:18:17 -0700349#if PSCI_OS_INIT_MODE
350/******************************************************************************
351 * Helper function to save a copy of the psci_req_local_pwr_states (prev) for a
352 * CPU (cpu_idx), and update psci_req_local_pwr_states with the new requested
353 * local power states (state_info).
354 *****************************************************************************/
355void psci_update_req_local_pwr_states(unsigned int end_pwrlvl,
356 unsigned int cpu_idx,
357 psci_power_state_t *state_info,
358 plat_local_state_t *prev)
359{
360 unsigned int lvl;
361#ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
362 unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
363#else
364 unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
365#endif
366 plat_local_state_t req_state;
367
368 for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
369 /* Save the previous requested local power state */
370 prev[lvl - 1U] = *psci_get_req_local_pwr_states(lvl, cpu_idx);
371
372 /* Update the new requested local power state */
373 if (lvl <= end_pwrlvl) {
374 req_state = state_info->pwr_domain_state[lvl];
375 } else {
376 req_state = state_info->pwr_domain_state[end_pwrlvl];
377 }
378 psci_set_req_local_pwr_state(lvl, cpu_idx, req_state);
379 }
380}
381
382/******************************************************************************
383 * Helper function to restore the previously saved requested local power states
384 * (prev) for a CPU (cpu_idx) to psci_req_local_pwr_states.
385 *****************************************************************************/
386void psci_restore_req_local_pwr_states(unsigned int cpu_idx,
387 plat_local_state_t *prev)
388{
389 unsigned int lvl;
390#ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
391 unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
392#else
393 unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
394#endif
395
396 for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
397 /* Restore the previous requested local power state */
398 psci_set_req_local_pwr_state(lvl, cpu_idx, prev[lvl - 1U]);
399 }
400}
401#endif
402
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000403/*
404 * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
405 * memory.
406 *
407 * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
408 * it's accessed by both cached and non-cached participants. To serve the common
409 * minimum, perform a cache flush before read and after write so that non-cached
410 * participants operate on latest data in main memory.
411 *
412 * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
413 * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
414 * In both cases, no cache operations are required.
415 */
416
417/*
418 * Retrieve local state of non-CPU power domain node from a non-cached CPU,
419 * after any required cache maintenance operation.
420 */
421static plat_local_state_t get_non_cpu_pd_node_local_state(
422 unsigned int parent_idx)
423{
Andrew F. Davise6f28fa2018-08-30 12:13:57 -0500424#if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000425 flush_dcache_range(
426 (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
427 sizeof(psci_non_cpu_pd_nodes[parent_idx]));
428#endif
429 return psci_non_cpu_pd_nodes[parent_idx].local_state;
430}
431
432/*
433 * Update local state of non-CPU power domain node from a cached CPU; perform
434 * any required cache maintenance operation afterwards.
435 */
436static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
437 plat_local_state_t state)
438{
439 psci_non_cpu_pd_nodes[parent_idx].local_state = state;
Andrew F. Davise6f28fa2018-08-30 12:13:57 -0500440#if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000441 flush_dcache_range(
442 (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
443 sizeof(psci_non_cpu_pd_nodes[parent_idx]));
444#endif
445}
446
Soby Mathew981487a2015-07-13 14:10:57 +0100447/******************************************************************************
448 * Helper function to return the current local power state of each power domain
449 * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
450 * function will be called after a cpu is powered on to find the local state
451 * each power domain has emerged from.
452 *****************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000453void psci_get_target_local_pwr_states(unsigned int cpu_idx, unsigned int end_pwrlvl,
Achin Gupta9b2bf252016-06-28 16:46:15 +0100454 psci_power_state_t *target_state)
Soby Mathew981487a2015-07-13 14:10:57 +0100455{
Soby Mathew011ca182015-07-29 17:05:03 +0100456 unsigned int parent_idx, lvl;
Soby Mathew981487a2015-07-13 14:10:57 +0100457 plat_local_state_t *pd_state = target_state->pwr_domain_state;
458
459 pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000460 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
Soby Mathew981487a2015-07-13 14:10:57 +0100461
462 /* Copy the local power state from node to state_info */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100463 for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000464 pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
Soby Mathew981487a2015-07-13 14:10:57 +0100465 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
466 }
467
468 /* Set the the higher levels to RUN */
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530469 for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
Soby Mathew981487a2015-07-13 14:10:57 +0100470 target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530471 }
Soby Mathew981487a2015-07-13 14:10:57 +0100472}
473
474/******************************************************************************
475 * Helper function to set the target local power state that each power domain
476 * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
477 * enter. This function will be called after coordination of requested power
478 * states has been done for each power level.
479 *****************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000480void psci_set_target_local_pwr_states(unsigned int cpu_idx, unsigned int end_pwrlvl,
Wing Lic0dc6392023-05-04 08:31:19 -0700481 const psci_power_state_t *target_state)
Soby Mathew981487a2015-07-13 14:10:57 +0100482{
Soby Mathew011ca182015-07-29 17:05:03 +0100483 unsigned int parent_idx, lvl;
Soby Mathew981487a2015-07-13 14:10:57 +0100484 const plat_local_state_t *pd_state = target_state->pwr_domain_state;
485
486 psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
Achin Guptaa45e3972013-12-05 15:10:48 +0000487
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100488 /*
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000489 * Need to flush as local_state might be accessed with Data Cache
Soby Mathew981487a2015-07-13 14:10:57 +0100490 * disabled during power on
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100491 */
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000492 psci_flush_cpu_data(psci_svc_cpu_data.local_state);
Soby Mathew981487a2015-07-13 14:10:57 +0100493
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000494 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
Soby Mathew981487a2015-07-13 14:10:57 +0100495
496 /* Copy the local_state from state_info */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100497 for (lvl = 1U; lvl <= end_pwrlvl; lvl++) {
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000498 set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
Soby Mathew981487a2015-07-13 14:10:57 +0100499 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
500 }
Achin Guptaa45e3972013-12-05 15:10:48 +0000501}
502
503/*******************************************************************************
Soby Mathew981487a2015-07-13 14:10:57 +0100504 * PSCI helper function to get the parent nodes corresponding to a cpu_index.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100505 ******************************************************************************/
Deepika Bhavnani79ffab52019-08-27 00:32:24 +0300506void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
Soby Mathew011ca182015-07-29 17:05:03 +0100507 unsigned int end_lvl,
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100508 unsigned int *node_index)
Soby Mathew981487a2015-07-13 14:10:57 +0100509{
510 unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node;
Varun Wadekar66231d12017-06-07 09:57:42 -0700511 unsigned int i;
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100512 unsigned int *node = node_index;
Soby Mathew981487a2015-07-13 14:10:57 +0100513
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100514 for (i = PSCI_CPU_PWR_LVL + 1U; i <= end_lvl; i++) {
515 *node = parent_node;
516 node++;
Soby Mathew981487a2015-07-13 14:10:57 +0100517 parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
518 }
519}
520
521/******************************************************************************
522 * This function is invoked post CPU power up and initialization. It sets the
523 * affinity info state, target power state and requested power state for the
524 * current CPU and all its ancestor power domains to RUN.
525 *****************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000526void psci_set_pwr_domains_to_run(unsigned int cpu_idx, unsigned int end_pwrlvl)
Soby Mathew981487a2015-07-13 14:10:57 +0100527{
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000528 unsigned int parent_idx, lvl;
Soby Mathew981487a2015-07-13 14:10:57 +0100529 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
530
531 /* Reset the local_state to RUN for the non cpu power domains. */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100532 for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000533 set_non_cpu_pd_node_local_state(parent_idx,
534 PSCI_LOCAL_STATE_RUN);
Soby Mathew981487a2015-07-13 14:10:57 +0100535 psci_set_req_local_pwr_state(lvl,
536 cpu_idx,
537 PSCI_LOCAL_STATE_RUN);
538 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
539 }
540
541 /* Set the affinity info state to ON */
542 psci_set_aff_info_state(AFF_STATE_ON);
543
544 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
Jeenu Viswambharan0b56d6f2017-01-06 14:58:11 +0000545 psci_flush_cpu_data(psci_svc_cpu_data);
Soby Mathew981487a2015-07-13 14:10:57 +0100546}
547
548/******************************************************************************
Wing Li2c556f32022-09-14 13:18:17 -0700549 * This function is used in platform-coordinated mode.
550 *
Soby Mathew981487a2015-07-13 14:10:57 +0100551 * This function is passed the local power states requested for each power
552 * domain (state_info) between the current CPU domain and its ancestors until
553 * the target power level (end_pwrlvl). It updates the array of requested power
554 * states with this information.
555 *
556 * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
557 * retrieves the states requested by all the cpus of which the power domain at
558 * that level is an ancestor. It passes this information to the platform to
559 * coordinate and return the target power state. If the target state for a level
560 * is RUN then subsequent levels are not considered. At the CPU level, state
561 * coordination is not required. Hence, the requested and the target states are
562 * the same.
563 *
564 * The 'state_info' is updated with the target state for each level between the
565 * CPU and the 'end_pwrlvl' and returned to the caller.
566 *
567 * This function will only be invoked with data cache enabled and while
568 * powering down a core.
569 *****************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000570void psci_do_state_coordination(unsigned int cpu_idx, unsigned int end_pwrlvl,
Soby Mathew011ca182015-07-29 17:05:03 +0100571 psci_power_state_t *state_info)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100572{
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000573 unsigned int lvl, parent_idx;
Deepika Bhavnani79ffab52019-08-27 00:32:24 +0300574 unsigned int start_idx;
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100575 unsigned int ncpus;
Nithin G8d7d93c2024-04-19 18:06:36 +0530576 plat_local_state_t target_state;
Soby Mathew981487a2015-07-13 14:10:57 +0100577
Soby Mathew1298e692016-02-02 14:23:10 +0000578 assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
Soby Mathew981487a2015-07-13 14:10:57 +0100579 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
580
581 /* For level 0, the requested state will be equivalent
582 to target state */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100583 for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
Soby Mathew981487a2015-07-13 14:10:57 +0100584
585 /* First update the requested power state */
586 psci_set_req_local_pwr_state(lvl, cpu_idx,
587 state_info->pwr_domain_state[lvl]);
588
589 /* Get the requested power states for this power level */
590 start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
Nithin G8d7d93c2024-04-19 18:06:36 +0530591 plat_local_state_t const *req_states = psci_get_req_local_pwr_states(lvl,
592 start_idx);
Soby Mathew981487a2015-07-13 14:10:57 +0100593
594 /*
595 * Let the platform coordinate amongst the requested states at
596 * this power level and return the target local power state.
597 */
598 ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
599 target_state = plat_get_target_pwr_state(lvl,
600 req_states,
601 ncpus);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100602
Soby Mathew981487a2015-07-13 14:10:57 +0100603 state_info->pwr_domain_state[lvl] = target_state;
604
605 /* Break early if the negotiated target power state is RUN */
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530606 if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0) {
Soby Mathew981487a2015-07-13 14:10:57 +0100607 break;
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530608 }
Soby Mathew981487a2015-07-13 14:10:57 +0100609
610 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
611 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100612
613 /*
Soby Mathew981487a2015-07-13 14:10:57 +0100614 * This is for cases when we break out of the above loop early because
615 * the target power state is RUN at a power level < end_pwlvl.
616 * We update the requested power state from state_info and then
617 * set the target state as RUN.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100618 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100619 for (lvl = lvl + 1U; lvl <= end_pwrlvl; lvl++) {
Soby Mathew981487a2015-07-13 14:10:57 +0100620 psci_set_req_local_pwr_state(lvl, cpu_idx,
621 state_info->pwr_domain_state[lvl]);
622 state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100623
Wing Li2c556f32022-09-14 13:18:17 -0700624 }
Wing Li2c556f32022-09-14 13:18:17 -0700625}
626
627#if PSCI_OS_INIT_MODE
628/******************************************************************************
629 * This function is used in OS-initiated mode.
630 *
631 * This function is passed the local power states requested for each power
632 * domain (state_info) between the current CPU domain and its ancestors until
633 * the target power level (end_pwrlvl), and ensures the requested power states
634 * are valid. It updates the array of requested power states with this
635 * information.
636 *
637 * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
638 * retrieves the states requested by all the cpus of which the power domain at
639 * that level is an ancestor. It passes this information to the platform to
640 * coordinate and return the target power state. If the requested state does
641 * not match the target state, the request is denied.
642 *
643 * The 'state_info' is not modified.
644 *
645 * This function will only be invoked with data cache enabled and while
646 * powering down a core.
647 *****************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000648int psci_validate_state_coordination(unsigned int cpu_idx, unsigned int end_pwrlvl,
Wing Li2c556f32022-09-14 13:18:17 -0700649 psci_power_state_t *state_info)
650{
651 int rc = PSCI_E_SUCCESS;
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000652 unsigned int lvl, parent_idx;
Wing Li2c556f32022-09-14 13:18:17 -0700653 unsigned int start_idx;
654 unsigned int ncpus;
655 plat_local_state_t target_state, *req_states;
656 plat_local_state_t prev[PLAT_MAX_PWR_LVL];
657
658 assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
659 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
660
661 /*
662 * Save a copy of the previous requested local power states and update
663 * the new requested local power states.
664 */
665 psci_update_req_local_pwr_states(end_pwrlvl, cpu_idx, state_info, prev);
666
667 for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
668 /* Get the requested power states for this power level */
669 start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
670 req_states = psci_get_req_local_pwr_states(lvl, start_idx);
671
672 /*
673 * Let the platform coordinate amongst the requested states at
674 * this power level and return the target local power state.
675 */
676 ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
677 target_state = plat_get_target_pwr_state(lvl,
678 req_states,
679 ncpus);
680
681 /*
682 * Verify that the requested power state matches the target
683 * local power state.
684 */
685 if (state_info->pwr_domain_state[lvl] != target_state) {
686 if (target_state == PSCI_LOCAL_STATE_RUN) {
687 rc = PSCI_E_DENIED;
688 } else {
689 rc = PSCI_E_INVALID_PARAMS;
690 }
691 goto exit;
692 }
Patrick Delaunayadf42d32023-10-17 20:05:52 +0200693
694 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
Wing Li2c556f32022-09-14 13:18:17 -0700695 }
696
697 /*
698 * Verify that the current core is the last running core at the
699 * specified power level.
700 */
701 lvl = state_info->last_at_pwrlvl;
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +0000702 if (!psci_is_last_cpu_to_idle_at_pwrlvl(cpu_idx, lvl)) {
Wing Li2c556f32022-09-14 13:18:17 -0700703 rc = PSCI_E_DENIED;
704 }
705
706exit:
707 if (rc != PSCI_E_SUCCESS) {
708 /* Restore the previous requested local power states. */
709 psci_restore_req_local_pwr_states(cpu_idx, prev);
710 return rc;
Soby Mathew981487a2015-07-13 14:10:57 +0100711 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100712
Wing Li2c556f32022-09-14 13:18:17 -0700713 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100714}
Wing Li2c556f32022-09-14 13:18:17 -0700715#endif
Achin Gupta4f6ad662013-10-25 09:08:21 +0100716
Soby Mathew981487a2015-07-13 14:10:57 +0100717/******************************************************************************
718 * This function validates a suspend request by making sure that if a standby
719 * state is requested then no power level is turned off and the highest power
720 * level is placed in a standby/retention state.
721 *
722 * It also ensures that the state level X will enter is not shallower than the
723 * state level X + 1 will enter.
724 *
725 * This validation will be enabled only for DEBUG builds as the platform is
726 * expected to perform these validations as well.
727 *****************************************************************************/
728int psci_validate_suspend_req(const psci_power_state_t *state_info,
729 unsigned int is_power_down_state)
Achin Gupta0959db52013-12-02 17:33:04 +0000730{
Soby Mathew981487a2015-07-13 14:10:57 +0100731 unsigned int max_off_lvl, target_lvl, max_retn_lvl;
732 plat_local_state_t state;
733 plat_local_state_type_t req_state_type, deepest_state_type;
734 int i;
Achin Gupta0959db52013-12-02 17:33:04 +0000735
Soby Mathew981487a2015-07-13 14:10:57 +0100736 /* Find the target suspend power level */
737 target_lvl = psci_find_target_suspend_lvl(state_info);
Soby Mathew011ca182015-07-29 17:05:03 +0100738 if (target_lvl == PSCI_INVALID_PWR_LVL)
Achin Gupta0959db52013-12-02 17:33:04 +0000739 return PSCI_E_INVALID_PARAMS;
740
Soby Mathew981487a2015-07-13 14:10:57 +0100741 /* All power domain levels are in a RUN state to begin with */
742 deepest_state_type = STATE_TYPE_RUN;
743
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100744 for (i = (int) target_lvl; i >= (int) PSCI_CPU_PWR_LVL; i--) {
Soby Mathew981487a2015-07-13 14:10:57 +0100745 state = state_info->pwr_domain_state[i];
746 req_state_type = find_local_state_type(state);
747
748 /*
749 * While traversing from the highest power level to the lowest,
750 * the state requested for lower levels has to be the same or
751 * deeper i.e. equal to or greater than the state at the higher
752 * levels. If this condition is true, then the requested state
753 * becomes the deepest state encountered so far.
754 */
755 if (req_state_type < deepest_state_type)
756 return PSCI_E_INVALID_PARAMS;
757 deepest_state_type = req_state_type;
758 }
759
760 /* Find the highest off power level */
761 max_off_lvl = psci_find_max_off_lvl(state_info);
762
763 /* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
Soby Mathew011ca182015-07-29 17:05:03 +0100764 max_retn_lvl = PSCI_INVALID_PWR_LVL;
Soby Mathew981487a2015-07-13 14:10:57 +0100765 if (target_lvl != max_off_lvl)
766 max_retn_lvl = target_lvl;
767
768 /*
769 * If this is not a request for a power down state then max off level
770 * has to be invalid and max retention level has to be a valid power
771 * level.
772 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100773 if ((is_power_down_state == 0U) &&
774 ((max_off_lvl != PSCI_INVALID_PWR_LVL) ||
775 (max_retn_lvl == PSCI_INVALID_PWR_LVL)))
Achin Gupta0959db52013-12-02 17:33:04 +0000776 return PSCI_E_INVALID_PARAMS;
777
778 return PSCI_E_SUCCESS;
779}
780
Soby Mathew981487a2015-07-13 14:10:57 +0100781/******************************************************************************
782 * This function finds the highest power level which will be powered down
783 * amongst all the power levels specified in the 'state_info' structure
784 *****************************************************************************/
785unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
Achin Guptacab78e42014-07-28 00:09:01 +0100786{
Soby Mathew981487a2015-07-13 14:10:57 +0100787 int i;
Achin Guptacab78e42014-07-28 00:09:01 +0100788
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100789 for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530790 if (is_local_state_off(state_info->pwr_domain_state[i]) != 0) {
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100791 return (unsigned int) i;
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530792 }
Soby Mathew981487a2015-07-13 14:10:57 +0100793 }
794
Soby Mathew011ca182015-07-29 17:05:03 +0100795 return PSCI_INVALID_PWR_LVL;
Soby Mathew981487a2015-07-13 14:10:57 +0100796}
797
798/******************************************************************************
799 * This functions finds the level of the highest power domain which will be
800 * placed in a low power state during a suspend operation.
801 *****************************************************************************/
802unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
803{
804 int i;
805
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100806 for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
807 if (is_local_state_run(state_info->pwr_domain_state[i]) == 0)
808 return (unsigned int) i;
Achin Guptacab78e42014-07-28 00:09:01 +0100809 }
Soby Mathew981487a2015-07-13 14:10:57 +0100810
Soby Mathew011ca182015-07-29 17:05:03 +0100811 return PSCI_INVALID_PWR_LVL;
Achin Guptacab78e42014-07-28 00:09:01 +0100812}
813
814/*******************************************************************************
Andrew F. Davis74e89782019-06-04 10:46:54 -0400815 * This function is passed the highest level in the topology tree that the
816 * operation should be applied to and a list of node indexes. It picks up locks
817 * from the node index list in order of increasing power domain level in the
818 * range specified.
Achin Gupta0959db52013-12-02 17:33:04 +0000819 ******************************************************************************/
Andrew F. Davis74e89782019-06-04 10:46:54 -0400820void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
821 const unsigned int *parent_nodes)
Achin Gupta0959db52013-12-02 17:33:04 +0000822{
Andrew F. Davis74e89782019-06-04 10:46:54 -0400823 unsigned int parent_idx;
Soby Mathew011ca182015-07-29 17:05:03 +0100824 unsigned int level;
Achin Gupta0959db52013-12-02 17:33:04 +0000825
Soby Mathew981487a2015-07-13 14:10:57 +0100826 /* No locking required for level 0. Hence start locking from level 1 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100827 for (level = PSCI_CPU_PWR_LVL + 1U; level <= end_pwrlvl; level++) {
Andrew F. Davis74e89782019-06-04 10:46:54 -0400828 parent_idx = parent_nodes[level - 1U];
Soby Mathew981487a2015-07-13 14:10:57 +0100829 psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
Achin Gupta0959db52013-12-02 17:33:04 +0000830 }
831}
832
833/*******************************************************************************
Andrew F. Davis74e89782019-06-04 10:46:54 -0400834 * This function is passed the highest level in the topology tree that the
835 * operation should be applied to and a list of node indexes. It releases the
836 * locks in order of decreasing power domain level in the range specified.
Achin Gupta0959db52013-12-02 17:33:04 +0000837 ******************************************************************************/
Andrew F. Davis74e89782019-06-04 10:46:54 -0400838void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
839 const unsigned int *parent_nodes)
Achin Gupta0959db52013-12-02 17:33:04 +0000840{
Andrew F. Davis74e89782019-06-04 10:46:54 -0400841 unsigned int parent_idx;
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100842 unsigned int level;
Achin Gupta0959db52013-12-02 17:33:04 +0000843
Soby Mathew981487a2015-07-13 14:10:57 +0100844 /* Unlock top down. No unlocking required for level 0. */
Zelalem91d80612020-02-12 10:37:03 -0600845 for (level = end_pwrlvl; level >= (PSCI_CPU_PWR_LVL + 1U); level--) {
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100846 parent_idx = parent_nodes[level - 1U];
Soby Mathew981487a2015-07-13 14:10:57 +0100847 psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
Achin Gupta0959db52013-12-02 17:33:04 +0000848 }
849}
850
851/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +0100852 * This function determines the full entrypoint information for the requested
Soby Mathew8595b872015-01-06 15:36:38 +0000853 * PSCI entrypoint on power on/resume and returns it.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100854 ******************************************************************************/
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700855#ifdef __aarch64__
Soby Mathewf1f97a12015-07-15 12:13:26 +0100856static int psci_get_ns_ep_info(entry_point_info_t *ep,
Soby Mathew011ca182015-07-29 17:05:03 +0100857 uintptr_t entrypoint,
858 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100859{
Soby Mathewa0fedc42016-06-16 14:52:04 +0100860 u_register_t ep_attr, sctlr;
Soby Mathew011ca182015-07-29 17:05:03 +0100861 unsigned int daif, ee, mode;
Soby Mathewa0fedc42016-06-16 14:52:04 +0100862 u_register_t ns_scr_el3 = read_scr_el3();
863 u_register_t ns_sctlr_el1 = read_sctlr_el1();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100864
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100865 sctlr = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
866 read_sctlr_el2() : ns_sctlr_el1;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100867 ee = 0;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100868
Andrew Thoelke4e126072014-06-04 21:10:52 +0100869 ep_attr = NON_SECURE | EP_ST_DISABLE;
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100870 if ((sctlr & SCTLR_EE_BIT) != 0U) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100871 ep_attr |= EP_EE_BIG;
872 ee = 1;
873 }
Soby Mathew8595b872015-01-06 15:36:38 +0000874 SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100875
Soby Mathew8595b872015-01-06 15:36:38 +0000876 ep->pc = entrypoint;
Douglas Raillarda8954fc2017-01-26 15:54:44 +0000877 zeromem(&ep->args, sizeof(ep->args));
Soby Mathew8595b872015-01-06 15:36:38 +0000878 ep->args.arg0 = context_id;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100879
880 /*
881 * Figure out whether the cpu enters the non-secure address space
882 * in aarch32 or aarch64
883 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100884 if ((ns_scr_el3 & SCR_RW_BIT) != 0U) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100885
886 /*
887 * Check whether a Thumb entry point has been provided for an
888 * aarch64 EL
889 */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100890 if ((entrypoint & 0x1UL) != 0UL)
Soby Mathewf1f97a12015-07-15 12:13:26 +0100891 return PSCI_E_INVALID_ADDRESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100892
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100893 mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? MODE_EL2 : MODE_EL1;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100894
Jimmy Brissoned202072020-08-04 16:18:52 -0500895 ep->spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX,
896 DISABLE_ALL_EXCEPTIONS);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100897 } else {
898
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100899 mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
900 MODE32_hyp : MODE32_svc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100901
902 /*
903 * TODO: Choose async. exception bits if HYP mode is not
904 * implemented according to the values of SCR.{AW, FW} bits
905 */
Vikram Kanigiri9851e422014-05-13 14:42:08 +0100906 daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
907
Jimmy Brissoned202072020-08-04 16:18:52 -0500908 ep->spsr = SPSR_MODE32((uint64_t)mode, entrypoint & 0x1, ee,
909 daif);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100910 }
911
Andrew Thoelke4e126072014-06-04 21:10:52 +0100912 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100913}
Julius Werner8e0ef0f2019-07-09 14:02:43 -0700914#else /* !__aarch64__ */
915static int psci_get_ns_ep_info(entry_point_info_t *ep,
916 uintptr_t entrypoint,
917 u_register_t context_id)
918{
919 u_register_t ep_attr;
920 unsigned int aif, ee, mode;
921 u_register_t scr = read_scr();
922 u_register_t ns_sctlr, sctlr;
923
924 /* Switch to non secure state */
925 write_scr(scr | SCR_NS_BIT);
926 isb();
927 ns_sctlr = read_sctlr();
928
929 sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
930
931 /* Return to original state */
932 write_scr(scr);
933 isb();
934 ee = 0;
935
936 ep_attr = NON_SECURE | EP_ST_DISABLE;
937 if (sctlr & SCTLR_EE_BIT) {
938 ep_attr |= EP_EE_BIG;
939 ee = 1;
940 }
941 SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
942
943 ep->pc = entrypoint;
944 zeromem(&ep->args, sizeof(ep->args));
945 ep->args.arg0 = context_id;
946
947 mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
948
949 /*
950 * TODO: Choose async. exception bits if HYP mode is not
951 * implemented according to the values of SCR.{AW, FW} bits
952 */
953 aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
954
955 ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
956
957 return PSCI_E_SUCCESS;
958}
959
960#endif /* __aarch64__ */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100961
962/*******************************************************************************
Soby Mathewf1f97a12015-07-15 12:13:26 +0100963 * This function validates the entrypoint with the platform layer if the
964 * appropriate pm_ops hook is exported by the platform and returns the
965 * 'entry_point_info'.
966 ******************************************************************************/
967int psci_validate_entry_point(entry_point_info_t *ep,
Soby Mathew011ca182015-07-29 17:05:03 +0100968 uintptr_t entrypoint,
969 u_register_t context_id)
Soby Mathewf1f97a12015-07-15 12:13:26 +0100970{
971 int rc;
972
973 /* Validate the entrypoint using platform psci_ops */
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +0100974 if (psci_plat_pm_ops->validate_ns_entrypoint != NULL) {
Soby Mathewf1f97a12015-07-15 12:13:26 +0100975 rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530976 if (rc != PSCI_E_SUCCESS) {
Soby Mathewf1f97a12015-07-15 12:13:26 +0100977 return PSCI_E_INVALID_ADDRESS;
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +0530978 }
Soby Mathewf1f97a12015-07-15 12:13:26 +0100979 }
980
981 /*
982 * Verify and derive the re-entry information for
983 * the non-secure world from the non-secure state from
984 * where this call originated.
985 */
986 rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
987 return rc;
988}
989
990/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +0100991 * Generic handler which is called when a cpu is physically powered on. It
Soby Mathew981487a2015-07-13 14:10:57 +0100992 * traverses the node information and finds the highest power level powered
993 * off and performs generic, architectural, platform setup and state management
994 * to power on that power level and power levels below it.
995 * e.g. For a cpu that's been powered on, it will call the platform specific
996 * code to enable the gic cpu interface and for a cluster it will enable
997 * coherency at the interconnect level in addition to gic cpu interface.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100998 ******************************************************************************/
Soby Mathewd0194872016-04-29 19:01:30 +0100999void psci_warmboot_entrypoint(void)
Achin Gupta4f6ad662013-10-25 09:08:21 +01001000{
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001001 unsigned int end_pwrlvl;
Deepika Bhavnani79ffab52019-08-27 00:32:24 +03001002 unsigned int cpu_idx = plat_my_core_pos();
Andrew F. Davis74e89782019-06-04 10:46:54 -04001003 unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
Soby Mathew981487a2015-07-13 14:10:57 +01001004 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
Achin Gupta4f6ad662013-10-25 09:08:21 +01001005
Boyan Karatotev36cebf92023-03-08 11:56:49 +00001006 /* Init registers that never change for the lifetime of TF-A */
Boyan Karatotevb2953472024-11-06 14:55:35 +00001007 cm_manage_extensions_el3(cpu_idx);
Boyan Karatotev36cebf92023-03-08 11:56:49 +00001008
Achin Gupta4f6ad662013-10-25 09:08:21 +01001009 /*
Soby Mathew981487a2015-07-13 14:10:57 +01001010 * Verify that we have been explicitly turned ON or resumed from
1011 * suspend.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001012 */
Soby Mathew981487a2015-07-13 14:10:57 +01001013 if (psci_get_aff_info_state() == AFF_STATE_OFF) {
Andrew Walbran8fe72b92020-01-23 16:22:44 +00001014 ERROR("Unexpected affinity info state.\n");
James Morrissey40a6f642014-02-10 14:24:36 +00001015 panic();
Soby Mathew981487a2015-07-13 14:10:57 +01001016 }
Achin Gupta4f6ad662013-10-25 09:08:21 +01001017
1018 /*
Soby Mathew981487a2015-07-13 14:10:57 +01001019 * Get the maximum power domain level to traverse to after this cpu
1020 * has been physically powered up.
Achin Gupta4f6ad662013-10-25 09:08:21 +01001021 */
Soby Mathew981487a2015-07-13 14:10:57 +01001022 end_pwrlvl = get_power_on_target_pwrlvl();
Achin Guptaf6b9e992014-07-31 11:19:11 +01001023
Andrew F. Davis74e89782019-06-04 10:46:54 -04001024 /* Get the parent nodes */
1025 psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
1026
Achin Guptaf6b9e992014-07-31 11:19:11 +01001027 /*
Soby Mathew981487a2015-07-13 14:10:57 +01001028 * This function acquires the lock corresponding to each power level so
1029 * that by the time all locks are taken, the system topology is snapshot
1030 * and state management can be done safely.
Achin Guptaf6b9e992014-07-31 11:19:11 +01001031 */
Andrew F. Davis74e89782019-06-04 10:46:54 -04001032 psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
Achin Guptaf6b9e992014-07-31 11:19:11 +01001033
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001034 psci_get_target_local_pwr_states(cpu_idx, end_pwrlvl, &state_info);
Soby Mathew8336f682017-10-16 15:19:31 +01001035
Yatharth Kochar241ec6c2016-05-09 18:26:35 +01001036#if ENABLE_PSCI_STAT
dp-arm66abfbe2017-01-31 13:01:04 +00001037 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar241ec6c2016-05-09 18:26:35 +01001038#endif
1039
Achin Gupta4f6ad662013-10-25 09:08:21 +01001040 /*
Soby Mathew981487a2015-07-13 14:10:57 +01001041 * This CPU could be resuming from suspend or it could have just been
1042 * turned on. To distinguish between these 2 cases, we examine the
1043 * affinity state of the CPU:
1044 * - If the affinity state is ON_PENDING then it has just been
1045 * turned on.
1046 * - Else it is resuming from suspend.
1047 *
1048 * Depending on the type of warm reset identified, choose the right set
1049 * of power management handler and perform the generic, architecture
1050 * and platform specific handling.
Achin Guptacab78e42014-07-28 00:09:01 +01001051 */
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +05301052 if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING) {
Soby Mathew981487a2015-07-13 14:10:57 +01001053 psci_cpu_on_finish(cpu_idx, &state_info);
Maheedhar Bollapallidfe13fb2024-04-25 11:47:27 +05301054 } else {
Boyan Karatotev7262eff2024-12-19 16:07:29 +00001055 unsigned int max_off_lvl = psci_find_max_off_lvl(&state_info);
1056
1057 assert(max_off_lvl != PSCI_INVALID_PWR_LVL);
1058 psci_cpu_suspend_to_powerdown_finish(cpu_idx, max_off_lvl, &state_info);
1059 }
Achin Guptacab78e42014-07-28 00:09:01 +01001060
1061 /*
Manish Pandeya14fb252024-06-22 00:00:18 +01001062 * Caches and (importantly) coherency are on so we can rely on seeing
1063 * whatever the primary gave us without explicit cache maintenance
1064 */
1065 entry_point_info_t *ep = get_cpu_data(warmboot_ep_info);
1066 cm_init_my_context(ep);
1067
1068 /*
Boyan Karatotev8ee32142023-05-17 12:20:09 +01001069 * Generic management: Now we just need to retrieve the
1070 * information that we had stashed away during the cpu_on
1071 * call to set this cpu on its way.
1072 */
1073 cm_prepare_el3_exit_ns();
1074
1075 /*
Soby Mathew981487a2015-07-13 14:10:57 +01001076 * Set the requested and target state of this CPU and all the higher
1077 * power domains which are ancestors of this CPU to run.
Achin Guptaf6b9e992014-07-31 11:19:11 +01001078 */
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001079 psci_set_pwr_domains_to_run(cpu_idx, end_pwrlvl);
Achin Guptaf6b9e992014-07-31 11:19:11 +01001080
Yatharth Kochar241ec6c2016-05-09 18:26:35 +01001081#if ENABLE_PSCI_STAT
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001082 psci_stats_update_pwr_up(cpu_idx, end_pwrlvl, &state_info);
Yatharth Kochar241ec6c2016-05-09 18:26:35 +01001083#endif
1084
Achin Guptaf6b9e992014-07-31 11:19:11 +01001085 /*
Soby Mathew981487a2015-07-13 14:10:57 +01001086 * This loop releases the lock corresponding to each power level
Achin Gupta0959db52013-12-02 17:33:04 +00001087 * in the reverse order to which they were acquired.
1088 */
Andrew F. Davis74e89782019-06-04 10:46:54 -04001089 psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +01001090}
Jeenu Viswambharan7f366602014-02-20 17:11:00 +00001091
1092/*******************************************************************************
1093 * This function initializes the set of hooks that PSCI invokes as part of power
1094 * management operation. The power management hooks are expected to be provided
1095 * by the SPD, after it finishes all its initialization
1096 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +01001097void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
Jeenu Viswambharan7f366602014-02-20 17:11:00 +00001098{
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001099 assert(pm != NULL);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +00001100 psci_spd_pm = pm;
Soby Mathew6cdddaf2015-01-07 11:10:22 +00001101
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001102 if (pm->svc_migrate != NULL)
Soby Mathew6cdddaf2015-01-07 11:10:22 +00001103 psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
1104
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001105 if (pm->svc_migrate_info != NULL)
Soby Mathew6cdddaf2015-01-07 11:10:22 +00001106 psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
1107 | define_psci_cap(PSCI_MIG_INFO_TYPE);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +00001108}
Juan Castillo4dc4a472014-08-12 11:17:06 +01001109
1110/*******************************************************************************
Soby Mathew110fe362014-10-23 10:35:34 +01001111 * This function invokes the migrate info hook in the spd_pm_ops. It performs
1112 * the necessary return value validation. If the Secure Payload is UP and
1113 * migrate capable, it returns the mpidr of the CPU on which the Secure payload
1114 * is resident through the mpidr parameter. Else the value of the parameter on
1115 * return is undefined.
1116 ******************************************************************************/
Soby Mathew011ca182015-07-29 17:05:03 +01001117int psci_spd_migrate_info(u_register_t *mpidr)
Soby Mathew110fe362014-10-23 10:35:34 +01001118{
1119 int rc;
1120
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001121 if ((psci_spd_pm == NULL) || (psci_spd_pm->svc_migrate_info == NULL))
Soby Mathew110fe362014-10-23 10:35:34 +01001122 return PSCI_E_NOT_SUPPORTED;
1123
1124 rc = psci_spd_pm->svc_migrate_info(mpidr);
1125
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001126 assert((rc == PSCI_TOS_UP_MIG_CAP) || (rc == PSCI_TOS_NOT_UP_MIG_CAP) ||
1127 (rc == PSCI_TOS_NOT_PRESENT_MP) || (rc == PSCI_E_NOT_SUPPORTED));
Soby Mathew110fe362014-10-23 10:35:34 +01001128
1129 return rc;
1130}
1131
1132
1133/*******************************************************************************
Soby Mathew981487a2015-07-13 14:10:57 +01001134 * This function prints the state of all power domains present in the
Juan Castillo4dc4a472014-08-12 11:17:06 +01001135 * system
1136 ******************************************************************************/
Soby Mathew981487a2015-07-13 14:10:57 +01001137void psci_print_power_domain_map(void)
Juan Castillo4dc4a472014-08-12 11:17:06 +01001138{
1139#if LOG_LEVEL >= LOG_LEVEL_INFO
Pankaj Gupta02c35682019-10-15 15:44:45 +05301140 unsigned int idx;
Soby Mathew981487a2015-07-13 14:10:57 +01001141 plat_local_state_t state;
1142 plat_local_state_type_t state_type;
1143
Juan Castillo4dc4a472014-08-12 11:17:06 +01001144 /* This array maps to the PSCI_STATE_X definitions in psci.h */
Soby Mathew24ab34f2016-05-03 17:11:42 +01001145 static const char * const psci_state_type_str[] = {
Juan Castillo4dc4a472014-08-12 11:17:06 +01001146 "ON",
Soby Mathew981487a2015-07-13 14:10:57 +01001147 "RETENTION",
Juan Castillo4dc4a472014-08-12 11:17:06 +01001148 "OFF",
Juan Castillo4dc4a472014-08-12 11:17:06 +01001149 };
1150
Soby Mathew981487a2015-07-13 14:10:57 +01001151 INFO("PSCI Power Domain Map:\n");
Pankaj Gupta02c35682019-10-15 15:44:45 +05301152 for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - psci_plat_core_count);
Soby Mathew981487a2015-07-13 14:10:57 +01001153 idx++) {
1154 state_type = find_local_state_type(
1155 psci_non_cpu_pd_nodes[idx].local_state);
Yann Gautier507e0cd2022-02-14 11:09:23 +01001156 INFO(" Domain Node : Level %u, parent_node %u,"
Soby Mathew981487a2015-07-13 14:10:57 +01001157 " State %s (0x%x)\n",
1158 psci_non_cpu_pd_nodes[idx].level,
1159 psci_non_cpu_pd_nodes[idx].parent_node,
1160 psci_state_type_str[state_type],
1161 psci_non_cpu_pd_nodes[idx].local_state);
1162 }
1163
Pankaj Gupta02c35682019-10-15 15:44:45 +05301164 for (idx = 0; idx < psci_plat_core_count; idx++) {
Soby Mathew981487a2015-07-13 14:10:57 +01001165 state = psci_get_cpu_local_state_by_idx(idx);
1166 state_type = find_local_state_type(state);
Yann Gautier507e0cd2022-02-14 11:09:23 +01001167 INFO(" CPU Node : MPID 0x%llx, parent_node %u,"
Soby Mathew981487a2015-07-13 14:10:57 +01001168 " State %s (0x%x)\n",
Soby Mathewa0fedc42016-06-16 14:52:04 +01001169 (unsigned long long)psci_cpu_pd_nodes[idx].mpidr,
Soby Mathew981487a2015-07-13 14:10:57 +01001170 psci_cpu_pd_nodes[idx].parent_node,
1171 psci_state_type_str[state_type],
1172 psci_get_cpu_local_state_by_idx(idx));
Juan Castillo4dc4a472014-08-12 11:17:06 +01001173 }
1174#endif
1175}
Soby Mathew981487a2015-07-13 14:10:57 +01001176
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00001177/******************************************************************************
1178 * Return whether any secondaries were powered up with CPU_ON call. A CPU that
1179 * have ever been powered up would have set its MPDIR value to something other
1180 * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to
1181 * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is
1182 * meaningful only when called on the primary CPU during early boot.
1183 *****************************************************************************/
1184int psci_secondaries_brought_up(void)
1185{
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001186 unsigned int idx, n_valid = 0U;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00001187
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001188 for (idx = 0U; idx < ARRAY_SIZE(psci_cpu_pd_nodes); idx++) {
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00001189 if (psci_cpu_pd_nodes[idx].mpidr != PSCI_INVALID_MPIDR)
1190 n_valid++;
1191 }
1192
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001193 assert(n_valid > 0U);
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00001194
Antonio Nino Diaz78a95a62018-07-17 15:10:08 +01001195 return (n_valid > 1U) ? 1 : 0;
Jeenu Viswambharanbc1a9292017-02-16 14:55:15 +00001196}
1197
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001198/*******************************************************************************
1199 * Initiate power down sequence, by calling power down operations registered for
1200 * this CPU.
1201 ******************************************************************************/
Boyan Karatotev7262eff2024-12-19 16:07:29 +00001202void psci_pwrdown_cpu_start(unsigned int power_level)
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001203{
Boyan Karatotevb33206f2024-10-10 08:11:09 +01001204#if ENABLE_RUNTIME_INSTRUMENTATION
1205
1206 /*
1207 * Flush cache line so that even if CPU power down happens
1208 * the timestamp update is reflected in memory.
1209 */
1210 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
1211 RT_INSTR_ENTER_CFLUSH,
1212 PMF_CACHE_MAINT);
1213#endif
1214
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001215#if HW_ASSISTED_COHERENCY
1216 /*
1217 * With hardware-assisted coherency, the CPU drivers only initiate the
1218 * power down sequence, without performing cache-maintenance operations
Andrew F. Davis564f9542018-08-30 12:08:01 -05001219 * in software. Data caches enabled both before and after this call.
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001220 */
1221 prepare_cpu_pwr_dwn(power_level);
1222#else
1223 /*
1224 * Without hardware-assisted coherency, the CPU drivers disable data
Andrew F. Davis564f9542018-08-30 12:08:01 -05001225 * caches, then perform cache-maintenance operations in software.
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001226 *
Andrew F. Davis564f9542018-08-30 12:08:01 -05001227 * This also calls prepare_cpu_pwr_dwn() to initiate power down
1228 * sequence, but that function will return with data caches disabled.
1229 * We must ensure that the stack memory is flushed out to memory before
1230 * we start popping from it again.
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001231 */
1232 psci_do_pwrdown_cache_maintenance(power_level);
1233#endif
Boyan Karatotevb33206f2024-10-10 08:11:09 +01001234
1235#if ENABLE_RUNTIME_INSTRUMENTATION
1236 PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
1237 RT_INSTR_EXIT_CFLUSH,
1238 PMF_NO_CACHE_MAINT);
1239#endif
Jeenu Viswambharan346bfd82017-01-05 11:01:02 +00001240}
Sandeep Tripathy12030042020-08-17 20:22:13 +05301241
1242/*******************************************************************************
Boyan Karatotev7262eff2024-12-19 16:07:29 +00001243 * Finish a terminal power down sequence, ending with a wfi. In case of wakeup
1244 * will retry the sleep and panic if it persists.
1245 ******************************************************************************/
1246void __dead2 psci_pwrdown_cpu_end_terminal(void)
1247{
Boyan Karatoteva6193b32024-09-20 13:37:51 +01001248#if ERRATA_SME_POWER_DOWN
1249 /*
1250 * force SME off to not get power down rejected. Getting here is
1251 * terminal so we don't care if we lose context because of another
1252 * wakeup
1253 */
1254 if (is_feat_sme_supported()) {
1255 write_svcr(0);
1256 isb();
1257 }
1258#endif /* ERRATA_SME_POWER_DOWN */
1259
Boyan Karatotev7262eff2024-12-19 16:07:29 +00001260 /*
1261 * Execute a wfi which, in most cases, will allow the power controller
1262 * to physically power down this cpu. Under some circumstances that may
1263 * be denied. Hopefully this is transient, retrying a few times should
1264 * power down.
1265 */
1266 for (int i = 0; i < 32; i++)
1267 psci_power_down_wfi();
1268
1269 /* Wake up wasn't transient. System is probably in a bad state. */
1270 ERROR("Could not power off CPU.\n");
1271 panic();
1272}
1273
1274/*******************************************************************************
1275 * Finish a non-terminal power down sequence, ending with a wfi. In case of
1276 * wakeup will unwind any CPU specific actions and return.
1277 ******************************************************************************/
1278
1279void psci_pwrdown_cpu_end_wakeup(unsigned int power_level)
1280{
1281 /*
1282 * Usually, will be terminal. In some circumstances the powerdown will
1283 * be denied and we'll need to unwind
1284 */
1285 psci_power_down_wfi();
1286
1287 /*
1288 * Waking up does not require hardware-assisted coherency, but that is
1289 * the case for every core that can wake up. Untangling the cache
1290 * coherency code from powerdown is a non-trivial effort which isn't
1291 * needed for our purposes.
1292 */
1293#if !FEAT_PABANDON
1294 ERROR("Systems without FEAT_PABANDON shouldn't wake up.\n");
1295 panic();
1296#else /* FEAT_PABANDON */
1297
1298 /*
1299 * Begin unwinding. Everything can be shared with CPU_ON and co later,
1300 * except the CPU specific bit. Cores that have hardware-assisted
1301 * coherency don't have much to do so just calling the hook again is
1302 * the simplest way to achieve this
1303 */
1304 prepare_cpu_pwr_dwn(power_level);
1305#endif /* FEAT_PABANDON */
1306}
1307
1308/*******************************************************************************
Sandeep Tripathy12030042020-08-17 20:22:13 +05301309 * This function invokes the callback 'stop_func()' with the 'mpidr' of each
1310 * online PE. Caller can pass suitable method to stop a remote core.
1311 *
1312 * 'wait_ms' is the timeout value in milliseconds for the other cores to
1313 * transition to power down state. Passing '0' makes it non-blocking.
1314 *
1315 * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the
1316 * given timeout.
1317 ******************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001318int psci_stop_other_cores(unsigned int this_cpu_idx, unsigned int wait_ms,
Sandeep Tripathy12030042020-08-17 20:22:13 +05301319 void (*stop_func)(u_register_t mpidr))
1320{
Sandeep Tripathy12030042020-08-17 20:22:13 +05301321 /* Invoke stop_func for each core */
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001322 for (unsigned int idx = 0U; idx < psci_plat_core_count; idx++) {
Sandeep Tripathy12030042020-08-17 20:22:13 +05301323 /* skip current CPU */
1324 if (idx == this_cpu_idx) {
1325 continue;
1326 }
1327
1328 /* Check if the CPU is ON */
1329 if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) {
1330 (*stop_func)(psci_cpu_pd_nodes[idx].mpidr);
1331 }
1332 }
1333
1334 /* Need to wait for other cores to shutdown */
1335 if (wait_ms != 0U) {
Maheedhar Bollapalli61192152024-04-23 11:49:04 +05301336 for (uint32_t delay_ms = wait_ms; ((delay_ms != 0U) &&
1337 (!psci_is_last_on_cpu(this_cpu_idx))); delay_ms--) {
Sandeep Tripathy12030042020-08-17 20:22:13 +05301338 mdelay(1U);
1339 }
1340
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001341 if (!psci_is_last_on_cpu(this_cpu_idx)) {
Sandeep Tripathy12030042020-08-17 20:22:13 +05301342 WARN("Failed to stop all cores!\n");
1343 psci_print_power_domain_map();
1344 return PSCI_E_DENIED;
1345 }
1346 }
1347
1348 return PSCI_E_SUCCESS;
1349}
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001350
1351/*******************************************************************************
1352 * This function verifies that all the other cores in the system have been
1353 * turned OFF and the current CPU is the last running CPU in the system.
1354 * Returns true if the current CPU is the last ON CPU or false otherwise.
1355 *
1356 * This API has following differences with psci_is_last_on_cpu
1357 * 1. PSCI states are locked
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001358 ******************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001359bool psci_is_last_on_cpu_safe(unsigned int this_core)
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001360{
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001361 unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001362
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +01001363 psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001364
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +01001365 psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001366
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001367 if (!psci_is_last_on_cpu(this_core)) {
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001368 psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +01001369 return false;
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001370 }
1371
Jayanth Dodderi Chidanand70763502022-08-22 23:46:10 +01001372 psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1373
Lucian Paul-Trifu5e685352022-03-02 21:28:24 +00001374 return true;
1375}
Wing Li71f69df2022-09-14 13:18:15 -07001376
1377/*******************************************************************************
1378 * This function verifies that all cores in the system have been turned ON.
1379 * Returns true, if all CPUs are ON or false otherwise.
1380 *
1381 * This API has following differences with psci_are_all_cpus_on
1382 * 1. PSCI states are locked
1383 ******************************************************************************/
Boyan Karatotevaa46ccd2024-11-06 16:26:15 +00001384bool psci_are_all_cpus_on_safe(unsigned int this_core)
Wing Li71f69df2022-09-14 13:18:15 -07001385{
Wing Li71f69df2022-09-14 13:18:15 -07001386 unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1387
1388 psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1389
1390 psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1391
1392 if (!psci_are_all_cpus_on()) {
1393 psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1394 return false;
1395 }
1396
1397 psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1398
1399 return true;
1400}
Manish V Badarkhed55c23f2025-05-29 14:55:39 +01001401
1402/*******************************************************************************
1403 * Safely counts the number of CPUs in the system that are currently in the ON
1404 * or ON_PENDING state.
1405 *
1406 * This function acquires and releases the necessary power domain locks to
1407 * ensure consistency of the CPU state information.
1408 *
1409 * @param this_core The index of the current core making the query.
1410 *
1411 * @return The number of CPUs currently in AFF_STATE_ON or AFF_STATE_ON_PENDING.
1412 ******************************************************************************/
1413unsigned int psci_num_cpus_running_on_safe(unsigned int this_core)
1414{
1415 unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1416 unsigned int no_of_cpus;
1417
1418 psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1419
1420 psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1421
1422 no_of_cpus = psci_num_cpus_running();
1423
1424 psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1425
1426 return no_of_cpus;
1427}