Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved. |
| 3 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Julien Grall | 68628bb | 2016-04-11 16:32:55 +0100 | [diff] [blame] | 18 | #define pr_fmt(fmt) "GICv3: " fmt |
| 19 | |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 20 | #include <linux/acpi.h> |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Sudeep Holla | 3708d52 | 2014-08-26 16:03:35 +0100 | [diff] [blame] | 22 | #include <linux/cpu_pm.h> |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/interrupt.h> |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 25 | #include <linux/irqdomain.h> |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 26 | #include <linux/of.h> |
| 27 | #include <linux/of_address.h> |
| 28 | #include <linux/of_irq.h> |
| 29 | #include <linux/percpu.h> |
| 30 | #include <linux/slab.h> |
| 31 | |
Joel Porquet | 41a83e06 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 32 | #include <linux/irqchip.h> |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 33 | #include <linux/irqchip/arm-gic-common.h> |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 34 | #include <linux/irqchip/arm-gic-v3.h> |
| 35 | |
| 36 | #include <asm/cputype.h> |
| 37 | #include <asm/exception.h> |
| 38 | #include <asm/smp_plat.h> |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 39 | #include <asm/virt.h> |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 40 | |
| 41 | #include "irq-gic-common.h" |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 42 | |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 43 | struct redist_region { |
| 44 | void __iomem *redist_base; |
| 45 | phys_addr_t phys_base; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 46 | bool single_redist; |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 49 | struct gic_chip_data { |
| 50 | void __iomem *dist_base; |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 51 | struct redist_region *redist_regions; |
| 52 | struct rdists rdists; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 53 | struct irq_domain *domain; |
| 54 | u64 redist_stride; |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 55 | u32 nr_redist_regions; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 56 | unsigned int irq_nr; |
| 57 | }; |
| 58 | |
| 59 | static struct gic_chip_data gic_data __read_mostly; |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 60 | static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 61 | |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 62 | static struct gic_kvm_info gic_v3_kvm_info; |
| 63 | |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 64 | #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist)) |
| 65 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 66 | #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K) |
| 67 | |
| 68 | /* Our default, arbitrary priority value. Linux only uses one anyway. */ |
| 69 | #define DEFAULT_PMR_VALUE 0xf0 |
| 70 | |
| 71 | static inline unsigned int gic_irq(struct irq_data *d) |
| 72 | { |
| 73 | return d->hwirq; |
| 74 | } |
| 75 | |
| 76 | static inline int gic_irq_in_rdist(struct irq_data *d) |
| 77 | { |
| 78 | return gic_irq(d) < 32; |
| 79 | } |
| 80 | |
| 81 | static inline void __iomem *gic_dist_base(struct irq_data *d) |
| 82 | { |
| 83 | if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */ |
| 84 | return gic_data_rdist_sgi_base(); |
| 85 | |
| 86 | if (d->hwirq <= 1023) /* SPI -> dist_base */ |
| 87 | return gic_data.dist_base; |
| 88 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | static void gic_do_wait_for_rwp(void __iomem *base) |
| 93 | { |
| 94 | u32 count = 1000000; /* 1s! */ |
| 95 | |
| 96 | while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) { |
| 97 | count--; |
| 98 | if (!count) { |
| 99 | pr_err_ratelimited("RWP timeout, gone fishing\n"); |
| 100 | return; |
| 101 | } |
| 102 | cpu_relax(); |
| 103 | udelay(1); |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | /* Wait for completion of a distributor change */ |
| 108 | static void gic_dist_wait_for_rwp(void) |
| 109 | { |
| 110 | gic_do_wait_for_rwp(gic_data.dist_base); |
| 111 | } |
| 112 | |
| 113 | /* Wait for completion of a redistributor change */ |
| 114 | static void gic_redist_wait_for_rwp(void) |
| 115 | { |
| 116 | gic_do_wait_for_rwp(gic_data_rdist_rd_base()); |
| 117 | } |
| 118 | |
Jean-Philippe Brucker | 7936e91 | 2015-10-01 13:47:14 +0100 | [diff] [blame] | 119 | #ifdef CONFIG_ARM64 |
Robert Richter | 8ac2a17 | 2015-09-21 22:58:39 +0200 | [diff] [blame] | 120 | static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx); |
Robert Richter | 6d4e11c | 2015-09-21 22:58:35 +0200 | [diff] [blame] | 121 | |
| 122 | static u64 __maybe_unused gic_read_iar(void) |
| 123 | { |
Robert Richter | 8ac2a17 | 2015-09-21 22:58:39 +0200 | [diff] [blame] | 124 | if (static_branch_unlikely(&is_cavium_thunderx)) |
Robert Richter | 6d4e11c | 2015-09-21 22:58:35 +0200 | [diff] [blame] | 125 | return gic_read_iar_cavium_thunderx(); |
| 126 | else |
| 127 | return gic_read_iar_common(); |
| 128 | } |
Jean-Philippe Brucker | 7936e91 | 2015-10-01 13:47:14 +0100 | [diff] [blame] | 129 | #endif |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 130 | |
Sudeep Holla | a2c2251 | 2014-08-26 16:03:34 +0100 | [diff] [blame] | 131 | static void gic_enable_redist(bool enable) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 132 | { |
| 133 | void __iomem *rbase; |
| 134 | u32 count = 1000000; /* 1s! */ |
| 135 | u32 val; |
| 136 | |
| 137 | rbase = gic_data_rdist_rd_base(); |
| 138 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 139 | val = readl_relaxed(rbase + GICR_WAKER); |
Sudeep Holla | a2c2251 | 2014-08-26 16:03:34 +0100 | [diff] [blame] | 140 | if (enable) |
| 141 | /* Wake up this CPU redistributor */ |
| 142 | val &= ~GICR_WAKER_ProcessorSleep; |
| 143 | else |
| 144 | val |= GICR_WAKER_ProcessorSleep; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 145 | writel_relaxed(val, rbase + GICR_WAKER); |
| 146 | |
Sudeep Holla | a2c2251 | 2014-08-26 16:03:34 +0100 | [diff] [blame] | 147 | if (!enable) { /* Check that GICR_WAKER is writeable */ |
| 148 | val = readl_relaxed(rbase + GICR_WAKER); |
| 149 | if (!(val & GICR_WAKER_ProcessorSleep)) |
| 150 | return; /* No PM support in this redistributor */ |
| 151 | } |
| 152 | |
| 153 | while (count--) { |
| 154 | val = readl_relaxed(rbase + GICR_WAKER); |
| 155 | if (enable ^ (val & GICR_WAKER_ChildrenAsleep)) |
| 156 | break; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 157 | cpu_relax(); |
| 158 | udelay(1); |
| 159 | }; |
Sudeep Holla | a2c2251 | 2014-08-26 16:03:34 +0100 | [diff] [blame] | 160 | if (!count) |
| 161 | pr_err_ratelimited("redistributor failed to %s...\n", |
| 162 | enable ? "wakeup" : "sleep"); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Routines to disable, enable, EOI and route interrupts |
| 167 | */ |
Marc Zyngier | b594c6e | 2015-03-18 11:01:24 +0000 | [diff] [blame] | 168 | static int gic_peek_irq(struct irq_data *d, u32 offset) |
| 169 | { |
| 170 | u32 mask = 1 << (gic_irq(d) % 32); |
| 171 | void __iomem *base; |
| 172 | |
| 173 | if (gic_irq_in_rdist(d)) |
| 174 | base = gic_data_rdist_sgi_base(); |
| 175 | else |
| 176 | base = gic_data.dist_base; |
| 177 | |
| 178 | return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask); |
| 179 | } |
| 180 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 181 | static void gic_poke_irq(struct irq_data *d, u32 offset) |
| 182 | { |
| 183 | u32 mask = 1 << (gic_irq(d) % 32); |
| 184 | void (*rwp_wait)(void); |
| 185 | void __iomem *base; |
| 186 | |
| 187 | if (gic_irq_in_rdist(d)) { |
| 188 | base = gic_data_rdist_sgi_base(); |
| 189 | rwp_wait = gic_redist_wait_for_rwp; |
| 190 | } else { |
| 191 | base = gic_data.dist_base; |
| 192 | rwp_wait = gic_dist_wait_for_rwp; |
| 193 | } |
| 194 | |
| 195 | writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4); |
| 196 | rwp_wait(); |
| 197 | } |
| 198 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 199 | static void gic_mask_irq(struct irq_data *d) |
| 200 | { |
| 201 | gic_poke_irq(d, GICD_ICENABLER); |
| 202 | } |
| 203 | |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 204 | static void gic_eoimode1_mask_irq(struct irq_data *d) |
| 205 | { |
| 206 | gic_mask_irq(d); |
Marc Zyngier | 530bf35 | 2015-08-26 17:00:43 +0100 | [diff] [blame] | 207 | /* |
| 208 | * When masking a forwarded interrupt, make sure it is |
| 209 | * deactivated as well. |
| 210 | * |
| 211 | * This ensures that an interrupt that is getting |
| 212 | * disabled/masked will not get "stuck", because there is |
| 213 | * noone to deactivate it (guest is being terminated). |
| 214 | */ |
Thomas Gleixner | 4df7f54 | 2015-09-15 13:19:16 +0200 | [diff] [blame] | 215 | if (irqd_is_forwarded_to_vcpu(d)) |
Marc Zyngier | 530bf35 | 2015-08-26 17:00:43 +0100 | [diff] [blame] | 216 | gic_poke_irq(d, GICD_ICACTIVER); |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 219 | static void gic_unmask_irq(struct irq_data *d) |
| 220 | { |
| 221 | gic_poke_irq(d, GICD_ISENABLER); |
| 222 | } |
| 223 | |
Marc Zyngier | b594c6e | 2015-03-18 11:01:24 +0000 | [diff] [blame] | 224 | static int gic_irq_set_irqchip_state(struct irq_data *d, |
| 225 | enum irqchip_irq_state which, bool val) |
| 226 | { |
| 227 | u32 reg; |
| 228 | |
| 229 | if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */ |
| 230 | return -EINVAL; |
| 231 | |
| 232 | switch (which) { |
| 233 | case IRQCHIP_STATE_PENDING: |
| 234 | reg = val ? GICD_ISPENDR : GICD_ICPENDR; |
| 235 | break; |
| 236 | |
| 237 | case IRQCHIP_STATE_ACTIVE: |
| 238 | reg = val ? GICD_ISACTIVER : GICD_ICACTIVER; |
| 239 | break; |
| 240 | |
| 241 | case IRQCHIP_STATE_MASKED: |
| 242 | reg = val ? GICD_ICENABLER : GICD_ISENABLER; |
| 243 | break; |
| 244 | |
| 245 | default: |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | gic_poke_irq(d, reg); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int gic_irq_get_irqchip_state(struct irq_data *d, |
| 254 | enum irqchip_irq_state which, bool *val) |
| 255 | { |
| 256 | if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */ |
| 257 | return -EINVAL; |
| 258 | |
| 259 | switch (which) { |
| 260 | case IRQCHIP_STATE_PENDING: |
| 261 | *val = gic_peek_irq(d, GICD_ISPENDR); |
| 262 | break; |
| 263 | |
| 264 | case IRQCHIP_STATE_ACTIVE: |
| 265 | *val = gic_peek_irq(d, GICD_ISACTIVER); |
| 266 | break; |
| 267 | |
| 268 | case IRQCHIP_STATE_MASKED: |
| 269 | *val = !gic_peek_irq(d, GICD_ISENABLER); |
| 270 | break; |
| 271 | |
| 272 | default: |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 279 | static void gic_eoi_irq(struct irq_data *d) |
| 280 | { |
| 281 | gic_write_eoir(gic_irq(d)); |
| 282 | } |
| 283 | |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 284 | static void gic_eoimode1_eoi_irq(struct irq_data *d) |
| 285 | { |
| 286 | /* |
Marc Zyngier | 530bf35 | 2015-08-26 17:00:43 +0100 | [diff] [blame] | 287 | * No need to deactivate an LPI, or an interrupt that |
| 288 | * is is getting forwarded to a vcpu. |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 289 | */ |
Thomas Gleixner | 4df7f54 | 2015-09-15 13:19:16 +0200 | [diff] [blame] | 290 | if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d)) |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 291 | return; |
| 292 | gic_write_dir(gic_irq(d)); |
| 293 | } |
| 294 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 295 | static int gic_set_type(struct irq_data *d, unsigned int type) |
| 296 | { |
| 297 | unsigned int irq = gic_irq(d); |
| 298 | void (*rwp_wait)(void); |
| 299 | void __iomem *base; |
| 300 | |
| 301 | /* Interrupt configuration for SGIs can't be changed */ |
| 302 | if (irq < 16) |
| 303 | return -EINVAL; |
| 304 | |
Liviu Dudau | fb7e7de | 2015-01-20 16:52:59 +0000 | [diff] [blame] | 305 | /* SPIs have restrictions on the supported types */ |
| 306 | if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH && |
| 307 | type != IRQ_TYPE_EDGE_RISING) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 308 | return -EINVAL; |
| 309 | |
| 310 | if (gic_irq_in_rdist(d)) { |
| 311 | base = gic_data_rdist_sgi_base(); |
| 312 | rwp_wait = gic_redist_wait_for_rwp; |
| 313 | } else { |
| 314 | base = gic_data.dist_base; |
| 315 | rwp_wait = gic_dist_wait_for_rwp; |
| 316 | } |
| 317 | |
Liviu Dudau | fb7e7de | 2015-01-20 16:52:59 +0000 | [diff] [blame] | 318 | return gic_configure_irq(irq, type, base, rwp_wait); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Marc Zyngier | 530bf35 | 2015-08-26 17:00:43 +0100 | [diff] [blame] | 321 | static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu) |
| 322 | { |
Thomas Gleixner | 4df7f54 | 2015-09-15 13:19:16 +0200 | [diff] [blame] | 323 | if (vcpu) |
| 324 | irqd_set_forwarded_to_vcpu(d); |
| 325 | else |
| 326 | irqd_clr_forwarded_to_vcpu(d); |
Marc Zyngier | 530bf35 | 2015-08-26 17:00:43 +0100 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 330 | static u64 gic_mpidr_to_affinity(unsigned long mpidr) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 331 | { |
| 332 | u64 aff; |
| 333 | |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 334 | aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 335 | MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 | |
| 336 | MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 | |
| 337 | MPIDR_AFFINITY_LEVEL(mpidr, 0)); |
| 338 | |
| 339 | return aff; |
| 340 | } |
| 341 | |
| 342 | static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs) |
| 343 | { |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 344 | u32 irqnr; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 345 | |
| 346 | do { |
| 347 | irqnr = gic_read_iar(); |
| 348 | |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 349 | if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) { |
Marc Zyngier | ebc6de0 | 2014-08-26 11:03:33 +0100 | [diff] [blame] | 350 | int err; |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 351 | |
| 352 | if (static_key_true(&supports_deactivate)) |
| 353 | gic_write_eoir(irqnr); |
| 354 | |
Marc Zyngier | ebc6de0 | 2014-08-26 11:03:33 +0100 | [diff] [blame] | 355 | err = handle_domain_irq(gic_data.domain, irqnr, regs); |
| 356 | if (err) { |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 357 | WARN_ONCE(true, "Unexpected interrupt received!\n"); |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 358 | if (static_key_true(&supports_deactivate)) { |
| 359 | if (irqnr < 8192) |
| 360 | gic_write_dir(irqnr); |
| 361 | } else { |
| 362 | gic_write_eoir(irqnr); |
| 363 | } |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 364 | } |
Marc Zyngier | ebc6de0 | 2014-08-26 11:03:33 +0100 | [diff] [blame] | 365 | continue; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 366 | } |
| 367 | if (irqnr < 16) { |
| 368 | gic_write_eoir(irqnr); |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 369 | if (static_key_true(&supports_deactivate)) |
| 370 | gic_write_dir(irqnr); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 371 | #ifdef CONFIG_SMP |
| 372 | handle_IPI(irqnr, regs); |
| 373 | #else |
| 374 | WARN_ONCE(true, "Unexpected SGI received!\n"); |
| 375 | #endif |
| 376 | continue; |
| 377 | } |
| 378 | } while (irqnr != ICC_IAR1_EL1_SPURIOUS); |
| 379 | } |
| 380 | |
| 381 | static void __init gic_dist_init(void) |
| 382 | { |
| 383 | unsigned int i; |
| 384 | u64 affinity; |
| 385 | void __iomem *base = gic_data.dist_base; |
| 386 | |
| 387 | /* Disable the distributor */ |
| 388 | writel_relaxed(0, base + GICD_CTLR); |
| 389 | gic_dist_wait_for_rwp(); |
| 390 | |
| 391 | gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp); |
| 392 | |
| 393 | /* Enable distributor with ARE, Group1 */ |
| 394 | writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1, |
| 395 | base + GICD_CTLR); |
| 396 | |
| 397 | /* |
| 398 | * Set all global interrupts to the boot CPU only. ARE must be |
| 399 | * enabled. |
| 400 | */ |
| 401 | affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id())); |
| 402 | for (i = 32; i < gic_data.irq_nr; i++) |
Jean-Philippe Brucker | 72c9712 | 2015-10-01 13:47:16 +0100 | [diff] [blame] | 403 | gic_write_irouter(affinity, base + GICD_IROUTER + i * 8); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | static int gic_populate_rdist(void) |
| 407 | { |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 408 | unsigned long mpidr = cpu_logical_map(smp_processor_id()); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 409 | u64 typer; |
| 410 | u32 aff; |
| 411 | int i; |
| 412 | |
| 413 | /* |
| 414 | * Convert affinity to a 32bit value that can be matched to |
| 415 | * GICR_TYPER bits [63:32]. |
| 416 | */ |
| 417 | aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 | |
| 418 | MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 | |
| 419 | MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 | |
| 420 | MPIDR_AFFINITY_LEVEL(mpidr, 0)); |
| 421 | |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 422 | for (i = 0; i < gic_data.nr_redist_regions; i++) { |
| 423 | void __iomem *ptr = gic_data.redist_regions[i].redist_base; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 424 | u32 reg; |
| 425 | |
| 426 | reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 427 | if (reg != GIC_PIDR2_ARCH_GICv3 && |
| 428 | reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */ |
| 429 | pr_warn("No redistributor present @%p\n", ptr); |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | do { |
Jean-Philippe Brucker | 72c9712 | 2015-10-01 13:47:16 +0100 | [diff] [blame] | 434 | typer = gic_read_typer(ptr + GICR_TYPER); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 435 | if ((typer >> 32) == aff) { |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 436 | u64 offset = ptr - gic_data.redist_regions[i].redist_base; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 437 | gic_data_rdist_rd_base() = ptr; |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 438 | gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset; |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 439 | pr_info("CPU%d: found redistributor %lx region %d:%pa\n", |
| 440 | smp_processor_id(), mpidr, i, |
| 441 | &gic_data_rdist()->phys_base); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 445 | if (gic_data.redist_regions[i].single_redist) |
| 446 | break; |
| 447 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 448 | if (gic_data.redist_stride) { |
| 449 | ptr += gic_data.redist_stride; |
| 450 | } else { |
| 451 | ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */ |
| 452 | if (typer & GICR_TYPER_VLPIS) |
| 453 | ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */ |
| 454 | } |
| 455 | } while (!(typer & GICR_TYPER_LAST)); |
| 456 | } |
| 457 | |
| 458 | /* We couldn't even deal with ourselves... */ |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 459 | WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n", |
| 460 | smp_processor_id(), mpidr); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 461 | return -ENODEV; |
| 462 | } |
| 463 | |
Sudeep Holla | 3708d52 | 2014-08-26 16:03:35 +0100 | [diff] [blame] | 464 | static void gic_cpu_sys_reg_init(void) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 465 | { |
Marc Zyngier | 7cabd00 | 2015-09-30 11:48:01 +0100 | [diff] [blame] | 466 | /* |
| 467 | * Need to check that the SRE bit has actually been set. If |
| 468 | * not, it means that SRE is disabled at EL2. We're going to |
| 469 | * die painfully, and there is nothing we can do about it. |
| 470 | * |
| 471 | * Kindly inform the luser. |
| 472 | */ |
| 473 | if (!gic_enable_sre()) |
| 474 | pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n"); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 475 | |
| 476 | /* Set priority mask register */ |
| 477 | gic_write_pmr(DEFAULT_PMR_VALUE); |
| 478 | |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 479 | if (static_key_true(&supports_deactivate)) { |
| 480 | /* EOI drops priority only (mode 1) */ |
| 481 | gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop); |
| 482 | } else { |
| 483 | /* EOI deactivates interrupt too (mode 0) */ |
| 484 | gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir); |
| 485 | } |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 486 | |
| 487 | /* ... and let's hit the road... */ |
| 488 | gic_write_grpen1(1); |
| 489 | } |
| 490 | |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 491 | static int gic_dist_supports_lpis(void) |
| 492 | { |
| 493 | return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS); |
| 494 | } |
| 495 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 496 | static void gic_cpu_init(void) |
| 497 | { |
| 498 | void __iomem *rbase; |
| 499 | |
| 500 | /* Register ourselves with the rest of the world */ |
| 501 | if (gic_populate_rdist()) |
| 502 | return; |
| 503 | |
Sudeep Holla | a2c2251 | 2014-08-26 16:03:34 +0100 | [diff] [blame] | 504 | gic_enable_redist(true); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 505 | |
| 506 | rbase = gic_data_rdist_sgi_base(); |
| 507 | |
| 508 | gic_cpu_config(rbase, gic_redist_wait_for_rwp); |
| 509 | |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 510 | /* Give LPIs a spin */ |
| 511 | if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis()) |
| 512 | its_cpu_init(); |
| 513 | |
Sudeep Holla | 3708d52 | 2014-08-26 16:03:35 +0100 | [diff] [blame] | 514 | /* initialise system registers */ |
| 515 | gic_cpu_sys_reg_init(); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | #ifdef CONFIG_SMP |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 519 | static int gic_secondary_init(struct notifier_block *nfb, |
| 520 | unsigned long action, void *hcpu) |
| 521 | { |
| 522 | if (action == CPU_STARTING || action == CPU_STARTING_FROZEN) |
| 523 | gic_cpu_init(); |
| 524 | return NOTIFY_OK; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Notifier for enabling the GIC CPU interface. Set an arbitrarily high |
| 529 | * priority because the GIC needs to be up before the ARM generic timers. |
| 530 | */ |
| 531 | static struct notifier_block gic_cpu_notifier = { |
| 532 | .notifier_call = gic_secondary_init, |
| 533 | .priority = 100, |
| 534 | }; |
| 535 | |
| 536 | static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask, |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 537 | unsigned long cluster_id) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 538 | { |
| 539 | int cpu = *base_cpu; |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 540 | unsigned long mpidr = cpu_logical_map(cpu); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 541 | u16 tlist = 0; |
| 542 | |
| 543 | while (cpu < nr_cpu_ids) { |
| 544 | /* |
| 545 | * If we ever get a cluster of more than 16 CPUs, just |
| 546 | * scream and skip that CPU. |
| 547 | */ |
| 548 | if (WARN_ON((mpidr & 0xff) >= 16)) |
| 549 | goto out; |
| 550 | |
| 551 | tlist |= 1 << (mpidr & 0xf); |
| 552 | |
| 553 | cpu = cpumask_next(cpu, mask); |
Vladimir Murzin | 614be38 | 2015-03-06 16:37:45 +0000 | [diff] [blame] | 554 | if (cpu >= nr_cpu_ids) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 555 | goto out; |
| 556 | |
| 557 | mpidr = cpu_logical_map(cpu); |
| 558 | |
| 559 | if (cluster_id != (mpidr & ~0xffUL)) { |
| 560 | cpu--; |
| 561 | goto out; |
| 562 | } |
| 563 | } |
| 564 | out: |
| 565 | *base_cpu = cpu; |
| 566 | return tlist; |
| 567 | } |
| 568 | |
Andre Przywara | 7e58027 | 2014-11-12 13:46:06 +0000 | [diff] [blame] | 569 | #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \ |
| 570 | (MPIDR_AFFINITY_LEVEL(cluster_id, level) \ |
| 571 | << ICC_SGI1R_AFFINITY_## level ##_SHIFT) |
| 572 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 573 | static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq) |
| 574 | { |
| 575 | u64 val; |
| 576 | |
Andre Przywara | 7e58027 | 2014-11-12 13:46:06 +0000 | [diff] [blame] | 577 | val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) | |
| 578 | MPIDR_TO_SGI_AFFINITY(cluster_id, 2) | |
| 579 | irq << ICC_SGI1R_SGI_ID_SHIFT | |
| 580 | MPIDR_TO_SGI_AFFINITY(cluster_id, 1) | |
| 581 | tlist << ICC_SGI1R_TARGET_LIST_SHIFT); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 582 | |
| 583 | pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val); |
| 584 | gic_write_sgi1r(val); |
| 585 | } |
| 586 | |
| 587 | static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq) |
| 588 | { |
| 589 | int cpu; |
| 590 | |
| 591 | if (WARN_ON(irq >= 16)) |
| 592 | return; |
| 593 | |
| 594 | /* |
| 595 | * Ensure that stores to Normal memory are visible to the |
| 596 | * other CPUs before issuing the IPI. |
| 597 | */ |
| 598 | smp_wmb(); |
| 599 | |
Rusty Russell | f9b531f | 2015-03-05 10:49:16 +1030 | [diff] [blame] | 600 | for_each_cpu(cpu, mask) { |
Jean-Philippe Brucker | f6c86a4 | 2015-10-01 13:47:15 +0100 | [diff] [blame] | 601 | unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 602 | u16 tlist; |
| 603 | |
| 604 | tlist = gic_compute_target_list(&cpu, mask, cluster_id); |
| 605 | gic_send_sgi(cluster_id, tlist, irq); |
| 606 | } |
| 607 | |
| 608 | /* Force the above writes to ICC_SGI1R_EL1 to be executed */ |
| 609 | isb(); |
| 610 | } |
| 611 | |
| 612 | static void gic_smp_init(void) |
| 613 | { |
| 614 | set_smp_cross_call(gic_raise_softirq); |
| 615 | register_cpu_notifier(&gic_cpu_notifier); |
| 616 | } |
| 617 | |
| 618 | static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 619 | bool force) |
| 620 | { |
| 621 | unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask); |
| 622 | void __iomem *reg; |
| 623 | int enabled; |
| 624 | u64 val; |
| 625 | |
| 626 | if (gic_irq_in_rdist(d)) |
| 627 | return -EINVAL; |
| 628 | |
| 629 | /* If interrupt was enabled, disable it first */ |
| 630 | enabled = gic_peek_irq(d, GICD_ISENABLER); |
| 631 | if (enabled) |
| 632 | gic_mask_irq(d); |
| 633 | |
| 634 | reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8); |
| 635 | val = gic_mpidr_to_affinity(cpu_logical_map(cpu)); |
| 636 | |
Jean-Philippe Brucker | 72c9712 | 2015-10-01 13:47:16 +0100 | [diff] [blame] | 637 | gic_write_irouter(val, reg); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | * If the interrupt was enabled, enabled it again. Otherwise, |
| 641 | * just wait for the distributor to have digested our changes. |
| 642 | */ |
| 643 | if (enabled) |
| 644 | gic_unmask_irq(d); |
| 645 | else |
| 646 | gic_dist_wait_for_rwp(); |
| 647 | |
Antoine Tenart | 0fc6fa2 | 2016-02-19 16:22:43 +0100 | [diff] [blame] | 648 | return IRQ_SET_MASK_OK_DONE; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 649 | } |
| 650 | #else |
| 651 | #define gic_set_affinity NULL |
| 652 | #define gic_smp_init() do { } while(0) |
| 653 | #endif |
| 654 | |
Sudeep Holla | 3708d52 | 2014-08-26 16:03:35 +0100 | [diff] [blame] | 655 | #ifdef CONFIG_CPU_PM |
| 656 | static int gic_cpu_pm_notifier(struct notifier_block *self, |
| 657 | unsigned long cmd, void *v) |
| 658 | { |
| 659 | if (cmd == CPU_PM_EXIT) { |
| 660 | gic_enable_redist(true); |
| 661 | gic_cpu_sys_reg_init(); |
| 662 | } else if (cmd == CPU_PM_ENTER) { |
| 663 | gic_write_grpen1(0); |
| 664 | gic_enable_redist(false); |
| 665 | } |
| 666 | return NOTIFY_OK; |
| 667 | } |
| 668 | |
| 669 | static struct notifier_block gic_cpu_pm_notifier_block = { |
| 670 | .notifier_call = gic_cpu_pm_notifier, |
| 671 | }; |
| 672 | |
| 673 | static void gic_cpu_pm_init(void) |
| 674 | { |
| 675 | cpu_pm_register_notifier(&gic_cpu_pm_notifier_block); |
| 676 | } |
| 677 | |
| 678 | #else |
| 679 | static inline void gic_cpu_pm_init(void) { } |
| 680 | #endif /* CONFIG_CPU_PM */ |
| 681 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 682 | static struct irq_chip gic_chip = { |
| 683 | .name = "GICv3", |
| 684 | .irq_mask = gic_mask_irq, |
| 685 | .irq_unmask = gic_unmask_irq, |
| 686 | .irq_eoi = gic_eoi_irq, |
| 687 | .irq_set_type = gic_set_type, |
| 688 | .irq_set_affinity = gic_set_affinity, |
Marc Zyngier | b594c6e | 2015-03-18 11:01:24 +0000 | [diff] [blame] | 689 | .irq_get_irqchip_state = gic_irq_get_irqchip_state, |
| 690 | .irq_set_irqchip_state = gic_irq_set_irqchip_state, |
Sudeep Holla | 55963c9 | 2015-06-05 11:59:57 +0100 | [diff] [blame] | 691 | .flags = IRQCHIP_SET_TYPE_MASKED, |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 692 | }; |
| 693 | |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 694 | static struct irq_chip gic_eoimode1_chip = { |
| 695 | .name = "GICv3", |
| 696 | .irq_mask = gic_eoimode1_mask_irq, |
| 697 | .irq_unmask = gic_unmask_irq, |
| 698 | .irq_eoi = gic_eoimode1_eoi_irq, |
| 699 | .irq_set_type = gic_set_type, |
| 700 | .irq_set_affinity = gic_set_affinity, |
| 701 | .irq_get_irqchip_state = gic_irq_get_irqchip_state, |
| 702 | .irq_set_irqchip_state = gic_irq_set_irqchip_state, |
Marc Zyngier | 530bf35 | 2015-08-26 17:00:43 +0100 | [diff] [blame] | 703 | .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity, |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 704 | .flags = IRQCHIP_SET_TYPE_MASKED, |
| 705 | }; |
| 706 | |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 707 | #define GIC_ID_NR (1U << gic_data.rdists.id_bits) |
| 708 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 709 | static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq, |
| 710 | irq_hw_number_t hw) |
| 711 | { |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 712 | struct irq_chip *chip = &gic_chip; |
| 713 | |
| 714 | if (static_key_true(&supports_deactivate)) |
| 715 | chip = &gic_eoimode1_chip; |
| 716 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 717 | /* SGIs are private to the core kernel */ |
| 718 | if (hw < 16) |
| 719 | return -EPERM; |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 720 | /* Nothing here */ |
| 721 | if (hw >= gic_data.irq_nr && hw < 8192) |
| 722 | return -EPERM; |
| 723 | /* Off limits */ |
| 724 | if (hw >= GIC_ID_NR) |
| 725 | return -EPERM; |
| 726 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 727 | /* PPIs */ |
| 728 | if (hw < 32) { |
| 729 | irq_set_percpu_devid(irq); |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 730 | irq_domain_set_info(d, irq, hw, chip, d->host_data, |
Marc Zyngier | 443acc4 | 2014-11-24 14:35:09 +0000 | [diff] [blame] | 731 | handle_percpu_devid_irq, NULL, NULL); |
Rob Herring | d17cab4 | 2015-08-29 18:01:22 -0500 | [diff] [blame] | 732 | irq_set_status_flags(irq, IRQ_NOAUTOEN); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 733 | } |
| 734 | /* SPIs */ |
| 735 | if (hw >= 32 && hw < gic_data.irq_nr) { |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 736 | irq_domain_set_info(d, irq, hw, chip, d->host_data, |
Marc Zyngier | 443acc4 | 2014-11-24 14:35:09 +0000 | [diff] [blame] | 737 | handle_fasteoi_irq, NULL, NULL); |
Rob Herring | d17cab4 | 2015-08-29 18:01:22 -0500 | [diff] [blame] | 738 | irq_set_probe(irq); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 739 | } |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 740 | /* LPIs */ |
| 741 | if (hw >= 8192 && hw < GIC_ID_NR) { |
| 742 | if (!gic_dist_supports_lpis()) |
| 743 | return -EPERM; |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 744 | irq_domain_set_info(d, irq, hw, chip, d->host_data, |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 745 | handle_fasteoi_irq, NULL, NULL); |
Marc Zyngier | da33f31 | 2014-11-24 14:35:18 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 751 | static int gic_irq_domain_translate(struct irq_domain *d, |
| 752 | struct irq_fwspec *fwspec, |
| 753 | unsigned long *hwirq, |
| 754 | unsigned int *type) |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 755 | { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 756 | if (is_of_node(fwspec->fwnode)) { |
| 757 | if (fwspec->param_count < 3) |
| 758 | return -EINVAL; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 759 | |
Marc Zyngier | db8c70e | 2015-10-14 12:27:16 +0100 | [diff] [blame] | 760 | switch (fwspec->param[0]) { |
| 761 | case 0: /* SPI */ |
| 762 | *hwirq = fwspec->param[1] + 32; |
| 763 | break; |
| 764 | case 1: /* PPI */ |
| 765 | *hwirq = fwspec->param[1] + 16; |
| 766 | break; |
| 767 | case GIC_IRQ_TYPE_LPI: /* LPI */ |
| 768 | *hwirq = fwspec->param[1]; |
| 769 | break; |
| 770 | default: |
| 771 | return -EINVAL; |
| 772 | } |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 773 | |
| 774 | *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK; |
| 775 | return 0; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 776 | } |
| 777 | |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 778 | if (is_fwnode_irqchip(fwspec->fwnode)) { |
| 779 | if(fwspec->param_count != 2) |
| 780 | return -EINVAL; |
| 781 | |
| 782 | *hwirq = fwspec->param[0]; |
| 783 | *type = fwspec->param[1]; |
| 784 | return 0; |
| 785 | } |
| 786 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 787 | return -EINVAL; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 788 | } |
| 789 | |
Marc Zyngier | 443acc4 | 2014-11-24 14:35:09 +0000 | [diff] [blame] | 790 | static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 791 | unsigned int nr_irqs, void *arg) |
| 792 | { |
| 793 | int i, ret; |
| 794 | irq_hw_number_t hwirq; |
| 795 | unsigned int type = IRQ_TYPE_NONE; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 796 | struct irq_fwspec *fwspec = arg; |
Marc Zyngier | 443acc4 | 2014-11-24 14:35:09 +0000 | [diff] [blame] | 797 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 798 | ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type); |
Marc Zyngier | 443acc4 | 2014-11-24 14:35:09 +0000 | [diff] [blame] | 799 | if (ret) |
| 800 | return ret; |
| 801 | |
| 802 | for (i = 0; i < nr_irqs; i++) |
| 803 | gic_irq_domain_map(domain, virq + i, hwirq + i); |
| 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 809 | unsigned int nr_irqs) |
| 810 | { |
| 811 | int i; |
| 812 | |
| 813 | for (i = 0; i < nr_irqs; i++) { |
| 814 | struct irq_data *d = irq_domain_get_irq_data(domain, virq + i); |
| 815 | irq_set_handler(virq + i, NULL); |
| 816 | irq_domain_reset_irq_data(d); |
| 817 | } |
| 818 | } |
| 819 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 820 | static const struct irq_domain_ops gic_irq_domain_ops = { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 821 | .translate = gic_irq_domain_translate, |
Marc Zyngier | 443acc4 | 2014-11-24 14:35:09 +0000 | [diff] [blame] | 822 | .alloc = gic_irq_domain_alloc, |
| 823 | .free = gic_irq_domain_free, |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 824 | }; |
| 825 | |
Robert Richter | 6d4e11c | 2015-09-21 22:58:35 +0200 | [diff] [blame] | 826 | static void gicv3_enable_quirks(void) |
| 827 | { |
Jean-Philippe Brucker | 7936e91 | 2015-10-01 13:47:14 +0100 | [diff] [blame] | 828 | #ifdef CONFIG_ARM64 |
Robert Richter | 6d4e11c | 2015-09-21 22:58:35 +0200 | [diff] [blame] | 829 | if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154)) |
Robert Richter | 8ac2a17 | 2015-09-21 22:58:39 +0200 | [diff] [blame] | 830 | static_branch_enable(&is_cavium_thunderx); |
Jean-Philippe Brucker | 7936e91 | 2015-10-01 13:47:14 +0100 | [diff] [blame] | 831 | #endif |
Robert Richter | 6d4e11c | 2015-09-21 22:58:35 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Tomasz Nowicki | db57d74 | 2016-01-19 14:11:14 +0100 | [diff] [blame] | 834 | static int __init gic_init_bases(void __iomem *dist_base, |
| 835 | struct redist_region *rdist_regs, |
| 836 | u32 nr_redist_regions, |
| 837 | u64 redist_stride, |
| 838 | struct fwnode_handle *handle) |
| 839 | { |
| 840 | struct device_node *node; |
| 841 | u32 typer; |
| 842 | int gic_irqs; |
| 843 | int err; |
| 844 | |
| 845 | if (!is_hyp_mode_available()) |
| 846 | static_key_slow_dec(&supports_deactivate); |
| 847 | |
| 848 | if (static_key_true(&supports_deactivate)) |
| 849 | pr_info("GIC: Using split EOI/Deactivate mode\n"); |
| 850 | |
| 851 | gic_data.dist_base = dist_base; |
| 852 | gic_data.redist_regions = rdist_regs; |
| 853 | gic_data.nr_redist_regions = nr_redist_regions; |
| 854 | gic_data.redist_stride = redist_stride; |
| 855 | |
| 856 | gicv3_enable_quirks(); |
| 857 | |
| 858 | /* |
| 859 | * Find out how many interrupts are supported. |
| 860 | * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI) |
| 861 | */ |
| 862 | typer = readl_relaxed(gic_data.dist_base + GICD_TYPER); |
| 863 | gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer); |
| 864 | gic_irqs = GICD_TYPER_IRQS(typer); |
| 865 | if (gic_irqs > 1020) |
| 866 | gic_irqs = 1020; |
| 867 | gic_data.irq_nr = gic_irqs; |
| 868 | |
| 869 | gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops, |
| 870 | &gic_data); |
| 871 | gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist)); |
| 872 | |
| 873 | if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) { |
| 874 | err = -ENOMEM; |
| 875 | goto out_free; |
| 876 | } |
| 877 | |
| 878 | set_handle_irq(gic_handle_irq); |
| 879 | |
| 880 | node = to_of_node(handle); |
| 881 | if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() && |
| 882 | node) /* Temp hack to prevent ITS init for ACPI */ |
| 883 | its_init(node, &gic_data.rdists, gic_data.domain); |
| 884 | |
| 885 | gic_smp_init(); |
| 886 | gic_dist_init(); |
| 887 | gic_cpu_init(); |
| 888 | gic_cpu_pm_init(); |
| 889 | |
| 890 | return 0; |
| 891 | |
| 892 | out_free: |
| 893 | if (gic_data.domain) |
| 894 | irq_domain_remove(gic_data.domain); |
| 895 | free_percpu(gic_data.rdists.rdist); |
| 896 | return err; |
| 897 | } |
| 898 | |
| 899 | static int __init gic_validate_dist_version(void __iomem *dist_base) |
| 900 | { |
| 901 | u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 902 | |
| 903 | if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4) |
| 904 | return -ENODEV; |
| 905 | |
| 906 | return 0; |
| 907 | } |
| 908 | |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 909 | static void __init gic_of_setup_kvm_info(struct device_node *node) |
| 910 | { |
| 911 | int ret; |
| 912 | struct resource r; |
| 913 | u32 gicv_idx; |
| 914 | |
| 915 | gic_v3_kvm_info.type = GIC_V3; |
| 916 | |
| 917 | gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0); |
| 918 | if (!gic_v3_kvm_info.maint_irq) |
| 919 | return; |
| 920 | |
| 921 | if (of_property_read_u32(node, "#redistributor-regions", |
| 922 | &gicv_idx)) |
| 923 | gicv_idx = 1; |
| 924 | |
| 925 | gicv_idx += 3; /* Also skip GICD, GICC, GICH */ |
| 926 | ret = of_address_to_resource(node, gicv_idx, &r); |
| 927 | if (!ret) |
| 928 | gic_v3_kvm_info.vcpu = r; |
| 929 | |
| 930 | gic_set_kvm_info(&gic_v3_kvm_info); |
| 931 | } |
| 932 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 933 | static int __init gic_of_init(struct device_node *node, struct device_node *parent) |
| 934 | { |
| 935 | void __iomem *dist_base; |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 936 | struct redist_region *rdist_regs; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 937 | u64 redist_stride; |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 938 | u32 nr_redist_regions; |
Tomasz Nowicki | db57d74 | 2016-01-19 14:11:14 +0100 | [diff] [blame] | 939 | int err, i; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 940 | |
| 941 | dist_base = of_iomap(node, 0); |
| 942 | if (!dist_base) { |
| 943 | pr_err("%s: unable to map gic dist registers\n", |
| 944 | node->full_name); |
| 945 | return -ENXIO; |
| 946 | } |
| 947 | |
Tomasz Nowicki | db57d74 | 2016-01-19 14:11:14 +0100 | [diff] [blame] | 948 | err = gic_validate_dist_version(dist_base); |
| 949 | if (err) { |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 950 | pr_err("%s: no distributor detected, giving up\n", |
| 951 | node->full_name); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 952 | goto out_unmap_dist; |
| 953 | } |
| 954 | |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 955 | if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions)) |
| 956 | nr_redist_regions = 1; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 957 | |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 958 | rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL); |
| 959 | if (!rdist_regs) { |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 960 | err = -ENOMEM; |
| 961 | goto out_unmap_dist; |
| 962 | } |
| 963 | |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 964 | for (i = 0; i < nr_redist_regions; i++) { |
| 965 | struct resource res; |
| 966 | int ret; |
| 967 | |
| 968 | ret = of_address_to_resource(node, 1 + i, &res); |
| 969 | rdist_regs[i].redist_base = of_iomap(node, 1 + i); |
| 970 | if (ret || !rdist_regs[i].redist_base) { |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 971 | pr_err("%s: couldn't map region %d\n", |
| 972 | node->full_name, i); |
| 973 | err = -ENODEV; |
| 974 | goto out_unmap_rdist; |
| 975 | } |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 976 | rdist_regs[i].phys_base = res.start; |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | if (of_property_read_u64(node, "redistributor-stride", &redist_stride)) |
| 980 | redist_stride = 0; |
| 981 | |
Tomasz Nowicki | db57d74 | 2016-01-19 14:11:14 +0100 | [diff] [blame] | 982 | err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions, |
| 983 | redist_stride, &node->fwnode); |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 984 | if (!err) { |
| 985 | gic_of_setup_kvm_info(node); |
Tomasz Nowicki | db57d74 | 2016-01-19 14:11:14 +0100 | [diff] [blame] | 986 | return 0; |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 987 | } |
Marc Zyngier | 0b6a3da | 2015-08-26 17:00:42 +0100 | [diff] [blame] | 988 | |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 989 | out_unmap_rdist: |
Marc Zyngier | f5c1434 | 2014-11-24 14:35:10 +0000 | [diff] [blame] | 990 | for (i = 0; i < nr_redist_regions; i++) |
| 991 | if (rdist_regs[i].redist_base) |
| 992 | iounmap(rdist_regs[i].redist_base); |
| 993 | kfree(rdist_regs); |
Marc Zyngier | 021f653 | 2014-06-30 16:01:31 +0100 | [diff] [blame] | 994 | out_unmap_dist: |
| 995 | iounmap(dist_base); |
| 996 | return err; |
| 997 | } |
| 998 | |
| 999 | IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1000 | |
| 1001 | #ifdef CONFIG_ACPI |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1002 | static struct |
| 1003 | { |
| 1004 | void __iomem *dist_base; |
| 1005 | struct redist_region *redist_regs; |
| 1006 | u32 nr_redist_regions; |
| 1007 | bool single_redist; |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 1008 | u32 maint_irq; |
| 1009 | int maint_irq_mode; |
| 1010 | phys_addr_t vcpu_base; |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1011 | } acpi_data __initdata; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1012 | |
| 1013 | static void __init |
| 1014 | gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base) |
| 1015 | { |
| 1016 | static int count = 0; |
| 1017 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1018 | acpi_data.redist_regs[count].phys_base = phys_base; |
| 1019 | acpi_data.redist_regs[count].redist_base = redist_base; |
| 1020 | acpi_data.redist_regs[count].single_redist = acpi_data.single_redist; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1021 | count++; |
| 1022 | } |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1023 | |
| 1024 | static int __init |
| 1025 | gic_acpi_parse_madt_redist(struct acpi_subtable_header *header, |
| 1026 | const unsigned long end) |
| 1027 | { |
| 1028 | struct acpi_madt_generic_redistributor *redist = |
| 1029 | (struct acpi_madt_generic_redistributor *)header; |
| 1030 | void __iomem *redist_base; |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1031 | |
| 1032 | redist_base = ioremap(redist->base_address, redist->length); |
| 1033 | if (!redist_base) { |
| 1034 | pr_err("Couldn't map GICR region @%llx\n", redist->base_address); |
| 1035 | return -ENOMEM; |
| 1036 | } |
| 1037 | |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1038 | gic_acpi_register_redist(redist->base_address, redist_base); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1039 | return 0; |
| 1040 | } |
| 1041 | |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1042 | static int __init |
| 1043 | gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header, |
| 1044 | const unsigned long end) |
| 1045 | { |
| 1046 | struct acpi_madt_generic_interrupt *gicc = |
| 1047 | (struct acpi_madt_generic_interrupt *)header; |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1048 | u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1049 | u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2; |
| 1050 | void __iomem *redist_base; |
| 1051 | |
| 1052 | redist_base = ioremap(gicc->gicr_base_address, size); |
| 1053 | if (!redist_base) |
| 1054 | return -ENOMEM; |
| 1055 | |
| 1056 | gic_acpi_register_redist(gicc->gicr_base_address, redist_base); |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | static int __init gic_acpi_collect_gicr_base(void) |
| 1061 | { |
| 1062 | acpi_tbl_entry_handler redist_parser; |
| 1063 | enum acpi_madt_type type; |
| 1064 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1065 | if (acpi_data.single_redist) { |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1066 | type = ACPI_MADT_TYPE_GENERIC_INTERRUPT; |
| 1067 | redist_parser = gic_acpi_parse_madt_gicc; |
| 1068 | } else { |
| 1069 | type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR; |
| 1070 | redist_parser = gic_acpi_parse_madt_redist; |
| 1071 | } |
| 1072 | |
| 1073 | /* Collect redistributor base addresses in GICR entries */ |
| 1074 | if (acpi_table_parse_madt(type, redist_parser, 0) > 0) |
| 1075 | return 0; |
| 1076 | |
| 1077 | pr_info("No valid GICR entries exist\n"); |
| 1078 | return -ENODEV; |
| 1079 | } |
| 1080 | |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1081 | static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header, |
| 1082 | const unsigned long end) |
| 1083 | { |
| 1084 | /* Subtable presence means that redist exists, that's it */ |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1088 | static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header, |
| 1089 | const unsigned long end) |
| 1090 | { |
| 1091 | struct acpi_madt_generic_interrupt *gicc = |
| 1092 | (struct acpi_madt_generic_interrupt *)header; |
| 1093 | |
| 1094 | /* |
| 1095 | * If GICC is enabled and has valid gicr base address, then it means |
| 1096 | * GICR base is presented via GICC |
| 1097 | */ |
| 1098 | if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address) |
| 1099 | return 0; |
| 1100 | |
| 1101 | return -ENODEV; |
| 1102 | } |
| 1103 | |
| 1104 | static int __init gic_acpi_count_gicr_regions(void) |
| 1105 | { |
| 1106 | int count; |
| 1107 | |
| 1108 | /* |
| 1109 | * Count how many redistributor regions we have. It is not allowed |
| 1110 | * to mix redistributor description, GICR and GICC subtables have to be |
| 1111 | * mutually exclusive. |
| 1112 | */ |
| 1113 | count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR, |
| 1114 | gic_acpi_match_gicr, 0); |
| 1115 | if (count > 0) { |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1116 | acpi_data.single_redist = false; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1117 | return count; |
| 1118 | } |
| 1119 | |
| 1120 | count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, |
| 1121 | gic_acpi_match_gicc, 0); |
| 1122 | if (count > 0) |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1123 | acpi_data.single_redist = true; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1124 | |
| 1125 | return count; |
| 1126 | } |
| 1127 | |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1128 | static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header, |
| 1129 | struct acpi_probe_entry *ape) |
| 1130 | { |
| 1131 | struct acpi_madt_generic_distributor *dist; |
| 1132 | int count; |
| 1133 | |
| 1134 | dist = (struct acpi_madt_generic_distributor *)header; |
| 1135 | if (dist->version != ape->driver_data) |
| 1136 | return false; |
| 1137 | |
| 1138 | /* We need to do that exercise anyway, the sooner the better */ |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1139 | count = gic_acpi_count_gicr_regions(); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1140 | if (count <= 0) |
| 1141 | return false; |
| 1142 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1143 | acpi_data.nr_redist_regions = count; |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1144 | return true; |
| 1145 | } |
| 1146 | |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 1147 | static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header, |
| 1148 | const unsigned long end) |
| 1149 | { |
| 1150 | struct acpi_madt_generic_interrupt *gicc = |
| 1151 | (struct acpi_madt_generic_interrupt *)header; |
| 1152 | int maint_irq_mode; |
| 1153 | static int first_madt = true; |
| 1154 | |
| 1155 | /* Skip unusable CPUs */ |
| 1156 | if (!(gicc->flags & ACPI_MADT_ENABLED)) |
| 1157 | return 0; |
| 1158 | |
| 1159 | maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ? |
| 1160 | ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE; |
| 1161 | |
| 1162 | if (first_madt) { |
| 1163 | first_madt = false; |
| 1164 | |
| 1165 | acpi_data.maint_irq = gicc->vgic_interrupt; |
| 1166 | acpi_data.maint_irq_mode = maint_irq_mode; |
| 1167 | acpi_data.vcpu_base = gicc->gicv_base_address; |
| 1168 | |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * The maintenance interrupt and GICV should be the same for every CPU |
| 1174 | */ |
| 1175 | if ((acpi_data.maint_irq != gicc->vgic_interrupt) || |
| 1176 | (acpi_data.maint_irq_mode != maint_irq_mode) || |
| 1177 | (acpi_data.vcpu_base != gicc->gicv_base_address)) |
| 1178 | return -EINVAL; |
| 1179 | |
| 1180 | return 0; |
| 1181 | } |
| 1182 | |
| 1183 | static bool __init gic_acpi_collect_virt_info(void) |
| 1184 | { |
| 1185 | int count; |
| 1186 | |
| 1187 | count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, |
| 1188 | gic_acpi_parse_virt_madt_gicc, 0); |
| 1189 | |
| 1190 | return (count > 0); |
| 1191 | } |
| 1192 | |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1193 | #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K) |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 1194 | #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K) |
| 1195 | #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K) |
| 1196 | |
| 1197 | static void __init gic_acpi_setup_kvm_info(void) |
| 1198 | { |
| 1199 | int irq; |
| 1200 | |
| 1201 | if (!gic_acpi_collect_virt_info()) { |
| 1202 | pr_warn("Unable to get hardware information used for virtualization\n"); |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | gic_v3_kvm_info.type = GIC_V3; |
| 1207 | |
| 1208 | irq = acpi_register_gsi(NULL, acpi_data.maint_irq, |
| 1209 | acpi_data.maint_irq_mode, |
| 1210 | ACPI_ACTIVE_HIGH); |
| 1211 | if (irq <= 0) |
| 1212 | return; |
| 1213 | |
| 1214 | gic_v3_kvm_info.maint_irq = irq; |
| 1215 | |
| 1216 | if (acpi_data.vcpu_base) { |
| 1217 | struct resource *vcpu = &gic_v3_kvm_info.vcpu; |
| 1218 | |
| 1219 | vcpu->flags = IORESOURCE_MEM; |
| 1220 | vcpu->start = acpi_data.vcpu_base; |
| 1221 | vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1; |
| 1222 | } |
| 1223 | |
| 1224 | gic_set_kvm_info(&gic_v3_kvm_info); |
| 1225 | } |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1226 | |
| 1227 | static int __init |
| 1228 | gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end) |
| 1229 | { |
| 1230 | struct acpi_madt_generic_distributor *dist; |
| 1231 | struct fwnode_handle *domain_handle; |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1232 | size_t size; |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1233 | int i, err; |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1234 | |
| 1235 | /* Get distributor base address */ |
| 1236 | dist = (struct acpi_madt_generic_distributor *)header; |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1237 | acpi_data.dist_base = ioremap(dist->base_address, |
| 1238 | ACPI_GICV3_DIST_MEM_SIZE); |
| 1239 | if (!acpi_data.dist_base) { |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1240 | pr_err("Unable to map GICD registers\n"); |
| 1241 | return -ENOMEM; |
| 1242 | } |
| 1243 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1244 | err = gic_validate_dist_version(acpi_data.dist_base); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1245 | if (err) { |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1246 | pr_err("No distributor detected at @%p, giving up", |
| 1247 | acpi_data.dist_base); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1248 | goto out_dist_unmap; |
| 1249 | } |
| 1250 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1251 | size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions; |
| 1252 | acpi_data.redist_regs = kzalloc(size, GFP_KERNEL); |
| 1253 | if (!acpi_data.redist_regs) { |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1254 | err = -ENOMEM; |
| 1255 | goto out_dist_unmap; |
| 1256 | } |
| 1257 | |
Tomasz Nowicki | b70fb7a | 2016-01-19 14:11:16 +0100 | [diff] [blame] | 1258 | err = gic_acpi_collect_gicr_base(); |
| 1259 | if (err) |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1260 | goto out_redist_unmap; |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1261 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1262 | domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1263 | if (!domain_handle) { |
| 1264 | err = -ENOMEM; |
| 1265 | goto out_redist_unmap; |
| 1266 | } |
| 1267 | |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1268 | err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs, |
| 1269 | acpi_data.nr_redist_regions, 0, domain_handle); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1270 | if (err) |
| 1271 | goto out_fwhandle_free; |
| 1272 | |
| 1273 | acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle); |
Julien Grall | 1839e57 | 2016-04-11 16:32:57 +0100 | [diff] [blame^] | 1274 | gic_acpi_setup_kvm_info(); |
| 1275 | |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1276 | return 0; |
| 1277 | |
| 1278 | out_fwhandle_free: |
| 1279 | irq_domain_free_fwnode(domain_handle); |
| 1280 | out_redist_unmap: |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1281 | for (i = 0; i < acpi_data.nr_redist_regions; i++) |
| 1282 | if (acpi_data.redist_regs[i].redist_base) |
| 1283 | iounmap(acpi_data.redist_regs[i].redist_base); |
| 1284 | kfree(acpi_data.redist_regs); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1285 | out_dist_unmap: |
Julien Grall | 611f039 | 2016-04-11 16:32:56 +0100 | [diff] [blame] | 1286 | iounmap(acpi_data.dist_base); |
Tomasz Nowicki | ffa7d61 | 2016-01-19 14:11:15 +0100 | [diff] [blame] | 1287 | return err; |
| 1288 | } |
| 1289 | IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, |
| 1290 | acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3, |
| 1291 | gic_acpi_init); |
| 1292 | IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, |
| 1293 | acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4, |
| 1294 | gic_acpi_init); |
| 1295 | IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR, |
| 1296 | acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE, |
| 1297 | gic_acpi_init); |
| 1298 | #endif |