blob: 4988e677af28c5072229f9db685a9bde34ad0d07 [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
Soby Mathewffb4ab12014-09-26 15:08:52 +010043typedef int (*afflvl_suspend_handler_t)(aff_map_node_t *node,
Soby Mathewffb4ab12014-09-26 15:08:52 +010044 unsigned int power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +010045
46/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010047 * This function saves the power state parameter passed in the current PSCI
48 * cpu_suspend call in the per-cpu data array.
Achin Guptaa45e3972013-12-05 15:10:48 +000049 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010050void psci_set_suspend_power_state(unsigned int power_state)
Achin Guptaa45e3972013-12-05 15:10:48 +000051{
Achin Guptaf3ccbab2014-07-25 14:52:47 +010052 set_cpu_data(psci_svc_cpu_data.power_state, power_state);
53 flush_cpu_data(psci_svc_cpu_data.power_state);
Achin Guptaa45e3972013-12-05 15:10:48 +000054}
55
56/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010057 * This function gets the affinity level till which the current cpu could be
58 * powered down during a cpu_suspend call. Returns PSCI_INVALID_DATA if the
59 * power state is invalid.
Vikram Kanigirif100f412014-04-01 19:26:26 +010060 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010061int psci_get_suspend_afflvl()
Vikram Kanigirif100f412014-04-01 19:26:26 +010062{
Achin Guptaf3ccbab2014-07-25 14:52:47 +010063 unsigned int power_state;
Vikram Kanigirif100f412014-04-01 19:26:26 +010064
Achin Guptaf3ccbab2014-07-25 14:52:47 +010065 power_state = get_cpu_data(psci_svc_cpu_data.power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +010066
Achin Guptaf3ccbab2014-07-25 14:52:47 +010067 return ((power_state == PSCI_INVALID_DATA) ?
68 power_state : psci_get_pstate_afflvl(power_state));
Vikram Kanigirif100f412014-04-01 19:26:26 +010069}
70
Vikram Kanigirif100f412014-04-01 19:26:26 +010071/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010072 * This function gets the state id of the current cpu from the power state
73 * parameter saved in the per-cpu data array. Returns PSCI_INVALID_DATA if the
74 * power state saved is invalid.
Vikram Kanigirif100f412014-04-01 19:26:26 +010075 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010076int psci_get_suspend_stateid()
Vikram Kanigirif100f412014-04-01 19:26:26 +010077{
78 unsigned int power_state;
79
Achin Guptaf3ccbab2014-07-25 14:52:47 +010080 power_state = get_cpu_data(psci_svc_cpu_data.power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +010081
Vikram Kanigirif100f412014-04-01 19:26:26 +010082 return ((power_state == PSCI_INVALID_DATA) ?
Achin Guptaf3ccbab2014-07-25 14:52:47 +010083 power_state : psci_get_pstate_id(power_state));
Vikram Kanigirif100f412014-04-01 19:26:26 +010084}
85
86/*******************************************************************************
Achin Guptaf3ccbab2014-07-25 14:52:47 +010087 * This function gets the state id of the cpu specified by the 'mpidr' parameter
88 * from the power state parameter saved in the per-cpu data array. Returns
89 * PSCI_INVALID_DATA if the power state saved is invalid.
Achin Guptaa45e3972013-12-05 15:10:48 +000090 ******************************************************************************/
Achin Guptaf3ccbab2014-07-25 14:52:47 +010091int psci_get_suspend_stateid_by_mpidr(unsigned long mpidr)
Achin Guptaa45e3972013-12-05 15:10:48 +000092{
Vikram Kanigirif100f412014-04-01 19:26:26 +010093 unsigned int power_state;
94
Achin Guptaf3ccbab2014-07-25 14:52:47 +010095 power_state = get_cpu_data_by_mpidr(mpidr,
96 psci_svc_cpu_data.power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +010097
Vikram Kanigirif100f412014-04-01 19:26:26 +010098 return ((power_state == PSCI_INVALID_DATA) ?
Achin Guptaf3ccbab2014-07-25 14:52:47 +010099 power_state : psci_get_pstate_id(power_state));
Achin Guptaa45e3972013-12-05 15:10:48 +0000100}
101
102/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103 * The next three functions implement a handler for each supported affinity
104 * level which is called when that affinity level is about to be suspended.
105 ******************************************************************************/
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100106static int psci_afflvl0_suspend(aff_map_node_t *cpu_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100107 unsigned int power_state)
108{
Achin Guptae1aa5162014-06-26 09:58:52 +0100109 unsigned long psci_entrypoint;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100110
111 /* Sanity check to safeguard against data corruption */
112 assert(cpu_node->level == MPIDR_AFFLVL0);
113
Vikram Kanigirif100f412014-04-01 19:26:26 +0100114 /* Save PSCI power state parameter for the core in suspend context */
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100115 psci_set_suspend_power_state(power_state);
Vikram Kanigirif100f412014-04-01 19:26:26 +0100116
Achin Gupta607084e2014-02-09 18:24:19 +0000117 /*
Soby Mathew8595b872015-01-06 15:36:38 +0000118 * Generic management: Allow the Secure world to suspend itself
Achin Gupta607084e2014-02-09 18:24:19 +0000119 */
120
121 /*
122 * Call the cpu suspend handler registered by the Secure Payload
123 * Dispatcher to let it do any bookeeping. If the handler encounters an
124 * error, it's expected to assert within
125 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000126 if (psci_spd_pm && psci_spd_pm->svc_suspend)
127 psci_spd_pm->svc_suspend(power_state);
Achin Gupta607084e2014-02-09 18:24:19 +0000128
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129
Achin Gupta4f6ad662013-10-25 09:08:21 +0100130 /* Set the secure world (EL3) re-entry point after BL1 */
131 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
132
133 /*
134 * Arch. management. Perform the necessary steps to flush all
135 * cpu caches.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100136 */
Achin Guptae1aa5162014-06-26 09:58:52 +0100137 psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100138
Achin Gupta56bcdc22014-07-28 00:15:23 +0100139 if (!psci_plat_pm_ops->affinst_suspend)
140 return PSCI_E_SUCCESS;
141
Achin Gupta4f6ad662013-10-25 09:08:21 +0100142 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100143 * Plat. management: Allow the platform to perform the
144 * necessary actions to turn off this cpu e.g. set the
145 * platform defined mailbox with the psci entrypoint,
146 * program the power controller etc.
147 */
Soby Mathewffb4ab12014-09-26 15:08:52 +0100148 return psci_plat_pm_ops->affinst_suspend(psci_entrypoint,
Achin Gupta56bcdc22014-07-28 00:15:23 +0100149 cpu_node->level,
150 psci_get_phys_state(cpu_node));
Achin Gupta4f6ad662013-10-25 09:08:21 +0100151}
152
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100153static int psci_afflvl1_suspend(aff_map_node_t *cluster_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100154 unsigned int power_state)
155{
Achin Gupta4f6ad662013-10-25 09:08:21 +0100156 unsigned int plat_state;
157 unsigned long psci_entrypoint;
158
159 /* Sanity check the cluster level */
160 assert(cluster_node->level == MPIDR_AFFLVL1);
161
162 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100163 * Arch. management: Flush all levels of caches to PoC if the
Achin Guptaf6b9e992014-07-31 11:19:11 +0100164 * cluster is to be shutdown.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100165 */
Achin Guptaf6b9e992014-07-31 11:19:11 +0100166 psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL1);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100167
Achin Gupta56bcdc22014-07-28 00:15:23 +0100168 if (!psci_plat_pm_ops->affinst_suspend)
169 return PSCI_E_SUCCESS;
170
Achin Gupta4f6ad662013-10-25 09:08:21 +0100171 /*
Achin Gupta56bcdc22014-07-28 00:15:23 +0100172 * Plat. Management. Allow the platform to do its cluster specific
173 * bookeeping e.g. turn off interconnect coherency, program the power
174 * controller etc. Sending the psci entrypoint is currently redundant
175 * beyond affinity level 0 but one never knows what a platform might
176 * do. Also it allows us to keep the platform handler prototype the
177 * same.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100178 */
Achin Gupta56bcdc22014-07-28 00:15:23 +0100179 plat_state = psci_get_phys_state(cluster_node);
180 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Soby Mathewffb4ab12014-09-26 15:08:52 +0100181 return psci_plat_pm_ops->affinst_suspend(psci_entrypoint,
Achin Gupta56bcdc22014-07-28 00:15:23 +0100182 cluster_node->level,
183 plat_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100184}
185
186
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100187static int psci_afflvl2_suspend(aff_map_node_t *system_node,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100188 unsigned int power_state)
189{
Achin Gupta4f6ad662013-10-25 09:08:21 +0100190 unsigned int plat_state;
191 unsigned long psci_entrypoint;
192
193 /* Cannot go beyond this */
194 assert(system_node->level == MPIDR_AFFLVL2);
195
196 /*
197 * Keep the physical state of the system handy to decide what
198 * action needs to be taken
199 */
Achin Gupta75f73672013-12-05 16:33:10 +0000200 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100201
202 /*
Achin Guptaf6b9e992014-07-31 11:19:11 +0100203 * Arch. management: Flush all levels of caches to PoC if the
204 * system is to be shutdown.
205 */
206 psci_do_pwrdown_cache_maintenance(MPIDR_AFFLVL2);
207
208 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000209 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100210 * at this affinity level
211 */
Achin Gupta56bcdc22014-07-28 00:15:23 +0100212 if (!psci_plat_pm_ops->affinst_suspend)
213 return PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100214
Achin Gupta56bcdc22014-07-28 00:15:23 +0100215 /*
216 * Sending the psci entrypoint is currently redundant
217 * beyond affinity level 0 but one never knows what a
218 * platform might do. Also it allows us to keep the
219 * platform handler prototype the same.
220 */
221 plat_state = psci_get_phys_state(system_node);
222 psci_entrypoint = (unsigned long) psci_aff_suspend_finish_entry;
Soby Mathewffb4ab12014-09-26 15:08:52 +0100223 return psci_plat_pm_ops->affinst_suspend(psci_entrypoint,
Achin Gupta56bcdc22014-07-28 00:15:23 +0100224 system_node->level,
225 plat_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100226}
227
Dan Handleye2712bc2014-04-10 15:37:22 +0100228static const afflvl_suspend_handler_t psci_afflvl_suspend_handlers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100229 psci_afflvl0_suspend,
230 psci_afflvl1_suspend,
231 psci_afflvl2_suspend,
232};
233
234/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000235 * This function takes an array of pointers to affinity instance nodes in the
236 * topology tree and calls the suspend handler for the corresponding affinity
237 * levels
238 ******************************************************************************/
Achin Gupta56bcdc22014-07-28 00:15:23 +0100239static int psci_call_suspend_handlers(aff_map_node_t *mpidr_nodes[],
Achin Gupta0959db52013-12-02 17:33:04 +0000240 int start_afflvl,
241 int end_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000242 unsigned int power_state)
243{
244 int rc = PSCI_E_INVALID_PARAMS, level;
Dan Handleye2712bc2014-04-10 15:37:22 +0100245 aff_map_node_t *node;
Achin Gupta0959db52013-12-02 17:33:04 +0000246
247 for (level = start_afflvl; level <= end_afflvl; level++) {
248 node = mpidr_nodes[level];
249 if (node == NULL)
250 continue;
251
252 /*
253 * TODO: In case of an error should there be a way
254 * of restoring what we might have torn down at
255 * lower affinity levels.
256 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100257 rc = psci_afflvl_suspend_handlers[level](node,
Achin Gupta0959db52013-12-02 17:33:04 +0000258 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.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100284 ******************************************************************************/
Soby Mathew8595b872015-01-06 15:36:38 +0000285int psci_afflvl_suspend(entry_point_info_t *ep,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100286 unsigned int power_state,
Achin Gupta0959db52013-12-02 17:33:04 +0000287 int start_afflvl,
288 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100289{
Achin Gupta0959db52013-12-02 17:33:04 +0000290 int rc = PSCI_E_SUCCESS;
Dan Handleye2712bc2014-04-10 15:37:22 +0100291 mpidr_aff_map_nodes_t mpidr_nodes;
Achin Guptaf6b9e992014-07-31 11:19:11 +0100292 unsigned int max_phys_off_afflvl;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100293
Achin Gupta4f6ad662013-10-25 09:08:21 +0100294 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000295 * Collect the pointers to the nodes in the topology tree for
296 * each affinity instance in the mpidr. If this function does
297 * not return successfully then either the mpidr or the affinity
298 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100299 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100300 rc = psci_get_aff_map_nodes(read_mpidr_el1() & MPIDR_AFFINITY_MASK,
Achin Gupta0959db52013-12-02 17:33:04 +0000301 start_afflvl,
302 end_afflvl,
303 mpidr_nodes);
304 if (rc != PSCI_E_SUCCESS)
305 return rc;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100306
307 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000308 * This function acquires the lock corresponding to each affinity
309 * level so that by the time all locks are taken, the system topology
310 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100311 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100312 psci_acquire_afflvl_locks(start_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000313 end_afflvl,
314 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100315
Achin Guptacab78e42014-07-28 00:09:01 +0100316 /*
317 * This function updates the state of each affinity instance
318 * corresponding to the mpidr in the range of affinity levels
319 * specified.
320 */
321 psci_do_afflvl_state_mgmt(start_afflvl,
322 end_afflvl,
323 mpidr_nodes,
324 PSCI_STATE_SUSPEND);
Achin Guptaf6b9e992014-07-31 11:19:11 +0100325
326 max_phys_off_afflvl = psci_find_max_phys_off_afflvl(start_afflvl,
327 end_afflvl,
328 mpidr_nodes);
329 assert(max_phys_off_afflvl != PSCI_INVALID_DATA);
330
331 /* Stash the highest affinity level that will be turned off */
332 psci_set_max_phys_off_afflvl(max_phys_off_afflvl);
333
Soby Mathew8595b872015-01-06 15:36:38 +0000334 /*
335 * Store the re-entry information for the non-secure world.
336 */
337 cm_init_context(read_mpidr_el1(), ep);
338
Achin Gupta0959db52013-12-02 17:33:04 +0000339 /* Perform generic, architecture and platform specific handling */
340 rc = psci_call_suspend_handlers(mpidr_nodes,
341 start_afflvl,
342 end_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000343 power_state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100344
345 /*
Achin Guptaf6b9e992014-07-31 11:19:11 +0100346 * Invalidate the entry for the highest affinity level stashed earlier.
347 * This ensures that any reads of this variable outside the power
348 * up/down sequences return PSCI_INVALID_DATA.
349 */
350 psci_set_max_phys_off_afflvl(PSCI_INVALID_DATA);
351
352 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000353 * Release the locks corresponding to each affinity level in the
354 * reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100355 */
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100356 psci_release_afflvl_locks(start_afflvl,
Achin Gupta0959db52013-12-02 17:33:04 +0000357 end_afflvl,
358 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100359
Achin Gupta4f6ad662013-10-25 09:08:21 +0100360 return rc;
361}
362
363/*******************************************************************************
364 * The following functions finish an earlier affinity suspend request. They
365 * are called by the common finisher routine in psci_common.c.
366 ******************************************************************************/
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100367static unsigned int psci_afflvl0_suspend_finish(aff_map_node_t *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100368{
Andrew Thoelke4e126072014-06-04 21:10:52 +0100369 unsigned int plat_state, state, rc;
Achin Gupta607084e2014-02-09 18:24:19 +0000370 int32_t suspend_level;
Soby Mathew2ed46e92014-07-04 16:02:26 +0100371 uint64_t counter_freq;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100372
373 assert(cpu_node->level == MPIDR_AFFLVL0);
374
Achin Gupta0959db52013-12-02 17:33:04 +0000375 /* Ensure we have been woken up from a suspended state */
Achin Gupta75f73672013-12-05 16:33:10 +0000376 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000377 assert(state == PSCI_STATE_SUSPEND);
378
Achin Gupta4f6ad662013-10-25 09:08:21 +0100379 /*
380 * Plat. management: Perform the platform specific actions
381 * before we change the state of the cpu e.g. enabling the
382 * gic or zeroing the mailbox register. If anything goes
383 * wrong then assert as there is no way to recover from this
384 * situation.
385 */
386 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000387
388 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000389 plat_state = get_phys_state(state);
Soby Mathewffb4ab12014-09-26 15:08:52 +0100390 rc = psci_plat_pm_ops->affinst_suspend_finish(cpu_node->level,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100391 plat_state);
392 assert(rc == PSCI_E_SUCCESS);
393 }
394
395 /* Get the index for restoring the re-entry information */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100396 /*
Achin Guptae1aa5162014-06-26 09:58:52 +0100397 * Arch. management: Enable the data cache, manage stack memory and
398 * restore the stashed EL3 architectural context from the 'cpu_context'
399 * structure for this cpu.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100400 */
Achin Guptae1aa5162014-06-26 09:58:52 +0100401 psci_do_pwrup_cache_maintenance();
Soby Mathew2ed46e92014-07-04 16:02:26 +0100402
403 /* Re-init the cntfrq_el0 register */
404 counter_freq = plat_get_syscnt_freq();
405 write_cntfrq_el0(counter_freq);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100406
407 /*
Achin Gupta607084e2014-02-09 18:24:19 +0000408 * Call the cpu suspend finish handler registered by the Secure Payload
409 * Dispatcher to let it do any bookeeping. If the handler encounters an
410 * error, it's expected to assert within
411 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000412 if (psci_spd_pm && psci_spd_pm->svc_suspend) {
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100413 suspend_level = psci_get_suspend_afflvl();
Vikram Kanigirif100f412014-04-01 19:26:26 +0100414 assert (suspend_level != PSCI_INVALID_DATA);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000415 psci_spd_pm->svc_suspend_finish(suspend_level);
Achin Gupta607084e2014-02-09 18:24:19 +0000416 }
417
Vikram Kanigirif100f412014-04-01 19:26:26 +0100418 /* Invalidate the suspend context for the node */
Achin Guptaf3ccbab2014-07-25 14:52:47 +0100419 psci_set_suspend_power_state(PSCI_INVALID_DATA);
Vikram Kanigirif100f412014-04-01 19:26:26 +0100420
Achin Gupta607084e2014-02-09 18:24:19 +0000421 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100422 * Generic management: Now we just need to retrieve the
423 * information that we had stashed away during the suspend
Achin Gupta3140a9e2013-12-02 16:23:12 +0000424 * call to set this cpu on its way.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100425 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100426 cm_prepare_el3_exit(NON_SECURE);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100427
428 /* Clean caches before re-entering normal world */
429 dcsw_op_louis(DCCSW);
430
Andrew Thoelke4e126072014-06-04 21:10:52 +0100431 rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100432 return rc;
433}
434
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100435static unsigned int psci_afflvl1_suspend_finish(aff_map_node_t *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100436{
Achin Gupta0959db52013-12-02 17:33:04 +0000437 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100438
439 assert(cluster_node->level == MPIDR_AFFLVL1);
440
441 /*
442 * Plat. management: Perform the platform specific actions
443 * as per the old state of the cluster e.g. enabling
444 * coherency at the interconnect depends upon the state with
445 * which this cluster was powered up. If anything goes wrong
446 * then assert as there is no way to recover from this
447 * situation.
448 */
449 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000450
451 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000452 plat_state = psci_get_phys_state(cluster_node);
Soby Mathewffb4ab12014-09-26 15:08:52 +0100453 rc = psci_plat_pm_ops->affinst_suspend_finish(cluster_node->level,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100454 plat_state);
455 assert(rc == PSCI_E_SUCCESS);
456 }
457
458 return rc;
459}
460
461
Andrew Thoelke2bc07852014-06-09 12:44:21 +0100462static unsigned int psci_afflvl2_suspend_finish(aff_map_node_t *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100463{
Achin Gupta0959db52013-12-02 17:33:04 +0000464 unsigned int plat_state, rc = PSCI_E_SUCCESS;;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100465
466 /* Cannot go beyond this affinity level */
467 assert(system_node->level == MPIDR_AFFLVL2);
468
469 /*
470 * Currently, there are no architectural actions to perform
471 * at the system level.
472 */
473
474 /*
475 * Plat. management: Perform the platform specific actions
476 * as per the old state of the cluster e.g. enabling
477 * coherency at the interconnect depends upon the state with
478 * which this cluster was powered up. If anything goes wrong
479 * then assert as there is no way to recover from this
480 * situation.
481 */
482 if (psci_plat_pm_ops->affinst_suspend_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000483
484 /* Get the physical state of the system */
Achin Gupta75f73672013-12-05 16:33:10 +0000485 plat_state = psci_get_phys_state(system_node);
Soby Mathewffb4ab12014-09-26 15:08:52 +0100486 rc = psci_plat_pm_ops->affinst_suspend_finish(system_node->level,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100487 plat_state);
488 assert(rc == PSCI_E_SUCCESS);
489 }
490
Achin Gupta4f6ad662013-10-25 09:08:21 +0100491 return rc;
492}
493
Dan Handleye2712bc2014-04-10 15:37:22 +0100494const afflvl_power_on_finisher_t psci_afflvl_suspend_finishers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100495 psci_afflvl0_suspend_finish,
496 psci_afflvl1_suspend_finish,
497 psci_afflvl2_suspend_finish,
498};