1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
7 #define pr_fmt(fmt) "GICv3: " fmt
9 #include <linux/acpi.h>
10 #include <linux/cpu.h>
11 #include <linux/cpu_pm.h>
12 #include <linux/delay.h>
13 #include <linux/interrupt.h>
14 #include <linux/irqdomain.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/percpu.h>
19 #include <linux/refcount.h>
20 #include <linux/slab.h>
22 #include <linux/irqchip.h>
23 #include <linux/irqchip/arm-gic-common.h>
24 #include <linux/irqchip/arm-gic-v3.h>
25 #include <linux/irqchip/irq-partition-percpu.h>
27 #include <asm/cputype.h>
28 #include <asm/exception.h>
29 #include <asm/smp_plat.h>
32 #include "irq-gic-common.h"
34 #define GICD_INT_NMI_PRI (GICD_INT_DEF_PRI & ~0x80)
36 #define FLAGS_WORKAROUND_GICR_WAKER_MSM8996 (1ULL << 0)
38 struct redist_region
{
39 void __iomem
*redist_base
;
40 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
;
55 struct partition_desc
**ppi_descs
;
58 static struct gic_chip_data gic_data __read_mostly
;
59 static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key
);
61 #define GIC_ID_NR (1U << GICD_TYPER_ID_BITS(gic_data.rdists.gicd_typer))
62 #define GIC_LINE_NR min(GICD_TYPER_SPIS(gic_data.rdists.gicd_typer), 1020U)
63 #define GIC_ESPI_NR GICD_TYPER_ESPIS(gic_data.rdists.gicd_typer)
66 * The behaviours of RPR and PMR registers differ depending on the value of
67 * SCR_EL3.FIQ, and the behaviour of non-secure priority registers of the
68 * distributor and redistributors depends on whether security is enabled in the
71 * When security is enabled, non-secure priority values from the (re)distributor
72 * are presented to the GIC CPUIF as follow:
73 * (GIC_(R)DIST_PRI[irq] >> 1) | 0x80;
75 * If SCR_EL3.FIQ == 1, the values writen to/read from PMR and RPR at non-secure
76 * EL1 are subject to a similar operation thus matching the priorities presented
77 * from the (re)distributor when security is enabled.
79 * see GICv3/GICv4 Architecture Specification (IHI0069D):
80 * - section 4.8.1 Non-secure accesses to register fields for Secure interrupt
82 * - Figure 4-7 Secure read of the priority field for a Non-secure Group 1
85 * For now, we only support pseudo-NMIs if we have non-secure view of
88 static DEFINE_STATIC_KEY_FALSE(supports_pseudo_nmis
);
91 * Global static key controlling whether an update to PMR allowing more
92 * interrupts requires to be propagated to the redistributor (DSB SY).
93 * And this needs to be exported for modules to be able to enable
96 DEFINE_STATIC_KEY_FALSE(gic_pmr_sync
);
97 EXPORT_SYMBOL(gic_pmr_sync
);
99 /* ppi_nmi_refs[n] == number of cpus having ppi[n + 16] set as NMI */
100 static refcount_t
*ppi_nmi_refs
;
102 static struct gic_kvm_info gic_v3_kvm_info
;
103 static DEFINE_PER_CPU(bool, has_rss
);
105 #define MPIDR_RS(mpidr) (((mpidr) & 0xF0UL) >> 4)
106 #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
107 #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
108 #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
110 /* Our default, arbitrary priority value. Linux only uses one anyway. */
111 #define DEFAULT_PMR_VALUE 0xf0
113 enum gic_intid_range
{
122 static enum gic_intid_range
__get_intid_range(irq_hw_number_t hwirq
)
129 case EPPI_BASE_INTID
... (EPPI_BASE_INTID
+ 63):
131 case ESPI_BASE_INTID
... (ESPI_BASE_INTID
+ 1023):
133 case 8192 ... GENMASK(23, 0):
136 return __INVALID_RANGE__
;
140 static enum gic_intid_range
get_intid_range(struct irq_data
*d
)
142 return __get_intid_range(d
->hwirq
);
145 static inline unsigned int gic_irq(struct irq_data
*d
)
150 static inline int gic_irq_in_rdist(struct irq_data
*d
)
152 enum gic_intid_range range
= get_intid_range(d
);
153 return range
== PPI_RANGE
|| range
== EPPI_RANGE
;
156 static inline void __iomem
*gic_dist_base(struct irq_data
*d
)
158 switch (get_intid_range(d
)) {
161 /* SGI+PPI -> SGI_base for this CPU */
162 return gic_data_rdist_sgi_base();
166 /* SPI -> dist_base */
167 return gic_data
.dist_base
;
174 static void gic_do_wait_for_rwp(void __iomem
*base
)
176 u32 count
= 1000000; /* 1s! */
178 while (readl_relaxed(base
+ GICD_CTLR
) & GICD_CTLR_RWP
) {
181 pr_err_ratelimited("RWP timeout, gone fishing\n");
189 /* Wait for completion of a distributor change */
190 static void gic_dist_wait_for_rwp(void)
192 gic_do_wait_for_rwp(gic_data
.dist_base
);
195 /* Wait for completion of a redistributor change */
196 static void gic_redist_wait_for_rwp(void)
198 gic_do_wait_for_rwp(gic_data_rdist_rd_base());
203 static u64 __maybe_unused
gic_read_iar(void)
205 if (cpus_have_const_cap(ARM64_WORKAROUND_CAVIUM_23154
))
206 return gic_read_iar_cavium_thunderx();
208 return gic_read_iar_common();
212 static void gic_enable_redist(bool enable
)
215 u32 count
= 1000000; /* 1s! */
218 if (gic_data
.flags
& FLAGS_WORKAROUND_GICR_WAKER_MSM8996
)
221 rbase
= gic_data_rdist_rd_base();
223 val
= readl_relaxed(rbase
+ GICR_WAKER
);
225 /* Wake up this CPU redistributor */
226 val
&= ~GICR_WAKER_ProcessorSleep
;
228 val
|= GICR_WAKER_ProcessorSleep
;
229 writel_relaxed(val
, rbase
+ GICR_WAKER
);
231 if (!enable
) { /* Check that GICR_WAKER is writeable */
232 val
= readl_relaxed(rbase
+ GICR_WAKER
);
233 if (!(val
& GICR_WAKER_ProcessorSleep
))
234 return; /* No PM support in this redistributor */
238 val
= readl_relaxed(rbase
+ GICR_WAKER
);
239 if (enable
^ (bool)(val
& GICR_WAKER_ChildrenAsleep
))
245 pr_err_ratelimited("redistributor failed to %s...\n",
246 enable
? "wakeup" : "sleep");
250 * Routines to disable, enable, EOI and route interrupts
252 static u32
convert_offset_index(struct irq_data
*d
, u32 offset
, u32
*index
)
254 switch (get_intid_range(d
)) {
261 * Contrary to the ESPI range, the EPPI range is contiguous
262 * to the PPI range in the registers, so let's adjust the
263 * displacement accordingly. Consistency is overrated.
265 *index
= d
->hwirq
- EPPI_BASE_INTID
+ 32;
268 *index
= d
->hwirq
- ESPI_BASE_INTID
;
271 return GICD_ISENABLERnE
;
273 return GICD_ICENABLERnE
;
275 return GICD_ISPENDRnE
;
277 return GICD_ICPENDRnE
;
279 return GICD_ISACTIVERnE
;
281 return GICD_ICACTIVERnE
;
282 case GICD_IPRIORITYR
:
283 return GICD_IPRIORITYRnE
;
287 return GICD_IROUTERnE
;
301 static int gic_peek_irq(struct irq_data
*d
, u32 offset
)
306 offset
= convert_offset_index(d
, offset
, &index
);
307 mask
= 1 << (index
% 32);
309 if (gic_irq_in_rdist(d
))
310 base
= gic_data_rdist_sgi_base();
312 base
= gic_data
.dist_base
;
314 return !!(readl_relaxed(base
+ offset
+ (index
/ 32) * 4) & mask
);
317 static void gic_poke_irq(struct irq_data
*d
, u32 offset
)
319 void (*rwp_wait
)(void);
323 offset
= convert_offset_index(d
, offset
, &index
);
324 mask
= 1 << (index
% 32);
326 if (gic_irq_in_rdist(d
)) {
327 base
= gic_data_rdist_sgi_base();
328 rwp_wait
= gic_redist_wait_for_rwp
;
330 base
= gic_data
.dist_base
;
331 rwp_wait
= gic_dist_wait_for_rwp
;
334 writel_relaxed(mask
, base
+ offset
+ (index
/ 32) * 4);
338 static void gic_mask_irq(struct irq_data
*d
)
340 gic_poke_irq(d
, GICD_ICENABLER
);
343 static void gic_eoimode1_mask_irq(struct irq_data
*d
)
347 * When masking a forwarded interrupt, make sure it is
348 * deactivated as well.
350 * This ensures that an interrupt that is getting
351 * disabled/masked will not get "stuck", because there is
352 * noone to deactivate it (guest is being terminated).
354 if (irqd_is_forwarded_to_vcpu(d
))
355 gic_poke_irq(d
, GICD_ICACTIVER
);
358 static void gic_unmask_irq(struct irq_data
*d
)
360 gic_poke_irq(d
, GICD_ISENABLER
);
363 static inline bool gic_supports_nmi(void)
365 return IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI
) &&
366 static_branch_likely(&supports_pseudo_nmis
);
369 static int gic_irq_set_irqchip_state(struct irq_data
*d
,
370 enum irqchip_irq_state which
, bool val
)
374 if (d
->hwirq
>= 8192) /* PPI/SPI only */
378 case IRQCHIP_STATE_PENDING
:
379 reg
= val
? GICD_ISPENDR
: GICD_ICPENDR
;
382 case IRQCHIP_STATE_ACTIVE
:
383 reg
= val
? GICD_ISACTIVER
: GICD_ICACTIVER
;
386 case IRQCHIP_STATE_MASKED
:
387 reg
= val
? GICD_ICENABLER
: GICD_ISENABLER
;
394 gic_poke_irq(d
, reg
);
398 static int gic_irq_get_irqchip_state(struct irq_data
*d
,
399 enum irqchip_irq_state which
, bool *val
)
401 if (d
->hwirq
>= 8192) /* PPI/SPI only */
405 case IRQCHIP_STATE_PENDING
:
406 *val
= gic_peek_irq(d
, GICD_ISPENDR
);
409 case IRQCHIP_STATE_ACTIVE
:
410 *val
= gic_peek_irq(d
, GICD_ISACTIVER
);
413 case IRQCHIP_STATE_MASKED
:
414 *val
= !gic_peek_irq(d
, GICD_ISENABLER
);
424 static void gic_irq_set_prio(struct irq_data
*d
, u8 prio
)
426 void __iomem
*base
= gic_dist_base(d
);
429 offset
= convert_offset_index(d
, GICD_IPRIORITYR
, &index
);
431 writeb_relaxed(prio
, base
+ offset
+ index
);
434 static u32
gic_get_ppi_index(struct irq_data
*d
)
436 switch (get_intid_range(d
)) {
438 return d
->hwirq
- 16;
440 return d
->hwirq
- EPPI_BASE_INTID
+ 16;
446 static int gic_irq_nmi_setup(struct irq_data
*d
)
448 struct irq_desc
*desc
= irq_to_desc(d
->irq
);
450 if (!gic_supports_nmi())
453 if (gic_peek_irq(d
, GICD_ISENABLER
)) {
454 pr_err("Cannot set NMI property of enabled IRQ %u\n", d
->irq
);
459 * A secondary irq_chip should be in charge of LPI request,
460 * it should not be possible to get there
462 if (WARN_ON(gic_irq(d
) >= 8192))
465 /* desc lock should already be held */
466 if (gic_irq_in_rdist(d
)) {
467 u32 idx
= gic_get_ppi_index(d
);
469 /* Setting up PPI as NMI, only switch handler for first NMI */
470 if (!refcount_inc_not_zero(&ppi_nmi_refs
[idx
])) {
471 refcount_set(&ppi_nmi_refs
[idx
], 1);
472 desc
->handle_irq
= handle_percpu_devid_fasteoi_nmi
;
475 desc
->handle_irq
= handle_fasteoi_nmi
;
478 gic_irq_set_prio(d
, GICD_INT_NMI_PRI
);
483 static void gic_irq_nmi_teardown(struct irq_data
*d
)
485 struct irq_desc
*desc
= irq_to_desc(d
->irq
);
487 if (WARN_ON(!gic_supports_nmi()))
490 if (gic_peek_irq(d
, GICD_ISENABLER
)) {
491 pr_err("Cannot set NMI property of enabled IRQ %u\n", d
->irq
);
496 * A secondary irq_chip should be in charge of LPI request,
497 * it should not be possible to get there
499 if (WARN_ON(gic_irq(d
) >= 8192))
502 /* desc lock should already be held */
503 if (gic_irq_in_rdist(d
)) {
504 u32 idx
= gic_get_ppi_index(d
);
506 /* Tearing down NMI, only switch handler for last NMI */
507 if (refcount_dec_and_test(&ppi_nmi_refs
[idx
]))
508 desc
->handle_irq
= handle_percpu_devid_irq
;
510 desc
->handle_irq
= handle_fasteoi_irq
;
513 gic_irq_set_prio(d
, GICD_INT_DEF_PRI
);
516 static void gic_eoi_irq(struct irq_data
*d
)
518 gic_write_eoir(gic_irq(d
));
521 static void gic_eoimode1_eoi_irq(struct irq_data
*d
)
524 * No need to deactivate an LPI, or an interrupt that
525 * is is getting forwarded to a vcpu.
527 if (gic_irq(d
) >= 8192 || irqd_is_forwarded_to_vcpu(d
))
529 gic_write_dir(gic_irq(d
));
532 static int gic_set_type(struct irq_data
*d
, unsigned int type
)
534 enum gic_intid_range range
;
535 unsigned int irq
= gic_irq(d
);
536 void (*rwp_wait
)(void);
541 /* Interrupt configuration for SGIs can't be changed */
545 range
= get_intid_range(d
);
547 /* SPIs have restrictions on the supported types */
548 if ((range
== SPI_RANGE
|| range
== ESPI_RANGE
) &&
549 type
!= IRQ_TYPE_LEVEL_HIGH
&& type
!= IRQ_TYPE_EDGE_RISING
)
552 if (gic_irq_in_rdist(d
)) {
553 base
= gic_data_rdist_sgi_base();
554 rwp_wait
= gic_redist_wait_for_rwp
;
556 base
= gic_data
.dist_base
;
557 rwp_wait
= gic_dist_wait_for_rwp
;
560 offset
= convert_offset_index(d
, GICD_ICFGR
, &index
);
562 ret
= gic_configure_irq(index
, type
, base
+ offset
, rwp_wait
);
563 if (ret
&& (range
== PPI_RANGE
|| range
== EPPI_RANGE
)) {
564 /* Misconfigured PPIs are usually not fatal */
565 pr_warn("GIC: PPI INTID%d is secure or misconfigured\n", irq
);
572 static int gic_irq_set_vcpu_affinity(struct irq_data
*d
, void *vcpu
)
575 irqd_set_forwarded_to_vcpu(d
);
577 irqd_clr_forwarded_to_vcpu(d
);
581 static u64
gic_mpidr_to_affinity(unsigned long mpidr
)
585 aff
= ((u64
)MPIDR_AFFINITY_LEVEL(mpidr
, 3) << 32 |
586 MPIDR_AFFINITY_LEVEL(mpidr
, 2) << 16 |
587 MPIDR_AFFINITY_LEVEL(mpidr
, 1) << 8 |
588 MPIDR_AFFINITY_LEVEL(mpidr
, 0));
593 static void gic_deactivate_unhandled(u32 irqnr
)
595 if (static_branch_likely(&supports_deactivate_key
)) {
597 gic_write_dir(irqnr
);
599 gic_write_eoir(irqnr
);
603 static inline void gic_handle_nmi(u32 irqnr
, struct pt_regs
*regs
)
605 bool irqs_enabled
= interrupts_enabled(regs
);
611 if (static_branch_likely(&supports_deactivate_key
))
612 gic_write_eoir(irqnr
);
614 * Leave the PSR.I bit set to prevent other NMIs to be
615 * received while handling this one.
616 * PSR.I will be restored when we ERET to the
617 * interrupted context.
619 err
= handle_domain_nmi(gic_data
.domain
, irqnr
, regs
);
621 gic_deactivate_unhandled(irqnr
);
627 static asmlinkage
void __exception_irq_entry
gic_handle_irq(struct pt_regs
*regs
)
631 irqnr
= gic_read_iar();
633 if (gic_supports_nmi() &&
634 unlikely(gic_read_rpr() == GICD_INT_NMI_PRI
)) {
635 gic_handle_nmi(irqnr
, regs
);
639 if (gic_prio_masking_enabled()) {
641 gic_arch_enable_irqs();
644 /* Check for special IDs first */
645 if ((irqnr
>= 1020 && irqnr
<= 1023))
648 /* Treat anything but SGIs in a uniform way */
649 if (likely(irqnr
> 15)) {
652 if (static_branch_likely(&supports_deactivate_key
))
653 gic_write_eoir(irqnr
);
657 err
= handle_domain_irq(gic_data
.domain
, irqnr
, regs
);
659 WARN_ONCE(true, "Unexpected interrupt received!\n");
660 gic_deactivate_unhandled(irqnr
);
665 gic_write_eoir(irqnr
);
666 if (static_branch_likely(&supports_deactivate_key
))
667 gic_write_dir(irqnr
);
670 * Unlike GICv2, we don't need an smp_rmb() here.
671 * The control dependency from gic_read_iar to
672 * the ISB in gic_write_eoir is enough to ensure
673 * that any shared data read by handle_IPI will
674 * be read after the ACK.
676 handle_IPI(irqnr
, regs
);
678 WARN_ONCE(true, "Unexpected SGI received!\n");
683 static u32
gic_get_pribits(void)
687 pribits
= gic_read_ctlr();
688 pribits
&= ICC_CTLR_EL1_PRI_BITS_MASK
;
689 pribits
>>= ICC_CTLR_EL1_PRI_BITS_SHIFT
;
695 static bool gic_has_group0(void)
700 old_pmr
= gic_read_pmr();
703 * Let's find out if Group0 is under control of EL3 or not by
704 * setting the highest possible, non-zero priority in PMR.
706 * If SCR_EL3.FIQ is set, the priority gets shifted down in
707 * order for the CPU interface to set bit 7, and keep the
708 * actual priority in the non-secure range. In the process, it
709 * looses the least significant bit and the actual priority
710 * becomes 0x80. Reading it back returns 0, indicating that
711 * we're don't have access to Group0.
713 gic_write_pmr(BIT(8 - gic_get_pribits()));
714 val
= gic_read_pmr();
716 gic_write_pmr(old_pmr
);
721 static void __init
gic_dist_init(void)
725 void __iomem
*base
= gic_data
.dist_base
;
727 /* Disable the distributor */
728 writel_relaxed(0, base
+ GICD_CTLR
);
729 gic_dist_wait_for_rwp();
732 * Configure SPIs as non-secure Group-1. This will only matter
733 * if the GIC only has a single security state. This will not
734 * do the right thing if the kernel is running in secure mode,
735 * but that's not the intended use case anyway.
737 for (i
= 32; i
< GIC_LINE_NR
; i
+= 32)
738 writel_relaxed(~0, base
+ GICD_IGROUPR
+ i
/ 8);
740 /* Extended SPI range, not handled by the GICv2/GICv3 common code */
741 for (i
= 0; i
< GIC_ESPI_NR
; i
+= 32) {
742 writel_relaxed(~0U, base
+ GICD_ICENABLERnE
+ i
/ 8);
743 writel_relaxed(~0U, base
+ GICD_ICACTIVERnE
+ i
/ 8);
746 for (i
= 0; i
< GIC_ESPI_NR
; i
+= 32)
747 writel_relaxed(~0U, base
+ GICD_IGROUPRnE
+ i
/ 8);
749 for (i
= 0; i
< GIC_ESPI_NR
; i
+= 16)
750 writel_relaxed(0, base
+ GICD_ICFGRnE
+ i
/ 4);
752 for (i
= 0; i
< GIC_ESPI_NR
; i
+= 4)
753 writel_relaxed(GICD_INT_DEF_PRI_X4
, base
+ GICD_IPRIORITYRnE
+ i
);
755 /* Now do the common stuff, and wait for the distributor to drain */
756 gic_dist_config(base
, GIC_LINE_NR
, gic_dist_wait_for_rwp
);
758 /* Enable distributor with ARE, Group1 */
759 writel_relaxed(GICD_CTLR_ARE_NS
| GICD_CTLR_ENABLE_G1A
| GICD_CTLR_ENABLE_G1
,
763 * Set all global interrupts to the boot CPU only. ARE must be
766 affinity
= gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
767 for (i
= 32; i
< GIC_LINE_NR
; i
++)
768 gic_write_irouter(affinity
, base
+ GICD_IROUTER
+ i
* 8);
770 for (i
= 0; i
< GIC_ESPI_NR
; i
++)
771 gic_write_irouter(affinity
, base
+ GICD_IROUTERnE
+ i
* 8);
774 static int gic_iterate_rdists(int (*fn
)(struct redist_region
*, void __iomem
*))
779 for (i
= 0; i
< gic_data
.nr_redist_regions
; i
++) {
780 void __iomem
*ptr
= gic_data
.redist_regions
[i
].redist_base
;
784 reg
= readl_relaxed(ptr
+ GICR_PIDR2
) & GIC_PIDR2_ARCH_MASK
;
785 if (reg
!= GIC_PIDR2_ARCH_GICv3
&&
786 reg
!= GIC_PIDR2_ARCH_GICv4
) { /* We're in trouble... */
787 pr_warn("No redistributor present @%p\n", ptr
);
792 typer
= gic_read_typer(ptr
+ GICR_TYPER
);
793 ret
= fn(gic_data
.redist_regions
+ i
, ptr
);
797 if (gic_data
.redist_regions
[i
].single_redist
)
800 if (gic_data
.redist_stride
) {
801 ptr
+= gic_data
.redist_stride
;
803 ptr
+= SZ_64K
* 2; /* Skip RD_base + SGI_base */
804 if (typer
& GICR_TYPER_VLPIS
)
805 ptr
+= SZ_64K
* 2; /* Skip VLPI_base + reserved page */
807 } while (!(typer
& GICR_TYPER_LAST
));
810 return ret
? -ENODEV
: 0;
813 static int __gic_populate_rdist(struct redist_region
*region
, void __iomem
*ptr
)
815 unsigned long mpidr
= cpu_logical_map(smp_processor_id());
820 * Convert affinity to a 32bit value that can be matched to
821 * GICR_TYPER bits [63:32].
823 aff
= (MPIDR_AFFINITY_LEVEL(mpidr
, 3) << 24 |
824 MPIDR_AFFINITY_LEVEL(mpidr
, 2) << 16 |
825 MPIDR_AFFINITY_LEVEL(mpidr
, 1) << 8 |
826 MPIDR_AFFINITY_LEVEL(mpidr
, 0));
828 typer
= gic_read_typer(ptr
+ GICR_TYPER
);
829 if ((typer
>> 32) == aff
) {
830 u64 offset
= ptr
- region
->redist_base
;
831 gic_data_rdist_rd_base() = ptr
;
832 gic_data_rdist()->phys_base
= region
->phys_base
+ offset
;
834 pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
835 smp_processor_id(), mpidr
,
836 (int)(region
- gic_data
.redist_regions
),
837 &gic_data_rdist()->phys_base
);
845 static int gic_populate_rdist(void)
847 if (gic_iterate_rdists(__gic_populate_rdist
) == 0)
850 /* We couldn't even deal with ourselves... */
851 WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
853 (unsigned long)cpu_logical_map(smp_processor_id()));
857 static int __gic_update_rdist_properties(struct redist_region
*region
,
860 u64 typer
= gic_read_typer(ptr
+ GICR_TYPER
);
862 gic_data
.rdists
.has_vlpis
&= !!(typer
& GICR_TYPER_VLPIS
);
864 /* RVPEID implies some form of DirectLPI, no matter what the doc says... :-/ */
865 gic_data
.rdists
.has_rvpeid
&= !!(typer
& GICR_TYPER_RVPEID
);
866 gic_data
.rdists
.has_direct_lpi
&= (!!(typer
& GICR_TYPER_DirectLPIS
) |
867 gic_data
.rdists
.has_rvpeid
);
869 /* Detect non-sensical configurations */
870 if (WARN_ON_ONCE(gic_data
.rdists
.has_rvpeid
&& !gic_data
.rdists
.has_vlpis
)) {
871 gic_data
.rdists
.has_direct_lpi
= false;
872 gic_data
.rdists
.has_vlpis
= false;
873 gic_data
.rdists
.has_rvpeid
= false;
876 gic_data
.ppi_nr
= min(GICR_TYPER_NR_PPIS(typer
), gic_data
.ppi_nr
);
881 static void gic_update_rdist_properties(void)
883 gic_data
.ppi_nr
= UINT_MAX
;
884 gic_iterate_rdists(__gic_update_rdist_properties
);
885 if (WARN_ON(gic_data
.ppi_nr
== UINT_MAX
))
887 pr_info("%d PPIs implemented\n", gic_data
.ppi_nr
);
888 pr_info("%sVLPI support, %sdirect LPI support, %sRVPEID support\n",
889 !gic_data
.rdists
.has_vlpis
? "no " : "",
890 !gic_data
.rdists
.has_direct_lpi
? "no " : "",
891 !gic_data
.rdists
.has_rvpeid
? "no " : "");
894 /* Check whether it's single security state view */
895 static inline bool gic_dist_security_disabled(void)
897 return readl_relaxed(gic_data
.dist_base
+ GICD_CTLR
) & GICD_CTLR_DS
;
900 static void gic_cpu_sys_reg_init(void)
902 int i
, cpu
= smp_processor_id();
903 u64 mpidr
= cpu_logical_map(cpu
);
904 u64 need_rss
= MPIDR_RS(mpidr
);
909 * Need to check that the SRE bit has actually been set. If
910 * not, it means that SRE is disabled at EL2. We're going to
911 * die painfully, and there is nothing we can do about it.
913 * Kindly inform the luser.
915 if (!gic_enable_sre())
916 pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
918 pribits
= gic_get_pribits();
920 group0
= gic_has_group0();
922 /* Set priority mask register */
923 if (!gic_prio_masking_enabled()) {
924 write_gicreg(DEFAULT_PMR_VALUE
, ICC_PMR_EL1
);
927 * Mismatch configuration with boot CPU, the system is likely
928 * to die as interrupt masking will not work properly on all
931 WARN_ON(gic_supports_nmi() && group0
&&
932 !gic_dist_security_disabled());
936 * Some firmwares hand over to the kernel with the BPR changed from
937 * its reset value (and with a value large enough to prevent
938 * any pre-emptive interrupts from working at all). Writing a zero
939 * to BPR restores is reset value.
943 if (static_branch_likely(&supports_deactivate_key
)) {
944 /* EOI drops priority only (mode 1) */
945 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop
);
947 /* EOI deactivates interrupt too (mode 0) */
948 gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir
);
951 /* Always whack Group0 before Group1 */
956 write_gicreg(0, ICC_AP0R3_EL1
);
957 write_gicreg(0, ICC_AP0R2_EL1
);
960 write_gicreg(0, ICC_AP0R1_EL1
);
964 write_gicreg(0, ICC_AP0R0_EL1
);
973 write_gicreg(0, ICC_AP1R3_EL1
);
974 write_gicreg(0, ICC_AP1R2_EL1
);
977 write_gicreg(0, ICC_AP1R1_EL1
);
981 write_gicreg(0, ICC_AP1R0_EL1
);
986 /* ... and let's hit the road... */
989 /* Keep the RSS capability status in per_cpu variable */
990 per_cpu(has_rss
, cpu
) = !!(gic_read_ctlr() & ICC_CTLR_EL1_RSS
);
992 /* Check all the CPUs have capable of sending SGIs to other CPUs */
993 for_each_online_cpu(i
) {
994 bool have_rss
= per_cpu(has_rss
, i
) && per_cpu(has_rss
, cpu
);
996 need_rss
|= MPIDR_RS(cpu_logical_map(i
));
997 if (need_rss
&& (!have_rss
))
998 pr_crit("CPU%d (%lx) can't SGI CPU%d (%lx), no RSS\n",
999 cpu
, (unsigned long)mpidr
,
1000 i
, (unsigned long)cpu_logical_map(i
));
1004 * GIC spec says, when ICC_CTLR_EL1.RSS==1 and GICD_TYPER.RSS==0,
1005 * writing ICC_ASGI1R_EL1 register with RS != 0 is a CONSTRAINED
1006 * UNPREDICTABLE choice of :
1007 * - The write is ignored.
1008 * - The RS field is treated as 0.
1010 if (need_rss
&& (!gic_data
.has_rss
))
1011 pr_crit_once("RSS is required but GICD doesn't support it\n");
1014 static bool gicv3_nolpi
;
1016 static int __init
gicv3_nolpi_cfg(char *buf
)
1018 return strtobool(buf
, &gicv3_nolpi
);
1020 early_param("irqchip.gicv3_nolpi", gicv3_nolpi_cfg
);
1022 static int gic_dist_supports_lpis(void)
1024 return (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS
) &&
1025 !!(readl_relaxed(gic_data
.dist_base
+ GICD_TYPER
) & GICD_TYPER_LPIS
) &&
1029 static void gic_cpu_init(void)
1031 void __iomem
*rbase
;
1034 /* Register ourselves with the rest of the world */
1035 if (gic_populate_rdist())
1038 gic_enable_redist(true);
1040 WARN((gic_data
.ppi_nr
> 16 || GIC_ESPI_NR
!= 0) &&
1041 !(gic_read_ctlr() & ICC_CTLR_EL1_ExtRange
),
1042 "Distributor has extended ranges, but CPU%d doesn't\n",
1043 smp_processor_id());
1045 rbase
= gic_data_rdist_sgi_base();
1047 /* Configure SGIs/PPIs as non-secure Group-1 */
1048 for (i
= 0; i
< gic_data
.ppi_nr
+ 16; i
+= 32)
1049 writel_relaxed(~0, rbase
+ GICR_IGROUPR0
+ i
/ 8);
1051 gic_cpu_config(rbase
, gic_data
.ppi_nr
+ 16, gic_redist_wait_for_rwp
);
1053 /* initialise system registers */
1054 gic_cpu_sys_reg_init();
1059 #define MPIDR_TO_SGI_RS(mpidr) (MPIDR_RS(mpidr) << ICC_SGI1R_RS_SHIFT)
1060 #define MPIDR_TO_SGI_CLUSTER_ID(mpidr) ((mpidr) & ~0xFUL)
1062 static int gic_starting_cpu(unsigned int cpu
)
1066 if (gic_dist_supports_lpis())
1072 static u16
gic_compute_target_list(int *base_cpu
, const struct cpumask
*mask
,
1073 unsigned long cluster_id
)
1075 int next_cpu
, cpu
= *base_cpu
;
1076 unsigned long mpidr
= cpu_logical_map(cpu
);
1079 while (cpu
< nr_cpu_ids
) {
1080 tlist
|= 1 << (mpidr
& 0xf);
1082 next_cpu
= cpumask_next(cpu
, mask
);
1083 if (next_cpu
>= nr_cpu_ids
)
1087 mpidr
= cpu_logical_map(cpu
);
1089 if (cluster_id
!= MPIDR_TO_SGI_CLUSTER_ID(mpidr
)) {
1099 #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
1100 (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
1101 << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
1103 static void gic_send_sgi(u64 cluster_id
, u16 tlist
, unsigned int irq
)
1107 val
= (MPIDR_TO_SGI_AFFINITY(cluster_id
, 3) |
1108 MPIDR_TO_SGI_AFFINITY(cluster_id
, 2) |
1109 irq
<< ICC_SGI1R_SGI_ID_SHIFT
|
1110 MPIDR_TO_SGI_AFFINITY(cluster_id
, 1) |
1111 MPIDR_TO_SGI_RS(cluster_id
) |
1112 tlist
<< ICC_SGI1R_TARGET_LIST_SHIFT
);
1114 pr_devel("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val
);
1115 gic_write_sgi1r(val
);
1118 static void gic_raise_softirq(const struct cpumask
*mask
, unsigned int irq
)
1122 if (WARN_ON(irq
>= 16))
1126 * Ensure that stores to Normal memory are visible to the
1127 * other CPUs before issuing the IPI.
1131 for_each_cpu(cpu
, mask
) {
1132 u64 cluster_id
= MPIDR_TO_SGI_CLUSTER_ID(cpu_logical_map(cpu
));
1135 tlist
= gic_compute_target_list(&cpu
, mask
, cluster_id
);
1136 gic_send_sgi(cluster_id
, tlist
, irq
);
1139 /* Force the above writes to ICC_SGI1R_EL1 to be executed */
1143 static void gic_smp_init(void)
1145 set_smp_cross_call(gic_raise_softirq
);
1146 cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING
,
1147 "irqchip/arm/gicv3:starting",
1148 gic_starting_cpu
, NULL
);
1151 static int gic_set_affinity(struct irq_data
*d
, const struct cpumask
*mask_val
,
1161 cpu
= cpumask_first(mask_val
);
1163 cpu
= cpumask_any_and(mask_val
, cpu_online_mask
);
1165 if (cpu
>= nr_cpu_ids
)
1168 if (gic_irq_in_rdist(d
))
1171 /* If interrupt was enabled, disable it first */
1172 enabled
= gic_peek_irq(d
, GICD_ISENABLER
);
1176 offset
= convert_offset_index(d
, GICD_IROUTER
, &index
);
1177 reg
= gic_dist_base(d
) + offset
+ (index
* 8);
1178 val
= gic_mpidr_to_affinity(cpu_logical_map(cpu
));
1180 gic_write_irouter(val
, reg
);
1183 * If the interrupt was enabled, enabled it again. Otherwise,
1184 * just wait for the distributor to have digested our changes.
1189 gic_dist_wait_for_rwp();
1191 irq_data_update_effective_affinity(d
, cpumask_of(cpu
));
1193 return IRQ_SET_MASK_OK_DONE
;
1196 #define gic_set_affinity NULL
1197 #define gic_smp_init() do { } while(0)
1200 #ifdef CONFIG_CPU_PM
1201 static int gic_cpu_pm_notifier(struct notifier_block
*self
,
1202 unsigned long cmd
, void *v
)
1204 if (cmd
== CPU_PM_EXIT
) {
1205 if (gic_dist_security_disabled())
1206 gic_enable_redist(true);
1207 gic_cpu_sys_reg_init();
1208 } else if (cmd
== CPU_PM_ENTER
&& gic_dist_security_disabled()) {
1209 gic_write_grpen1(0);
1210 gic_enable_redist(false);
1215 static struct notifier_block gic_cpu_pm_notifier_block
= {
1216 .notifier_call
= gic_cpu_pm_notifier
,
1219 static void gic_cpu_pm_init(void)
1221 cpu_pm_register_notifier(&gic_cpu_pm_notifier_block
);
1225 static inline void gic_cpu_pm_init(void) { }
1226 #endif /* CONFIG_CPU_PM */
1228 static struct irq_chip gic_chip
= {
1230 .irq_mask
= gic_mask_irq
,
1231 .irq_unmask
= gic_unmask_irq
,
1232 .irq_eoi
= gic_eoi_irq
,
1233 .irq_set_type
= gic_set_type
,
1234 .irq_set_affinity
= gic_set_affinity
,
1235 .irq_get_irqchip_state
= gic_irq_get_irqchip_state
,
1236 .irq_set_irqchip_state
= gic_irq_set_irqchip_state
,
1237 .irq_nmi_setup
= gic_irq_nmi_setup
,
1238 .irq_nmi_teardown
= gic_irq_nmi_teardown
,
1239 .flags
= IRQCHIP_SET_TYPE_MASKED
|
1240 IRQCHIP_SKIP_SET_WAKE
|
1241 IRQCHIP_MASK_ON_SUSPEND
,
1244 static struct irq_chip gic_eoimode1_chip
= {
1246 .irq_mask
= gic_eoimode1_mask_irq
,
1247 .irq_unmask
= gic_unmask_irq
,
1248 .irq_eoi
= gic_eoimode1_eoi_irq
,
1249 .irq_set_type
= gic_set_type
,
1250 .irq_set_affinity
= gic_set_affinity
,
1251 .irq_get_irqchip_state
= gic_irq_get_irqchip_state
,
1252 .irq_set_irqchip_state
= gic_irq_set_irqchip_state
,
1253 .irq_set_vcpu_affinity
= gic_irq_set_vcpu_affinity
,
1254 .irq_nmi_setup
= gic_irq_nmi_setup
,
1255 .irq_nmi_teardown
= gic_irq_nmi_teardown
,
1256 .flags
= IRQCHIP_SET_TYPE_MASKED
|
1257 IRQCHIP_SKIP_SET_WAKE
|
1258 IRQCHIP_MASK_ON_SUSPEND
,
1261 static int gic_irq_domain_map(struct irq_domain
*d
, unsigned int irq
,
1264 struct irq_chip
*chip
= &gic_chip
;
1266 if (static_branch_likely(&supports_deactivate_key
))
1267 chip
= &gic_eoimode1_chip
;
1269 switch (__get_intid_range(hw
)) {
1272 irq_set_percpu_devid(irq
);
1273 irq_domain_set_info(d
, irq
, hw
, chip
, d
->host_data
,
1274 handle_percpu_devid_irq
, NULL
, NULL
);
1275 irq_set_status_flags(irq
, IRQ_NOAUTOEN
);
1280 irq_domain_set_info(d
, irq
, hw
, chip
, d
->host_data
,
1281 handle_fasteoi_irq
, NULL
, NULL
);
1283 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq
)));
1287 if (!gic_dist_supports_lpis())
1289 irq_domain_set_info(d
, irq
, hw
, chip
, d
->host_data
,
1290 handle_fasteoi_irq
, NULL
, NULL
);
1300 #define GIC_IRQ_TYPE_PARTITION (GIC_IRQ_TYPE_LPI + 1)
1302 static int gic_irq_domain_translate(struct irq_domain
*d
,
1303 struct irq_fwspec
*fwspec
,
1304 unsigned long *hwirq
,
1307 if (is_of_node(fwspec
->fwnode
)) {
1308 if (fwspec
->param_count
< 3)
1311 switch (fwspec
->param
[0]) {
1313 *hwirq
= fwspec
->param
[1] + 32;
1316 *hwirq
= fwspec
->param
[1] + 16;
1319 *hwirq
= fwspec
->param
[1] + ESPI_BASE_INTID
;
1322 *hwirq
= fwspec
->param
[1] + EPPI_BASE_INTID
;
1324 case GIC_IRQ_TYPE_LPI
: /* LPI */
1325 *hwirq
= fwspec
->param
[1];
1327 case GIC_IRQ_TYPE_PARTITION
:
1328 *hwirq
= fwspec
->param
[1];
1329 if (fwspec
->param
[1] >= 16)
1330 *hwirq
+= EPPI_BASE_INTID
- 16;
1338 *type
= fwspec
->param
[2] & IRQ_TYPE_SENSE_MASK
;
1341 * Make it clear that broken DTs are... broken.
1342 * Partitionned PPIs are an unfortunate exception.
1344 WARN_ON(*type
== IRQ_TYPE_NONE
&&
1345 fwspec
->param
[0] != GIC_IRQ_TYPE_PARTITION
);
1349 if (is_fwnode_irqchip(fwspec
->fwnode
)) {
1350 if(fwspec
->param_count
!= 2)
1353 *hwirq
= fwspec
->param
[0];
1354 *type
= fwspec
->param
[1];
1356 WARN_ON(*type
== IRQ_TYPE_NONE
);
1363 static int gic_irq_domain_alloc(struct irq_domain
*domain
, unsigned int virq
,
1364 unsigned int nr_irqs
, void *arg
)
1367 irq_hw_number_t hwirq
;
1368 unsigned int type
= IRQ_TYPE_NONE
;
1369 struct irq_fwspec
*fwspec
= arg
;
1371 ret
= gic_irq_domain_translate(domain
, fwspec
, &hwirq
, &type
);
1375 for (i
= 0; i
< nr_irqs
; i
++) {
1376 ret
= gic_irq_domain_map(domain
, virq
+ i
, hwirq
+ i
);
1384 static void gic_irq_domain_free(struct irq_domain
*domain
, unsigned int virq
,
1385 unsigned int nr_irqs
)
1389 for (i
= 0; i
< nr_irqs
; i
++) {
1390 struct irq_data
*d
= irq_domain_get_irq_data(domain
, virq
+ i
);
1391 irq_set_handler(virq
+ i
, NULL
);
1392 irq_domain_reset_irq_data(d
);
1396 static int gic_irq_domain_select(struct irq_domain
*d
,
1397 struct irq_fwspec
*fwspec
,
1398 enum irq_domain_bus_token bus_token
)
1401 if (fwspec
->fwnode
!= d
->fwnode
)
1404 /* If this is not DT, then we have a single domain */
1405 if (!is_of_node(fwspec
->fwnode
))
1409 * If this is a PPI and we have a 4th (non-null) parameter,
1410 * then we need to match the partition domain.
1412 if (fwspec
->param_count
>= 4 &&
1413 fwspec
->param
[0] == 1 && fwspec
->param
[3] != 0 &&
1415 return d
== partition_get_domain(gic_data
.ppi_descs
[fwspec
->param
[1]]);
1417 return d
== gic_data
.domain
;
1420 static const struct irq_domain_ops gic_irq_domain_ops
= {
1421 .translate
= gic_irq_domain_translate
,
1422 .alloc
= gic_irq_domain_alloc
,
1423 .free
= gic_irq_domain_free
,
1424 .select
= gic_irq_domain_select
,
1427 static int partition_domain_translate(struct irq_domain
*d
,
1428 struct irq_fwspec
*fwspec
,
1429 unsigned long *hwirq
,
1432 struct device_node
*np
;
1435 if (!gic_data
.ppi_descs
)
1438 np
= of_find_node_by_phandle(fwspec
->param
[3]);
1442 ret
= partition_translate_id(gic_data
.ppi_descs
[fwspec
->param
[1]],
1443 of_node_to_fwnode(np
));
1448 *type
= fwspec
->param
[2] & IRQ_TYPE_SENSE_MASK
;
1453 static const struct irq_domain_ops partition_domain_ops
= {
1454 .translate
= partition_domain_translate
,
1455 .select
= gic_irq_domain_select
,
1458 static bool gic_enable_quirk_msm8996(void *data
)
1460 struct gic_chip_data
*d
= data
;
1462 d
->flags
|= FLAGS_WORKAROUND_GICR_WAKER_MSM8996
;
1467 static bool gic_enable_quirk_hip06_07(void *data
)
1469 struct gic_chip_data
*d
= data
;
1472 * HIP06 GICD_IIDR clashes with GIC-600 product number (despite
1473 * not being an actual ARM implementation). The saving grace is
1474 * that GIC-600 doesn't have ESPI, so nothing to do in that case.
1475 * HIP07 doesn't even have a proper IIDR, and still pretends to
1476 * have ESPI. In both cases, put them right.
1478 if (d
->rdists
.gicd_typer
& GICD_TYPER_ESPI
) {
1479 /* Zero both ESPI and the RES0 field next to it... */
1480 d
->rdists
.gicd_typer
&= ~GENMASK(9, 8);
1487 static const struct gic_quirk gic_quirks
[] = {
1489 .desc
= "GICv3: Qualcomm MSM8996 broken firmware",
1490 .compatible
= "qcom,msm8996-gic-v3",
1491 .init
= gic_enable_quirk_msm8996
,
1494 .desc
= "GICv3: HIP06 erratum 161010803",
1497 .init
= gic_enable_quirk_hip06_07
,
1500 .desc
= "GICv3: HIP07 erratum 161010803",
1503 .init
= gic_enable_quirk_hip06_07
,
1509 static void gic_enable_nmi_support(void)
1513 if (!gic_prio_masking_enabled())
1516 if (gic_has_group0() && !gic_dist_security_disabled()) {
1517 pr_warn("SCR_EL3.FIQ is cleared, cannot enable use of pseudo-NMIs\n");
1521 ppi_nmi_refs
= kcalloc(gic_data
.ppi_nr
, sizeof(*ppi_nmi_refs
), GFP_KERNEL
);
1525 for (i
= 0; i
< gic_data
.ppi_nr
; i
++)
1526 refcount_set(&ppi_nmi_refs
[i
], 0);
1529 * Linux itself doesn't use 1:N distribution, so has no need to
1530 * set PMHE. The only reason to have it set is if EL3 requires it
1531 * (and we can't change it).
1533 if (gic_read_ctlr() & ICC_CTLR_EL1_PMHE_MASK
)
1534 static_branch_enable(&gic_pmr_sync
);
1536 pr_info("%s ICC_PMR_EL1 synchronisation\n",
1537 static_branch_unlikely(&gic_pmr_sync
) ? "Forcing" : "Relaxing");
1539 static_branch_enable(&supports_pseudo_nmis
);
1541 if (static_branch_likely(&supports_deactivate_key
))
1542 gic_eoimode1_chip
.flags
|= IRQCHIP_SUPPORTS_NMI
;
1544 gic_chip
.flags
|= IRQCHIP_SUPPORTS_NMI
;
1547 static int __init
gic_init_bases(void __iomem
*dist_base
,
1548 struct redist_region
*rdist_regs
,
1549 u32 nr_redist_regions
,
1551 struct fwnode_handle
*handle
)
1556 if (!is_hyp_mode_available())
1557 static_branch_disable(&supports_deactivate_key
);
1559 if (static_branch_likely(&supports_deactivate_key
))
1560 pr_info("GIC: Using split EOI/Deactivate mode\n");
1562 gic_data
.fwnode
= handle
;
1563 gic_data
.dist_base
= dist_base
;
1564 gic_data
.redist_regions
= rdist_regs
;
1565 gic_data
.nr_redist_regions
= nr_redist_regions
;
1566 gic_data
.redist_stride
= redist_stride
;
1569 * Find out how many interrupts are supported.
1571 typer
= readl_relaxed(gic_data
.dist_base
+ GICD_TYPER
);
1572 gic_data
.rdists
.gicd_typer
= typer
;
1574 gic_enable_quirks(readl_relaxed(gic_data
.dist_base
+ GICD_IIDR
),
1575 gic_quirks
, &gic_data
);
1577 pr_info("%d SPIs implemented\n", GIC_LINE_NR
- 32);
1578 pr_info("%d Extended SPIs implemented\n", GIC_ESPI_NR
);
1580 gic_data
.rdists
.gicd_typer2
= readl_relaxed(gic_data
.dist_base
+ GICD_TYPER2
);
1582 gic_data
.domain
= irq_domain_create_tree(handle
, &gic_irq_domain_ops
,
1584 irq_domain_update_bus_token(gic_data
.domain
, DOMAIN_BUS_WIRED
);
1585 gic_data
.rdists
.rdist
= alloc_percpu(typeof(*gic_data
.rdists
.rdist
));
1586 gic_data
.rdists
.has_rvpeid
= true;
1587 gic_data
.rdists
.has_vlpis
= true;
1588 gic_data
.rdists
.has_direct_lpi
= true;
1590 if (WARN_ON(!gic_data
.domain
) || WARN_ON(!gic_data
.rdists
.rdist
)) {
1595 gic_data
.has_rss
= !!(typer
& GICD_TYPER_RSS
);
1596 pr_info("Distributor has %sRange Selector support\n",
1597 gic_data
.has_rss
? "" : "no ");
1599 if (typer
& GICD_TYPER_MBIS
) {
1600 err
= mbi_init(handle
, gic_data
.domain
);
1602 pr_err("Failed to initialize MBIs\n");
1605 set_handle_irq(gic_handle_irq
);
1607 gic_update_rdist_properties();
1614 if (gic_dist_supports_lpis()) {
1615 its_init(handle
, &gic_data
.rdists
, gic_data
.domain
);
1618 if (IS_ENABLED(CONFIG_ARM_GIC_V2M
))
1619 gicv2m_init(handle
, gic_data
.domain
);
1622 gic_enable_nmi_support();
1627 if (gic_data
.domain
)
1628 irq_domain_remove(gic_data
.domain
);
1629 free_percpu(gic_data
.rdists
.rdist
);
1633 static int __init
gic_validate_dist_version(void __iomem
*dist_base
)
1635 u32 reg
= readl_relaxed(dist_base
+ GICD_PIDR2
) & GIC_PIDR2_ARCH_MASK
;
1637 if (reg
!= GIC_PIDR2_ARCH_GICv3
&& reg
!= GIC_PIDR2_ARCH_GICv4
)
1643 /* Create all possible partitions at boot time */
1644 static void __init
gic_populate_ppi_partitions(struct device_node
*gic_node
)
1646 struct device_node
*parts_node
, *child_part
;
1647 int part_idx
= 0, i
;
1649 struct partition_affinity
*parts
;
1651 parts_node
= of_get_child_by_name(gic_node
, "ppi-partitions");
1655 gic_data
.ppi_descs
= kcalloc(gic_data
.ppi_nr
, sizeof(*gic_data
.ppi_descs
), GFP_KERNEL
);
1656 if (!gic_data
.ppi_descs
)
1659 nr_parts
= of_get_child_count(parts_node
);
1664 parts
= kcalloc(nr_parts
, sizeof(*parts
), GFP_KERNEL
);
1665 if (WARN_ON(!parts
))
1668 for_each_child_of_node(parts_node
, child_part
) {
1669 struct partition_affinity
*part
;
1672 part
= &parts
[part_idx
];
1674 part
->partition_id
= of_node_to_fwnode(child_part
);
1676 pr_info("GIC: PPI partition %pOFn[%d] { ",
1677 child_part
, part_idx
);
1679 n
= of_property_count_elems_of_size(child_part
, "affinity",
1683 for (i
= 0; i
< n
; i
++) {
1686 struct device_node
*cpu_node
;
1688 err
= of_property_read_u32_index(child_part
, "affinity",
1693 cpu_node
= of_find_node_by_phandle(cpu_phandle
);
1694 if (WARN_ON(!cpu_node
))
1697 cpu
= of_cpu_node_to_id(cpu_node
);
1698 if (WARN_ON(cpu
< 0))
1701 pr_cont("%pOF[%d] ", cpu_node
, cpu
);
1703 cpumask_set_cpu(cpu
, &part
->mask
);
1710 for (i
= 0; i
< gic_data
.ppi_nr
; i
++) {
1712 struct partition_desc
*desc
;
1713 struct irq_fwspec ppi_fwspec
= {
1714 .fwnode
= gic_data
.fwnode
,
1717 [0] = GIC_IRQ_TYPE_PARTITION
,
1719 [2] = IRQ_TYPE_NONE
,
1723 irq
= irq_create_fwspec_mapping(&ppi_fwspec
);
1726 desc
= partition_create_desc(gic_data
.fwnode
, parts
, nr_parts
,
1727 irq
, &partition_domain_ops
);
1731 gic_data
.ppi_descs
[i
] = desc
;
1735 of_node_put(parts_node
);
1738 static void __init
gic_of_setup_kvm_info(struct device_node
*node
)
1744 gic_v3_kvm_info
.type
= GIC_V3
;
1746 gic_v3_kvm_info
.maint_irq
= irq_of_parse_and_map(node
, 0);
1747 if (!gic_v3_kvm_info
.maint_irq
)
1750 if (of_property_read_u32(node
, "#redistributor-regions",
1754 gicv_idx
+= 3; /* Also skip GICD, GICC, GICH */
1755 ret
= of_address_to_resource(node
, gicv_idx
, &r
);
1757 gic_v3_kvm_info
.vcpu
= r
;
1759 gic_v3_kvm_info
.has_v4
= gic_data
.rdists
.has_vlpis
;
1760 gic_set_kvm_info(&gic_v3_kvm_info
);
1763 static int __init
gic_of_init(struct device_node
*node
, struct device_node
*parent
)
1765 void __iomem
*dist_base
;
1766 struct redist_region
*rdist_regs
;
1768 u32 nr_redist_regions
;
1771 dist_base
= of_iomap(node
, 0);
1773 pr_err("%pOF: unable to map gic dist registers\n", node
);
1777 err
= gic_validate_dist_version(dist_base
);
1779 pr_err("%pOF: no distributor detected, giving up\n", node
);
1780 goto out_unmap_dist
;
1783 if (of_property_read_u32(node
, "#redistributor-regions", &nr_redist_regions
))
1784 nr_redist_regions
= 1;
1786 rdist_regs
= kcalloc(nr_redist_regions
, sizeof(*rdist_regs
),
1790 goto out_unmap_dist
;
1793 for (i
= 0; i
< nr_redist_regions
; i
++) {
1794 struct resource res
;
1797 ret
= of_address_to_resource(node
, 1 + i
, &res
);
1798 rdist_regs
[i
].redist_base
= of_iomap(node
, 1 + i
);
1799 if (ret
|| !rdist_regs
[i
].redist_base
) {
1800 pr_err("%pOF: couldn't map region %d\n", node
, i
);
1802 goto out_unmap_rdist
;
1804 rdist_regs
[i
].phys_base
= res
.start
;
1807 if (of_property_read_u64(node
, "redistributor-stride", &redist_stride
))
1810 gic_enable_of_quirks(node
, gic_quirks
, &gic_data
);
1812 err
= gic_init_bases(dist_base
, rdist_regs
, nr_redist_regions
,
1813 redist_stride
, &node
->fwnode
);
1815 goto out_unmap_rdist
;
1817 gic_populate_ppi_partitions(node
);
1819 if (static_branch_likely(&supports_deactivate_key
))
1820 gic_of_setup_kvm_info(node
);
1824 for (i
= 0; i
< nr_redist_regions
; i
++)
1825 if (rdist_regs
[i
].redist_base
)
1826 iounmap(rdist_regs
[i
].redist_base
);
1833 IRQCHIP_DECLARE(gic_v3
, "arm,gic-v3", gic_of_init
);
1838 void __iomem
*dist_base
;
1839 struct redist_region
*redist_regs
;
1840 u32 nr_redist_regions
;
1844 phys_addr_t vcpu_base
;
1845 } acpi_data __initdata
;
1848 gic_acpi_register_redist(phys_addr_t phys_base
, void __iomem
*redist_base
)
1850 static int count
= 0;
1852 acpi_data
.redist_regs
[count
].phys_base
= phys_base
;
1853 acpi_data
.redist_regs
[count
].redist_base
= redist_base
;
1854 acpi_data
.redist_regs
[count
].single_redist
= acpi_data
.single_redist
;
1859 gic_acpi_parse_madt_redist(union acpi_subtable_headers
*header
,
1860 const unsigned long end
)
1862 struct acpi_madt_generic_redistributor
*redist
=
1863 (struct acpi_madt_generic_redistributor
*)header
;
1864 void __iomem
*redist_base
;
1866 redist_base
= ioremap(redist
->base_address
, redist
->length
);
1868 pr_err("Couldn't map GICR region @%llx\n", redist
->base_address
);
1872 gic_acpi_register_redist(redist
->base_address
, redist_base
);
1877 gic_acpi_parse_madt_gicc(union acpi_subtable_headers
*header
,
1878 const unsigned long end
)
1880 struct acpi_madt_generic_interrupt
*gicc
=
1881 (struct acpi_madt_generic_interrupt
*)header
;
1882 u32 reg
= readl_relaxed(acpi_data
.dist_base
+ GICD_PIDR2
) & GIC_PIDR2_ARCH_MASK
;
1883 u32 size
= reg
== GIC_PIDR2_ARCH_GICv4
? SZ_64K
* 4 : SZ_64K
* 2;
1884 void __iomem
*redist_base
;
1886 /* GICC entry which has !ACPI_MADT_ENABLED is not unusable so skip */
1887 if (!(gicc
->flags
& ACPI_MADT_ENABLED
))
1890 redist_base
= ioremap(gicc
->gicr_base_address
, size
);
1894 gic_acpi_register_redist(gicc
->gicr_base_address
, redist_base
);
1898 static int __init
gic_acpi_collect_gicr_base(void)
1900 acpi_tbl_entry_handler redist_parser
;
1901 enum acpi_madt_type type
;
1903 if (acpi_data
.single_redist
) {
1904 type
= ACPI_MADT_TYPE_GENERIC_INTERRUPT
;
1905 redist_parser
= gic_acpi_parse_madt_gicc
;
1907 type
= ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR
;
1908 redist_parser
= gic_acpi_parse_madt_redist
;
1911 /* Collect redistributor base addresses in GICR entries */
1912 if (acpi_table_parse_madt(type
, redist_parser
, 0) > 0)
1915 pr_info("No valid GICR entries exist\n");
1919 static int __init
gic_acpi_match_gicr(union acpi_subtable_headers
*header
,
1920 const unsigned long end
)
1922 /* Subtable presence means that redist exists, that's it */
1926 static int __init
gic_acpi_match_gicc(union acpi_subtable_headers
*header
,
1927 const unsigned long end
)
1929 struct acpi_madt_generic_interrupt
*gicc
=
1930 (struct acpi_madt_generic_interrupt
*)header
;
1933 * If GICC is enabled and has valid gicr base address, then it means
1934 * GICR base is presented via GICC
1936 if ((gicc
->flags
& ACPI_MADT_ENABLED
) && gicc
->gicr_base_address
)
1940 * It's perfectly valid firmware can pass disabled GICC entry, driver
1941 * should not treat as errors, skip the entry instead of probe fail.
1943 if (!(gicc
->flags
& ACPI_MADT_ENABLED
))
1949 static int __init
gic_acpi_count_gicr_regions(void)
1954 * Count how many redistributor regions we have. It is not allowed
1955 * to mix redistributor description, GICR and GICC subtables have to be
1956 * mutually exclusive.
1958 count
= acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR
,
1959 gic_acpi_match_gicr
, 0);
1961 acpi_data
.single_redist
= false;
1965 count
= acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT
,
1966 gic_acpi_match_gicc
, 0);
1968 acpi_data
.single_redist
= true;
1973 static bool __init
acpi_validate_gic_table(struct acpi_subtable_header
*header
,
1974 struct acpi_probe_entry
*ape
)
1976 struct acpi_madt_generic_distributor
*dist
;
1979 dist
= (struct acpi_madt_generic_distributor
*)header
;
1980 if (dist
->version
!= ape
->driver_data
)
1983 /* We need to do that exercise anyway, the sooner the better */
1984 count
= gic_acpi_count_gicr_regions();
1988 acpi_data
.nr_redist_regions
= count
;
1992 static int __init
gic_acpi_parse_virt_madt_gicc(union acpi_subtable_headers
*header
,
1993 const unsigned long end
)
1995 struct acpi_madt_generic_interrupt
*gicc
=
1996 (struct acpi_madt_generic_interrupt
*)header
;
1998 static int first_madt
= true;
2000 /* Skip unusable CPUs */
2001 if (!(gicc
->flags
& ACPI_MADT_ENABLED
))
2004 maint_irq_mode
= (gicc
->flags
& ACPI_MADT_VGIC_IRQ_MODE
) ?
2005 ACPI_EDGE_SENSITIVE
: ACPI_LEVEL_SENSITIVE
;
2010 acpi_data
.maint_irq
= gicc
->vgic_interrupt
;
2011 acpi_data
.maint_irq_mode
= maint_irq_mode
;
2012 acpi_data
.vcpu_base
= gicc
->gicv_base_address
;
2018 * The maintenance interrupt and GICV should be the same for every CPU
2020 if ((acpi_data
.maint_irq
!= gicc
->vgic_interrupt
) ||
2021 (acpi_data
.maint_irq_mode
!= maint_irq_mode
) ||
2022 (acpi_data
.vcpu_base
!= gicc
->gicv_base_address
))
2028 static bool __init
gic_acpi_collect_virt_info(void)
2032 count
= acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT
,
2033 gic_acpi_parse_virt_madt_gicc
, 0);
2038 #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
2039 #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
2040 #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
2042 static void __init
gic_acpi_setup_kvm_info(void)
2046 if (!gic_acpi_collect_virt_info()) {
2047 pr_warn("Unable to get hardware information used for virtualization\n");
2051 gic_v3_kvm_info
.type
= GIC_V3
;
2053 irq
= acpi_register_gsi(NULL
, acpi_data
.maint_irq
,
2054 acpi_data
.maint_irq_mode
,
2059 gic_v3_kvm_info
.maint_irq
= irq
;
2061 if (acpi_data
.vcpu_base
) {
2062 struct resource
*vcpu
= &gic_v3_kvm_info
.vcpu
;
2064 vcpu
->flags
= IORESOURCE_MEM
;
2065 vcpu
->start
= acpi_data
.vcpu_base
;
2066 vcpu
->end
= vcpu
->start
+ ACPI_GICV2_VCPU_MEM_SIZE
- 1;
2069 gic_v3_kvm_info
.has_v4
= gic_data
.rdists
.has_vlpis
;
2070 gic_set_kvm_info(&gic_v3_kvm_info
);
2074 gic_acpi_init(struct acpi_subtable_header
*header
, const unsigned long end
)
2076 struct acpi_madt_generic_distributor
*dist
;
2077 struct fwnode_handle
*domain_handle
;
2081 /* Get distributor base address */
2082 dist
= (struct acpi_madt_generic_distributor
*)header
;
2083 acpi_data
.dist_base
= ioremap(dist
->base_address
,
2084 ACPI_GICV3_DIST_MEM_SIZE
);
2085 if (!acpi_data
.dist_base
) {
2086 pr_err("Unable to map GICD registers\n");
2090 err
= gic_validate_dist_version(acpi_data
.dist_base
);
2092 pr_err("No distributor detected at @%p, giving up\n",
2093 acpi_data
.dist_base
);
2094 goto out_dist_unmap
;
2097 size
= sizeof(*acpi_data
.redist_regs
) * acpi_data
.nr_redist_regions
;
2098 acpi_data
.redist_regs
= kzalloc(size
, GFP_KERNEL
);
2099 if (!acpi_data
.redist_regs
) {
2101 goto out_dist_unmap
;
2104 err
= gic_acpi_collect_gicr_base();
2106 goto out_redist_unmap
;
2108 domain_handle
= irq_domain_alloc_fwnode(&dist
->base_address
);
2109 if (!domain_handle
) {
2111 goto out_redist_unmap
;
2114 err
= gic_init_bases(acpi_data
.dist_base
, acpi_data
.redist_regs
,
2115 acpi_data
.nr_redist_regions
, 0, domain_handle
);
2117 goto out_fwhandle_free
;
2119 acpi_set_irq_model(ACPI_IRQ_MODEL_GIC
, domain_handle
);
2121 if (static_branch_likely(&supports_deactivate_key
))
2122 gic_acpi_setup_kvm_info();
2127 irq_domain_free_fwnode(domain_handle
);
2129 for (i
= 0; i
< acpi_data
.nr_redist_regions
; i
++)
2130 if (acpi_data
.redist_regs
[i
].redist_base
)
2131 iounmap(acpi_data
.redist_regs
[i
].redist_base
);
2132 kfree(acpi_data
.redist_regs
);
2134 iounmap(acpi_data
.dist_base
);
2137 IRQCHIP_ACPI_DECLARE(gic_v3
, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR
,
2138 acpi_validate_gic_table
, ACPI_MADT_GIC_VERSION_V3
,
2140 IRQCHIP_ACPI_DECLARE(gic_v4
, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR
,
2141 acpi_validate_gic_table
, ACPI_MADT_GIC_VERSION_V4
,
2143 IRQCHIP_ACPI_DECLARE(gic_v3_or_v4
, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR
,
2144 acpi_validate_gic_table
, ACPI_MADT_GIC_VERSION_NONE
,