From: "Vítor Hugo Silva" <vitorhg20080@gmail.com>
To: linux-riscv@lists.infradead.org
Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>, Oleg Nesterov <oleg@redhat.com>
Subject: [PATCH RFC 02/19] riscv: Handle ESP32-P4 CLIC trap context
Date: Wed, 25 Nov 2026 23:30:03 +0000 [thread overview]
Message-ID: <20261125233020.1380594-3-vitorhg20080@gmail.com> (raw)
In-Reply-To: <20261125233020.1380594-1-vitorhg20080@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-25 23:30 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 23:30 [PATCH RFC 00/19] riscv: Add initial support for Espressif ESP32-P4 Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 01/19] riscv: Prepare ESP32-P4 early CLIC trap vectors Vítor Hugo Silva
2026-11-25 23:30 ` Vítor Hugo Silva [this message]
2026-11-25 23:30 ` [PATCH RFC 03/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 04/19] irqchip: Add Espressif ESP32-P4 CLIC support Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 05/19] dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt matrix Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 06/19] irqchip: Add Espressif ESP32-P4 interrupt matrix support Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 07/19] dt-bindings: timer: Add Espressif ESP32-P4 CLINT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 08/19] clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 09/19] dt-bindings: serial: Add Espressif ESP32-P4 UART Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 10/19] serial: esp32: Restore the ESP32 UART driver Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 11/19] serial: esp32: Add support for the ESP32-P4 UART Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 12/19] serial: esp32: Rearm TX interrupts when the FIFO is full Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 13/19] serial: esp32: Add early console support for ESP32-P4 Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 14/19] riscv: Synchronize instructions written to ESP32-P4 PSRAM Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 15/19] riscv: Align init data to the ESP32-P4 PMP granularity Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 16/19] dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 17/19] dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 18/19] riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT Vítor Hugo Silva
2026-11-25 23:30 ` [PATCH RFC 19/19] MAINTAINERS: Add the Espressif ESP32-P4 platform Vítor Hugo Silva
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261125233020.1380594-3-vitorhg20080@gmail.com \
--to=vitorhg20080@gmail.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=oleg@redhat.com \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®