blob: 8457af1ad1c04aae5c6098c4766cc1fb9cf3e4ba [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>
Dan Handley2bd4ef22014-04-09 13:14:54 +010032#include <debug.h>
Dan Handley930ee2e2014-04-17 17:48:52 +010033#include <gic_v2.h>
34#include <gic_v3.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010035#include <platform.h>
36#include <stdint.h>
Ian Spray84687392014-01-02 16:57:12 +000037
38
39/*******************************************************************************
40 * TODO: Revisit if priorities are being set such that no non-secure interrupt
41 * can have a higher priority than a secure one as recommended in the GICv2 spec
42 ******************************************************************************/
43
44/*******************************************************************************
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);
89
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);
Ian Spray84687392014-01-02 16:57:12 +0000106
107 /* Restore SCR_EL3 */
108 write_scr(scr_val);
109}
110
111/*******************************************************************************
112 * This function does some minimal GICv3 configuration when cores go
113 * down.
114 ******************************************************************************/
115void gicv3_cpuif_deactivate(void)
116{
Harry Liebeleaec5902013-12-12 13:00:29 +0000117 unsigned int val;
118 uintptr_t base;
Ian Spray84687392014-01-02 16:57:12 +0000119
120 /*
121 * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and
122 * wait for GICR_WAKER.ChildrenAsleep to get set.
123 * (GICv3 Architecture specification 5.4.23).
124 * GICR_WAKER is NOT banked per CPU, compute the correct base address
125 * per CPU.
Ian Spray84687392014-01-02 16:57:12 +0000126 */
Harry Liebeleaec5902013-12-12 13:00:29 +0000127 base = gicv3_get_rdist(BASE_GICR_BASE, read_mpidr());
128 if (base == (uintptr_t)NULL) {
129 /* No re-distributor base address. This interface cannot be
130 * configured.
131 */
132 panic();
133 }
134
Ian Spray84687392014-01-02 16:57:12 +0000135 val = gicr_read_waker(base);
136 val |= WAKER_PS;
137 gicr_write_waker(base, val);
138 dsb();
139
140 /* We need to wait for ChildrenAsleep to set. */
141 val = gicr_read_waker(base);
142 while ((val & WAKER_CA) == 0) {
143 val = gicr_read_waker(base);
144 }
145}
146
147
148/*******************************************************************************
149 * Enable secure interrupts and use FIQs to route them. Disable legacy bypass
150 * and set the priority mask register to allow all interrupts to trickle in.
151 ******************************************************************************/
152void gic_cpuif_setup(unsigned int gicc_base)
153{
154 unsigned int val;
155
156 val = gicc_read_iidr(gicc_base);
157
158 /*
159 * If GICv3 we need to do a bit of additional setup. We want to
160 * allow default GICv2 behaviour but allow the next stage to
161 * enable full gicv3 features.
162 */
163 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
164 gicv3_cpuif_setup();
165 }
166
167 val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0;
168 val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1;
169
Jon Medhurstd0212c22014-02-11 14:48:56 +0000170 gicc_write_pmr(gicc_base, GIC_PRI_MASK);
Ian Spray84687392014-01-02 16:57:12 +0000171 gicc_write_ctlr(gicc_base, val);
172}
173
174/*******************************************************************************
175 * Place the cpu interface in a state where it can never make a cpu exit wfi as
176 * as result of an asserted interrupt. This is critical for powering down a cpu
177 ******************************************************************************/
178void gic_cpuif_deactivate(unsigned int gicc_base)
179{
180 unsigned int val;
181
182 /* Disable secure, non-secure interrupts and disable their bypass */
183 val = gicc_read_ctlr(gicc_base);
184 val &= ~(ENABLE_GRP0 | ENABLE_GRP1);
185 val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0;
186 val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1;
187 gicc_write_ctlr(gicc_base, val);
188
189 val = gicc_read_iidr(gicc_base);
190
191 /*
192 * If GICv3 we need to do a bit of additional setup. Make sure the
193 * RDIST is put to sleep.
194 */
195 if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) {
196 gicv3_cpuif_deactivate();
197 }
198}
199
200/*******************************************************************************
201 * Per cpu gic distributor setup which will be done by all cpus after a cold
202 * boot/hotplug. This marks out the secure interrupts & enables them.
203 ******************************************************************************/
204void gic_pcpu_distif_setup(unsigned int gicd_base)
205{
206 gicd_write_igroupr(gicd_base, 0, ~0);
207
208 gicd_clr_igroupr(gicd_base, IRQ_SEC_PHY_TIMER);
209 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_0);
210 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_1);
211 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_2);
212 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_3);
213 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_4);
214 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_5);
215 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_6);
216 gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_7);
217
Jon Medhurstd0212c22014-02-11 14:48:56 +0000218 gicd_set_ipriorityr(gicd_base, IRQ_SEC_PHY_TIMER, GIC_HIGHEST_SEC_PRIORITY);
219 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_0, GIC_HIGHEST_SEC_PRIORITY);
220 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_1, GIC_HIGHEST_SEC_PRIORITY);
221 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_2, GIC_HIGHEST_SEC_PRIORITY);
222 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_3, GIC_HIGHEST_SEC_PRIORITY);
223 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_4, GIC_HIGHEST_SEC_PRIORITY);
224 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_5, GIC_HIGHEST_SEC_PRIORITY);
225 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_6, GIC_HIGHEST_SEC_PRIORITY);
226 gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_7, GIC_HIGHEST_SEC_PRIORITY);
Ian Spray84687392014-01-02 16:57:12 +0000227
228 gicd_set_isenabler(gicd_base, IRQ_SEC_PHY_TIMER);
229 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_0);
230 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_1);
231 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_2);
232 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_3);
233 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_4);
234 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_5);
235 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_6);
236 gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_7);
237}
238
239/*******************************************************************************
240 * Global gic distributor setup which will be done by the primary cpu after a
241 * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It
242 * then enables the secure GIC distributor interface.
243 ******************************************************************************/
244void gic_distif_setup(unsigned int gicd_base)
245{
246 unsigned int ctr, num_ints, ctlr;
247
248 /* Disable the distributor before going further */
249 ctlr = gicd_read_ctlr(gicd_base);
250 ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1);
251 gicd_write_ctlr(gicd_base, ctlr);
252
253 /*
254 * Mark out non-secure interrupts. Calculate number of
255 * IGROUPR registers to consider. Will be equal to the
256 * number of IT_LINES
257 */
258 num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK;
259 num_ints++;
260 for (ctr = 0; ctr < num_ints; ctr++)
261 gicd_write_igroupr(gicd_base, ctr << IGROUPR_SHIFT, ~0);
262
263 /* Configure secure interrupts now */
264 gicd_clr_igroupr(gicd_base, IRQ_TZ_WDOG);
Jon Medhurstd0212c22014-02-11 14:48:56 +0000265 gicd_set_ipriorityr(gicd_base, IRQ_TZ_WDOG, GIC_HIGHEST_SEC_PRIORITY);
Ian Spray84687392014-01-02 16:57:12 +0000266 gicd_set_itargetsr(gicd_base, IRQ_TZ_WDOG,
267 platform_get_core_pos(read_mpidr()));
268 gicd_set_isenabler(gicd_base, IRQ_TZ_WDOG);
269 gic_pcpu_distif_setup(gicd_base);
270
271 gicd_write_ctlr(gicd_base, ctlr | ENABLE_GRP0);
272}
273
274void gic_setup(void)
275{
276 unsigned int gicd_base, gicc_base;
277
278 gicd_base = platform_get_cfgvar(CONFIG_GICD_ADDR);
279 gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR);
280
281 gic_cpuif_setup(gicc_base);
282 gic_distif_setup(gicd_base);
283}