blob: 54f263482cfe4f2d3bdf96bc0e77fa0821402023 [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>
Achin Guptaf3ccbab2014-07-25 14:52:47 +010037#include <cpu_data.h>
Soby Mathew2ed46e92014-07-04 16:02:26 +010038#include <platform.h>
Dan Handleybcd60ba2014-04-17 18:53:42 +010039#include <runtime_svc.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010040#include <stddef.h>
Dan Handley714a0d22014-04-09 13:13:04 +010041#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010042
Andrew Thoelke2bc07852014-06-09 12:44:21 +010043typedef int (*afflvl_suspend_handler_t)(aff_map_node_t *,
Achin Gupta4f6ad662013-10-25 09:08:21 +010044 unsigned long,
45 unsigned long,
46 unsigned int);
47
48/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010049 * This function saves the power state parameter passed in the current PSCI
50 * cpu_suspend call in the per-cpu data array.
Achin Guptaa45e3972013-12-05 15:10:48 +000051 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010052void psci_set_suspend_power_state(unsigned int power_state)
Achin Guptaa45e3972013-12-05 15:10:48 +000053{
Achin Guptaf3ccbab2014-07-25 14:52:47 +010054 set_cpu_data(psci_svc_cpu_data.power_state, power_state);
55 flush_cpu_data(psci_svc_cpu_data.power_state);
Achin Guptaa45e3972013-12-05 15:10:48 +000056}
57
58/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010059 * This function gets the affinity level till which the current cpu could be
60 * powered down during a cpu_suspend call. Returns PSCI_INVALID_DATA if the
61 * power state is invalid.
Vikram Kanigirif100f412014-04-01 19:26:26 +010062 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010063int psci_get_suspend_afflvl()
Vikram Kanigirif100f412014-04-01 19:26:26 +010064{
Achin Guptaf3ccbab2014-07-25 14:52:47 +010065 unsigned int power_state;
Vikram Kanigirif100f412014-04-01 19:26:26 +010066
Achin Guptaf3ccbab2014-07-25 14:52:47 +010067 power_state = get_cpu_data(psci_svc_cpu_data.power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +010068
Achin Guptaf3ccbab2014-07-25 14:52:47 +010069 return ((power_state == PSCI_INVALID_DATA) ?
70 power_state : psci_get_pstate_afflvl(power_state));
Vikram Kanigirif100f412014-04-01 19:26:26 +010071}
72
Vikram Kanigirif100f412014-04-01 19:26:26 +010073/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010074 * This function gets the state id of the current cpu from the power state
75 * parameter saved in the per-cpu data array. Returns PSCI_INVALID_DATA if the
76 * power state saved is invalid.
Vikram Kanigirif100f412014-04-01 19:26:26 +010077 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010078int psci_get_suspend_stateid()
Vikram Kanigirif100f412014-04-01 19:26:26 +010079{
80 unsigned int power_state;
81
Achin Guptaf3ccbab2014-07-25 14:52:47 +010082 power_state = get_cpu_data(psci_svc_cpu_data.power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +010083
Vikram Kanigirif100f412014-04-01 19:26:26 +010084 return ((power_state == PSCI_INVALID_DATA) ?
Achin Guptaf3ccbab2014-07-25 14:52:47 +010085 power_state : psci_get_pstate_id(power_state));
Vikram Kanigirif100f412014-04-01 19:26:26 +010086}
87
88/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010089 * This function gets the state id of the cpu specified by the 'mpidr' parameter
90 * from the power state parameter saved in the per-cpu data array. Returns
91 * PSCI_INVALID_DATA if the power state saved is invalid.
Achin Guptaa45e3972013-12-05 15:10:48 +000092 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010093int psci_get_suspend_stateid_by_mpidr(unsigned long mpidr)
Achin Guptaa45e3972013-12-05 15:10:48 +000094{
Vikram Kanigirif100f412014-04-01 19:26:26 +010095 unsigned int power_state;
96
Achin Guptaf3ccbab2014-07-25 14:52:47 +010097 power_state = get_cpu_data_by_mpidr(mpidr,
98 psci_svc_cpu_data.power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +010099
Vikram Kanigirif100f412014-04-01 19:26:26 +0100100 return ((power_state == PSCI_INVALID_DATA) ?
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100101 power_state : psci_get_pstate_id(power_state));
Achin Guptaa45e3972013-12-05 15:10:48 +0000102}
103
104/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +0100105 * The next three functions implement a handler for each supported affinity
106 * level which is called when that affinity level is about to be suspended.
107 ******************************************************************************/
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100108static int psci_afflvl0_suspend(aff_map_node_t *cpu_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100109 unsigned long ns_entrypoint,
110 unsigned long context_id,
111 unsigned int power_state)
112{
Andrew Thoelke4e126072014-06-04 21:10:52 +0100113 unsigned int plat_state;
Achin Guptae1aa5162014-06-26 09:58:52 +0100114 unsigned long psci_entrypoint;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100115 uint32_t ns_scr_el3 = read_scr_el3();
116 uint32_t ns_sctlr_el1 = read_sctlr_el1();
117 int rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100118
119 /* Sanity check to safeguard against data corruption */
120 assert(cpu_node->level == MPIDR_AFFLVL0);
121
Vikram Kanigirif100f412014-04-01 19:26:26 +0100122 /* Save PSCI power state parameter for the core in suspend context */
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100123 psci_set_suspend_power_state(power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +0100124
Achin Gupta607084e2014-02-09 18:24:19 +0000125 /*
126 * Generic management: Store the re-entry information for the non-secure
127 * world and allow the secure world to suspend itself
128 */
129
130 /*
131 * Call the cpu suspend handler registered by the Secure Payload
132 * Dispatcher to let it do any bookeeping. If the handler encounters an
133 * error, it's expected to assert within
134 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000135 if (psci_spd_pm && psci_spd_pm->svc_suspend)
136 psci_spd_pm->svc_suspend(power_state);
Achin Gupta607084e2014-02-09 18:24:19 +0000137
Achin Gupta4f6ad662013-10-25 09:08:21 +0100138 /*
139 * Generic management: Store the re-entry information for the
140 * non-secure world
141 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100142 rc = psci_save_ns_entry(read_mpidr_el1(), ns_entrypoint, context_id,
143 ns_scr_el3, ns_sctlr_el1);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100144 if (rc != PSCI_E_SUCCESS)
145 return rc;
146
Achin Gupta4f6ad662013-10-25 09:08:21 +0100147 /* Set the secure world (EL3) re-entry point after BL1 */
148 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
149
150 /*
151 * Arch. management. Perform the necessary steps to flush all
152 * cpu caches.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100153 */
Achin Guptae1aa5162014-06-26 09:58:52 +0100154 psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100155
156 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100157 * Plat. management: Allow the platform to perform the
158 * necessary actions to turn off this cpu e.g. set the
159 * platform defined mailbox with the psci entrypoint,
160 * program the power controller etc.
161 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100162 rc = PSCI_E_SUCCESS;
163
Achin Gupta4f6ad662013-10-25 09:08:21 +0100164 if (psci_plat_pm_ops->affinst_suspend) {
Achin Gupta75f73672013-12-05 16:33:10 +0000165 plat_state = psci_get_phys_state(cpu_node);
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100166 rc = psci_plat_pm_ops->affinst_suspend(read_mpidr_el1(),
Achin Gupta4f6ad662013-10-25 09:08:21 +0100167 psci_entrypoint,
168 ns_entrypoint,
169 cpu_node->level,
170 plat_state);
171 }
172
173 return rc;
174}
175
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100176static int psci_afflvl1_suspend(aff_map_node_t *cluster_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100177 unsigned long ns_entrypoint,
178 unsigned long context_id,
179 unsigned int power_state)
180{
181 int rc = PSCI_E_SUCCESS;
182 unsigned int plat_state;
183 unsigned long psci_entrypoint;
184
185 /* Sanity check the cluster level */
186 assert(cluster_node->level == MPIDR_AFFLVL1);
187
188 /*
189 * Keep the physical state of this cluster handy to decide
190 * what action needs to be taken
191 */
Achin Gupta75f73672013-12-05 16:33:10 +0000192 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100193
194 /*
195 * Arch. management: Flush all levels of caches to PoC if the
Achin Guptaf6b9e992014-07-31 11:19:11 +0100196 * cluster is to be shutdown.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100197 */
Achin Guptaf6b9e992014-07-31 11:19:11 +0100198 psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL1);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100199
200 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000201 * Plat. Management. Allow the platform to do its cluster
Achin Gupta4f6ad662013-10-25 09:08:21 +0100202 * specific bookeeping e.g. turn off interconnect coherency,
203 * program the power controller etc.
204 */
205 if (psci_plat_pm_ops->affinst_suspend) {
206
207 /*
208 * Sending the psci entrypoint is currently redundant
209 * beyond affinity level 0 but one never knows what a
210 * platform might do. Also it allows us to keep the
211 * platform handler prototype the same.
212 */
213 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100214 rc = psci_plat_pm_ops->affinst_suspend(read_mpidr_el1(),
Achin Gupta4f6ad662013-10-25 09:08:21 +0100215 psci_entrypoint,
216 ns_entrypoint,
217 cluster_node->level,
218 plat_state);
219 }
220
221 return rc;
222}
223
224
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100225static int psci_afflvl2_suspend(aff_map_node_t *system_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100226 unsigned long ns_entrypoint,
227 unsigned long context_id,
228 unsigned int power_state)
229{
230 int rc = PSCI_E_SUCCESS;
231 unsigned int plat_state;
232 unsigned long psci_entrypoint;
233
234 /* Cannot go beyond this */
235 assert(system_node->level == MPIDR_AFFLVL2);
236
237 /*
238 * Keep the physical state of the system handy to decide what
239 * action needs to be taken
240 */
Achin Gupta75f73672013-12-05 16:33:10 +0000241 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100242
243 /*
Achin Guptaf6b9e992014-07-31 11:19:11 +0100244 * Arch. management: Flush all levels of caches to PoC if the
245 * system is to be shutdown.
246 */
247 psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL2);
248
249 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000250 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100251 * at this affinity level
252 */
253 if (psci_plat_pm_ops->affinst_suspend) {
254
255 /*
256 * Sending the psci entrypoint is currently redundant
257 * beyond affinity level 0 but one never knows what a
258 * platform might do. Also it allows us to keep the
259 * platform handler prototype the same.
260 */
261 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100262 rc = psci_plat_pm_ops->affinst_suspend(read_mpidr_el1(),
Achin Gupta4f6ad662013-10-25 09:08:21 +0100263 psci_entrypoint,
264 ns_entrypoint,
265 system_node->level,
266 plat_state);
267 }
268
269 return rc;
270}
271
Dan Handleye2712bc2014-04-10 15:37:22 +0100272static const afflvl_suspend_handler_t psci_afflvl_suspend_handlers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100273 psci_afflvl0_suspend,
274 psci_afflvl1_suspend,
275 psci_afflvl2_suspend,
276};
277
278/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000279 * This function takes an array of pointers to affinity instance nodes in the
280 * topology tree and calls the suspend handler for the corresponding affinity
281 * levels
282 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100283static int psci_call_suspend_handlers(mpidr_aff_map_nodes_t mpidr_nodes,
Achin Gupta0959db52013-12-02 17:33:04 +0000284 int start_afflvl,
285 int end_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000286 unsigned long entrypoint,
287 unsigned long context_id,
288 unsigned int power_state)
289{
290 int rc = PSCI_E_INVALID_PARAMS, level;
Dan Handleye2712bc2014-04-10 15:37:22 +0100291 aff_map_node_t *node;
Achin Gupta0959db52013-12-02 17:33:04 +0000292
293 for (level = start_afflvl; level <= end_afflvl; level++) {
294 node = mpidr_nodes[level];
295 if (node == NULL)
296 continue;
297
298 /*
299 * TODO: In case of an error should there be a way
300 * of restoring what we might have torn down at
301 * lower affinity levels.
302 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100303 rc = psci_afflvl_suspend_handlers[level](node,
Achin Gupta0959db52013-12-02 17:33:04 +0000304 entrypoint,
305 context_id,
306 power_state);
307 if (rc != PSCI_E_SUCCESS)
308 break;
309 }
310
311 return rc;
312}
313
314/*******************************************************************************
315 * Top level handler which is called when a cpu wants to suspend its execution.
316 * It is assumed that along with turning the cpu off, higher affinity levels
317 * until the target affinity level will be turned off as well. It traverses
318 * through all the affinity levels performing generic, architectural, platform
319 * setup and state management e.g. for a cluster that's to be suspended, it will
320 * call the platform specific code which will disable coherency at the
321 * interconnect level if the cpu is the last in the cluster. For a cpu it could
322 * mean programming the power controller etc.
323 *
324 * The state of all the relevant affinity levels is changed prior to calling the
325 * affinity level specific handlers as their actions would depend upon the state
326 * the affinity level is about to enter.
327 *
328 * The affinity level specific handlers are called in ascending order i.e. from
329 * the lowest to the highest affinity level implemented by the platform because
330 * to turn off affinity level X it is neccesary to turn off affinity level X - 1
331 * first.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100332 ******************************************************************************/
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100333int psci_afflvl_suspend(unsigned long entrypoint,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100334 unsigned long context_id,
335 unsigned int power_state,
Achin Gupta0959db52013-12-02 17:33:04 +0000336 int start_afflvl,
337 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100338{
Achin Gupta0959db52013-12-02 17:33:04 +0000339 int rc = PSCI_E_SUCCESS;
Dan Handleye2712bc2014-04-10 15:37:22 +0100340 mpidr_aff_map_nodes_t mpidr_nodes;
Achin Guptaf6b9e992014-07-31 11:19:11 +0100341 unsigned int max_phys_off_afflvl;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100342
Achin Gupta4f6ad662013-10-25 09:08:21 +0100343 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000344 * Collect the pointers to the nodes in the topology tree for
345 * each affinity instance in the mpidr. If this function does
346 * not return successfully then either the mpidr or the affinity
347 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100348 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100349 rc = psci_get_aff_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Achin Gupta0959db52013-12-02 17:33:04 +0000350 start_afflvl,
351 end_afflvl,
352 mpidr_nodes);
353 if (rc != PSCI_E_SUCCESS)
354 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100355
356 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000357 * This function acquires the lock corresponding to each affinity
358 * level so that by the time all locks are taken, the system topology
359 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100360 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100361 psci_acquire_afflvl_locks(start_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000362 end_afflvl,
363 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100364
Achin Guptacab78e42014-07-28 00:09:01 +0100365 /*
366 * This function updates the state of each affinity instance
367 * corresponding to the mpidr in the range of affinity levels
368 * specified.
369 */
370 psci_do_afflvl_state_mgmt(start_afflvl,
371 end_afflvl,
372 mpidr_nodes,
373 PSCI_STATE_SUSPEND);
Achin Guptaf6b9e992014-07-31 11:19:11 +0100374
375 max_phys_off_afflvl = psci_find_max_phys_off_afflvl(start_afflvl,
376 end_afflvl,
377 mpidr_nodes);
378 assert(max_phys_off_afflvl != PSCI_INVALID_DATA);
379
380 /* Stash the highest affinity level that will be turned off */
381 psci_set_max_phys_off_afflvl(max_phys_off_afflvl);
382
Achin Gupta0959db52013-12-02 17:33:04 +0000383 /* Perform generic, architecture and platform specific handling */
384 rc = psci_call_suspend_handlers(mpidr_nodes,
385 start_afflvl,
386 end_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000387 entrypoint,
388 context_id,
389 power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100390
391 /*
Achin Guptaf6b9e992014-07-31 11:19:11 +0100392 * Invalidate the entry for the highest affinity level stashed earlier.
393 * This ensures that any reads of this variable outside the power
394 * up/down sequences return PSCI_INVALID_DATA.
395 */
396 psci_set_max_phys_off_afflvl(PSCI_INVALID_DATA);
397
398 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000399 * Release the locks corresponding to each affinity level in the
400 * reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100401 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100402 psci_release_afflvl_locks(start_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000403 end_afflvl,
404 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100405
Achin Gupta4f6ad662013-10-25 09:08:21 +0100406 return rc;
407}
408
409/*******************************************************************************
410 * The following functions finish an earlier affinity suspend request. They
411 * are called by the common finisher routine in psci_common.c.
412 ******************************************************************************/
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100413static unsigned int psci_afflvl0_suspend_finish(aff_map_node_t *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100414{
Andrew Thoelke4e126072014-06-04 21:10:52 +0100415 unsigned int plat_state, state, rc;
Achin Gupta607084e2014-02-09 18:24:19 +0000416 int32_t suspend_level;
Soby Mathew2ed46e92014-07-04 16:02:26 +0100417 uint64_t counter_freq;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100418
419 assert(cpu_node->level == MPIDR_AFFLVL0);
420
Achin Gupta0959db52013-12-02 17:33:04 +0000421 /* Ensure we have been woken up from a suspended state */
Achin Gupta75f73672013-12-05 16:33:10 +0000422 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000423 assert(state == PSCI_STATE_SUSPEND);
424
Achin Gupta4f6ad662013-10-25 09:08:21 +0100425 /*
426 * Plat. management: Perform the platform specific actions
427 * before we change the state of the cpu e.g. enabling the
428 * gic or zeroing the mailbox register. If anything goes
429 * wrong then assert as there is no way to recover from this
430 * situation.
431 */
432 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000433
434 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000435 plat_state = get_phys_state(state);
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100436 rc = psci_plat_pm_ops->affinst_suspend_finish(read_mpidr_el1(),
Achin Gupta4f6ad662013-10-25 09:08:21 +0100437 cpu_node->level,
438 plat_state);
439 assert(rc == PSCI_E_SUCCESS);
440 }
441
442 /* Get the index for restoring the re-entry information */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100443 /*
Achin Guptae1aa5162014-06-26 09:58:52 +0100444 * Arch. management: Enable the data cache, manage stack memory and
445 * restore the stashed EL3 architectural context from the 'cpu_context'
446 * structure for this cpu.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100447 */
Achin Guptae1aa5162014-06-26 09:58:52 +0100448 psci_do_pwrup_cache_maintenance();
Soby Mathew2ed46e92014-07-04 16:02:26 +0100449
450 /* Re-init the cntfrq_el0 register */
451 counter_freq = plat_get_syscnt_freq();
452 write_cntfrq_el0(counter_freq);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100453
454 /*
Achin Gupta607084e2014-02-09 18:24:19 +0000455 * Call the cpu suspend finish handler registered by the Secure Payload
456 * Dispatcher to let it do any bookeeping. If the handler encounters an
457 * error, it's expected to assert within
458 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000459 if (psci_spd_pm && psci_spd_pm->svc_suspend) {
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100460 suspend_level = psci_get_suspend_afflvl();
Vikram Kanigirif100f412014-04-01 19:26:26 +0100461 assert (suspend_level != PSCI_INVALID_DATA);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000462 psci_spd_pm->svc_suspend_finish(suspend_level);
Achin Gupta607084e2014-02-09 18:24:19 +0000463 }
464
Vikram Kanigirif100f412014-04-01 19:26:26 +0100465 /* Invalidate the suspend context for the node */
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100466 psci_set_suspend_power_state(PSCI_INVALID_DATA);
Vikram Kanigirif100f412014-04-01 19:26:26 +0100467
Achin Gupta607084e2014-02-09 18:24:19 +0000468 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100469 * Generic management: Now we just need to retrieve the
470 * information that we had stashed away during the suspend
Achin Gupta3140a9e2013-12-02 16:23:12 +0000471 * call to set this cpu on its way.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100472 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100473 cm_prepare_el3_exit(NON_SECURE);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100474
475 /* Clean caches before re-entering normal world */
476 dcsw_op_louis(DCCSW);
477
Andrew Thoelke4e126072014-06-04 21:10:52 +0100478 rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100479 return rc;
480}
481
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100482static unsigned int psci_afflvl1_suspend_finish(aff_map_node_t *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100483{
Achin Gupta0959db52013-12-02 17:33:04 +0000484 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100485
486 assert(cluster_node->level == MPIDR_AFFLVL1);
487
488 /*
489 * Plat. management: Perform the platform specific actions
490 * as per the old state of the cluster e.g. enabling
491 * coherency at the interconnect depends upon the state with
492 * which this cluster was powered up. If anything goes wrong
493 * then assert as there is no way to recover from this
494 * situation.
495 */
496 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000497
498 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000499 plat_state = psci_get_phys_state(cluster_node);
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100500 rc = psci_plat_pm_ops->affinst_suspend_finish(read_mpidr_el1(),
Achin Gupta4f6ad662013-10-25 09:08:21 +0100501 cluster_node->level,
502 plat_state);
503 assert(rc == PSCI_E_SUCCESS);
504 }
505
506 return rc;
507}
508
509
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100510static unsigned int psci_afflvl2_suspend_finish(aff_map_node_t *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100511{
Achin Gupta0959db52013-12-02 17:33:04 +0000512 unsigned int plat_state, rc = PSCI_E_SUCCESS;;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100513
514 /* Cannot go beyond this affinity level */
515 assert(system_node->level == MPIDR_AFFLVL2);
516
517 /*
518 * Currently, there are no architectural actions to perform
519 * at the system level.
520 */
521
522 /*
523 * Plat. management: Perform the platform specific actions
524 * as per the old state of the cluster e.g. enabling
525 * coherency at the interconnect depends upon the state with
526 * which this cluster was powered up. If anything goes wrong
527 * then assert as there is no way to recover from this
528 * situation.
529 */
530 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000531
532 /* Get the physical state of the system */
Achin Gupta75f73672013-12-05 16:33:10 +0000533 plat_state = psci_get_phys_state(system_node);
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100534 rc = psci_plat_pm_ops->affinst_suspend_finish(read_mpidr_el1(),
Achin Gupta4f6ad662013-10-25 09:08:21 +0100535 system_node->level,
536 plat_state);
537 assert(rc == PSCI_E_SUCCESS);
538 }
539
Achin Gupta4f6ad662013-10-25 09:08:21 +0100540 return rc;
541}
542
Dan Handleye2712bc2014-04-10 15:37:22 +0100543const afflvl_power_on_finisher_t psci_afflvl_suspend_finishers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100544 psci_afflvl0_suspend_finish,
545 psci_afflvl1_suspend_finish,
546 psci_afflvl2_suspend_finish,
547};