Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [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. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [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 | |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 31 | #include <arch.h> |
Jon Medhurst | d0212c2 | 2014-02-11 14:48:56 +0000 | [diff] [blame] | 32 | #include <assert.h> |
Dan Handley | 930ee2e | 2014-04-17 17:48:52 +0100 | [diff] [blame] | 33 | #include <gic_v2.h> |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 34 | #include <interrupt_mgmt.h> |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 35 | #include <mmio.h> |
| 36 | |
| 37 | /******************************************************************************* |
Sandrine Bailleux | 27866d8 | 2013-10-25 15:33:39 +0100 | [diff] [blame] | 38 | * GIC Distributor interface accessors for reading entire registers |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 39 | ******************************************************************************/ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 40 | |
| 41 | unsigned int gicd_read_igroupr(unsigned int base, unsigned int id) |
| 42 | { |
| 43 | unsigned n = id >> IGROUPR_SHIFT; |
| 44 | return mmio_read_32(base + GICD_IGROUPR + (n << 2)); |
| 45 | } |
| 46 | |
| 47 | unsigned int gicd_read_isenabler(unsigned int base, unsigned int id) |
| 48 | { |
| 49 | unsigned n = id >> ISENABLER_SHIFT; |
| 50 | return mmio_read_32(base + GICD_ISENABLER + (n << 2)); |
| 51 | } |
| 52 | |
| 53 | unsigned int gicd_read_icenabler(unsigned int base, unsigned int id) |
| 54 | { |
| 55 | unsigned n = id >> ICENABLER_SHIFT; |
| 56 | return mmio_read_32(base + GICD_ICENABLER + (n << 2)); |
| 57 | } |
| 58 | |
| 59 | unsigned int gicd_read_ispendr(unsigned int base, unsigned int id) |
| 60 | { |
| 61 | unsigned n = id >> ISPENDR_SHIFT; |
| 62 | return mmio_read_32(base + GICD_ISPENDR + (n << 2)); |
| 63 | } |
| 64 | |
| 65 | unsigned int gicd_read_icpendr(unsigned int base, unsigned int id) |
| 66 | { |
| 67 | unsigned n = id >> ICPENDR_SHIFT; |
| 68 | return mmio_read_32(base + GICD_ICPENDR + (n << 2)); |
| 69 | } |
| 70 | |
| 71 | unsigned int gicd_read_isactiver(unsigned int base, unsigned int id) |
| 72 | { |
| 73 | unsigned n = id >> ISACTIVER_SHIFT; |
| 74 | return mmio_read_32(base + GICD_ISACTIVER + (n << 2)); |
| 75 | } |
| 76 | |
| 77 | unsigned int gicd_read_icactiver(unsigned int base, unsigned int id) |
| 78 | { |
| 79 | unsigned n = id >> ICACTIVER_SHIFT; |
| 80 | return mmio_read_32(base + GICD_ICACTIVER + (n << 2)); |
| 81 | } |
| 82 | |
| 83 | unsigned int gicd_read_ipriorityr(unsigned int base, unsigned int id) |
| 84 | { |
| 85 | unsigned n = id >> IPRIORITYR_SHIFT; |
| 86 | return mmio_read_32(base + GICD_IPRIORITYR + (n << 2)); |
| 87 | } |
| 88 | |
| 89 | unsigned int gicd_read_itargetsr(unsigned int base, unsigned int id) |
| 90 | { |
| 91 | unsigned n = id >> ITARGETSR_SHIFT; |
| 92 | return mmio_read_32(base + GICD_ITARGETSR + (n << 2)); |
| 93 | } |
| 94 | |
| 95 | unsigned int gicd_read_icfgr(unsigned int base, unsigned int id) |
| 96 | { |
| 97 | unsigned n = id >> ICFGR_SHIFT; |
| 98 | return mmio_read_32(base + GICD_ICFGR + (n << 2)); |
| 99 | } |
| 100 | |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 101 | unsigned int gicd_read_cpendsgir(unsigned int base, unsigned int id) |
| 102 | { |
| 103 | unsigned n = id >> CPENDSGIR_SHIFT; |
| 104 | return mmio_read_32(base + GICD_CPENDSGIR + (n << 2)); |
| 105 | } |
| 106 | |
| 107 | unsigned int gicd_read_spendsgir(unsigned int base, unsigned int id) |
| 108 | { |
| 109 | unsigned n = id >> SPENDSGIR_SHIFT; |
| 110 | return mmio_read_32(base + GICD_SPENDSGIR + (n << 2)); |
| 111 | } |
| 112 | |
| 113 | /******************************************************************************* |
Sandrine Bailleux | 27866d8 | 2013-10-25 15:33:39 +0100 | [diff] [blame] | 114 | * GIC Distributor interface accessors for writing entire registers |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 115 | ******************************************************************************/ |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 116 | |
| 117 | void gicd_write_igroupr(unsigned int base, unsigned int id, unsigned int val) |
| 118 | { |
| 119 | unsigned n = id >> IGROUPR_SHIFT; |
| 120 | mmio_write_32(base + GICD_IGROUPR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void gicd_write_isenabler(unsigned int base, unsigned int id, unsigned int val) |
| 124 | { |
| 125 | unsigned n = id >> ISENABLER_SHIFT; |
| 126 | mmio_write_32(base + GICD_ISENABLER + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void gicd_write_icenabler(unsigned int base, unsigned int id, unsigned int val) |
| 130 | { |
| 131 | unsigned n = id >> ICENABLER_SHIFT; |
| 132 | mmio_write_32(base + GICD_ICENABLER + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void gicd_write_ispendr(unsigned int base, unsigned int id, unsigned int val) |
| 136 | { |
| 137 | unsigned n = id >> ISPENDR_SHIFT; |
| 138 | mmio_write_32(base + GICD_ISPENDR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void gicd_write_icpendr(unsigned int base, unsigned int id, unsigned int val) |
| 142 | { |
| 143 | unsigned n = id >> ICPENDR_SHIFT; |
| 144 | mmio_write_32(base + GICD_ICPENDR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void gicd_write_isactiver(unsigned int base, unsigned int id, unsigned int val) |
| 148 | { |
| 149 | unsigned n = id >> ISACTIVER_SHIFT; |
| 150 | mmio_write_32(base + GICD_ISACTIVER + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void gicd_write_icactiver(unsigned int base, unsigned int id, unsigned int val) |
| 154 | { |
| 155 | unsigned n = id >> ICACTIVER_SHIFT; |
| 156 | mmio_write_32(base + GICD_ICACTIVER + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void gicd_write_ipriorityr(unsigned int base, unsigned int id, unsigned int val) |
| 160 | { |
| 161 | unsigned n = id >> IPRIORITYR_SHIFT; |
| 162 | mmio_write_32(base + GICD_IPRIORITYR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void gicd_write_itargetsr(unsigned int base, unsigned int id, unsigned int val) |
| 166 | { |
| 167 | unsigned n = id >> ITARGETSR_SHIFT; |
| 168 | mmio_write_32(base + GICD_ITARGETSR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void gicd_write_icfgr(unsigned int base, unsigned int id, unsigned int val) |
| 172 | { |
| 173 | unsigned n = id >> ICFGR_SHIFT; |
| 174 | mmio_write_32(base + GICD_ICFGR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void gicd_write_cpendsgir(unsigned int base, unsigned int id, unsigned int val) |
| 178 | { |
| 179 | unsigned n = id >> CPENDSGIR_SHIFT; |
| 180 | mmio_write_32(base + GICD_CPENDSGIR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void gicd_write_spendsgir(unsigned int base, unsigned int id, unsigned int val) |
| 184 | { |
| 185 | unsigned n = id >> SPENDSGIR_SHIFT; |
| 186 | mmio_write_32(base + GICD_SPENDSGIR + (n << 2), val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /******************************************************************************* |
Sandrine Bailleux | 27866d8 | 2013-10-25 15:33:39 +0100 | [diff] [blame] | 190 | * GIC Distributor interface accessors for individual interrupt manipulation |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 191 | ******************************************************************************/ |
| 192 | unsigned int gicd_get_igroupr(unsigned int base, unsigned int id) |
| 193 | { |
| 194 | unsigned bit_num = id & ((1 << IGROUPR_SHIFT) - 1); |
| 195 | unsigned int reg_val = gicd_read_igroupr(base, id); |
| 196 | |
| 197 | return (reg_val >> bit_num) & 0x1; |
| 198 | } |
| 199 | |
| 200 | void gicd_set_igroupr(unsigned int base, unsigned int id) |
| 201 | { |
| 202 | unsigned bit_num = id & ((1 << IGROUPR_SHIFT) - 1); |
| 203 | unsigned int reg_val = gicd_read_igroupr(base, id); |
| 204 | |
| 205 | gicd_write_igroupr(base, id, reg_val | (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void gicd_clr_igroupr(unsigned int base, unsigned int id) |
| 209 | { |
| 210 | unsigned bit_num = id & ((1 << IGROUPR_SHIFT) - 1); |
| 211 | unsigned int reg_val = gicd_read_igroupr(base, id); |
| 212 | |
| 213 | gicd_write_igroupr(base, id, reg_val & ~(1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void gicd_set_isenabler(unsigned int base, unsigned int id) |
| 217 | { |
| 218 | unsigned bit_num = id & ((1 << ISENABLER_SHIFT) - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 219 | |
Juan Castillo | 969bdb2 | 2014-04-28 12:48:40 +0100 | [diff] [blame] | 220 | gicd_write_isenabler(base, id, (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void gicd_set_icenabler(unsigned int base, unsigned int id) |
| 224 | { |
| 225 | unsigned bit_num = id & ((1 << ICENABLER_SHIFT) - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 226 | |
Juan Castillo | 969bdb2 | 2014-04-28 12:48:40 +0100 | [diff] [blame] | 227 | gicd_write_icenabler(base, id, (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void gicd_set_ispendr(unsigned int base, unsigned int id) |
| 231 | { |
| 232 | unsigned bit_num = id & ((1 << ISPENDR_SHIFT) - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 233 | |
Juan Castillo | 969bdb2 | 2014-04-28 12:48:40 +0100 | [diff] [blame] | 234 | gicd_write_ispendr(base, id, (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void gicd_set_icpendr(unsigned int base, unsigned int id) |
| 238 | { |
| 239 | unsigned bit_num = id & ((1 << ICPENDR_SHIFT) - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 240 | |
Juan Castillo | 969bdb2 | 2014-04-28 12:48:40 +0100 | [diff] [blame] | 241 | gicd_write_icpendr(base, id, (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void gicd_set_isactiver(unsigned int base, unsigned int id) |
| 245 | { |
| 246 | unsigned bit_num = id & ((1 << ISACTIVER_SHIFT) - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 247 | |
Juan Castillo | 969bdb2 | 2014-04-28 12:48:40 +0100 | [diff] [blame] | 248 | gicd_write_isactiver(base, id, (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void gicd_set_icactiver(unsigned int base, unsigned int id) |
| 252 | { |
| 253 | unsigned bit_num = id & ((1 << ICACTIVER_SHIFT) - 1); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 254 | |
Juan Castillo | 969bdb2 | 2014-04-28 12:48:40 +0100 | [diff] [blame] | 255 | gicd_write_icactiver(base, id, (1 << bit_num)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Make sure that the interrupt's group is set before expecting |
| 260 | * this function to do its job correctly. |
| 261 | */ |
| 262 | void gicd_set_ipriorityr(unsigned int base, unsigned int id, unsigned int pri) |
| 263 | { |
Jon Medhurst | d0212c2 | 2014-02-11 14:48:56 +0000 | [diff] [blame] | 264 | unsigned int reg = base + GICD_IPRIORITYR + (id & ~3); |
| 265 | unsigned int shift = (id & 3) << 3; |
| 266 | unsigned int reg_val = mmio_read_32(reg); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Enforce ARM recommendation to manage priority values such |
| 270 | * that group1 interrupts always have a lower priority than |
Jon Medhurst | d0212c2 | 2014-02-11 14:48:56 +0000 | [diff] [blame] | 271 | * group0 interrupts. |
| 272 | * Note, lower numerical values are higher priorities so the comparison |
| 273 | * checks below are reversed from what might be expected. |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 274 | */ |
Jon Medhurst | d0212c2 | 2014-02-11 14:48:56 +0000 | [diff] [blame] | 275 | assert(gicd_get_igroupr(base, id) == GRP1 ? |
| 276 | pri >= GIC_HIGHEST_NS_PRIORITY && |
| 277 | pri <= GIC_LOWEST_NS_PRIORITY : |
| 278 | pri >= GIC_HIGHEST_SEC_PRIORITY && |
| 279 | pri <= GIC_LOWEST_SEC_PRIORITY); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 280 | |
Jon Medhurst | d0212c2 | 2014-02-11 14:48:56 +0000 | [diff] [blame] | 281 | reg_val &= ~(GIC_PRI_MASK << shift); |
| 282 | reg_val |= (pri & GIC_PRI_MASK) << shift; |
| 283 | mmio_write_32(reg, reg_val); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void gicd_set_itargetsr(unsigned int base, unsigned int id, unsigned int iface) |
| 287 | { |
| 288 | unsigned byte_off = id & ((1 << ITARGETSR_SHIFT) - 1); |
| 289 | unsigned int reg_val = gicd_read_itargetsr(base, id); |
| 290 | |
| 291 | gicd_write_itargetsr(base, id, reg_val | |
| 292 | (1 << iface) << (byte_off << 3)); |
Achin Gupta | 4f6ad66 | 2013-10-25 09:08:21 +0100 | [diff] [blame] | 293 | } |
| 294 | |
Achin Gupta | 191e86e | 2014-05-09 10:03:15 +0100 | [diff] [blame] | 295 | /******************************************************************************* |
| 296 | * This function allows the interrupt management framework to determine (through |
| 297 | * the platform) which interrupt line (IRQ/FIQ) to use for an interrupt type to |
| 298 | * route it to EL3. The interrupt line is represented as the bit position of the |
| 299 | * IRQ or FIQ bit in the SCR_EL3. |
| 300 | ******************************************************************************/ |
| 301 | uint32_t gicv2_interrupt_type_to_line(uint32_t cpuif_base, uint32_t type) |
| 302 | { |
| 303 | uint32_t gicc_ctlr; |
| 304 | |
| 305 | /* Non-secure interrupts are signalled on the IRQ line always */ |
| 306 | if (type == INTR_TYPE_NS) |
| 307 | return __builtin_ctz(SCR_IRQ_BIT); |
| 308 | |
| 309 | /* |
| 310 | * Secure interrupts are signalled using the IRQ line if the FIQ_EN |
| 311 | * bit is not set else they are signalled using the FIQ line. |
| 312 | */ |
| 313 | gicc_ctlr = gicc_read_ctlr(cpuif_base); |
| 314 | if (gicc_ctlr & FIQ_EN) |
| 315 | return __builtin_ctz(SCR_FIQ_BIT); |
| 316 | else |
| 317 | return __builtin_ctz(SCR_IRQ_BIT); |
| 318 | } |