[PATCH RFC 01/19] riscv: Prepare ESP32-P4 early CLIC trap vectors

From: Vítor Hugo Silva

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


The ESP32-P4 HP cores run with mtvec.MODE fixed to CLIC. The legacy
mie and mip CSRs cannot mask CLIC interrupts, and mtvec.BASE requires
64-byte alignment.

Add an opt-in configuration for RV32, single-hart M-mode. Mask local
interrupts through mstatus.MIE at kernel entry. Align the early park,
PMP probe and normal exception vectors to 64 bytes.

AI assisted with the trap entry changes and elaborate the help
description.

Link: https://documentation.espressif.com/esp32-p4_technical_reference_manual_en.pdf
Signed-off-by: Vítor Hugo Silva <vitorhg20080@xxxxxxxxx>
---
arch/riscv/Kconfig.socs | 9 +++++++++
arch/riscv/kernel/entry.S | 6 ++++++
arch/riscv/kernel/head.S | 13 +++++++++++++
3 files changed, 28 insertions(+)

diff --git a/arch/riscv/Kconfig.socs b/arch/riscv/Kconfig.socs
index cecbce0c4983..0272923c7d88 100644
--- a/arch/riscv/Kconfig.socs
+++ b/arch/riscv/Kconfig.socs
@@ -12,6 +12,15 @@ config ARCH_ANLOGIC
help
This enables support for Anlogic SoC platform hardware.

+config RISCV_ESP32P4_CLIC
+ bool "Espressif ESP32-P4 early CLIC setup"
+ depends on ARCH_RV32I && RISCV_M_MODE && !SMP
+ help
+ Enable early machine-mode trap setup for a single ESP32-P4 hart.
+ This aligns the trap vectors to 64 bytes as required by the CLIC.
+ During kernel entry, it clears mstatus.MIE to mask interrupts,
+ since the legacy mie and mip CSRs do not control the CLIC.
+
config ARCH_ESWIN
bool "ESWIN SoCs"
help
diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
index 61b7a7716697..f659bf62bb39 100644
--- a/arch/riscv/kernel/entry.S
+++ b/arch/riscv/kernel/entry.S
@@ -125,7 +125,13 @@
CONFIG_RISCV_USER_CFI)
.endm

+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* mtvec.BASE starts at bit 6 in CLIC mode. */
+ .balign 64
+SYM_CODE_START_NOALIGN(handle_exception)
+#else
SYM_CODE_START(handle_exception)
+#endif
/*
* If coming from userspace, preserve the user thread pointer and load
* the kernel thread pointer. If we came from the kernel, the scratch
diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
index 1631fd6bc786..f266c4238b2f 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -183,7 +183,11 @@ secondary_start_sbi:
call smp_callin
#endif /* CONFIG_SMP */

+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+.balign 64
+#else
.align 2
+#endif
.Lsecondary_park:
/*
* Park this hart if we:
@@ -210,9 +214,14 @@ secondary_start_sbi:
SYM_CODE_END(_start)

SYM_CODE_START(_start_kernel)
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+ /* The CLIC uses mstatus.MIE to mask interrupts globally. */
+ csrci CSR_STATUS, SR_IE
+#else
/* Mask all interrupts */
csrw CSR_IE, zero
csrw CSR_IP, zero
+#endif

#ifdef CONFIG_RISCV_M_MODE
/* flush the instruction cache */
@@ -233,7 +242,11 @@ SYM_CODE_START(_start_kernel)
csrw CSR_PMPADDR0, a0
li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
csrw CSR_PMPCFG0, a0
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+.balign 64
+#else
.align 2
+#endif
.Lpmp_done:

/*
--
2.56.1