1532ed618SSoby Mathew /*
23b802105SBoyan Karatotev * Copyright (c) 2013-2025, Arm Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew */
6532ed618SSoby Mathew
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <string.h>
909d40e0eSAntonio Nino Diaz
10532ed618SSoby Mathew #include <arch.h>
11777f1f68SJayanth Dodderi Chidanand #include <arch_features.h>
12532ed618SSoby Mathew #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1409d40e0eSAntonio Nino Diaz #include <common/debug.h>
15532ed618SSoby Mathew #include <context.h>
1622744909SSandeep Tripathy #include <drivers/delay_timer.h>
17232c1892SBoyan Karatotev #include <lib/cpus/cpu_ops.h>
1809d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
19777f1f68SJayanth Dodderi Chidanand #include <lib/extensions/spe.h>
20*9f407e44SRohit Mathew #include <lib/per_cpu/per_cpu.h>
219b1e800eSBoyan Karatotev #include <lib/pmf/pmf.h>
229b1e800eSBoyan Karatotev #include <lib/runtime_instr.h>
2309d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2409d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2509d40e0eSAntonio Nino Diaz
26532ed618SSoby Mathew #include "psci_private.h"
27532ed618SSoby Mathew
28532ed618SSoby Mathew /*
29532ed618SSoby Mathew * SPD power management operations, expected to be supplied by the registered
30532ed618SSoby Mathew * SPD on successful SP initialization
31532ed618SSoby Mathew */
32532ed618SSoby Mathew const spd_pm_ops_t *psci_spd_pm;
33532ed618SSoby Mathew
34532ed618SSoby Mathew /*
35532ed618SSoby Mathew * PSCI requested local power state map. This array is used to store the local
36532ed618SSoby Mathew * power states requested by a CPU for power levels from level 1 to
37532ed618SSoby Mathew * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
38532ed618SSoby Mathew * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
39532ed618SSoby Mathew * CPU are the same.
40532ed618SSoby Mathew *
41532ed618SSoby Mathew * During state coordination, the platform is passed an array containing the
42532ed618SSoby Mathew * local states requested for a particular non cpu power domain by each cpu
43532ed618SSoby Mathew * within the domain.
44532ed618SSoby Mathew *
45532ed618SSoby Mathew * TODO: Dense packing of the requested states will cause cache thrashing
46532ed618SSoby Mathew * when multiple power domains write to it. If we allocate the requested
47532ed618SSoby Mathew * states at each power level in a cache-line aligned per-domain memory,
48532ed618SSoby Mathew * the cache thrashing can be avoided.
49532ed618SSoby Mathew */
50532ed618SSoby Mathew static plat_local_state_t
51532ed618SSoby Mathew psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
52532ed618SSoby Mathew
53ab4df50cSPankaj Gupta unsigned int psci_plat_core_count;
54532ed618SSoby Mathew
55532ed618SSoby Mathew /*******************************************************************************
56532ed618SSoby Mathew * Arrays that hold the platform's power domain tree information for state
57532ed618SSoby Mathew * management of power domains.
58532ed618SSoby Mathew * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
59532ed618SSoby Mathew * which is an ancestor of a CPU power domain.
60532ed618SSoby Mathew * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
61532ed618SSoby Mathew ******************************************************************************/
62532ed618SSoby Mathew non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
63532ed618SSoby Mathew #if USE_COHERENT_MEM
64da04341eSChris Kay __section(".tzfw_coherent_mem")
65532ed618SSoby Mathew #endif
66532ed618SSoby Mathew ;
67532ed618SSoby Mathew
68b0408e87SJeenu Viswambharan /* Lock for PSCI state coordination */
69b0408e87SJeenu Viswambharan DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
70532ed618SSoby Mathew
71*9f407e44SRohit Mathew PER_CPU_DEFINE(cpu_pd_node_t, psci_cpu_pd_nodes);
72532ed618SSoby Mathew
73532ed618SSoby Mathew /*******************************************************************************
74532ed618SSoby Mathew * Pointer to functions exported by the platform to complete power mgmt. ops
75532ed618SSoby Mathew ******************************************************************************/
76532ed618SSoby Mathew const plat_psci_ops_t *psci_plat_pm_ops;
77532ed618SSoby Mathew
78532ed618SSoby Mathew /******************************************************************************
79532ed618SSoby Mathew * Check that the maximum power level supported by the platform makes sense
80532ed618SSoby Mathew *****************************************************************************/
816b7b0f36SAntonio Nino Diaz CASSERT((PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL) &&
826b7b0f36SAntonio Nino Diaz (PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL),
83532ed618SSoby Mathew assert_platform_max_pwrlvl_check);
84532ed618SSoby Mathew
85b88a4416SWing Li #if PSCI_OS_INIT_MODE
86b88a4416SWing Li /*******************************************************************************
87b88a4416SWing Li * The power state coordination mode used in CPU_SUSPEND.
88b88a4416SWing Li * Defaults to platform-coordinated mode.
89b88a4416SWing Li ******************************************************************************/
90b88a4416SWing Li suspend_mode_t psci_suspend_mode = PLAT_COORD;
91b88a4416SWing Li #endif
92b88a4416SWing Li
93532ed618SSoby Mathew /*
94532ed618SSoby Mathew * The plat_local_state used by the platform is one of these types: RUN,
95532ed618SSoby Mathew * RETENTION and OFF. The platform can define further sub-states for each type
96532ed618SSoby Mathew * apart from RUN. This categorization is done to verify the sanity of the
97532ed618SSoby Mathew * psci_power_state passed by the platform and to print debug information. The
98532ed618SSoby Mathew * categorization is done on the basis of the following conditions:
99532ed618SSoby Mathew *
100532ed618SSoby Mathew * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
101532ed618SSoby Mathew *
102532ed618SSoby Mathew * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
103532ed618SSoby Mathew * STATE_TYPE_RETN.
104532ed618SSoby Mathew *
105532ed618SSoby Mathew * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
106532ed618SSoby Mathew * STATE_TYPE_OFF.
107532ed618SSoby Mathew */
108532ed618SSoby Mathew typedef enum plat_local_state_type {
109532ed618SSoby Mathew STATE_TYPE_RUN = 0,
110532ed618SSoby Mathew STATE_TYPE_RETN,
111532ed618SSoby Mathew STATE_TYPE_OFF
112532ed618SSoby Mathew } plat_local_state_type_t;
113532ed618SSoby Mathew
11497373c33SAntonio Nino Diaz /* Function used to categorize plat_local_state. */
find_local_state_type(plat_local_state_t state)11597373c33SAntonio Nino Diaz static plat_local_state_type_t find_local_state_type(plat_local_state_t state)
11697373c33SAntonio Nino Diaz {
11797373c33SAntonio Nino Diaz if (state != 0U) {
11897373c33SAntonio Nino Diaz if (state > PLAT_MAX_RET_STATE) {
11997373c33SAntonio Nino Diaz return STATE_TYPE_OFF;
12097373c33SAntonio Nino Diaz } else {
12197373c33SAntonio Nino Diaz return STATE_TYPE_RETN;
12297373c33SAntonio Nino Diaz }
12397373c33SAntonio Nino Diaz } else {
12497373c33SAntonio Nino Diaz return STATE_TYPE_RUN;
12597373c33SAntonio Nino Diaz }
12697373c33SAntonio Nino Diaz }
127532ed618SSoby Mathew
128532ed618SSoby Mathew /******************************************************************************
129532ed618SSoby Mathew * Check that the maximum retention level supported by the platform is less
130532ed618SSoby Mathew * than the maximum off level.
131532ed618SSoby Mathew *****************************************************************************/
1326b7b0f36SAntonio Nino Diaz CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE,
133532ed618SSoby Mathew assert_platform_max_off_and_retn_state_check);
134532ed618SSoby Mathew
135532ed618SSoby Mathew /******************************************************************************
136532ed618SSoby Mathew * This function ensures that the power state parameter in a CPU_SUSPEND request
137532ed618SSoby Mathew * is valid. If so, it returns the requested states for each power level.
138532ed618SSoby Mathew *****************************************************************************/
psci_validate_power_state(unsigned int power_state,psci_power_state_t * state_info)139532ed618SSoby Mathew int psci_validate_power_state(unsigned int power_state,
140532ed618SSoby Mathew psci_power_state_t *state_info)
141532ed618SSoby Mathew {
142532ed618SSoby Mathew /* Check SBZ bits in power state are zero */
143c7b0a28dSMaheedhar Bollapalli if (psci_check_power_state(power_state) != 0U) {
144532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS;
145c7b0a28dSMaheedhar Bollapalli }
1466b7b0f36SAntonio Nino Diaz assert(psci_plat_pm_ops->validate_power_state != NULL);
147532ed618SSoby Mathew
148532ed618SSoby Mathew /* Validate the power_state using platform pm_ops */
149532ed618SSoby Mathew return psci_plat_pm_ops->validate_power_state(power_state, state_info);
150532ed618SSoby Mathew }
151532ed618SSoby Mathew
152532ed618SSoby Mathew /******************************************************************************
153532ed618SSoby Mathew * This function retrieves the `psci_power_state_t` for system suspend from
154532ed618SSoby Mathew * the platform.
155532ed618SSoby Mathew *****************************************************************************/
psci_query_sys_suspend_pwrstate(psci_power_state_t * state_info)156532ed618SSoby Mathew void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
157532ed618SSoby Mathew {
158532ed618SSoby Mathew /*
159532ed618SSoby Mathew * Assert that the required pm_ops hook is implemented to ensure that
160532ed618SSoby Mathew * the capability detected during psci_setup() is valid.
161532ed618SSoby Mathew */
1626b7b0f36SAntonio Nino Diaz assert(psci_plat_pm_ops->get_sys_suspend_power_state != NULL);
163532ed618SSoby Mathew
164532ed618SSoby Mathew /*
165532ed618SSoby Mathew * Query the platform for the power_state required for system suspend
166532ed618SSoby Mathew */
167532ed618SSoby Mathew psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
168532ed618SSoby Mathew }
169532ed618SSoby Mathew
170606b7430SWing Li #if PSCI_OS_INIT_MODE
171606b7430SWing Li /*******************************************************************************
172606b7430SWing Li * This function verifies that all the other cores at the 'end_pwrlvl' have been
173606b7430SWing Li * idled and the current CPU is the last running CPU at the 'end_pwrlvl'.
174606b7430SWing Li * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
175606b7430SWing Li * otherwise.
176606b7430SWing Li ******************************************************************************/
psci_is_last_cpu_to_idle_at_pwrlvl(unsigned int my_idx,unsigned int end_pwrlvl)1773b802105SBoyan Karatotev static bool psci_is_last_cpu_to_idle_at_pwrlvl(unsigned int my_idx, unsigned int end_pwrlvl)
178606b7430SWing Li {
1793b802105SBoyan Karatotev unsigned int lvl;
180152ad112SMark Dykes unsigned int parent_idx = 0;
181606b7430SWing Li unsigned int cpu_start_idx, ncpus, cpu_idx;
182606b7430SWing Li plat_local_state_t local_state;
183606b7430SWing Li
184606b7430SWing Li if (end_pwrlvl == PSCI_CPU_PWR_LVL) {
185606b7430SWing Li return true;
186606b7430SWing Li }
187606b7430SWing Li
188*9f407e44SRohit Mathew parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, my_idx)->parent_node;
18901959a16SCharlie Bareham for (lvl = PSCI_CPU_PWR_LVL + U(1); lvl < end_pwrlvl; lvl++) {
19001959a16SCharlie Bareham parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
191606b7430SWing Li }
192606b7430SWing Li
193606b7430SWing Li cpu_start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
194606b7430SWing Li ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
195606b7430SWing Li
196606b7430SWing Li for (cpu_idx = cpu_start_idx; cpu_idx < cpu_start_idx + ncpus;
197606b7430SWing Li cpu_idx++) {
198606b7430SWing Li local_state = psci_get_cpu_local_state_by_idx(cpu_idx);
199606b7430SWing Li if (cpu_idx == my_idx) {
200606b7430SWing Li assert(is_local_state_run(local_state) != 0);
201606b7430SWing Li continue;
202606b7430SWing Li }
203606b7430SWing Li
204606b7430SWing Li if (is_local_state_run(local_state) != 0) {
205606b7430SWing Li return false;
206606b7430SWing Li }
207606b7430SWing Li }
208606b7430SWing Li
209606b7430SWing Li return true;
210606b7430SWing Li }
211606b7430SWing Li #endif
212606b7430SWing Li
213532ed618SSoby Mathew /*******************************************************************************
214b88a4416SWing Li * This function verifies that all the other cores in the system have been
215532ed618SSoby Mathew * turned OFF and the current CPU is the last running CPU in the system.
216b41b0824SJayanth Dodderi Chidanand * Returns true, if the current CPU is the last ON CPU or false otherwise.
217532ed618SSoby Mathew ******************************************************************************/
psci_is_last_on_cpu(unsigned int my_idx)2183b802105SBoyan Karatotev bool psci_is_last_on_cpu(unsigned int my_idx)
219532ed618SSoby Mathew {
220a7be2a57SManish V Badarkhe for (unsigned int cpu_idx = 0U; cpu_idx < psci_plat_core_count; cpu_idx++) {
221532ed618SSoby Mathew if (cpu_idx == my_idx) {
222532ed618SSoby Mathew assert(psci_get_aff_info_state() == AFF_STATE_ON);
223532ed618SSoby Mathew continue;
224532ed618SSoby Mathew }
225532ed618SSoby Mathew
226b41b0824SJayanth Dodderi Chidanand if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF) {
227b41b0824SJayanth Dodderi Chidanand VERBOSE("core=%u other than current core=%u %s\n",
228b41b0824SJayanth Dodderi Chidanand cpu_idx, my_idx, "running in the system");
229b41b0824SJayanth Dodderi Chidanand return false;
230b41b0824SJayanth Dodderi Chidanand }
231532ed618SSoby Mathew }
232532ed618SSoby Mathew
233b41b0824SJayanth Dodderi Chidanand return true;
234532ed618SSoby Mathew }
235532ed618SSoby Mathew
236532ed618SSoby Mathew /*******************************************************************************
237b88a4416SWing Li * This function verifies that all cores in the system have been turned ON.
238b88a4416SWing Li * Returns true, if all CPUs are ON or false otherwise.
239b88a4416SWing Li ******************************************************************************/
psci_are_all_cpus_on(void)240b88a4416SWing Li static bool psci_are_all_cpus_on(void)
241b88a4416SWing Li {
242b88a4416SWing Li unsigned int cpu_idx;
243b88a4416SWing Li
244a7be2a57SManish V Badarkhe for (cpu_idx = 0U; cpu_idx < psci_plat_core_count; cpu_idx++) {
245b88a4416SWing Li if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_OFF) {
246b88a4416SWing Li return false;
247b88a4416SWing Li }
248b88a4416SWing Li }
249b88a4416SWing Li
250b88a4416SWing Li return true;
251b88a4416SWing Li }
252b88a4416SWing Li
253b88a4416SWing Li /*******************************************************************************
254a7be2a57SManish V Badarkhe * Counts the number of CPUs in the system that are currently in the ON or
255a7be2a57SManish V Badarkhe * ON_PENDING state.
256a7be2a57SManish V Badarkhe *
257a7be2a57SManish V Badarkhe * @note This function does not acquire any power domain locks. It must only be
258a7be2a57SManish V Badarkhe * called in contexts where it is guaranteed that PSCI state transitions
259a7be2a57SManish V Badarkhe * are not concurrently happening, or where locks are already held.
260a7be2a57SManish V Badarkhe *
261a7be2a57SManish V Badarkhe * @return The number of CPUs currently in AFF_STATE_ON or AFF_STATE_ON_PENDING.
262a7be2a57SManish V Badarkhe ******************************************************************************/
psci_num_cpus_running(void)263a7be2a57SManish V Badarkhe static unsigned int psci_num_cpus_running(void)
264a7be2a57SManish V Badarkhe {
265a7be2a57SManish V Badarkhe unsigned int cpu_idx;
266a7be2a57SManish V Badarkhe unsigned int no_of_cpus = 0U;
267a7be2a57SManish V Badarkhe aff_info_state_t aff_state;
268a7be2a57SManish V Badarkhe
269a7be2a57SManish V Badarkhe for (cpu_idx = 0U; cpu_idx < psci_plat_core_count; cpu_idx++) {
270a7be2a57SManish V Badarkhe aff_state = psci_get_aff_info_state_by_idx(cpu_idx);
271a7be2a57SManish V Badarkhe if (aff_state == AFF_STATE_ON ||
272a7be2a57SManish V Badarkhe aff_state == AFF_STATE_ON_PENDING) {
273a7be2a57SManish V Badarkhe no_of_cpus++;
274a7be2a57SManish V Badarkhe }
275a7be2a57SManish V Badarkhe }
276a7be2a57SManish V Badarkhe
277a7be2a57SManish V Badarkhe return no_of_cpus;
278a7be2a57SManish V Badarkhe }
279a7be2a57SManish V Badarkhe
280a7be2a57SManish V Badarkhe /*******************************************************************************
281532ed618SSoby Mathew * Routine to return the maximum power level to traverse to after a cpu has
282532ed618SSoby Mathew * been physically powered up. It is expected to be called immediately after
283532ed618SSoby Mathew * reset from assembler code.
284532ed618SSoby Mathew ******************************************************************************/
get_power_on_target_pwrlvl(void)285532ed618SSoby Mathew static unsigned int get_power_on_target_pwrlvl(void)
286532ed618SSoby Mathew {
287532ed618SSoby Mathew unsigned int pwrlvl;
288532ed618SSoby Mathew
289532ed618SSoby Mathew /*
290532ed618SSoby Mathew * Assume that this cpu was suspended and retrieve its target power
2910c836554SBoyan Karatotev * level. If it wasn't, the cpu is off so this will be PLAT_MAX_PWR_LVL.
292532ed618SSoby Mathew */
293532ed618SSoby Mathew pwrlvl = psci_get_suspend_pwrlvl();
2940c411c78SDeepika Bhavnani assert(pwrlvl < PSCI_INVALID_PWR_LVL);
295532ed618SSoby Mathew return pwrlvl;
296532ed618SSoby Mathew }
297532ed618SSoby Mathew
298532ed618SSoby Mathew /******************************************************************************
299532ed618SSoby Mathew * Helper function to update the requested local power state array. This array
300532ed618SSoby Mathew * does not store the requested state for the CPU power level. Hence an
30141af0515SDeepika Bhavnani * assertion is added to prevent us from accessing the CPU power level.
302532ed618SSoby Mathew *****************************************************************************/
psci_set_req_local_pwr_state(unsigned int pwrlvl,unsigned int cpu_idx,plat_local_state_t req_pwr_state)303532ed618SSoby Mathew static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
304532ed618SSoby Mathew unsigned int cpu_idx,
305532ed618SSoby Mathew plat_local_state_t req_pwr_state)
306532ed618SSoby Mathew {
307532ed618SSoby Mathew assert(pwrlvl > PSCI_CPU_PWR_LVL);
30841af0515SDeepika Bhavnani if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
309ab4df50cSPankaj Gupta (cpu_idx < psci_plat_core_count)) {
3106b7b0f36SAntonio Nino Diaz psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx] = req_pwr_state;
31141af0515SDeepika Bhavnani }
312532ed618SSoby Mathew }
313532ed618SSoby Mathew
314532ed618SSoby Mathew /******************************************************************************
315532ed618SSoby Mathew * This function initializes the psci_req_local_pwr_states.
316532ed618SSoby Mathew *****************************************************************************/
psci_init_req_local_pwr_states(void)31787c85134SDaniel Boulby void __init psci_init_req_local_pwr_states(void)
318532ed618SSoby Mathew {
319532ed618SSoby Mathew /* Initialize the requested state of all non CPU power domains as OFF */
3206b7b0f36SAntonio Nino Diaz unsigned int pwrlvl;
321ab4df50cSPankaj Gupta unsigned int core;
3226b7b0f36SAntonio Nino Diaz
3236b7b0f36SAntonio Nino Diaz for (pwrlvl = 0U; pwrlvl < PLAT_MAX_PWR_LVL; pwrlvl++) {
324ab4df50cSPankaj Gupta for (core = 0; core < psci_plat_core_count; core++) {
3256b7b0f36SAntonio Nino Diaz psci_req_local_pwr_states[pwrlvl][core] =
3266b7b0f36SAntonio Nino Diaz PLAT_MAX_OFF_STATE;
3276b7b0f36SAntonio Nino Diaz }
3286b7b0f36SAntonio Nino Diaz }
329532ed618SSoby Mathew }
330532ed618SSoby Mathew
331532ed618SSoby Mathew /******************************************************************************
332532ed618SSoby Mathew * Helper function to return a reference to an array containing the local power
333532ed618SSoby Mathew * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
334532ed618SSoby Mathew * array will be the number of cpu power domains of which this power domain is
335532ed618SSoby Mathew * an ancestor. These requested states will be used to determine a suitable
336532ed618SSoby Mathew * target state for this power domain during psci state coordination. An
337532ed618SSoby Mathew * assertion is added to prevent us from accessing the CPU power level.
338532ed618SSoby Mathew *****************************************************************************/
psci_get_req_local_pwr_states(unsigned int pwrlvl,unsigned int cpu_idx)339532ed618SSoby Mathew static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
340fc81021aSDeepika Bhavnani unsigned int cpu_idx)
341532ed618SSoby Mathew {
342532ed618SSoby Mathew assert(pwrlvl > PSCI_CPU_PWR_LVL);
343532ed618SSoby Mathew
34441af0515SDeepika Bhavnani if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
345ab4df50cSPankaj Gupta (cpu_idx < psci_plat_core_count)) {
3466b7b0f36SAntonio Nino Diaz return &psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx];
347bac32cc4SSaivardhan Thatikonda } else {
34841af0515SDeepika Bhavnani return NULL;
349532ed618SSoby Mathew }
350bac32cc4SSaivardhan Thatikonda }
351532ed618SSoby Mathew
352606b7430SWing Li #if PSCI_OS_INIT_MODE
353606b7430SWing Li /******************************************************************************
354606b7430SWing Li * Helper function to save a copy of the psci_req_local_pwr_states (prev) for a
355606b7430SWing Li * CPU (cpu_idx), and update psci_req_local_pwr_states with the new requested
356606b7430SWing Li * local power states (state_info).
357606b7430SWing Li *****************************************************************************/
psci_update_req_local_pwr_states(unsigned int end_pwrlvl,unsigned int cpu_idx,psci_power_state_t * state_info,plat_local_state_t * prev)358606b7430SWing Li void psci_update_req_local_pwr_states(unsigned int end_pwrlvl,
359606b7430SWing Li unsigned int cpu_idx,
360606b7430SWing Li psci_power_state_t *state_info,
361606b7430SWing Li plat_local_state_t *prev)
362606b7430SWing Li {
363606b7430SWing Li unsigned int lvl;
364606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
365606b7430SWing Li unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
366606b7430SWing Li #else
367606b7430SWing Li unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
368606b7430SWing Li #endif
369606b7430SWing Li plat_local_state_t req_state;
370606b7430SWing Li
371606b7430SWing Li for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
372606b7430SWing Li /* Save the previous requested local power state */
373606b7430SWing Li prev[lvl - 1U] = *psci_get_req_local_pwr_states(lvl, cpu_idx);
374606b7430SWing Li
375606b7430SWing Li /* Update the new requested local power state */
376606b7430SWing Li if (lvl <= end_pwrlvl) {
377606b7430SWing Li req_state = state_info->pwr_domain_state[lvl];
378606b7430SWing Li } else {
379606b7430SWing Li req_state = state_info->pwr_domain_state[end_pwrlvl];
380606b7430SWing Li }
381606b7430SWing Li psci_set_req_local_pwr_state(lvl, cpu_idx, req_state);
382606b7430SWing Li }
383606b7430SWing Li }
384606b7430SWing Li
385606b7430SWing Li /******************************************************************************
386606b7430SWing Li * Helper function to restore the previously saved requested local power states
387606b7430SWing Li * (prev) for a CPU (cpu_idx) to psci_req_local_pwr_states.
388606b7430SWing Li *****************************************************************************/
psci_restore_req_local_pwr_states(unsigned int cpu_idx,plat_local_state_t * prev)389606b7430SWing Li void psci_restore_req_local_pwr_states(unsigned int cpu_idx,
390606b7430SWing Li plat_local_state_t *prev)
391606b7430SWing Li {
392606b7430SWing Li unsigned int lvl;
393606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
394606b7430SWing Li unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
395606b7430SWing Li #else
396606b7430SWing Li unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
397606b7430SWing Li #endif
398606b7430SWing Li
399606b7430SWing Li for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
400606b7430SWing Li /* Restore the previous requested local power state */
401606b7430SWing Li psci_set_req_local_pwr_state(lvl, cpu_idx, prev[lvl - 1U]);
402606b7430SWing Li }
403606b7430SWing Li }
404606b7430SWing Li #endif
405606b7430SWing Li
406a10d3632SJeenu Viswambharan /*
407a10d3632SJeenu Viswambharan * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
408a10d3632SJeenu Viswambharan * memory.
409a10d3632SJeenu Viswambharan *
410a10d3632SJeenu Viswambharan * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
411a10d3632SJeenu Viswambharan * it's accessed by both cached and non-cached participants. To serve the common
412a10d3632SJeenu Viswambharan * minimum, perform a cache flush before read and after write so that non-cached
413a10d3632SJeenu Viswambharan * participants operate on latest data in main memory.
414a10d3632SJeenu Viswambharan *
415a10d3632SJeenu Viswambharan * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
416a10d3632SJeenu Viswambharan * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
417a10d3632SJeenu Viswambharan * In both cases, no cache operations are required.
418a10d3632SJeenu Viswambharan */
419a10d3632SJeenu Viswambharan
420a10d3632SJeenu Viswambharan /*
421a10d3632SJeenu Viswambharan * Retrieve local state of non-CPU power domain node from a non-cached CPU,
422a10d3632SJeenu Viswambharan * after any required cache maintenance operation.
423a10d3632SJeenu Viswambharan */
get_non_cpu_pd_node_local_state(unsigned int parent_idx)424a10d3632SJeenu Viswambharan static plat_local_state_t get_non_cpu_pd_node_local_state(
425a10d3632SJeenu Viswambharan unsigned int parent_idx)
426a10d3632SJeenu Viswambharan {
427f996a5f7SAndrew F. Davis #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
428a10d3632SJeenu Viswambharan flush_dcache_range(
429a10d3632SJeenu Viswambharan (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
430a10d3632SJeenu Viswambharan sizeof(psci_non_cpu_pd_nodes[parent_idx]));
431a10d3632SJeenu Viswambharan #endif
432a10d3632SJeenu Viswambharan return psci_non_cpu_pd_nodes[parent_idx].local_state;
433a10d3632SJeenu Viswambharan }
434a10d3632SJeenu Viswambharan
435a10d3632SJeenu Viswambharan /*
436a10d3632SJeenu Viswambharan * Update local state of non-CPU power domain node from a cached CPU; perform
437a10d3632SJeenu Viswambharan * any required cache maintenance operation afterwards.
438a10d3632SJeenu Viswambharan */
set_non_cpu_pd_node_local_state(unsigned int parent_idx,plat_local_state_t state)439a10d3632SJeenu Viswambharan static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
440a10d3632SJeenu Viswambharan plat_local_state_t state)
441a10d3632SJeenu Viswambharan {
442a10d3632SJeenu Viswambharan psci_non_cpu_pd_nodes[parent_idx].local_state = state;
443f996a5f7SAndrew F. Davis #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
444a10d3632SJeenu Viswambharan flush_dcache_range(
445a10d3632SJeenu Viswambharan (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
446a10d3632SJeenu Viswambharan sizeof(psci_non_cpu_pd_nodes[parent_idx]));
447a10d3632SJeenu Viswambharan #endif
448a10d3632SJeenu Viswambharan }
449a10d3632SJeenu Viswambharan
450532ed618SSoby Mathew /******************************************************************************
451532ed618SSoby Mathew * Helper function to return the current local power state of each power domain
452532ed618SSoby Mathew * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
453532ed618SSoby Mathew * function will be called after a cpu is powered on to find the local state
454532ed618SSoby Mathew * each power domain has emerged from.
455532ed618SSoby Mathew *****************************************************************************/
psci_get_target_local_pwr_states(unsigned int cpu_idx,unsigned int end_pwrlvl,psci_power_state_t * target_state)4563b802105SBoyan Karatotev void psci_get_target_local_pwr_states(unsigned int cpu_idx, unsigned int end_pwrlvl,
457532ed618SSoby Mathew psci_power_state_t *target_state)
458532ed618SSoby Mathew {
459532ed618SSoby Mathew unsigned int parent_idx, lvl;
460532ed618SSoby Mathew plat_local_state_t *pd_state = target_state->pwr_domain_state;
461532ed618SSoby Mathew
462532ed618SSoby Mathew pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
463*9f407e44SRohit Mathew parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
464532ed618SSoby Mathew
465532ed618SSoby Mathew /* Copy the local power state from node to state_info */
4666b7b0f36SAntonio Nino Diaz for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
467a10d3632SJeenu Viswambharan pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
468532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
469532ed618SSoby Mathew }
470532ed618SSoby Mathew
471532ed618SSoby Mathew /* Set the the higher levels to RUN */
472c7b0a28dSMaheedhar Bollapalli for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
473532ed618SSoby Mathew target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
474532ed618SSoby Mathew }
475c7b0a28dSMaheedhar Bollapalli }
476532ed618SSoby Mathew
477532ed618SSoby Mathew /******************************************************************************
478532ed618SSoby Mathew * Helper function to set the target local power state that each power domain
479532ed618SSoby Mathew * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
480532ed618SSoby Mathew * enter. This function will be called after coordination of requested power
481532ed618SSoby Mathew * states has been done for each power level.
482532ed618SSoby Mathew *****************************************************************************/
psci_set_target_local_pwr_states(unsigned int cpu_idx,unsigned int end_pwrlvl,const psci_power_state_t * target_state)4833b802105SBoyan Karatotev void psci_set_target_local_pwr_states(unsigned int cpu_idx, unsigned int end_pwrlvl,
484532ed618SSoby Mathew const psci_power_state_t *target_state)
485532ed618SSoby Mathew {
486532ed618SSoby Mathew unsigned int parent_idx, lvl;
487532ed618SSoby Mathew const plat_local_state_t *pd_state = target_state->pwr_domain_state;
488532ed618SSoby Mathew
489532ed618SSoby Mathew psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
490532ed618SSoby Mathew
491532ed618SSoby Mathew /*
492a10d3632SJeenu Viswambharan * Need to flush as local_state might be accessed with Data Cache
493532ed618SSoby Mathew * disabled during power on
494532ed618SSoby Mathew */
495a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data.local_state);
496532ed618SSoby Mathew
497*9f407e44SRohit Mathew parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
498532ed618SSoby Mathew
499532ed618SSoby Mathew /* Copy the local_state from state_info */
5006b7b0f36SAntonio Nino Diaz for (lvl = 1U; lvl <= end_pwrlvl; lvl++) {
501a10d3632SJeenu Viswambharan set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
502532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
503532ed618SSoby Mathew }
504532ed618SSoby Mathew }
505532ed618SSoby Mathew
506532ed618SSoby Mathew /*******************************************************************************
507532ed618SSoby Mathew * PSCI helper function to get the parent nodes corresponding to a cpu_index.
508532ed618SSoby Mathew ******************************************************************************/
psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,unsigned int end_lvl,unsigned int * node_index)509fc81021aSDeepika Bhavnani void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
510532ed618SSoby Mathew unsigned int end_lvl,
5116b7b0f36SAntonio Nino Diaz unsigned int *node_index)
512532ed618SSoby Mathew {
513*9f407e44SRohit Mathew unsigned int parent_node =
514*9f407e44SRohit Mathew PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
5156311f63dSVarun Wadekar unsigned int i;
5166b7b0f36SAntonio Nino Diaz unsigned int *node = node_index;
517532ed618SSoby Mathew
5186b7b0f36SAntonio Nino Diaz for (i = PSCI_CPU_PWR_LVL + 1U; i <= end_lvl; i++) {
5196b7b0f36SAntonio Nino Diaz *node = parent_node;
5206b7b0f36SAntonio Nino Diaz node++;
521532ed618SSoby Mathew parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
522532ed618SSoby Mathew }
523532ed618SSoby Mathew }
524532ed618SSoby Mathew
525532ed618SSoby Mathew /******************************************************************************
526532ed618SSoby Mathew * This function is invoked post CPU power up and initialization. It sets the
527532ed618SSoby Mathew * affinity info state, target power state and requested power state for the
528532ed618SSoby Mathew * current CPU and all its ancestor power domains to RUN.
529532ed618SSoby Mathew *****************************************************************************/
psci_set_pwr_domains_to_run(unsigned int cpu_idx,unsigned int end_pwrlvl)5303b802105SBoyan Karatotev void psci_set_pwr_domains_to_run(unsigned int cpu_idx, unsigned int end_pwrlvl)
531532ed618SSoby Mathew {
5323b802105SBoyan Karatotev unsigned int parent_idx, lvl;
533*9f407e44SRohit Mathew parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
534532ed618SSoby Mathew
535532ed618SSoby Mathew /* Reset the local_state to RUN for the non cpu power domains. */
5366b7b0f36SAntonio Nino Diaz for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
537a10d3632SJeenu Viswambharan set_non_cpu_pd_node_local_state(parent_idx,
538a10d3632SJeenu Viswambharan PSCI_LOCAL_STATE_RUN);
539532ed618SSoby Mathew psci_set_req_local_pwr_state(lvl,
540532ed618SSoby Mathew cpu_idx,
541532ed618SSoby Mathew PSCI_LOCAL_STATE_RUN);
542532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
543532ed618SSoby Mathew }
544532ed618SSoby Mathew
545532ed618SSoby Mathew /* Set the affinity info state to ON */
546532ed618SSoby Mathew psci_set_aff_info_state(AFF_STATE_ON);
547532ed618SSoby Mathew
548532ed618SSoby Mathew psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
549a10d3632SJeenu Viswambharan psci_flush_cpu_data(psci_svc_cpu_data);
550532ed618SSoby Mathew }
551532ed618SSoby Mathew
552532ed618SSoby Mathew /******************************************************************************
553606b7430SWing Li * This function is used in platform-coordinated mode.
554606b7430SWing Li *
555532ed618SSoby Mathew * This function is passed the local power states requested for each power
556532ed618SSoby Mathew * domain (state_info) between the current CPU domain and its ancestors until
557532ed618SSoby Mathew * the target power level (end_pwrlvl). It updates the array of requested power
558532ed618SSoby Mathew * states with this information.
559532ed618SSoby Mathew *
560532ed618SSoby Mathew * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
561532ed618SSoby Mathew * retrieves the states requested by all the cpus of which the power domain at
562532ed618SSoby Mathew * that level is an ancestor. It passes this information to the platform to
563532ed618SSoby Mathew * coordinate and return the target power state. If the target state for a level
564532ed618SSoby Mathew * is RUN then subsequent levels are not considered. At the CPU level, state
565532ed618SSoby Mathew * coordination is not required. Hence, the requested and the target states are
566532ed618SSoby Mathew * the same.
567532ed618SSoby Mathew *
568532ed618SSoby Mathew * The 'state_info' is updated with the target state for each level between the
569532ed618SSoby Mathew * CPU and the 'end_pwrlvl' and returned to the caller.
570532ed618SSoby Mathew *
571532ed618SSoby Mathew * This function will only be invoked with data cache enabled and while
572532ed618SSoby Mathew * powering down a core.
573532ed618SSoby Mathew *****************************************************************************/
psci_do_state_coordination(unsigned int cpu_idx,unsigned int end_pwrlvl,psci_power_state_t * state_info)5743b802105SBoyan Karatotev void psci_do_state_coordination(unsigned int cpu_idx, unsigned int end_pwrlvl,
575532ed618SSoby Mathew psci_power_state_t *state_info)
576532ed618SSoby Mathew {
5773b802105SBoyan Karatotev unsigned int lvl, parent_idx;
578fc81021aSDeepika Bhavnani unsigned int start_idx;
5796b7b0f36SAntonio Nino Diaz unsigned int ncpus;
5807b970841SNithin G plat_local_state_t target_state;
581532ed618SSoby Mathew
582532ed618SSoby Mathew assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
583*9f407e44SRohit Mathew parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
584532ed618SSoby Mathew
585532ed618SSoby Mathew /* For level 0, the requested state will be equivalent
586532ed618SSoby Mathew to target state */
5876b7b0f36SAntonio Nino Diaz for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
588532ed618SSoby Mathew
589532ed618SSoby Mathew /* First update the requested power state */
590532ed618SSoby Mathew psci_set_req_local_pwr_state(lvl, cpu_idx,
591532ed618SSoby Mathew state_info->pwr_domain_state[lvl]);
592532ed618SSoby Mathew
593532ed618SSoby Mathew /* Get the requested power states for this power level */
594532ed618SSoby Mathew start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
5957b970841SNithin G plat_local_state_t const *req_states = psci_get_req_local_pwr_states(lvl,
5967b970841SNithin G start_idx);
597532ed618SSoby Mathew
598532ed618SSoby Mathew /*
599532ed618SSoby Mathew * Let the platform coordinate amongst the requested states at
600532ed618SSoby Mathew * this power level and return the target local power state.
601532ed618SSoby Mathew */
602532ed618SSoby Mathew ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
603532ed618SSoby Mathew target_state = plat_get_target_pwr_state(lvl,
604532ed618SSoby Mathew req_states,
605532ed618SSoby Mathew ncpus);
606532ed618SSoby Mathew
607532ed618SSoby Mathew state_info->pwr_domain_state[lvl] = target_state;
608532ed618SSoby Mathew
609532ed618SSoby Mathew /* Break early if the negotiated target power state is RUN */
610c7b0a28dSMaheedhar Bollapalli if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0) {
611532ed618SSoby Mathew break;
612c7b0a28dSMaheedhar Bollapalli }
613532ed618SSoby Mathew
614532ed618SSoby Mathew parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
615532ed618SSoby Mathew }
616532ed618SSoby Mathew
617532ed618SSoby Mathew /*
618532ed618SSoby Mathew * This is for cases when we break out of the above loop early because
619532ed618SSoby Mathew * the target power state is RUN at a power level < end_pwlvl.
620532ed618SSoby Mathew * We update the requested power state from state_info and then
621532ed618SSoby Mathew * set the target state as RUN.
622532ed618SSoby Mathew */
6236b7b0f36SAntonio Nino Diaz for (lvl = lvl + 1U; lvl <= end_pwrlvl; lvl++) {
624532ed618SSoby Mathew psci_set_req_local_pwr_state(lvl, cpu_idx,
625532ed618SSoby Mathew state_info->pwr_domain_state[lvl]);
626532ed618SSoby Mathew state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
627532ed618SSoby Mathew
628532ed618SSoby Mathew }
629532ed618SSoby Mathew }
630532ed618SSoby Mathew
631606b7430SWing Li #if PSCI_OS_INIT_MODE
632606b7430SWing Li /******************************************************************************
633606b7430SWing Li * This function is used in OS-initiated mode.
634606b7430SWing Li *
635606b7430SWing Li * This function is passed the local power states requested for each power
636606b7430SWing Li * domain (state_info) between the current CPU domain and its ancestors until
637606b7430SWing Li * the target power level (end_pwrlvl), and ensures the requested power states
638606b7430SWing Li * are valid. It updates the array of requested power states with this
639606b7430SWing Li * information.
640606b7430SWing Li *
641606b7430SWing Li * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
642606b7430SWing Li * retrieves the states requested by all the cpus of which the power domain at
643606b7430SWing Li * that level is an ancestor. It passes this information to the platform to
644606b7430SWing Li * coordinate and return the target power state. If the requested state does
645606b7430SWing Li * not match the target state, the request is denied.
646606b7430SWing Li *
647606b7430SWing Li * The 'state_info' is not modified.
648606b7430SWing Li *
649606b7430SWing Li * This function will only be invoked with data cache enabled and while
650606b7430SWing Li * powering down a core.
651606b7430SWing Li *****************************************************************************/
psci_validate_state_coordination(unsigned int cpu_idx,unsigned int end_pwrlvl,psci_power_state_t * state_info)6523b802105SBoyan Karatotev int psci_validate_state_coordination(unsigned int cpu_idx, unsigned int end_pwrlvl,
653606b7430SWing Li psci_power_state_t *state_info)
654606b7430SWing Li {
655606b7430SWing Li int rc = PSCI_E_SUCCESS;
6563b802105SBoyan Karatotev unsigned int lvl, parent_idx;
657606b7430SWing Li unsigned int start_idx;
658606b7430SWing Li unsigned int ncpus;
659606b7430SWing Li plat_local_state_t target_state, *req_states;
660606b7430SWing Li plat_local_state_t prev[PLAT_MAX_PWR_LVL];
661606b7430SWing Li
662606b7430SWing Li assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
663*9f407e44SRohit Mathew parent_idx = PER_CPU_BY_INDEX(psci_cpu_pd_nodes, cpu_idx)->parent_node;
664606b7430SWing Li
665606b7430SWing Li /*
666606b7430SWing Li * Save a copy of the previous requested local power states and update
667606b7430SWing Li * the new requested local power states.
668606b7430SWing Li */
669606b7430SWing Li psci_update_req_local_pwr_states(end_pwrlvl, cpu_idx, state_info, prev);
670606b7430SWing Li
671606b7430SWing Li for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
672606b7430SWing Li /* Get the requested power states for this power level */
673606b7430SWing Li start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
674606b7430SWing Li req_states = psci_get_req_local_pwr_states(lvl, start_idx);
675606b7430SWing Li
676606b7430SWing Li /*
677606b7430SWing Li * Let the platform coordinate amongst the requested states at
678606b7430SWing Li * this power level and return the target local power state.
679606b7430SWing Li */
680606b7430SWing Li ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
681606b7430SWing Li target_state = plat_get_target_pwr_state(lvl,
682606b7430SWing Li req_states,
683606b7430SWing Li ncpus);
684606b7430SWing Li
685606b7430SWing Li /*
686606b7430SWing Li * Verify that the requested power state matches the target
687606b7430SWing Li * local power state.
688606b7430SWing Li */
689606b7430SWing Li if (state_info->pwr_domain_state[lvl] != target_state) {
690606b7430SWing Li if (target_state == PSCI_LOCAL_STATE_RUN) {
691606b7430SWing Li rc = PSCI_E_DENIED;
692606b7430SWing Li } else {
693606b7430SWing Li rc = PSCI_E_INVALID_PARAMS;
694606b7430SWing Li }
695606b7430SWing Li goto exit;
696606b7430SWing Li }
697412d92fdSPatrick Delaunay
698412d92fdSPatrick Delaunay parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
699606b7430SWing Li }
700606b7430SWing Li
701606b7430SWing Li /*
702606b7430SWing Li * Verify that the current core is the last running core at the
703606b7430SWing Li * specified power level.
704606b7430SWing Li */
705606b7430SWing Li lvl = state_info->last_at_pwrlvl;
7063b802105SBoyan Karatotev if (!psci_is_last_cpu_to_idle_at_pwrlvl(cpu_idx, lvl)) {
707606b7430SWing Li rc = PSCI_E_DENIED;
708606b7430SWing Li }
709606b7430SWing Li
710606b7430SWing Li exit:
711606b7430SWing Li if (rc != PSCI_E_SUCCESS) {
712606b7430SWing Li /* Restore the previous requested local power states. */
713606b7430SWing Li psci_restore_req_local_pwr_states(cpu_idx, prev);
714606b7430SWing Li return rc;
715606b7430SWing Li }
716606b7430SWing Li
717606b7430SWing Li return rc;
718606b7430SWing Li }
719606b7430SWing Li #endif
720606b7430SWing Li
721532ed618SSoby Mathew /******************************************************************************
722532ed618SSoby Mathew * This function validates a suspend request by making sure that if a standby
723532ed618SSoby Mathew * state is requested then no power level is turned off and the highest power
724532ed618SSoby Mathew * level is placed in a standby/retention state.
725532ed618SSoby Mathew *
726532ed618SSoby Mathew * It also ensures that the state level X will enter is not shallower than the
727532ed618SSoby Mathew * state level X + 1 will enter.
728532ed618SSoby Mathew *
729532ed618SSoby Mathew * This validation will be enabled only for DEBUG builds as the platform is
730532ed618SSoby Mathew * expected to perform these validations as well.
731532ed618SSoby Mathew *****************************************************************************/
psci_validate_suspend_req(const psci_power_state_t * state_info,unsigned int is_power_down_state)732532ed618SSoby Mathew int psci_validate_suspend_req(const psci_power_state_t *state_info,
733532ed618SSoby Mathew unsigned int is_power_down_state)
734532ed618SSoby Mathew {
735532ed618SSoby Mathew unsigned int max_off_lvl, target_lvl, max_retn_lvl;
736532ed618SSoby Mathew plat_local_state_t state;
737532ed618SSoby Mathew plat_local_state_type_t req_state_type, deepest_state_type;
738532ed618SSoby Mathew int i;
739532ed618SSoby Mathew
740532ed618SSoby Mathew /* Find the target suspend power level */
741532ed618SSoby Mathew target_lvl = psci_find_target_suspend_lvl(state_info);
742bac32cc4SSaivardhan Thatikonda if (target_lvl == PSCI_INVALID_PWR_LVL) {
743532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS;
744bac32cc4SSaivardhan Thatikonda }
745532ed618SSoby Mathew
746532ed618SSoby Mathew /* All power domain levels are in a RUN state to begin with */
747532ed618SSoby Mathew deepest_state_type = STATE_TYPE_RUN;
748532ed618SSoby Mathew
7496b7b0f36SAntonio Nino Diaz for (i = (int) target_lvl; i >= (int) PSCI_CPU_PWR_LVL; i--) {
750532ed618SSoby Mathew state = state_info->pwr_domain_state[i];
751532ed618SSoby Mathew req_state_type = find_local_state_type(state);
752532ed618SSoby Mathew
753532ed618SSoby Mathew /*
754532ed618SSoby Mathew * While traversing from the highest power level to the lowest,
755532ed618SSoby Mathew * the state requested for lower levels has to be the same or
756532ed618SSoby Mathew * deeper i.e. equal to or greater than the state at the higher
757532ed618SSoby Mathew * levels. If this condition is true, then the requested state
758532ed618SSoby Mathew * becomes the deepest state encountered so far.
759532ed618SSoby Mathew */
760bac32cc4SSaivardhan Thatikonda if (req_state_type < deepest_state_type) {
761532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS;
762bac32cc4SSaivardhan Thatikonda }
763532ed618SSoby Mathew deepest_state_type = req_state_type;
764532ed618SSoby Mathew }
765532ed618SSoby Mathew
766532ed618SSoby Mathew /* Find the highest off power level */
767532ed618SSoby Mathew max_off_lvl = psci_find_max_off_lvl(state_info);
768532ed618SSoby Mathew
769532ed618SSoby Mathew /* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
770532ed618SSoby Mathew max_retn_lvl = PSCI_INVALID_PWR_LVL;
771bac32cc4SSaivardhan Thatikonda if (target_lvl != max_off_lvl) {
772532ed618SSoby Mathew max_retn_lvl = target_lvl;
773bac32cc4SSaivardhan Thatikonda }
774532ed618SSoby Mathew
775532ed618SSoby Mathew /*
776532ed618SSoby Mathew * If this is not a request for a power down state then max off level
777532ed618SSoby Mathew * has to be invalid and max retention level has to be a valid power
778532ed618SSoby Mathew * level.
779532ed618SSoby Mathew */
7806b7b0f36SAntonio Nino Diaz if ((is_power_down_state == 0U) &&
7816b7b0f36SAntonio Nino Diaz ((max_off_lvl != PSCI_INVALID_PWR_LVL) ||
782bac32cc4SSaivardhan Thatikonda (max_retn_lvl == PSCI_INVALID_PWR_LVL))) {
783532ed618SSoby Mathew return PSCI_E_INVALID_PARAMS;
784bac32cc4SSaivardhan Thatikonda }
785532ed618SSoby Mathew
786532ed618SSoby Mathew return PSCI_E_SUCCESS;
787532ed618SSoby Mathew }
788532ed618SSoby Mathew
789532ed618SSoby Mathew /******************************************************************************
790532ed618SSoby Mathew * This function finds the highest power level which will be powered down
791532ed618SSoby Mathew * amongst all the power levels specified in the 'state_info' structure
792532ed618SSoby Mathew *****************************************************************************/
psci_find_max_off_lvl(const psci_power_state_t * state_info)793532ed618SSoby Mathew unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
794532ed618SSoby Mathew {
795532ed618SSoby Mathew int i;
796532ed618SSoby Mathew
7976b7b0f36SAntonio Nino Diaz for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
798c7b0a28dSMaheedhar Bollapalli if (is_local_state_off(state_info->pwr_domain_state[i]) != 0) {
7996b7b0f36SAntonio Nino Diaz return (unsigned int) i;
800532ed618SSoby Mathew }
801c7b0a28dSMaheedhar Bollapalli }
802532ed618SSoby Mathew
803532ed618SSoby Mathew return PSCI_INVALID_PWR_LVL;
804532ed618SSoby Mathew }
805532ed618SSoby Mathew
806532ed618SSoby Mathew /******************************************************************************
807532ed618SSoby Mathew * This functions finds the level of the highest power domain which will be
808532ed618SSoby Mathew * placed in a low power state during a suspend operation.
809532ed618SSoby Mathew *****************************************************************************/
psci_find_target_suspend_lvl(const psci_power_state_t * state_info)810532ed618SSoby Mathew unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
811532ed618SSoby Mathew {
812532ed618SSoby Mathew int i;
813532ed618SSoby Mathew
8146b7b0f36SAntonio Nino Diaz for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
815bac32cc4SSaivardhan Thatikonda if (is_local_state_run(state_info->pwr_domain_state[i]) == 0) {
8166b7b0f36SAntonio Nino Diaz return (unsigned int) i;
817532ed618SSoby Mathew }
818bac32cc4SSaivardhan Thatikonda }
819532ed618SSoby Mathew
820532ed618SSoby Mathew return PSCI_INVALID_PWR_LVL;
821532ed618SSoby Mathew }
822532ed618SSoby Mathew
823532ed618SSoby Mathew /*******************************************************************************
82474d27d00SAndrew F. Davis * This function is passed the highest level in the topology tree that the
82574d27d00SAndrew F. Davis * operation should be applied to and a list of node indexes. It picks up locks
82674d27d00SAndrew F. Davis * from the node index list in order of increasing power domain level in the
82774d27d00SAndrew F. Davis * range specified.
828532ed618SSoby Mathew ******************************************************************************/
psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,const unsigned int * parent_nodes)82974d27d00SAndrew F. Davis void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
83074d27d00SAndrew F. Davis const unsigned int *parent_nodes)
831532ed618SSoby Mathew {
83274d27d00SAndrew F. Davis unsigned int parent_idx;
833532ed618SSoby Mathew unsigned int level;
834532ed618SSoby Mathew
835532ed618SSoby Mathew /* No locking required for level 0. Hence start locking from level 1 */
8366b7b0f36SAntonio Nino Diaz for (level = PSCI_CPU_PWR_LVL + 1U; level <= end_pwrlvl; level++) {
83774d27d00SAndrew F. Davis parent_idx = parent_nodes[level - 1U];
838532ed618SSoby Mathew psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
839532ed618SSoby Mathew }
840532ed618SSoby Mathew }
841532ed618SSoby Mathew
842532ed618SSoby Mathew /*******************************************************************************
84374d27d00SAndrew F. Davis * This function is passed the highest level in the topology tree that the
84474d27d00SAndrew F. Davis * operation should be applied to and a list of node indexes. It releases the
84574d27d00SAndrew F. Davis * locks in order of decreasing power domain level in the range specified.
846532ed618SSoby Mathew ******************************************************************************/
psci_release_pwr_domain_locks(unsigned int end_pwrlvl,const unsigned int * parent_nodes)84774d27d00SAndrew F. Davis void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
84874d27d00SAndrew F. Davis const unsigned int *parent_nodes)
849532ed618SSoby Mathew {
85074d27d00SAndrew F. Davis unsigned int parent_idx;
8516b7b0f36SAntonio Nino Diaz unsigned int level;
852532ed618SSoby Mathew
853532ed618SSoby Mathew /* Unlock top down. No unlocking required for level 0. */
8542fe75a2dSZelalem for (level = end_pwrlvl; level >= (PSCI_CPU_PWR_LVL + 1U); level--) {
8556b7b0f36SAntonio Nino Diaz parent_idx = parent_nodes[level - 1U];
856532ed618SSoby Mathew psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
857532ed618SSoby Mathew }
858532ed618SSoby Mathew }
859532ed618SSoby Mathew
860532ed618SSoby Mathew /*******************************************************************************
861532ed618SSoby Mathew * This function determines the full entrypoint information for the requested
862532ed618SSoby Mathew * PSCI entrypoint on power on/resume and returns it.
863532ed618SSoby Mathew ******************************************************************************/
864402b3cf8SJulius Werner #ifdef __aarch64__
psci_get_ns_ep_info(entry_point_info_t * ep,uintptr_t entrypoint,u_register_t context_id)865532ed618SSoby Mathew static int psci_get_ns_ep_info(entry_point_info_t *ep,
866532ed618SSoby Mathew uintptr_t entrypoint,
867532ed618SSoby Mathew u_register_t context_id)
868532ed618SSoby Mathew {
869532ed618SSoby Mathew u_register_t ep_attr, sctlr;
870532ed618SSoby Mathew unsigned int daif, ee, mode;
871532ed618SSoby Mathew u_register_t ns_scr_el3 = read_scr_el3();
872532ed618SSoby Mathew u_register_t ns_sctlr_el1 = read_sctlr_el1();
873532ed618SSoby Mathew
8746b7b0f36SAntonio Nino Diaz sctlr = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
8756b7b0f36SAntonio Nino Diaz read_sctlr_el2() : ns_sctlr_el1;
876532ed618SSoby Mathew ee = 0;
877532ed618SSoby Mathew
878532ed618SSoby Mathew ep_attr = NON_SECURE | EP_ST_DISABLE;
8796b7b0f36SAntonio Nino Diaz if ((sctlr & SCTLR_EE_BIT) != 0U) {
880532ed618SSoby Mathew ep_attr |= EP_EE_BIG;
881532ed618SSoby Mathew ee = 1;
882532ed618SSoby Mathew }
883532ed618SSoby Mathew SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
884532ed618SSoby Mathew
885532ed618SSoby Mathew ep->pc = entrypoint;
88632f0d3c6SDouglas Raillard zeromem(&ep->args, sizeof(ep->args));
887532ed618SSoby Mathew ep->args.arg0 = context_id;
888532ed618SSoby Mathew
889532ed618SSoby Mathew /*
890532ed618SSoby Mathew * Figure out whether the cpu enters the non-secure address space
891532ed618SSoby Mathew * in aarch32 or aarch64
892532ed618SSoby Mathew */
8936b7b0f36SAntonio Nino Diaz if ((ns_scr_el3 & SCR_RW_BIT) != 0U) {
894532ed618SSoby Mathew
895532ed618SSoby Mathew /*
896532ed618SSoby Mathew * Check whether a Thumb entry point has been provided for an
897532ed618SSoby Mathew * aarch64 EL
898532ed618SSoby Mathew */
899bac32cc4SSaivardhan Thatikonda if ((entrypoint & 0x1UL) != 0UL) {
900532ed618SSoby Mathew return PSCI_E_INVALID_ADDRESS;
901bac32cc4SSaivardhan Thatikonda }
902532ed618SSoby Mathew
9036b7b0f36SAntonio Nino Diaz mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? MODE_EL2 : MODE_EL1;
904532ed618SSoby Mathew
905d7b5f408SJimmy Brisson ep->spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX,
906d7b5f408SJimmy Brisson DISABLE_ALL_EXCEPTIONS);
907532ed618SSoby Mathew } else {
908532ed618SSoby Mathew
9096b7b0f36SAntonio Nino Diaz mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
9106b7b0f36SAntonio Nino Diaz MODE32_hyp : MODE32_svc;
911532ed618SSoby Mathew
912532ed618SSoby Mathew /*
913532ed618SSoby Mathew * TODO: Choose async. exception bits if HYP mode is not
914532ed618SSoby Mathew * implemented according to the values of SCR.{AW, FW} bits
915532ed618SSoby Mathew */
916532ed618SSoby Mathew daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
917532ed618SSoby Mathew
918d7b5f408SJimmy Brisson ep->spsr = SPSR_MODE32((uint64_t)mode, entrypoint & 0x1, ee,
919d7b5f408SJimmy Brisson daif);
920532ed618SSoby Mathew }
921532ed618SSoby Mathew
922532ed618SSoby Mathew return PSCI_E_SUCCESS;
923532ed618SSoby Mathew }
924402b3cf8SJulius Werner #else /* !__aarch64__ */
psci_get_ns_ep_info(entry_point_info_t * ep,uintptr_t entrypoint,u_register_t context_id)925402b3cf8SJulius Werner static int psci_get_ns_ep_info(entry_point_info_t *ep,
926402b3cf8SJulius Werner uintptr_t entrypoint,
927402b3cf8SJulius Werner u_register_t context_id)
928402b3cf8SJulius Werner {
929402b3cf8SJulius Werner u_register_t ep_attr;
930402b3cf8SJulius Werner unsigned int aif, ee, mode;
931402b3cf8SJulius Werner u_register_t scr = read_scr();
932402b3cf8SJulius Werner u_register_t ns_sctlr, sctlr;
933402b3cf8SJulius Werner
934402b3cf8SJulius Werner /* Switch to non secure state */
935402b3cf8SJulius Werner write_scr(scr | SCR_NS_BIT);
936402b3cf8SJulius Werner isb();
937402b3cf8SJulius Werner ns_sctlr = read_sctlr();
938402b3cf8SJulius Werner
939402b3cf8SJulius Werner sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
940402b3cf8SJulius Werner
941402b3cf8SJulius Werner /* Return to original state */
942402b3cf8SJulius Werner write_scr(scr);
943402b3cf8SJulius Werner isb();
944402b3cf8SJulius Werner ee = 0;
945402b3cf8SJulius Werner
946402b3cf8SJulius Werner ep_attr = NON_SECURE | EP_ST_DISABLE;
947402b3cf8SJulius Werner if (sctlr & SCTLR_EE_BIT) {
948402b3cf8SJulius Werner ep_attr |= EP_EE_BIG;
949402b3cf8SJulius Werner ee = 1;
950402b3cf8SJulius Werner }
951402b3cf8SJulius Werner SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
952402b3cf8SJulius Werner
953402b3cf8SJulius Werner ep->pc = entrypoint;
954402b3cf8SJulius Werner zeromem(&ep->args, sizeof(ep->args));
955402b3cf8SJulius Werner ep->args.arg0 = context_id;
956402b3cf8SJulius Werner
957402b3cf8SJulius Werner mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
958402b3cf8SJulius Werner
959402b3cf8SJulius Werner /*
960402b3cf8SJulius Werner * TODO: Choose async. exception bits if HYP mode is not
961402b3cf8SJulius Werner * implemented according to the values of SCR.{AW, FW} bits
962402b3cf8SJulius Werner */
963402b3cf8SJulius Werner aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
964402b3cf8SJulius Werner
965402b3cf8SJulius Werner ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
966402b3cf8SJulius Werner
967402b3cf8SJulius Werner return PSCI_E_SUCCESS;
968402b3cf8SJulius Werner }
969402b3cf8SJulius Werner
970402b3cf8SJulius Werner #endif /* __aarch64__ */
971532ed618SSoby Mathew
972532ed618SSoby Mathew /*******************************************************************************
973532ed618SSoby Mathew * This function validates the entrypoint with the platform layer if the
974532ed618SSoby Mathew * appropriate pm_ops hook is exported by the platform and returns the
975532ed618SSoby Mathew * 'entry_point_info'.
976532ed618SSoby Mathew ******************************************************************************/
psci_validate_entry_point(entry_point_info_t * ep,uintptr_t entrypoint,u_register_t context_id)977532ed618SSoby Mathew int psci_validate_entry_point(entry_point_info_t *ep,
978532ed618SSoby Mathew uintptr_t entrypoint,
979532ed618SSoby Mathew u_register_t context_id)
980532ed618SSoby Mathew {
981532ed618SSoby Mathew int rc;
982532ed618SSoby Mathew
983532ed618SSoby Mathew /* Validate the entrypoint using platform psci_ops */
9846b7b0f36SAntonio Nino Diaz if (psci_plat_pm_ops->validate_ns_entrypoint != NULL) {
985532ed618SSoby Mathew rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
986c7b0a28dSMaheedhar Bollapalli if (rc != PSCI_E_SUCCESS) {
987532ed618SSoby Mathew return PSCI_E_INVALID_ADDRESS;
988532ed618SSoby Mathew }
989c7b0a28dSMaheedhar Bollapalli }
990532ed618SSoby Mathew
991532ed618SSoby Mathew /*
992532ed618SSoby Mathew * Verify and derive the re-entry information for
993532ed618SSoby Mathew * the non-secure world from the non-secure state from
994532ed618SSoby Mathew * where this call originated.
995532ed618SSoby Mathew */
996532ed618SSoby Mathew rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
997532ed618SSoby Mathew return rc;
998532ed618SSoby Mathew }
999532ed618SSoby Mathew
1000532ed618SSoby Mathew /*******************************************************************************
1001532ed618SSoby Mathew * Generic handler which is called when a cpu is physically powered on. It
1002532ed618SSoby Mathew * traverses the node information and finds the highest power level powered
1003532ed618SSoby Mathew * off and performs generic, architectural, platform setup and state management
1004532ed618SSoby Mathew * to power on that power level and power levels below it.
1005532ed618SSoby Mathew * e.g. For a cpu that's been powered on, it will call the platform specific
1006532ed618SSoby Mathew * code to enable the gic cpu interface and for a cluster it will enable
1007532ed618SSoby Mathew * coherency at the interconnect level in addition to gic cpu interface.
1008532ed618SSoby Mathew ******************************************************************************/
psci_warmboot_entrypoint(unsigned int cpu_idx)100963900851SBoyan Karatotev void psci_warmboot_entrypoint(unsigned int cpu_idx)
1010532ed618SSoby Mathew {
10116b7b0f36SAntonio Nino Diaz unsigned int end_pwrlvl;
101274d27d00SAndrew F. Davis unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1013532ed618SSoby Mathew psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
1014532ed618SSoby Mathew
1015532ed618SSoby Mathew /*
1016532ed618SSoby Mathew * Verify that we have been explicitly turned ON or resumed from
1017532ed618SSoby Mathew * suspend.
1018532ed618SSoby Mathew */
1019532ed618SSoby Mathew if (psci_get_aff_info_state() == AFF_STATE_OFF) {
102033e8c569SAndrew Walbran ERROR("Unexpected affinity info state.\n");
1021532ed618SSoby Mathew panic();
1022532ed618SSoby Mathew }
1023532ed618SSoby Mathew
1024532ed618SSoby Mathew /*
1025532ed618SSoby Mathew * Get the maximum power domain level to traverse to after this cpu
1026532ed618SSoby Mathew * has been physically powered up.
1027532ed618SSoby Mathew */
1028532ed618SSoby Mathew end_pwrlvl = get_power_on_target_pwrlvl();
1029532ed618SSoby Mathew
103074d27d00SAndrew F. Davis /* Get the parent nodes */
103174d27d00SAndrew F. Davis psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
103274d27d00SAndrew F. Davis
1033532ed618SSoby Mathew /*
1034532ed618SSoby Mathew * This function acquires the lock corresponding to each power level so
1035532ed618SSoby Mathew * that by the time all locks are taken, the system topology is snapshot
1036532ed618SSoby Mathew * and state management can be done safely.
1037532ed618SSoby Mathew */
103874d27d00SAndrew F. Davis psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
1039532ed618SSoby Mathew
10403b802105SBoyan Karatotev psci_get_target_local_pwr_states(cpu_idx, end_pwrlvl, &state_info);
1041bfc87a8dSSoby Mathew
1042532ed618SSoby Mathew #if ENABLE_PSCI_STAT
104304c1db1eSdp-arm plat_psci_stat_accounting_stop(&state_info);
1044532ed618SSoby Mathew #endif
1045532ed618SSoby Mathew
1046532ed618SSoby Mathew /*
1047532ed618SSoby Mathew * This CPU could be resuming from suspend or it could have just been
1048532ed618SSoby Mathew * turned on. To distinguish between these 2 cases, we examine the
1049532ed618SSoby Mathew * affinity state of the CPU:
1050532ed618SSoby Mathew * - If the affinity state is ON_PENDING then it has just been
1051532ed618SSoby Mathew * turned on.
1052532ed618SSoby Mathew * - Else it is resuming from suspend.
1053532ed618SSoby Mathew *
1054532ed618SSoby Mathew * Depending on the type of warm reset identified, choose the right set
1055532ed618SSoby Mathew * of power management handler and perform the generic, architecture
1056532ed618SSoby Mathew * and platform specific handling.
1057532ed618SSoby Mathew */
1058c7b0a28dSMaheedhar Bollapalli if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING) {
1059532ed618SSoby Mathew psci_cpu_on_finish(cpu_idx, &state_info);
1060c7b0a28dSMaheedhar Bollapalli } else {
10612b5e00d4SBoyan Karatotev unsigned int max_off_lvl = psci_find_max_off_lvl(&state_info);
10622b5e00d4SBoyan Karatotev
10632b5e00d4SBoyan Karatotev assert(max_off_lvl != PSCI_INVALID_PWR_LVL);
106404c39e46SBoyan Karatotev psci_cpu_suspend_to_powerdown_finish(cpu_idx, max_off_lvl, &state_info, false);
10652b5e00d4SBoyan Karatotev }
1066532ed618SSoby Mathew
1067532ed618SSoby Mathew /*
1068ef738d19SManish Pandey * Caches and (importantly) coherency are on so we can rely on seeing
1069ef738d19SManish Pandey * whatever the primary gave us without explicit cache maintenance
1070ef738d19SManish Pandey */
1071ef738d19SManish Pandey entry_point_info_t *ep = get_cpu_data(warmboot_ep_info);
1072ef738d19SManish Pandey cm_init_my_context(ep);
1073ef738d19SManish Pandey
1074ef738d19SManish Pandey /*
1075e07e7392SBoyan Karatotev * Generic management: Now we just need to retrieve the
1076e07e7392SBoyan Karatotev * information that we had stashed away during the cpu_on
1077e07e7392SBoyan Karatotev * call to set this cpu on its way.
1078e07e7392SBoyan Karatotev */
1079e07e7392SBoyan Karatotev cm_prepare_el3_exit_ns();
1080e07e7392SBoyan Karatotev
1081e07e7392SBoyan Karatotev /*
1082532ed618SSoby Mathew * Set the requested and target state of this CPU and all the higher
1083532ed618SSoby Mathew * power domains which are ancestors of this CPU to run.
1084532ed618SSoby Mathew */
10853b802105SBoyan Karatotev psci_set_pwr_domains_to_run(cpu_idx, end_pwrlvl);
1086532ed618SSoby Mathew
1087532ed618SSoby Mathew #if ENABLE_PSCI_STAT
10883b802105SBoyan Karatotev psci_stats_update_pwr_up(cpu_idx, end_pwrlvl, &state_info);
1089532ed618SSoby Mathew #endif
1090532ed618SSoby Mathew
1091532ed618SSoby Mathew /*
1092532ed618SSoby Mathew * This loop releases the lock corresponding to each power level
1093532ed618SSoby Mathew * in the reverse order to which they were acquired.
1094532ed618SSoby Mathew */
109574d27d00SAndrew F. Davis psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
1096532ed618SSoby Mathew }
1097532ed618SSoby Mathew
1098532ed618SSoby Mathew /*******************************************************************************
1099532ed618SSoby Mathew * This function initializes the set of hooks that PSCI invokes as part of power
1100532ed618SSoby Mathew * management operation. The power management hooks are expected to be provided
1101532ed618SSoby Mathew * by the SPD, after it finishes all its initialization
1102532ed618SSoby Mathew ******************************************************************************/
psci_register_spd_pm_hook(const spd_pm_ops_t * pm)1103532ed618SSoby Mathew void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
1104532ed618SSoby Mathew {
11056b7b0f36SAntonio Nino Diaz assert(pm != NULL);
1106532ed618SSoby Mathew psci_spd_pm = pm;
1107532ed618SSoby Mathew
1108bac32cc4SSaivardhan Thatikonda if (pm->svc_migrate != NULL) {
1109532ed618SSoby Mathew psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
1110bac32cc4SSaivardhan Thatikonda }
1111532ed618SSoby Mathew
1112bac32cc4SSaivardhan Thatikonda if (pm->svc_migrate_info != NULL) {
1113532ed618SSoby Mathew psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
1114532ed618SSoby Mathew | define_psci_cap(PSCI_MIG_INFO_TYPE);
1115532ed618SSoby Mathew }
1116bac32cc4SSaivardhan Thatikonda }
1117532ed618SSoby Mathew
1118532ed618SSoby Mathew /*******************************************************************************
1119532ed618SSoby Mathew * This function invokes the migrate info hook in the spd_pm_ops. It performs
1120532ed618SSoby Mathew * the necessary return value validation. If the Secure Payload is UP and
1121532ed618SSoby Mathew * migrate capable, it returns the mpidr of the CPU on which the Secure payload
1122532ed618SSoby Mathew * is resident through the mpidr parameter. Else the value of the parameter on
1123532ed618SSoby Mathew * return is undefined.
1124532ed618SSoby Mathew ******************************************************************************/
psci_spd_migrate_info(u_register_t * mpidr)1125532ed618SSoby Mathew int psci_spd_migrate_info(u_register_t *mpidr)
1126532ed618SSoby Mathew {
1127532ed618SSoby Mathew int rc;
1128532ed618SSoby Mathew
1129bac32cc4SSaivardhan Thatikonda if ((psci_spd_pm == NULL) || (psci_spd_pm->svc_migrate_info == NULL)) {
1130532ed618SSoby Mathew return PSCI_E_NOT_SUPPORTED;
1131bac32cc4SSaivardhan Thatikonda }
1132532ed618SSoby Mathew
1133532ed618SSoby Mathew rc = psci_spd_pm->svc_migrate_info(mpidr);
1134532ed618SSoby Mathew
11356b7b0f36SAntonio Nino Diaz assert((rc == PSCI_TOS_UP_MIG_CAP) || (rc == PSCI_TOS_NOT_UP_MIG_CAP) ||
11366b7b0f36SAntonio Nino Diaz (rc == PSCI_TOS_NOT_PRESENT_MP) || (rc == PSCI_E_NOT_SUPPORTED));
1137532ed618SSoby Mathew
1138532ed618SSoby Mathew return rc;
1139532ed618SSoby Mathew }
1140532ed618SSoby Mathew
1141532ed618SSoby Mathew
1142532ed618SSoby Mathew /*******************************************************************************
1143532ed618SSoby Mathew * This function prints the state of all power domains present in the
1144532ed618SSoby Mathew * system
1145532ed618SSoby Mathew ******************************************************************************/
psci_print_power_domain_map(void)1146532ed618SSoby Mathew void psci_print_power_domain_map(void)
1147532ed618SSoby Mathew {
1148532ed618SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_INFO
1149ab4df50cSPankaj Gupta unsigned int idx;
1150532ed618SSoby Mathew plat_local_state_t state;
1151532ed618SSoby Mathew plat_local_state_type_t state_type;
1152532ed618SSoby Mathew
1153532ed618SSoby Mathew /* This array maps to the PSCI_STATE_X definitions in psci.h */
1154532ed618SSoby Mathew static const char * const psci_state_type_str[] = {
1155532ed618SSoby Mathew "ON",
1156532ed618SSoby Mathew "RETENTION",
1157532ed618SSoby Mathew "OFF",
1158532ed618SSoby Mathew };
1159532ed618SSoby Mathew
1160532ed618SSoby Mathew INFO("PSCI Power Domain Map:\n");
1161ab4df50cSPankaj Gupta for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - psci_plat_core_count);
1162532ed618SSoby Mathew idx++) {
1163532ed618SSoby Mathew state_type = find_local_state_type(
1164532ed618SSoby Mathew psci_non_cpu_pd_nodes[idx].local_state);
1165b9338eeeSYann Gautier INFO(" Domain Node : Level %u, parent_node %u,"
1166532ed618SSoby Mathew " State %s (0x%x)\n",
1167532ed618SSoby Mathew psci_non_cpu_pd_nodes[idx].level,
1168532ed618SSoby Mathew psci_non_cpu_pd_nodes[idx].parent_node,
1169532ed618SSoby Mathew psci_state_type_str[state_type],
1170532ed618SSoby Mathew psci_non_cpu_pd_nodes[idx].local_state);
1171532ed618SSoby Mathew }
1172532ed618SSoby Mathew
1173ab4df50cSPankaj Gupta for (idx = 0; idx < psci_plat_core_count; idx++) {
1174532ed618SSoby Mathew state = psci_get_cpu_local_state_by_idx(idx);
1175532ed618SSoby Mathew state_type = find_local_state_type(state);
1176b9338eeeSYann Gautier INFO(" CPU Node : MPID 0x%llx, parent_node %u,"
1177532ed618SSoby Mathew " State %s (0x%x)\n",
1178*9f407e44SRohit Mathew (unsigned long long)PER_CPU_BY_INDEX(psci_cpu_pd_nodes, idx)->mpidr,
1179*9f407e44SRohit Mathew PER_CPU_BY_INDEX(psci_cpu_pd_nodes, idx)->parent_node,
1180532ed618SSoby Mathew psci_state_type_str[state_type],
1181532ed618SSoby Mathew psci_get_cpu_local_state_by_idx(idx));
1182532ed618SSoby Mathew }
1183532ed618SSoby Mathew #endif
1184532ed618SSoby Mathew }
1185532ed618SSoby Mathew
1186b10d4499SJeenu Viswambharan /******************************************************************************
1187b10d4499SJeenu Viswambharan * Return whether any secondaries were powered up with CPU_ON call. A CPU that
1188b10d4499SJeenu Viswambharan * have ever been powered up would have set its MPDIR value to something other
1189b10d4499SJeenu Viswambharan * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to
1190b10d4499SJeenu Viswambharan * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is
1191b10d4499SJeenu Viswambharan * meaningful only when called on the primary CPU during early boot.
1192b10d4499SJeenu Viswambharan *****************************************************************************/
psci_secondaries_brought_up(void)1193b10d4499SJeenu Viswambharan int psci_secondaries_brought_up(void)
1194b10d4499SJeenu Viswambharan {
11956b7b0f36SAntonio Nino Diaz unsigned int idx, n_valid = 0U;
1196b10d4499SJeenu Viswambharan
1197*9f407e44SRohit Mathew for (idx = 0U; idx < PLATFORM_CORE_COUNT; idx++) {
1198*9f407e44SRohit Mathew if (PER_CPU_BY_INDEX(psci_cpu_pd_nodes, idx)->mpidr != PSCI_INVALID_MPIDR) {
1199b10d4499SJeenu Viswambharan n_valid++;
1200b10d4499SJeenu Viswambharan }
1201bac32cc4SSaivardhan Thatikonda }
1202b10d4499SJeenu Viswambharan
12036b7b0f36SAntonio Nino Diaz assert(n_valid > 0U);
1204b10d4499SJeenu Viswambharan
12056b7b0f36SAntonio Nino Diaz return (n_valid > 1U) ? 1 : 0;
1206b10d4499SJeenu Viswambharan }
1207b10d4499SJeenu Viswambharan
call_cpu_pwr_dwn(unsigned int power_level)1208461b62b5SBoyan Karatotev static u_register_t call_cpu_pwr_dwn(unsigned int power_level)
1209aadb4b56SBoyan Karatotev {
1210aadb4b56SBoyan Karatotev struct cpu_ops *ops = get_cpu_data(cpu_ops_ptr);
1211aadb4b56SBoyan Karatotev
1212aadb4b56SBoyan Karatotev /* Call the last available power down handler */
1213aadb4b56SBoyan Karatotev if (power_level > CPU_MAX_PWR_DWN_OPS - 1) {
1214aadb4b56SBoyan Karatotev power_level = CPU_MAX_PWR_DWN_OPS - 1;
1215aadb4b56SBoyan Karatotev }
1216aadb4b56SBoyan Karatotev
1217aadb4b56SBoyan Karatotev assert(ops != NULL);
1218aadb4b56SBoyan Karatotev assert(ops->pwr_dwn_ops[power_level] != NULL);
1219aadb4b56SBoyan Karatotev
1220aadb4b56SBoyan Karatotev return ops->pwr_dwn_ops[power_level]();
1221aadb4b56SBoyan Karatotev }
1222aadb4b56SBoyan Karatotev
prepare_cpu_pwr_dwn(unsigned int power_level)1223aadb4b56SBoyan Karatotev static void prepare_cpu_pwr_dwn(unsigned int power_level)
1224aadb4b56SBoyan Karatotev {
1225461b62b5SBoyan Karatotev /* ignore the return, all cpus should behave the same */
1226461b62b5SBoyan Karatotev (void)call_cpu_pwr_dwn(power_level);
1227461b62b5SBoyan Karatotev }
1228461b62b5SBoyan Karatotev
prepare_cpu_pwr_up(unsigned int power_level)1229461b62b5SBoyan Karatotev static void prepare_cpu_pwr_up(unsigned int power_level)
1230461b62b5SBoyan Karatotev {
1231461b62b5SBoyan Karatotev /*
1232461b62b5SBoyan Karatotev * Call the pwr_dwn cpu hook again, indicating that an abandon happened.
1233461b62b5SBoyan Karatotev * The cpu driver is expected to clean up. We ask it to return
1234461b62b5SBoyan Karatotev * PABANDON_ACK to indicate that it has handled this. This is a
1235461b62b5SBoyan Karatotev * heuristic: the value has been chosen such that an unported CPU is
1236461b62b5SBoyan Karatotev * extremely unlikely to return this value.
1237461b62b5SBoyan Karatotev */
1238461b62b5SBoyan Karatotev u_register_t ret = call_cpu_pwr_dwn(power_level);
1239461b62b5SBoyan Karatotev
1240461b62b5SBoyan Karatotev /* unreachable on AArch32 so cast down to calm the compiler */
1241461b62b5SBoyan Karatotev if (ret != (u_register_t) PABANDON_ACK) {
1242461b62b5SBoyan Karatotev panic();
1243461b62b5SBoyan Karatotev }
1244aadb4b56SBoyan Karatotev }
1245aadb4b56SBoyan Karatotev
1246b0408e87SJeenu Viswambharan /*******************************************************************************
1247b0408e87SJeenu Viswambharan * Initiate power down sequence, by calling power down operations registered for
1248b0408e87SJeenu Viswambharan * this CPU.
1249b0408e87SJeenu Viswambharan ******************************************************************************/
psci_pwrdown_cpu_start(unsigned int power_level)12502b5e00d4SBoyan Karatotev void psci_pwrdown_cpu_start(unsigned int power_level)
1251b0408e87SJeenu Viswambharan {
12529b1e800eSBoyan Karatotev #if ENABLE_RUNTIME_INSTRUMENTATION
12539b1e800eSBoyan Karatotev
12549b1e800eSBoyan Karatotev /*
12559b1e800eSBoyan Karatotev * Flush cache line so that even if CPU power down happens
12569b1e800eSBoyan Karatotev * the timestamp update is reflected in memory.
12579b1e800eSBoyan Karatotev */
12589b1e800eSBoyan Karatotev PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
12599b1e800eSBoyan Karatotev RT_INSTR_ENTER_CFLUSH,
12609b1e800eSBoyan Karatotev PMF_CACHE_MAINT);
12619b1e800eSBoyan Karatotev #endif
12629b1e800eSBoyan Karatotev
1263aadb4b56SBoyan Karatotev #if !HW_ASSISTED_COHERENCY
1264b0408e87SJeenu Viswambharan /*
1265aadb4b56SBoyan Karatotev * Disable data caching and handle the stack's cache maintenance.
1266b0408e87SJeenu Viswambharan *
1267aadb4b56SBoyan Karatotev * If the core can't automatically exit coherency, the cpu driver needs
1268aadb4b56SBoyan Karatotev * to flush caches and exit coherency. We can't do this with data caches
1269aadb4b56SBoyan Karatotev * enabled. The cpu driver will decide which caches to flush based on
1270aadb4b56SBoyan Karatotev * the power level.
1271aadb4b56SBoyan Karatotev *
1272aadb4b56SBoyan Karatotev * If automatic coherency management is possible, we can keep data
1273aadb4b56SBoyan Karatotev * caches on until the very end and let hardware do cache maintenance.
1274b0408e87SJeenu Viswambharan */
1275aadb4b56SBoyan Karatotev psci_do_pwrdown_cache_maintenance();
1276b0408e87SJeenu Viswambharan #endif
12779b1e800eSBoyan Karatotev
1278aadb4b56SBoyan Karatotev /* Initiate the power down sequence by calling into the cpu driver. */
1279aadb4b56SBoyan Karatotev prepare_cpu_pwr_dwn(power_level);
1280aadb4b56SBoyan Karatotev
12819b1e800eSBoyan Karatotev #if ENABLE_RUNTIME_INSTRUMENTATION
12829b1e800eSBoyan Karatotev PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
12839b1e800eSBoyan Karatotev RT_INSTR_EXIT_CFLUSH,
12849b1e800eSBoyan Karatotev PMF_NO_CACHE_MAINT);
12859b1e800eSBoyan Karatotev #endif
1286b0408e87SJeenu Viswambharan }
128722744909SSandeep Tripathy
128822744909SSandeep Tripathy /*******************************************************************************
12892b5e00d4SBoyan Karatotev * Finish a terminal power down sequence, ending with a wfi. In case of wakeup
12902b5e00d4SBoyan Karatotev * will retry the sleep and panic if it persists.
12912b5e00d4SBoyan Karatotev ******************************************************************************/
psci_pwrdown_cpu_end_terminal(void)12922b5e00d4SBoyan Karatotev void __dead2 psci_pwrdown_cpu_end_terminal(void)
12932b5e00d4SBoyan Karatotev {
129445c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN
129545c7328cSBoyan Karatotev /*
129645c7328cSBoyan Karatotev * force SME off to not get power down rejected. Getting here is
129745c7328cSBoyan Karatotev * terminal so we don't care if we lose context because of another
129845c7328cSBoyan Karatotev * wakeup
129945c7328cSBoyan Karatotev */
130045c7328cSBoyan Karatotev if (is_feat_sme_supported()) {
130145c7328cSBoyan Karatotev write_svcr(0);
130245c7328cSBoyan Karatotev isb();
130345c7328cSBoyan Karatotev }
130445c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */
130545c7328cSBoyan Karatotev
1306232c1892SBoyan Karatotev /* ensure write buffer empty */
1307232c1892SBoyan Karatotev dsbsy();
1308232c1892SBoyan Karatotev
13092b5e00d4SBoyan Karatotev /*
13102b5e00d4SBoyan Karatotev * Execute a wfi which, in most cases, will allow the power controller
13112b5e00d4SBoyan Karatotev * to physically power down this cpu. Under some circumstances that may
13122b5e00d4SBoyan Karatotev * be denied. Hopefully this is transient, retrying a few times should
13132b5e00d4SBoyan Karatotev * power down.
13142b5e00d4SBoyan Karatotev */
13152b5e00d4SBoyan Karatotev for (int i = 0; i < 32; i++)
1316232c1892SBoyan Karatotev wfi();
13172b5e00d4SBoyan Karatotev
13182b5e00d4SBoyan Karatotev /* Wake up wasn't transient. System is probably in a bad state. */
13192b5e00d4SBoyan Karatotev ERROR("Could not power off CPU.\n");
13202b5e00d4SBoyan Karatotev panic();
13212b5e00d4SBoyan Karatotev }
13222b5e00d4SBoyan Karatotev
13232b5e00d4SBoyan Karatotev /*******************************************************************************
13242b5e00d4SBoyan Karatotev * Finish a non-terminal power down sequence, ending with a wfi. In case of
13252b5e00d4SBoyan Karatotev * wakeup will unwind any CPU specific actions and return.
13262b5e00d4SBoyan Karatotev ******************************************************************************/
13272b5e00d4SBoyan Karatotev
psci_pwrdown_cpu_end_wakeup(unsigned int power_level)13282b5e00d4SBoyan Karatotev void psci_pwrdown_cpu_end_wakeup(unsigned int power_level)
13292b5e00d4SBoyan Karatotev {
1330232c1892SBoyan Karatotev /* ensure write buffer empty */
1331232c1892SBoyan Karatotev dsbsy();
1332232c1892SBoyan Karatotev
13332b5e00d4SBoyan Karatotev /*
1334232c1892SBoyan Karatotev * Turn the core off. Usually, will be terminal. In some circumstances
1335232c1892SBoyan Karatotev * the powerdown will be denied and we'll need to unwind.
13362b5e00d4SBoyan Karatotev */
1337232c1892SBoyan Karatotev wfi();
13382b5e00d4SBoyan Karatotev
13392b5e00d4SBoyan Karatotev /*
13402b5e00d4SBoyan Karatotev * Waking up does not require hardware-assisted coherency, but that is
134104c39e46SBoyan Karatotev * the case for every core that can wake up. Can either happen because
134204c39e46SBoyan Karatotev * of errata or pabandon.
13432b5e00d4SBoyan Karatotev */
134404c39e46SBoyan Karatotev #if !defined(__aarch64__) || !HW_ASSISTED_COHERENCY
134504c39e46SBoyan Karatotev ERROR("AArch32 systems shouldn't wake up.\n");
13462b5e00d4SBoyan Karatotev panic();
134704c39e46SBoyan Karatotev #endif
13482b5e00d4SBoyan Karatotev /*
13492b5e00d4SBoyan Karatotev * Begin unwinding. Everything can be shared with CPU_ON and co later,
13502b5e00d4SBoyan Karatotev * except the CPU specific bit. Cores that have hardware-assisted
1351461b62b5SBoyan Karatotev * coherency should be able to handle this.
13522b5e00d4SBoyan Karatotev */
1353461b62b5SBoyan Karatotev prepare_cpu_pwr_up(power_level);
13542b5e00d4SBoyan Karatotev }
13552b5e00d4SBoyan Karatotev
13562b5e00d4SBoyan Karatotev /*******************************************************************************
135722744909SSandeep Tripathy * This function invokes the callback 'stop_func()' with the 'mpidr' of each
135822744909SSandeep Tripathy * online PE. Caller can pass suitable method to stop a remote core.
135922744909SSandeep Tripathy *
136022744909SSandeep Tripathy * 'wait_ms' is the timeout value in milliseconds for the other cores to
136122744909SSandeep Tripathy * transition to power down state. Passing '0' makes it non-blocking.
136222744909SSandeep Tripathy *
136322744909SSandeep Tripathy * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the
136422744909SSandeep Tripathy * given timeout.
136522744909SSandeep Tripathy ******************************************************************************/
psci_stop_other_cores(unsigned int this_cpu_idx,unsigned int wait_ms,void (* stop_func)(u_register_t mpidr))13663b802105SBoyan Karatotev int psci_stop_other_cores(unsigned int this_cpu_idx, unsigned int wait_ms,
136722744909SSandeep Tripathy void (*stop_func)(u_register_t mpidr))
136822744909SSandeep Tripathy {
136922744909SSandeep Tripathy /* Invoke stop_func for each core */
13703b802105SBoyan Karatotev for (unsigned int idx = 0U; idx < psci_plat_core_count; idx++) {
137122744909SSandeep Tripathy /* skip current CPU */
137222744909SSandeep Tripathy if (idx == this_cpu_idx) {
137322744909SSandeep Tripathy continue;
137422744909SSandeep Tripathy }
137522744909SSandeep Tripathy
137622744909SSandeep Tripathy /* Check if the CPU is ON */
137722744909SSandeep Tripathy if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) {
1378*9f407e44SRohit Mathew (*stop_func)(PER_CPU_BY_INDEX(psci_cpu_pd_nodes, idx)->mpidr);
137922744909SSandeep Tripathy }
138022744909SSandeep Tripathy }
138122744909SSandeep Tripathy
138222744909SSandeep Tripathy /* Need to wait for other cores to shutdown */
138322744909SSandeep Tripathy if (wait_ms != 0U) {
1384e64cdee4SMaheedhar Bollapalli for (uint32_t delay_ms = wait_ms; ((delay_ms != 0U) &&
1385e64cdee4SMaheedhar Bollapalli (!psci_is_last_on_cpu(this_cpu_idx))); delay_ms--) {
138622744909SSandeep Tripathy mdelay(1U);
138722744909SSandeep Tripathy }
138822744909SSandeep Tripathy
13893b802105SBoyan Karatotev if (!psci_is_last_on_cpu(this_cpu_idx)) {
139022744909SSandeep Tripathy WARN("Failed to stop all cores!\n");
139122744909SSandeep Tripathy psci_print_power_domain_map();
139222744909SSandeep Tripathy return PSCI_E_DENIED;
139322744909SSandeep Tripathy }
139422744909SSandeep Tripathy }
139522744909SSandeep Tripathy
139622744909SSandeep Tripathy return PSCI_E_SUCCESS;
139722744909SSandeep Tripathy }
1398ce14a12fSLucian Paul-Trifu
1399ce14a12fSLucian Paul-Trifu /*******************************************************************************
1400ce14a12fSLucian Paul-Trifu * This function verifies that all the other cores in the system have been
1401ce14a12fSLucian Paul-Trifu * turned OFF and the current CPU is the last running CPU in the system.
1402ce14a12fSLucian Paul-Trifu * Returns true if the current CPU is the last ON CPU or false otherwise.
1403ce14a12fSLucian Paul-Trifu *
1404ce14a12fSLucian Paul-Trifu * This API has following differences with psci_is_last_on_cpu
1405ce14a12fSLucian Paul-Trifu * 1. PSCI states are locked
1406ce14a12fSLucian Paul-Trifu ******************************************************************************/
psci_is_last_on_cpu_safe(unsigned int this_core)14073b802105SBoyan Karatotev bool psci_is_last_on_cpu_safe(unsigned int this_core)
1408ce14a12fSLucian Paul-Trifu {
1409ce14a12fSLucian Paul-Trifu unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1410ce14a12fSLucian Paul-Trifu
1411b41b0824SJayanth Dodderi Chidanand psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1412ce14a12fSLucian Paul-Trifu
1413ce14a12fSLucian Paul-Trifu psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1414ce14a12fSLucian Paul-Trifu
14153b802105SBoyan Karatotev if (!psci_is_last_on_cpu(this_core)) {
1416b41b0824SJayanth Dodderi Chidanand psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1417ce14a12fSLucian Paul-Trifu return false;
1418ce14a12fSLucian Paul-Trifu }
1419ce14a12fSLucian Paul-Trifu
1420ce14a12fSLucian Paul-Trifu psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1421ce14a12fSLucian Paul-Trifu
1422ce14a12fSLucian Paul-Trifu return true;
1423ce14a12fSLucian Paul-Trifu }
1424b88a4416SWing Li
1425b88a4416SWing Li /*******************************************************************************
1426b88a4416SWing Li * This function verifies that all cores in the system have been turned ON.
1427b88a4416SWing Li * Returns true, if all CPUs are ON or false otherwise.
1428b88a4416SWing Li *
1429b88a4416SWing Li * This API has following differences with psci_are_all_cpus_on
1430b88a4416SWing Li * 1. PSCI states are locked
1431b88a4416SWing Li ******************************************************************************/
psci_are_all_cpus_on_safe(unsigned int this_core)14323b802105SBoyan Karatotev bool psci_are_all_cpus_on_safe(unsigned int this_core)
1433b88a4416SWing Li {
1434b88a4416SWing Li unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1435b88a4416SWing Li
1436b88a4416SWing Li psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1437b88a4416SWing Li
1438b88a4416SWing Li psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1439b88a4416SWing Li
1440b88a4416SWing Li if (!psci_are_all_cpus_on()) {
1441b88a4416SWing Li psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1442b88a4416SWing Li return false;
1443b88a4416SWing Li }
1444b88a4416SWing Li
1445b88a4416SWing Li psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1446b88a4416SWing Li
1447b88a4416SWing Li return true;
1448b88a4416SWing Li }
1449a7be2a57SManish V Badarkhe
1450a7be2a57SManish V Badarkhe /*******************************************************************************
1451a7be2a57SManish V Badarkhe * Safely counts the number of CPUs in the system that are currently in the ON
1452a7be2a57SManish V Badarkhe * or ON_PENDING state.
1453a7be2a57SManish V Badarkhe *
1454a7be2a57SManish V Badarkhe * This function acquires and releases the necessary power domain locks to
1455a7be2a57SManish V Badarkhe * ensure consistency of the CPU state information.
1456a7be2a57SManish V Badarkhe *
1457a7be2a57SManish V Badarkhe * @param this_core The index of the current core making the query.
1458a7be2a57SManish V Badarkhe *
1459a7be2a57SManish V Badarkhe * @return The number of CPUs currently in AFF_STATE_ON or AFF_STATE_ON_PENDING.
1460a7be2a57SManish V Badarkhe ******************************************************************************/
psci_num_cpus_running_on_safe(unsigned int this_core)1461a7be2a57SManish V Badarkhe unsigned int psci_num_cpus_running_on_safe(unsigned int this_core)
1462a7be2a57SManish V Badarkhe {
1463a7be2a57SManish V Badarkhe unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1464a7be2a57SManish V Badarkhe unsigned int no_of_cpus;
1465a7be2a57SManish V Badarkhe
1466a7be2a57SManish V Badarkhe psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1467a7be2a57SManish V Badarkhe
1468a7be2a57SManish V Badarkhe psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1469a7be2a57SManish V Badarkhe
1470a7be2a57SManish V Badarkhe no_of_cpus = psci_num_cpus_running();
1471a7be2a57SManish V Badarkhe
1472a7be2a57SManish V Badarkhe psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1473a7be2a57SManish V Badarkhe
1474a7be2a57SManish V Badarkhe return no_of_cpus;
1475a7be2a57SManish V Badarkhe }
1476