blob: 14f524c8fe6fcdc4657e7774189ab622c7eb64f9 [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>
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 ******************************************************************************/
Achin Gupta75f73672013-12-05 16:33:10 +000049static int cpu_on_validate_state(aff_map_node *node)
Achin Gupta4f6ad662013-10-25 09:08:21 +010050{
51 unsigned int psci_state;
52
53 /* Get the raw psci state */
Achin Gupta75f73672013-12-05 16:33:10 +000054 psci_state = psci_get_state(node);
Achin Gupta4f6ad662013-10-25 09:08:21 +010055
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 */
Achin Gupta75f73672013-12-05 16:33:10 +000087 rc = cpu_on_validate_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +010088 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
Achin Gupta75f73672013-12-05 16:33:10 +0000104 /* State management: Set this cpu's state as ON PENDING */
105 psci_set_state(cpu_node, PSCI_STATE_ON_PENDING);
106
Achin Gupta4f6ad662013-10-25 09:08:21 +0100107 /*
108 * Plat. management: Give the platform the current state
109 * of the target cpu to allow it to perform the necessary
110 * steps to power on.
111 */
112 if (psci_plat_pm_ops->affinst_on) {
113
114 /* Get the current physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000115 plat_state = psci_get_phys_state(cpu_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100116 rc = psci_plat_pm_ops->affinst_on(target_cpu,
117 psci_entrypoint,
118 ns_entrypoint,
119 cpu_node->level,
120 plat_state);
121 }
122
123 return rc;
124}
125
126/*******************************************************************************
127 * Handler routine to turn a cluster on. It takes care or any generic, arch.
128 * or platform specific setup required.
129 * TODO: Split this code across separate handlers for each type of setup?
130 ******************************************************************************/
131static int psci_afflvl1_on(unsigned long target_cpu,
132 aff_map_node *cluster_node,
133 unsigned long ns_entrypoint,
134 unsigned long context_id)
135{
136 int rc = PSCI_E_SUCCESS;
137 unsigned int plat_state;
138 unsigned long psci_entrypoint;
139
140 assert(cluster_node->level == MPIDR_AFFLVL1);
141
142 /*
143 * There is no generic and arch. specific cluster
144 * management required
145 */
146
Achin Gupta75f73672013-12-05 16:33:10 +0000147 /* State management: Is not required while turning a cluster on */
148
Achin Gupta4f6ad662013-10-25 09:08:21 +0100149 /*
150 * Plat. management: Give the platform the current state
151 * of the target cpu to allow it to perform the necessary
152 * steps to power on.
153 */
154 if (psci_plat_pm_ops->affinst_on) {
Achin Gupta75f73672013-12-05 16:33:10 +0000155 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100156 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
157 rc = psci_plat_pm_ops->affinst_on(target_cpu,
158 psci_entrypoint,
159 ns_entrypoint,
160 cluster_node->level,
161 plat_state);
162 }
163
164 return rc;
165}
166
167/*******************************************************************************
168 * Handler routine to turn a cluster of clusters on. It takes care or any
169 * generic, arch. or platform specific setup required.
170 * TODO: Split this code across separate handlers for each type of setup?
171 ******************************************************************************/
172static int psci_afflvl2_on(unsigned long target_cpu,
173 aff_map_node *system_node,
174 unsigned long ns_entrypoint,
175 unsigned long context_id)
176{
177 int rc = PSCI_E_SUCCESS;
178 unsigned int plat_state;
179 unsigned long psci_entrypoint;
180
181 /* Cannot go beyond affinity level 2 in this psci imp. */
182 assert(system_node->level == MPIDR_AFFLVL2);
183
184 /*
185 * There is no generic and arch. specific system management
186 * required
187 */
188
Achin Gupta75f73672013-12-05 16:33:10 +0000189 /* State management: Is not required while turning a system on */
190
Achin Gupta4f6ad662013-10-25 09:08:21 +0100191 /*
192 * Plat. management: Give the platform the current state
193 * of the target cpu to allow it to perform the necessary
194 * steps to power on.
195 */
196 if (psci_plat_pm_ops->affinst_on) {
Achin Gupta75f73672013-12-05 16:33:10 +0000197 plat_state = psci_get_phys_state(system_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100198 psci_entrypoint = (unsigned long) psci_aff_on_finish_entry;
199 rc = psci_plat_pm_ops->affinst_on(target_cpu,
200 psci_entrypoint,
201 ns_entrypoint,
202 system_node->level,
203 plat_state);
204 }
205
206 return rc;
207}
208
209/* Private data structure to make this handlers accessible through indexing */
210static const afflvl_on_handler psci_afflvl_on_handlers[] = {
211 psci_afflvl0_on,
212 psci_afflvl1_on,
213 psci_afflvl2_on,
214};
215
216/*******************************************************************************
Achin Gupta0959db52013-12-02 17:33:04 +0000217 * This function takes an array of pointers to affinity instance nodes in the
218 * topology tree and calls the on handler for the corresponding affinity
219 * levels
220 ******************************************************************************/
221static int psci_call_on_handlers(mpidr_aff_map_nodes target_cpu_nodes,
222 int start_afflvl,
223 int end_afflvl,
224 unsigned long target_cpu,
225 unsigned long entrypoint,
226 unsigned long context_id)
227{
228 int rc = PSCI_E_INVALID_PARAMS, level;
229 aff_map_node *node;
230
231 for (level = end_afflvl; level >= start_afflvl; level--) {
232 node = target_cpu_nodes[level];
233 if (node == NULL)
234 continue;
235
236 /*
237 * TODO: In case of an error should there be a way
238 * of undoing what we might have setup at higher
239 * affinity levels.
240 */
241 rc = psci_afflvl_on_handlers[level](target_cpu,
242 node,
243 entrypoint,
244 context_id);
245 if (rc != PSCI_E_SUCCESS)
246 break;
247 }
248
249 return rc;
250}
251
252/*******************************************************************************
253 * Generic handler which is called to physically power on a cpu identified by
254 * its mpidr. It traverses through all the affinity levels performing generic,
255 * architectural, platform setup and state management e.g. for a cpu that is
256 * to be powered on, it will ensure that enough information is stashed for it
257 * to resume execution in the non-secure security state.
258 *
259 * The state of all the relevant affinity levels is changed after calling the
260 * affinity level specific handlers as their actions would depend upon the state
261 * the affinity level is currently in.
262 *
263 * The affinity level specific handlers are called in descending order i.e. from
264 * the highest to the lowest affinity level implemented by the platform because
265 * to turn on affinity level X it is neccesary to turn on affinity level X + 1
266 * first.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100267 ******************************************************************************/
268int psci_afflvl_on(unsigned long target_cpu,
269 unsigned long entrypoint,
270 unsigned long context_id,
Achin Gupta0959db52013-12-02 17:33:04 +0000271 int start_afflvl,
272 int end_afflvl)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100273{
Achin Gupta0959db52013-12-02 17:33:04 +0000274 int rc = PSCI_E_SUCCESS;
275 mpidr_aff_map_nodes target_cpu_nodes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100276 unsigned long mpidr = read_mpidr() & MPIDR_AFFINITY_MASK;
277
278 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000279 * Collect the pointers to the nodes in the topology tree for
280 * each affinity instance in the mpidr. If this function does
281 * not return successfully then either the mpidr or the affinity
282 * levels are incorrect.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100283 */
Achin Gupta0959db52013-12-02 17:33:04 +0000284 rc = psci_get_aff_map_nodes(target_cpu,
285 start_afflvl,
286 end_afflvl,
287 target_cpu_nodes);
288 if (rc != PSCI_E_SUCCESS)
289 return rc;
290
Achin Gupta4f6ad662013-10-25 09:08:21 +0100291
292 /*
Achin Gupta0959db52013-12-02 17:33:04 +0000293 * This function acquires the lock corresponding to each affinity
294 * level so that by the time all locks are taken, the system topology
295 * is snapshot and state management can be done safely.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100296 */
Achin Gupta0959db52013-12-02 17:33:04 +0000297 psci_acquire_afflvl_locks(mpidr,
298 start_afflvl,
299 end_afflvl,
300 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100301
Achin Gupta0959db52013-12-02 17:33:04 +0000302 /* Perform generic, architecture and platform specific handling. */
303 rc = psci_call_on_handlers(target_cpu_nodes,
304 start_afflvl,
305 end_afflvl,
306 target_cpu,
307 entrypoint,
308 context_id);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100309
Achin Gupta4f6ad662013-10-25 09:08:21 +0100310 /*
311 * This loop releases the lock corresponding to each affinity level
Achin Gupta0959db52013-12-02 17:33:04 +0000312 * in the reverse order to which they were acquired.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100313 */
Achin Gupta0959db52013-12-02 17:33:04 +0000314 psci_release_afflvl_locks(mpidr,
315 start_afflvl,
316 end_afflvl,
317 target_cpu_nodes);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100318
319 return rc;
320}
321
322/*******************************************************************************
323 * The following functions finish an earlier affinity power on request. They
324 * are called by the common finisher routine in psci_common.c.
325 ******************************************************************************/
326static unsigned int psci_afflvl0_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000327 aff_map_node *cpu_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100328{
Achin Gupta0959db52013-12-02 17:33:04 +0000329 unsigned int index, plat_state, state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100330
331 assert(cpu_node->level == MPIDR_AFFLVL0);
332
Achin Gupta0959db52013-12-02 17:33:04 +0000333 /* Ensure we have been explicitly woken up by another cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000334 state = psci_get_state(cpu_node);
Achin Gupta0959db52013-12-02 17:33:04 +0000335 assert(state == PSCI_STATE_ON_PENDING);
336
Achin Gupta4f6ad662013-10-25 09:08:21 +0100337 /*
338 * Plat. management: Perform the platform specific actions
339 * for this cpu e.g. enabling the gic or zeroing the mailbox
340 * register. The actual state of this cpu has already been
341 * changed.
342 */
343 if (psci_plat_pm_ops->affinst_on_finish) {
344
Achin Gupta0959db52013-12-02 17:33:04 +0000345 /* Get the physical state of this cpu */
Achin Gupta75f73672013-12-05 16:33:10 +0000346 plat_state = get_phys_state(state);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100347 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
348 cpu_node->level,
349 plat_state);
350 assert(rc == PSCI_E_SUCCESS);
351 }
352
353 /*
354 * Arch. management: Turn on mmu & restore architectural state
355 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100356 enable_mmu();
357
358 /*
359 * All the platform specific actions for turning this cpu
360 * on have completed. Perform enough arch.initialization
361 * to run in the non-secure address space.
362 */
363 bl31_arch_setup();
364
365 /*
366 * Generic management: Now we just need to retrieve the
367 * information that we had stashed away during the cpu_on
Achin Gupta3140a9e2013-12-02 16:23:12 +0000368 * call to set this cpu on its way. First get the index
Achin Gupta4f6ad662013-10-25 09:08:21 +0100369 * for restoring the re-entry info
370 */
371 index = cpu_node->data;
Achin Guptac8afc782013-11-25 18:45:02 +0000372 psci_get_ns_entry_info(index);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100373
Achin Gupta75f73672013-12-05 16:33:10 +0000374 /* State management: mark this cpu as on */
375 psci_set_state(cpu_node, PSCI_STATE_ON);
376
Achin Gupta4f6ad662013-10-25 09:08:21 +0100377 /* Clean caches before re-entering normal world */
378 dcsw_op_louis(DCCSW);
379
380 return rc;
381}
382
383static unsigned int psci_afflvl1_on_finish(unsigned long mpidr,
Achin Gupta0959db52013-12-02 17:33:04 +0000384 aff_map_node *cluster_node)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100385{
Achin Gupta0959db52013-12-02 17:33:04 +0000386 unsigned int plat_state, rc = PSCI_E_SUCCESS;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100387
388 assert(cluster_node->level == MPIDR_AFFLVL1);
389
390 /*
391 * Plat. management: Perform the platform specific actions
392 * as per the old state of the cluster e.g. enabling
393 * coherency at the interconnect depends upon the state with
394 * which this cluster was powered up. If anything goes wrong
395 * then assert as there is no way to recover from this
396 * situation.
397 */
398 if (psci_plat_pm_ops->affinst_on_finish) {
Achin Gupta0959db52013-12-02 17:33:04 +0000399
400 /* Get the physical state of this cluster */
Achin Gupta75f73672013-12-05 16:33:10 +0000401 plat_state = psci_get_phys_state(cluster_node);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100402 rc = psci_plat_pm_ops->affinst_on_finish(mpidr,
403 cluster_node->level,
404 plat_state);
405 assert(rc == PSCI_E_SUCCESS);
406 }
407
Achin Gupta75f73672013-12-05 16:33:10 +0000408 /* State management: Increment the cluster reference count */
409 psci_set_state(cluster_node, PSCI_STATE_ON);
410
Achin Gupta4f6ad662013-10-25 09:08:21 +0100411 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 */
Achin Gupta75f73672013-12-05 16:33:10 +0000439 plat_state = psci_get_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
Achin Gupta75f73672013-12-05 16:33:10 +0000446 /* State management: Increment the system reference count */
447 psci_set_state(system_node, PSCI_STATE_ON);
448
Achin Gupta4f6ad662013-10-25 09:08:21 +0100449 return rc;
450}
451
452const afflvl_power_on_finisher psci_afflvl_on_finishers[] = {
453 psci_afflvl0_on_finish,
454 psci_afflvl1_on_finish,
455 psci_afflvl2_on_finish,
456};
457