blob: d22904cd9559ddc297015dc141b78a4d1895f46d [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>
32#include <string.h>
33#include <assert.h>
Achin Gupta0a9f7472014-02-09 17:48:12 +000034#include <debug.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010035#include <arch_helpers.h>
36#include <console.h>
37#include <platform.h>
38#include <psci.h>
39#include <psci_private.h>
Achin Gupta0a9f7472014-02-09 17:48:12 +000040#include <context_mgmt.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010041
42typedef int (*afflvl_on_handler)(unsigned long,
43 aff_map_node *,
44 unsigned long,
45 unsigned long);
46
47/*******************************************************************************
48 * This function checks whether a cpu which has been requested to be turned on
49 * is OFF to begin with.
50 ******************************************************************************/
Achin Gupta75f73672013-12-05 16:33:10 +000051static int cpu_on_validate_state(aff_map_node *node)
Achin Gupta4f6ad662013-10-25 09:08:21 +010052{
53 unsigned int psci_state;
54
55 /* Get the raw psci state */
Achin Gupta75f73672013-12-05 16:33:10 +000056 psci_state = psci_get_state(node);
Achin Gupta4f6ad662013-10-25 09:08:21 +010057
58 if (psci_state == PSCI_STATE_ON || psci_state == PSCI_STATE_SUSPEND)
59 return PSCI_E_ALREADY_ON;
60
61 if (psci_state == PSCI_STATE_ON_PENDING)
62 return PSCI_E_ON_PENDING;
63
64 assert(psci_state == PSCI_STATE_OFF);
65 return PSCI_E_SUCCESS;
66}
67
68/*******************************************************************************
69 * Handler routine to turn a cpu on. It takes care of any generic, architectural
70 * or platform specific setup required.
71 * TODO: Split this code across separate handlers for each type of setup?
72 ******************************************************************************/
73static int psci_afflvl0_on(unsigned long target_cpu,
74 aff_map_node *cpu_node,
75 unsigned long ns_entrypoint,
76 unsigned long context_id)
77{
78 unsigned int index, plat_state;
79 unsigned long psci_entrypoint;
80 int rc;
81
82 /* Sanity check to safeguard against data corruption */
83 assert(cpu_node->level == MPIDR_AFFLVL0);
84
85 /*
86 * Generic management: Ensure that the cpu is off to be
87 * turned on
88 */
Achin Gupta75f73672013-12-05 16:33:10 +000089 rc = cpu_on_validate_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +010090 if (rc != PSCI_E_SUCCESS)
91 return rc;
92
93 /*
94 * Arch. management: Derive the re-entry information for
95 * the non-secure world from the non-secure state from
96 * where this call originated.
97 */
98 index = cpu_node->data;
99 rc = psci_set_ns_entry_info(index, ns_entrypoint, context_id);
100 if (rc != PSCI_E_SUCCESS)
101 return rc;
102
103 /* Set the secure world (EL3) re-entry point after BL1 */
104 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
105
Achin Gupta75f73672013-12-05 16:33:10 +0000106 /* State management: Set this cpu's state as ON PENDING */
107 psci_set_state(cpu_node, PSCI_STATE_ON_PENDING);
108
Achin Gupta4f6ad662013-10-25 09:08:21 +0100109 /*
110 * Plat. management: Give the platform the current state
111 * of the target cpu to allow it to perform the necessary
112 * steps to power on.
113 */
114 if (psci_plat_pm_ops->affinst_on) {
115
116 /* Get the current physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000117 plat_state = psci_get_phys_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100118 rc = psci_plat_pm_ops->affinst_on(target_cpu,
119 psci_entrypoint,
120 ns_entrypoint,
121 cpu_node->level,
122 plat_state);
123 }
124
125 return rc;
126}
127
128/*******************************************************************************
129 * Handler routine to turn a cluster on. It takes care or any generic, arch.
130 * or platform specific setup required.
131 * TODO: Split this code across separate handlers for each type of setup?
132 ******************************************************************************/
133static int psci_afflvl1_on(unsigned long target_cpu,
134 aff_map_node *cluster_node,
135 unsigned long ns_entrypoint,
136 unsigned long context_id)
137{
138 int rc = PSCI_E_SUCCESS;
139 unsigned int plat_state;
140 unsigned long psci_entrypoint;
141
142 assert(cluster_node->level == MPIDR_AFFLVL1);
143
144 /*
145 * There is no generic and arch. specific cluster
146 * management required
147 */
148
Achin Gupta75f73672013-12-05 16:33:10 +0000149 /* State management: Is not required while turning a cluster on */
150
Achin Gupta4f6ad662013-10-25 09:08:21 +0100151 /*
152 * Plat. management: Give the platform the current state
153 * of the target cpu to allow it to perform the necessary
154 * steps to power on.
155 */
156 if (psci_plat_pm_ops->affinst_on) {
Achin Gupta75f73672013-12-05 16:33:10 +0000157 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100158 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
159 rc = psci_plat_pm_ops->affinst_on(target_cpu,
160 psci_entrypoint,
161 ns_entrypoint,
162 cluster_node->level,
163 plat_state);
164 }
165
166 return rc;
167}
168
169/*******************************************************************************
170 * Handler routine to turn a cluster of clusters on. It takes care or any
171 * generic, arch. or platform specific setup required.
172 * TODO: Split this code across separate handlers for each type of setup?
173 ******************************************************************************/
174static int psci_afflvl2_on(unsigned long target_cpu,
175 aff_map_node *system_node,
176 unsigned long ns_entrypoint,
177 unsigned long context_id)
178{
179 int rc = PSCI_E_SUCCESS;
180 unsigned int plat_state;
181 unsigned long psci_entrypoint;
182
183 /* Cannot go beyond affinity level 2 in this psci imp. */
184 assert(system_node->level == MPIDR_AFFLVL2);
185
186 /*
187 * There is no generic and arch. specific system management
188 * required
189 */
190
Achin Gupta75f73672013-12-05 16:33:10 +0000191 /* State management: Is not required while turning a system on */
192
Achin Gupta4f6ad662013-10-25 09:08:21 +0100193 /*
194 * Plat. management: Give the platform the current state
195 * of the target cpu to allow it to perform the necessary
196 * steps to power on.
197 */
198 if (psci_plat_pm_ops->affinst_on) {
Achin Gupta75f73672013-12-05 16:33:10 +0000199 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100200 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
201 rc = psci_plat_pm_ops->affinst_on(target_cpu,
202 psci_entrypoint,
203 ns_entrypoint,
204 system_node->level,
205 plat_state);
206 }
207
208 return rc;
209}
210
211/* Private data structure to make this handlers accessible through indexing */
212static const afflvl_on_handler psci_afflvl_on_handlers[] = {
213 psci_afflvl0_on,
214 psci_afflvl1_on,
215 psci_afflvl2_on,
216};
217
218/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000219 * This function takes an array of pointers to affinity instance nodes in the
220 * topology tree and calls the on handler for the corresponding affinity
221 * levels
222 ******************************************************************************/
223static int psci_call_on_handlers(mpidr_aff_map_nodes target_cpu_nodes,
224 int start_afflvl,
225 int end_afflvl,
226 unsigned long target_cpu,
227 unsigned long entrypoint,
228 unsigned long context_id)
229{
230 int rc = PSCI_E_INVALID_PARAMS, level;
231 aff_map_node *node;
232
233 for (level = end_afflvl; level >= start_afflvl; level--) {
234 node = target_cpu_nodes[level];
235 if (node == NULL)
236 continue;
237
238 /*
239 * TODO: In case of an error should there be a way
240 * of undoing what we might have setup at higher
241 * affinity levels.
242 */
243 rc = psci_afflvl_on_handlers[level](target_cpu,
244 node,
245 entrypoint,
246 context_id);
247 if (rc != PSCI_E_SUCCESS)
248 break;
249 }
250
251 return rc;
252}
253
254/*******************************************************************************
255 * Generic handler which is called to physically power on a cpu identified by
256 * its mpidr. It traverses through all the affinity levels performing generic,
257 * architectural, platform setup and state management e.g. for a cpu that is
258 * to be powered on, it will ensure that enough information is stashed for it
259 * to resume execution in the non-secure security state.
260 *
261 * The state of all the relevant affinity levels is changed after calling the
262 * affinity level specific handlers as their actions would depend upon the state
263 * the affinity level is currently in.
264 *
265 * The affinity level specific handlers are called in descending order i.e. from
266 * the highest to the lowest affinity level implemented by the platform because
267 * to turn on affinity level X it is neccesary to turn on affinity level X + 1
268 * first.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100269 ******************************************************************************/
270int psci_afflvl_on(unsigned long target_cpu,
271 unsigned long entrypoint,
272 unsigned long context_id,
Achin Gupta0959db52013-12-02 17:33:04 +0000273 int start_afflvl,
274 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100275{
Achin Gupta0959db52013-12-02 17:33:04 +0000276 int rc = PSCI_E_SUCCESS;
277 mpidr_aff_map_nodes target_cpu_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100278 unsigned long mpidr = read_mpidr() & MPIDR_AFFINITY_MASK;
279
280 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000281 * Collect the pointers to the nodes in the topology tree for
282 * each affinity instance in the mpidr. If this function does
283 * not return successfully then either the mpidr or the affinity
284 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100285 */
Achin Gupta0959db52013-12-02 17:33:04 +0000286 rc = psci_get_aff_map_nodes(target_cpu,
287 start_afflvl,
288 end_afflvl,
289 target_cpu_nodes);
290 if (rc != PSCI_E_SUCCESS)
291 return rc;
292
Achin Gupta4f6ad662013-10-25 09:08:21 +0100293
294 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000295 * This function acquires the lock corresponding to each affinity
296 * level so that by the time all locks are taken, the system topology
297 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100298 */
Achin Gupta0959db52013-12-02 17:33:04 +0000299 psci_acquire_afflvl_locks(mpidr,
300 start_afflvl,
301 end_afflvl,
302 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100303
Achin Gupta0959db52013-12-02 17:33:04 +0000304 /* Perform generic, architecture and platform specific handling. */
305 rc = psci_call_on_handlers(target_cpu_nodes,
306 start_afflvl,
307 end_afflvl,
308 target_cpu,
309 entrypoint,
310 context_id);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100311
Achin Gupta4f6ad662013-10-25 09:08:21 +0100312 /*
313 * This loop releases the lock corresponding to each affinity level
Achin Gupta0959db52013-12-02 17:33:04 +0000314 * in the reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100315 */
Achin Gupta0959db52013-12-02 17:33:04 +0000316 psci_release_afflvl_locks(mpidr,
317 start_afflvl,
318 end_afflvl,
319 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100320
321 return rc;
322}
323
324/*******************************************************************************
325 * The following functions finish an earlier affinity power on request. They
326 * are called by the common finisher routine in psci_common.c.
327 ******************************************************************************/
328static unsigned int psci_afflvl0_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000329 aff_map_node *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100330{
Achin Gupta0959db52013-12-02 17:33:04 +0000331 unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100332
333 assert(cpu_node->level == MPIDR_AFFLVL0);
334
Achin Gupta0959db52013-12-02 17:33:04 +0000335 /* Ensure we have been explicitly woken up by another cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000336 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000337 assert(state == PSCI_STATE_ON_PENDING);
338
Achin Gupta4f6ad662013-10-25 09:08:21 +0100339 /*
340 * Plat. management: Perform the platform specific actions
341 * for this cpu e.g. enabling the gic or zeroing the mailbox
342 * register. The actual state of this cpu has already been
343 * changed.
344 */
345 if (psci_plat_pm_ops->affinst_on_finish) {
346
Achin Gupta0959db52013-12-02 17:33:04 +0000347 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000348 plat_state = get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100349 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
350 cpu_node->level,
351 plat_state);
352 assert(rc == PSCI_E_SUCCESS);
353 }
354
355 /*
356 * Arch. management: Turn on mmu & restore architectural state
357 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100358 enable_mmu();
359
360 /*
361 * All the platform specific actions for turning this cpu
362 * on have completed. Perform enough arch.initialization
363 * to run in the non-secure address space.
364 */
365 bl31_arch_setup();
366
367 /*
368 * Generic management: Now we just need to retrieve the
369 * information that we had stashed away during the cpu_on
Achin Gupta3140a9e2013-12-02 16:23:12 +0000370 * call to set this cpu on its way. First get the index
Achin Gupta4f6ad662013-10-25 09:08:21 +0100371 * for restoring the re-entry info
372 */
373 index = cpu_node->data;
Achin Guptac8afc782013-11-25 18:45:02 +0000374 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100375
Achin Gupta75f73672013-12-05 16:33:10 +0000376 /* State management: mark this cpu as on */
377 psci_set_state(cpu_node, PSCI_STATE_ON);
378
Achin Gupta4f6ad662013-10-25 09:08:21 +0100379 /* Clean caches before re-entering normal world */
380 dcsw_op_louis(DCCSW);
381
382 return rc;
383}
384
385static unsigned int psci_afflvl1_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000386 aff_map_node *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100387{
Achin Gupta0959db52013-12-02 17:33:04 +0000388 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100389
390 assert(cluster_node->level == MPIDR_AFFLVL1);
391
392 /*
393 * Plat. management: Perform the platform specific actions
394 * as per the old state of the cluster e.g. enabling
395 * coherency at the interconnect depends upon the state with
396 * which this cluster was powered up. If anything goes wrong
397 * then assert as there is no way to recover from this
398 * situation.
399 */
400 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000401
402 /* Get the physical state of this cluster */
Achin Gupta75f73672013-12-05 16:33:10 +0000403 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100404 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
405 cluster_node->level,
406 plat_state);
407 assert(rc == PSCI_E_SUCCESS);
408 }
409
Achin Gupta75f73672013-12-05 16:33:10 +0000410 /* State management: Increment the cluster reference count */
411 psci_set_state(cluster_node, PSCI_STATE_ON);
412
Achin Gupta4f6ad662013-10-25 09:08:21 +0100413 return rc;
414}
415
416
417static unsigned int psci_afflvl2_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000418 aff_map_node *system_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100419{
Achin Gupta0959db52013-12-02 17:33:04 +0000420 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100421
422 /* Cannot go beyond this affinity level */
423 assert(system_node->level == MPIDR_AFFLVL2);
424
425 /*
426 * Currently, there are no architectural actions to perform
427 * at the system level.
428 */
429
430 /*
431 * Plat. management: Perform the platform specific actions
432 * as per the old state of the cluster e.g. enabling
433 * coherency at the interconnect depends upon the state with
434 * which this cluster was powered up. If anything goes wrong
435 * then assert as there is no way to recover from this
436 * situation.
437 */
438 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000439
440 /* Get the physical state of the system */
Achin Gupta75f73672013-12-05 16:33:10 +0000441 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100442 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
443 system_node->level,
444 plat_state);
445 assert(rc == PSCI_E_SUCCESS);
446 }
447
Achin Gupta75f73672013-12-05 16:33:10 +0000448 /* State management: Increment the system reference count */
449 psci_set_state(system_node, PSCI_STATE_ON);
450
Achin Gupta4f6ad662013-10-25 09:08:21 +0100451 return rc;
452}
453
454const afflvl_power_on_finisher psci_afflvl_on_finishers[] = {
455 psci_afflvl0_on_finish,
456 psci_afflvl1_on_finish,
457 psci_afflvl2_on_finish,
458};
459