Skip to contentSkip to conversation

riscv: implement Zicbom-based CMO instructions + the t-head variant

PATCH Superseded Heiko Stuebner 6 participants 18 messages Jun 10, 2022 – Jun 17, 2022

Subsystems (2)

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover riscv: implement Zicbom-based CMO instructions + the t-head variant

This series is based on the alternatives changes done in my svpbmt series and thus also depends on Atish's isa-extension parsing series.

It implements using the cache-management instructions from the Zicbom- extension to handle cache flush, etc actions on platforms needing them.

SoCs using cpu cores from T-Head like the Allwinne D1 implement a different set of cache instructions. But while they are different, instructions they provide the same functionality, so a variant can easly hook into the existing alternatives mechanism on those.

An ongoing discussion is about the currently used pre-coded instructions. Palmer's current thinking is that we should wait until the relevant instructions have landed in binutils.

On the other hand this would result in massive waiting times for all new instructions in the future as well, as it isn't only the time till a binutils patch is accepted but also then the wait for the next release and _after_ that the wait until these new releases are available in regular distributions.

So ideally it would be nice to find some sort of compromise in the middle somewhere.

The series sits on top of my svpbmt fixup series, which for example includes the conversion away from function pointers for the check-functions. And also uses my nops-series.

changes in v3:
- rebase onto 5.19-rc1 + svpbmt-fixup-series
- adapt wording for block-size binding
- include asm/cacheflush.h into dma-noncoherent to fix the no-prototype error clang seems to generate
- use __nops macro for readability
- add some received tags
- add a0 to the clobber list

changes in v2:
- cbom-block-size is hardware-specific and comes from firmware
- update Kconfig name to use the ISA extension name
- select the ALTERNATIVES symbol when enabled
- shorten the line lengths of the errata-assembly

Heiko Stuebner (3):
dt-bindings: riscv: document cbom-block-size
riscv: Implement Zicbom-based cache management operations
riscv: implement cache-management errata for T-Head SoCs

 .../devicetree/bindings/riscv/cpus.yaml       |  5 +
 arch/riscv/Kconfig                            | 15 +++
 arch/riscv/Kconfig.erratas                    | 10 ++
 arch/riscv/errata/thead/errata.c              | 15 +++
 arch/riscv/include/asm/cacheflush.h           |  6 ++
 arch/riscv/include/asm/errata_list.h          | 76 ++++++++++++++-
 arch/riscv/include/asm/hwcap.h                |  1 +
 arch/riscv/kernel/cpu.c                       |  1 +
 arch/riscv/kernel/cpufeature.c                | 18 ++++
 arch/riscv/kernel/setup.c                     |  2 +
 arch/riscv/mm/Makefile                        |  1 +
 arch/riscv/mm/dma-noncoherent.c               | 93 +++++++++++++++++++
 12 files changed, 241 insertions(+), 2 deletions(-)
 create mode 100644 arch/riscv/mm/dma-noncoherent.c
Signature · 1 line
2.35.1
#1 lore raw

PATCH v3 1/3 dt-bindings: riscv: document cbom-block-size

The Zicbom operates on a block-size defined for the cpu-core, which does not necessarily match other cache-sizes used.

So add the necessary property for the system to know the core's block-size.

---
 Documentation/devicetree/bindings/riscv/cpus.yaml | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/riscv/cpus.yaml +5 -0
index d632ac76532e..873dd12f6e89 100644
@@ -63,6 +63,11 @@ properties:
       - riscv,sv48
       - riscv,none
 
+  riscv,cbom-block-size:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description:
+      The blocksize in bytes for the Zicbom cache operations.
+
   riscv,isa:
     description:
       Identifies the specific RISC-V instruction set architecture
Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
maintainer Acked-by on v3 1/3
#2 lore raw

Re: [PATCH 1/3] dt-bindings: riscv: document cbom-block-size

Quoted 12 lines · Heiko Stuebner

The Zicbom operates on a block-size defined for the cpu-core, which does not necessarily match other cache-sizes used.

So add the necessary property for the system to know the core's block-size.

---
 Documentation/devicetree/bindings/riscv/cpus.yaml | 5 +++++
 1 file changed, 5 insertions(+)
#3 lore raw

PATCH v3 2/3 riscv: Implement Zicbom-based cache management operations

In reply to Heiko Stuebner

The Zicbom ISA-extension was ratified in november 2021 and introduces instructions for dcache invalidate, clean and flush operations.

Implement cache management operations based on them.

Of course not all cores will support this, so implement an alternative-based mechanism that replaces empty instructions with ones done around Zicbom instructions.

We're using prebuild instructions for the Zicbom instructions for now, to not require a bleeding-edge compiler (gcc-12) for these somewhat simple instructions.

---
 arch/riscv/Kconfig                   | 15 +++++
 arch/riscv/include/asm/cacheflush.h  |  6 ++
 arch/riscv/include/asm/errata_list.h | 36 ++++++++++-
 arch/riscv/include/asm/hwcap.h       |  1 +
 arch/riscv/kernel/cpu.c              |  1 +
 arch/riscv/kernel/cpufeature.c       | 18 ++++++
 arch/riscv/kernel/setup.c            |  2 +
 arch/riscv/mm/Makefile               |  1 +
 arch/riscv/mm/dma-noncoherent.c      | 93 ++++++++++++++++++++++++++++
 9 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/mm/dma-noncoherent.c
arch/riscv/Kconfig +15 -0
index 32ffef9f6e5b..384d0c15f2b6 100644
@@ -376,6 +376,21 @@ config RISCV_ISA_SVPBMT
 
 	   If you don't know what to do here, say Y.
 
+config RISCV_ISA_ZICBOM
+	bool "Zicbom extension support for non-coherent dma operation"
+	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select ARCH_HAS_SETUP_DMA_OPS
+	select DMA_DIRECT_REMAP
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZICBOM extension
+	   (Cache Block Management Operations) and enable its usage.
+
+	   If you don't know what to do here, say Y.
+
 config FPU
 	bool "FPU support"
 	default y
arch/riscv/include/asm/cacheflush.h +6 -0
index 23ff70350992..eb12d014b158 100644
@@ -42,6 +42,12 @@ void flush_icache_mm(struct mm_struct *mm, bool local);
 
 #endif /* CONFIG_SMP */
 
+#ifdef CONFIG_RISCV_ISA_ZICBOM
+void riscv_init_cbom_blocksize(void);
+#else
+static inline void riscv_init_cbom_blocksize(void) { }
+#endif
+
 /*
  * Bits in sys_riscv_flush_icache()'s flags argument.
  */
arch/riscv/include/asm/errata_list.h +35 -1
index 398e351e7002..2e80a75b5241 100644
@@ -20,7 +20,8 @@
 #endif
 
 #define	CPUFEATURE_SVPBMT 0
-#define	CPUFEATURE_NUMBER 1
+#define	CPUFEATURE_CMO 1
+#define	CPUFEATURE_NUMBER 2
 
 #ifdef __ASSEMBLY__
 
@@ -87,6 +88,39 @@ asm volatile(ALTERNATIVE(						\
 #define ALT_THEAD_PMA(_val)
 #endif
 
+/*
+ * cbo.clean rs1
+ * | 31 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *    0...01     rs1       010      00000  0001111
+ *
+ * cbo.flush rs1
+ * | 31 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *    0...10     rs1       010      00000  0001111
+ *
+ * cbo.inval rs1
+ * | 31 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *    0...00     rs1       010      00000  0001111
+ */
+#define CBO_INVAL_A0	".long 0x15200F"
+#define CBO_CLEAN_A0	".long 0x25200F"
+#define CBO_FLUSH_A0	".long 0x05200F"
+
+#define ALT_CMO_OP(_op, _start, _size, _cachesize)			\
+asm volatile(ALTERNATIVE(						\
+	__nops(5),							\
+	"mv a0, %1\n\t"							\
+	"j 2f\n\t"							\
+	"3:\n\t"							\
+	CBO_##_op##_A0 "\n\t"						\
+	"add a0, a0, %0\n\t"						\
+	"2:\n\t"							\
+	"bltu a0, %2, 3b\n\t", 0,					\
+		CPUFEATURE_CMO, CONFIG_RISCV_ISA_ZICBOM)		\
+	: : "r"(_cachesize),						\
+	    "r"(ALIGN((_start), (_cachesize))),				\
+	    "r"(ALIGN((_start) + (_size), (_cachesize)))		\
+	: "a0")
+
 #endif /* __ASSEMBLY__ */
 
 #endif
arch/riscv/include/asm/hwcap.h +1 -0
index 4e2486881840..6044e402003d 100644
@@ -53,6 +53,7 @@ extern unsigned long elf_hwcap;
 enum riscv_isa_ext_id {
 	RISCV_ISA_EXT_SSCOFPMF = RISCV_ISA_EXT_BASE,
 	RISCV_ISA_EXT_SVPBMT,
+	RISCV_ISA_EXT_ZICBOM,
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/kernel/cpu.c +1 -0
index fba9e9f46a8c..0365557f7122 100644
@@ -89,6 +89,7 @@ int riscv_of_parent_hartid(struct device_node *node)
 static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
 };
 
arch/riscv/kernel/cpufeature.c +18 -0
index 6a40cb8134bd..e956f4d763ec 100644
@@ -199,6 +199,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 			}
 #undef SET_ISA_EXT_MAP
 		}
@@ -259,6 +260,20 @@ static bool __init_or_module cpufeature_probe_svpbmt(unsigned int stage)
 	return false;
 }
 
+static bool __init_or_module cpufeature_probe_cmo(unsigned int stage)
+{
+#ifdef CONFIG_RISCV_ISA_ZICBOM
+	switch (stage) {
+	case RISCV_ALTERNATIVES_EARLY_BOOT:
+		return false;
+	default:
+		return riscv_isa_extension_available(NULL, ZICBOM);
+	}
+#endif
+
+	return false;
+}
+
 /*
  * Probe presence of individual extensions.
  *
@@ -273,6 +288,9 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
 	if (cpufeature_probe_svpbmt(stage))
 		cpu_req_feature |= (1U << CPUFEATURE_SVPBMT);
 
+	if (cpufeature_probe_cmo(stage))
+		cpu_req_feature |= (1U << CPUFEATURE_CMO);
+
 	return cpu_req_feature;
 }
 
arch/riscv/kernel/setup.c +2 -0
index f0f36a4a0e9b..95ef6e2bf45c 100644
@@ -22,6 +22,7 @@
 #include <linux/crash_dump.h>
 
 #include <asm/alternative.h>
+#include <asm/cacheflush.h>
 #include <asm/cpu_ops.h>
 #include <asm/early_ioremap.h>
 #include <asm/pgtable.h>
@@ -296,6 +297,7 @@ void __init setup_arch(char **cmdline_p)
 #endif
 
 	riscv_fill_hwcap();
+	riscv_init_cbom_blocksize();
 	apply_boot_alternatives();
 }
 
arch/riscv/mm/Makefile +1 -0
index ac7a25298a04..548f2f3c00e9 100644
@@ -30,3 +30,4 @@ endif
 endif
 
 obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
+obj-$(CONFIG_RISCV_ISA_ZICBOM) += dma-noncoherent.o
arch/riscv/mm/dma-noncoherent.c new file +93 -0

93 lines not on this page · raw message

Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#4 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Quoted 6 lines · Heiko Stuebner
arch/riscv/Kconfig +0 -0
index 32ffef9f6e5b..384d0c15f2b6 100644
@@ -376,6 +376,21 @@ config RISCV_ISA_SVPBMT
 

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 386

Quoted 10 lines
arch/riscv/Kconfig +8 -0
 	   If you don't know what to do here, say Y.
 
+config RISCV_ISA_ZICBOM
+	bool "Zicbom extension support for non-coherent dma operation"
+	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select ARCH_HAS_SETUP_DMA_OPS
+	select DMA_DIRECT_REMAP
+	select RISCV_ALTERNATIVE

Since RISCV_ALTERNATIVE depends on !XIP_KERNEL and since 'select' does not follow any dependency chains, this config also needs to depend on !XIP_KERNEL.

Quoted 6 lines
arch/riscv/Kconfig +6 -0
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZICBOM extension
+	   (Cache Block Management Operations) and enable its usage.
+
+	   If you don't know what to do here, say Y.
Signature · 1 line
~Randy
#5 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

In reply to Heiko Stuebner

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 389

Quoted 11 lines · Heiko Stuebner
arch/riscv/Kconfig +11 -0
+config RISCV_ISA_ZICBOM
+	bool "Zicbom extension support for non-coherent dma operation"
+	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select ARCH_HAS_SETUP_DMA_OPS
+	select DMA_DIRECT_REMAP
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZICBOM extension

Overly long line here.

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 392

Quoted 3 lines
arch/riscv/Kconfig +3 -0
+	   (Cache Block Management Operations) and enable its usage.
+
+	   If you don't know what to do here, say Y.

But more importantly I think the whole text here is not very helpful. What users care about is non-coherent DMA support. What extension is used for that is rather secondary. Also please capitalize DMA.

Quoted 5 lines
arch/riscv/mm/dma-noncoherent.c new file +5 -0
+void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
+{
+	switch (dir) {
+	case DMA_TO_DEVICE:
+		ALT_CMO_OP(CLEAN, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);

arch/riscv/mm/dma-noncoherent.c line 34

Quoted 10 lines
arch/riscv/mm/dma-noncoherent.c new file +10 -0
+		break;
+	case DMA_FROM_DEVICE:
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	case DMA_BIDIRECTIONAL:
+		ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	default:
+		break;
+	}

Pleae avoid all these crazy long lines. and use a logical variable for the virtual address. And why do you pass that virtual address as an unsigned long to ALT_CMO_OP? You're going to make your life much easier if you simply always pass a pointer.

Last but not last, does in RISC-V clean mean writeback and flush mean writeback plus invalidate? If so the code is correct, but the choice of names in the RISC-V spec is extremely unfortunate.

arch/riscv/mm/dma-noncoherent.c line 49

Quoted 13 lines
arch/riscv/mm/dma-noncoherent.c new file +13 -0
+void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
+{
+	switch (dir) {
+	case DMA_TO_DEVICE:
+		break;
+	case DMA_FROM_DEVICE:
+	case DMA_BIDIRECTIONAL:
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	default:
+		break;
+	}
+}

Same comment here and in few other places.

arch/riscv/mm/dma-noncoherent.c line 57

Quoted 8 lines
arch/riscv/mm/dma-noncoherent.c new file +8 -0
+
+void arch_dma_prep_coherent(struct page *page, size_t size)
+{
+	void *flush_addr = page_address(page);
+
+	memset(flush_addr, 0, size);
+	ALT_CMO_OP(FLUSH, (unsigned long)flush_addr, size, riscv_cbom_block_size);
+}

arch_dma_prep_coherent should never zero the memory, that is left for the upper layers.`

arch/riscv/mm/dma-noncoherent.c line 62

Quoted 4 lines
arch/riscv/mm/dma-noncoherent.c new file +4 -0
+void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
+		const struct iommu_ops *iommu, bool coherent)
+{
+	/* If a specific device is dma-coherent, set it here */

This comment isn't all that useful.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev->dma_coherent = coherent;
+}

But more importantly, this assums that once this code is built all devices are non-coherent by default. I.e. with this patch applied and the config option enabled we'll now suddenly start doing cache management operations or setups that didn't do it before.

#6 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Hi Christoph,

Am Freitag, 10. Juni 2022, 07:56:08 CEST schrieb Christoph Hellwig:

Quoted 6 lines · Heiko Stuebner
Quoted 5 lines
arch/riscv/Kconfig +5 -0
+config RISCV_ISA_ZICBOM
+	bool "Zicbom extension support for non-coherent dma operation"
+	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
Quoted 8 lines

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 389

Quoted 6 lines
arch/riscv/Kconfig +6 -0
+	select ARCH_HAS_SETUP_DMA_OPS
+	select DMA_DIRECT_REMAP
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZICBOM extension

Overly long line here.

fixed

Quoted 8 lines

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 392

Quoted 4 lines
arch/riscv/Kconfig +3 -0
+	   (Cache Block Management Operations) and enable its usage.
+
+	   If you don't know what to do here, say Y.

But more importantly I think the whole text here is not very helpful. What users care about is non-coherent DMA support. What extension is used for that is rather secondary.

I guess it might make sense to split that in some way.
I.e. Zicbom provides one implementation for handling non-coherence, the D1 uses different (but very similar) instructions while the SoC on the Beagle-V does something completely different.

So I guess it could make sense to have a general DMA_NONCOHERENT option and which gets selected by the relevant users.

This also fixes the issue that Zicbom needs a very new binutils but if beagle-v support happens that wouldn't need that.

Quoted 1 line

Also please capitalize DMA.

fixed

Quoted 12 lines
Quoted 12 lines
arch/riscv/mm/dma-noncoherent.c new file +12 -0
+void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
+{
+	switch (dir) {
+	case DMA_TO_DEVICE:
+		ALT_CMO_OP(CLEAN, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	case DMA_FROM_DEVICE:
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	case DMA_BIDIRECTIONAL:
+		ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
Quoted 8 lines

arch/riscv/mm/dma-noncoherent.c line 34

Quoted 3 lines
arch/riscv/mm/dma-noncoherent.c new file +3 -0
+	default:
+		break;
+	}

Pleae avoid all these crazy long lines. and use a logical variable for the virtual address. And why do you pass that virtual address as an unsigned long to ALT_CMO_OP? You're going to make your life much easier if you simply always pass a pointer.

fixed all of those.
And of course you're right, not having the cast when calling ALT_CMO_OP makes things definitly a lot nicer looking.

Quoted 3 lines

Last but not last, does in RISC-V clean mean writeback and flush mean writeback plus invalidate? If so the code is correct, but the choice of names in the RISC-V spec is extremely unfortunate.

clean:
makes data [...] visible to a set of non-coherent agents [...] by performing a write transfer of a copy of a cache block [...]

flush:
performs a clean followed by an invalidate

So that's a yes to your question

Quoted 7 lines
Quoted 7 lines
arch/riscv/mm/dma-noncoherent.c new file +7 -0
+void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
+{
+	switch (dir) {
+	case DMA_TO_DEVICE:
+		break;
+	case DMA_FROM_DEVICE:
+	case DMA_BIDIRECTIONAL:
Quoted 8 lines

arch/riscv/mm/dma-noncoherent.c line 35

Quoted 6 lines
arch/riscv/mm/dma-noncoherent.c new file +6 -0
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	default:
+		break;
+	}
+}

Same comment here and in few other places.

fixed

Quoted 11 lines

arch/riscv/mm/dma-noncoherent.c line 57

Quoted 8 lines
arch/riscv/mm/dma-noncoherent.c new file +8 -0
+
+void arch_dma_prep_coherent(struct page *page, size_t size)
+{
+	void *flush_addr = page_address(page);
+
+	memset(flush_addr, 0, size);
+	ALT_CMO_OP(FLUSH, (unsigned long)flush_addr, size, riscv_cbom_block_size);
+}

arch_dma_prep_coherent should never zero the memory, that is left for the upper layers.`

fixed

Quoted 6 lines

arch/riscv/mm/dma-noncoherent.c line 62

Quoted 4 lines
arch/riscv/mm/dma-noncoherent.c new file +4 -0
+void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
+		const struct iommu_ops *iommu, bool coherent)
+{
+	/* If a specific device is dma-coherent, set it here */

This comment isn't all that useful.

ok, I've dropped it

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	dev->dma_coherent = coherent;
+}

But more importantly, this assums that once this code is built all devices are non-coherent by default. I.e. with this patch applied and the config option enabled we'll now suddenly start doing cache management operations or setups that didn't do it before.

If I'm reading things correctly [0], the default for those functions is for those to be empty - but defined in the coherent case.

When you look at the definition of ALT_CMO_OP

#define ALT_CMO_OP(_op, _start, _size, _cachesize)                      \
asm volatile(ALTERNATIVE_2(                                             \
        __nops(6),                                                      \

you'll see that it's default variant is to do nothing and it doing any
non-coherency voodoo is only patched in if the Zicbom extension (or T-Head errata) is detected at runtime.

So in the coherent case (with the memset removed as you suggested), the arch_sync_dma_* and arch_dma_prep_coherent functions end up as something like

void arch_dma_prep_coherent(struct page *page, size_t size)

{
        void *flush_addr = page_address(page);

        nops(6);
}

which is very mich similar to the defaults [0] I guess, or am I overlooking something?

Thanks for taking the time for that review
Heiko

[0] https://elixir.bootlin.com/linux/latest/source/include/linux/dma-map-ops.h#L293

#7 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Quoted 2 lines · Heiko Stübner

If I'm reading things correctly [0], the default for those functions is for those to be empty - but defined in the coherent case.

That's not the point.

Zicbom is just an extension that allows the CPU to support managing cache state. Non-coherent DMA is just one of the use cases there are others like persistent memory. And when a CPU core supports Zicbom it might or might not have any non-coherent periphals. Or even some coherent and some non-coherent ones, something that is pretty common in arm/arm64 CPUs, where PCIe is usually cache coherent, but some other cheap periphals might not be.

That is why Linux ports require the plaform (usually through DT or ACPI) to mark which devices are coherent and which ones are not.

#8 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Hi,

Am Mittwoch, 15. Juni 2022, 19:49:10 CEST schrieb Christoph Hellwig:

Quoted 14 lines · Heiko Stübner
Quoted 2 lines

If I'm reading things correctly [0], the default for those functions is for those to be empty - but defined in the coherent case.

That's not the point.

Zicbom is just an extension that allows the CPU to support managing cache state. Non-coherent DMA is just one of the use cases there are others like persistent memory. And when a CPU core supports Zicbom it might or might not have any non-coherent periphals. Or even some coherent and some non-coherent ones, something that is pretty common in arm/arm64 CPUs, where PCIe is usually cache coherent, but some other cheap periphals might not be.

Quoted 3 lines

That is why Linux ports require the plaform (usually through DT or ACPI) to mark which devices are coherent and which ones are not.

I "get" it now I think. I was somewhat struggling what you were aiming at, but that was something of not seeing "the forest for the trees" on my part. And of course you were right in recognizing that issue :-) .

Without CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE and friends dev_is_dma_coherent() will always return true otherwise the dma_coherent attribute. Hence the "coherent" value for every system not managing things will suddenly show as non-coherent where it showed as coherent before.

As we already have detection-points for non-coherent systems (zicbom detection, t-head errata detection) I guess just also switching some boolean might solve that, so that arch_setup_dma_ops() will set the dma_coherent attribute to true always except when some non-coherent system is detected.

void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
const struct iommu_ops *iommu, bool coherent)

{
        /* only track coherent attributes, if cache-management is available */
        if (enable_noncoherency)
                dev->dma_coherent = coherent;
        else
                dev->dma_coherent = true;
}

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#9 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Quoted 4 lines · Heiko Stübner

Without CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE and friends dev_is_dma_coherent() will always return true otherwise the dma_coherent attribute. Hence the "coherent" value for every system not managing things will suddenly show as non-coherent where it showed as coherent before.

Yes.

Quoted 2 lines

As we already have detection-points for non-coherent systems (zicbom detection, t-head errata detection)

No, we don't. There are plenty of reasons to support Zicbom without every having any non-coherent DMA periphals. Or just some non-coherent ones. So that alone is not a good indicator at all.

The proper interface for that in DT-based setups i of_dma_is_coherent(), which looks at the dma-coherent DT property. And given that RISC-V started out as all coherent we need to follow the powerpc way (CONFIG_OF_DMA_DEFAULT_COHERENT) and default to coherent with an explcit propery for non-coherent devices, and not the arm/arm64 way where non-coherent is the default and coherent devices need the property.

#10 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Am Donnerstag, 16. Juni 2022, 13:53:42 CEST schrieb Christoph Hellwig:

Quoted 15 lines · Heiko Stübner
Quoted 4 lines

Without CONFIG_ARCH_HAS_SYNC_DMA_FOR_DEVICE and friends dev_is_dma_coherent() will always return true otherwise the dma_coherent attribute. Hence the "coherent" value for every system not managing things will suddenly show as non-coherent where it showed as coherent before.

Yes.

Quoted 2 lines

As we already have detection-points for non-coherent systems (zicbom detection, t-head errata detection)

No, we don't. There are plenty of reasons to support Zicbom without every having any non-coherent DMA periphals. Or just some non-coherent ones. So that alone is not a good indicator at all.

Quoted 6 lines

The proper interface for that in DT-based setups i of_dma_is_coherent(), which looks at the dma-coherent DT property. And given that RISC-V started out as all coherent we need to follow the powerpc way (CONFIG_OF_DMA_DEFAULT_COHERENT) and default to coherent with an explcit propery for non-coherent devices, and not the arm/arm64 way where non-coherent is the default and coherent devices need the property.

I did look at the dma-coherent-property -> of_dma_is_coherent()

	 -> of_dma_configure_id() -> arch_setup_dma_ops() chain yesterday

which setups the value dev_is_dma_coherent() returns.

The Zicbom extension will only be in new platforms, so none of the currently supported ones will have that. So my thinking was that we can default to true in arch_setup_dma_ops() and only use the read coherency value when actual cache-managment-operations are defined.

My guess was that new platforms implementing cache-management will want to be non-coherent by default?

So if the kernel is running on a platform not implementing cache-management dev_is_dma_coherent() would always return true as it does now, while on a new platform implementing cache-management we'd default to non-coherent and expect that new devicetree/firmware to specify the coherent devices.

#11 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Quoted 2 lines · Heiko Stübner

My guess was that new platforms implementing cache-management will want to be non-coherent by default?

No. Cache incoherent DMA is absolutely horrible and almost impossible to get right for the corner cases. It is a cost cutting measure seen on cheap SOCs and mostly avoided for more enterprise grade products.

#12 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Am Donnerstag, 16. Juni 2022, 14:11:57 CEST schrieb Christoph Hellwig:

Quoted 7 lines · Heiko Stübner
Quoted 2 lines

My guess was that new platforms implementing cache-management will want to be non-coherent by default?

No. Cache incoherent DMA is absolutely horrible and almost impossible to get right for the corner cases. It is a cost cutting measure seen on cheap SOCs and mostly avoided for more enterprise grade products.

ok, then we'll do it the other way around as suggested :-) . Coherent by default and marking the non-coherent parts.

DT people on IRC yesterday were also open to adding a dma-noncoherent property for that case.

Heiko

#13 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

In reply to Heiko Stuebner
Quoted 38 lines · Heiko Stuebner

The Zicbom ISA-extension was ratified in november 2021 and introduces instructions for dcache invalidate, clean and flush operations.

Implement cache management operations based on them.

Of course not all cores will support this, so implement an alternative-based mechanism that replaces empty instructions with ones done around Zicbom instructions.

We're using prebuild instructions for the Zicbom instructions for now, to not require a bleeding-edge compiler (gcc-12) for these somewhat simple instructions.

---
 arch/riscv/Kconfig                   | 15 +++++
 arch/riscv/include/asm/cacheflush.h  |  6 ++
 arch/riscv/include/asm/errata_list.h | 36 ++++++++++-
 arch/riscv/include/asm/hwcap.h       |  1 +
 arch/riscv/kernel/cpu.c              |  1 +
 arch/riscv/kernel/cpufeature.c       | 18 ++++++
 arch/riscv/kernel/setup.c            |  2 +
 arch/riscv/mm/Makefile               |  1 +
 arch/riscv/mm/dma-noncoherent.c      | 93 ++++++++++++++++++++++++++++
 9 files changed, 172 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/mm/dma-noncoherent.c
arch/riscv/Kconfig +0 -0
index 32ffef9f6e5b..384d0c15f2b6 100644

arch/riscv/Kconfig config RISCV_ISA_SVPBMT · line 384

Quoted 9 lines
arch/riscv/Kconfig +6 -0
@@ -376,6 +376,21 @@ config RISCV_ISA_SVPBMT
 
 	   If you don't know what to do here, say Y.
 
+config RISCV_ISA_ZICBOM
+	bool "Zicbom extension support for non-coherent dma operation"
+	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select ARCH_HAS_SETUP_DMA_OPS

ARCH_HAS_SETUP_DMA_OPS needs to be separate from the non-coherent DMA option, because iommu_setup_dma_ops() will need to be called from arch_setup_dma_ops() even on a fully-coherent system. (But this change is not strictly necessary for this series.)

Quoted 34 lines
arch/riscv/Kconfig +9 -0
+	select DMA_DIRECT_REMAP
+	select RISCV_ALTERNATIVE
+	default y
+	help
+	   Adds support to dynamically detect the presence of the ZICBOM extension
+	   (Cache Block Management Operations) and enable its usage.
+
+	   If you don't know what to do here, say Y.
+
 config FPU
 	bool "FPU support"
 	default y
arch/riscv/include/asm/cacheflush.h +6 -0
index 23ff70350992..eb12d014b158 100644
@@ -42,6 +42,12 @@ void flush_icache_mm(struct mm_struct *mm, bool local);
 
 #endif /* CONFIG_SMP */
 
+#ifdef CONFIG_RISCV_ISA_ZICBOM
+void riscv_init_cbom_blocksize(void);
+#else
+static inline void riscv_init_cbom_blocksize(void) { }
+#endif
+
 /*
  * Bits in sys_riscv_flush_icache()'s flags argument.
  */
arch/riscv/include/asm/errata_list.h +0 -0
index 398e351e7002..2e80a75b5241 100644

arch/riscv/include/asm/errata_list.h line 23

Quoted 5 lines
arch/riscv/include/asm/errata_list.h +1 -1
@@ -20,7 +20,8 @@
 #endif
 
 #define	CPUFEATURE_SVPBMT 0
-#define	CPUFEATURE_NUMBER 1
+#define	CPUFEATURE_CMO 1

This seems like it should be CPUFEATURE_ZICBOM for consistency.

Quoted 29 lines
arch/riscv/include/asm/errata_list.h +22 -0
+#define	CPUFEATURE_NUMBER 2
 
 #ifdef __ASSEMBLY__
 
@@ -87,6 +88,39 @@ asm volatile(ALTERNATIVE(						\
 #define ALT_THEAD_PMA(_val)
 #endif
 
+/*
+ * cbo.clean rs1
+ * | 31 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *    0...01     rs1       010      00000  0001111
+ *
+ * cbo.flush rs1
+ * | 31 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *    0...10     rs1       010      00000  0001111
+ *
+ * cbo.inval rs1
+ * | 31 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *    0...00     rs1       010      00000  0001111
+ */
+#define CBO_INVAL_A0	".long 0x15200F"
+#define CBO_CLEAN_A0	".long 0x25200F"
+#define CBO_FLUSH_A0	".long 0x05200F"
+
+#define ALT_CMO_OP(_op, _start, _size, _cachesize)			\
+asm volatile(ALTERNATIVE(						\
+	__nops(5),							\
+	"mv a0, %1\n\t"							\

arch/riscv/include/asm/errata_list.h asm volatile(ALTERNATIVE( \ · line 121

Quoted 10 lines
arch/riscv/include/asm/errata_list.h +10 -0
+	"j 2f\n\t"							\
+	"3:\n\t"							\
+	CBO_##_op##_A0 "\n\t"						\
+	"add a0, a0, %0\n\t"						\
+	"2:\n\t"							\
+	"bltu a0, %2, 3b\n\t", 0,					\
+		CPUFEATURE_CMO, CONFIG_RISCV_ISA_ZICBOM)		\
+	: : "r"(_cachesize),						\
+	    "r"(ALIGN((_start), (_cachesize))),				\
+	    "r"(ALIGN((_start) + (_size), (_cachesize)))		\

Here, the starting address needs to be truncated, not aligned upward. Otherwise, the first partial cache line will be missed. And since the starting address is known to be a multiple of the cache line size, the ending address does not need to be rounded up either:

-           "r"(ALIGN((_start), (_cachesize))),          \
-           "r"(ALIGN((_start) + (_size), (_cachesize))) \
+           "r"((_start) & ~((_cachesize) - 1UL)),       \
+           "r"((_start) + (_size))                      \

Then to prevent causing any collateral damage, you need to make sure slab allocations can not share a cache block:

arch/riscv/include/asm/cache.h +2 -0
@@ -11,6 +11,8 @@
 
 #define L1_CACHE_BYTES         (1 << L1_CACHE_SHIFT)
 
+#define ARCH_DMA_MINALIGN      L1_CACHE_BYTES
+
 /*
  * RISC-V requires the stack pointer to be 16-byte aligned, so ensure that
  * the flat loader aligns it accordingly.
Quoted 125 lines
arch/riscv/include/asm/errata_list.h +2 -0
+	: "a0")
+
 #endif /* __ASSEMBLY__ */
 
 #endif
arch/riscv/include/asm/hwcap.h +1 -0
index 4e2486881840..6044e402003d 100644
@@ -53,6 +53,7 @@ extern unsigned long elf_hwcap;
 enum riscv_isa_ext_id {
 	RISCV_ISA_EXT_SSCOFPMF = RISCV_ISA_EXT_BASE,
 	RISCV_ISA_EXT_SVPBMT,
+	RISCV_ISA_EXT_ZICBOM,
 	RISCV_ISA_EXT_ID_MAX = RISCV_ISA_EXT_MAX,
 };
 
arch/riscv/kernel/cpu.c +1 -0
index fba9e9f46a8c..0365557f7122 100644
@@ -89,6 +89,7 @@ int riscv_of_parent_hartid(struct device_node *node)
 static struct riscv_isa_ext_data isa_ext_arr[] = {
 	__RISCV_ISA_EXT_DATA(sscofpmf, RISCV_ISA_EXT_SSCOFPMF),
 	__RISCV_ISA_EXT_DATA(svpbmt, RISCV_ISA_EXT_SVPBMT),
+	__RISCV_ISA_EXT_DATA(zicbom, RISCV_ISA_EXT_ZICBOM),
 	__RISCV_ISA_EXT_DATA("", RISCV_ISA_EXT_MAX),
 };
 
arch/riscv/kernel/cpufeature.c +18 -0
index 6a40cb8134bd..e956f4d763ec 100644
@@ -199,6 +199,7 @@ void __init riscv_fill_hwcap(void)
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
+				SET_ISA_EXT_MAP("zicbom", RISCV_ISA_EXT_ZICBOM);
 			}
 #undef SET_ISA_EXT_MAP
 		}
@@ -259,6 +260,20 @@ static bool __init_or_module cpufeature_probe_svpbmt(unsigned int stage)
 	return false;
 }
 
+static bool __init_or_module cpufeature_probe_cmo(unsigned int stage)
+{
+#ifdef CONFIG_RISCV_ISA_ZICBOM
+	switch (stage) {
+	case RISCV_ALTERNATIVES_EARLY_BOOT:
+		return false;
+	default:
+		return riscv_isa_extension_available(NULL, ZICBOM);
+	}
+#endif
+
+	return false;
+}
+
 /*
  * Probe presence of individual extensions.
  *
@@ -273,6 +288,9 @@ static u32 __init_or_module cpufeature_probe(unsigned int stage)
 	if (cpufeature_probe_svpbmt(stage))
 		cpu_req_feature |= (1U << CPUFEATURE_SVPBMT);
 
+	if (cpufeature_probe_cmo(stage))
+		cpu_req_feature |= (1U << CPUFEATURE_CMO);
+
 	return cpu_req_feature;
 }
 
arch/riscv/kernel/setup.c +2 -0
index f0f36a4a0e9b..95ef6e2bf45c 100644
@@ -22,6 +22,7 @@
 #include <linux/crash_dump.h>
 
 #include <asm/alternative.h>
+#include <asm/cacheflush.h>
 #include <asm/cpu_ops.h>
 #include <asm/early_ioremap.h>
 #include <asm/pgtable.h>
@@ -296,6 +297,7 @@ void __init setup_arch(char **cmdline_p)
 #endif
 
 	riscv_fill_hwcap();
+	riscv_init_cbom_blocksize();
 	apply_boot_alternatives();
 }
 
arch/riscv/mm/Makefile +1 -0
index ac7a25298a04..548f2f3c00e9 100644
@@ -30,3 +30,4 @@ endif
 endif
 
 obj-$(CONFIG_DEBUG_VIRTUAL) += physaddr.o
+obj-$(CONFIG_RISCV_ISA_ZICBOM) += dma-noncoherent.o
arch/riscv/mm/dma-noncoherent.c new file +18 -0
new file mode 100644
index 000000000000..f77f4c529835
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * RISC-V specific functions to support DMA for non-coherent devices
+ *
+ * Copyright (c) 2021 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/dma-direct.h>
+#include <linux/dma-map-ops.h>
+#include <linux/init.h>
+#include <linux/io.h>
+#include <linux/libfdt.h>
+#include <linux/mm.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <asm/cacheflush.h>
+
+static unsigned int riscv_cbom_block_size = L1_CACHE_BYTES;

arch/riscv/mm/dma-noncoherent.c line 28

Quoted 10 lines
arch/riscv/mm/dma-noncoherent.c new file +10 -0
+
+void arch_sync_dma_for_device(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
+{
+	switch (dir) {
+	case DMA_TO_DEVICE:
+		ALT_CMO_OP(CLEAN, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	case DMA_FROM_DEVICE:
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;

arch_sync_dma_for_device(DMA_FROM_DEVICE) is a no-op from the CPU's perspective. Invalidating the CPU's cache goes in arch_sync_dma_for_cpu(DMA_FROM_DEVICE).

Quoted 6 lines
arch/riscv/mm/dma-noncoherent.c new file +6 -0
+	case DMA_BIDIRECTIONAL:
+		ALT_CMO_OP(FLUSH, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;
+	default:
+		break;
+	}

arch/riscv/mm/dma-noncoherent.c line 44

Quoted 10 lines
arch/riscv/mm/dma-noncoherent.c new file +10 -0
+}
+
+void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, enum dma_data_direction dir)
+{
+	switch (dir) {
+	case DMA_TO_DEVICE:
+		break;
+	case DMA_FROM_DEVICE:
+	case DMA_BIDIRECTIONAL:
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);

For arch_sync_dma_for_cpu(DMA_BIDIRECTIONAL), we expect the CPU to have written to the buffer, so this should flush, not invalidate.

Regards,
Samuel

Quoted 50 lines
arch/riscv/mm/dma-noncoherent.c new file +49 -0
+		break;
+	default:
+		break;
+	}
+}
+
+void arch_dma_prep_coherent(struct page *page, size_t size)
+{
+	void *flush_addr = page_address(page);
+
+	memset(flush_addr, 0, size);
+	ALT_CMO_OP(FLUSH, (unsigned long)flush_addr, size, riscv_cbom_block_size);
+}
+
+void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
+		const struct iommu_ops *iommu, bool coherent)
+{
+	/* If a specific device is dma-coherent, set it here */
+	dev->dma_coherent = coherent;
+}
+
+void riscv_init_cbom_blocksize(void)
+{
+	struct device_node *node;
+	int ret;
+	u32 val;
+
+	for_each_of_cpu_node(node) {
+		int hartid = riscv_of_processor_hartid(node);
+		int cbom_hartid;
+
+		if (hartid < 0)
+			continue;
+
+		/* set block-size for cbom extension if available */
+		ret = of_property_read_u32(node, "riscv,cbom-block-size", &val);
+		if (ret)
+			continue;
+
+		if (!riscv_cbom_block_size) {
+			riscv_cbom_block_size = val;
+			cbom_hartid = hartid;
+		} else {
+			if (riscv_cbom_block_size != val)
+				pr_warn("cbom-block-size mismatched between harts %d and %d\n",
+					cbom_hartid, hartid);
+		}
+	}
+}
#14 lore raw

Re: [PATCH 2/3] riscv: Implement Zicbom-based cache management operations

Quoted 11 lines · Samuel Holland

Review

Quoted 6 lines
(quoted diff) +6 -0
+config RISCV_ISA_ZICBOM
+	bool "Zicbom extension support for non-coherent dma operation"
+	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select ARCH_HAS_SETUP_DMA_OPS

ARCH_HAS_SETUP_DMA_OPS needs to be separate from the non-coherent DMA option, because iommu_setup_dma_ops() will need to be called from arch_setup_dma_ops() even on a fully-coherent system. (But this change is not strictly necessary for this series.)

It doesn't need to be separate, you can just add another select for the symbol.

Quoted 6 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+	case DMA_FROM_DEVICE:
+		ALT_CMO_OP(INVAL, (unsigned long)phys_to_virt(paddr), size, riscv_cbom_block_size);
+		break;

arch_sync_dma_for_device(DMA_FROM_DEVICE) is a no-op from the CPU's perspective. Invalidating the CPU's cache goes in arch_sync_dma_for_cpu(DMA_FROM_DEVICE).

Only if you guarantee that there is never any speculation. See:

https://lore.kernel.org/lkml/[email protected]

#15 lore raw

PATCH v3 3/3 riscv: implement cache-management errata for T-Head SoCs

In reply to Heiko Stuebner

The T-Head C906 and C910 implement a scheme for handling cache operations different from the generic Zicbom extension.

Add an errata for it next to the generic dma coherency ops.

---
 arch/riscv/Kconfig.erratas           | 10 ++++++
 arch/riscv/errata/thead/errata.c     | 15 +++++++++
 arch/riscv/include/asm/errata_list.h | 48 +++++++++++++++++++++++++---
 3 files changed, 68 insertions(+), 5 deletions(-)
arch/riscv/Kconfig.erratas +10 -0
index 457ac72c9b36..4a390035abc7 100644
@@ -55,4 +55,14 @@ config ERRATA_THEAD_PBMT
 
 	  If you don't know what to do here, say "Y".
 
+config ERRATA_THEAD_CMO
+	bool "Apply T-Head cache management errata"
+	depends on ERRATA_THEAD && RISCV_ISA_ZICBOM
+	default y
+	help
+	  This will apply the cache management errata to handle the
+	  non-standard handling on non-coherent operations on T-Head SoCs.
+
+	  If you don't know what to do here, say "Y".
+
 endmenu
arch/riscv/errata/thead/errata.c +15 -0
index b37b6fedd53b..a3b3612d19df 100644
@@ -27,6 +27,18 @@ static bool errata_probe_pbmt(unsigned int stage,
 	return false;
 }
 
+static bool errata_probe_cmo(unsigned int stage,
+			     unsigned long arch_id, unsigned long impid)
+{
+	if (arch_id != 0 || impid != 0)
+		return false;
+
+	if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+		return false;
+
+	return true;
+}
+
 static u32 thead_errata_probe(unsigned int stage,
 			      unsigned long archid, unsigned long impid)
 {
@@ -35,6 +47,9 @@ static u32 thead_errata_probe(unsigned int stage,
 	if (errata_probe_pbmt(stage, archid, impid))
 		cpu_req_errata |= (1U << ERRATA_THEAD_PBMT);
 
+	if (errata_probe_cmo(stage, archid, impid))
+		 cpu_req_errata |= (1U << ERRATA_THEAD_CMO);
+
 	return cpu_req_errata;
 }
 
arch/riscv/include/asm/errata_list.h +43 -5

67 lines not on this page · raw message

Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Reviewed-by on v3 3/3
#16 lore raw

Re: [PATCH 3/3] riscv: implement cache-management errata for T-Head SoCs

Quoted 143 lines · Heiko Stuebner

The T-Head C906 and C910 implement a scheme for handling cache operations different from the generic Zicbom extension.

Add an errata for it next to the generic dma coherency ops.

---
 arch/riscv/Kconfig.erratas           | 10 ++++++
 arch/riscv/errata/thead/errata.c     | 15 +++++++++
 arch/riscv/include/asm/errata_list.h | 48 +++++++++++++++++++++++++---
 3 files changed, 68 insertions(+), 5 deletions(-)
arch/riscv/Kconfig.erratas +10 -0
index 457ac72c9b36..4a390035abc7 100644
@@ -55,4 +55,14 @@ config ERRATA_THEAD_PBMT
 
          If you don't know what to do here, say "Y".
 
+config ERRATA_THEAD_CMO
+       bool "Apply T-Head cache management errata"
+       depends on ERRATA_THEAD && RISCV_ISA_ZICBOM
+       default y
+       help
+         This will apply the cache management errata to handle the
+         non-standard handling on non-coherent operations on T-Head SoCs.
+
+         If you don't know what to do here, say "Y".
+
 endmenu
arch/riscv/errata/thead/errata.c +15 -0
index b37b6fedd53b..a3b3612d19df 100644
@@ -27,6 +27,18 @@ static bool errata_probe_pbmt(unsigned int stage,
        return false;
 }
 
+static bool errata_probe_cmo(unsigned int stage,
+                            unsigned long arch_id, unsigned long impid)
+{
+       if (arch_id != 0 || impid != 0)
+               return false;
+
+       if (stage == RISCV_ALTERNATIVES_EARLY_BOOT)
+               return false;
+
+       return true;
+}
+
 static u32 thead_errata_probe(unsigned int stage,
                              unsigned long archid, unsigned long impid)
 {
@@ -35,6 +47,9 @@ static u32 thead_errata_probe(unsigned int stage,
        if (errata_probe_pbmt(stage, archid, impid))
                cpu_req_errata |= (1U << ERRATA_THEAD_PBMT);
 
+       if (errata_probe_cmo(stage, archid, impid))
+                cpu_req_errata |= (1U << ERRATA_THEAD_CMO);
+
        return cpu_req_errata;
 }
 
arch/riscv/include/asm/errata_list.h +43 -6
index 2e80a75b5241..a48605d55c3c 100644
@@ -16,7 +16,8 @@
 
 #ifdef CONFIG_ERRATA_THEAD
 #define        ERRATA_THEAD_PBMT 0
-#define        ERRATA_THEAD_NUMBER 1
+#define        ERRATA_THEAD_CMO 1
+#define        ERRATA_THEAD_NUMBER 2
 #endif
 
 #define        CPUFEATURE_SVPBMT 0
@@ -105,17 +106,54 @@ asm volatile(ALTERNATIVE(                                         \
 #define CBO_CLEAN_A0   ".long 0x25200F"
 #define CBO_FLUSH_A0   ".long 0x05200F"
 
+/*
+ * dcache.ipa rs1 (invalidate, physical address)
+ * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *   0000001    01010      rs1       000      00000  0001011
+ * dache.iva rs1 (invalida, virtual address)
+ *   0000001    00110      rs1       000      00000  0001011
+ *
+ * dcache.cpa rs1 (clean, physical address)
+ * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *   0000001    01001      rs1       000      00000  0001011
+ * dcache.cva rs1 (clean, virtual address)
+ *   0000001    00100      rs1       000      00000  0001011
+ *
+ * dcache.cipa rs1 (clean then invalidate, physical address)
+ * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *   0000001    01011      rs1       000      00000  0001011
+ * dcache.civa rs1 (... virtual address)
+ *   0000001    00111      rs1       000      00000  0001011
+ *
+ * sync.s (make sure all cache operations finished)
+ * | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
+ *   0000000    11001     00000      000      00000  0001011
+ */
+#define THEAD_INVAL_A0 ".long 0x0265000b"
+#define THEAD_CLEAN_A0 ".long 0x0245000b"
+#define THEAD_FLUSH_A0 ".long 0x0275000b"
+#define THEAD_SYNC_S   ".long 0x0190000b"
+
 #define ALT_CMO_OP(_op, _start, _size, _cachesize)                     \
-asm volatile(ALTERNATIVE(                                              \
-       __nops(5),                                                      \
+asm volatile(ALTERNATIVE_2(                                            \
+       __nops(6),                                                      \
        "mv a0, %1\n\t"                                                 \
        "j 2f\n\t"                                                      \
        "3:\n\t"                                                        \
        CBO_##_op##_A0 "\n\t"                                           \
        "add a0, a0, %0\n\t"                                            \
        "2:\n\t"                                                        \
-       "bltu a0, %2, 3b\n\t", 0,                                       \
-               CPUFEATURE_CMO, CONFIG_RISCV_ISA_ZICBOM)                \
+       "bltu a0, %2, 3b\n\t"                                           \
+       "nop", 0, CPUFEATURE_CMO, CONFIG_RISCV_ISA_ZICBOM,              \
+       "mv a0, %1\n\t"                                                 \
+       "j 2f\n\t"                                                      \
+       "3:\n\t"                                                        \
+       THEAD_##_op##_A0 "\n\t"                                         \
+       "add a0, a0, %0\n\t"                                            \
+       "2:\n\t"                                                        \
+       "bltu a0, %2, 3b\n\t"                                           \
+       THEAD_SYNC_S, THEAD_VENDOR_ID,                                  \
+                       ERRATA_THEAD_CMO, CONFIG_ERRATA_THEAD_CMO)      \
        : : "r"(_cachesize),                                            \
            "r"(ALIGN((_start), (_cachesize))),                         \
            "r"(ALIGN((_start) + (_size), (_cachesize)))                \
--

2.35.1

Signature · 4 lines
Best Regards
 Guo Ren

ML: https://lore.kernel.org/linux-csky/
#17 lore raw

Re: [PATCH 3/3] riscv: implement cache-management errata for T-Head SoCs

In reply to Heiko Stuebner
Quoted 6 lines · Heiko Stuebner

The T-Head C906 and C910 implement a scheme for handling cache operations different from the generic Zicbom extension.

Add an errata for it next to the generic dma coherency ops.

v3 breaks Ethernet on D1, because the packet tx buffers are not aligned to the cache block size. See my comments on patch 2. With the changes suggested there, all functionality is again working.

Please drop this tag until I have a chance to test the next version.

Regards,
Samuel