blob: c9c3b2ce6cc42796cab0e828c5ce880bea2094b2 [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_on_handler)(unsigned long,
41 aff_map_node *,
42 unsigned long,
43 unsigned long);
44
45/*******************************************************************************
46 * This function checks whether a cpu which has been requested to be turned on
47 * is OFF to begin with.
48 ******************************************************************************/
49static int cpu_on_validate_state(unsigned int state)
50{
51 unsigned int psci_state;
52
53 /* Get the raw psci state */
54 psci_state = psci_get_state(state);
55
56 if (psci_state == PSCI_STATE_ON || psci_state == PSCI_STATE_SUSPEND)
57 return PSCI_E_ALREADY_ON;
58
59 if (psci_state == PSCI_STATE_ON_PENDING)
60 return PSCI_E_ON_PENDING;
61
62 assert(psci_state == PSCI_STATE_OFF);
63 return PSCI_E_SUCCESS;
64}
65
66/*******************************************************************************
67 * Handler routine to turn a cpu on. It takes care of any generic, architectural
68 * or platform specific setup required.
69 * TODO: Split this code across separate handlers for each type of setup?
70 ******************************************************************************/
71static int psci_afflvl0_on(unsigned long target_cpu,
72 aff_map_node *cpu_node,
73 unsigned long ns_entrypoint,
74 unsigned long context_id)
75{
76 unsigned int index, plat_state;
77 unsigned long psci_entrypoint;
78 int rc;
79
80 /* Sanity check to safeguard against data corruption */
81 assert(cpu_node->level == MPIDR_AFFLVL0);
82
83 /*
84 * Generic management: Ensure that the cpu is off to be
85 * turned on
86 */
87 rc = cpu_on_validate_state(cpu_node->state);
88 if (rc != PSCI_E_SUCCESS)
89 return rc;
90
91 /*
92 * Arch. management: Derive the re-entry information for
93 * the non-secure world from the non-secure state from
94 * where this call originated.
95 */
96 index = cpu_node->data;
97 rc = psci_set_ns_entry_info(index, ns_entrypoint, context_id);
98 if (rc != PSCI_E_SUCCESS)
99 return rc;
100
101 /* Set the secure world (EL3) re-entry point after BL1 */
102 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
103
104 /*
105 * Plat. management: Give the platform the current state
106 * of the target cpu to allow it to perform the necessary
107 * steps to power on.
108 */
109 if (psci_plat_pm_ops->affinst_on) {
110
111 /* Get the current physical state of this cpu */
112 plat_state = psci_get_aff_phys_state(cpu_node);
113 rc = psci_plat_pm_ops->affinst_on(target_cpu,
114 psci_entrypoint,
115 ns_entrypoint,
116 cpu_node->level,
117 plat_state);
118 }
119
120 return rc;
121}
122
123/*******************************************************************************
124 * Handler routine to turn a cluster on. It takes care or any generic, arch.
125 * or platform specific setup required.
126 * TODO: Split this code across separate handlers for each type of setup?
127 ******************************************************************************/
128static int psci_afflvl1_on(unsigned long target_cpu,
129 aff_map_node *cluster_node,
130 unsigned long ns_entrypoint,
131 unsigned long context_id)
132{
133 int rc = PSCI_E_SUCCESS;
134 unsigned int plat_state;
135 unsigned long psci_entrypoint;
136
137 assert(cluster_node->level == MPIDR_AFFLVL1);
138
139 /*
140 * There is no generic and arch. specific cluster
141 * management required
142 */
143
144 /*
145 * Plat. management: Give the platform the current state
146 * of the target cpu to allow it to perform the necessary
147 * steps to power on.
148 */
149 if (psci_plat_pm_ops->affinst_on) {
150 plat_state = psci_get_aff_phys_state(cluster_node);
151 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
152 rc = psci_plat_pm_ops->affinst_on(target_cpu,
153 psci_entrypoint,
154 ns_entrypoint,
155 cluster_node->level,
156 plat_state);
157 }
158
159 return rc;
160}
161
162/*******************************************************************************
163 * Handler routine to turn a cluster of clusters on. It takes care or any
164 * generic, arch. or platform specific setup required.
165 * TODO: Split this code across separate handlers for each type of setup?
166 ******************************************************************************/
167static int psci_afflvl2_on(unsigned long target_cpu,
168 aff_map_node *system_node,
169 unsigned long ns_entrypoint,
170 unsigned long context_id)
171{
172 int rc = PSCI_E_SUCCESS;
173 unsigned int plat_state;
174 unsigned long psci_entrypoint;
175
176 /* Cannot go beyond affinity level 2 in this psci imp. */
177 assert(system_node->level == MPIDR_AFFLVL2);
178
179 /*
180 * There is no generic and arch. specific system management
181 * required
182 */
183
184 /*
185 * Plat. management: Give the platform the current state
186 * of the target cpu to allow it to perform the necessary
187 * steps to power on.
188 */
189 if (psci_plat_pm_ops->affinst_on) {
190 plat_state = psci_get_aff_phys_state(system_node);
191 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
192 rc = psci_plat_pm_ops->affinst_on(target_cpu,
193 psci_entrypoint,
194 ns_entrypoint,
195 system_node->level,
196 plat_state);
197 }
198
199 return rc;
200}
201
202/* Private data structure to make this handlers accessible through indexing */
203static const afflvl_on_handler psci_afflvl_on_handlers[] = {
204 psci_afflvl0_on,
205 psci_afflvl1_on,
206 psci_afflvl2_on,
207};
208
209/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000210 * This function takes an array of pointers to affinity instance nodes in the
211 * topology tree and calls the on handler for the corresponding affinity
212 * levels
213 ******************************************************************************/
214static int psci_call_on_handlers(mpidr_aff_map_nodes target_cpu_nodes,
215 int start_afflvl,
216 int end_afflvl,
217 unsigned long target_cpu,
218 unsigned long entrypoint,
219 unsigned long context_id)
220{
221 int rc = PSCI_E_INVALID_PARAMS, level;
222 aff_map_node *node;
223
224 for (level = end_afflvl; level >= start_afflvl; level--) {
225 node = target_cpu_nodes[level];
226 if (node == NULL)
227 continue;
228
229 /*
230 * TODO: In case of an error should there be a way
231 * of undoing what we might have setup at higher
232 * affinity levels.
233 */
234 rc = psci_afflvl_on_handlers[level](target_cpu,
235 node,
236 entrypoint,
237 context_id);
238 if (rc != PSCI_E_SUCCESS)
239 break;
240 }
241
242 return rc;
243}
244
245/*******************************************************************************
246 * Generic handler which is called to physically power on a cpu identified by
247 * its mpidr. It traverses through all the affinity levels performing generic,
248 * architectural, platform setup and state management e.g. for a cpu that is
249 * to be powered on, it will ensure that enough information is stashed for it
250 * to resume execution in the non-secure security state.
251 *
252 * The state of all the relevant affinity levels is changed after calling the
253 * affinity level specific handlers as their actions would depend upon the state
254 * the affinity level is currently in.
255 *
256 * The affinity level specific handlers are called in descending order i.e. from
257 * the highest to the lowest affinity level implemented by the platform because
258 * to turn on affinity level X it is neccesary to turn on affinity level X + 1
259 * first.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100260 ******************************************************************************/
261int psci_afflvl_on(unsigned long target_cpu,
262 unsigned long entrypoint,
263 unsigned long context_id,
Achin Gupta0959db52013-12-02 17:33:04 +0000264 int start_afflvl,
265 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100266{
Achin Gupta0959db52013-12-02 17:33:04 +0000267 int rc = PSCI_E_SUCCESS;
268 mpidr_aff_map_nodes target_cpu_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100269 unsigned long mpidr = read_mpidr() & MPIDR_AFFINITY_MASK;
270
271 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000272 * Collect the pointers to the nodes in the topology tree for
273 * each affinity instance in the mpidr. If this function does
274 * not return successfully then either the mpidr or the affinity
275 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100276 */
Achin Gupta0959db52013-12-02 17:33:04 +0000277 rc = psci_get_aff_map_nodes(target_cpu,
278 start_afflvl,
279 end_afflvl,
280 target_cpu_nodes);
281 if (rc != PSCI_E_SUCCESS)
282 return rc;
283
Achin Gupta4f6ad662013-10-25 09:08:21 +0100284
285 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000286 * This function acquires the lock corresponding to each affinity
287 * level so that by the time all locks are taken, the system topology
288 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100289 */
Achin Gupta0959db52013-12-02 17:33:04 +0000290 psci_acquire_afflvl_locks(mpidr,
291 start_afflvl,
292 end_afflvl,
293 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100294
Achin Gupta0959db52013-12-02 17:33:04 +0000295 /* Perform generic, architecture and platform specific handling. */
296 rc = psci_call_on_handlers(target_cpu_nodes,
297 start_afflvl,
298 end_afflvl,
299 target_cpu,
300 entrypoint,
301 context_id);
302 if (rc != PSCI_E_SUCCESS)
303 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100304
305 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000306 * State management: Update the state of each affinity instance
307 * between the start and end affinity levels
Achin Gupta4f6ad662013-10-25 09:08:21 +0100308 */
Achin Gupta0959db52013-12-02 17:33:04 +0000309 psci_change_state(target_cpu_nodes,
310 start_afflvl,
311 end_afflvl,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100312 PSCI_STATE_ON_PENDING);
313
314exit:
315 /*
316 * This loop releases the lock corresponding to each affinity level
Achin Gupta0959db52013-12-02 17:33:04 +0000317 * in the reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100318 */
Achin Gupta0959db52013-12-02 17:33:04 +0000319 psci_release_afflvl_locks(mpidr,
320 start_afflvl,
321 end_afflvl,
322 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100323
324 return rc;
325}
326
327/*******************************************************************************
328 * The following functions finish an earlier affinity power on request. They
329 * are called by the common finisher routine in psci_common.c.
330 ******************************************************************************/
331static unsigned int psci_afflvl0_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000332 aff_map_node *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100333{
Achin Gupta0959db52013-12-02 17:33:04 +0000334 unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100335
336 assert(cpu_node->level == MPIDR_AFFLVL0);
337
Achin Gupta0959db52013-12-02 17:33:04 +0000338 /* Ensure we have been explicitly woken up by another cpu */
339 state = psci_get_state(cpu_node->state);
340 assert(state == PSCI_STATE_ON_PENDING);
341
Achin Gupta4f6ad662013-10-25 09:08:21 +0100342 /*
343 * Plat. management: Perform the platform specific actions
344 * for this cpu e.g. enabling the gic or zeroing the mailbox
345 * register. The actual state of this cpu has already been
346 * changed.
347 */
348 if (psci_plat_pm_ops->affinst_on_finish) {
349
Achin Gupta0959db52013-12-02 17:33:04 +0000350 /* Get the physical state of this cpu */
351 plat_state = psci_get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100352 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
353 cpu_node->level,
354 plat_state);
355 assert(rc == PSCI_E_SUCCESS);
356 }
357
358 /*
359 * Arch. management: Turn on mmu & restore architectural state
360 */
361 write_vbar((unsigned long) runtime_exceptions);
362 enable_mmu();
363
364 /*
365 * All the platform specific actions for turning this cpu
366 * on have completed. Perform enough arch.initialization
367 * to run in the non-secure address space.
368 */
369 bl31_arch_setup();
370
371 /*
372 * Generic management: Now we just need to retrieve the
373 * information that we had stashed away during the cpu_on
Achin Gupta3140a9e2013-12-02 16:23:12 +0000374 * call to set this cpu on its way. First get the index
Achin Gupta4f6ad662013-10-25 09:08:21 +0100375 * for restoring the re-entry info
376 */
377 index = cpu_node->data;
Achin Guptac8afc782013-11-25 18:45:02 +0000378 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100379
380 /* Clean caches before re-entering normal world */
381 dcsw_op_louis(DCCSW);
382
383 return rc;
384}
385
386static unsigned int psci_afflvl1_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000387 aff_map_node *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100388{
Achin Gupta0959db52013-12-02 17:33:04 +0000389 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100390
391 assert(cluster_node->level == MPIDR_AFFLVL1);
392
393 /*
394 * Plat. management: Perform the platform specific actions
395 * as per the old state of the cluster e.g. enabling
396 * coherency at the interconnect depends upon the state with
397 * which this cluster was powered up. If anything goes wrong
398 * then assert as there is no way to recover from this
399 * situation.
400 */
401 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000402
403 /* Get the physical state of this cluster */
404 plat_state = psci_get_aff_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100405 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
406 cluster_node->level,
407 plat_state);
408 assert(rc == PSCI_E_SUCCESS);
409 }
410
411 return rc;
412}
413
414
415static unsigned int psci_afflvl2_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000416 aff_map_node *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100417{
Achin Gupta0959db52013-12-02 17:33:04 +0000418 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100419
420 /* Cannot go beyond this affinity level */
421 assert(system_node->level == MPIDR_AFFLVL2);
422
423 /*
424 * Currently, there are no architectural actions to perform
425 * at the system level.
426 */
427
428 /*
429 * Plat. management: Perform the platform specific actions
430 * as per the old state of the cluster e.g. enabling
431 * coherency at the interconnect depends upon the state with
432 * which this cluster was powered up. If anything goes wrong
433 * then assert as there is no way to recover from this
434 * situation.
435 */
436 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000437
438 /* Get the physical state of the system */
439 plat_state = psci_get_aff_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100440 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
441 system_node->level,
442 plat_state);
443 assert(rc == PSCI_E_SUCCESS);
444 }
445
446 return rc;
447}
448
449const afflvl_power_on_finisher psci_afflvl_on_finishers[] = {
450 psci_afflvl0_on_finish,
451 psci_afflvl1_on_finish,
452 psci_afflvl2_on_finish,
453};
454