Skip to contentSkip to conversation

openrisc: Add cacheinfo support and introduce new utility functions

PATCH Superseded Sahil Siddiq 2 participants 6 messages Mar 28, 2025 – Mar 29, 2025 Openrisc architecture

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover openrisc: Add cacheinfo support and introduce new utility functions

Hi,

The main purpose of this series is to expose CPU cache attributes for OpenRISC in sysfs using the cacheinfo API. The core implementation to achieve this is in patch #3. Patch #1 and #2 add certain enhancements to simplify the implementation of cacheinfo support.

Patch #1 removes duplication of cache-related data members in struct cpuinfo_or1k.

Patch #2 introduces several utility functions. One set of functions is used to check if the cache components and SPRs exist before attempting to use them. The other set provides a convenient interface to flush or invalidate a range of cache blocks.

This version addresses review comments posted in response to v3. In commit #2, I chose not to make "cache_loop_page()" inline after reading point 15 in the coding style doc [1]. Let me know if it should be made inline.

Thanks,
Sahil

[1] https://www.kernel.org/doc/html/latest/process/coding-style.html

Sahil Siddiq (3):
openrisc: Refactor struct cpuinfo_or1k to reduce duplication
openrisc: Introduce new utility functions to flush and invalidate
caches
openrisc: Add cacheinfo support

 arch/openrisc/include/asm/cacheflush.h |  17 ++++
 arch/openrisc/include/asm/cpuinfo.h    |  31 ++++++--
 arch/openrisc/kernel/Makefile          |   2 +-
 arch/openrisc/kernel/cacheinfo.c       | 104 +++++++++++++++++++++++++
 arch/openrisc/kernel/dma.c             |  18 +----
 arch/openrisc/kernel/setup.c           |  45 +----------
 arch/openrisc/mm/cache.c               |  87 +++++++++++++++++++--
 arch/openrisc/mm/init.c                |   5 +-
 8 files changed, 235 insertions(+), 74 deletions(-)
 create mode 100644 arch/openrisc/kernel/cacheinfo.c

base-commit: ea1413e5b53a8dd4fa7675edb23cdf828bbdce1e

Signature · 1 line
2.48.1
#1 lore raw

PATCH v4 1/3 openrisc: Refactor struct cpuinfo_or1k to reduce duplication

The "cpuinfo_or1k" structure currently has identical data members for different cache components.

Remove these fields out of struct cpuinfo_or1k and into its own struct. This reduces duplication while keeping cpuinfo_or1k extensible so more cache descriptors can be added in the future.

Also add a new field "sets" to the new structure.

---
No changes from v3 -> v4.

Changes from v1/v2 -> v3:
- arch/openrisc/kernel/setup.c:
(print_cpuinfo):
1. Cascade changes made to struct cpuinfo_or1k.
2. These lines are ultimately shifted to the new file created in

     patch #3.

(setup_cpuinfo): Likewise.
(show_cpuinfo): Likewise.

 arch/openrisc/include/asm/cpuinfo.h | 16 +++++-----
 arch/openrisc/kernel/setup.c        | 45 ++++++++++++++---------------
 2 files changed, 31 insertions(+), 30 deletions(-)
arch/openrisc/include/asm/cpuinfo.h +9 -7
index 5e4744153d0e..82f5d4c06314 100644
@@ -15,16 +15,18 @@
 #ifndef __ASM_OPENRISC_CPUINFO_H
 #define __ASM_OPENRISC_CPUINFO_H
 
+struct cache_desc {
+	u32 size;
+	u32 sets;
+	u32 block_size;
+	u32 ways;
+};
+
 struct cpuinfo_or1k {
 	u32 clock_frequency;
 
-	u32 icache_size;
-	u32 icache_block_size;
-	u32 icache_ways;
-
-	u32 dcache_size;
-	u32 dcache_block_size;
-	u32 dcache_ways;
+	struct cache_desc icache;
+	struct cache_desc dcache;
 
 	u16 coreid;
 };
arch/openrisc/kernel/setup.c +22 -23

77 lines not on this page · raw message

Signature · 1 line
2.48.1
#2 lore raw

PATCH v4 2/3 openrisc: Introduce new utility functions to flush and invalidate caches

In reply to Sahil Siddiq

According to the OpenRISC architecture manual, the dcache and icache may not be present. When these caches are present, the invalidate and flush registers may be absent. The current implementation does not perform checks to verify their presence before utilizing cache registers, or invalidating and flushing cache blocks.

Introduce new functions to detect the presence of cache components and related special-purpose registers.

There are a few places where a range of addresses have to be flushed or invalidated and the implementation is duplicated. Introduce new utility functions and macros that generalize this implementation and reduce duplication.

---
Changes from v3 -> v4:
- arch/openrisc/include/asm/cpuinfo.h: Move new definitions to cache.c.
- arch/openrisc/mm/cache.c:
(cache_loop): Split function.
(cache_loop_page): New function.
(cpu_cache_is_present): Move definition here. (cb_inv_flush_is_implemented): Move definition here.

Changes from v2 -> v3:
- arch/openrisc/include/asm/cacheflush.h: Declare new functions and macros.
- arch/openrisc/include/asm/cpuinfo.h: Implement new functions. (cpu_cache_is_present):
1. The implementation of this function was strewn all over the place in the previous versions.
2. Fix condition. The condition in the previous version was incorrect. (cb_inv_flush_is_implemented): New function.
- arch/openrisc/kernel/dma.c: Use new functions.
- arch/openrisc/mm/cache.c:
(cache_loop): Extend function.
(local_*_page_*): Use new cache_loop interface. (local_*_range_*): Implement new functions.
- arch/openrisc/mm/init.c: Use new functions.

 arch/openrisc/include/asm/cacheflush.h | 17 +++++
 arch/openrisc/include/asm/cpuinfo.h    | 15 +++++
 arch/openrisc/kernel/dma.c             | 18 ++----
 arch/openrisc/mm/cache.c               | 87 +++++++++++++++++++++++---
 arch/openrisc/mm/init.c                |  5 +-
 5 files changed, 118 insertions(+), 24 deletions(-)
arch/openrisc/include/asm/cacheflush.h +17 -0
index 984c331ff5f4..0e60af486ec1 100644
@@ -23,6 +23,9 @@
  */
 extern void local_dcache_page_flush(struct page *page);
 extern void local_icache_page_inv(struct page *page);
+extern void local_dcache_range_flush(unsigned long start, unsigned long end);
+extern void local_dcache_range_inv(unsigned long start, unsigned long end);
+extern void local_icache_range_inv(unsigned long start, unsigned long end);
 
 /*
  * Data cache flushing always happen on the local cpu. Instruction cache
@@ -38,6 +41,20 @@ extern void local_icache_page_inv(struct page *page);
 extern void smp_icache_page_inv(struct page *page);
 #endif /* CONFIG_SMP */
 
+/*
+ * Even if the actual block size is larger than L1_CACHE_BYTES, paddr
+ * can be incremented by L1_CACHE_BYTES. When paddr is written to the
+ * invalidate register, the entire cache line encompassing this address
+ * is invalidated. Each subsequent reference to the same cache line will
+ * not affect the invalidation process.
+ */
+#define local_dcache_block_flush(addr) \
+	local_dcache_range_flush(addr, addr + L1_CACHE_BYTES)
+#define local_dcache_block_inv(addr) \
+	local_dcache_range_inv(addr, addr + L1_CACHE_BYTES)
+#define local_icache_block_inv(addr) \
+	local_icache_range_inv(addr, addr + L1_CACHE_BYTES)
+
 /*
  * Synchronizes caches. Whenever a cpu writes executable code to memory, this
  * should be called to make sure the processor sees the newly written code.
arch/openrisc/include/asm/cpuinfo.h +15 -0
index 82f5d4c06314..e46afbfe9b5a 100644
@@ -15,6 +15,9 @@
 #ifndef __ASM_OPENRISC_CPUINFO_H
 #define __ASM_OPENRISC_CPUINFO_H
 
+#include <asm/spr.h>
+#include <asm/spr_defs.h>
+
 struct cache_desc {
 	u32 size;
 	u32 sets;
@@ -34,4 +37,16 @@ struct cpuinfo_or1k {
 extern struct cpuinfo_or1k cpuinfo_or1k[NR_CPUS];
 extern void setup_cpuinfo(void);
 
+/*
+ * Check if the cache component exists.
+ */
+extern bool cpu_cache_is_present(const unsigned int cache_type);
+
+/*
+ * Check if the cache block flush/invalidate register is implemented for the
+ * cache component.
+ */
+extern bool cb_inv_flush_is_implemented(const unsigned int reg,
+					const unsigned int cache_type);
+
 #endif /* __ASM_OPENRISC_CPUINFO_H */
arch/openrisc/kernel/dma.c +4 -14
index b3edbb33b621..3a7b5baaa450 100644
@@ -17,6 +17,7 @@
 #include <linux/pagewalk.h>
 
 #include <asm/cpuinfo.h>
+#include <asm/cacheflush.h>
 #include <asm/spr_defs.h>
 #include <asm/tlbflush.h>
 
@@ -24,9 +25,6 @@ static int
 page_set_nocache(pte_t *pte, unsigned long addr,
 		 unsigned long next, struct mm_walk *walk)
 {
-	unsigned long cl;
-	struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
-
 	pte_val(*pte) |= _PAGE_CI;
 
 	/*
@@ -36,8 +34,7 @@ page_set_nocache(pte_t *pte, unsigned long addr,
 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
 
 	/* Flush page out of dcache */
-	for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo->dcache_block_size)
-		mtspr(SPR_DCBFR, cl);
+	local_dcache_range_flush(__pa(addr), __pa(next));
 
 	return 0;
 }
@@ -98,21 +95,14 @@ void arch_dma_clear_uncached(void *cpu_addr, size_t size)
 void arch_sync_dma_for_device(phys_addr_t addr, size_t size,
 		enum dma_data_direction dir)
 {
-	unsigned long cl;
-	struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
-
 	switch (dir) {
 	case DMA_TO_DEVICE:
 		/* Flush the dcache for the requested range */
-		for (cl = addr; cl < addr + size;
-		     cl += cpuinfo->dcache_block_size)
-			mtspr(SPR_DCBFR, cl);
+		local_dcache_range_flush(addr, addr + size);
 		break;
 	case DMA_FROM_DEVICE:
 		/* Invalidate the dcache for the requested range */
-		for (cl = addr; cl < addr + size;
-		     cl += cpuinfo->dcache_block_size)
-			mtspr(SPR_DCBIR, cl);
+		local_dcache_range_inv(addr, addr + size);
 		break;
 	default:
 		/*
arch/openrisc/mm/cache.c +79 -8

113 lines not on this page · raw message

arch/openrisc/mm/init.c +3 -2
index d0cb1a0126f9..46b8720db08e 100644
@@ -35,6 +35,7 @@
 #include <asm/fixmap.h>
 #include <asm/tlbflush.h>
 #include <asm/sections.h>
+#include <asm/cacheflush.h>
 
 int mem_init_done;
 
@@ -176,8 +177,8 @@ void __init paging_init(void)
 	barrier();
 
 	/* Invalidate instruction caches after code modification */
-	mtspr(SPR_ICBIR, 0x900);
-	mtspr(SPR_ICBIR, 0xa00);
+	local_icache_block_inv(0x900);
+	local_icache_block_inv(0xa00);
 
 	/* New TLB miss handlers and kernel page tables are in now place.
 	 * Make sure that page flags get updated for all pages in TLB by
Signature · 1 line
2.48.1
maintainer
#3 lore raw

Re: [PATCH v4 2/3] openrisc: Introduce new utility functions to flush and invalidate caches

Quoted 96 lines · Sahil Siddiq

According to the OpenRISC architecture manual, the dcache and icache may not be present. When these caches are present, the invalidate and flush registers may be absent. The current implementation does not perform checks to verify their presence before utilizing cache registers, or invalidating and flushing cache blocks.

Introduce new functions to detect the presence of cache components and related special-purpose registers.

There are a few places where a range of addresses have to be flushed or invalidated and the implementation is duplicated. Introduce new utility functions and macros that generalize this implementation and reduce duplication.

---
Changes from v3 -> v4:
- arch/openrisc/include/asm/cpuinfo.h: Move new definitions to cache.c.
- arch/openrisc/mm/cache.c:
(cache_loop): Split function.
(cache_loop_page): New function.
(cpu_cache_is_present): Move definition here. (cb_inv_flush_is_implemented): Move definition here.

Changes from v2 -> v3:
- arch/openrisc/include/asm/cacheflush.h: Declare new functions and macros.
- arch/openrisc/include/asm/cpuinfo.h: Implement new functions. (cpu_cache_is_present):
1. The implementation of this function was strewn all over the place in the previous versions.
2. Fix condition. The condition in the previous version was incorrect. (cb_inv_flush_is_implemented): New function.
- arch/openrisc/kernel/dma.c: Use new functions.
- arch/openrisc/mm/cache.c:
(cache_loop): Extend function.
(local_*_page_*): Use new cache_loop interface. (local_*_range_*): Implement new functions.
- arch/openrisc/mm/init.c: Use new functions.

 arch/openrisc/include/asm/cacheflush.h | 17 +++++
 arch/openrisc/include/asm/cpuinfo.h    | 15 +++++
 arch/openrisc/kernel/dma.c             | 18 ++----
 arch/openrisc/mm/cache.c               | 87 +++++++++++++++++++++++---
 arch/openrisc/mm/init.c                |  5 +-
 5 files changed, 118 insertions(+), 24 deletions(-)
arch/openrisc/include/asm/cacheflush.h +17 -0
index 984c331ff5f4..0e60af486ec1 100644
@@ -23,6 +23,9 @@
  */
 extern void local_dcache_page_flush(struct page *page);
 extern void local_icache_page_inv(struct page *page);
+extern void local_dcache_range_flush(unsigned long start, unsigned long end);
+extern void local_dcache_range_inv(unsigned long start, unsigned long end);
+extern void local_icache_range_inv(unsigned long start, unsigned long end);
 
 /*
  * Data cache flushing always happen on the local cpu. Instruction cache
@@ -38,6 +41,20 @@ extern void local_icache_page_inv(struct page *page);
 extern void smp_icache_page_inv(struct page *page);
 #endif /* CONFIG_SMP */
 
+/*
+ * Even if the actual block size is larger than L1_CACHE_BYTES, paddr
+ * can be incremented by L1_CACHE_BYTES. When paddr is written to the
+ * invalidate register, the entire cache line encompassing this address
+ * is invalidated. Each subsequent reference to the same cache line will
+ * not affect the invalidation process.
+ */
+#define local_dcache_block_flush(addr) \
+	local_dcache_range_flush(addr, addr + L1_CACHE_BYTES)
+#define local_dcache_block_inv(addr) \
+	local_dcache_range_inv(addr, addr + L1_CACHE_BYTES)
+#define local_icache_block_inv(addr) \
+	local_icache_range_inv(addr, addr + L1_CACHE_BYTES)
+
 /*
  * Synchronizes caches. Whenever a cpu writes executable code to memory, this
  * should be called to make sure the processor sees the newly written code.
arch/openrisc/include/asm/cpuinfo.h +3 -0
index 82f5d4c06314..e46afbfe9b5a 100644
@@ -15,6 +15,9 @@
 #ifndef __ASM_OPENRISC_CPUINFO_H
 #define __ASM_OPENRISC_CPUINFO_H
 
+#include <asm/spr.h>
+#include <asm/spr_defs.h>
+
 struct cache_desc {
 	u32 size;
 	u32 sets;

arch/openrisc/include/asm/cpuinfo.h struct cpuinfo_or1k { · line 43

Quoted 7 lines
arch/openrisc/include/asm/cpuinfo.h +4 -0
@@ -34,4 +37,16 @@ struct cpuinfo_or1k {
 extern struct cpuinfo_or1k cpuinfo_or1k[NR_CPUS];
 extern void setup_cpuinfo(void);
 
+/*
+ * Check if the cache component exists.
+ */
+extern bool cpu_cache_is_present(const unsigned int cache_type);

This is used in cacheinfo. OK.

arch/openrisc/include/asm/cpuinfo.h struct cpuinfo_or1k { · line 50

Quoted 6 lines
arch/openrisc/include/asm/cpuinfo.h +6 -0
+/*
+ * Check if the cache block flush/invalidate register is implemented for the
+ * cache component.
+ */
+extern bool cb_inv_flush_is_implemented(const unsigned int reg,
+					const unsigned int cache_type);

But this function doesnt seem to be used anywhere but in cache.c. Does it need to be public?

Quoted 120 lines
arch/openrisc/include/asm/cpuinfo.h +0 -0
 #endif /* __ASM_OPENRISC_CPUINFO_H */
arch/openrisc/kernel/dma.c +4 -14
index b3edbb33b621..3a7b5baaa450 100644
@@ -17,6 +17,7 @@
 #include <linux/pagewalk.h>
 
 #include <asm/cpuinfo.h>
+#include <asm/cacheflush.h>
 #include <asm/spr_defs.h>
 #include <asm/tlbflush.h>
 
@@ -24,9 +25,6 @@ static int
 page_set_nocache(pte_t *pte, unsigned long addr,
 		 unsigned long next, struct mm_walk *walk)
 {
-	unsigned long cl;
-	struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
-
 	pte_val(*pte) |= _PAGE_CI;
 
 	/*
@@ -36,8 +34,7 @@ page_set_nocache(pte_t *pte, unsigned long addr,
 	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
 
 	/* Flush page out of dcache */
-	for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo->dcache_block_size)
-		mtspr(SPR_DCBFR, cl);
+	local_dcache_range_flush(__pa(addr), __pa(next));
 
 	return 0;
 }
@@ -98,21 +95,14 @@ void arch_dma_clear_uncached(void *cpu_addr, size_t size)
 void arch_sync_dma_for_device(phys_addr_t addr, size_t size,
 		enum dma_data_direction dir)
 {
-	unsigned long cl;
-	struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
-
 	switch (dir) {
 	case DMA_TO_DEVICE:
 		/* Flush the dcache for the requested range */
-		for (cl = addr; cl < addr + size;
-		     cl += cpuinfo->dcache_block_size)
-			mtspr(SPR_DCBFR, cl);
+		local_dcache_range_flush(addr, addr + size);
 		break;
 	case DMA_FROM_DEVICE:
 		/* Invalidate the dcache for the requested range */
-		for (cl = addr; cl < addr + size;
-		     cl += cpuinfo->dcache_block_size)
-			mtspr(SPR_DCBIR, cl);
+		local_dcache_range_inv(addr, addr + size);
 		break;
 	default:
 		/*
arch/openrisc/mm/cache.c +30 -8
index eb43b73f3855..3bf6d728d2d2 100644
@@ -14,31 +14,63 @@
 #include <asm/spr_defs.h>
 #include <asm/cache.h>
 #include <asm/cacheflush.h>
+#include <asm/cpuinfo.h>
 #include <asm/tlbflush.h>
 
-static __always_inline void cache_loop(struct page *page, const unsigned int reg)
+static __always_inline void cache_loop(unsigned long paddr, unsigned long end,
+				       const unsigned int reg, const unsigned int cache_type)
 {
-	unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT;
-	unsigned long line = paddr & ~(L1_CACHE_BYTES - 1);
+	if (!cpu_cache_is_present(cache_type))
+		return;
+
+	if (!cb_inv_flush_is_implemented(reg, cache_type))
+		return;
 
-	while (line < paddr + PAGE_SIZE) {
-		mtspr(reg, line);
-		line += L1_CACHE_BYTES;
+	while (paddr < end) {
+		mtspr(reg, paddr);
+		paddr += L1_CACHE_BYTES;
 	}
 }
 
+static void cache_loop_page(struct page *page, const unsigned int reg,
+					    const unsigned int cache_type)
+{
+	unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT;
+	unsigned long end = paddr + PAGE_SIZE;
+
+	paddr &= ~(L1_CACHE_BYTES - 1);
+
+	cache_loop(paddr, end, reg, cache_type);
+}
+
 void local_dcache_page_flush(struct page *page)
 {
-	cache_loop(page, SPR_DCBFR);
+	cache_loop_page(page, SPR_DCBFR, SPR_UPR_DCP);
 }
 EXPORT_SYMBOL(local_dcache_page_flush);
 
 void local_icache_page_inv(struct page *page)
 {
-	cache_loop(page, SPR_ICBIR);
+	cache_loop_page(page, SPR_ICBIR, SPR_UPR_ICP);
 }
 EXPORT_SYMBOL(local_icache_page_inv);
 
+void local_dcache_range_flush(unsigned long start, unsigned long end)
+{
+	cache_loop(start, end, SPR_DCBFR, SPR_UPR_DCP);
+}
+
+void local_dcache_range_inv(unsigned long start, unsigned long end)

arch/openrisc/mm/cache.c line 73

Quoted 10 lines
arch/openrisc/mm/cache.c +10 -0
+{
+	cache_loop(start, end, SPR_DCBIR, SPR_UPR_DCP);
+}
+
+void local_icache_range_inv(unsigned long start, unsigned long end)
+{
+	cache_loop(start, end, SPR_ICBIR, SPR_UPR_ICP);
+}
+
+

There is an extra newline here.

Quoted 36 lines
arch/openrisc/mm/cache.c +29 -0
 void update_cache(struct vm_area_struct *vma, unsigned long address,
 	pte_t *pte)
 {
@@ -59,3 +91,42 @@ void update_cache(struct vm_area_struct *vma, unsigned long address,
 	}
 }
 
+/*
+ * Check if the cache component exists.
+ */
+bool cpu_cache_is_present(const unsigned int cache_type)
+{
+	unsigned long upr = mfspr(SPR_UPR);
+
+	return !!(upr & (SPR_UPR_UP | cache_type));
+}
+
+ /*
+  * Check if the cache block flush/invalidate register is implemented for the
+  * cache component.
+  */
+bool cb_inv_flush_is_implemented(const unsigned int reg,
+				 const unsigned int cache_type)
+{
+	unsigned long cfgr;
+
+	if (cache_type == SPR_UPR_DCP) {
+		cfgr = mfspr(SPR_DCCFGR);
+		if (reg == SPR_DCBFR)
+			return !!(cfgr & SPR_DCCFGR_CBFRI);
+
+		if (reg == SPR_DCBIR)
+			return !!(cfgr & SPR_DCCFGR_CBIRI);
+	}
+
+	/*

arch/openrisc/mm/cache.c void update_cache(struct vm_area_struct *vma, unsigned long address, · line 132

Quoted 10 lines
arch/openrisc/mm/cache.c +10 -0
+	 * The cache block flush register is not implemented for the instruction
+	 * cache.
+	 */
+	if (cache_type == SPR_UPR_ICP) {
+		cfgr = mfspr(SPR_ICCFGR);
+		return !!(cfgr & SPR_ICCFGR_CBIRI);
+	}
+
+	return false;
+}

Usually we try to define functions before they are used. This and cpu_cache_is_present should be above cache_loop.

As I mentioned, this may be a bit overkill, OpenRISC completely ignore writes to SPR registers that are not implemented. They are basically nops. It may help to avoid running cache loops, but really there are no implementations I know of that have caches (SPR_UPR_UP) but no cache invalidation/flush registers.

-Stafford

Quoted 26 lines
arch/openrisc/mm/init.c +3 -2
index d0cb1a0126f9..46b8720db08e 100644
@@ -35,6 +35,7 @@
 #include <asm/fixmap.h>
 #include <asm/tlbflush.h>
 #include <asm/sections.h>
+#include <asm/cacheflush.h>
 
 int mem_init_done;
 
@@ -176,8 +177,8 @@ void __init paging_init(void)
 	barrier();
 
 	/* Invalidate instruction caches after code modification */
-	mtspr(SPR_ICBIR, 0x900);
-	mtspr(SPR_ICBIR, 0xa00);
+	local_icache_block_inv(0x900);
+	local_icache_block_inv(0xa00);
 
 	/* New TLB miss handlers and kernel page tables are in now place.
 	 * Make sure that page flags get updated for all pages in TLB by
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH v4 2/3] openrisc: Introduce new utility functions to flush and invalidate caches

Hi,

Quoted 27 lines · Stafford Horne
Quoted 16 lines · Sahil Siddiq

[...]

arch/openrisc/include/asm/cpuinfo.h +3 -0
index 82f5d4c06314..e46afbfe9b5a 100644
@@ -15,6 +15,9 @@
  #ifndef __ASM_OPENRISC_CPUINFO_H
  #define __ASM_OPENRISC_CPUINFO_H
 
+#include <asm/spr.h>
+#include <asm/spr_defs.h>
+
  struct cache_desc {
  	u32 size;
  	u32 sets;

arch/openrisc/include/asm/cpuinfo.h struct cpuinfo_or1k { · line 43

Quoted 7 lines
arch/openrisc/include/asm/cpuinfo.h +4 -0
@@ -34,4 +37,16 @@ struct cpuinfo_or1k {
  extern struct cpuinfo_or1k cpuinfo_or1k[NR_CPUS];
  extern void setup_cpuinfo(void);
 
+/*
+ * Check if the cache component exists.
+ */
+extern bool cpu_cache_is_present(const unsigned int cache_type);

This is used in cacheinfo. OK.

Quoted 10 lines

arch/openrisc/include/asm/cpuinfo.h struct cpuinfo_or1k { · line 50

Quoted 6 lines
arch/openrisc/include/asm/cpuinfo.h +6 -0
+/*
+ * Check if the cache block flush/invalidate register is implemented for the
+ * cache component.
+ */
+extern bool cb_inv_flush_is_implemented(const unsigned int reg,
+					const unsigned int cache_type);

But this function doesnt seem to be used anywhere but in cache.c. Does it need to be public?

Right, this doesn't have to be public. However, based on the description below, it's better to remove this function.

Quoted 124 lines
Quoted 124 lines
arch/openrisc/include/asm/cpuinfo.h +0 -0
  #endif /* __ASM_OPENRISC_CPUINFO_H */
arch/openrisc/kernel/dma.c +4 -14
index b3edbb33b621..3a7b5baaa450 100644
@@ -17,6 +17,7 @@
  #include <linux/pagewalk.h>
 
  #include <asm/cpuinfo.h>
+#include <asm/cacheflush.h>
  #include <asm/spr_defs.h>
  #include <asm/tlbflush.h>
 
@@ -24,9 +25,6 @@ static int
  page_set_nocache(pte_t *pte, unsigned long addr,
  		 unsigned long next, struct mm_walk *walk)
  {
-	unsigned long cl;
-	struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
-
  	pte_val(*pte) |= _PAGE_CI;
 
  	/*
@@ -36,8 +34,7 @@ page_set_nocache(pte_t *pte, unsigned long addr,
  	flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
 
  	/* Flush page out of dcache */
-	for (cl = __pa(addr); cl < __pa(next); cl += cpuinfo->dcache_block_size)
-		mtspr(SPR_DCBFR, cl);
+	local_dcache_range_flush(__pa(addr), __pa(next));
 
  	return 0;
  }
@@ -98,21 +95,14 @@ void arch_dma_clear_uncached(void *cpu_addr, size_t size)
  void arch_sync_dma_for_device(phys_addr_t addr, size_t size,
  		enum dma_data_direction dir)
  {
-	unsigned long cl;
-	struct cpuinfo_or1k *cpuinfo = &cpuinfo_or1k[smp_processor_id()];
-
  	switch (dir) {
  	case DMA_TO_DEVICE:
  		/* Flush the dcache for the requested range */
-		for (cl = addr; cl < addr + size;
-		     cl += cpuinfo->dcache_block_size)
-			mtspr(SPR_DCBFR, cl);
+		local_dcache_range_flush(addr, addr + size);
  		break;
  	case DMA_FROM_DEVICE:
  		/* Invalidate the dcache for the requested range */
-		for (cl = addr; cl < addr + size;
-		     cl += cpuinfo->dcache_block_size)
-			mtspr(SPR_DCBIR, cl);
+		local_dcache_range_inv(addr, addr + size);
  		break;
  	default:
  		/*
arch/openrisc/mm/cache.c +34 -8
index eb43b73f3855..3bf6d728d2d2 100644
@@ -14,31 +14,63 @@
  #include <asm/spr_defs.h>
  #include <asm/cache.h>
  #include <asm/cacheflush.h>
+#include <asm/cpuinfo.h>
  #include <asm/tlbflush.h>
 
-static __always_inline void cache_loop(struct page *page, const unsigned int reg)
+static __always_inline void cache_loop(unsigned long paddr, unsigned long end,
+				       const unsigned int reg, const unsigned int cache_type)
  {
-	unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT;
-	unsigned long line = paddr & ~(L1_CACHE_BYTES - 1);
+	if (!cpu_cache_is_present(cache_type))
+		return;
+
+	if (!cb_inv_flush_is_implemented(reg, cache_type))
+		return;
 
-	while (line < paddr + PAGE_SIZE) {
-		mtspr(reg, line);
-		line += L1_CACHE_BYTES;
+	while (paddr < end) {
+		mtspr(reg, paddr);
+		paddr += L1_CACHE_BYTES;
  	}
  }
 
+static void cache_loop_page(struct page *page, const unsigned int reg,
+					    const unsigned int cache_type)
+{
+	unsigned long paddr = page_to_pfn(page) << PAGE_SHIFT;
+	unsigned long end = paddr + PAGE_SIZE;
+
+	paddr &= ~(L1_CACHE_BYTES - 1);
+
+	cache_loop(paddr, end, reg, cache_type);
+}
+
  void local_dcache_page_flush(struct page *page)
  {
-	cache_loop(page, SPR_DCBFR);
+	cache_loop_page(page, SPR_DCBFR, SPR_UPR_DCP);
  }
  EXPORT_SYMBOL(local_dcache_page_flush);
 
  void local_icache_page_inv(struct page *page)
  {
-	cache_loop(page, SPR_ICBIR);
+	cache_loop_page(page, SPR_ICBIR, SPR_UPR_ICP);
  }
  EXPORT_SYMBOL(local_icache_page_inv);
 
+void local_dcache_range_flush(unsigned long start, unsigned long end)
+{
+	cache_loop(start, end, SPR_DCBFR, SPR_UPR_DCP);
+}
+
+void local_dcache_range_inv(unsigned long start, unsigned long end)
+{
+	cache_loop(start, end, SPR_DCBIR, SPR_UPR_DCP);
+}
+
Quoted 8 lines

arch/openrisc/mm/cache.c line 73

Quoted 6 lines
arch/openrisc/mm/cache.c +6 -0
+void local_icache_range_inv(unsigned long start, unsigned long end)
+{
+	cache_loop(start, end, SPR_ICBIR, SPR_UPR_ICP);
+}
+
+

There is an extra newline here.

Oh, I missed this. I'll fix it.

Quoted 41 lines
Quoted 41 lines
arch/openrisc/mm/cache.c +34 -0
  void update_cache(struct vm_area_struct *vma, unsigned long address,
  	pte_t *pte)
  {
@@ -59,3 +91,42 @@ void update_cache(struct vm_area_struct *vma, unsigned long address,
  	}
  }
 
+/*
+ * Check if the cache component exists.
+ */
+bool cpu_cache_is_present(const unsigned int cache_type)
+{
+	unsigned long upr = mfspr(SPR_UPR);
+
+	return !!(upr & (SPR_UPR_UP | cache_type));
+}
+
+ /*
+  * Check if the cache block flush/invalidate register is implemented for the
+  * cache component.
+  */
+bool cb_inv_flush_is_implemented(const unsigned int reg,
+				 const unsigned int cache_type)
+{
+	unsigned long cfgr;
+
+	if (cache_type == SPR_UPR_DCP) {
+		cfgr = mfspr(SPR_DCCFGR);
+		if (reg == SPR_DCBFR)
+			return !!(cfgr & SPR_DCCFGR_CBFRI);
+
+		if (reg == SPR_DCBIR)
+			return !!(cfgr & SPR_DCCFGR_CBIRI);
+	}
+
+	/*
+	 * The cache block flush register is not implemented for the instruction
+	 * cache.
+	 */
+	if (cache_type == SPR_UPR_ICP) {
+		cfgr = mfspr(SPR_ICCFGR);
Quoted 8 lines

arch/openrisc/mm/cache.c void update_cache(struct vm_area_struct *vma, unsigned long address, · line 132

Quoted 5 lines
arch/openrisc/mm/cache.c +5 -0
+		return !!(cfgr & SPR_ICCFGR_CBIRI);
+	}
+
+	return false;
+}

Usually we try to define functions before they are used. This and cpu_cache_is_present should be above cache_loop.

Got it. I'll shift this.

Quoted 5 lines

As I mentioned, this may be a bit overkill, OpenRISC completely ignore writes to SPR registers that are not implemented. They are basically nops. It may help to avoid running cache loops, but really there are no implementations I know of that have caches (SPR_UPR_UP) but no cache invalidation/flush registers.

Understood. I'll remove this function.

Thanks,
Sahil

#5 lore raw

PATCH v4 3/3 openrisc: Add cacheinfo support

In reply to Sahil Siddiq

Add cacheinfo support for OpenRISC.

Currently, a few CPU cache attributes pertaining to OpenRISC processors are exposed along with other unrelated CPU attributes in the procfs file system (/proc/cpuinfo). However, a few cache attributes remain unexposed.

Provide a mechanism that the generic cacheinfo infrastructure can employ to expose these attributes via the sysfs file system. These attributes can then be exposed in /sys/devices/system/cpu/cpuX/cache/indexN. Move the implementation to pull cache attributes from the processor's registers from arch/openrisc/kernel/setup.c with a few modifications.

This implementation is based on similar work done for MIPS and LoongArch.

---
Changes from v3 -> v4:
- arch/openrisc/kernel/cacheinfo.c: Fix build warning detected by kernel test robot.

Changes from v2 -> v3:
- arch/openrisc/kernel/cacheinfo.c:
1. Use new functions introduced in patch #2.
2. Address review comments regarding coding style.
- arch/openrisc/kernel/setup.c:
(print_cpuinfo): Don't remove detection of UPR register.

 arch/openrisc/kernel/Makefile    |   2 +-
 arch/openrisc/kernel/cacheinfo.c | 104 +++++++++++++++++++++++++++++++
 arch/openrisc/kernel/setup.c     |  44 +------------
 3 files changed, 108 insertions(+), 42 deletions(-)
 create mode 100644 arch/openrisc/kernel/cacheinfo.c
arch/openrisc/kernel/Makefile +1 -1
index 79129161f3e0..e4c7d9bdd598 100644
@@ -7,7 +7,7 @@ extra-y	:= vmlinux.lds
 
 obj-y	:= head.o setup.o or32_ksyms.o process.o dma.o \
 	   traps.o time.o irq.o entry.o ptrace.o signal.o \
-	   sys_call_table.o unwinder.o
+	   sys_call_table.o unwinder.o cacheinfo.o
 
 obj-$(CONFIG_SMP)		+= smp.o sync-timer.o
 obj-$(CONFIG_STACKTRACE)	+= stacktrace.o
arch/openrisc/kernel/cacheinfo.c new file +104 -0

104 lines not on this page · raw message

arch/openrisc/kernel/setup.c +3 -41

79 lines not on this page · raw message

Signature · 1 line
2.48.1