blob: db3c9cf6fd304915e9bfe3be321ef9d262f175bd [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);
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{
279 unsigned int gicd_base, gicc_base;
280
281 gicd_base = platform_get_cfgvar(CONFIG_GICD_ADDR);
282 gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR);
283
284 gic_cpuif_setup(gicc_base);
285 gic_distif_setup(gicd_base);
286}