[PATCH RFC 02/19] riscv: Handle ESP32-P4 CLIC trap context

From: Vítor Hugo Silva

Date: Wed Nov 25 2026 - 18:30:45 EST


The ESP32-P4 CLIC encodes trap return state alongside the cause
code and interrupt flag in mcause. The RISC-V exception dispatcher
expects only the cause code and interrupt flag.

Save the raw mcause in pt_regs, normalize the cause for dispatch
and translate MPP and MPIE into the saved status layout. Restore
the saved interrupt level and rebuild MPP and MPIE from pt_regs
on the non-vectored trap return path.

The current CLIC level stays active until mret. irq_exit_rcu() can
run softirqs with interrupts enabled, and irqentry_exit() can
schedule before the final mret. Holding the hardirq level across
either path blocks interrupts at that level or below.

Restore the interrupted context's level with an M-mode mret
after the hardirq handler and before irq_exit_rcu(). Keep interrupts
masked across this intermediate return and retain the original
trap frame.

Tested on a Waveshare ESP32-P4-Module-DEV-KIT with revision v3.2
silicon. The kernel booted to a UART shell, completed 20 consecutive
two-process pipelines and continued to receive CLINT timer and UART0
interrupts.

AI assisted with the assembly and JTAG diagnosis.

Link: https://documentation.espressif.com/esp32-p4_technical_reference_manual_en.pdf
Signed-off-by: Vítor Hugo Silva <vitorhg20080@xxxxxxxxx>
---
arch/riscv/include/asm/asm-prototypes.h | 4 ++
arch/riscv/include/asm/ptrace.h | 4 ++
arch/riscv/kernel/asm-offsets.c | 3 ++
arch/riscv/kernel/entry.S | 61 +++++++++++++++++++++++++
arch/riscv/kernel/traps.c | 4 ++
5 files changed, 76 insertions(+)

diff --git a/arch/riscv/include/asm/asm-prototypes.h b/arch/riscv/include/asm/asm-prototypes.h
index a0ca9efff267..e0fec71022f9 100644
--- a/arch/riscv/include/asm/asm-prototypes.h
+++ b/arch/riscv/include/asm/asm-prototypes.h
@@ -64,4 +64,8 @@ asmlinkage void handle_bad_stack(struct pt_regs *regs);
asmlinkage void do_page_fault(struct pt_regs *regs);
asmlinkage void do_irq(struct pt_regs *regs);

+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+asmlinkage void esp32p4_clic_restore_irq_level(unsigned long cause);
+#endif
+
#endif /* _ASM_RISCV_PROTOTYPES_H */
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index addc8188152f..5729fbac0b57 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -53,6 +53,10 @@ struct pt_regs {
unsigned long cause;
/* a0 value before the syscall */
unsigned long orig_a0;
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* Raw mcause for CLIC trap return */
+ unsigned long clic_mcause;
+#endif
};

#define PTRACE_SYSEMU 0x1f
diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c
index a75f0cfea1e9..9f8310dd0a6e 100644
--- a/arch/riscv/kernel/asm-offsets.c
+++ b/arch/riscv/kernel/asm-offsets.c
@@ -130,6 +130,9 @@ void asm_offsets(void)
OFFSET(PT_STATUS, pt_regs, status);
OFFSET(PT_BADADDR, pt_regs, badaddr);
OFFSET(PT_CAUSE, pt_regs, cause);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ OFFSET(PT_CLIC_MCAUSE, pt_regs, clic_mcause);
+#endif

OFFSET(SUSPEND_CONTEXT_REGS, suspend_context, regs);

diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index f659bf62bb39..91269ab82c60 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -196,6 +196,22 @@ SYM_CODE_START(handle_exception)
csrr s2, CSR_EPC
csrr s3, CSR_TVAL
csrr s4, CSR_CAUSE
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ REG_S s4, PT_CLIC_MCAUSE(sp)
+ /* Translate CLIC MPP and MPIE into the saved mstatus layout. */
+ li t0, ~(SR_MPP | SR_MPIE)
+ and s1, s1, t0
+ srli t0, s4, 28 - 11
+ li t1, SR_MPP
+ and t0, t0, t1
+ or s1, s1, t0
+ srli t0, s4, 27 - 7
+ andi t0, t0, SR_MPIE
+ or s1, s1, t0
+ /* Keep the interrupt flag and the six bit CLIC cause code. */
+ li t0, CAUSE_IRQ_FLAG | 0x3f
+ and s4, s4, t0
+#endif
csrr s5, CSR_SCRATCH
REG_S s0, PT_SP(sp)
REG_S s1, PT_STATUS(sp)
@@ -311,6 +327,25 @@ SYM_CODE_START_NOALIGN(ret_from_exception)

csrw CSR_STATUS, a0
csrw CSR_EPC, a2
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /*
+ * Preserve INT, MPIL and CODE from the saved trap.
+ * Clear MINHV so mret uses mepc as an instruction address.
+ */
+ REG_L a3, PT_CLIC_MCAUSE(sp)
+ li a4, CAUSE_IRQ_FLAG | (0xff << 16) | 0x3f
+ and a3, a3, a4
+
+ /* Rebuild MPP and MPIE from the current pt_regs status. */
+ li a4, SR_MPP
+ and a4, a0, a4
+ slli a4, a4, 28 - 11
+ or a3, a3, a4
+ andi a4, a0, SR_MPIE
+ slli a4, a4, 27 - 7
+ or a3, a3, a4
+ csrw CSR_CAUSE, a3
+#endif

REG_L x1, PT_RA(sp)
REG_L x3, PT_GP(sp)
@@ -329,6 +364,32 @@ SYM_INNER_LABEL(ret_from_exception_end, SYM_L_GLOBAL)
SYM_CODE_END(ret_from_exception)
ASM_NOKPROBE(ret_from_exception)

+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+/* Restore MPIL from the IRQ frame without leaving M-mode. */
+SYM_FUNC_START(esp32p4_clic_restore_irq_level)
+ csrr t0, CSR_CAUSE
+ csrr t1, CSR_EPC
+ csrrci t2, CSR_STATUS, SR_IE
+
+ li t3, CAUSE_IRQ_FLAG | (0xff << 16) | 0x3f
+ and a0, a0, t3
+ /* MPP=M and MPIE=0 keep interrupts disabled on the synthetic return. */
+ li t3, 3 << 28
+ or a0, a0, t3
+ csrw CSR_CAUSE, a0
+ lla a0, .Lclic_irq_level_restored
+ csrw CSR_EPC, a0
+ mret
+
+.Lclic_irq_level_restored:
+ csrw CSR_CAUSE, t0
+ csrw CSR_EPC, t1
+ csrw CSR_STATUS, t2
+ ret
+SYM_FUNC_END(esp32p4_clic_restore_irq_level)
+ASM_NOKPROBE(esp32p4_clic_restore_irq_level)
+#endif
+
#ifdef CONFIG_VMAP_STACK
SYM_CODE_START_LOCAL(handle_kernel_stack_overflow)
/* we reach here from kernel context, sscratch must be 0 */
diff --git a/arch/riscv/kernel/traps.c b/arch/riscv/kernel/traps.c
index f8292adda099..28343a601969 100644
--- a/arch/riscv/kernel/traps.c
+++ b/arch/riscv/kernel/traps.c
@@ -428,6 +428,10 @@ static void noinstr handle_riscv_irq(struct pt_regs *regs)
old_regs = set_irq_regs(regs);
handle_arch_irq(regs);
set_irq_regs(old_regs);
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* Restore the interrupted level before irq_exit_rcu() can run softirqs. */
+ esp32p4_clic_restore_irq_level(regs->clic_mcause);
+#endif
irq_exit_rcu();
}

--
2.56.1