Skip to contentSkip to conversation

riscv: andes: add L2 cache flush support

PATCH In review Randolph Lin 2 participants 6 messages Oct 28 – Nov 2

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

cover riscv: andes: add L2 cache flush support

On Andes, flush_dcache_all() only flushes L1, and flush_dcache_range() and invalidate_dcache_range() call it. Without the I/O coherence port (IOCP), DMA masters do not snoop the CPU caches, so dirty lines left in L2 make them read stale data.

This series adds a flush_all operation to the cache uclass, implements it in the Andes L2 cache driver and calls it from flush_dcache_all(). In U-Boot proper, enable_caches() runs before driver model is set up after relocation and cannot find the L2 cache device, so patch 4 runs it again once driver model is ready. U-Boot proper in S-mode then drives L2 itself, as the Linux ax45mp_cache driver does.

Side effects: each range operation flushes the whole L2 cache, "dcache off" and "dcache on" turn L2 off and on in S-mode, and L2 is now enabled in M-mode U-Boot proper.

checkpatch warns about the #ifdef in patch 3, which matches the rest of the file, and does not know the Assisted-by tag from Linux's Documentation/process/coding-assistants.rst.

Randolph Lin (4):
dm: cache: Add flush_all operation
cache: andes-l2: implement flush_all
riscv: andes: flush the L2 cache in flush_dcache_all()
riscv: andes: probe the L2 cache device in U-Boot proper

 arch/riscv/cpu/andes/cache.c   | 19 +++++++++++++++++++
 drivers/cache/cache-andes-l2.c | 16 ++++++++++++++--
 drivers/cache/cache-uclass.c   | 10 ++++++++++
 include/cache.h                | 16 ++++++++++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 dm: cache: Add flush_all operation

The cache uclass can enable and disable a cache controller, and disable() writes the cache back before turning it off. There is no way to write back and invalidate a cache while keeping it enabled, which flush_dcache_all() needs for an outer cache such as the Andes L2 cache.

Add an optional flush_all() operation and a cache_flush_all() wrapper, in the same style as cache_enable() and cache_disable().

Assisted-by Claude:claude-opus-5-5
---
 drivers/cache/cache-uclass.c | 10 ++++++++++
 include/cache.h              | 16 ++++++++++++++++
 2 files changed, 26 insertions(+)
drivers/cache/cache-uclass.c +10 -0
index 300e7bc86e1..b66dd500ee0 100644
@@ -38,6 +38,16 @@ int cache_disable(struct udevice *dev)
 	return ops->disable(dev);
 }
 
+int cache_flush_all(struct udevice *dev)
+{
+	struct cache_ops *ops = cache_get_ops(dev);
+
+	if (!ops->flush_all)
+		return -ENOSYS;
+
+	return ops->flush_all(dev);
+}
+
 UCLASS_DRIVER(cache) = {
 	.id		= UCLASS_CACHE,
 	.name		= "cache",
include/cache.h +16 -0
index 296ae3c8b48..08a820f7dd8 100644
@@ -42,6 +42,14 @@ struct cache_ops {
 	 * @return 0 if OK, -ve on error
 	 */
 	int (*disable)(struct udevice *dev);
+
+	/**
+	 * flush_all() - Write back and invalidate the whole cache
+	 *
+	 * @dev:	Device to check (UCLASS_CACHE)
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*flush_all)(struct udevice *dev);
 };
 
 #define cache_get_ops(dev)	((struct cache_ops *)(dev)->driver->ops)
@@ -70,4 +78,12 @@ int cache_enable(struct udevice *dev);
  * Return: 0 if OK, -ve on error
  */
 int cache_disable(struct udevice *dev);
+
+/**
+ * cache_flush_all() - Write back and invalidate the whole cache
+ *
+ * @dev:	Device to check (UCLASS_CACHE)
+ * Return: 0 if OK, -ve on error
+ */
+int cache_flush_all(struct udevice *dev);
 #endif
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/4 cache: andes-l2: implement flush_all

In reply to Randolph Lin

Move the write-back-and-invalidate sequence out of andes_l2_disable() into andes_l2_flush_all() and expose it as the flush_all operation.

andes_l2_disable() calls andes_l2_flush_all() before clearing the enable bit, so disabling the cache works as before.

Assisted-by Claude:claude-opus-5-5
---
 drivers/cache/cache-andes-l2.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/cache/cache-andes-l2.c +14 -2
index 45a4f216b07..007b23af2b9 100644
@@ -91,7 +91,7 @@ static int andes_l2_enable(struct udevice *dev)
 	return 0;
 }
 
-static int andes_l2_disable(struct udevice *dev)
+static int andes_l2_flush_all(struct udevice *dev)
 {
 	struct andes_l2_plat *plat = dev_get_plat(dev);
 	volatile struct l2cache *regs = plat->regs;
@@ -109,12 +109,23 @@ static int andes_l2_disable(struct udevice *dev)
 				hang();
 			}
 		}
-		clrbits_le32(&regs->control, L2_ENABLE);
 	}
 
 	return 0;
 }
 
+static int andes_l2_disable(struct udevice *dev)
+{
+	struct andes_l2_plat *plat = dev_get_plat(dev);
+	struct l2cache *regs = plat->regs;
+
+	andes_l2_flush_all(dev);
+	if (regs)
+		clrbits_le32(&regs->control, L2_ENABLE);
+
+	return 0;
+}
+
 static int andes_l2_of_to_plat(struct udevice *dev)
 {
 	struct andes_l2_plat *plat = dev_get_plat(dev);
@@ -192,6 +203,7 @@ static const struct udevice_id andes_l2_cache_ids[] = {
 static const struct cache_ops andes_l2_cache_ops = {
 	.enable		= andes_l2_enable,
 	.disable	= andes_l2_disable,
+	.flush_all	= andes_l2_flush_all,
 };
 
 U_BOOT_DRIVER(andes_l2_cache) = {
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/4 riscv: andes: flush the L2 cache in flush_dcache_all()

In reply to Randolph Lin

flush_dcache_all() is expected to write back and invalidate every level of the data cache, but on Andes it only flushes L1. Flush L2 through the cache uclass as well.

Until the L2 cache device is probed, andes_l2_flush_all() returns without touching the hardware, as andes_l2_enable() and andes_l2_disable() already do.

flush_dcache_range() and invalidate_dcache_range() call flush_dcache_all(), so they now flush the whole L2 cache too. Flushing by physical address is left out to keep this change small.

Assisted-by Claude:claude-opus-5-5
---
 arch/riscv/cpu/andes/cache.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/riscv/cpu/andes/cache.c +4 -0
index bb57498d75a..30d766da8f9 100644
@@ -44,6 +44,10 @@ static void cache_ops(int (*ops)(struct udevice *dev))
 void flush_dcache_all(void)
 {
 	csr_write(CSR_UCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL);
+
+#ifdef CONFIG_ANDES_L2_CACHE
+	cache_ops(cache_flush_all);
+#endif
 }
 
 void flush_dcache_range(unsigned long start, unsigned long end)
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/4 riscv: andes: probe the L2 cache device in U-Boot proper

In reply to Randolph Lin

In U-Boot proper, enable_caches() runs from initr_caches(), before initr_dm() sets up driver model again, so it cannot find the L2 cache device and flush_dcache_all() never reaches L2. Without the I/O coherence port (IOCP), DMA masters do not snoop the CPU caches and can then read stale data from memory.

Call enable_caches() again from an EVT_DM_POST_INIT_R spy, unless SYS_DCACHE_OFF is set. In S-mode, U-Boot proper then drives the L2 controller itself, as the Linux ax45mp_cache driver does, and "dcache off" and "dcache on" turn L2 off and on. In M-mode U-Boot proper, L2 is now enabled, as enable_caches() intends.

Assisted-by Claude:claude-opus-5-5
---
 arch/riscv/cpu/andes/cache.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/riscv/cpu/andes/cache.c +15 -0
index 30d766da8f9..9a1ca950e11 100644
@@ -10,6 +10,7 @@
 #include <cpu_func.h>
 #include <dm.h>
 #include <dm/uclass-internal.h>
+#include <event.h>
 #include <asm/arch-andes/csr.h>
 
 #ifdef CONFIG_ANDES_L2_CACHE
@@ -30,6 +31,20 @@ void enable_caches(void)
 	}
 }
 
+/*
+ * In U-Boot proper, enable_caches() runs before driver model is set up
+ * again after relocation and cannot find the L2 cache device. Run it
+ * again once driver model is ready.
+ */
+static int andes_l2_init_r(void)
+{
+	if (!CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
+		enable_caches();
+
+	return 0;
+}
+EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_R, andes_l2_init_r);
+
 static void cache_ops(int (*ops)(struct udevice *dev))
 {
 	struct udevice *dev = NULL;
Signature · 1 line
2.34.1
Reviewed-by on the v1 series
#5 lore raw
In reply to Randolph Lin
Quoted 26 lines · Randolph Lin

On Andes, flush_dcache_all() only flushes L1, and flush_dcache_range() and invalidate_dcache_range() call it. Without the I/O coherence port (IOCP), DMA masters do not snoop the CPU caches, so dirty lines left in L2 make them read stale data.

This series adds a flush_all operation to the cache uclass, implements it in the Andes L2 cache driver and calls it from flush_dcache_all(). In U-Boot proper, enable_caches() runs before driver model is set up after relocation and cannot find the L2 cache device, so patch 4 runs it again once driver model is ready. U-Boot proper in S-mode then drives L2 itself, as the Linux ax45mp_cache driver does.

Side effects: each range operation flushes the whole L2 cache, "dcache off" and "dcache on" turn L2 off and on in S-mode, and L2 is now enabled in M-mode U-Boot proper.

checkpatch warns about the #ifdef in patch 3, which matches the rest of the file, and does not know the Assisted-by tag from Linux's Documentation/process/coding-assistants.rst.

Randolph Lin (4):
dm: cache: Add flush_all operation
cache: andes-l2: implement flush_all
riscv: andes: flush the L2 cache in flush_dcache_all()
riscv: andes: probe the L2 cache device in U-Boot proper

Quoted 5 lines
 arch/riscv/cpu/andes/cache.c   | 19 +++++++++++++++++++
 drivers/cache/cache-andes-l2.c | 16 ++++++++++++++--
 drivers/cache/cache-uclass.c   | 10 ++++++++++
 include/cache.h                | 16 ++++++++++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)

For the whole series,