blob: 77a8bef93034df82fba457430bb0530c21d92136 [file] [log] [blame]
Ian Spray84687392014-01-02 16:57:12 +00001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Ian Spray84687392014-01-02 16:57:12 +00003 *
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 <arch_helpers.h>
Achin Gupta191e86e2014-05-09 10:03:15 +010032#include <assert.h>
33#include <bl_common.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010034#include <debug.h>
Dan Handley930ee2e2014-04-17 17:48:52 +010035#include <gic_v2.h>
36#include <gic_v3.h>
Achin Gupta191e86e2014-05-09 10:03:15 +010037#include <interrupt_mgmt.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010038#include <platform.h>
Dan Handley1c54d972014-06-20 12:02:01 +010039#include <plat_config.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010040#include <stdint.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010041#include "fvp_def.h"
42#include "fvp_private.h"
Ian Spray84687392014-01-02 16:57:12 +000043
Ian Spray84687392014-01-02 16:57:12 +000044/*******************************************************************************
45 * This function does some minimal GICv3 configuration. The Firmware itself does
46 * not fully support GICv3 at this time and relies on GICv2 emulation as
47 * provided by GICv3. This function allows software (like Linux) in later stages
48 * to use full GICv3 features.
49 ******************************************************************************/
50void gicv3_cpuif_setup(void)
51{
Harry Liebeleaec5902013-12-12 13:00:29 +000052 unsigned int scr_val, val;
53 uintptr_t base;
Ian Spray84687392014-01-02 16:57:12 +000054
55 /*
56 * When CPUs come out of reset they have their GICR_WAKER.ProcessorSleep
57 * bit set. In order to allow interrupts to get routed to the CPU we
58 * need to clear this bit if set and wait for GICR_WAKER.ChildrenAsleep
59 * to clear (GICv3 Architecture specification 5.4.23).
60 * GICR_WAKER is NOT banked per CPU, compute the correct base address
61 * per CPU.
Ian Spray84687392014-01-02 16:57:12 +000062 */
Harry Liebeleaec5902013-12-12 13:00:29 +000063 base = gicv3_get_rdist(BASE_GICR_BASE, read_mpidr());
64 if (base == (uintptr_t)NULL) {
65 /* No re-distributor base address. This interface cannot be
66 * configured.
67 */
68 panic();
69 }
70
Ian Spray84687392014-01-02 16:57:12 +000071 val = gicr_read_waker(base);
72
73 val &= ~WAKER_PS;
74 gicr_write_waker(base, val);
75 dsb();
76
77 /* We need to wait for ChildrenAsleep to clear. */
78 val = gicr_read_waker(base);
79 while (val & WAKER_CA) {
80 val = gicr_read_waker(base);
81 }
82
83 /*
84 * We need to set SCR_EL3.NS in order to see GICv3 non-secure state.
85 * Restore SCR_EL3.NS again before exit.
86 */
87 scr_val = read_scr();
88 write_scr(scr_val | SCR_NS_BIT);
Andrew Thoelke42e75a72014-04-28 12:28:39 +010089 isb(); /* ensure NS=1 takes effect before accessing ICC_SRE_EL2 */
Ian Spray84687392014-01-02 16:57:12 +000090
91 /*
92 * By default EL2 and NS-EL1 software should be able to enable GICv3
93 * System register access without any configuration at EL3. But it turns
94 * out that GICC PMR as set in GICv2 mode does not affect GICv3 mode. So
95 * we need to set it here again. In order to do that we need to enable
96 * register access. We leave it enabled as it should be fine and might
97 * prevent problems with later software trying to access GIC System
98 * Registers.
99 */
100 val = read_icc_sre_el3();
101 write_icc_sre_el3(val | ICC_SRE_EN | ICC_SRE_SRE);
102
103 val = read_icc_sre_el2();
104 write_icc_sre_el2(val | ICC_SRE_EN | ICC_SRE_SRE);
105
Jon Medhurstd0212c22014-02-11 14:48:56 +0000106 write_icc_pmr_el1(GIC_PRI_MASK);
Andrew Thoelke42e75a72014-04-28 12:28:39 +0100107 isb(); /* commite ICC_* changes before setting NS=0 */
Ian Spray84687392014-01-02 16:57:12 +0000108
109 /* Restore SCR_EL3 */
110 write_scr(scr_val);
Andrew Thoelke42e75a72014-04-28 12:28:39 +0100111 isb(); /* ensure NS=0 takes effect immediately */
Ian Spray84687392014-01-02 16:57:12 +0000112}
113
114/*******************************************************************************
115 * This function does some minimal GICv3 configuration when cores go
116 * down.
117 ******************************************************************************/
118void gicv3_cpuif_deactivate(void)
119{
Harry Liebeleaec5902013-12-12 13:00:29 +0000120 unsigned int val;
121 uintptr_t base;
Ian Spray84687392014-01-02 16:57:12 +0000122
123 /*
124 * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and
125 * wait for GICR_WAKER.ChildrenAsleep to get set.
126 * (GICv3 Architecture specification 5.4.23).
127 * GICR_WAKER is NOT banked per CPU, compute the correct base address
128 * per CPU.
Ian Spray84687392014-01-02 16:57:12 +0000129 */
Harry Liebeleaec5902013-12-12 13:00:29 +0000130 base = gicv3_get_rdist(BASE_GICR_BASE, read_mpidr());
131 if (base == (uintptr_t)NULL) {
132 /* No re-distributor base address. This interface cannot be
133 * configured.
134 */
135 panic();
136 }
137
Ian Spray84687392014-01-02 16:57:12 +0000138 val = gicr_read_waker(base);
139 val |= WAKER_PS;
140 gicr_write_waker(base, val);
141 dsb();
142
143 /* We need to wait for ChildrenAsleep to set. */
144 val = gicr_read_waker(base);
145 while ((val & WAKER_CA) == 0) {
146 val = gicr_read_waker(base);
147 }
148}
149
150
151/*******************************************************************************
152 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
153 * and set the priority mask register to allow all interrupts to trickle in.
154 ******************************************************************************/
155void gic_cpuif_setup(unsigned int gicc_base)
156{
157 unsigned int val;
158
159 val = gicc_read_iidr(gicc_base);
160
161 /*
162 * If GICv3 we need to do a bit of additional setup. We want to
163 * allow default GICv2 behaviour but allow the next stage to
164 * enable full gicv3 features.
165 */
166 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
167 gicv3_cpuif_setup();
168 }
169
170 val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0;
171 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
172
Jon Medhurstd0212c22014-02-11 14:48:56 +0000173 gicc_write_pmr(gicc_base, GIC_PRI_MASK);
Ian Spray84687392014-01-02 16:57:12 +0000174 gicc_write_ctlr(gicc_base, val);
175}
176
177/*******************************************************************************
178 * Place the cpu interface in a state where it can never make a cpu exit wfi as
179 * as result of an asserted interrupt. This is critical for powering down a cpu
180 ******************************************************************************/
181void gic_cpuif_deactivate(unsigned int gicc_base)
182{
183 unsigned int val;
184
185 /* Disable secure, non-secure interrupts and disable their bypass */
186 val = gicc_read_ctlr(gicc_base);
187 val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
188 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
189 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
190 gicc_write_ctlr(gicc_base, val);
191
192 val = gicc_read_iidr(gicc_base);
193
194 /*
195 * If GICv3 we need to do a bit of additional setup. Make sure the
196 * RDIST is put to sleep.
197 */
198 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
199 gicv3_cpuif_deactivate();
200 }
201}
202
203/*******************************************************************************
204 * Per cpu gic distributor setup which will be done by all cpus after a cold
205 * boot/hotplug. This marks out the secure interrupts & enables them.
206 ******************************************************************************/
207void gic_pcpu_distif_setup(unsigned int gicd_base)
208{
209 gicd_write_igroupr(gicd_base, 0, ~0);
210
211 gicd_clr_igroupr(gicd_base, IRQ_SEC_PHY_TIMER);
212 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_0);
213 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_1);
214 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_2);
215 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_3);
216 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_4);
217 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_5);
218 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_6);
219 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_7);
220
Jon Medhurstd0212c22014-02-11 14:48:56 +0000221 gicd_set_ipriorityr(gicd_base, IRQ_SEC_PHY_TIMER, GIC_HIGHEST_SEC_PRIORITY);
222 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_0, GIC_HIGHEST_SEC_PRIORITY);
223 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_1, GIC_HIGHEST_SEC_PRIORITY);
224 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_2, GIC_HIGHEST_SEC_PRIORITY);
225 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_3, GIC_HIGHEST_SEC_PRIORITY);
226 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_4, GIC_HIGHEST_SEC_PRIORITY);
227 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_5, GIC_HIGHEST_SEC_PRIORITY);
228 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_6, GIC_HIGHEST_SEC_PRIORITY);
229 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_7, GIC_HIGHEST_SEC_PRIORITY);
Ian Spray84687392014-01-02 16:57:12 +0000230
231 gicd_set_isenabler(gicd_base, IRQ_SEC_PHY_TIMER);
232 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_0);
233 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_1);
234 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_2);
235 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_3);
236 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_4);
237 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_5);
238 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_6);
239 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_7);
240}
241
242/*******************************************************************************
243 * Global gic distributor setup which will be done by the primary cpu after a
244 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
245 * then enables the secure GIC distributor interface.
246 ******************************************************************************/
247void gic_distif_setup(unsigned int gicd_base)
248{
249 unsigned int ctr, num_ints, ctlr;
250
251 /* Disable the distributor before going further */
252 ctlr = gicd_read_ctlr(gicd_base);
253 ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1);
254 gicd_write_ctlr(gicd_base, ctlr);
255
256 /*
257 * Mark out non-secure interrupts. Calculate number of
258 * IGROUPR registers to consider. Will be equal to the
259 * number of IT_LINES
260 */
261 num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK;
262 num_ints++;
263 for (ctr = 0; ctr < num_ints; ctr++)
264 gicd_write_igroupr(gicd_base, ctr << IGROUPR_SHIFT, ~0);
265
266 /* Configure secure interrupts now */
267 gicd_clr_igroupr(gicd_base, IRQ_TZ_WDOG);
Jon Medhurstd0212c22014-02-11 14:48:56 +0000268 gicd_set_ipriorityr(gicd_base, IRQ_TZ_WDOG, GIC_HIGHEST_SEC_PRIORITY);
Ian Spray84687392014-01-02 16:57:12 +0000269 gicd_set_itargetsr(gicd_base, IRQ_TZ_WDOG,
270 platform_get_core_pos(read_mpidr()));
271 gicd_set_isenabler(gicd_base, IRQ_TZ_WDOG);
272 gic_pcpu_distif_setup(gicd_base);
273
274 gicd_write_ctlr(gicd_base, ctlr | ENABLE_GRP0);
275}
276
277void gic_setup(void)
278{
Dan Handley1c54d972014-06-20 12:02:01 +0100279 gic_cpuif_setup(get_plat_config()->gicc_base);
280 gic_distif_setup(get_plat_config()->gicd_base);
Ian Spray84687392014-01-02 16:57:12 +0000281}
Achin Gupta191e86e2014-05-09 10:03:15 +0100282
283/*******************************************************************************
284 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
285 * The interrupt controller knows which pin/line it uses to signal a type of
286 * interrupt. The platform knows which interrupt controller type is being used
287 * in a particular security state e.g. with an ARM GIC, normal world could use
288 * the GICv2 features while the secure world could use GICv3 features and vice
289 * versa.
290 * This function is exported by the platform to let the interrupt management
291 * framework determine for a type of interrupt and security state, which line
292 * should be used in the SCR_EL3 to control its routing to EL3. The interrupt
293 * line is represented as the bit position of the IRQ or FIQ bit in the SCR_EL3.
294 ******************************************************************************/
295uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state)
296{
Dan Handley1c54d972014-06-20 12:02:01 +0100297 uint32_t gicc_base = get_plat_config()->gicc_base;
Achin Gupta191e86e2014-05-09 10:03:15 +0100298
299 assert(type == INTR_TYPE_S_EL1 ||
300 type == INTR_TYPE_EL3 ||
301 type == INTR_TYPE_NS);
302
303 assert(security_state == NON_SECURE || security_state == SECURE);
304
305 /*
306 * We ignore the security state parameter under the assumption that
307 * both normal and secure worlds are using ARM GICv2. This parameter
308 * will be used when the secure world starts using GICv3.
309 */
310#if FVP_GIC_ARCH == 2
311 return gicv2_interrupt_type_to_line(gicc_base, type);
312#else
313#error "Invalid GIC architecture version specified for FVP port"
314#endif
315}
316
Achin Gupta02d36282014-05-04 19:02:52 +0100317#if FVP_GIC_ARCH == 2
318/*******************************************************************************
319 * This function returns the type of the highest priority pending interrupt at
320 * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no
321 * interrupt pending.
322 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100323uint32_t plat_ic_get_pending_interrupt_type(void)
Achin Gupta02d36282014-05-04 19:02:52 +0100324{
Dan Handley1c54d972014-06-20 12:02:01 +0100325 uint32_t id;
Achin Gupta02d36282014-05-04 19:02:52 +0100326
Dan Handley1c54d972014-06-20 12:02:01 +0100327 id = gicc_read_hppir(get_plat_config()->gicc_base);
Achin Gupta02d36282014-05-04 19:02:52 +0100328
329 /* Assume that all secure interrupts are S-EL1 interrupts */
330 if (id < 1022)
331 return INTR_TYPE_S_EL1;
332
333 if (id == GIC_SPURIOUS_INTERRUPT)
334 return INTR_TYPE_INVAL;
335
336 return INTR_TYPE_NS;
337}
338
339/*******************************************************************************
340 * This function returns the id of the highest priority pending interrupt at
341 * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no
342 * interrupt pending.
343 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100344uint32_t plat_ic_get_pending_interrupt_id(void)
Achin Gupta02d36282014-05-04 19:02:52 +0100345{
346 uint32_t id, gicc_base;
347
Dan Handley1c54d972014-06-20 12:02:01 +0100348 gicc_base = get_plat_config()->gicc_base;
Achin Gupta02d36282014-05-04 19:02:52 +0100349 id = gicc_read_hppir(gicc_base);
350
351 if (id < 1022)
352 return id;
353
354 if (id == 1023)
355 return INTR_ID_UNAVAILABLE;
356
357 /*
358 * Find out which non-secure interrupt it is under the assumption that
359 * the GICC_CTLR.AckCtl bit is 0.
360 */
361 return gicc_read_ahppir(gicc_base);
362}
363
364/*******************************************************************************
365 * This functions reads the GIC cpu interface Interrupt Acknowledge register
366 * to start handling the pending interrupt. It returns the contents of the IAR.
367 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100368uint32_t plat_ic_acknowledge_interrupt(void)
Achin Gupta02d36282014-05-04 19:02:52 +0100369{
Dan Handley1c54d972014-06-20 12:02:01 +0100370 return gicc_read_IAR(get_plat_config()->gicc_base);
Achin Gupta02d36282014-05-04 19:02:52 +0100371}
372
373/*******************************************************************************
374 * This functions writes the GIC cpu interface End Of Interrupt register with
375 * the passed value to finish handling the active interrupt
376 ******************************************************************************/
Dan Handley701fea72014-05-27 16:17:21 +0100377void plat_ic_end_of_interrupt(uint32_t id)
Achin Gupta02d36282014-05-04 19:02:52 +0100378{
Dan Handley1c54d972014-06-20 12:02:01 +0100379 gicc_write_EOIR(get_plat_config()->gicc_base, id);
Achin Gupta02d36282014-05-04 19:02:52 +0100380 return;
381}
382
383/*******************************************************************************
384 * This function returns the type of the interrupt id depending upon the group
385 * this interrupt has been configured under by the interrupt controller i.e.
386 * group0 or group1.
387 ******************************************************************************/
Dan Handley701fea72014-05-27 16:17:21 +0100388uint32_t plat_ic_get_interrupt_type(uint32_t id)
Achin Gupta02d36282014-05-04 19:02:52 +0100389{
390 uint32_t group;
391
Dan Handley1c54d972014-06-20 12:02:01 +0100392 group = gicd_get_igroupr(get_plat_config()->gicd_base, id);
Achin Gupta02d36282014-05-04 19:02:52 +0100393
394 /* Assume that all secure interrupts are S-EL1 interrupts */
395 if (group == GRP0)
396 return INTR_TYPE_S_EL1;
397 else
398 return INTR_TYPE_NS;
399}
400
401#else
402#error "Invalid GIC architecture version specified for FVP port"
403#endif