xref: /rk3399_ARM-atf/lib/psci/psci_common.c (revision b41b082464a164f32a5f1397894619c6b0680ed3)
1532ed618SSoby Mathew /*
2b9338eeeSYann Gautier  * Copyright (c) 2013-2022, 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>
11532ed618SSoby Mathew #include <arch_helpers.h>
1209d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
14532ed618SSoby Mathew #include <context.h>
1522744909SSandeep Tripathy #include <drivers/delay_timer.h>
1609d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
1709d40e0eSAntonio Nino Diaz #include <lib/utils.h>
1809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
1909d40e0eSAntonio Nino Diaz 
20532ed618SSoby Mathew #include "psci_private.h"
21532ed618SSoby Mathew 
22532ed618SSoby Mathew /*
23532ed618SSoby Mathew  * SPD power management operations, expected to be supplied by the registered
24532ed618SSoby Mathew  * SPD on successful SP initialization
25532ed618SSoby Mathew  */
26532ed618SSoby Mathew const spd_pm_ops_t *psci_spd_pm;
27532ed618SSoby Mathew 
28532ed618SSoby Mathew /*
29532ed618SSoby Mathew  * PSCI requested local power state map. This array is used to store the local
30532ed618SSoby Mathew  * power states requested by a CPU for power levels from level 1 to
31532ed618SSoby Mathew  * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
32532ed618SSoby Mathew  * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
33532ed618SSoby Mathew  * CPU are the same.
34532ed618SSoby Mathew  *
35532ed618SSoby Mathew  * During state coordination, the platform is passed an array containing the
36532ed618SSoby Mathew  * local states requested for a particular non cpu power domain by each cpu
37532ed618SSoby Mathew  * within the domain.
38532ed618SSoby Mathew  *
39532ed618SSoby Mathew  * TODO: Dense packing of the requested states will cause cache thrashing
40532ed618SSoby Mathew  * when multiple power domains write to it. If we allocate the requested
41532ed618SSoby Mathew  * states at each power level in a cache-line aligned per-domain memory,
42532ed618SSoby Mathew  * the cache thrashing can be avoided.
43532ed618SSoby Mathew  */
44532ed618SSoby Mathew static plat_local_state_t
45532ed618SSoby Mathew 	psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
46532ed618SSoby Mathew 
47ab4df50cSPankaj Gupta unsigned int psci_plat_core_count;
48532ed618SSoby Mathew 
49532ed618SSoby Mathew /*******************************************************************************
50532ed618SSoby Mathew  * Arrays that hold the platform's power domain tree information for state
51532ed618SSoby Mathew  * management of power domains.
52532ed618SSoby Mathew  * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
53532ed618SSoby Mathew  * which is an ancestor of a CPU power domain.
54532ed618SSoby Mathew  * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
55532ed618SSoby Mathew  ******************************************************************************/
56532ed618SSoby Mathew non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
57532ed618SSoby Mathew #if USE_COHERENT_MEM
58532ed618SSoby Mathew __section("tzfw_coherent_mem")
59532ed618SSoby Mathew #endif
60532ed618SSoby Mathew ;
61532ed618SSoby Mathew 
62b0408e87SJeenu Viswambharan /* Lock for PSCI state coordination */
63b0408e87SJeenu Viswambharan DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
64532ed618SSoby Mathew 
65532ed618SSoby Mathew cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
66532ed618SSoby Mathew 
67532ed618SSoby Mathew /*******************************************************************************
68532ed618SSoby Mathew  * Pointer to functions exported by the platform to complete power mgmt. ops
69532ed618SSoby Mathew  ******************************************************************************/
70532ed618SSoby Mathew const plat_psci_ops_t *psci_plat_pm_ops;
71532ed618SSoby Mathew 
72532ed618SSoby Mathew /******************************************************************************
73532ed618SSoby Mathew  * Check that the maximum power level supported by the platform makes sense
74532ed618SSoby Mathew  *****************************************************************************/
756b7b0f36SAntonio Nino Diaz CASSERT((PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL) &&
766b7b0f36SAntonio Nino Diaz 	(PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL),
77532ed618SSoby Mathew 	assert_platform_max_pwrlvl_check);
78532ed618SSoby Mathew 
79532ed618SSoby Mathew /*
80532ed618SSoby Mathew  * The plat_local_state used by the platform is one of these types: RUN,
81532ed618SSoby Mathew  * RETENTION and OFF. The platform can define further sub-states for each type
82532ed618SSoby Mathew  * apart from RUN. This categorization is done to verify the sanity of the
83532ed618SSoby Mathew  * psci_power_state passed by the platform and to print debug information. The
84532ed618SSoby Mathew  * categorization is done on the basis of the following conditions:
85532ed618SSoby Mathew  *
86532ed618SSoby Mathew  * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
87532ed618SSoby Mathew  *
88532ed618SSoby Mathew  * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
89532ed618SSoby Mathew  *    STATE_TYPE_RETN.
90532ed618SSoby Mathew  *
91532ed618SSoby Mathew  * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
92532ed618SSoby Mathew  *    STATE_TYPE_OFF.
93532ed618SSoby Mathew  */
94532ed618SSoby Mathew typedef enum plat_local_state_type {
95532ed618SSoby Mathew 	STATE_TYPE_RUN = 0,
96532ed618SSoby Mathew 	STATE_TYPE_RETN,
97532ed618SSoby Mathew 	STATE_TYPE_OFF
98532ed618SSoby Mathew } plat_local_state_type_t;
99532ed618SSoby Mathew 
10097373c33SAntonio Nino Diaz /* Function used to categorize plat_local_state. */
10197373c33SAntonio Nino Diaz static plat_local_state_type_t find_local_state_type(plat_local_state_t state)
10297373c33SAntonio Nino Diaz {
10397373c33SAntonio Nino Diaz 	if (state != 0U) {
10497373c33SAntonio Nino Diaz 		if (state > PLAT_MAX_RET_STATE) {
10597373c33SAntonio Nino Diaz 			return STATE_TYPE_OFF;
10697373c33SAntonio Nino Diaz 		} else {
10797373c33SAntonio Nino Diaz 			return STATE_TYPE_RETN;
10897373c33SAntonio Nino Diaz 		}
10997373c33SAntonio Nino Diaz 	} else {
11097373c33SAntonio Nino Diaz 		return STATE_TYPE_RUN;
11197373c33SAntonio Nino Diaz 	}
11297373c33SAntonio Nino Diaz }
113532ed618SSoby Mathew 
114532ed618SSoby Mathew /******************************************************************************
115532ed618SSoby Mathew  * Check that the maximum retention level supported by the platform is less
116532ed618SSoby Mathew  * than the maximum off level.
117532ed618SSoby Mathew  *****************************************************************************/
1186b7b0f36SAntonio Nino Diaz CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE,
119532ed618SSoby Mathew 		assert_platform_max_off_and_retn_state_check);
120532ed618SSoby Mathew 
121532ed618SSoby Mathew /******************************************************************************
122532ed618SSoby Mathew  * This function ensures that the power state parameter in a CPU_SUSPEND request
123532ed618SSoby Mathew  * is valid. If so, it returns the requested states for each power level.
124532ed618SSoby Mathew  *****************************************************************************/
125532ed618SSoby Mathew int psci_validate_power_state(unsigned int power_state,
126532ed618SSoby Mathew 			      psci_power_state_t *state_info)
127532ed618SSoby Mathew {
128532ed618SSoby Mathew 	/* Check SBZ bits in power state are zero */
1296b7b0f36SAntonio Nino Diaz 	if (psci_check_power_state(power_state) != 0U)
130532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
131532ed618SSoby Mathew 
1326b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->validate_power_state != NULL);
133532ed618SSoby Mathew 
134532ed618SSoby Mathew 	/* Validate the power_state using platform pm_ops */
135532ed618SSoby Mathew 	return psci_plat_pm_ops->validate_power_state(power_state, state_info);
136532ed618SSoby Mathew }
137532ed618SSoby Mathew 
138532ed618SSoby Mathew /******************************************************************************
139532ed618SSoby Mathew  * This function retrieves the `psci_power_state_t` for system suspend from
140532ed618SSoby Mathew  * the platform.
141532ed618SSoby Mathew  *****************************************************************************/
142532ed618SSoby Mathew void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
143532ed618SSoby Mathew {
144532ed618SSoby Mathew 	/*
145532ed618SSoby Mathew 	 * Assert that the required pm_ops hook is implemented to ensure that
146532ed618SSoby Mathew 	 * the capability detected during psci_setup() is valid.
147532ed618SSoby Mathew 	 */
1486b7b0f36SAntonio Nino Diaz 	assert(psci_plat_pm_ops->get_sys_suspend_power_state != NULL);
149532ed618SSoby Mathew 
150532ed618SSoby Mathew 	/*
151532ed618SSoby Mathew 	 * Query the platform for the power_state required for system suspend
152532ed618SSoby Mathew 	 */
153532ed618SSoby Mathew 	psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
154532ed618SSoby Mathew }
155532ed618SSoby Mathew 
156532ed618SSoby Mathew /*******************************************************************************
157532ed618SSoby Mathew  * This function verifies that the all the other cores in the system have been
158532ed618SSoby Mathew  * turned OFF and the current CPU is the last running CPU in the system.
159*b41b0824SJayanth Dodderi Chidanand  * Returns true, if the current CPU is the last ON CPU or false otherwise.
160532ed618SSoby Mathew  ******************************************************************************/
161*b41b0824SJayanth Dodderi Chidanand bool psci_is_last_on_cpu(void)
162532ed618SSoby Mathew {
163fc81021aSDeepika Bhavnani 	unsigned int cpu_idx, my_idx = plat_my_core_pos();
164532ed618SSoby Mathew 
165*b41b0824SJayanth Dodderi Chidanand 	for (cpu_idx = 0; cpu_idx < psci_plat_core_count; cpu_idx++) {
166532ed618SSoby Mathew 		if (cpu_idx == my_idx) {
167532ed618SSoby Mathew 			assert(psci_get_aff_info_state() == AFF_STATE_ON);
168532ed618SSoby Mathew 			continue;
169532ed618SSoby Mathew 		}
170532ed618SSoby Mathew 
171*b41b0824SJayanth Dodderi Chidanand 		if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF) {
172*b41b0824SJayanth Dodderi Chidanand 			VERBOSE("core=%u other than current core=%u %s\n",
173*b41b0824SJayanth Dodderi Chidanand 				cpu_idx, my_idx, "running in the system");
174*b41b0824SJayanth Dodderi Chidanand 			return false;
175*b41b0824SJayanth Dodderi Chidanand 		}
176532ed618SSoby Mathew 	}
177532ed618SSoby Mathew 
178*b41b0824SJayanth Dodderi Chidanand 	return true;
179532ed618SSoby Mathew }
180532ed618SSoby Mathew 
181532ed618SSoby Mathew /*******************************************************************************
182532ed618SSoby Mathew  * Routine to return the maximum power level to traverse to after a cpu has
183532ed618SSoby Mathew  * been physically powered up. It is expected to be called immediately after
184532ed618SSoby Mathew  * reset from assembler code.
185532ed618SSoby Mathew  ******************************************************************************/
186532ed618SSoby Mathew static unsigned int get_power_on_target_pwrlvl(void)
187532ed618SSoby Mathew {
188532ed618SSoby Mathew 	unsigned int pwrlvl;
189532ed618SSoby Mathew 
190532ed618SSoby Mathew 	/*
191532ed618SSoby Mathew 	 * Assume that this cpu was suspended and retrieve its target power
192532ed618SSoby Mathew 	 * level. If it is invalid then it could only have been turned off
193532ed618SSoby Mathew 	 * earlier. PLAT_MAX_PWR_LVL will be the highest power level a
194532ed618SSoby Mathew 	 * cpu can be turned off to.
195532ed618SSoby Mathew 	 */
196532ed618SSoby Mathew 	pwrlvl = psci_get_suspend_pwrlvl();
197532ed618SSoby Mathew 	if (pwrlvl == PSCI_INVALID_PWR_LVL)
198532ed618SSoby Mathew 		pwrlvl = PLAT_MAX_PWR_LVL;
1990c411c78SDeepika Bhavnani 	assert(pwrlvl < PSCI_INVALID_PWR_LVL);
200532ed618SSoby Mathew 	return pwrlvl;
201532ed618SSoby Mathew }
202532ed618SSoby Mathew 
203532ed618SSoby Mathew /******************************************************************************
204532ed618SSoby Mathew  * Helper function to update the requested local power state array. This array
205532ed618SSoby Mathew  * does not store the requested state for the CPU power level. Hence an
20641af0515SDeepika Bhavnani  * assertion is added to prevent us from accessing the CPU power level.
207532ed618SSoby Mathew  *****************************************************************************/
208532ed618SSoby Mathew static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
209532ed618SSoby Mathew 					 unsigned int cpu_idx,
210532ed618SSoby Mathew 					 plat_local_state_t req_pwr_state)
211532ed618SSoby Mathew {
212532ed618SSoby Mathew 	assert(pwrlvl > PSCI_CPU_PWR_LVL);
21341af0515SDeepika Bhavnani 	if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
214ab4df50cSPankaj Gupta 			(cpu_idx < psci_plat_core_count)) {
2156b7b0f36SAntonio Nino Diaz 		psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx] = req_pwr_state;
21641af0515SDeepika Bhavnani 	}
217532ed618SSoby Mathew }
218532ed618SSoby Mathew 
219532ed618SSoby Mathew /******************************************************************************
220532ed618SSoby Mathew  * This function initializes the psci_req_local_pwr_states.
221532ed618SSoby Mathew  *****************************************************************************/
22287c85134SDaniel Boulby void __init psci_init_req_local_pwr_states(void)
223532ed618SSoby Mathew {
224532ed618SSoby Mathew 	/* Initialize the requested state of all non CPU power domains as OFF */
2256b7b0f36SAntonio Nino Diaz 	unsigned int pwrlvl;
226ab4df50cSPankaj Gupta 	unsigned int core;
2276b7b0f36SAntonio Nino Diaz 
2286b7b0f36SAntonio Nino Diaz 	for (pwrlvl = 0U; pwrlvl < PLAT_MAX_PWR_LVL; pwrlvl++) {
229ab4df50cSPankaj Gupta 		for (core = 0; core < psci_plat_core_count; core++) {
2306b7b0f36SAntonio Nino Diaz 			psci_req_local_pwr_states[pwrlvl][core] =
2316b7b0f36SAntonio Nino Diaz 				PLAT_MAX_OFF_STATE;
2326b7b0f36SAntonio Nino Diaz 		}
2336b7b0f36SAntonio Nino Diaz 	}
234532ed618SSoby Mathew }
235532ed618SSoby Mathew 
236532ed618SSoby Mathew /******************************************************************************
237532ed618SSoby Mathew  * Helper function to return a reference to an array containing the local power
238532ed618SSoby Mathew  * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
239532ed618SSoby Mathew  * array will be the number of cpu power domains of which this power domain is
240532ed618SSoby Mathew  * an ancestor. These requested states will be used to determine a suitable
241532ed618SSoby Mathew  * target state for this power domain during psci state coordination. An
242532ed618SSoby Mathew  * assertion is added to prevent us from accessing the CPU power level.
243532ed618SSoby Mathew  *****************************************************************************/
244532ed618SSoby Mathew static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
245fc81021aSDeepika Bhavnani 							 unsigned int cpu_idx)
246532ed618SSoby Mathew {
247532ed618SSoby Mathew 	assert(pwrlvl > PSCI_CPU_PWR_LVL);
248532ed618SSoby Mathew 
24941af0515SDeepika Bhavnani 	if ((pwrlvl > PSCI_CPU_PWR_LVL) && (pwrlvl <= PLAT_MAX_PWR_LVL) &&
250ab4df50cSPankaj Gupta 			(cpu_idx < psci_plat_core_count)) {
2516b7b0f36SAntonio Nino Diaz 		return &psci_req_local_pwr_states[pwrlvl - 1U][cpu_idx];
25241af0515SDeepika Bhavnani 	} else
25341af0515SDeepika Bhavnani 		return NULL;
254532ed618SSoby Mathew }
255532ed618SSoby Mathew 
256a10d3632SJeenu Viswambharan /*
257a10d3632SJeenu Viswambharan  * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
258a10d3632SJeenu Viswambharan  * memory.
259a10d3632SJeenu Viswambharan  *
260a10d3632SJeenu Viswambharan  * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
261a10d3632SJeenu Viswambharan  * it's accessed by both cached and non-cached participants. To serve the common
262a10d3632SJeenu Viswambharan  * minimum, perform a cache flush before read and after write so that non-cached
263a10d3632SJeenu Viswambharan  * participants operate on latest data in main memory.
264a10d3632SJeenu Viswambharan  *
265a10d3632SJeenu Viswambharan  * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
266a10d3632SJeenu Viswambharan  * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
267a10d3632SJeenu Viswambharan  * In both cases, no cache operations are required.
268a10d3632SJeenu Viswambharan  */
269a10d3632SJeenu Viswambharan 
270a10d3632SJeenu Viswambharan /*
271a10d3632SJeenu Viswambharan  * Retrieve local state of non-CPU power domain node from a non-cached CPU,
272a10d3632SJeenu Viswambharan  * after any required cache maintenance operation.
273a10d3632SJeenu Viswambharan  */
274a10d3632SJeenu Viswambharan static plat_local_state_t get_non_cpu_pd_node_local_state(
275a10d3632SJeenu Viswambharan 		unsigned int parent_idx)
276a10d3632SJeenu Viswambharan {
277f996a5f7SAndrew F. Davis #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
278a10d3632SJeenu Viswambharan 	flush_dcache_range(
279a10d3632SJeenu Viswambharan 			(uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
280a10d3632SJeenu Viswambharan 			sizeof(psci_non_cpu_pd_nodes[parent_idx]));
281a10d3632SJeenu Viswambharan #endif
282a10d3632SJeenu Viswambharan 	return psci_non_cpu_pd_nodes[parent_idx].local_state;
283a10d3632SJeenu Viswambharan }
284a10d3632SJeenu Viswambharan 
285a10d3632SJeenu Viswambharan /*
286a10d3632SJeenu Viswambharan  * Update local state of non-CPU power domain node from a cached CPU; perform
287a10d3632SJeenu Viswambharan  * any required cache maintenance operation afterwards.
288a10d3632SJeenu Viswambharan  */
289a10d3632SJeenu Viswambharan static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
290a10d3632SJeenu Viswambharan 		plat_local_state_t state)
291a10d3632SJeenu Viswambharan {
292a10d3632SJeenu Viswambharan 	psci_non_cpu_pd_nodes[parent_idx].local_state = state;
293f996a5f7SAndrew F. Davis #if !(USE_COHERENT_MEM || HW_ASSISTED_COHERENCY || WARMBOOT_ENABLE_DCACHE_EARLY)
294a10d3632SJeenu Viswambharan 	flush_dcache_range(
295a10d3632SJeenu Viswambharan 			(uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
296a10d3632SJeenu Viswambharan 			sizeof(psci_non_cpu_pd_nodes[parent_idx]));
297a10d3632SJeenu Viswambharan #endif
298a10d3632SJeenu Viswambharan }
299a10d3632SJeenu Viswambharan 
300532ed618SSoby Mathew /******************************************************************************
301532ed618SSoby Mathew  * Helper function to return the current local power state of each power domain
302532ed618SSoby Mathew  * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
303532ed618SSoby Mathew  * function will be called after a cpu is powered on to find the local state
304532ed618SSoby Mathew  * each power domain has emerged from.
305532ed618SSoby Mathew  *****************************************************************************/
30661eae524SAchin Gupta void psci_get_target_local_pwr_states(unsigned int end_pwrlvl,
307532ed618SSoby Mathew 				      psci_power_state_t *target_state)
308532ed618SSoby Mathew {
309532ed618SSoby Mathew 	unsigned int parent_idx, lvl;
310532ed618SSoby Mathew 	plat_local_state_t *pd_state = target_state->pwr_domain_state;
311532ed618SSoby Mathew 
312532ed618SSoby Mathew 	pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
313532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
314532ed618SSoby Mathew 
315532ed618SSoby Mathew 	/* Copy the local power state from node to state_info */
3166b7b0f36SAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
317a10d3632SJeenu Viswambharan 		pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
318532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
319532ed618SSoby Mathew 	}
320532ed618SSoby Mathew 
321532ed618SSoby Mathew 	/* Set the the higher levels to RUN */
322532ed618SSoby Mathew 	for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
323532ed618SSoby Mathew 		target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
324532ed618SSoby Mathew }
325532ed618SSoby Mathew 
326532ed618SSoby Mathew /******************************************************************************
327532ed618SSoby Mathew  * Helper function to set the target local power state that each power domain
328532ed618SSoby Mathew  * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
329532ed618SSoby Mathew  * enter. This function will be called after coordination of requested power
330532ed618SSoby Mathew  * states has been done for each power level.
331532ed618SSoby Mathew  *****************************************************************************/
332532ed618SSoby Mathew static void psci_set_target_local_pwr_states(unsigned int end_pwrlvl,
333532ed618SSoby Mathew 					const psci_power_state_t *target_state)
334532ed618SSoby Mathew {
335532ed618SSoby Mathew 	unsigned int parent_idx, lvl;
336532ed618SSoby Mathew 	const plat_local_state_t *pd_state = target_state->pwr_domain_state;
337532ed618SSoby Mathew 
338532ed618SSoby Mathew 	psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
339532ed618SSoby Mathew 
340532ed618SSoby Mathew 	/*
341a10d3632SJeenu Viswambharan 	 * Need to flush as local_state might be accessed with Data Cache
342532ed618SSoby Mathew 	 * disabled during power on
343532ed618SSoby Mathew 	 */
344a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data.local_state);
345532ed618SSoby Mathew 
346532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
347532ed618SSoby Mathew 
348532ed618SSoby Mathew 	/* Copy the local_state from state_info */
3496b7b0f36SAntonio Nino Diaz 	for (lvl = 1U; lvl <= end_pwrlvl; lvl++) {
350a10d3632SJeenu Viswambharan 		set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
351532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
352532ed618SSoby Mathew 	}
353532ed618SSoby Mathew }
354532ed618SSoby Mathew 
355532ed618SSoby Mathew 
356532ed618SSoby Mathew /*******************************************************************************
357532ed618SSoby Mathew  * PSCI helper function to get the parent nodes corresponding to a cpu_index.
358532ed618SSoby Mathew  ******************************************************************************/
359fc81021aSDeepika Bhavnani void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
360532ed618SSoby Mathew 				      unsigned int end_lvl,
3616b7b0f36SAntonio Nino Diaz 				      unsigned int *node_index)
362532ed618SSoby Mathew {
363532ed618SSoby Mathew 	unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node;
3646311f63dSVarun Wadekar 	unsigned int i;
3656b7b0f36SAntonio Nino Diaz 	unsigned int *node = node_index;
366532ed618SSoby Mathew 
3676b7b0f36SAntonio Nino Diaz 	for (i = PSCI_CPU_PWR_LVL + 1U; i <= end_lvl; i++) {
3686b7b0f36SAntonio Nino Diaz 		*node = parent_node;
3696b7b0f36SAntonio Nino Diaz 		node++;
370532ed618SSoby Mathew 		parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
371532ed618SSoby Mathew 	}
372532ed618SSoby Mathew }
373532ed618SSoby Mathew 
374532ed618SSoby Mathew /******************************************************************************
375532ed618SSoby Mathew  * This function is invoked post CPU power up and initialization. It sets the
376532ed618SSoby Mathew  * affinity info state, target power state and requested power state for the
377532ed618SSoby Mathew  * current CPU and all its ancestor power domains to RUN.
378532ed618SSoby Mathew  *****************************************************************************/
379532ed618SSoby Mathew void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl)
380532ed618SSoby Mathew {
381532ed618SSoby Mathew 	unsigned int parent_idx, cpu_idx = plat_my_core_pos(), lvl;
382532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
383532ed618SSoby Mathew 
384532ed618SSoby Mathew 	/* Reset the local_state to RUN for the non cpu power domains. */
3856b7b0f36SAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
386a10d3632SJeenu Viswambharan 		set_non_cpu_pd_node_local_state(parent_idx,
387a10d3632SJeenu Viswambharan 				PSCI_LOCAL_STATE_RUN);
388532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl,
389532ed618SSoby Mathew 					     cpu_idx,
390532ed618SSoby Mathew 					     PSCI_LOCAL_STATE_RUN);
391532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
392532ed618SSoby Mathew 	}
393532ed618SSoby Mathew 
394532ed618SSoby Mathew 	/* Set the affinity info state to ON */
395532ed618SSoby Mathew 	psci_set_aff_info_state(AFF_STATE_ON);
396532ed618SSoby Mathew 
397532ed618SSoby Mathew 	psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
398a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data);
399532ed618SSoby Mathew }
400532ed618SSoby Mathew 
401532ed618SSoby Mathew /******************************************************************************
402532ed618SSoby Mathew  * This function is passed the local power states requested for each power
403532ed618SSoby Mathew  * domain (state_info) between the current CPU domain and its ancestors until
404532ed618SSoby Mathew  * the target power level (end_pwrlvl). It updates the array of requested power
405532ed618SSoby Mathew  * states with this information.
406532ed618SSoby Mathew  *
407532ed618SSoby Mathew  * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
408532ed618SSoby Mathew  * retrieves the states requested by all the cpus of which the power domain at
409532ed618SSoby Mathew  * that level is an ancestor. It passes this information to the platform to
410532ed618SSoby Mathew  * coordinate and return the target power state. If the target state for a level
411532ed618SSoby Mathew  * is RUN then subsequent levels are not considered. At the CPU level, state
412532ed618SSoby Mathew  * coordination is not required. Hence, the requested and the target states are
413532ed618SSoby Mathew  * the same.
414532ed618SSoby Mathew  *
415532ed618SSoby Mathew  * The 'state_info' is updated with the target state for each level between the
416532ed618SSoby Mathew  * CPU and the 'end_pwrlvl' and returned to the caller.
417532ed618SSoby Mathew  *
418532ed618SSoby Mathew  * This function will only be invoked with data cache enabled and while
419532ed618SSoby Mathew  * powering down a core.
420532ed618SSoby Mathew  *****************************************************************************/
421532ed618SSoby Mathew void psci_do_state_coordination(unsigned int end_pwrlvl,
422532ed618SSoby Mathew 				psci_power_state_t *state_info)
423532ed618SSoby Mathew {
424532ed618SSoby Mathew 	unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos();
425fc81021aSDeepika Bhavnani 	unsigned int start_idx;
4266b7b0f36SAntonio Nino Diaz 	unsigned int ncpus;
427532ed618SSoby Mathew 	plat_local_state_t target_state, *req_states;
428532ed618SSoby Mathew 
429532ed618SSoby Mathew 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
430532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
431532ed618SSoby Mathew 
432532ed618SSoby Mathew 	/* For level 0, the requested state will be equivalent
433532ed618SSoby Mathew 	   to target state */
4346b7b0f36SAntonio Nino Diaz 	for (lvl = PSCI_CPU_PWR_LVL + 1U; lvl <= end_pwrlvl; lvl++) {
435532ed618SSoby Mathew 
436532ed618SSoby Mathew 		/* First update the requested power state */
437532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl, cpu_idx,
438532ed618SSoby Mathew 					     state_info->pwr_domain_state[lvl]);
439532ed618SSoby Mathew 
440532ed618SSoby Mathew 		/* Get the requested power states for this power level */
441532ed618SSoby Mathew 		start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
442532ed618SSoby Mathew 		req_states = psci_get_req_local_pwr_states(lvl, start_idx);
443532ed618SSoby Mathew 
444532ed618SSoby Mathew 		/*
445532ed618SSoby Mathew 		 * Let the platform coordinate amongst the requested states at
446532ed618SSoby Mathew 		 * this power level and return the target local power state.
447532ed618SSoby Mathew 		 */
448532ed618SSoby Mathew 		ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
449532ed618SSoby Mathew 		target_state = plat_get_target_pwr_state(lvl,
450532ed618SSoby Mathew 							 req_states,
451532ed618SSoby Mathew 							 ncpus);
452532ed618SSoby Mathew 
453532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = target_state;
454532ed618SSoby Mathew 
455532ed618SSoby Mathew 		/* Break early if the negotiated target power state is RUN */
4566b7b0f36SAntonio Nino Diaz 		if (is_local_state_run(state_info->pwr_domain_state[lvl]) != 0)
457532ed618SSoby Mathew 			break;
458532ed618SSoby Mathew 
459532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
460532ed618SSoby Mathew 	}
461532ed618SSoby Mathew 
462532ed618SSoby Mathew 	/*
463532ed618SSoby Mathew 	 * This is for cases when we break out of the above loop early because
464532ed618SSoby Mathew 	 * the target power state is RUN at a power level < end_pwlvl.
465532ed618SSoby Mathew 	 * We update the requested power state from state_info and then
466532ed618SSoby Mathew 	 * set the target state as RUN.
467532ed618SSoby Mathew 	 */
4686b7b0f36SAntonio Nino Diaz 	for (lvl = lvl + 1U; lvl <= end_pwrlvl; lvl++) {
469532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl, cpu_idx,
470532ed618SSoby Mathew 					     state_info->pwr_domain_state[lvl]);
471532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
472532ed618SSoby Mathew 
473532ed618SSoby Mathew 	}
474532ed618SSoby Mathew 
475532ed618SSoby Mathew 	/* Update the target state in the power domain nodes */
476532ed618SSoby Mathew 	psci_set_target_local_pwr_states(end_pwrlvl, state_info);
477532ed618SSoby Mathew }
478532ed618SSoby Mathew 
479532ed618SSoby Mathew /******************************************************************************
480532ed618SSoby Mathew  * This function validates a suspend request by making sure that if a standby
481532ed618SSoby Mathew  * state is requested then no power level is turned off and the highest power
482532ed618SSoby Mathew  * level is placed in a standby/retention state.
483532ed618SSoby Mathew  *
484532ed618SSoby Mathew  * It also ensures that the state level X will enter is not shallower than the
485532ed618SSoby Mathew  * state level X + 1 will enter.
486532ed618SSoby Mathew  *
487532ed618SSoby Mathew  * This validation will be enabled only for DEBUG builds as the platform is
488532ed618SSoby Mathew  * expected to perform these validations as well.
489532ed618SSoby Mathew  *****************************************************************************/
490532ed618SSoby Mathew int psci_validate_suspend_req(const psci_power_state_t *state_info,
491532ed618SSoby Mathew 			      unsigned int is_power_down_state)
492532ed618SSoby Mathew {
493532ed618SSoby Mathew 	unsigned int max_off_lvl, target_lvl, max_retn_lvl;
494532ed618SSoby Mathew 	plat_local_state_t state;
495532ed618SSoby Mathew 	plat_local_state_type_t req_state_type, deepest_state_type;
496532ed618SSoby Mathew 	int i;
497532ed618SSoby Mathew 
498532ed618SSoby Mathew 	/* Find the target suspend power level */
499532ed618SSoby Mathew 	target_lvl = psci_find_target_suspend_lvl(state_info);
500532ed618SSoby Mathew 	if (target_lvl == PSCI_INVALID_PWR_LVL)
501532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
502532ed618SSoby Mathew 
503532ed618SSoby Mathew 	/* All power domain levels are in a RUN state to begin with */
504532ed618SSoby Mathew 	deepest_state_type = STATE_TYPE_RUN;
505532ed618SSoby Mathew 
5066b7b0f36SAntonio Nino Diaz 	for (i = (int) target_lvl; i >= (int) PSCI_CPU_PWR_LVL; i--) {
507532ed618SSoby Mathew 		state = state_info->pwr_domain_state[i];
508532ed618SSoby Mathew 		req_state_type = find_local_state_type(state);
509532ed618SSoby Mathew 
510532ed618SSoby Mathew 		/*
511532ed618SSoby Mathew 		 * While traversing from the highest power level to the lowest,
512532ed618SSoby Mathew 		 * the state requested for lower levels has to be the same or
513532ed618SSoby Mathew 		 * deeper i.e. equal to or greater than the state at the higher
514532ed618SSoby Mathew 		 * levels. If this condition is true, then the requested state
515532ed618SSoby Mathew 		 * becomes the deepest state encountered so far.
516532ed618SSoby Mathew 		 */
517532ed618SSoby Mathew 		if (req_state_type < deepest_state_type)
518532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
519532ed618SSoby Mathew 		deepest_state_type = req_state_type;
520532ed618SSoby Mathew 	}
521532ed618SSoby Mathew 
522532ed618SSoby Mathew 	/* Find the highest off power level */
523532ed618SSoby Mathew 	max_off_lvl = psci_find_max_off_lvl(state_info);
524532ed618SSoby Mathew 
525532ed618SSoby Mathew 	/* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
526532ed618SSoby Mathew 	max_retn_lvl = PSCI_INVALID_PWR_LVL;
527532ed618SSoby Mathew 	if (target_lvl != max_off_lvl)
528532ed618SSoby Mathew 		max_retn_lvl = target_lvl;
529532ed618SSoby Mathew 
530532ed618SSoby Mathew 	/*
531532ed618SSoby Mathew 	 * If this is not a request for a power down state then max off level
532532ed618SSoby Mathew 	 * has to be invalid and max retention level has to be a valid power
533532ed618SSoby Mathew 	 * level.
534532ed618SSoby Mathew 	 */
5356b7b0f36SAntonio Nino Diaz 	if ((is_power_down_state == 0U) &&
5366b7b0f36SAntonio Nino Diaz 			((max_off_lvl != PSCI_INVALID_PWR_LVL) ||
5376b7b0f36SAntonio Nino Diaz 			 (max_retn_lvl == PSCI_INVALID_PWR_LVL)))
538532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
539532ed618SSoby Mathew 
540532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
541532ed618SSoby Mathew }
542532ed618SSoby Mathew 
543532ed618SSoby Mathew /******************************************************************************
544532ed618SSoby Mathew  * This function finds the highest power level which will be powered down
545532ed618SSoby Mathew  * amongst all the power levels specified in the 'state_info' structure
546532ed618SSoby Mathew  *****************************************************************************/
547532ed618SSoby Mathew unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
548532ed618SSoby Mathew {
549532ed618SSoby Mathew 	int i;
550532ed618SSoby Mathew 
5516b7b0f36SAntonio Nino Diaz 	for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
5526b7b0f36SAntonio Nino Diaz 		if (is_local_state_off(state_info->pwr_domain_state[i]) != 0)
5536b7b0f36SAntonio Nino Diaz 			return (unsigned int) i;
554532ed618SSoby Mathew 	}
555532ed618SSoby Mathew 
556532ed618SSoby Mathew 	return PSCI_INVALID_PWR_LVL;
557532ed618SSoby Mathew }
558532ed618SSoby Mathew 
559532ed618SSoby Mathew /******************************************************************************
560532ed618SSoby Mathew  * This functions finds the level of the highest power domain which will be
561532ed618SSoby Mathew  * placed in a low power state during a suspend operation.
562532ed618SSoby Mathew  *****************************************************************************/
563532ed618SSoby Mathew unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
564532ed618SSoby Mathew {
565532ed618SSoby Mathew 	int i;
566532ed618SSoby Mathew 
5676b7b0f36SAntonio Nino Diaz 	for (i = (int) PLAT_MAX_PWR_LVL; i >= (int) PSCI_CPU_PWR_LVL; i--) {
5686b7b0f36SAntonio Nino Diaz 		if (is_local_state_run(state_info->pwr_domain_state[i]) == 0)
5696b7b0f36SAntonio Nino Diaz 			return (unsigned int) i;
570532ed618SSoby Mathew 	}
571532ed618SSoby Mathew 
572532ed618SSoby Mathew 	return PSCI_INVALID_PWR_LVL;
573532ed618SSoby Mathew }
574532ed618SSoby Mathew 
575532ed618SSoby Mathew /*******************************************************************************
57674d27d00SAndrew F. Davis  * This function is passed the highest level in the topology tree that the
57774d27d00SAndrew F. Davis  * operation should be applied to and a list of node indexes. It picks up locks
57874d27d00SAndrew F. Davis  * from the node index list in order of increasing power domain level in the
57974d27d00SAndrew F. Davis  * range specified.
580532ed618SSoby Mathew  ******************************************************************************/
58174d27d00SAndrew F. Davis void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
58274d27d00SAndrew F. Davis 				   const unsigned int *parent_nodes)
583532ed618SSoby Mathew {
58474d27d00SAndrew F. Davis 	unsigned int parent_idx;
585532ed618SSoby Mathew 	unsigned int level;
586532ed618SSoby Mathew 
587532ed618SSoby Mathew 	/* No locking required for level 0. Hence start locking from level 1 */
5886b7b0f36SAntonio Nino Diaz 	for (level = PSCI_CPU_PWR_LVL + 1U; level <= end_pwrlvl; level++) {
58974d27d00SAndrew F. Davis 		parent_idx = parent_nodes[level - 1U];
590532ed618SSoby Mathew 		psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
591532ed618SSoby Mathew 	}
592532ed618SSoby Mathew }
593532ed618SSoby Mathew 
594532ed618SSoby Mathew /*******************************************************************************
59574d27d00SAndrew F. Davis  * This function is passed the highest level in the topology tree that the
59674d27d00SAndrew F. Davis  * operation should be applied to and a list of node indexes. It releases the
59774d27d00SAndrew F. Davis  * locks in order of decreasing power domain level in the range specified.
598532ed618SSoby Mathew  ******************************************************************************/
59974d27d00SAndrew F. Davis void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
60074d27d00SAndrew F. Davis 				   const unsigned int *parent_nodes)
601532ed618SSoby Mathew {
60274d27d00SAndrew F. Davis 	unsigned int parent_idx;
6036b7b0f36SAntonio Nino Diaz 	unsigned int level;
604532ed618SSoby Mathew 
605532ed618SSoby Mathew 	/* Unlock top down. No unlocking required for level 0. */
6062fe75a2dSZelalem 	for (level = end_pwrlvl; level >= (PSCI_CPU_PWR_LVL + 1U); level--) {
6076b7b0f36SAntonio Nino Diaz 		parent_idx = parent_nodes[level - 1U];
608532ed618SSoby Mathew 		psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
609532ed618SSoby Mathew 	}
610532ed618SSoby Mathew }
611532ed618SSoby Mathew 
612532ed618SSoby Mathew /*******************************************************************************
613532ed618SSoby Mathew  * Simple routine to determine whether a mpidr is valid or not.
614532ed618SSoby Mathew  ******************************************************************************/
615532ed618SSoby Mathew int psci_validate_mpidr(u_register_t mpidr)
616532ed618SSoby Mathew {
617532ed618SSoby Mathew 	if (plat_core_pos_by_mpidr(mpidr) < 0)
618532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
619532ed618SSoby Mathew 
620532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
621532ed618SSoby Mathew }
622532ed618SSoby Mathew 
623532ed618SSoby Mathew /*******************************************************************************
624532ed618SSoby Mathew  * This function determines the full entrypoint information for the requested
625532ed618SSoby Mathew  * PSCI entrypoint on power on/resume and returns it.
626532ed618SSoby Mathew  ******************************************************************************/
627402b3cf8SJulius Werner #ifdef __aarch64__
628532ed618SSoby Mathew static int psci_get_ns_ep_info(entry_point_info_t *ep,
629532ed618SSoby Mathew 			       uintptr_t entrypoint,
630532ed618SSoby Mathew 			       u_register_t context_id)
631532ed618SSoby Mathew {
632532ed618SSoby Mathew 	u_register_t ep_attr, sctlr;
633532ed618SSoby Mathew 	unsigned int daif, ee, mode;
634532ed618SSoby Mathew 	u_register_t ns_scr_el3 = read_scr_el3();
635532ed618SSoby Mathew 	u_register_t ns_sctlr_el1 = read_sctlr_el1();
636532ed618SSoby Mathew 
6376b7b0f36SAntonio Nino Diaz 	sctlr = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
6386b7b0f36SAntonio Nino Diaz 		read_sctlr_el2() : ns_sctlr_el1;
639532ed618SSoby Mathew 	ee = 0;
640532ed618SSoby Mathew 
641532ed618SSoby Mathew 	ep_attr = NON_SECURE | EP_ST_DISABLE;
6426b7b0f36SAntonio Nino Diaz 	if ((sctlr & SCTLR_EE_BIT) != 0U) {
643532ed618SSoby Mathew 		ep_attr |= EP_EE_BIG;
644532ed618SSoby Mathew 		ee = 1;
645532ed618SSoby Mathew 	}
646532ed618SSoby Mathew 	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
647532ed618SSoby Mathew 
648532ed618SSoby Mathew 	ep->pc = entrypoint;
64932f0d3c6SDouglas Raillard 	zeromem(&ep->args, sizeof(ep->args));
650532ed618SSoby Mathew 	ep->args.arg0 = context_id;
651532ed618SSoby Mathew 
652532ed618SSoby Mathew 	/*
653532ed618SSoby Mathew 	 * Figure out whether the cpu enters the non-secure address space
654532ed618SSoby Mathew 	 * in aarch32 or aarch64
655532ed618SSoby Mathew 	 */
6566b7b0f36SAntonio Nino Diaz 	if ((ns_scr_el3 & SCR_RW_BIT) != 0U) {
657532ed618SSoby Mathew 
658532ed618SSoby Mathew 		/*
659532ed618SSoby Mathew 		 * Check whether a Thumb entry point has been provided for an
660532ed618SSoby Mathew 		 * aarch64 EL
661532ed618SSoby Mathew 		 */
6626b7b0f36SAntonio Nino Diaz 		if ((entrypoint & 0x1UL) != 0UL)
663532ed618SSoby Mathew 			return PSCI_E_INVALID_ADDRESS;
664532ed618SSoby Mathew 
6656b7b0f36SAntonio Nino Diaz 		mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ? MODE_EL2 : MODE_EL1;
666532ed618SSoby Mathew 
667d7b5f408SJimmy Brisson 		ep->spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX,
668d7b5f408SJimmy Brisson 				   DISABLE_ALL_EXCEPTIONS);
669532ed618SSoby Mathew 	} else {
670532ed618SSoby Mathew 
6716b7b0f36SAntonio Nino Diaz 		mode = ((ns_scr_el3 & SCR_HCE_BIT) != 0U) ?
6726b7b0f36SAntonio Nino Diaz 			MODE32_hyp : MODE32_svc;
673532ed618SSoby Mathew 
674532ed618SSoby Mathew 		/*
675532ed618SSoby Mathew 		 * TODO: Choose async. exception bits if HYP mode is not
676532ed618SSoby Mathew 		 * implemented according to the values of SCR.{AW, FW} bits
677532ed618SSoby Mathew 		 */
678532ed618SSoby Mathew 		daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
679532ed618SSoby Mathew 
680d7b5f408SJimmy Brisson 		ep->spsr = SPSR_MODE32((uint64_t)mode, entrypoint & 0x1, ee,
681d7b5f408SJimmy Brisson 				       daif);
682532ed618SSoby Mathew 	}
683532ed618SSoby Mathew 
684532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
685532ed618SSoby Mathew }
686402b3cf8SJulius Werner #else /* !__aarch64__ */
687402b3cf8SJulius Werner static int psci_get_ns_ep_info(entry_point_info_t *ep,
688402b3cf8SJulius Werner 			       uintptr_t entrypoint,
689402b3cf8SJulius Werner 			       u_register_t context_id)
690402b3cf8SJulius Werner {
691402b3cf8SJulius Werner 	u_register_t ep_attr;
692402b3cf8SJulius Werner 	unsigned int aif, ee, mode;
693402b3cf8SJulius Werner 	u_register_t scr = read_scr();
694402b3cf8SJulius Werner 	u_register_t ns_sctlr, sctlr;
695402b3cf8SJulius Werner 
696402b3cf8SJulius Werner 	/* Switch to non secure state */
697402b3cf8SJulius Werner 	write_scr(scr | SCR_NS_BIT);
698402b3cf8SJulius Werner 	isb();
699402b3cf8SJulius Werner 	ns_sctlr = read_sctlr();
700402b3cf8SJulius Werner 
701402b3cf8SJulius Werner 	sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
702402b3cf8SJulius Werner 
703402b3cf8SJulius Werner 	/* Return to original state */
704402b3cf8SJulius Werner 	write_scr(scr);
705402b3cf8SJulius Werner 	isb();
706402b3cf8SJulius Werner 	ee = 0;
707402b3cf8SJulius Werner 
708402b3cf8SJulius Werner 	ep_attr = NON_SECURE | EP_ST_DISABLE;
709402b3cf8SJulius Werner 	if (sctlr & SCTLR_EE_BIT) {
710402b3cf8SJulius Werner 		ep_attr |= EP_EE_BIG;
711402b3cf8SJulius Werner 		ee = 1;
712402b3cf8SJulius Werner 	}
713402b3cf8SJulius Werner 	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
714402b3cf8SJulius Werner 
715402b3cf8SJulius Werner 	ep->pc = entrypoint;
716402b3cf8SJulius Werner 	zeromem(&ep->args, sizeof(ep->args));
717402b3cf8SJulius Werner 	ep->args.arg0 = context_id;
718402b3cf8SJulius Werner 
719402b3cf8SJulius Werner 	mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
720402b3cf8SJulius Werner 
721402b3cf8SJulius Werner 	/*
722402b3cf8SJulius Werner 	 * TODO: Choose async. exception bits if HYP mode is not
723402b3cf8SJulius Werner 	 * implemented according to the values of SCR.{AW, FW} bits
724402b3cf8SJulius Werner 	 */
725402b3cf8SJulius Werner 	aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
726402b3cf8SJulius Werner 
727402b3cf8SJulius Werner 	ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
728402b3cf8SJulius Werner 
729402b3cf8SJulius Werner 	return PSCI_E_SUCCESS;
730402b3cf8SJulius Werner }
731402b3cf8SJulius Werner 
732402b3cf8SJulius Werner #endif /* __aarch64__ */
733532ed618SSoby Mathew 
734532ed618SSoby Mathew /*******************************************************************************
735532ed618SSoby Mathew  * This function validates the entrypoint with the platform layer if the
736532ed618SSoby Mathew  * appropriate pm_ops hook is exported by the platform and returns the
737532ed618SSoby Mathew  * 'entry_point_info'.
738532ed618SSoby Mathew  ******************************************************************************/
739532ed618SSoby Mathew int psci_validate_entry_point(entry_point_info_t *ep,
740532ed618SSoby Mathew 			      uintptr_t entrypoint,
741532ed618SSoby Mathew 			      u_register_t context_id)
742532ed618SSoby Mathew {
743532ed618SSoby Mathew 	int rc;
744532ed618SSoby Mathew 
745532ed618SSoby Mathew 	/* Validate the entrypoint using platform psci_ops */
7466b7b0f36SAntonio Nino Diaz 	if (psci_plat_pm_ops->validate_ns_entrypoint != NULL) {
747532ed618SSoby Mathew 		rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
748532ed618SSoby Mathew 		if (rc != PSCI_E_SUCCESS)
749532ed618SSoby Mathew 			return PSCI_E_INVALID_ADDRESS;
750532ed618SSoby Mathew 	}
751532ed618SSoby Mathew 
752532ed618SSoby Mathew 	/*
753532ed618SSoby Mathew 	 * Verify and derive the re-entry information for
754532ed618SSoby Mathew 	 * the non-secure world from the non-secure state from
755532ed618SSoby Mathew 	 * where this call originated.
756532ed618SSoby Mathew 	 */
757532ed618SSoby Mathew 	rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
758532ed618SSoby Mathew 	return rc;
759532ed618SSoby Mathew }
760532ed618SSoby Mathew 
761532ed618SSoby Mathew /*******************************************************************************
762532ed618SSoby Mathew  * Generic handler which is called when a cpu is physically powered on. It
763532ed618SSoby Mathew  * traverses the node information and finds the highest power level powered
764532ed618SSoby Mathew  * off and performs generic, architectural, platform setup and state management
765532ed618SSoby Mathew  * to power on that power level and power levels below it.
766532ed618SSoby Mathew  * e.g. For a cpu that's been powered on, it will call the platform specific
767532ed618SSoby Mathew  * code to enable the gic cpu interface and for a cluster it will enable
768532ed618SSoby Mathew  * coherency at the interconnect level in addition to gic cpu interface.
769532ed618SSoby Mathew  ******************************************************************************/
770cf0b1492SSoby Mathew void psci_warmboot_entrypoint(void)
771532ed618SSoby Mathew {
7726b7b0f36SAntonio Nino Diaz 	unsigned int end_pwrlvl;
773fc81021aSDeepika Bhavnani 	unsigned int cpu_idx = plat_my_core_pos();
77474d27d00SAndrew F. Davis 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
775532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
776532ed618SSoby Mathew 
777532ed618SSoby Mathew 	/*
778532ed618SSoby Mathew 	 * Verify that we have been explicitly turned ON or resumed from
779532ed618SSoby Mathew 	 * suspend.
780532ed618SSoby Mathew 	 */
781532ed618SSoby Mathew 	if (psci_get_aff_info_state() == AFF_STATE_OFF) {
78233e8c569SAndrew Walbran 		ERROR("Unexpected affinity info state.\n");
783532ed618SSoby Mathew 		panic();
784532ed618SSoby Mathew 	}
785532ed618SSoby Mathew 
786532ed618SSoby Mathew 	/*
787532ed618SSoby Mathew 	 * Get the maximum power domain level to traverse to after this cpu
788532ed618SSoby Mathew 	 * has been physically powered up.
789532ed618SSoby Mathew 	 */
790532ed618SSoby Mathew 	end_pwrlvl = get_power_on_target_pwrlvl();
791532ed618SSoby Mathew 
79274d27d00SAndrew F. Davis 	/* Get the parent nodes */
79374d27d00SAndrew F. Davis 	psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
79474d27d00SAndrew F. Davis 
795532ed618SSoby Mathew 	/*
796532ed618SSoby Mathew 	 * This function acquires the lock corresponding to each power level so
797532ed618SSoby Mathew 	 * that by the time all locks are taken, the system topology is snapshot
798532ed618SSoby Mathew 	 * and state management can be done safely.
799532ed618SSoby Mathew 	 */
80074d27d00SAndrew F. Davis 	psci_acquire_pwr_domain_locks(end_pwrlvl, parent_nodes);
801532ed618SSoby Mathew 
802bfc87a8dSSoby Mathew 	psci_get_target_local_pwr_states(end_pwrlvl, &state_info);
803bfc87a8dSSoby Mathew 
804532ed618SSoby Mathew #if ENABLE_PSCI_STAT
80504c1db1eSdp-arm 	plat_psci_stat_accounting_stop(&state_info);
806532ed618SSoby Mathew #endif
807532ed618SSoby Mathew 
808532ed618SSoby Mathew 	/*
809532ed618SSoby Mathew 	 * This CPU could be resuming from suspend or it could have just been
810532ed618SSoby Mathew 	 * turned on. To distinguish between these 2 cases, we examine the
811532ed618SSoby Mathew 	 * affinity state of the CPU:
812532ed618SSoby Mathew 	 *  - If the affinity state is ON_PENDING then it has just been
813532ed618SSoby Mathew 	 *    turned on.
814532ed618SSoby Mathew 	 *  - Else it is resuming from suspend.
815532ed618SSoby Mathew 	 *
816532ed618SSoby Mathew 	 * Depending on the type of warm reset identified, choose the right set
817532ed618SSoby Mathew 	 * of power management handler and perform the generic, architecture
818532ed618SSoby Mathew 	 * and platform specific handling.
819532ed618SSoby Mathew 	 */
820532ed618SSoby Mathew 	if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING)
821532ed618SSoby Mathew 		psci_cpu_on_finish(cpu_idx, &state_info);
822532ed618SSoby Mathew 	else
823532ed618SSoby Mathew 		psci_cpu_suspend_finish(cpu_idx, &state_info);
824532ed618SSoby Mathew 
825532ed618SSoby Mathew 	/*
826532ed618SSoby Mathew 	 * Set the requested and target state of this CPU and all the higher
827532ed618SSoby Mathew 	 * power domains which are ancestors of this CPU to run.
828532ed618SSoby Mathew 	 */
829532ed618SSoby Mathew 	psci_set_pwr_domains_to_run(end_pwrlvl);
830532ed618SSoby Mathew 
831532ed618SSoby Mathew #if ENABLE_PSCI_STAT
832532ed618SSoby Mathew 	/*
833532ed618SSoby Mathew 	 * Update PSCI stats.
834532ed618SSoby Mathew 	 * Caches are off when writing stats data on the power down path.
835532ed618SSoby Mathew 	 * Since caches are now enabled, it's necessary to do cache
836532ed618SSoby Mathew 	 * maintenance before reading that same data.
837532ed618SSoby Mathew 	 */
83804c1db1eSdp-arm 	psci_stats_update_pwr_up(end_pwrlvl, &state_info);
839532ed618SSoby Mathew #endif
840532ed618SSoby Mathew 
841532ed618SSoby Mathew 	/*
842532ed618SSoby Mathew 	 * This loop releases the lock corresponding to each power level
843532ed618SSoby Mathew 	 * in the reverse order to which they were acquired.
844532ed618SSoby Mathew 	 */
84574d27d00SAndrew F. Davis 	psci_release_pwr_domain_locks(end_pwrlvl, parent_nodes);
846532ed618SSoby Mathew }
847532ed618SSoby Mathew 
848532ed618SSoby Mathew /*******************************************************************************
849532ed618SSoby Mathew  * This function initializes the set of hooks that PSCI invokes as part of power
850532ed618SSoby Mathew  * management operation. The power management hooks are expected to be provided
851532ed618SSoby Mathew  * by the SPD, after it finishes all its initialization
852532ed618SSoby Mathew  ******************************************************************************/
853532ed618SSoby Mathew void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
854532ed618SSoby Mathew {
8556b7b0f36SAntonio Nino Diaz 	assert(pm != NULL);
856532ed618SSoby Mathew 	psci_spd_pm = pm;
857532ed618SSoby Mathew 
8586b7b0f36SAntonio Nino Diaz 	if (pm->svc_migrate != NULL)
859532ed618SSoby Mathew 		psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
860532ed618SSoby Mathew 
8616b7b0f36SAntonio Nino Diaz 	if (pm->svc_migrate_info != NULL)
862532ed618SSoby Mathew 		psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
863532ed618SSoby Mathew 				| define_psci_cap(PSCI_MIG_INFO_TYPE);
864532ed618SSoby Mathew }
865532ed618SSoby Mathew 
866532ed618SSoby Mathew /*******************************************************************************
867532ed618SSoby Mathew  * This function invokes the migrate info hook in the spd_pm_ops. It performs
868532ed618SSoby Mathew  * the necessary return value validation. If the Secure Payload is UP and
869532ed618SSoby Mathew  * migrate capable, it returns the mpidr of the CPU on which the Secure payload
870532ed618SSoby Mathew  * is resident through the mpidr parameter. Else the value of the parameter on
871532ed618SSoby Mathew  * return is undefined.
872532ed618SSoby Mathew  ******************************************************************************/
873532ed618SSoby Mathew int psci_spd_migrate_info(u_register_t *mpidr)
874532ed618SSoby Mathew {
875532ed618SSoby Mathew 	int rc;
876532ed618SSoby Mathew 
8776b7b0f36SAntonio Nino Diaz 	if ((psci_spd_pm == NULL) || (psci_spd_pm->svc_migrate_info == NULL))
878532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
879532ed618SSoby Mathew 
880532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate_info(mpidr);
881532ed618SSoby Mathew 
8826b7b0f36SAntonio Nino Diaz 	assert((rc == PSCI_TOS_UP_MIG_CAP) || (rc == PSCI_TOS_NOT_UP_MIG_CAP) ||
8836b7b0f36SAntonio Nino Diaz 	       (rc == PSCI_TOS_NOT_PRESENT_MP) || (rc == PSCI_E_NOT_SUPPORTED));
884532ed618SSoby Mathew 
885532ed618SSoby Mathew 	return rc;
886532ed618SSoby Mathew }
887532ed618SSoby Mathew 
888532ed618SSoby Mathew 
889532ed618SSoby Mathew /*******************************************************************************
890532ed618SSoby Mathew  * This function prints the state of all power domains present in the
891532ed618SSoby Mathew  * system
892532ed618SSoby Mathew  ******************************************************************************/
893532ed618SSoby Mathew void psci_print_power_domain_map(void)
894532ed618SSoby Mathew {
895532ed618SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_INFO
896ab4df50cSPankaj Gupta 	unsigned int idx;
897532ed618SSoby Mathew 	plat_local_state_t state;
898532ed618SSoby Mathew 	plat_local_state_type_t state_type;
899532ed618SSoby Mathew 
900532ed618SSoby Mathew 	/* This array maps to the PSCI_STATE_X definitions in psci.h */
901532ed618SSoby Mathew 	static const char * const psci_state_type_str[] = {
902532ed618SSoby Mathew 		"ON",
903532ed618SSoby Mathew 		"RETENTION",
904532ed618SSoby Mathew 		"OFF",
905532ed618SSoby Mathew 	};
906532ed618SSoby Mathew 
907532ed618SSoby Mathew 	INFO("PSCI Power Domain Map:\n");
908ab4df50cSPankaj Gupta 	for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - psci_plat_core_count);
909532ed618SSoby Mathew 							idx++) {
910532ed618SSoby Mathew 		state_type = find_local_state_type(
911532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].local_state);
912b9338eeeSYann Gautier 		INFO("  Domain Node : Level %u, parent_node %u,"
913532ed618SSoby Mathew 				" State %s (0x%x)\n",
914532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].level,
915532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].parent_node,
916532ed618SSoby Mathew 				psci_state_type_str[state_type],
917532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].local_state);
918532ed618SSoby Mathew 	}
919532ed618SSoby Mathew 
920ab4df50cSPankaj Gupta 	for (idx = 0; idx < psci_plat_core_count; idx++) {
921532ed618SSoby Mathew 		state = psci_get_cpu_local_state_by_idx(idx);
922532ed618SSoby Mathew 		state_type = find_local_state_type(state);
923b9338eeeSYann Gautier 		INFO("  CPU Node : MPID 0x%llx, parent_node %u,"
924532ed618SSoby Mathew 				" State %s (0x%x)\n",
925532ed618SSoby Mathew 				(unsigned long long)psci_cpu_pd_nodes[idx].mpidr,
926532ed618SSoby Mathew 				psci_cpu_pd_nodes[idx].parent_node,
927532ed618SSoby Mathew 				psci_state_type_str[state_type],
928532ed618SSoby Mathew 				psci_get_cpu_local_state_by_idx(idx));
929532ed618SSoby Mathew 	}
930532ed618SSoby Mathew #endif
931532ed618SSoby Mathew }
932532ed618SSoby Mathew 
933b10d4499SJeenu Viswambharan /******************************************************************************
934b10d4499SJeenu Viswambharan  * Return whether any secondaries were powered up with CPU_ON call. A CPU that
935b10d4499SJeenu Viswambharan  * have ever been powered up would have set its MPDIR value to something other
936b10d4499SJeenu Viswambharan  * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to
937b10d4499SJeenu Viswambharan  * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is
938b10d4499SJeenu Viswambharan  * meaningful only when called on the primary CPU during early boot.
939b10d4499SJeenu Viswambharan  *****************************************************************************/
940b10d4499SJeenu Viswambharan int psci_secondaries_brought_up(void)
941b10d4499SJeenu Viswambharan {
9426b7b0f36SAntonio Nino Diaz 	unsigned int idx, n_valid = 0U;
943b10d4499SJeenu Viswambharan 
9446b7b0f36SAntonio Nino Diaz 	for (idx = 0U; idx < ARRAY_SIZE(psci_cpu_pd_nodes); idx++) {
945b10d4499SJeenu Viswambharan 		if (psci_cpu_pd_nodes[idx].mpidr != PSCI_INVALID_MPIDR)
946b10d4499SJeenu Viswambharan 			n_valid++;
947b10d4499SJeenu Viswambharan 	}
948b10d4499SJeenu Viswambharan 
9496b7b0f36SAntonio Nino Diaz 	assert(n_valid > 0U);
950b10d4499SJeenu Viswambharan 
9516b7b0f36SAntonio Nino Diaz 	return (n_valid > 1U) ? 1 : 0;
952b10d4499SJeenu Viswambharan }
953b10d4499SJeenu Viswambharan 
954b0408e87SJeenu Viswambharan /*******************************************************************************
955b0408e87SJeenu Viswambharan  * Initiate power down sequence, by calling power down operations registered for
956b0408e87SJeenu Viswambharan  * this CPU.
957b0408e87SJeenu Viswambharan  ******************************************************************************/
95865bbb935SPranav Madhu void psci_pwrdown_cpu(unsigned int power_level)
959b0408e87SJeenu Viswambharan {
960b0408e87SJeenu Viswambharan #if HW_ASSISTED_COHERENCY
961b0408e87SJeenu Viswambharan 	/*
962b0408e87SJeenu Viswambharan 	 * With hardware-assisted coherency, the CPU drivers only initiate the
963b0408e87SJeenu Viswambharan 	 * power down sequence, without performing cache-maintenance operations
964c98db6c6SAndrew F. Davis 	 * in software. Data caches enabled both before and after this call.
965b0408e87SJeenu Viswambharan 	 */
966b0408e87SJeenu Viswambharan 	prepare_cpu_pwr_dwn(power_level);
967b0408e87SJeenu Viswambharan #else
968b0408e87SJeenu Viswambharan 	/*
969b0408e87SJeenu Viswambharan 	 * Without hardware-assisted coherency, the CPU drivers disable data
970c98db6c6SAndrew F. Davis 	 * caches, then perform cache-maintenance operations in software.
971b0408e87SJeenu Viswambharan 	 *
972c98db6c6SAndrew F. Davis 	 * This also calls prepare_cpu_pwr_dwn() to initiate power down
973c98db6c6SAndrew F. Davis 	 * sequence, but that function will return with data caches disabled.
974c98db6c6SAndrew F. Davis 	 * We must ensure that the stack memory is flushed out to memory before
975c98db6c6SAndrew F. Davis 	 * we start popping from it again.
976b0408e87SJeenu Viswambharan 	 */
977b0408e87SJeenu Viswambharan 	psci_do_pwrdown_cache_maintenance(power_level);
978b0408e87SJeenu Viswambharan #endif
979b0408e87SJeenu Viswambharan }
98022744909SSandeep Tripathy 
98122744909SSandeep Tripathy /*******************************************************************************
98222744909SSandeep Tripathy  * This function invokes the callback 'stop_func()' with the 'mpidr' of each
98322744909SSandeep Tripathy  * online PE. Caller can pass suitable method to stop a remote core.
98422744909SSandeep Tripathy  *
98522744909SSandeep Tripathy  * 'wait_ms' is the timeout value in milliseconds for the other cores to
98622744909SSandeep Tripathy  * transition to power down state. Passing '0' makes it non-blocking.
98722744909SSandeep Tripathy  *
98822744909SSandeep Tripathy  * The function returns 'PSCI_E_DENIED' if some cores failed to stop within the
98922744909SSandeep Tripathy  * given timeout.
99022744909SSandeep Tripathy  ******************************************************************************/
99122744909SSandeep Tripathy int psci_stop_other_cores(unsigned int wait_ms,
99222744909SSandeep Tripathy 				   void (*stop_func)(u_register_t mpidr))
99322744909SSandeep Tripathy {
99422744909SSandeep Tripathy 	unsigned int idx, this_cpu_idx;
99522744909SSandeep Tripathy 
99622744909SSandeep Tripathy 	this_cpu_idx = plat_my_core_pos();
99722744909SSandeep Tripathy 
99822744909SSandeep Tripathy 	/* Invoke stop_func for each core */
99922744909SSandeep Tripathy 	for (idx = 0U; idx < psci_plat_core_count; idx++) {
100022744909SSandeep Tripathy 		/* skip current CPU */
100122744909SSandeep Tripathy 		if (idx == this_cpu_idx) {
100222744909SSandeep Tripathy 			continue;
100322744909SSandeep Tripathy 		}
100422744909SSandeep Tripathy 
100522744909SSandeep Tripathy 		/* Check if the CPU is ON */
100622744909SSandeep Tripathy 		if (psci_get_aff_info_state_by_idx(idx) == AFF_STATE_ON) {
100722744909SSandeep Tripathy 			(*stop_func)(psci_cpu_pd_nodes[idx].mpidr);
100822744909SSandeep Tripathy 		}
100922744909SSandeep Tripathy 	}
101022744909SSandeep Tripathy 
101122744909SSandeep Tripathy 	/* Need to wait for other cores to shutdown */
101222744909SSandeep Tripathy 	if (wait_ms != 0U) {
1013*b41b0824SJayanth Dodderi Chidanand 		while ((wait_ms-- != 0U) && (!psci_is_last_on_cpu())) {
101422744909SSandeep Tripathy 			mdelay(1U);
101522744909SSandeep Tripathy 		}
101622744909SSandeep Tripathy 
1017*b41b0824SJayanth Dodderi Chidanand 		if (!psci_is_last_on_cpu()) {
101822744909SSandeep Tripathy 			WARN("Failed to stop all cores!\n");
101922744909SSandeep Tripathy 			psci_print_power_domain_map();
102022744909SSandeep Tripathy 			return PSCI_E_DENIED;
102122744909SSandeep Tripathy 		}
102222744909SSandeep Tripathy 	}
102322744909SSandeep Tripathy 
102422744909SSandeep Tripathy 	return PSCI_E_SUCCESS;
102522744909SSandeep Tripathy }
1026ce14a12fSLucian Paul-Trifu 
1027ce14a12fSLucian Paul-Trifu /*******************************************************************************
1028ce14a12fSLucian Paul-Trifu  * This function verifies that all the other cores in the system have been
1029ce14a12fSLucian Paul-Trifu  * turned OFF and the current CPU is the last running CPU in the system.
1030ce14a12fSLucian Paul-Trifu  * Returns true if the current CPU is the last ON CPU or false otherwise.
1031ce14a12fSLucian Paul-Trifu  *
1032ce14a12fSLucian Paul-Trifu  * This API has following differences with psci_is_last_on_cpu
1033ce14a12fSLucian Paul-Trifu  *  1. PSCI states are locked
1034ce14a12fSLucian Paul-Trifu  ******************************************************************************/
1035ce14a12fSLucian Paul-Trifu bool psci_is_last_on_cpu_safe(void)
1036ce14a12fSLucian Paul-Trifu {
1037ce14a12fSLucian Paul-Trifu 	unsigned int this_core = plat_my_core_pos();
1038ce14a12fSLucian Paul-Trifu 	unsigned int parent_nodes[PLAT_MAX_PWR_LVL] = {0};
1039ce14a12fSLucian Paul-Trifu 
1040*b41b0824SJayanth Dodderi Chidanand 	psci_get_parent_pwr_domain_nodes(this_core, PLAT_MAX_PWR_LVL, parent_nodes);
1041ce14a12fSLucian Paul-Trifu 
1042ce14a12fSLucian Paul-Trifu 	psci_acquire_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1043ce14a12fSLucian Paul-Trifu 
1044*b41b0824SJayanth Dodderi Chidanand 	if (!psci_is_last_on_cpu()) {
1045*b41b0824SJayanth Dodderi Chidanand 		psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1046ce14a12fSLucian Paul-Trifu 		return false;
1047ce14a12fSLucian Paul-Trifu 	}
1048ce14a12fSLucian Paul-Trifu 
1049ce14a12fSLucian Paul-Trifu 	psci_release_pwr_domain_locks(PLAT_MAX_PWR_LVL, parent_nodes);
1050ce14a12fSLucian Paul-Trifu 
1051ce14a12fSLucian Paul-Trifu 	return true;
1052ce14a12fSLucian Paul-Trifu }
1053