blob: 810075b986c51fc4040fc4e6aaa3711ffa225265 [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/*******************************************************************************
229 * This function implements the core of the processing required to suspend a cpu
230 * It'S assumed that along with suspending the cpu, higher affinity levels will
231 * be suspended as far as possible. Suspending a cpu is equivalent to physically
232 * powering it down, but the cpu is still available to the OS for scheduling.
233 * We first need to determine the new state off all the affinity instances in
234 * the mpidr corresponding to the target cpu. Action will be taken on the basis
235 * of this new state. To do the state change we first need to acquire the locks
236 * for all the implemented affinity level to be able to snapshot the system
237 * state. Then we need to start suspending affinity levels from the lowest to
238 * the highest (e.g. a cpu needs to be suspended before a cluster can be). To
239 * achieve this flow, we start acquiring the locks from the highest to the
240 * lowest affinity level. Once we reach affinity level 0, we do the state change
241 * followed by the actions corresponding to the new state for affinity level 0.
242 * Actions as per the updated state for higher affinity levels are performed as
243 * we unwind back to highest affinity level.
244 ******************************************************************************/
245int psci_afflvl_suspend(unsigned long mpidr,
246 unsigned long entrypoint,
247 unsigned long context_id,
248 unsigned int power_state,
249 int cur_afflvl,
250 int tgt_afflvl)
251{
252 int rc = PSCI_E_SUCCESS, level;
253 unsigned int prev_state, next_state;
254 aff_map_node *aff_node;
255
256 mpidr &= MPIDR_AFFINITY_MASK;
257
258 /*
259 * Some affinity instances at levels between the current and
260 * target levels could be absent in the mpidr. Skip them and
261 * start from the first present instance.
262 */
263 level = psci_get_first_present_afflvl(mpidr,
264 cur_afflvl,
265 tgt_afflvl,
266 &aff_node);
267
268 /*
269 * Return if there are no more affinity instances beyond this
270 * level to process. Else ensure that the returned affinity
271 * node makes sense.
272 */
273 if (aff_node == NULL)
274 return rc;
275
276 assert(level == aff_node->level);
277
278 /*
279 * This function acquires the lock corresponding to each
280 * affinity level so that state management can be done safely.
281 */
282 bakery_lock_get(mpidr, &aff_node->lock);
283
284 /* Keep the old state and the next one handy */
285 prev_state = psci_get_state(aff_node->state);
286 next_state = PSCI_STATE_SUSPEND;
287
288 /*
289 * We start from the highest affinity level and work our way
290 * downwards to the lowest i.e. MPIDR_AFFLVL0.
291 */
292 if (aff_node->level == tgt_afflvl) {
293 psci_change_state(mpidr,
294 tgt_afflvl,
295 get_max_afflvl(),
296 next_state);
297 } else {
298 rc = psci_afflvl_suspend(mpidr,
299 entrypoint,
300 context_id,
301 power_state,
302 level - 1,
303 tgt_afflvl);
304 if (rc != PSCI_E_SUCCESS) {
305 psci_set_state(aff_node->state, prev_state);
306 goto exit;
307 }
308 }
309
310 /*
311 * Perform generic, architecture and platform specific
312 * handling
313 */
314 rc = psci_afflvl_suspend_handlers[level](mpidr,
315 aff_node,
316 entrypoint,
317 context_id,
318 power_state);
319 if (rc != PSCI_E_SUCCESS) {
320 psci_set_state(aff_node->state, prev_state);
321 goto exit;
322 }
323
324 /*
325 * If all has gone as per plan then this cpu should be
326 * marked as OFF
327 */
328 if (level == MPIDR_AFFLVL0) {
329 next_state = psci_get_state(aff_node->state);
330 assert(next_state == PSCI_STATE_SUSPEND);
331 }
332
333exit:
334 bakery_lock_release(mpidr, &aff_node->lock);
335 return rc;
336}
337
338/*******************************************************************************
339 * The following functions finish an earlier affinity suspend request. They
340 * are called by the common finisher routine in psci_common.c.
341 ******************************************************************************/
342static unsigned int psci_afflvl0_suspend_finish(unsigned long mpidr,
343 aff_map_node *cpu_node,
344 unsigned int prev_state)
345{
346 unsigned int index, plat_state, rc = 0;
347
348 assert(cpu_node->level == MPIDR_AFFLVL0);
349
350 /*
351 * Plat. management: Perform the platform specific actions
352 * before we change the state of the cpu e.g. enabling the
353 * gic or zeroing the mailbox register. If anything goes
354 * wrong then assert as there is no way to recover from this
355 * situation.
356 */
357 if (psci_plat_pm_ops->affinst_suspend_finish) {
358 plat_state = psci_get_phys_state(prev_state);
359 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
360 cpu_node->level,
361 plat_state);
362 assert(rc == PSCI_E_SUCCESS);
363 }
364
365 /* Get the index for restoring the re-entry information */
366 index = cpu_node->data;
367
368 /*
369 * Arch. management: Restore the stashed secure architectural
370 * context in the right order.
371 */
372 write_vbar(psci_secure_context[index].vbar);
Sandrine Bailleux37382742013-11-18 17:26:59 +0000373 write_daif(read_daif() | psci_secure_context[index].pstate);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100374 write_mair(psci_secure_context[index].mair);
375 write_tcr(psci_secure_context[index].tcr);
376 write_ttbr0(psci_secure_context[index].ttbr);
377 write_sctlr(psci_secure_context[index].sctlr);
378
379 /* MMU and coherency should be enabled by now */
380 write_scr(psci_secure_context[index].scr);
381 write_cptr(psci_secure_context[index].cptr);
382 write_cpacr(psci_secure_context[index].cpacr);
383 write_cntfrq_el0(psci_secure_context[index].cntfrq);
384
385 /*
386 * Generic management: Now we just need to retrieve the
387 * information that we had stashed away during the suspend
Achin Gupta3140a9e2013-12-02 16:23:12 +0000388 * call to set this cpu on its way.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100389 */
Achin Guptac8afc782013-11-25 18:45:02 +0000390 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100391
392 /* Clean caches before re-entering normal world */
393 dcsw_op_louis(DCCSW);
394
395 return rc;
396}
397
398static unsigned int psci_afflvl1_suspend_finish(unsigned long mpidr,
399 aff_map_node *cluster_node,
400 unsigned int prev_state)
401{
402 unsigned int rc = 0;
403 unsigned int plat_state;
404
405 assert(cluster_node->level == MPIDR_AFFLVL1);
406
407 /*
408 * Plat. management: Perform the platform specific actions
409 * as per the old state of the cluster e.g. enabling
410 * coherency at the interconnect depends upon the state with
411 * which this cluster was powered up. If anything goes wrong
412 * then assert as there is no way to recover from this
413 * situation.
414 */
415 if (psci_plat_pm_ops->affinst_suspend_finish) {
416 plat_state = psci_get_phys_state(prev_state);
417 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
418 cluster_node->level,
419 plat_state);
420 assert(rc == PSCI_E_SUCCESS);
421 }
422
423 return rc;
424}
425
426
427static unsigned int psci_afflvl2_suspend_finish(unsigned long mpidr,
428 aff_map_node *system_node,
429 unsigned int target_afflvl)
430{
431 int rc = PSCI_E_SUCCESS;
432 unsigned int plat_state;
433
434 /* Cannot go beyond this affinity level */
435 assert(system_node->level == MPIDR_AFFLVL2);
436
437 /*
438 * Currently, there are no architectural actions to perform
439 * at the system level.
440 */
441
442 /*
443 * Plat. management: Perform the platform specific actions
444 * as per the old state of the cluster e.g. enabling
445 * coherency at the interconnect depends upon the state with
446 * which this cluster was powered up. If anything goes wrong
447 * then assert as there is no way to recover from this
448 * situation.
449 */
450 if (psci_plat_pm_ops->affinst_suspend_finish) {
451 plat_state = psci_get_phys_state(system_node->state);
452 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
453 system_node->level,
454 plat_state);
455 assert(rc == PSCI_E_SUCCESS);
456 }
457
458 return rc;
459}
460
461const afflvl_power_on_finisher psci_afflvl_suspend_finishers[] = {
462 psci_afflvl0_suspend_finish,
463 psci_afflvl1_suspend_finish,
464 psci_afflvl2_suspend_finish,
465};
466