blob: d9e9ccee0f7bda53512cebaa51c2111475d6751c [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 Mathew12d0d002015-04-09 13:40:55 +0100267 * Simple routine to determine whether a mpidr is valid or not.
Soby Mathewb48349e2015-06-29 16:30:12 +0100268 ******************************************************************************/
Soby Mathew12d0d002015-04-09 13:40:55 +0100269int psci_validate_mpidr(unsigned long mpidr)
Soby Mathewb48349e2015-06-29 16:30:12 +0100270{
Soby Mathew12d0d002015-04-09 13:40:55 +0100271 if (plat_core_pos_by_mpidr(mpidr) < 0)
Soby Mathewb48349e2015-06-29 16:30:12 +0100272 return PSCI_E_INVALID_PARAMS;
Soby Mathew12d0d002015-04-09 13:40:55 +0100273
274 return PSCI_E_SUCCESS;
Soby Mathewb48349e2015-06-29 16:30:12 +0100275}
276
277/*******************************************************************************
278 * This function determines the full entrypoint information for the requested
279 * PSCI entrypoint on power on/resume and returns it.
280 ******************************************************************************/
281int psci_get_ns_ep_info(entry_point_info_t *ep,
282 uint64_t entrypoint, uint64_t context_id)
283{
284 uint32_t ep_attr, mode, sctlr, daif, ee;
285 uint32_t ns_scr_el3 = read_scr_el3();
286 uint32_t ns_sctlr_el1 = read_sctlr_el1();
287
288 sctlr = ns_scr_el3 & SCR_HCE_BIT ? read_sctlr_el2() : ns_sctlr_el1;
289 ee = 0;
290
291 ep_attr = NON_SECURE | EP_ST_DISABLE;
292 if (sctlr & SCTLR_EE_BIT) {
293 ep_attr |= EP_EE_BIG;
294 ee = 1;
295 }
296 SET_PARAM_HEAD(ep, PARAM_EP, VERSION_1, ep_attr);
297
298 ep->pc = entrypoint;
299 memset(&ep->args, 0, sizeof(ep->args));
300 ep->args.arg0 = context_id;
301
302 /*
303 * Figure out whether the cpu enters the non-secure address space
304 * in aarch32 or aarch64
305 */
306 if (ns_scr_el3 & SCR_RW_BIT) {
307
308 /*
309 * Check whether a Thumb entry point has been provided for an
310 * aarch64 EL
311 */
312 if (entrypoint & 0x1)
313 return PSCI_E_INVALID_PARAMS;
314
315 mode = ns_scr_el3 & SCR_HCE_BIT ? MODE_EL2 : MODE_EL1;
316
317 ep->spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
318 } else {
319
320 mode = ns_scr_el3 & SCR_HCE_BIT ? MODE32_hyp : MODE32_svc;
321
322 /*
323 * TODO: Choose async. exception bits if HYP mode is not
324 * implemented according to the values of SCR.{AW, FW} bits
325 */
326 daif = DAIF_ABT_BIT | DAIF_IRQ_BIT | DAIF_FIQ_BIT;
327
328 ep->spsr = SPSR_MODE32(mode, entrypoint & 0x1, ee, daif);
329 }
330
331 return PSCI_E_SUCCESS;
332}
333
334/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100335 * This function takes a pointer to a power domain node in the topology tree
336 * and returns its state. State of a non-leaf node needs to be calculated.
Soby Mathewb48349e2015-06-29 16:30:12 +0100337 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100338unsigned short psci_get_state(pwr_map_node_t *node)
Soby Mathewb48349e2015-06-29 16:30:12 +0100339{
340#if !USE_COHERENT_MEM
341 flush_dcache_range((uint64_t) node, sizeof(*node));
342#endif
343
344 assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL);
345
346 /* A cpu node just contains the state which can be directly returned */
347 if (node->level == MPIDR_AFFLVL0)
348 return (node->state >> PSCI_STATE_SHIFT) & PSCI_STATE_MASK;
349
350 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100351 * For a power level higher than a cpu, the state has to be
Soby Mathewb48349e2015-06-29 16:30:12 +0100352 * calculated. It depends upon the value of the reference count
Soby Mathew4067dc32015-05-05 16:33:16 +0100353 * which is managed by each node at the next lower power level
Soby Mathewb48349e2015-06-29 16:30:12 +0100354 * e.g. for a cluster, each cpu increments/decrements the reference
Soby Mathew4067dc32015-05-05 16:33:16 +0100355 * count. If the reference count is 0 then the power level is
Soby Mathewb48349e2015-06-29 16:30:12 +0100356 * OFF else ON.
357 */
358 if (node->ref_count)
359 return PSCI_STATE_ON;
360 else
361 return PSCI_STATE_OFF;
362}
363
364/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100365 * This function takes a pointer to a power domain node in the topology
366 * tree and a target state. State of a non-leaf node needs to be converted
367 * to a reference count. State of a leaf node can be set directly.
Soby Mathewb48349e2015-06-29 16:30:12 +0100368 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100369void psci_set_state(pwr_map_node_t *node, unsigned short state)
Soby Mathewb48349e2015-06-29 16:30:12 +0100370{
371 assert(node->level >= MPIDR_AFFLVL0 && node->level <= MPIDR_MAX_AFFLVL);
372
373 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100374 * For a power level higher than a cpu, the state is used
Soby Mathewb48349e2015-06-29 16:30:12 +0100375 * to decide whether the reference count is incremented or
376 * decremented. Entry into the ON_PENDING state does not have
377 * effect.
378 */
379 if (node->level > MPIDR_AFFLVL0) {
380 switch (state) {
381 case PSCI_STATE_ON:
382 node->ref_count++;
383 break;
384 case PSCI_STATE_OFF:
385 case PSCI_STATE_SUSPEND:
386 node->ref_count--;
387 break;
388 case PSCI_STATE_ON_PENDING:
389 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100390 * A power level higher than a cpu will not undergo
Soby Mathewb48349e2015-06-29 16:30:12 +0100391 * a state change when it is about to be turned on
392 */
393 return;
394 default:
395 assert(0);
396 }
397 } else {
398 node->state &= ~(PSCI_STATE_MASK << PSCI_STATE_SHIFT);
399 node->state |= (state & PSCI_STATE_MASK) << PSCI_STATE_SHIFT;
400 }
401
402#if !USE_COHERENT_MEM
403 flush_dcache_range((uint64_t) node, sizeof(*node));
404#endif
405}
406
407/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100408 * A power domain could be on, on_pending, suspended or off. These are the
Soby Mathewb48349e2015-06-29 16:30:12 +0100409 * logical states it can be in. Physically either it is off or on. When it is in
410 * the state on_pending then it is about to be turned on. It is not possible to
Soby Mathew4067dc32015-05-05 16:33:16 +0100411 * tell whether that's actually happened or not. So we err on the side of
412 * caution & treat the power domain as being turned off.
Soby Mathewb48349e2015-06-29 16:30:12 +0100413 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100414unsigned short psci_get_phys_state(pwr_map_node_t *node)
Soby Mathewb48349e2015-06-29 16:30:12 +0100415{
416 unsigned int state;
417
418 state = psci_get_state(node);
419 return get_phys_state(state);
420}
421
422/*******************************************************************************
Soby Mathewb48349e2015-06-29 16:30:12 +0100423 * Generic handler which is called when a cpu is physically powered on. It
Soby Mathew4067dc32015-05-05 16:33:16 +0100424 * traverses the node information and finds the highest power level powered
Soby Mathew6590ce22015-06-30 11:00:24 +0100425 * off and performs generic, architectural, platform setup and state management
Soby Mathew4067dc32015-05-05 16:33:16 +0100426 * to power on that power level and power levels below it.
Soby Mathew6590ce22015-06-30 11:00:24 +0100427 * e.g. For a cpu that's been powered on, it will call the platform specific
428 * code to enable the gic cpu interface and for a cluster it will enable
429 * coherency at the interconnect level in addition to gic cpu interface.
Soby Mathewb48349e2015-06-29 16:30:12 +0100430 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100431void psci_power_up_finish(int end_pwrlvl,
432 pwrlvl_power_on_finisher_t pon_handler)
Soby Mathewb48349e2015-06-29 16:30:12 +0100433{
Soby Mathew4067dc32015-05-05 16:33:16 +0100434 mpidr_pwr_map_nodes_t mpidr_nodes;
Soby Mathewb48349e2015-06-29 16:30:12 +0100435 int rc;
Soby Mathew4067dc32015-05-05 16:33:16 +0100436 unsigned int max_phys_off_pwrlvl;
Soby Mathewb48349e2015-06-29 16:30:12 +0100437
438
439 /*
440 * Collect the pointers to the nodes in the topology tree for
Soby Mathew4067dc32015-05-05 16:33:16 +0100441 * each power domain instances in the mpidr. If this function does
442 * not return successfully then either the mpidr or the power
Soby Mathewb48349e2015-06-29 16:30:12 +0100443 * levels are incorrect. Either case is an irrecoverable error.
444 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100445 rc = psci_get_pwr_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Soby Mathew6590ce22015-06-30 11:00:24 +0100446 MPIDR_AFFLVL0,
Soby Mathew4067dc32015-05-05 16:33:16 +0100447 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100448 mpidr_nodes);
449 if (rc != PSCI_E_SUCCESS)
450 panic();
451
452 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100453 * This function acquires the lock corresponding to each power
Soby Mathewb48349e2015-06-29 16:30:12 +0100454 * level so that by the time all locks are taken, the system topology
455 * is snapshot and state management can be done safely.
456 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100457 psci_acquire_pwr_domain_locks(MPIDR_AFFLVL0,
458 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100459 mpidr_nodes);
460
Soby Mathew4067dc32015-05-05 16:33:16 +0100461 max_phys_off_pwrlvl = psci_find_max_phys_off_pwrlvl(MPIDR_AFFLVL0,
462 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100463 mpidr_nodes);
Soby Mathew4067dc32015-05-05 16:33:16 +0100464 assert(max_phys_off_pwrlvl != PSCI_INVALID_DATA);
Soby Mathewb48349e2015-06-29 16:30:12 +0100465
Soby Mathewb48349e2015-06-29 16:30:12 +0100466 /* Perform generic, architecture and platform specific handling */
Soby Mathew4067dc32015-05-05 16:33:16 +0100467 pon_handler(mpidr_nodes, max_phys_off_pwrlvl);
Soby Mathewb48349e2015-06-29 16:30:12 +0100468
469 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100470 * This function updates the state of each power instance
471 * corresponding to the mpidr in the range of power levels
Soby Mathewb48349e2015-06-29 16:30:12 +0100472 * specified.
473 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100474 psci_do_state_coordination(MPIDR_AFFLVL0,
475 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100476 mpidr_nodes,
477 PSCI_STATE_ON);
478
479 /*
Soby Mathew4067dc32015-05-05 16:33:16 +0100480 * This loop releases the lock corresponding to each power level
Soby Mathewb48349e2015-06-29 16:30:12 +0100481 * in the reverse order to which they were acquired.
482 */
Soby Mathew4067dc32015-05-05 16:33:16 +0100483 psci_release_pwr_domain_locks(MPIDR_AFFLVL0,
484 end_pwrlvl,
Soby Mathewb48349e2015-06-29 16:30:12 +0100485 mpidr_nodes);
486}
487
488/*******************************************************************************
489 * This function initializes the set of hooks that PSCI invokes as part of power
490 * management operation. The power management hooks are expected to be provided
491 * by the SPD, after it finishes all its initialization
492 ******************************************************************************/
493void psci_register_spd_pm_hook(const spd_pm_ops_t *pm)
494{
495 assert(pm);
496 psci_spd_pm = pm;
497
498 if (pm->svc_migrate)
499 psci_caps |= define_psci_cap(PSCI_MIG_AARCH64);
500
501 if (pm->svc_migrate_info)
502 psci_caps |= define_psci_cap(PSCI_MIG_INFO_UP_CPU_AARCH64)
503 | define_psci_cap(PSCI_MIG_INFO_TYPE);
504}
505
506/*******************************************************************************
507 * This function invokes the migrate info hook in the spd_pm_ops. It performs
508 * the necessary return value validation. If the Secure Payload is UP and
509 * migrate capable, it returns the mpidr of the CPU on which the Secure payload
510 * is resident through the mpidr parameter. Else the value of the parameter on
511 * return is undefined.
512 ******************************************************************************/
513int psci_spd_migrate_info(uint64_t *mpidr)
514{
515 int rc;
516
517 if (!psci_spd_pm || !psci_spd_pm->svc_migrate_info)
518 return PSCI_E_NOT_SUPPORTED;
519
520 rc = psci_spd_pm->svc_migrate_info(mpidr);
521
522 assert(rc == PSCI_TOS_UP_MIG_CAP || rc == PSCI_TOS_NOT_UP_MIG_CAP \
523 || rc == PSCI_TOS_NOT_PRESENT_MP || rc == PSCI_E_NOT_SUPPORTED);
524
525 return rc;
526}
527
528
529/*******************************************************************************
Soby Mathew4067dc32015-05-05 16:33:16 +0100530 * This function prints the state of all power domains present in the
Soby Mathewb48349e2015-06-29 16:30:12 +0100531 * system
532 ******************************************************************************/
Soby Mathew4067dc32015-05-05 16:33:16 +0100533void psci_print_power_domain_map(void)
Soby Mathewb48349e2015-06-29 16:30:12 +0100534{
535#if LOG_LEVEL >= LOG_LEVEL_INFO
Soby Mathew4067dc32015-05-05 16:33:16 +0100536 pwr_map_node_t *node;
Soby Mathewb48349e2015-06-29 16:30:12 +0100537 unsigned int idx;
538 /* This array maps to the PSCI_STATE_X definitions in psci.h */
539 static const char *psci_state_str[] = {
540 "ON",
541 "OFF",
542 "ON_PENDING",
543 "SUSPEND"
544 };
545
Soby Mathew4067dc32015-05-05 16:33:16 +0100546 INFO("PSCI Power Domain Map:\n");
547 for (idx = 0; idx < PSCI_NUM_PWR_DOMAINS; idx++) {
548 node = &psci_pwr_domain_map[idx];
549 if (!(node->state & PSCI_PWR_DOMAIN_PRESENT)) {
Soby Mathewb48349e2015-06-29 16:30:12 +0100550 continue;
551 }
Soby Mathew4067dc32015-05-05 16:33:16 +0100552 INFO(" pwrInst: Level %u, MPID 0x%lx, State %s\n",
Soby Mathewb48349e2015-06-29 16:30:12 +0100553 node->level, node->mpidr,
554 psci_state_str[psci_get_state(node)]);
555 }
556#endif
557}