blob: 026690d25f0f87534919e32187e3e337dced6062 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
dp-arm04c1db12017-01-31 13:01:04 +00002 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
Dan Handley97043ac2014-04-09 13:14:54 +010031#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010032#include <arch_helpers.h>
Dan Handley97043ac2014-04-09 13:14:54 +010033#include <assert.h>
34#include <bl_common.h>
35#include <context.h>
Jeenu Viswambharancaa84932014-02-06 10:36:15 +000036#include <context_mgmt.h>
Dan Handley35e98e52014-04-09 13:13:04 +010037#include <debug.h>
Dan Handley5f0cdb02014-05-14 17:44:19 +010038#include <platform.h>
Andrew Thoelke167a9352014-06-04 21:10:52 +010039#include <string.h>
Douglas Raillard32f0d3c2017-01-26 15:54:44 +000040#include <utils.h>
Dan Handley35e98e52014-04-09 13:13:04 +010041#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010042
Achin Gupta607084e2014-02-09 18:24:19 +000043/*
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000044 * SPD power management operations, expected to be supplied by the registered
45 * SPD on successful SP initialization
Achin Gupta607084e2014-02-09 18:24:19 +000046 */
Dan Handleyfb037bf2014-04-10 15:37:22 +010047const spd_pm_ops_t *psci_spd_pm;
Achin Gupta607084e2014-02-09 18:24:19 +000048
Soby Mathew67487842015-07-13 14:10:57 +010049/*
50 * PSCI requested local power state map. This array is used to store the local
51 * power states requested by a CPU for power levels from level 1 to
52 * PLAT_MAX_PWR_LVL. It does not store the requested local power state for power
53 * level 0 (PSCI_CPU_PWR_LVL) as the requested and the target power state for a
54 * CPU are the same.
55 *
56 * During state coordination, the platform is passed an array containing the
57 * local states requested for a particular non cpu power domain by each cpu
58 * within the domain.
59 *
60 * TODO: Dense packing of the requested states will cause cache thrashing
61 * when multiple power domains write to it. If we allocate the requested
62 * states at each power level in a cache-line aligned per-domain memory,
63 * the cache thrashing can be avoided.
64 */
65static plat_local_state_t
66 psci_req_local_pwr_states[PLAT_MAX_PWR_LVL][PLATFORM_CORE_COUNT];
67
68
Achin Gupta4f6ad662013-10-25 09:08:21 +010069/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +010070 * Arrays that hold the platform's power domain tree information for state
71 * management of power domains.
72 * Each node in the array 'psci_non_cpu_pd_nodes' corresponds to a power domain
73 * which is an ancestor of a CPU power domain.
74 * Each node in the array 'psci_cpu_pd_nodes' corresponds to a cpu power domain
Achin Gupta4f6ad662013-10-25 09:08:21 +010075 ******************************************************************************/
Soby Mathew67487842015-07-13 14:10:57 +010076non_cpu_pd_node_t psci_non_cpu_pd_nodes[PSCI_NUM_NON_CPU_PWR_DOMAINS]
Soby Mathewab8707e2015-01-08 18:02:44 +000077#if USE_COHERENT_MEM
Soren Brinkmann65cd2992016-01-14 10:11:05 -080078__section("tzfw_coherent_mem")
Soby Mathewab8707e2015-01-08 18:02:44 +000079#endif
80;
Achin Gupta4f6ad662013-10-25 09:08:21 +010081
Andrew Thoelkeee7b35c2015-09-10 11:39:36 +010082DEFINE_BAKERY_LOCK(psci_locks[PSCI_NUM_NON_CPU_PWR_DOMAINS]);
83
Soby Mathew67487842015-07-13 14:10:57 +010084cpu_pd_node_t psci_cpu_pd_nodes[PLATFORM_CORE_COUNT];
85
Achin Gupta4f6ad662013-10-25 09:08:21 +010086/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +010087 * Pointer to functions exported by the platform to complete power mgmt. ops
88 ******************************************************************************/
Soby Mathew67487842015-07-13 14:10:57 +010089const plat_psci_ops_t *psci_plat_pm_ops;
Achin Gupta4f6ad662013-10-25 09:08:21 +010090
Soby Mathew67487842015-07-13 14:10:57 +010091/******************************************************************************
92 * Check that the maximum power level supported by the platform makes sense
93 *****************************************************************************/
94CASSERT(PLAT_MAX_PWR_LVL <= PSCI_MAX_PWR_LVL && \
95 PLAT_MAX_PWR_LVL >= PSCI_CPU_PWR_LVL, \
96 assert_platform_max_pwrlvl_check);
Soby Mathew8c32bc22015-02-12 14:45:02 +000097
Soby Mathew67487842015-07-13 14:10:57 +010098/*
99 * The plat_local_state used by the platform is one of these types: RUN,
100 * RETENTION and OFF. The platform can define further sub-states for each type
101 * apart from RUN. This categorization is done to verify the sanity of the
102 * psci_power_state passed by the platform and to print debug information. The
103 * categorization is done on the basis of the following conditions:
104 *
105 * 1. If (plat_local_state == 0) then the category is STATE_TYPE_RUN.
106 *
107 * 2. If (0 < plat_local_state <= PLAT_MAX_RET_STATE), then the category is
108 * STATE_TYPE_RETN.
109 *
110 * 3. If (plat_local_state > PLAT_MAX_RET_STATE), then the category is
111 * STATE_TYPE_OFF.
112 */
113typedef enum plat_local_state_type {
114 STATE_TYPE_RUN = 0,
115 STATE_TYPE_RETN,
116 STATE_TYPE_OFF
117} plat_local_state_type_t;
118
119/* The macro used to categorize plat_local_state. */
120#define find_local_state_type(plat_local_state) \
121 ((plat_local_state) ? ((plat_local_state > PLAT_MAX_RET_STATE) \
122 ? STATE_TYPE_OFF : STATE_TYPE_RETN) \
123 : STATE_TYPE_RUN)
124
125/******************************************************************************
126 * Check that the maximum retention level supported by the platform is less
127 * than the maximum off level.
128 *****************************************************************************/
129CASSERT(PLAT_MAX_RET_STATE < PLAT_MAX_OFF_STATE, \
130 assert_platform_max_off_and_retn_state_check);
131
132/******************************************************************************
133 * This function ensures that the power state parameter in a CPU_SUSPEND request
134 * is valid. If so, it returns the requested states for each power level.
135 *****************************************************************************/
136int psci_validate_power_state(unsigned int power_state,
137 psci_power_state_t *state_info)
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100138{
Soby Mathew67487842015-07-13 14:10:57 +0100139 /* Check SBZ bits in power state are zero */
140 if (psci_check_power_state(power_state))
141 return PSCI_E_INVALID_PARAMS;
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100142
Soby Mathew67487842015-07-13 14:10:57 +0100143 assert(psci_plat_pm_ops->validate_power_state);
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100144
Soby Mathew67487842015-07-13 14:10:57 +0100145 /* Validate the power_state using platform pm_ops */
146 return psci_plat_pm_ops->validate_power_state(power_state, state_info);
147}
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100148
Soby Mathew67487842015-07-13 14:10:57 +0100149/******************************************************************************
150 * This function retrieves the `psci_power_state_t` for system suspend from
151 * the platform.
152 *****************************************************************************/
153void psci_query_sys_suspend_pwrstate(psci_power_state_t *state_info)
154{
155 /*
156 * Assert that the required pm_ops hook is implemented to ensure that
157 * the capability detected during psci_setup() is valid.
158 */
159 assert(psci_plat_pm_ops->get_sys_suspend_power_state);
160
161 /*
162 * Query the platform for the power_state required for system suspend
163 */
164 psci_plat_pm_ops->get_sys_suspend_power_state(state_info);
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100165}
166
167/*******************************************************************************
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000168 * This function verifies that the all the other cores in the system have been
169 * turned OFF and the current CPU is the last running CPU in the system.
170 * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
171 * otherwise.
172 ******************************************************************************/
173unsigned int psci_is_last_on_cpu(void)
174{
Soby Mathew67487842015-07-13 14:10:57 +0100175 unsigned int cpu_idx, my_idx = plat_my_core_pos();
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000176
Soby Mathew67487842015-07-13 14:10:57 +0100177 for (cpu_idx = 0; cpu_idx < PLATFORM_CORE_COUNT; cpu_idx++) {
178 if (cpu_idx == my_idx) {
179 assert(psci_get_aff_info_state() == AFF_STATE_ON);
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000180 continue;
181 }
182
Soby Mathew67487842015-07-13 14:10:57 +0100183 if (psci_get_aff_info_state_by_idx(cpu_idx) != AFF_STATE_OFF)
Soby Mathewc0aff0e2014-12-17 14:47:57 +0000184 return 0;
185 }
186
187 return 1;
188}
189
190/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +0100191 * Routine to return the maximum power level to traverse to after a cpu has
Achin Guptaa45e3972013-12-05 15:10:48 +0000192 * been physically powered up. It is expected to be called immediately after
Achin Gupta776b68a2014-07-25 14:52:47 +0100193 * reset from assembler code.
Achin Guptaa45e3972013-12-05 15:10:48 +0000194 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100195static unsigned int get_power_on_target_pwrlvl(void)
Achin Guptaa45e3972013-12-05 15:10:48 +0000196{
Soby Mathew9d070b92015-07-29 17:05:03 +0100197 unsigned int pwrlvl;
Achin Guptaa45e3972013-12-05 15:10:48 +0000198
199 /*
Soby Mathew67487842015-07-13 14:10:57 +0100200 * Assume that this cpu was suspended and retrieve its target power
Achin Gupta776b68a2014-07-25 14:52:47 +0100201 * level. If it is invalid then it could only have been turned off
Soby Mathew67487842015-07-13 14:10:57 +0100202 * earlier. PLAT_MAX_PWR_LVL will be the highest power level a
Achin Gupta776b68a2014-07-25 14:52:47 +0100203 * cpu can be turned off to.
204 */
Soby Mathew67487842015-07-13 14:10:57 +0100205 pwrlvl = psci_get_suspend_pwrlvl();
Soby Mathew9d070b92015-07-29 17:05:03 +0100206 if (pwrlvl == PSCI_INVALID_PWR_LVL)
Soby Mathew67487842015-07-13 14:10:57 +0100207 pwrlvl = PLAT_MAX_PWR_LVL;
208 return pwrlvl;
Achin Guptaa45e3972013-12-05 15:10:48 +0000209}
210
Soby Mathew67487842015-07-13 14:10:57 +0100211/******************************************************************************
212 * Helper function to update the requested local power state array. This array
213 * does not store the requested state for the CPU power level. Hence an
214 * assertion is added to prevent us from accessing the wrong index.
215 *****************************************************************************/
216static void psci_set_req_local_pwr_state(unsigned int pwrlvl,
217 unsigned int cpu_idx,
218 plat_local_state_t req_pwr_state)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100219{
Soby Mathew67487842015-07-13 14:10:57 +0100220 assert(pwrlvl > PSCI_CPU_PWR_LVL);
221 psci_req_local_pwr_states[pwrlvl - 1][cpu_idx] = req_pwr_state;
222}
Achin Gupta4f6ad662013-10-25 09:08:21 +0100223
Soby Mathew67487842015-07-13 14:10:57 +0100224/******************************************************************************
225 * This function initializes the psci_req_local_pwr_states.
226 *****************************************************************************/
227void psci_init_req_local_pwr_states(void)
228{
229 /* Initialize the requested state of all non CPU power domains as OFF */
230 memset(&psci_req_local_pwr_states, PLAT_MAX_OFF_STATE,
231 sizeof(psci_req_local_pwr_states));
232}
233
234/******************************************************************************
235 * Helper function to return a reference to an array containing the local power
236 * states requested by each cpu for a power domain at 'pwrlvl'. The size of the
237 * array will be the number of cpu power domains of which this power domain is
238 * an ancestor. These requested states will be used to determine a suitable
239 * target state for this power domain during psci state coordination. An
240 * assertion is added to prevent us from accessing the CPU power level.
241 *****************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100242static plat_local_state_t *psci_get_req_local_pwr_states(unsigned int pwrlvl,
243 unsigned int cpu_idx)
Soby Mathew67487842015-07-13 14:10:57 +0100244{
245 assert(pwrlvl > PSCI_CPU_PWR_LVL);
246
247 return &psci_req_local_pwr_states[pwrlvl - 1][cpu_idx];
248}
249
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000250/*
251 * psci_non_cpu_pd_nodes can be placed either in normal memory or coherent
252 * memory.
253 *
254 * With !USE_COHERENT_MEM, psci_non_cpu_pd_nodes is placed in normal memory,
255 * it's accessed by both cached and non-cached participants. To serve the common
256 * minimum, perform a cache flush before read and after write so that non-cached
257 * participants operate on latest data in main memory.
258 *
259 * When USE_COHERENT_MEM is used, psci_non_cpu_pd_nodes is placed in coherent
260 * memory. With HW_ASSISTED_COHERENCY, all PSCI participants are cache-coherent.
261 * In both cases, no cache operations are required.
262 */
263
264/*
265 * Retrieve local state of non-CPU power domain node from a non-cached CPU,
266 * after any required cache maintenance operation.
267 */
268static plat_local_state_t get_non_cpu_pd_node_local_state(
269 unsigned int parent_idx)
270{
271#if !USE_COHERENT_MEM || !HW_ASSISTED_COHERENCY
272 flush_dcache_range(
273 (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
274 sizeof(psci_non_cpu_pd_nodes[parent_idx]));
275#endif
276 return psci_non_cpu_pd_nodes[parent_idx].local_state;
277}
278
279/*
280 * Update local state of non-CPU power domain node from a cached CPU; perform
281 * any required cache maintenance operation afterwards.
282 */
283static void set_non_cpu_pd_node_local_state(unsigned int parent_idx,
284 plat_local_state_t state)
285{
286 psci_non_cpu_pd_nodes[parent_idx].local_state = state;
287#if !USE_COHERENT_MEM || !HW_ASSISTED_COHERENCY
288 flush_dcache_range(
289 (uintptr_t) &psci_non_cpu_pd_nodes[parent_idx],
290 sizeof(psci_non_cpu_pd_nodes[parent_idx]));
291#endif
292}
293
Soby Mathew67487842015-07-13 14:10:57 +0100294/******************************************************************************
295 * Helper function to return the current local power state of each power domain
296 * from the current cpu power domain to its ancestor at the 'end_pwrlvl'. This
297 * function will be called after a cpu is powered on to find the local state
298 * each power domain has emerged from.
299 *****************************************************************************/
Achin Gupta61eae522016-06-28 16:46:15 +0100300void psci_get_target_local_pwr_states(unsigned int end_pwrlvl,
301 psci_power_state_t *target_state)
Soby Mathew67487842015-07-13 14:10:57 +0100302{
Soby Mathew9d070b92015-07-29 17:05:03 +0100303 unsigned int parent_idx, lvl;
Soby Mathew67487842015-07-13 14:10:57 +0100304 plat_local_state_t *pd_state = target_state->pwr_domain_state;
305
306 pd_state[PSCI_CPU_PWR_LVL] = psci_get_cpu_local_state();
307 parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
308
309 /* Copy the local power state from node to state_info */
310 for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) {
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000311 pd_state[lvl] = get_non_cpu_pd_node_local_state(parent_idx);
Soby Mathew67487842015-07-13 14:10:57 +0100312 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
313 }
314
315 /* Set the the higher levels to RUN */
316 for (; lvl <= PLAT_MAX_PWR_LVL; lvl++)
317 target_state->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
318}
319
320/******************************************************************************
321 * Helper function to set the target local power state that each power domain
322 * from the current cpu power domain to its ancestor at the 'end_pwrlvl' will
323 * enter. This function will be called after coordination of requested power
324 * states has been done for each power level.
325 *****************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100326static void psci_set_target_local_pwr_states(unsigned int end_pwrlvl,
Soby Mathew67487842015-07-13 14:10:57 +0100327 const psci_power_state_t *target_state)
328{
Soby Mathew9d070b92015-07-29 17:05:03 +0100329 unsigned int parent_idx, lvl;
Soby Mathew67487842015-07-13 14:10:57 +0100330 const plat_local_state_t *pd_state = target_state->pwr_domain_state;
331
332 psci_set_cpu_local_state(pd_state[PSCI_CPU_PWR_LVL]);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100333
334 /*
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000335 * Need to flush as local_state might be accessed with Data Cache
Soby Mathew67487842015-07-13 14:10:57 +0100336 * disabled during power on
Achin Gupta4f6ad662013-10-25 09:08:21 +0100337 */
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000338 psci_flush_cpu_data(psci_svc_cpu_data.local_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100339
Soby Mathew67487842015-07-13 14:10:57 +0100340 parent_idx = psci_cpu_pd_nodes[plat_my_core_pos()].parent_node;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100341
Soby Mathew67487842015-07-13 14:10:57 +0100342 /* Copy the local_state from state_info */
343 for (lvl = 1; lvl <= end_pwrlvl; lvl++) {
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000344 set_non_cpu_pd_node_local_state(parent_idx, pd_state[lvl]);
Soby Mathew67487842015-07-13 14:10:57 +0100345 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
346 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100347}
348
Soby Mathew67487842015-07-13 14:10:57 +0100349
Achin Gupta4f6ad662013-10-25 09:08:21 +0100350/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +0100351 * PSCI helper function to get the parent nodes corresponding to a cpu_index.
Achin Gupta0959db52013-12-02 17:33:04 +0000352 ******************************************************************************/
Soby Mathew67487842015-07-13 14:10:57 +0100353void psci_get_parent_pwr_domain_nodes(unsigned int cpu_idx,
Soby Mathew9d070b92015-07-29 17:05:03 +0100354 unsigned int end_lvl,
Soby Mathew67487842015-07-13 14:10:57 +0100355 unsigned int node_index[])
Achin Gupta0959db52013-12-02 17:33:04 +0000356{
Soby Mathew67487842015-07-13 14:10:57 +0100357 unsigned int parent_node = psci_cpu_pd_nodes[cpu_idx].parent_node;
358 int i;
359
360 for (i = PSCI_CPU_PWR_LVL + 1; i <= end_lvl; i++) {
361 *node_index++ = parent_node;
362 parent_node = psci_non_cpu_pd_nodes[parent_node].parent_node;
363 }
364}
365
366/******************************************************************************
367 * This function is invoked post CPU power up and initialization. It sets the
368 * affinity info state, target power state and requested power state for the
369 * current CPU and all its ancestor power domains to RUN.
370 *****************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100371void psci_set_pwr_domains_to_run(unsigned int end_pwrlvl)
Soby Mathew67487842015-07-13 14:10:57 +0100372{
Soby Mathew9d070b92015-07-29 17:05:03 +0100373 unsigned int parent_idx, cpu_idx = plat_my_core_pos(), lvl;
Soby Mathew67487842015-07-13 14:10:57 +0100374 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
375
376 /* Reset the local_state to RUN for the non cpu power domains. */
377 for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) {
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000378 set_non_cpu_pd_node_local_state(parent_idx,
379 PSCI_LOCAL_STATE_RUN);
Soby Mathew67487842015-07-13 14:10:57 +0100380 psci_set_req_local_pwr_state(lvl,
381 cpu_idx,
382 PSCI_LOCAL_STATE_RUN);
383 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
384 }
385
386 /* Set the affinity info state to ON */
387 psci_set_aff_info_state(AFF_STATE_ON);
388
389 psci_set_cpu_local_state(PSCI_LOCAL_STATE_RUN);
Jeenu Viswambharana10d3632017-01-06 14:58:11 +0000390 psci_flush_cpu_data(psci_svc_cpu_data);
Soby Mathew67487842015-07-13 14:10:57 +0100391}
392
393/******************************************************************************
394 * This function is passed the local power states requested for each power
395 * domain (state_info) between the current CPU domain and its ancestors until
396 * the target power level (end_pwrlvl). It updates the array of requested power
397 * states with this information.
398 *
399 * Then, for each level (apart from the CPU level) until the 'end_pwrlvl', it
400 * retrieves the states requested by all the cpus of which the power domain at
401 * that level is an ancestor. It passes this information to the platform to
402 * coordinate and return the target power state. If the target state for a level
403 * is RUN then subsequent levels are not considered. At the CPU level, state
404 * coordination is not required. Hence, the requested and the target states are
405 * the same.
406 *
407 * The 'state_info' is updated with the target state for each level between the
408 * CPU and the 'end_pwrlvl' and returned to the caller.
409 *
410 * This function will only be invoked with data cache enabled and while
411 * powering down a core.
412 *****************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100413void psci_do_state_coordination(unsigned int end_pwrlvl,
414 psci_power_state_t *state_info)
Soby Mathew67487842015-07-13 14:10:57 +0100415{
416 unsigned int lvl, parent_idx, cpu_idx = plat_my_core_pos();
417 unsigned int start_idx, ncpus;
418 plat_local_state_t target_state, *req_states;
419
Soby Mathew6d189692016-02-02 14:23:10 +0000420 assert(end_pwrlvl <= PLAT_MAX_PWR_LVL);
Soby Mathew67487842015-07-13 14:10:57 +0100421 parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
422
423 /* For level 0, the requested state will be equivalent
424 to target state */
425 for (lvl = PSCI_CPU_PWR_LVL + 1; lvl <= end_pwrlvl; lvl++) {
426
427 /* First update the requested power state */
428 psci_set_req_local_pwr_state(lvl, cpu_idx,
429 state_info->pwr_domain_state[lvl]);
430
431 /* Get the requested power states for this power level */
432 start_idx = psci_non_cpu_pd_nodes[parent_idx].cpu_start_idx;
433 req_states = psci_get_req_local_pwr_states(lvl, start_idx);
434
435 /*
436 * Let the platform coordinate amongst the requested states at
437 * this power level and return the target local power state.
438 */
439 ncpus = psci_non_cpu_pd_nodes[parent_idx].ncpus;
440 target_state = plat_get_target_pwr_state(lvl,
441 req_states,
442 ncpus);
443
444 state_info->pwr_domain_state[lvl] = target_state;
445
446 /* Break early if the negotiated target power state is RUN */
447 if (is_local_state_run(state_info->pwr_domain_state[lvl]))
448 break;
449
450 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
451 }
452
453 /*
454 * This is for cases when we break out of the above loop early because
455 * the target power state is RUN at a power level < end_pwlvl.
456 * We update the requested power state from state_info and then
457 * set the target state as RUN.
458 */
459 for (lvl = lvl + 1; lvl <= end_pwrlvl; lvl++) {
460 psci_set_req_local_pwr_state(lvl, cpu_idx,
461 state_info->pwr_domain_state[lvl]);
462 state_info->pwr_domain_state[lvl] = PSCI_LOCAL_STATE_RUN;
463
464 }
465
466 /* Update the target state in the power domain nodes */
467 psci_set_target_local_pwr_states(end_pwrlvl, state_info);
468}
469
470/******************************************************************************
471 * This function validates a suspend request by making sure that if a standby
472 * state is requested then no power level is turned off and the highest power
473 * level is placed in a standby/retention state.
474 *
475 * It also ensures that the state level X will enter is not shallower than the
476 * state level X + 1 will enter.
477 *
478 * This validation will be enabled only for DEBUG builds as the platform is
479 * expected to perform these validations as well.
480 *****************************************************************************/
481int psci_validate_suspend_req(const psci_power_state_t *state_info,
482 unsigned int is_power_down_state)
483{
484 unsigned int max_off_lvl, target_lvl, max_retn_lvl;
485 plat_local_state_t state;
486 plat_local_state_type_t req_state_type, deepest_state_type;
487 int i;
488
489 /* Find the target suspend power level */
490 target_lvl = psci_find_target_suspend_lvl(state_info);
Soby Mathew9d070b92015-07-29 17:05:03 +0100491 if (target_lvl == PSCI_INVALID_PWR_LVL)
Achin Gupta0959db52013-12-02 17:33:04 +0000492 return PSCI_E_INVALID_PARAMS;
493
Soby Mathew67487842015-07-13 14:10:57 +0100494 /* All power domain levels are in a RUN state to begin with */
495 deepest_state_type = STATE_TYPE_RUN;
Achin Gupta0959db52013-12-02 17:33:04 +0000496
Soby Mathew67487842015-07-13 14:10:57 +0100497 for (i = target_lvl; i >= PSCI_CPU_PWR_LVL; i--) {
498 state = state_info->pwr_domain_state[i];
499 req_state_type = find_local_state_type(state);
500
501 /*
502 * While traversing from the highest power level to the lowest,
503 * the state requested for lower levels has to be the same or
504 * deeper i.e. equal to or greater than the state at the higher
505 * levels. If this condition is true, then the requested state
506 * becomes the deepest state encountered so far.
507 */
508 if (req_state_type < deepest_state_type)
509 return PSCI_E_INVALID_PARAMS;
510 deepest_state_type = req_state_type;
511 }
512
513 /* Find the highest off power level */
514 max_off_lvl = psci_find_max_off_lvl(state_info);
515
516 /* The target_lvl is either equal to the max_off_lvl or max_retn_lvl */
Soby Mathew9d070b92015-07-29 17:05:03 +0100517 max_retn_lvl = PSCI_INVALID_PWR_LVL;
Soby Mathew67487842015-07-13 14:10:57 +0100518 if (target_lvl != max_off_lvl)
519 max_retn_lvl = target_lvl;
520
521 /*
522 * If this is not a request for a power down state then max off level
523 * has to be invalid and max retention level has to be a valid power
524 * level.
525 */
Soby Mathew9d070b92015-07-29 17:05:03 +0100526 if (!is_power_down_state && (max_off_lvl != PSCI_INVALID_PWR_LVL ||
527 max_retn_lvl == PSCI_INVALID_PWR_LVL))
Achin Gupta0959db52013-12-02 17:33:04 +0000528 return PSCI_E_INVALID_PARAMS;
529
530 return PSCI_E_SUCCESS;
531}
532
Soby Mathew67487842015-07-13 14:10:57 +0100533/******************************************************************************
534 * This function finds the highest power level which will be powered down
535 * amongst all the power levels specified in the 'state_info' structure
536 *****************************************************************************/
537unsigned int psci_find_max_off_lvl(const psci_power_state_t *state_info)
Achin Gupta84c9f102014-07-28 00:09:01 +0100538{
Soby Mathew67487842015-07-13 14:10:57 +0100539 int i;
Achin Gupta84c9f102014-07-28 00:09:01 +0100540
Soby Mathew67487842015-07-13 14:10:57 +0100541 for (i = PLAT_MAX_PWR_LVL; i >= PSCI_CPU_PWR_LVL; i--) {
542 if (is_local_state_off(state_info->pwr_domain_state[i]))
543 return i;
Achin Gupta84c9f102014-07-28 00:09:01 +0100544 }
Soby Mathew67487842015-07-13 14:10:57 +0100545
Soby Mathew9d070b92015-07-29 17:05:03 +0100546 return PSCI_INVALID_PWR_LVL;
Soby Mathew67487842015-07-13 14:10:57 +0100547}
548
549/******************************************************************************
550 * This functions finds the level of the highest power domain which will be
551 * placed in a low power state during a suspend operation.
552 *****************************************************************************/
553unsigned int psci_find_target_suspend_lvl(const psci_power_state_t *state_info)
554{
555 int i;
556
557 for (i = PLAT_MAX_PWR_LVL; i >= PSCI_CPU_PWR_LVL; i--) {
558 if (!is_local_state_run(state_info->pwr_domain_state[i]))
559 return i;
560 }
561
Soby Mathew9d070b92015-07-29 17:05:03 +0100562 return PSCI_INVALID_PWR_LVL;
Achin Gupta84c9f102014-07-28 00:09:01 +0100563}
564
565/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +0100566 * This function is passed a cpu_index and the highest level in the topology
567 * tree that the operation should be applied to. It picks up locks in order of
568 * increasing power domain level in the range specified.
Achin Gupta0959db52013-12-02 17:33:04 +0000569 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100570void psci_acquire_pwr_domain_locks(unsigned int end_pwrlvl,
571 unsigned int cpu_idx)
Achin Gupta0959db52013-12-02 17:33:04 +0000572{
Soby Mathew67487842015-07-13 14:10:57 +0100573 unsigned int parent_idx = psci_cpu_pd_nodes[cpu_idx].parent_node;
Soby Mathew9d070b92015-07-29 17:05:03 +0100574 unsigned int level;
Achin Gupta0959db52013-12-02 17:33:04 +0000575
Soby Mathew67487842015-07-13 14:10:57 +0100576 /* No locking required for level 0. Hence start locking from level 1 */
577 for (level = PSCI_CPU_PWR_LVL + 1; level <= end_pwrlvl; level++) {
578 psci_lock_get(&psci_non_cpu_pd_nodes[parent_idx]);
579 parent_idx = psci_non_cpu_pd_nodes[parent_idx].parent_node;
Achin Gupta0959db52013-12-02 17:33:04 +0000580 }
581}
582
583/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +0100584 * This function is passed a cpu_index and the highest level in the topology
585 * tree that the operation should be applied to. It releases the locks in order
586 * of decreasing power domain level in the range specified.
Achin Gupta0959db52013-12-02 17:33:04 +0000587 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100588void psci_release_pwr_domain_locks(unsigned int end_pwrlvl,
589 unsigned int cpu_idx)
Achin Gupta0959db52013-12-02 17:33:04 +0000590{
Soby Mathew67487842015-07-13 14:10:57 +0100591 unsigned int parent_idx, parent_nodes[PLAT_MAX_PWR_LVL] = {0};
Achin Gupta0959db52013-12-02 17:33:04 +0000592 int level;
593
Soby Mathew67487842015-07-13 14:10:57 +0100594 /* Get the parent nodes */
595 psci_get_parent_pwr_domain_nodes(cpu_idx, end_pwrlvl, parent_nodes);
Soby Mathew8c5fe0b2015-01-08 18:02:19 +0000596
Soby Mathew67487842015-07-13 14:10:57 +0100597 /* Unlock top down. No unlocking required for level 0. */
598 for (level = end_pwrlvl; level >= PSCI_CPU_PWR_LVL + 1; level--) {
599 parent_idx = parent_nodes[level - 1];
600 psci_lock_release(&psci_non_cpu_pd_nodes[parent_idx]);
Achin Gupta0959db52013-12-02 17:33:04 +0000601 }
602}
603
604/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +0100605 * Simple routine to determine whether a mpidr is valid or not.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100606 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100607int psci_validate_mpidr(u_register_t mpidr)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100608{
Soby Mathew67487842015-07-13 14:10:57 +0100609 if (plat_core_pos_by_mpidr(mpidr) < 0)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100610 return PSCI_E_INVALID_PARAMS;
Soby Mathew67487842015-07-13 14:10:57 +0100611
612 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100613}
614
615/*******************************************************************************
Andrew Thoelke167a9352014-06-04 21:10:52 +0100616 * This function determines the full entrypoint information for the requested
Soby Mathew78879b92015-01-06 15:36:38 +0000617 * PSCI entrypoint on power on/resume and returns it.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100618 ******************************************************************************/
Soby Mathew727e5232016-05-05 14:11:23 +0100619#ifdef AARCH32
620static int psci_get_ns_ep_info(entry_point_info_t *ep,
621 uintptr_t entrypoint,
622 u_register_t context_id)
623{
624 u_register_t ep_attr;
625 unsigned int aif, ee, mode;
626 u_register_t scr = read_scr();
627 u_register_t ns_sctlr, sctlr;
628
629 /* Switch to non secure state */
630 write_scr(scr | SCR_NS_BIT);
631 isb();
632 ns_sctlr = read_sctlr();
633
634 sctlr = scr & SCR_HCE_BIT ? read_hsctlr() : ns_sctlr;
635
636 /* Return to original state */
637 write_scr(scr);
638 isb();
639 ee = 0;
640
641 ep_attr = NON_SECURE | EP_ST_DISABLE;
642 if (sctlr & SCTLR_EE_BIT) {
643 ep_attr |= EP_EE_BIG;
644 ee = 1;
645 }
646 SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
647
648 ep->pc = entrypoint;
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000649 zeromem(&ep->args, sizeof(ep->args));
Soby Mathew727e5232016-05-05 14:11:23 +0100650 ep->args.arg0 = context_id;
651
652 mode = scr & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
653
654 /*
655 * TODO: Choose async. exception bits if HYP mode is not
656 * implemented according to the values of SCR.{AW, FW} bits
657 */
658 aif = SPSR_ABT_BIT | SPSR_IRQ_BIT | SPSR_FIQ_BIT;
659
660 ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, aif);
661
662 return PSCI_E_SUCCESS;
663}
664
665#else
Soby Mathew617540d2015-07-15 12:13:26 +0100666static int psci_get_ns_ep_info(entry_point_info_t *ep,
Soby Mathew9d070b92015-07-29 17:05:03 +0100667 uintptr_t entrypoint,
668 u_register_t context_id)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100669{
Soby Mathew4c0d0392016-06-16 14:52:04 +0100670 u_register_t ep_attr, sctlr;
Soby Mathew9d070b92015-07-29 17:05:03 +0100671 unsigned int daif, ee, mode;
Soby Mathew4c0d0392016-06-16 14:52:04 +0100672 u_register_t ns_scr_el3 = read_scr_el3();
673 u_register_t ns_sctlr_el1 = read_sctlr_el1();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100674
Andrew Thoelke167a9352014-06-04 21:10:52 +0100675 sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1;
676 ee = 0;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100677
Andrew Thoelke167a9352014-06-04 21:10:52 +0100678 ep_attr = NON_SECURE | EP_ST_DISABLE;
679 if (sctlr & SCTLR_EE_BIT) {
680 ep_attr |= EP_EE_BIG;
681 ee = 1;
682 }
Soby Mathew78879b92015-01-06 15:36:38 +0000683 SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100684
Soby Mathew78879b92015-01-06 15:36:38 +0000685 ep->pc = entrypoint;
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000686 zeromem(&ep->args, sizeof(ep->args));
Soby Mathew78879b92015-01-06 15:36:38 +0000687 ep->args.arg0 = context_id;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100688
689 /*
690 * Figure out whether the cpu enters the non-secure address space
691 * in aarch32 or aarch64
692 */
Andrew Thoelke167a9352014-06-04 21:10:52 +0100693 if (ns_scr_el3 & SCR_RW_BIT) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100694
695 /*
696 * Check whether a Thumb entry point has been provided for an
697 * aarch64 EL
698 */
699 if (entrypoint & 0x1)
Soby Mathew617540d2015-07-15 12:13:26 +0100700 return PSCI_E_INVALID_ADDRESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100701
Andrew Thoelke167a9352014-06-04 21:10:52 +0100702 mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100703
Soby Mathew78879b92015-01-06 15:36:38 +0000704 ep->spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100705 } else {
706
Andrew Thoelke167a9352014-06-04 21:10:52 +0100707 mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100708
709 /*
710 * TODO: Choose async. exception bits if HYP mode is not
711 * implemented according to the values of SCR.{AW, FW} bits
712 */
Vikram Kanigiri23ff9ba2014-05-13 14:42:08 +0100713 daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
714
Soby Mathew78879b92015-01-06 15:36:38 +0000715 ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100716 }
717
Andrew Thoelke167a9352014-06-04 21:10:52 +0100718 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100719}
Soby Mathew727e5232016-05-05 14:11:23 +0100720#endif
Achin Gupta4f6ad662013-10-25 09:08:21 +0100721
722/*******************************************************************************
Soby Mathew617540d2015-07-15 12:13:26 +0100723 * This function validates the entrypoint with the platform layer if the
724 * appropriate pm_ops hook is exported by the platform and returns the
725 * 'entry_point_info'.
726 ******************************************************************************/
727int psci_validate_entry_point(entry_point_info_t *ep,
Soby Mathew9d070b92015-07-29 17:05:03 +0100728 uintptr_t entrypoint,
729 u_register_t context_id)
Soby Mathew617540d2015-07-15 12:13:26 +0100730{
731 int rc;
732
733 /* Validate the entrypoint using platform psci_ops */
734 if (psci_plat_pm_ops->validate_ns_entrypoint) {
735 rc = psci_plat_pm_ops->validate_ns_entrypoint(entrypoint);
736 if (rc != PSCI_E_SUCCESS)
737 return PSCI_E_INVALID_ADDRESS;
738 }
739
740 /*
741 * Verify and derive the re-entry information for
742 * the non-secure world from the non-secure state from
743 * where this call originated.
744 */
745 rc = psci_get_ns_ep_info(ep, entrypoint, context_id);
746 return rc;
747}
748
749/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +0100750 * Generic handler which is called when a cpu is physically powered on. It
Soby Mathew67487842015-07-13 14:10:57 +0100751 * traverses the node information and finds the highest power level powered
752 * off and performs generic, architectural, platform setup and state management
753 * to power on that power level and power levels below it.
754 * e.g. For a cpu that's been powered on, it will call the platform specific
755 * code to enable the gic cpu interface and for a cluster it will enable
756 * coherency at the interconnect level in addition to gic cpu interface.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100757 ******************************************************************************/
Soby Mathewcf0b1492016-04-29 19:01:30 +0100758void psci_warmboot_entrypoint(void)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100759{
Soby Mathew9d070b92015-07-29 17:05:03 +0100760 unsigned int end_pwrlvl, cpu_idx = plat_my_core_pos();
Soby Mathew67487842015-07-13 14:10:57 +0100761 psci_power_state_t state_info = { {PSCI_LOCAL_STATE_RUN} };
Achin Gupta4f6ad662013-10-25 09:08:21 +0100762
Achin Gupta4f6ad662013-10-25 09:08:21 +0100763 /*
Soby Mathew67487842015-07-13 14:10:57 +0100764 * Verify that we have been explicitly turned ON or resumed from
765 * suspend.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100766 */
Soby Mathew67487842015-07-13 14:10:57 +0100767 if (psci_get_aff_info_state() == AFF_STATE_OFF) {
768 ERROR("Unexpected affinity info state");
James Morrissey40a6f642014-02-10 14:24:36 +0000769 panic();
Soby Mathew67487842015-07-13 14:10:57 +0100770 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100771
772 /*
Soby Mathew67487842015-07-13 14:10:57 +0100773 * Get the maximum power domain level to traverse to after this cpu
774 * has been physically powered up.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100775 */
Soby Mathew67487842015-07-13 14:10:57 +0100776 end_pwrlvl = get_power_on_target_pwrlvl();
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100777
778 /*
Soby Mathew67487842015-07-13 14:10:57 +0100779 * This function acquires the lock corresponding to each power level so
780 * that by the time all locks are taken, the system topology is snapshot
781 * and state management can be done safely.
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100782 */
Soby Mathew67487842015-07-13 14:10:57 +0100783 psci_acquire_pwr_domain_locks(end_pwrlvl,
784 cpu_idx);
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100785
Yatharth Kochar170fb932016-05-09 18:26:35 +0100786#if ENABLE_PSCI_STAT
dp-arm04c1db12017-01-31 13:01:04 +0000787 plat_psci_stat_accounting_stop(&state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100788#endif
789
Soby Mathew67487842015-07-13 14:10:57 +0100790 psci_get_target_local_pwr_states(end_pwrlvl, &state_info);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100791
792 /*
Soby Mathew67487842015-07-13 14:10:57 +0100793 * This CPU could be resuming from suspend or it could have just been
794 * turned on. To distinguish between these 2 cases, we examine the
795 * affinity state of the CPU:
796 * - If the affinity state is ON_PENDING then it has just been
797 * turned on.
798 * - Else it is resuming from suspend.
799 *
800 * Depending on the type of warm reset identified, choose the right set
801 * of power management handler and perform the generic, architecture
802 * and platform specific handling.
Achin Gupta84c9f102014-07-28 00:09:01 +0100803 */
Soby Mathew67487842015-07-13 14:10:57 +0100804 if (psci_get_aff_info_state() == AFF_STATE_ON_PENDING)
805 psci_cpu_on_finish(cpu_idx, &state_info);
806 else
807 psci_cpu_suspend_finish(cpu_idx, &state_info);
Achin Gupta84c9f102014-07-28 00:09:01 +0100808
809 /*
Soby Mathew67487842015-07-13 14:10:57 +0100810 * Set the requested and target state of this CPU and all the higher
811 * power domains which are ancestors of this CPU to run.
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100812 */
Soby Mathew67487842015-07-13 14:10:57 +0100813 psci_set_pwr_domains_to_run(end_pwrlvl);
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100814
Yatharth Kochar170fb932016-05-09 18:26:35 +0100815#if ENABLE_PSCI_STAT
816 /*
817 * Update PSCI stats.
818 * Caches are off when writing stats data on the power down path.
819 * Since caches are now enabled, it's necessary to do cache
820 * maintenance before reading that same data.
821 */
dp-arm04c1db12017-01-31 13:01:04 +0000822 psci_stats_update_pwr_up(end_pwrlvl, &state_info);
Yatharth Kochar170fb932016-05-09 18:26:35 +0100823#endif
824
Achin Gupta0a46e2c2014-07-31 11:19:11 +0100825 /*
Soby Mathew67487842015-07-13 14:10:57 +0100826 * This loop releases the lock corresponding to each power level
Achin Gupta0959db52013-12-02 17:33:04 +0000827 * in the reverse order to which they were acquired.
828 */
Soby Mathew67487842015-07-13 14:10:57 +0100829 psci_release_pwr_domain_locks(end_pwrlvl,
830 cpu_idx);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100831}
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000832
833/*******************************************************************************
834 * This function initializes the set of hooks that PSCI invokes as part of power
835 * management operation. The power management hooks are expected to be provided
836 * by the SPD, after it finishes all its initialization
837 ******************************************************************************/
Dan Handleyfb037bf2014-04-10 15:37:22 +0100838void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000839{
Soby Mathew90e82582015-01-07 11:10:22 +0000840 assert(pm);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000841 psci_spd_pm = pm;
Soby Mathew90e82582015-01-07 11:10:22 +0000842
843 if (pm->svc_migrate)
844 psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
845
846 if (pm->svc_migrate_info)
847 psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
848 | define_psci_cap(PSCI_MIG_INFO_TYPE);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000849}
Juan Castillod5f13092014-08-12 11:17:06 +0100850
851/*******************************************************************************
Soby Mathew8991eed2014-10-23 10:35:34 +0100852 * This function invokes the migrate info hook in the spd_pm_ops. It performs
853 * the necessary return value validation. If the Secure Payload is UP and
854 * migrate capable, it returns the mpidr of the CPU on which the Secure payload
855 * is resident through the mpidr parameter. Else the value of the parameter on
856 * return is undefined.
857 ******************************************************************************/
Soby Mathew9d070b92015-07-29 17:05:03 +0100858int psci_spd_migrate_info(u_register_t *mpidr)
Soby Mathew8991eed2014-10-23 10:35:34 +0100859{
860 int rc;
861
862 if (!psci_spd_pm || !psci_spd_pm->svc_migrate_info)
863 return PSCI_E_NOT_SUPPORTED;
864
865 rc = psci_spd_pm->svc_migrate_info(mpidr);
866
867 assert(rc == PSCI_TOS_UP_MIG_CAP || rc == PSCI_TOS_NOT_UP_MIG_CAP \
868 || rc == PSCI_TOS_NOT_PRESENT_MP || rc == PSCI_E_NOT_SUPPORTED);
869
870 return rc;
871}
872
873
874/*******************************************************************************
Soby Mathew67487842015-07-13 14:10:57 +0100875 * This function prints the state of all power domains present in the
Juan Castillod5f13092014-08-12 11:17:06 +0100876 * system
877 ******************************************************************************/
Soby Mathew67487842015-07-13 14:10:57 +0100878void psci_print_power_domain_map(void)
Juan Castillod5f13092014-08-12 11:17:06 +0100879{
880#if LOG_LEVEL >= LOG_LEVEL_INFO
Juan Castillod5f13092014-08-12 11:17:06 +0100881 unsigned int idx;
Soby Mathew67487842015-07-13 14:10:57 +0100882 plat_local_state_t state;
883 plat_local_state_type_t state_type;
884
Juan Castillod5f13092014-08-12 11:17:06 +0100885 /* This array maps to the PSCI_STATE_X definitions in psci.h */
Soby Mathewda554d72016-05-03 17:11:42 +0100886 static const char * const psci_state_type_str[] = {
Juan Castillod5f13092014-08-12 11:17:06 +0100887 "ON",
Soby Mathew67487842015-07-13 14:10:57 +0100888 "RETENTION",
Juan Castillod5f13092014-08-12 11:17:06 +0100889 "OFF",
Juan Castillod5f13092014-08-12 11:17:06 +0100890 };
891
Soby Mathew67487842015-07-13 14:10:57 +0100892 INFO("PSCI Power Domain Map:\n");
893 for (idx = 0; idx < (PSCI_NUM_PWR_DOMAINS - PLATFORM_CORE_COUNT);
894 idx++) {
895 state_type = find_local_state_type(
896 psci_non_cpu_pd_nodes[idx].local_state);
897 INFO(" Domain Node : Level %u, parent_node %d,"
898 " State %s (0x%x)\n",
899 psci_non_cpu_pd_nodes[idx].level,
900 psci_non_cpu_pd_nodes[idx].parent_node,
901 psci_state_type_str[state_type],
902 psci_non_cpu_pd_nodes[idx].local_state);
903 }
904
905 for (idx = 0; idx < PLATFORM_CORE_COUNT; idx++) {
906 state = psci_get_cpu_local_state_by_idx(idx);
907 state_type = find_local_state_type(state);
Soby Mathew4c0d0392016-06-16 14:52:04 +0100908 INFO(" CPU Node : MPID 0x%llx, parent_node %d,"
Soby Mathew67487842015-07-13 14:10:57 +0100909 " State %s (0x%x)\n",
Soby Mathew4c0d0392016-06-16 14:52:04 +0100910 (unsigned long long)psci_cpu_pd_nodes[idx].mpidr,
Soby Mathew67487842015-07-13 14:10:57 +0100911 psci_cpu_pd_nodes[idx].parent_node,
912 psci_state_type_str[state_type],
913 psci_get_cpu_local_state_by_idx(idx));
Juan Castillod5f13092014-08-12 11:17:06 +0100914 }
915#endif
916}
Soby Mathew67487842015-07-13 14:10:57 +0100917
918#if ENABLE_PLAT_COMPAT
919/*******************************************************************************
920 * PSCI Compatibility helper function to return the 'power_state' parameter of
921 * the PSCI CPU SUSPEND request for the current CPU. Returns PSCI_INVALID_DATA
922 * if not invoked within CPU_SUSPEND for the current CPU.
923 ******************************************************************************/
924int psci_get_suspend_powerstate(void)
925{
926 /* Sanity check to verify that CPU is within CPU_SUSPEND */
927 if (psci_get_aff_info_state() == AFF_STATE_ON &&
928 !is_local_state_run(psci_get_cpu_local_state()))
929 return psci_power_state_compat[plat_my_core_pos()];
930
931 return PSCI_INVALID_DATA;
932}
933
934/*******************************************************************************
935 * PSCI Compatibility helper function to return the state id of the current
936 * cpu encoded in the 'power_state' parameter. Returns PSCI_INVALID_DATA
937 * if not invoked within CPU_SUSPEND for the current CPU.
938 ******************************************************************************/
939int psci_get_suspend_stateid(void)
940{
941 unsigned int power_state;
942 power_state = psci_get_suspend_powerstate();
943 if (power_state != PSCI_INVALID_DATA)
944 return psci_get_pstate_id(power_state);
945
946 return PSCI_INVALID_DATA;
947}
948
949/*******************************************************************************
950 * PSCI Compatibility helper function to return the state id encoded in the
951 * 'power_state' parameter of the CPU specified by 'mpidr'. Returns
952 * PSCI_INVALID_DATA if the CPU is not in CPU_SUSPEND.
953 ******************************************************************************/
954int psci_get_suspend_stateid_by_mpidr(unsigned long mpidr)
955{
956 int cpu_idx = plat_core_pos_by_mpidr(mpidr);
957
958 if (cpu_idx == -1)
959 return PSCI_INVALID_DATA;
960
961 /* Sanity check to verify that the CPU is in CPU_SUSPEND */
962 if (psci_get_aff_info_state_by_idx(cpu_idx) == AFF_STATE_ON &&
963 !is_local_state_run(psci_get_cpu_local_state_by_idx(cpu_idx)))
964 return psci_get_pstate_id(psci_power_state_compat[cpu_idx]);
965
966 return PSCI_INVALID_DATA;
967}
968
969/*******************************************************************************
970 * This function returns highest affinity level which is in OFF
971 * state. The affinity instance with which the level is associated is
972 * determined by the caller.
973 ******************************************************************************/
974unsigned int psci_get_max_phys_off_afflvl(void)
975{
976 psci_power_state_t state_info;
977
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000978 zeromem(&state_info, sizeof(state_info));
Soby Mathew67487842015-07-13 14:10:57 +0100979 psci_get_target_local_pwr_states(PLAT_MAX_PWR_LVL, &state_info);
980
981 return psci_find_target_suspend_lvl(&state_info);
982}
983
984/*******************************************************************************
985 * PSCI Compatibility helper function to return target affinity level requested
986 * for the CPU_SUSPEND. This function assumes affinity levels correspond to
987 * power domain levels on the platform.
988 ******************************************************************************/
989int psci_get_suspend_afflvl(void)
990{
991 return psci_get_suspend_pwrlvl();
992}
993
994#endif