blob: 55578379dcf38b59a278656b1e4025fa6ef730d5 [file] [log] [blame]
Soby Mathewb48349e2015-06-29 16:30:12 +01001/*
Soby Mathew4067dc32015-05-05 16:33:16 +01002 * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
Soby Mathewb48349e2015-06-29 16:30:12 +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
31#include <arch.h>
32#include <arch_helpers.h>
33#include <assert.h>
34#include <bl_common.h>
35#include <context.h>
36#include <context_mgmt.h>
37#include <debug.h>
38#include <platform.h>
39#include <string.h>
40#include "psci_private.h"
41
42/*
43 * SPD power management operations, expected to be supplied by the registered
44 * SPD on successful SP initialization
45 */
46const spd_pm_ops_t *psci_spd_pm;
47
48/*******************************************************************************
49 * Grand array that holds the platform's topology information for state
Soby Mathew4067dc32015-05-05 16:33:16 +010050 * management of power domain instances. Each node (pwr_map_node) in the array
51 * corresponds to a power domain instance e.g. cluster, cpu within an mpidr
Soby Mathewb48349e2015-06-29 16:30:12 +010052 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +010053pwr_map_node_t psci_pwr_domain_map[PSCI_NUM_PWR_DOMAINS]
Soby Mathewb48349e2015-06-29 16:30:12 +010054#if USE_COHERENT_MEM
55__attribute__ ((section("tzfw_coherent_mem")))
56#endif
57;
58
59/*******************************************************************************
60 * Pointer to functions exported by the platform to complete power mgmt. ops
61 ******************************************************************************/
62const plat_pm_ops_t *psci_plat_pm_ops;
63
64/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +010065 * Check that the maximum power level supported by the platform makes sense
Soby Mathewb48349e2015-06-29 16:30:12 +010066 * ****************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +010067CASSERT(PLAT_MAX_PWR_LVL <= MPIDR_MAX_AFFLVL && \
68 PLAT_MAX_PWR_LVL >= MPIDR_AFFLVL0, \
69 assert_platform_max_pwrlvl_check);
Soby Mathewb48349e2015-06-29 16:30:12 +010070
71/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +010072 * This function is passed an array of pointers to power domain nodes in the
73 * topology tree for an mpidr. It iterates through the nodes to find the
74 * highest power level where the power domain is marked as physically powered
75 * off.
Soby Mathewb48349e2015-06-29 16:30:12 +010076 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +010077uint32_t psci_find_max_phys_off_pwrlvl(uint32_t start_pwrlvl,
78 uint32_t end_pwrlvl,
79 pwr_map_node_t *mpidr_nodes[])
Soby Mathewb48349e2015-06-29 16:30:12 +010080{
Soby Mathew4067dc32015-05-05 16:33:16 +010081 uint32_t max_pwrlvl = PSCI_INVALID_DATA;
Soby Mathewb48349e2015-06-29 16:30:12 +010082
Soby Mathew4067dc32015-05-05 16:33:16 +010083 for (; start_pwrlvl <= end_pwrlvl; start_pwrlvl++) {
84 if (mpidr_nodes[start_pwrlvl] == NULL)
Soby Mathewb48349e2015-06-29 16:30:12 +010085 continue;
86
Soby Mathew4067dc32015-05-05 16:33:16 +010087 if (psci_get_phys_state(mpidr_nodes[start_pwrlvl]) ==
Soby Mathewb48349e2015-06-29 16:30:12 +010088 PSCI_STATE_OFF)
Soby Mathew4067dc32015-05-05 16:33:16 +010089 max_pwrlvl = start_pwrlvl;
Soby Mathewb48349e2015-06-29 16:30:12 +010090 }
91
Soby Mathew4067dc32015-05-05 16:33:16 +010092 return max_pwrlvl;
Soby Mathewb48349e2015-06-29 16:30:12 +010093}
94
95/*******************************************************************************
96 * This function verifies that the all the other cores in the system have been
97 * turned OFF and the current CPU is the last running CPU in the system.
98 * Returns 1 (true) if the current CPU is the last ON CPU or 0 (false)
99 * otherwise.
100 ******************************************************************************/
101unsigned int psci_is_last_on_cpu(void)
102{
103 unsigned long mpidr = read_mpidr_el1() & MPIDR_AFFINITY_MASK;
104 unsigned int i;
105
Soby Mathew4067dc32015-05-05 16:33:16 +0100106 for (i = psci_pwr_lvl_limits[MPIDR_AFFLVL0].min;
107 i <= psci_pwr_lvl_limits[MPIDR_AFFLVL0].max; i++) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100108
Soby Mathew4067dc32015-05-05 16:33:16 +0100109 assert(psci_pwr_domain_map[i].level == MPIDR_AFFLVL0);
Soby Mathewb48349e2015-06-29 16:30:12 +0100110
Soby Mathew4067dc32015-05-05 16:33:16 +0100111 if (!(psci_pwr_domain_map[i].state & PSCI_AFF_PRESENT))
Soby Mathewb48349e2015-06-29 16:30:12 +0100112 continue;
113
Soby Mathew4067dc32015-05-05 16:33:16 +0100114 if (psci_pwr_domain_map[i].mpidr == mpidr) {
115 assert(psci_get_state(&psci_pwr_domain_map[i])
Soby Mathewb48349e2015-06-29 16:30:12 +0100116 == PSCI_STATE_ON);
117 continue;
118 }
119
Soby Mathew4067dc32015-05-05 16:33:16 +0100120 if (psci_get_state(&psci_pwr_domain_map[i]) != PSCI_STATE_OFF)
Soby Mathewb48349e2015-06-29 16:30:12 +0100121 return 0;
122 }
123
124 return 1;
125}
126
127/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100128 * Routine to return the maximum power level to traverse to after a cpu has
Soby Mathewb48349e2015-06-29 16:30:12 +0100129 * been physically powered up. It is expected to be called immediately after
130 * reset from assembler code.
131 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100132int get_power_on_target_pwrlvl(void)
Soby Mathewb48349e2015-06-29 16:30:12 +0100133{
Soby Mathew4067dc32015-05-05 16:33:16 +0100134 int pwrlvl;
Soby Mathewb48349e2015-06-29 16:30:12 +0100135
136#if DEBUG
137 unsigned int state;
Soby Mathew4067dc32015-05-05 16:33:16 +0100138 pwr_map_node_t *node;
Soby Mathewb48349e2015-06-29 16:30:12 +0100139
140 /* Retrieve our node from the topology tree */
Soby Mathew4067dc32015-05-05 16:33:16 +0100141 node = psci_get_pwr_map_node(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Soby Mathewb48349e2015-06-29 16:30:12 +0100142 MPIDR_AFFLVL0);
143 assert(node);
144
145 /*
146 * Sanity check the state of the cpu. It should be either suspend or "on
147 * pending"
148 */
149 state = psci_get_state(node);
150 assert(state == PSCI_STATE_SUSPEND || state == PSCI_STATE_ON_PENDING);
151#endif
152
153 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100154 * Assume that this cpu was suspended and retrieve its target power
Soby Mathewb48349e2015-06-29 16:30:12 +0100155 * level. If it is invalid then it could only have been turned off
Soby Mathew4067dc32015-05-05 16:33:16 +0100156 * earlier. PLAT_MAX_PWR_LVL will be the highest power level a
Soby Mathewb48349e2015-06-29 16:30:12 +0100157 * cpu can be turned off to.
158 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100159 pwrlvl = psci_get_suspend_pwrlvl();
160 if (pwrlvl == PSCI_INVALID_DATA)
161 pwrlvl = PLAT_MAX_PWR_LVL;
162 return pwrlvl;
Soby Mathewb48349e2015-06-29 16:30:12 +0100163}
164
165/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100166 * Simple routine to set the id of a power domain instance at a given level
167 * in the mpidr. The assumption is that the affinity level and the power
168 * level are the same.
Soby Mathewb48349e2015-06-29 16:30:12 +0100169 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100170unsigned long mpidr_set_pwr_domain_inst(unsigned long mpidr,
171 unsigned char pwr_inst,
172 int pwr_lvl)
Soby Mathewb48349e2015-06-29 16:30:12 +0100173{
174 unsigned long aff_shift;
175
Soby Mathew4067dc32015-05-05 16:33:16 +0100176 assert(pwr_lvl <= MPIDR_AFFLVL3);
Soby Mathewb48349e2015-06-29 16:30:12 +0100177
178 /*
179 * Decide the number of bits to shift by depending upon
Soby Mathew4067dc32015-05-05 16:33:16 +0100180 * the power level
Soby Mathewb48349e2015-06-29 16:30:12 +0100181 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100182 aff_shift = get_afflvl_shift(pwr_lvl);
Soby Mathewb48349e2015-06-29 16:30:12 +0100183
184 /* Clear the existing affinity instance & set the new one*/
185 mpidr &= ~(((unsigned long)MPIDR_AFFLVL_MASK) << aff_shift);
Soby Mathew4067dc32015-05-05 16:33:16 +0100186 mpidr |= ((unsigned long)pwr_inst) << aff_shift;
Soby Mathewb48349e2015-06-29 16:30:12 +0100187
188 return mpidr;
189}
190
191/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100192 * This function sanity checks a range of power levels.
Soby Mathewb48349e2015-06-29 16:30:12 +0100193 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100194int psci_check_pwrlvl_range(int start_pwrlvl, int end_pwrlvl)
Soby Mathewb48349e2015-06-29 16:30:12 +0100195{
196 /* Sanity check the parameters passed */
Soby Mathew4067dc32015-05-05 16:33:16 +0100197 if (end_pwrlvl > PLAT_MAX_PWR_LVL)
Soby Mathewb48349e2015-06-29 16:30:12 +0100198 return PSCI_E_INVALID_PARAMS;
199
Soby Mathew4067dc32015-05-05 16:33:16 +0100200 if (start_pwrlvl < MPIDR_AFFLVL0)
Soby Mathewb48349e2015-06-29 16:30:12 +0100201 return PSCI_E_INVALID_PARAMS;
202
Soby Mathew4067dc32015-05-05 16:33:16 +0100203 if (end_pwrlvl < start_pwrlvl)
Soby Mathewb48349e2015-06-29 16:30:12 +0100204 return PSCI_E_INVALID_PARAMS;
205
206 return PSCI_E_SUCCESS;
207}
208
209/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100210 * This function is passed an array of pointers to power domain nodes in the
Soby Mathewb48349e2015-06-29 16:30:12 +0100211 * topology tree for an mpidr and the state which each node should transition
Soby Mathew4067dc32015-05-05 16:33:16 +0100212 * to. It updates the state of each node between the specified power levels.
Soby Mathewb48349e2015-06-29 16:30:12 +0100213 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100214void psci_do_state_coordination(uint32_t start_pwrlvl,
215 uint32_t end_pwrlvl,
216 pwr_map_node_t *mpidr_nodes[],
Soby Mathewb48349e2015-06-29 16:30:12 +0100217 uint32_t state)
218{
219 uint32_t level;
220
Soby Mathew4067dc32015-05-05 16:33:16 +0100221 for (level = start_pwrlvl; level <= end_pwrlvl; level++) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100222 if (mpidr_nodes[level] == NULL)
223 continue;
224 psci_set_state(mpidr_nodes[level], state);
225 }
226}
227
228/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100229 * This function is passed an array of pointers to power domain nodes in the
230 * topology tree for an mpidr. It picks up locks for each power level bottom
Soby Mathewb48349e2015-06-29 16:30:12 +0100231 * up in the range specified.
232 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100233void psci_acquire_pwr_domain_locks(int start_pwrlvl,
234 int end_pwrlvl,
235 pwr_map_node_t *mpidr_nodes[])
Soby Mathewb48349e2015-06-29 16:30:12 +0100236{
237 int level;
238
Soby Mathew4067dc32015-05-05 16:33:16 +0100239 for (level = start_pwrlvl; level <= end_pwrlvl; level++) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100240 if (mpidr_nodes[level] == NULL)
241 continue;
242
243 psci_lock_get(mpidr_nodes[level]);
244 }
245}
246
247/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100248 * This function is passed an array of pointers to power domain nodes in the
249 * topology tree for an mpidr. It releases the lock for each power level top
Soby Mathewb48349e2015-06-29 16:30:12 +0100250 * down in the range specified.
251 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100252void psci_release_pwr_domain_locks(int start_pwrlvl,
253 int end_pwrlvl,
254 pwr_map_node_t *mpidr_nodes[])
Soby Mathewb48349e2015-06-29 16:30:12 +0100255{
256 int level;
257
Soby Mathew4067dc32015-05-05 16:33:16 +0100258 for (level = end_pwrlvl; level >= start_pwrlvl; level--) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100259 if (mpidr_nodes[level] == NULL)
260 continue;
261
262 psci_lock_release(mpidr_nodes[level]);
263 }
264}
265
266/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100267 * Simple routine to determine whether an power domain instance at a given
268 * level in an mpidr exists or not.
Soby Mathewb48349e2015-06-29 16:30:12 +0100269 ******************************************************************************/
270int psci_validate_mpidr(unsigned long mpidr, int level)
271{
Soby Mathew4067dc32015-05-05 16:33:16 +0100272 pwr_map_node_t *node;
Soby Mathewb48349e2015-06-29 16:30:12 +0100273
Soby Mathew4067dc32015-05-05 16:33:16 +0100274 node = psci_get_pwr_map_node(mpidr, level);
275 if (node && (node->state & PSCI_PWR_DOMAIN_PRESENT))
Soby Mathewb48349e2015-06-29 16:30:12 +0100276 return PSCI_E_SUCCESS;
277 else
278 return PSCI_E_INVALID_PARAMS;
279}
280
281/*******************************************************************************
282 * This function determines the full entrypoint information for the requested
283 * PSCI entrypoint on power on/resume and returns it.
284 ******************************************************************************/
285int psci_get_ns_ep_info(entry_point_info_t *ep,
286 uint64_t entrypoint, uint64_t context_id)
287{
288 uint32_t ep_attr, mode, sctlr, daif, ee;
289 uint32_t ns_scr_el3 = read_scr_el3();
290 uint32_t ns_sctlr_el1 = read_sctlr_el1();
291
292 sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1;
293 ee = 0;
294
295 ep_attr = NON_SECURE | EP_ST_DISABLE;
296 if (sctlr & SCTLR_EE_BIT) {
297 ep_attr |= EP_EE_BIG;
298 ee = 1;
299 }
300 SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
301
302 ep->pc = entrypoint;
303 memset(&ep->args, 0, sizeof(ep->args));
304 ep->args.arg0 = context_id;
305
306 /*
307 * Figure out whether the cpu enters the non-secure address space
308 * in aarch32 or aarch64
309 */
310 if (ns_scr_el3 & SCR_RW_BIT) {
311
312 /*
313 * Check whether a Thumb entry point has been provided for an
314 * aarch64 EL
315 */
316 if (entrypoint & 0x1)
317 return PSCI_E_INVALID_PARAMS;
318
319 mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1;
320
321 ep->spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
322 } else {
323
324 mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
325
326 /*
327 * TODO: Choose async. exception bits if HYP mode is not
328 * implemented according to the values of SCR.{AW, FW} bits
329 */
330 daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
331
332 ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif);
333 }
334
335 return PSCI_E_SUCCESS;
336}
337
338/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100339 * This function takes a pointer to a power domain node in the topology tree
340 * and returns its state. State of a non-leaf node needs to be calculated.
Soby Mathewb48349e2015-06-29 16:30:12 +0100341 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100342unsigned short psci_get_state(pwr_map_node_t *node)
Soby Mathewb48349e2015-06-29 16:30:12 +0100343{
344#if !USE_COHERENT_MEM
345 flush_dcache_range((uint64_t) node, sizeof(*node));
346#endif
347
348 assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL);
349
350 /* A cpu node just contains the state which can be directly returned */
351 if (node->level == MPIDR_AFFLVL0)
352 return (node->state >> PSCI_STATE_SHIFT) & PSCI_STATE_MASK;
353
354 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100355 * For a power level higher than a cpu, the state has to be
Soby Mathewb48349e2015-06-29 16:30:12 +0100356 * calculated. It depends upon the value of the reference count
Soby Mathew4067dc32015-05-05 16:33:16 +0100357 * which is managed by each node at the next lower power level
Soby Mathewb48349e2015-06-29 16:30:12 +0100358 * e.g. for a cluster, each cpu increments/decrements the reference
Soby Mathew4067dc32015-05-05 16:33:16 +0100359 * count. If the reference count is 0 then the power level is
Soby Mathewb48349e2015-06-29 16:30:12 +0100360 * OFF else ON.
361 */
362 if (node->ref_count)
363 return PSCI_STATE_ON;
364 else
365 return PSCI_STATE_OFF;
366}
367
368/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100369 * This function takes a pointer to a power domain node in the topology
370 * tree and a target state. State of a non-leaf node needs to be converted
371 * to a reference count. State of a leaf node can be set directly.
Soby Mathewb48349e2015-06-29 16:30:12 +0100372 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100373void psci_set_state(pwr_map_node_t *node, unsigned short state)
Soby Mathewb48349e2015-06-29 16:30:12 +0100374{
375 assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL);
376
377 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100378 * For a power level higher than a cpu, the state is used
Soby Mathewb48349e2015-06-29 16:30:12 +0100379 * to decide whether the reference count is incremented or
380 * decremented. Entry into the ON_PENDING state does not have
381 * effect.
382 */
383 if (node->level > MPIDR_AFFLVL0) {
384 switch (state) {
385 case PSCI_STATE_ON:
386 node->ref_count++;
387 break;
388 case PSCI_STATE_OFF:
389 case PSCI_STATE_SUSPEND:
390 node->ref_count--;
391 break;
392 case PSCI_STATE_ON_PENDING:
393 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100394 * A power level higher than a cpu will not undergo
Soby Mathewb48349e2015-06-29 16:30:12 +0100395 * a state change when it is about to be turned on
396 */
397 return;
398 default:
399 assert(0);
400 }
401 } else {
402 node->state &= ~(PSCI_STATE_MASK << PSCI_STATE_SHIFT);
403 node->state |= (state & PSCI_STATE_MASK) << PSCI_STATE_SHIFT;
404 }
405
406#if !USE_COHERENT_MEM
407 flush_dcache_range((uint64_t) node, sizeof(*node));
408#endif
409}
410
411/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100412 * A power domain could be on, on_pending, suspended or off. These are the
Soby Mathewb48349e2015-06-29 16:30:12 +0100413 * logical states it can be in. Physically either it is off or on. When it is in
414 * the state on_pending then it is about to be turned on. It is not possible to
Soby Mathew4067dc32015-05-05 16:33:16 +0100415 * tell whether that's actually happened or not. So we err on the side of
416 * caution & treat the power domain as being turned off.
Soby Mathewb48349e2015-06-29 16:30:12 +0100417 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100418unsigned short psci_get_phys_state(pwr_map_node_t *node)
Soby Mathewb48349e2015-06-29 16:30:12 +0100419{
420 unsigned int state;
421
422 state = psci_get_state(node);
423 return get_phys_state(state);
424}
425
426/*******************************************************************************
Soby Mathewb48349e2015-06-29 16:30:12 +0100427 * Generic handler which is called when a cpu is physically powered on. It
Soby Mathew4067dc32015-05-05 16:33:16 +0100428 * traverses the node information and finds the highest power level powered
Soby Mathew6590ce22015-06-30 11:00:24 +0100429 * off and performs generic, architectural, platform setup and state management
Soby Mathew4067dc32015-05-05 16:33:16 +0100430 * to power on that power level and power levels below it.
Soby Mathew6590ce22015-06-30 11:00:24 +0100431 * e.g. For a cpu that's been powered on, it will call the platform specific
432 * code to enable the gic cpu interface and for a cluster it will enable
433 * coherency at the interconnect level in addition to gic cpu interface.
Soby Mathewb48349e2015-06-29 16:30:12 +0100434 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100435void psci_power_up_finish(int end_pwrlvl,
436 pwrlvl_power_on_finisher_t pon_handler)
Soby Mathewb48349e2015-06-29 16:30:12 +0100437{
Soby Mathew4067dc32015-05-05 16:33:16 +0100438 mpidr_pwr_map_nodes_t mpidr_nodes;
Soby Mathewb48349e2015-06-29 16:30:12 +0100439 int rc;
Soby Mathew4067dc32015-05-05 16:33:16 +0100440 unsigned int max_phys_off_pwrlvl;
Soby Mathewb48349e2015-06-29 16:30:12 +0100441
442
443 /*
444 * Collect the pointers to the nodes in the topology tree for
Soby Mathew4067dc32015-05-05 16:33:16 +0100445 * each power domain instances in the mpidr. If this function does
446 * not return successfully then either the mpidr or the power
Soby Mathewb48349e2015-06-29 16:30:12 +0100447 * levels are incorrect. Either case is an irrecoverable error.
448 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100449 rc = psci_get_pwr_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Soby Mathew6590ce22015-06-30 11:00:24 +0100450 MPIDR_AFFLVL0,
Soby Mathew4067dc32015-05-05 16:33:16 +0100451 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100452 mpidr_nodes);
453 if (rc != PSCI_E_SUCCESS)
454 panic();
455
456 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100457 * This function acquires the lock corresponding to each power
Soby Mathewb48349e2015-06-29 16:30:12 +0100458 * level so that by the time all locks are taken, the system topology
459 * is snapshot and state management can be done safely.
460 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100461 psci_acquire_pwr_domain_locks(MPIDR_AFFLVL0,
462 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100463 mpidr_nodes);
464
Soby Mathew4067dc32015-05-05 16:33:16 +0100465 max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(MPIDR_AFFLVL0,
466 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100467 mpidr_nodes);
Soby Mathew4067dc32015-05-05 16:33:16 +0100468 assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA);
Soby Mathewb48349e2015-06-29 16:30:12 +0100469
Soby Mathewb48349e2015-06-29 16:30:12 +0100470 /* Perform generic, architecture and platform specific handling */
Soby Mathew4067dc32015-05-05 16:33:16 +0100471 pon_handler(mpidr_nodes, max_phys_off_pwrlvl);
Soby Mathewb48349e2015-06-29 16:30:12 +0100472
473 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100474 * This function updates the state of each power instance
475 * corresponding to the mpidr in the range of power levels
Soby Mathewb48349e2015-06-29 16:30:12 +0100476 * specified.
477 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100478 psci_do_state_coordination(MPIDR_AFFLVL0,
479 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100480 mpidr_nodes,
481 PSCI_STATE_ON);
482
483 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100484 * This loop releases the lock corresponding to each power level
Soby Mathewb48349e2015-06-29 16:30:12 +0100485 * in the reverse order to which they were acquired.
486 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100487 psci_release_pwr_domain_locks(MPIDR_AFFLVL0,
488 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100489 mpidr_nodes);
490}
491
492/*******************************************************************************
493 * This function initializes the set of hooks that PSCI invokes as part of power
494 * management operation. The power management hooks are expected to be provided
495 * by the SPD, after it finishes all its initialization
496 ******************************************************************************/
497void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
498{
499 assert(pm);
500 psci_spd_pm = pm;
501
502 if (pm->svc_migrate)
503 psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
504
505 if (pm->svc_migrate_info)
506 psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
507 | define_psci_cap(PSCI_MIG_INFO_TYPE);
508}
509
510/*******************************************************************************
511 * This function invokes the migrate info hook in the spd_pm_ops. It performs
512 * the necessary return value validation. If the Secure Payload is UP and
513 * migrate capable, it returns the mpidr of the CPU on which the Secure payload
514 * is resident through the mpidr parameter. Else the value of the parameter on
515 * return is undefined.
516 ******************************************************************************/
517int psci_spd_migrate_info(uint64_t *mpidr)
518{
519 int rc;
520
521 if (!psci_spd_pm || !psci_spd_pm->svc_migrate_info)
522 return PSCI_E_NOT_SUPPORTED;
523
524 rc = psci_spd_pm->svc_migrate_info(mpidr);
525
526 assert(rc == PSCI_TOS_UP_MIG_CAP || rc == PSCI_TOS_NOT_UP_MIG_CAP \
527 || rc == PSCI_TOS_NOT_PRESENT_MP || rc == PSCI_E_NOT_SUPPORTED);
528
529 return rc;
530}
531
532
533/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100534 * This function prints the state of all power domains present in the
Soby Mathewb48349e2015-06-29 16:30:12 +0100535 * system
536 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100537void psci_print_power_domain_map(void)
Soby Mathewb48349e2015-06-29 16:30:12 +0100538{
539#if LOG_LEVEL >= LOG_LEVEL_INFO
Soby Mathew4067dc32015-05-05 16:33:16 +0100540 pwr_map_node_t *node;
Soby Mathewb48349e2015-06-29 16:30:12 +0100541 unsigned int idx;
542 /* This array maps to the PSCI_STATE_X definitions in psci.h */
543 static const char *psci_state_str[] = {
544 "ON",
545 "OFF",
546 "ON_PENDING",
547 "SUSPEND"
548 };
549
Soby Mathew4067dc32015-05-05 16:33:16 +0100550 INFO("PSCI Power Domain Map:\n");
551 for (idx = 0; idx < PSCI_NUM_PWR_DOMAINS; idx++) {
552 node = &psci_pwr_domain_map[idx];
553 if (!(node->state & PSCI_PWR_DOMAIN_PRESENT)) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100554 continue;
555 }
Soby Mathew4067dc32015-05-05 16:33:16 +0100556 INFO(" pwrInst: Level %u, MPID 0x%lx, State %s\n",
Soby Mathewb48349e2015-06-29 16:30:12 +0100557 node->level, node->mpidr,
558 psci_state_str[psci_get_state(node)]);
559 }
560#endif
561}