blob: a48b29b91769c81bb516fa8a7af3e9cf6af7c887 [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>
39#include <stdint.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010040#include "fvp_def.h"
41#include "fvp_private.h"
Ian Spray84687392014-01-02 16:57:12 +000042
Ian Spray84687392014-01-02 16:57:12 +000043/*******************************************************************************
44 * This function does some minimal GICv3 configuration. The Firmware itself does
45 * not fully support GICv3 at this time and relies on GICv2 emulation as
46 * provided by GICv3. This function allows software (like Linux) in later stages
47 * to use full GICv3 features.
48 ******************************************************************************/
49void gicv3_cpuif_setup(void)
50{
Harry Liebeleaec5902013-12-12 13:00:29 +000051 unsigned int scr_val, val;
52 uintptr_t base;
Ian Spray84687392014-01-02 16:57:12 +000053
54 /*
55 * When CPUs come out of reset they have their GICR_WAKER.ProcessorSleep
56 * bit set. In order to allow interrupts to get routed to the CPU we
57 * need to clear this bit if set and wait for GICR_WAKER.ChildrenAsleep
58 * to clear (GICv3 Architecture specification 5.4.23).
59 * GICR_WAKER is NOT banked per CPU, compute the correct base address
60 * per CPU.
Ian Spray84687392014-01-02 16:57:12 +000061 */
Harry Liebeleaec5902013-12-12 13:00:29 +000062 base = gicv3_get_rdist(BASE_GICR_BASE, read_mpidr());
63 if (base == (uintptr_t)NULL) {
64 /* No re-distributor base address. This interface cannot be
65 * configured.
66 */
67 panic();
68 }
69
Ian Spray84687392014-01-02 16:57:12 +000070 val = gicr_read_waker(base);
71
72 val &= ~WAKER_PS;
73 gicr_write_waker(base, val);
74 dsb();
75
76 /* We need to wait for ChildrenAsleep to clear. */
77 val = gicr_read_waker(base);
78 while (val & WAKER_CA) {
79 val = gicr_read_waker(base);
80 }
81
82 /*
83 * We need to set SCR_EL3.NS in order to see GICv3 non-secure state.
84 * Restore SCR_EL3.NS again before exit.
85 */
86 scr_val = read_scr();
87 write_scr(scr_val | SCR_NS_BIT);
Andrew Thoelke42e75a72014-04-28 12:28:39 +010088 isb(); /* ensure NS=1 takes effect before accessing ICC_SRE_EL2 */
Ian Spray84687392014-01-02 16:57:12 +000089
90 /*
91 * By default EL2 and NS-EL1 software should be able to enable GICv3
92 * System register access without any configuration at EL3. But it turns
93 * out that GICC PMR as set in GICv2 mode does not affect GICv3 mode. So
94 * we need to set it here again. In order to do that we need to enable
95 * register access. We leave it enabled as it should be fine and might
96 * prevent problems with later software trying to access GIC System
97 * Registers.
98 */
99 val = read_icc_sre_el3();
100 write_icc_sre_el3(val | ICC_SRE_EN | ICC_SRE_SRE);
101
102 val = read_icc_sre_el2();
103 write_icc_sre_el2(val | ICC_SRE_EN | ICC_SRE_SRE);
104
Jon Medhurstd0212c22014-02-11 14:48:56 +0000105 write_icc_pmr_el1(GIC_PRI_MASK);
Andrew Thoelke42e75a72014-04-28 12:28:39 +0100106 isb(); /* commite ICC_* changes before setting NS=0 */
Ian Spray84687392014-01-02 16:57:12 +0000107
108 /* Restore SCR_EL3 */
109 write_scr(scr_val);
Andrew Thoelke42e75a72014-04-28 12:28:39 +0100110 isb(); /* ensure NS=0 takes effect immediately */
Ian Spray84687392014-01-02 16:57:12 +0000111}
112
113/*******************************************************************************
114 * This function does some minimal GICv3 configuration when cores go
115 * down.
116 ******************************************************************************/
117void gicv3_cpuif_deactivate(void)
118{
Harry Liebeleaec5902013-12-12 13:00:29 +0000119 unsigned int val;
120 uintptr_t base;
Ian Spray84687392014-01-02 16:57:12 +0000121
122 /*
123 * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and
124 * wait for GICR_WAKER.ChildrenAsleep to get set.
125 * (GICv3 Architecture specification 5.4.23).
126 * GICR_WAKER is NOT banked per CPU, compute the correct base address
127 * per CPU.
Ian Spray84687392014-01-02 16:57:12 +0000128 */
Harry Liebeleaec5902013-12-12 13:00:29 +0000129 base = gicv3_get_rdist(BASE_GICR_BASE, read_mpidr());
130 if (base == (uintptr_t)NULL) {
131 /* No re-distributor base address. This interface cannot be
132 * configured.
133 */
134 panic();
135 }
136
Ian Spray84687392014-01-02 16:57:12 +0000137 val = gicr_read_waker(base);
138 val |= WAKER_PS;
139 gicr_write_waker(base, val);
140 dsb();
141
142 /* We need to wait for ChildrenAsleep to set. */
143 val = gicr_read_waker(base);
144 while ((val & WAKER_CA) == 0) {
145 val = gicr_read_waker(base);
146 }
147}
148
149
150/*******************************************************************************
151 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
152 * and set the priority mask register to allow all interrupts to trickle in.
153 ******************************************************************************/
154void gic_cpuif_setup(unsigned int gicc_base)
155{
156 unsigned int val;
157
158 val = gicc_read_iidr(gicc_base);
159
160 /*
161 * If GICv3 we need to do a bit of additional setup. We want to
162 * allow default GICv2 behaviour but allow the next stage to
163 * enable full gicv3 features.
164 */
165 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
166 gicv3_cpuif_setup();
167 }
168
169 val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0;
170 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
171
Jon Medhurstd0212c22014-02-11 14:48:56 +0000172 gicc_write_pmr(gicc_base, GIC_PRI_MASK);
Ian Spray84687392014-01-02 16:57:12 +0000173 gicc_write_ctlr(gicc_base, val);
174}
175
176/*******************************************************************************
177 * Place the cpu interface in a state where it can never make a cpu exit wfi as
178 * as result of an asserted interrupt. This is critical for powering down a cpu
179 ******************************************************************************/
180void gic_cpuif_deactivate(unsigned int gicc_base)
181{
182 unsigned int val;
183
184 /* Disable secure, non-secure interrupts and disable their bypass */
185 val = gicc_read_ctlr(gicc_base);
186 val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
187 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
188 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
189 gicc_write_ctlr(gicc_base, val);
190
191 val = gicc_read_iidr(gicc_base);
192
193 /*
194 * If GICv3 we need to do a bit of additional setup. Make sure the
195 * RDIST is put to sleep.
196 */
197 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
198 gicv3_cpuif_deactivate();
199 }
200}
201
202/*******************************************************************************
203 * Per cpu gic distributor setup which will be done by all cpus after a cold
204 * boot/hotplug. This marks out the secure interrupts & enables them.
205 ******************************************************************************/
206void gic_pcpu_distif_setup(unsigned int gicd_base)
207{
208 gicd_write_igroupr(gicd_base, 0, ~0);
209
210 gicd_clr_igroupr(gicd_base, IRQ_SEC_PHY_TIMER);
211 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_0);
212 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_1);
213 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_2);
214 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_3);
215 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_4);
216 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_5);
217 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_6);
218 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_7);
219
Jon Medhurstd0212c22014-02-11 14:48:56 +0000220 gicd_set_ipriorityr(gicd_base, IRQ_SEC_PHY_TIMER, GIC_HIGHEST_SEC_PRIORITY);
221 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_0, GIC_HIGHEST_SEC_PRIORITY);
222 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_1, GIC_HIGHEST_SEC_PRIORITY);
223 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_2, GIC_HIGHEST_SEC_PRIORITY);
224 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_3, GIC_HIGHEST_SEC_PRIORITY);
225 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_4, GIC_HIGHEST_SEC_PRIORITY);
226 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_5, GIC_HIGHEST_SEC_PRIORITY);
227 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_6, GIC_HIGHEST_SEC_PRIORITY);
228 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_7, GIC_HIGHEST_SEC_PRIORITY);
Ian Spray84687392014-01-02 16:57:12 +0000229
230 gicd_set_isenabler(gicd_base, IRQ_SEC_PHY_TIMER);
231 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_0);
232 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_1);
233 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_2);
234 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_3);
235 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_4);
236 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_5);
237 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_6);
238 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_7);
239}
240
241/*******************************************************************************
242 * Global gic distributor setup which will be done by the primary cpu after a
243 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
244 * then enables the secure GIC distributor interface.
245 ******************************************************************************/
246void gic_distif_setup(unsigned int gicd_base)
247{
248 unsigned int ctr, num_ints, ctlr;
249
250 /* Disable the distributor before going further */
251 ctlr = gicd_read_ctlr(gicd_base);
252 ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1);
253 gicd_write_ctlr(gicd_base, ctlr);
254
255 /*
256 * Mark out non-secure interrupts. Calculate number of
257 * IGROUPR registers to consider. Will be equal to the
258 * number of IT_LINES
259 */
260 num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK;
261 num_ints++;
262 for (ctr = 0; ctr < num_ints; ctr++)
263 gicd_write_igroupr(gicd_base, ctr << IGROUPR_SHIFT, ~0);
264
265 /* Configure secure interrupts now */
266 gicd_clr_igroupr(gicd_base, IRQ_TZ_WDOG);
Jon Medhurstd0212c22014-02-11 14:48:56 +0000267 gicd_set_ipriorityr(gicd_base, IRQ_TZ_WDOG, GIC_HIGHEST_SEC_PRIORITY);
Ian Spray84687392014-01-02 16:57:12 +0000268 gicd_set_itargetsr(gicd_base, IRQ_TZ_WDOG,
269 platform_get_core_pos(read_mpidr()));
270 gicd_set_isenabler(gicd_base, IRQ_TZ_WDOG);
271 gic_pcpu_distif_setup(gicd_base);
272
273 gicd_write_ctlr(gicd_base, ctlr | ENABLE_GRP0);
274}
275
276void gic_setup(void)
277{
278 unsigned int gicd_base, gicc_base;
279
Dan Handleyea451572014-05-15 14:53:30 +0100280 gicd_base = fvp_get_cfgvar(CONFIG_GICD_ADDR);
281 gicc_base = fvp_get_cfgvar(CONFIG_GICC_ADDR);
Ian Spray84687392014-01-02 16:57:12 +0000282
283 gic_cpuif_setup(gicc_base);
284 gic_distif_setup(gicd_base);
285}
Achin Gupta191e86e2014-05-09 10:03:15 +0100286
287/*******************************************************************************
288 * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins.
289 * The interrupt controller knows which pin/line it uses to signal a type of
290 * interrupt. The platform knows which interrupt controller type is being used
291 * in a particular security state e.g. with an ARM GIC, normal world could use
292 * the GICv2 features while the secure world could use GICv3 features and vice
293 * versa.
294 * This function is exported by the platform to let the interrupt management
295 * framework determine for a type of interrupt and security state, which line
296 * should be used in the SCR_EL3 to control its routing to EL3. The interrupt
297 * line is represented as the bit position of the IRQ or FIQ bit in the SCR_EL3.
298 ******************************************************************************/
299uint32_t plat_interrupt_type_to_line(uint32_t type, uint32_t security_state)
300{
Dan Handleyea451572014-05-15 14:53:30 +0100301 uint32_t gicc_base = fvp_get_cfgvar(CONFIG_GICC_ADDR);
Achin Gupta191e86e2014-05-09 10:03:15 +0100302
303 assert(type == INTR_TYPE_S_EL1 ||
304 type == INTR_TYPE_EL3 ||
305 type == INTR_TYPE_NS);
306
307 assert(security_state == NON_SECURE || security_state == SECURE);
308
309 /*
310 * We ignore the security state parameter under the assumption that
311 * both normal and secure worlds are using ARM GICv2. This parameter
312 * will be used when the secure world starts using GICv3.
313 */
314#if FVP_GIC_ARCH == 2
315 return gicv2_interrupt_type_to_line(gicc_base, type);
316#else
317#error "Invalid GIC architecture version specified for FVP port"
318#endif
319}
320
Achin Gupta02d36282014-05-04 19:02:52 +0100321#if FVP_GIC_ARCH == 2
322/*******************************************************************************
323 * This function returns the type of the highest priority pending interrupt at
324 * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no
325 * interrupt pending.
326 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100327uint32_t plat_ic_get_pending_interrupt_type(void)
Achin Gupta02d36282014-05-04 19:02:52 +0100328{
329 uint32_t id, gicc_base;
330
Dan Handleyea451572014-05-15 14:53:30 +0100331 gicc_base = fvp_get_cfgvar(CONFIG_GICC_ADDR);
Achin Gupta02d36282014-05-04 19:02:52 +0100332 id = gicc_read_hppir(gicc_base);
333
334 /* Assume that all secure interrupts are S-EL1 interrupts */
335 if (id < 1022)
336 return INTR_TYPE_S_EL1;
337
338 if (id == GIC_SPURIOUS_INTERRUPT)
339 return INTR_TYPE_INVAL;
340
341 return INTR_TYPE_NS;
342}
343
344/*******************************************************************************
345 * This function returns the id of the highest priority pending interrupt at
346 * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no
347 * interrupt pending.
348 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100349uint32_t plat_ic_get_pending_interrupt_id(void)
Achin Gupta02d36282014-05-04 19:02:52 +0100350{
351 uint32_t id, gicc_base;
352
Dan Handleyea451572014-05-15 14:53:30 +0100353 gicc_base = fvp_get_cfgvar(CONFIG_GICC_ADDR);
Achin Gupta02d36282014-05-04 19:02:52 +0100354 id = gicc_read_hppir(gicc_base);
355
356 if (id < 1022)
357 return id;
358
359 if (id == 1023)
360 return INTR_ID_UNAVAILABLE;
361
362 /*
363 * Find out which non-secure interrupt it is under the assumption that
364 * the GICC_CTLR.AckCtl bit is 0.
365 */
366 return gicc_read_ahppir(gicc_base);
367}
368
369/*******************************************************************************
370 * This functions reads the GIC cpu interface Interrupt Acknowledge register
371 * to start handling the pending interrupt. It returns the contents of the IAR.
372 ******************************************************************************/
Juan Castillo2d552402014-06-13 17:05:10 +0100373uint32_t plat_ic_acknowledge_interrupt(void)
Achin Gupta02d36282014-05-04 19:02:52 +0100374{
Dan Handleyea451572014-05-15 14:53:30 +0100375 return gicc_read_IAR(fvp_get_cfgvar(CONFIG_GICC_ADDR));
Achin Gupta02d36282014-05-04 19:02:52 +0100376}
377
378/*******************************************************************************
379 * This functions writes the GIC cpu interface End Of Interrupt register with
380 * the passed value to finish handling the active interrupt
381 ******************************************************************************/
Dan Handley701fea72014-05-27 16:17:21 +0100382void plat_ic_end_of_interrupt(uint32_t id)
Achin Gupta02d36282014-05-04 19:02:52 +0100383{
Dan Handleyea451572014-05-15 14:53:30 +0100384 gicc_write_EOIR(fvp_get_cfgvar(CONFIG_GICC_ADDR), id);
Achin Gupta02d36282014-05-04 19:02:52 +0100385 return;
386}
387
388/*******************************************************************************
389 * This function returns the type of the interrupt id depending upon the group
390 * this interrupt has been configured under by the interrupt controller i.e.
391 * group0 or group1.
392 ******************************************************************************/
Dan Handley701fea72014-05-27 16:17:21 +0100393uint32_t plat_ic_get_interrupt_type(uint32_t id)
Achin Gupta02d36282014-05-04 19:02:52 +0100394{
395 uint32_t group;
396
Dan Handleyea451572014-05-15 14:53:30 +0100397 group = gicd_get_igroupr(fvp_get_cfgvar(CONFIG_GICD_ADDR), id);
Achin Gupta02d36282014-05-04 19:02:52 +0100398
399 /* Assume that all secure interrupts are S-EL1 interrupts */
400 if (group == GRP0)
401 return INTR_TYPE_S_EL1;
402 else
403 return INTR_TYPE_NS;
404}
405
406#else
407#error "Invalid GIC architecture version specified for FVP port"
408#endif