1021f6537SMarc Zyngier /* 2021f6537SMarc Zyngier * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved. 3021f6537SMarc Zyngier * Author: Marc Zyngier <marc.zyngier@arm.com> 4021f6537SMarc Zyngier * 5021f6537SMarc Zyngier * This program is free software; you can redistribute it and/or modify 6021f6537SMarc Zyngier * it under the terms of the GNU General Public License version 2 as 7021f6537SMarc Zyngier * published by the Free Software Foundation. 8021f6537SMarc Zyngier * 9021f6537SMarc Zyngier * This program is distributed in the hope that it will be useful, 10021f6537SMarc Zyngier * but WITHOUT ANY WARRANTY; without even the implied warranty of 11021f6537SMarc Zyngier * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12021f6537SMarc Zyngier * GNU General Public License for more details. 13021f6537SMarc Zyngier * 14021f6537SMarc Zyngier * You should have received a copy of the GNU General Public License 15021f6537SMarc Zyngier * along with this program. If not, see <http://www.gnu.org/licenses/>. 16021f6537SMarc Zyngier */ 17021f6537SMarc Zyngier 18021f6537SMarc Zyngier #include <linux/cpu.h> 193708d52fSSudeep Holla #include <linux/cpu_pm.h> 20021f6537SMarc Zyngier #include <linux/delay.h> 21021f6537SMarc Zyngier #include <linux/interrupt.h> 22021f6537SMarc Zyngier #include <linux/of.h> 23021f6537SMarc Zyngier #include <linux/of_address.h> 24021f6537SMarc Zyngier #include <linux/of_irq.h> 25021f6537SMarc Zyngier #include <linux/percpu.h> 26021f6537SMarc Zyngier #include <linux/slab.h> 27021f6537SMarc Zyngier 28021f6537SMarc Zyngier #include <linux/irqchip/arm-gic-v3.h> 29021f6537SMarc Zyngier 30021f6537SMarc Zyngier #include <asm/cputype.h> 31021f6537SMarc Zyngier #include <asm/exception.h> 32021f6537SMarc Zyngier #include <asm/smp_plat.h> 33021f6537SMarc Zyngier 34021f6537SMarc Zyngier #include "irq-gic-common.h" 35021f6537SMarc Zyngier #include "irqchip.h" 36021f6537SMarc Zyngier 37f5c1434cSMarc Zyngier struct redist_region { 38f5c1434cSMarc Zyngier void __iomem *redist_base; 39f5c1434cSMarc Zyngier phys_addr_t phys_base; 40f5c1434cSMarc Zyngier }; 41f5c1434cSMarc Zyngier 42021f6537SMarc Zyngier struct gic_chip_data { 43021f6537SMarc Zyngier void __iomem *dist_base; 44f5c1434cSMarc Zyngier struct redist_region *redist_regions; 45f5c1434cSMarc Zyngier struct rdists rdists; 46021f6537SMarc Zyngier struct irq_domain *domain; 47021f6537SMarc Zyngier u64 redist_stride; 48f5c1434cSMarc Zyngier u32 nr_redist_regions; 49021f6537SMarc Zyngier unsigned int irq_nr; 50021f6537SMarc Zyngier }; 51021f6537SMarc Zyngier 52021f6537SMarc Zyngier static struct gic_chip_data gic_data __read_mostly; 53021f6537SMarc Zyngier 54f5c1434cSMarc Zyngier #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist)) 55f5c1434cSMarc Zyngier #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) 56021f6537SMarc Zyngier #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K) 57021f6537SMarc Zyngier 58021f6537SMarc Zyngier /* Our default, arbitrary priority value. Linux only uses one anyway. */ 59021f6537SMarc Zyngier #define DEFAULT_PMR_VALUE 0xf0 60021f6537SMarc Zyngier 61021f6537SMarc Zyngier static inline unsigned int gic_irq(struct irq_data *d) 62021f6537SMarc Zyngier { 63021f6537SMarc Zyngier return d->hwirq; 64021f6537SMarc Zyngier } 65021f6537SMarc Zyngier 66021f6537SMarc Zyngier static inline int gic_irq_in_rdist(struct irq_data *d) 67021f6537SMarc Zyngier { 68021f6537SMarc Zyngier return gic_irq(d) < 32; 69021f6537SMarc Zyngier } 70021f6537SMarc Zyngier 71021f6537SMarc Zyngier static inline void __iomem *gic_dist_base(struct irq_data *d) 72021f6537SMarc Zyngier { 73021f6537SMarc Zyngier if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */ 74021f6537SMarc Zyngier return gic_data_rdist_sgi_base(); 75021f6537SMarc Zyngier 76021f6537SMarc Zyngier if (d->hwirq <= 1023) /* SPI -> dist_base */ 77021f6537SMarc Zyngier return gic_data.dist_base; 78021f6537SMarc Zyngier 79021f6537SMarc Zyngier return NULL; 80021f6537SMarc Zyngier } 81021f6537SMarc Zyngier 82021f6537SMarc Zyngier static void gic_do_wait_for_rwp(void __iomem *base) 83021f6537SMarc Zyngier { 84021f6537SMarc Zyngier u32 count = 1000000; /* 1s! */ 85021f6537SMarc Zyngier 86021f6537SMarc Zyngier while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) { 87021f6537SMarc Zyngier count--; 88021f6537SMarc Zyngier if (!count) { 89021f6537SMarc Zyngier pr_err_ratelimited("RWP timeout, gone fishing\n"); 90021f6537SMarc Zyngier return; 91021f6537SMarc Zyngier } 92021f6537SMarc Zyngier cpu_relax(); 93021f6537SMarc Zyngier udelay(1); 94021f6537SMarc Zyngier }; 95021f6537SMarc Zyngier } 96021f6537SMarc Zyngier 97021f6537SMarc Zyngier /* Wait for completion of a distributor change */ 98021f6537SMarc Zyngier static void gic_dist_wait_for_rwp(void) 99021f6537SMarc Zyngier { 100021f6537SMarc Zyngier gic_do_wait_for_rwp(gic_data.dist_base); 101021f6537SMarc Zyngier } 102021f6537SMarc Zyngier 103021f6537SMarc Zyngier /* Wait for completion of a redistributor change */ 104021f6537SMarc Zyngier static void gic_redist_wait_for_rwp(void) 105021f6537SMarc Zyngier { 106021f6537SMarc Zyngier gic_do_wait_for_rwp(gic_data_rdist_rd_base()); 107021f6537SMarc Zyngier } 108021f6537SMarc Zyngier 109021f6537SMarc Zyngier /* Low level accessors */ 110c44e9d77SMark Brown static u64 __maybe_unused gic_read_iar(void) 111021f6537SMarc Zyngier { 112021f6537SMarc Zyngier u64 irqstat; 113021f6537SMarc Zyngier 11472c58395SCatalin Marinas asm volatile("mrs_s %0, " __stringify(ICC_IAR1_EL1) : "=r" (irqstat)); 115021f6537SMarc Zyngier return irqstat; 116021f6537SMarc Zyngier } 117021f6537SMarc Zyngier 118c44e9d77SMark Brown static void __maybe_unused gic_write_pmr(u64 val) 119021f6537SMarc Zyngier { 12072c58395SCatalin Marinas asm volatile("msr_s " __stringify(ICC_PMR_EL1) ", %0" : : "r" (val)); 121021f6537SMarc Zyngier } 122021f6537SMarc Zyngier 123c44e9d77SMark Brown static void __maybe_unused gic_write_ctlr(u64 val) 124021f6537SMarc Zyngier { 12572c58395SCatalin Marinas asm volatile("msr_s " __stringify(ICC_CTLR_EL1) ", %0" : : "r" (val)); 126021f6537SMarc Zyngier isb(); 127021f6537SMarc Zyngier } 128021f6537SMarc Zyngier 129c44e9d77SMark Brown static void __maybe_unused gic_write_grpen1(u64 val) 130021f6537SMarc Zyngier { 13172c58395SCatalin Marinas asm volatile("msr_s " __stringify(ICC_GRPEN1_EL1) ", %0" : : "r" (val)); 132021f6537SMarc Zyngier isb(); 133021f6537SMarc Zyngier } 134021f6537SMarc Zyngier 135c44e9d77SMark Brown static void __maybe_unused gic_write_sgi1r(u64 val) 136021f6537SMarc Zyngier { 13772c58395SCatalin Marinas asm volatile("msr_s " __stringify(ICC_SGI1R_EL1) ", %0" : : "r" (val)); 138021f6537SMarc Zyngier } 139021f6537SMarc Zyngier 140021f6537SMarc Zyngier static void gic_enable_sre(void) 141021f6537SMarc Zyngier { 142021f6537SMarc Zyngier u64 val; 143021f6537SMarc Zyngier 14472c58395SCatalin Marinas asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val)); 145021f6537SMarc Zyngier val |= ICC_SRE_EL1_SRE; 14672c58395SCatalin Marinas asm volatile("msr_s " __stringify(ICC_SRE_EL1) ", %0" : : "r" (val)); 147021f6537SMarc Zyngier isb(); 148021f6537SMarc Zyngier 149021f6537SMarc Zyngier /* 150021f6537SMarc Zyngier * Need to check that the SRE bit has actually been set. If 151021f6537SMarc Zyngier * not, it means that SRE is disabled at EL2. We're going to 152021f6537SMarc Zyngier * die painfully, and there is nothing we can do about it. 153021f6537SMarc Zyngier * 154021f6537SMarc Zyngier * Kindly inform the luser. 155021f6537SMarc Zyngier */ 15672c58395SCatalin Marinas asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val)); 157021f6537SMarc Zyngier if (!(val & ICC_SRE_EL1_SRE)) 158021f6537SMarc Zyngier pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n"); 159021f6537SMarc Zyngier } 160021f6537SMarc Zyngier 161a2c22510SSudeep Holla static void gic_enable_redist(bool enable) 162021f6537SMarc Zyngier { 163021f6537SMarc Zyngier void __iomem *rbase; 164021f6537SMarc Zyngier u32 count = 1000000; /* 1s! */ 165021f6537SMarc Zyngier u32 val; 166021f6537SMarc Zyngier 167021f6537SMarc Zyngier rbase = gic_data_rdist_rd_base(); 168021f6537SMarc Zyngier 169021f6537SMarc Zyngier val = readl_relaxed(rbase + GICR_WAKER); 170a2c22510SSudeep Holla if (enable) 171a2c22510SSudeep Holla /* Wake up this CPU redistributor */ 172021f6537SMarc Zyngier val &= ~GICR_WAKER_ProcessorSleep; 173a2c22510SSudeep Holla else 174a2c22510SSudeep Holla val |= GICR_WAKER_ProcessorSleep; 175021f6537SMarc Zyngier writel_relaxed(val, rbase + GICR_WAKER); 176021f6537SMarc Zyngier 177a2c22510SSudeep Holla if (!enable) { /* Check that GICR_WAKER is writeable */ 178a2c22510SSudeep Holla val = readl_relaxed(rbase + GICR_WAKER); 179a2c22510SSudeep Holla if (!(val & GICR_WAKER_ProcessorSleep)) 180a2c22510SSudeep Holla return; /* No PM support in this redistributor */ 181021f6537SMarc Zyngier } 182a2c22510SSudeep Holla 183a2c22510SSudeep Holla while (count--) { 184a2c22510SSudeep Holla val = readl_relaxed(rbase + GICR_WAKER); 185a2c22510SSudeep Holla if (enable ^ (val & GICR_WAKER_ChildrenAsleep)) 186a2c22510SSudeep Holla break; 187021f6537SMarc Zyngier cpu_relax(); 188021f6537SMarc Zyngier udelay(1); 189021f6537SMarc Zyngier }; 190a2c22510SSudeep Holla if (!count) 191a2c22510SSudeep Holla pr_err_ratelimited("redistributor failed to %s...\n", 192a2c22510SSudeep Holla enable ? "wakeup" : "sleep"); 193021f6537SMarc Zyngier } 194021f6537SMarc Zyngier 195021f6537SMarc Zyngier /* 196021f6537SMarc Zyngier * Routines to disable, enable, EOI and route interrupts 197021f6537SMarc Zyngier */ 198b594c6e2SMarc Zyngier static int gic_peek_irq(struct irq_data *d, u32 offset) 199b594c6e2SMarc Zyngier { 200b594c6e2SMarc Zyngier u32 mask = 1 << (gic_irq(d) % 32); 201b594c6e2SMarc Zyngier void __iomem *base; 202b594c6e2SMarc Zyngier 203b594c6e2SMarc Zyngier if (gic_irq_in_rdist(d)) 204b594c6e2SMarc Zyngier base = gic_data_rdist_sgi_base(); 205b594c6e2SMarc Zyngier else 206b594c6e2SMarc Zyngier base = gic_data.dist_base; 207b594c6e2SMarc Zyngier 208b594c6e2SMarc Zyngier return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask); 209b594c6e2SMarc Zyngier } 210b594c6e2SMarc Zyngier 211021f6537SMarc Zyngier static void gic_poke_irq(struct irq_data *d, u32 offset) 212021f6537SMarc Zyngier { 213021f6537SMarc Zyngier u32 mask = 1 << (gic_irq(d) % 32); 214021f6537SMarc Zyngier void (*rwp_wait)(void); 215021f6537SMarc Zyngier void __iomem *base; 216021f6537SMarc Zyngier 217021f6537SMarc Zyngier if (gic_irq_in_rdist(d)) { 218021f6537SMarc Zyngier base = gic_data_rdist_sgi_base(); 219021f6537SMarc Zyngier rwp_wait = gic_redist_wait_for_rwp; 220021f6537SMarc Zyngier } else { 221021f6537SMarc Zyngier base = gic_data.dist_base; 222021f6537SMarc Zyngier rwp_wait = gic_dist_wait_for_rwp; 223021f6537SMarc Zyngier } 224021f6537SMarc Zyngier 225021f6537SMarc Zyngier writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4); 226021f6537SMarc Zyngier rwp_wait(); 227021f6537SMarc Zyngier } 228021f6537SMarc Zyngier 229021f6537SMarc Zyngier static void gic_mask_irq(struct irq_data *d) 230021f6537SMarc Zyngier { 231021f6537SMarc Zyngier gic_poke_irq(d, GICD_ICENABLER); 232021f6537SMarc Zyngier } 233021f6537SMarc Zyngier 234021f6537SMarc Zyngier static void gic_unmask_irq(struct irq_data *d) 235021f6537SMarc Zyngier { 236021f6537SMarc Zyngier gic_poke_irq(d, GICD_ISENABLER); 237021f6537SMarc Zyngier } 238021f6537SMarc Zyngier 239b594c6e2SMarc Zyngier static int gic_irq_set_irqchip_state(struct irq_data *d, 240b594c6e2SMarc Zyngier enum irqchip_irq_state which, bool val) 241b594c6e2SMarc Zyngier { 242b594c6e2SMarc Zyngier u32 reg; 243b594c6e2SMarc Zyngier 244b594c6e2SMarc Zyngier if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */ 245b594c6e2SMarc Zyngier return -EINVAL; 246b594c6e2SMarc Zyngier 247b594c6e2SMarc Zyngier switch (which) { 248b594c6e2SMarc Zyngier case IRQCHIP_STATE_PENDING: 249b594c6e2SMarc Zyngier reg = val ? GICD_ISPENDR : GICD_ICPENDR; 250b594c6e2SMarc Zyngier break; 251b594c6e2SMarc Zyngier 252b594c6e2SMarc Zyngier case IRQCHIP_STATE_ACTIVE: 253b594c6e2SMarc Zyngier reg = val ? GICD_ISACTIVER : GICD_ICACTIVER; 254b594c6e2SMarc Zyngier break; 255b594c6e2SMarc Zyngier 256b594c6e2SMarc Zyngier case IRQCHIP_STATE_MASKED: 257b594c6e2SMarc Zyngier reg = val ? GICD_ICENABLER : GICD_ISENABLER; 258b594c6e2SMarc Zyngier break; 259b594c6e2SMarc Zyngier 260b594c6e2SMarc Zyngier default: 261b594c6e2SMarc Zyngier return -EINVAL; 262b594c6e2SMarc Zyngier } 263b594c6e2SMarc Zyngier 264b594c6e2SMarc Zyngier gic_poke_irq(d, reg); 265b594c6e2SMarc Zyngier return 0; 266b594c6e2SMarc Zyngier } 267b594c6e2SMarc Zyngier 268b594c6e2SMarc Zyngier static int gic_irq_get_irqchip_state(struct irq_data *d, 269b594c6e2SMarc Zyngier enum irqchip_irq_state which, bool *val) 270b594c6e2SMarc Zyngier { 271b594c6e2SMarc Zyngier if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */ 272b594c6e2SMarc Zyngier return -EINVAL; 273b594c6e2SMarc Zyngier 274b594c6e2SMarc Zyngier switch (which) { 275b594c6e2SMarc Zyngier case IRQCHIP_STATE_PENDING: 276b594c6e2SMarc Zyngier *val = gic_peek_irq(d, GICD_ISPENDR); 277b594c6e2SMarc Zyngier break; 278b594c6e2SMarc Zyngier 279b594c6e2SMarc Zyngier case IRQCHIP_STATE_ACTIVE: 280b594c6e2SMarc Zyngier *val = gic_peek_irq(d, GICD_ISACTIVER); 281b594c6e2SMarc Zyngier break; 282b594c6e2SMarc Zyngier 283b594c6e2SMarc Zyngier case IRQCHIP_STATE_MASKED: 284b594c6e2SMarc Zyngier *val = !gic_peek_irq(d, GICD_ISENABLER); 285b594c6e2SMarc Zyngier break; 286b594c6e2SMarc Zyngier 287b594c6e2SMarc Zyngier default: 288b594c6e2SMarc Zyngier return -EINVAL; 289b594c6e2SMarc Zyngier } 290b594c6e2SMarc Zyngier 291b594c6e2SMarc Zyngier return 0; 292b594c6e2SMarc Zyngier } 293b594c6e2SMarc Zyngier 294021f6537SMarc Zyngier static void gic_eoi_irq(struct irq_data *d) 295021f6537SMarc Zyngier { 296021f6537SMarc Zyngier gic_write_eoir(gic_irq(d)); 297021f6537SMarc Zyngier } 298021f6537SMarc Zyngier 299021f6537SMarc Zyngier static int gic_set_type(struct irq_data *d, unsigned int type) 300021f6537SMarc Zyngier { 301021f6537SMarc Zyngier unsigned int irq = gic_irq(d); 302021f6537SMarc Zyngier void (*rwp_wait)(void); 303021f6537SMarc Zyngier void __iomem *base; 304021f6537SMarc Zyngier 305021f6537SMarc Zyngier /* Interrupt configuration for SGIs can't be changed */ 306021f6537SMarc Zyngier if (irq < 16) 307021f6537SMarc Zyngier return -EINVAL; 308021f6537SMarc Zyngier 309fb7e7debSLiviu Dudau /* SPIs have restrictions on the supported types */ 310fb7e7debSLiviu Dudau if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH && 311fb7e7debSLiviu Dudau type != IRQ_TYPE_EDGE_RISING) 312021f6537SMarc Zyngier return -EINVAL; 313021f6537SMarc Zyngier 314021f6537SMarc Zyngier if (gic_irq_in_rdist(d)) { 315021f6537SMarc Zyngier base = gic_data_rdist_sgi_base(); 316021f6537SMarc Zyngier rwp_wait = gic_redist_wait_for_rwp; 317021f6537SMarc Zyngier } else { 318021f6537SMarc Zyngier base = gic_data.dist_base; 319021f6537SMarc Zyngier rwp_wait = gic_dist_wait_for_rwp; 320021f6537SMarc Zyngier } 321021f6537SMarc Zyngier 322fb7e7debSLiviu Dudau return gic_configure_irq(irq, type, base, rwp_wait); 323021f6537SMarc Zyngier } 324021f6537SMarc Zyngier 325021f6537SMarc Zyngier static u64 gic_mpidr_to_affinity(u64 mpidr) 326021f6537SMarc Zyngier { 327021f6537SMarc Zyngier u64 aff; 328021f6537SMarc Zyngier 329021f6537SMarc Zyngier aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 | 330021f6537SMarc Zyngier MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 | 331021f6537SMarc Zyngier MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 | 332021f6537SMarc Zyngier MPIDR_AFFINITY_LEVEL(mpidr, 0)); 333021f6537SMarc Zyngier 334021f6537SMarc Zyngier return aff; 335021f6537SMarc Zyngier } 336021f6537SMarc Zyngier 337021f6537SMarc Zyngier static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs) 338021f6537SMarc Zyngier { 339021f6537SMarc Zyngier u64 irqnr; 340021f6537SMarc Zyngier 341021f6537SMarc Zyngier do { 342021f6537SMarc Zyngier irqnr = gic_read_iar(); 343021f6537SMarc Zyngier 344da33f31dSMarc Zyngier if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) { 345ebc6de00SMarc Zyngier int err; 346ebc6de00SMarc Zyngier err = handle_domain_irq(gic_data.domain, irqnr, regs); 347ebc6de00SMarc Zyngier if (err) { 348da33f31dSMarc Zyngier WARN_ONCE(true, "Unexpected interrupt received!\n"); 349021f6537SMarc Zyngier gic_write_eoir(irqnr); 350021f6537SMarc Zyngier } 351ebc6de00SMarc Zyngier continue; 352ebc6de00SMarc Zyngier } 353021f6537SMarc Zyngier if (irqnr < 16) { 354021f6537SMarc Zyngier gic_write_eoir(irqnr); 355021f6537SMarc Zyngier #ifdef CONFIG_SMP 356021f6537SMarc Zyngier handle_IPI(irqnr, regs); 357021f6537SMarc Zyngier #else 358021f6537SMarc Zyngier WARN_ONCE(true, "Unexpected SGI received!\n"); 359021f6537SMarc Zyngier #endif 360021f6537SMarc Zyngier continue; 361021f6537SMarc Zyngier } 362021f6537SMarc Zyngier } while (irqnr != ICC_IAR1_EL1_SPURIOUS); 363021f6537SMarc Zyngier } 364021f6537SMarc Zyngier 365021f6537SMarc Zyngier static void __init gic_dist_init(void) 366021f6537SMarc Zyngier { 367021f6537SMarc Zyngier unsigned int i; 368021f6537SMarc Zyngier u64 affinity; 369021f6537SMarc Zyngier void __iomem *base = gic_data.dist_base; 370021f6537SMarc Zyngier 371021f6537SMarc Zyngier /* Disable the distributor */ 372021f6537SMarc Zyngier writel_relaxed(0, base + GICD_CTLR); 373021f6537SMarc Zyngier gic_dist_wait_for_rwp(); 374021f6537SMarc Zyngier 375021f6537SMarc Zyngier gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp); 376021f6537SMarc Zyngier 377021f6537SMarc Zyngier /* Enable distributor with ARE, Group1 */ 378021f6537SMarc Zyngier writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1, 379021f6537SMarc Zyngier base + GICD_CTLR); 380021f6537SMarc Zyngier 381021f6537SMarc Zyngier /* 382021f6537SMarc Zyngier * Set all global interrupts to the boot CPU only. ARE must be 383021f6537SMarc Zyngier * enabled. 384021f6537SMarc Zyngier */ 385021f6537SMarc Zyngier affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id())); 386021f6537SMarc Zyngier for (i = 32; i < gic_data.irq_nr; i++) 387021f6537SMarc Zyngier writeq_relaxed(affinity, base + GICD_IROUTER + i * 8); 388021f6537SMarc Zyngier } 389021f6537SMarc Zyngier 390021f6537SMarc Zyngier static int gic_populate_rdist(void) 391021f6537SMarc Zyngier { 392021f6537SMarc Zyngier u64 mpidr = cpu_logical_map(smp_processor_id()); 393021f6537SMarc Zyngier u64 typer; 394021f6537SMarc Zyngier u32 aff; 395021f6537SMarc Zyngier int i; 396021f6537SMarc Zyngier 397021f6537SMarc Zyngier /* 398021f6537SMarc Zyngier * Convert affinity to a 32bit value that can be matched to 399021f6537SMarc Zyngier * GICR_TYPER bits [63:32]. 400021f6537SMarc Zyngier */ 401021f6537SMarc Zyngier aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 | 402021f6537SMarc Zyngier MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 | 403021f6537SMarc Zyngier MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 | 404021f6537SMarc Zyngier MPIDR_AFFINITY_LEVEL(mpidr, 0)); 405021f6537SMarc Zyngier 406f5c1434cSMarc Zyngier for (i = 0; i < gic_data.nr_redist_regions; i++) { 407f5c1434cSMarc Zyngier void __iomem *ptr = gic_data.redist_regions[i].redist_base; 408021f6537SMarc Zyngier u32 reg; 409021f6537SMarc Zyngier 410021f6537SMarc Zyngier reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK; 411021f6537SMarc Zyngier if (reg != GIC_PIDR2_ARCH_GICv3 && 412021f6537SMarc Zyngier reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */ 413021f6537SMarc Zyngier pr_warn("No redistributor present @%p\n", ptr); 414021f6537SMarc Zyngier break; 415021f6537SMarc Zyngier } 416021f6537SMarc Zyngier 417021f6537SMarc Zyngier do { 418021f6537SMarc Zyngier typer = readq_relaxed(ptr + GICR_TYPER); 419021f6537SMarc Zyngier if ((typer >> 32) == aff) { 420f5c1434cSMarc Zyngier u64 offset = ptr - gic_data.redist_regions[i].redist_base; 421021f6537SMarc Zyngier gic_data_rdist_rd_base() = ptr; 422f5c1434cSMarc Zyngier gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset; 423f5c1434cSMarc Zyngier pr_info("CPU%d: found redistributor %llx region %d:%pa\n", 424021f6537SMarc Zyngier smp_processor_id(), 425f5c1434cSMarc Zyngier (unsigned long long)mpidr, 426f5c1434cSMarc Zyngier i, &gic_data_rdist()->phys_base); 427021f6537SMarc Zyngier return 0; 428021f6537SMarc Zyngier } 429021f6537SMarc Zyngier 430021f6537SMarc Zyngier if (gic_data.redist_stride) { 431021f6537SMarc Zyngier ptr += gic_data.redist_stride; 432021f6537SMarc Zyngier } else { 433021f6537SMarc Zyngier ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */ 434021f6537SMarc Zyngier if (typer & GICR_TYPER_VLPIS) 435021f6537SMarc Zyngier ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */ 436021f6537SMarc Zyngier } 437021f6537SMarc Zyngier } while (!(typer & GICR_TYPER_LAST)); 438021f6537SMarc Zyngier } 439021f6537SMarc Zyngier 440021f6537SMarc Zyngier /* We couldn't even deal with ourselves... */ 441021f6537SMarc Zyngier WARN(true, "CPU%d: mpidr %llx has no re-distributor!\n", 442021f6537SMarc Zyngier smp_processor_id(), (unsigned long long)mpidr); 443021f6537SMarc Zyngier return -ENODEV; 444021f6537SMarc Zyngier } 445021f6537SMarc Zyngier 4463708d52fSSudeep Holla static void gic_cpu_sys_reg_init(void) 447021f6537SMarc Zyngier { 448021f6537SMarc Zyngier /* Enable system registers */ 449021f6537SMarc Zyngier gic_enable_sre(); 450021f6537SMarc Zyngier 451021f6537SMarc Zyngier /* Set priority mask register */ 452021f6537SMarc Zyngier gic_write_pmr(DEFAULT_PMR_VALUE); 453021f6537SMarc Zyngier 454021f6537SMarc Zyngier /* EOI deactivates interrupt too (mode 0) */ 455021f6537SMarc Zyngier gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir); 456021f6537SMarc Zyngier 457021f6537SMarc Zyngier /* ... and let's hit the road... */ 458021f6537SMarc Zyngier gic_write_grpen1(1); 459021f6537SMarc Zyngier } 460021f6537SMarc Zyngier 461da33f31dSMarc Zyngier static int gic_dist_supports_lpis(void) 462da33f31dSMarc Zyngier { 463da33f31dSMarc Zyngier return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS); 464da33f31dSMarc Zyngier } 465da33f31dSMarc Zyngier 466021f6537SMarc Zyngier static void gic_cpu_init(void) 467021f6537SMarc Zyngier { 468021f6537SMarc Zyngier void __iomem *rbase; 469021f6537SMarc Zyngier 470021f6537SMarc Zyngier /* Register ourselves with the rest of the world */ 471021f6537SMarc Zyngier if (gic_populate_rdist()) 472021f6537SMarc Zyngier return; 473021f6537SMarc Zyngier 474a2c22510SSudeep Holla gic_enable_redist(true); 475021f6537SMarc Zyngier 476021f6537SMarc Zyngier rbase = gic_data_rdist_sgi_base(); 477021f6537SMarc Zyngier 478021f6537SMarc Zyngier gic_cpu_config(rbase, gic_redist_wait_for_rwp); 479021f6537SMarc Zyngier 480da33f31dSMarc Zyngier /* Give LPIs a spin */ 481da33f31dSMarc Zyngier if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis()) 482da33f31dSMarc Zyngier its_cpu_init(); 483da33f31dSMarc Zyngier 4843708d52fSSudeep Holla /* initialise system registers */ 4853708d52fSSudeep Holla gic_cpu_sys_reg_init(); 486021f6537SMarc Zyngier } 487021f6537SMarc Zyngier 488021f6537SMarc Zyngier #ifdef CONFIG_SMP 489021f6537SMarc Zyngier static int gic_secondary_init(struct notifier_block *nfb, 490021f6537SMarc Zyngier unsigned long action, void *hcpu) 491021f6537SMarc Zyngier { 492021f6537SMarc Zyngier if (action == CPU_STARTING || action == CPU_STARTING_FROZEN) 493021f6537SMarc Zyngier gic_cpu_init(); 494021f6537SMarc Zyngier return NOTIFY_OK; 495021f6537SMarc Zyngier } 496021f6537SMarc Zyngier 497021f6537SMarc Zyngier /* 498021f6537SMarc Zyngier * Notifier for enabling the GIC CPU interface. Set an arbitrarily high 499021f6537SMarc Zyngier * priority because the GIC needs to be up before the ARM generic timers. 500021f6537SMarc Zyngier */ 501021f6537SMarc Zyngier static struct notifier_block gic_cpu_notifier = { 502021f6537SMarc Zyngier .notifier_call = gic_secondary_init, 503021f6537SMarc Zyngier .priority = 100, 504021f6537SMarc Zyngier }; 505021f6537SMarc Zyngier 506021f6537SMarc Zyngier static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask, 507021f6537SMarc Zyngier u64 cluster_id) 508021f6537SMarc Zyngier { 509021f6537SMarc Zyngier int cpu = *base_cpu; 510021f6537SMarc Zyngier u64 mpidr = cpu_logical_map(cpu); 511021f6537SMarc Zyngier u16 tlist = 0; 512021f6537SMarc Zyngier 513021f6537SMarc Zyngier while (cpu < nr_cpu_ids) { 514021f6537SMarc Zyngier /* 515021f6537SMarc Zyngier * If we ever get a cluster of more than 16 CPUs, just 516021f6537SMarc Zyngier * scream and skip that CPU. 517021f6537SMarc Zyngier */ 518021f6537SMarc Zyngier if (WARN_ON((mpidr & 0xff) >= 16)) 519021f6537SMarc Zyngier goto out; 520021f6537SMarc Zyngier 521021f6537SMarc Zyngier tlist |= 1 << (mpidr & 0xf); 522021f6537SMarc Zyngier 523021f6537SMarc Zyngier cpu = cpumask_next(cpu, mask); 524614be385SVladimir Murzin if (cpu >= nr_cpu_ids) 525021f6537SMarc Zyngier goto out; 526021f6537SMarc Zyngier 527021f6537SMarc Zyngier mpidr = cpu_logical_map(cpu); 528021f6537SMarc Zyngier 529021f6537SMarc Zyngier if (cluster_id != (mpidr & ~0xffUL)) { 530021f6537SMarc Zyngier cpu--; 531021f6537SMarc Zyngier goto out; 532021f6537SMarc Zyngier } 533021f6537SMarc Zyngier } 534021f6537SMarc Zyngier out: 535021f6537SMarc Zyngier *base_cpu = cpu; 536021f6537SMarc Zyngier return tlist; 537021f6537SMarc Zyngier } 538021f6537SMarc Zyngier 5397e580278SAndre Przywara #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \ 5407e580278SAndre Przywara (MPIDR_AFFINITY_LEVEL(cluster_id, level) \ 5417e580278SAndre Przywara << ICC_SGI1R_AFFINITY_## level ##_SHIFT) 5427e580278SAndre Przywara 543021f6537SMarc Zyngier static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq) 544021f6537SMarc Zyngier { 545021f6537SMarc Zyngier u64 val; 546021f6537SMarc Zyngier 5477e580278SAndre Przywara val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) | 5487e580278SAndre Przywara MPIDR_TO_SGI_AFFINITY(cluster_id, 2) | 5497e580278SAndre Przywara irq << ICC_SGI1R_SGI_ID_SHIFT | 5507e580278SAndre Przywara MPIDR_TO_SGI_AFFINITY(cluster_id, 1) | 5517e580278SAndre Przywara tlist << ICC_SGI1R_TARGET_LIST_SHIFT); 552021f6537SMarc Zyngier 553021f6537SMarc Zyngier pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val); 554021f6537SMarc Zyngier gic_write_sgi1r(val); 555021f6537SMarc Zyngier } 556021f6537SMarc Zyngier 557021f6537SMarc Zyngier static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq) 558021f6537SMarc Zyngier { 559021f6537SMarc Zyngier int cpu; 560021f6537SMarc Zyngier 561021f6537SMarc Zyngier if (WARN_ON(irq >= 16)) 562021f6537SMarc Zyngier return; 563021f6537SMarc Zyngier 564021f6537SMarc Zyngier /* 565021f6537SMarc Zyngier * Ensure that stores to Normal memory are visible to the 566021f6537SMarc Zyngier * other CPUs before issuing the IPI. 567021f6537SMarc Zyngier */ 568021f6537SMarc Zyngier smp_wmb(); 569021f6537SMarc Zyngier 570f9b531feSRusty Russell for_each_cpu(cpu, mask) { 571021f6537SMarc Zyngier u64 cluster_id = cpu_logical_map(cpu) & ~0xffUL; 572021f6537SMarc Zyngier u16 tlist; 573021f6537SMarc Zyngier 574021f6537SMarc Zyngier tlist = gic_compute_target_list(&cpu, mask, cluster_id); 575021f6537SMarc Zyngier gic_send_sgi(cluster_id, tlist, irq); 576021f6537SMarc Zyngier } 577021f6537SMarc Zyngier 578021f6537SMarc Zyngier /* Force the above writes to ICC_SGI1R_EL1 to be executed */ 579021f6537SMarc Zyngier isb(); 580021f6537SMarc Zyngier } 581021f6537SMarc Zyngier 582021f6537SMarc Zyngier static void gic_smp_init(void) 583021f6537SMarc Zyngier { 584021f6537SMarc Zyngier set_smp_cross_call(gic_raise_softirq); 585021f6537SMarc Zyngier register_cpu_notifier(&gic_cpu_notifier); 586021f6537SMarc Zyngier } 587021f6537SMarc Zyngier 588021f6537SMarc Zyngier static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val, 589021f6537SMarc Zyngier bool force) 590021f6537SMarc Zyngier { 591021f6537SMarc Zyngier unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask); 592021f6537SMarc Zyngier void __iomem *reg; 593021f6537SMarc Zyngier int enabled; 594021f6537SMarc Zyngier u64 val; 595021f6537SMarc Zyngier 596021f6537SMarc Zyngier if (gic_irq_in_rdist(d)) 597021f6537SMarc Zyngier return -EINVAL; 598021f6537SMarc Zyngier 599021f6537SMarc Zyngier /* If interrupt was enabled, disable it first */ 600021f6537SMarc Zyngier enabled = gic_peek_irq(d, GICD_ISENABLER); 601021f6537SMarc Zyngier if (enabled) 602021f6537SMarc Zyngier gic_mask_irq(d); 603021f6537SMarc Zyngier 604021f6537SMarc Zyngier reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8); 605021f6537SMarc Zyngier val = gic_mpidr_to_affinity(cpu_logical_map(cpu)); 606021f6537SMarc Zyngier 607021f6537SMarc Zyngier writeq_relaxed(val, reg); 608021f6537SMarc Zyngier 609021f6537SMarc Zyngier /* 610021f6537SMarc Zyngier * If the interrupt was enabled, enabled it again. Otherwise, 611021f6537SMarc Zyngier * just wait for the distributor to have digested our changes. 612021f6537SMarc Zyngier */ 613021f6537SMarc Zyngier if (enabled) 614021f6537SMarc Zyngier gic_unmask_irq(d); 615021f6537SMarc Zyngier else 616021f6537SMarc Zyngier gic_dist_wait_for_rwp(); 617021f6537SMarc Zyngier 618021f6537SMarc Zyngier return IRQ_SET_MASK_OK; 619021f6537SMarc Zyngier } 620021f6537SMarc Zyngier #else 621021f6537SMarc Zyngier #define gic_set_affinity NULL 622021f6537SMarc Zyngier #define gic_smp_init() do { } while(0) 623021f6537SMarc Zyngier #endif 624021f6537SMarc Zyngier 6253708d52fSSudeep Holla #ifdef CONFIG_CPU_PM 6263708d52fSSudeep Holla static int gic_cpu_pm_notifier(struct notifier_block *self, 6273708d52fSSudeep Holla unsigned long cmd, void *v) 6283708d52fSSudeep Holla { 6293708d52fSSudeep Holla if (cmd == CPU_PM_EXIT) { 6303708d52fSSudeep Holla gic_enable_redist(true); 6313708d52fSSudeep Holla gic_cpu_sys_reg_init(); 6323708d52fSSudeep Holla } else if (cmd == CPU_PM_ENTER) { 6333708d52fSSudeep Holla gic_write_grpen1(0); 6343708d52fSSudeep Holla gic_enable_redist(false); 6353708d52fSSudeep Holla } 6363708d52fSSudeep Holla return NOTIFY_OK; 6373708d52fSSudeep Holla } 6383708d52fSSudeep Holla 6393708d52fSSudeep Holla static struct notifier_block gic_cpu_pm_notifier_block = { 6403708d52fSSudeep Holla .notifier_call = gic_cpu_pm_notifier, 6413708d52fSSudeep Holla }; 6423708d52fSSudeep Holla 6433708d52fSSudeep Holla static void gic_cpu_pm_init(void) 6443708d52fSSudeep Holla { 6453708d52fSSudeep Holla cpu_pm_register_notifier(&gic_cpu_pm_notifier_block); 6463708d52fSSudeep Holla } 6473708d52fSSudeep Holla 6483708d52fSSudeep Holla #else 6493708d52fSSudeep Holla static inline void gic_cpu_pm_init(void) { } 6503708d52fSSudeep Holla #endif /* CONFIG_CPU_PM */ 6513708d52fSSudeep Holla 652021f6537SMarc Zyngier static struct irq_chip gic_chip = { 653021f6537SMarc Zyngier .name = "GICv3", 654021f6537SMarc Zyngier .irq_mask = gic_mask_irq, 655021f6537SMarc Zyngier .irq_unmask = gic_unmask_irq, 656021f6537SMarc Zyngier .irq_eoi = gic_eoi_irq, 657021f6537SMarc Zyngier .irq_set_type = gic_set_type, 658021f6537SMarc Zyngier .irq_set_affinity = gic_set_affinity, 659b594c6e2SMarc Zyngier .irq_get_irqchip_state = gic_irq_get_irqchip_state, 660b594c6e2SMarc Zyngier .irq_set_irqchip_state = gic_irq_set_irqchip_state, 66155963c9fSSudeep Holla .flags = IRQCHIP_SET_TYPE_MASKED, 662021f6537SMarc Zyngier }; 663021f6537SMarc Zyngier 664da33f31dSMarc Zyngier #define GIC_ID_NR (1U << gic_data.rdists.id_bits) 665da33f31dSMarc Zyngier 666021f6537SMarc Zyngier static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq, 667021f6537SMarc Zyngier irq_hw_number_t hw) 668021f6537SMarc Zyngier { 669021f6537SMarc Zyngier /* SGIs are private to the core kernel */ 670021f6537SMarc Zyngier if (hw < 16) 671021f6537SMarc Zyngier return -EPERM; 672da33f31dSMarc Zyngier /* Nothing here */ 673da33f31dSMarc Zyngier if (hw >= gic_data.irq_nr && hw < 8192) 674da33f31dSMarc Zyngier return -EPERM; 675da33f31dSMarc Zyngier /* Off limits */ 676da33f31dSMarc Zyngier if (hw >= GIC_ID_NR) 677da33f31dSMarc Zyngier return -EPERM; 678da33f31dSMarc Zyngier 679021f6537SMarc Zyngier /* PPIs */ 680021f6537SMarc Zyngier if (hw < 32) { 681021f6537SMarc Zyngier irq_set_percpu_devid(irq); 682443acc4fSMarc Zyngier irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data, 683443acc4fSMarc Zyngier handle_percpu_devid_irq, NULL, NULL); 684021f6537SMarc Zyngier set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN); 685021f6537SMarc Zyngier } 686021f6537SMarc Zyngier /* SPIs */ 687021f6537SMarc Zyngier if (hw >= 32 && hw < gic_data.irq_nr) { 688443acc4fSMarc Zyngier irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data, 689443acc4fSMarc Zyngier handle_fasteoi_irq, NULL, NULL); 690021f6537SMarc Zyngier set_irq_flags(irq, IRQF_VALID | IRQF_PROBE); 691021f6537SMarc Zyngier } 692da33f31dSMarc Zyngier /* LPIs */ 693da33f31dSMarc Zyngier if (hw >= 8192 && hw < GIC_ID_NR) { 694da33f31dSMarc Zyngier if (!gic_dist_supports_lpis()) 695da33f31dSMarc Zyngier return -EPERM; 696da33f31dSMarc Zyngier irq_domain_set_info(d, irq, hw, &gic_chip, d->host_data, 697da33f31dSMarc Zyngier handle_fasteoi_irq, NULL, NULL); 698da33f31dSMarc Zyngier set_irq_flags(irq, IRQF_VALID); 699da33f31dSMarc Zyngier } 700da33f31dSMarc Zyngier 701021f6537SMarc Zyngier return 0; 702021f6537SMarc Zyngier } 703021f6537SMarc Zyngier 704021f6537SMarc Zyngier static int gic_irq_domain_xlate(struct irq_domain *d, 705021f6537SMarc Zyngier struct device_node *controller, 706021f6537SMarc Zyngier const u32 *intspec, unsigned int intsize, 707021f6537SMarc Zyngier unsigned long *out_hwirq, unsigned int *out_type) 708021f6537SMarc Zyngier { 709021f6537SMarc Zyngier if (d->of_node != controller) 710021f6537SMarc Zyngier return -EINVAL; 711021f6537SMarc Zyngier if (intsize < 3) 712021f6537SMarc Zyngier return -EINVAL; 713021f6537SMarc Zyngier 714021f6537SMarc Zyngier switch(intspec[0]) { 715021f6537SMarc Zyngier case 0: /* SPI */ 716021f6537SMarc Zyngier *out_hwirq = intspec[1] + 32; 717021f6537SMarc Zyngier break; 718021f6537SMarc Zyngier case 1: /* PPI */ 719021f6537SMarc Zyngier *out_hwirq = intspec[1] + 16; 720021f6537SMarc Zyngier break; 721da33f31dSMarc Zyngier case GIC_IRQ_TYPE_LPI: /* LPI */ 722da33f31dSMarc Zyngier *out_hwirq = intspec[1]; 723da33f31dSMarc Zyngier break; 724021f6537SMarc Zyngier default: 725021f6537SMarc Zyngier return -EINVAL; 726021f6537SMarc Zyngier } 727021f6537SMarc Zyngier 728021f6537SMarc Zyngier *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK; 729021f6537SMarc Zyngier return 0; 730021f6537SMarc Zyngier } 731021f6537SMarc Zyngier 732443acc4fSMarc Zyngier static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, 733443acc4fSMarc Zyngier unsigned int nr_irqs, void *arg) 734443acc4fSMarc Zyngier { 735443acc4fSMarc Zyngier int i, ret; 736443acc4fSMarc Zyngier irq_hw_number_t hwirq; 737443acc4fSMarc Zyngier unsigned int type = IRQ_TYPE_NONE; 738443acc4fSMarc Zyngier struct of_phandle_args *irq_data = arg; 739443acc4fSMarc Zyngier 740443acc4fSMarc Zyngier ret = gic_irq_domain_xlate(domain, irq_data->np, irq_data->args, 741443acc4fSMarc Zyngier irq_data->args_count, &hwirq, &type); 742443acc4fSMarc Zyngier if (ret) 743443acc4fSMarc Zyngier return ret; 744443acc4fSMarc Zyngier 745443acc4fSMarc Zyngier for (i = 0; i < nr_irqs; i++) 746443acc4fSMarc Zyngier gic_irq_domain_map(domain, virq + i, hwirq + i); 747443acc4fSMarc Zyngier 748443acc4fSMarc Zyngier return 0; 749443acc4fSMarc Zyngier } 750443acc4fSMarc Zyngier 751443acc4fSMarc Zyngier static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq, 752443acc4fSMarc Zyngier unsigned int nr_irqs) 753443acc4fSMarc Zyngier { 754443acc4fSMarc Zyngier int i; 755443acc4fSMarc Zyngier 756443acc4fSMarc Zyngier for (i = 0; i < nr_irqs; i++) { 757443acc4fSMarc Zyngier struct irq_data *d = irq_domain_get_irq_data(domain, virq + i); 758443acc4fSMarc Zyngier irq_set_handler(virq + i, NULL); 759443acc4fSMarc Zyngier irq_domain_reset_irq_data(d); 760443acc4fSMarc Zyngier } 761443acc4fSMarc Zyngier } 762443acc4fSMarc Zyngier 763021f6537SMarc Zyngier static const struct irq_domain_ops gic_irq_domain_ops = { 764021f6537SMarc Zyngier .xlate = gic_irq_domain_xlate, 765443acc4fSMarc Zyngier .alloc = gic_irq_domain_alloc, 766443acc4fSMarc Zyngier .free = gic_irq_domain_free, 767021f6537SMarc Zyngier }; 768021f6537SMarc Zyngier 769021f6537SMarc Zyngier static int __init gic_of_init(struct device_node *node, struct device_node *parent) 770021f6537SMarc Zyngier { 771021f6537SMarc Zyngier void __iomem *dist_base; 772f5c1434cSMarc Zyngier struct redist_region *rdist_regs; 773021f6537SMarc Zyngier u64 redist_stride; 774f5c1434cSMarc Zyngier u32 nr_redist_regions; 775f5c1434cSMarc Zyngier u32 typer; 776021f6537SMarc Zyngier u32 reg; 777021f6537SMarc Zyngier int gic_irqs; 778021f6537SMarc Zyngier int err; 779021f6537SMarc Zyngier int i; 780021f6537SMarc Zyngier 781021f6537SMarc Zyngier dist_base = of_iomap(node, 0); 782021f6537SMarc Zyngier if (!dist_base) { 783021f6537SMarc Zyngier pr_err("%s: unable to map gic dist registers\n", 784021f6537SMarc Zyngier node->full_name); 785021f6537SMarc Zyngier return -ENXIO; 786021f6537SMarc Zyngier } 787021f6537SMarc Zyngier 788021f6537SMarc Zyngier reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK; 789021f6537SMarc Zyngier if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4) { 790021f6537SMarc Zyngier pr_err("%s: no distributor detected, giving up\n", 791021f6537SMarc Zyngier node->full_name); 792021f6537SMarc Zyngier err = -ENODEV; 793021f6537SMarc Zyngier goto out_unmap_dist; 794021f6537SMarc Zyngier } 795021f6537SMarc Zyngier 796f5c1434cSMarc Zyngier if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions)) 797f5c1434cSMarc Zyngier nr_redist_regions = 1; 798021f6537SMarc Zyngier 799f5c1434cSMarc Zyngier rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL); 800f5c1434cSMarc Zyngier if (!rdist_regs) { 801021f6537SMarc Zyngier err = -ENOMEM; 802021f6537SMarc Zyngier goto out_unmap_dist; 803021f6537SMarc Zyngier } 804021f6537SMarc Zyngier 805f5c1434cSMarc Zyngier for (i = 0; i < nr_redist_regions; i++) { 806f5c1434cSMarc Zyngier struct resource res; 807f5c1434cSMarc Zyngier int ret; 808f5c1434cSMarc Zyngier 809f5c1434cSMarc Zyngier ret = of_address_to_resource(node, 1 + i, &res); 810f5c1434cSMarc Zyngier rdist_regs[i].redist_base = of_iomap(node, 1 + i); 811f5c1434cSMarc Zyngier if (ret || !rdist_regs[i].redist_base) { 812021f6537SMarc Zyngier pr_err("%s: couldn't map region %d\n", 813021f6537SMarc Zyngier node->full_name, i); 814021f6537SMarc Zyngier err = -ENODEV; 815021f6537SMarc Zyngier goto out_unmap_rdist; 816021f6537SMarc Zyngier } 817f5c1434cSMarc Zyngier rdist_regs[i].phys_base = res.start; 818021f6537SMarc Zyngier } 819021f6537SMarc Zyngier 820021f6537SMarc Zyngier if (of_property_read_u64(node, "redistributor-stride", &redist_stride)) 821021f6537SMarc Zyngier redist_stride = 0; 822021f6537SMarc Zyngier 823021f6537SMarc Zyngier gic_data.dist_base = dist_base; 824f5c1434cSMarc Zyngier gic_data.redist_regions = rdist_regs; 825f5c1434cSMarc Zyngier gic_data.nr_redist_regions = nr_redist_regions; 826021f6537SMarc Zyngier gic_data.redist_stride = redist_stride; 827021f6537SMarc Zyngier 828021f6537SMarc Zyngier /* 829021f6537SMarc Zyngier * Find out how many interrupts are supported. 830021f6537SMarc Zyngier * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI) 831021f6537SMarc Zyngier */ 832f5c1434cSMarc Zyngier typer = readl_relaxed(gic_data.dist_base + GICD_TYPER); 833f5c1434cSMarc Zyngier gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer); 834f5c1434cSMarc Zyngier gic_irqs = GICD_TYPER_IRQS(typer); 835021f6537SMarc Zyngier if (gic_irqs > 1020) 836021f6537SMarc Zyngier gic_irqs = 1020; 837021f6537SMarc Zyngier gic_data.irq_nr = gic_irqs; 838021f6537SMarc Zyngier 839021f6537SMarc Zyngier gic_data.domain = irq_domain_add_tree(node, &gic_irq_domain_ops, 840021f6537SMarc Zyngier &gic_data); 841f5c1434cSMarc Zyngier gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist)); 842021f6537SMarc Zyngier 843f5c1434cSMarc Zyngier if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) { 844021f6537SMarc Zyngier err = -ENOMEM; 845021f6537SMarc Zyngier goto out_free; 846021f6537SMarc Zyngier } 847021f6537SMarc Zyngier 848021f6537SMarc Zyngier set_handle_irq(gic_handle_irq); 849021f6537SMarc Zyngier 850da33f31dSMarc Zyngier if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis()) 851da33f31dSMarc Zyngier its_init(node, &gic_data.rdists, gic_data.domain); 852da33f31dSMarc Zyngier 853021f6537SMarc Zyngier gic_smp_init(); 854021f6537SMarc Zyngier gic_dist_init(); 855021f6537SMarc Zyngier gic_cpu_init(); 8563708d52fSSudeep Holla gic_cpu_pm_init(); 857021f6537SMarc Zyngier 858021f6537SMarc Zyngier return 0; 859021f6537SMarc Zyngier 860021f6537SMarc Zyngier out_free: 861021f6537SMarc Zyngier if (gic_data.domain) 862021f6537SMarc Zyngier irq_domain_remove(gic_data.domain); 863f5c1434cSMarc Zyngier free_percpu(gic_data.rdists.rdist); 864021f6537SMarc Zyngier out_unmap_rdist: 865f5c1434cSMarc Zyngier for (i = 0; i < nr_redist_regions; i++) 866f5c1434cSMarc Zyngier if (rdist_regs[i].redist_base) 867f5c1434cSMarc Zyngier iounmap(rdist_regs[i].redist_base); 868f5c1434cSMarc Zyngier kfree(rdist_regs); 869021f6537SMarc Zyngier out_unmap_dist: 870021f6537SMarc Zyngier iounmap(dist_base); 871021f6537SMarc Zyngier return err; 872021f6537SMarc Zyngier } 873021f6537SMarc Zyngier 874021f6537SMarc Zyngier IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init); 875