xref: /openbmc/linux/drivers/irqchip/irq-mips-gic.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
18a19b8f1SAndrew Bresticker /*
28a19b8f1SAndrew Bresticker  * This file is subject to the terms and conditions of the GNU General Public
38a19b8f1SAndrew Bresticker  * License.  See the file "COPYING" in the main directory of this archive
48a19b8f1SAndrew Bresticker  * for more details.
58a19b8f1SAndrew Bresticker  *
68a19b8f1SAndrew Bresticker  * Copyright (C) 2008 Ralf Baechle (ralf@linux-mips.org)
78a19b8f1SAndrew Bresticker  * Copyright (C) 2012 MIPS Technologies, Inc.  All rights reserved.
88a19b8f1SAndrew Bresticker  */
91f19aee0SMatt Redfearn 
101f19aee0SMatt Redfearn #define pr_fmt(fmt) "irq-mips-gic: " fmt
111f19aee0SMatt Redfearn 
12357a9c4bSGeert Uytterhoeven #include <linux/bitfield.h>
138a19b8f1SAndrew Bresticker #include <linux/bitmap.h>
14fb8f7be1SAndrew Bresticker #include <linux/clocksource.h>
15da61fcf9SPaul Burton #include <linux/cpuhotplug.h>
168a19b8f1SAndrew Bresticker #include <linux/init.h>
1718743d27SAndrew Bresticker #include <linux/interrupt.h>
18fb8f7be1SAndrew Bresticker #include <linux/irq.h>
1941a83e06SJoel Porquet #include <linux/irqchip.h>
201982752fSMarc Zyngier #include <linux/irqdomain.h>
21a7057270SAndrew Bresticker #include <linux/of_address.h>
22aa493737SPaul Burton #include <linux/percpu.h>
2318743d27SAndrew Bresticker #include <linux/sched.h>
248a19b8f1SAndrew Bresticker #include <linux/smp.h>
258a19b8f1SAndrew Bresticker 
26e83f7e02SPaul Burton #include <asm/mips-cps.h>
278a19b8f1SAndrew Bresticker #include <asm/setup.h>
288a19b8f1SAndrew Bresticker #include <asm/traps.h>
298a19b8f1SAndrew Bresticker 
30a7057270SAndrew Bresticker #include <dt-bindings/interrupt-controller/mips-gic.h>
31a7057270SAndrew Bresticker 
32b11d4c1fSPaul Burton #define GIC_MAX_INTRS		256
33aa493737SPaul Burton #define GIC_MAX_LONGS		BITS_TO_LONGS(GIC_MAX_INTRS)
348a19b8f1SAndrew Bresticker 
35b11d4c1fSPaul Burton /* Add 2 to convert GIC CPU pin to core interrupt */
36b11d4c1fSPaul Burton #define GIC_CPU_PIN_OFFSET	2
378a19b8f1SAndrew Bresticker 
38b11d4c1fSPaul Burton /* Mapped interrupt to pin X, then GIC will generate the vector (X+1). */
39b11d4c1fSPaul Burton #define GIC_PIN_TO_VEC_OFFSET	1
402af70a96SQais Yousef 
41b11d4c1fSPaul Burton /* Convert between local/shared IRQ number and GIC HW IRQ number. */
42b11d4c1fSPaul Burton #define GIC_LOCAL_HWIRQ_BASE	0
43b11d4c1fSPaul Burton #define GIC_LOCAL_TO_HWIRQ(x)	(GIC_LOCAL_HWIRQ_BASE + (x))
44b11d4c1fSPaul Burton #define GIC_HWIRQ_TO_LOCAL(x)	((x) - GIC_LOCAL_HWIRQ_BASE)
45b11d4c1fSPaul Burton #define GIC_SHARED_HWIRQ_BASE	GIC_NUM_LOCAL_INTRS
46b11d4c1fSPaul Burton #define GIC_SHARED_TO_HWIRQ(x)	(GIC_SHARED_HWIRQ_BASE + (x))
47b11d4c1fSPaul Burton #define GIC_HWIRQ_TO_SHARED(x)	((x) - GIC_SHARED_HWIRQ_BASE)
48b11d4c1fSPaul Burton 
49582e2b4aSPaul Burton void __iomem *mips_gic_base;
508a19b8f1SAndrew Bresticker 
51b0e453ffSWei Yongjun static DEFINE_PER_CPU_READ_MOSTLY(unsigned long[GIC_MAX_LONGS], pcpu_masks);
528a19b8f1SAndrew Bresticker 
533d6a0e41SJiaxun Yang static DEFINE_RAW_SPINLOCK(gic_lock);
54c49581a4SAndrew Bresticker static struct irq_domain *gic_irq_domain;
55fbd55241SAndrew Bresticker static int gic_shared_intrs;
563263d085SAndrew Bresticker static unsigned int gic_cpu_pin;
574a6a3ea3SAndrew Bresticker static struct irq_chip gic_level_irq_controller, gic_edge_irq_controller;
588190cc57SSamuel Holland 
598190cc57SSamuel Holland #ifdef CONFIG_GENERIC_IRQ_IPI
6061dc367eSPaul Burton static DECLARE_BITMAP(ipi_resrv, GIC_MAX_INTRS);
6161dc367eSPaul Burton static DECLARE_BITMAP(ipi_available, GIC_MAX_INTRS);
628190cc57SSamuel Holland #endif /* CONFIG_GENERIC_IRQ_IPI */
638a19b8f1SAndrew Bresticker 
64da61fcf9SPaul Burton static struct gic_all_vpes_chip_data {
65da61fcf9SPaul Burton 	u32	map;
66da61fcf9SPaul Burton 	bool	mask;
67da61fcf9SPaul Burton } gic_all_vpes_chip_data[GIC_NUM_LOCAL_INTRS];
68da61fcf9SPaul Burton 
gic_clear_pcpu_masks(unsigned int intr)697778c4b2SPaul Burton static void gic_clear_pcpu_masks(unsigned int intr)
705f68fea0SAndrew Bresticker {
717778c4b2SPaul Burton 	unsigned int i;
725f68fea0SAndrew Bresticker 
737778c4b2SPaul Burton 	/* Clear the interrupt's bit in all pcpu_masks */
747778c4b2SPaul Burton 	for_each_possible_cpu(i)
757778c4b2SPaul Burton 		clear_bit(intr, per_cpu_ptr(pcpu_masks, i));
76835d2b45SPaul Burton }
77835d2b45SPaul Burton 
gic_local_irq_is_routable(int intr)78e9de688dSAndrew Bresticker static bool gic_local_irq_is_routable(int intr)
79e9de688dSAndrew Bresticker {
80e9de688dSAndrew Bresticker 	u32 vpe_ctl;
81e9de688dSAndrew Bresticker 
82e9de688dSAndrew Bresticker 	/* All local interrupts are routable in EIC mode. */
83e9de688dSAndrew Bresticker 	if (cpu_has_veic)
84e9de688dSAndrew Bresticker 		return true;
85e9de688dSAndrew Bresticker 
860d0cf58cSPaul Burton 	vpe_ctl = read_gic_vl_ctl();
87e9de688dSAndrew Bresticker 	switch (intr) {
88e9de688dSAndrew Bresticker 	case GIC_LOCAL_INT_TIMER:
890d0cf58cSPaul Burton 		return vpe_ctl & GIC_VX_CTL_TIMER_ROUTABLE;
90e9de688dSAndrew Bresticker 	case GIC_LOCAL_INT_PERFCTR:
910d0cf58cSPaul Burton 		return vpe_ctl & GIC_VX_CTL_PERFCNT_ROUTABLE;
92e9de688dSAndrew Bresticker 	case GIC_LOCAL_INT_FDC:
930d0cf58cSPaul Burton 		return vpe_ctl & GIC_VX_CTL_FDC_ROUTABLE;
94e9de688dSAndrew Bresticker 	case GIC_LOCAL_INT_SWINT0:
95e9de688dSAndrew Bresticker 	case GIC_LOCAL_INT_SWINT1:
960d0cf58cSPaul Burton 		return vpe_ctl & GIC_VX_CTL_SWINT_ROUTABLE;
97e9de688dSAndrew Bresticker 	default:
98e9de688dSAndrew Bresticker 		return true;
99e9de688dSAndrew Bresticker 	}
100e9de688dSAndrew Bresticker }
101e9de688dSAndrew Bresticker 
gic_bind_eic_interrupt(int irq,int set)1023263d085SAndrew Bresticker static void gic_bind_eic_interrupt(int irq, int set)
1038a19b8f1SAndrew Bresticker {
1048a19b8f1SAndrew Bresticker 	/* Convert irq vector # to hw int # */
1058a19b8f1SAndrew Bresticker 	irq -= GIC_PIN_TO_VEC_OFFSET;
1068a19b8f1SAndrew Bresticker 
1078a19b8f1SAndrew Bresticker 	/* Set irq to use shadow set */
1080d0cf58cSPaul Burton 	write_gic_vl_eic_shadow_set(irq, set);
1098a19b8f1SAndrew Bresticker }
1108a19b8f1SAndrew Bresticker 
gic_send_ipi(struct irq_data * d,unsigned int cpu)111bb11cff3SQais Yousef static void gic_send_ipi(struct irq_data *d, unsigned int cpu)
1128a19b8f1SAndrew Bresticker {
113bb11cff3SQais Yousef 	irq_hw_number_t hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(d));
114bb11cff3SQais Yousef 
1153680746aSPaul Burton 	write_gic_wedge(GIC_WEDGE_RW | hwirq);
1168a19b8f1SAndrew Bresticker }
1178a19b8f1SAndrew Bresticker 
gic_get_c0_compare_int(void)118e9de688dSAndrew Bresticker int gic_get_c0_compare_int(void)
119e9de688dSAndrew Bresticker {
120e9de688dSAndrew Bresticker 	if (!gic_local_irq_is_routable(GIC_LOCAL_INT_TIMER))
121e9de688dSAndrew Bresticker 		return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
122e9de688dSAndrew Bresticker 	return irq_create_mapping(gic_irq_domain,
123e9de688dSAndrew Bresticker 				  GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_TIMER));
124e9de688dSAndrew Bresticker }
125e9de688dSAndrew Bresticker 
gic_get_c0_perfcount_int(void)126e9de688dSAndrew Bresticker int gic_get_c0_perfcount_int(void)
127e9de688dSAndrew Bresticker {
128e9de688dSAndrew Bresticker 	if (!gic_local_irq_is_routable(GIC_LOCAL_INT_PERFCTR)) {
1297e3e6cb2SJames Hogan 		/* Is the performance counter shared with the timer? */
130e9de688dSAndrew Bresticker 		if (cp0_perfcount_irq < 0)
131e9de688dSAndrew Bresticker 			return -1;
132e9de688dSAndrew Bresticker 		return MIPS_CPU_IRQ_BASE + cp0_perfcount_irq;
133e9de688dSAndrew Bresticker 	}
134e9de688dSAndrew Bresticker 	return irq_create_mapping(gic_irq_domain,
135e9de688dSAndrew Bresticker 				  GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_PERFCTR));
136e9de688dSAndrew Bresticker }
137e9de688dSAndrew Bresticker 
gic_get_c0_fdc_int(void)1386429e2b6SJames Hogan int gic_get_c0_fdc_int(void)
1396429e2b6SJames Hogan {
1406429e2b6SJames Hogan 	if (!gic_local_irq_is_routable(GIC_LOCAL_INT_FDC)) {
1416429e2b6SJames Hogan 		/* Is the FDC IRQ even present? */
1426429e2b6SJames Hogan 		if (cp0_fdc_irq < 0)
1436429e2b6SJames Hogan 			return -1;
1446429e2b6SJames Hogan 		return MIPS_CPU_IRQ_BASE + cp0_fdc_irq;
1456429e2b6SJames Hogan 	}
1466429e2b6SJames Hogan 
1476429e2b6SJames Hogan 	return irq_create_mapping(gic_irq_domain,
1486429e2b6SJames Hogan 				  GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_FDC));
1496429e2b6SJames Hogan }
1506429e2b6SJames Hogan 
gic_handle_shared_int(bool chained)1511b3ed367SRabin Vincent static void gic_handle_shared_int(bool chained)
1528a19b8f1SAndrew Bresticker {
153046a6ee2SMarc Zyngier 	unsigned int intr;
1548f5ee79cSAndrew Bresticker 	unsigned long *pcpu_mask;
1558f5ee79cSAndrew Bresticker 	DECLARE_BITMAP(pending, GIC_MAX_INTRS);
1568a19b8f1SAndrew Bresticker 
1578a19b8f1SAndrew Bresticker 	/* Get per-cpu bitmaps */
158aa493737SPaul Burton 	pcpu_mask = this_cpu_ptr(pcpu_masks);
1598a19b8f1SAndrew Bresticker 
1607778c4b2SPaul Burton 	if (mips_cm_is64)
161e98fcb2aSPaul Burton 		__ioread64_copy(pending, addr_gic_pend(),
162e98fcb2aSPaul Burton 				DIV_ROUND_UP(gic_shared_intrs, 64));
1637778c4b2SPaul Burton 	else
164e98fcb2aSPaul Burton 		__ioread32_copy(pending, addr_gic_pend(),
165e98fcb2aSPaul Burton 				DIV_ROUND_UP(gic_shared_intrs, 32));
1668a19b8f1SAndrew Bresticker 
167fbd55241SAndrew Bresticker 	bitmap_and(pending, pending, pcpu_mask, gic_shared_intrs);
1688a19b8f1SAndrew Bresticker 
169cae750baSPaul Burton 	for_each_set_bit(intr, pending, gic_shared_intrs) {
1701b3ed367SRabin Vincent 		if (chained)
171046a6ee2SMarc Zyngier 			generic_handle_domain_irq(gic_irq_domain,
172046a6ee2SMarc Zyngier 						  GIC_SHARED_TO_HWIRQ(intr));
1731b3ed367SRabin Vincent 		else
1741fee9db9SMarc Zyngier 			do_domain_IRQ(gic_irq_domain,
1751fee9db9SMarc Zyngier 				      GIC_SHARED_TO_HWIRQ(intr));
176d7eb4f2eSQais Yousef 	}
1778a19b8f1SAndrew Bresticker }
1788a19b8f1SAndrew Bresticker 
gic_mask_irq(struct irq_data * d)1798a19b8f1SAndrew Bresticker static void gic_mask_irq(struct irq_data *d)
1808a19b8f1SAndrew Bresticker {
1817778c4b2SPaul Burton 	unsigned int intr = GIC_HWIRQ_TO_SHARED(d->hwirq);
1827778c4b2SPaul Burton 
18390019f8fSPaul Burton 	write_gic_rmask(intr);
1847778c4b2SPaul Burton 	gic_clear_pcpu_masks(intr);
1858a19b8f1SAndrew Bresticker }
1868a19b8f1SAndrew Bresticker 
gic_unmask_irq(struct irq_data * d)1878a19b8f1SAndrew Bresticker static void gic_unmask_irq(struct irq_data *d)
1888a19b8f1SAndrew Bresticker {
1897778c4b2SPaul Burton 	unsigned int intr = GIC_HWIRQ_TO_SHARED(d->hwirq);
1907778c4b2SPaul Burton 	unsigned int cpu;
1917778c4b2SPaul Burton 
19290019f8fSPaul Burton 	write_gic_smask(intr);
1937778c4b2SPaul Burton 
1947778c4b2SPaul Burton 	gic_clear_pcpu_masks(intr);
195d9f82930SPaul Burton 	cpu = cpumask_first(irq_data_get_effective_affinity_mask(d));
1967778c4b2SPaul Burton 	set_bit(intr, per_cpu_ptr(pcpu_masks, cpu));
1978a19b8f1SAndrew Bresticker }
1988a19b8f1SAndrew Bresticker 
gic_ack_irq(struct irq_data * d)1995561c9e4SAndrew Bresticker static void gic_ack_irq(struct irq_data *d)
2005561c9e4SAndrew Bresticker {
201e9de688dSAndrew Bresticker 	unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
202c49581a4SAndrew Bresticker 
2033680746aSPaul Burton 	write_gic_wedge(irq);
2045561c9e4SAndrew Bresticker }
2055561c9e4SAndrew Bresticker 
gic_set_type(struct irq_data * d,unsigned int type)20695150ae8SAndrew Bresticker static int gic_set_type(struct irq_data *d, unsigned int type)
20795150ae8SAndrew Bresticker {
2085af3e93eSPaul Burton 	unsigned int irq, pol, trig, dual;
20995150ae8SAndrew Bresticker 	unsigned long flags;
2105af3e93eSPaul Burton 
2115af3e93eSPaul Burton 	irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
2128a19b8f1SAndrew Bresticker 
2133d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
21495150ae8SAndrew Bresticker 	switch (type & IRQ_TYPE_SENSE_MASK) {
21595150ae8SAndrew Bresticker 	case IRQ_TYPE_EDGE_FALLING:
2165af3e93eSPaul Burton 		pol = GIC_POL_FALLING_EDGE;
2175af3e93eSPaul Burton 		trig = GIC_TRIG_EDGE;
2185af3e93eSPaul Burton 		dual = GIC_DUAL_SINGLE;
21995150ae8SAndrew Bresticker 		break;
22095150ae8SAndrew Bresticker 	case IRQ_TYPE_EDGE_RISING:
2215af3e93eSPaul Burton 		pol = GIC_POL_RISING_EDGE;
2225af3e93eSPaul Burton 		trig = GIC_TRIG_EDGE;
2235af3e93eSPaul Burton 		dual = GIC_DUAL_SINGLE;
22495150ae8SAndrew Bresticker 		break;
22595150ae8SAndrew Bresticker 	case IRQ_TYPE_EDGE_BOTH:
2265af3e93eSPaul Burton 		pol = 0; /* Doesn't matter */
2275af3e93eSPaul Burton 		trig = GIC_TRIG_EDGE;
2285af3e93eSPaul Burton 		dual = GIC_DUAL_DUAL;
22995150ae8SAndrew Bresticker 		break;
23095150ae8SAndrew Bresticker 	case IRQ_TYPE_LEVEL_LOW:
2315af3e93eSPaul Burton 		pol = GIC_POL_ACTIVE_LOW;
2325af3e93eSPaul Burton 		trig = GIC_TRIG_LEVEL;
2335af3e93eSPaul Burton 		dual = GIC_DUAL_SINGLE;
23495150ae8SAndrew Bresticker 		break;
23595150ae8SAndrew Bresticker 	case IRQ_TYPE_LEVEL_HIGH:
23695150ae8SAndrew Bresticker 	default:
2375af3e93eSPaul Burton 		pol = GIC_POL_ACTIVE_HIGH;
2385af3e93eSPaul Burton 		trig = GIC_TRIG_LEVEL;
2395af3e93eSPaul Burton 		dual = GIC_DUAL_SINGLE;
24095150ae8SAndrew Bresticker 		break;
24195150ae8SAndrew Bresticker 	}
24295150ae8SAndrew Bresticker 
2435af3e93eSPaul Burton 	change_gic_pol(irq, pol);
2445af3e93eSPaul Burton 	change_gic_trig(irq, trig);
2455af3e93eSPaul Burton 	change_gic_dual(irq, dual);
2465af3e93eSPaul Burton 
2475af3e93eSPaul Burton 	if (trig == GIC_TRIG_EDGE)
248a595fc51SThomas Gleixner 		irq_set_chip_handler_name_locked(d, &gic_edge_irq_controller,
2494a6a3ea3SAndrew Bresticker 						 handle_edge_irq, NULL);
250a595fc51SThomas Gleixner 	else
251a595fc51SThomas Gleixner 		irq_set_chip_handler_name_locked(d, &gic_level_irq_controller,
2524a6a3ea3SAndrew Bresticker 						 handle_level_irq, NULL);
2533d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
25495150ae8SAndrew Bresticker 
25595150ae8SAndrew Bresticker 	return 0;
25695150ae8SAndrew Bresticker }
25795150ae8SAndrew Bresticker 
25895150ae8SAndrew Bresticker #ifdef CONFIG_SMP
gic_set_affinity(struct irq_data * d,const struct cpumask * cpumask,bool force)2598a19b8f1SAndrew Bresticker static int gic_set_affinity(struct irq_data *d, const struct cpumask *cpumask,
2608a19b8f1SAndrew Bresticker 			    bool force)
2618a19b8f1SAndrew Bresticker {
262e9de688dSAndrew Bresticker 	unsigned int irq = GIC_HWIRQ_TO_SHARED(d->hwirq);
2638a19b8f1SAndrew Bresticker 	unsigned long flags;
26407df8bfeSPaul Burton 	unsigned int cpu;
2658a19b8f1SAndrew Bresticker 
26607df8bfeSPaul Burton 	cpu = cpumask_first_and(cpumask, cpu_online_mask);
26707df8bfeSPaul Burton 	if (cpu >= NR_CPUS)
26814d160abSAndrew Bresticker 		return -EINVAL;
2698a19b8f1SAndrew Bresticker 
2708a19b8f1SAndrew Bresticker 	/* Assumption : cpumask refers to a single CPU */
2713d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
2728a19b8f1SAndrew Bresticker 
2738a19b8f1SAndrew Bresticker 	/* Re-route this IRQ */
27407df8bfeSPaul Burton 	write_gic_map_vp(irq, BIT(mips_cm_vp_id(cpu)));
2758a19b8f1SAndrew Bresticker 
2768a19b8f1SAndrew Bresticker 	/* Update the pcpu_masks */
2777778c4b2SPaul Burton 	gic_clear_pcpu_masks(irq);
2787778c4b2SPaul Burton 	if (read_gic_mask(irq))
27907df8bfeSPaul Burton 		set_bit(irq, per_cpu_ptr(pcpu_masks, cpu));
2808a19b8f1SAndrew Bresticker 
28118416e45SMarc Zyngier 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
2823d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
2838a19b8f1SAndrew Bresticker 
2847f15a648SPaul Burton 	return IRQ_SET_MASK_OK;
2858a19b8f1SAndrew Bresticker }
2868a19b8f1SAndrew Bresticker #endif
2878a19b8f1SAndrew Bresticker 
2884a6a3ea3SAndrew Bresticker static struct irq_chip gic_level_irq_controller = {
2894a6a3ea3SAndrew Bresticker 	.name			=	"MIPS GIC",
2904a6a3ea3SAndrew Bresticker 	.irq_mask		=	gic_mask_irq,
2914a6a3ea3SAndrew Bresticker 	.irq_unmask		=	gic_unmask_irq,
2924a6a3ea3SAndrew Bresticker 	.irq_set_type		=	gic_set_type,
2934a6a3ea3SAndrew Bresticker #ifdef CONFIG_SMP
2944a6a3ea3SAndrew Bresticker 	.irq_set_affinity	=	gic_set_affinity,
2954a6a3ea3SAndrew Bresticker #endif
2964a6a3ea3SAndrew Bresticker };
2974a6a3ea3SAndrew Bresticker 
2984a6a3ea3SAndrew Bresticker static struct irq_chip gic_edge_irq_controller = {
2998a19b8f1SAndrew Bresticker 	.name			=	"MIPS GIC",
3005561c9e4SAndrew Bresticker 	.irq_ack		=	gic_ack_irq,
3018a19b8f1SAndrew Bresticker 	.irq_mask		=	gic_mask_irq,
3028a19b8f1SAndrew Bresticker 	.irq_unmask		=	gic_unmask_irq,
30395150ae8SAndrew Bresticker 	.irq_set_type		=	gic_set_type,
3048a19b8f1SAndrew Bresticker #ifdef CONFIG_SMP
3058a19b8f1SAndrew Bresticker 	.irq_set_affinity	=	gic_set_affinity,
3068a19b8f1SAndrew Bresticker #endif
307bb11cff3SQais Yousef 	.ipi_send_single	=	gic_send_ipi,
3088a19b8f1SAndrew Bresticker };
3098a19b8f1SAndrew Bresticker 
gic_handle_local_int(bool chained)3101b3ed367SRabin Vincent static void gic_handle_local_int(bool chained)
311e9de688dSAndrew Bresticker {
312e9de688dSAndrew Bresticker 	unsigned long pending, masked;
313046a6ee2SMarc Zyngier 	unsigned int intr;
314e9de688dSAndrew Bresticker 
3159da3c645SPaul Burton 	pending = read_gic_vl_pend();
3169da3c645SPaul Burton 	masked = read_gic_vl_mask();
317e9de688dSAndrew Bresticker 
318e9de688dSAndrew Bresticker 	bitmap_and(&pending, &pending, &masked, GIC_NUM_LOCAL_INTRS);
319e9de688dSAndrew Bresticker 
3200f4ed158SPaul Burton 	for_each_set_bit(intr, &pending, GIC_NUM_LOCAL_INTRS) {
3211b3ed367SRabin Vincent 		if (chained)
322046a6ee2SMarc Zyngier 			generic_handle_domain_irq(gic_irq_domain,
323046a6ee2SMarc Zyngier 						  GIC_LOCAL_TO_HWIRQ(intr));
3241b3ed367SRabin Vincent 		else
3251fee9db9SMarc Zyngier 			do_domain_IRQ(gic_irq_domain,
3261fee9db9SMarc Zyngier 				      GIC_LOCAL_TO_HWIRQ(intr));
327d7eb4f2eSQais Yousef 	}
328e9de688dSAndrew Bresticker }
329e9de688dSAndrew Bresticker 
gic_mask_local_irq(struct irq_data * d)330e9de688dSAndrew Bresticker static void gic_mask_local_irq(struct irq_data *d)
331e9de688dSAndrew Bresticker {
332e9de688dSAndrew Bresticker 	int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
333e9de688dSAndrew Bresticker 
3349da3c645SPaul Burton 	write_gic_vl_rmask(BIT(intr));
335e9de688dSAndrew Bresticker }
336e9de688dSAndrew Bresticker 
gic_unmask_local_irq(struct irq_data * d)337e9de688dSAndrew Bresticker static void gic_unmask_local_irq(struct irq_data *d)
338e9de688dSAndrew Bresticker {
339e9de688dSAndrew Bresticker 	int intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
340e9de688dSAndrew Bresticker 
3419da3c645SPaul Burton 	write_gic_vl_smask(BIT(intr));
342e9de688dSAndrew Bresticker }
343e9de688dSAndrew Bresticker 
344e9de688dSAndrew Bresticker static struct irq_chip gic_local_irq_controller = {
345e9de688dSAndrew Bresticker 	.name			=	"MIPS GIC Local",
346e9de688dSAndrew Bresticker 	.irq_mask		=	gic_mask_local_irq,
347e9de688dSAndrew Bresticker 	.irq_unmask		=	gic_unmask_local_irq,
348e9de688dSAndrew Bresticker };
349e9de688dSAndrew Bresticker 
gic_mask_local_irq_all_vpes(struct irq_data * d)350e9de688dSAndrew Bresticker static void gic_mask_local_irq_all_vpes(struct irq_data *d)
351e9de688dSAndrew Bresticker {
352da61fcf9SPaul Burton 	struct gic_all_vpes_chip_data *cd;
353e9de688dSAndrew Bresticker 	unsigned long flags;
354da61fcf9SPaul Burton 	int intr, cpu;
355da61fcf9SPaul Burton 
356da61fcf9SPaul Burton 	intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
357da61fcf9SPaul Burton 	cd = irq_data_get_irq_chip_data(d);
358da61fcf9SPaul Burton 	cd->mask = false;
359e9de688dSAndrew Bresticker 
3603d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
361da61fcf9SPaul Burton 	for_each_online_cpu(cpu) {
362da61fcf9SPaul Burton 		write_gic_vl_other(mips_cm_vp_id(cpu));
3639da3c645SPaul Burton 		write_gic_vo_rmask(BIT(intr));
364e9de688dSAndrew Bresticker 	}
3653d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
366e9de688dSAndrew Bresticker }
367e9de688dSAndrew Bresticker 
gic_unmask_local_irq_all_vpes(struct irq_data * d)368e9de688dSAndrew Bresticker static void gic_unmask_local_irq_all_vpes(struct irq_data *d)
369e9de688dSAndrew Bresticker {
370da61fcf9SPaul Burton 	struct gic_all_vpes_chip_data *cd;
371e9de688dSAndrew Bresticker 	unsigned long flags;
372da61fcf9SPaul Burton 	int intr, cpu;
373da61fcf9SPaul Burton 
374da61fcf9SPaul Burton 	intr = GIC_HWIRQ_TO_LOCAL(d->hwirq);
375da61fcf9SPaul Burton 	cd = irq_data_get_irq_chip_data(d);
376da61fcf9SPaul Burton 	cd->mask = true;
377e9de688dSAndrew Bresticker 
3783d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
379da61fcf9SPaul Burton 	for_each_online_cpu(cpu) {
380da61fcf9SPaul Burton 		write_gic_vl_other(mips_cm_vp_id(cpu));
3819da3c645SPaul Burton 		write_gic_vo_smask(BIT(intr));
382e9de688dSAndrew Bresticker 	}
3833d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
384e9de688dSAndrew Bresticker }
385e9de688dSAndrew Bresticker 
gic_all_vpes_irq_cpu_online(void)386dd098a0eSMarc Zyngier static void gic_all_vpes_irq_cpu_online(void)
387da61fcf9SPaul Burton {
388dd098a0eSMarc Zyngier 	static const unsigned int local_intrs[] = {
389dd098a0eSMarc Zyngier 		GIC_LOCAL_INT_TIMER,
390dd098a0eSMarc Zyngier 		GIC_LOCAL_INT_PERFCTR,
391dd098a0eSMarc Zyngier 		GIC_LOCAL_INT_FDC,
392dd098a0eSMarc Zyngier 	};
393dd098a0eSMarc Zyngier 	unsigned long flags;
394dd098a0eSMarc Zyngier 	int i;
395dd098a0eSMarc Zyngier 
3963d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
397dd098a0eSMarc Zyngier 
398dd098a0eSMarc Zyngier 	for (i = 0; i < ARRAY_SIZE(local_intrs); i++) {
399dd098a0eSMarc Zyngier 		unsigned int intr = local_intrs[i];
400da61fcf9SPaul Burton 		struct gic_all_vpes_chip_data *cd;
401da61fcf9SPaul Burton 
4022c6c9c04SJiaxun Yang 		if (!gic_local_irq_is_routable(intr))
4032c6c9c04SJiaxun Yang 			continue;
404dd098a0eSMarc Zyngier 		cd = &gic_all_vpes_chip_data[intr];
4056d4d367dSPaul Burton 		write_gic_vl_map(mips_gic_vx_map_reg(intr), cd->map);
406da61fcf9SPaul Burton 		if (cd->mask)
407da61fcf9SPaul Burton 			write_gic_vl_smask(BIT(intr));
408da61fcf9SPaul Burton 	}
409da61fcf9SPaul Burton 
4103d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
411dd098a0eSMarc Zyngier }
412dd098a0eSMarc Zyngier 
413e9de688dSAndrew Bresticker static struct irq_chip gic_all_vpes_local_irq_controller = {
414e9de688dSAndrew Bresticker 	.name			= "MIPS GIC Local",
415e9de688dSAndrew Bresticker 	.irq_mask		= gic_mask_local_irq_all_vpes,
416e9de688dSAndrew Bresticker 	.irq_unmask		= gic_unmask_local_irq_all_vpes,
417e9de688dSAndrew Bresticker };
418e9de688dSAndrew Bresticker 
__gic_irq_dispatch(void)41918743d27SAndrew Bresticker static void __gic_irq_dispatch(void)
4208a19b8f1SAndrew Bresticker {
4211b3ed367SRabin Vincent 	gic_handle_local_int(false);
4221b3ed367SRabin Vincent 	gic_handle_shared_int(false);
42318743d27SAndrew Bresticker }
42418743d27SAndrew Bresticker 
gic_irq_dispatch(struct irq_desc * desc)425bd0b9ac4SThomas Gleixner static void gic_irq_dispatch(struct irq_desc *desc)
42618743d27SAndrew Bresticker {
4271b3ed367SRabin Vincent 	gic_handle_local_int(true);
4281b3ed367SRabin Vincent 	gic_handle_shared_int(true);
42918743d27SAndrew Bresticker }
43018743d27SAndrew Bresticker 
gic_shared_irq_domain_map(struct irq_domain * d,unsigned int virq,irq_hw_number_t hw,unsigned int cpu)431e9de688dSAndrew Bresticker static int gic_shared_irq_domain_map(struct irq_domain *d, unsigned int virq,
4327778c4b2SPaul Burton 				     irq_hw_number_t hw, unsigned int cpu)
433e9de688dSAndrew Bresticker {
434e9de688dSAndrew Bresticker 	int intr = GIC_HWIRQ_TO_SHARED(hw);
435d9f82930SPaul Burton 	struct irq_data *data;
436c49581a4SAndrew Bresticker 	unsigned long flags;
437c49581a4SAndrew Bresticker 
438d9f82930SPaul Burton 	data = irq_get_irq_data(virq);
439d9f82930SPaul Burton 
4403d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
441d3e8cf44SPaul Burton 	write_gic_map_pin(intr, GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin);
4427778c4b2SPaul Burton 	write_gic_map_vp(intr, BIT(mips_cm_vp_id(cpu)));
443d9f82930SPaul Burton 	irq_data_update_effective_affinity(data, cpumask_of(cpu));
4443d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
445c49581a4SAndrew Bresticker 
446c49581a4SAndrew Bresticker 	return 0;
447c49581a4SAndrew Bresticker }
448c49581a4SAndrew Bresticker 
gic_irq_domain_xlate(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,irq_hw_number_t * out_hwirq,unsigned int * out_type)449b87281e7SPaul Burton static int gic_irq_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
450c98c1822SQais Yousef 				const u32 *intspec, unsigned int intsize,
451c98c1822SQais Yousef 				irq_hw_number_t *out_hwirq,
452c98c1822SQais Yousef 				unsigned int *out_type)
453c98c1822SQais Yousef {
454c98c1822SQais Yousef 	if (intsize != 3)
455c98c1822SQais Yousef 		return -EINVAL;
456c98c1822SQais Yousef 
457c98c1822SQais Yousef 	if (intspec[0] == GIC_SHARED)
458c98c1822SQais Yousef 		*out_hwirq = GIC_SHARED_TO_HWIRQ(intspec[1]);
459c98c1822SQais Yousef 	else if (intspec[0] == GIC_LOCAL)
460c98c1822SQais Yousef 		*out_hwirq = GIC_LOCAL_TO_HWIRQ(intspec[1]);
461c98c1822SQais Yousef 	else
462c98c1822SQais Yousef 		return -EINVAL;
463c98c1822SQais Yousef 	*out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
464c98c1822SQais Yousef 
465c98c1822SQais Yousef 	return 0;
466c98c1822SQais Yousef }
467c98c1822SQais Yousef 
gic_irq_domain_map(struct irq_domain * d,unsigned int virq,irq_hw_number_t hwirq)4688ada00a6SMatt Redfearn static int gic_irq_domain_map(struct irq_domain *d, unsigned int virq,
4698ada00a6SMatt Redfearn 			      irq_hw_number_t hwirq)
470c98c1822SQais Yousef {
471da61fcf9SPaul Burton 	struct gic_all_vpes_chip_data *cd;
47263b746b1SPaul Burton 	unsigned long flags;
47363b746b1SPaul Burton 	unsigned int intr;
474da61fcf9SPaul Burton 	int err, cpu;
47563b746b1SPaul Burton 	u32 map;
476c98c1822SQais Yousef 
4778ada00a6SMatt Redfearn 	if (hwirq >= GIC_SHARED_HWIRQ_BASE) {
4788190cc57SSamuel Holland #ifdef CONFIG_GENERIC_IRQ_IPI
479b87281e7SPaul Burton 		/* verify that shared irqs don't conflict with an IPI irq */
480b87281e7SPaul Burton 		if (test_bit(GIC_HWIRQ_TO_SHARED(hwirq), ipi_resrv))
481b87281e7SPaul Burton 			return -EBUSY;
4828190cc57SSamuel Holland #endif /* CONFIG_GENERIC_IRQ_IPI */
483c98c1822SQais Yousef 
484b87281e7SPaul Burton 		err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
485b87281e7SPaul Burton 						    &gic_level_irq_controller,
486b87281e7SPaul Burton 						    NULL);
487b87281e7SPaul Burton 		if (err)
488b87281e7SPaul Burton 			return err;
489b87281e7SPaul Burton 
49018416e45SMarc Zyngier 		irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq)));
491b87281e7SPaul Burton 		return gic_shared_irq_domain_map(d, virq, hwirq, 0);
492c98c1822SQais Yousef 	}
493c98c1822SQais Yousef 
49463b746b1SPaul Burton 	intr = GIC_HWIRQ_TO_LOCAL(hwirq);
49563b746b1SPaul Burton 	map = GIC_MAP_PIN_MAP_TO_PIN | gic_cpu_pin;
49663b746b1SPaul Burton 
497dd098a0eSMarc Zyngier 	/*
498915649daSwangjianli 	 * If adding support for more per-cpu interrupts, keep the
499dd098a0eSMarc Zyngier 	 * array in gic_all_vpes_irq_cpu_online() in sync.
500dd098a0eSMarc Zyngier 	 */
50163b746b1SPaul Burton 	switch (intr) {
502b87281e7SPaul Burton 	case GIC_LOCAL_INT_TIMER:
503b87281e7SPaul Burton 	case GIC_LOCAL_INT_PERFCTR:
504b87281e7SPaul Burton 	case GIC_LOCAL_INT_FDC:
505b87281e7SPaul Burton 		/*
506b87281e7SPaul Burton 		 * HACK: These are all really percpu interrupts, but
507b87281e7SPaul Burton 		 * the rest of the MIPS kernel code does not use the
508b87281e7SPaul Burton 		 * percpu IRQ API for them.
509b87281e7SPaul Burton 		 */
510da61fcf9SPaul Burton 		cd = &gic_all_vpes_chip_data[intr];
511da61fcf9SPaul Burton 		cd->map = map;
512b87281e7SPaul Burton 		err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
513b87281e7SPaul Burton 						    &gic_all_vpes_local_irq_controller,
514da61fcf9SPaul Burton 						    cd);
515b87281e7SPaul Burton 		if (err)
516b87281e7SPaul Burton 			return err;
517b87281e7SPaul Burton 
518b87281e7SPaul Burton 		irq_set_handler(virq, handle_percpu_irq);
519b87281e7SPaul Burton 		break;
520b87281e7SPaul Burton 
521b87281e7SPaul Burton 	default:
522b87281e7SPaul Burton 		err = irq_domain_set_hwirq_and_chip(d, virq, hwirq,
523b87281e7SPaul Burton 						    &gic_local_irq_controller,
524b87281e7SPaul Burton 						    NULL);
525b87281e7SPaul Burton 		if (err)
526b87281e7SPaul Burton 			return err;
527b87281e7SPaul Burton 
528b87281e7SPaul Burton 		irq_set_handler(virq, handle_percpu_devid_irq);
529b87281e7SPaul Burton 		irq_set_percpu_devid(virq);
530b87281e7SPaul Burton 		break;
531c98c1822SQais Yousef 	}
532c98c1822SQais Yousef 
53363b746b1SPaul Burton 	if (!gic_local_irq_is_routable(intr))
53463b746b1SPaul Burton 		return -EPERM;
53563b746b1SPaul Burton 
5363d6a0e41SJiaxun Yang 	raw_spin_lock_irqsave(&gic_lock, flags);
537da61fcf9SPaul Burton 	for_each_online_cpu(cpu) {
538da61fcf9SPaul Burton 		write_gic_vl_other(mips_cm_vp_id(cpu));
5396d4d367dSPaul Burton 		write_gic_vo_map(mips_gic_vx_map_reg(intr), map);
54063b746b1SPaul Burton 	}
5413d6a0e41SJiaxun Yang 	raw_spin_unlock_irqrestore(&gic_lock, flags);
54263b746b1SPaul Burton 
54363b746b1SPaul Burton 	return 0;
544b87281e7SPaul Burton }
545b87281e7SPaul Burton 
gic_irq_domain_alloc(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs,void * arg)5468ada00a6SMatt Redfearn static int gic_irq_domain_alloc(struct irq_domain *d, unsigned int virq,
5478ada00a6SMatt Redfearn 				unsigned int nr_irqs, void *arg)
5488ada00a6SMatt Redfearn {
5498ada00a6SMatt Redfearn 	struct irq_fwspec *fwspec = arg;
5508ada00a6SMatt Redfearn 	irq_hw_number_t hwirq;
5518ada00a6SMatt Redfearn 
5528ada00a6SMatt Redfearn 	if (fwspec->param[0] == GIC_SHARED)
5538ada00a6SMatt Redfearn 		hwirq = GIC_SHARED_TO_HWIRQ(fwspec->param[1]);
5548ada00a6SMatt Redfearn 	else
5558ada00a6SMatt Redfearn 		hwirq = GIC_LOCAL_TO_HWIRQ(fwspec->param[1]);
5568ada00a6SMatt Redfearn 
5578ada00a6SMatt Redfearn 	return gic_irq_domain_map(d, virq, hwirq);
5588ada00a6SMatt Redfearn }
5598ada00a6SMatt Redfearn 
gic_irq_domain_free(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs)560*90e921d7SArnd Bergmann static void gic_irq_domain_free(struct irq_domain *d, unsigned int virq,
561c98c1822SQais Yousef 			 unsigned int nr_irqs)
562c98c1822SQais Yousef {
563c98c1822SQais Yousef }
564c98c1822SQais Yousef 
565b87281e7SPaul Burton static const struct irq_domain_ops gic_irq_domain_ops = {
566b87281e7SPaul Burton 	.xlate = gic_irq_domain_xlate,
567b87281e7SPaul Burton 	.alloc = gic_irq_domain_alloc,
568b87281e7SPaul Burton 	.free = gic_irq_domain_free,
5698ada00a6SMatt Redfearn 	.map = gic_irq_domain_map,
5702af70a96SQais Yousef };
5712af70a96SQais Yousef 
5728190cc57SSamuel Holland #ifdef CONFIG_GENERIC_IRQ_IPI
5738190cc57SSamuel Holland 
gic_ipi_domain_xlate(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,irq_hw_number_t * out_hwirq,unsigned int * out_type)5742af70a96SQais Yousef static int gic_ipi_domain_xlate(struct irq_domain *d, struct device_node *ctrlr,
5752af70a96SQais Yousef 				const u32 *intspec, unsigned int intsize,
5762af70a96SQais Yousef 				irq_hw_number_t *out_hwirq,
5772af70a96SQais Yousef 				unsigned int *out_type)
5782af70a96SQais Yousef {
5792af70a96SQais Yousef 	/*
5802af70a96SQais Yousef 	 * There's nothing to translate here. hwirq is dynamically allocated and
5812af70a96SQais Yousef 	 * the irq type is always edge triggered.
5822af70a96SQais Yousef 	 * */
5832af70a96SQais Yousef 	*out_hwirq = 0;
5842af70a96SQais Yousef 	*out_type = IRQ_TYPE_EDGE_RISING;
5852af70a96SQais Yousef 
5862af70a96SQais Yousef 	return 0;
5872af70a96SQais Yousef }
5882af70a96SQais Yousef 
gic_ipi_domain_alloc(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs,void * arg)5892af70a96SQais Yousef static int gic_ipi_domain_alloc(struct irq_domain *d, unsigned int virq,
5902af70a96SQais Yousef 				unsigned int nr_irqs, void *arg)
5912af70a96SQais Yousef {
5922af70a96SQais Yousef 	struct cpumask *ipimask = arg;
593b87281e7SPaul Burton 	irq_hw_number_t hwirq, base_hwirq;
594b87281e7SPaul Burton 	int cpu, ret, i;
5952af70a96SQais Yousef 
596b87281e7SPaul Burton 	base_hwirq = find_first_bit(ipi_available, gic_shared_intrs);
597b87281e7SPaul Burton 	if (base_hwirq == gic_shared_intrs)
598b87281e7SPaul Burton 		return -ENOMEM;
599b87281e7SPaul Burton 
600b87281e7SPaul Burton 	/* check that we have enough space */
601b87281e7SPaul Burton 	for (i = base_hwirq; i < nr_irqs; i++) {
602b87281e7SPaul Burton 		if (!test_bit(i, ipi_available))
603b87281e7SPaul Burton 			return -EBUSY;
604b87281e7SPaul Burton 	}
605b87281e7SPaul Burton 	bitmap_clear(ipi_available, base_hwirq, nr_irqs);
606b87281e7SPaul Burton 
607b87281e7SPaul Burton 	/* map the hwirq for each cpu consecutively */
608b87281e7SPaul Burton 	i = 0;
609b87281e7SPaul Burton 	for_each_cpu(cpu, ipimask) {
610b87281e7SPaul Burton 		hwirq = GIC_SHARED_TO_HWIRQ(base_hwirq + i);
611b87281e7SPaul Burton 
612b87281e7SPaul Burton 		ret = irq_domain_set_hwirq_and_chip(d, virq + i, hwirq,
613b87281e7SPaul Burton 						    &gic_edge_irq_controller,
614b87281e7SPaul Burton 						    NULL);
6152af70a96SQais Yousef 		if (ret)
616b87281e7SPaul Burton 			goto error;
6172af70a96SQais Yousef 
618b87281e7SPaul Burton 		ret = irq_domain_set_hwirq_and_chip(d->parent, virq + i, hwirq,
6192af70a96SQais Yousef 						    &gic_edge_irq_controller,
6202af70a96SQais Yousef 						    NULL);
6212af70a96SQais Yousef 		if (ret)
6222af70a96SQais Yousef 			goto error;
6232af70a96SQais Yousef 
6242af70a96SQais Yousef 		ret = irq_set_irq_type(virq + i, IRQ_TYPE_EDGE_RISING);
6252af70a96SQais Yousef 		if (ret)
6262af70a96SQais Yousef 			goto error;
627b87281e7SPaul Burton 
628b87281e7SPaul Burton 		ret = gic_shared_irq_domain_map(d, virq + i, hwirq, cpu);
629b87281e7SPaul Burton 		if (ret)
630b87281e7SPaul Burton 			goto error;
631b87281e7SPaul Burton 
632b87281e7SPaul Burton 		i++;
6332af70a96SQais Yousef 	}
6342af70a96SQais Yousef 
6352af70a96SQais Yousef 	return 0;
6362af70a96SQais Yousef error:
637b87281e7SPaul Burton 	bitmap_set(ipi_available, base_hwirq, nr_irqs);
6382af70a96SQais Yousef 	return ret;
6392af70a96SQais Yousef }
6402af70a96SQais Yousef 
gic_ipi_domain_free(struct irq_domain * d,unsigned int virq,unsigned int nr_irqs)641b0e453ffSWei Yongjun static void gic_ipi_domain_free(struct irq_domain *d, unsigned int virq,
6422af70a96SQais Yousef 				unsigned int nr_irqs)
6432af70a96SQais Yousef {
644b87281e7SPaul Burton 	irq_hw_number_t base_hwirq;
645b87281e7SPaul Burton 	struct irq_data *data;
646b87281e7SPaul Burton 
647b87281e7SPaul Burton 	data = irq_get_irq_data(virq);
648b87281e7SPaul Burton 	if (!data)
649b87281e7SPaul Burton 		return;
650b87281e7SPaul Burton 
651b87281e7SPaul Burton 	base_hwirq = GIC_HWIRQ_TO_SHARED(irqd_to_hwirq(data));
652b87281e7SPaul Burton 	bitmap_set(ipi_available, base_hwirq, nr_irqs);
6532af70a96SQais Yousef }
6542af70a96SQais Yousef 
gic_ipi_domain_match(struct irq_domain * d,struct device_node * node,enum irq_domain_bus_token bus_token)655b0e453ffSWei Yongjun static int gic_ipi_domain_match(struct irq_domain *d, struct device_node *node,
6562af70a96SQais Yousef 				enum irq_domain_bus_token bus_token)
6572af70a96SQais Yousef {
6582af70a96SQais Yousef 	bool is_ipi;
6592af70a96SQais Yousef 
6602af70a96SQais Yousef 	switch (bus_token) {
6612af70a96SQais Yousef 	case DOMAIN_BUS_IPI:
6622af70a96SQais Yousef 		is_ipi = d->bus_token == bus_token;
663547aefc4SPaul Burton 		return (!node || to_of_node(d->fwnode) == node) && is_ipi;
6642af70a96SQais Yousef 		break;
6652af70a96SQais Yousef 	default:
6662af70a96SQais Yousef 		return 0;
6672af70a96SQais Yousef 	}
6682af70a96SQais Yousef }
6692af70a96SQais Yousef 
6700b7e815aSTobias Klauser static const struct irq_domain_ops gic_ipi_domain_ops = {
6712af70a96SQais Yousef 	.xlate = gic_ipi_domain_xlate,
6722af70a96SQais Yousef 	.alloc = gic_ipi_domain_alloc,
6732af70a96SQais Yousef 	.free = gic_ipi_domain_free,
6742af70a96SQais Yousef 	.match = gic_ipi_domain_match,
675c49581a4SAndrew Bresticker };
676c49581a4SAndrew Bresticker 
gic_register_ipi_domain(struct device_node * node)6778190cc57SSamuel Holland static int gic_register_ipi_domain(struct device_node *node)
6788190cc57SSamuel Holland {
6798190cc57SSamuel Holland 	struct irq_domain *gic_ipi_domain;
6808190cc57SSamuel Holland 	unsigned int v[2], num_ipis;
6818190cc57SSamuel Holland 
6828190cc57SSamuel Holland 	gic_ipi_domain = irq_domain_add_hierarchy(gic_irq_domain,
6838190cc57SSamuel Holland 						  IRQ_DOMAIN_FLAG_IPI_PER_CPU,
6848190cc57SSamuel Holland 						  GIC_NUM_LOCAL_INTRS + gic_shared_intrs,
6858190cc57SSamuel Holland 						  node, &gic_ipi_domain_ops, NULL);
6868190cc57SSamuel Holland 	if (!gic_ipi_domain) {
6878190cc57SSamuel Holland 		pr_err("Failed to add IPI domain");
6888190cc57SSamuel Holland 		return -ENXIO;
6898190cc57SSamuel Holland 	}
6908190cc57SSamuel Holland 
6918190cc57SSamuel Holland 	irq_domain_update_bus_token(gic_ipi_domain, DOMAIN_BUS_IPI);
6928190cc57SSamuel Holland 
6938190cc57SSamuel Holland 	if (node &&
6948190cc57SSamuel Holland 	    !of_property_read_u32_array(node, "mti,reserved-ipi-vectors", v, 2)) {
6958190cc57SSamuel Holland 		bitmap_set(ipi_resrv, v[0], v[1]);
6968190cc57SSamuel Holland 	} else {
6978190cc57SSamuel Holland 		/*
6988190cc57SSamuel Holland 		 * Reserve 2 interrupts per possible CPU/VP for use as IPIs,
6998190cc57SSamuel Holland 		 * meeting the requirements of arch/mips SMP.
7008190cc57SSamuel Holland 		 */
7018190cc57SSamuel Holland 		num_ipis = 2 * num_possible_cpus();
7028190cc57SSamuel Holland 		bitmap_set(ipi_resrv, gic_shared_intrs - num_ipis, num_ipis);
7038190cc57SSamuel Holland 	}
7048190cc57SSamuel Holland 
7058190cc57SSamuel Holland 	bitmap_copy(ipi_available, ipi_resrv, GIC_MAX_INTRS);
7068190cc57SSamuel Holland 
7078190cc57SSamuel Holland 	return 0;
7088190cc57SSamuel Holland }
7098190cc57SSamuel Holland 
7108190cc57SSamuel Holland #else /* !CONFIG_GENERIC_IRQ_IPI */
7118190cc57SSamuel Holland 
gic_register_ipi_domain(struct device_node * node)7128190cc57SSamuel Holland static inline int gic_register_ipi_domain(struct device_node *node)
7138190cc57SSamuel Holland {
7148190cc57SSamuel Holland 	return 0;
7158190cc57SSamuel Holland }
7168190cc57SSamuel Holland 
7178190cc57SSamuel Holland #endif /* !CONFIG_GENERIC_IRQ_IPI */
7188190cc57SSamuel Holland 
gic_cpu_startup(unsigned int cpu)719da61fcf9SPaul Burton static int gic_cpu_startup(unsigned int cpu)
720da61fcf9SPaul Burton {
721890f6b55SPaul Burton 	/* Enable or disable EIC */
722890f6b55SPaul Burton 	change_gic_vl_ctl(GIC_VX_CTL_EIC,
723890f6b55SPaul Burton 			  cpu_has_veic ? GIC_VX_CTL_EIC : 0);
724890f6b55SPaul Burton 
72525ac19e1SPaul Burton 	/* Clear all local IRQ masks (ie. disable all local interrupts) */
72625ac19e1SPaul Burton 	write_gic_vl_rmask(~0);
72725ac19e1SPaul Burton 
728dd098a0eSMarc Zyngier 	/* Enable desired interrupts */
729dd098a0eSMarc Zyngier 	gic_all_vpes_irq_cpu_online();
730da61fcf9SPaul Burton 
731da61fcf9SPaul Burton 	return 0;
732da61fcf9SPaul Burton }
7338a19b8f1SAndrew Bresticker 
gic_of_init(struct device_node * node,struct device_node * parent)734fbea7541SPaul Burton static int __init gic_of_init(struct device_node *node,
735fbea7541SPaul Burton 			      struct device_node *parent)
7368a19b8f1SAndrew Bresticker {
7378190cc57SSamuel Holland 	unsigned int cpu_vec, i, gicconfig;
738b2b2e584SPaul Burton 	unsigned long reserved;
739fbea7541SPaul Burton 	phys_addr_t gic_base;
740fbea7541SPaul Burton 	struct resource res;
741fbea7541SPaul Burton 	size_t gic_len;
7428190cc57SSamuel Holland 	int ret;
7438a19b8f1SAndrew Bresticker 
744fbea7541SPaul Burton 	/* Find the first available CPU vector. */
745b2b2e584SPaul Burton 	i = 0;
746a08588eaSPaul Burton 	reserved = (C_SW0 | C_SW1) >> __ffs(C_SW0);
747fbea7541SPaul Burton 	while (!of_property_read_u32_index(node, "mti,reserved-cpu-vectors",
748fbea7541SPaul Burton 					   i++, &cpu_vec))
749fbea7541SPaul Burton 		reserved |= BIT(cpu_vec);
750c0a9f72cSAlex Smith 
751b2b2e584SPaul Burton 	cpu_vec = find_first_zero_bit(&reserved, hweight_long(ST0_IM));
752b2b2e584SPaul Burton 	if (cpu_vec == hweight_long(ST0_IM)) {
7531f19aee0SMatt Redfearn 		pr_err("No CPU vectors available\n");
754fbea7541SPaul Burton 		return -ENODEV;
755fbea7541SPaul Burton 	}
7568a19b8f1SAndrew Bresticker 
757fbea7541SPaul Burton 	if (of_address_to_resource(node, 0, &res)) {
758fbea7541SPaul Burton 		/*
759fbea7541SPaul Burton 		 * Probe the CM for the GIC base address if not specified
760fbea7541SPaul Burton 		 * in the device-tree.
761fbea7541SPaul Burton 		 */
762fbea7541SPaul Burton 		if (mips_cm_present()) {
763fbea7541SPaul Burton 			gic_base = read_gcr_gic_base() &
764fbea7541SPaul Burton 				~CM_GCR_GIC_BASE_GICEN;
765fbea7541SPaul Burton 			gic_len = 0x20000;
766666740fdSMatt Redfearn 			pr_warn("Using inherited base address %pa\n",
767666740fdSMatt Redfearn 				&gic_base);
768fbea7541SPaul Burton 		} else {
7691f19aee0SMatt Redfearn 			pr_err("Failed to get memory range\n");
770fbea7541SPaul Burton 			return -ENODEV;
771fbea7541SPaul Burton 		}
772fbea7541SPaul Burton 	} else {
773fbea7541SPaul Burton 		gic_base = res.start;
774fbea7541SPaul Burton 		gic_len = resource_size(&res);
775fbea7541SPaul Burton 	}
7768a19b8f1SAndrew Bresticker 
777fbea7541SPaul Burton 	if (mips_cm_present()) {
778fbea7541SPaul Burton 		write_gcr_gic_base(gic_base | CM_GCR_GIC_BASE_GICEN);
779fbea7541SPaul Burton 		/* Ensure GIC region is enabled before trying to access it */
780fbea7541SPaul Burton 		__sync();
781fbea7541SPaul Burton 	}
782fbea7541SPaul Burton 
7834bdc0d67SChristoph Hellwig 	mips_gic_base = ioremap(gic_base, gic_len);
78471349cc8SWilliam Dean 	if (!mips_gic_base) {
78571349cc8SWilliam Dean 		pr_err("Failed to ioremap gic_base\n");
78671349cc8SWilliam Dean 		return -ENOMEM;
78771349cc8SWilliam Dean 	}
7888a19b8f1SAndrew Bresticker 
7893680746aSPaul Burton 	gicconfig = read_gic_config();
790357a9c4bSGeert Uytterhoeven 	gic_shared_intrs = FIELD_GET(GIC_CONFIG_NUMINTERRUPTS, gicconfig);
7913680746aSPaul Burton 	gic_shared_intrs = (gic_shared_intrs + 1) * 8;
7928a19b8f1SAndrew Bresticker 
7938a19b8f1SAndrew Bresticker 	if (cpu_has_veic) {
7948a19b8f1SAndrew Bresticker 		/* Always use vector 1 in EIC mode */
7958a19b8f1SAndrew Bresticker 		gic_cpu_pin = 0;
7968a19b8f1SAndrew Bresticker 		set_vi_handler(gic_cpu_pin + GIC_PIN_TO_VEC_OFFSET,
7978a19b8f1SAndrew Bresticker 			       __gic_irq_dispatch);
7988a19b8f1SAndrew Bresticker 	} else {
7998a19b8f1SAndrew Bresticker 		gic_cpu_pin = cpu_vec - GIC_CPU_PIN_OFFSET;
8008a19b8f1SAndrew Bresticker 		irq_set_chained_handler(MIPS_CPU_IRQ_BASE + cpu_vec,
8018a19b8f1SAndrew Bresticker 					gic_irq_dispatch);
8028a19b8f1SAndrew Bresticker 	}
8038a19b8f1SAndrew Bresticker 
804a7057270SAndrew Bresticker 	gic_irq_domain = irq_domain_add_simple(node, GIC_NUM_LOCAL_INTRS +
805fbea7541SPaul Burton 					       gic_shared_intrs, 0,
8068a19b8f1SAndrew Bresticker 					       &gic_irq_domain_ops, NULL);
807fbea7541SPaul Burton 	if (!gic_irq_domain) {
8081f19aee0SMatt Redfearn 		pr_err("Failed to add IRQ domain");
809fbea7541SPaul Burton 		return -ENXIO;
810fbea7541SPaul Burton 	}
8118a19b8f1SAndrew Bresticker 
8128190cc57SSamuel Holland 	ret = gic_register_ipi_domain(node);
8138190cc57SSamuel Holland 	if (ret)
8148190cc57SSamuel Holland 		return ret;
81587888bcbSPaul Burton 
81687888bcbSPaul Burton 	board_bind_eic_interrupt = &gic_bind_eic_interrupt;
81787888bcbSPaul Burton 
81887888bcbSPaul Burton 	/* Setup defaults */
81987888bcbSPaul Burton 	for (i = 0; i < gic_shared_intrs; i++) {
82087888bcbSPaul Burton 		change_gic_pol(i, GIC_POL_ACTIVE_HIGH);
82187888bcbSPaul Burton 		change_gic_trig(i, GIC_TRIG_LEVEL);
82290019f8fSPaul Burton 		write_gic_rmask(i);
8238a19b8f1SAndrew Bresticker 	}
824a7057270SAndrew Bresticker 
825da61fcf9SPaul Burton 	return cpuhp_setup_state(CPUHP_AP_IRQ_MIPS_GIC_STARTING,
826da61fcf9SPaul Burton 				 "irqchip/mips/gic:starting",
827da61fcf9SPaul Burton 				 gic_cpu_startup, NULL);
828a7057270SAndrew Bresticker }
829a7057270SAndrew Bresticker IRQCHIP_DECLARE(mips_gic, "mti,gic", gic_of_init);
830