blob: 21a4d1a6dd995fb416e7955eeced5442fbf207b0 [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
Dan Handley2bd4ef22014-04-09 13:14:54 +010031#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010032#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010033#include <assert.h>
34#include <string.h>
Dan Handley714a0d22014-04-09 13:13:04 +010035#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010036
Dan Handleye2712bc2014-04-10 15:37:22 +010037typedef int (*afflvl_off_handler_t)(unsigned long, aff_map_node_t *);
Achin Gupta4f6ad662013-10-25 09:08:21 +010038
39/*******************************************************************************
40 * The next three functions implement a handler for each supported affinity
41 * level which is called when that affinity level is turned off.
42 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010043static int psci_afflvl0_off(unsigned long mpidr, aff_map_node_t *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +010044{
45 unsigned int index, plat_state;
46 int rc = PSCI_E_SUCCESS;
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +000047 unsigned long sctlr;
Achin Gupta4f6ad662013-10-25 09:08:21 +010048
49 assert(cpu_node->level == MPIDR_AFFLVL0);
50
Achin Gupta75f73672013-12-05 16:33:10 +000051 /* State management: mark this cpu as turned off */
52 psci_set_state(cpu_node, PSCI_STATE_OFF);
53
Achin Gupta4f6ad662013-10-25 09:08:21 +010054 /*
Achin Gupta607084e2014-02-09 18:24:19 +000055 * Generic management: Get the index for clearing any lingering re-entry
56 * information and allow the secure world to switch itself off
57 */
58
59 /*
60 * Call the cpu off handler registered by the Secure Payload Dispatcher
61 * to let it do any bookeeping. Assume that the SPD always reports an
62 * E_DENIED error if SP refuse to power down
Achin Gupta4f6ad662013-10-25 09:08:21 +010063 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000064 if (psci_spd_pm && psci_spd_pm->svc_off) {
65 rc = psci_spd_pm->svc_off(0);
Achin Gupta607084e2014-02-09 18:24:19 +000066 if (rc)
67 return rc;
68 }
69
Achin Gupta4f6ad662013-10-25 09:08:21 +010070 index = cpu_node->data;
71 memset(&psci_ns_entry_info[index], 0, sizeof(psci_ns_entry_info[index]));
72
73 /*
74 * Arch. management. Perform the necessary steps to flush all
75 * cpu caches.
76 *
77 * TODO: This power down sequence varies across cpus so it needs to be
78 * abstracted out on the basis of the MIDR like in cpu_reset_handler().
79 * Do the bare minimal for the time being. Fix this before porting to
80 * Cortex models.
81 */
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +000082 sctlr = read_sctlr_el3();
Achin Gupta4f6ad662013-10-25 09:08:21 +010083 sctlr &= ~SCTLR_C_BIT;
Vikram Kanigiri78a6e0c2014-03-11 17:41:00 +000084 write_sctlr_el3(sctlr);
Andrew Thoelke42e75a72014-04-28 12:28:39 +010085 isb(); /* ensure MMU disable takes immediate effect */
Achin Gupta4f6ad662013-10-25 09:08:21 +010086
87 /*
88 * CAUTION: This flush to the level of unification makes an assumption
89 * about the cache hierarchy at affinity level 0 (cpu) in the platform.
90 * Ideally the platform should tell psci which levels to flush to exit
91 * coherency.
92 */
93 dcsw_op_louis(DCCISW);
94
95 /*
96 * Plat. management: Perform platform specific actions to turn this
97 * cpu off e.g. exit cpu coherency, program the power controller etc.
98 */
99 if (psci_plat_pm_ops->affinst_off) {
100
101 /* Get the current physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000102 plat_state = psci_get_phys_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103 rc = psci_plat_pm_ops->affinst_off(mpidr,
104 cpu_node->level,
105 plat_state);
106 }
107
Achin Gupta4f6ad662013-10-25 09:08:21 +0100108 return rc;
109}
110
Dan Handleye2712bc2014-04-10 15:37:22 +0100111static int psci_afflvl1_off(unsigned long mpidr, aff_map_node_t *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100112{
113 int rc = PSCI_E_SUCCESS;
114 unsigned int plat_state;
115
116 /* Sanity check the cluster level */
117 assert(cluster_node->level == MPIDR_AFFLVL1);
118
Achin Gupta75f73672013-12-05 16:33:10 +0000119 /* State management: Decrement the cluster reference count */
120 psci_set_state(cluster_node, PSCI_STATE_OFF);
121
Achin Gupta4f6ad662013-10-25 09:08:21 +0100122 /*
123 * Keep the physical state of this cluster handy to decide
124 * what action needs to be taken
125 */
Achin Gupta75f73672013-12-05 16:33:10 +0000126 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100127
128 /*
129 * Arch. Management. Flush all levels of caches to PoC if
130 * the cluster is to be shutdown
131 */
132 if (plat_state == PSCI_STATE_OFF)
133 dcsw_op_all(DCCISW);
134
135 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000136 * Plat. Management. Allow the platform to do its cluster
Achin Gupta4f6ad662013-10-25 09:08:21 +0100137 * specific bookeeping e.g. turn off interconnect coherency,
138 * program the power controller etc.
139 */
140 if (psci_plat_pm_ops->affinst_off)
141 rc = psci_plat_pm_ops->affinst_off(mpidr,
142 cluster_node->level,
143 plat_state);
144
145 return rc;
146}
147
Dan Handleye2712bc2014-04-10 15:37:22 +0100148static int psci_afflvl2_off(unsigned long mpidr, aff_map_node_t *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149{
150 int rc = PSCI_E_SUCCESS;
151 unsigned int plat_state;
152
153 /* Cannot go beyond this level */
154 assert(system_node->level == MPIDR_AFFLVL2);
155
Achin Gupta75f73672013-12-05 16:33:10 +0000156 /* State management: Decrement the system reference count */
157 psci_set_state(system_node, PSCI_STATE_OFF);
158
Achin Gupta4f6ad662013-10-25 09:08:21 +0100159 /*
160 * Keep the physical state of the system handy to decide what
161 * action needs to be taken
162 */
Achin Gupta75f73672013-12-05 16:33:10 +0000163 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100164
165 /* No arch. and generic bookeeping to do here currently */
166
167 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000168 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100169 * at this affinity level
170 */
171 if (psci_plat_pm_ops->affinst_off)
172 rc = psci_plat_pm_ops->affinst_off(mpidr,
173 system_node->level,
174 plat_state);
175 return rc;
176}
177
Dan Handleye2712bc2014-04-10 15:37:22 +0100178static const afflvl_off_handler_t psci_afflvl_off_handlers[] = {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100179 psci_afflvl0_off,
180 psci_afflvl1_off,
181 psci_afflvl2_off,
182};
183
184/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000185 * This function takes an array of pointers to affinity instance nodes in the
186 * topology tree and calls the off handler for the corresponding affinity
187 * levels
188 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100189static int psci_call_off_handlers(mpidr_aff_map_nodes_t mpidr_nodes,
Achin Gupta0959db52013-12-02 17:33:04 +0000190 int start_afflvl,
191 int end_afflvl,
192 unsigned long mpidr)
193{
194 int rc = PSCI_E_INVALID_PARAMS, level;
Dan Handleye2712bc2014-04-10 15:37:22 +0100195 aff_map_node_t *node;
Achin Gupta0959db52013-12-02 17:33:04 +0000196
197 for (level = start_afflvl; level <= end_afflvl; level++) {
198 node = mpidr_nodes[level];
199 if (node == NULL)
200 continue;
201
202 /*
203 * TODO: In case of an error should there be a way
204 * of restoring what we might have torn down at
205 * lower affinity levels.
206 */
207 rc = psci_afflvl_off_handlers[level](mpidr, node);
208 if (rc != PSCI_E_SUCCESS)
209 break;
210 }
211
212 return rc;
213}
214
215/*******************************************************************************
216 * Top level handler which is called when a cpu wants to power itself down.
217 * It's assumed that along with turning the cpu off, higher affinity levels will
218 * be turned off as far as possible. It traverses through all the affinity
219 * levels performing generic, architectural, platform setup and state management
220 * e.g. for a cluster that's to be powered off, it will call the platform
221 * specific code which will disable coherency at the interconnect level if the
222 * cpu is the last in the cluster. For a cpu it could mean programming the power
223 * the power controller etc.
224 *
225 * The state of all the relevant affinity levels is changed prior to calling the
226 * affinity level specific handlers as their actions would depend upon the state
227 * the affinity level is about to enter.
228 *
229 * The affinity level specific handlers are called in ascending order i.e. from
230 * the lowest to the highest affinity level implemented by the platform because
231 * to turn off affinity level X it is neccesary to turn off affinity level X - 1
232 * first.
233 *
234 * CAUTION: This function is called with coherent stacks so that coherency can
235 * be turned off and caches can be flushed safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100236 ******************************************************************************/
237int psci_afflvl_off(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000238 int start_afflvl,
239 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100240{
Achin Gupta0959db52013-12-02 17:33:04 +0000241 int rc = PSCI_E_SUCCESS;
Dan Handleye2712bc2014-04-10 15:37:22 +0100242 mpidr_aff_map_nodes_t mpidr_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100243
244 mpidr &= MPIDR_AFFINITY_MASK;;
245
246 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000247 * Collect the pointers to the nodes in the topology tree for
248 * each affinity instance in the mpidr. If this function does
249 * not return successfully then either the mpidr or the affinity
250 * levels are incorrect. In either case, we cannot return back
251 * to the caller as it would not know what to do.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100252 */
Achin Gupta0959db52013-12-02 17:33:04 +0000253 rc = psci_get_aff_map_nodes(mpidr,
254 start_afflvl,
255 end_afflvl,
256 mpidr_nodes);
257 assert (rc == PSCI_E_SUCCESS);
258
Achin Gupta4f6ad662013-10-25 09:08:21 +0100259 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000260 * This function acquires the lock corresponding to each affinity
261 * level so that by the time all locks are taken, the system topology
262 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100263 */
Achin Gupta0959db52013-12-02 17:33:04 +0000264 psci_acquire_afflvl_locks(mpidr,
265 start_afflvl,
266 end_afflvl,
267 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100268
Achin Gupta0959db52013-12-02 17:33:04 +0000269 /* Perform generic, architecture and platform specific handling */
270 rc = psci_call_off_handlers(mpidr_nodes,
271 start_afflvl,
272 end_afflvl,
273 mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100274
275 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000276 * Release the locks corresponding to each affinity level in the
277 * reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100278 */
Achin Gupta0959db52013-12-02 17:33:04 +0000279 psci_release_afflvl_locks(mpidr,
280 start_afflvl,
281 end_afflvl,
282 mpidr_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100283
Achin Gupta4f6ad662013-10-25 09:08:21 +0100284 return rc;
285}