blob: 1275358a70e7f5d0d5e2ea20b7acaa60074a9a84 [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
31#include <stdio.h>
Dan Handley5b827a82014-04-17 18:53:42 +010032#include <stdint.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010033#include <string.h>
34#include <assert.h>
Dan Handley5b827a82014-04-17 18:53:42 +010035#include <bl31.h>
Achin Gupta0a9f7472014-02-09 17:48:12 +000036#include <debug.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010037#include <arch_helpers.h>
38#include <console.h>
39#include <platform.h>
40#include <psci.h>
Achin Gupta0a9f7472014-02-09 17:48:12 +000041#include <context_mgmt.h>
Dan Handley5b827a82014-04-17 18:53:42 +010042#include <runtime_svc.h>
Dan Handley35e98e52014-04-09 13:13:04 +010043#include "psci_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010044
45typedef int (*afflvl_on_handler)(unsigned long,
46 aff_map_node *,
47 unsigned long,
48 unsigned long);
49
50/*******************************************************************************
51 * This function checks whether a cpu which has been requested to be turned on
52 * is OFF to begin with.
53 ******************************************************************************/
Achin Gupta75f73672013-12-05 16:33:10 +000054static int cpu_on_validate_state(aff_map_node *node)
Achin Gupta4f6ad662013-10-25 09:08:21 +010055{
56 unsigned int psci_state;
57
58 /* Get the raw psci state */
Achin Gupta75f73672013-12-05 16:33:10 +000059 psci_state = psci_get_state(node);
Achin Gupta4f6ad662013-10-25 09:08:21 +010060
61 if (psci_state == PSCI_STATE_ON || psci_state == PSCI_STATE_SUSPEND)
62 return PSCI_E_ALREADY_ON;
63
64 if (psci_state == PSCI_STATE_ON_PENDING)
65 return PSCI_E_ON_PENDING;
66
67 assert(psci_state == PSCI_STATE_OFF);
68 return PSCI_E_SUCCESS;
69}
70
71/*******************************************************************************
72 * Handler routine to turn a cpu on. It takes care of any generic, architectural
73 * or platform specific setup required.
74 * TODO: Split this code across separate handlers for each type of setup?
75 ******************************************************************************/
76static int psci_afflvl0_on(unsigned long target_cpu,
77 aff_map_node *cpu_node,
78 unsigned long ns_entrypoint,
79 unsigned long context_id)
80{
81 unsigned int index, plat_state;
82 unsigned long psci_entrypoint;
83 int rc;
84
85 /* Sanity check to safeguard against data corruption */
86 assert(cpu_node->level == MPIDR_AFFLVL0);
87
88 /*
89 * Generic management: Ensure that the cpu is off to be
90 * turned on
91 */
Achin Gupta75f73672013-12-05 16:33:10 +000092 rc = cpu_on_validate_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +010093 if (rc != PSCI_E_SUCCESS)
94 return rc;
95
96 /*
Achin Gupta607084e2014-02-09 18:24:19 +000097 * Call the cpu on handler registered by the Secure Payload Dispatcher
98 * to let it do any bookeeping. If the handler encounters an error, it's
99 * expected to assert within
100 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000101 if (psci_spd_pm && psci_spd_pm->svc_on)
102 psci_spd_pm->svc_on(target_cpu);
Achin Gupta607084e2014-02-09 18:24:19 +0000103
104 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100105 * Arch. management: Derive the re-entry information for
106 * the non-secure world from the non-secure state from
107 * where this call originated.
108 */
109 index = cpu_node->data;
110 rc = psci_set_ns_entry_info(index, ns_entrypoint, context_id);
111 if (rc != PSCI_E_SUCCESS)
112 return rc;
113
114 /* Set the secure world (EL3) re-entry point after BL1 */
115 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
116
Achin Gupta75f73672013-12-05 16:33:10 +0000117 /* State management: Set this cpu's state as ON PENDING */
118 psci_set_state(cpu_node, PSCI_STATE_ON_PENDING);
119
Achin Gupta4f6ad662013-10-25 09:08:21 +0100120 /*
121 * Plat. management: Give the platform the current state
122 * of the target cpu to allow it to perform the necessary
123 * steps to power on.
124 */
125 if (psci_plat_pm_ops->affinst_on) {
126
127 /* Get the current physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000128 plat_state = psci_get_phys_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100129 rc = psci_plat_pm_ops->affinst_on(target_cpu,
130 psci_entrypoint,
131 ns_entrypoint,
132 cpu_node->level,
133 plat_state);
134 }
135
136 return rc;
137}
138
139/*******************************************************************************
140 * Handler routine to turn a cluster on. It takes care or any generic, arch.
141 * or platform specific setup required.
142 * TODO: Split this code across separate handlers for each type of setup?
143 ******************************************************************************/
144static int psci_afflvl1_on(unsigned long target_cpu,
145 aff_map_node *cluster_node,
146 unsigned long ns_entrypoint,
147 unsigned long context_id)
148{
149 int rc = PSCI_E_SUCCESS;
150 unsigned int plat_state;
151 unsigned long psci_entrypoint;
152
153 assert(cluster_node->level == MPIDR_AFFLVL1);
154
155 /*
156 * There is no generic and arch. specific cluster
157 * management required
158 */
159
Achin Gupta75f73672013-12-05 16:33:10 +0000160 /* State management: Is not required while turning a cluster on */
161
Achin Gupta4f6ad662013-10-25 09:08:21 +0100162 /*
163 * Plat. management: Give the platform the current state
164 * of the target cpu to allow it to perform the necessary
165 * steps to power on.
166 */
167 if (psci_plat_pm_ops->affinst_on) {
Achin Gupta75f73672013-12-05 16:33:10 +0000168 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100169 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
170 rc = psci_plat_pm_ops->affinst_on(target_cpu,
171 psci_entrypoint,
172 ns_entrypoint,
173 cluster_node->level,
174 plat_state);
175 }
176
177 return rc;
178}
179
180/*******************************************************************************
181 * Handler routine to turn a cluster of clusters on. It takes care or any
182 * generic, arch. or platform specific setup required.
183 * TODO: Split this code across separate handlers for each type of setup?
184 ******************************************************************************/
185static int psci_afflvl2_on(unsigned long target_cpu,
186 aff_map_node *system_node,
187 unsigned long ns_entrypoint,
188 unsigned long context_id)
189{
190 int rc = PSCI_E_SUCCESS;
191 unsigned int plat_state;
192 unsigned long psci_entrypoint;
193
194 /* Cannot go beyond affinity level 2 in this psci imp. */
195 assert(system_node->level == MPIDR_AFFLVL2);
196
197 /*
198 * There is no generic and arch. specific system management
199 * required
200 */
201
Achin Gupta75f73672013-12-05 16:33:10 +0000202 /* State management: Is not required while turning a system on */
203
Achin Gupta4f6ad662013-10-25 09:08:21 +0100204 /*
205 * Plat. management: Give the platform the current state
206 * of the target cpu to allow it to perform the necessary
207 * steps to power on.
208 */
209 if (psci_plat_pm_ops->affinst_on) {
Achin Gupta75f73672013-12-05 16:33:10 +0000210 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100211 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
212 rc = psci_plat_pm_ops->affinst_on(target_cpu,
213 psci_entrypoint,
214 ns_entrypoint,
215 system_node->level,
216 plat_state);
217 }
218
219 return rc;
220}
221
222/* Private data structure to make this handlers accessible through indexing */
223static const afflvl_on_handler psci_afflvl_on_handlers[] = {
224 psci_afflvl0_on,
225 psci_afflvl1_on,
226 psci_afflvl2_on,
227};
228
229/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000230 * This function takes an array of pointers to affinity instance nodes in the
231 * topology tree and calls the on handler for the corresponding affinity
232 * levels
233 ******************************************************************************/
234static int psci_call_on_handlers(mpidr_aff_map_nodes target_cpu_nodes,
235 int start_afflvl,
236 int end_afflvl,
237 unsigned long target_cpu,
238 unsigned long entrypoint,
239 unsigned long context_id)
240{
241 int rc = PSCI_E_INVALID_PARAMS, level;
242 aff_map_node *node;
243
244 for (level = end_afflvl; level >= start_afflvl; level--) {
245 node = target_cpu_nodes[level];
246 if (node == NULL)
247 continue;
248
249 /*
250 * TODO: In case of an error should there be a way
251 * of undoing what we might have setup at higher
252 * affinity levels.
253 */
254 rc = psci_afflvl_on_handlers[level](target_cpu,
255 node,
256 entrypoint,
257 context_id);
258 if (rc != PSCI_E_SUCCESS)
259 break;
260 }
261
262 return rc;
263}
264
265/*******************************************************************************
266 * Generic handler which is called to physically power on a cpu identified by
267 * its mpidr. It traverses through all the affinity levels performing generic,
268 * architectural, platform setup and state management e.g. for a cpu that is
269 * to be powered on, it will ensure that enough information is stashed for it
270 * to resume execution in the non-secure security state.
271 *
272 * The state of all the relevant affinity levels is changed after calling the
273 * affinity level specific handlers as their actions would depend upon the state
274 * the affinity level is currently in.
275 *
276 * The affinity level specific handlers are called in descending order i.e. from
277 * the highest to the lowest affinity level implemented by the platform because
278 * to turn on affinity level X it is neccesary to turn on affinity level X + 1
279 * first.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100280 ******************************************************************************/
281int psci_afflvl_on(unsigned long target_cpu,
282 unsigned long entrypoint,
283 unsigned long context_id,
Achin Gupta0959db52013-12-02 17:33:04 +0000284 int start_afflvl,
285 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100286{
Achin Gupta0959db52013-12-02 17:33:04 +0000287 int rc = PSCI_E_SUCCESS;
288 mpidr_aff_map_nodes target_cpu_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100289 unsigned long mpidr = read_mpidr() & MPIDR_AFFINITY_MASK;
290
291 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000292 * Collect the pointers to the nodes in the topology tree for
293 * each affinity instance in the mpidr. If this function does
294 * not return successfully then either the mpidr or the affinity
295 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100296 */
Achin Gupta0959db52013-12-02 17:33:04 +0000297 rc = psci_get_aff_map_nodes(target_cpu,
298 start_afflvl,
299 end_afflvl,
300 target_cpu_nodes);
301 if (rc != PSCI_E_SUCCESS)
302 return rc;
303
Achin Gupta4f6ad662013-10-25 09:08:21 +0100304
305 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000306 * This function acquires the lock corresponding to each affinity
307 * level so that by the time all locks are taken, the system topology
308 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100309 */
Achin Gupta0959db52013-12-02 17:33:04 +0000310 psci_acquire_afflvl_locks(mpidr,
311 start_afflvl,
312 end_afflvl,
313 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100314
Achin Gupta0959db52013-12-02 17:33:04 +0000315 /* Perform generic, architecture and platform specific handling. */
316 rc = psci_call_on_handlers(target_cpu_nodes,
317 start_afflvl,
318 end_afflvl,
319 target_cpu,
320 entrypoint,
321 context_id);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100322
323 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100324 * This loop releases the lock corresponding to each affinity level
Achin Gupta0959db52013-12-02 17:33:04 +0000325 * in the reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100326 */
Achin Gupta0959db52013-12-02 17:33:04 +0000327 psci_release_afflvl_locks(mpidr,
328 start_afflvl,
329 end_afflvl,
330 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100331
332 return rc;
333}
334
335/*******************************************************************************
336 * The following functions finish an earlier affinity power on request. They
337 * are called by the common finisher routine in psci_common.c.
338 ******************************************************************************/
339static unsigned int psci_afflvl0_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000340 aff_map_node *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100341{
Achin Gupta0959db52013-12-02 17:33:04 +0000342 unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100343
344 assert(cpu_node->level == MPIDR_AFFLVL0);
345
Achin Gupta0959db52013-12-02 17:33:04 +0000346 /* Ensure we have been explicitly woken up by another cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000347 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000348 assert(state == PSCI_STATE_ON_PENDING);
349
Achin Gupta4f6ad662013-10-25 09:08:21 +0100350 /*
351 * Plat. management: Perform the platform specific actions
352 * for this cpu e.g. enabling the gic or zeroing the mailbox
353 * register. The actual state of this cpu has already been
354 * changed.
355 */
356 if (psci_plat_pm_ops->affinst_on_finish) {
357
Achin Gupta0959db52013-12-02 17:33:04 +0000358 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000359 plat_state = get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100360 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
361 cpu_node->level,
362 plat_state);
363 assert(rc == PSCI_E_SUCCESS);
364 }
365
366 /*
367 * Arch. management: Turn on mmu & restore architectural state
368 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100369 enable_mmu();
370
371 /*
372 * All the platform specific actions for turning this cpu
373 * on have completed. Perform enough arch.initialization
374 * to run in the non-secure address space.
375 */
376 bl31_arch_setup();
377
378 /*
Achin Gupta607084e2014-02-09 18:24:19 +0000379 * Use the more complex exception vectors to enable SPD
380 * initialisation. SP_EL3 should point to a 'cpu_context'
381 * structure which has an exception stack allocated. The
382 * calling cpu should have set the context already
383 */
384 assert(cm_get_context(mpidr, NON_SECURE));
385 cm_set_next_eret_context(NON_SECURE);
386 write_vbar_el3((uint64_t) runtime_exceptions);
387
388 /*
389 * Call the cpu on finish handler registered by the Secure Payload
390 * Dispatcher to let it do any bookeeping. If the handler encounters an
391 * error, it's expected to assert within
392 */
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000393 if (psci_spd_pm && psci_spd_pm->svc_on_finish)
394 psci_spd_pm->svc_on_finish(0);
Achin Gupta607084e2014-02-09 18:24:19 +0000395
396 /*
Achin Gupta4f6ad662013-10-25 09:08:21 +0100397 * Generic management: Now we just need to retrieve the
398 * information that we had stashed away during the cpu_on
Achin Gupta3140a9e2013-12-02 16:23:12 +0000399 * call to set this cpu on its way. First get the index
Achin Gupta4f6ad662013-10-25 09:08:21 +0100400 * for restoring the re-entry info
401 */
402 index = cpu_node->data;
Achin Guptac8afc782013-11-25 18:45:02 +0000403 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100404
Achin Gupta75f73672013-12-05 16:33:10 +0000405 /* State management: mark this cpu as on */
406 psci_set_state(cpu_node, PSCI_STATE_ON);
407
Achin Gupta4f6ad662013-10-25 09:08:21 +0100408 /* Clean caches before re-entering normal world */
409 dcsw_op_louis(DCCSW);
410
411 return rc;
412}
413
414static unsigned int psci_afflvl1_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000415 aff_map_node *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100416{
Achin Gupta0959db52013-12-02 17:33:04 +0000417 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100418
419 assert(cluster_node->level == MPIDR_AFFLVL1);
420
421 /*
422 * Plat. management: Perform the platform specific actions
423 * as per the old state of the cluster e.g. enabling
424 * coherency at the interconnect depends upon the state with
425 * which this cluster was powered up. If anything goes wrong
426 * then assert as there is no way to recover from this
427 * situation.
428 */
429 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000430
431 /* Get the physical state of this cluster */
Achin Gupta75f73672013-12-05 16:33:10 +0000432 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100433 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
434 cluster_node->level,
435 plat_state);
436 assert(rc == PSCI_E_SUCCESS);
437 }
438
Achin Gupta75f73672013-12-05 16:33:10 +0000439 /* State management: Increment the cluster reference count */
440 psci_set_state(cluster_node, PSCI_STATE_ON);
441
Achin Gupta4f6ad662013-10-25 09:08:21 +0100442 return rc;
443}
444
445
446static unsigned int psci_afflvl2_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000447 aff_map_node *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100448{
Achin Gupta0959db52013-12-02 17:33:04 +0000449 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100450
451 /* Cannot go beyond this affinity level */
452 assert(system_node->level == MPIDR_AFFLVL2);
453
454 /*
455 * Currently, there are no architectural actions to perform
456 * at the system level.
457 */
458
459 /*
460 * Plat. management: Perform the platform specific actions
461 * as per the old state of the cluster e.g. enabling
462 * coherency at the interconnect depends upon the state with
463 * which this cluster was powered up. If anything goes wrong
464 * then assert as there is no way to recover from this
465 * situation.
466 */
467 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000468
469 /* Get the physical state of the system */
Achin Gupta75f73672013-12-05 16:33:10 +0000470 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100471 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
472 system_node->level,
473 plat_state);
474 assert(rc == PSCI_E_SUCCESS);
475 }
476
Achin Gupta75f73672013-12-05 16:33:10 +0000477 /* State management: Increment the system reference count */
478 psci_set_state(system_node, PSCI_STATE_ON);
479
Achin Gupta4f6ad662013-10-25 09:08:21 +0100480 return rc;
481}
482
483const afflvl_power_on_finisher psci_afflvl_on_finishers[] = {
484 psci_afflvl0_on_finish,
485 psci_afflvl1_on_finish,
486 psci_afflvl2_on_finish,
487};
488