U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs
@ 2026-11-24 19:52 Indrek Kruusa
  2026-11-24 19:52 ` [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs Indrek Kruusa
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Indrek Kruusa @ 2026-11-24 19:52 UTC (permalink / raw)
  To: Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot, Indrek Kruusa

This series consolidates the vendor-specific cache operations of the
T-Head C9xx cores and the machine-mode CSRs those ports use, and
implements the whole-cache operations.  It is a prerequisite for the
upcoming Allwinner D1/D1s support.  However, no definitions have been
added yet that only D1 would use.

The series adds arch/riscv/include/asm/thead_csr.h, with the CSR numbers
and bitfields the T-Head ports use today.  It also adds
arch/riscv/lib/thead_cache.c: the renamed thead_cmo.c, with the enable and
disable functions taken from arch/riscv/cpu/th1520/cache.c, which is
removed.

The whole-cache operations have an earlier version in Samuel Holland's
d1-wip branch [1], reworked here for the in-tree ports.

[1] https://github.com/smaeul/u-boot/tree/d1-wip

SoCs select THEAD_CACHE.  For the L2 operations (C910) there is an
additional THEAD_L2_CACHE selection: the whole-cache operations cover only
the L1 of the calling core, while the address-range ones already reach the
L2 and every core of the cluster, so only the former need an operation on
top.

The code is built for both U-Boot proper and SPL.  Where an operation makes
no sense it is compiled out: the D-cache ones with SYS_DCACHE_OFF, the MHCR
ones when U-Boot runs in S-mode.  CV1800B is covered too.

Build tested for th1520_lpi4a, including the SPL link, and for
milkv_duo.


Indrek Kruusa (4):
  riscv: thead: add a common header for the C9xx CSRs
  riscv: th1520: use the common C9xx CSR definitions
  riscv: lib: rename thead_cmo.c to thead_cache.c
  riscv: lib: add the whole-cache and enable operations

 arch/riscv/Kconfig                 |  17 ++--
 arch/riscv/cpu/cv1800b/Kconfig     |   2 +-
 arch/riscv/cpu/th1520/Kconfig      |   3 +-
 arch/riscv/cpu/th1520/Makefile     |   1 -
 arch/riscv/cpu/th1520/cache.c      |  34 --------
 arch/riscv/cpu/th1520/spl.c        |  96 ++++++---------------
 arch/riscv/include/asm/thead_csr.h | 105 +++++++++++++++++++++++
 arch/riscv/lib/Makefile            |   2 +-
 arch/riscv/lib/thead_cache.c       | 129 +++++++++++++++++++++++++++++
 arch/riscv/lib/thead_cmo.c         |  45 ----------
 10 files changed, 275 insertions(+), 159 deletions(-)
 delete mode 100644 arch/riscv/cpu/th1520/cache.c
 create mode 100644 arch/riscv/include/asm/thead_csr.h
 create mode 100644 arch/riscv/lib/thead_cache.c
 delete mode 100644 arch/riscv/lib/thead_cmo.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs
  2026-11-24 19:52 [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Indrek Kruusa
@ 2026-11-24 19:52 ` Indrek Kruusa
  2026-11-25 15:00   ` Junhui Liu
  2026-11-24 19:52 ` [PATCH 2/4] riscv: th1520: use the common C9xx CSR definitions Indrek Kruusa
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: Indrek Kruusa @ 2026-11-24 19:52 UTC (permalink / raw)
  To: Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot, Indrek Kruusa

Add a header with the register numbers and bitfields currently used by the
C9xx ports. It is placed in asm/ so that both SPL and U-Boot proper can
include it. M-mode only.

Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
---
 arch/riscv/include/asm/thead_csr.h | 105 +++++++++++++++++++++++++++++
 1 file changed, 105 insertions(+)
 create mode 100644 arch/riscv/include/asm/thead_csr.h

diff --git a/arch/riscv/include/asm/thead_csr.h b/arch/riscv/include/asm/thead_csr.h
new file mode 100644
index 00000000000..32d3bf773ee
--- /dev/null
+++ b/arch/riscv/include/asm/thead_csr.h
@@ -0,0 +1,105 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (C) 2025 Yao Zi <ziyao@disroot.org>
+ * Copyright (C) 2026, Indrek Kruusa <indrek.kruusa@gmail.com>
+ *
+ * T-Head C9xx machine-mode control registers.
+ *
+ * Register numbers are shared, but the fields defined here are only those used
+ * by the in-tree ports. Availability and layout may differ between cores. Any
+ * known differences that could cause incorrect configuration are noted at the
+ * corresponding definitions.
+ */
+
+#ifndef _ASM_RISCV_THEAD_CSR_H
+#define _ASM_RISCV_THEAD_CSR_H
+
+#include <linux/bitops.h>
+
+/* M-mode control registers */
+#define CSR_MXSTATUS		0x7c0
+#define CSR_MHCR		0x7c1
+#define CSR_MCOR		0x7c2
+#define CSR_MHINT		0x7c5
+
+/* not decoded by every core */
+#define CSR_MCCR2		0x7c3
+#define CSR_MHINT2		0x7cc
+#define CSR_MHINT4		0x7ce
+#define CSR_MSMPR		0x7f3
+
+/* MXSTATUS - machine-mode status */
+#define MXSTATUS_THEADISAEE	BIT(22)	/* T-HEAD ISA extensions enable */
+#define MXSTATUS_MAEE		BIT(21)	/* MMU honours the PTE memory type */
+#define MXSTATUS_CLINTEE	BIT(17)	/* CLINT extension enable */
+#define MXSTATUS_UCME		BIT(16)	/* user-mode cache ops enable */
+#define MXSTATUS_MM		BIT(15)	/* misaligned access enable */
+
+/*
+ * MHCR - machine-mode hardware configuration
+ *
+ * WB and WBR are read-only and always read back as 1.
+ */
+#define MHCR_IE			BIT(0)	/* I-cache enable */
+#define MHCR_DE			BIT(1)	/* D-cache enable */
+#define MHCR_WA			BIT(2)	/* D-cache write allocate */
+#define MHCR_WB			BIT(3)	/* D-cache write back */
+#define MHCR_RS			BIT(4)	/* return stack enable */
+#define MHCR_BPE		BIT(5)	/* branch prediction enable */
+#define MHCR_BTB		BIT(6)	/* branch target buffer enable */
+#define MHCR_WBR		BIT(8)	/* write-back read allocate */
+
+/*
+ * MCOR - machine-mode cache operation
+ *
+ * The address is common but the encoding is not: the C906 decodes a single
+ * INV and CLR bit qualified by a cache select field, the C910 four separate
+ * invalidate bits.  The layout below is the C910's; a core whose manual
+ * documents the C906 form must not reuse these bits.
+ */
+#define MCOR_CACHE_SEL(n)	((n) << 0)	/* 1: I-cache, 2: D-cache, 3: both */
+#define MCOR_INV		BIT(4)		/* invalidate the selected caches */
+#define MCOR_BHT_INV		BIT(16)		/* branch history table */
+#define MCOR_BTB_INV		BIT(17)		/* branch target buffer */
+#define MCOR_IBP_INV		BIT(18)		/* indirect branch predictor */
+
+/* MCCR2 - machine-mode L2 cache configuration */
+#define MCCR2_RFE		BIT(0)		/* read allocate enable */
+#define MCCR2_L2EN		BIT(3)		/* L2 cache enable */
+#define MCCR2_DLNTCY(n)		((n) << 16)	/* data RAM latency */
+#define MCCR2_DSETUP		BIT(19)		/* data RAM setup time */
+#define MCCR2_TLNTCY(n)		((n) << 22)	/* tag RAM latency */
+#define MCCR2_TSETUP		BIT(25)		/* tag RAM setup time */
+#define MCCR2_IPRF(n)		((n) << 29)	/* instruction prefetch lines */
+#define MCCR2_TPRF		BIT(31)		/* tag RAM prefetch enable */
+
+/*
+ * MHINT - machine-mode implicit operation
+ *
+ * AMR selects how many consecutive stores to the same 4 KiB region make the
+ * core stop allocating cache lines for the rest of it.  It is a two-bit field
+ * on the C906 but the C910 only decodes bit 3.
+ *
+ * Some of the fields the C906 does not have also mean something else there:
+ * bit 20 is L2STPLD on the C910 and AEE on the C906, so it is left undefined
+ * until one of them needs it.
+ */
+#define MHINT_DPLD		BIT(2)		/* D-cache prefetch enable */
+#define MHINT_AMR(n)		((n) << 3)	/* write-allocate adjustment */
+#define MHINT_IPLD		BIT(8)		/* I-cache prefetch enable */
+#define MHINT_LPE		BIT(9)		/* loop prefetch enable */
+#define MHINT_DCACHE_PREF_DIST(n)	((n) << 13)	/* in 64 B lines */
+/* not decoded by every core */
+#define MHINT_L2PLD		BIT(15)		/* L2 prefetch enable */
+#define MHINT_L2_PREF_DIST(n)	((n) << 16)	/* in 64 B lines */
+#define MHINT_NSFE		BIT(18)
+#define MHINT_TLB_BROAD_DIS	BIT(21)		/* keep TLB ops hart-local */
+#define MHINT_FENCERW_BROAD_DIS	BIT(22)	/* keep fence rw hart-local */
+
+/* MHINT2 - per-unit clock gating */
+#define MHINT2_LOCAL_ICG_EN(n)	BIT((n) + 14)
+
+/* MSMPR - machine-mode SMP control */
+#define MSMPR_SMPEN		BIT(0)		/* cache snooping enable */
+
+#endif /* _ASM_RISCV_THEAD_CSR_H */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/4] riscv: th1520: use the common C9xx CSR definitions
  2026-11-24 19:52 [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Indrek Kruusa
  2026-11-24 19:52 ` [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs Indrek Kruusa
@ 2026-11-24 19:52 ` Indrek Kruusa
  2026-11-24 19:52 ` [PATCH 3/4] riscv: lib: rename thead_cmo.c to thead_cache.c Indrek Kruusa
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Indrek Kruusa @ 2026-11-24 19:52 UTC (permalink / raw)
  To: Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot, Indrek Kruusa

Use asm/thead_csr.h instead of the definitions in spl.c.  The header
keeps CSR_ on register numbers and drops it on bitfields; the values
written are unchanged.

Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
---
 arch/riscv/cpu/th1520/spl.c | 96 ++++++++++---------------------------
 1 file changed, 26 insertions(+), 70 deletions(-)

diff --git a/arch/riscv/cpu/th1520/spl.c b/arch/riscv/cpu/th1520/spl.c
index ceb934021d9..d6fa8724334 100644
--- a/arch/riscv/cpu/th1520/spl.c
+++ b/arch/riscv/cpu/th1520/spl.c
@@ -4,6 +4,7 @@
  */
 #include <asm/arch/iopmp.h>
 #include <asm/io.h>
+#include <asm/thead_csr.h>
 #include <cpu_func.h>
 #include <dm.h>
 #include <linux/sizes.h>
@@ -20,52 +21,6 @@
 #define  TH1520_SUBSYS_RST_VI_N		BIT(1)
 #define  TH1520_SUBSYS_RST_DSP_N	BIT(0)
 
-#define CSR_MXSTATUS			0x7c0
-#define  CSR_MXSTATUS_THEADISAEE	BIT(22)
-#define  CSR_MXSTATUS_MAEE		BIT(21)
-#define  CSR_MXSTATUS_CLINTEE		BIT(17)
-#define  CSR_MXSTATUS_UCME		BIT(16)
-#define  CSR_MXSTATUS_MM		BIT(15)
-#define CSR_MHCR			0x7c1
-#define  CSR_MHCR_WBR			BIT(8)
-#define  CSR_MHCR_BTB			BIT(6)
-#define  CSR_MHCR_BPE			BIT(5)
-#define  CSR_MHCR_RS			BIT(4)
-#define  CSR_MHCR_WB			BIT(3)
-#define  CSR_MHCR_WA			BIT(2)
-#define  CSR_MHCR_DE			BIT(1)
-#define  CSR_MHCR_IE			BIT(0)
-#define CSR_MCOR			0x7c2
-#define  CSR_MCOR_IBP_INV		BIT(18)
-#define  CSR_MCOR_BTB_INV		BIT(17)
-#define  CSR_MCOR_BHT_INV		BIT(16)
-#define  CSR_MCOR_CACHE_INV		BIT(4)
-#define CSR_MCCR2			0x7c3
-#define  CSR_MCCR2_TPRF			BIT(31)
-#define  CSR_MCCR2_IPRF(n)		((n) << 29)
-#define  CSR_MCCR2_TSETUP		BIT(25)
-#define  CSR_MCCR2_TLNTCY(n)		((n) << 22)
-#define  CSR_MCCR2_DSETUP		BIT(19)
-#define  CSR_MCCR2_DLNTCY(n)		((n) << 16)
-#define  CSR_MCCR2_L2EN			BIT(3)
-#define  CSR_MCCR2_RFE			BIT(0)
-#define CSR_MHINT			0x7c5
-#define  CSR_MHINT_FENCERW_BROAD_DIS	BIT(22)
-#define  CSR_MHINT_TLB_BRAOD_DIS	BIT(21)
-#define  CSR_MHINT_NSFE			BIT(18)
-#define  CSR_MHINT_L2_PREF_DIST(n)	((n) << 16)
-#define  CSR_MHINT_L2PLD		BIT(15)
-#define  CSR_MHINT_DCACHE_PREF_DIST(n)	((n) << 13)
-#define  CSR_MHINT_LPE			BIT(9)
-#define  CSR_MHINT_ICACHE_PREF		BIT(8)
-#define  CSR_MHINT_AMR			BIT(3)
-#define  CSR_MHINT_DCACHE_PREF		BIT(2)
-#define CSR_MHINT2			0x7cc
-#define  CSR_MHINT2_LOCAL_ICG_EN(n)	BIT((n) + 14)
-#define CSR_MHINT4			0x7ce
-#define CSR_MSMPR			0x7f3
-#define  CSR_MSMPR_SMPEN		BIT(0)
-
 int spl_dram_init(void)
 {
 	int ret;
@@ -123,11 +78,11 @@ void harts_early_init(void)
 	int i;
 
 	/* Invalidate cache and buffer entries */
-	csr_write(CSR_MCOR, CSR_MCOR_IBP_INV | CSR_MCOR_BTB_INV |
-			    CSR_MCOR_BHT_INV | CSR_MCOR_CACHE_INV | 0x3);
+	csr_write(CSR_MCOR, MCOR_IBP_INV | MCOR_BTB_INV | MCOR_BHT_INV |
+			    MCOR_INV | MCOR_CACHE_SEL(3));
 
 	/* Enable cache snooping */
-	csr_write(CSR_MSMPR, CSR_MSMPR_SMPEN);
+	csr_write(CSR_MSMPR, MSMPR_SMPEN);
 
 	/*
 	 * Configure and enable L2 cache,
@@ -135,27 +90,28 @@ void harts_early_init(void)
 	 *	Prefetch 3 cache lines of instructions
 	 *	Enable read allocation
 	 */
-	csr_write(CSR_MCCR2, CSR_MCCR2_TPRF   | CSR_MCCR2_IPRF(3)	|
-			     CSR_MCCR2_TSETUP | CSR_MCCR2_TLNTCY(1)	|
-			     CSR_MCCR2_DSETUP | CSR_MCCR2_DLNTCY(1)	|
-			     CSR_MCCR2_L2EN   | CSR_MCCR2_RFE);
-	csr_write(CSR_MXSTATUS, CSR_MXSTATUS_THEADISAEE | CSR_MXSTATUS_MAEE |
-				CSR_MXSTATUS_CLINTEE    | CSR_MXSTATUS_UCME |
-				CSR_MXSTATUS_MM);
-	csr_write(CSR_MHINT, CSR_MHINT_FENCERW_BROAD_DIS	|
-			     CSR_MHINT_TLB_BRAOD_DIS		|
-			     CSR_MHINT_NSFE			|
-			     CSR_MHINT_L2_PREF_DIST(2)		|
-			     CSR_MHINT_L2PLD			|
-			     CSR_MHINT_DCACHE_PREF_DIST(3)	|
-			     CSR_MHINT_LPE			|
-			     CSR_MHINT_ICACHE_PREF		|
-			     CSR_MHINT_AMR			|
-			     CSR_MHINT_DCACHE_PREF);
-	csr_write(CSR_MHCR, CSR_MHCR_WBR | CSR_MHCR_BTB | CSR_MHCR_BPE |
-			    CSR_MHCR_RS  | CSR_MHCR_WB  | CSR_MHCR_WA | 0x3);
-	csr_write(CSR_MHINT2, CSR_MHINT2_LOCAL_ICG_EN(8)	|
-			      CSR_MHINT2_LOCAL_ICG_EN(3));
+	csr_write(CSR_MCCR2, MCCR2_TPRF   | MCCR2_IPRF(3)		|
+			     MCCR2_TSETUP | MCCR2_TLNTCY(1)		|
+			     MCCR2_DSETUP | MCCR2_DLNTCY(1)		|
+			     MCCR2_L2EN   | MCCR2_RFE);
+	csr_write(CSR_MXSTATUS, MXSTATUS_THEADISAEE | MXSTATUS_MAEE |
+				MXSTATUS_CLINTEE    | MXSTATUS_UCME |
+				MXSTATUS_MM);
+	csr_write(CSR_MHINT, MHINT_FENCERW_BROAD_DIS	|
+			     MHINT_TLB_BROAD_DIS	|
+			     MHINT_NSFE			|
+			     MHINT_L2_PREF_DIST(2)	|
+			     MHINT_L2PLD		|
+			     MHINT_DCACHE_PREF_DIST(3)	|
+			     MHINT_LPE			|
+			     MHINT_IPLD			|
+			     MHINT_AMR(1)		|
+			     MHINT_DPLD);
+	csr_write(CSR_MHCR, MHCR_WBR | MHCR_BTB | MHCR_BPE |
+			    MHCR_RS  | MHCR_WB  | MHCR_WA  |
+			    MHCR_IE  | MHCR_DE);
+	csr_write(CSR_MHINT2, MHINT2_LOCAL_ICG_EN(8)	|
+			      MHINT2_LOCAL_ICG_EN(3));
 	csr_write(CSR_MHINT4, 0x410);
 
 	/*
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/4] riscv: lib: rename thead_cmo.c to thead_cache.c
  2026-11-24 19:52 [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Indrek Kruusa
  2026-11-24 19:52 ` [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs Indrek Kruusa
  2026-11-24 19:52 ` [PATCH 2/4] riscv: th1520: use the common C9xx CSR definitions Indrek Kruusa
@ 2026-11-24 19:52 ` Indrek Kruusa
  2026-11-24 19:52 ` [PATCH 4/4] riscv: lib: add the whole-cache and enable operations Indrek Kruusa
  2026-11-25 17:23 ` [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Yao Zi
  4 siblings, 0 replies; 7+ messages in thread
From: Indrek Kruusa @ 2026-11-24 19:52 UTC (permalink / raw)
  To: Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot, Indrek Kruusa

The file will also hold the cache enable and disable functions, which are
not maintenance operations.  Rename the file and the SYS_CACHE_THEAD_CMO
symbol to THEAD_CACHE to match.

No functional change.

Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
---
 arch/riscv/Kconfig                            | 10 ++++------
 arch/riscv/cpu/cv1800b/Kconfig                |  2 +-
 arch/riscv/cpu/th1520/Kconfig                 |  2 +-
 arch/riscv/lib/Makefile                       |  2 +-
 arch/riscv/lib/{thead_cmo.c => thead_cache.c} |  0
 5 files changed, 7 insertions(+), 9 deletions(-)
 rename arch/riscv/lib/{thead_cmo.c => thead_cache.c} (100%)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 386241efd4a..23fe867c840 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -89,13 +89,11 @@ config SYS_DCACHE_OFF
 	help
 	  Do not enable data cache in U-Boot.
 
-config SYS_CACHE_THEAD_CMO
-	bool "THEAD non-standard cache operations"
-	depends on !SYS_DCACHE_OFF
-	default n
+config THEAD_CACHE
+	bool
 	help
-	  Support for non-standard cache management operations on SoCs based on
-	  T-Head C906/C910 cores.
+	  Support for T-Head C9xx cache operations not covered by the RISC-V
+	  base ISA.
 
 config SPL_SYS_DCACHE_OFF
 	bool "Do not enable dcache in SPL"
diff --git a/arch/riscv/cpu/cv1800b/Kconfig b/arch/riscv/cpu/cv1800b/Kconfig
index 57f724ae043..8fd09719852 100644
--- a/arch/riscv/cpu/cv1800b/Kconfig
+++ b/arch/riscv/cpu/cv1800b/Kconfig
@@ -6,7 +6,7 @@ config SOPHGO_CV1800B
 	bool
 	select ARCH_EARLY_INIT_R
 	select SYS_CACHE_SHIFT_6
-	select SYS_CACHE_THEAD_CMO
+	select THEAD_CACHE
 	imply CPU
 	imply CPU_RISCV
 	imply RISCV_TIMER
diff --git a/arch/riscv/cpu/th1520/Kconfig b/arch/riscv/cpu/th1520/Kconfig
index a02f5f24906..738bdc7523f 100644
--- a/arch/riscv/cpu/th1520/Kconfig
+++ b/arch/riscv/cpu/th1520/Kconfig
@@ -9,7 +9,7 @@ config THEAD_TH1520
 	select SYS_CACHE_SHIFT_6
 	select SUPPORT_SPL
 	select BINMAN if SPL
-	select SYS_CACHE_THEAD_CMO
+	select THEAD_CACHE
 	select CLK_THEAD
 	select PINCTRL_TH1520
 	imply CPU
diff --git a/arch/riscv/lib/Makefile b/arch/riscv/lib/Makefile
index a527b3e9ae3..89c4fd4387e 100644
--- a/arch/riscv/lib/Makefile
+++ b/arch/riscv/lib/Makefile
@@ -13,7 +13,7 @@ obj-$(CONFIG_$(PHASE_)LIB_BOOTI) += image.o
 obj-$(CONFIG_CMD_GO) += boot.o
 obj-y	+= cache.o
 obj-$(CONFIG_SIFIVE_CACHE) += sifive_cache.o
-obj-$(CONFIG_SYS_CACHE_THEAD_CMO) += thead_cmo.o
+obj-$(CONFIG_THEAD_CACHE) += thead_cache.o
 ifeq ($(CONFIG_$(PHASE_)RISCV_MMODE),y)
 obj-$(CONFIG_$(PHASE_)RISCV_ACLINT) += aclint_ipi.o
 obj-$(CONFIG_ANDES_PLICSW) += andes_plicsw.o
diff --git a/arch/riscv/lib/thead_cmo.c b/arch/riscv/lib/thead_cache.c
similarity index 100%
rename from arch/riscv/lib/thead_cmo.c
rename to arch/riscv/lib/thead_cache.c
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 4/4] riscv: lib: add the whole-cache and enable operations
  2026-11-24 19:52 [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Indrek Kruusa
                   ` (2 preceding siblings ...)
  2026-11-24 19:52 ` [PATCH 3/4] riscv: lib: rename thead_cmo.c to thead_cache.c Indrek Kruusa
@ 2026-11-24 19:52 ` Indrek Kruusa
  2026-11-25 17:23 ` [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Yao Zi
  4 siblings, 0 replies; 7+ messages in thread
From: Indrek Kruusa @ 2026-11-24 19:52 UTC (permalink / raw)
  To: Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot, Indrek Kruusa

Add flush_dcache_all() and invalidate_dcache_all(), and the cache
enable, disable and status functions.

On C910 the whole-cache instructions act on the L1 of the calling core
only, so the L2 is cleaned on top of them when THEAD_L2_CACHE is set.
The address-range operations need nothing on top: since commit
b3d8c7bd081a ("riscv: thead: fix D-cache flushing") they clean and
invalidate, and they already reach the L2 and every core.

The enable and status functions use MHCR, an M-mode register, so they
are compiled out where U-Boot runs in S-mode, as on CV1800B.

Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
---
 arch/riscv/Kconfig             |   7 ++
 arch/riscv/cpu/th1520/Kconfig  |   1 +
 arch/riscv/cpu/th1520/Makefile |   1 -
 arch/riscv/cpu/th1520/cache.c  |  34 ----------
 arch/riscv/lib/thead_cache.c   | 116 ++++++++++++++++++++++++++++-----
 5 files changed, 108 insertions(+), 51 deletions(-)
 delete mode 100644 arch/riscv/cpu/th1520/cache.c

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 23fe867c840..421219fa17a 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -95,6 +95,13 @@ config THEAD_CACHE
 	  Support for T-Head C9xx cache operations not covered by the RISC-V
 	  base ISA.
 
+config THEAD_L2_CACHE
+	bool
+	depends on THEAD_CACHE
+	help
+	  Make the whole-cache operations cover the L2.  This option is not
+	  about enabling the L2 hardware.
+
 config SPL_SYS_DCACHE_OFF
 	bool "Do not enable dcache in SPL"
 	depends on SPL
diff --git a/arch/riscv/cpu/th1520/Kconfig b/arch/riscv/cpu/th1520/Kconfig
index 738bdc7523f..baff8ba1934 100644
--- a/arch/riscv/cpu/th1520/Kconfig
+++ b/arch/riscv/cpu/th1520/Kconfig
@@ -10,6 +10,7 @@ config THEAD_TH1520
 	select SUPPORT_SPL
 	select BINMAN if SPL
 	select THEAD_CACHE
+	select THEAD_L2_CACHE
 	select CLK_THEAD
 	select PINCTRL_TH1520
 	imply CPU
diff --git a/arch/riscv/cpu/th1520/Makefile b/arch/riscv/cpu/th1520/Makefile
index d971ea7390d..2b7253867a4 100644
--- a/arch/riscv/cpu/th1520/Makefile
+++ b/arch/riscv/cpu/th1520/Makefile
@@ -2,7 +2,6 @@
 #
 # Copyright (C) 2025, Yao Zi <ziyao@disroot.org>
 
-obj-y += cache.o
 obj-y += cpu.o
 obj-y += dram.o
 obj-$(CONFIG_SPL_BUILD) += spl.o
diff --git a/arch/riscv/cpu/th1520/cache.c b/arch/riscv/cpu/th1520/cache.c
deleted file mode 100644
index b2fec229363..00000000000
--- a/arch/riscv/cpu/th1520/cache.c
+++ /dev/null
@@ -1,34 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Copyright (c) 2025 Yao Zi <ziyao@disroot.org>
- */
-
-#include <asm/io.h>
-#include <cpu_func.h>
-#include <linux/bitops.h>
-
-#define CSR_MHCR		0x7c1
-#define  CSR_MHCR_IE		BIT(0)
-#define  CSR_MHCR_DE		BIT(1)
-
-#if CONFIG_IS_ENABLED(RISCV_MMODE)
-void icache_enable(void)
-{
-	csr_write(CSR_MHCR, csr_read(CSR_MHCR) | CSR_MHCR_IE);
-}
-
-void dcache_enable(void)
-{
-	csr_write(CSR_MHCR, csr_read(CSR_MHCR) | CSR_MHCR_DE);
-}
-
-int icache_status(void)
-{
-	return (csr_read(CSR_MHCR) & CSR_MHCR_IE) != 0;
-}
-
-int dcache_status(void)
-{
-	return (csr_read(CSR_MHCR) & CSR_MHCR_DE) != 0;
-}
-#endif /* CONFIG_IS_ENABLED(RISCV_MMODE) */
diff --git a/arch/riscv/lib/thead_cache.c b/arch/riscv/lib/thead_cache.c
index 763ff799edd..a1104a53dc1 100644
--- a/arch/riscv/lib/thead_cache.c
+++ b/arch/riscv/lib/thead_cache.c
@@ -1,36 +1,49 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (c) 2024, Kongyang Liu <seashell11234455@gmail.com>
+ * Copyright (C) 2025, Yao Zi <ziyao@disroot.org>
+ * Copyright (C) 2026, Indrek Kruusa <indrek.kruusa@gmail.com>
+ *
+ * Cache operations for the T-Head C9xx cores.
  */
 
+#include <asm/csr.h>
+#include <asm/thead_csr.h>
 #include <cpu_func.h>
 
 /*
- * dcache.ipa rs1 (invalidate)
- * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
- *   0000001    01010      rs1       000      00000  0001011
- *
- * dcache.cpa rs1 (clean)
- * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
- *   0000001    01001      rs1       000      00000  0001011
+ * These instructions are not known to the assembler, so they are emitted as
+ * raw encodings.  They all use funct3 = 0, rd = 0 and opcode 0001011, with the
+ * operation in funct5 and the address of the affected line, if any, in rs1:
  *
- * dcache.cipa rs1 (clean then invalidate)
- * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
- *   0000001    01011      rs1       000      00000  0001011
+ *   dcache.ipa   rs1   invalidate the line at a0
+ *   dcache.cipa  rs1   clean and invalidate the line at a0
+ *   dcache.iall        invalidate all L1 entries
+ *   dcache.ciall       clean and invalidate all L1 entries
+ *   l2cache.iall       invalidate the L2
+ *   l2cache.ciall      clean and invalidate the L2
+ *   sync.s             wait for the operation to complete
  *
- * sync.s
- * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
- *   0000000    11001     00000      000      00000  0001011
+ * They only decode while MXSTATUS.THEADISAEE is set.  Where U-Boot runs in
+ * M-mode, harts_early_init() sets it; on S-mode ports the FSBL has already
+ * enabled the extensions.
  */
 #define DCACHE_IPA_A0	".long 0x02a5000b"
-#define DCACHE_CPA_A0	".long 0x0295000b"
 #define DCACHE_CIPA_A0	".long 0x02b5000b"
+#define DCACHE_IALL	".long 0x0020000b"
+#define DCACHE_CIALL	".long 0x0030000b"
+#define L2CACHE_IALL	".long 0x0160000b"
+#define L2CACHE_CIALL	".long 0x0170000b"
 
 #define SYNC_S		".long 0x0190000b"
 
+#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
+
 void invalidate_dcache_range(unsigned long start, unsigned long end)
 {
-	register unsigned long i asm("a0") = start & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
+	register unsigned long i asm("a0") =
+		start & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
+
 	for (; i < end; i += CONFIG_SYS_CACHELINE_SIZE)
 		__asm__ __volatile__(DCACHE_IPA_A0);
 	__asm__ __volatile__(SYNC_S);
@@ -38,8 +51,79 @@ void invalidate_dcache_range(unsigned long start, unsigned long end)
 
 void flush_dcache_range(unsigned long start, unsigned long end)
 {
-	register unsigned long i asm("a0") = start & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
+	register unsigned long i asm("a0") =
+		start & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
+
 	for (; i < end; i += CONFIG_SYS_CACHELINE_SIZE)
 		__asm__ __volatile__(DCACHE_CIPA_A0);
 	__asm__ __volatile__(SYNC_S);
 }
+
+/*
+ * The whole-cache operations act on the L1 of the calling core only, so the
+ * L2 is cleaned on top when CONFIG_THEAD_L2_CACHE is set.  The address-range
+ * operations above are different: they act on both levels and every core of
+ * the cluster, which is why nothing is added for them.
+ */
+void invalidate_dcache_all(void)
+{
+	__asm__ __volatile__(DCACHE_IALL);
+	if (IS_ENABLED(CONFIG_THEAD_L2_CACHE))
+		__asm__ __volatile__(L2CACHE_IALL);
+	__asm__ __volatile__(SYNC_S);
+}
+
+void flush_dcache_all(void)
+{
+	__asm__ __volatile__(DCACHE_CIALL);
+	if (IS_ENABLED(CONFIG_THEAD_L2_CACHE))
+		__asm__ __volatile__(L2CACHE_CIALL);
+	__asm__ __volatile__(SYNC_S);
+}
+
+#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
+
+/*
+ * MHCR is not accessible from S-mode, so this half is only built where U-Boot
+ * runs in M-mode.
+ */
+#if CONFIG_IS_ENABLED(RISCV_MMODE)
+
+void icache_enable(void)
+{
+	invalidate_icache_all();
+	csr_set(CSR_MHCR, MHCR_IE);
+}
+
+void icache_disable(void)
+{
+	csr_clear(CSR_MHCR, MHCR_IE);
+}
+
+int icache_status(void)
+{
+	return csr_read(CSR_MHCR) & MHCR_IE;
+}
+
+#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
+
+void dcache_enable(void)
+{
+	invalidate_dcache_all();
+	csr_set(CSR_MHCR, MHCR_DE);
+}
+
+void dcache_disable(void)
+{
+	flush_dcache_all();
+	csr_clear(CSR_MHCR, MHCR_DE);
+}
+
+int dcache_status(void)
+{
+	return csr_read(CSR_MHCR) & MHCR_DE;
+}
+
+#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
+
+#endif /* CONFIG_IS_ENABLED(RISCV_MMODE) */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs
  2026-11-24 19:52 ` [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs Indrek Kruusa
@ 2026-11-25 15:00   ` Junhui Liu
  0 siblings, 0 replies; 7+ messages in thread
From: Junhui Liu @ 2026-11-25 15:00 UTC (permalink / raw)
  To: Indrek Kruusa, Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot

Hi Indrek,

Thanks for doing these!

On Wed Nov 25, 2026 at 3:52 AM CST, Indrek Kruusa wrote:
> Add a header with the register numbers and bitfields currently used by the
> C9xx ports. It is placed in asm/ so that both SPL and U-Boot proper can
> include it. M-mode only.
>
> Signed-off-by: Indrek Kruusa <indrek.kruusa@gmail.com>
> ---
>  arch/riscv/include/asm/thead_csr.h | 105 +++++++++++++++++++++++++++++
>  1 file changed, 105 insertions(+)
>  create mode 100644 arch/riscv/include/asm/thead_csr.h
>
> diff --git a/arch/riscv/include/asm/thead_csr.h b/arch/riscv/include/asm/thead_csr.h
> new file mode 100644
> index 00000000000..32d3bf773ee
> --- /dev/null
> +++ b/arch/riscv/include/asm/thead_csr.h
> @@ -0,0 +1,105 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * Copyright (C) 2025 Yao Zi <ziyao@disroot.org>
> + * Copyright (C) 2026, Indrek Kruusa <indrek.kruusa@gmail.com>
> + *
> + * T-Head C9xx machine-mode control registers.
> + *
> + * Register numbers are shared, but the fields defined here are only those used
> + * by the in-tree ports. Availability and layout may differ between cores. Any
> + * known differences that could cause incorrect configuration are noted at the
> + * corresponding definitions.
> + */
> +
> +#ifndef _ASM_RISCV_THEAD_CSR_H
> +#define _ASM_RISCV_THEAD_CSR_H
> +
> +#include <linux/bitops.h>
> +
> +/* M-mode control registers */
> +#define CSR_MXSTATUS		0x7c0
> +#define CSR_MHCR		0x7c1
> +#define CSR_MCOR		0x7c2
> +#define CSR_MHINT		0x7c5
> +
> +/* not decoded by every core */
> +#define CSR_MCCR2		0x7c3
> +#define CSR_MHINT2		0x7cc
> +#define CSR_MHINT4		0x7ce
> +#define CSR_MSMPR		0x7f3
> +
> +/* MXSTATUS - machine-mode status */
> +#define MXSTATUS_THEADISAEE	BIT(22)	/* T-HEAD ISA extensions enable */
> +#define MXSTATUS_MAEE		BIT(21)	/* MMU honours the PTE memory type */
> +#define MXSTATUS_CLINTEE	BIT(17)	/* CLINT extension enable */
> +#define MXSTATUS_UCME		BIT(16)	/* user-mode cache ops enable */
> +#define MXSTATUS_MM		BIT(15)	/* misaligned access enable */

Could you keep Ziyao's original style, with the bit definitions grouped
under each CSR definition? That makes it easier to see which register
each bit belongs to.

Could you also keep the existing names, e.g. CSR_MXSTATUS_THEADISAEE?
Then patches 1 and 2 could be combined into a straightforward move of
the definitions into the common header, without renaming their uses
in arch/riscv/cpu/th1520/spl.c. Any new definitions could be added in a
separate patch, or later in the D1 series when needed. This keeps the
code move separate from other changes and makes it easier to review.

> +
> +/*
> + * MHCR - machine-mode hardware configuration
> + *
> + * WB and WBR are read-only and always read back as 1.
> + */
> +#define MHCR_IE			BIT(0)	/* I-cache enable */
> +#define MHCR_DE			BIT(1)	/* D-cache enable */
> +#define MHCR_WA			BIT(2)	/* D-cache write allocate */
> +#define MHCR_WB			BIT(3)	/* D-cache write back */
> +#define MHCR_RS			BIT(4)	/* return stack enable */
> +#define MHCR_BPE		BIT(5)	/* branch prediction enable */
> +#define MHCR_BTB		BIT(6)	/* branch target buffer enable */
> +#define MHCR_WBR		BIT(8)	/* write-back read allocate */
> +
> +/*
> + * MCOR - machine-mode cache operation
> + *
> + * The address is common but the encoding is not: the C906 decodes a single
> + * INV and CLR bit qualified by a cache select field, the C910 four separate
> + * invalidate bits.  The layout below is the C910's; a core whose manual
> + * documents the C906 form must not reuse these bits.
> + */
> +#define MCOR_CACHE_SEL(n)	((n) << 0)	/* 1: I-cache, 2: D-cache, 3: both */
> +#define MCOR_INV		BIT(4)		/* invalidate the selected caches */
> +#define MCOR_BHT_INV		BIT(16)		/* branch history table */
> +#define MCOR_BTB_INV		BIT(17)		/* branch target buffer */
> +#define MCOR_IBP_INV		BIT(18)		/* indirect branch predictor */
> +
> +/* MCCR2 - machine-mode L2 cache configuration */
> +#define MCCR2_RFE		BIT(0)		/* read allocate enable */
> +#define MCCR2_L2EN		BIT(3)		/* L2 cache enable */
> +#define MCCR2_DLNTCY(n)		((n) << 16)	/* data RAM latency */
> +#define MCCR2_DSETUP		BIT(19)		/* data RAM setup time */
> +#define MCCR2_TLNTCY(n)		((n) << 22)	/* tag RAM latency */
> +#define MCCR2_TSETUP		BIT(25)		/* tag RAM setup time */
> +#define MCCR2_IPRF(n)		((n) << 29)	/* instruction prefetch lines */
> +#define MCCR2_TPRF		BIT(31)		/* tag RAM prefetch enable */
> +
> +/*
> + * MHINT - machine-mode implicit operation
> + *
> + * AMR selects how many consecutive stores to the same 4 KiB region make the
> + * core stop allocating cache lines for the rest of it.  It is a two-bit field
> + * on the C906 but the C910 only decodes bit 3.
> + *
> + * Some of the fields the C906 does not have also mean something else there:
> + * bit 20 is L2STPLD on the C910 and AEE on the C906, so it is left undefined
> + * until one of them needs it.
> + */
> +#define MHINT_DPLD		BIT(2)		/* D-cache prefetch enable */
> +#define MHINT_AMR(n)		((n) << 3)	/* write-allocate adjustment */
> +#define MHINT_IPLD		BIT(8)		/* I-cache prefetch enable */
> +#define MHINT_LPE		BIT(9)		/* loop prefetch enable */
> +#define MHINT_DCACHE_PREF_DIST(n)	((n) << 13)	/* in 64 B lines */
> +/* not decoded by every core */
> +#define MHINT_L2PLD		BIT(15)		/* L2 prefetch enable */
> +#define MHINT_L2_PREF_DIST(n)	((n) << 16)	/* in 64 B lines */
> +#define MHINT_NSFE		BIT(18)
> +#define MHINT_TLB_BROAD_DIS	BIT(21)		/* keep TLB ops hart-local */
> +#define MHINT_FENCERW_BROAD_DIS	BIT(22)	/* keep fence rw hart-local */
> +
> +/* MHINT2 - per-unit clock gating */
> +#define MHINT2_LOCAL_ICG_EN(n)	BIT((n) + 14)
> +
> +/* MSMPR - machine-mode SMP control */
> +#define MSMPR_SMPEN		BIT(0)		/* cache snooping enable */

I think most of the comments in this file can be dropped. The CSR and
bit names are generally clear enough, and the details are available in
the XuanTie manuals. Brief notes about differences between cores should
be enough.

> +
> +#endif /* _ASM_RISCV_THEAD_CSR_H */

-- 
Best regards,
Junhui Liu


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs
  2026-11-24 19:52 [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Indrek Kruusa
                   ` (3 preceding siblings ...)
  2026-11-24 19:52 ` [PATCH 4/4] riscv: lib: add the whole-cache and enable operations Indrek Kruusa
@ 2026-11-25 17:23 ` Yao Zi
  4 siblings, 0 replies; 7+ messages in thread
From: Yao Zi @ 2026-11-25 17:23 UTC (permalink / raw)
  To: Indrek Kruusa, Tim Ouyang, Leo Liang
  Cc: Yao Zi, Tom Rini, Han Gao, Kongyang Liu, Junhui Liu,
	Samuel Holland, Andre Przywara, Hiago De Franco, E Shattow,
	Yixun Lan, Raymond Mao, u-boot

On Tue, Nov 24, 2026 at 09:52:33PM +0200, Indrek Kruusa wrote:
> This series consolidates the vendor-specific cache operations of the
> T-Head C9xx cores and the machine-mode CSRs those ports use, and
> implements the whole-cache operations.  It is a prerequisite for the
> upcoming Allwinner D1/D1s support.  However, no definitions have been
> added yet that only D1 would use.
> 
> The series adds arch/riscv/include/asm/thead_csr.h, with the CSR numbers
> and bitfields the T-Head ports use today.  It also adds
> arch/riscv/lib/thead_cache.c: the renamed thead_cmo.c, with the enable and
> disable functions taken from arch/riscv/cpu/th1520/cache.c, which is
> removed.

This isn't what I'd expect. I'd prefer to have separate thead_cmo.c and
thead_cache.c, and also separate Kconfig options, where the earlier
defines operations based on XTheadCMO, and the latter implements
machine-mode-only cache management operations through CSR.

A strong reason to separate them is that Xuantie cores with these cache
CSRs available might not use XTheadCMO, iirc Xuantie C908 is such an
example.

[...]

-- 
Thanks,
Yao Zi

Meow-mrrp-mrow, I'm a cat.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-25 17:24 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 19:52 [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Indrek Kruusa
2026-11-24 19:52 ` [PATCH 1/4] riscv: thead: add a common header for the C9xx CSRs Indrek Kruusa
2026-11-25 15:00   ` Junhui Liu
2026-11-24 19:52 ` [PATCH 2/4] riscv: th1520: use the common C9xx CSR definitions Indrek Kruusa
2026-11-24 19:52 ` [PATCH 3/4] riscv: lib: rename thead_cmo.c to thead_cache.c Indrek Kruusa
2026-11-24 19:52 ` [PATCH 4/4] riscv: lib: add the whole-cache and enable operations Indrek Kruusa
2026-11-25 17:23 ` [PATCH 0/4] riscv: consolidate the T-Head C9xx cache operations and CSRs Yao Zi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox