xref: /openbmc/linux/drivers/irqchip/irq-riscv-intc.c (revision 6c71a0574249f5e5a45fe055ab5f837023d5eeca)
16b7ce892SAnup Patel // SPDX-License-Identifier: GPL-2.0
26b7ce892SAnup Patel /*
36b7ce892SAnup Patel  * Copyright (C) 2012 Regents of the University of California
46b7ce892SAnup Patel  * Copyright (C) 2017-2018 SiFive
56b7ce892SAnup Patel  * Copyright (C) 2020 Western Digital Corporation or its affiliates.
66b7ce892SAnup Patel  */
76b7ce892SAnup Patel 
86b7ce892SAnup Patel #define pr_fmt(fmt) "riscv-intc: " fmt
97023b9d8SSunil V L #include <linux/acpi.h>
106b7ce892SAnup Patel #include <linux/atomic.h>
116b7ce892SAnup Patel #include <linux/bits.h>
126b7ce892SAnup Patel #include <linux/cpu.h>
136b7ce892SAnup Patel #include <linux/irq.h>
146b7ce892SAnup Patel #include <linux/irqchip.h>
156b7ce892SAnup Patel #include <linux/irqdomain.h>
166b7ce892SAnup Patel #include <linux/interrupt.h>
176b7ce892SAnup Patel #include <linux/module.h>
186b7ce892SAnup Patel #include <linux/of.h>
196b7ce892SAnup Patel #include <linux/smp.h>
2085ca483eSYu Chien Peter Lin #include <linux/soc/andes/irq.h>
216b7ce892SAnup Patel 
226b7ce892SAnup Patel static struct irq_domain *intc_domain;
2348209534SYu Chien Peter Lin static unsigned int riscv_intc_nr_irqs __ro_after_init = BITS_PER_LONG;
2448209534SYu Chien Peter Lin static unsigned int riscv_intc_custom_base __ro_after_init = BITS_PER_LONG;
2548209534SYu Chien Peter Lin static unsigned int riscv_intc_custom_nr_irqs __ro_after_init;
266b7ce892SAnup Patel 
riscv_intc_irq(struct pt_regs * regs)276b7ce892SAnup Patel static asmlinkage void riscv_intc_irq(struct pt_regs *regs)
286b7ce892SAnup Patel {
296b7ce892SAnup Patel 	unsigned long cause = regs->cause & ~CAUSE_IRQ_FLAG;
306b7ce892SAnup Patel 
3148209534SYu Chien Peter Lin 	if (generic_handle_domain_irq(intc_domain, cause))
3248209534SYu Chien Peter Lin 		pr_warn_ratelimited("Failed to handle interrupt (cause: %ld)\n", cause);
336b7ce892SAnup Patel }
346b7ce892SAnup Patel 
356b7ce892SAnup Patel /*
366b7ce892SAnup Patel  * On RISC-V systems local interrupts are masked or unmasked by writing
376b7ce892SAnup Patel  * the SIE (Supervisor Interrupt Enable) CSR.  As CSRs can only be written
386b7ce892SAnup Patel  * on the local hart, these functions can only be called on the hart that
396b7ce892SAnup Patel  * corresponds to the IRQ chip.
406b7ce892SAnup Patel  */
416b7ce892SAnup Patel 
riscv_intc_irq_mask(struct irq_data * d)426b7ce892SAnup Patel static void riscv_intc_irq_mask(struct irq_data *d)
436b7ce892SAnup Patel {
446b7ce892SAnup Patel 	csr_clear(CSR_IE, BIT(d->hwirq));
456b7ce892SAnup Patel }
466b7ce892SAnup Patel 
riscv_intc_irq_unmask(struct irq_data * d)476b7ce892SAnup Patel static void riscv_intc_irq_unmask(struct irq_data *d)
486b7ce892SAnup Patel {
496b7ce892SAnup Patel 	csr_set(CSR_IE, BIT(d->hwirq));
506b7ce892SAnup Patel }
516b7ce892SAnup Patel 
andes_intc_irq_mask(struct irq_data * d)5285ca483eSYu Chien Peter Lin static void andes_intc_irq_mask(struct irq_data *d)
5385ca483eSYu Chien Peter Lin {
5485ca483eSYu Chien Peter Lin 	/*
5585ca483eSYu Chien Peter Lin 	 * Andes specific S-mode local interrupt causes (hwirq)
5685ca483eSYu Chien Peter Lin 	 * are defined as (256 + n) and controlled by n-th bit
5785ca483eSYu Chien Peter Lin 	 * of SLIE.
5885ca483eSYu Chien Peter Lin 	 */
5985ca483eSYu Chien Peter Lin 	unsigned int mask = BIT(d->hwirq % BITS_PER_LONG);
6085ca483eSYu Chien Peter Lin 
6185ca483eSYu Chien Peter Lin 	if (d->hwirq < ANDES_SLI_CAUSE_BASE)
6285ca483eSYu Chien Peter Lin 		csr_clear(CSR_IE, mask);
6385ca483eSYu Chien Peter Lin 	else
6485ca483eSYu Chien Peter Lin 		csr_clear(ANDES_CSR_SLIE, mask);
6585ca483eSYu Chien Peter Lin }
6685ca483eSYu Chien Peter Lin 
andes_intc_irq_unmask(struct irq_data * d)6785ca483eSYu Chien Peter Lin static void andes_intc_irq_unmask(struct irq_data *d)
6885ca483eSYu Chien Peter Lin {
6985ca483eSYu Chien Peter Lin 	unsigned int mask = BIT(d->hwirq % BITS_PER_LONG);
7085ca483eSYu Chien Peter Lin 
7185ca483eSYu Chien Peter Lin 	if (d->hwirq < ANDES_SLI_CAUSE_BASE)
7285ca483eSYu Chien Peter Lin 		csr_set(CSR_IE, mask);
7385ca483eSYu Chien Peter Lin 	else
7485ca483eSYu Chien Peter Lin 		csr_set(ANDES_CSR_SLIE, mask);
7585ca483eSYu Chien Peter Lin }
7685ca483eSYu Chien Peter Lin 
riscv_intc_irq_eoi(struct irq_data * d)77f8415f2dSAnup Patel static void riscv_intc_irq_eoi(struct irq_data *d)
78f8415f2dSAnup Patel {
79f8415f2dSAnup Patel 	/*
80f8415f2dSAnup Patel 	 * The RISC-V INTC driver uses handle_percpu_devid_irq() flow
81f8415f2dSAnup Patel 	 * for the per-HART local interrupts and child irqchip drivers
82f8415f2dSAnup Patel 	 * (such as PLIC, SBI IPI, CLINT, APLIC, IMSIC, etc) implement
83f8415f2dSAnup Patel 	 * chained handlers for the per-HART local interrupts.
84f8415f2dSAnup Patel 	 *
85f8415f2dSAnup Patel 	 * In the absence of irq_eoi(), the chained_irq_enter() and
86f8415f2dSAnup Patel 	 * chained_irq_exit() functions (used by child irqchip drivers)
87f8415f2dSAnup Patel 	 * will do unnecessary mask/unmask of per-HART local interrupts
88f8415f2dSAnup Patel 	 * at the time of handling interrupts. To avoid this, we provide
89f8415f2dSAnup Patel 	 * an empty irq_eoi() callback for RISC-V INTC irqchip.
90f8415f2dSAnup Patel 	 */
91f8415f2dSAnup Patel }
92f8415f2dSAnup Patel 
936b7ce892SAnup Patel static struct irq_chip riscv_intc_chip = {
946b7ce892SAnup Patel 	.name = "RISC-V INTC",
956b7ce892SAnup Patel 	.irq_mask = riscv_intc_irq_mask,
966b7ce892SAnup Patel 	.irq_unmask = riscv_intc_irq_unmask,
97f8415f2dSAnup Patel 	.irq_eoi = riscv_intc_irq_eoi,
986b7ce892SAnup Patel };
996b7ce892SAnup Patel 
10085ca483eSYu Chien Peter Lin static struct irq_chip andes_intc_chip = {
10185ca483eSYu Chien Peter Lin 	.name		= "RISC-V INTC",
10285ca483eSYu Chien Peter Lin 	.irq_mask	= andes_intc_irq_mask,
10385ca483eSYu Chien Peter Lin 	.irq_unmask	= andes_intc_irq_unmask,
10485ca483eSYu Chien Peter Lin 	.irq_eoi	= riscv_intc_irq_eoi,
10585ca483eSYu Chien Peter Lin };
10685ca483eSYu Chien Peter Lin 
riscv_intc_domain_map(struct irq_domain * d,unsigned int irq,irq_hw_number_t hwirq)1076b7ce892SAnup Patel static int riscv_intc_domain_map(struct irq_domain *d, unsigned int irq,
1086b7ce892SAnup Patel 				 irq_hw_number_t hwirq)
1096b7ce892SAnup Patel {
11085ca483eSYu Chien Peter Lin 	struct irq_chip *chip = d->host_data;
11185ca483eSYu Chien Peter Lin 
1126b7ce892SAnup Patel 	irq_set_percpu_devid(irq);
11385ca483eSYu Chien Peter Lin 	irq_domain_set_info(d, irq, hwirq, chip, NULL, handle_percpu_devid_irq,
11485ca483eSYu Chien Peter Lin 			    NULL, NULL);
1156b7ce892SAnup Patel 
1166b7ce892SAnup Patel 	return 0;
1176b7ce892SAnup Patel }
1186b7ce892SAnup Patel 
riscv_intc_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * arg)119832f15f4SAnup Patel static int riscv_intc_domain_alloc(struct irq_domain *domain,
120832f15f4SAnup Patel 				   unsigned int virq, unsigned int nr_irqs,
121832f15f4SAnup Patel 				   void *arg)
122832f15f4SAnup Patel {
123832f15f4SAnup Patel 	int i, ret;
124832f15f4SAnup Patel 	irq_hw_number_t hwirq;
125832f15f4SAnup Patel 	unsigned int type = IRQ_TYPE_NONE;
126832f15f4SAnup Patel 	struct irq_fwspec *fwspec = arg;
127832f15f4SAnup Patel 
128832f15f4SAnup Patel 	ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
129832f15f4SAnup Patel 	if (ret)
130832f15f4SAnup Patel 		return ret;
131832f15f4SAnup Patel 
13248209534SYu Chien Peter Lin 	/*
13348209534SYu Chien Peter Lin 	 * Only allow hwirq for which we have corresponding standard or
13448209534SYu Chien Peter Lin 	 * custom interrupt enable register.
13548209534SYu Chien Peter Lin 	 */
13648209534SYu Chien Peter Lin 	if ((hwirq >= riscv_intc_nr_irqs && hwirq < riscv_intc_custom_base) ||
13748209534SYu Chien Peter Lin 	    (hwirq >= riscv_intc_custom_base + riscv_intc_custom_nr_irqs))
13848209534SYu Chien Peter Lin 		return -EINVAL;
13948209534SYu Chien Peter Lin 
140832f15f4SAnup Patel 	for (i = 0; i < nr_irqs; i++) {
141832f15f4SAnup Patel 		ret = riscv_intc_domain_map(domain, virq + i, hwirq + i);
142832f15f4SAnup Patel 		if (ret)
143832f15f4SAnup Patel 			return ret;
144832f15f4SAnup Patel 	}
145832f15f4SAnup Patel 
146832f15f4SAnup Patel 	return 0;
147832f15f4SAnup Patel }
148832f15f4SAnup Patel 
1496b7ce892SAnup Patel static const struct irq_domain_ops riscv_intc_domain_ops = {
1506b7ce892SAnup Patel 	.map	= riscv_intc_domain_map,
1516b7ce892SAnup Patel 	.xlate	= irq_domain_xlate_onecell,
152832f15f4SAnup Patel 	.alloc	= riscv_intc_domain_alloc
1536b7ce892SAnup Patel };
1546b7ce892SAnup Patel 
riscv_intc_hwnode(void)1550c60a31cSAnup Patel static struct fwnode_handle *riscv_intc_hwnode(void)
1560c60a31cSAnup Patel {
1570c60a31cSAnup Patel 	return intc_domain->fwnode;
1580c60a31cSAnup Patel }
1590c60a31cSAnup Patel 
riscv_intc_init_common(struct fwnode_handle * fn,struct irq_chip * chip)16085ca483eSYu Chien Peter Lin static int __init riscv_intc_init_common(struct fwnode_handle *fn,
16185ca483eSYu Chien Peter Lin 					 struct irq_chip *chip)
1627023b9d8SSunil V L {
1637023b9d8SSunil V L 	int rc;
1647023b9d8SSunil V L 
16585ca483eSYu Chien Peter Lin 	intc_domain = irq_domain_create_tree(fn, &riscv_intc_domain_ops, chip);
1667023b9d8SSunil V L 	if (!intc_domain) {
1677023b9d8SSunil V L 		pr_err("unable to add IRQ domain\n");
1687023b9d8SSunil V L 		return -ENXIO;
1697023b9d8SSunil V L 	}
1707023b9d8SSunil V L 
1717023b9d8SSunil V L 	rc = set_handle_irq(&riscv_intc_irq);
1727023b9d8SSunil V L 	if (rc) {
1737023b9d8SSunil V L 		pr_err("failed to set irq handler\n");
1747023b9d8SSunil V L 		return rc;
1757023b9d8SSunil V L 	}
1767023b9d8SSunil V L 
1777023b9d8SSunil V L 	riscv_set_intc_hwnode_fn(riscv_intc_hwnode);
1787023b9d8SSunil V L 
17948209534SYu Chien Peter Lin 	pr_info("%d local interrupts mapped\n", riscv_intc_nr_irqs);
18048209534SYu Chien Peter Lin 	if (riscv_intc_custom_nr_irqs) {
18148209534SYu Chien Peter Lin 		pr_info("%d custom local interrupts mapped\n",
18248209534SYu Chien Peter Lin 			riscv_intc_custom_nr_irqs);
18348209534SYu Chien Peter Lin 	}
1847023b9d8SSunil V L 
1857023b9d8SSunil V L 	return 0;
1867023b9d8SSunil V L }
1877023b9d8SSunil V L 
riscv_intc_init(struct device_node * node,struct device_node * parent)1886b7ce892SAnup Patel static int __init riscv_intc_init(struct device_node *node,
1896b7ce892SAnup Patel 				  struct device_node *parent)
1906b7ce892SAnup Patel {
19185ca483eSYu Chien Peter Lin 	struct irq_chip *chip = &riscv_intc_chip;
192ad635e72SSunil V L 	unsigned long hartid;
19385ca483eSYu Chien Peter Lin 	int rc;
1946b7ce892SAnup Patel 
195ad635e72SSunil V L 	rc = riscv_of_parent_hartid(node, &hartid);
196ad635e72SSunil V L 	if (rc < 0) {
197559fe74bSPalmer Dabbelt 		pr_warn("unable to find hart id for %pOF\n", node);
1986b7ce892SAnup Patel 		return 0;
1996b7ce892SAnup Patel 	}
2006b7ce892SAnup Patel 
2016b7ce892SAnup Patel 	/*
2026b7ce892SAnup Patel 	 * The DT will have one INTC DT node under each CPU (or HART)
2036b7ce892SAnup Patel 	 * DT node so riscv_intc_init() function will be called once
2046b7ce892SAnup Patel 	 * for each INTC DT node. We only need to do INTC initialization
2056b7ce892SAnup Patel 	 * for the INTC DT node belonging to boot CPU (or boot HART).
2066b7ce892SAnup Patel 	 */
207e13cd66bSAnup Patel 	if (riscv_hartid_to_cpuid(hartid) != smp_processor_id()) {
208e13cd66bSAnup Patel 		/*
209e13cd66bSAnup Patel 		 * The INTC nodes of each CPU are suppliers for downstream
210e13cd66bSAnup Patel 		 * interrupt controllers (such as PLIC, IMSIC and APLIC
211e13cd66bSAnup Patel 		 * direct-mode) so we should mark an INTC node as initialized
212e13cd66bSAnup Patel 		 * if we are not creating IRQ domain for it.
213e13cd66bSAnup Patel 		 */
214e13cd66bSAnup Patel 		fwnode_dev_initialized(of_fwnode_handle(node), true);
2156b7ce892SAnup Patel 		return 0;
216e13cd66bSAnup Patel 	}
2176b7ce892SAnup Patel 
21885ca483eSYu Chien Peter Lin 	if (of_device_is_compatible(node, "andestech,cpu-intc")) {
21985ca483eSYu Chien Peter Lin 		riscv_intc_custom_base = ANDES_SLI_CAUSE_BASE;
22085ca483eSYu Chien Peter Lin 		riscv_intc_custom_nr_irqs = ANDES_RV_IRQ_LAST;
22185ca483eSYu Chien Peter Lin 		chip = &andes_intc_chip;
22285ca483eSYu Chien Peter Lin 	}
22385ca483eSYu Chien Peter Lin 
22485ca483eSYu Chien Peter Lin 	return riscv_intc_init_common(of_node_to_fwnode(node), chip);
2256b7ce892SAnup Patel }
2266b7ce892SAnup Patel 
2276b7ce892SAnup Patel IRQCHIP_DECLARE(riscv, "riscv,cpu-intc", riscv_intc_init);
22885ca483eSYu Chien Peter Lin IRQCHIP_DECLARE(andes, "andestech,cpu-intc", riscv_intc_init);
2297023b9d8SSunil V L 
2307023b9d8SSunil V L #ifdef CONFIG_ACPI
2317023b9d8SSunil V L 
riscv_intc_acpi_init(union acpi_subtable_headers * header,const unsigned long end)2327023b9d8SSunil V L static int __init riscv_intc_acpi_init(union acpi_subtable_headers *header,
2337023b9d8SSunil V L 				       const unsigned long end)
2347023b9d8SSunil V L {
2357023b9d8SSunil V L 	struct acpi_madt_rintc *rintc;
236*1c88d94aSSunil V L 	struct fwnode_handle *fn;
237*1c88d94aSSunil V L 	int rc;
2387023b9d8SSunil V L 
2397023b9d8SSunil V L 	rintc = (struct acpi_madt_rintc *)header;
2407023b9d8SSunil V L 
2417023b9d8SSunil V L 	/*
2427023b9d8SSunil V L 	 * The ACPI MADT will have one INTC for each CPU (or HART)
2437023b9d8SSunil V L 	 * so riscv_intc_acpi_init() function will be called once
2447023b9d8SSunil V L 	 * for each INTC. We only do INTC initialization
2457023b9d8SSunil V L 	 * for the INTC belonging to the boot CPU (or boot HART).
2467023b9d8SSunil V L 	 */
2477023b9d8SSunil V L 	if (riscv_hartid_to_cpuid(rintc->hart_id) != smp_processor_id())
2487023b9d8SSunil V L 		return 0;
2497023b9d8SSunil V L 
2507023b9d8SSunil V L 	fn = irq_domain_alloc_named_fwnode("RISCV-INTC");
2517023b9d8SSunil V L 	if (!fn) {
2527023b9d8SSunil V L 		pr_err("unable to allocate INTC FW node\n");
2537023b9d8SSunil V L 		return -ENOMEM;
2547023b9d8SSunil V L 	}
2557023b9d8SSunil V L 
256*1c88d94aSSunil V L 	rc = riscv_intc_init_common(fn, &riscv_intc_chip);
257*1c88d94aSSunil V L 	if (rc)
258*1c88d94aSSunil V L 		irq_domain_free_fwnode(fn);
259*1c88d94aSSunil V L 
260*1c88d94aSSunil V L 	return rc;
2617023b9d8SSunil V L }
2627023b9d8SSunil V L 
2637023b9d8SSunil V L IRQCHIP_ACPI_DECLARE(riscv_intc, ACPI_MADT_TYPE_RINTC, NULL,
2647023b9d8SSunil V L 		     ACPI_MADT_RINTC_VERSION_V1, riscv_intc_acpi_init);
2657023b9d8SSunil V L #endif
266