xref: /rk3399_ARM-atf/lib/psci/psci_common.c (revision 5722b78cdb4a69d08c3c585aae2fb8dd9cbb9bfc)
1532ed618SSoby Mathew /*
204c1db1eSdp-arm  * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5532ed618SSoby Mathew  */
6532ed618SSoby Mathew 
7532ed618SSoby Mathew #include <arch.h>
8532ed618SSoby Mathew #include <arch_helpers.h>
9532ed618SSoby Mathew #include <assert.h>
10532ed618SSoby Mathew #include <bl_common.h>
11532ed618SSoby Mathew #include <context.h>
12532ed618SSoby Mathew #include <context_mgmt.h>
13532ed618SSoby Mathew #include <debug.h>
14532ed618SSoby Mathew #include <platform.h>
15532ed618SSoby Mathew #include <string.h>
1632f0d3c6SDouglas Raillard #include <utils.h>
17532ed618SSoby Mathew #include "psci_private.h"
18532ed618SSoby Mathew 
19532ed618SSoby Mathew /*
20532ed618SSoby Mathew  * SPD power management operations, expected to be supplied by the registered
21532ed618SSoby Mathew  * SPD on successful SP initialization
22532ed618SSoby Mathew  */
23532ed618SSoby Mathew const spd_pm_ops_t *psci_spd_pm;
24532ed618SSoby Mathew 
25532ed618SSoby Mathew /*
26532ed618SSoby Mathew  * PSCI requested local power state map. This array is used to store the local
27532ed618SSoby Mathew  * power states requested by a CPU for power levels from level 1 to
28532ed618SSoby Mathew  * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
29532ed618SSoby Mathew  * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
30532ed618SSoby Mathew  * CPU are the same.
31532ed618SSoby Mathew  *
32532ed618SSoby Mathew  * During state coordination, the platform is passed an array containing the
33532ed618SSoby Mathew  * local states requested for a particular non cpu power domain by each cpu
34532ed618SSoby Mathew  * within the domain.
35532ed618SSoby Mathew  *
36532ed618SSoby Mathew  * TODO: Dense packing of the requested states will cause cache thrashing
37532ed618SSoby Mathew  * when multiple power domains write to it. If we allocate the requested
38532ed618SSoby Mathew  * states at each power level in a cache-line aligned per-domain memory,
39532ed618SSoby Mathew  * the cache thrashing can be avoided.
40532ed618SSoby Mathew  */
41532ed618SSoby Mathew static plat_local_state_t
42532ed618SSoby Mathew 	psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
43532ed618SSoby Mathew 
44532ed618SSoby Mathew 
45532ed618SSoby Mathew /*******************************************************************************
46532ed618SSoby Mathew  * Arrays that hold the platform's power domain tree information for state
47532ed618SSoby Mathew  * management of power domains.
48532ed618SSoby Mathew  * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
49532ed618SSoby Mathew  * which is an ancestor of a CPU power domain.
50532ed618SSoby Mathew  * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
51532ed618SSoby Mathew  ******************************************************************************/
52532ed618SSoby Mathew non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
53532ed618SSoby Mathew #if USE_COHERENT_MEM
54532ed618SSoby Mathew __section("tzfw_coherent_mem")
55532ed618SSoby Mathew #endif
56532ed618SSoby Mathew ;
57532ed618SSoby Mathew 
58b0408e87SJeenu Viswambharan /* Lock for PSCI state coordination */
59b0408e87SJeenu Viswambharan DEFINE_PSCI_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
60532ed618SSoby Mathew 
61532ed618SSoby Mathew cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
62532ed618SSoby Mathew 
63532ed618SSoby Mathew /*******************************************************************************
64532ed618SSoby Mathew  * Pointer to functions exported by the platform to complete power mgmt. ops
65532ed618SSoby Mathew  ******************************************************************************/
66532ed618SSoby Mathew const plat_psci_ops_t *psci_plat_pm_ops;
67532ed618SSoby Mathew 
68532ed618SSoby Mathew /******************************************************************************
69532ed618SSoby Mathew  * Check that the maximum power level supported by the platform makes sense
70532ed618SSoby Mathew  *****************************************************************************/
71532ed618SSoby Mathew CASSERT(PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL && \
72532ed618SSoby Mathew 		PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL, \
73532ed618SSoby Mathew 		assert_platform_max_pwrlvl_check);
74532ed618SSoby Mathew 
75532ed618SSoby Mathew /*
76532ed618SSoby Mathew  * The plat_local_state used by the platform is one of these types: RUN,
77532ed618SSoby Mathew  * RETENTION and OFF. The platform can define further sub-states for each type
78532ed618SSoby Mathew  * apart from RUN. This categorization is done to verify the sanity of the
79532ed618SSoby Mathew  * psci_power_state passed by the platform and to print debug information. The
80532ed618SSoby Mathew  * categorization is done on the basis of the following conditions:
81532ed618SSoby Mathew  *
82532ed618SSoby Mathew  * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
83532ed618SSoby Mathew  *
84532ed618SSoby Mathew  * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
85532ed618SSoby Mathew  *    STATE_TYPE_RETN.
86532ed618SSoby Mathew  *
87532ed618SSoby Mathew  * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
88532ed618SSoby Mathew  *    STATE_TYPE_OFF.
89532ed618SSoby Mathew  */
90532ed618SSoby Mathew typedef enum plat_local_state_type {
91532ed618SSoby Mathew 	STATE_TYPE_RUN = 0,
92532ed618SSoby Mathew 	STATE_TYPE_RETN,
93532ed618SSoby Mathew 	STATE_TYPE_OFF
94532ed618SSoby Mathew } plat_local_state_type_t;
95532ed618SSoby Mathew 
96532ed618SSoby Mathew /* The macro used to categorize plat_local_state. */
97532ed618SSoby Mathew #define find_local_state_type(plat_local_state)					\
98532ed618SSoby Mathew 		((plat_local_state) ? ((plat_local_state > PLAT_MAX_RET_STATE)	\
99532ed618SSoby Mathew 		? STATE_TYPE_OFF : STATE_TYPE_RETN)				\
100532ed618SSoby Mathew 		: STATE_TYPE_RUN)
101532ed618SSoby Mathew 
102532ed618SSoby Mathew /******************************************************************************
103532ed618SSoby Mathew  * Check that the maximum retention level supported by the platform is less
104532ed618SSoby Mathew  * than the maximum off level.
105532ed618SSoby Mathew  *****************************************************************************/
106532ed618SSoby Mathew CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE, \
107532ed618SSoby Mathew 		assert_platform_max_off_and_retn_state_check);
108532ed618SSoby Mathew 
109532ed618SSoby Mathew /******************************************************************************
110532ed618SSoby Mathew  * This function ensures that the power state parameter in a CPU_SUSPEND request
111532ed618SSoby Mathew  * is valid. If so, it returns the requested states for each power level.
112532ed618SSoby Mathew  *****************************************************************************/
113532ed618SSoby Mathew int psci_validate_power_state(unsigned int power_state,
114532ed618SSoby Mathew 			      psci_power_state_t *state_info)
115532ed618SSoby Mathew {
116532ed618SSoby Mathew 	/* Check SBZ bits in power state are zero */
117532ed618SSoby Mathew 	if (psci_check_power_state(power_state))
118532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
119532ed618SSoby Mathew 
120532ed618SSoby Mathew 	assert(psci_plat_pm_ops->validate_power_state);
121532ed618SSoby Mathew 
122532ed618SSoby Mathew 	/* Validate the power_state using platform pm_ops */
123532ed618SSoby Mathew 	return psci_plat_pm_ops->validate_power_state(power_state, state_info);
124532ed618SSoby Mathew }
125532ed618SSoby Mathew 
126532ed618SSoby Mathew /******************************************************************************
127532ed618SSoby Mathew  * This function retrieves the `psci_power_state_t` for system suspend from
128532ed618SSoby Mathew  * the platform.
129532ed618SSoby Mathew  *****************************************************************************/
130532ed618SSoby Mathew void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
131532ed618SSoby Mathew {
132532ed618SSoby Mathew 	/*
133532ed618SSoby Mathew 	 * Assert that the required pm_ops hook is implemented to ensure that
134532ed618SSoby Mathew 	 * the capability detected during psci_setup() is valid.
135532ed618SSoby Mathew 	 */
136532ed618SSoby Mathew 	assert(psci_plat_pm_ops->get_sys_suspend_power_state);
137532ed618SSoby Mathew 
138532ed618SSoby Mathew 	/*
139532ed618SSoby Mathew 	 * Query the platform for the power_state required for system suspend
140532ed618SSoby Mathew 	 */
141532ed618SSoby Mathew 	psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
142532ed618SSoby Mathew }
143532ed618SSoby Mathew 
144532ed618SSoby Mathew /*******************************************************************************
145532ed618SSoby Mathew  * This function verifies that the all the other cores in the system have been
146532ed618SSoby Mathew  * turned OFF and the current CPU is the last running CPU in the system.
147532ed618SSoby Mathew  * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
148532ed618SSoby Mathew  * otherwise.
149532ed618SSoby Mathew  ******************************************************************************/
150532ed618SSoby Mathew unsigned int psci_is_last_on_cpu(void)
151532ed618SSoby Mathew {
152532ed618SSoby Mathew 	unsigned int cpu_idx, my_idx = plat_my_core_pos();
153532ed618SSoby Mathew 
154532ed618SSoby Mathew 	for (cpu_idx = 0; cpu_idx < PLATFORM_CORE_COUNT; cpu_idx++) {
155532ed618SSoby Mathew 		if (cpu_idx == my_idx) {
156532ed618SSoby Mathew 			assert(psci_get_aff_info_state() == AFF_STATE_ON);
157532ed618SSoby Mathew 			continue;
158532ed618SSoby Mathew 		}
159532ed618SSoby Mathew 
160532ed618SSoby Mathew 		if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF)
161532ed618SSoby Mathew 			return 0;
162532ed618SSoby Mathew 	}
163532ed618SSoby Mathew 
164532ed618SSoby Mathew 	return 1;
165532ed618SSoby Mathew }
166532ed618SSoby Mathew 
167532ed618SSoby Mathew /*******************************************************************************
168532ed618SSoby Mathew  * Routine to return the maximum power level to traverse to after a cpu has
169532ed618SSoby Mathew  * been physically powered up. It is expected to be called immediately after
170532ed618SSoby Mathew  * reset from assembler code.
171532ed618SSoby Mathew  ******************************************************************************/
172532ed618SSoby Mathew static unsigned int get_power_on_target_pwrlvl(void)
173532ed618SSoby Mathew {
174532ed618SSoby Mathew 	unsigned int pwrlvl;
175532ed618SSoby Mathew 
176532ed618SSoby Mathew 	/*
177532ed618SSoby Mathew 	 * Assume that this cpu was suspended and retrieve its target power
178532ed618SSoby Mathew 	 * level. If it is invalid then it could only have been turned off
179532ed618SSoby Mathew 	 * earlier. PLAT_MAX_PWR_LVL will be the highest power level a
180532ed618SSoby Mathew 	 * cpu can be turned off to.
181532ed618SSoby Mathew 	 */
182532ed618SSoby Mathew 	pwrlvl = psci_get_suspend_pwrlvl();
183532ed618SSoby Mathew 	if (pwrlvl == PSCI_INVALID_PWR_LVL)
184532ed618SSoby Mathew 		pwrlvl = PLAT_MAX_PWR_LVL;
185532ed618SSoby Mathew 	return pwrlvl;
186532ed618SSoby Mathew }
187532ed618SSoby Mathew 
188532ed618SSoby Mathew /******************************************************************************
189532ed618SSoby Mathew  * Helper function to update the requested local power state array. This array
190532ed618SSoby Mathew  * does not store the requested state for the CPU power level. Hence an
191532ed618SSoby Mathew  * assertion is added to prevent us from accessing the wrong index.
192532ed618SSoby Mathew  *****************************************************************************/
193532ed618SSoby Mathew static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
194532ed618SSoby Mathew 					 unsigned int cpu_idx,
195532ed618SSoby Mathew 					 plat_local_state_t req_pwr_state)
196532ed618SSoby Mathew {
197*5722b78cSAlistair Francis 	/*
198*5722b78cSAlistair Francis 	 * This should never happen, we have this here to avoid
199*5722b78cSAlistair Francis 	 * "array subscript is above array bounds" errors in GCC.
200*5722b78cSAlistair Francis 	 */
201532ed618SSoby Mathew 	assert(pwrlvl > PSCI_CPU_PWR_LVL);
202*5722b78cSAlistair Francis #pragma GCC diagnostic push
203*5722b78cSAlistair Francis #pragma GCC diagnostic ignored "-Warray-bounds"
204532ed618SSoby Mathew 	psci_req_local_pwr_states[pwrlvl - 1][cpu_idx] = req_pwr_state;
205*5722b78cSAlistair Francis #pragma GCC diagnostic pop
206532ed618SSoby Mathew }
207532ed618SSoby Mathew 
208532ed618SSoby Mathew /******************************************************************************
209532ed618SSoby Mathew  * This function initializes the psci_req_local_pwr_states.
210532ed618SSoby Mathew  *****************************************************************************/
211532ed618SSoby Mathew void psci_init_req_local_pwr_states(void)
212532ed618SSoby Mathew {
213532ed618SSoby Mathew 	/* Initialize the requested state of all non CPU power domains as OFF */
214532ed618SSoby Mathew 	memset(&psci_req_local_pwr_states, PLAT_MAX_OFF_STATE,
215532ed618SSoby Mathew 			sizeof(psci_req_local_pwr_states));
216532ed618SSoby Mathew }
217532ed618SSoby Mathew 
218532ed618SSoby Mathew /******************************************************************************
219532ed618SSoby Mathew  * Helper function to return a reference to an array containing the local power
220532ed618SSoby Mathew  * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
221532ed618SSoby Mathew  * array will be the number of cpu power domains of which this power domain is
222532ed618SSoby Mathew  * an ancestor. These requested states will be used to determine a suitable
223532ed618SSoby Mathew  * target state for this power domain during psci state coordination. An
224532ed618SSoby Mathew  * assertion is added to prevent us from accessing the CPU power level.
225532ed618SSoby Mathew  *****************************************************************************/
226532ed618SSoby Mathew static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
227532ed618SSoby Mathew 							 unsigned int cpu_idx)
228532ed618SSoby Mathew {
229532ed618SSoby Mathew 	assert(pwrlvl > PSCI_CPU_PWR_LVL);
230532ed618SSoby Mathew 
231532ed618SSoby Mathew 	return &psci_req_local_pwr_states[pwrlvl - 1][cpu_idx];
232532ed618SSoby Mathew }
233532ed618SSoby Mathew 
234a10d3632SJeenu Viswambharan /*
235a10d3632SJeenu Viswambharan  * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
236a10d3632SJeenu Viswambharan  * memory.
237a10d3632SJeenu Viswambharan  *
238a10d3632SJeenu Viswambharan  * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
239a10d3632SJeenu Viswambharan  * it's accessed by both cached and non-cached participants. To serve the common
240a10d3632SJeenu Viswambharan  * minimum, perform a cache flush before read and after write so that non-cached
241a10d3632SJeenu Viswambharan  * participants operate on latest data in main memory.
242a10d3632SJeenu Viswambharan  *
243a10d3632SJeenu Viswambharan  * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
244a10d3632SJeenu Viswambharan  * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
245a10d3632SJeenu Viswambharan  * In both cases, no cache operations are required.
246a10d3632SJeenu Viswambharan  */
247a10d3632SJeenu Viswambharan 
248a10d3632SJeenu Viswambharan /*
249a10d3632SJeenu Viswambharan  * Retrieve local state of non-CPU power domain node from a non-cached CPU,
250a10d3632SJeenu Viswambharan  * after any required cache maintenance operation.
251a10d3632SJeenu Viswambharan  */
252a10d3632SJeenu Viswambharan static plat_local_state_t get_non_cpu_pd_node_local_state(
253a10d3632SJeenu Viswambharan 		unsigned int parent_idx)
254a10d3632SJeenu Viswambharan {
255a10d3632SJeenu Viswambharan #if !USE_COHERENT_MEM || !HW_ASSISTED_COHERENCY
256a10d3632SJeenu Viswambharan 	flush_dcache_range(
257a10d3632SJeenu Viswambharan 			(uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
258a10d3632SJeenu Viswambharan 			sizeof(psci_non_cpu_pd_nodes[parent_idx]));
259a10d3632SJeenu Viswambharan #endif
260a10d3632SJeenu Viswambharan 	return psci_non_cpu_pd_nodes[parent_idx].local_state;
261a10d3632SJeenu Viswambharan }
262a10d3632SJeenu Viswambharan 
263a10d3632SJeenu Viswambharan /*
264a10d3632SJeenu Viswambharan  * Update local state of non-CPU power domain node from a cached CPU; perform
265a10d3632SJeenu Viswambharan  * any required cache maintenance operation afterwards.
266a10d3632SJeenu Viswambharan  */
267a10d3632SJeenu Viswambharan static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
268a10d3632SJeenu Viswambharan 		plat_local_state_t state)
269a10d3632SJeenu Viswambharan {
270a10d3632SJeenu Viswambharan 	psci_non_cpu_pd_nodes[parent_idx].local_state = state;
271a10d3632SJeenu Viswambharan #if !USE_COHERENT_MEM || !HW_ASSISTED_COHERENCY
272a10d3632SJeenu Viswambharan 	flush_dcache_range(
273a10d3632SJeenu Viswambharan 			(uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
274a10d3632SJeenu Viswambharan 			sizeof(psci_non_cpu_pd_nodes[parent_idx]));
275a10d3632SJeenu Viswambharan #endif
276a10d3632SJeenu Viswambharan }
277a10d3632SJeenu Viswambharan 
278532ed618SSoby Mathew /******************************************************************************
279532ed618SSoby Mathew  * Helper function to return the current local power state of each power domain
280532ed618SSoby Mathew  * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
281532ed618SSoby Mathew  * function will be called after a cpu is powered on to find the local state
282532ed618SSoby Mathew  * each power domain has emerged from.
283532ed618SSoby Mathew  *****************************************************************************/
28461eae524SAchin Gupta void psci_get_target_local_pwr_states(unsigned int end_pwrlvl,
285532ed618SSoby Mathew 				      psci_power_state_t *target_state)
286532ed618SSoby Mathew {
287532ed618SSoby Mathew 	unsigned int parent_idx, lvl;
288532ed618SSoby Mathew 	plat_local_state_t *pd_state = target_state->pwr_domain_state;
289532ed618SSoby Mathew 
290532ed618SSoby Mathew 	pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
291532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
292532ed618SSoby Mathew 
293532ed618SSoby Mathew 	/* Copy the local power state from node to state_info */
294532ed618SSoby Mathew 	for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) {
295a10d3632SJeenu Viswambharan 		pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
296532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
297532ed618SSoby Mathew 	}
298532ed618SSoby Mathew 
299532ed618SSoby Mathew 	/* Set the the higher levels to RUN */
300532ed618SSoby Mathew 	for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
301532ed618SSoby Mathew 		target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
302532ed618SSoby Mathew }
303532ed618SSoby Mathew 
304532ed618SSoby Mathew /******************************************************************************
305532ed618SSoby Mathew  * Helper function to set the target local power state that each power domain
306532ed618SSoby Mathew  * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
307532ed618SSoby Mathew  * enter. This function will be called after coordination of requested power
308532ed618SSoby Mathew  * states has been done for each power level.
309532ed618SSoby Mathew  *****************************************************************************/
310532ed618SSoby Mathew static void psci_set_target_local_pwr_states(unsigned int end_pwrlvl,
311532ed618SSoby Mathew 					const psci_power_state_t *target_state)
312532ed618SSoby Mathew {
313532ed618SSoby Mathew 	unsigned int parent_idx, lvl;
314532ed618SSoby Mathew 	const plat_local_state_t *pd_state = target_state->pwr_domain_state;
315532ed618SSoby Mathew 
316532ed618SSoby Mathew 	psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
317532ed618SSoby Mathew 
318532ed618SSoby Mathew 	/*
319a10d3632SJeenu Viswambharan 	 * Need to flush as local_state might be accessed with Data Cache
320532ed618SSoby Mathew 	 * disabled during power on
321532ed618SSoby Mathew 	 */
322a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data.local_state);
323532ed618SSoby Mathew 
324532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
325532ed618SSoby Mathew 
326532ed618SSoby Mathew 	/* Copy the local_state from state_info */
327532ed618SSoby Mathew 	for (lvl = 1; lvl <= end_pwrlvl; lvl++) {
328a10d3632SJeenu Viswambharan 		set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
329532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
330532ed618SSoby Mathew 	}
331532ed618SSoby Mathew }
332532ed618SSoby Mathew 
333532ed618SSoby Mathew 
334532ed618SSoby Mathew /*******************************************************************************
335532ed618SSoby Mathew  * PSCI helper function to get the parent nodes corresponding to a cpu_index.
336532ed618SSoby Mathew  ******************************************************************************/
337532ed618SSoby Mathew void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
338532ed618SSoby Mathew 				      unsigned int end_lvl,
339532ed618SSoby Mathew 				      unsigned int node_index[])
340532ed618SSoby Mathew {
341532ed618SSoby Mathew 	unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node;
3426311f63dSVarun Wadekar 	unsigned int i;
343532ed618SSoby Mathew 
344532ed618SSoby Mathew 	for (i = PSCI_CPU_PWR_LVL + 1; i <= end_lvl; i++) {
345532ed618SSoby Mathew 		*node_index++ = parent_node;
346532ed618SSoby Mathew 		parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
347532ed618SSoby Mathew 	}
348532ed618SSoby Mathew }
349532ed618SSoby Mathew 
350532ed618SSoby Mathew /******************************************************************************
351532ed618SSoby Mathew  * This function is invoked post CPU power up and initialization. It sets the
352532ed618SSoby Mathew  * affinity info state, target power state and requested power state for the
353532ed618SSoby Mathew  * current CPU and all its ancestor power domains to RUN.
354532ed618SSoby Mathew  *****************************************************************************/
355532ed618SSoby Mathew void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl)
356532ed618SSoby Mathew {
357532ed618SSoby Mathew 	unsigned int parent_idx, cpu_idx = plat_my_core_pos(), lvl;
358532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
359532ed618SSoby Mathew 
360532ed618SSoby Mathew 	/* Reset the local_state to RUN for the non cpu power domains. */
361532ed618SSoby Mathew 	for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) {
362a10d3632SJeenu Viswambharan 		set_non_cpu_pd_node_local_state(parent_idx,
363a10d3632SJeenu Viswambharan 				PSCI_LOCAL_STATE_RUN);
364532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl,
365532ed618SSoby Mathew 					     cpu_idx,
366532ed618SSoby Mathew 					     PSCI_LOCAL_STATE_RUN);
367532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
368532ed618SSoby Mathew 	}
369532ed618SSoby Mathew 
370532ed618SSoby Mathew 	/* Set the affinity info state to ON */
371532ed618SSoby Mathew 	psci_set_aff_info_state(AFF_STATE_ON);
372532ed618SSoby Mathew 
373532ed618SSoby Mathew 	psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
374a10d3632SJeenu Viswambharan 	psci_flush_cpu_data(psci_svc_cpu_data);
375532ed618SSoby Mathew }
376532ed618SSoby Mathew 
377532ed618SSoby Mathew /******************************************************************************
378532ed618SSoby Mathew  * This function is passed the local power states requested for each power
379532ed618SSoby Mathew  * domain (state_info) between the current CPU domain and its ancestors until
380532ed618SSoby Mathew  * the target power level (end_pwrlvl). It updates the array of requested power
381532ed618SSoby Mathew  * states with this information.
382532ed618SSoby Mathew  *
383532ed618SSoby Mathew  * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
384532ed618SSoby Mathew  * retrieves the states requested by all the cpus of which the power domain at
385532ed618SSoby Mathew  * that level is an ancestor. It passes this information to the platform to
386532ed618SSoby Mathew  * coordinate and return the target power state. If the target state for a level
387532ed618SSoby Mathew  * is RUN then subsequent levels are not considered. At the CPU level, state
388532ed618SSoby Mathew  * coordination is not required. Hence, the requested and the target states are
389532ed618SSoby Mathew  * the same.
390532ed618SSoby Mathew  *
391532ed618SSoby Mathew  * The 'state_info' is updated with the target state for each level between the
392532ed618SSoby Mathew  * CPU and the 'end_pwrlvl' and returned to the caller.
393532ed618SSoby Mathew  *
394532ed618SSoby Mathew  * This function will only be invoked with data cache enabled and while
395532ed618SSoby Mathew  * powering down a core.
396532ed618SSoby Mathew  *****************************************************************************/
397532ed618SSoby Mathew void psci_do_state_coordination(unsigned int end_pwrlvl,
398532ed618SSoby Mathew 				psci_power_state_t *state_info)
399532ed618SSoby Mathew {
400532ed618SSoby Mathew 	unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos();
401532ed618SSoby Mathew 	unsigned int start_idx, ncpus;
402532ed618SSoby Mathew 	plat_local_state_t target_state, *req_states;
403532ed618SSoby Mathew 
404532ed618SSoby Mathew 	assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
405532ed618SSoby Mathew 	parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
406532ed618SSoby Mathew 
407532ed618SSoby Mathew 	/* For level 0, the requested state will be equivalent
408532ed618SSoby Mathew 	   to target state */
409532ed618SSoby Mathew 	for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) {
410532ed618SSoby Mathew 
411532ed618SSoby Mathew 		/* First update the requested power state */
412532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl, cpu_idx,
413532ed618SSoby Mathew 					     state_info->pwr_domain_state[lvl]);
414532ed618SSoby Mathew 
415532ed618SSoby Mathew 		/* Get the requested power states for this power level */
416532ed618SSoby Mathew 		start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
417532ed618SSoby Mathew 		req_states = psci_get_req_local_pwr_states(lvl, start_idx);
418532ed618SSoby Mathew 
419532ed618SSoby Mathew 		/*
420532ed618SSoby Mathew 		 * Let the platform coordinate amongst the requested states at
421532ed618SSoby Mathew 		 * this power level and return the target local power state.
422532ed618SSoby Mathew 		 */
423532ed618SSoby Mathew 		ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
424532ed618SSoby Mathew 		target_state = plat_get_target_pwr_state(lvl,
425532ed618SSoby Mathew 							 req_states,
426532ed618SSoby Mathew 							 ncpus);
427532ed618SSoby Mathew 
428532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = target_state;
429532ed618SSoby Mathew 
430532ed618SSoby Mathew 		/* Break early if the negotiated target power state is RUN */
431532ed618SSoby Mathew 		if (is_local_state_run(state_info->pwr_domain_state[lvl]))
432532ed618SSoby Mathew 			break;
433532ed618SSoby Mathew 
434532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
435532ed618SSoby Mathew 	}
436532ed618SSoby Mathew 
437532ed618SSoby Mathew 	/*
438532ed618SSoby Mathew 	 * This is for cases when we break out of the above loop early because
439532ed618SSoby Mathew 	 * the target power state is RUN at a power level < end_pwlvl.
440532ed618SSoby Mathew 	 * We update the requested power state from state_info and then
441532ed618SSoby Mathew 	 * set the target state as RUN.
442532ed618SSoby Mathew 	 */
443532ed618SSoby Mathew 	for (lvl = lvl + 1; lvl <= end_pwrlvl; lvl++) {
444532ed618SSoby Mathew 		psci_set_req_local_pwr_state(lvl, cpu_idx,
445532ed618SSoby Mathew 					     state_info->pwr_domain_state[lvl]);
446532ed618SSoby Mathew 		state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
447532ed618SSoby Mathew 
448532ed618SSoby Mathew 	}
449532ed618SSoby Mathew 
450532ed618SSoby Mathew 	/* Update the target state in the power domain nodes */
451532ed618SSoby Mathew 	psci_set_target_local_pwr_states(end_pwrlvl, state_info);
452532ed618SSoby Mathew }
453532ed618SSoby Mathew 
454532ed618SSoby Mathew /******************************************************************************
455532ed618SSoby Mathew  * This function validates a suspend request by making sure that if a standby
456532ed618SSoby Mathew  * state is requested then no power level is turned off and the highest power
457532ed618SSoby Mathew  * level is placed in a standby/retention state.
458532ed618SSoby Mathew  *
459532ed618SSoby Mathew  * It also ensures that the state level X will enter is not shallower than the
460532ed618SSoby Mathew  * state level X + 1 will enter.
461532ed618SSoby Mathew  *
462532ed618SSoby Mathew  * This validation will be enabled only for DEBUG builds as the platform is
463532ed618SSoby Mathew  * expected to perform these validations as well.
464532ed618SSoby Mathew  *****************************************************************************/
465532ed618SSoby Mathew int psci_validate_suspend_req(const psci_power_state_t *state_info,
466532ed618SSoby Mathew 			      unsigned int is_power_down_state)
467532ed618SSoby Mathew {
468532ed618SSoby Mathew 	unsigned int max_off_lvl, target_lvl, max_retn_lvl;
469532ed618SSoby Mathew 	plat_local_state_t state;
470532ed618SSoby Mathew 	plat_local_state_type_t req_state_type, deepest_state_type;
471532ed618SSoby Mathew 	int i;
472532ed618SSoby Mathew 
473532ed618SSoby Mathew 	/* Find the target suspend power level */
474532ed618SSoby Mathew 	target_lvl = psci_find_target_suspend_lvl(state_info);
475532ed618SSoby Mathew 	if (target_lvl == PSCI_INVALID_PWR_LVL)
476532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
477532ed618SSoby Mathew 
478532ed618SSoby Mathew 	/* All power domain levels are in a RUN state to begin with */
479532ed618SSoby Mathew 	deepest_state_type = STATE_TYPE_RUN;
480532ed618SSoby Mathew 
481532ed618SSoby Mathew 	for (i = target_lvl; i >= PSCI_CPU_PWR_LVL; i--) {
482532ed618SSoby Mathew 		state = state_info->pwr_domain_state[i];
483532ed618SSoby Mathew 		req_state_type = find_local_state_type(state);
484532ed618SSoby Mathew 
485532ed618SSoby Mathew 		/*
486532ed618SSoby Mathew 		 * While traversing from the highest power level to the lowest,
487532ed618SSoby Mathew 		 * the state requested for lower levels has to be the same or
488532ed618SSoby Mathew 		 * deeper i.e. equal to or greater than the state at the higher
489532ed618SSoby Mathew 		 * levels. If this condition is true, then the requested state
490532ed618SSoby Mathew 		 * becomes the deepest state encountered so far.
491532ed618SSoby Mathew 		 */
492532ed618SSoby Mathew 		if (req_state_type < deepest_state_type)
493532ed618SSoby Mathew 			return PSCI_E_INVALID_PARAMS;
494532ed618SSoby Mathew 		deepest_state_type = req_state_type;
495532ed618SSoby Mathew 	}
496532ed618SSoby Mathew 
497532ed618SSoby Mathew 	/* Find the highest off power level */
498532ed618SSoby Mathew 	max_off_lvl = psci_find_max_off_lvl(state_info);
499532ed618SSoby Mathew 
500532ed618SSoby Mathew 	/* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
501532ed618SSoby Mathew 	max_retn_lvl = PSCI_INVALID_PWR_LVL;
502532ed618SSoby Mathew 	if (target_lvl != max_off_lvl)
503532ed618SSoby Mathew 		max_retn_lvl = target_lvl;
504532ed618SSoby Mathew 
505532ed618SSoby Mathew 	/*
506532ed618SSoby Mathew 	 * If this is not a request for a power down state then max off level
507532ed618SSoby Mathew 	 * has to be invalid and max retention level has to be a valid power
508532ed618SSoby Mathew 	 * level.
509532ed618SSoby Mathew 	 */
510532ed618SSoby Mathew 	if (!is_power_down_state && (max_off_lvl != PSCI_INVALID_PWR_LVL ||
511532ed618SSoby Mathew 				    max_retn_lvl == PSCI_INVALID_PWR_LVL))
512532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
513532ed618SSoby Mathew 
514532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
515532ed618SSoby Mathew }
516532ed618SSoby Mathew 
517532ed618SSoby Mathew /******************************************************************************
518532ed618SSoby Mathew  * This function finds the highest power level which will be powered down
519532ed618SSoby Mathew  * amongst all the power levels specified in the 'state_info' structure
520532ed618SSoby Mathew  *****************************************************************************/
521532ed618SSoby Mathew unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
522532ed618SSoby Mathew {
523532ed618SSoby Mathew 	int i;
524532ed618SSoby Mathew 
525532ed618SSoby Mathew 	for (i = PLAT_MAX_PWR_LVL; i >= PSCI_CPU_PWR_LVL; i--) {
526532ed618SSoby Mathew 		if (is_local_state_off(state_info->pwr_domain_state[i]))
527532ed618SSoby Mathew 			return i;
528532ed618SSoby Mathew 	}
529532ed618SSoby Mathew 
530532ed618SSoby Mathew 	return PSCI_INVALID_PWR_LVL;
531532ed618SSoby Mathew }
532532ed618SSoby Mathew 
533532ed618SSoby Mathew /******************************************************************************
534532ed618SSoby Mathew  * This functions finds the level of the highest power domain which will be
535532ed618SSoby Mathew  * placed in a low power state during a suspend operation.
536532ed618SSoby Mathew  *****************************************************************************/
537532ed618SSoby Mathew unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
538532ed618SSoby Mathew {
539532ed618SSoby Mathew 	int i;
540532ed618SSoby Mathew 
541532ed618SSoby Mathew 	for (i = PLAT_MAX_PWR_LVL; i >= PSCI_CPU_PWR_LVL; i--) {
542532ed618SSoby Mathew 		if (!is_local_state_run(state_info->pwr_domain_state[i]))
543532ed618SSoby Mathew 			return i;
544532ed618SSoby Mathew 	}
545532ed618SSoby Mathew 
546532ed618SSoby Mathew 	return PSCI_INVALID_PWR_LVL;
547532ed618SSoby Mathew }
548532ed618SSoby Mathew 
549532ed618SSoby Mathew /*******************************************************************************
550532ed618SSoby Mathew  * This function is passed a cpu_index and the highest level in the topology
551532ed618SSoby Mathew  * tree that the operation should be applied to. It picks up locks in order of
552532ed618SSoby Mathew  * increasing power domain level in the range specified.
553532ed618SSoby Mathew  ******************************************************************************/
554532ed618SSoby Mathew void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
555532ed618SSoby Mathew 				   unsigned int cpu_idx)
556532ed618SSoby Mathew {
557532ed618SSoby Mathew 	unsigned int parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
558532ed618SSoby Mathew 	unsigned int level;
559532ed618SSoby Mathew 
560532ed618SSoby Mathew 	/* No locking required for level 0. Hence start locking from level 1 */
561532ed618SSoby Mathew 	for (level = PSCI_CPU_PWR_LVL + 1; level <= end_pwrlvl; level++) {
562532ed618SSoby Mathew 		psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
563532ed618SSoby Mathew 		parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
564532ed618SSoby Mathew 	}
565532ed618SSoby Mathew }
566532ed618SSoby Mathew 
567532ed618SSoby Mathew /*******************************************************************************
568532ed618SSoby Mathew  * This function is passed a cpu_index and the highest level in the topology
569532ed618SSoby Mathew  * tree that the operation should be applied to. It releases the locks in order
570532ed618SSoby Mathew  * of decreasing power domain level in the range specified.
571532ed618SSoby Mathew  ******************************************************************************/
572532ed618SSoby Mathew void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
573532ed618SSoby Mathew 				   unsigned int cpu_idx)
574532ed618SSoby Mathew {
575532ed618SSoby Mathew 	unsigned int parent_idx, parent_nodes[PLAT_MAX_PWR_LVL] = {0};
576532ed618SSoby Mathew 	int level;
577532ed618SSoby Mathew 
578532ed618SSoby Mathew 	/* Get the parent nodes */
579532ed618SSoby Mathew 	psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
580532ed618SSoby Mathew 
581532ed618SSoby Mathew 	/* Unlock top down. No unlocking required for level 0. */
582532ed618SSoby Mathew 	for (level = end_pwrlvl; level >= PSCI_CPU_PWR_LVL + 1; level--) {
583532ed618SSoby Mathew 		parent_idx = parent_nodes[level - 1];
584532ed618SSoby Mathew 		psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
585532ed618SSoby Mathew 	}
586532ed618SSoby Mathew }
587532ed618SSoby Mathew 
588532ed618SSoby Mathew /*******************************************************************************
589532ed618SSoby Mathew  * Simple routine to determine whether a mpidr is valid or not.
590532ed618SSoby Mathew  ******************************************************************************/
591532ed618SSoby Mathew int psci_validate_mpidr(u_register_t mpidr)
592532ed618SSoby Mathew {
593532ed618SSoby Mathew 	if (plat_core_pos_by_mpidr(mpidr) < 0)
594532ed618SSoby Mathew 		return PSCI_E_INVALID_PARAMS;
595532ed618SSoby Mathew 
596532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
597532ed618SSoby Mathew }
598532ed618SSoby Mathew 
599532ed618SSoby Mathew /*******************************************************************************
600532ed618SSoby Mathew  * This function determines the full entrypoint information for the requested
601532ed618SSoby Mathew  * PSCI entrypoint on power on/resume and returns it.
602532ed618SSoby Mathew  ******************************************************************************/
603727e5238SSoby Mathew #ifdef AARCH32
604727e5238SSoby Mathew static int psci_get_ns_ep_info(entry_point_info_t *ep,
605727e5238SSoby Mathew 			       uintptr_t entrypoint,
606727e5238SSoby Mathew 			       u_register_t context_id)
607727e5238SSoby Mathew {
608727e5238SSoby Mathew 	u_register_t ep_attr;
609727e5238SSoby Mathew 	unsigned int aif, ee, mode;
610727e5238SSoby Mathew 	u_register_t scr = read_scr();
611727e5238SSoby Mathew 	u_register_t ns_sctlr, sctlr;
612727e5238SSoby Mathew 
613727e5238SSoby Mathew 	/* Switch to non secure state */
614727e5238SSoby Mathew 	write_scr(scr | SCR_NS_BIT);
615727e5238SSoby Mathew 	isb();
616727e5238SSoby Mathew 	ns_sctlr = read_sctlr();
617727e5238SSoby Mathew 
618727e5238SSoby Mathew 	sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
619727e5238SSoby Mathew 
620727e5238SSoby Mathew 	/* Return to original state */
621727e5238SSoby Mathew 	write_scr(scr);
622727e5238SSoby Mathew 	isb();
623727e5238SSoby Mathew 	ee = 0;
624727e5238SSoby Mathew 
625727e5238SSoby Mathew 	ep_attr = NON_SECURE | EP_ST_DISABLE;
626727e5238SSoby Mathew 	if (sctlr & SCTLR_EE_BIT) {
627727e5238SSoby Mathew 		ep_attr |= EP_EE_BIG;
628727e5238SSoby Mathew 		ee = 1;
629727e5238SSoby Mathew 	}
630727e5238SSoby Mathew 	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
631727e5238SSoby Mathew 
632727e5238SSoby Mathew 	ep->pc = entrypoint;
63332f0d3c6SDouglas Raillard 	zeromem(&ep->args, sizeof(ep->args));
634727e5238SSoby Mathew 	ep->args.arg0 = context_id;
635727e5238SSoby Mathew 
636727e5238SSoby Mathew 	mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
637727e5238SSoby Mathew 
638727e5238SSoby Mathew 	/*
639727e5238SSoby Mathew 	 * TODO: Choose async. exception bits if HYP mode is not
640727e5238SSoby Mathew 	 * implemented according to the values of SCR.{AW, FW} bits
641727e5238SSoby Mathew 	 */
642727e5238SSoby Mathew 	aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
643727e5238SSoby Mathew 
644727e5238SSoby Mathew 	ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
645727e5238SSoby Mathew 
646727e5238SSoby Mathew 	return PSCI_E_SUCCESS;
647727e5238SSoby Mathew }
648727e5238SSoby Mathew 
649727e5238SSoby Mathew #else
650532ed618SSoby Mathew static int psci_get_ns_ep_info(entry_point_info_t *ep,
651532ed618SSoby Mathew 			       uintptr_t entrypoint,
652532ed618SSoby Mathew 			       u_register_t context_id)
653532ed618SSoby Mathew {
654532ed618SSoby Mathew 	u_register_t ep_attr, sctlr;
655532ed618SSoby Mathew 	unsigned int daif, ee, mode;
656532ed618SSoby Mathew 	u_register_t ns_scr_el3 = read_scr_el3();
657532ed618SSoby Mathew 	u_register_t ns_sctlr_el1 = read_sctlr_el1();
658532ed618SSoby Mathew 
659532ed618SSoby Mathew 	sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1;
660532ed618SSoby Mathew 	ee = 0;
661532ed618SSoby Mathew 
662532ed618SSoby Mathew 	ep_attr = NON_SECURE | EP_ST_DISABLE;
663532ed618SSoby Mathew 	if (sctlr & SCTLR_EE_BIT) {
664532ed618SSoby Mathew 		ep_attr |= EP_EE_BIG;
665532ed618SSoby Mathew 		ee = 1;
666532ed618SSoby Mathew 	}
667532ed618SSoby Mathew 	SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
668532ed618SSoby Mathew 
669532ed618SSoby Mathew 	ep->pc = entrypoint;
67032f0d3c6SDouglas Raillard 	zeromem(&ep->args, sizeof(ep->args));
671532ed618SSoby Mathew 	ep->args.arg0 = context_id;
672532ed618SSoby Mathew 
673532ed618SSoby Mathew 	/*
674532ed618SSoby Mathew 	 * Figure out whether the cpu enters the non-secure address space
675532ed618SSoby Mathew 	 * in aarch32 or aarch64
676532ed618SSoby Mathew 	 */
677532ed618SSoby Mathew 	if (ns_scr_el3 & SCR_RW_BIT) {
678532ed618SSoby Mathew 
679532ed618SSoby Mathew 		/*
680532ed618SSoby Mathew 		 * Check whether a Thumb entry point has been provided for an
681532ed618SSoby Mathew 		 * aarch64 EL
682532ed618SSoby Mathew 		 */
683532ed618SSoby Mathew 		if (entrypoint & 0x1)
684532ed618SSoby Mathew 			return PSCI_E_INVALID_ADDRESS;
685532ed618SSoby Mathew 
686532ed618SSoby Mathew 		mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1;
687532ed618SSoby Mathew 
688532ed618SSoby Mathew 		ep->spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
689532ed618SSoby Mathew 	} else {
690532ed618SSoby Mathew 
691532ed618SSoby Mathew 		mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
692532ed618SSoby Mathew 
693532ed618SSoby Mathew 		/*
694532ed618SSoby Mathew 		 * TODO: Choose async. exception bits if HYP mode is not
695532ed618SSoby Mathew 		 * implemented according to the values of SCR.{AW, FW} bits
696532ed618SSoby Mathew 		 */
697532ed618SSoby Mathew 		daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
698532ed618SSoby Mathew 
699532ed618SSoby Mathew 		ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif);
700532ed618SSoby Mathew 	}
701532ed618SSoby Mathew 
702532ed618SSoby Mathew 	return PSCI_E_SUCCESS;
703532ed618SSoby Mathew }
704727e5238SSoby Mathew #endif
705532ed618SSoby Mathew 
706532ed618SSoby Mathew /*******************************************************************************
707532ed618SSoby Mathew  * This function validates the entrypoint with the platform layer if the
708532ed618SSoby Mathew  * appropriate pm_ops hook is exported by the platform and returns the
709532ed618SSoby Mathew  * 'entry_point_info'.
710532ed618SSoby Mathew  ******************************************************************************/
711532ed618SSoby Mathew int psci_validate_entry_point(entry_point_info_t *ep,
712532ed618SSoby Mathew 			      uintptr_t entrypoint,
713532ed618SSoby Mathew 			      u_register_t context_id)
714532ed618SSoby Mathew {
715532ed618SSoby Mathew 	int rc;
716532ed618SSoby Mathew 
717532ed618SSoby Mathew 	/* Validate the entrypoint using platform psci_ops */
718532ed618SSoby Mathew 	if (psci_plat_pm_ops->validate_ns_entrypoint) {
719532ed618SSoby Mathew 		rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
720532ed618SSoby Mathew 		if (rc != PSCI_E_SUCCESS)
721532ed618SSoby Mathew 			return PSCI_E_INVALID_ADDRESS;
722532ed618SSoby Mathew 	}
723532ed618SSoby Mathew 
724532ed618SSoby Mathew 	/*
725532ed618SSoby Mathew 	 * Verify and derive the re-entry information for
726532ed618SSoby Mathew 	 * the non-secure world from the non-secure state from
727532ed618SSoby Mathew 	 * where this call originated.
728532ed618SSoby Mathew 	 */
729532ed618SSoby Mathew 	rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
730532ed618SSoby Mathew 	return rc;
731532ed618SSoby Mathew }
732532ed618SSoby Mathew 
733532ed618SSoby Mathew /*******************************************************************************
734532ed618SSoby Mathew  * Generic handler which is called when a cpu is physically powered on. It
735532ed618SSoby Mathew  * traverses the node information and finds the highest power level powered
736532ed618SSoby Mathew  * off and performs generic, architectural, platform setup and state management
737532ed618SSoby Mathew  * to power on that power level and power levels below it.
738532ed618SSoby Mathew  * e.g. For a cpu that's been powered on, it will call the platform specific
739532ed618SSoby Mathew  * code to enable the gic cpu interface and for a cluster it will enable
740532ed618SSoby Mathew  * coherency at the interconnect level in addition to gic cpu interface.
741532ed618SSoby Mathew  ******************************************************************************/
742cf0b1492SSoby Mathew void psci_warmboot_entrypoint(void)
743532ed618SSoby Mathew {
744532ed618SSoby Mathew 	unsigned int end_pwrlvl, cpu_idx = plat_my_core_pos();
745532ed618SSoby Mathew 	psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
746532ed618SSoby Mathew 
747532ed618SSoby Mathew 	/*
748532ed618SSoby Mathew 	 * Verify that we have been explicitly turned ON or resumed from
749532ed618SSoby Mathew 	 * suspend.
750532ed618SSoby Mathew 	 */
751532ed618SSoby Mathew 	if (psci_get_aff_info_state() == AFF_STATE_OFF) {
752532ed618SSoby Mathew 		ERROR("Unexpected affinity info state");
753532ed618SSoby Mathew 		panic();
754532ed618SSoby Mathew 	}
755532ed618SSoby Mathew 
756532ed618SSoby Mathew 	/*
757532ed618SSoby Mathew 	 * Get the maximum power domain level to traverse to after this cpu
758532ed618SSoby Mathew 	 * has been physically powered up.
759532ed618SSoby Mathew 	 */
760532ed618SSoby Mathew 	end_pwrlvl = get_power_on_target_pwrlvl();
761532ed618SSoby Mathew 
762532ed618SSoby Mathew 	/*
763532ed618SSoby Mathew 	 * This function acquires the lock corresponding to each power level so
764532ed618SSoby Mathew 	 * that by the time all locks are taken, the system topology is snapshot
765532ed618SSoby Mathew 	 * and state management can be done safely.
766532ed618SSoby Mathew 	 */
767532ed618SSoby Mathew 	psci_acquire_pwr_domain_locks(end_pwrlvl,
768532ed618SSoby Mathew 				      cpu_idx);
769532ed618SSoby Mathew 
770532ed618SSoby Mathew #if ENABLE_PSCI_STAT
77104c1db1eSdp-arm 	plat_psci_stat_accounting_stop(&state_info);
772532ed618SSoby Mathew #endif
773532ed618SSoby Mathew 
774532ed618SSoby Mathew 	psci_get_target_local_pwr_states(end_pwrlvl, &state_info);
775532ed618SSoby Mathew 
776532ed618SSoby Mathew 	/*
777532ed618SSoby Mathew 	 * This CPU could be resuming from suspend or it could have just been
778532ed618SSoby Mathew 	 * turned on. To distinguish between these 2 cases, we examine the
779532ed618SSoby Mathew 	 * affinity state of the CPU:
780532ed618SSoby Mathew 	 *  - If the affinity state is ON_PENDING then it has just been
781532ed618SSoby Mathew 	 *    turned on.
782532ed618SSoby Mathew 	 *  - Else it is resuming from suspend.
783532ed618SSoby Mathew 	 *
784532ed618SSoby Mathew 	 * Depending on the type of warm reset identified, choose the right set
785532ed618SSoby Mathew 	 * of power management handler and perform the generic, architecture
786532ed618SSoby Mathew 	 * and platform specific handling.
787532ed618SSoby Mathew 	 */
788532ed618SSoby Mathew 	if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING)
789532ed618SSoby Mathew 		psci_cpu_on_finish(cpu_idx, &state_info);
790532ed618SSoby Mathew 	else
791532ed618SSoby Mathew 		psci_cpu_suspend_finish(cpu_idx, &state_info);
792532ed618SSoby Mathew 
793532ed618SSoby Mathew 	/*
794532ed618SSoby Mathew 	 * Set the requested and target state of this CPU and all the higher
795532ed618SSoby Mathew 	 * power domains which are ancestors of this CPU to run.
796532ed618SSoby Mathew 	 */
797532ed618SSoby Mathew 	psci_set_pwr_domains_to_run(end_pwrlvl);
798532ed618SSoby Mathew 
799532ed618SSoby Mathew #if ENABLE_PSCI_STAT
800532ed618SSoby Mathew 	/*
801532ed618SSoby Mathew 	 * Update PSCI stats.
802532ed618SSoby Mathew 	 * Caches are off when writing stats data on the power down path.
803532ed618SSoby Mathew 	 * Since caches are now enabled, it's necessary to do cache
804532ed618SSoby Mathew 	 * maintenance before reading that same data.
805532ed618SSoby Mathew 	 */
80604c1db1eSdp-arm 	psci_stats_update_pwr_up(end_pwrlvl, &state_info);
807532ed618SSoby Mathew #endif
808532ed618SSoby Mathew 
809532ed618SSoby Mathew 	/*
810532ed618SSoby Mathew 	 * This loop releases the lock corresponding to each power level
811532ed618SSoby Mathew 	 * in the reverse order to which they were acquired.
812532ed618SSoby Mathew 	 */
813532ed618SSoby Mathew 	psci_release_pwr_domain_locks(end_pwrlvl,
814532ed618SSoby Mathew 				      cpu_idx);
815532ed618SSoby Mathew }
816532ed618SSoby Mathew 
817532ed618SSoby Mathew /*******************************************************************************
818532ed618SSoby Mathew  * This function initializes the set of hooks that PSCI invokes as part of power
819532ed618SSoby Mathew  * management operation. The power management hooks are expected to be provided
820532ed618SSoby Mathew  * by the SPD, after it finishes all its initialization
821532ed618SSoby Mathew  ******************************************************************************/
822532ed618SSoby Mathew void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
823532ed618SSoby Mathew {
824532ed618SSoby Mathew 	assert(pm);
825532ed618SSoby Mathew 	psci_spd_pm = pm;
826532ed618SSoby Mathew 
827532ed618SSoby Mathew 	if (pm->svc_migrate)
828532ed618SSoby Mathew 		psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
829532ed618SSoby Mathew 
830532ed618SSoby Mathew 	if (pm->svc_migrate_info)
831532ed618SSoby Mathew 		psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
832532ed618SSoby Mathew 				| define_psci_cap(PSCI_MIG_INFO_TYPE);
833532ed618SSoby Mathew }
834532ed618SSoby Mathew 
835532ed618SSoby Mathew /*******************************************************************************
836532ed618SSoby Mathew  * This function invokes the migrate info hook in the spd_pm_ops. It performs
837532ed618SSoby Mathew  * the necessary return value validation. If the Secure Payload is UP and
838532ed618SSoby Mathew  * migrate capable, it returns the mpidr of the CPU on which the Secure payload
839532ed618SSoby Mathew  * is resident through the mpidr parameter. Else the value of the parameter on
840532ed618SSoby Mathew  * return is undefined.
841532ed618SSoby Mathew  ******************************************************************************/
842532ed618SSoby Mathew int psci_spd_migrate_info(u_register_t *mpidr)
843532ed618SSoby Mathew {
844532ed618SSoby Mathew 	int rc;
845532ed618SSoby Mathew 
846532ed618SSoby Mathew 	if (!psci_spd_pm || !psci_spd_pm->svc_migrate_info)
847532ed618SSoby Mathew 		return PSCI_E_NOT_SUPPORTED;
848532ed618SSoby Mathew 
849532ed618SSoby Mathew 	rc = psci_spd_pm->svc_migrate_info(mpidr);
850532ed618SSoby Mathew 
851532ed618SSoby Mathew 	assert(rc == PSCI_TOS_UP_MIG_CAP || rc == PSCI_TOS_NOT_UP_MIG_CAP \
852532ed618SSoby Mathew 		|| rc == PSCI_TOS_NOT_PRESENT_MP || rc == PSCI_E_NOT_SUPPORTED);
853532ed618SSoby Mathew 
854532ed618SSoby Mathew 	return rc;
855532ed618SSoby Mathew }
856532ed618SSoby Mathew 
857532ed618SSoby Mathew 
858532ed618SSoby Mathew /*******************************************************************************
859532ed618SSoby Mathew  * This function prints the state of all power domains present in the
860532ed618SSoby Mathew  * system
861532ed618SSoby Mathew  ******************************************************************************/
862532ed618SSoby Mathew void psci_print_power_domain_map(void)
863532ed618SSoby Mathew {
864532ed618SSoby Mathew #if LOG_LEVEL >= LOG_LEVEL_INFO
865532ed618SSoby Mathew 	unsigned int idx;
866532ed618SSoby Mathew 	plat_local_state_t state;
867532ed618SSoby Mathew 	plat_local_state_type_t state_type;
868532ed618SSoby Mathew 
869532ed618SSoby Mathew 	/* This array maps to the PSCI_STATE_X definitions in psci.h */
870532ed618SSoby Mathew 	static const char * const psci_state_type_str[] = {
871532ed618SSoby Mathew 		"ON",
872532ed618SSoby Mathew 		"RETENTION",
873532ed618SSoby Mathew 		"OFF",
874532ed618SSoby Mathew 	};
875532ed618SSoby Mathew 
876532ed618SSoby Mathew 	INFO("PSCI Power Domain Map:\n");
877532ed618SSoby Mathew 	for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - PLATFORM_CORE_COUNT);
878532ed618SSoby Mathew 							idx++) {
879532ed618SSoby Mathew 		state_type = find_local_state_type(
880532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].local_state);
881532ed618SSoby Mathew 		INFO("  Domain Node : Level %u, parent_node %d,"
882532ed618SSoby Mathew 				" State %s (0x%x)\n",
883532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].level,
884532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].parent_node,
885532ed618SSoby Mathew 				psci_state_type_str[state_type],
886532ed618SSoby Mathew 				psci_non_cpu_pd_nodes[idx].local_state);
887532ed618SSoby Mathew 	}
888532ed618SSoby Mathew 
889532ed618SSoby Mathew 	for (idx = 0; idx < PLATFORM_CORE_COUNT; idx++) {
890532ed618SSoby Mathew 		state = psci_get_cpu_local_state_by_idx(idx);
891532ed618SSoby Mathew 		state_type = find_local_state_type(state);
892532ed618SSoby Mathew 		INFO("  CPU Node : MPID 0x%llx, parent_node %d,"
893532ed618SSoby Mathew 				" State %s (0x%x)\n",
894532ed618SSoby Mathew 				(unsigned long long)psci_cpu_pd_nodes[idx].mpidr,
895532ed618SSoby Mathew 				psci_cpu_pd_nodes[idx].parent_node,
896532ed618SSoby Mathew 				psci_state_type_str[state_type],
897532ed618SSoby Mathew 				psci_get_cpu_local_state_by_idx(idx));
898532ed618SSoby Mathew 	}
899532ed618SSoby Mathew #endif
900532ed618SSoby Mathew }
901532ed618SSoby Mathew 
902b10d4499SJeenu Viswambharan /******************************************************************************
903b10d4499SJeenu Viswambharan  * Return whether any secondaries were powered up with CPU_ON call. A CPU that
904b10d4499SJeenu Viswambharan  * have ever been powered up would have set its MPDIR value to something other
905b10d4499SJeenu Viswambharan  * than PSCI_INVALID_MPIDR. Note that MPDIR isn't reset back to
906b10d4499SJeenu Viswambharan  * PSCI_INVALID_MPIDR when a CPU is powered down later, so the return value is
907b10d4499SJeenu Viswambharan  * meaningful only when called on the primary CPU during early boot.
908b10d4499SJeenu Viswambharan  *****************************************************************************/
909b10d4499SJeenu Viswambharan int psci_secondaries_brought_up(void)
910b10d4499SJeenu Viswambharan {
9116311f63dSVarun Wadekar 	unsigned int idx, n_valid = 0;
912b10d4499SJeenu Viswambharan 
913b10d4499SJeenu Viswambharan 	for (idx = 0; idx < ARRAY_SIZE(psci_cpu_pd_nodes); idx++) {
914b10d4499SJeenu Viswambharan 		if (psci_cpu_pd_nodes[idx].mpidr != PSCI_INVALID_MPIDR)
915b10d4499SJeenu Viswambharan 			n_valid++;
916b10d4499SJeenu Viswambharan 	}
917b10d4499SJeenu Viswambharan 
918b10d4499SJeenu Viswambharan 	assert(n_valid);
919b10d4499SJeenu Viswambharan 
920b10d4499SJeenu Viswambharan 	return (n_valid > 1);
921b10d4499SJeenu Viswambharan }
922b10d4499SJeenu Viswambharan 
923532ed618SSoby Mathew #if ENABLE_PLAT_COMPAT
924532ed618SSoby Mathew /*******************************************************************************
925532ed618SSoby Mathew  * PSCI Compatibility helper function to return the 'power_state' parameter of
926532ed618SSoby Mathew  * the PSCI CPU SUSPEND request for the current CPU. Returns PSCI_INVALID_DATA
927532ed618SSoby Mathew  * if not invoked within CPU_SUSPEND for the current CPU.
928532ed618SSoby Mathew  ******************************************************************************/
929532ed618SSoby Mathew int psci_get_suspend_powerstate(void)
930532ed618SSoby Mathew {
931532ed618SSoby Mathew 	/* Sanity check to verify that CPU is within CPU_SUSPEND */
932532ed618SSoby Mathew 	if (psci_get_aff_info_state() == AFF_STATE_ON &&
933532ed618SSoby Mathew 		!is_local_state_run(psci_get_cpu_local_state()))
934532ed618SSoby Mathew 		return psci_power_state_compat[plat_my_core_pos()];
935532ed618SSoby Mathew 
936532ed618SSoby Mathew 	return PSCI_INVALID_DATA;
937532ed618SSoby Mathew }
938532ed618SSoby Mathew 
939532ed618SSoby Mathew /*******************************************************************************
940532ed618SSoby Mathew  * PSCI Compatibility helper function to return the state id of the current
941532ed618SSoby Mathew  * cpu encoded in the 'power_state' parameter. Returns PSCI_INVALID_DATA
942532ed618SSoby Mathew  * if not invoked within CPU_SUSPEND for the current CPU.
943532ed618SSoby Mathew  ******************************************************************************/
944532ed618SSoby Mathew int psci_get_suspend_stateid(void)
945532ed618SSoby Mathew {
946532ed618SSoby Mathew 	unsigned int power_state;
947532ed618SSoby Mathew 	power_state = psci_get_suspend_powerstate();
948532ed618SSoby Mathew 	if (power_state != PSCI_INVALID_DATA)
949532ed618SSoby Mathew 		return psci_get_pstate_id(power_state);
950532ed618SSoby Mathew 
951532ed618SSoby Mathew 	return PSCI_INVALID_DATA;
952532ed618SSoby Mathew }
953532ed618SSoby Mathew 
954532ed618SSoby Mathew /*******************************************************************************
955532ed618SSoby Mathew  * PSCI Compatibility helper function to return the state id encoded in the
956532ed618SSoby Mathew  * 'power_state' parameter of the CPU specified by 'mpidr'. Returns
957532ed618SSoby Mathew  * PSCI_INVALID_DATA if the CPU is not in CPU_SUSPEND.
958532ed618SSoby Mathew  ******************************************************************************/
959532ed618SSoby Mathew int psci_get_suspend_stateid_by_mpidr(unsigned long mpidr)
960532ed618SSoby Mathew {
961532ed618SSoby Mathew 	int cpu_idx = plat_core_pos_by_mpidr(mpidr);
962532ed618SSoby Mathew 
963532ed618SSoby Mathew 	if (cpu_idx == -1)
964532ed618SSoby Mathew 		return PSCI_INVALID_DATA;
965532ed618SSoby Mathew 
966532ed618SSoby Mathew 	/* Sanity check to verify that the CPU is in CPU_SUSPEND */
967532ed618SSoby Mathew 	if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_ON &&
968532ed618SSoby Mathew 		!is_local_state_run(psci_get_cpu_local_state_by_idx(cpu_idx)))
969532ed618SSoby Mathew 		return psci_get_pstate_id(psci_power_state_compat[cpu_idx]);
970532ed618SSoby Mathew 
971532ed618SSoby Mathew 	return PSCI_INVALID_DATA;
972532ed618SSoby Mathew }
973532ed618SSoby Mathew 
974532ed618SSoby Mathew /*******************************************************************************
975532ed618SSoby Mathew  * This function returns highest affinity level which is in OFF
976532ed618SSoby Mathew  * state. The affinity instance with which the level is associated is
977532ed618SSoby Mathew  * determined by the caller.
978532ed618SSoby Mathew  ******************************************************************************/
979532ed618SSoby Mathew unsigned int psci_get_max_phys_off_afflvl(void)
980532ed618SSoby Mathew {
981532ed618SSoby Mathew 	psci_power_state_t state_info;
982532ed618SSoby Mathew 
98332f0d3c6SDouglas Raillard 	zeromem(&state_info, sizeof(state_info));
984532ed618SSoby Mathew 	psci_get_target_local_pwr_states(PLAT_MAX_PWR_LVL, &state_info);
985532ed618SSoby Mathew 
986532ed618SSoby Mathew 	return psci_find_target_suspend_lvl(&state_info);
987532ed618SSoby Mathew }
988532ed618SSoby Mathew 
989532ed618SSoby Mathew /*******************************************************************************
990532ed618SSoby Mathew  * PSCI Compatibility helper function to return target affinity level requested
991532ed618SSoby Mathew  * for the CPU_SUSPEND. This function assumes affinity levels correspond to
992532ed618SSoby Mathew  * power domain levels on the platform.
993532ed618SSoby Mathew  ******************************************************************************/
994532ed618SSoby Mathew int psci_get_suspend_afflvl(void)
995532ed618SSoby Mathew {
996532ed618SSoby Mathew 	return psci_get_suspend_pwrlvl();
997532ed618SSoby Mathew }
998532ed618SSoby Mathew 
999532ed618SSoby Mathew #endif
1000b0408e87SJeenu Viswambharan 
1001b0408e87SJeenu Viswambharan /*******************************************************************************
1002b0408e87SJeenu Viswambharan  * Initiate power down sequence, by calling power down operations registered for
1003b0408e87SJeenu Viswambharan  * this CPU.
1004b0408e87SJeenu Viswambharan  ******************************************************************************/
1005b0408e87SJeenu Viswambharan void psci_do_pwrdown_sequence(unsigned int power_level)
1006b0408e87SJeenu Viswambharan {
1007b0408e87SJeenu Viswambharan #if HW_ASSISTED_COHERENCY
1008b0408e87SJeenu Viswambharan 	/*
1009b0408e87SJeenu Viswambharan 	 * With hardware-assisted coherency, the CPU drivers only initiate the
1010b0408e87SJeenu Viswambharan 	 * power down sequence, without performing cache-maintenance operations
1011b0408e87SJeenu Viswambharan 	 * in software. Data caches and MMU remain enabled both before and after
1012b0408e87SJeenu Viswambharan 	 * this call.
1013b0408e87SJeenu Viswambharan 	 */
1014b0408e87SJeenu Viswambharan 	prepare_cpu_pwr_dwn(power_level);
1015b0408e87SJeenu Viswambharan #else
1016b0408e87SJeenu Viswambharan 	/*
1017b0408e87SJeenu Viswambharan 	 * Without hardware-assisted coherency, the CPU drivers disable data
1018b0408e87SJeenu Viswambharan 	 * caches and MMU, then perform cache-maintenance operations in
1019b0408e87SJeenu Viswambharan 	 * software.
1020b0408e87SJeenu Viswambharan 	 *
1021b0408e87SJeenu Viswambharan 	 * We ought to call prepare_cpu_pwr_dwn() to initiate power down
1022b0408e87SJeenu Viswambharan 	 * sequence. We currently have data caches and MMU enabled, but the
1023b0408e87SJeenu Viswambharan 	 * function will return with data caches and MMU disabled. We must
1024b0408e87SJeenu Viswambharan 	 * ensure that the stack memory is flushed out to memory before we start
1025b0408e87SJeenu Viswambharan 	 * popping from it again.
1026b0408e87SJeenu Viswambharan 	 */
1027b0408e87SJeenu Viswambharan 	psci_do_pwrdown_cache_maintenance(power_level);
1028b0408e87SJeenu Viswambharan #endif
1029b0408e87SJeenu Viswambharan }
1030