New RISC-V Local Interrupt Controller Driver
Subsystems (6)
v3 cover New RISC-V Local Interrupt Controller Driver
This patchset provides a new RISC-V Local Interrupt Controller Driver for managing per-CPU local interrupts. The overall approach is inspired from the way per-CPU local interrupts are handled by Linux ARM64 and ARM GICv3 driver.
Few advantages of having this new driver are as follows:
1. It registers all local interrupts as per-CPU interrupts
2. We can develop drivers for devices with per-CPU local interrupts without changing arch code or this driver
3. It allows local interrupt controller DT node under each CPU DT node as well as single system-wide DT node for local interrupt controller.
With this patchset, output of "cat /proc/interrupts" looks as follows:
CPU0 CPU1 CPU2 CPU3
2: 379 0 0 0 SiFive PLIC 10 ttyS0 3: 591 0 0 0 SiFive PLIC 8 virtio0 5: 5079 10821 8435 12984 RISC-V INTC 5 riscv-timer IPI0: 2045 2537 891 870 Rescheduling interrupts IPI1: 9 269 91 168 Function call interrupts IPI2: 0 0 0 0 CPU stop interrupts
The patchset is based up Linux-5.6-rc2 and can be found at riscv_intc_v3 branch of: https://github.com/avpatel/linux.git
Changes since v2:
- Dropped PATCH2 since it was merged long-time back
- Rebased series from Linux-4.19-rc2 to Linux-5.6-rc2
Changes since v1:
- Removed changes related to puggable IPI triggering
- Separate patch for self-contained IPI handling routine
- Removed patch for GENERIC_IRQ kconfig options
- Added patch to remove do_IRQ() function
- Rebased upon Atish's SMP patches
Anup Patel (4):
RISC-V: self-contained IPI handling routine
irqchip: RISC-V Per-HART Local Interrupt Controller Driver
clocksource: timer-riscv: Make timer interrupt as a per-CPU interrupt
RISC-V: Remove do_IRQ() function
arch/riscv/Kconfig | 1 + arch/riscv/include/asm/irq.h | 5 -- arch/riscv/include/asm/smp.h | 3 + arch/riscv/kernel/entry.S | 4 +- arch/riscv/kernel/irq.c | 33 +------- arch/riscv/kernel/smp.c | 11 ++- arch/riscv/kernel/traps.c | 2 - drivers/clocksource/timer-riscv.c | 79 ++++++++++++------ drivers/irqchip/Kconfig | 13 +++ drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-intc.c | 131 ++++++++++++++++++++++++++++++ drivers/irqchip/irq-sifive-plic.c | 39 ++++++++- include/linux/cpuhotplug.h | 2 + 13 files changed, 252 insertions(+), 72 deletions(-) create mode 100644 drivers/irqchip/irq-riscv-intc.c
Signature · 1 line
2.17.1
PATCH v3 1/4 RISC-V: self-contained IPI handling routine
Currently, the IPI handling routine riscv_software_interrupt() does not take any argument and also does not perform irq_enter()/irq_exit().
This patch makes IPI handling routine more self-contained by:
1. Passing "pt_regs *" argument
2. Explicitly doing irq_enter()/irq_exit()
3. Explicitly save/restore "pt_regs *" using set_irq_regs()
With above changes, IPI handling routine does not depend on caller function to perform irq_enter()/irq_exit() and save/restore of "pt_regs *" hence its more self-contained. This also enables us to call IPI handling routine from IRQCHIP drivers.
--- arch/riscv/include/asm/irq.h | 1 - arch/riscv/include/asm/smp.h | 3 +++ arch/riscv/kernel/irq.c | 16 ++++++++++------ arch/riscv/kernel/smp.c | 11 +++++++++-- 4 files changed, 22 insertions(+), 9 deletions(-)
arch/riscv/include/asm/irq.h +0 -1
@@ -13,7 +13,6 @@#define NR_IRQS 0void riscv_timer_interrupt (void ); -void riscv_software_interrupt (void );#include <asm-generic/irq.h>
arch/riscv/include/asm/smp.h +3 -0
@@ -28,6 +28,9 @@ void show_ipi_stats(struct seq_file *p, int prec);/* SMP initialization hook for setup_arch */ void __init setup_smp (void ); +/* Called from C code, this handles an IPI. */ +void handle_IPI (struct pt_regs *regs); +/* Hook for the generic smp_call_function_many() routine. */ void arch_send_call_function_ipi_mask (struct cpumask *mask);
arch/riscv/kernel/irq.c +10 -6
@@ -19,12 +19,15 @@ int arch_show_interrupts(struct seq_file *p, int prec) asmlinkage __visible void __irq_entry do_IRQ(struct pt_regs *regs) { -struct pt_regs *old_regs =set_irq_regs (regs); +struct pt_regs *old_regs; -irq_enter ();switch (regs->cause & ~CAUSE_IRQ_FLAG ) {case RV_IRQ_TIMER : + old_regs =set_irq_regs (regs); +irq_enter ();riscv_timer_interrupt (); +irq_exit (); +set_irq_regs (old_regs);break ;#ifdef CONFIG_SMP case RV_IRQ_SOFT :
@@ -32,19 +35,20 @@ asmlinkage __visible void __irq_entry do_IRQ(struct pt_regs *regs)* We only use software interrupts to pass IPIs, so if a non-SMP * system gets one, then we don't know what to do. */ -riscv_software_interrupt (); +handle_IPI (regs);break ;#endif case RV_IRQ_EXT : + old_regs =set_irq_regs (regs); +irq_enter ();handle_arch_irq (regs); +irq_exit (); +set_irq_regs (old_regs);break ;default :pr_alert ("unexpected interrupt cause 0x%lx" , regs->cause);BUG (); } -irq_exit (); - -set_irq_regs (old_regs); }void __init init_IRQ (void )
arch/riscv/kernel/smp.c +9 -2
@@ -121,11 +121,14 @@ static inline void clear_ipi(void)clint_clear_ipi (cpuid_to_hartid_map (smp_processor_id ())); } -void riscv_software_interrupt (void ) +void handle_IPI (struct pt_regs *regs) { +struct pt_regs *old_regs =set_irq_regs (regs);unsigned long *pending_ipis = &ipi_data[smp_processor_id ()].bits;unsigned long *stats = ipi_data[smp_processor_id ()].stats; +irq_enter (); +clear_ipi ();while (true) {
@@ -136,7 +139,7 @@ void riscv_software_interrupt(void) ops =xchg (pending_ipis,0 );if (ops ==0 ) -return ; + goto done;if (ops & (1 <<IPI_RESCHEDULE )) { stats[IPI_RESCHEDULE ]++;
@@ -158,6 +161,10 @@ void riscv_software_interrupt(void)/* Order data access and bit testing. */ mb (); } + +done: +irq_exit (); +set_irq_regs (old_regs); }static const char *const ipi_names[] = {
Signature · 1 line
2.17.1
PATCH v3 2/4 irqchip: RISC-V Per-HART Local Interrupt Controller Driver
In reply to Anup PatelThe RISC-V per-HART local interrupt controller manages software interrupts, timer interrupts, external interrupts (which are routed via the platform level interrupt controller) and per-HART local interrupts.
This patch add a driver for RISC-V local interrupt controller. It is a major re-write over perviously submitted version. (Refer, https://www.spinics.net/lists/devicetree/msg241230.html)
Few advantages of this new driver over previous one are:
1. It registers all local interrupts as per-CPU interrupts
2. We can develop drivers for devices with per-CPU local interrupts without changing arch code or this driver
3. It will be useful to direct interrupt injection in KVM RISC-V as well
The RISC-V INTC driver is compliant with RISC-V Hart-Level Interrupt Controller DT bindings located at:Documentation/devicetree/bindings/interrupt-controller/riscv,cpu-intc.txt
--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/irq.h | 2 - arch/riscv/kernel/irq.c | 33 +------ arch/riscv/kernel/traps.c | 2 - drivers/irqchip/Kconfig | 13 +++ drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-intc.c | 139 ++++++++++++++++++++++++++++++ drivers/irqchip/irq-sifive-plic.c | 39 ++++++++- include/linux/cpuhotplug.h | 2 + 9 files changed, 194 insertions(+), 38 deletions(-) create mode 100644 drivers/irqchip/irq-riscv-intc.c
arch/riscv/Kconfig +1 -0
@@ -39,6 +39,7 @@ config RISCV select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP select HAVE_SYSCALL_TRACEPOINTS + select HANDLE_DOMAIN_IRQ select IRQ_DOMAIN select SPARSE_IRQ select SYSCTL_EXCEPTION_TRACE
arch/riscv/include/asm/irq.h +0 -2
@@ -10,8 +10,6 @@#include <linux/interrupt.h> #include <linux/linkage.h> -#define NR_IRQS 0 -void riscv_timer_interrupt (void );#include <asm-generic/irq.h>
arch/riscv/kernel/irq.c +3 -30
@@ -7,7 +7,6 @@#include <linux/interrupt.h> #include <linux/irqchip.h> -#include <linux/irqdomain.h> #include <linux/seq_file.h> #include <asm/smp.h>
@@ -19,39 +18,13 @@ int arch_show_interrupts(struct seq_file *p, int prec) asmlinkage __visible void __irq_entry do_IRQ(struct pt_regs *regs) { -struct pt_regs *old_regs; - -switch (regs->cause & ~CAUSE_IRQ_FLAG ) { -case RV_IRQ_TIMER : - old_regs =set_irq_regs (regs); -irq_enter (); -riscv_timer_interrupt (); -irq_exit (); -set_irq_regs (old_regs); -break ; -#ifdef CONFIG_SMP -case RV_IRQ_SOFT : -/* -* We only use software interrupts to pass IPIs, so if a non-SMP -* system gets one, then we don't know what to do. -*/ -handle_IPI (regs); -break ; -#endif -case RV_IRQ_EXT : - old_regs =set_irq_regs (regs); -irq_enter (); +if (handle_arch_irq)handle_arch_irq (regs); -irq_exit (); -set_irq_regs (old_regs); -break ; -default : -pr_alert ("unexpected interrupt cause 0x%lx" , regs->cause); -BUG (); - } }void __init init_IRQ (void ) {irqchip_init (); +if (!handle_arch_irq) +panic ("No interrupt controller found." ); }
arch/riscv/kernel/traps.c +0 -2
@@ -156,6 +156,4 @@ void __init trap_init(void)csr_write (CSR_SCRATCH ,0 );/* Set the exception vector address */ csr_write (CSR_TVEC , &handle_exception); -/* Enable all interrupts */ -csr_write (CSR_IE ,-1 ); }
drivers/irqchip/Kconfig +13 -0
@@ -493,6 +493,19 @@ config TI_SCI_INTA_IRQCHIP If you wish to use interrupt aggregator irq resources managed by the TI System Controller, say Y here. Otherwise, say N. +config RISCV_INTC + bool "RISC-V Local Interrupt Controller" + depends on RISCV + default y + help + This enables support for the per-HART local interrupt controller + found in standard RISC-V systems. The per-HART local interrupt + controller handles timer interrupts, software interrupts, and + hardware interrupts. Without a per-HART local interrupt controller, + a RISC-V system will be unable to handle any interrupts. + + If you don't know what to do here, say Y. + config SIFIVE_PLIC bool "SiFive Platform-Level Interrupt Controller" depends on RISCV
drivers/irqchip/Makefile +1 -0
@@ -98,6 +98,7 @@ obj-$(CONFIG_NDS32) += irq-ativic32.o obj-$(CONFIG_QCOM_PDC) += qcom-pdc.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o +obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.o obj-$(CONFIG_IMX_INTMUX) += irq-imx-intmux.o
drivers/irqchip/irq-riscv-intc.c new file +139 -0
139 lines not on this page · raw message
drivers/irqchip/irq-sifive-plic.c +35 -4
@@ -4,10 +4,12 @@* Copyright (C) 2018 Christoph Hellwig */ #define pr_fmt (fmt) "plic: " fmt +#include <linux/cpu.h> #include <linux/interrupt.h> #include <linux/io.h> #include <linux/irq.h> #include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> #include <linux/irqdomain.h> #include <linux/module.h> #include <linux/of.h>
@@ -187,6 +189,7 @@ static const struct irq_domain_ops plic_irqdomain_ops = { .free = irq_domain_free_irqs_top, }; +static int plic_parent_irq;static struct irq_domain *plic_irqdomain;/*
@@ -195,15 +198,17 @@ static struct irq_domain *plic_irqdomain;* that source ID back to the same claim register. This automatically enables * and disables the interrupt, so there's nothing else to do. */ -static void plic_handle_irq (struct pt_regs *regs) +static void plic_handle_irq (struct irq_desc *desc) {struct plic_handler *handler =this_cpu_ptr (&plic_handlers); +struct irq_chip *chip =irq_desc_get_chip (desc);void __iomem *claim = handler->hart_base +CONTEXT_CLAIM ;irq_hw_number_t hwirq;WARN_ON_ONCE (!handler->present); -csr_clear (CSR_IE ,IE_EIE ); +chained_irq_enter (chip, desc); +while ((hwirq =readl (claim))) {int irq =irq_find_mapping (plic_irqdomain, hwirq);
@@ -213,7 +218,23 @@ static void plic_handle_irq(struct pt_regs *regs)else generic_handle_irq (irq); } -csr_set (CSR_IE ,IE_EIE ); + +chained_irq_exit (chip, desc); +} + +static int plic_cpu_starting (unsigned int cpu) +{ +if (plic_parent_irq) +enable_percpu_irq (plic_parent_irq, +irq_get_trigger_type (plic_parent_irq)); +return 0 ; +} + +static int plic_cpu_dying (unsigned int cpu) +{ +if (plic_parent_irq) +disable_percpu_irq (plic_parent_irq); +return 0 ; }/*
@@ -281,6 +302,13 @@ static int __init plic_init(struct device_node *node,if (parent.args[0 ] !=RV_IRQ_EXT )continue ; +if (irq_find_host (parent.np)) { + plic_parent_irq =irq_of_parse_and_map (node, i); +if (plic_parent_irq) +irq_set_chained_handler (plic_parent_irq, + plic_handle_irq); + } + hartid =plic_find_hart_id (parent.np);if (hartid <0 ) {pr_warn ("failed to parse hart ID for context %d.\n" , i);
@@ -320,9 +348,12 @@ static int __init plic_init(struct device_node *node, nr_handlers++; } +cpuhp_setup_state (CPUHP_AP_IRQ_PLIC_STARTING , +"irqchip/sifive/plic:starting" , + plic_cpu_starting, plic_cpu_dying); +pr_info ("mapped %d interrupts with %d handlers for %d contexts.\n" , nr_irqs, nr_handlers, nr_contexts); -set_handle_irq (plic_handle_irq);return 0 ; out_iounmap:
include/linux/cpuhotplug.h +2 -0
@@ -102,6 +102,8 @@ enum cpuhp_state {CPUHP_AP_IRQ_ARMADA_XP_STARTING ,CPUHP_AP_IRQ_BCM2836_STARTING ,CPUHP_AP_IRQ_MIPS_GIC_STARTING , +CPUHP_AP_IRQ_RISCV_STARTING , +CPUHP_AP_IRQ_PLIC_STARTING ,CPUHP_AP_ARM_MVEBU_COHERENCY ,CPUHP_AP_MICROCODE_LOADER , CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING,
Signature · 1 line
2.17.1
PATCH v3 3/4 clocksource: timer-riscv: Make timer interrupt as a per-CPU interrupt
In reply to Anup PatelInstead of directly calling RISC-V timer interrupt handler from RISC-V local interrupt conntroller driver, this patch implements RISC-V timer interrupt as a per-CPU interrupt using per-CPU APIs of Linux IRQ subsystem.
--- arch/riscv/include/asm/irq.h | 2 - drivers/clocksource/timer-riscv.c | 79 ++++++++++++++++++++----------- drivers/irqchip/irq-riscv-intc.c | 8 ---- 3 files changed, 52 insertions(+), 37 deletions(-)
arch/riscv/include/asm/irq.h +0 -2
@@ -10,8 +10,6 @@#include <linux/interrupt.h> #include <linux/linkage.h> -void riscv_timer_interrupt (void ); -#include <asm-generic/irq.h> #endif /* _ASM_RISCV_IRQ_H */
drivers/clocksource/timer-riscv.c +52 -27
130 lines not on this page · raw message
drivers/irqchip/irq-riscv-intc.c +0 -8
@@ -21,20 +21,12 @@ static atomic_t intc_init = ATOMIC_INIT(0); static asmlinkage void riscv_intc_irq(struct pt_regs *regs) { -struct pt_regs *old_regs;unsigned long cause = regs->cause & ~CAUSE_IRQ_FLAG ;if (unlikely (cause >=BITS_PER_LONG ))panic ("unexpected interrupt cause" );switch (cause) { -case RV_IRQ_TIMER : - old_regs =set_irq_regs (regs); -irq_enter (); -riscv_timer_interrupt (); -irq_exit (); -set_irq_regs (old_regs); -break ;#ifdef CONFIG_SMP case RV_IRQ_SOFT :/*
Signature · 1 line
2.17.1
PATCH v3 4/4 RISC-V: Remove do_IRQ() function
In reply to Anup PatelThe only thing do_IRQ() does is call handle_arch_irq function pointer. We can very well call handle_arch_irq function pointer directly from assembly and remove do_IRQ() function hence this patch.
--- arch/riscv/kernel/entry.S | 4 +++- arch/riscv/kernel/irq.c | 6 ------ 2 files changed, 3 insertions(+), 7 deletions(-)
arch/riscv/kernel/entry.S +3 -1
@@ -183,7 +183,9 @@ ENTRY(handle_exception) /* Handle interrupts */ move a0, sp /* pt_regs */ - tail do_IRQ + la a1, handle_arch_irq + REG_L a1, (a1) + jr a1 1: /* * Exceptions run with interrupts enabled or disabled depending on the
arch/riscv/kernel/irq.c +0 -6
@@ -16,12 +16,6 @@ int arch_show_interrupts(struct seq_file *p, int prec)return 0 ; } -asmlinkage __visible void __irq_entry do_IRQ(struct pt_regs *regs) -{ -if (handle_arch_irq) -handle_arch_irq (regs); -} -void __init init_IRQ (void ) {irqchip_init ();
Signature · 1 line
2.17.1