blob: 186f048b1142d4f0697b8df73077513d18f895b2 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleyab2d31e2013-12-02 19:25:12 +00002 * Copyright (c) 2013, 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>
39
40typedef int (*afflvl_suspend_handler)(unsigned long,
41 aff_map_node *,
42 unsigned long,
43 unsigned long,
44 unsigned int);
45
46/*******************************************************************************
47 * The next three functions implement a handler for each supported affinity
48 * level which is called when that affinity level is about to be suspended.
49 ******************************************************************************/
50static int psci_afflvl0_suspend(unsigned long mpidr,
51 aff_map_node *cpu_node,
52 unsigned long ns_entrypoint,
53 unsigned long context_id,
54 unsigned int power_state)
55{
56 unsigned int index, plat_state;
57 unsigned long psci_entrypoint, sctlr = read_sctlr();
58 int rc = PSCI_E_SUCCESS;
59
60 /* Sanity check to safeguard against data corruption */
61 assert(cpu_node->level == MPIDR_AFFLVL0);
62
63 /*
64 * Generic management: Store the re-entry information for the
65 * non-secure world
66 */
67 index = cpu_node->data;
68 rc = psci_set_ns_entry_info(index, ns_entrypoint, context_id);
69 if (rc != PSCI_E_SUCCESS)
70 return rc;
71
72 /*
73 * Arch. management: Save the secure context, flush the
74 * L1 caches and exit intra-cluster coherency et al
75 */
76 psci_secure_context[index].sctlr = read_sctlr();
77 psci_secure_context[index].scr = read_scr();
78 psci_secure_context[index].cptr = read_cptr();
79 psci_secure_context[index].cpacr = read_cpacr();
80 psci_secure_context[index].cntfrq = read_cntfrq_el0();
81 psci_secure_context[index].mair = read_mair();
82 psci_secure_context[index].tcr = read_tcr();
83 psci_secure_context[index].ttbr = read_ttbr0();
84 psci_secure_context[index].vbar = read_vbar();
Sandrine Bailleux37382742013-11-18 17:26:59 +000085 psci_secure_context[index].pstate =
86 read_daif() & (DAIF_ABT_BIT | DAIF_DBG_BIT);
Achin Gupta4f6ad662013-10-25 09:08:21 +010087
88 /* Set the secure world (EL3) re-entry point after BL1 */
89 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
90
91 /*
92 * Arch. management. Perform the necessary steps to flush all
93 * cpu caches.
94 *
95 * TODO: This power down sequence varies across cpus so it needs to be
96 * abstracted out on the basis of the MIDR like in cpu_reset_handler().
97 * Do the bare minimal for the time being. Fix this before porting to
98 * Cortex models.
99 */
100 sctlr &= ~SCTLR_C_BIT;
101 write_sctlr(sctlr);
102
103 /*
104 * CAUTION: This flush to the level of unification makes an assumption
105 * about the cache hierarchy at affinity level 0 (cpu) in the platform.
106 * Ideally the platform should tell psci which levels to flush to exit
107 * coherency.
108 */
109 dcsw_op_louis(DCCISW);
110
111 /*
112 * Plat. management: Allow the platform to perform the
113 * necessary actions to turn off this cpu e.g. set the
114 * platform defined mailbox with the psci entrypoint,
115 * program the power controller etc.
116 */
117 if (psci_plat_pm_ops->affinst_suspend) {
118 plat_state = psci_get_aff_phys_state(cpu_node);
119 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
120 psci_entrypoint,
121 ns_entrypoint,
122 cpu_node->level,
123 plat_state);
124 }
125
126 return rc;
127}
128
129static int psci_afflvl1_suspend(unsigned long mpidr,
130 aff_map_node *cluster_node,
131 unsigned long ns_entrypoint,
132 unsigned long context_id,
133 unsigned int power_state)
134{
135 int rc = PSCI_E_SUCCESS;
136 unsigned int plat_state;
137 unsigned long psci_entrypoint;
138
139 /* Sanity check the cluster level */
140 assert(cluster_node->level == MPIDR_AFFLVL1);
141
142 /*
143 * Keep the physical state of this cluster handy to decide
144 * what action needs to be taken
145 */
146 plat_state = psci_get_aff_phys_state(cluster_node);
147
148 /*
149 * Arch. management: Flush all levels of caches to PoC if the
150 * cluster is to be shutdown
151 */
152 if (plat_state == PSCI_STATE_OFF)
153 dcsw_op_all(DCCISW);
154
155 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000156 * Plat. Management. Allow the platform to do its cluster
Achin Gupta4f6ad662013-10-25 09:08:21 +0100157 * specific bookeeping e.g. turn off interconnect coherency,
158 * program the power controller etc.
159 */
160 if (psci_plat_pm_ops->affinst_suspend) {
161
162 /*
163 * Sending the psci entrypoint is currently redundant
164 * beyond affinity level 0 but one never knows what a
165 * platform might do. Also it allows us to keep the
166 * platform handler prototype the same.
167 */
168 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100169 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
170 psci_entrypoint,
171 ns_entrypoint,
172 cluster_node->level,
173 plat_state);
174 }
175
176 return rc;
177}
178
179
180static int psci_afflvl2_suspend(unsigned long mpidr,
181 aff_map_node *system_node,
182 unsigned long ns_entrypoint,
183 unsigned long context_id,
184 unsigned int power_state)
185{
186 int rc = PSCI_E_SUCCESS;
187 unsigned int plat_state;
188 unsigned long psci_entrypoint;
189
190 /* Cannot go beyond this */
191 assert(system_node->level == MPIDR_AFFLVL2);
192
193 /*
194 * Keep the physical state of the system handy to decide what
195 * action needs to be taken
196 */
197 plat_state = psci_get_aff_phys_state(system_node);
198
199 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000200 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100201 * at this affinity level
202 */
203 if (psci_plat_pm_ops->affinst_suspend) {
204
205 /*
206 * Sending the psci entrypoint is currently redundant
207 * beyond affinity level 0 but one never knows what a
208 * platform might do. Also it allows us to keep the
209 * platform handler prototype the same.
210 */
211 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100212 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
213 psci_entrypoint,
214 ns_entrypoint,
215 system_node->level,
216 plat_state);
217 }
218
219 return rc;
220}
221
222static const afflvl_suspend_handler psci_afflvl_suspend_handlers[] = {
223 psci_afflvl0_suspend,
224 psci_afflvl1_suspend,
225 psci_afflvl2_suspend,
226};
227
228/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000229 * This function takes an array of pointers to affinity instance nodes in the
230 * topology tree and calls the suspend handler for the corresponding affinity
231 * levels
232 ******************************************************************************/
233static int psci_call_suspend_handlers(mpidr_aff_map_nodes mpidr_nodes,
234 int start_afflvl,
235 int end_afflvl,
236 unsigned long mpidr,
237 unsigned long entrypoint,
238 unsigned long context_id,
239 unsigned int power_state)
240{
241 int rc = PSCI_E_INVALID_PARAMS, level;
242 aff_map_node *node;
243
244 for (level = start_afflvl; level <= end_afflvl; level++) {
245 node = mpidr_nodes[level];
246 if (node == NULL)
247 continue;
248
249 /*
250 * TODO: In case of an error should there be a way
251 * of restoring what we might have torn down at
252 * lower affinity levels.
253 */
254 rc = psci_afflvl_suspend_handlers[level](mpidr,
255 node,
256 entrypoint,
257 context_id,
258 power_state);
259 if (rc != PSCI_E_SUCCESS)
260 break;
261 }
262
263 return rc;
264}
265
266/*******************************************************************************
267 * Top level handler which is called when a cpu wants to suspend its execution.
268 * It is assumed that along with turning the cpu off, higher affinity levels
269 * until the target affinity level will be turned off as well. It traverses
270 * through all the affinity levels performing generic, architectural, platform
271 * setup and state management e.g. for a cluster that's to be suspended, it will
272 * call the platform specific code which will disable coherency at the
273 * interconnect level if the cpu is the last in the cluster. For a cpu it could
274 * mean programming the power controller etc.
275 *
276 * The state of all the relevant affinity levels is changed prior to calling the
277 * affinity level specific handlers as their actions would depend upon the state
278 * the affinity level is about to enter.
279 *
280 * The affinity level specific handlers are called in ascending order i.e. from
281 * the lowest to the highest affinity level implemented by the platform because
282 * to turn off affinity level X it is neccesary to turn off affinity level X - 1
283 * first.
284 *
285 * CAUTION: This function is called with coherent stacks so that coherency can
286 * be turned off and caches can be flushed safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100287 ******************************************************************************/
288int psci_afflvl_suspend(unsigned long mpidr,
289 unsigned long entrypoint,
290 unsigned long context_id,
291 unsigned int power_state,
Achin Gupta0959db52013-12-02 17:33:04 +0000292 int start_afflvl,
293 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100294{
Achin Gupta0959db52013-12-02 17:33:04 +0000295 int rc = PSCI_E_SUCCESS;
296 unsigned int prev_state;
297 mpidr_aff_map_nodes mpidr_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100298
299 mpidr &= MPIDR_AFFINITY_MASK;
300
301 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000302 * Collect the pointers to the nodes in the topology tree for
303 * each affinity instance in the mpidr. If this function does
304 * not return successfully then either the mpidr or the affinity
305 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100306 */
Achin Gupta0959db52013-12-02 17:33:04 +0000307 rc = psci_get_aff_map_nodes(mpidr,
308 start_afflvl,
309 end_afflvl,
310 mpidr_nodes);
311 if (rc != PSCI_E_SUCCESS)
312 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100313
314 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000315 * This function acquires the lock corresponding to each affinity
316 * level so that by the time all locks are taken, the system topology
317 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100318 */
Achin Gupta0959db52013-12-02 17:33:04 +0000319 psci_acquire_afflvl_locks(mpidr,
320 start_afflvl,
321 end_afflvl,
322 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100323
324 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000325 * Keep the old cpu state handy. It will be used to restore the
326 * system to its original state in case something goes wrong
Achin Gupta4f6ad662013-10-25 09:08:21 +0100327 */
Achin Gupta0959db52013-12-02 17:33:04 +0000328 prev_state = psci_get_state(mpidr_nodes[MPIDR_AFFLVL0]->state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100329
330 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000331 * State management: Update the state of each affinity instance
332 * between the start and end affinity levels
Achin Gupta4f6ad662013-10-25 09:08:21 +0100333 */
Achin Gupta0959db52013-12-02 17:33:04 +0000334 psci_change_state(mpidr_nodes,
335 start_afflvl,
336 end_afflvl,
337 PSCI_STATE_SUSPEND);
338
339 /* Perform generic, architecture and platform specific handling */
340 rc = psci_call_suspend_handlers(mpidr_nodes,
341 start_afflvl,
342 end_afflvl,
343 mpidr,
344 entrypoint,
345 context_id,
346 power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100347
348 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000349 * If an error is returned by a handler then restore the cpu state
350 * to its original value. If the cpu state is restored then that
351 * should result in the state of the higher affinity levels to
352 * get restored as well.
353 * TODO: We are not undoing any architectural or platform specific
354 * operations that might have completed before encountering the
355 * error. The system might not be in a stable state.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100356 */
Achin Gupta0959db52013-12-02 17:33:04 +0000357 if (rc != PSCI_E_SUCCESS)
358 psci_change_state(mpidr_nodes,
359 start_afflvl,
360 end_afflvl,
361 prev_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100362
363 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000364 * Release the locks corresponding to each affinity level in the
365 * reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100366 */
Achin Gupta0959db52013-12-02 17:33:04 +0000367 psci_release_afflvl_locks(mpidr,
368 start_afflvl,
369 end_afflvl,
370 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100371
Achin Gupta4f6ad662013-10-25 09:08:21 +0100372 return rc;
373}
374
375/*******************************************************************************
376 * The following functions finish an earlier affinity suspend request. They
377 * are called by the common finisher routine in psci_common.c.
378 ******************************************************************************/
379static unsigned int psci_afflvl0_suspend_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000380 aff_map_node *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100381{
Achin Gupta0959db52013-12-02 17:33:04 +0000382 unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100383
384 assert(cpu_node->level == MPIDR_AFFLVL0);
385
Achin Gupta0959db52013-12-02 17:33:04 +0000386 /* Ensure we have been woken up from a suspended state */
387 state = psci_get_state(cpu_node->state);
388 assert(state == PSCI_STATE_SUSPEND);
389
Achin Gupta4f6ad662013-10-25 09:08:21 +0100390 /*
391 * Plat. management: Perform the platform specific actions
392 * before we change the state of the cpu e.g. enabling the
393 * gic or zeroing the mailbox register. If anything goes
394 * wrong then assert as there is no way to recover from this
395 * situation.
396 */
397 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000398
399 /* Get the physical state of this cpu */
400 plat_state = psci_get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100401 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
402 cpu_node->level,
403 plat_state);
404 assert(rc == PSCI_E_SUCCESS);
405 }
406
407 /* Get the index for restoring the re-entry information */
408 index = cpu_node->data;
409
410 /*
411 * Arch. management: Restore the stashed secure architectural
412 * context in the right order.
413 */
414 write_vbar(psci_secure_context[index].vbar);
Sandrine Bailleux37382742013-11-18 17:26:59 +0000415 write_daif(read_daif() | psci_secure_context[index].pstate);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100416 write_mair(psci_secure_context[index].mair);
417 write_tcr(psci_secure_context[index].tcr);
418 write_ttbr0(psci_secure_context[index].ttbr);
419 write_sctlr(psci_secure_context[index].sctlr);
420
421 /* MMU and coherency should be enabled by now */
422 write_scr(psci_secure_context[index].scr);
423 write_cptr(psci_secure_context[index].cptr);
424 write_cpacr(psci_secure_context[index].cpacr);
425 write_cntfrq_el0(psci_secure_context[index].cntfrq);
426
427 /*
428 * Generic management: Now we just need to retrieve the
429 * information that we had stashed away during the suspend
Achin Gupta3140a9e2013-12-02 16:23:12 +0000430 * call to set this cpu on its way.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100431 */
Achin Guptac8afc782013-11-25 18:45:02 +0000432 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100433
434 /* Clean caches before re-entering normal world */
435 dcsw_op_louis(DCCSW);
436
437 return rc;
438}
439
440static unsigned int psci_afflvl1_suspend_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000441 aff_map_node *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100442{
Achin Gupta0959db52013-12-02 17:33:04 +0000443 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100444
445 assert(cluster_node->level == MPIDR_AFFLVL1);
446
447 /*
448 * Plat. management: Perform the platform specific actions
449 * as per the old state of the cluster e.g. enabling
450 * coherency at the interconnect depends upon the state with
451 * which this cluster was powered up. If anything goes wrong
452 * then assert as there is no way to recover from this
453 * situation.
454 */
455 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000456
457 /* Get the physical state of this cpu */
458 plat_state = psci_get_aff_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100459 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
460 cluster_node->level,
461 plat_state);
462 assert(rc == PSCI_E_SUCCESS);
463 }
464
465 return rc;
466}
467
468
469static unsigned int psci_afflvl2_suspend_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000470 aff_map_node *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100471{
Achin Gupta0959db52013-12-02 17:33:04 +0000472 unsigned int plat_state, rc = PSCI_E_SUCCESS;;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100473
474 /* Cannot go beyond this affinity level */
475 assert(system_node->level == MPIDR_AFFLVL2);
476
477 /*
478 * Currently, there are no architectural actions to perform
479 * at the system level.
480 */
481
482 /*
483 * Plat. management: Perform the platform specific actions
484 * as per the old state of the cluster e.g. enabling
485 * coherency at the interconnect depends upon the state with
486 * which this cluster was powered up. If anything goes wrong
487 * then assert as there is no way to recover from this
488 * situation.
489 */
490 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000491
492 /* Get the physical state of the system */
493 plat_state = psci_get_aff_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100494 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
495 system_node->level,
496 plat_state);
497 assert(rc == PSCI_E_SUCCESS);
498 }
499
500 return rc;
501}
502
503const afflvl_power_on_finisher psci_afflvl_suspend_finishers[] = {
504 psci_afflvl0_suspend_finish,
505 psci_afflvl1_suspend_finish,
506 psci_afflvl2_suspend_finish,
507};
508