2 * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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.
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.
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/>.
18 #include <linux/cpu.h>
19 #include <linux/cpu_pm.h>
20 #include <linux/delay.h>
21 #include <linux/interrupt.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/percpu.h>
26 #include <linux/slab.h>
28 #include <linux/irqchip.h>
29 #include <linux/irqchip/arm-gic-v3.h>
30 #include <linux/irqchip/irq-partition-percpu.h>
32 #include <asm/cputype.h>
33 #include <asm/exception.h>
34 #include <asm/smp_plat.h>
37 #include "irq-gic-common.h"
39 struct redist_region {
40 void __iomem *redist_base;
41 phys_addr_t phys_base;
44 struct gic_chip_data {
45 struct fwnode_handle *fwnode;
46 void __iomem *dist_base;
47 struct redist_region *redist_regions;
49 struct irq_domain *domain;
51 u32 nr_redist_regions;
53 struct partition_desc *ppi_descs[16];
56 static struct gic_chip_data gic_data __read_mostly;
57 static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
59 #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
60 #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
61 #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
63 /* Our default, arbitrary priority value. Linux only uses one anyway. */
64 #define DEFAULT_PMR_VALUE 0xf0
66 static inline unsigned int gic_irq(struct irq_data *d)
71 static inline int gic_irq_in_rdist(struct irq_data *d)
73 return gic_irq(d) < 32;
76 static inline void __iomem *gic_dist_base(struct irq_data *d)
78 if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
79 return gic_data_rdist_sgi_base();
81 if (d->hwirq <= 1023) /* SPI -> dist_base */
82 return gic_data.dist_base;
87 static void gic_do_wait_for_rwp(void __iomem *base)
89 u32 count = 1000000; /* 1s! */
91 while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
94 pr_err_ratelimited("RWP timeout, gone fishing\n");
102 /* Wait for completion of a distributor change */
103 static void gic_dist_wait_for_rwp(void)
105 gic_do_wait_for_rwp(gic_data.dist_base);
108 /* Wait for completion of a redistributor change */
109 static void gic_redist_wait_for_rwp(void)
111 gic_do_wait_for_rwp(gic_data_rdist_rd_base());
115 static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx);
117 static u64 __maybe_unused gic_read_iar(void)
119 if (static_branch_unlikely(&is_cavium_thunderx))
120 return gic_read_iar_cavium_thunderx();
122 return gic_read_iar_common();
126 static void gic_enable_redist(bool enable)
129 u32 count = 1000000; /* 1s! */
132 rbase = gic_data_rdist_rd_base();
134 val = readl_relaxed(rbase + GICR_WAKER);
136 /* Wake up this CPU redistributor */
137 val &= ~GICR_WAKER_ProcessorSleep;
139 val |= GICR_WAKER_ProcessorSleep;
140 writel_relaxed(val, rbase + GICR_WAKER);
142 if (!enable) { /* Check that GICR_WAKER is writeable */
143 val = readl_relaxed(rbase + GICR_WAKER);
144 if (!(val & GICR_WAKER_ProcessorSleep))
145 return; /* No PM support in this redistributor */
149 val = readl_relaxed(rbase + GICR_WAKER);
150 if (enable ^ (val & GICR_WAKER_ChildrenAsleep))
156 pr_err_ratelimited("redistributor failed to %s...\n",
157 enable ? "wakeup" : "sleep");
161 * Routines to disable, enable, EOI and route interrupts
163 static int gic_peek_irq(struct irq_data *d, u32 offset)
165 u32 mask = 1 << (gic_irq(d) % 32);
168 if (gic_irq_in_rdist(d))
169 base = gic_data_rdist_sgi_base();
171 base = gic_data.dist_base;
173 return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
176 static void gic_poke_irq(struct irq_data *d, u32 offset)
178 u32 mask = 1 << (gic_irq(d) % 32);
179 void (*rwp_wait)(void);
182 if (gic_irq_in_rdist(d)) {
183 base = gic_data_rdist_sgi_base();
184 rwp_wait = gic_redist_wait_for_rwp;
186 base = gic_data.dist_base;
187 rwp_wait = gic_dist_wait_for_rwp;
190 writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
194 static void gic_mask_irq(struct irq_data *d)
196 gic_poke_irq(d, GICD_ICENABLER);
199 static void gic_eoimode1_mask_irq(struct irq_data *d)
203 * When masking a forwarded interrupt, make sure it is
204 * deactivated as well.
206 * This ensures that an interrupt that is getting
207 * disabled/masked will not get "stuck", because there is
208 * noone to deactivate it (guest is being terminated).
210 if (irqd_is_forwarded_to_vcpu(d))
211 gic_poke_irq(d, GICD_ICACTIVER);
214 static void gic_unmask_irq(struct irq_data *d)
216 gic_poke_irq(d, GICD_ISENABLER);
219 #ifdef CONFIG_ARCH_ROCKCHIP
220 static int gic_retrigger(struct irq_data *d)
222 gic_poke_irq(d, GICD_ISPENDR);
223 /* the genirq layer expects 0 if we can't retrigger in hardware */
228 static int gic_irq_set_irqchip_state(struct irq_data *d,
229 enum irqchip_irq_state which, bool val)
233 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
237 case IRQCHIP_STATE_PENDING:
238 reg = val ? GICD_ISPENDR : GICD_ICPENDR;
241 case IRQCHIP_STATE_ACTIVE:
242 reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
245 case IRQCHIP_STATE_MASKED:
246 reg = val ? GICD_ICENABLER : GICD_ISENABLER;
253 gic_poke_irq(d, reg);
257 static int gic_irq_get_irqchip_state(struct irq_data *d,
258 enum irqchip_irq_state which, bool *val)
260 if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
264 case IRQCHIP_STATE_PENDING:
265 *val = gic_peek_irq(d, GICD_ISPENDR);
268 case IRQCHIP_STATE_ACTIVE:
269 *val = gic_peek_irq(d, GICD_ISACTIVER);
272 case IRQCHIP_STATE_MASKED:
273 *val = !gic_peek_irq(d, GICD_ISENABLER);
283 static void gic_eoi_irq(struct irq_data *d)
285 gic_write_eoir(gic_irq(d));
288 static void gic_eoimode1_eoi_irq(struct irq_data *d)
291 * No need to deactivate an LPI, or an interrupt that
292 * is is getting forwarded to a vcpu.
294 if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
296 gic_write_dir(gic_irq(d));
299 static int gic_set_type(struct irq_data *d, unsigned int type)
301 unsigned int irq = gic_irq(d);
302 void (*rwp_wait)(void);
305 /* Interrupt configuration for SGIs can't be changed */
309 /* SPIs have restrictions on the supported types */
310 if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
311 type != IRQ_TYPE_EDGE_RISING)
314 if (gic_irq_in_rdist(d)) {
315 base = gic_data_rdist_sgi_base();
316 rwp_wait = gic_redist_wait_for_rwp;
318 base = gic_data.dist_base;
319 rwp_wait = gic_dist_wait_for_rwp;
322 return gic_configure_irq(irq, type, base, rwp_wait);
325 static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
328 irqd_set_forwarded_to_vcpu(d);
330 irqd_clr_forwarded_to_vcpu(d);
334 static u64 gic_mpidr_to_affinity(unsigned long mpidr)
338 aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
339 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
340 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
341 MPIDR_AFFINITY_LEVEL(mpidr, 0));
346 static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
351 irqnr = gic_read_iar();
353 if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
356 if (static_key_true(&supports_deactivate))
357 gic_write_eoir(irqnr);
359 err = handle_domain_irq(gic_data.domain, irqnr, regs);
361 WARN_ONCE(true, "Unexpected interrupt received!\n");
362 if (static_key_true(&supports_deactivate)) {
364 gic_write_dir(irqnr);
366 gic_write_eoir(irqnr);
372 gic_write_eoir(irqnr);
373 if (static_key_true(&supports_deactivate))
374 gic_write_dir(irqnr);
377 * Unlike GICv2, we don't need an smp_rmb() here.
378 * The control dependency from gic_read_iar to
379 * the ISB in gic_write_eoir is enough to ensure
380 * that any shared data read by handle_IPI will
381 * be read after the ACK.
383 handle_IPI(irqnr, regs);
385 WARN_ONCE(true, "Unexpected SGI received!\n");
389 } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
392 static void __init gic_dist_init(void)
396 void __iomem *base = gic_data.dist_base;
398 /* Disable the distributor */
399 writel_relaxed(0, base + GICD_CTLR);
400 gic_dist_wait_for_rwp();
403 * Configure SPIs as non-secure Group-1. This will only matter
404 * if the GIC only has a single security state. This will not
405 * do the right thing if the kernel is running in secure mode,
406 * but that's not the intended use case anyway.
408 for (i = 32; i < gic_data.irq_nr; i += 32)
409 writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
411 gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
413 /* Enable distributor with ARE, Group1 */
414 writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
418 * Set all global interrupts to the boot CPU only. ARE must be
421 affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
422 for (i = 32; i < gic_data.irq_nr; i++)
423 gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
426 static int gic_populate_rdist(void)
428 unsigned long mpidr = cpu_logical_map(smp_processor_id());
434 * Convert affinity to a 32bit value that can be matched to
435 * GICR_TYPER bits [63:32].
437 aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
438 MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
439 MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
440 MPIDR_AFFINITY_LEVEL(mpidr, 0));
442 for (i = 0; i < gic_data.nr_redist_regions; i++) {
443 void __iomem *ptr = gic_data.redist_regions[i].redist_base;
446 reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
447 if (reg != GIC_PIDR2_ARCH_GICv3 &&
448 reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
449 pr_warn("No redistributor present @%p\n", ptr);
454 typer = gic_read_typer(ptr + GICR_TYPER);
455 if ((typer >> 32) == aff) {
456 u64 offset = ptr - gic_data.redist_regions[i].redist_base;
457 gic_data_rdist_rd_base() = ptr;
458 gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
459 pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
460 smp_processor_id(), mpidr, i,
461 &gic_data_rdist()->phys_base);
465 if (gic_data.redist_stride) {
466 ptr += gic_data.redist_stride;
468 ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
469 if (typer & GICR_TYPER_VLPIS)
470 ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
472 } while (!(typer & GICR_TYPER_LAST));
475 /* We couldn't even deal with ourselves... */
476 WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
477 smp_processor_id(), mpidr);
481 static void gic_cpu_sys_reg_init(void)
484 * Need to check that the SRE bit has actually been set. If
485 * not, it means that SRE is disabled at EL2. We're going to
486 * die painfully, and there is nothing we can do about it.
488 * Kindly inform the luser.
490 if (!gic_enable_sre())
491 pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
493 /* Set priority mask register */
494 gic_write_pmr(DEFAULT_PMR_VALUE);
496 if (static_key_true(&supports_deactivate)) {
497 /* EOI drops priority only (mode 1) */
498 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
500 /* EOI deactivates interrupt too (mode 0) */
501 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
504 /* ... and let's hit the road... */
508 static int gic_dist_supports_lpis(void)
510 return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
513 static void gic_cpu_init(void)
517 /* Register ourselves with the rest of the world */
518 if (gic_populate_rdist())
521 gic_enable_redist(true);
523 rbase = gic_data_rdist_sgi_base();
525 /* Configure SGIs/PPIs as non-secure Group-1 */
526 writel_relaxed(~0, rbase + GICR_IGROUPR0);
528 gic_cpu_config(rbase, gic_redist_wait_for_rwp);
530 /* Give LPIs a spin */
531 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
534 /* initialise system registers */
535 gic_cpu_sys_reg_init();
539 static int gic_secondary_init(struct notifier_block *nfb,
540 unsigned long action, void *hcpu)
542 if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
548 * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
549 * priority because the GIC needs to be up before the ARM generic timers.
551 static struct notifier_block gic_cpu_notifier = {
552 .notifier_call = gic_secondary_init,
556 static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
557 unsigned long cluster_id)
559 int next_cpu, cpu = *base_cpu;
560 unsigned long mpidr = cpu_logical_map(cpu);
563 while (cpu < nr_cpu_ids) {
565 * If we ever get a cluster of more than 16 CPUs, just
566 * scream and skip that CPU.
568 if (WARN_ON((mpidr & 0xff) >= 16))
571 tlist |= 1 << (mpidr & 0xf);
573 next_cpu = cpumask_next(cpu, mask);
574 if (next_cpu >= nr_cpu_ids)
578 mpidr = cpu_logical_map(cpu);
580 if (cluster_id != (mpidr & ~0xffUL)) {
590 #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
591 (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
592 << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
594 static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
598 val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
599 MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
600 irq << ICC_SGI1R_SGI_ID_SHIFT |
601 MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
602 tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
604 pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
605 gic_write_sgi1r(val);
608 static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
612 if (WARN_ON(irq >= 16))
616 * Ensure that stores to Normal memory are visible to the
617 * other CPUs before issuing the IPI.
621 for_each_cpu(cpu, mask) {
622 unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
625 tlist = gic_compute_target_list(&cpu, mask, cluster_id);
626 gic_send_sgi(cluster_id, tlist, irq);
629 /* Force the above writes to ICC_SGI1R_EL1 to be executed */
633 static void gic_smp_init(void)
635 set_smp_cross_call(gic_raise_softirq);
636 register_cpu_notifier(&gic_cpu_notifier);
639 static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
642 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
647 if (gic_irq_in_rdist(d))
650 /* If interrupt was enabled, disable it first */
651 enabled = gic_peek_irq(d, GICD_ISENABLER);
655 reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
656 val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
658 gic_write_irouter(val, reg);
661 * If the interrupt was enabled, enabled it again. Otherwise,
662 * just wait for the distributor to have digested our changes.
667 gic_dist_wait_for_rwp();
669 return IRQ_SET_MASK_OK;
672 #define gic_set_affinity NULL
673 #define gic_smp_init() do { } while(0)
677 static int gic_cpu_pm_notifier(struct notifier_block *self,
678 unsigned long cmd, void *v)
680 if (cmd == CPU_PM_EXIT) {
681 gic_enable_redist(true);
682 gic_cpu_sys_reg_init();
683 } else if (cmd == CPU_PM_ENTER) {
685 gic_enable_redist(false);
690 static struct notifier_block gic_cpu_pm_notifier_block = {
691 .notifier_call = gic_cpu_pm_notifier,
694 static void gic_cpu_pm_init(void)
696 cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
700 static inline void gic_cpu_pm_init(void) { }
701 #endif /* CONFIG_CPU_PM */
703 static struct irq_chip gic_chip = {
705 .irq_mask = gic_mask_irq,
706 .irq_unmask = gic_unmask_irq,
707 .irq_eoi = gic_eoi_irq,
708 .irq_set_type = gic_set_type,
709 #ifdef CONFIG_ARCH_ROCKCHIP
710 .irq_retrigger = gic_retrigger,
712 .irq_set_affinity = gic_set_affinity,
713 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
714 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
715 .flags = IRQCHIP_SET_TYPE_MASKED,
718 static struct irq_chip gic_eoimode1_chip = {
720 .irq_mask = gic_eoimode1_mask_irq,
721 .irq_unmask = gic_unmask_irq,
722 .irq_eoi = gic_eoimode1_eoi_irq,
723 #ifdef CONFIG_ARCH_ROCKCHIP
724 .irq_retrigger = gic_retrigger,
726 .irq_set_type = gic_set_type,
727 .irq_set_affinity = gic_set_affinity,
728 .irq_get_irqchip_state = gic_irq_get_irqchip_state,
729 .irq_set_irqchip_state = gic_irq_set_irqchip_state,
730 .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
731 .flags = IRQCHIP_SET_TYPE_MASKED,
734 #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
736 static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
739 struct irq_chip *chip = &gic_chip;
741 if (static_key_true(&supports_deactivate))
742 chip = &gic_eoimode1_chip;
744 /* SGIs are private to the core kernel */
748 if (hw >= gic_data.irq_nr && hw < 8192)
756 irq_set_percpu_devid(irq);
757 irq_domain_set_info(d, irq, hw, chip, d->host_data,
758 handle_percpu_devid_irq, NULL, NULL);
759 irq_set_status_flags(irq, IRQ_NOAUTOEN);
762 if (hw >= 32 && hw < gic_data.irq_nr) {
763 irq_domain_set_info(d, irq, hw, chip, d->host_data,
764 handle_fasteoi_irq, NULL, NULL);
768 if (hw >= 8192 && hw < GIC_ID_NR) {
769 if (!gic_dist_supports_lpis())
771 irq_domain_set_info(d, irq, hw, chip, d->host_data,
772 handle_fasteoi_irq, NULL, NULL);
778 static int gic_irq_domain_translate(struct irq_domain *d,
779 struct irq_fwspec *fwspec,
780 unsigned long *hwirq,
783 if (is_of_node(fwspec->fwnode)) {
784 if (fwspec->param_count < 3)
787 switch (fwspec->param[0]) {
789 *hwirq = fwspec->param[1] + 32;
792 *hwirq = fwspec->param[1] + 16;
794 case GIC_IRQ_TYPE_LPI: /* LPI */
795 *hwirq = fwspec->param[1];
801 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
808 static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
809 unsigned int nr_irqs, void *arg)
812 irq_hw_number_t hwirq;
813 unsigned int type = IRQ_TYPE_NONE;
814 struct irq_fwspec *fwspec = arg;
816 ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
820 for (i = 0; i < nr_irqs; i++)
821 gic_irq_domain_map(domain, virq + i, hwirq + i);
826 static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
827 unsigned int nr_irqs)
831 for (i = 0; i < nr_irqs; i++) {
832 struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
833 irq_set_handler(virq + i, NULL);
834 irq_domain_reset_irq_data(d);
838 static int gic_irq_domain_select(struct irq_domain *d,
839 struct irq_fwspec *fwspec,
840 enum irq_domain_bus_token bus_token)
843 if (fwspec->fwnode != d->fwnode)
846 /* If this is not DT, then we have a single domain */
847 if (!is_of_node(fwspec->fwnode))
851 * If this is a PPI and we have a 4th (non-null) parameter,
852 * then we need to match the partition domain.
854 if (fwspec->param_count >= 4 &&
855 fwspec->param[0] == 1 && fwspec->param[3] != 0)
856 return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
858 return d == gic_data.domain;
861 static const struct irq_domain_ops gic_irq_domain_ops = {
862 .translate = gic_irq_domain_translate,
863 .alloc = gic_irq_domain_alloc,
864 .free = gic_irq_domain_free,
865 .select = gic_irq_domain_select,
868 static int partition_domain_translate(struct irq_domain *d,
869 struct irq_fwspec *fwspec,
870 unsigned long *hwirq,
873 struct device_node *np;
876 np = of_find_node_by_phandle(fwspec->param[3]);
880 ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
881 of_node_to_fwnode(np));
886 *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
891 static const struct irq_domain_ops partition_domain_ops = {
892 .translate = partition_domain_translate,
893 .select = gic_irq_domain_select,
896 static void gicv3_enable_quirks(void)
899 if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
900 static_branch_enable(&is_cavium_thunderx);
904 static int __init gic_init_bases(void __iomem *dist_base,
905 struct redist_region *rdist_regs,
906 u32 nr_redist_regions,
908 struct fwnode_handle *handle)
910 struct device_node *node;
915 if (!is_hyp_mode_available())
916 static_key_slow_dec(&supports_deactivate);
918 if (static_key_true(&supports_deactivate))
919 pr_info("GIC: Using split EOI/Deactivate mode\n");
921 gic_data.fwnode = handle;
922 gic_data.dist_base = dist_base;
923 gic_data.redist_regions = rdist_regs;
924 gic_data.nr_redist_regions = nr_redist_regions;
925 gic_data.redist_stride = redist_stride;
927 gicv3_enable_quirks();
930 * Find out how many interrupts are supported.
931 * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
933 typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
934 gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
935 gic_irqs = GICD_TYPER_IRQS(typer);
938 gic_data.irq_nr = gic_irqs;
940 gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
942 gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
944 if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
949 set_handle_irq(gic_handle_irq);
951 node = to_of_node(handle);
952 if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
953 node) /* Temp hack to prevent ITS init for ACPI */
954 its_init(node, &gic_data.rdists, gic_data.domain);
965 irq_domain_remove(gic_data.domain);
966 free_percpu(gic_data.rdists.rdist);
970 static int __init gic_validate_dist_version(void __iomem *dist_base)
972 u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
974 if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
980 static int get_cpu_number(struct device_node *dn)
986 cell = of_get_property(dn, "reg", NULL);
990 hwid = of_read_number(cell, of_n_addr_cells(dn));
993 * Non affinity bits must be set to 0 in the DT
995 if (hwid & ~MPIDR_HWID_BITMASK)
998 for (i = 0; i < num_possible_cpus(); i++)
999 if (cpu_logical_map(i) == hwid)
1005 /* Create all possible partitions at boot time */
1006 static void gic_populate_ppi_partitions(struct device_node *gic_node)
1008 struct device_node *parts_node, *child_part;
1009 int part_idx = 0, i;
1011 struct partition_affinity *parts;
1013 parts_node = of_find_node_by_name(gic_node, "ppi-partitions");
1017 nr_parts = of_get_child_count(parts_node);
1022 parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
1023 if (WARN_ON(!parts))
1026 for_each_child_of_node(parts_node, child_part) {
1027 struct partition_affinity *part;
1030 part = &parts[part_idx];
1032 part->partition_id = of_node_to_fwnode(child_part);
1034 pr_info("GIC: PPI partition %s[%d] { ",
1035 child_part->name, part_idx);
1037 n = of_property_count_elems_of_size(child_part, "affinity",
1041 for (i = 0; i < n; i++) {
1044 struct device_node *cpu_node;
1046 err = of_property_read_u32_index(child_part, "affinity",
1051 cpu_node = of_find_node_by_phandle(cpu_phandle);
1052 if (WARN_ON(!cpu_node))
1055 cpu = get_cpu_number(cpu_node);
1056 if (WARN_ON(cpu == -1))
1059 pr_cont("%s[%d] ", cpu_node->full_name, cpu);
1061 cpumask_set_cpu(cpu, &part->mask);
1068 for (i = 0; i < 16; i++) {
1070 struct partition_desc *desc;
1071 struct irq_fwspec ppi_fwspec = {
1072 .fwnode = gic_data.fwnode,
1077 [2] = IRQ_TYPE_NONE,
1081 irq = irq_create_fwspec_mapping(&ppi_fwspec);
1084 desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
1085 irq, &partition_domain_ops);
1089 gic_data.ppi_descs[i] = desc;
1093 static int __init gic_of_init(struct device_node *node, struct device_node *parent)
1095 void __iomem *dist_base;
1096 struct redist_region *rdist_regs;
1098 u32 nr_redist_regions;
1101 dist_base = of_iomap(node, 0);
1103 pr_err("%s: unable to map gic dist registers\n",
1108 err = gic_validate_dist_version(dist_base);
1110 pr_err("%s: no distributor detected, giving up\n",
1112 goto out_unmap_dist;
1115 if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
1116 nr_redist_regions = 1;
1118 rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
1121 goto out_unmap_dist;
1124 for (i = 0; i < nr_redist_regions; i++) {
1125 struct resource res;
1128 ret = of_address_to_resource(node, 1 + i, &res);
1129 rdist_regs[i].redist_base = of_iomap(node, 1 + i);
1130 if (ret || !rdist_regs[i].redist_base) {
1131 pr_err("%s: couldn't map region %d\n",
1132 node->full_name, i);
1134 goto out_unmap_rdist;
1136 rdist_regs[i].phys_base = res.start;
1139 if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
1142 err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
1143 redist_stride, &node->fwnode);
1145 goto out_unmap_rdist;
1147 gic_populate_ppi_partitions(node);
1151 for (i = 0; i < nr_redist_regions; i++)
1152 if (rdist_regs[i].redist_base)
1153 iounmap(rdist_regs[i].redist_base);
1160 IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);