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