git: d584228930a1 - main - plic, aplic: handle all pending interrupts for hart

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Wed, 30 Oct 2024 18:21:01 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=d584228930a1689479d62fb303a51c42ecd7b6ec

commit d584228930a1689479d62fb303a51c42ecd7b6ec
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2024-10-30 18:16:47 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2024-10-30 18:16:47 +0000

    plic, aplic: handle all pending interrupts for hart
    
    Otherwise, we are going to take another interrupt-induced exception
    immediately upon execution of the sret instruction -- overall wasting
    cycles.
    
    Reviewed by:    br
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D47134
---
 sys/riscv/riscv/aplic.c | 18 ++++++++++--------
 sys/riscv/riscv/plic.c  |  5 ++---
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/sys/riscv/riscv/aplic.c b/sys/riscv/riscv/aplic.c
index af58bb014745..6550b7ba36d1 100644
--- a/sys/riscv/riscv/aplic.c
+++ b/sys/riscv/riscv/aplic.c
@@ -224,21 +224,23 @@ aplic_intr(void *arg)
 {
 	struct aplic_softc *sc;
 	struct trapframe *tf;
-	u_int claimi, prio, irq;
+	uint32_t claimi;
+	u_int prio, irq;
 	int cpu;
 
 	sc = arg;
 	cpu = PCPU_GET(cpuid);
 
-	/* Claim any pending interrupt. */
-	claimi = aplic_read(sc, APLIC_IDC_CLAIMI(sc, cpu));
-	prio = APLIC_IDC_CLAIMI_PRIO(claimi);
-	irq = APLIC_IDC_CLAIMI_IRQ(claimi);
+	/* Claim all pending interrupts. */
+	while ((claimi = aplic_read(sc, APLIC_IDC_CLAIMI(sc, cpu))) != 0) {
+		prio = APLIC_IDC_CLAIMI_PRIO(claimi);
+		irq = APLIC_IDC_CLAIMI_IRQ(claimi);
 
-	KASSERT((irq != 0), ("Invalid IRQ 0"));
+		KASSERT((irq != 0), ("Invalid IRQ 0"));
 
-	tf = curthread->td_intr_frame;
-	aplic_irq_dispatch(sc, irq, prio, tf);
+		tf = curthread->td_intr_frame;
+		aplic_irq_dispatch(sc, irq, prio, tf);
+	}
 
 	return (FILTER_HANDLED);
 }
diff --git a/sys/riscv/riscv/plic.c b/sys/riscv/riscv/plic.c
index 7ae68c00d78a..e28019e6f458 100644
--- a/sys/riscv/riscv/plic.c
+++ b/sys/riscv/riscv/plic.c
@@ -175,9 +175,8 @@ plic_intr(void *arg)
 	sc = arg;
 	cpu = PCPU_GET(cpuid);
 
-	/* Claim any pending interrupt. */
-	pending = RD4(sc, PLIC_CLAIM(sc, cpu));
-	if (pending) {
+	/* Claim all pending interrupts. */
+	while ((pending = RD4(sc, PLIC_CLAIM(sc, cpu))) != 0) {
 		tf = curthread->td_intr_frame;
 		plic_irq_dispatch(sc, pending, tf);
 	}