Skip to contentSkip to conversation

riscv: Add initial support for Espressif ESP32-P4

RFC PATCH New Vítor Hugo Silva 1 participant 20 messages Nov 25

Subsystems (10)
lore
Share & tools
lore
#0 lore raw

cover riscv: Add initial support for Espressif ESP32-P4

Hello,

This series of patches adds the initial support for the Espressif's ESP32-P4 on the Waveshare ESP32-P4-Module-DEV-KIT. It boots in M-mode on a RV32 NOMMU kernel with one HP hart.

The series adds CLIC trap handling, drivers for clic and for interrupt matrix, and extends the CLINT timer driver. It restores a previously removed ESP32 UART driver with ESP32-P4 UART support and early console support. It also synchronizes the instructions written to the PSRAM, aligns the init data to the ESP32-P4 PMP granularity, and it adds DT bindings and the board devicetree.

The boot firmware initializes PSRAM, loads the kernel, DTB and the initramfs from the microSD, and it supplies the timebase-frequency before jumping into the kernel.

The bootloader source and microSD boot instructions are available at:
https://github.com/pushdword/waveshare-esp32-p4-bootloader/blob/390880e881208ab16aa16eb520b3e5ea0c63c0a2/README.md

I tested this series on the hardware, the Waveshare ESP32-P4-Module-DEV-KIT with the ESP32-P4 revision v3.2 with 32MB of PSRAM. The kernel booted to the UART shell from the initramfs. The /proc/interrupts showed the CLINT IRQ's increased, UART0 interrupts were routed through the interrupts matrix.

It would be nice to you all send feedback on restoring the interrupted CLIC level before irq_exit_rcu() and the interface for propagating instruction-cache sync failures to NOMMU executable loading, and signal setup.

AI disclaimer: I have used 6.2-Sol to assist development and to assist debug/troubleshoot some ongoing issues. I have used Codex's 6-Sol with Daybreak Blue enabled to generate peer-review-like report to pinpoint potential security issues regarding the patches. I manually reviewed and validated each change, including the AI-assisted changes. I also manually tested each change on the hardware as described above.

Vítor Hugo Silva (19):
riscv: Prepare ESP32-P4 early CLIC trap vectors
riscv: Handle ESP32-P4 CLIC trap context
dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC
irqchip: Add Espressif ESP32-P4 CLIC support
dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt
matrix
irqchip: Add Espressif ESP32-P4 interrupt matrix support
dt-bindings: timer: Add Espressif ESP32-P4 CLINT clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support
dt-bindings: serial: Add Espressif ESP32-P4 UART
serial: esp32: Restore the ESP32 UART driver
serial: esp32: Add support for the ESP32-P4 UART
serial: esp32: Rearm TX interrupts when the FIFO is full
serial: esp32: Add early console support for ESP32-P4
riscv: Synchronize instructions written to ESP32-P4 PSRAM
riscv: Align init data to the ESP32-P4 PMP granularity
dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT
dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts
riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT
MAINTAINERS: Add the Espressif ESP32-P4 platform

 .../esp,esp32p4-clic.yaml                     |  53 +
 .../esp,esp32p4-intmtx.yaml                   |  67 ++
 .../devicetree/bindings/riscv/cpus.yaml       |  17 +-
 .../devicetree/bindings/riscv/espressif.yaml  |  22 +
 .../bindings/serial/esp,esp32-uart.yaml       |   4 +-
 .../bindings/timer/sifive,clint.yaml          |  28 +
 MAINTAINERS                                   |  14 +
 arch/riscv/Kconfig.socs                       |   9 +
 arch/riscv/boot/dts/Makefile                  |   1 +
 arch/riscv/boot/dts/espressif/Makefile        |   2 +
 .../esp32p4-waveshare-module-dev-kit.dts      |  46 +
 arch/riscv/boot/dts/espressif/esp32p4.dtsi    |  64 ++
 arch/riscv/include/asm/asm-prototypes.h       |   4 +
 arch/riscv/include/asm/cacheflush.h           |   6 +
 arch/riscv/include/asm/ptrace.h               |   4 +
 arch/riscv/kernel/asm-offsets.c               |   3 +
 arch/riscv/kernel/entry.S                     |  67 ++
 arch/riscv/kernel/head.S                      |  13 +
 arch/riscv/kernel/patch.c                     | 132 ++-
 arch/riscv/kernel/signal.c                    |   6 +-
 arch/riscv/kernel/traps.c                     |   4 +
 arch/riscv/kernel/vmlinux.lds.S               |   4 +
 drivers/clocksource/timer-clint.c             |  57 +-
 drivers/irqchip/Makefile                      |   2 +
 drivers/irqchip/irq-esp32p4-clic.c            | 178 ++++
 drivers/irqchip/irq-esp32p4-intmtx.c          | 139 +++
 drivers/tty/serial/Kconfig                    |  15 +
 drivers/tty/serial/Makefile                   |   1 +
 drivers/tty/serial/esp32_uart.c               | 940 ++++++++++++++++++
 fs/binfmt_flat.c                              |   6 +-
 fs/exec.c                                     |  10 +-
 include/linux/cacheflush.h                    |   7 +
 32 files changed, 1910 insertions(+), 15 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
 create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
 create mode 100644 Documentation/devicetree/bindings/riscv/espressif.yaml
 create mode 100644 arch/riscv/boot/dts/espressif/Makefile
 create mode 100644 arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts
 create mode 100644 arch/riscv/boot/dts/espressif/esp32p4.dtsi
 create mode 100644 drivers/irqchip/irq-esp32p4-clic.c
 create mode 100644 drivers/irqchip/irq-esp32p4-intmtx.c
 create mode 100644 drivers/tty/serial/esp32_uart.c

base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3

Signature · 1 line
2.56.1
#1 lore raw

RFC PATCH 1/19 riscv: Prepare ESP32-P4 early CLIC trap vectors

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.

---
 arch/riscv/Kconfig.socs   |  9 +++++++++
 arch/riscv/kernel/entry.S |  6 ++++++
 arch/riscv/kernel/head.S  | 13 +++++++++++++
 3 files changed, 28 insertions(+)
arch/riscv/Kconfig.socs +9 -0
index cecbce0c4983..0272923c7d88 100644
@@ -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
arch/riscv/kernel/entry.S +6 -0
index 61b7a7716697..f659bf62bb39 100644
@@ -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
arch/riscv/kernel/head.S +13 -0
index 1631fd6bc786..f266c4238b2f 100644
@@ -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:
 
 	/*
Signature · 1 line
2.56.1
#2 lore raw

RFC PATCH 2/19 riscv: Handle ESP32-P4 CLIC trap context

In reply to Vítor Hugo Silva

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.

---
 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(+)
arch/riscv/include/asm/asm-prototypes.h +4 -0
index a0ca9efff267..e0fec71022f9 100644
@@ -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 */
arch/riscv/include/asm/ptrace.h +4 -0
index addc8188152f..5729fbac0b57 100644
@@ -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
arch/riscv/kernel/asm-offsets.c +3 -0
index a75f0cfea1e9..9f8310dd0a6e 100644
@@ -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);
 
arch/riscv/kernel/entry.S +61 -0

79 lines not on this page · raw message

arch/riscv/kernel/traps.c +4 -0
index f8292adda099..28343a601969 100644
@@ -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();
 }
 
Signature · 1 line
2.56.1
#3 lore raw

RFC PATCH 3/19 dt-bindings: interrupt-controller: Add Espressif ESP32-P4 CLIC

In reply to Vítor Hugo Silva

Describe the memory-mapped core-local interrupt controller on the ESP32-P4. Define its two-cell interrupt specifier as the CLIC interrupt number and trigger type.

AI assisted with drafting the schema.

---
 .../esp,esp32p4-clic.yaml                     | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml new file +53 -0
new file mode 100644
index 000000000000..2ef36e4e4019
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/interrupt-controller/esp,esp32p4-clic.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Espressif ESP32-P4 Core-Local Interrupt Controller
+
+maintainers:
+  - Vítor Hugo Silva <[email protected]>
+
+description:
+  Each high-performance RISC-V core has a CLIC that receives the local
+  software and timer interrupts and interrupts from the interrupt matrix.
+
+properties:
+  compatible:
+    enum:
+      - esp,esp32p4-clic
+      - esp,esp32p4-v3-clic
+
+  reg:
+    maxItems: 1
+
+  interrupt-controller: true
+
+  '#interrupt-cells':
+    const: 2
+    description:
+      The first cell is the CLIC interrupt number. Interrupts 3 and 7 are
+      the local machine-mode software and timer interrupts, respectively.
+      Interrupts 16 through 47 are inputs from the interrupt matrix.
+
+      The second cell is the trigger type defined in
+      dt-bindings/interrupt-controller/irq.h. Use IRQ_TYPE_LEVEL_HIGH for
+      the local CLINT interrupts and for signals from the interrupt matrix.
+
+required:
+  - compatible
+  - reg
+  - interrupt-controller
+  - '#interrupt-cells'
+
+additionalProperties: false
+
+examples:
+  - |
+    interrupt-controller@20800000 {
+        compatible = "esp,esp32p4-clic";
+        reg = <0x20800000 0x10000>;
+        interrupt-controller;
+        #interrupt-cells = <2>;
+    };
Signature · 1 line
2.56.1
#4 lore raw

RFC PATCH 4/19 irqchip: Add Espressif ESP32-P4 CLIC support

In reply to Vítor Hugo Silva

Register a hierarchical IRQ domain for the ESP32-P4 CLIC. Handle per-CPU software and timer interrupts and support active-high level triggering on the 32 external interrupt inputs.

Tested on the Waveshare ESP32-P4-Module-DEV-KIT hardware. Verified that the CLIC handled local timer interrupts from the CLINT and external interrupts routed through the interrupt matrix.

AI assisted with driver development, validation and testing.

---
 drivers/irqchip/Makefile           |   1 +
 drivers/irqchip/irq-esp32p4-clic.c | 178 +++++++++++++++++++++++++++++
 2 files changed, 179 insertions(+)
 create mode 100644 drivers/irqchip/irq-esp32p4-clic.c
drivers/irqchip/Makefile +1 -0
index 16a5fd126c9a..408d06c20ab8 100644
@@ -102,6 +102,7 @@ obj-$(CONFIG_QCOM_MPM)			+= irq-qcom-mpm.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_RISCV_ESP32P4_CLIC)	+= irq-esp32p4-clic.o
 obj-$(CONFIG_RISCV_APLIC)		+= irq-riscv-aplic-main.o irq-riscv-aplic-direct.o
 obj-$(CONFIG_RISCV_APLIC_MSI)		+= irq-riscv-aplic-msi.o
 obj-$(CONFIG_RISCV_IMSIC)		+= irq-riscv-imsic-state.o irq-riscv-imsic-early.o irq-riscv-imsic-platform.o
drivers/irqchip/irq-esp32p4-clic.c new file +178 -0

178 lines not on this page · raw message

Signature · 1 line
2.56.1
#5 lore raw

RFC PATCH 5/19 dt-bindings: interrupt-controller: Add Espressif ESP32-P4 interrupt matrix

In reply to Vítor Hugo Silva

Describe the interrupt matrix that routes peripheral interrupt sources to the CLIC. Define the interrupt parent and the two-cell specifier for the peripheral source number and trigger type.

Tested on a Waveshare ESP32-P4-Module-DEV-KIT.

AI assisted with drafting and validating the binding.

---
 .../esp,esp32p4-intmtx.yaml                   | 67 +++++++++++++++++++
 1 file changed, 67 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml new file +67 -0
new file mode 100644
index 000000000000..20f36c4f45da
@@ -0,0 +1,67 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/interrupt-controller/esp,esp32p4-intmtx.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Espressif ESP32-P4 High-Performance Interrupt Matrix
+
+maintainers:
+  - Vítor Hugo Silva <[email protected]>
+
+description:
+  The high-performance interrupt matrix routes peripheral interrupt sources
+  to the external inputs of a high-performance core's CLIC. The matrix can
+  independently select the destination input for each peripheral source.
+  ESP32-P4 revision 3.x adds three interrupt routing registers.
+
+properties:
+  compatible:
+    enum:
+      - esp,esp32p4-intmtx
+      - esp,esp32p4-v3-intmtx
+
+  reg:
+    maxItems: 1
+
+  interrupt-controller: true
+
+  '#interrupt-cells':
+    const: 2
+    description:
+      The first cell is the peripheral interrupt source number in the matrix.
+      The second cell is the trigger type as defined in
+      dt-bindings/interrupt-controller/irq.h.
+
+  '#address-cells':
+    const: 0
+
+  interrupt-parent:
+    description: Phandle to the CLIC that receives interrupts from this matrix.
+
+required:
+  - compatible
+  - reg
+  - interrupt-controller
+  - '#interrupt-cells'
+  - interrupt-parent
+
+additionalProperties: false
+
+examples:
+  - |
+    clic: interrupt-controller@20800000 {
+        compatible = "esp,esp32p4-clic";
+        reg = <0x20800000 0x10000>;
+        interrupt-controller;
+        #interrupt-cells = <2>;
+    };
+
+    interrupt-controller@500d6000 {
+        compatible = "esp,esp32p4-intmtx";
+        reg = <0x500d6000 0x400>;
+        interrupt-controller;
+        #interrupt-cells = <2>;
+        #address-cells = <0>;
+        interrupt-parent = <&clic>;
+    };
Signature · 1 line
2.56.1
#6 lore raw

RFC PATCH 6/19 irqchip: Add Espressif ESP32-P4 interrupt matrix support

In reply to Vítor Hugo Silva

Register a child IRQ domain for the ESP32-P4 interrupt matrix. Allocate a CLIC external interrupt input for each peripheral interrupt and program the route when the interrupt is activated. Support active-high level interrupts for source numbers 0 through 127.

Disconnect firmware routes before registering the IRQ domain. On ESP32-P4 revision 3.x, also clear the three additional routing registers.

Tested on a Waveshare ESP32-P4-Module-DEV-KIT. Verified that peripheral interrupts were delivered to the CLIC through the matrix.

AI assisted with driver development and validation.

---
 drivers/irqchip/Makefile             |   1 +
 drivers/irqchip/irq-esp32p4-intmtx.c | 139 +++++++++++++++++++++++++++
 2 files changed, 140 insertions(+)
 create mode 100644 drivers/irqchip/irq-esp32p4-intmtx.c
drivers/irqchip/Makefile +1 -0
index 408d06c20ab8..772d9558bbc8 100644
@@ -103,6 +103,7 @@ 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_RISCV_ESP32P4_CLIC)	+= irq-esp32p4-clic.o
+obj-$(CONFIG_RISCV_ESP32P4_CLIC)	+= irq-esp32p4-intmtx.o
 obj-$(CONFIG_RISCV_APLIC)		+= irq-riscv-aplic-main.o irq-riscv-aplic-direct.o
 obj-$(CONFIG_RISCV_APLIC_MSI)		+= irq-riscv-aplic-msi.o
 obj-$(CONFIG_RISCV_IMSIC)		+= irq-riscv-imsic-state.o irq-riscv-imsic-early.o irq-riscv-imsic-platform.o
drivers/irqchip/irq-esp32p4-intmtx.c new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
2.56.1
#7 lore raw

RFC PATCH 7/19 dt-bindings: timer: Add Espressif ESP32-P4 CLINT

In reply to Vítor Hugo Silva

Document the ESP32-P4 CLINT compatible and its machine-mode software and timer interrupts routed to the CLIC. Add an example showing both interrupt specifiers.

AI assisted with drafting the binding.

---
 .../bindings/timer/sifive,clint.yaml          | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
Documentation/devicetree/bindings/timer/sifive,clint.yaml +28 -0
index d53f52e80d88..e6d08a4ff702 100644
@@ -27,6 +27,7 @@ description:
 properties:
   compatible:
     oneOf:
+      - const: esp,esp32p4-clint
       - items:
           - enum:
               - canaan,k210-clint       # Canaan Kendryte K210
@@ -74,6 +75,17 @@ properties:
     minItems: 1
     maxItems: 4095
 
+if:
+  properties:
+    compatible:
+      const: esp,esp32p4-clint
+then:
+  properties:
+    interrupts-extended:
+      items:
+        - description: Machine-mode software interrupt
+        - description: Machine-mode timer interrupt
+
 additionalProperties: false
 
 required:
@@ -91,4 +103,20 @@ examples:
                             <&cpu4intc 3>, <&cpu4intc 7>;
       reg = <0x2000000 0x10000>;
     };
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    timer@20000000 {
+        compatible = "esp,esp32p4-clint";
+        reg = <0x20000000 0x10000>;
+        interrupts-extended = <&clic 3 IRQ_TYPE_LEVEL_HIGH>,
+                              <&clic 7 IRQ_TYPE_LEVEL_HIGH>;
+    };
+
+    clic: interrupt-controller@20800000 {
+        compatible = "esp,esp32p4-clic";
+        reg = <0x20800000 0x10000>;
+        interrupt-controller;
+        #interrupt-cells = <2>;
+    };
 ...
Signature · 1 line
2.56.1
#8 lore raw

RFC PATCH 8/19 clocksource/drivers/timer-clint: Add Espressif ESP32-P4 support

In reply to Vítor Hugo Silva

The ESP32-P4 delivers machine-mode timer interrupts through the CLIC. Select the ESP32-P4 handling by compatible string, parse the two-cell interrupt specifier and clear the timer interrupt through mtimecmp.

Program mtimecmp with three 32-bit writes to prevent an intermediate compare value from triggering an early interrupt on RV32.

Enable the ESP32-P4 mtime counter before registering the clocksource so it can be used even if firmware stopped it.

Tested on a Waveshare ESP32-P4-Module-DEV-KIT. Verified CLINT clocksource selection and delivery of timer interrupts.

AI assisted with driver development.

---
 drivers/clocksource/timer-clint.c | 57 ++++++++++++++++++++++++++++---
 1 file changed, 53 insertions(+), 4 deletions(-)
drivers/clocksource/timer-clint.c +53 -4
index e56eee7e3781..bf5bae077d7a 100644
@@ -20,6 +20,7 @@
 #include <linux/irq.h>
 #include <linux/irqchip/chained_irq.h>
 #include <linux/irqdomain.h>
+#include <linux/limits.h>
 #include <linux/of_irq.h>
 #include <linux/smp.h>
 #include <linux/timex.h>
@@ -31,6 +32,8 @@
 #define CLINT_IPI_OFF		0
 #define CLINT_TIMER_CMP_OFF	0x4000
 #define CLINT_TIMER_VAL_OFF	0xbff8
+#define ESP32P4_CLINT_MTIME_CTL_OFF	0x4010
+#define ESP32P4_CLINT_MTIME_EN		BIT(0)
 
 /* CLINT manages IPI and Timer for RISC-V M-mode  */
 static u32 __iomem *clint_ipi_base;
@@ -39,6 +42,7 @@ static u64 __iomem *clint_timer_cmp;
 static u64 __iomem *clint_timer_val;
 static unsigned long clint_timer_freq;
 static unsigned int clint_timer_irq;
+static bool clint_uses_clic __ro_after_init;
 
 #ifdef CONFIG_RISCV_M_MODE
 u64 __iomem *clint_time_val;
@@ -119,6 +123,29 @@ static int clint_clock_next_event(unsigned long delta,
 	return 0;
 }
 
+static void esp32p4_clint_write_cmp(u64 value)
+{
+	u32 __iomem *cmp = (u32 __iomem *)clint_timer_cmp;
+
+	/* Keep the intermediate RV32 compare values from expiring early. */
+	writel_relaxed(U32_MAX, cmp);
+	writel_relaxed((u32)(value >> 32), cmp + 1);
+	writel_relaxed((u32)value, cmp);
+}
+
+static int esp32p4_clint_next_event(unsigned long delta,
+				    struct clock_event_device *ce)
+{
+	esp32p4_clint_write_cmp(clint_get_cycles64() + delta);
+	return 0;
+}
+
+static int esp32p4_clint_shutdown(struct clock_event_device *ce)
+{
+	esp32p4_clint_write_cmp(U64_MAX);
+	return 0;
+}
+
 static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) = {
 	.name		= "clint_clockevent",
 	.features	= CLOCK_EVT_FEAT_ONESHOT,
@@ -130,13 +157,22 @@ static int clint_timer_starting_cpu(unsigned int cpu)
 {
 	struct clock_event_device *ce = per_cpu_ptr(&clint_clock_event, cpu);
 
+	if (clint_uses_clic) {
+		ce->set_next_event = esp32p4_clint_next_event;
+		ce->set_state_shutdown = esp32p4_clint_shutdown;
+		ce->set_state_oneshot_stopped = esp32p4_clint_shutdown;
+		esp32p4_clint_shutdown(ce);
+	}
+
 	ce->cpumask = cpumask_of(cpu);
 	clockevents_config_and_register(ce, clint_timer_freq, 100, ULONG_MAX);
 
 	enable_percpu_irq(clint_timer_irq,
 			  irq_get_trigger_type(clint_timer_irq));
-	enable_percpu_irq(clint_ipi_irq,
-			  irq_get_trigger_type(clint_ipi_irq));
+	if (!clint_uses_clic)
+		enable_percpu_irq(clint_ipi_irq,
+				  irq_get_trigger_type(clint_ipi_irq));
+
 	return 0;
 }
 
@@ -155,7 +191,10 @@ static irqreturn_t clint_timer_interrupt(int irq, void *dev_id)
 {
 	struct clock_event_device *evdev = this_cpu_ptr(&clint_clock_event);
 
-	csr_clear(CSR_IE, IE_TIE);
+	if (clint_uses_clic)
+		esp32p4_clint_shutdown(evdev);
+	else
+		csr_clear(CSR_IE, IE_TIE);
 	evdev->event_handler(evdev);
 
 	return IRQ_HANDLED;
@@ -163,6 +202,7 @@ static irqreturn_t clint_timer_interrupt(int irq, void *dev_id)
 
 static int __init clint_timer_init_dt(struct device_node *np)
 {
+	bool esp32p4 = of_device_is_compatible(np, "esp,esp32p4-clint");
 	int rc;
 	u32 i, nr_irqs;
 	void __iomem *base;
@@ -179,7 +219,7 @@ static int __init clint_timer_init_dt(struct device_node *np)
 			continue;
 		}
 
-		if ((oirq.args_count != 1) ||
+		if (oirq.args_count != (esp32p4 ? 2 : 1) ||
 		    (oirq.args[0] != RV_IRQ_TIMER &&
 		     oirq.args[0] != RV_IRQ_SOFT)) {
 			pr_err("%pOFP: invalid irq %d (hwirq %d)\n",
@@ -212,10 +252,18 @@ static int __init clint_timer_init_dt(struct device_node *np)
 		return -ENODEV;
 	}
 
+	if (esp32p4) {
+		void __iomem *ctl = base + ESP32P4_CLINT_MTIME_CTL_OFF;
+		u32 val = readl(ctl);
+
+		writel(val | ESP32P4_CLINT_MTIME_EN, ctl);
+	}
+
 	clint_ipi_base = base + CLINT_IPI_OFF;
 	clint_timer_cmp = base + CLINT_TIMER_CMP_OFF;
 	clint_timer_val = base + CLINT_TIMER_VAL_OFF;
 	clint_timer_freq = riscv_timebase;
+	clint_uses_clic = esp32p4;
 
 #ifdef CONFIG_RISCV_M_MODE
 	/*
@@ -275,3 +323,4 @@ fail_iounmap:
 
 TIMER_OF_DECLARE(clint_timer, "riscv,clint0", clint_timer_init_dt);
 TIMER_OF_DECLARE(clint_timer1, "sifive,clint0", clint_timer_init_dt);
+TIMER_OF_DECLARE(esp32p4_clint, "esp,esp32p4-clint", clint_timer_init_dt);
Signature · 1 line
2.56.1
#9 lore raw

RFC PATCH 9/19 dt-bindings: serial: Add Espressif ESP32-P4 UART

In reply to Vítor Hugo Silva

Document the ESP32-P4 UART compatible using the existing binding properties for the register range, interrupt and input clock.

AI assisted with drafting the binding.

---
 Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/serial/esp,esp32-uart.yaml +3 -1
index 2a80ca997a0c..bf141db5e2e5 100644
@@ -13,7 +13,8 @@ maintainers:
 description:
   ESP32 UART controller is a part of the ESP32 SoC.
   ESP32S3 UART controller is a part of the ESP32S3 SoC.
-  Both SoCs are produced by Espressif Systems Co. Ltd.
+  ESP32-P4 UART controller is a part of the ESP32-P4 SoC.
+  These SoCs are produced by Espressif Systems Co. Ltd.
 
 allOf:
   - $ref: serial.yaml#
@@ -22,6 +23,7 @@ properties:
   compatible:
     enum:
       - esp,esp32-uart
+      - esp,esp32p4-uart
       - esp,esp32s3-uart
 
   reg:
Signature · 1 line
2.56.1
#10 lore raw

RFC PATCH 10/19 serial: esp32: Restore the ESP32 UART driver

In reply to Vítor Hugo Silva

Restore the UART driver originally added by Max Filippov in commit 8cc89a229aac ("drivers/tty/serial: add driver for the ESP32 UART"). Use the driver source from the parent of commit 2dccde6f5f2f ("serial: remove drivers for espressif esp32").

Keep esp32_uart_console_setup() in regular text because a late probe can register the console after init memory has been freed.

Make the restored driver available under COMPILE_TEST in preparation for ESP32-P4 support.

AI assisted with preparing and validating the restoration.

---
 drivers/tty/serial/Kconfig      |  13 +
 drivers/tty/serial/Makefile     |   1 +
 drivers/tty/serial/esp32_uart.c | 779 ++++++++++++++++++++++++++++++++
 3 files changed, 793 insertions(+)
 create mode 100644 drivers/tty/serial/esp32_uart.c
drivers/tty/serial/Kconfig +13 -0
index 3864cef711a6..83a37922fb07 100644
@@ -97,6 +97,19 @@ config SERIAL_EARLYCON_RISCV_SBI
 	  with "earlycon=sbi" on the kernel command line. The console is
 	  enabled when early_param is processed.
 
+config SERIAL_ESP32
+	tristate "Espressif ESP32 UART support"
+	depends on COMPILE_TEST && OF && HAS_IOMEM
+	select SERIAL_CORE
+	select SERIAL_CORE_CONSOLE
+	select SERIAL_EARLYCON
+	help
+	  Driver for the UART controllers of the Espressif ESP32xx SoCs.
+	  When earlycon option is enabled the following kernel command line
+	  snippets may be used:
+	    earlycon=esp32s3uart,mmio32,0x60000000,115200n8,40000000
+	    earlycon=esp32uart,mmio32,0x3ff40000,115200n8
+
 config SERIAL_SB1250_DUART
 	tristate "BCM1xxx on-chip DUART serial support"
 	depends on SIBYTE_SB1xxx_SOC=y
drivers/tty/serial/Makefile +1 -0
index d63b126f40ca..11cb7e3a0f37 100644
@@ -9,6 +9,7 @@ serial_base-y := serial_core.o serial_base_bus.o serial_ctrl.o serial_port.o
 obj-$(CONFIG_SERIAL_EARLYCON) += earlycon.o
 obj-$(CONFIG_SERIAL_EARLYCON_SEMIHOST) += earlycon-semihost.o
 obj-$(CONFIG_SERIAL_EARLYCON_RISCV_SBI) += earlycon-riscv-sbi.o
+obj-$(CONFIG_SERIAL_ESP32) += esp32_uart.o
 
 # These Sparc drivers have to appear before others such as 8250
 # which share ttySx minor node space.  Otherwise console device
drivers/tty/serial/esp32_uart.c new file +779 -0

779 lines not on this page · raw message

Signature · 1 line
2.56.1
#11 lore raw

RFC PATCH 11/19 serial: esp32: Add support for the ESP32-P4 UART

In reply to Vítor Hugo Silva

Enable the ESP32 UART driver on ESP32-P4 and describe the SoC's register layout in the variant data. Synchronize configuration writes to the UART clock domain and keep baud rate requests within the limits of the ESP32-P4 divider.

Propagate IRQ lookup errors, including deferred probe. Serialize UART port registration and removal, and clear the port table entry when registration fails or the port is removed. Keep the console setup callback available for probes after init memory is freed.

Tested on a Waveshare ESP32-P4-Module-DEV-KIT with the transmit interrupt fix applied. Verified UART receive interrupts and serial console operation.

AI assisted with driver development.

---
 drivers/tty/serial/Kconfig      |   6 +-
 drivers/tty/serial/esp32_uart.c | 167 ++++++++++++++++++++++++++++----
 2 files changed, 151 insertions(+), 22 deletions(-)
drivers/tty/serial/Kconfig +4 -2
index 83a37922fb07..6d3d3a342101 100644
@@ -99,12 +99,14 @@ config SERIAL_EARLYCON_RISCV_SBI
 
 config SERIAL_ESP32
 	tristate "Espressif ESP32 UART support"
-	depends on COMPILE_TEST && OF && HAS_IOMEM
+	depends on OF && HAS_IOMEM
+	depends on RISCV_ESP32P4_CLIC || COMPILE_TEST
 	select SERIAL_CORE
 	select SERIAL_CORE_CONSOLE
 	select SERIAL_EARLYCON
 	help
-	  Driver for the UART controllers of the Espressif ESP32xx SoCs.
+	  Driver for the UART controllers of the Espressif ESP32xx SoCs,
+	  including the ESP32-P4.
 	  When earlycon option is enabled the following kernel command line
 	  snippets may be used:
 	    earlycon=esp32s3uart,mmio32,0x60000000,115200n8,40000000
drivers/tty/serial/esp32_uart.c +147 -20

293 lines not on this page · raw message

Signature · 1 line
2.56.1
#12 lore raw

RFC PATCH 12/19 serial: esp32: Rearm TX interrupts when the FIFO is full

In reply to Vítor Hugo Silva

When transmission starts with a full FIFO, the driver can leave queued data waiting with the TX interrupt disabled. Rearm the interrupt whenever there is data ready to send, including a pending high-priority character.

Hold the port lock in the TX interrupt handler as required by uart_port_tx_limited().

Tested on a Waveshare ESP32-P4-Module-DEV-KIT. Verified that serial output completed during SDMMC reads.

AI assisted with driver development.

---
 drivers/tty/serial/esp32_uart.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/tty/serial/esp32_uart.c +8 -4
index 304e1345518b..002d6fdbd875 100644
@@ -354,14 +354,14 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
 	unsigned int pending;
 	u8 ch;
 
-	if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
-		return;
-
+	tx_fifo_used = min_t(u32, tx_fifo_used, ESP32_UART_TX_FIFO_SIZE);
 	pending = uart_port_tx_limited(port, ch,
 				       ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
 				       true, esp32_uart_put_char(port, ch),
 				       ({}));
-	if (pending) {
+
+	/* Rearm TX even if the FIFO had no room for queued data. */
+	if (port->x_char || (pending && !uart_tx_stopped(port))) {
 		u32 int_ena;
 
 		int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
@@ -372,7 +372,11 @@ static void esp32_uart_transmit_buffer(struct uart_port *port)
 
 static void esp32_uart_txint(struct uart_port *port)
 {
+	unsigned long flags;
+
+	uart_port_lock_irqsave(port, &flags);
 	esp32_uart_transmit_buffer(port);
+	uart_port_unlock_irqrestore(port, flags);
 }
 
 static irqreturn_t esp32_uart_int(int irq, void *dev_id)
Signature · 1 line
2.56.1
#13 lore raw

RFC PATCH 13/19 serial: esp32: Add early console support for ESP32-P4

In reply to Vítor Hugo Silva

Register an early console for the ESP32-P4 UART using the UART configuration left by firmware.

Early console writes can run before timer initialization, and normal console writes can run with local interrupts disabled. Use a shared polling routine that bounds the wait for TX FIFO space by a fixed number of reads.

Allow 10,000 reads per character for the ESP32-P4 early console and 1,000,000 for the other callers. Drop the character if the FIFO remains full for the entire polling budget.

Verified early boot output and a working UART shell on the Waveshare ESP32-P4-Module-DEV-KIT with the driver built into an RV32 NOMMU kernel. With a simulated FIFO, both limits dropped the character when the FIFO stayed full and wrote it when space became available on the last read.

AI assisted with integrating the early console and checking its registration in the linked kernel.

---
 drivers/tty/serial/esp32_uart.c | 50 ++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 10 deletions(-)
drivers/tty/serial/esp32_uart.c +40 -10
index 002d6fdbd875..451caeb406fe 100644
@@ -25,6 +25,7 @@
 
 #define ESP32_UART_TX_FIFO_SIZE	127
 #define ESP32_UART_RX_FIFO_SIZE	127
+#define ESP32_UART_TX_POLLS		1000000
 
 #define UART_FIFO_REG			0x00
 #define UART_INT_RAW_REG		0x04
@@ -107,6 +108,7 @@
 #define ESP32P4_UART_SW_DTR			BIT(20)
 #define ESP32P4_UART_RTS_INV			BIT(18)
 #define ESP32P4_UART_DTR_INV			BIT(19)
+#define ESP32P4_EARLYCON_TX_POLLS	10000
 
 struct esp32_port {
 	struct uart_port port;
@@ -334,18 +336,18 @@ static void esp32_uart_put_char(struct uart_port *port, u8 c)
 	esp32_uart_write(port, UART_FIFO_REG, c);
 }
 
-static void esp32_uart_put_char_sync(struct uart_port *port, u8 c)
+static void esp32_uart_put_char_sync(struct uart_port *port, u8 c,
+				     unsigned int polls)
 {
-	unsigned long timeout = jiffies + HZ;
-
-	while (esp32_uart_tx_fifo_cnt(port) >= ESP32_UART_TX_FIFO_SIZE) {
-		if (time_after(jiffies, timeout)) {
-			dev_warn(port->dev, "timeout waiting for TX FIFO\n");
+	/* Console writes can run before timer init or with interrupts disabled. */
+	while (polls--) {
+		if (esp32_uart_tx_fifo_cnt(port) < ESP32_UART_TX_FIFO_SIZE) {
+			esp32_uart_put_char(port, c);
 			return;
 		}
+
 		cpu_relax();
 	}
-	esp32_uart_put_char(port, c);
 }
 
 static void esp32_uart_transmit_buffer(struct uart_port *port)
@@ -601,7 +603,7 @@ static int esp32_uart_poll_init(struct uart_port *port)
 
 static void esp32_uart_poll_put_char(struct uart_port *port, unsigned char c)
 {
-	esp32_uart_put_char_sync(port, c);
+	esp32_uart_put_char_sync(port, c, ESP32_UART_TX_POLLS);
 }
 
 static int esp32_uart_poll_get_char(struct uart_port *port)
@@ -635,7 +637,7 @@ static const struct uart_ops esp32_uart_pops = {
 
 static void esp32_uart_console_putchar(struct uart_port *port, u8 c)
 {
-	esp32_uart_put_char_sync(port, c);
+	esp32_uart_put_char_sync(port, c, ESP32_UART_TX_POLLS);
 }
 
 static void esp32_uart_string_write(struct uart_port *port, const char *s,
@@ -718,7 +720,7 @@ static struct console esp32_uart_console = {
 
 static void esp32_uart_earlycon_putchar(struct uart_port *port, u8 c)
 {
-	esp32_uart_put_char_sync(port, c);
+	esp32_uart_put_char_sync(port, c, ESP32_UART_TX_POLLS);
 }
 
 static void esp32_uart_earlycon_write(struct console *con, const char *s,
@@ -784,6 +786,34 @@ static int __init esp32s3_uart_early_console_setup(struct earlycon_device *devic
 OF_EARLYCON_DECLARE(esp32s3uart, "esp,esp32s3-uart",
 		    esp32s3_uart_early_console_setup);
 
+static void esp32p4_uart_earlycon_putchar(struct uart_port *port, u8 c)
+{
+	esp32_uart_put_char_sync(port, c, ESP32P4_EARLYCON_TX_POLLS);
+}
+
+static void esp32p4_uart_earlycon_write(struct console *con, const char *s,
+					unsigned int n)
+{
+	struct earlycon_device *dev = con->data;
+
+	uart_console_write(&dev->port, s, n, esp32p4_uart_earlycon_putchar);
+}
+
+static int __init esp32p4_uart_early_console_setup(struct earlycon_device *device,
+						   const char *options)
+{
+	if (!device->port.membase)
+		return -ENODEV;
+
+	/* Use the UART configuration left by firmware. */
+	device->port.private_data = (void *)&esp32p4_variant;
+	device->con->write = esp32p4_uart_earlycon_write;
+
+	return 0;
+}
+OF_EARLYCON_DECLARE(esp32p4, "esp,esp32p4-uart",
+		    esp32p4_uart_early_console_setup);
+
 static struct uart_driver esp32_uart_reg = {
 	.owner		= THIS_MODULE,
 	.driver_name	= DRIVER_NAME,
Signature · 1 line
2.56.1
#14 lore raw

RFC PATCH 14/19 riscv: Synchronize instructions written to ESP32-P4 PSRAM

In reply to Vítor Hugo Silva

On ESP32-P4, a load can observe new instruction bytes in the data cache while the instruction fetch path still sees old bytes. The runtime constant patching code can then execute stale instructions and use an invalid address during boot.

Write back the L1 data and L2 caches for the modified PSRAM range, invalidate the local L1 instruction cache for that range and issue fence.i before executing the modified instructions. Apply the same sequence to NOMMU userspace code, including FLAT binaries and the signal return trampoline.

Split the aligned range into 32 KiB chunks and restore the caller's IRQ state after each chunk. In instrumented measurements on the Waveshare board, the longest observed IRQ-disabled window fell from 2.69 ms with 128 KiB chunks to 1.13 ms with 32 KiB chunks.

Bound cache synchronization by a fixed number of reads per command. After a timeout, stop issuing commands because the controller may still be busy. Propagate failures through executable loading and signal setup. Treat a failure to synchronize patched kernel text as fatal.

The ESP32-P4 board probe observed stale instructions after fence.i alone. Combining data writeback and instruction cache invalidation made the modified instructions visible.

Verified boot to the UART shell on a Waveshare ESP32-P4-Module-DEV-KIT with SDMMC disabled in Linux. The shell handled SIGUSR1 and resumed execution. With CLIC disabled, the RV32 NOMMU patch.o and signal.o matched the reference byte for byte.

AI assisted with isolating the cache synchronization change from the rest of the ESP32-P4 port.

---
 arch/riscv/include/asm/cacheflush.h |   6 ++
 arch/riscv/kernel/patch.c           | 132 +++++++++++++++++++++++++++-
 arch/riscv/kernel/signal.c          |   6 +-
 fs/binfmt_flat.c                    |   6 +-
 fs/exec.c                           |  10 ++-
 include/linux/cacheflush.h          |   7 ++
 6 files changed, 160 insertions(+), 7 deletions(-)
arch/riscv/include/asm/cacheflush.h +6 -0
index c2b0a2928f06..a5c20d7d1c9d 100644
@@ -85,6 +85,12 @@ static inline void flush_icache_range(unsigned long start, unsigned long end)
 	flush_icache_all();
 }
 
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+int esp32p4_sync_user_text(unsigned long start, unsigned long end);
+
+#define flush_icache_user_range_checked esp32p4_sync_user_text
+#endif
+
 extern unsigned int riscv_cbom_block_size;
 extern unsigned int riscv_cboz_block_size;
 extern unsigned int riscv_cbop_block_size;
arch/riscv/kernel/patch.c +131 -1

146 lines not on this page · raw message

arch/riscv/kernel/signal.c +4 -2
index 59784dc117e4..05266d7c1466 100644
@@ -7,6 +7,7 @@
  */
 
 #include <linux/compat.h>
+#include <linux/cacheflush.h>
 #include <linux/signal.h>
 #include <linux/uaccess.h>
 #include <linux/syscalls.h>
@@ -21,7 +22,6 @@
 #include <asm/switch_to.h>
 #include <asm/vector.h>
 #include <asm/csr.h>
-#include <asm/cacheflush.h>
 #include <asm/usercfi.h>
 
 unsigned long signal_minsigstksz __ro_after_init;
@@ -453,7 +453,9 @@ static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
 
 	addr = (unsigned long)&frame->sigreturn_code;
 	/* Make sure the two instructions are pushed to icache. */
-	flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
+	err = flush_icache_user_range_checked(addr, addr + sizeof(frame->sigreturn_code));
+	if (err)
+		return err;
 
 	regs->ra = addr;
 #endif /* CONFIG_MMU */
fs/binfmt_flat.c +4 -2
index b5b5ca1a44f7..890a81d805dc 100644
@@ -32,6 +32,7 @@
 #include <linux/user.h>
 #include <linux/slab.h>
 #include <linux/binfmts.h>
+#include <linux/cacheflush.h>
 #include <linux/personality.h>
 #include <linux/init.h>
 #include <linux/flat.h>
@@ -40,7 +41,6 @@
 
 #include <asm/byteorder.h>
 #include <linux/unaligned.h>
-#include <asm/cacheflush.h>
 #include <asm/page.h>
 #include <asm/flat.h>
 
@@ -830,7 +830,9 @@ static int load_flat_file(struct linux_binprm *bprm,
 #endif /* CONFIG_BINFMT_FLAT_OLD */
 	}
 
-	flush_icache_user_range(start_code, end_code);
+	ret = flush_icache_user_range_checked(start_code, end_code);
+	if (ret)
+		return ret;
 
 	/* zero the BSS,  BRK and stack areas */
 	if (clear_user((void __user *)(datapos + data_len), bss_len +
fs/exec.c +8 -2
index 8d2b14e1d455..9066d2bebd1a 100644
@@ -23,6 +23,7 @@
  * formats.
  */
 
+#include <linux/cacheflush.h>
 #include <linux/kernel_read_file.h>
 #include <linux/slab.h>
 #include <linux/file.h>
@@ -827,8 +828,13 @@ EXPORT_SYMBOL(open_exec);
 ssize_t read_code(struct file *file, unsigned long addr, loff_t pos, size_t len)
 {
 	ssize_t res = vfs_read(file, (void __user *)addr, len, &pos);
-	if (res > 0)
-		flush_icache_user_range(addr, addr + len);
+
+	if (res > 0) {
+		int ret = flush_icache_user_range_checked(addr, addr + len);
+
+		if (ret)
+			return ret;
+	}
 	return res;
 }
 EXPORT_SYMBOL(read_code);
include/linux/cacheflush.h +7 -0
index 55f297b2c23f..6a7d6f425737 100644
@@ -26,4 +26,11 @@ static inline void flush_icache_pages(struct vm_area_struct *vma,
 
 #define flush_icache_page(vma, page)	flush_icache_pages(vma, page, 1)
 
+#ifndef flush_icache_user_range_checked
+#define flush_icache_user_range_checked(start, end) ({	\
+	flush_icache_user_range((start), (end));		\
+	0;						\
+})
+#endif
+
 #endif /* _LINUX_CACHEFLUSH_H */
Signature · 1 line
2.56.1
#15 lore raw

RFC PATCH 15/19 riscv: Align init data to the ESP32-P4 PMP granularity

In reply to Vítor Hugo Silva

ESP32-P4 has a PMP granularity of 128 bytes. The RISC-V linker script can place __init_data_begin at a smaller alignment, so a boot stage using that symbol to separate executable code from writable data cannot always encode the boundary exactly.

Align __init_data_begin to 128 bytes on ESP32-P4 so PMP can apply distinct permissions to the two regions.

Verified the alignment in an RV32 NOMMU kernel that booted to the UART shell on a Waveshare ESP32-P4-Module-DEV-KIT.

AI assisted with preparing the patch and validating the linker layout.

---
 arch/riscv/kernel/vmlinux.lds.S | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/kernel/vmlinux.lds.S +4 -0
index 1f4f8496941a..e4498a9fdc6c 100644
@@ -77,6 +77,10 @@ SECTIONS
 #ifdef CONFIG_EFI
 	. = ALIGN(PECOFF_SECTION_ALIGNMENT);
 	__pecoff_text_end = .;
+#endif
+#ifdef CONFIG_RISCV_ESP32P4_CLIC
+	/* Keep init data outside the last executable PMP granule. */
+	. = ALIGN(SZ_128);
 #endif
 	/* Start of init data section */
 	__init_data_begin = .;
Signature · 1 line
2.56.1
#16 lore raw

RFC PATCH 16/19 dt-bindings: riscv: Add Waveshare ESP32-P4-Module-DEV-KIT

In reply to Vítor Hugo Silva

Document the root compatible for the Waveshare ESP32-P4-Module-DEV-KIT with the Espressif ESP32-P4 SoC compatible as its fallback.

AI assisted with writing and validating the schema.

---
 .../devicetree/bindings/riscv/espressif.yaml  | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/riscv/espressif.yaml
Documentation/devicetree/bindings/riscv/espressif.yaml new file +22 -0
new file mode 100644
index 000000000000..564e93cd5f30
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/riscv/espressif.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Espressif ESP32-P4-based boards
+
+maintainers:
+  - Vítor Hugo Silva <[email protected]>
+
+properties:
+  $nodename:
+    const: '/'
+  compatible:
+    items:
+      - const: waveshare,esp32-p4-module-dev-kit
+      - const: esp,esp32p4
+
+additionalProperties: true
+
+...
Signature · 1 line
2.56.1
#17 lore raw

RFC PATCH 17/19 dt-bindings: riscv: cpus: Document Espressif ESP32-P4 HP harts

In reply to Vítor Hugo Silva

Add esp,esp32p4-hp as a compatible for the ESP32-P4 high-performance harts, with riscv as the fallback.

These harts receive interrupts through the memory-mapped CLIC, which has its own binding and node. Disallow the CSR-based interrupt controller child for ESP32-P4 HP harts and keep requiring it for other RISC-V CPUs.

AI assisted with writing and validating the schema change.

---
 .../devicetree/bindings/riscv/cpus.yaml         | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
Documentation/devicetree/bindings/riscv/cpus.yaml +14 -3
index 0da219ae6769..17fef52aede1 100644
@@ -38,6 +38,19 @@ allOf:
     then:
       properties:
         thead,vlenb: false
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: esp,esp32p4-hp
+      required:
+        - compatible
+    then:
+      properties:
+        interrupt-controller: false
+    else:
+      required:
+        - interrupt-controller
 
 properties:
   compatible:
@@ -48,6 +61,7 @@ properties:
               - amd,mbv64
               - andestech,ax45mp
               - canaan,k210
+              - esp,esp32p4-hp
               - nuclei,ux900
               - sifive,bullet0
               - sifive,e5
@@ -154,9 +168,6 @@ dependencies:
   riscv,isa-base: ["riscv,isa-extensions"]
   riscv,isa-extensions: ["riscv,isa-base"]
 
-required:
-  - interrupt-controller
-
 unevaluatedProperties: false
 
 examples:
Signature · 1 line
2.56.1
#18 lore raw

RFC PATCH 18/19 riscv: dts: Add Waveshare ESP32-P4-Module-DEV-KIT

In reply to Vítor Hugo Silva

Add the initial ESP32-P4 SoC description for the CLIC, CLINT, interrupt matrix and UART0. Describe the board's 32 MiB of PSRAM and the 40 MHz clock used by UART0.

Build the board DTB when ESP32-P4 CLIC support is enabled. The initial device tree supports booting to a UART shell on the Waveshare board. Use UART0 as the default console at 115200 baud.

Describe both HP harts in the ESP32-P4 SoC device tree. The initial Linux support runs on the boot hart only.

Select the revision 3.x interrupt matrix on the Waveshare board.

The CLINT timebase follows the CPU's clock. The firmware must populate /cpus/timebase-frequency with the actual rate before booting Linux.

AI assisted with formatting the DTS and validating the resulting DTB.

---
 arch/riscv/boot/dts/Makefile                  |  1 +
 arch/riscv/boot/dts/espressif/Makefile        |  2 +
 .../esp32p4-waveshare-module-dev-kit.dts      | 46 +++++++++++++
 arch/riscv/boot/dts/espressif/esp32p4.dtsi    | 64 +++++++++++++++++++
 4 files changed, 113 insertions(+)
 create mode 100644 arch/riscv/boot/dts/espressif/Makefile
 create mode 100644 arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts
 create mode 100644 arch/riscv/boot/dts/espressif/esp32p4.dtsi
arch/riscv/boot/dts/Makefile +1 -0
index 69d8751fb17c..fa0f23de2616 100644
@@ -3,6 +3,7 @@ subdir-y += allwinner
 subdir-y += andes
 subdir-y += anlogic
 subdir-y += canaan
+subdir-y += espressif
 subdir-y += eswin
 subdir-y += microchip
 subdir-y += renesas
arch/riscv/boot/dts/espressif/Makefile new file +2 -0
new file mode 100644
index 000000000000..fd89616862fb
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+dtb-$(CONFIG_RISCV_ESP32P4_CLIC) += esp32p4-waveshare-module-dev-kit.dtb
arch/riscv/boot/dts/espressif/esp32p4-waveshare-module-dev-kit.dts new file +46 -0

46 lines not on this page · raw message

arch/riscv/boot/dts/espressif/esp32p4.dtsi new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.56.1
#19 lore raw

RFC PATCH 19/19 MAINTAINERS: Add the Espressif ESP32-P4 platform

In reply to Vítor Hugo Silva

Add an entry for the ESP32-P4 Device Tree files and interrupt controller drivers. Match ESP32-P4-specific changes in shared files by keyword.

AI assisted with validating patch routing.

---
 MAINTAINERS | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
MAINTAINERS +14 -0
index ccbcdc0799b9..538f1a353526 100644
@@ -24162,6 +24162,20 @@ F:	arch/riscv/
 N:	riscv
 K:	riscv
 
+RISC-V ESPRESSIF ESP32-P4 SUPPORT
+M:	Vítor Hugo Silva <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-clic.yaml
+F:	Documentation/devicetree/bindings/interrupt-controller/esp,esp32p4-intmtx.yaml
+F:	Documentation/devicetree/bindings/riscv/espressif.yaml
+F:	arch/riscv/boot/dts/espressif/Makefile
+F:	arch/riscv/boot/dts/espressif/esp32p4*
+F:	drivers/irqchip/irq-esp32p4-clic.c
+F:	drivers/irqchip/irq-esp32p4-intmtx.c
+K:	\b(CONFIG_)?RISCV_ESP32P4_CLIC\b
+K:	\b(esp32p4|ESP32P4)(\b|_)
+
 RISC-V IOMMU
 M:	Tomasz Jeznach <[email protected]>
 L:	[email protected]
Signature · 1 line
2.56.1