blob: c12ad437ce011c7a5c0130f7e45c05339f229fba [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, 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
31#include <stdio.h>
32#include <string.h>
33#include <assert.h>
34#include <arch_helpers.h>
35#include <console.h>
36#include <platform.h>
37#include <psci.h>
38#include <psci_private.h>
Achin Guptaef7a28c2014-02-01 08:59:56 +000039#include <context_mgmt.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
41typedef int (*afflvl_suspend_handler)(unsigned long,
42 aff_map_node *,
43 unsigned long,
44 unsigned long,
45 unsigned int);
46
47/*******************************************************************************
Achin Guptaa45e3972013-12-05 15:10:48 +000048 * This function sets the affinity level till which the current cpu is being
49 * powered down to during a cpu_suspend call
50 ******************************************************************************/
51void psci_set_suspend_afflvl(aff_map_node *node, int afflvl)
52{
53 /*
54 * Check that nobody else is calling this function on our behalf &
55 * this information is being set only in the cpu node
56 */
57 assert(node->mpidr == (read_mpidr() & MPIDR_AFFINITY_MASK));
58 assert(node->level == MPIDR_AFFLVL0);
59
60 /*
61 * Store the affinity level we are powering down to in our context.
62 * The cache flush in the suspend code will ensure that this info
63 * is available immediately upon resuming.
64 */
65 psci_suspend_context[node->data].suspend_level = afflvl;
66}
67
68/*******************************************************************************
69 * This function gets the affinity level till which the current cpu was powered
70 * down during a cpu_suspend call.
71 ******************************************************************************/
72int psci_get_suspend_afflvl(aff_map_node *node)
73{
74 /* Return the target affinity level */
75 return psci_suspend_context[node->data].suspend_level;
76}
77
78/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +010079 * The next three functions implement a handler for each supported affinity
80 * level which is called when that affinity level is about to be suspended.
81 ******************************************************************************/
82static int psci_afflvl0_suspend(unsigned long mpidr,
83 aff_map_node *cpu_node,
84 unsigned long ns_entrypoint,
85 unsigned long context_id,
86 unsigned int power_state)
87{
88 unsigned int index, plat_state;
89 unsigned long psci_entrypoint, sctlr = read_sctlr();
90 int rc = PSCI_E_SUCCESS;
91
92 /* Sanity check to safeguard against data corruption */
93 assert(cpu_node->level == MPIDR_AFFLVL0);
94
Achin Gupta75f73672013-12-05 16:33:10 +000095 /* State management: mark this cpu as suspended */
96 psci_set_state(cpu_node, PSCI_STATE_SUSPEND);
97
Achin Gupta4f6ad662013-10-25 09:08:21 +010098 /*
99 * Generic management: Store the re-entry information for the
100 * non-secure world
101 */
102 index = cpu_node->data;
103 rc = psci_set_ns_entry_info(index, ns_entrypoint, context_id);
104 if (rc != PSCI_E_SUCCESS)
105 return rc;
106
107 /*
Achin Guptaef7a28c2014-02-01 08:59:56 +0000108 * Arch. management: Save the EL3 state in the 'cpu_context'
109 * structure that has been allocated for this cpu, flush the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100110 * L1 caches and exit intra-cluster coherency et al
111 */
Achin Guptaef7a28c2014-02-01 08:59:56 +0000112 cm_el3_sysregs_context_save(NON_SECURE);
113 rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100114
115 /* Set the secure world (EL3) re-entry point after BL1 */
116 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
117
118 /*
119 * Arch. management. Perform the necessary steps to flush all
120 * cpu caches.
121 *
122 * TODO: This power down sequence varies across cpus so it needs to be
123 * abstracted out on the basis of the MIDR like in cpu_reset_handler().
124 * Do the bare minimal for the time being. Fix this before porting to
125 * Cortex models.
126 */
127 sctlr &= ~SCTLR_C_BIT;
128 write_sctlr(sctlr);
129
130 /*
131 * CAUTION: This flush to the level of unification makes an assumption
132 * about the cache hierarchy at affinity level 0 (cpu) in the platform.
133 * Ideally the platform should tell psci which levels to flush to exit
134 * coherency.
135 */
136 dcsw_op_louis(DCCISW);
137
138 /*
139 * Plat. management: Allow the platform to perform the
140 * necessary actions to turn off this cpu e.g. set the
141 * platform defined mailbox with the psci entrypoint,
142 * program the power controller etc.
143 */
144 if (psci_plat_pm_ops->affinst_suspend) {
Achin Gupta75f73672013-12-05 16:33:10 +0000145 plat_state = psci_get_phys_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100146 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
147 psci_entrypoint,
148 ns_entrypoint,
149 cpu_node->level,
150 plat_state);
151 }
152
153 return rc;
154}
155
156static int psci_afflvl1_suspend(unsigned long mpidr,
157 aff_map_node *cluster_node,
158 unsigned long ns_entrypoint,
159 unsigned long context_id,
160 unsigned int power_state)
161{
162 int rc = PSCI_E_SUCCESS;
163 unsigned int plat_state;
164 unsigned long psci_entrypoint;
165
166 /* Sanity check the cluster level */
167 assert(cluster_node->level == MPIDR_AFFLVL1);
168
Achin Gupta75f73672013-12-05 16:33:10 +0000169 /* State management: Decrement the cluster reference count */
170 psci_set_state(cluster_node, PSCI_STATE_SUSPEND);
171
Achin Gupta4f6ad662013-10-25 09:08:21 +0100172 /*
173 * Keep the physical state of this cluster handy to decide
174 * what action needs to be taken
175 */
Achin Gupta75f73672013-12-05 16:33:10 +0000176 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100177
178 /*
179 * Arch. management: Flush all levels of caches to PoC if the
180 * cluster is to be shutdown
181 */
182 if (plat_state == PSCI_STATE_OFF)
183 dcsw_op_all(DCCISW);
184
185 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000186 * Plat. Management. Allow the platform to do its cluster
Achin Gupta4f6ad662013-10-25 09:08:21 +0100187 * specific bookeeping e.g. turn off interconnect coherency,
188 * program the power controller etc.
189 */
190 if (psci_plat_pm_ops->affinst_suspend) {
191
192 /*
193 * Sending the psci entrypoint is currently redundant
194 * beyond affinity level 0 but one never knows what a
195 * platform might do. Also it allows us to keep the
196 * platform handler prototype the same.
197 */
198 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100199 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
200 psci_entrypoint,
201 ns_entrypoint,
202 cluster_node->level,
203 plat_state);
204 }
205
206 return rc;
207}
208
209
210static int psci_afflvl2_suspend(unsigned long mpidr,
211 aff_map_node *system_node,
212 unsigned long ns_entrypoint,
213 unsigned long context_id,
214 unsigned int power_state)
215{
216 int rc = PSCI_E_SUCCESS;
217 unsigned int plat_state;
218 unsigned long psci_entrypoint;
219
220 /* Cannot go beyond this */
221 assert(system_node->level == MPIDR_AFFLVL2);
222
Achin Gupta75f73672013-12-05 16:33:10 +0000223 /* State management: Decrement the system reference count */
224 psci_set_state(system_node, PSCI_STATE_SUSPEND);
225
Achin Gupta4f6ad662013-10-25 09:08:21 +0100226 /*
227 * Keep the physical state of the system handy to decide what
228 * action needs to be taken
229 */
Achin Gupta75f73672013-12-05 16:33:10 +0000230 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100231
232 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000233 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100234 * at this affinity level
235 */
236 if (psci_plat_pm_ops->affinst_suspend) {
237
238 /*
239 * Sending the psci entrypoint is currently redundant
240 * beyond affinity level 0 but one never knows what a
241 * platform might do. Also it allows us to keep the
242 * platform handler prototype the same.
243 */
244 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100245 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
246 psci_entrypoint,
247 ns_entrypoint,
248 system_node->level,
249 plat_state);
250 }
251
252 return rc;
253}
254
255static const afflvl_suspend_handler psci_afflvl_suspend_handlers[] = {
256 psci_afflvl0_suspend,
257 psci_afflvl1_suspend,
258 psci_afflvl2_suspend,
259};
260
261/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000262 * This function takes an array of pointers to affinity instance nodes in the
263 * topology tree and calls the suspend handler for the corresponding affinity
264 * levels
265 ******************************************************************************/
266static int psci_call_suspend_handlers(mpidr_aff_map_nodes mpidr_nodes,
267 int start_afflvl,
268 int end_afflvl,
269 unsigned long mpidr,
270 unsigned long entrypoint,
271 unsigned long context_id,
272 unsigned int power_state)
273{
274 int rc = PSCI_E_INVALID_PARAMS, level;
275 aff_map_node *node;
276
277 for (level = start_afflvl; level <= end_afflvl; level++) {
278 node = mpidr_nodes[level];
279 if (node == NULL)
280 continue;
281
282 /*
283 * TODO: In case of an error should there be a way
284 * of restoring what we might have torn down at
285 * lower affinity levels.
286 */
287 rc = psci_afflvl_suspend_handlers[level](mpidr,
288 node,
289 entrypoint,
290 context_id,
291 power_state);
292 if (rc != PSCI_E_SUCCESS)
293 break;
294 }
295
296 return rc;
297}
298
299/*******************************************************************************
300 * Top level handler which is called when a cpu wants to suspend its execution.
301 * It is assumed that along with turning the cpu off, higher affinity levels
302 * until the target affinity level will be turned off as well. It traverses
303 * through all the affinity levels performing generic, architectural, platform
304 * setup and state management e.g. for a cluster that's to be suspended, it will
305 * call the platform specific code which will disable coherency at the
306 * interconnect level if the cpu is the last in the cluster. For a cpu it could
307 * mean programming the power controller etc.
308 *
309 * The state of all the relevant affinity levels is changed prior to calling the
310 * affinity level specific handlers as their actions would depend upon the state
311 * the affinity level is about to enter.
312 *
313 * The affinity level specific handlers are called in ascending order i.e. from
314 * the lowest to the highest affinity level implemented by the platform because
315 * to turn off affinity level X it is neccesary to turn off affinity level X - 1
316 * first.
317 *
318 * CAUTION: This function is called with coherent stacks so that coherency can
319 * be turned off and caches can be flushed safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100320 ******************************************************************************/
321int psci_afflvl_suspend(unsigned long mpidr,
322 unsigned long entrypoint,
323 unsigned long context_id,
324 unsigned int power_state,
Achin Gupta0959db52013-12-02 17:33:04 +0000325 int start_afflvl,
326 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100327{
Achin Gupta0959db52013-12-02 17:33:04 +0000328 int rc = PSCI_E_SUCCESS;
Achin Gupta0959db52013-12-02 17:33:04 +0000329 mpidr_aff_map_nodes mpidr_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100330
331 mpidr &= MPIDR_AFFINITY_MASK;
332
333 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000334 * Collect the pointers to the nodes in the topology tree for
335 * each affinity instance in the mpidr. If this function does
336 * not return successfully then either the mpidr or the affinity
337 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100338 */
Achin Gupta0959db52013-12-02 17:33:04 +0000339 rc = psci_get_aff_map_nodes(mpidr,
340 start_afflvl,
341 end_afflvl,
342 mpidr_nodes);
343 if (rc != PSCI_E_SUCCESS)
344 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100345
346 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000347 * This function acquires the lock corresponding to each affinity
348 * level so that by the time all locks are taken, the system topology
349 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100350 */
Achin Gupta0959db52013-12-02 17:33:04 +0000351 psci_acquire_afflvl_locks(mpidr,
352 start_afflvl,
353 end_afflvl,
354 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100355
Achin Gupta0959db52013-12-02 17:33:04 +0000356
Achin Guptaa45e3972013-12-05 15:10:48 +0000357 /* Save the affinity level till which this cpu can be powered down */
358 psci_set_suspend_afflvl(mpidr_nodes[MPIDR_AFFLVL0], end_afflvl);
359
Achin Gupta0959db52013-12-02 17:33:04 +0000360 /* Perform generic, architecture and platform specific handling */
361 rc = psci_call_suspend_handlers(mpidr_nodes,
362 start_afflvl,
363 end_afflvl,
364 mpidr,
365 entrypoint,
366 context_id,
367 power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100368
369 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000370 * Release the locks corresponding to each affinity level in the
371 * reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100372 */
Achin Gupta0959db52013-12-02 17:33:04 +0000373 psci_release_afflvl_locks(mpidr,
374 start_afflvl,
375 end_afflvl,
376 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100377
Achin Gupta4f6ad662013-10-25 09:08:21 +0100378 return rc;
379}
380
381/*******************************************************************************
382 * The following functions finish an earlier affinity suspend request. They
383 * are called by the common finisher routine in psci_common.c.
384 ******************************************************************************/
385static unsigned int psci_afflvl0_suspend_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000386 aff_map_node *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100387{
Achin Gupta0959db52013-12-02 17:33:04 +0000388 unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100389
390 assert(cpu_node->level == MPIDR_AFFLVL0);
391
Achin Gupta0959db52013-12-02 17:33:04 +0000392 /* Ensure we have been woken up from a suspended state */
Achin Gupta75f73672013-12-05 16:33:10 +0000393 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000394 assert(state == PSCI_STATE_SUSPEND);
395
Achin Gupta4f6ad662013-10-25 09:08:21 +0100396 /*
397 * Plat. management: Perform the platform specific actions
398 * before we change the state of the cpu e.g. enabling the
399 * gic or zeroing the mailbox register. If anything goes
400 * wrong then assert as there is no way to recover from this
401 * situation.
402 */
403 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000404
405 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000406 plat_state = get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100407 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
408 cpu_node->level,
409 plat_state);
410 assert(rc == PSCI_E_SUCCESS);
411 }
412
413 /* Get the index for restoring the re-entry information */
414 index = cpu_node->data;
415
416 /*
Achin Guptaef7a28c2014-02-01 08:59:56 +0000417 * Arch. management: Restore the stashed EL3 architectural
418 * context from the 'cpu_context' structure for this cpu.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100419 */
Achin Guptaef7a28c2014-02-01 08:59:56 +0000420 cm_el3_sysregs_context_restore(NON_SECURE);
421 rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100422
423 /*
424 * Generic management: Now we just need to retrieve the
425 * information that we had stashed away during the suspend
Achin Gupta3140a9e2013-12-02 16:23:12 +0000426 * call to set this cpu on its way.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100427 */
Achin Guptac8afc782013-11-25 18:45:02 +0000428 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100429
Achin Gupta75f73672013-12-05 16:33:10 +0000430 /* State management: mark this cpu as on */
431 psci_set_state(cpu_node, PSCI_STATE_ON);
432
Achin Gupta4f6ad662013-10-25 09:08:21 +0100433 /* Clean caches before re-entering normal world */
434 dcsw_op_louis(DCCSW);
435
436 return rc;
437}
438
439static unsigned int psci_afflvl1_suspend_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000440 aff_map_node *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100441{
Achin Gupta0959db52013-12-02 17:33:04 +0000442 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100443
444 assert(cluster_node->level == MPIDR_AFFLVL1);
445
446 /*
447 * Plat. management: Perform the platform specific actions
448 * as per the old state of the cluster e.g. enabling
449 * coherency at the interconnect depends upon the state with
450 * which this cluster was powered up. If anything goes wrong
451 * then assert as there is no way to recover from this
452 * situation.
453 */
454 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000455
456 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000457 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100458 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
459 cluster_node->level,
460 plat_state);
461 assert(rc == PSCI_E_SUCCESS);
462 }
463
Achin Gupta75f73672013-12-05 16:33:10 +0000464 /* State management: Increment the cluster reference count */
465 psci_set_state(cluster_node, PSCI_STATE_ON);
466
Achin Gupta4f6ad662013-10-25 09:08:21 +0100467 return rc;
468}
469
470
471static unsigned int psci_afflvl2_suspend_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000472 aff_map_node *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100473{
Achin Gupta0959db52013-12-02 17:33:04 +0000474 unsigned int plat_state, rc = PSCI_E_SUCCESS;;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100475
476 /* Cannot go beyond this affinity level */
477 assert(system_node->level == MPIDR_AFFLVL2);
478
479 /*
480 * Currently, there are no architectural actions to perform
481 * at the system level.
482 */
483
484 /*
485 * Plat. management: Perform the platform specific actions
486 * as per the old state of the cluster e.g. enabling
487 * coherency at the interconnect depends upon the state with
488 * which this cluster was powered up. If anything goes wrong
489 * then assert as there is no way to recover from this
490 * situation.
491 */
492 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000493
494 /* Get the physical state of the system */
Achin Gupta75f73672013-12-05 16:33:10 +0000495 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100496 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
497 system_node->level,
498 plat_state);
499 assert(rc == PSCI_E_SUCCESS);
500 }
501
Achin Gupta75f73672013-12-05 16:33:10 +0000502 /* State management: Increment the system reference count */
503 psci_set_state(system_node, PSCI_STATE_ON);
504
Achin Gupta4f6ad662013-10-25 09:08:21 +0100505 return rc;
506}
507
508const afflvl_power_on_finisher psci_afflvl_suspend_finishers[] = {
509 psci_afflvl0_suspend_finish,
510 psci_afflvl1_suspend_finish,
511 psci_afflvl2_suspend_finish,
512};
513