Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. |
| 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 | |
| 31 | #include <arch_helpers.h> |
| 32 | #include <assert.h> |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 33 | #include <arm_gic.h> |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 34 | #include <bl_common.h> |
| 35 | #include <debug.h> |
| 36 | #include <gic_v2.h> |
| 37 | #include <interrupt_mgmt.h> |
| 38 | #include <platform.h> |
| 39 | #include <stdint.h> |
| 40 | #include <tegra_private.h> |
| 41 | #include <tegra_def.h> |
| 42 | |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 43 | /* Value used to initialize Non-Secure IRQ priorities four at a time */ |
| 44 | #define GICD_IPRIORITYR_DEF_VAL \ |
| 45 | (GIC_HIGHEST_NS_PRIORITY | \ |
| 46 | (GIC_HIGHEST_NS_PRIORITY << 8) | \ |
| 47 | (GIC_HIGHEST_NS_PRIORITY << 16) | \ |
| 48 | (GIC_HIGHEST_NS_PRIORITY << 24)) |
| 49 | |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 50 | static const irq_sec_cfg_t *g_irq_sec_ptr; |
Varun Wadekar | b7b4575 | 2015-12-28 14:55:41 -0800 | [diff] [blame] | 51 | static unsigned int g_num_irqs; |
| 52 | |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 53 | /******************************************************************************* |
| 54 | * Place the cpu interface in a state where it can never make a cpu exit wfi as |
| 55 | * as result of an asserted interrupt. This is critical for powering down a cpu |
| 56 | ******************************************************************************/ |
| 57 | void tegra_gic_cpuif_deactivate(void) |
| 58 | { |
| 59 | unsigned int val; |
| 60 | |
| 61 | /* Disable secure, non-secure interrupts and disable their bypass */ |
| 62 | val = gicc_read_ctlr(TEGRA_GICC_BASE); |
| 63 | val &= ~(ENABLE_GRP0 | ENABLE_GRP1); |
| 64 | val |= FIQ_BYP_DIS_GRP1 | FIQ_BYP_DIS_GRP0; |
| 65 | val |= IRQ_BYP_DIS_GRP0 | IRQ_BYP_DIS_GRP1; |
| 66 | gicc_write_ctlr(TEGRA_GICC_BASE, val); |
| 67 | } |
| 68 | |
| 69 | /******************************************************************************* |
| 70 | * Enable secure interrupts and set the priority mask register to allow all |
| 71 | * interrupts to trickle in. |
| 72 | ******************************************************************************/ |
| 73 | static void tegra_gic_cpuif_setup(unsigned int gicc_base) |
| 74 | { |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 75 | unsigned int val; |
| 76 | |
| 77 | val = ENABLE_GRP0 | ENABLE_GRP1 | FIQ_EN | FIQ_BYP_DIS_GRP0; |
| 78 | val |= IRQ_BYP_DIS_GRP0 | FIQ_BYP_DIS_GRP1 | IRQ_BYP_DIS_GRP1; |
| 79 | |
| 80 | gicc_write_ctlr(gicc_base, val); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 81 | gicc_write_pmr(gicc_base, GIC_PRI_MASK); |
| 82 | } |
| 83 | |
| 84 | /******************************************************************************* |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 85 | * Per cpu gic distributor setup which will be done by all cpus after a cold |
| 86 | * boot/hotplug. This marks out the secure interrupts & enables them. |
| 87 | ******************************************************************************/ |
| 88 | static void tegra_gic_pcpu_distif_setup(unsigned int gicd_base) |
| 89 | { |
| 90 | unsigned int index, sec_ppi_sgi_mask = 0; |
| 91 | |
| 92 | assert(gicd_base); |
| 93 | |
| 94 | /* Setup PPI priorities doing four at a time */ |
| 95 | for (index = 0; index < 32; index += 4) { |
| 96 | gicd_write_ipriorityr(gicd_base, index, |
| 97 | GICD_IPRIORITYR_DEF_VAL); |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Invert the bitmask to create a mask for non-secure PPIs and |
| 102 | * SGIs. Program the GICD_IGROUPR0 with this bit mask. This write will |
| 103 | * update the GICR_IGROUPR0 as well in case we are running on a GICv3 |
| 104 | * system. This is critical if GICD_CTLR.ARE_NS=1. |
| 105 | */ |
| 106 | gicd_write_igroupr(gicd_base, 0, ~sec_ppi_sgi_mask); |
| 107 | } |
| 108 | |
| 109 | /******************************************************************************* |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 110 | * Global gic distributor setup which will be done by the primary cpu after a |
| 111 | * cold boot. It marks out the non secure SPIs, PPIs & SGIs and enables them. |
| 112 | * It then enables the secure GIC distributor interface. |
| 113 | ******************************************************************************/ |
| 114 | static void tegra_gic_distif_setup(unsigned int gicd_base) |
| 115 | { |
Varun Wadekar | b7b4575 | 2015-12-28 14:55:41 -0800 | [diff] [blame] | 116 | unsigned int index, num_ints, irq_num; |
| 117 | uint8_t target_cpus; |
| 118 | uint32_t val; |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Mark out non-secure interrupts. Calculate number of |
| 122 | * IGROUPR registers to consider. Will be equal to the |
| 123 | * number of IT_LINES |
| 124 | */ |
| 125 | num_ints = gicd_read_typer(gicd_base) & IT_LINES_NO_MASK; |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 126 | num_ints = (num_ints + 1) << 5; |
| 127 | for (index = MIN_SPI_ID; index < num_ints; index += 32) |
| 128 | gicd_write_igroupr(gicd_base, index, ~0); |
| 129 | |
| 130 | /* Setup SPI priorities doing four at a time */ |
| 131 | for (index = MIN_SPI_ID; index < num_ints; index += 4) { |
| 132 | gicd_write_ipriorityr(gicd_base, index, |
| 133 | GICD_IPRIORITYR_DEF_VAL); |
| 134 | } |
| 135 | |
Varun Wadekar | b7b4575 | 2015-12-28 14:55:41 -0800 | [diff] [blame] | 136 | /* Configure SPI secure interrupts now */ |
| 137 | if (g_irq_sec_ptr) { |
| 138 | |
Varun Wadekar | b7b4575 | 2015-12-28 14:55:41 -0800 | [diff] [blame] | 139 | for (index = 0; index < g_num_irqs; index++) { |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 140 | irq_num = (g_irq_sec_ptr + index)->irq; |
| 141 | target_cpus = (g_irq_sec_ptr + index)->target_cpus; |
Varun Wadekar | b7b4575 | 2015-12-28 14:55:41 -0800 | [diff] [blame] | 142 | |
| 143 | if (irq_num >= MIN_SPI_ID) { |
| 144 | |
| 145 | /* Configure as a secure interrupt */ |
| 146 | gicd_clr_igroupr(gicd_base, irq_num); |
| 147 | |
| 148 | /* Configure SPI priority */ |
| 149 | mmio_write_8(gicd_base + GICD_IPRIORITYR + |
| 150 | irq_num, |
| 151 | GIC_HIGHEST_SEC_PRIORITY & |
| 152 | GIC_PRI_MASK); |
| 153 | |
| 154 | /* Configure as level triggered */ |
| 155 | val = gicd_read_icfgr(gicd_base, irq_num); |
| 156 | val |= (3 << ((irq_num & 0xF) << 1)); |
| 157 | gicd_write_icfgr(gicd_base, irq_num, val); |
| 158 | |
| 159 | /* Route SPI to the target CPUs */ |
| 160 | gicd_set_itargetsr(gicd_base, irq_num, |
| 161 | target_cpus); |
| 162 | |
| 163 | /* Enable this interrupt */ |
| 164 | gicd_set_isenabler(gicd_base, irq_num); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 169 | /* |
| 170 | * Configure the SGI and PPI. This is done in a separated function |
| 171 | * because each CPU is responsible for initializing its own private |
| 172 | * interrupts. |
| 173 | */ |
| 174 | tegra_gic_pcpu_distif_setup(gicd_base); |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 175 | |
| 176 | /* enable distributor */ |
| 177 | gicd_write_ctlr(gicd_base, ENABLE_GRP0 | ENABLE_GRP1); |
| 178 | } |
| 179 | |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 180 | void tegra_gic_setup(const irq_sec_cfg_t *irq_sec_ptr, unsigned int num_irqs) |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 181 | { |
Varun Wadekar | b7b4575 | 2015-12-28 14:55:41 -0800 | [diff] [blame] | 182 | g_irq_sec_ptr = irq_sec_ptr; |
| 183 | g_num_irqs = num_irqs; |
| 184 | |
Varun Wadekar | b316e24 | 2015-05-19 16:48:04 +0530 | [diff] [blame] | 185 | tegra_gic_cpuif_setup(TEGRA_GICC_BASE); |
| 186 | tegra_gic_distif_setup(TEGRA_GICD_BASE); |
| 187 | } |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 188 | |
| 189 | /******************************************************************************* |
| 190 | * An ARM processor signals interrupt exceptions through the IRQ and FIQ pins. |
| 191 | * The interrupt controller knows which pin/line it uses to signal a type of |
| 192 | * interrupt. This function provides a common implementation of |
| 193 | * plat_interrupt_type_to_line() in an ARM GIC environment for optional re-use |
| 194 | * across platforms. It lets the interrupt management framework determine |
| 195 | * for a type of interrupt and security state, which line should be used in the |
| 196 | * SCR_EL3 to control its routing to EL3. The interrupt line is represented as |
| 197 | * the bit position of the IRQ or FIQ bit in the SCR_EL3. |
| 198 | ******************************************************************************/ |
| 199 | uint32_t tegra_gic_interrupt_type_to_line(uint32_t type, |
| 200 | uint32_t security_state) |
| 201 | { |
| 202 | assert(type == INTR_TYPE_S_EL1 || |
| 203 | type == INTR_TYPE_EL3 || |
| 204 | type == INTR_TYPE_NS); |
| 205 | |
| 206 | assert(sec_state_is_valid(security_state)); |
| 207 | |
| 208 | /* |
| 209 | * We ignore the security state parameter under the assumption that |
| 210 | * both normal and secure worlds are using ARM GICv2. This parameter |
| 211 | * will be used when the secure world starts using GICv3. |
| 212 | */ |
| 213 | #if ARM_GIC_ARCH == 2 |
| 214 | return gicv2_interrupt_type_to_line(TEGRA_GICC_BASE, type); |
| 215 | #else |
| 216 | #error "Invalid ARM GIC architecture version specified for platform port" |
| 217 | #endif /* ARM_GIC_ARCH */ |
| 218 | } |
| 219 | |
| 220 | #if ARM_GIC_ARCH == 2 |
| 221 | /******************************************************************************* |
| 222 | * This function returns the type of the highest priority pending interrupt at |
| 223 | * the GIC cpu interface. INTR_TYPE_INVAL is returned when there is no |
| 224 | * interrupt pending. |
| 225 | ******************************************************************************/ |
| 226 | uint32_t tegra_gic_get_pending_interrupt_type(void) |
| 227 | { |
| 228 | uint32_t id; |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 229 | unsigned int index; |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 230 | |
| 231 | id = gicc_read_hppir(TEGRA_GICC_BASE) & INT_ID_MASK; |
| 232 | |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 233 | /* get the interrupt type */ |
| 234 | if (id < 1022) { |
| 235 | for (index = 0; index < g_num_irqs; index++) { |
| 236 | if (id == (g_irq_sec_ptr + index)->irq) |
| 237 | return (g_irq_sec_ptr + index)->type; |
| 238 | } |
| 239 | } |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 240 | |
| 241 | if (id == GIC_SPURIOUS_INTERRUPT) |
| 242 | return INTR_TYPE_INVAL; |
| 243 | |
| 244 | return INTR_TYPE_NS; |
| 245 | } |
| 246 | |
| 247 | /******************************************************************************* |
| 248 | * This function returns the id of the highest priority pending interrupt at |
| 249 | * the GIC cpu interface. INTR_ID_UNAVAILABLE is returned when there is no |
| 250 | * interrupt pending. |
| 251 | ******************************************************************************/ |
| 252 | uint32_t tegra_gic_get_pending_interrupt_id(void) |
| 253 | { |
| 254 | uint32_t id; |
| 255 | |
| 256 | id = gicc_read_hppir(TEGRA_GICC_BASE) & INT_ID_MASK; |
| 257 | |
| 258 | if (id < 1022) |
| 259 | return id; |
| 260 | |
| 261 | if (id == 1023) |
| 262 | return INTR_ID_UNAVAILABLE; |
| 263 | |
| 264 | /* |
| 265 | * Find out which non-secure interrupt it is under the assumption that |
| 266 | * the GICC_CTLR.AckCtl bit is 0. |
| 267 | */ |
| 268 | return gicc_read_ahppir(TEGRA_GICC_BASE) & INT_ID_MASK; |
| 269 | } |
| 270 | |
| 271 | /******************************************************************************* |
| 272 | * This functions reads the GIC cpu interface Interrupt Acknowledge register |
| 273 | * to start handling the pending interrupt. It returns the contents of the IAR. |
| 274 | ******************************************************************************/ |
| 275 | uint32_t tegra_gic_acknowledge_interrupt(void) |
| 276 | { |
| 277 | return gicc_read_IAR(TEGRA_GICC_BASE); |
| 278 | } |
| 279 | |
| 280 | /******************************************************************************* |
| 281 | * This functions writes the GIC cpu interface End Of Interrupt register with |
| 282 | * the passed value to finish handling the active interrupt |
| 283 | ******************************************************************************/ |
| 284 | void tegra_gic_end_of_interrupt(uint32_t id) |
| 285 | { |
| 286 | gicc_write_EOIR(TEGRA_GICC_BASE, id); |
| 287 | } |
| 288 | |
| 289 | /******************************************************************************* |
| 290 | * This function returns the type of the interrupt id depending upon the group |
| 291 | * this interrupt has been configured under by the interrupt controller i.e. |
| 292 | * group0 or group1. |
| 293 | ******************************************************************************/ |
| 294 | uint32_t tegra_gic_get_interrupt_type(uint32_t id) |
| 295 | { |
| 296 | uint32_t group; |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 297 | unsigned int index; |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 298 | |
| 299 | group = gicd_get_igroupr(TEGRA_GICD_BASE, id); |
| 300 | |
Varun Wadekar | c6c386d | 2016-05-20 16:21:22 -0700 | [diff] [blame] | 301 | /* get the interrupt type */ |
| 302 | if (group == GRP0) { |
| 303 | for (index = 0; index < g_num_irqs; index++) { |
| 304 | if (id == (g_irq_sec_ptr + index)->irq) |
| 305 | return (g_irq_sec_ptr + index)->type; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return INTR_TYPE_NS; |
Varun Wadekar | d3a4150 | 2015-06-16 11:23:00 +0530 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | #else |
| 313 | #error "Invalid ARM GIC architecture version specified for platform port" |
| 314 | #endif /* ARM_GIC_ARCH */ |
| 315 | |
| 316 | uint32_t plat_ic_get_pending_interrupt_id(void) |
| 317 | { |
| 318 | return tegra_gic_get_pending_interrupt_id(); |
| 319 | } |
| 320 | |
| 321 | uint32_t plat_ic_get_pending_interrupt_type(void) |
| 322 | { |
| 323 | return tegra_gic_get_pending_interrupt_type(); |
| 324 | } |
| 325 | |
| 326 | uint32_t plat_ic_acknowledge_interrupt(void) |
| 327 | { |
| 328 | return tegra_gic_acknowledge_interrupt(); |
| 329 | } |
| 330 | |
| 331 | uint32_t plat_ic_get_interrupt_type(uint32_t id) |
| 332 | { |
| 333 | return tegra_gic_get_interrupt_type(id); |
| 334 | } |
| 335 | |
| 336 | void plat_ic_end_of_interrupt(uint32_t id) |
| 337 | { |
| 338 | tegra_gic_end_of_interrupt(id); |
| 339 | } |
| 340 | |
| 341 | uint32_t plat_interrupt_type_to_line(uint32_t type, |
| 342 | uint32_t security_state) |
| 343 | { |
| 344 | return tegra_gic_interrupt_type_to_line(type, security_state); |
| 345 | } |