blob: f796718e5f7424af02e66ad9cb626528e5e9e91c [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
Achin Gupta4f6ad662013-10-25 09:08:21 +010031#include <arch_helpers.h>
Dan Handley97043ac2014-04-09 13:14:54 +010032#include <assert.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010033#include <bakery_lock.h>
34#include <cci400.h>
Dan Handley97043ac2014-04-09 13:14:54 +010035#include <mmio.h>
36#include <platform.h>
Dan Handley6f3b1952014-06-20 12:02:01 +010037#include <plat_config.h>
Dan Handley5f0cdb02014-05-14 17:44:19 +010038#include <platform_def.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010039#include <psci.h>
Dan Handleye8246c02014-04-11 11:52:12 +010040#include "drivers/pwrc/fvp_pwrc.h"
Dan Handley5f0cdb02014-05-14 17:44:19 +010041#include "fvp_def.h"
42#include "fvp_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010043
44/*******************************************************************************
Vikram Kanigirid118f9f2014-03-21 11:57:10 +000045 * FVP handler called when an affinity instance is about to enter standby.
46 ******************************************************************************/
47int fvp_affinst_standby(unsigned int power_state)
48{
49 unsigned int target_afflvl;
50
51 /* Sanity check the requested state */
52 target_afflvl = psci_get_pstate_afflvl(power_state);
53
54 /*
55 * It's possible to enter standby only on affinity level 0 i.e. a cpu
56 * on the FVP. Ignore any other affinity level.
57 */
58 if (target_afflvl != MPIDR_AFFLVL0)
59 return PSCI_E_INVALID_PARAMS;
60
Andrew Thoelke8cec5982014-04-28 12:28:39 +010061 /*
62 * Enter standby state
63 * dsb is good practice before using wfi to enter low power states
64 */
65 dsb();
Vikram Kanigirid118f9f2014-03-21 11:57:10 +000066 wfi();
67
68 return PSCI_E_SUCCESS;
69}
70
71/*******************************************************************************
Achin Gupta4f6ad662013-10-25 09:08:21 +010072 * FVP handler called when an affinity instance is about to be turned on. The
73 * level and mpidr determine the affinity instance.
74 ******************************************************************************/
75int fvp_affinst_on(unsigned long mpidr,
76 unsigned long sec_entrypoint,
77 unsigned long ns_entrypoint,
78 unsigned int afflvl,
79 unsigned int state)
80{
81 int rc = PSCI_E_SUCCESS;
82 unsigned long linear_id;
Dan Handleyfb037bf2014-04-10 15:37:22 +010083 mailbox_t *fvp_mboxes;
Achin Gupta4f6ad662013-10-25 09:08:21 +010084 unsigned int psysr;
85
Achin Gupta4f6ad662013-10-25 09:08:21 +010086 /*
87 * It's possible to turn on only affinity level 0 i.e. a cpu
88 * on the FVP. Ignore any other affinity level.
89 */
90 if (afflvl != MPIDR_AFFLVL0)
91 goto exit;
92
93 /*
94 * Ensure that we do not cancel an inflight power off request
95 * for the target cpu. That would leave it in a zombie wfi.
96 * Wait for it to power off, program the jump address for the
97 * target cpu and then program the power controller to turn
98 * that cpu on
99 */
100 do {
101 psysr = fvp_pwrc_read_psysr(mpidr);
102 } while (psysr & PSYSR_AFF_L0);
103
104 linear_id = platform_get_core_pos(mpidr);
Dan Handleyfb037bf2014-04-10 15:37:22 +0100105 fvp_mboxes = (mailbox_t *) (TZDRAM_BASE + MBOX_OFF);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100106 fvp_mboxes[linear_id].value = sec_entrypoint;
107 flush_dcache_range((unsigned long) &fvp_mboxes[linear_id],
108 sizeof(unsigned long));
109
110 fvp_pwrc_write_pponr(mpidr);
111
112exit:
113 return rc;
114}
115
116/*******************************************************************************
117 * FVP handler called when an affinity instance is about to be turned off. The
118 * level and mpidr determine the affinity instance. The 'state' arg. allows the
119 * platform to decide whether the cluster is being turned off and take apt
120 * actions.
121 *
122 * CAUTION: This function is called with coherent stacks so that caches can be
123 * turned off, flushed and coherency disabled. There is no guarantee that caches
124 * will remain turned on across calls to this function as each affinity level is
125 * dealt with. So do not write & read global variables across calls. It will be
126 * wise to do flush a write to the global to prevent unpredictable results.
127 ******************************************************************************/
128int fvp_affinst_off(unsigned long mpidr,
129 unsigned int afflvl,
130 unsigned int state)
131{
132 int rc = PSCI_E_SUCCESS;
133 unsigned int gicc_base, ectlr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100134
135 switch (afflvl) {
136 case MPIDR_AFFLVL1:
137 if (state == PSCI_STATE_OFF) {
138 /*
139 * Disable coherency if this cluster is to be
140 * turned off
141 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100142 if (get_plat_config()->flags & CONFIG_HAS_CCI)
Harry Liebel30affd52013-10-30 17:41:48 +0000143 cci_disable_coherency(mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100144
145 /*
146 * Program the power controller to turn the
147 * cluster off
148 */
149 fvp_pwrc_write_pcoffr(mpidr);
150
151 }
152 break;
153
154 case MPIDR_AFFLVL0:
155 if (state == PSCI_STATE_OFF) {
156
157 /*
158 * Take this cpu out of intra-cluster coherency if
159 * the FVP flavour supports the SMP bit.
160 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100161 if (get_plat_config()->flags & CONFIG_CPUECTLR_SMP_BIT) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100162 ectlr = read_cpuectlr();
163 ectlr &= ~CPUECTLR_SMP_BIT;
164 write_cpuectlr(ectlr);
165 }
166
167 /*
168 * Prevent interrupts from spuriously waking up
169 * this cpu
170 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100171 gicc_base = get_plat_config()->gicc_base;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100172 gic_cpuif_deactivate(gicc_base);
173
174 /*
175 * Program the power controller to power this
176 * cpu off
177 */
178 fvp_pwrc_write_ppoffr(mpidr);
179 }
180 break;
181
182 default:
183 assert(0);
184 }
185
186 return rc;
187}
188
189/*******************************************************************************
190 * FVP handler called when an affinity instance is about to be suspended. The
191 * level and mpidr determine the affinity instance. The 'state' arg. allows the
192 * platform to decide whether the cluster is being turned off and take apt
193 * actions.
194 *
195 * CAUTION: This function is called with coherent stacks so that caches can be
196 * turned off, flushed and coherency disabled. There is no guarantee that caches
197 * will remain turned on across calls to this function as each affinity level is
198 * dealt with. So do not write & read global variables across calls. It will be
199 * wise to do flush a write to the global to prevent unpredictable results.
200 ******************************************************************************/
201int fvp_affinst_suspend(unsigned long mpidr,
202 unsigned long sec_entrypoint,
203 unsigned long ns_entrypoint,
204 unsigned int afflvl,
205 unsigned int state)
206{
207 int rc = PSCI_E_SUCCESS;
208 unsigned int gicc_base, ectlr;
Dan Handley6f3b1952014-06-20 12:02:01 +0100209 unsigned long linear_id;
Dan Handleyfb037bf2014-04-10 15:37:22 +0100210 mailbox_t *fvp_mboxes;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100211
Achin Gupta4f6ad662013-10-25 09:08:21 +0100212 switch (afflvl) {
213 case MPIDR_AFFLVL1:
214 if (state == PSCI_STATE_OFF) {
215 /*
216 * Disable coherency if this cluster is to be
217 * turned off
218 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100219 if (get_plat_config()->flags & CONFIG_HAS_CCI)
Harry Liebel30affd52013-10-30 17:41:48 +0000220 cci_disable_coherency(mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100221
222 /*
223 * Program the power controller to turn the
224 * cluster off
225 */
226 fvp_pwrc_write_pcoffr(mpidr);
227
228 }
229 break;
230
231 case MPIDR_AFFLVL0:
232 if (state == PSCI_STATE_OFF) {
233 /*
234 * Take this cpu out of intra-cluster coherency if
235 * the FVP flavour supports the SMP bit.
236 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100237 if (get_plat_config()->flags & CONFIG_CPUECTLR_SMP_BIT) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100238 ectlr = read_cpuectlr();
239 ectlr &= ~CPUECTLR_SMP_BIT;
240 write_cpuectlr(ectlr);
241 }
242
243 /* Program the jump address for the target cpu */
244 linear_id = platform_get_core_pos(mpidr);
Dan Handleyfb037bf2014-04-10 15:37:22 +0100245 fvp_mboxes = (mailbox_t *) (TZDRAM_BASE + MBOX_OFF);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100246 fvp_mboxes[linear_id].value = sec_entrypoint;
247 flush_dcache_range((unsigned long) &fvp_mboxes[linear_id],
248 sizeof(unsigned long));
249
250 /*
251 * Prevent interrupts from spuriously waking up
252 * this cpu
253 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100254 gicc_base = get_plat_config()->gicc_base;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100255 gic_cpuif_deactivate(gicc_base);
256
257 /*
258 * Program the power controller to power this
259 * cpu off and enable wakeup interrupts.
260 */
Achin Guptab127cdb2013-11-12 16:40:00 +0000261 fvp_pwrc_set_wen(mpidr);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100262 fvp_pwrc_write_ppoffr(mpidr);
263 }
264 break;
265
266 default:
267 assert(0);
268 }
269
Achin Gupta4f6ad662013-10-25 09:08:21 +0100270 return rc;
271}
272
273/*******************************************************************************
274 * FVP handler called when an affinity instance has just been powered on after
275 * being turned off earlier. The level and mpidr determine the affinity
276 * instance. The 'state' arg. allows the platform to decide whether the cluster
277 * was turned off prior to wakeup and do what's necessary to setup it up
278 * correctly.
279 ******************************************************************************/
280int fvp_affinst_on_finish(unsigned long mpidr,
281 unsigned int afflvl,
282 unsigned int state)
283{
284 int rc = PSCI_E_SUCCESS;
Dan Handley6f3b1952014-06-20 12:02:01 +0100285 unsigned long linear_id;
Dan Handleyfb037bf2014-04-10 15:37:22 +0100286 mailbox_t *fvp_mboxes;
Soby Mathewb1e71b22014-06-06 10:18:52 +0100287 unsigned int gicd_base, gicc_base, ectlr;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100288
289 switch (afflvl) {
290
291 case MPIDR_AFFLVL1:
292 /* Enable coherency if this cluster was off */
Harry Liebel30affd52013-10-30 17:41:48 +0000293 if (state == PSCI_STATE_OFF) {
Achin Guptab2187ab2014-01-17 16:52:29 +0000294
295 /*
296 * This CPU might have woken up whilst the
297 * cluster was attempting to power down. In
298 * this case the FVP power controller will
299 * have a pending cluster power off request
300 * which needs to be cleared by writing to the
301 * PPONR register. This prevents the power
302 * controller from interpreting a subsequent
303 * entry of this cpu into a simple wfi as a
304 * power down request.
305 */
306 fvp_pwrc_write_pponr(mpidr);
307
Vikram Kanigiridbad1ba2014-04-24 11:02:16 +0100308 fvp_cci_setup();
Harry Liebel30affd52013-10-30 17:41:48 +0000309 }
Achin Gupta4f6ad662013-10-25 09:08:21 +0100310 break;
311
312 case MPIDR_AFFLVL0:
313 /*
314 * Ignore the state passed for a cpu. It could only have
315 * been off if we are here.
316 */
317
318 /*
319 * Turn on intra-cluster coherency if the FVP flavour supports
320 * it.
321 */
Dan Handley6f3b1952014-06-20 12:02:01 +0100322 if (get_plat_config()->flags & CONFIG_CPUECTLR_SMP_BIT) {
Achin Gupta4f6ad662013-10-25 09:08:21 +0100323 ectlr = read_cpuectlr();
324 ectlr |= CPUECTLR_SMP_BIT;
325 write_cpuectlr(ectlr);
326 }
327
Achin Guptab127cdb2013-11-12 16:40:00 +0000328 /*
329 * Clear PWKUPR.WEN bit to ensure interrupts do not interfere
330 * with a cpu power down unless the bit is set again
331 */
332 fvp_pwrc_clr_wen(mpidr);
333
Achin Gupta4f6ad662013-10-25 09:08:21 +0100334 /* Zero the jump address in the mailbox for this cpu */
Dan Handleyfb037bf2014-04-10 15:37:22 +0100335 fvp_mboxes = (mailbox_t *) (TZDRAM_BASE + MBOX_OFF);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100336 linear_id = platform_get_core_pos(mpidr);
337 fvp_mboxes[linear_id].value = 0;
338 flush_dcache_range((unsigned long) &fvp_mboxes[linear_id],
339 sizeof(unsigned long));
340
Achin Gupta4f6ad662013-10-25 09:08:21 +0100341 /* Enable the gic cpu interface */
Dan Handley6f3b1952014-06-20 12:02:01 +0100342 gicc_base = get_plat_config()->gicc_base;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100343 gic_cpuif_setup(gicc_base);
344
345 /* TODO: This setup is needed only after a cold boot */
Dan Handley6f3b1952014-06-20 12:02:01 +0100346 gicd_base = get_plat_config()->gicd_base;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100347 gic_pcpu_distif_setup(gicd_base);
348
Achin Gupta4f6ad662013-10-25 09:08:21 +0100349 break;
350
351 default:
352 assert(0);
353 }
354
355 return rc;
356}
357
358/*******************************************************************************
359 * FVP handler called when an affinity instance has just been powered on after
360 * having been suspended earlier. The level and mpidr determine the affinity
361 * instance.
362 * TODO: At the moment we reuse the on finisher and reinitialize the secure
363 * context. Need to implement a separate suspend finisher.
364 ******************************************************************************/
365int fvp_affinst_suspend_finish(unsigned long mpidr,
366 unsigned int afflvl,
367 unsigned int state)
368{
369 return fvp_affinst_on_finish(mpidr, afflvl, state);
370}
371
372
373/*******************************************************************************
374 * Export the platform handlers to enable psci to invoke them
375 ******************************************************************************/
Dan Handley625de1d2014-04-23 13:47:06 +0100376static const plat_pm_ops_t fvp_plat_pm_ops = {
Vikram Kanigirid118f9f2014-03-21 11:57:10 +0000377 fvp_affinst_standby,
Achin Gupta4f6ad662013-10-25 09:08:21 +0100378 fvp_affinst_on,
379 fvp_affinst_off,
380 fvp_affinst_suspend,
381 fvp_affinst_on_finish,
382 fvp_affinst_suspend_finish,
383};
384
385/*******************************************************************************
386 * Export the platform specific power ops & initialize the fvp power controller
387 ******************************************************************************/
Dan Handley625de1d2014-04-23 13:47:06 +0100388int platform_setup_pm(const plat_pm_ops_t **plat_ops)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100389{
390 *plat_ops = &fvp_plat_pm_ops;
391 return 0;
392}