blob: f43dcedf1e6f857cdc0f413516f9682bc02d4cf4 [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
Achin Gupta4f6ad662013-10-25 09:08:21 +010031#include <assert.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010032#include <bl_common.h>
33#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010034#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010035#include <context.h>
Achin Guptaef7a28c2014-02-01 08:59:56 +000036#include <context_mgmt.h>
Dan Handleybcd60ba2014-04-17 18:53:42 +010037#include <runtime_svc.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010038#include <stddef.h>
Dan Handley714a0d22014-04-09 13:13:04 +010039#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010040
Dan Handleye2712bc2014-04-10 15:37:22 +010041typedef int (*afflvl_suspend_handler_t)(unsigned long,
42 aff_map_node_t *,
Achin Gupta4f6ad662013-10-25 09:08:21 +010043 unsigned long,
44 unsigned long,
45 unsigned int);
46
47/*******************************************************************************
Vikram Kanigirif100f412014-04-01 19:26:26 +010048 * This function sets the power state of the current cpu while
49 * powering down during a cpu_suspend call
Achin Guptaa45e3972013-12-05 15:10:48 +000050 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010051void psci_set_suspend_power_state(aff_map_node_t *node, unsigned int power_state)
Achin Guptaa45e3972013-12-05 15:10:48 +000052{
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
Vikram Kanigirif100f412014-04-01 19:26:26 +010060 /* Save PSCI power state parameter for the core in suspend context */
61 psci_suspend_context[node->data].power_state = power_state;
62
Achin Guptaa45e3972013-12-05 15:10:48 +000063 /*
Vikram Kanigirif100f412014-04-01 19:26:26 +010064 * Flush the suspend data to PoC since it will be accessed while
65 * returning back from suspend with the caches turned off
Achin Guptaa45e3972013-12-05 15:10:48 +000066 */
Vikram Kanigirif100f412014-04-01 19:26:26 +010067 flush_dcache_range(
68 (unsigned long)&psci_suspend_context[node->data],
Dan Handleye2712bc2014-04-10 15:37:22 +010069 sizeof(suspend_context_t));
Achin Guptaa45e3972013-12-05 15:10:48 +000070}
71
72/*******************************************************************************
Vikram Kanigirif100f412014-04-01 19:26:26 +010073 * This function gets the affinity level till which a cpu is powered down
74 * during a cpu_suspend call. Returns PSCI_INVALID_DATA if the
75 * power state saved for the node is invalid
76 ******************************************************************************/
77int psci_get_suspend_afflvl(unsigned long mpidr)
78{
Dan Handleye2712bc2014-04-10 15:37:22 +010079 aff_map_node_t *node;
Vikram Kanigirif100f412014-04-01 19:26:26 +010080
81 node = psci_get_aff_map_node(mpidr & MPIDR_AFFINITY_MASK,
82 MPIDR_AFFLVL0);
83 assert(node);
84
85 return psci_get_aff_map_node_suspend_afflvl(node);
86}
87
88
89/*******************************************************************************
Achin Guptaa45e3972013-12-05 15:10:48 +000090 * This function gets the affinity level till which the current cpu was powered
Vikram Kanigirif100f412014-04-01 19:26:26 +010091 * down during a cpu_suspend call. Returns PSCI_INVALID_DATA if the
92 * power state saved for the node is invalid
93 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010094int psci_get_aff_map_node_suspend_afflvl(aff_map_node_t *node)
Vikram Kanigirif100f412014-04-01 19:26:26 +010095{
96 unsigned int power_state;
97
98 assert(node->level == MPIDR_AFFLVL0);
99
100 power_state = psci_suspend_context[node->data].power_state;
101 return ((power_state == PSCI_INVALID_DATA) ?
102 power_state : psci_get_pstate_afflvl(power_state));
103}
104
105/*******************************************************************************
106 * This function gets the state id of a cpu stored in suspend context
107 * while powering down during a cpu_suspend call. Returns 0xFFFFFFFF
108 * if the power state saved for the node is invalid
Achin Guptaa45e3972013-12-05 15:10:48 +0000109 ******************************************************************************/
Vikram Kanigirif100f412014-04-01 19:26:26 +0100110int psci_get_suspend_stateid(unsigned long mpidr)
Achin Guptaa45e3972013-12-05 15:10:48 +0000111{
Dan Handleye2712bc2014-04-10 15:37:22 +0100112 aff_map_node_t *node;
Vikram Kanigirif100f412014-04-01 19:26:26 +0100113 unsigned int power_state;
114
115 node = psci_get_aff_map_node(mpidr & MPIDR_AFFINITY_MASK,
116 MPIDR_AFFLVL0);
117 assert(node);
118 assert(node->level == MPIDR_AFFLVL0);
119
120 power_state = psci_suspend_context[node->data].power_state;
121 return ((power_state == PSCI_INVALID_DATA) ?
122 power_state : psci_get_pstate_id(power_state));
Achin Guptaa45e3972013-12-05 15:10:48 +0000123}
124
125/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +0100126 * The next three functions implement a handler for each supported affinity
127 * level which is called when that affinity level is about to be suspended.
128 ******************************************************************************/
129static int psci_afflvl0_suspend(unsigned long mpidr,
Dan Handleye2712bc2014-04-10 15:37:22 +0100130 aff_map_node_t *cpu_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100131 unsigned long ns_entrypoint,
132 unsigned long context_id,
133 unsigned int power_state)
134{
Andrew Thoelke4e126072014-06-04 21:10:52 +0100135 unsigned int plat_state;
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +0000136 unsigned long psci_entrypoint, sctlr;
Dan Handleye2712bc2014-04-10 15:37:22 +0100137 el3_state_t *saved_el3_state;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100138 uint32_t ns_scr_el3 = read_scr_el3();
139 uint32_t ns_sctlr_el1 = read_sctlr_el1();
140 int rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100141
142 /* Sanity check to safeguard against data corruption */
143 assert(cpu_node->level == MPIDR_AFFLVL0);
144
Vikram Kanigirif100f412014-04-01 19:26:26 +0100145 /* Save PSCI power state parameter for the core in suspend context */
146 psci_set_suspend_power_state(cpu_node, power_state);
147
Achin Gupta607084e2014-02-09 18:24:19 +0000148 /*
149 * Generic management: Store the re-entry information for the non-secure
150 * world and allow the secure world to suspend itself
151 */
152
153 /*
154 * Call the cpu suspend handler registered by the Secure Payload
155 * Dispatcher to let it do any bookeeping. If the handler encounters an
156 * error, it's expected to assert within
157 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000158 if (psci_spd_pm && psci_spd_pm->svc_suspend)
159 psci_spd_pm->svc_suspend(power_state);
Achin Gupta607084e2014-02-09 18:24:19 +0000160
Achin Gupta75f73672013-12-05 16:33:10 +0000161 /* State management: mark this cpu as suspended */
162 psci_set_state(cpu_node, PSCI_STATE_SUSPEND);
163
Achin Gupta4f6ad662013-10-25 09:08:21 +0100164 /*
165 * Generic management: Store the re-entry information for the
166 * non-secure world
167 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100168 rc = psci_save_ns_entry(read_mpidr_el1(), ns_entrypoint, context_id,
169 ns_scr_el3, ns_sctlr_el1);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100170 if (rc != PSCI_E_SUCCESS)
171 return rc;
172
173 /*
Achin Guptaef7a28c2014-02-01 08:59:56 +0000174 * Arch. management: Save the EL3 state in the 'cpu_context'
175 * structure that has been allocated for this cpu, flush the
Achin Gupta4f6ad662013-10-25 09:08:21 +0100176 * L1 caches and exit intra-cluster coherency et al
177 */
Achin Guptaef7a28c2014-02-01 08:59:56 +0000178 cm_el3_sysregs_context_save(NON_SECURE);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100179
Achin Gupta0a9f7472014-02-09 17:48:12 +0000180 /*
181 * The EL3 state to PoC since it will be accessed after a
182 * reset with the caches turned off
183 */
Andrew Thoelkea2f65532014-05-14 17:09:32 +0100184 saved_el3_state = get_el3state_ctx(cm_get_context(NON_SECURE));
Achin Gupta0a9f7472014-02-09 17:48:12 +0000185 flush_dcache_range((uint64_t) saved_el3_state, sizeof(*saved_el3_state));
186
Achin Gupta4f6ad662013-10-25 09:08:21 +0100187 /* Set the secure world (EL3) re-entry point after BL1 */
188 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
189
190 /*
191 * Arch. management. Perform the necessary steps to flush all
192 * cpu caches.
193 *
194 * TODO: This power down sequence varies across cpus so it needs to be
195 * abstracted out on the basis of the MIDR like in cpu_reset_handler().
196 * Do the bare minimal for the time being. Fix this before porting to
197 * Cortex models.
198 */
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +0000199 sctlr = read_sctlr_el3();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100200 sctlr &= ~SCTLR_C_BIT;
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +0000201 write_sctlr_el3(sctlr);
Andrew Thoelke42e75a72014-04-28 12:28:39 +0100202 isb(); /* ensure MMU disable takes immediate effect */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100203
204 /*
205 * CAUTION: This flush to the level of unification makes an assumption
206 * about the cache hierarchy at affinity level 0 (cpu) in the platform.
207 * Ideally the platform should tell psci which levels to flush to exit
208 * coherency.
209 */
210 dcsw_op_louis(DCCISW);
211
212 /*
213 * Plat. management: Allow the platform to perform the
214 * necessary actions to turn off this cpu e.g. set the
215 * platform defined mailbox with the psci entrypoint,
216 * program the power controller etc.
217 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100218 rc = PSCI_E_SUCCESS;
219
Achin Gupta4f6ad662013-10-25 09:08:21 +0100220 if (psci_plat_pm_ops->affinst_suspend) {
Achin Gupta75f73672013-12-05 16:33:10 +0000221 plat_state = psci_get_phys_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100222 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
223 psci_entrypoint,
224 ns_entrypoint,
225 cpu_node->level,
226 plat_state);
227 }
228
229 return rc;
230}
231
232static int psci_afflvl1_suspend(unsigned long mpidr,
Dan Handleye2712bc2014-04-10 15:37:22 +0100233 aff_map_node_t *cluster_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100234 unsigned long ns_entrypoint,
235 unsigned long context_id,
236 unsigned int power_state)
237{
238 int rc = PSCI_E_SUCCESS;
239 unsigned int plat_state;
240 unsigned long psci_entrypoint;
241
242 /* Sanity check the cluster level */
243 assert(cluster_node->level == MPIDR_AFFLVL1);
244
Achin Gupta75f73672013-12-05 16:33:10 +0000245 /* State management: Decrement the cluster reference count */
246 psci_set_state(cluster_node, PSCI_STATE_SUSPEND);
247
Achin Gupta4f6ad662013-10-25 09:08:21 +0100248 /*
249 * Keep the physical state of this cluster handy to decide
250 * what action needs to be taken
251 */
Achin Gupta75f73672013-12-05 16:33:10 +0000252 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100253
254 /*
255 * Arch. management: Flush all levels of caches to PoC if the
256 * cluster is to be shutdown
257 */
258 if (plat_state == PSCI_STATE_OFF)
259 dcsw_op_all(DCCISW);
260
261 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000262 * Plat. Management. Allow the platform to do its cluster
Achin Gupta4f6ad662013-10-25 09:08:21 +0100263 * specific bookeeping e.g. turn off interconnect coherency,
264 * program the power controller etc.
265 */
266 if (psci_plat_pm_ops->affinst_suspend) {
267
268 /*
269 * Sending the psci entrypoint is currently redundant
270 * beyond affinity level 0 but one never knows what a
271 * platform might do. Also it allows us to keep the
272 * platform handler prototype the same.
273 */
274 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100275 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
276 psci_entrypoint,
277 ns_entrypoint,
278 cluster_node->level,
279 plat_state);
280 }
281
282 return rc;
283}
284
285
286static int psci_afflvl2_suspend(unsigned long mpidr,
Dan Handleye2712bc2014-04-10 15:37:22 +0100287 aff_map_node_t *system_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100288 unsigned long ns_entrypoint,
289 unsigned long context_id,
290 unsigned int power_state)
291{
292 int rc = PSCI_E_SUCCESS;
293 unsigned int plat_state;
294 unsigned long psci_entrypoint;
295
296 /* Cannot go beyond this */
297 assert(system_node->level == MPIDR_AFFLVL2);
298
Achin Gupta75f73672013-12-05 16:33:10 +0000299 /* State management: Decrement the system reference count */
300 psci_set_state(system_node, PSCI_STATE_SUSPEND);
301
Achin Gupta4f6ad662013-10-25 09:08:21 +0100302 /*
303 * Keep the physical state of the system handy to decide what
304 * action needs to be taken
305 */
Achin Gupta75f73672013-12-05 16:33:10 +0000306 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100307
308 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000309 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100310 * at this affinity level
311 */
312 if (psci_plat_pm_ops->affinst_suspend) {
313
314 /*
315 * Sending the psci entrypoint is currently redundant
316 * beyond affinity level 0 but one never knows what a
317 * platform might do. Also it allows us to keep the
318 * platform handler prototype the same.
319 */
320 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100321 rc = psci_plat_pm_ops->affinst_suspend(mpidr,
322 psci_entrypoint,
323 ns_entrypoint,
324 system_node->level,
325 plat_state);
326 }
327
328 return rc;
329}
330
Dan Handleye2712bc2014-04-10 15:37:22 +0100331static const afflvl_suspend_handler_t psci_afflvl_suspend_handlers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100332 psci_afflvl0_suspend,
333 psci_afflvl1_suspend,
334 psci_afflvl2_suspend,
335};
336
337/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000338 * This function takes an array of pointers to affinity instance nodes in the
339 * topology tree and calls the suspend handler for the corresponding affinity
340 * levels
341 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100342static int psci_call_suspend_handlers(mpidr_aff_map_nodes_t mpidr_nodes,
Achin Gupta0959db52013-12-02 17:33:04 +0000343 int start_afflvl,
344 int end_afflvl,
345 unsigned long mpidr,
346 unsigned long entrypoint,
347 unsigned long context_id,
348 unsigned int power_state)
349{
350 int rc = PSCI_E_INVALID_PARAMS, level;
Dan Handleye2712bc2014-04-10 15:37:22 +0100351 aff_map_node_t *node;
Achin Gupta0959db52013-12-02 17:33:04 +0000352
353 for (level = start_afflvl; level <= end_afflvl; level++) {
354 node = mpidr_nodes[level];
355 if (node == NULL)
356 continue;
357
358 /*
359 * TODO: In case of an error should there be a way
360 * of restoring what we might have torn down at
361 * lower affinity levels.
362 */
363 rc = psci_afflvl_suspend_handlers[level](mpidr,
364 node,
365 entrypoint,
366 context_id,
367 power_state);
368 if (rc != PSCI_E_SUCCESS)
369 break;
370 }
371
372 return rc;
373}
374
375/*******************************************************************************
376 * Top level handler which is called when a cpu wants to suspend its execution.
377 * It is assumed that along with turning the cpu off, higher affinity levels
378 * until the target affinity level will be turned off as well. It traverses
379 * through all the affinity levels performing generic, architectural, platform
380 * setup and state management e.g. for a cluster that's to be suspended, it will
381 * call the platform specific code which will disable coherency at the
382 * interconnect level if the cpu is the last in the cluster. For a cpu it could
383 * mean programming the power controller etc.
384 *
385 * The state of all the relevant affinity levels is changed prior to calling the
386 * affinity level specific handlers as their actions would depend upon the state
387 * the affinity level is about to enter.
388 *
389 * The affinity level specific handlers are called in ascending order i.e. from
390 * the lowest to the highest affinity level implemented by the platform because
391 * to turn off affinity level X it is neccesary to turn off affinity level X - 1
392 * first.
393 *
394 * CAUTION: This function is called with coherent stacks so that coherency can
395 * be turned off and caches can be flushed safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100396 ******************************************************************************/
397int psci_afflvl_suspend(unsigned long mpidr,
398 unsigned long entrypoint,
399 unsigned long context_id,
400 unsigned int power_state,
Achin Gupta0959db52013-12-02 17:33:04 +0000401 int start_afflvl,
402 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100403{
Achin Gupta0959db52013-12-02 17:33:04 +0000404 int rc = PSCI_E_SUCCESS;
Dan Handleye2712bc2014-04-10 15:37:22 +0100405 mpidr_aff_map_nodes_t mpidr_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100406
407 mpidr &= MPIDR_AFFINITY_MASK;
408
409 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000410 * Collect the pointers to the nodes in the topology tree for
411 * each affinity instance in the mpidr. If this function does
412 * not return successfully then either the mpidr or the affinity
413 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100414 */
Achin Gupta0959db52013-12-02 17:33:04 +0000415 rc = psci_get_aff_map_nodes(mpidr,
416 start_afflvl,
417 end_afflvl,
418 mpidr_nodes);
419 if (rc != PSCI_E_SUCCESS)
420 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100421
422 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000423 * This function acquires the lock corresponding to each affinity
424 * level so that by the time all locks are taken, the system topology
425 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100426 */
Achin Gupta0959db52013-12-02 17:33:04 +0000427 psci_acquire_afflvl_locks(mpidr,
428 start_afflvl,
429 end_afflvl,
430 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100431
Achin Gupta0959db52013-12-02 17:33:04 +0000432 /* Perform generic, architecture and platform specific handling */
433 rc = psci_call_suspend_handlers(mpidr_nodes,
434 start_afflvl,
435 end_afflvl,
436 mpidr,
437 entrypoint,
438 context_id,
439 power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100440
441 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000442 * Release the locks corresponding to each affinity level in the
443 * reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100444 */
Achin Gupta0959db52013-12-02 17:33:04 +0000445 psci_release_afflvl_locks(mpidr,
446 start_afflvl,
447 end_afflvl,
448 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100449
Achin Gupta4f6ad662013-10-25 09:08:21 +0100450 return rc;
451}
452
453/*******************************************************************************
454 * The following functions finish an earlier affinity suspend request. They
455 * are called by the common finisher routine in psci_common.c.
456 ******************************************************************************/
457static unsigned int psci_afflvl0_suspend_finish(unsigned long mpidr,
Dan Handleye2712bc2014-04-10 15:37:22 +0100458 aff_map_node_t *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100459{
Andrew Thoelke4e126072014-06-04 21:10:52 +0100460 unsigned int plat_state, state, rc;
Achin Gupta607084e2014-02-09 18:24:19 +0000461 int32_t suspend_level;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100462
463 assert(cpu_node->level == MPIDR_AFFLVL0);
464
Achin Gupta0959db52013-12-02 17:33:04 +0000465 /* Ensure we have been woken up from a suspended state */
Achin Gupta75f73672013-12-05 16:33:10 +0000466 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000467 assert(state == PSCI_STATE_SUSPEND);
468
Achin Gupta4f6ad662013-10-25 09:08:21 +0100469 /*
470 * Plat. management: Perform the platform specific actions
471 * before we change the state of the cpu e.g. enabling the
472 * gic or zeroing the mailbox register. If anything goes
473 * wrong then assert as there is no way to recover from this
474 * situation.
475 */
476 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000477
478 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000479 plat_state = get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100480 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
481 cpu_node->level,
482 plat_state);
483 assert(rc == PSCI_E_SUCCESS);
484 }
485
486 /* Get the index for restoring the re-entry information */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100487 /*
Achin Guptaef7a28c2014-02-01 08:59:56 +0000488 * Arch. management: Restore the stashed EL3 architectural
489 * context from the 'cpu_context' structure for this cpu.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100490 */
Achin Guptaef7a28c2014-02-01 08:59:56 +0000491 cm_el3_sysregs_context_restore(NON_SECURE);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100492
493 /*
Achin Gupta607084e2014-02-09 18:24:19 +0000494 * Call the cpu suspend finish handler registered by the Secure Payload
495 * Dispatcher to let it do any bookeeping. If the handler encounters an
496 * error, it's expected to assert within
497 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000498 if (psci_spd_pm && psci_spd_pm->svc_suspend) {
Vikram Kanigirif100f412014-04-01 19:26:26 +0100499 suspend_level = psci_get_aff_map_node_suspend_afflvl(cpu_node);
500 assert (suspend_level != PSCI_INVALID_DATA);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000501 psci_spd_pm->svc_suspend_finish(suspend_level);
Achin Gupta607084e2014-02-09 18:24:19 +0000502 }
503
Vikram Kanigirif100f412014-04-01 19:26:26 +0100504 /* Invalidate the suspend context for the node */
505 psci_set_suspend_power_state(cpu_node, PSCI_INVALID_DATA);
506
Achin Gupta607084e2014-02-09 18:24:19 +0000507 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100508 * Generic management: Now we just need to retrieve the
509 * information that we had stashed away during the suspend
Achin Gupta3140a9e2013-12-02 16:23:12 +0000510 * call to set this cpu on its way.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100511 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100512 cm_prepare_el3_exit(NON_SECURE);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100513
Achin Gupta75f73672013-12-05 16:33:10 +0000514 /* State management: mark this cpu as on */
515 psci_set_state(cpu_node, PSCI_STATE_ON);
516
Achin Gupta4f6ad662013-10-25 09:08:21 +0100517 /* Clean caches before re-entering normal world */
518 dcsw_op_louis(DCCSW);
519
Andrew Thoelke4e126072014-06-04 21:10:52 +0100520 rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100521 return rc;
522}
523
524static unsigned int psci_afflvl1_suspend_finish(unsigned long mpidr,
Dan Handleye2712bc2014-04-10 15:37:22 +0100525 aff_map_node_t *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100526{
Achin Gupta0959db52013-12-02 17:33:04 +0000527 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100528
529 assert(cluster_node->level == MPIDR_AFFLVL1);
530
531 /*
532 * Plat. management: Perform the platform specific actions
533 * as per the old state of the cluster e.g. enabling
534 * coherency at the interconnect depends upon the state with
535 * which this cluster was powered up. If anything goes wrong
536 * then assert as there is no way to recover from this
537 * situation.
538 */
539 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000540
541 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000542 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100543 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
544 cluster_node->level,
545 plat_state);
546 assert(rc == PSCI_E_SUCCESS);
547 }
548
Achin Gupta75f73672013-12-05 16:33:10 +0000549 /* State management: Increment the cluster reference count */
550 psci_set_state(cluster_node, PSCI_STATE_ON);
551
Achin Gupta4f6ad662013-10-25 09:08:21 +0100552 return rc;
553}
554
555
556static unsigned int psci_afflvl2_suspend_finish(unsigned long mpidr,
Dan Handleye2712bc2014-04-10 15:37:22 +0100557 aff_map_node_t *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100558{
Achin Gupta0959db52013-12-02 17:33:04 +0000559 unsigned int plat_state, rc = PSCI_E_SUCCESS;;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100560
561 /* Cannot go beyond this affinity level */
562 assert(system_node->level == MPIDR_AFFLVL2);
563
564 /*
565 * Currently, there are no architectural actions to perform
566 * at the system level.
567 */
568
569 /*
570 * Plat. management: Perform the platform specific actions
571 * as per the old state of the cluster e.g. enabling
572 * coherency at the interconnect depends upon the state with
573 * which this cluster was powered up. If anything goes wrong
574 * then assert as there is no way to recover from this
575 * situation.
576 */
577 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000578
579 /* Get the physical state of the system */
Achin Gupta75f73672013-12-05 16:33:10 +0000580 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100581 rc = psci_plat_pm_ops->affinst_suspend_finish(mpidr,
582 system_node->level,
583 plat_state);
584 assert(rc == PSCI_E_SUCCESS);
585 }
586
Achin Gupta75f73672013-12-05 16:33:10 +0000587 /* State management: Increment the system reference count */
588 psci_set_state(system_node, PSCI_STATE_ON);
589
Achin Gupta4f6ad662013-10-25 09:08:21 +0100590 return rc;
591}
592
Dan Handleye2712bc2014-04-10 15:37:22 +0100593const afflvl_power_on_finisher_t psci_afflvl_suspend_finishers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100594 psci_afflvl0_suspend_finish,
595 psci_afflvl1_suspend_finish,
596 psci_afflvl2_suspend_finish,
597};
598