xref: /rk3399_ARM-atf/lib/psci/psci_common.c (revision ef738d19d3565e44ed44a6b06c6d9ab036322ac5)
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>
1709d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
18777f1f68SJayanth Dodderi Chidanand #include <lib/extensions/spe.h>
199b1e800eSBoyan Karatotev #include <lib/pmf/pmf.h>
209b1e800eSBoyan Karatotev #include <lib/runtime_instr.h>
2109d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2309d40e0eSAntonio Nino Diaz 
24532ed618SSoby Mathew #include "psci_private.h"
25532ed618SSoby Mathew 
26532ed618SSoby Mathew /*
27532ed618SSoby Mathew  * SPD power management operations, expected to be supplied by the registered
28532ed618SSoby Mathew  * SPD on successful SP initialization
29532ed618SSoby Mathew  */
30532ed618SSoby Mathew const spd_pm_ops_t *psci_spd_pm;
31532ed618SSoby Mathew 
32532ed618SSoby Mathew /*
33532ed618SSoby Mathew  * PSCI requested local power state map. This array is used to store the local
34532ed618SSoby Mathew  * power states requested by a CPU for power levels from level 1 to
35532ed618SSoby Mathew  * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
36532ed618SSoby Mathew  * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
37532ed618SSoby Mathew  * CPU are the same.
38532ed618SSoby Mathew  *
39532ed618SSoby Mathew  * During state coordination, the platform is passed an array containing the
40532ed618SSoby Mathew  * local states requested for a particular non cpu power domain by each cpu
41532ed618SSoby Mathew  * within the domain.
42532ed618SSoby Mathew  *
43532ed618SSoby Mathew  * TODO: Dense packing of the requested states will cause cache thrashing
44532ed618SSoby Mathew  * when multiple power domains write to it. If we allocate the requested
45532ed618SSoby Mathew  * states at each power level in a cache-line aligned per-domain memory,
46532ed618SSoby Mathew  * the cache thrashing can be avoided.
47532ed618SSoby Mathew  */
48532ed618SSoby Mathew static plat_local_state_t
49532ed618SSoby Mathew 	psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
50532ed618SSoby Mathew 
51ab4df50cSPankaj Gupta unsigned int psci_plat_core_count;
52532ed618SSoby Mathew 
53532ed618SSoby Mathew /*******************************************************************************
54532ed618SSoby Mathew  * Arrays that hold the platform's power domain tree information for state
55532ed618SSoby Mathew  * management of power domains.
56532ed618SSoby Mathew  * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
57532ed618SSoby Mathew  * which is an ancestor of a CPU power domain.
58532ed618SSoby Mathew  * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
59532ed618SSoby Mathew  ******************************************************************************/
60532ed618SSoby Mathew non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
61532ed618SSoby Mathew #if USE_COHERENT_MEM
62da04341eSChris Kay __section(".tzfw_coherent_mem")
63532ed618SSoby Mathew #endif
64532ed618SSoby Mathew ;
65532ed618SSoby Mathew 
66b0408e87SJeenu Viswambharan /* Lock for PSCI state coordination */
67b0408e87SJeenu Viswambharan DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
68532ed618SSoby Mathew 
69532ed618SSoby Mathew cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
70532ed618SSoby Mathew 
71532ed618SSoby Mathew /*******************************************************************************
72532ed618SSoby Mathew  * Pointer to functions exported by the platform to complete power mgmt. ops
73532ed618SSoby Mathew  ******************************************************************************/
74532ed618SSoby Mathew const plat_psci_ops_t *psci_plat_pm_ops;
75532ed618SSoby Mathew 
76532ed618SSoby Mathew /******************************************************************************
77532ed618SSoby Mathew  * Check that the maximum power level supported by the platform makes sense
78532ed618SSoby Mathew  *****************************************************************************/
796b7b0f36SAntonio Nino Diaz CASSERT((PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL) &&
806b7b0f36SAntonio Nino Diaz 	(PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL),
81532ed618SSoby Mathew 	assert_platform_max_pwrlvl_check);
82532ed618SSoby Mathew 
83b88a4416SWing Li #if PSCI_OS_INIT_MODE
84b88a4416SWing Li /*******************************************************************************
85b88a4416SWing Li  * The power state coordination mode used in CPU_SUSPEND.
86b88a4416SWing Li  * Defaults to platform-coordinated mode.
87b88a4416SWing Li  ******************************************************************************/
88b88a4416SWing Li suspend_mode_t psci_suspend_mode = PLAT_COORD;
89b88a4416SWing Li #endif
90b88a4416SWing Li 
91532ed618SSoby Mathew /*
92532ed618SSoby Mathew  * The plat_local_state used by the platform is one of these types: RUN,
93532ed618SSoby Mathew  * RETENTION and OFF. The platform can define further sub-states for each type
94532ed618SSoby Mathew  * apart from RUN. This categorization is done to verify the sanity of the
95532ed618SSoby Mathew  * psci_power_state passed by the platform and to print debug information. The
96532ed618SSoby Mathew  * categorization is done on the basis of the following conditions:
97532ed618SSoby Mathew  *
98532ed618SSoby Mathew  * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
99532ed618SSoby Mathew  *
100532ed618SSoby Mathew  * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
101532ed618SSoby Mathew  *    STATE_TYPE_RETN.
102532ed618SSoby Mathew  *
103532ed618SSoby Mathew  * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
104532ed618SSoby Mathew  *    STATE_TYPE_OFF.
105532ed618SSoby Mathew  */
106532ed618SSoby Mathew typedef enum plat_local_state_type {
107532ed618SSoby Mathew 	STATE_TYPE_RUN = 0,
108532ed618SSoby Mathew 	STATE_TYPE_RETN,
109532ed618SSoby Mathew 	STATE_TYPE_OFF
110532ed618SSoby Mathew } plat_local_state_type_t;
111532ed618SSoby Mathew 
11297373c33SAntonio Nino Diaz /* Function used to categorize plat_local_state. */
11397373c33SAntonio Nino Diaz static plat_local_state_type_t find_local_state_type(plat_local_state_t state)
11497373c33SAntonio Nino Diaz {
11597373c33SAntonio Nino Diaz 	if (state != 0U) {
11697373c33SAntonio Nino Diaz 		if (state > PLAT_MAX_RET_STATE) {
11797373c33SAntonio Nino Diaz 			return STATE_TYPE_OFF;
11897373c33SAntonio Nino Diaz 		} else {
11997373c33SAntonio Nino Diaz 			return STATE_TYPE_RETN;
12097373c33SAntonio Nino Diaz 		}
12197373c33SAntonio Nino Diaz 	} else {
12297373c33SAntonio Nino Diaz 		return STATE_TYPE_RUN;
12397373c33SAntonio Nino Diaz 	}
12497373c33SAntonio Nino Diaz }
125532ed618SSoby Mathew 
126532ed618SSoby Mathew /******************************************************************************
127532ed618SSoby Mathew  * Check that the maximum retention level supported by the platform is less
128532ed618SSoby Mathew  * than the maximum off level.
129532ed618SSoby Mathew  *****************************************************************************/
1306b7b0f36SAntonio Nino Diaz CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE,
131532ed618SSoby Mathew 		assert_platform_max_off_and_retn_state_check);
132532ed618SSoby Mathew 
133532ed618SSoby Mathew /******************************************************************************
134532ed618SSoby Mathew  * This function ensures that the power state parameter in a CPU_SUSPEND request
135532ed618SSoby Mathew  * is valid. If so, it returns the requested states for each power level.
136532ed618SSoby Mathew  *****************************************************************************/
137532ed618SSoby Mathew int psci_validate_power_state(unsigned int power_state,
138532ed618SSoby Mathew 			      psci_power_state_t *state_info)
139532ed618SSoby Mathew {
140532ed618SSoby Mathew 	/* Check SBZ bits in power state are zero */
141c7b0a28dSMaheedhar Bollapalli 	if (psci_check_power_state(power_state) != 0U) {
142532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
143c7b0a28dSMaheedhar Bollapalli 	}
1446b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->validate_power_state != NULL);
145532ed618SSoby Mathew 
146532ed618SSoby Mathew 	/* Validate the power_state using platform pm_ops */
147532ed618SSoby Mathew 	return psci_plat_pm_ops->validate_power_state(power_state, state_info);
148532ed618SSoby Mathew }
149532ed618SSoby Mathew 
150532ed618SSoby Mathew /******************************************************************************
151532ed618SSoby Mathew  * This function retrieves the `psci_power_state_t` for system suspend from
152532ed618SSoby Mathew  * the platform.
153532ed618SSoby Mathew  *****************************************************************************/
154532ed618SSoby Mathew void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
155532ed618SSoby Mathew {
156532ed618SSoby Mathew 	/*
157532ed618SSoby Mathew 	 * Assert that the required pm_ops hook is implemented to ensure that
158532ed618SSoby Mathew 	 * the capability detected during psci_setup() is valid.
159532ed618SSoby Mathew 	 */
1606b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_sys_suspend_power_state != NULL);
161532ed618SSoby Mathew 
162532ed618SSoby Mathew 	/*
163532ed618SSoby Mathew 	 * Query the platform for the power_state required for system suspend
164532ed618SSoby Mathew 	 */
165532ed618SSoby Mathew 	psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
166532ed618SSoby Mathew }
167532ed618SSoby Mathew 
168606b7430SWing Li #if PSCI_OS_INIT_MODE
169606b7430SWing Li /*******************************************************************************
170606b7430SWing Li  * This function verifies that all the other cores at the 'end_pwrlvl' have been
171606b7430SWing Li  * idled and the current CPU is the last running CPU at the 'end_pwrlvl'.
172606b7430SWing Li  * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
173606b7430SWing Li  * otherwise.
174606b7430SWing Li  ******************************************************************************/
1753b802105SBoyan Karatotev static bool psci_is_last_cpu_to_idle_at_pwrlvl(unsigned int my_idx, unsigned int end_pwrlvl)
176606b7430SWing Li {
1773b802105SBoyan Karatotev 	unsigned int lvl;
178152ad112SMark Dykes 	unsigned int parent_idx = 0;
179606b7430SWing Li 	unsigned int cpu_start_idx, ncpus, cpu_idx;
180606b7430SWing Li 	plat_local_state_t local_state;
181606b7430SWing Li 
182606b7430SWing Li 	if (end_pwrlvl == PSCI_CPU_PWR_LVL) {
183606b7430SWing Li 		return true;
184606b7430SWing Li 	}
185606b7430SWing Li 
186606b7430SWing Li 	parent_idx = psci_cpu_pd_nodes[my_idx].parent_node;
18701959a16SCharlie Bareham 	for (lvl = PSCI_CPU_PWR_LVL + U(1); lvl < end_pwrlvl; lvl++) {
18801959a16SCharlie Bareham 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
189606b7430SWing Li 	}
190606b7430SWing Li 
191606b7430SWing Li 	cpu_start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
192606b7430SWing Li 	ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
193606b7430SWing Li 
194606b7430SWing Li 	for (cpu_idx = cpu_start_idx; cpu_idx < cpu_start_idx + ncpus;
195606b7430SWing Li 			cpu_idx++) {
196606b7430SWing Li 		local_state = psci_get_cpu_local_state_by_idx(cpu_idx);
197606b7430SWing Li 		if (cpu_idx == my_idx) {
198606b7430SWing Li 			assert(is_local_state_run(local_state) != 0);
199606b7430SWing Li 			continue;
200606b7430SWing Li 		}
201606b7430SWing Li 
202606b7430SWing Li 		if (is_local_state_run(local_state) != 0) {
203606b7430SWing Li 			return false;
204606b7430SWing Li 		}
205606b7430SWing Li 	}
206606b7430SWing Li 
207606b7430SWing Li 	return true;
208606b7430SWing Li }
209606b7430SWing Li #endif
210606b7430SWing Li 
211532ed618SSoby Mathew /*******************************************************************************
212b88a4416SWing Li  * This function verifies that all the other cores in the system have been
213532ed618SSoby Mathew  * turned OFF and the current CPU is the last running CPU in the system.
214b41b0824SJayanth Dodderi Chidanand  * Returns true, if the current CPU is the last ON CPU or false otherwise.
215532ed618SSoby Mathew  ******************************************************************************/
2163b802105SBoyan Karatotev bool psci_is_last_on_cpu(unsigned int my_idx)
217532ed618SSoby Mathew {
2183b802105SBoyan Karatotev 	for (unsigned int cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
219532ed618SSoby Mathew 		if (cpu_idx == my_idx) {
220532ed618SSoby Mathew 			assert(psci_get_aff_info_state() == AFF_STATE_ON);
221532ed618SSoby Mathew 			continue;
222532ed618SSoby Mathew 		}
223532ed618SSoby Mathew 
224b41b0824SJayanth Dodderi Chidanand 		if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF) {
225b41b0824SJayanth Dodderi Chidanand 			VERBOSE("core=%u other than current core=%u %s\n",
226b41b0824SJayanth Dodderi Chidanand 				cpu_idx, my_idx, "running in the system");
227b41b0824SJayanth Dodderi Chidanand 			return false;
228b41b0824SJayanth Dodderi Chidanand 		}
229532ed618SSoby Mathew 	}
230532ed618SSoby Mathew 
231b41b0824SJayanth Dodderi Chidanand 	return true;
232532ed618SSoby Mathew }
233532ed618SSoby Mathew 
234532ed618SSoby Mathew /*******************************************************************************
235b88a4416SWing Li  * This function verifies that all cores in the system have been turned ON.
236b88a4416SWing Li  * Returns true, if all CPUs are ON or false otherwise.
237b88a4416SWing Li  ******************************************************************************/
238b88a4416SWing Li static bool psci_are_all_cpus_on(void)
239b88a4416SWing Li {
240b88a4416SWing Li 	unsigned int cpu_idx;
241b88a4416SWing Li 
242b88a4416SWing Li 	for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
243b88a4416SWing Li 		if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_OFF) {
244b88a4416SWing Li 			return false;
245b88a4416SWing Li 		}
246b88a4416SWing Li 	}
247b88a4416SWing Li 
248b88a4416SWing Li 	return true;
249b88a4416SWing Li }
250b88a4416SWing Li 
251b88a4416SWing Li /*******************************************************************************
252532ed618SSoby Mathew  * Routine to return the maximum power level to traverse to after a cpu has
253532ed618SSoby Mathew  * been physically powered up. It is expected to be called immediately after
254532ed618SSoby Mathew  * reset from assembler code.
255532ed618SSoby Mathew  ******************************************************************************/
256532ed618SSoby Mathew static unsigned int get_power_on_target_pwrlvl(void)
257532ed618SSoby Mathew {
258532ed618SSoby Mathew 	unsigned int pwrlvl;
259532ed618SSoby Mathew 
260532ed618SSoby Mathew 	/*
261532ed618SSoby Mathew 	 * Assume that this cpu was suspended and retrieve its target power
2620c836554SBoyan Karatotev 	 * level. If it wasn't, the cpu is off so this will be PLAT_MAX_PWR_LVL.
263532ed618SSoby Mathew 	 */
264532ed618SSoby Mathew 	pwrlvl = psci_get_suspend_pwrlvl();
2650c411c78SDeepika Bhavnani 	assert(pwrlvl < PSCI_INVALID_PWR_LVL);
266532ed618SSoby Mathew 	return pwrlvl;
267532ed618SSoby Mathew }
268532ed618SSoby Mathew 
269532ed618SSoby Mathew /******************************************************************************
270532ed618SSoby Mathew  * Helper function to update the requested local power state array. This array
271532ed618SSoby Mathew  * does not store the requested state for the CPU power level. Hence an
27241af0515SDeepika Bhavnani  * assertion is added to prevent us from accessing the CPU power level.
273532ed618SSoby Mathew  *****************************************************************************/
274532ed618SSoby Mathew static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
275532ed618SSoby Mathew 					 unsigned int cpu_idx,
276532ed618SSoby Mathew 					 plat_local_state_t req_pwr_state)
277532ed618SSoby Mathew {
278532ed618SSoby Mathew 	assert(pwrlvl > PSCI_CPU_PWR_LVL);
27941af0515SDeepika Bhavnani 	if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
280ab4df50cSPankaj Gupta 			(cpu_idx < psci_plat_core_count)) {
2816b7b0f36SAntonio Nino Diaz 		psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx] = req_pwr_state;
28241af0515SDeepika Bhavnani 	}
283532ed618SSoby Mathew }
284532ed618SSoby Mathew 
285532ed618SSoby Mathew /******************************************************************************
286532ed618SSoby Mathew  * This function initializes the psci_req_local_pwr_states.
287532ed618SSoby Mathew  *****************************************************************************/
28887c85134SDaniel Boulby void __init psci_init_req_local_pwr_states(void)
289532ed618SSoby Mathew {
290532ed618SSoby Mathew 	/* Initialize the requested state of all non CPU power domains as OFF */
2916b7b0f36SAntonio Nino Diaz 	unsigned int pwrlvl;
292ab4df50cSPankaj Gupta 	unsigned int core;
2936b7b0f36SAntonio Nino Diaz 
2946b7b0f36SAntonio Nino Diaz 	for (pwrlvl = 0U; pwrlvl < PLAT_MAX_PWR_LVL; pwrlvl++) {
295ab4df50cSPankaj Gupta 		for (core = 0; core < psci_plat_core_count; core++) {
2966b7b0f36SAntonio Nino Diaz 			psci_req_local_pwr_states[pwrlvl][core] =
2976b7b0f36SAntonio Nino Diaz 				PLAT_MAX_OFF_STATE;
2986b7b0f36SAntonio Nino Diaz 		}
2996b7b0f36SAntonio Nino Diaz 	}
300532ed618SSoby Mathew }
301532ed618SSoby Mathew 
302532ed618SSoby Mathew /******************************************************************************
303532ed618SSoby Mathew  * Helper function to return a reference to an array containing the local power
304532ed618SSoby Mathew  * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
305532ed618SSoby Mathew  * array will be the number of cpu power domains of which this power domain is
306532ed618SSoby Mathew  * an ancestor. These requested states will be used to determine a suitable
307532ed618SSoby Mathew  * target state for this power domain during psci state coordination. An
308532ed618SSoby Mathew  * assertion is added to prevent us from accessing the CPU power level.
309532ed618SSoby Mathew  *****************************************************************************/
310532ed618SSoby Mathew static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
311fc81021aSDeepika Bhavnani 							 unsigned int cpu_idx)
312532ed618SSoby Mathew {
313532ed618SSoby Mathew 	assert(pwrlvl > PSCI_CPU_PWR_LVL);
314532ed618SSoby Mathew 
31541af0515SDeepika Bhavnani 	if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
316ab4df50cSPankaj Gupta 			(cpu_idx < psci_plat_core_count)) {
3176b7b0f36SAntonio Nino Diaz 		return &psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx];
31841af0515SDeepika Bhavnani 	} else
31941af0515SDeepika Bhavnani 		return NULL;
320532ed618SSoby Mathew }
321532ed618SSoby Mathew 
322606b7430SWing Li #if PSCI_OS_INIT_MODE
323606b7430SWing Li /******************************************************************************
324606b7430SWing Li  * Helper function to save a copy of the psci_req_local_pwr_states (prev) for a
325606b7430SWing Li  * CPU (cpu_idx), and update psci_req_local_pwr_states with the new requested
326606b7430SWing Li  * local power states (state_info).
327606b7430SWing Li  *****************************************************************************/
328606b7430SWing Li void psci_update_req_local_pwr_states(unsigned int end_pwrlvl,
329606b7430SWing Li 				      unsigned int cpu_idx,
330606b7430SWing Li 				      psci_power_state_t *state_info,
331606b7430SWing Li 				      plat_local_state_t *prev)
332606b7430SWing Li {
333606b7430SWing Li 	unsigned int lvl;
334606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
335606b7430SWing Li 	unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
336606b7430SWing Li #else
337606b7430SWing Li 	unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
338606b7430SWing Li #endif
339606b7430SWing Li 	plat_local_state_t req_state;
340606b7430SWing Li 
341606b7430SWing Li 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
342606b7430SWing Li 		/* Save the previous requested local power state */
343606b7430SWing Li 		prev[lvl - 1U] = *psci_get_req_local_pwr_states(lvl, cpu_idx);
344606b7430SWing Li 
345606b7430SWing Li 		/* Update the new requested local power state */
346606b7430SWing Li 		if (lvl <= end_pwrlvl) {
347606b7430SWing Li 			req_state = state_info->pwr_domain_state[lvl];
348606b7430SWing Li 		} else {
349606b7430SWing Li 			req_state = state_info->pwr_domain_state[end_pwrlvl];
350606b7430SWing Li 		}
351606b7430SWing Li 		psci_set_req_local_pwr_state(lvl, cpu_idx, req_state);
352606b7430SWing Li 	}
353606b7430SWing Li }
354606b7430SWing Li 
355606b7430SWing Li /******************************************************************************
356606b7430SWing Li  * Helper function to restore the previously saved requested local power states
357606b7430SWing Li  * (prev) for a CPU (cpu_idx) to psci_req_local_pwr_states.
358606b7430SWing Li  *****************************************************************************/
359606b7430SWing Li void psci_restore_req_local_pwr_states(unsigned int cpu_idx,
360606b7430SWing Li 				       plat_local_state_t *prev)
361606b7430SWing Li {
362606b7430SWing Li 	unsigned int lvl;
363606b7430SWing Li #ifdef PLAT_MAX_CPU_SUSPEND_PWR_LVL
364606b7430SWing Li 	unsigned int max_pwrlvl = PLAT_MAX_CPU_SUSPEND_PWR_LVL;
365606b7430SWing Li #else
366606b7430SWing Li 	unsigned int max_pwrlvl = PLAT_MAX_PWR_LVL;
367606b7430SWing Li #endif
368606b7430SWing Li 
369606b7430SWing Li 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= max_pwrlvl; lvl++) {
370606b7430SWing Li 		/* Restore the previous requested local power state */
371606b7430SWing Li 		psci_set_req_local_pwr_state(lvl, cpu_idx, prev[lvl - 1U]);
372606b7430SWing Li 	}
373606b7430SWing Li }
374606b7430SWing Li #endif
375606b7430SWing Li 
376a10d3632SJeenu Viswambharan /*
377a10d3632SJeenu Viswambharan  * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
378a10d3632SJeenu Viswambharan  * memory.
379a10d3632SJeenu Viswambharan  *
380a10d3632SJeenu Viswambharan  * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
381a10d3632SJeenu Viswambharan  * it's accessed by both cached and non-cached participants. To serve the common
382a10d3632SJeenu Viswambharan  * minimum, perform a cache flush before read and after write so that non-cached
383a10d3632SJeenu Viswambharan  * participants operate on latest data in main memory.
384a10d3632SJeenu Viswambharan  *
385a10d3632SJeenu Viswambharan  * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
386a10d3632SJeenu Viswambharan  * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
387a10d3632SJeenu Viswambharan  * In both cases, no cache operations are required.
388a10d3632SJeenu Viswambharan  */
389a10d3632SJeenu Viswambharan 
390a10d3632SJeenu Viswambharan /*
391a10d3632SJeenu Viswambharan  * Retrieve local state of non-CPU power domain node from a non-cached CPU,
392a10d3632SJeenu Viswambharan  * after any required cache maintenance operation.
393a10d3632SJeenu Viswambharan  */
394a10d3632SJeenu Viswambharan static plat_local_state_t get_non_cpu_pd_node_local_state(
395a10d3632SJeenu Viswambharan 		unsigned int parent_idx)
396a10d3632SJeenu Viswambharan {
397f996a5f7SAndrew F. Davis #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
398a10d3632SJeenu Viswambharan 	flush_dcache_range(
399a10d3632SJeenu Viswambharan 			(uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
400a10d3632SJeenu Viswambharan 			sizeof(psci_non_cpu_pd_nodes[parent_idx]));
401a10d3632SJeenu Viswambharan #endif
402a10d3632SJeenu Viswambharan 	return psci_non_cpu_pd_nodes[parent_idx].local_state;
403a10d3632SJeenu Viswambharan }
404a10d3632SJeenu Viswambharan 
405a10d3632SJeenu Viswambharan /*
406a10d3632SJeenu Viswambharan  * Update local state of non-CPU power domain node from a cached CPU; perform
407a10d3632SJeenu Viswambharan  * any required cache maintenance operation afterwards.
408a10d3632SJeenu Viswambharan  */
409a10d3632SJeenu Viswambharan static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
410a10d3632SJeenu Viswambharan 		plat_local_state_t state)
411a10d3632SJeenu Viswambharan {
412a10d3632SJeenu Viswambharan 	psci_non_cpu_pd_nodes[parent_idx].local_state = state;
413f996a5f7SAndrew F. Davis #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
414a10d3632SJeenu Viswambharan 	flush_dcache_range(
415a10d3632SJeenu Viswambharan 			(uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
416a10d3632SJeenu Viswambharan 			sizeof(psci_non_cpu_pd_nodes[parent_idx]));
417a10d3632SJeenu Viswambharan #endif
418a10d3632SJeenu Viswambharan }
419a10d3632SJeenu Viswambharan 
420532ed618SSoby Mathew /******************************************************************************
421532ed618SSoby Mathew  * Helper function to return the current local power state of each power domain
422532ed618SSoby Mathew  * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
423532ed618SSoby Mathew  * function will be called after a cpu is powered on to find the local state
424532ed618SSoby Mathew  * each power domain has emerged from.
425532ed618SSoby Mathew  *****************************************************************************/
4263b802105SBoyan Karatotev void psci_get_target_local_pwr_states(unsigned int cpu_idx, unsigned int end_pwrlvl,
427532ed618SSoby Mathew 				      psci_power_state_t *target_state)
428532ed618SSoby Mathew {
429532ed618SSoby Mathew 	unsigned int parent_idx, lvl;
430532ed618SSoby Mathew 	plat_local_state_t *pd_state = target_state->pwr_domain_state;
431532ed618SSoby Mathew 
432532ed618SSoby Mathew 	pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
4333b802105SBoyan Karatotev 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
434532ed618SSoby Mathew 
435532ed618SSoby Mathew 	/* Copy the local power state from node to state_info */
4366b7b0f36SAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
437a10d3632SJeenu Viswambharan 		pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
438532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
439532ed618SSoby Mathew 	}
440532ed618SSoby Mathew 
441532ed618SSoby Mathew 	/* Set the the higher levels to RUN */
442c7b0a28dSMaheedhar Bollapalli 	for (; lvl <= PLAT_MAX_PWR_LVL; lvl++) {
443532ed618SSoby Mathew 		target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
444532ed618SSoby Mathew 	}
445c7b0a28dSMaheedhar Bollapalli }
446532ed618SSoby Mathew 
447532ed618SSoby Mathew /******************************************************************************
448532ed618SSoby Mathew  * Helper function to set the target local power state that each power domain
449532ed618SSoby Mathew  * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
450532ed618SSoby Mathew  * enter. This function will be called after coordination of requested power
451532ed618SSoby Mathew  * states has been done for each power level.
452532ed618SSoby Mathew  *****************************************************************************/
4533b802105SBoyan Karatotev void psci_set_target_local_pwr_states(unsigned int cpu_idx, unsigned int end_pwrlvl,
454532ed618SSoby Mathew 				      const psci_power_state_t *target_state)
455532ed618SSoby Mathew {
456532ed618SSoby Mathew 	unsigned int parent_idx, lvl;
457532ed618SSoby Mathew 	const plat_local_state_t *pd_state = target_state->pwr_domain_state;
458532ed618SSoby Mathew 
459532ed618SSoby Mathew 	psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
460532ed618SSoby Mathew 
461532ed618SSoby Mathew 	/*
462a10d3632SJeenu Viswambharan 	 * Need to flush as local_state might be accessed with Data Cache
463532ed618SSoby Mathew 	 * disabled during power on
464532ed618SSoby Mathew 	 */
465a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data.local_state);
466532ed618SSoby Mathew 
4673b802105SBoyan Karatotev 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
468532ed618SSoby Mathew 
469532ed618SSoby Mathew 	/* Copy the local_state from state_info */
4706b7b0f36SAntonio Nino Diaz 	for (lvl = 1U; lvl <= end_pwrlvl; lvl++) {
471a10d3632SJeenu Viswambharan 		set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
472532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
473532ed618SSoby Mathew 	}
474532ed618SSoby Mathew }
475532ed618SSoby Mathew 
476532ed618SSoby Mathew /*******************************************************************************
477532ed618SSoby Mathew  * PSCI helper function to get the parent nodes corresponding to a cpu_index.
478532ed618SSoby Mathew  ******************************************************************************/
479fc81021aSDeepika Bhavnani void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
480532ed618SSoby Mathew 				      unsigned int end_lvl,
4816b7b0f36SAntonio Nino Diaz 				      unsigned int *node_index)
482532ed618SSoby Mathew {
483532ed618SSoby Mathew 	unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node;
4846311f63dSVarun Wadekar 	unsigned int i;
4856b7b0f36SAntonio Nino Diaz 	unsigned int *node = node_index;
486532ed618SSoby Mathew 
4876b7b0f36SAntonio Nino Diaz 	for (i = PSCI_CPU_PWR_LVL + 1U; i <= end_lvl; i++) {
4886b7b0f36SAntonio Nino Diaz 		*node = parent_node;
4896b7b0f36SAntonio Nino Diaz 		node++;
490532ed618SSoby Mathew 		parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
491532ed618SSoby Mathew 	}
492532ed618SSoby Mathew }
493532ed618SSoby Mathew 
494532ed618SSoby Mathew /******************************************************************************
495532ed618SSoby Mathew  * This function is invoked post CPU power up and initialization. It sets the
496532ed618SSoby Mathew  * affinity info state, target power state and requested power state for the
497532ed618SSoby Mathew  * current CPU and all its ancestor power domains to RUN.
498532ed618SSoby Mathew  *****************************************************************************/
4993b802105SBoyan Karatotev void psci_set_pwr_domains_to_run(unsigned int cpu_idx, unsigned int end_pwrlvl)
500532ed618SSoby Mathew {
5013b802105SBoyan Karatotev 	unsigned int parent_idx, lvl;
502532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
503532ed618SSoby Mathew 
504532ed618SSoby Mathew 	/* Reset the local_state to RUN for the non cpu power domains. */
5056b7b0f36SAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
506a10d3632SJeenu Viswambharan 		set_non_cpu_pd_node_local_state(parent_idx,
507a10d3632SJeenu Viswambharan 				PSCI_LOCAL_STATE_RUN);
508532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl,
509532ed618SSoby Mathew 					     cpu_idx,
510532ed618SSoby Mathew 					     PSCI_LOCAL_STATE_RUN);
511532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
512532ed618SSoby Mathew 	}
513532ed618SSoby Mathew 
514532ed618SSoby Mathew 	/* Set the affinity info state to ON */
515532ed618SSoby Mathew 	psci_set_aff_info_state(AFF_STATE_ON);
516532ed618SSoby Mathew 
517532ed618SSoby Mathew 	psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
518a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data);
519532ed618SSoby Mathew }
520532ed618SSoby Mathew 
521532ed618SSoby Mathew /******************************************************************************
522606b7430SWing Li  * This function is used in platform-coordinated mode.
523606b7430SWing Li  *
524532ed618SSoby Mathew  * This function is passed the local power states requested for each power
525532ed618SSoby Mathew  * domain (state_info) between the current CPU domain and its ancestors until
526532ed618SSoby Mathew  * the target power level (end_pwrlvl). It updates the array of requested power
527532ed618SSoby Mathew  * states with this information.
528532ed618SSoby Mathew  *
529532ed618SSoby Mathew  * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
530532ed618SSoby Mathew  * retrieves the states requested by all the cpus of which the power domain at
531532ed618SSoby Mathew  * that level is an ancestor. It passes this information to the platform to
532532ed618SSoby Mathew  * coordinate and return the target power state. If the target state for a level
533532ed618SSoby Mathew  * is RUN then subsequent levels are not considered. At the CPU level, state
534532ed618SSoby Mathew  * coordination is not required. Hence, the requested and the target states are
535532ed618SSoby Mathew  * the same.
536532ed618SSoby Mathew  *
537532ed618SSoby Mathew  * The 'state_info' is updated with the target state for each level between the
538532ed618SSoby Mathew  * CPU and the 'end_pwrlvl' and returned to the caller.
539532ed618SSoby Mathew  *
540532ed618SSoby Mathew  * This function will only be invoked with data cache enabled and while
541532ed618SSoby Mathew  * powering down a core.
542532ed618SSoby Mathew  *****************************************************************************/
5433b802105SBoyan Karatotev void psci_do_state_coordination(unsigned int cpu_idx, unsigned int end_pwrlvl,
544532ed618SSoby Mathew 				psci_power_state_t *state_info)
545532ed618SSoby Mathew {
5463b802105SBoyan Karatotev 	unsigned int lvl, parent_idx;
547fc81021aSDeepika Bhavnani 	unsigned int start_idx;
5486b7b0f36SAntonio Nino Diaz 	unsigned int ncpus;
5497b970841SNithin G 	plat_local_state_t target_state;
550532ed618SSoby Mathew 
551532ed618SSoby Mathew 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
552532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
553532ed618SSoby Mathew 
554532ed618SSoby Mathew 	/* For level 0, the requested state will be equivalent
555532ed618SSoby Mathew 	   to target state */
5566b7b0f36SAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
557532ed618SSoby Mathew 
558532ed618SSoby Mathew 		/* First update the requested power state */
559532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl, cpu_idx,
560532ed618SSoby Mathew 					     state_info->pwr_domain_state[lvl]);
561532ed618SSoby Mathew 
562532ed618SSoby Mathew 		/* Get the requested power states for this power level */
563532ed618SSoby Mathew 		start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
5647b970841SNithin G 		plat_local_state_t const *req_states = psci_get_req_local_pwr_states(lvl,
5657b970841SNithin G 										start_idx);
566532ed618SSoby Mathew 
567532ed618SSoby Mathew 		/*
568532ed618SSoby Mathew 		 * Let the platform coordinate amongst the requested states at
569532ed618SSoby Mathew 		 * this power level and return the target local power state.
570532ed618SSoby Mathew 		 */
571532ed618SSoby Mathew 		ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
572532ed618SSoby Mathew 		target_state = plat_get_target_pwr_state(lvl,
573532ed618SSoby Mathew 							 req_states,
574532ed618SSoby Mathew 							 ncpus);
575532ed618SSoby Mathew 
576532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = target_state;
577532ed618SSoby Mathew 
578532ed618SSoby Mathew 		/* Break early if the negotiated target power state is RUN */
579c7b0a28dSMaheedhar Bollapalli 		if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0) {
580532ed618SSoby Mathew 			break;
581c7b0a28dSMaheedhar Bollapalli 		}
582532ed618SSoby Mathew 
583532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
584532ed618SSoby Mathew 	}
585532ed618SSoby Mathew 
586532ed618SSoby Mathew 	/*
587532ed618SSoby Mathew 	 * This is for cases when we break out of the above loop early because
588532ed618SSoby Mathew 	 * the target power state is RUN at a power level < end_pwlvl.
589532ed618SSoby Mathew 	 * We update the requested power state from state_info and then
590532ed618SSoby Mathew 	 * set the target state as RUN.
591532ed618SSoby Mathew 	 */
5926b7b0f36SAntonio Nino Diaz 	for (lvl = lvl + 1U; lvl <= end_pwrlvl; lvl++) {
593532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl, cpu_idx,
594532ed618SSoby Mathew 					     state_info->pwr_domain_state[lvl]);
595532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
596532ed618SSoby Mathew 
597532ed618SSoby Mathew 	}
598532ed618SSoby Mathew }
599532ed618SSoby Mathew 
600606b7430SWing Li #if PSCI_OS_INIT_MODE
601606b7430SWing Li /******************************************************************************
602606b7430SWing Li  * This function is used in OS-initiated mode.
603606b7430SWing Li  *
604606b7430SWing Li  * This function is passed the local power states requested for each power
605606b7430SWing Li  * domain (state_info) between the current CPU domain and its ancestors until
606606b7430SWing Li  * the target power level (end_pwrlvl), and ensures the requested power states
607606b7430SWing Li  * are valid. It updates the array of requested power states with this
608606b7430SWing Li  * information.
609606b7430SWing Li  *
610606b7430SWing Li  * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
611606b7430SWing Li  * retrieves the states requested by all the cpus of which the power domain at
612606b7430SWing Li  * that level is an ancestor. It passes this information to the platform to
613606b7430SWing Li  * coordinate and return the target power state. If the requested state does
614606b7430SWing Li  * not match the target state, the request is denied.
615606b7430SWing Li  *
616606b7430SWing Li  * The 'state_info' is not modified.
617606b7430SWing Li  *
618606b7430SWing Li  * This function will only be invoked with data cache enabled and while
619606b7430SWing Li  * powering down a core.
620606b7430SWing Li  *****************************************************************************/
6213b802105SBoyan Karatotev int psci_validate_state_coordination(unsigned int cpu_idx, unsigned int end_pwrlvl,
622606b7430SWing Li 				     psci_power_state_t *state_info)
623606b7430SWing Li {
624606b7430SWing Li 	int rc = PSCI_E_SUCCESS;
6253b802105SBoyan Karatotev 	unsigned int lvl, parent_idx;
626606b7430SWing Li 	unsigned int start_idx;
627606b7430SWing Li 	unsigned int ncpus;
628606b7430SWing Li 	plat_local_state_t target_state, *req_states;
629606b7430SWing Li 	plat_local_state_t prev[PLAT_MAX_PWR_LVL];
630606b7430SWing Li 
631606b7430SWing Li 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
632606b7430SWing Li 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
633606b7430SWing Li 
634606b7430SWing Li 	/*
635606b7430SWing Li 	 * Save a copy of the previous requested local power states and update
636606b7430SWing Li 	 * the new requested local power states.
637606b7430SWing Li 	 */
638606b7430SWing Li 	psci_update_req_local_pwr_states(end_pwrlvl, cpu_idx, state_info, prev);
639606b7430SWing Li 
640606b7430SWing Li 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
641606b7430SWing Li 		/* Get the requested power states for this power level */
642606b7430SWing Li 		start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
643606b7430SWing Li 		req_states = psci_get_req_local_pwr_states(lvl, start_idx);
644606b7430SWing Li 
645606b7430SWing Li 		/*
646606b7430SWing Li 		 * Let the platform coordinate amongst the requested states at
647606b7430SWing Li 		 * this power level and return the target local power state.
648606b7430SWing Li 		 */
649606b7430SWing Li 		ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
650606b7430SWing Li 		target_state = plat_get_target_pwr_state(lvl,
651606b7430SWing Li 							 req_states,
652606b7430SWing Li 							 ncpus);
653606b7430SWing Li 
654606b7430SWing Li 		/*
655606b7430SWing Li 		 * Verify that the requested power state matches the target
656606b7430SWing Li 		 * local power state.
657606b7430SWing Li 		 */
658606b7430SWing Li 		if (state_info->pwr_domain_state[lvl] != target_state) {
659606b7430SWing Li 			if (target_state == PSCI_LOCAL_STATE_RUN) {
660606b7430SWing Li 				rc = PSCI_E_DENIED;
661606b7430SWing Li 			} else {
662606b7430SWing Li 				rc = PSCI_E_INVALID_PARAMS;
663606b7430SWing Li 			}
664606b7430SWing Li 			goto exit;
665606b7430SWing Li 		}
666412d92fdSPatrick Delaunay 
667412d92fdSPatrick Delaunay 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
668606b7430SWing Li 	}
669606b7430SWing Li 
670606b7430SWing Li 	/*
671606b7430SWing Li 	 * Verify that the current core is the last running core at the
672606b7430SWing Li 	 * specified power level.
673606b7430SWing Li 	 */
674606b7430SWing Li 	lvl = state_info->last_at_pwrlvl;
6753b802105SBoyan Karatotev 	if (!psci_is_last_cpu_to_idle_at_pwrlvl(cpu_idx, lvl)) {
676606b7430SWing Li 		rc = PSCI_E_DENIED;
677606b7430SWing Li 	}
678606b7430SWing Li 
679606b7430SWing Li exit:
680606b7430SWing Li 	if (rc != PSCI_E_SUCCESS) {
681606b7430SWing Li 		/* Restore the previous requested local power states. */
682606b7430SWing Li 		psci_restore_req_local_pwr_states(cpu_idx, prev);
683606b7430SWing Li 		return rc;
684606b7430SWing Li 	}
685606b7430SWing Li 
686606b7430SWing Li 	return rc;
687606b7430SWing Li }
688606b7430SWing Li #endif
689606b7430SWing Li 
690532ed618SSoby Mathew /******************************************************************************
691532ed618SSoby Mathew  * This function validates a suspend request by making sure that if a standby
692532ed618SSoby Mathew  * state is requested then no power level is turned off and the highest power
693532ed618SSoby Mathew  * level is placed in a standby/retention state.
694532ed618SSoby Mathew  *
695532ed618SSoby Mathew  * It also ensures that the state level X will enter is not shallower than the
696532ed618SSoby Mathew  * state level X + 1 will enter.
697532ed618SSoby Mathew  *
698532ed618SSoby Mathew  * This validation will be enabled only for DEBUG builds as the platform is
699532ed618SSoby Mathew  * expected to perform these validations as well.
700532ed618SSoby Mathew  *****************************************************************************/
701532ed618SSoby Mathew int psci_validate_suspend_req(const psci_power_state_t *state_info,
702532ed618SSoby Mathew 			      unsigned int is_power_down_state)
703532ed618SSoby Mathew {
704532ed618SSoby Mathew 	unsigned int max_off_lvl, target_lvl, max_retn_lvl;
705532ed618SSoby Mathew 	plat_local_state_t state;
706532ed618SSoby Mathew 	plat_local_state_type_t req_state_type, deepest_state_type;
707532ed618SSoby Mathew 	int i;
708532ed618SSoby Mathew 
709532ed618SSoby Mathew 	/* Find the target suspend power level */
710532ed618SSoby Mathew 	target_lvl = psci_find_target_suspend_lvl(state_info);
711532ed618SSoby Mathew 	if (target_lvl == PSCI_INVALID_PWR_LVL)
712532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
713532ed618SSoby Mathew 
714532ed618SSoby Mathew 	/* All power domain levels are in a RUN state to begin with */
715532ed618SSoby Mathew 	deepest_state_type = STATE_TYPE_RUN;
716532ed618SSoby Mathew 
7176b7b0f36SAntonio Nino Diaz 	for (i = (int) target_lvl; i >= (int) PSCI_CPU_PWR_LVL; i--) {
718532ed618SSoby Mathew 		state = state_info->pwr_domain_state[i];
719532ed618SSoby Mathew 		req_state_type = find_local_state_type(state);
720532ed618SSoby Mathew 
721532ed618SSoby Mathew 		/*
722532ed618SSoby Mathew 		 * While traversing from the highest power level to the lowest,
723532ed618SSoby Mathew 		 * the state requested for lower levels has to be the same or
724532ed618SSoby Mathew 		 * deeper i.e. equal to or greater than the state at the higher
725532ed618SSoby Mathew 		 * levels. If this condition is true, then the requested state
726532ed618SSoby Mathew 		 * becomes the deepest state encountered so far.
727532ed618SSoby Mathew 		 */
728532ed618SSoby Mathew 		if (req_state_type < deepest_state_type)
729532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
730532ed618SSoby Mathew 		deepest_state_type = req_state_type;
731532ed618SSoby Mathew 	}
732532ed618SSoby Mathew 
733532ed618SSoby Mathew 	/* Find the highest off power level */
734532ed618SSoby Mathew 	max_off_lvl = psci_find_max_off_lvl(state_info);
735532ed618SSoby Mathew 
736532ed618SSoby Mathew 	/* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
737532ed618SSoby Mathew 	max_retn_lvl = PSCI_INVALID_PWR_LVL;
738532ed618SSoby Mathew 	if (target_lvl != max_off_lvl)
739532ed618SSoby Mathew 		max_retn_lvl = target_lvl;
740532ed618SSoby Mathew 
741532ed618SSoby Mathew 	/*
742532ed618SSoby Mathew 	 * If this is not a request for a power down state then max off level
743532ed618SSoby Mathew 	 * has to be invalid and max retention level has to be a valid power
744532ed618SSoby Mathew 	 * level.
745532ed618SSoby Mathew 	 */
7466b7b0f36SAntonio Nino Diaz 	if ((is_power_down_state == 0U) &&
7476b7b0f36SAntonio Nino Diaz 			((max_off_lvl != PSCI_INVALID_PWR_LVL) ||
7486b7b0f36SAntonio Nino Diaz 			 (max_retn_lvl == PSCI_INVALID_PWR_LVL)))
749532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
750532ed618SSoby Mathew 
751532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
752532ed618SSoby Mathew }
753532ed618SSoby Mathew 
754532ed618SSoby Mathew /******************************************************************************
755532ed618SSoby Mathew  * This function finds the highest power level which will be powered down
756532ed618SSoby Mathew  * amongst all the power levels specified in the 'state_info' structure
757532ed618SSoby Mathew  *****************************************************************************/
758532ed618SSoby Mathew unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
759532ed618SSoby Mathew {
760532ed618SSoby Mathew 	int i;
761532ed618SSoby Mathew 
7626b7b0f36SAntonio Nino Diaz 	for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
763c7b0a28dSMaheedhar Bollapalli 		if (is_local_state_off(state_info->pwr_domain_state[i]) != 0) {
7646b7b0f36SAntonio Nino Diaz 			return (unsigned int) i;
765532ed618SSoby Mathew 		}
766c7b0a28dSMaheedhar Bollapalli 	}
767532ed618SSoby Mathew 
768532ed618SSoby Mathew 	return PSCI_INVALID_PWR_LVL;
769532ed618SSoby Mathew }
770532ed618SSoby Mathew 
771532ed618SSoby Mathew /******************************************************************************
772532ed618SSoby Mathew  * This functions finds the level of the highest power domain which will be
773532ed618SSoby Mathew  * placed in a low power state during a suspend operation.
774532ed618SSoby Mathew  *****************************************************************************/
775532ed618SSoby Mathew unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
776532ed618SSoby Mathew {
777532ed618SSoby Mathew 	int i;
778532ed618SSoby Mathew 
7796b7b0f36SAntonio Nino Diaz 	for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
7806b7b0f36SAntonio Nino Diaz 		if (is_local_state_run(state_info->pwr_domain_state[i]) == 0)
7816b7b0f36SAntonio Nino Diaz 			return (unsigned int) i;
782532ed618SSoby Mathew 	}
783532ed618SSoby Mathew 
784532ed618SSoby Mathew 	return PSCI_INVALID_PWR_LVL;
785532ed618SSoby Mathew }
786532ed618SSoby Mathew 
787532ed618SSoby Mathew /*******************************************************************************
78874d27d00SAndrew F. Davis  * This function is passed the highest level in the topology tree that the
78974d27d00SAndrew F. Davis  * operation should be applied to and a list of node indexes. It picks up locks
79074d27d00SAndrew F. Davis  * from the node index list in order of increasing power domain level in the
79174d27d00SAndrew F. Davis  * range specified.
792532ed618SSoby Mathew  ******************************************************************************/
79374d27d00SAndrew F. Davis void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
79474d27d00SAndrew F. Davis 				   const unsigned int *parent_nodes)
795532ed618SSoby Mathew {
79674d27d00SAndrew F. Davis 	unsigned int parent_idx;
797532ed618SSoby Mathew 	unsigned int level;
798532ed618SSoby Mathew 
799532ed618SSoby Mathew 	/* No locking required for level 0. Hence start locking from level 1 */
8006b7b0f36SAntonio Nino Diaz 	for (level = PSCI_CPU_PWR_LVL + 1U; level <= end_pwrlvl; level++) {
80174d27d00SAndrew F. Davis 		parent_idx = parent_nodes[level - 1U];
802532ed618SSoby Mathew 		psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
803532ed618SSoby Mathew 	}
804532ed618SSoby Mathew }
805532ed618SSoby Mathew 
806532ed618SSoby Mathew /*******************************************************************************
80774d27d00SAndrew F. Davis  * This function is passed the highest level in the topology tree that the
80874d27d00SAndrew F. Davis  * operation should be applied to and a list of node indexes. It releases the
80974d27d00SAndrew F. Davis  * locks in order of decreasing power domain level in the range specified.
810532ed618SSoby Mathew  ******************************************************************************/
81174d27d00SAndrew F. Davis void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
81274d27d00SAndrew F. Davis 				   const unsigned int *parent_nodes)
813532ed618SSoby Mathew {
81474d27d00SAndrew F. Davis 	unsigned int parent_idx;
8156b7b0f36SAntonio Nino Diaz 	unsigned int level;
816532ed618SSoby Mathew 
817532ed618SSoby Mathew 	/* Unlock top down. No unlocking required for level 0. */
8182fe75a2dSZelalem 	for (level = end_pwrlvl; level >= (PSCI_CPU_PWR_LVL + 1U); level--) {
8196b7b0f36SAntonio Nino Diaz 		parent_idx = parent_nodes[level - 1U];
820532ed618SSoby Mathew 		psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
821532ed618SSoby Mathew 	}
822532ed618SSoby Mathew }
823532ed618SSoby Mathew 
824532ed618SSoby Mathew /*******************************************************************************
825532ed618SSoby Mathew  * This function determines the full entrypoint information for the requested
826532ed618SSoby Mathew  * PSCI entrypoint on power on/resume and returns it.
827532ed618SSoby Mathew  ******************************************************************************/
828402b3cf8SJulius Werner #ifdef __aarch64__
829532ed618SSoby Mathew static int psci_get_ns_ep_info(entry_point_info_t *ep,
830532ed618SSoby Mathew 			       uintptr_t entrypoint,
831532ed618SSoby Mathew 			       u_register_t context_id)
832532ed618SSoby Mathew {
833532ed618SSoby Mathew 	u_register_t ep_attr, sctlr;
834532ed618SSoby Mathew 	unsigned int daif, ee, mode;
835532ed618SSoby Mathew 	u_register_t ns_scr_el3 = read_scr_el3();
836532ed618SSoby Mathew 	u_register_t ns_sctlr_el1 = read_sctlr_el1();
837532ed618SSoby Mathew 
8386b7b0f36SAntonio Nino Diaz 	sctlr = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
8396b7b0f36SAntonio Nino Diaz 		read_sctlr_el2() : ns_sctlr_el1;
840532ed618SSoby Mathew 	ee = 0;
841532ed618SSoby Mathew 
842532ed618SSoby Mathew 	ep_attr = NON_SECURE | EP_ST_DISABLE;
8436b7b0f36SAntonio Nino Diaz 	if ((sctlr & SCTLR_EE_BIT) != 0U) {
844532ed618SSoby Mathew 		ep_attr |= EP_EE_BIG;
845532ed618SSoby Mathew 		ee = 1;
846532ed618SSoby Mathew 	}
847532ed618SSoby Mathew 	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
848532ed618SSoby Mathew 
849532ed618SSoby Mathew 	ep->pc = entrypoint;
85032f0d3c6SDouglas Raillard 	zeromem(&ep->args, sizeof(ep->args));
851532ed618SSoby Mathew 	ep->args.arg0 = context_id;
852532ed618SSoby Mathew 
853532ed618SSoby Mathew 	/*
854532ed618SSoby Mathew 	 * Figure out whether the cpu enters the non-secure address space
855532ed618SSoby Mathew 	 * in aarch32 or aarch64
856532ed618SSoby Mathew 	 */
8576b7b0f36SAntonio Nino Diaz 	if ((ns_scr_el3 & SCR_RW_BIT) != 0U) {
858532ed618SSoby Mathew 
859532ed618SSoby Mathew 		/*
860532ed618SSoby Mathew 		 * Check whether a Thumb entry point has been provided for an
861532ed618SSoby Mathew 		 * aarch64 EL
862532ed618SSoby Mathew 		 */
8636b7b0f36SAntonio Nino Diaz 		if ((entrypoint & 0x1UL) != 0UL)
864532ed618SSoby Mathew 			return PSCI_E_INVALID_ADDRESS;
865532ed618SSoby Mathew 
8666b7b0f36SAntonio Nino Diaz 		mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? MODE_EL2 : MODE_EL1;
867532ed618SSoby Mathew 
868d7b5f408SJimmy Brisson 		ep->spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX,
869d7b5f408SJimmy Brisson 				   DISABLE_ALL_EXCEPTIONS);
870532ed618SSoby Mathew 	} else {
871532ed618SSoby Mathew 
8726b7b0f36SAntonio Nino Diaz 		mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
8736b7b0f36SAntonio Nino Diaz 			MODE32_hyp : MODE32_svc;
874532ed618SSoby Mathew 
875532ed618SSoby Mathew 		/*
876532ed618SSoby Mathew 		 * TODO: Choose async. exception bits if HYP mode is not
877532ed618SSoby Mathew 		 * implemented according to the values of SCR.{AW, FW} bits
878532ed618SSoby Mathew 		 */
879532ed618SSoby Mathew 		daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
880532ed618SSoby Mathew 
881d7b5f408SJimmy Brisson 		ep->spsr = SPSR_MODE32((uint64_t)mode, entrypoint & 0x1, ee,
882d7b5f408SJimmy Brisson 				       daif);
883532ed618SSoby Mathew 	}
884532ed618SSoby Mathew 
885532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
886532ed618SSoby Mathew }
887402b3cf8SJulius Werner #else /* !__aarch64__ */
888402b3cf8SJulius Werner static int psci_get_ns_ep_info(entry_point_info_t *ep,
889402b3cf8SJulius Werner 			       uintptr_t entrypoint,
890402b3cf8SJulius Werner 			       u_register_t context_id)
891402b3cf8SJulius Werner {
892402b3cf8SJulius Werner 	u_register_t ep_attr;
893402b3cf8SJulius Werner 	unsigned int aif, ee, mode;
894402b3cf8SJulius Werner 	u_register_t scr = read_scr();
895402b3cf8SJulius Werner 	u_register_t ns_sctlr, sctlr;
896402b3cf8SJulius Werner 
897402b3cf8SJulius Werner 	/* Switch to non secure state */
898402b3cf8SJulius Werner 	write_scr(scr | SCR_NS_BIT);
899402b3cf8SJulius Werner 	isb();
900402b3cf8SJulius Werner 	ns_sctlr = read_sctlr();
901402b3cf8SJulius Werner 
902402b3cf8SJulius Werner 	sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
903402b3cf8SJulius Werner 
904402b3cf8SJulius Werner 	/* Return to original state */
905402b3cf8SJulius Werner 	write_scr(scr);
906402b3cf8SJulius Werner 	isb();
907402b3cf8SJulius Werner 	ee = 0;
908402b3cf8SJulius Werner 
909402b3cf8SJulius Werner 	ep_attr = NON_SECURE | EP_ST_DISABLE;
910402b3cf8SJulius Werner 	if (sctlr & SCTLR_EE_BIT) {
911402b3cf8SJulius Werner 		ep_attr |= EP_EE_BIG;
912402b3cf8SJulius Werner 		ee = 1;
913402b3cf8SJulius Werner 	}
914402b3cf8SJulius Werner 	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
915402b3cf8SJulius Werner 
916402b3cf8SJulius Werner 	ep->pc = entrypoint;
917402b3cf8SJulius Werner 	zeromem(&ep->args, sizeof(ep->args));
918402b3cf8SJulius Werner 	ep->args.arg0 = context_id;
919402b3cf8SJulius Werner 
920402b3cf8SJulius Werner 	mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
921402b3cf8SJulius Werner 
922402b3cf8SJulius Werner 	/*
923402b3cf8SJulius Werner 	 * TODO: Choose async. exception bits if HYP mode is not
924402b3cf8SJulius Werner 	 * implemented according to the values of SCR.{AW, FW} bits
925402b3cf8SJulius Werner 	 */
926402b3cf8SJulius Werner 	aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
927402b3cf8SJulius Werner 
928402b3cf8SJulius Werner 	ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
929402b3cf8SJulius Werner 
930402b3cf8SJulius Werner 	return PSCI_E_SUCCESS;
931402b3cf8SJulius Werner }
932402b3cf8SJulius Werner 
933402b3cf8SJulius Werner #endif /* __aarch64__ */
934532ed618SSoby Mathew 
935532ed618SSoby Mathew /*******************************************************************************
936532ed618SSoby Mathew  * This function validates the entrypoint with the platform layer if the
937532ed618SSoby Mathew  * appropriate pm_ops hook is exported by the platform and returns the
938532ed618SSoby Mathew  * 'entry_point_info'.
939532ed618SSoby Mathew  ******************************************************************************/
940532ed618SSoby Mathew int psci_validate_entry_point(entry_point_info_t *ep,
941532ed618SSoby Mathew 			      uintptr_t entrypoint,
942532ed618SSoby Mathew 			      u_register_t context_id)
943532ed618SSoby Mathew {
944532ed618SSoby Mathew 	int rc;
945532ed618SSoby Mathew 
946532ed618SSoby Mathew 	/* Validate the entrypoint using platform psci_ops */
9476b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->validate_ns_entrypoint != NULL) {
948532ed618SSoby Mathew 		rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
949c7b0a28dSMaheedhar Bollapalli 		if (rc != PSCI_E_SUCCESS) {
950532ed618SSoby Mathew 			return PSCI_E_INVALID_ADDRESS;
951532ed618SSoby Mathew 		}
952c7b0a28dSMaheedhar Bollapalli 	}
953532ed618SSoby Mathew 
954532ed618SSoby Mathew 	/*
955532ed618SSoby Mathew 	 * Verify and derive the re-entry information for
956532ed618SSoby Mathew 	 * the non-secure world from the non-secure state from
957532ed618SSoby Mathew 	 * where this call originated.
958532ed618SSoby Mathew 	 */
959532ed618SSoby Mathew 	rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
960532ed618SSoby Mathew 	return rc;
961532ed618SSoby Mathew }
962532ed618SSoby Mathew 
963532ed618SSoby Mathew /*******************************************************************************
964532ed618SSoby Mathew  * Generic handler which is called when a cpu is physically powered on. It
965532ed618SSoby Mathew  * traverses the node information and finds the highest power level powered
966532ed618SSoby Mathew  * off and performs generic, architectural, platform setup and state management
967532ed618SSoby Mathew  * to power on that power level and power levels below it.
968532ed618SSoby Mathew  * e.g. For a cpu that's been powered on, it will call the platform specific
969532ed618SSoby Mathew  * code to enable the gic cpu interface and for a cluster it will enable
970532ed618SSoby Mathew  * coherency at the interconnect level in addition to gic cpu interface.
971532ed618SSoby Mathew  ******************************************************************************/
972cf0b1492SSoby Mathew void psci_warmboot_entrypoint(void)
973532ed618SSoby Mathew {
9746b7b0f36SAntonio Nino Diaz 	unsigned int end_pwrlvl;
975fc81021aSDeepika Bhavnani 	unsigned int cpu_idx = plat_my_core_pos();
97674d27d00SAndrew F. Davis 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
977532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
978532ed618SSoby Mathew 
97924a70738SBoyan Karatotev 	/* Init registers that never change for the lifetime of TF-A */
98083ec7e45SBoyan Karatotev 	cm_manage_extensions_el3(cpu_idx);
98124a70738SBoyan Karatotev 
982532ed618SSoby Mathew 	/*
983532ed618SSoby Mathew 	 * Verify that we have been explicitly turned ON or resumed from
984532ed618SSoby Mathew 	 * suspend.
985532ed618SSoby Mathew 	 */
986532ed618SSoby Mathew 	if (psci_get_aff_info_state() == AFF_STATE_OFF) {
98733e8c569SAndrew Walbran 		ERROR("Unexpected affinity info state.\n");
988532ed618SSoby Mathew 		panic();
989532ed618SSoby Mathew 	}
990532ed618SSoby Mathew 
991532ed618SSoby Mathew 	/*
992532ed618SSoby Mathew 	 * Get the maximum power domain level to traverse to after this cpu
993532ed618SSoby Mathew 	 * has been physically powered up.
994532ed618SSoby Mathew 	 */
995532ed618SSoby Mathew 	end_pwrlvl = get_power_on_target_pwrlvl();
996532ed618SSoby Mathew 
99774d27d00SAndrew F. Davis 	/* Get the parent nodes */
99874d27d00SAndrew F. Davis 	psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
99974d27d00SAndrew F. Davis 
1000532ed618SSoby Mathew 	/*
1001532ed618SSoby Mathew 	 * This function acquires the lock corresponding to each power level so
1002532ed618SSoby Mathew 	 * that by the time all locks are taken, the system topology is snapshot
1003532ed618SSoby Mathew 	 * and state management can be done safely.
1004532ed618SSoby Mathew 	 */
100574d27d00SAndrew F. Davis 	psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
1006532ed618SSoby Mathew 
10073b802105SBoyan Karatotev 	psci_get_target_local_pwr_states(cpu_idx, end_pwrlvl, &state_info);
1008bfc87a8dSSoby Mathew 
1009532ed618SSoby Mathew #if ENABLE_PSCI_STAT
101004c1db1eSdp-arm 	plat_psci_stat_accounting_stop(&state_info);
1011532ed618SSoby Mathew #endif
1012532ed618SSoby Mathew 
1013532ed618SSoby Mathew 	/*
1014532ed618SSoby Mathew 	 * This CPU could be resuming from suspend or it could have just been
1015532ed618SSoby Mathew 	 * turned on. To distinguish between these 2 cases, we examine the
1016532ed618SSoby Mathew 	 * affinity state of the CPU:
1017532ed618SSoby Mathew 	 *  - If the affinity state is ON_PENDING then it has just been
1018532ed618SSoby Mathew 	 *    turned on.
1019532ed618SSoby Mathew 	 *  - Else it is resuming from suspend.
1020532ed618SSoby Mathew 	 *
1021532ed618SSoby Mathew 	 * Depending on the type of warm reset identified, choose the right set
1022532ed618SSoby Mathew 	 * of power management handler and perform the generic, architecture
1023532ed618SSoby Mathew 	 * and platform specific handling.
1024532ed618SSoby Mathew 	 */
1025c7b0a28dSMaheedhar Bollapalli 	if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING) {
1026532ed618SSoby Mathew 		psci_cpu_on_finish(cpu_idx, &state_info);
1027c7b0a28dSMaheedhar Bollapalli 	} else {
10282b5e00d4SBoyan Karatotev 		unsigned int max_off_lvl = psci_find_max_off_lvl(&state_info);
10292b5e00d4SBoyan Karatotev 
10302b5e00d4SBoyan Karatotev 		assert(max_off_lvl != PSCI_INVALID_PWR_LVL);
10312b5e00d4SBoyan Karatotev 		psci_cpu_suspend_to_powerdown_finish(cpu_idx, max_off_lvl, &state_info);
10322b5e00d4SBoyan Karatotev 	}
1033532ed618SSoby Mathew 
1034532ed618SSoby Mathew 	/*
1035*ef738d19SManish Pandey 	 * Caches and (importantly) coherency are on so we can rely on seeing
1036*ef738d19SManish Pandey 	 * whatever the primary gave us without explicit cache maintenance
1037*ef738d19SManish Pandey 	 */
1038*ef738d19SManish Pandey 	entry_point_info_t *ep = get_cpu_data(warmboot_ep_info);
1039*ef738d19SManish Pandey 	cm_init_my_context(ep);
1040*ef738d19SManish Pandey 
1041*ef738d19SManish Pandey 	/*
1042e07e7392SBoyan Karatotev 	 * Generic management: Now we just need to retrieve the
1043e07e7392SBoyan Karatotev 	 * information that we had stashed away during the cpu_on
1044e07e7392SBoyan Karatotev 	 * call to set this cpu on its way.
1045e07e7392SBoyan Karatotev 	 */
1046e07e7392SBoyan Karatotev 	cm_prepare_el3_exit_ns();
1047e07e7392SBoyan Karatotev 
1048e07e7392SBoyan Karatotev 	/*
1049532ed618SSoby Mathew 	 * Set the requested and target state of this CPU and all the higher
1050532ed618SSoby Mathew 	 * power domains which are ancestors of this CPU to run.
1051532ed618SSoby Mathew 	 */
10523b802105SBoyan Karatotev 	psci_set_pwr_domains_to_run(cpu_idx, end_pwrlvl);
1053532ed618SSoby Mathew 
1054532ed618SSoby Mathew #if ENABLE_PSCI_STAT
10553b802105SBoyan Karatotev 	psci_stats_update_pwr_up(cpu_idx, end_pwrlvl, &state_info);
1056532ed618SSoby Mathew #endif
1057532ed618SSoby Mathew 
1058532ed618SSoby Mathew 	/*
1059532ed618SSoby Mathew 	 * This loop releases the lock corresponding to each power level
1060532ed618SSoby Mathew 	 * in the reverse order to which they were acquired.
1061532ed618SSoby Mathew 	 */
106274d27d00SAndrew F. Davis 	psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
1063532ed618SSoby Mathew }
1064532ed618SSoby Mathew 
1065532ed618SSoby Mathew /*******************************************************************************
1066532ed618SSoby Mathew  * This function initializes the set of hooks that PSCI invokes as part of power
1067532ed618SSoby Mathew  * management operation. The power management hooks are expected to be provided
1068532ed618SSoby Mathew  * by the SPD, after it finishes all its initialization
1069532ed618SSoby Mathew  ******************************************************************************/
1070532ed618SSoby Mathew void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
1071532ed618SSoby Mathew {
10726b7b0f36SAntonio Nino Diaz 	assert(pm != NULL);
1073532ed618SSoby Mathew 	psci_spd_pm = pm;
1074532ed618SSoby Mathew 
10756b7b0f36SAntonio Nino Diaz 	if (pm->svc_migrate != NULL)
1076532ed618SSoby Mathew 		psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
1077532ed618SSoby Mathew 
10786b7b0f36SAntonio Nino Diaz 	if (pm->svc_migrate_info != NULL)
1079532ed618SSoby Mathew 		psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
1080532ed618SSoby Mathew 				| define_psci_cap(PSCI_MIG_INFO_TYPE);
1081532ed618SSoby Mathew }
1082532ed618SSoby Mathew 
1083532ed618SSoby Mathew /*******************************************************************************
1084532ed618SSoby Mathew  * This function invokes the migrate info hook in the spd_pm_ops. It performs
1085532ed618SSoby Mathew  * the necessary return value validation. If the Secure Payload is UP and
1086532ed618SSoby Mathew  * migrate capable, it returns the mpidr of the CPU on which the Secure payload
1087532ed618SSoby Mathew  * is resident through the mpidr parameter. Else the value of the parameter on
1088532ed618SSoby Mathew  * return is undefined.
1089532ed618SSoby Mathew  ******************************************************************************/
1090532ed618SSoby Mathew int psci_spd_migrate_info(u_register_t *mpidr)
1091532ed618SSoby Mathew {
1092532ed618SSoby Mathew 	int rc;
1093532ed618SSoby Mathew 
10946b7b0f36SAntonio Nino Diaz 	if ((psci_spd_pm == NULL) || (psci_spd_pm->svc_migrate_info == NULL))
1095532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
1096532ed618SSoby Mathew 
1097532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate_info(mpidr);
1098532ed618SSoby Mathew 
10996b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_TOS_UP_MIG_CAP) || (rc == PSCI_TOS_NOT_UP_MIG_CAP) ||
11006b7b0f36SAntonio Nino Diaz 	       (rc == PSCI_TOS_NOT_PRESENT_MP) || (rc == PSCI_E_NOT_SUPPORTED));
1101532ed618SSoby Mathew 
1102532ed618SSoby Mathew 	return rc;
1103532ed618SSoby Mathew }
1104532ed618SSoby Mathew 
1105532ed618SSoby Mathew 
1106532ed618SSoby Mathew /*******************************************************************************
1107532ed618SSoby Mathew  * This function prints the state of all power domains present in the
1108532ed618SSoby Mathew  * system
1109532ed618SSoby Mathew  ******************************************************************************/
1110532ed618SSoby Mathew void psci_print_power_domain_map(void)
1111532ed618SSoby Mathew {
1112532ed618SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_INFO
1113ab4df50cSPankaj Gupta 	unsigned int idx;
1114532ed618SSoby Mathew 	plat_local_state_t state;
1115532ed618SSoby Mathew 	plat_local_state_type_t state_type;
1116532ed618SSoby Mathew 
1117532ed618SSoby Mathew 	/* This array maps to the PSCI_STATE_X definitions in psci.h */
1118532ed618SSoby Mathew 	static const char * const psci_state_type_str[] = {
1119532ed618SSoby Mathew 		"ON",
1120532ed618SSoby Mathew 		"RETENTION",
1121532ed618SSoby Mathew 		"OFF",
1122532ed618SSoby Mathew 	};
1123532ed618SSoby Mathew 
1124532ed618SSoby Mathew 	INFO("PSCI Power Domain Map:\n");
1125ab4df50cSPankaj Gupta 	for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - psci_plat_core_count);
1126532ed618SSoby Mathew 							idx++) {
1127532ed618SSoby Mathew 		state_type = find_local_state_type(
1128532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].local_state);
1129b9338eeeSYann Gautier 		INFO("  Domain Node : Level %u, parent_node %u,"
1130532ed618SSoby Mathew 				" State %s (0x%x)\n",
1131532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].level,
1132532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].parent_node,
1133532ed618SSoby Mathew 				psci_state_type_str[state_type],
1134532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].local_state);
1135532ed618SSoby Mathew 	}
1136532ed618SSoby Mathew 
1137ab4df50cSPankaj Gupta 	for (idx = 0; idx < psci_plat_core_count; idx++) {
1138532ed618SSoby Mathew 		state = psci_get_cpu_local_state_by_idx(idx);
1139532ed618SSoby Mathew 		state_type = find_local_state_type(state);
1140b9338eeeSYann Gautier 		INFO("  CPU Node : MPID 0x%llx, parent_node %u,"
1141532ed618SSoby Mathew 				" State %s (0x%x)\n",
1142532ed618SSoby Mathew 				(unsigned long long)psci_cpu_pd_nodes[idx].mpidr,
1143532ed618SSoby Mathew 				psci_cpu_pd_nodes[idx].parent_node,
1144532ed618SSoby Mathew 				psci_state_type_str[state_type],
1145532ed618SSoby Mathew 				psci_get_cpu_local_state_by_idx(idx));
1146532ed618SSoby Mathew 	}
1147532ed618SSoby Mathew #endif
1148532ed618SSoby Mathew }
1149532ed618SSoby Mathew 
1150b10d4499SJeenu Viswambharan /******************************************************************************
1151b10d4499SJeenu Viswambharan  * Return whether any secondaries were powered up with CPU_ON call. A CPU that
1152b10d4499SJeenu Viswambharan  * have ever been powered up would have set its MPDIR value to something other
1153b10d4499SJeenu Viswambharan  * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to
1154b10d4499SJeenu Viswambharan  * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is
1155b10d4499SJeenu Viswambharan  * meaningful only when called on the primary CPU during early boot.
1156b10d4499SJeenu Viswambharan  *****************************************************************************/
1157b10d4499SJeenu Viswambharan int psci_secondaries_brought_up(void)
1158b10d4499SJeenu Viswambharan {
11596b7b0f36SAntonio Nino Diaz 	unsigned int idx, n_valid = 0U;
1160b10d4499SJeenu Viswambharan 
11616b7b0f36SAntonio Nino Diaz 	for (idx = 0U; idx < ARRAY_SIZE(psci_cpu_pd_nodes); idx++) {
1162b10d4499SJeenu Viswambharan 		if (psci_cpu_pd_nodes[idx].mpidr != PSCI_INVALID_MPIDR)
1163b10d4499SJeenu Viswambharan 			n_valid++;
1164b10d4499SJeenu Viswambharan 	}
1165b10d4499SJeenu Viswambharan 
11666b7b0f36SAntonio Nino Diaz 	assert(n_valid > 0U);
1167b10d4499SJeenu Viswambharan 
11686b7b0f36SAntonio Nino Diaz 	return (n_valid > 1U) ? 1 : 0;
1169b10d4499SJeenu Viswambharan }
1170b10d4499SJeenu Viswambharan 
1171b0408e87SJeenu Viswambharan /*******************************************************************************
1172b0408e87SJeenu Viswambharan  * Initiate power down sequence, by calling power down operations registered for
1173b0408e87SJeenu Viswambharan  * this CPU.
1174b0408e87SJeenu Viswambharan  ******************************************************************************/
11752b5e00d4SBoyan Karatotev void psci_pwrdown_cpu_start(unsigned int power_level)
1176b0408e87SJeenu Viswambharan {
11779b1e800eSBoyan Karatotev #if ENABLE_RUNTIME_INSTRUMENTATION
11789b1e800eSBoyan Karatotev 
11799b1e800eSBoyan Karatotev 	/*
11809b1e800eSBoyan Karatotev 	 * Flush cache line so that even if CPU power down happens
11819b1e800eSBoyan Karatotev 	 * the timestamp update is reflected in memory.
11829b1e800eSBoyan Karatotev 	 */
11839b1e800eSBoyan Karatotev 	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
11849b1e800eSBoyan Karatotev 		RT_INSTR_ENTER_CFLUSH,
11859b1e800eSBoyan Karatotev 		PMF_CACHE_MAINT);
11869b1e800eSBoyan Karatotev #endif
11879b1e800eSBoyan Karatotev 
1188b0408e87SJeenu Viswambharan #if HW_ASSISTED_COHERENCY
1189b0408e87SJeenu Viswambharan 	/*
1190b0408e87SJeenu Viswambharan 	 * With hardware-assisted coherency, the CPU drivers only initiate the
1191b0408e87SJeenu Viswambharan 	 * power down sequence, without performing cache-maintenance operations
1192c98db6c6SAndrew F. Davis 	 * in software. Data caches enabled both before and after this call.
1193b0408e87SJeenu Viswambharan 	 */
1194b0408e87SJeenu Viswambharan 	prepare_cpu_pwr_dwn(power_level);
1195b0408e87SJeenu Viswambharan #else
1196b0408e87SJeenu Viswambharan 	/*
1197b0408e87SJeenu Viswambharan 	 * Without hardware-assisted coherency, the CPU drivers disable data
1198c98db6c6SAndrew F. Davis 	 * caches, then perform cache-maintenance operations in software.
1199b0408e87SJeenu Viswambharan 	 *
1200c98db6c6SAndrew F. Davis 	 * This also calls prepare_cpu_pwr_dwn() to initiate power down
1201c98db6c6SAndrew F. Davis 	 * sequence, but that function will return with data caches disabled.
1202c98db6c6SAndrew F. Davis 	 * We must ensure that the stack memory is flushed out to memory before
1203c98db6c6SAndrew F. Davis 	 * we start popping from it again.
1204b0408e87SJeenu Viswambharan 	 */
1205b0408e87SJeenu Viswambharan 	psci_do_pwrdown_cache_maintenance(power_level);
1206b0408e87SJeenu Viswambharan #endif
12079b1e800eSBoyan Karatotev 
12089b1e800eSBoyan Karatotev #if ENABLE_RUNTIME_INSTRUMENTATION
12099b1e800eSBoyan Karatotev 	PMF_CAPTURE_TIMESTAMP(rt_instr_svc,
12109b1e800eSBoyan Karatotev 		RT_INSTR_EXIT_CFLUSH,
12119b1e800eSBoyan Karatotev 		PMF_NO_CACHE_MAINT);
12129b1e800eSBoyan Karatotev #endif
1213b0408e87SJeenu Viswambharan }
121422744909SSandeep Tripathy 
121522744909SSandeep Tripathy /*******************************************************************************
12162b5e00d4SBoyan Karatotev  * Finish a terminal power down sequence, ending with a wfi. In case of wakeup
12172b5e00d4SBoyan Karatotev  * will retry the sleep and panic if it persists.
12182b5e00d4SBoyan Karatotev  ******************************************************************************/
12192b5e00d4SBoyan Karatotev void __dead2 psci_pwrdown_cpu_end_terminal(void)
12202b5e00d4SBoyan Karatotev {
122145c7328cSBoyan Karatotev #if ERRATA_SME_POWER_DOWN
122245c7328cSBoyan Karatotev 	/*
122345c7328cSBoyan Karatotev 	 * force SME off to not get power down rejected. Getting here is
122445c7328cSBoyan Karatotev 	 * terminal so we don't care if we lose context because of another
122545c7328cSBoyan Karatotev 	 * wakeup
122645c7328cSBoyan Karatotev 	 */
122745c7328cSBoyan Karatotev 	if (is_feat_sme_supported()) {
122845c7328cSBoyan Karatotev 		write_svcr(0);
122945c7328cSBoyan Karatotev 		isb();
123045c7328cSBoyan Karatotev 	}
123145c7328cSBoyan Karatotev #endif /* ERRATA_SME_POWER_DOWN */
123245c7328cSBoyan Karatotev 
12332b5e00d4SBoyan Karatotev 	/*
12342b5e00d4SBoyan Karatotev 	 * Execute a wfi which, in most cases, will allow the power controller
12352b5e00d4SBoyan Karatotev 	 * to physically power down this cpu. Under some circumstances that may
12362b5e00d4SBoyan Karatotev 	 * be denied. Hopefully this is transient, retrying a few times should
12372b5e00d4SBoyan Karatotev 	 * power down.
12382b5e00d4SBoyan Karatotev 	 */
12392b5e00d4SBoyan Karatotev 	for (int i = 0; i < 32; i++)
12402b5e00d4SBoyan Karatotev 		psci_power_down_wfi();
12412b5e00d4SBoyan Karatotev 
12422b5e00d4SBoyan Karatotev 	/* Wake up wasn't transient. System is probably in a bad state. */
12432b5e00d4SBoyan Karatotev 	ERROR("Could not power off CPU.\n");
12442b5e00d4SBoyan Karatotev 	panic();
12452b5e00d4SBoyan Karatotev }
12462b5e00d4SBoyan Karatotev 
12472b5e00d4SBoyan Karatotev /*******************************************************************************
12482b5e00d4SBoyan Karatotev  * Finish a non-terminal power down sequence, ending with a wfi. In case of
12492b5e00d4SBoyan Karatotev  * wakeup will unwind any CPU specific actions and return.
12502b5e00d4SBoyan Karatotev  ******************************************************************************/
12512b5e00d4SBoyan Karatotev 
12522b5e00d4SBoyan Karatotev void psci_pwrdown_cpu_end_wakeup(unsigned int power_level)
12532b5e00d4SBoyan Karatotev {
12542b5e00d4SBoyan Karatotev 	/*
12552b5e00d4SBoyan Karatotev 	 * Usually, will be terminal. In some circumstances the powerdown will
12562b5e00d4SBoyan Karatotev 	 * be denied and we'll need to unwind
12572b5e00d4SBoyan Karatotev 	 */
12582b5e00d4SBoyan Karatotev 	psci_power_down_wfi();
12592b5e00d4SBoyan Karatotev 
12602b5e00d4SBoyan Karatotev 	/*
12612b5e00d4SBoyan Karatotev 	 * Waking up does not require hardware-assisted coherency, but that is
12622b5e00d4SBoyan Karatotev 	 * the case for every core that can wake up. Untangling the cache
12632b5e00d4SBoyan Karatotev 	 * coherency code from powerdown is a non-trivial effort which isn't
12642b5e00d4SBoyan Karatotev 	 * needed for our purposes.
12652b5e00d4SBoyan Karatotev 	 */
12662b5e00d4SBoyan Karatotev #if !FEAT_PABANDON
12672b5e00d4SBoyan Karatotev 	ERROR("Systems without FEAT_PABANDON shouldn't wake up.\n");
12682b5e00d4SBoyan Karatotev 	panic();
12692b5e00d4SBoyan Karatotev #else /* FEAT_PABANDON */
12702b5e00d4SBoyan Karatotev 
12712b5e00d4SBoyan Karatotev 	/*
12722b5e00d4SBoyan Karatotev 	 * Begin unwinding. Everything can be shared with CPU_ON and co later,
12732b5e00d4SBoyan Karatotev 	 * except the CPU specific bit. Cores that have hardware-assisted
12742b5e00d4SBoyan Karatotev 	 * coherency don't have much to do so just calling the hook again is
12752b5e00d4SBoyan Karatotev 	 * the simplest way to achieve this
12762b5e00d4SBoyan Karatotev 	 */
12772b5e00d4SBoyan Karatotev 	prepare_cpu_pwr_dwn(power_level);
12782b5e00d4SBoyan Karatotev #endif /* FEAT_PABANDON */
12792b5e00d4SBoyan Karatotev }
12802b5e00d4SBoyan Karatotev 
12812b5e00d4SBoyan Karatotev /*******************************************************************************
128222744909SSandeep Tripathy  * This function invokes the callback 'stop_func()' with the 'mpidr' of each
128322744909SSandeep Tripathy  * online PE. Caller can pass suitable method to stop a remote core.
128422744909SSandeep Tripathy  *
128522744909SSandeep Tripathy  * 'wait_ms' is the timeout value in milliseconds for the other cores to
128622744909SSandeep Tripathy  * transition to power down state. Passing '0' makes it non-blocking.
128722744909SSandeep Tripathy  *
128822744909SSandeep Tripathy  * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the
128922744909SSandeep Tripathy  * given timeout.
129022744909SSandeep Tripathy  ******************************************************************************/
12913b802105SBoyan Karatotev int psci_stop_other_cores(unsigned int this_cpu_idx, unsigned int wait_ms,
129222744909SSandeep Tripathy 				   void (*stop_func)(u_register_t mpidr))
129322744909SSandeep Tripathy {
129422744909SSandeep Tripathy 	/* Invoke stop_func for each core */
12953b802105SBoyan Karatotev 	for (unsigned int idx = 0U; idx < psci_plat_core_count; idx++) {
129622744909SSandeep Tripathy 		/* skip current CPU */
129722744909SSandeep Tripathy 		if (idx == this_cpu_idx) {
129822744909SSandeep Tripathy 			continue;
129922744909SSandeep Tripathy 		}
130022744909SSandeep Tripathy 
130122744909SSandeep Tripathy 		/* Check if the CPU is ON */
130222744909SSandeep Tripathy 		if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) {
130322744909SSandeep Tripathy 			(*stop_func)(psci_cpu_pd_nodes[idx].mpidr);
130422744909SSandeep Tripathy 		}
130522744909SSandeep Tripathy 	}
130622744909SSandeep Tripathy 
130722744909SSandeep Tripathy 	/* Need to wait for other cores to shutdown */
130822744909SSandeep Tripathy 	if (wait_ms != 0U) {
1309e64cdee4SMaheedhar Bollapalli 		for (uint32_t delay_ms = wait_ms; ((delay_ms != 0U) &&
1310e64cdee4SMaheedhar Bollapalli 					(!psci_is_last_on_cpu(this_cpu_idx))); delay_ms--) {
131122744909SSandeep Tripathy 			mdelay(1U);
131222744909SSandeep Tripathy 		}
131322744909SSandeep Tripathy 
13143b802105SBoyan Karatotev 		if (!psci_is_last_on_cpu(this_cpu_idx)) {
131522744909SSandeep Tripathy 			WARN("Failed to stop all cores!\n");
131622744909SSandeep Tripathy 			psci_print_power_domain_map();
131722744909SSandeep Tripathy 			return PSCI_E_DENIED;
131822744909SSandeep Tripathy 		}
131922744909SSandeep Tripathy 	}
132022744909SSandeep Tripathy 
132122744909SSandeep Tripathy 	return PSCI_E_SUCCESS;
132222744909SSandeep Tripathy }
1323ce14a12fSLucian Paul-Trifu 
1324ce14a12fSLucian Paul-Trifu /*******************************************************************************
1325ce14a12fSLucian Paul-Trifu  * This function verifies that all the other cores in the system have been
1326ce14a12fSLucian Paul-Trifu  * turned OFF and the current CPU is the last running CPU in the system.
1327ce14a12fSLucian Paul-Trifu  * Returns true if the current CPU is the last ON CPU or false otherwise.
1328ce14a12fSLucian Paul-Trifu  *
1329ce14a12fSLucian Paul-Trifu  * This API has following differences with psci_is_last_on_cpu
1330ce14a12fSLucian Paul-Trifu  *  1. PSCI states are locked
1331ce14a12fSLucian Paul-Trifu  ******************************************************************************/
13323b802105SBoyan Karatotev bool psci_is_last_on_cpu_safe(unsigned int this_core)
1333ce14a12fSLucian Paul-Trifu {
1334ce14a12fSLucian Paul-Trifu 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1335ce14a12fSLucian Paul-Trifu 
1336b41b0824SJayanth Dodderi Chidanand 	psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1337ce14a12fSLucian Paul-Trifu 
1338ce14a12fSLucian Paul-Trifu 	psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1339ce14a12fSLucian Paul-Trifu 
13403b802105SBoyan Karatotev 	if (!psci_is_last_on_cpu(this_core)) {
1341b41b0824SJayanth Dodderi Chidanand 		psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1342ce14a12fSLucian Paul-Trifu 		return false;
1343ce14a12fSLucian Paul-Trifu 	}
1344ce14a12fSLucian Paul-Trifu 
1345ce14a12fSLucian Paul-Trifu 	psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1346ce14a12fSLucian Paul-Trifu 
1347ce14a12fSLucian Paul-Trifu 	return true;
1348ce14a12fSLucian Paul-Trifu }
1349b88a4416SWing Li 
1350b88a4416SWing Li /*******************************************************************************
1351b88a4416SWing Li  * This function verifies that all cores in the system have been turned ON.
1352b88a4416SWing Li  * Returns true, if all CPUs are ON or false otherwise.
1353b88a4416SWing Li  *
1354b88a4416SWing Li  * This API has following differences with psci_are_all_cpus_on
1355b88a4416SWing Li  *  1. PSCI states are locked
1356b88a4416SWing Li  ******************************************************************************/
13573b802105SBoyan Karatotev bool psci_are_all_cpus_on_safe(unsigned int this_core)
1358b88a4416SWing Li {
1359b88a4416SWing Li 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1360b88a4416SWing Li 
1361b88a4416SWing Li 	psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1362b88a4416SWing Li 
1363b88a4416SWing Li 	psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1364b88a4416SWing Li 
1365b88a4416SWing Li 	if (!psci_are_all_cpus_on()) {
1366b88a4416SWing Li 		psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1367b88a4416SWing Li 		return false;
1368b88a4416SWing Li 	}
1369b88a4416SWing Li 
1370b88a4416SWing Li 	psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1371b88a4416SWing Li 
1372b88a4416SWing Li 	return true;
1373b88a4416SWing Li }
1374