xref: /linux/drivers/irqchip/irq-apple-aic.c (revision a5e8801202b318622ea526aa5625e5f7eceb4d26)
176cde263SHector Martin // SPDX-License-Identifier: GPL-2.0-or-later
276cde263SHector Martin /*
376cde263SHector Martin  * Copyright The Asahi Linux Contributors
476cde263SHector Martin  *
576cde263SHector Martin  * Based on irq-lpc32xx:
676cde263SHector Martin  *   Copyright 2015-2016 Vladimir Zapolskiy <vz@mleia.com>
776cde263SHector Martin  * Based on irq-bcm2836:
876cde263SHector Martin  *   Copyright 2015 Broadcom
976cde263SHector Martin  */
1076cde263SHector Martin 
1176cde263SHector Martin /*
1276cde263SHector Martin  * AIC is a fairly simple interrupt controller with the following features:
1376cde263SHector Martin  *
1476cde263SHector Martin  * - 896 level-triggered hardware IRQs
1576cde263SHector Martin  *   - Single mask bit per IRQ
1676cde263SHector Martin  *   - Per-IRQ affinity setting
1776cde263SHector Martin  *   - Automatic masking on event delivery (auto-ack)
1876cde263SHector Martin  *   - Software triggering (ORed with hw line)
1976cde263SHector Martin  * - 2 per-CPU IPIs (meant as "self" and "other", but they are
2076cde263SHector Martin  *   interchangeable if not symmetric)
2176cde263SHector Martin  * - Automatic prioritization (single event/ack register per CPU, lower IRQs =
2276cde263SHector Martin  *   higher priority)
2376cde263SHector Martin  * - Automatic masking on ack
2476cde263SHector Martin  * - Default "this CPU" register view and explicit per-CPU views
2576cde263SHector Martin  *
2676cde263SHector Martin  * In addition, this driver also handles FIQs, as these are routed to the same
2776cde263SHector Martin  * IRQ vector. These are used for Fast IPIs (TODO), the ARMv8 timer IRQs, and
2876cde263SHector Martin  * performance counters (TODO).
2976cde263SHector Martin  *
3076cde263SHector Martin  * Implementation notes:
3176cde263SHector Martin  *
3276cde263SHector Martin  * - This driver creates two IRQ domains, one for HW IRQs and internal FIQs,
3376cde263SHector Martin  *   and one for IPIs.
3476cde263SHector Martin  * - Since Linux needs more than 2 IPIs, we implement a software IRQ controller
3576cde263SHector Martin  *   and funnel all IPIs into one per-CPU IPI (the second "self" IPI is unused).
3676cde263SHector Martin  * - FIQ hwirq numbers are assigned after true hwirqs, and are per-cpu.
3776cde263SHector Martin  * - DT bindings use 3-cell form (like GIC):
3876cde263SHector Martin  *   - <0 nr flags> - hwirq #nr
3976cde263SHector Martin  *   - <1 nr flags> - FIQ #nr
4076cde263SHector Martin  *     - nr=0  Physical HV timer
4176cde263SHector Martin  *     - nr=1  Virtual HV timer
4276cde263SHector Martin  *     - nr=2  Physical guest timer
4376cde263SHector Martin  *     - nr=3  Virtual guest timer
4476cde263SHector Martin  */
4576cde263SHector Martin 
4676cde263SHector Martin #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
4776cde263SHector Martin 
4876cde263SHector Martin #include <linux/bits.h>
4976cde263SHector Martin #include <linux/bitfield.h>
5076cde263SHector Martin #include <linux/cpuhotplug.h>
5176cde263SHector Martin #include <linux/io.h>
5276cde263SHector Martin #include <linux/irqchip.h>
53b6ca556cSMarc Zyngier #include <linux/irqchip/arm-vgic-info.h>
5476cde263SHector Martin #include <linux/irqdomain.h>
5576cde263SHector Martin #include <linux/limits.h>
5676cde263SHector Martin #include <linux/of_address.h>
5776cde263SHector Martin #include <linux/slab.h>
5876cde263SHector Martin #include <asm/exception.h>
5976cde263SHector Martin #include <asm/sysreg.h>
6076cde263SHector Martin #include <asm/virt.h>
6176cde263SHector Martin 
6276cde263SHector Martin #include <dt-bindings/interrupt-controller/apple-aic.h>
6376cde263SHector Martin 
6476cde263SHector Martin /*
6576cde263SHector Martin  * AIC registers (MMIO)
6676cde263SHector Martin  */
6776cde263SHector Martin 
6876cde263SHector Martin #define AIC_INFO		0x0004
6976cde263SHector Martin #define AIC_INFO_NR_HW		GENMASK(15, 0)
7076cde263SHector Martin 
7176cde263SHector Martin #define AIC_CONFIG		0x0010
7276cde263SHector Martin 
7376cde263SHector Martin #define AIC_WHOAMI		0x2000
7476cde263SHector Martin #define AIC_EVENT		0x2004
7576cde263SHector Martin #define AIC_EVENT_TYPE		GENMASK(31, 16)
7676cde263SHector Martin #define AIC_EVENT_NUM		GENMASK(15, 0)
7776cde263SHector Martin 
7876cde263SHector Martin #define AIC_EVENT_TYPE_HW	1
7976cde263SHector Martin #define AIC_EVENT_TYPE_IPI	4
8076cde263SHector Martin #define AIC_EVENT_IPI_OTHER	1
8176cde263SHector Martin #define AIC_EVENT_IPI_SELF	2
8276cde263SHector Martin 
8376cde263SHector Martin #define AIC_IPI_SEND		0x2008
8476cde263SHector Martin #define AIC_IPI_ACK		0x200c
8576cde263SHector Martin #define AIC_IPI_MASK_SET	0x2024
8676cde263SHector Martin #define AIC_IPI_MASK_CLR	0x2028
8776cde263SHector Martin 
8876cde263SHector Martin #define AIC_IPI_SEND_CPU(cpu)	BIT(cpu)
8976cde263SHector Martin 
9076cde263SHector Martin #define AIC_IPI_OTHER		BIT(0)
9176cde263SHector Martin #define AIC_IPI_SELF		BIT(31)
9276cde263SHector Martin 
9376cde263SHector Martin #define AIC_TARGET_CPU		0x3000
9476cde263SHector Martin #define AIC_SW_SET		0x4000
9576cde263SHector Martin #define AIC_SW_CLR		0x4080
9676cde263SHector Martin #define AIC_MASK_SET		0x4100
9776cde263SHector Martin #define AIC_MASK_CLR		0x4180
9876cde263SHector Martin 
9976cde263SHector Martin #define AIC_CPU_IPI_SET(cpu)	(0x5008 + ((cpu) << 7))
10076cde263SHector Martin #define AIC_CPU_IPI_CLR(cpu)	(0x500c + ((cpu) << 7))
10176cde263SHector Martin #define AIC_CPU_IPI_MASK_SET(cpu) (0x5024 + ((cpu) << 7))
10276cde263SHector Martin #define AIC_CPU_IPI_MASK_CLR(cpu) (0x5028 + ((cpu) << 7))
10376cde263SHector Martin 
10476cde263SHector Martin #define MASK_REG(x)		(4 * ((x) >> 5))
10576cde263SHector Martin #define MASK_BIT(x)		BIT((x) & GENMASK(4, 0))
10676cde263SHector Martin 
10776cde263SHector Martin /*
10876cde263SHector Martin  * IMP-DEF sysregs that control FIQ sources
10976cde263SHector Martin  * Note: sysreg-based IPIs are not supported yet.
11076cde263SHector Martin  */
11176cde263SHector Martin 
11276cde263SHector Martin /* Core PMC control register */
11376cde263SHector Martin #define SYS_IMP_APL_PMCR0_EL1		sys_reg(3, 1, 15, 0, 0)
11476cde263SHector Martin #define PMCR0_IMODE			GENMASK(10, 8)
11576cde263SHector Martin #define PMCR0_IMODE_OFF			0
11676cde263SHector Martin #define PMCR0_IMODE_PMI			1
11776cde263SHector Martin #define PMCR0_IMODE_AIC			2
11876cde263SHector Martin #define PMCR0_IMODE_HALT		3
11976cde263SHector Martin #define PMCR0_IMODE_FIQ			4
12076cde263SHector Martin #define PMCR0_IACT			BIT(11)
12176cde263SHector Martin 
12276cde263SHector Martin /* IPI request registers */
12376cde263SHector Martin #define SYS_IMP_APL_IPI_RR_LOCAL_EL1	sys_reg(3, 5, 15, 0, 0)
12476cde263SHector Martin #define SYS_IMP_APL_IPI_RR_GLOBAL_EL1	sys_reg(3, 5, 15, 0, 1)
12576cde263SHector Martin #define IPI_RR_CPU			GENMASK(7, 0)
12676cde263SHector Martin /* Cluster only used for the GLOBAL register */
12776cde263SHector Martin #define IPI_RR_CLUSTER			GENMASK(23, 16)
12876cde263SHector Martin #define IPI_RR_TYPE			GENMASK(29, 28)
12976cde263SHector Martin #define IPI_RR_IMMEDIATE		0
13076cde263SHector Martin #define IPI_RR_RETRACT			1
13176cde263SHector Martin #define IPI_RR_DEFERRED			2
13276cde263SHector Martin #define IPI_RR_NOWAKE			3
13376cde263SHector Martin 
13476cde263SHector Martin /* IPI status register */
13576cde263SHector Martin #define SYS_IMP_APL_IPI_SR_EL1		sys_reg(3, 5, 15, 1, 1)
13676cde263SHector Martin #define IPI_SR_PENDING			BIT(0)
13776cde263SHector Martin 
13876cde263SHector Martin /* Guest timer FIQ enable register */
13976cde263SHector Martin #define SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2	sys_reg(3, 5, 15, 1, 3)
14076cde263SHector Martin #define VM_TMR_FIQ_ENABLE_V		BIT(0)
14176cde263SHector Martin #define VM_TMR_FIQ_ENABLE_P		BIT(1)
14276cde263SHector Martin 
14376cde263SHector Martin /* Deferred IPI countdown register */
14476cde263SHector Martin #define SYS_IMP_APL_IPI_CR_EL1		sys_reg(3, 5, 15, 3, 1)
14576cde263SHector Martin 
14676cde263SHector Martin /* Uncore PMC control register */
14776cde263SHector Martin #define SYS_IMP_APL_UPMCR0_EL1		sys_reg(3, 7, 15, 0, 4)
14876cde263SHector Martin #define UPMCR0_IMODE			GENMASK(18, 16)
14976cde263SHector Martin #define UPMCR0_IMODE_OFF		0
15076cde263SHector Martin #define UPMCR0_IMODE_AIC		2
15176cde263SHector Martin #define UPMCR0_IMODE_HALT		3
15276cde263SHector Martin #define UPMCR0_IMODE_FIQ		4
15376cde263SHector Martin 
15476cde263SHector Martin /* Uncore PMC status register */
15576cde263SHector Martin #define SYS_IMP_APL_UPMSR_EL1		sys_reg(3, 7, 15, 6, 4)
15676cde263SHector Martin #define UPMSR_IACT			BIT(0)
15776cde263SHector Martin 
15876cde263SHector Martin #define AIC_NR_FIQ		4
15976cde263SHector Martin #define AIC_NR_SWIPI		32
16076cde263SHector Martin 
16176cde263SHector Martin /*
16276cde263SHector Martin  * FIQ hwirq index definitions: FIQ sources use the DT binding defines
16376cde263SHector Martin  * directly, except that timers are special. At the irqchip level, the
16476cde263SHector Martin  * two timer types are represented by their access method: _EL0 registers
16576cde263SHector Martin  * or _EL02 registers. In the DT binding, the timers are represented
16676cde263SHector Martin  * by their purpose (HV or guest). This mapping is for when the kernel is
16776cde263SHector Martin  * running at EL2 (with VHE). When the kernel is running at EL1, the
16876cde263SHector Martin  * mapping differs and aic_irq_domain_translate() performs the remapping.
16976cde263SHector Martin  */
17076cde263SHector Martin 
17176cde263SHector Martin #define AIC_TMR_EL0_PHYS	AIC_TMR_HV_PHYS
17276cde263SHector Martin #define AIC_TMR_EL0_VIRT	AIC_TMR_HV_VIRT
17376cde263SHector Martin #define AIC_TMR_EL02_PHYS	AIC_TMR_GUEST_PHYS
17476cde263SHector Martin #define AIC_TMR_EL02_VIRT	AIC_TMR_GUEST_VIRT
17576cde263SHector Martin 
17676cde263SHector Martin struct aic_irq_chip {
17776cde263SHector Martin 	void __iomem *base;
17876cde263SHector Martin 	struct irq_domain *hw_domain;
17976cde263SHector Martin 	struct irq_domain *ipi_domain;
180*a5e88012SMarc Zyngier 	struct {
181*a5e88012SMarc Zyngier 		cpumask_t aff;
182*a5e88012SMarc Zyngier 	} *fiq_aff[AIC_NR_FIQ];
18376cde263SHector Martin 	int nr_hw;
18476cde263SHector Martin };
18576cde263SHector Martin 
18676cde263SHector Martin static DEFINE_PER_CPU(uint32_t, aic_fiq_unmasked);
18776cde263SHector Martin 
18876cde263SHector Martin static DEFINE_PER_CPU(atomic_t, aic_vipi_flag);
18976cde263SHector Martin static DEFINE_PER_CPU(atomic_t, aic_vipi_enable);
19076cde263SHector Martin 
19176cde263SHector Martin static struct aic_irq_chip *aic_irqc;
19276cde263SHector Martin 
19376cde263SHector Martin static void aic_handle_ipi(struct pt_regs *regs);
19476cde263SHector Martin 
19576cde263SHector Martin static u32 aic_ic_read(struct aic_irq_chip *ic, u32 reg)
19676cde263SHector Martin {
19776cde263SHector Martin 	return readl_relaxed(ic->base + reg);
19876cde263SHector Martin }
19976cde263SHector Martin 
20076cde263SHector Martin static void aic_ic_write(struct aic_irq_chip *ic, u32 reg, u32 val)
20176cde263SHector Martin {
20276cde263SHector Martin 	writel_relaxed(val, ic->base + reg);
20376cde263SHector Martin }
20476cde263SHector Martin 
20576cde263SHector Martin /*
20676cde263SHector Martin  * IRQ irqchip
20776cde263SHector Martin  */
20876cde263SHector Martin 
20976cde263SHector Martin static void aic_irq_mask(struct irq_data *d)
21076cde263SHector Martin {
21176cde263SHector Martin 	struct aic_irq_chip *ic = irq_data_get_irq_chip_data(d);
21276cde263SHector Martin 
21376cde263SHector Martin 	aic_ic_write(ic, AIC_MASK_SET + MASK_REG(irqd_to_hwirq(d)),
21476cde263SHector Martin 		     MASK_BIT(irqd_to_hwirq(d)));
21576cde263SHector Martin }
21676cde263SHector Martin 
21776cde263SHector Martin static void aic_irq_unmask(struct irq_data *d)
21876cde263SHector Martin {
21976cde263SHector Martin 	struct aic_irq_chip *ic = irq_data_get_irq_chip_data(d);
22076cde263SHector Martin 
22176cde263SHector Martin 	aic_ic_write(ic, AIC_MASK_CLR + MASK_REG(d->hwirq),
22276cde263SHector Martin 		     MASK_BIT(irqd_to_hwirq(d)));
22376cde263SHector Martin }
22476cde263SHector Martin 
22576cde263SHector Martin static void aic_irq_eoi(struct irq_data *d)
22676cde263SHector Martin {
22776cde263SHector Martin 	/*
22876cde263SHector Martin 	 * Reading the interrupt reason automatically acknowledges and masks
22976cde263SHector Martin 	 * the IRQ, so we just unmask it here if needed.
23076cde263SHector Martin 	 */
23160a1cd10SSven Peter 	if (!irqd_irq_masked(d))
23276cde263SHector Martin 		aic_irq_unmask(d);
23376cde263SHector Martin }
23476cde263SHector Martin 
23576cde263SHector Martin static void __exception_irq_entry aic_handle_irq(struct pt_regs *regs)
23676cde263SHector Martin {
23776cde263SHector Martin 	struct aic_irq_chip *ic = aic_irqc;
23876cde263SHector Martin 	u32 event, type, irq;
23976cde263SHector Martin 
24076cde263SHector Martin 	do {
24176cde263SHector Martin 		/*
24276cde263SHector Martin 		 * We cannot use a relaxed read here, as reads from DMA buffers
24376cde263SHector Martin 		 * need to be ordered after the IRQ fires.
24476cde263SHector Martin 		 */
24576cde263SHector Martin 		event = readl(ic->base + AIC_EVENT);
24676cde263SHector Martin 		type = FIELD_GET(AIC_EVENT_TYPE, event);
24776cde263SHector Martin 		irq = FIELD_GET(AIC_EVENT_NUM, event);
24876cde263SHector Martin 
24976cde263SHector Martin 		if (type == AIC_EVENT_TYPE_HW)
2500953fb26SMark Rutland 			generic_handle_domain_irq(aic_irqc->hw_domain, irq);
25176cde263SHector Martin 		else if (type == AIC_EVENT_TYPE_IPI && irq == 1)
25276cde263SHector Martin 			aic_handle_ipi(regs);
25376cde263SHector Martin 		else if (event != 0)
25476cde263SHector Martin 			pr_err_ratelimited("Unknown IRQ event %d, %d\n", type, irq);
25576cde263SHector Martin 	} while (event);
25676cde263SHector Martin 
25776cde263SHector Martin 	/*
25876cde263SHector Martin 	 * vGIC maintenance interrupts end up here too, so we need to check
25976cde263SHector Martin 	 * for them separately. This should never trigger if KVM is working
26076cde263SHector Martin 	 * properly, because it will have already taken care of clearing it
26176cde263SHector Martin 	 * on guest exit before this handler runs.
26276cde263SHector Martin 	 */
26376cde263SHector Martin 	if (is_kernel_in_hyp_mode() && (read_sysreg_s(SYS_ICH_HCR_EL2) & ICH_HCR_EN) &&
26476cde263SHector Martin 		read_sysreg_s(SYS_ICH_MISR_EL2) != 0) {
26576cde263SHector Martin 		pr_err_ratelimited("vGIC IRQ fired and not handled by KVM, disabling.\n");
26676cde263SHector Martin 		sysreg_clear_set_s(SYS_ICH_HCR_EL2, ICH_HCR_EN, 0);
26776cde263SHector Martin 	}
26876cde263SHector Martin }
26976cde263SHector Martin 
27076cde263SHector Martin static int aic_irq_set_affinity(struct irq_data *d,
27176cde263SHector Martin 				const struct cpumask *mask_val, bool force)
27276cde263SHector Martin {
27376cde263SHector Martin 	irq_hw_number_t hwirq = irqd_to_hwirq(d);
27476cde263SHector Martin 	struct aic_irq_chip *ic = irq_data_get_irq_chip_data(d);
27576cde263SHector Martin 	int cpu;
27676cde263SHector Martin 
27776cde263SHector Martin 	if (force)
27876cde263SHector Martin 		cpu = cpumask_first(mask_val);
27976cde263SHector Martin 	else
28076cde263SHector Martin 		cpu = cpumask_any_and(mask_val, cpu_online_mask);
28176cde263SHector Martin 
28276cde263SHector Martin 	aic_ic_write(ic, AIC_TARGET_CPU + hwirq * 4, BIT(cpu));
28376cde263SHector Martin 	irq_data_update_effective_affinity(d, cpumask_of(cpu));
28476cde263SHector Martin 
28576cde263SHector Martin 	return IRQ_SET_MASK_OK;
28676cde263SHector Martin }
28776cde263SHector Martin 
28876cde263SHector Martin static int aic_irq_set_type(struct irq_data *d, unsigned int type)
28976cde263SHector Martin {
29076cde263SHector Martin 	/*
29176cde263SHector Martin 	 * Some IRQs (e.g. MSIs) implicitly have edge semantics, and we don't
29276cde263SHector Martin 	 * have a way to find out the type of any given IRQ, so just allow both.
29376cde263SHector Martin 	 */
29476cde263SHector Martin 	return (type == IRQ_TYPE_LEVEL_HIGH || type == IRQ_TYPE_EDGE_RISING) ? 0 : -EINVAL;
29576cde263SHector Martin }
29676cde263SHector Martin 
29776cde263SHector Martin static struct irq_chip aic_chip = {
29876cde263SHector Martin 	.name = "AIC",
29976cde263SHector Martin 	.irq_mask = aic_irq_mask,
30076cde263SHector Martin 	.irq_unmask = aic_irq_unmask,
30176cde263SHector Martin 	.irq_eoi = aic_irq_eoi,
30276cde263SHector Martin 	.irq_set_affinity = aic_irq_set_affinity,
30376cde263SHector Martin 	.irq_set_type = aic_irq_set_type,
30476cde263SHector Martin };
30576cde263SHector Martin 
30676cde263SHector Martin /*
30776cde263SHector Martin  * FIQ irqchip
30876cde263SHector Martin  */
30976cde263SHector Martin 
31076cde263SHector Martin static unsigned long aic_fiq_get_idx(struct irq_data *d)
31176cde263SHector Martin {
31276cde263SHector Martin 	struct aic_irq_chip *ic = irq_data_get_irq_chip_data(d);
31376cde263SHector Martin 
31476cde263SHector Martin 	return irqd_to_hwirq(d) - ic->nr_hw;
31576cde263SHector Martin }
31676cde263SHector Martin 
31776cde263SHector Martin static void aic_fiq_set_mask(struct irq_data *d)
31876cde263SHector Martin {
31976cde263SHector Martin 	/* Only the guest timers have real mask bits, unfortunately. */
32076cde263SHector Martin 	switch (aic_fiq_get_idx(d)) {
32176cde263SHector Martin 	case AIC_TMR_EL02_PHYS:
32276cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, VM_TMR_FIQ_ENABLE_P, 0);
32376cde263SHector Martin 		isb();
32476cde263SHector Martin 		break;
32576cde263SHector Martin 	case AIC_TMR_EL02_VIRT:
32676cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, VM_TMR_FIQ_ENABLE_V, 0);
32776cde263SHector Martin 		isb();
32876cde263SHector Martin 		break;
32976cde263SHector Martin 	default:
33076cde263SHector Martin 		break;
33176cde263SHector Martin 	}
33276cde263SHector Martin }
33376cde263SHector Martin 
33476cde263SHector Martin static void aic_fiq_clear_mask(struct irq_data *d)
33576cde263SHector Martin {
33676cde263SHector Martin 	switch (aic_fiq_get_idx(d)) {
33776cde263SHector Martin 	case AIC_TMR_EL02_PHYS:
33876cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, 0, VM_TMR_FIQ_ENABLE_P);
33976cde263SHector Martin 		isb();
34076cde263SHector Martin 		break;
34176cde263SHector Martin 	case AIC_TMR_EL02_VIRT:
34276cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2, 0, VM_TMR_FIQ_ENABLE_V);
34376cde263SHector Martin 		isb();
34476cde263SHector Martin 		break;
34576cde263SHector Martin 	default:
34676cde263SHector Martin 		break;
34776cde263SHector Martin 	}
34876cde263SHector Martin }
34976cde263SHector Martin 
35076cde263SHector Martin static void aic_fiq_mask(struct irq_data *d)
35176cde263SHector Martin {
35276cde263SHector Martin 	aic_fiq_set_mask(d);
35376cde263SHector Martin 	__this_cpu_and(aic_fiq_unmasked, ~BIT(aic_fiq_get_idx(d)));
35476cde263SHector Martin }
35576cde263SHector Martin 
35676cde263SHector Martin static void aic_fiq_unmask(struct irq_data *d)
35776cde263SHector Martin {
35876cde263SHector Martin 	aic_fiq_clear_mask(d);
35976cde263SHector Martin 	__this_cpu_or(aic_fiq_unmasked, BIT(aic_fiq_get_idx(d)));
36076cde263SHector Martin }
36176cde263SHector Martin 
36276cde263SHector Martin static void aic_fiq_eoi(struct irq_data *d)
36376cde263SHector Martin {
36476cde263SHector Martin 	/* We mask to ack (where we can), so we need to unmask at EOI. */
36576cde263SHector Martin 	if (__this_cpu_read(aic_fiq_unmasked) & BIT(aic_fiq_get_idx(d)))
36676cde263SHector Martin 		aic_fiq_clear_mask(d);
36776cde263SHector Martin }
36876cde263SHector Martin 
36976cde263SHector Martin #define TIMER_FIRING(x)                                                        \
37076cde263SHector Martin 	(((x) & (ARCH_TIMER_CTRL_ENABLE | ARCH_TIMER_CTRL_IT_MASK |            \
37176cde263SHector Martin 		 ARCH_TIMER_CTRL_IT_STAT)) ==                                  \
37276cde263SHector Martin 	 (ARCH_TIMER_CTRL_ENABLE | ARCH_TIMER_CTRL_IT_STAT))
37376cde263SHector Martin 
37476cde263SHector Martin static void __exception_irq_entry aic_handle_fiq(struct pt_regs *regs)
37576cde263SHector Martin {
37676cde263SHector Martin 	/*
37776cde263SHector Martin 	 * It would be really nice if we had a system register that lets us get
37876cde263SHector Martin 	 * the FIQ source state without having to peek down into sources...
37976cde263SHector Martin 	 * but such a register does not seem to exist.
38076cde263SHector Martin 	 *
38176cde263SHector Martin 	 * So, we have these potential sources to test for:
38276cde263SHector Martin 	 *  - Fast IPIs (not yet used)
38376cde263SHector Martin 	 *  - The 4 timers (CNTP, CNTV for each of HV and guest)
38476cde263SHector Martin 	 *  - Per-core PMCs (not yet supported)
38576cde263SHector Martin 	 *  - Per-cluster uncore PMCs (not yet supported)
38676cde263SHector Martin 	 *
38776cde263SHector Martin 	 * Since not dealing with any of these results in a FIQ storm,
38876cde263SHector Martin 	 * we check for everything here, even things we don't support yet.
38976cde263SHector Martin 	 */
39076cde263SHector Martin 
39176cde263SHector Martin 	if (read_sysreg_s(SYS_IMP_APL_IPI_SR_EL1) & IPI_SR_PENDING) {
39276cde263SHector Martin 		pr_err_ratelimited("Fast IPI fired. Acking.\n");
39376cde263SHector Martin 		write_sysreg_s(IPI_SR_PENDING, SYS_IMP_APL_IPI_SR_EL1);
39476cde263SHector Martin 	}
39576cde263SHector Martin 
39676cde263SHector Martin 	if (TIMER_FIRING(read_sysreg(cntp_ctl_el0)))
3970953fb26SMark Rutland 		generic_handle_domain_irq(aic_irqc->hw_domain,
3980953fb26SMark Rutland 					  aic_irqc->nr_hw + AIC_TMR_EL0_PHYS);
39976cde263SHector Martin 
40076cde263SHector Martin 	if (TIMER_FIRING(read_sysreg(cntv_ctl_el0)))
4010953fb26SMark Rutland 		generic_handle_domain_irq(aic_irqc->hw_domain,
4020953fb26SMark Rutland 					  aic_irqc->nr_hw + AIC_TMR_EL0_VIRT);
40376cde263SHector Martin 
40476cde263SHector Martin 	if (is_kernel_in_hyp_mode()) {
40576cde263SHector Martin 		uint64_t enabled = read_sysreg_s(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2);
40676cde263SHector Martin 
40776cde263SHector Martin 		if ((enabled & VM_TMR_FIQ_ENABLE_P) &&
40876cde263SHector Martin 		    TIMER_FIRING(read_sysreg_s(SYS_CNTP_CTL_EL02)))
4090953fb26SMark Rutland 			generic_handle_domain_irq(aic_irqc->hw_domain,
4100953fb26SMark Rutland 						  aic_irqc->nr_hw + AIC_TMR_EL02_PHYS);
41176cde263SHector Martin 
41276cde263SHector Martin 		if ((enabled & VM_TMR_FIQ_ENABLE_V) &&
41376cde263SHector Martin 		    TIMER_FIRING(read_sysreg_s(SYS_CNTV_CTL_EL02)))
4140953fb26SMark Rutland 			generic_handle_domain_irq(aic_irqc->hw_domain,
4150953fb26SMark Rutland 						  aic_irqc->nr_hw + AIC_TMR_EL02_VIRT);
41676cde263SHector Martin 	}
41776cde263SHector Martin 
41876cde263SHector Martin 	if ((read_sysreg_s(SYS_IMP_APL_PMCR0_EL1) & (PMCR0_IMODE | PMCR0_IACT)) ==
41976cde263SHector Martin 			(FIELD_PREP(PMCR0_IMODE, PMCR0_IMODE_FIQ) | PMCR0_IACT)) {
42076cde263SHector Martin 		/*
42176cde263SHector Martin 		 * Not supported yet, let's figure out how to handle this when
42276cde263SHector Martin 		 * we implement these proprietary performance counters. For now,
42376cde263SHector Martin 		 * just mask it and move on.
42476cde263SHector Martin 		 */
42576cde263SHector Martin 		pr_err_ratelimited("PMC FIQ fired. Masking.\n");
42676cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_PMCR0_EL1, PMCR0_IMODE | PMCR0_IACT,
42776cde263SHector Martin 				   FIELD_PREP(PMCR0_IMODE, PMCR0_IMODE_OFF));
42876cde263SHector Martin 	}
42976cde263SHector Martin 
43076cde263SHector Martin 	if (FIELD_GET(UPMCR0_IMODE, read_sysreg_s(SYS_IMP_APL_UPMCR0_EL1)) == UPMCR0_IMODE_FIQ &&
43176cde263SHector Martin 			(read_sysreg_s(SYS_IMP_APL_UPMSR_EL1) & UPMSR_IACT)) {
43276cde263SHector Martin 		/* Same story with uncore PMCs */
43376cde263SHector Martin 		pr_err_ratelimited("Uncore PMC FIQ fired. Masking.\n");
43476cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_UPMCR0_EL1, UPMCR0_IMODE,
43576cde263SHector Martin 				   FIELD_PREP(UPMCR0_IMODE, UPMCR0_IMODE_OFF));
43676cde263SHector Martin 	}
43776cde263SHector Martin }
43876cde263SHector Martin 
43976cde263SHector Martin static int aic_fiq_set_type(struct irq_data *d, unsigned int type)
44076cde263SHector Martin {
44176cde263SHector Martin 	return (type == IRQ_TYPE_LEVEL_HIGH) ? 0 : -EINVAL;
44276cde263SHector Martin }
44376cde263SHector Martin 
44476cde263SHector Martin static struct irq_chip fiq_chip = {
44576cde263SHector Martin 	.name = "AIC-FIQ",
44676cde263SHector Martin 	.irq_mask = aic_fiq_mask,
44776cde263SHector Martin 	.irq_unmask = aic_fiq_unmask,
44876cde263SHector Martin 	.irq_ack = aic_fiq_set_mask,
44976cde263SHector Martin 	.irq_eoi = aic_fiq_eoi,
45076cde263SHector Martin 	.irq_set_type = aic_fiq_set_type,
45176cde263SHector Martin };
45276cde263SHector Martin 
45376cde263SHector Martin /*
45476cde263SHector Martin  * Main IRQ domain
45576cde263SHector Martin  */
45676cde263SHector Martin 
45776cde263SHector Martin static int aic_irq_domain_map(struct irq_domain *id, unsigned int irq,
45876cde263SHector Martin 			      irq_hw_number_t hw)
45976cde263SHector Martin {
46076cde263SHector Martin 	struct aic_irq_chip *ic = id->host_data;
46176cde263SHector Martin 
46276cde263SHector Martin 	if (hw < ic->nr_hw) {
46376cde263SHector Martin 		irq_domain_set_info(id, irq, hw, &aic_chip, id->host_data,
46476cde263SHector Martin 				    handle_fasteoi_irq, NULL, NULL);
46576cde263SHector Martin 		irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(irq)));
46676cde263SHector Martin 	} else {
46776cde263SHector Martin 		irq_set_percpu_devid(irq);
46876cde263SHector Martin 		irq_domain_set_info(id, irq, hw, &fiq_chip, id->host_data,
46976cde263SHector Martin 				    handle_percpu_devid_irq, NULL, NULL);
47076cde263SHector Martin 	}
47176cde263SHector Martin 
47276cde263SHector Martin 	return 0;
47376cde263SHector Martin }
47476cde263SHector Martin 
47576cde263SHector Martin static int aic_irq_domain_translate(struct irq_domain *id,
47676cde263SHector Martin 				    struct irq_fwspec *fwspec,
47776cde263SHector Martin 				    unsigned long *hwirq,
47876cde263SHector Martin 				    unsigned int *type)
47976cde263SHector Martin {
48076cde263SHector Martin 	struct aic_irq_chip *ic = id->host_data;
48176cde263SHector Martin 
48276cde263SHector Martin 	if (fwspec->param_count != 3 || !is_of_node(fwspec->fwnode))
48376cde263SHector Martin 		return -EINVAL;
48476cde263SHector Martin 
48576cde263SHector Martin 	switch (fwspec->param[0]) {
48676cde263SHector Martin 	case AIC_IRQ:
48776cde263SHector Martin 		if (fwspec->param[1] >= ic->nr_hw)
48876cde263SHector Martin 			return -EINVAL;
48976cde263SHector Martin 		*hwirq = fwspec->param[1];
49076cde263SHector Martin 		break;
49176cde263SHector Martin 	case AIC_FIQ:
49276cde263SHector Martin 		if (fwspec->param[1] >= AIC_NR_FIQ)
49376cde263SHector Martin 			return -EINVAL;
49476cde263SHector Martin 		*hwirq = ic->nr_hw + fwspec->param[1];
49576cde263SHector Martin 
49676cde263SHector Martin 		/*
49776cde263SHector Martin 		 * In EL1 the non-redirected registers are the guest's,
49876cde263SHector Martin 		 * not EL2's, so remap the hwirqs to match.
49976cde263SHector Martin 		 */
50076cde263SHector Martin 		if (!is_kernel_in_hyp_mode()) {
50176cde263SHector Martin 			switch (fwspec->param[1]) {
50276cde263SHector Martin 			case AIC_TMR_GUEST_PHYS:
50376cde263SHector Martin 				*hwirq = ic->nr_hw + AIC_TMR_EL0_PHYS;
50476cde263SHector Martin 				break;
50576cde263SHector Martin 			case AIC_TMR_GUEST_VIRT:
50676cde263SHector Martin 				*hwirq = ic->nr_hw + AIC_TMR_EL0_VIRT;
50776cde263SHector Martin 				break;
50876cde263SHector Martin 			case AIC_TMR_HV_PHYS:
50976cde263SHector Martin 			case AIC_TMR_HV_VIRT:
51076cde263SHector Martin 				return -ENOENT;
51176cde263SHector Martin 			default:
51276cde263SHector Martin 				break;
51376cde263SHector Martin 			}
51476cde263SHector Martin 		}
51576cde263SHector Martin 		break;
51676cde263SHector Martin 	default:
51776cde263SHector Martin 		return -EINVAL;
51876cde263SHector Martin 	}
51976cde263SHector Martin 
52076cde263SHector Martin 	*type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
52176cde263SHector Martin 
52276cde263SHector Martin 	return 0;
52376cde263SHector Martin }
52476cde263SHector Martin 
52576cde263SHector Martin static int aic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
52676cde263SHector Martin 				unsigned int nr_irqs, void *arg)
52776cde263SHector Martin {
52876cde263SHector Martin 	unsigned int type = IRQ_TYPE_NONE;
52976cde263SHector Martin 	struct irq_fwspec *fwspec = arg;
53076cde263SHector Martin 	irq_hw_number_t hwirq;
53176cde263SHector Martin 	int i, ret;
53276cde263SHector Martin 
53376cde263SHector Martin 	ret = aic_irq_domain_translate(domain, fwspec, &hwirq, &type);
53476cde263SHector Martin 	if (ret)
53576cde263SHector Martin 		return ret;
53676cde263SHector Martin 
53776cde263SHector Martin 	for (i = 0; i < nr_irqs; i++) {
53876cde263SHector Martin 		ret = aic_irq_domain_map(domain, virq + i, hwirq + i);
53976cde263SHector Martin 		if (ret)
54076cde263SHector Martin 			return ret;
54176cde263SHector Martin 	}
54276cde263SHector Martin 
54376cde263SHector Martin 	return 0;
54476cde263SHector Martin }
54576cde263SHector Martin 
54676cde263SHector Martin static void aic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
54776cde263SHector Martin 				unsigned int nr_irqs)
54876cde263SHector Martin {
54976cde263SHector Martin 	int i;
55076cde263SHector Martin 
55176cde263SHector Martin 	for (i = 0; i < nr_irqs; i++) {
55276cde263SHector Martin 		struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
55376cde263SHector Martin 
55476cde263SHector Martin 		irq_set_handler(virq + i, NULL);
55576cde263SHector Martin 		irq_domain_reset_irq_data(d);
55676cde263SHector Martin 	}
55776cde263SHector Martin }
55876cde263SHector Martin 
55976cde263SHector Martin static const struct irq_domain_ops aic_irq_domain_ops = {
56076cde263SHector Martin 	.translate	= aic_irq_domain_translate,
56176cde263SHector Martin 	.alloc		= aic_irq_domain_alloc,
56276cde263SHector Martin 	.free		= aic_irq_domain_free,
56376cde263SHector Martin };
56476cde263SHector Martin 
56576cde263SHector Martin /*
56676cde263SHector Martin  * IPI irqchip
56776cde263SHector Martin  */
56876cde263SHector Martin 
56976cde263SHector Martin static void aic_ipi_mask(struct irq_data *d)
57076cde263SHector Martin {
57176cde263SHector Martin 	u32 irq_bit = BIT(irqd_to_hwirq(d));
57276cde263SHector Martin 
57376cde263SHector Martin 	/* No specific ordering requirements needed here. */
57476cde263SHector Martin 	atomic_andnot(irq_bit, this_cpu_ptr(&aic_vipi_enable));
57576cde263SHector Martin }
57676cde263SHector Martin 
57776cde263SHector Martin static void aic_ipi_unmask(struct irq_data *d)
57876cde263SHector Martin {
57976cde263SHector Martin 	struct aic_irq_chip *ic = irq_data_get_irq_chip_data(d);
58076cde263SHector Martin 	u32 irq_bit = BIT(irqd_to_hwirq(d));
58176cde263SHector Martin 
58276cde263SHector Martin 	atomic_or(irq_bit, this_cpu_ptr(&aic_vipi_enable));
58376cde263SHector Martin 
58476cde263SHector Martin 	/*
58576cde263SHector Martin 	 * The atomic_or() above must complete before the atomic_read()
58676cde263SHector Martin 	 * below to avoid racing aic_ipi_send_mask().
58776cde263SHector Martin 	 */
58876cde263SHector Martin 	smp_mb__after_atomic();
58976cde263SHector Martin 
59076cde263SHector Martin 	/*
59176cde263SHector Martin 	 * If a pending vIPI was unmasked, raise a HW IPI to ourselves.
59276cde263SHector Martin 	 * No barriers needed here since this is a self-IPI.
59376cde263SHector Martin 	 */
59476cde263SHector Martin 	if (atomic_read(this_cpu_ptr(&aic_vipi_flag)) & irq_bit)
59576cde263SHector Martin 		aic_ic_write(ic, AIC_IPI_SEND, AIC_IPI_SEND_CPU(smp_processor_id()));
59676cde263SHector Martin }
59776cde263SHector Martin 
59876cde263SHector Martin static void aic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
59976cde263SHector Martin {
60076cde263SHector Martin 	struct aic_irq_chip *ic = irq_data_get_irq_chip_data(d);
60176cde263SHector Martin 	u32 irq_bit = BIT(irqd_to_hwirq(d));
60276cde263SHector Martin 	u32 send = 0;
60376cde263SHector Martin 	int cpu;
60476cde263SHector Martin 	unsigned long pending;
60576cde263SHector Martin 
60676cde263SHector Martin 	for_each_cpu(cpu, mask) {
60776cde263SHector Martin 		/*
60876cde263SHector Martin 		 * This sequence is the mirror of the one in aic_ipi_unmask();
60976cde263SHector Martin 		 * see the comment there. Additionally, release semantics
61076cde263SHector Martin 		 * ensure that the vIPI flag set is ordered after any shared
61176cde263SHector Martin 		 * memory accesses that precede it. This therefore also pairs
61276cde263SHector Martin 		 * with the atomic_fetch_andnot in aic_handle_ipi().
61376cde263SHector Martin 		 */
61476cde263SHector Martin 		pending = atomic_fetch_or_release(irq_bit, per_cpu_ptr(&aic_vipi_flag, cpu));
61576cde263SHector Martin 
61676cde263SHector Martin 		/*
61776cde263SHector Martin 		 * The atomic_fetch_or_release() above must complete before the
61876cde263SHector Martin 		 * atomic_read() below to avoid racing aic_ipi_unmask().
61976cde263SHector Martin 		 */
62076cde263SHector Martin 		smp_mb__after_atomic();
62176cde263SHector Martin 
62276cde263SHector Martin 		if (!(pending & irq_bit) &&
62376cde263SHector Martin 		    (atomic_read(per_cpu_ptr(&aic_vipi_enable, cpu)) & irq_bit))
62476cde263SHector Martin 			send |= AIC_IPI_SEND_CPU(cpu);
62576cde263SHector Martin 	}
62676cde263SHector Martin 
62776cde263SHector Martin 	/*
62876cde263SHector Martin 	 * The flag writes must complete before the physical IPI is issued
62976cde263SHector Martin 	 * to another CPU. This is implied by the control dependency on
63076cde263SHector Martin 	 * the result of atomic_read_acquire() above, which is itself
63176cde263SHector Martin 	 * already ordered after the vIPI flag write.
63276cde263SHector Martin 	 */
63376cde263SHector Martin 	if (send)
63476cde263SHector Martin 		aic_ic_write(ic, AIC_IPI_SEND, send);
63576cde263SHector Martin }
63676cde263SHector Martin 
63776cde263SHector Martin static struct irq_chip ipi_chip = {
63876cde263SHector Martin 	.name = "AIC-IPI",
63976cde263SHector Martin 	.irq_mask = aic_ipi_mask,
64076cde263SHector Martin 	.irq_unmask = aic_ipi_unmask,
64176cde263SHector Martin 	.ipi_send_mask = aic_ipi_send_mask,
64276cde263SHector Martin };
64376cde263SHector Martin 
64476cde263SHector Martin /*
64576cde263SHector Martin  * IPI IRQ domain
64676cde263SHector Martin  */
64776cde263SHector Martin 
64876cde263SHector Martin static void aic_handle_ipi(struct pt_regs *regs)
64976cde263SHector Martin {
65076cde263SHector Martin 	int i;
65176cde263SHector Martin 	unsigned long enabled, firing;
65276cde263SHector Martin 
65376cde263SHector Martin 	/*
65476cde263SHector Martin 	 * Ack the IPI. We need to order this after the AIC event read, but
65576cde263SHector Martin 	 * that is enforced by normal MMIO ordering guarantees.
65676cde263SHector Martin 	 */
65776cde263SHector Martin 	aic_ic_write(aic_irqc, AIC_IPI_ACK, AIC_IPI_OTHER);
65876cde263SHector Martin 
65976cde263SHector Martin 	/*
66076cde263SHector Martin 	 * The mask read does not need to be ordered. Only we can change
66176cde263SHector Martin 	 * our own mask anyway, so no races are possible here, as long as
66276cde263SHector Martin 	 * we are properly in the interrupt handler (which is covered by
66376cde263SHector Martin 	 * the barrier that is part of the top-level AIC handler's readl()).
66476cde263SHector Martin 	 */
66576cde263SHector Martin 	enabled = atomic_read(this_cpu_ptr(&aic_vipi_enable));
66676cde263SHector Martin 
66776cde263SHector Martin 	/*
66876cde263SHector Martin 	 * Clear the IPIs we are about to handle. This pairs with the
66976cde263SHector Martin 	 * atomic_fetch_or_release() in aic_ipi_send_mask(), and needs to be
67076cde263SHector Martin 	 * ordered after the aic_ic_write() above (to avoid dropping vIPIs) and
67176cde263SHector Martin 	 * before IPI handling code (to avoid races handling vIPIs before they
67276cde263SHector Martin 	 * are signaled). The former is taken care of by the release semantics
67376cde263SHector Martin 	 * of the write portion, while the latter is taken care of by the
67476cde263SHector Martin 	 * acquire semantics of the read portion.
67576cde263SHector Martin 	 */
67676cde263SHector Martin 	firing = atomic_fetch_andnot(enabled, this_cpu_ptr(&aic_vipi_flag)) & enabled;
67776cde263SHector Martin 
67876cde263SHector Martin 	for_each_set_bit(i, &firing, AIC_NR_SWIPI)
6790953fb26SMark Rutland 		generic_handle_domain_irq(aic_irqc->ipi_domain, i);
68076cde263SHector Martin 
68176cde263SHector Martin 	/*
68276cde263SHector Martin 	 * No ordering needed here; at worst this just changes the timing of
68376cde263SHector Martin 	 * when the next IPI will be delivered.
68476cde263SHector Martin 	 */
68576cde263SHector Martin 	aic_ic_write(aic_irqc, AIC_IPI_MASK_CLR, AIC_IPI_OTHER);
68676cde263SHector Martin }
68776cde263SHector Martin 
68876cde263SHector Martin static int aic_ipi_alloc(struct irq_domain *d, unsigned int virq,
68976cde263SHector Martin 			 unsigned int nr_irqs, void *args)
69076cde263SHector Martin {
69176cde263SHector Martin 	int i;
69276cde263SHector Martin 
69376cde263SHector Martin 	for (i = 0; i < nr_irqs; i++) {
69476cde263SHector Martin 		irq_set_percpu_devid(virq + i);
69576cde263SHector Martin 		irq_domain_set_info(d, virq + i, i, &ipi_chip, d->host_data,
69676cde263SHector Martin 				    handle_percpu_devid_irq, NULL, NULL);
69776cde263SHector Martin 	}
69876cde263SHector Martin 
69976cde263SHector Martin 	return 0;
70076cde263SHector Martin }
70176cde263SHector Martin 
70276cde263SHector Martin static void aic_ipi_free(struct irq_domain *d, unsigned int virq, unsigned int nr_irqs)
70376cde263SHector Martin {
70476cde263SHector Martin 	/* Not freeing IPIs */
70576cde263SHector Martin }
70676cde263SHector Martin 
70776cde263SHector Martin static const struct irq_domain_ops aic_ipi_domain_ops = {
70876cde263SHector Martin 	.alloc = aic_ipi_alloc,
70976cde263SHector Martin 	.free = aic_ipi_free,
71076cde263SHector Martin };
71176cde263SHector Martin 
7123d9e575fSDonghyeok Kim static int __init aic_init_smp(struct aic_irq_chip *irqc, struct device_node *node)
71376cde263SHector Martin {
71476cde263SHector Martin 	struct irq_domain *ipi_domain;
71576cde263SHector Martin 	int base_ipi;
71676cde263SHector Martin 
71776cde263SHector Martin 	ipi_domain = irq_domain_create_linear(irqc->hw_domain->fwnode, AIC_NR_SWIPI,
71876cde263SHector Martin 					      &aic_ipi_domain_ops, irqc);
71976cde263SHector Martin 	if (WARN_ON(!ipi_domain))
72076cde263SHector Martin 		return -ENODEV;
72176cde263SHector Martin 
72276cde263SHector Martin 	ipi_domain->flags |= IRQ_DOMAIN_FLAG_IPI_SINGLE;
72376cde263SHector Martin 	irq_domain_update_bus_token(ipi_domain, DOMAIN_BUS_IPI);
72476cde263SHector Martin 
72576cde263SHector Martin 	base_ipi = __irq_domain_alloc_irqs(ipi_domain, -1, AIC_NR_SWIPI,
72676cde263SHector Martin 					   NUMA_NO_NODE, NULL, false, NULL);
72776cde263SHector Martin 
72876cde263SHector Martin 	if (WARN_ON(!base_ipi)) {
72976cde263SHector Martin 		irq_domain_remove(ipi_domain);
73076cde263SHector Martin 		return -ENODEV;
73176cde263SHector Martin 	}
73276cde263SHector Martin 
73376cde263SHector Martin 	set_smp_ipi_range(base_ipi, AIC_NR_SWIPI);
73476cde263SHector Martin 
73576cde263SHector Martin 	irqc->ipi_domain = ipi_domain;
73676cde263SHector Martin 
73776cde263SHector Martin 	return 0;
73876cde263SHector Martin }
73976cde263SHector Martin 
74076cde263SHector Martin static int aic_init_cpu(unsigned int cpu)
74176cde263SHector Martin {
74276cde263SHector Martin 	/* Mask all hard-wired per-CPU IRQ/FIQ sources */
74376cde263SHector Martin 
74476cde263SHector Martin 	/* Pending Fast IPI FIQs */
74576cde263SHector Martin 	write_sysreg_s(IPI_SR_PENDING, SYS_IMP_APL_IPI_SR_EL1);
74676cde263SHector Martin 
74776cde263SHector Martin 	/* Timer FIQs */
74876cde263SHector Martin 	sysreg_clear_set(cntp_ctl_el0, 0, ARCH_TIMER_CTRL_IT_MASK);
74976cde263SHector Martin 	sysreg_clear_set(cntv_ctl_el0, 0, ARCH_TIMER_CTRL_IT_MASK);
75076cde263SHector Martin 
75176cde263SHector Martin 	/* EL2-only (VHE mode) IRQ sources */
75276cde263SHector Martin 	if (is_kernel_in_hyp_mode()) {
75376cde263SHector Martin 		/* Guest timers */
75476cde263SHector Martin 		sysreg_clear_set_s(SYS_IMP_APL_VM_TMR_FIQ_ENA_EL2,
75576cde263SHector Martin 				   VM_TMR_FIQ_ENABLE_V | VM_TMR_FIQ_ENABLE_P, 0);
75676cde263SHector Martin 
75776cde263SHector Martin 		/* vGIC maintenance IRQ */
75876cde263SHector Martin 		sysreg_clear_set_s(SYS_ICH_HCR_EL2, ICH_HCR_EN, 0);
75976cde263SHector Martin 	}
76076cde263SHector Martin 
76176cde263SHector Martin 	/* PMC FIQ */
76276cde263SHector Martin 	sysreg_clear_set_s(SYS_IMP_APL_PMCR0_EL1, PMCR0_IMODE | PMCR0_IACT,
76376cde263SHector Martin 			   FIELD_PREP(PMCR0_IMODE, PMCR0_IMODE_OFF));
76476cde263SHector Martin 
76576cde263SHector Martin 	/* Uncore PMC FIQ */
76676cde263SHector Martin 	sysreg_clear_set_s(SYS_IMP_APL_UPMCR0_EL1, UPMCR0_IMODE,
76776cde263SHector Martin 			   FIELD_PREP(UPMCR0_IMODE, UPMCR0_IMODE_OFF));
76876cde263SHector Martin 
76976cde263SHector Martin 	/* Commit all of the above */
77076cde263SHector Martin 	isb();
77176cde263SHector Martin 
77276cde263SHector Martin 	/*
77376cde263SHector Martin 	 * Make sure the kernel's idea of logical CPU order is the same as AIC's
77476cde263SHector Martin 	 * If we ever end up with a mismatch here, we will have to introduce
77576cde263SHector Martin 	 * a mapping table similar to what other irqchip drivers do.
77676cde263SHector Martin 	 */
77776cde263SHector Martin 	WARN_ON(aic_ic_read(aic_irqc, AIC_WHOAMI) != smp_processor_id());
77876cde263SHector Martin 
77976cde263SHector Martin 	/*
78076cde263SHector Martin 	 * Always keep IPIs unmasked at the hardware level (except auto-masking
78176cde263SHector Martin 	 * by AIC during processing). We manage masks at the vIPI level.
78276cde263SHector Martin 	 */
78376cde263SHector Martin 	aic_ic_write(aic_irqc, AIC_IPI_ACK, AIC_IPI_SELF | AIC_IPI_OTHER);
78476cde263SHector Martin 	aic_ic_write(aic_irqc, AIC_IPI_MASK_SET, AIC_IPI_SELF);
78576cde263SHector Martin 	aic_ic_write(aic_irqc, AIC_IPI_MASK_CLR, AIC_IPI_OTHER);
78676cde263SHector Martin 
78776cde263SHector Martin 	/* Initialize the local mask state */
78876cde263SHector Martin 	__this_cpu_write(aic_fiq_unmasked, 0);
78976cde263SHector Martin 
79076cde263SHector Martin 	return 0;
79176cde263SHector Martin }
79276cde263SHector Martin 
793b6ca556cSMarc Zyngier static struct gic_kvm_info vgic_info __initdata = {
794b6ca556cSMarc Zyngier 	.type			= GIC_V3,
795b6ca556cSMarc Zyngier 	.no_maint_irq_mask	= true,
796b6ca556cSMarc Zyngier 	.no_hw_deactivation	= true,
797b6ca556cSMarc Zyngier };
798b6ca556cSMarc Zyngier 
799*a5e88012SMarc Zyngier static void build_fiq_affinity(struct aic_irq_chip *ic, struct device_node *aff)
800*a5e88012SMarc Zyngier {
801*a5e88012SMarc Zyngier 	int i, n;
802*a5e88012SMarc Zyngier 	u32 fiq;
803*a5e88012SMarc Zyngier 
804*a5e88012SMarc Zyngier 	if (of_property_read_u32(aff, "apple,fiq-index", &fiq) ||
805*a5e88012SMarc Zyngier 	    WARN_ON(fiq >= AIC_NR_FIQ) || ic->fiq_aff[fiq])
806*a5e88012SMarc Zyngier 		return;
807*a5e88012SMarc Zyngier 
808*a5e88012SMarc Zyngier 	n = of_property_count_elems_of_size(aff, "cpus", sizeof(u32));
809*a5e88012SMarc Zyngier 	if (WARN_ON(n < 0))
810*a5e88012SMarc Zyngier 		return;
811*a5e88012SMarc Zyngier 
812*a5e88012SMarc Zyngier 	ic->fiq_aff[fiq] = kzalloc(sizeof(ic->fiq_aff[fiq]), GFP_KERNEL);
813*a5e88012SMarc Zyngier 	if (!ic->fiq_aff[fiq])
814*a5e88012SMarc Zyngier 		return;
815*a5e88012SMarc Zyngier 
816*a5e88012SMarc Zyngier 	for (i = 0; i < n; i++) {
817*a5e88012SMarc Zyngier 		struct device_node *cpu_node;
818*a5e88012SMarc Zyngier 		u32 cpu_phandle;
819*a5e88012SMarc Zyngier 		int cpu;
820*a5e88012SMarc Zyngier 
821*a5e88012SMarc Zyngier 		if (of_property_read_u32_index(aff, "cpus", i, &cpu_phandle))
822*a5e88012SMarc Zyngier 			continue;
823*a5e88012SMarc Zyngier 
824*a5e88012SMarc Zyngier 		cpu_node = of_find_node_by_phandle(cpu_phandle);
825*a5e88012SMarc Zyngier 		if (WARN_ON(!cpu_node))
826*a5e88012SMarc Zyngier 			continue;
827*a5e88012SMarc Zyngier 
828*a5e88012SMarc Zyngier 		cpu = of_cpu_node_to_id(cpu_node);
829*a5e88012SMarc Zyngier 		if (WARN_ON(cpu < 0))
830*a5e88012SMarc Zyngier 			continue;
831*a5e88012SMarc Zyngier 
832*a5e88012SMarc Zyngier 		cpumask_set_cpu(cpu, &ic->fiq_aff[fiq]->aff);
833*a5e88012SMarc Zyngier 	}
834*a5e88012SMarc Zyngier }
835*a5e88012SMarc Zyngier 
83676cde263SHector Martin static int __init aic_of_ic_init(struct device_node *node, struct device_node *parent)
83776cde263SHector Martin {
83876cde263SHector Martin 	int i;
83976cde263SHector Martin 	void __iomem *regs;
84076cde263SHector Martin 	u32 info;
84176cde263SHector Martin 	struct aic_irq_chip *irqc;
842*a5e88012SMarc Zyngier 	struct device_node *affs;
84376cde263SHector Martin 
84476cde263SHector Martin 	regs = of_iomap(node, 0);
84576cde263SHector Martin 	if (WARN_ON(!regs))
84676cde263SHector Martin 		return -EIO;
84776cde263SHector Martin 
84876cde263SHector Martin 	irqc = kzalloc(sizeof(*irqc), GFP_KERNEL);
84976cde263SHector Martin 	if (!irqc)
85076cde263SHector Martin 		return -ENOMEM;
85176cde263SHector Martin 
85276cde263SHector Martin 	aic_irqc = irqc;
85376cde263SHector Martin 	irqc->base = regs;
85476cde263SHector Martin 
85576cde263SHector Martin 	info = aic_ic_read(irqc, AIC_INFO);
85676cde263SHector Martin 	irqc->nr_hw = FIELD_GET(AIC_INFO_NR_HW, info);
85776cde263SHector Martin 
85876cde263SHector Martin 	irqc->hw_domain = irq_domain_create_linear(of_node_to_fwnode(node),
85976cde263SHector Martin 						   irqc->nr_hw + AIC_NR_FIQ,
86076cde263SHector Martin 						   &aic_irq_domain_ops, irqc);
86176cde263SHector Martin 	if (WARN_ON(!irqc->hw_domain)) {
86276cde263SHector Martin 		iounmap(irqc->base);
86376cde263SHector Martin 		kfree(irqc);
86476cde263SHector Martin 		return -ENODEV;
86576cde263SHector Martin 	}
86676cde263SHector Martin 
86776cde263SHector Martin 	irq_domain_update_bus_token(irqc->hw_domain, DOMAIN_BUS_WIRED);
86876cde263SHector Martin 
86976cde263SHector Martin 	if (aic_init_smp(irqc, node)) {
87076cde263SHector Martin 		irq_domain_remove(irqc->hw_domain);
87176cde263SHector Martin 		iounmap(irqc->base);
87276cde263SHector Martin 		kfree(irqc);
87376cde263SHector Martin 		return -ENODEV;
87476cde263SHector Martin 	}
87576cde263SHector Martin 
876*a5e88012SMarc Zyngier 	affs = of_get_child_by_name(node, "affinities");
877*a5e88012SMarc Zyngier 	if (affs) {
878*a5e88012SMarc Zyngier 		struct device_node *chld;
879*a5e88012SMarc Zyngier 
880*a5e88012SMarc Zyngier 		for_each_child_of_node(affs, chld)
881*a5e88012SMarc Zyngier 			build_fiq_affinity(irqc, chld);
882*a5e88012SMarc Zyngier 	}
883*a5e88012SMarc Zyngier 
88476cde263SHector Martin 	set_handle_irq(aic_handle_irq);
88576cde263SHector Martin 	set_handle_fiq(aic_handle_fiq);
88676cde263SHector Martin 
88776cde263SHector Martin 	for (i = 0; i < BITS_TO_U32(irqc->nr_hw); i++)
88876cde263SHector Martin 		aic_ic_write(irqc, AIC_MASK_SET + i * 4, U32_MAX);
88976cde263SHector Martin 	for (i = 0; i < BITS_TO_U32(irqc->nr_hw); i++)
89076cde263SHector Martin 		aic_ic_write(irqc, AIC_SW_CLR + i * 4, U32_MAX);
89176cde263SHector Martin 	for (i = 0; i < irqc->nr_hw; i++)
89276cde263SHector Martin 		aic_ic_write(irqc, AIC_TARGET_CPU + i * 4, 1);
89376cde263SHector Martin 
89476cde263SHector Martin 	if (!is_kernel_in_hyp_mode())
89576cde263SHector Martin 		pr_info("Kernel running in EL1, mapping interrupts");
89676cde263SHector Martin 
89776cde263SHector Martin 	cpuhp_setup_state(CPUHP_AP_IRQ_APPLE_AIC_STARTING,
89876cde263SHector Martin 			  "irqchip/apple-aic/ipi:starting",
89976cde263SHector Martin 			  aic_init_cpu, NULL);
90076cde263SHector Martin 
901b6ca556cSMarc Zyngier 	vgic_set_kvm_info(&vgic_info);
902b6ca556cSMarc Zyngier 
90376cde263SHector Martin 	pr_info("Initialized with %d IRQs, %d FIQs, %d vIPIs\n",
90476cde263SHector Martin 		irqc->nr_hw, AIC_NR_FIQ, AIC_NR_SWIPI);
90576cde263SHector Martin 
90676cde263SHector Martin 	return 0;
90776cde263SHector Martin }
90876cde263SHector Martin 
90976cde263SHector Martin IRQCHIP_DECLARE(apple_m1_aic, "apple,aic", aic_of_ic_init);
910