Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 1 | /* |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 2 | * Copyright (c) 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 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 32 | #include <arch_helpers.h> |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 33 | #include <arm_gic.h> |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 34 | #include <assert.h> |
| 35 | #include <bl_common.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 36 | #include <debug.h> |
Dan Handley | 930ee2e | 2014-04-17 17:48:52 +0100 | [diff] [blame] | 37 | #include <gic_v2.h> |
| 38 | #include <gic_v3.h> |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 39 | #include <interrupt_mgmt.h> |
Dan Handley | 2bd4ef2 | 2014-04-09 13:14:54 +0100 | [diff] [blame] | 40 | #include <platform.h> |
| 41 | #include <stdint.h> |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 42 | |
| 43 | |
| 44 | static unsigned int g_gicc_base; |
| 45 | static unsigned int g_gicd_base; |
| 46 | static unsigned long g_gicr_base; |
| 47 | static const unsigned int *g_irq_sec_ptr; |
| 48 | static unsigned int g_num_irqs; |
| 49 | |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 50 | |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 51 | /******************************************************************************* |
| 52 | * This function does some minimal GICv3 configuration. The Firmware itself does |
| 53 | * not fully support GICv3 at this time and relies on GICv2 emulation as |
| 54 | * provided by GICv3. This function allows software (like Linux) in later stages |
| 55 | * to use full GICv3 features. |
| 56 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 57 | static void gicv3_cpuif_setup(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 58 | { |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 59 | unsigned int scr_val, val; |
| 60 | uintptr_t base; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * When CPUs come out of reset they have their GICR_WAKER.ProcessorSleep |
| 64 | * bit set. In order to allow interrupts to get routed to the CPU we |
| 65 | * need to clear this bit if set and wait for GICR_WAKER.ChildrenAsleep |
| 66 | * to clear (GICv3 Architecture specification 5.4.23). |
| 67 | * GICR_WAKER is NOT banked per CPU, compute the correct base address |
| 68 | * per CPU. |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 69 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 70 | assert(g_gicr_base); |
| 71 | base = gicv3_get_rdist(g_gicr_base, read_mpidr()); |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 72 | if (base == (uintptr_t)NULL) { |
| 73 | /* No re-distributor base address. This interface cannot be |
| 74 | * configured. |
| 75 | */ |
| 76 | panic(); |
| 77 | } |
| 78 | |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 79 | val = gicr_read_waker(base); |
| 80 | |
| 81 | val &= ~WAKER_PS; |
| 82 | gicr_write_waker(base, val); |
| 83 | dsb(); |
| 84 | |
| 85 | /* We need to wait for ChildrenAsleep to clear. */ |
| 86 | val = gicr_read_waker(base); |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 87 | while (val & WAKER_CA) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 88 | val = gicr_read_waker(base); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * We need to set SCR_EL3.NS in order to see GICv3 non-secure state. |
| 92 | * Restore SCR_EL3.NS again before exit. |
| 93 | */ |
| 94 | scr_val = read_scr(); |
| 95 | write_scr(scr_val | SCR_NS_BIT); |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 96 | isb(); /* ensure NS=1 takes effect before accessing ICC_SRE_EL2 */ |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * By default EL2 and NS-EL1 software should be able to enable GICv3 |
| 100 | * System register access without any configuration at EL3. But it turns |
| 101 | * out that GICC PMR as set in GICv2 mode does not affect GICv3 mode. So |
| 102 | * we need to set it here again. In order to do that we need to enable |
| 103 | * register access. We leave it enabled as it should be fine and might |
| 104 | * prevent problems with later software trying to access GIC System |
| 105 | * Registers. |
| 106 | */ |
| 107 | val = read_icc_sre_el3(); |
| 108 | write_icc_sre_el3(val | ICC_SRE_EN | ICC_SRE_SRE); |
| 109 | |
| 110 | val = read_icc_sre_el2(); |
| 111 | write_icc_sre_el2(val | ICC_SRE_EN | ICC_SRE_SRE); |
| 112 | |
Jon Medhurst | d0212c2 | 2014-02-11 14:48:56 +0000 | [diff] [blame] | 113 | write_icc_pmr_el1(GIC_PRI_MASK); |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 114 | isb(); /* commit ICC_* changes before setting NS=0 */ |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 115 | |
| 116 | /* Restore SCR_EL3 */ |
| 117 | write_scr(scr_val); |
Andrew Thoelke | 42e75a7 | 2014-04-28 12:28:39 +0100 | [diff] [blame] | 118 | isb(); /* ensure NS=0 takes effect immediately */ |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /******************************************************************************* |
| 122 | * This function does some minimal GICv3 configuration when cores go |
| 123 | * down. |
| 124 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 125 | static void gicv3_cpuif_deactivate(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 126 | { |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 127 | unsigned int val; |
| 128 | uintptr_t base; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * When taking CPUs down we need to set GICR_WAKER.ProcessorSleep and |
| 132 | * wait for GICR_WAKER.ChildrenAsleep to get set. |
| 133 | * (GICv3 Architecture specification 5.4.23). |
| 134 | * GICR_WAKER is NOT banked per CPU, compute the correct base address |
| 135 | * per CPU. |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 136 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 137 | assert(g_gicr_base); |
| 138 | base = gicv3_get_rdist(g_gicr_base, read_mpidr()); |
Harry Liebel | eaec590 | 2013-12-12 13:00:29 +0000 | [diff] [blame] | 139 | if (base == (uintptr_t)NULL) { |
| 140 | /* No re-distributor base address. This interface cannot be |
| 141 | * configured. |
| 142 | */ |
| 143 | panic(); |
| 144 | } |
| 145 | |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 146 | val = gicr_read_waker(base); |
| 147 | val |= WAKER_PS; |
| 148 | gicr_write_waker(base, val); |
| 149 | dsb(); |
| 150 | |
| 151 | /* We need to wait for ChildrenAsleep to set. */ |
| 152 | val = gicr_read_waker(base); |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 153 | while ((val & WAKER_CA) == 0) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 154 | val = gicr_read_waker(base); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | |
| 158 | /******************************************************************************* |
| 159 | * Enable secure interrupts and use FIQs to route them. Disable legacy bypass |
| 160 | * and set the priority mask register to allow all interrupts to trickle in. |
| 161 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 162 | void arm_gic_cpuif_setup(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 163 | { |
| 164 | unsigned int val; |
| 165 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 166 | assert(g_gicc_base); |
| 167 | val = gicc_read_iidr(g_gicc_base); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * If GICv3 we need to do a bit of additional setup. We want to |
| 171 | * allow default GICv2 behaviour but allow the next stage to |
| 172 | * enable full gicv3 features. |
| 173 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 174 | if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 175 | gicv3_cpuif_setup(); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 176 | |
| 177 | val = ENABLE_GRP0 | FIQ_EN | FIQ_BYP_DIS_GRP0; |
| 178 | val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1; |
| 179 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 180 | gicc_write_pmr(g_gicc_base, GIC_PRI_MASK); |
| 181 | gicc_write_ctlr(g_gicc_base, val); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /******************************************************************************* |
| 185 | * Place the cpu interface in a state where it can never make a cpu exit wfi as |
| 186 | * as result of an asserted interrupt. This is critical for powering down a cpu |
| 187 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 188 | void arm_gic_cpuif_deactivate(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 189 | { |
| 190 | unsigned int val; |
| 191 | |
| 192 | /* Disable secure, non-secure interrupts and disable their bypass */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 193 | assert(g_gicc_base); |
| 194 | val = gicc_read_ctlr(g_gicc_base); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 195 | val &= ~(ENABLE_GRP0 | ENABLE_GRP1); |
| 196 | val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0; |
| 197 | val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1; |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 198 | gicc_write_ctlr(g_gicc_base, val); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 199 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 200 | val = gicc_read_iidr(g_gicc_base); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * If GICv3 we need to do a bit of additional setup. Make sure the |
| 204 | * RDIST is put to sleep. |
| 205 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 206 | if (((val >> GICC_IIDR_ARCH_SHIFT) & GICC_IIDR_ARCH_MASK) >= 3) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 207 | gicv3_cpuif_deactivate(); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /******************************************************************************* |
| 211 | * Per cpu gic distributor setup which will be done by all cpus after a cold |
| 212 | * boot/hotplug. This marks out the secure interrupts & enables them. |
| 213 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 214 | void arm_gic_pcpu_distif_setup(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 215 | { |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 216 | unsigned int index, irq_num; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 217 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 218 | assert(g_gicd_base); |
| 219 | gicd_write_igroupr(g_gicd_base, 0, ~0); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 220 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 221 | assert(g_irq_sec_ptr); |
| 222 | for (index = 0; index < g_num_irqs; index++) { |
| 223 | irq_num = g_irq_sec_ptr[index]; |
| 224 | if (irq_num < MIN_SPI_ID) { |
| 225 | /* We have an SGI or a PPI */ |
| 226 | gicd_clr_igroupr(g_gicd_base, irq_num); |
| 227 | gicd_set_ipriorityr(g_gicd_base, irq_num, |
| 228 | GIC_HIGHEST_SEC_PRIORITY); |
| 229 | gicd_set_isenabler(g_gicd_base, irq_num); |
| 230 | } |
| 231 | } |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /******************************************************************************* |
| 235 | * Global gic distributor setup which will be done by the primary cpu after a |
| 236 | * cold boot. It marks out the secure SPIs, PPIs & SGIs and enables them. It |
| 237 | * then enables the secure GIC distributor interface. |
| 238 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 239 | static void arm_gic_distif_setup(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 240 | { |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 241 | unsigned int num_ints, ctlr, index, irq_num; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 242 | |
| 243 | /* Disable the distributor before going further */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 244 | assert(g_gicd_base); |
| 245 | ctlr = gicd_read_ctlr(g_gicd_base); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 246 | ctlr &= ~(ENABLE_GRP0 | ENABLE_GRP1); |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 247 | gicd_write_ctlr(g_gicd_base, ctlr); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * Mark out non-secure interrupts. Calculate number of |
| 251 | * IGROUPR registers to consider. Will be equal to the |
| 252 | * number of IT_LINES |
| 253 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 254 | num_ints = gicd_read_typer(g_gicd_base) & IT_LINES_NO_MASK; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 255 | num_ints++; |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 256 | for (index = 0; index < num_ints; index++) |
| 257 | gicd_write_igroupr(g_gicd_base, index << IGROUPR_SHIFT, ~0); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 258 | |
| 259 | /* Configure secure interrupts now */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 260 | assert(g_irq_sec_ptr); |
| 261 | for (index = 0; index < g_num_irqs; index++) { |
| 262 | irq_num = g_irq_sec_ptr[index]; |
| 263 | if (irq_num >= MIN_SPI_ID) { |
| 264 | /* We have an SPI */ |
| 265 | gicd_clr_igroupr(g_gicd_base, irq_num); |
| 266 | gicd_set_ipriorityr(g_gicd_base, irq_num, |
| 267 | GIC_HIGHEST_SEC_PRIORITY); |
| 268 | gicd_set_itargetsr(g_gicd_base, irq_num, |
| 269 | platform_get_core_pos(read_mpidr())); |
| 270 | gicd_set_isenabler(g_gicd_base, irq_num); |
| 271 | } |
| 272 | } |
| 273 | arm_gic_pcpu_distif_setup(); |
| 274 | |
| 275 | gicd_write_ctlr(g_gicd_base, ctlr | ENABLE_GRP0); |
| 276 | } |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 277 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 278 | /******************************************************************************* |
| 279 | * Initialize the ARM GIC driver with the provided platform inputs |
| 280 | ******************************************************************************/ |
| 281 | void arm_gic_init(unsigned int gicc_base, |
| 282 | unsigned int gicd_base, |
| 283 | unsigned long gicr_base, |
| 284 | const unsigned int *irq_sec_ptr, |
| 285 | unsigned int num_irqs |
| 286 | ) |
| 287 | { |
| 288 | assert(gicc_base); |
| 289 | assert(gicd_base); |
| 290 | assert(gicr_base); |
| 291 | assert(irq_sec_ptr); |
| 292 | g_gicc_base = gicc_base; |
| 293 | g_gicd_base = gicd_base; |
| 294 | g_gicr_base = gicr_base; |
| 295 | g_irq_sec_ptr = irq_sec_ptr; |
| 296 | g_num_irqs = num_irqs; |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 299 | /******************************************************************************* |
| 300 | * Setup the ARM GIC CPU and distributor interfaces. |
| 301 | ******************************************************************************/ |
| 302 | void arm_gic_setup(void) |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 303 | { |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 304 | arm_gic_cpuif_setup(); |
| 305 | arm_gic_distif_setup(); |
Ian Spray | 8468739 | 2014-01-02 16:57:12 +0000 | [diff] [blame] | 306 | } |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 307 | |
| 308 | /******************************************************************************* |
| 309 | * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. |
| 310 | * The interrupt controller knows which pin/line it uses to signal a type of |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 311 | * interrupt. This function provides a common implementation of |
| 312 | * plat_interrupt_type_to_line() in an ARM GIC environment for optional re-use |
| 313 | * across platforms. It lets the interrupt management framework determine |
| 314 | * for a type of interrupt and security state, which line should be used in the |
| 315 | * SCR_EL3 to control its routing to EL3. The interrupt line is represented as |
| 316 | * the bit position of the IRQ or FIQ bit in the SCR_EL3. |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 317 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 318 | uint32_t arm_gic_interrupt_type_to_line(uint32_t type, |
| 319 | uint32_t security_state) |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 320 | { |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 321 | assert(type == INTR_TYPE_S_EL1 || |
| 322 | type == INTR_TYPE_EL3 || |
| 323 | type == INTR_TYPE_NS); |
| 324 | |
| 325 | assert(security_state == NON_SECURE || security_state == SECURE); |
| 326 | |
| 327 | /* |
| 328 | * We ignore the security state parameter under the assumption that |
| 329 | * both normal and secure worlds are using ARM GICv2. This parameter |
| 330 | * will be used when the secure world starts using GICv3. |
| 331 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 332 | #if ARM_GIC_ARCH == 2 |
| 333 | return gicv2_interrupt_type_to_line(g_gicc_base, type); |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 334 | #else |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 335 | #error "Invalid ARM GIC architecture version specified for platform port" |
| 336 | #endif /* ARM_GIC_ARCH */ |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 339 | #if ARM_GIC_ARCH == 2 |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 340 | /******************************************************************************* |
| 341 | * This function returns the type of the highest priority pending interrupt at |
| 342 | * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no |
| 343 | * interrupt pending. |
| 344 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 345 | uint32_t arm_gic_get_pending_interrupt_type(void) |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 346 | { |
Dan Handley | 1c54d97 | 2014-06-20 12:02:01 +0100 | [diff] [blame] | 347 | uint32_t id; |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 348 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 349 | assert(g_gicc_base); |
| 350 | id = gicc_read_hppir(g_gicc_base); |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 351 | |
| 352 | /* Assume that all secure interrupts are S-EL1 interrupts */ |
| 353 | if (id < 1022) |
| 354 | return INTR_TYPE_S_EL1; |
| 355 | |
| 356 | if (id == GIC_SPURIOUS_INTERRUPT) |
| 357 | return INTR_TYPE_INVAL; |
| 358 | |
| 359 | return INTR_TYPE_NS; |
| 360 | } |
| 361 | |
| 362 | /******************************************************************************* |
| 363 | * This function returns the id of the highest priority pending interrupt at |
| 364 | * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no |
| 365 | * interrupt pending. |
| 366 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 367 | uint32_t arm_gic_get_pending_interrupt_id(void) |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 368 | { |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 369 | uint32_t id; |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 370 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 371 | assert(g_gicc_base); |
| 372 | id = gicc_read_hppir(g_gicc_base); |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 373 | |
| 374 | if (id < 1022) |
| 375 | return id; |
| 376 | |
| 377 | if (id == 1023) |
| 378 | return INTR_ID_UNAVAILABLE; |
| 379 | |
| 380 | /* |
| 381 | * Find out which non-secure interrupt it is under the assumption that |
| 382 | * the GICC_CTLR.AckCtl bit is 0. |
| 383 | */ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 384 | return gicc_read_ahppir(g_gicc_base); |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /******************************************************************************* |
| 388 | * This functions reads the GIC cpu interface Interrupt Acknowledge register |
| 389 | * to start handling the pending interrupt. It returns the contents of the IAR. |
| 390 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 391 | uint32_t arm_gic_acknowledge_interrupt(void) |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 392 | { |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 393 | assert(g_gicc_base); |
| 394 | return gicc_read_IAR(g_gicc_base); |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /******************************************************************************* |
| 398 | * This functions writes the GIC cpu interface End Of Interrupt register with |
| 399 | * the passed value to finish handling the active interrupt |
| 400 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 401 | void arm_gic_end_of_interrupt(uint32_t id) |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 402 | { |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 403 | assert(g_gicc_base); |
| 404 | gicc_write_EOIR(g_gicc_base, id); |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | /******************************************************************************* |
| 408 | * This function returns the type of the interrupt id depending upon the group |
| 409 | * this interrupt has been configured under by the interrupt controller i.e. |
| 410 | * group0 or group1. |
| 411 | ******************************************************************************/ |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 412 | uint32_t arm_gic_get_interrupt_type(uint32_t id) |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 413 | { |
| 414 | uint32_t group; |
| 415 | |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 416 | assert(g_gicd_base); |
| 417 | group = gicd_get_igroupr(g_gicd_base, id); |
Achin Gupta | 02d3628 | 2014-05-04 19:02:52 +0100 | [diff] [blame] | 418 | |
| 419 | /* Assume that all secure interrupts are S-EL1 interrupts */ |
| 420 | if (group == GRP0) |
| 421 | return INTR_TYPE_S_EL1; |
| 422 | else |
| 423 | return INTR_TYPE_NS; |
| 424 | } |
| 425 | |
| 426 | #else |
Dan Handley | fb42b12 | 2014-06-20 09:43:15 +0100 | [diff] [blame] | 427 | #error "Invalid ARM GIC architecture version specified for platform port" |
| 428 | #endif /* ARM_GIC_ARCH */ |