blob: b62ae2955b9a2f725f6fd8d74e674587cab0463e [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleyab2d31e2013-12-02 19:25:12 +00002 * Copyright (c) 2013, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <stdio.h>
32#include <string.h>
33#include <assert.h>
34#include <arch_helpers.h>
35#include <console.h>
36#include <platform.h>
37#include <psci.h>
38#include <psci_private.h>
39
40typedef int (*afflvl_off_handler)(unsigned long, aff_map_node *);
41
42/*******************************************************************************
43 * The next three functions implement a handler for each supported affinity
44 * level which is called when that affinity level is turned off.
45 ******************************************************************************/
46static int psci_afflvl0_off(unsigned long mpidr, aff_map_node *cpu_node)
47{
48 unsigned int index, plat_state;
49 int rc = PSCI_E_SUCCESS;
50 unsigned long sctlr = read_sctlr();
51
52 assert(cpu_node->level == MPIDR_AFFLVL0);
53
54 /*
55 * Generic management: Get the index for clearing any
56 * lingering re-entry information
57 */
58 index = cpu_node->data;
59 memset(&psci_ns_entry_info[index], 0, sizeof(psci_ns_entry_info[index]));
60
61 /*
62 * Arch. management. Perform the necessary steps to flush all
63 * cpu caches.
64 *
65 * TODO: This power down sequence varies across cpus so it needs to be
66 * abstracted out on the basis of the MIDR like in cpu_reset_handler().
67 * Do the bare minimal for the time being. Fix this before porting to
68 * Cortex models.
69 */
70 sctlr &= ~SCTLR_C_BIT;
71 write_sctlr(sctlr);
72
73 /*
74 * CAUTION: This flush to the level of unification makes an assumption
75 * about the cache hierarchy at affinity level 0 (cpu) in the platform.
76 * Ideally the platform should tell psci which levels to flush to exit
77 * coherency.
78 */
79 dcsw_op_louis(DCCISW);
80
81 /*
82 * Plat. management: Perform platform specific actions to turn this
83 * cpu off e.g. exit cpu coherency, program the power controller etc.
84 */
85 if (psci_plat_pm_ops->affinst_off) {
86
87 /* Get the current physical state of this cpu */
88 plat_state = psci_get_aff_phys_state(cpu_node);
89 rc = psci_plat_pm_ops->affinst_off(mpidr,
90 cpu_node->level,
91 plat_state);
92 }
93
Achin Gupta4f6ad662013-10-25 09:08:21 +010094 return rc;
95}
96
97static int psci_afflvl1_off(unsigned long mpidr, aff_map_node *cluster_node)
98{
99 int rc = PSCI_E_SUCCESS;
100 unsigned int plat_state;
101
102 /* Sanity check the cluster level */
103 assert(cluster_node->level == MPIDR_AFFLVL1);
104
105 /*
106 * Keep the physical state of this cluster handy to decide
107 * what action needs to be taken
108 */
109 plat_state = psci_get_aff_phys_state(cluster_node);
110
111 /*
112 * Arch. Management. Flush all levels of caches to PoC if
113 * the cluster is to be shutdown
114 */
115 if (plat_state == PSCI_STATE_OFF)
116 dcsw_op_all(DCCISW);
117
118 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000119 * Plat. Management. Allow the platform to do its cluster
Achin Gupta4f6ad662013-10-25 09:08:21 +0100120 * specific bookeeping e.g. turn off interconnect coherency,
121 * program the power controller etc.
122 */
123 if (psci_plat_pm_ops->affinst_off)
124 rc = psci_plat_pm_ops->affinst_off(mpidr,
125 cluster_node->level,
126 plat_state);
127
128 return rc;
129}
130
131static int psci_afflvl2_off(unsigned long mpidr, aff_map_node *system_node)
132{
133 int rc = PSCI_E_SUCCESS;
134 unsigned int plat_state;
135
136 /* Cannot go beyond this level */
137 assert(system_node->level == MPIDR_AFFLVL2);
138
139 /*
140 * Keep the physical state of the system handy to decide what
141 * action needs to be taken
142 */
143 plat_state = psci_get_aff_phys_state(system_node);
144
145 /* No arch. and generic bookeeping to do here currently */
146
147 /*
Achin Gupta3140a9e2013-12-02 16:23:12 +0000148 * Plat. Management : Allow the platform to do its bookeeping
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149 * at this affinity level
150 */
151 if (psci_plat_pm_ops->affinst_off)
152 rc = psci_plat_pm_ops->affinst_off(mpidr,
153 system_node->level,
154 plat_state);
155 return rc;
156}
157
158static const afflvl_off_handler psci_afflvl_off_handlers[] = {
159 psci_afflvl0_off,
160 psci_afflvl1_off,
161 psci_afflvl2_off,
162};
163
164/*******************************************************************************
165 * This function implements the core of the processing required to turn a cpu
166 * off. It's assumed that along with turning the cpu off, higher affinity levels
167 * will be turned off as far as possible. We first need to determine the new
168 * state off all the affinity instances in the mpidr corresponding to the target
169 * cpu. Action will be taken on the basis of this new state. To do the state
170 * change we first need to acquire the locks for all the implemented affinity
171 * level to be able to snapshot the system state. Then we need to start turning
172 * affinity levels off from the lowest to the highest (e.g. a cpu needs to be
173 * off before a cluster can be turned off). To achieve this flow, we start
174 * acquiring the locks from the highest to the lowest affinity level. Once we
175 * reach affinity level 0, we do the state change followed by the actions
176 * corresponding to the new state for affinity level 0. Actions as per the
177 * updated state for higher affinity levels are performed as we unwind back to
178 * highest affinity level.
179 ******************************************************************************/
180int psci_afflvl_off(unsigned long mpidr,
181 int cur_afflvl,
182 int tgt_afflvl)
183{
184 int rc = PSCI_E_SUCCESS, level;
185 unsigned int next_state, prev_state;
186 aff_map_node *aff_node;
187
188 mpidr &= MPIDR_AFFINITY_MASK;;
189
190 /*
191 * Some affinity instances at levels between the current and
192 * target levels could be absent in the mpidr. Skip them and
193 * start from the first present instance.
194 */
195 level = psci_get_first_present_afflvl(mpidr,
196 cur_afflvl,
197 tgt_afflvl,
198 &aff_node);
199 /*
200 * Return if there are no more affinity instances beyond this
201 * level to process. Else ensure that the returned affinity
202 * node makes sense.
203 */
204 if (aff_node == NULL)
205 return rc;
206
207 assert(level == aff_node->level);
208
209 /*
210 * This function acquires the lock corresponding to each
211 * affinity level so that state management can be done safely.
212 */
213 bakery_lock_get(mpidr, &aff_node->lock);
214
215 /* Keep the old state and the next one handy */
216 prev_state = psci_get_state(aff_node->state);
217 next_state = PSCI_STATE_OFF;
218
219 /*
220 * We start from the highest affinity level and work our way
221 * downwards to the lowest i.e. MPIDR_AFFLVL0.
222 */
223 if (aff_node->level == tgt_afflvl) {
224 psci_change_state(mpidr,
225 tgt_afflvl,
226 get_max_afflvl(),
227 next_state);
228 } else {
229 rc = psci_afflvl_off(mpidr, level - 1, tgt_afflvl);
230 if (rc != PSCI_E_SUCCESS) {
231 psci_set_state(aff_node->state, prev_state);
232 goto exit;
233 }
234 }
235
236 /*
237 * Perform generic, architecture and platform specific
238 * handling
239 */
240 rc = psci_afflvl_off_handlers[level](mpidr, aff_node);
241 if (rc != PSCI_E_SUCCESS) {
242 psci_set_state(aff_node->state, prev_state);
243 goto exit;
244 }
245
246 /*
247 * If all has gone as per plan then this cpu should be
248 * marked as OFF
249 */
250 if (level == MPIDR_AFFLVL0) {
251 next_state = psci_get_state(aff_node->state);
252 assert(next_state == PSCI_STATE_OFF);
253 }
254
255exit:
256 bakery_lock_release(mpidr, &aff_node->lock);
257 return rc;
258}