Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 1 | /* |
Dan Handley | e83b0ca | 2014-01-14 18:17:09 +0000 | [diff] [blame] | 2 | * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 3 | * |
| 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 | |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 31 | #include <stdint.h> |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 32 | #include <arch_helpers.h> |
| 33 | #include <platform.h> |
| 34 | #include <gic.h> |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 35 | #include <debug.h> |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | /******************************************************************************* |
| 39 | * TODO: Revisit if priorities are being set such that no non-secure interrupt |
| 40 | * can have a higher priority than a secure one as recommended in the GICv2 spec |
| 41 | ******************************************************************************/ |
| 42 | |
| 43 | /******************************************************************************* |
| 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 | ******************************************************************************/ |
| 49 | void gicv3_cpuif_setup(void) |
| 50 | { |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 51 | unsigned int scr_val, val; |
| 52 | uintptr_t base; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 53 | |
| 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 Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 61 | */ |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 62 | 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 Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 70 | 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); |
| 88 | |
| 89 | /* |
| 90 | * By default EL2 and NS-EL1 software should be able to enable GICv3 |
| 91 | * System register access without any configuration at EL3. But it turns |
| 92 | * out that GICC PMR as set in GICv2 mode does not affect GICv3 mode. So |
| 93 | * we need to set it here again. In order to do that we need to enable |
| 94 | * register access. We leave it enabled as it should be fine and might |
| 95 | * prevent problems with later software trying to access GIC System |
| 96 | * Registers. |
| 97 | */ |
| 98 | val = read_icc_sre_el3(); |
| 99 | write_icc_sre_el3(val | ICC_SRE_EN | ICC_SRE_SRE); |
| 100 | |
| 101 | val = read_icc_sre_el2(); |
| 102 | write_icc_sre_el2(val | ICC_SRE_EN | ICC_SRE_SRE); |
| 103 | |
| 104 | write_icc_pmr_el1(MAX_PRI_VAL); |
| 105 | |
| 106 | /* Restore SCR_EL3 */ |
| 107 | write_scr(scr_val); |
| 108 | } |
| 109 | |
| 110 | /******************************************************************************* |
| 111 | * This function does some minimal GICv3 configuration when cores go |
| 112 | * down. |
| 113 | ******************************************************************************/ |
| 114 | void gicv3_cpuif_deactivate(void) |
| 115 | { |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 116 | unsigned int val; |
| 117 | uintptr_t base; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and |
| 121 | * wait for GICR_WAKER.ChildrenAsleep to get set. |
| 122 | * (GICv3 Architecture specification 5.4.23). |
| 123 | * GICR_WAKER is NOT banked per CPU, compute the correct base address |
| 124 | * per CPU. |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 125 | */ |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 126 | base = gicv3_get_rdist(BASE_GICR_BASE, read_mpidr()); |
| 127 | if (base == (uintptr_t)NULL) { |
| 128 | /* No re-distributor base address. This interface cannot be |
| 129 | * configured. |
| 130 | */ |
| 131 | panic(); |
| 132 | } |
| 133 | |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 134 | val = gicr_read_waker(base); |
| 135 | val |= WAKER_PS; |
| 136 | gicr_write_waker(base, val); |
| 137 | dsb(); |
| 138 | |
| 139 | /* We need to wait for ChildrenAsleep to set. */ |
| 140 | val = gicr_read_waker(base); |
| 141 | while ((val & WAKER_CA) == 0) { |
| 142 | val = gicr_read_waker(base); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /******************************************************************************* |
| 148 | * Enable secure interrupts and use FIQs to route them. Disable legacy bypass |
| 149 | * and set the priority mask register to allow all interrupts to trickle in. |
| 150 | ******************************************************************************/ |
| 151 | void gic_cpuif_setup(unsigned int gicc_base) |
| 152 | { |
| 153 | unsigned int val; |
| 154 | |
| 155 | val = gicc_read_iidr(gicc_base); |
| 156 | |
| 157 | /* |
| 158 | * If GICv3 we need to do a bit of additional setup. We want to |
| 159 | * allow default GICv2 behaviour but allow the next stage to |
| 160 | * enable full gicv3 features. |
| 161 | */ |
| 162 | if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) { |
| 163 | gicv3_cpuif_setup(); |
| 164 | } |
| 165 | |
| 166 | val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0; |
| 167 | val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1; |
| 168 | |
| 169 | gicc_write_pmr(gicc_base, MAX_PRI_VAL); |
| 170 | gicc_write_ctlr(gicc_base, val); |
| 171 | } |
| 172 | |
| 173 | /******************************************************************************* |
| 174 | * Place the cpu interface in a state where it can never make a cpu exit wfi as |
| 175 | * as result of an asserted interrupt. This is critical for powering down a cpu |
| 176 | ******************************************************************************/ |
| 177 | void gic_cpuif_deactivate(unsigned int gicc_base) |
| 178 | { |
| 179 | unsigned int val; |
| 180 | |
| 181 | /* Disable secure, non-secure interrupts and disable their bypass */ |
| 182 | val = gicc_read_ctlr(gicc_base); |
| 183 | val &= ~(ENABLE_GRP0 | ENABLE_GRP1); |
| 184 | val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0; |
| 185 | val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1; |
| 186 | gicc_write_ctlr(gicc_base, val); |
| 187 | |
| 188 | val = gicc_read_iidr(gicc_base); |
| 189 | |
| 190 | /* |
| 191 | * If GICv3 we need to do a bit of additional setup. Make sure the |
| 192 | * RDIST is put to sleep. |
| 193 | */ |
| 194 | if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) { |
| 195 | gicv3_cpuif_deactivate(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /******************************************************************************* |
| 200 | * Per cpu gic distributor setup which will be done by all cpus after a cold |
| 201 | * boot/hotplug. This marks out the secure interrupts & enables them. |
| 202 | ******************************************************************************/ |
| 203 | void gic_pcpu_distif_setup(unsigned int gicd_base) |
| 204 | { |
| 205 | gicd_write_igroupr(gicd_base, 0, ~0); |
| 206 | |
| 207 | gicd_clr_igroupr(gicd_base, IRQ_SEC_PHY_TIMER); |
| 208 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_0); |
| 209 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_1); |
| 210 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_2); |
| 211 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_3); |
| 212 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_4); |
| 213 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_5); |
| 214 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_6); |
| 215 | gicd_clr_igroupr(gicd_base, IRQ_SEC_SGI_7); |
| 216 | |
| 217 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_PHY_TIMER, MAX_PRI_VAL); |
| 218 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_0, MAX_PRI_VAL); |
| 219 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_1, MAX_PRI_VAL); |
| 220 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_2, MAX_PRI_VAL); |
| 221 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_3, MAX_PRI_VAL); |
| 222 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_4, MAX_PRI_VAL); |
| 223 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_5, MAX_PRI_VAL); |
| 224 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_6, MAX_PRI_VAL); |
| 225 | gicd_set_ipriorityr(gicd_base, IRQ_SEC_SGI_7, MAX_PRI_VAL); |
| 226 | |
| 227 | gicd_set_isenabler(gicd_base, IRQ_SEC_PHY_TIMER); |
| 228 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_0); |
| 229 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_1); |
| 230 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_2); |
| 231 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_3); |
| 232 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_4); |
| 233 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_5); |
| 234 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_6); |
| 235 | gicd_set_isenabler(gicd_base, IRQ_SEC_SGI_7); |
| 236 | } |
| 237 | |
| 238 | /******************************************************************************* |
| 239 | * Global gic distributor setup which will be done by the primary cpu after a |
| 240 | * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It |
| 241 | * then enables the secure GIC distributor interface. |
| 242 | ******************************************************************************/ |
| 243 | void gic_distif_setup(unsigned int gicd_base) |
| 244 | { |
| 245 | unsigned int ctr, num_ints, ctlr; |
| 246 | |
| 247 | /* Disable the distributor before going further */ |
| 248 | ctlr = gicd_read_ctlr(gicd_base); |
| 249 | ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1); |
| 250 | gicd_write_ctlr(gicd_base, ctlr); |
| 251 | |
| 252 | /* |
| 253 | * Mark out non-secure interrupts. Calculate number of |
| 254 | * IGROUPR registers to consider. Will be equal to the |
| 255 | * number of IT_LINES |
| 256 | */ |
| 257 | num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK; |
| 258 | num_ints++; |
| 259 | for (ctr = 0; ctr < num_ints; ctr++) |
| 260 | gicd_write_igroupr(gicd_base, ctr << IGROUPR_SHIFT, ~0); |
| 261 | |
| 262 | /* Configure secure interrupts now */ |
| 263 | gicd_clr_igroupr(gicd_base, IRQ_TZ_WDOG); |
| 264 | gicd_set_ipriorityr(gicd_base, IRQ_TZ_WDOG, MAX_PRI_VAL); |
| 265 | gicd_set_itargetsr(gicd_base, IRQ_TZ_WDOG, |
| 266 | platform_get_core_pos(read_mpidr())); |
| 267 | gicd_set_isenabler(gicd_base, IRQ_TZ_WDOG); |
| 268 | gic_pcpu_distif_setup(gicd_base); |
| 269 | |
| 270 | gicd_write_ctlr(gicd_base, ctlr | ENABLE_GRP0); |
| 271 | } |
| 272 | |
| 273 | void gic_setup(void) |
| 274 | { |
| 275 | unsigned int gicd_base, gicc_base; |
| 276 | |
| 277 | gicd_base = platform_get_cfgvar(CONFIG_GICD_ADDR); |
| 278 | gicc_base = platform_get_cfgvar(CONFIG_GICC_ADDR); |
| 279 | |
| 280 | gic_cpuif_setup(gicc_base); |
| 281 | gic_distif_setup(gicd_base); |
| 282 | } |