Cache coherency management subsystem
Subsystems (11)
cover Cache coherency management subsystem
Note that I've only a vague idea of who will care about this so please do +CC others as needed.
On x86 there is the much loved WBINVD instruction that causes a write back and invalidate of all caches in the system. It is expensive but it is necessary in a few corner cases. These are cases where the contents of Physical Memory may change without any writes from the host. Whilst there are a few reasons this might happen, the one I care about here is when we are adding or removing mappings on CXL. So typically going from there being actual memory at a host Physical Address to nothing there (reads as zero, writes dropped) or visa-versa. That involves the reprogramming of address decoders (HDM Decoders); in the near future it may also include the device offering dynamic capacity extents. The thing that makes it very hard to handle with CPU flushes is that the instructions are normally VA based and not guaranteed to reach beyond the Point of Coherence or similar. You might be able to (ab)use various flush operations intended to ensure persistence memory but in general they don't work either.
So on other architectures such as ARM64 we have no instruction similar to WBINVD but we may have device interfaces in the system that provide a way to ensure a PA range undergoes the write back and invalidate action. This RFC is to find a way to support those cache maintenance device interfaces. The ones I know about are much more flexible than WBINVD, allowing invalidation of particular PA ranges, or a much richer set of flush types (not supported yet as not needed for upstream use cases).
To illustrate how a solution might work, I've taken both a HiSilicon design (slight quirk as registers overlap with existing PMU driver) and more controversially a firmware interface proposal from ARM (wrapped up in made up ACPI) that was dropped from the released spec but for which the alpha spec is still available.
Why drivers/cache?
- Mainly because it exists and smells like a reasonable place.
- Conor, you are maintainer for this currently do you mind us putting this stuff in there?
Why not just register a singleton function pointer?
- Systems may include multiple cache control devices, responsible for different parts of the PA address range (interleaving etc make this complex). They may not all share a common hardware interface.
- A device class is more convenient than managing multiple homogeneous device instances within a driver.
- Disadvantage is that we need this small class
Generalizing to more arch?
- I've started with ARM64, but if useful elsewhere the small amount of arch code could be moved to a generic location.
QEMU emulation code at
http://gitlab.com/jic23/qemu cxl-2025-03-20
Why an RFC?
- I'm really just looking for feedback on whether the class approach is the way to go at this stage. I'm not strongly attached to it but it feels like the right balance of complexity and flexibility to me.
- I made up the ACPI spec - it's not documented, non official and honestly needs work. I would however like to get feedback on whether it is something we want to try and get through the ACPI Working group as a much improved code first proposal? The potential justification being to avoid the need for lots trivial drivers where maybe a bit of DSDT interpreted code does the job better.
Jonathan Cameron (3):
cache: coherency device class
acpi: PoC of Cache control via ACPI0019 and _DSM
Hack: Pretend we have PSCI 1.2
Yicong Yang (3):
memregion: Support fine grained invalidate by
cpu_cache_invalidate_memregion()
arm64: Support ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
cache: Support cache maintenance for HiSilicon SoC Hydra Home Agent
arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cacheflush.h | 14 ++ arch/arm64/mm/flush.c | 42 ++++++ arch/x86/mm/pat/set_memory.c | 2 +- drivers/acpi/Makefile | 1 + drivers/cache/Kconfig | 26 ++++ drivers/cache/Makefile | 4 + drivers/cache/acpi_cache_control.c | 157 ++++++++++++++++++++++ drivers/cache/coherency_core.c | 130 +++++++++++++++++++ drivers/cache/hisi_soc_hha.c | 193 ++++++++++++++++++++++++++++ drivers/cxl/core/region.c | 6 +- drivers/firmware/psci/psci.c | 2 + drivers/nvdimm/region.c | 3 +- drivers/nvdimm/region_devs.c | 3 +- include/linux/cache_coherency.h | 60 +++++++++ include/linux/memregion.h | 8 +- 16 files changed, 646 insertions(+), 6 deletions(-) create mode 100644 drivers/cache/acpi_cache_control.c create mode 100644 drivers/cache/coherency_core.c create mode 100644 drivers/cache/hisi_soc_hha.c create mode 100644 include/linux/cache_coherency.h
Signature · 1 line
2.43.0
RFC PATCH 1/6 memregion: Support fine grained invalidate by cpu_cache_invalidate_memregion()
From: Yicong Yang <[email protected]>
Extend cpu_cache_invalidate_memregion() to support invalidate certain range of memory. Control of types of invalidation is left for when usecases turn up. For now everything is Clean and Invalidate.
--- arch/x86/mm/pat/set_memory.c | 2 +- drivers/cxl/core/region.c | 6 +++++- drivers/nvdimm/region.c | 3 ++- drivers/nvdimm/region_devs.c | 3 ++- include/linux/memregion.h | 8 ++++++-- 5 files changed, 16 insertions(+), 6 deletions(-)
arch/x86/mm/pat/set_memory.c +1 -1
@@ -354,7 +354,7 @@ bool cpu_cache_has_invalidate_memregion(void) }EXPORT_SYMBOL_NS_GPL (cpu_cache_has_invalidate_memregion,"DEVMEM" ); -int cpu_cache_invalidate_memregion (int res_desc) +int cpu_cache_invalidate_memregion (int res_desc,phys_addr_t start,size_t len) {if (WARN_ON_ONCE (!cpu_cache_has_invalidate_memregion ()))return -ENXIO ;
drivers/cxl/core/region.c +5 -1
@@ -238,7 +238,11 @@ static int cxl_region_invalidate_memregion(struct cxl_region *cxlr) } } -cpu_cache_invalidate_memregion (IORES_DESC_CXL ); +if (!cxlr->params.res) +return -ENXIO ; +cpu_cache_invalidate_memregion (IORES_DESC_CXL , + cxlr->params.res->start, +resource_size (cxlr->params.res));return 0 ; }
drivers/nvdimm/region.c +2 -1
@@ -110,7 +110,8 @@ static void nd_region_remove(struct device *dev)* here is ok. */ if (cpu_cache_has_invalidate_memregion ()) -cpu_cache_invalidate_memregion (IORES_DESC_PERSISTENT_MEMORY ); +cpu_cache_invalidate_memregion (IORES_DESC_PERSISTENT_MEMORY , +0 ,-1 ); }static int child_notify (struct device *dev,void *data)
drivers/nvdimm/region_devs.c +2 -1
@@ -90,7 +90,8 @@ static int nd_region_invalidate_memregion(struct nd_region *nd_region) } } -cpu_cache_invalidate_memregion (IORES_DESC_PERSISTENT_MEMORY ); +cpu_cache_invalidate_memregion (IORES_DESC_PERSISTENT_MEMORY , +0 ,-1 ); out:for (i =0 ; i < nd_region->ndr_mappings; i++) {struct nd_mapping *nd_mapping = &nd_region->mapping[i];
include/linux/memregion.h +6 -2
@@ -28,6 +28,9 @@ static inline void memregion_free(int id)* cpu_cache_invalidate_memregion - drop any CPU cached data for * memregions described by @res_desc * @res_desc: one of the IORES_DESC_* types +* @start: start physical address of the target memory region. +* @len: length of the target memory region. -1 for all the regions of +* the target type. * * Perform cache maintenance after a memory event / operation that * changes the contents of physical memory in a cache-incoherent manner.
@@ -46,7 +49,7 @@ static inline void memregion_free(int id)* the cache maintenance. */ #ifdef CONFIG_ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION -int cpu_cache_invalidate_memregion (int res_desc); +int cpu_cache_invalidate_memregion (int res_desc,phys_addr_t start,size_t len);bool cpu_cache_has_invalidate_memregion (void );#else static inline bool cpu_cache_has_invalidate_memregion (void )
@@ -54,7 +57,8 @@ static inline bool cpu_cache_has_invalidate_memregion(void)return false; } -static inline int cpu_cache_invalidate_memregion (int res_desc) +static inline int cpu_cache_invalidate_memregion (int res_desc, +phys_addr_t start,size_t len) {WARN_ON_ONCE ("CPU cache invalidation required" );return -ENXIO ;
Signature · 1 line
2.43.0
RFC PATCH 2/6 arm64: Support ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
In reply to Jonathan CameronFrom: Yicong Yang <[email protected]>
ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION provides the mechanism for invalidate certain memory regions in a cache-incoherent manner. Currently is used by NVIDMM and CXL memory. This is mainly done by the system component and is implementation define per spec. Provides a method for the platforms register their own invalidate method and implement ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/cacheflush.h | 14 ++++++++++ arch/arm64/mm/flush.c | 42 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -21,6 +21,7 @@ config ARM64 select ARCH_ENABLE_THP_MIGRATION if TRANSPARENT_HUGEPAGE select ARCH_HAS_CACHE_LINE_SIZE select ARCH_HAS_CC_PLATFORM + select ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION select ARCH_HAS_CRC32 select ARCH_HAS_CRC_T10DIF if KERNEL_MODE_NEON select ARCH_HAS_CURRENT_STACK_POINTER
arch/arm64/include/asm/cacheflush.h +14 -0
@@ -139,6 +139,20 @@ static __always_inline void icache_inval_all_pou(void) dsb(ish); } +#ifdef CONFIG_ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION + +#include <linux/memregion.h> + +struct system_cache_flush_method { +int (*invalidate_memregion)(int res_desc, +phys_addr_t start,size_t len); +}; + +void arm64_set_sys_cache_flush_method (const struct system_cache_flush_method *method); +void arm64_clr_sys_cache_flush_method (const struct system_cache_flush_method *method); + +#endif /* CONFIG_ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION */ +#include <asm-generic/cacheflush.h> #endif /* __ASM_CACHEFLUSH_H */
arch/arm64/mm/flush.c +42 -0
51 lines not on this page · raw message
Signature · 1 line
2.43.0
Catalin Marinas
maintainerRe: [RFC PATCH 2/6] arm64: Support ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
Quoted 4 lines · Jonathan Cameron
arch/arm64/include/asm/cacheflush.h +4
-0+struct system_cache_flush_method { +int (*invalidate_memregion)(int res_desc, +phys_addr_t start,size_t len); +};
[...]
arch/arm64/mm/flush.c void arch_invalidate_pmem(void *addr, size_t size) · line 134
Quoted 8 lines
arch/arm64/mm/flush.c +8
-0+int cpu_cache_invalidate_memregion (int res_desc,phys_addr_t start,size_t len) +{ +guard (spinlock_irqsave)(&scfm_lock); +if (!scfm_data) +return -EOPNOTSUPP ; + +return scfm_data->invalidate_memregion (res_desc, start, len); +}
WBINVD on x86 deals with the CPU caches as well. Even the API naming in Linux implies CPU caches. IIUC, devices registering to the above on Arm SoCs can only deal with system caches. Is it sufficient?
Signature · 1 line
Catalin
Re: [RFC PATCH 2/6] arm64: Support ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
Quoted 6 lines · Catalin Marinas
Quoted 4 lines · Jonathan Cameron
arch/arm64/include/asm/cacheflush.h +4
-0+struct system_cache_flush_method { +int (*invalidate_memregion)(int res_desc, +phys_addr_t start,size_t len); +};[...]
Quoted 13 lines
arch/arm64/mm/flush.c void arch_invalidate_pmem(void *addr, size_t size) · line 134
Quoted 8 lines
arch/arm64/mm/flush.c +8
-0+int cpu_cache_invalidate_memregion (int res_desc,phys_addr_t start,size_t len) +{ +guard (spinlock_irqsave)(&scfm_lock); +if (!scfm_data) +return -EOPNOTSUPP ; + +return scfm_data->invalidate_memregion (res_desc, start, len); +}WBINVD on x86 deals with the CPU caches as well. Even the API naming in Linux implies CPU caches. IIUC, devices registering to the above on Arm SoCs can only deal with system caches. Is it sufficient?
The device driver who register this method should handle this. If the hardware support maintaining the coherency among the system, for example on system cache invalidation the hardware is also able to invalidate the involved cachelines on all the subordinate caches (L1/L2/etc, by back invalidate snoop or other ways), then software don't need to invalidate the non-system cache explicitly. Otherwise the driver need to explicitly invalidate the non-system cache explicitly in their scfm_data::invalidate_memregion() method. Here in the generally code we simply don't know the capability of the hardware.
Thanks.
Re: [RFC PATCH 2/6] arm64: Support ARCH_HAS_CPU_CACHE_INVALIDATE_MEMREGION
Quoted 21 lines · On Sat, 29 Mar 2025 15:14:14 +0800 Yicong Yang <[email protected]> wrote:
Quoted 6 lines · Catalin Marinas
Quoted 4 lines · Jonathan Cameron
arch/arm64/include/asm/cacheflush.h +4
-0+struct system_cache_flush_method { +int (*invalidate_memregion)(int res_desc, +phys_addr_t start,size_t len); +};[...]
Quoted 13 lines
arch/arm64/mm/flush.c void arch_invalidate_pmem(void *addr, size_t size) · line 134
Quoted 8 lines
arch/arm64/mm/flush.c +8
-0+int cpu_cache_invalidate_memregion (int res_desc,phys_addr_t start,size_t len) +{ +guard (spinlock_irqsave)(&scfm_lock); +if (!scfm_data) +return -EOPNOTSUPP ; + +return scfm_data->invalidate_memregion (res_desc, start, len); +}WBINVD on x86 deals with the CPU caches as well. Even the API naming in Linux implies CPU caches. IIUC, devices registering to the above on Arm SoCs can only deal with system caches. Is it sufficient?
Quoted 9 lines
The device driver who register this method should handle this. If the hardware support maintaining the coherency among the system, for example on system cache invalidation the hardware is also able to invalidate the involved cachelines on all the subordinate caches (L1/L2/etc, by back invalidate snoop or other ways), then software don't need to invalidate the non-system cache explicitly. Otherwise the driver need to explicitly invalidate the non-system cache explicitly in their
scfm_data::invalidate_memregion()method. Here in the generally code we simply don't know the capability of the hardware.
Coming to this a little late. It would definitely be helpful to understand whether other hardware implementations where this is relevant (e.g. CXL capable systems) do require explicit flushes of the caches nearer the CPU and any dance that they need in that case to ensure no race conditions leave stale lines.
The PSCI spec (that never was) did allow for reporting that it was necessary to stop all traffic prior to flushes, but that is crazy to support and someone built the wrong system if they need to do that under even vaguely normal software flows.
Jonathan
Quoted 3 lines
Thanks.
RFC PATCH 3/6 cache: coherency device class
In reply to Jonathan CameronSome systems contain devices that are capable of issuing certain cache invalidation messages to all components in the system. A typical use is when the memory backing a physical address is being changed, such as when a region is configured for CXL or a dynamic capacity event occurs.
These perform a similar action to the WBINVD instruction on x86 but may provide finer granularity and/or a richer set of actions.
Add a tiny device class to which drivers may register. Each driver provides an ops structure and the first op is Write Back and Invalidate by PA Range. The driver may over invalidate.
An optional completion check is also provided. If present that should be called to ensure that the action has finished.
If multiple agents are present in the system each should register with this class and the core code will issue the invalidate to all of them before checking for completion on each. This is done to avoid need for filtering in the core code which can become complex when interleave, potentially across different cache coherency hardware, is going on, so it is easier to tell everyone and let those who don't care do nothing.
RFC question:
1. Location. Conor, do you mind us putting this in drivers/cache? I'm more than happy to add a maintainers entry and not make this your problem!
2. Need to figure out if all agents have turned up yet? No idea how to ensure this.
3. Is just iterating over devices registered with the class the simplest solution?
4. Given we have a nice device class, to which we can add sysfs attributes, what info might be useful to userspace? It might be useful to indicate how expensive a flush might be for example or where in the topology of the system it is being triggered?
--- drivers/cache/Kconfig | 6 ++ drivers/cache/Makefile | 2 + drivers/cache/coherency_core.c | 130 ++++++++++++++++++++++++++++++++ include/linux/cache_coherency.h | 60 +++++++++++++++ 4 files changed, 198 insertions(+)
drivers/cache/Kconfig +6 -0
@@ -1,6 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 menu "Cache Drivers" +config CACHE_COHERENCY_CLASS + bool "Cache coherency control class" + help + Class to which coherency control drivers register allowing core kernel + subsystems to issue invalidations and similar coherency operations. + config AX45MP_L2_CACHE bool "Andes Technology AX45MP L2 Cache controller" depends on RISCV
drivers/cache/Makefile +2 -0
@@ -3,3 +3,5 @@ obj-$(CONFIG_AX45MP_L2_CACHE) += ax45mp_cache.o obj-$(CONFIG_SIFIVE_CCACHE) += sifive_ccache.o obj-$(CONFIG_STARFIVE_STARLINK_CACHE) += starfive_starlink_cache.o + +obj-$(CONFIG_CACHE_COHERENCY_CLASS) += coherency_core.o
drivers/cache/coherency_core.c new file +130 -0
130 lines not on this page · raw message
include/linux/cache_coherency.h new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [RFC PATCH 3/6] cache: coherency device class
drivers/cache/Kconfig line 5
Quoted 8 lines · Jonathan Cameron
drivers/cache/Kconfig +2
-0@@ -1,6 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 menu "Cache Drivers" +config CACHE_COHERENCY_CLASS + bool "Cache coherency control class"
Why can't this be a module? And why would anyone want to turn it off?
drivers/cache/Kconfig line 8
Quoted 3 lines
drivers/cache/Kconfig +3
-0+ help + Class to which coherency control drivers register allowing core kernel + subsystems to issue invalidations and similar coherency operations.
What "core kernel subsystems"?
drivers/cache/Kconfig line 12
Quoted 4 lines
drivers/cache/Kconfig +1
-0+ config AX45MP_L2_CACHE bool "Andes Technology AX45MP L2 Cache controller" depends on RISCV
Shouldn't all of these now depend on CACHE_COHERENCY_CLASS?
drivers/cache/Makefile line 7
Quoted 10 lines
drivers/cache/Makefile +2
-0@@ -3,3 +3,5 @@ obj-$(CONFIG_AX45MP_L2_CACHE) += ax45mp_cache.o obj-$(CONFIG_SIFIVE_CCACHE) += sifive_ccache.o obj-$(CONFIG_STARFIVE_STARLINK_CACHE) += starfive_starlink_cache.o + +obj-$(CONFIG_CACHE_COHERENCY_CLASS) += coherency_core.o
Why the blank line?
Quoted 122 lines
drivers/cache/coherency_core.c new file +116
-0@@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Class to manage OS controlled coherency agents within the system. +* Specifically to enable operations such as write back and invalidate. +* +* Copyright: Huawei 2025 +* Some elements based on fwctl class as an example of a modern +* lightweight class. +*/ + +#include <linux/cache_coherency.h> +#include <linux/container_of.h> +#include <linux/idr.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> + +#include <asm/cacheflush.h> + +static DEFINE_IDA (cache_coherency_ida); + +static void cache_coherency_device_release (struct device *device) +{ +struct cache_coherency_device *ccd = + container_of(device, struct cache_coherency_device, dev); + +ida_free (&cache_coherency_ida, ccd->id); +} + +static struct class cache_coherency_class = { + .name ="cache_coherency" , + .dev_release = cache_coherency_device_release, +}; + +static int cache_inval_one (struct device *dev,void *data) +{ +struct cache_coherency_device *ccd = + container_of(dev, struct cache_coherency_device, dev); + +if (!ccd->ops) +return -EINVAL ; + +return ccd->ops->wbinv (ccd, data); +} + +static int cache_inval_done_one (struct device *dev,void *data) +{ +struct cache_coherency_device *ccd = + container_of(dev, struct cache_coherency_device, dev); +if (!ccd->ops) +return -EINVAL ; + +return ccd->ops->done (ccd); +} + +static int cache_invalidate_memregion (int res_desc, +phys_addr_t addr,size_t size) +{ +int ret; +struct cc_inval_params params = { + .addr = addr, + .size = size, + }; + + ret =class_for_each_device (&cache_coherency_class,NULL , ¶ms, + cache_inval_one); +if (ret) +return ret; + +return class_for_each_device (&cache_coherency_class,NULL ,NULL , + cache_inval_done_one); +} + +static const struct system_cache_flush_method cache_flush_method = { + .invalidate_memregion = cache_invalidate_memregion, +}; + +struct cache_coherency_device * +_cache_coherency_alloc_device (struct device *parent, +const struct coherency_ops *ops,size_t size) +{ + +if (!ops || !ops->wbinv) +return NULL ; + + struct cache_coherency_device *ccd __free(kfree) = kzalloc(size, GFP_KERNEL); + +if (!ccd) +return NULL ; + + ccd->dev.class = &cache_coherency_class; + ccd->dev.parent = parent; + ccd->ops = ops; + ccd->id =ida_alloc (&cache_coherency_ida,GFP_KERNEL ); + +if (dev_set_name (&ccd->dev,"cache_coherency%d" , ccd->id)) +return NULL ; + +device_initialize (&ccd->dev); + +return_ptr (ccd); +} +EXPORT_SYMBOL_NS_GPL (_cache_coherency_alloc_device,"CACHE_COHERENCY" ); + +int cache_coherency_device_register (struct cache_coherency_device *ccd) +{ +return device_add (&ccd->dev); +} +EXPORT_SYMBOL_NS_GPL (cache_coherency_device_register,"CACHE_COHERENCY" ); + +void cache_coherency_device_unregister (struct cache_coherency_device *ccd) +{ +device_del (&ccd->dev); +} +EXPORT_SYMBOL_NS_GPL (cache_coherency_device_unregister,"CACHE_COHERENCY" ); +
drivers/cache/coherency_core.c line 126
Quoted 10 lines
drivers/cache/coherency_core.c new file +10
-0+static int __init cache_coherency_init (void ) +{ +int ret; + + ret =class_register (&cache_coherency_class); +if (ret) +return ret; + +//TODO: generalize +arm64_set_sys_cache_flush_method (&cache_flush_method);
I'm guessing this will blow up the build on non-x86 builds :)
drivers/cache/coherency_core.c
Quoted 5 lines
drivers/cache/coherency_core.c new file +5
-0+struct cache_coherency_device { +struct device dev; +const struct coherency_ops *ops; +int id; +};
Classes are normally for user/kernel apis, what is this going to be used for? I don't see any new user/kernel apis happening, so why do you need a struct device to be created?
thanks,
greg k-h
Re: [RFC PATCH 3/6] cache: coherency device class
Quoted 11 lines · On Thu, 20 Mar 2025 14:12:15 -0700 Greg KH <[email protected]> wrote:
drivers/cache/Kconfig line 5
Quoted 8 lines · Jonathan Cameron
drivers/cache/Kconfig +2
-0@@ -1,6 +1,12 @@ # SPDX-License-Identifier: GPL-2.0 menu "Cache Drivers" +config CACHE_COHERENCY_CLASS + bool "Cache coherency control class"Why can't this be a module? And why would anyone want to turn it off?
If you don't have the hardware you won't want the infrastructure. I'll add a note. If you do have the hardware and don't have memory subsystems that need to use it (maybe no CXL hardware for instance and that's the only user on a particular platform).
Quoted 6 lines
drivers/cache/Kconfig line 8
Quoted 4 lines
drivers/cache/Kconfig +3
-0+ help + Class to which coherency control drivers register allowing core kernel + subsystems to issue invalidations and similar coherency operations.What "core kernel subsystems".
I'll expand on that. Memory hotplug related stuff (currently CXL and NVDIMM) but the thing is more general that that.
Quoted 7 lines
drivers/cache/Kconfig line 12
Quoted 5 lines
drivers/cache/Kconfig +1
-0+ config AX45MP_L2_CACHE bool "Andes Technology AX45MP L2 Cache controller" depends on RISCVShouldn't all of these now depend on
CACHE_COHERENCY_CLASS?
Nope. They are unrelated existing cache related drivers. The question
to Conor is whether he minds me putting this in the existing directory and to others on whether it's a good idea.
Quoted 13 lines
drivers/cache/Makefile line 7
Quoted 11 lines
drivers/cache/Makefile +2
-0@@ -3,3 +3,5 @@ obj-$(CONFIG_AX45MP_L2_CACHE) += ax45mp_cache.o obj-$(CONFIG_SIFIVE_CCACHE) += sifive_ccache.o obj-$(CONFIG_STARFIVE_STARLINK_CACHE) += starfive_starlink_cache.o + +obj-$(CONFIG_CACHE_COHERENCY_CLASS) += coherency_core.oWhy the blank line?
To separate existing stuff that happens to be cache related from this new class. Kind of camping in a directory because seemed silly to have drivers/cache and drivers/cache_coherency
Quoted 127 lines
Quoted 127 lines
drivers/cache/coherency_core.c new file +120
-0@@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Class to manage OS controlled coherency agents within the system. +* Specifically to enable operations such as write back and invalidate. +* +* Copyright: Huawei 2025 +* Some elements based on fwctl class as an example of a modern +* lightweight class. +*/ + +#include <linux/cache_coherency.h> +#include <linux/container_of.h> +#include <linux/idr.h> +#include <linux/fs.h> +#include <linux/module.h> +#include <linux/slab.h> + +#include <asm/cacheflush.h> + +static DEFINE_IDA (cache_coherency_ida); + +static void cache_coherency_device_release (struct device *device) +{ +struct cache_coherency_device *ccd = + container_of(device, struct cache_coherency_device, dev); + +ida_free (&cache_coherency_ida, ccd->id); +} + +static struct class cache_coherency_class = { + .name ="cache_coherency" , + .dev_release = cache_coherency_device_release, +}; + +static int cache_inval_one (struct device *dev,void *data) +{ +struct cache_coherency_device *ccd = + container_of(dev, struct cache_coherency_device, dev); + +if (!ccd->ops) +return -EINVAL ; + +return ccd->ops->wbinv (ccd, data); +} + +static int cache_inval_done_one (struct device *dev,void *data) +{ +struct cache_coherency_device *ccd = + container_of(dev, struct cache_coherency_device, dev); +if (!ccd->ops) +return -EINVAL ; + +return ccd->ops->done (ccd); +} + +static int cache_invalidate_memregion (int res_desc, +phys_addr_t addr,size_t size) +{ +int ret; +struct cc_inval_params params = { + .addr = addr, + .size = size, + }; + + ret =class_for_each_device (&cache_coherency_class,NULL , ¶ms, + cache_inval_one); +if (ret) +return ret; + +return class_for_each_device (&cache_coherency_class,NULL ,NULL , + cache_inval_done_one); +} + +static const struct system_cache_flush_method cache_flush_method = { + .invalidate_memregion = cache_invalidate_memregion, +}; + +struct cache_coherency_device * +_cache_coherency_alloc_device (struct device *parent, +const struct coherency_ops *ops,size_t size) +{ + +if (!ops || !ops->wbinv) +return NULL ; + + struct cache_coherency_device *ccd __free(kfree) = kzalloc(size, GFP_KERNEL); + +if (!ccd) +return NULL ; + + ccd->dev.class = &cache_coherency_class; + ccd->dev.parent = parent; + ccd->ops = ops; + ccd->id =ida_alloc (&cache_coherency_ida,GFP_KERNEL ); + +if (dev_set_name (&ccd->dev,"cache_coherency%d" , ccd->id)) +return NULL ; + +device_initialize (&ccd->dev); + +return_ptr (ccd); +} +EXPORT_SYMBOL_NS_GPL (_cache_coherency_alloc_device,"CACHE_COHERENCY" ); + +int cache_coherency_device_register (struct cache_coherency_device *ccd) +{ +return device_add (&ccd->dev); +} +EXPORT_SYMBOL_NS_GPL (cache_coherency_device_register,"CACHE_COHERENCY" ); + +void cache_coherency_device_unregister (struct cache_coherency_device *ccd) +{ +device_del (&ccd->dev); +} +EXPORT_SYMBOL_NS_GPL (cache_coherency_device_unregister,"CACHE_COHERENCY" ); + +static int __init cache_coherency_init (void ) +{ +int ret; +
Quoted 8 lines
drivers/cache/coherency_core.c line 126
Quoted 6 lines
drivers/cache/coherency_core.c new file +6
-0+ ret =class_register (&cache_coherency_class); +if (ret) +return ret; + +//TODO: generalize +arm64_set_sys_cache_flush_method (&cache_flush_method);I'm guessing this will blow up the build on non-x86 builds :)
Yup. That's a TODO That needs fixing.
Quoted 10 lines
drivers/cache/coherency_core.c
Quoted 6 lines
drivers/cache/coherency_core.c new file +5
-0+struct cache_coherency_device { +struct device dev; +const struct coherency_ops *ops; +int id; +};Classes are normally for user/kernel apis, what is this going to be used for? I don't see any new user/kernel apis happening, so why do you need a struct device to be created?
I'm kind of expecting to grow some userspace ABI around a few things but indeed there isn't any yet. Stuff that has been suggested is:
* Descriptive stuff useful for performance estimation.
* Cache locking - as kind of the opposite of flushes.
* Maybe more direct user interfaces to control it (I'm wary of that though).
Mostly thought, the idea was avoid rolling my own similar registration infrastructure for this case. Absolutely can do a subsystem without a class if that seems to be the way to go. It'll be a little more complex though.
Thanks,
Jonathan
Quoted 5 lines
thanks,
greg k-h
RFC PATCH 4/6 cache: Support cache maintenance for HiSilicon SoC Hydra Home Agent
In reply to Jonathan CameronFrom: Yicong Yang <[email protected]>
Hydra Home Agent is a device used for maintain cache coherency, add support of cache maintenance operations for it.
Same as HiSilicon L3 cache and L3 cache PMU, memory resource of HHA conflicts with that of HHA PMU. Same workaround is implemented here to workaround resource conflict check.
--- drivers/cache/Kconfig | 12 +++ drivers/cache/Makefile | 1 + drivers/cache/hisi_soc_hha.c | 193 +++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+)
drivers/cache/Kconfig +12 -0
@@ -6,6 +6,18 @@ config CACHE_COHERENCY_CLASS help Class to which coherency control drivers register allowing core kernel subsystems to issue invalidations and similar coherency operations. +if CACHE_COHERENCY_CLASS + +config HISI_SOC_HHA + tristate "HiSilicon Hydra Home Agent (HHA) device driver" + depends on ARM64 && ACPI || COMPILE_TEST + help + The Hydra Home Agent (HHA) is responsible of cache coherency + on SoC. This drivers provides cache maintenance functions of HHA. + + This driver can be built as a module. If so, the module will be + called hisi_soc_hha. +endif config AX45MP_L2_CACHE bool "Andes Technology AX45MP L2 Cache controller"
drivers/cache/Makefile +1 -0
@@ -5,3 +5,4 @@ obj-$(CONFIG_SIFIVE_CCACHE) += sifive_ccache.o obj-$(CONFIG_STARFIVE_STARLINK_CACHE) += starfive_starlink_cache.o obj-$(CONFIG_CACHE_COHERENCY_CLASS) += coherency_core.o +obj-$(CONFIG_HISI_SOC_HHA) += hisi_soc_hha.o
drivers/cache/hisi_soc_hha.c new file +193 -0
193 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 5/6 acpi: PoC of Cache control via ACPI0019 and _DSM
In reply to Jonathan CameronDo not merge. This is the bare outline of what may become an ACPI code first specification proposal.
From various discussions, it has become clear that there is some desire not to end up needing a cache flushing driver for every host that supports the flush by PA range functionality that is needed for CXL and similar disagregated memory pools where the host PA mapping to actual memory may change over time and where various races can occur with prefetchers making it hard to use CPU instructions to flush all stale data out.
There was once an ARM PSCI specification [1] that defined a firmware interface to solve this problem. However that meant dropping into a more privileged mode, or chatting to an external firmware. That was overkill for those systems that provide a simple MMIO register interface for these operations. That specification never made it beyond alpha level.
For the typical class of machine that actually uses these disaggregated pools, ACPI can potentially provide the same benefits with a great deal more flexibility. A _DSM in DSDT via operation regions, may be used to do any of:
1) Make firmware calls
2) Operate a register based state machine.
3) Most other things you might dream of.
This was prototyped against an implementation of the ARM specification in [1] wrapped up in _DSM magic. That was chosen to give a second (be it abandoned) example of how this cache control class can be used.
DSDT blob example that uses PSCI underneath.
Device (CCT0)
{
Name (_HID, "ACPI0019") // _HID: Hardware ID
Name (_UID, Zero) // _UID: Unique ID
OperationRegion (AFFH, FFixedHW, One, 0x90)
Field (AFFH, BufferAcc, NoLock, Preserve)
{
AFFX, 1152
}
Method (_DSM, 4, Serialized) // _DSM: Device-Specific Method
{
If ((Arg0 == ToUUID ("61fdc7d5-1468-4807-b565-515bf6b75319") /* Unknown UUID */))
{
If ((Arg2 == Zero))
{
Return (Buffer (One)
{
0x0F // .
})
}
If ((Arg2 == One))
{
Name (CCR1, Buffer (0x08)
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
})
CreateDWordField (CCR1, Zero, RRR1)
CCR1 = AFFX = Buffer (0x10)
{
/* 0000 */ 0x17, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0x00, // ........
/* 0008 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
}
Name (CCOP, Zero)
CCOP = RRR1 /* \_SB_.CCT0._DSM.RRR1 */
CCR1 = AFFX = Buffer (0x10)
{
/* 0000 */ 0x17, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0x00, // ........
/* 0008 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
}
Name (CCLA, Zero)
CCLA = RRR1 /* \_SB_.CCT0._DSM.RRR1 */
CCR1 = AFFX = Buffer (0x10)
{
/* 0000 */ 0x17, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0x00, // ........
/* 0008 */ 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
}
Name (CCRL, Zero)
CCRL = RRR1 /* \_SB_.CCT0._DSM.RRR1 */
Return (Package (0x04)
{
CCOP,
CCLA,
CCRL,
One
})
}
If ((Arg2 == 0x02))
{
If (!((ObjectType (Arg3) == 0x04) && (SizeOf (Arg3) == 0x03)))
{
Return (One)
}
Name (BUFF, Buffer (0x28)
{
/* 0000 */ 0x16, 0x00, 0x00, 0xC4, 0x00, 0x00, 0x00, 0x00, // ........
/* 0008 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
/* 0010 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
/* 0018 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ........
/* 0020 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // ........
})
CreateQWordField (BUFF, 0x08, BFBA)
CreateQWordField (BUFF, 0x10, BFSZ)
CreateQWordField (BUFF, 0x20, BFFL)
Local0 = Arg3 [Zero]
BFBA = DerefOf (Local0)
Local0 = Arg3 [One]
BFSZ = DerefOf (Local0)
Local0 = Arg3 [0x02]
Local1 = DerefOf (Local0)
If ((Local1 != Zero))
{
Return (One)
}
AFFX = BUFF /* \_SB_.CCT0._DSM.BUFF */
Return (Zero)
}
If ((Arg2 == 0x03))
{
Return (Zero)
}
}
}
}--- drivers/acpi/Makefile | 1 + drivers/cache/Kconfig | 8 ++ drivers/cache/Makefile | 1 + drivers/cache/acpi_cache_control.c | 157 +++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+)
drivers/acpi/Makefile +1 -0
@@ -129,3 +129,4 @@ obj-$(CONFIG_ACPI_VIOT) += viot.o obj-$(CONFIG_RISCV) += riscv/ obj-$(CONFIG_X86) += x86/ +
drivers/cache/Kconfig +8 -0
@@ -17,6 +17,14 @@ config HISI_SOC_HHA This driver can be built as a module. If so, the module will be called hisi_soc_hha. + +config ACPI_CACHE_CONTROL + tristate "ACPI cache maintenance" + depends on ARM64 + help + ACPI0019 device ID in DSDT identifies an interface that may be used + to carry out certain forms of cache flush operation. + endif config AX45MP_L2_CACHE
drivers/cache/Makefile +1 -0
@@ -6,3 +6,4 @@ obj-$(CONFIG_STARFIVE_STARLINK_CACHE) += starfive_starlink_cache.o obj-$(CONFIG_CACHE_COHERENCY_CLASS) += coherency_core.o obj-$(CONFIG_HISI_SOC_HHA) += hisi_soc_hha.o +obj-$(CONFIG_ACPI_CACHE_CONTROL) += acpi_cache_control.o
drivers/cache/acpi_cache_control.c new file +157 -0
157 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH 6/6 Hack: Pretend we have PSCI 1.2
In reply to Jonathan CameronNeed to update QEMU to PSCI 1.2. In meantime lie.
This is just here to aid testing, not for review!
--- drivers/firmware/psci/psci.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/firmware/psci/psci.c +2 -0
@@ -646,6 +646,8 @@ static void __init psci_init_smccc(void) } } +/* Hack until qemu version stuff updated */ +arm_smccc_version_init (ARM_SMCCC_VERSION_1_2 , psci_conduit);/* * Conveniently, the SMCCC and PSCI versions are encoded the * same way. No, this isn't accidental.
Signature · 1 line
2.43.0
Conor Dooley
maintainerQuoted 33 lines · Jonathan Cameron
Note that I've only a vague idea of who will care about this so please do +CC others as needed.
On x86 there is the much loved WBINVD instruction that causes a write back and invalidate of all caches in the system. It is expensive but it is necessary in a few corner cases. These are cases where the contents of Physical Memory may change without any writes from the host. Whilst there are a few reasons this might happen, the one I care about here is when we are adding or removing mappings on CXL. So typically going from there being actual memory at a host Physical Address to nothing there (reads as zero, writes dropped) or visa-versa. That involves the reprogramming of address decoders (HDM Decoders); in the near future it may also include the device offering dynamic capacity extents. The thing that makes it very hard to handle with CPU flushes is that the instructions are normally VA based and not guaranteed to reach beyond the Point of Coherence or similar. You might be able to (ab)use various flush operations intended to ensure persistence memory but in general they don't work either.
So on other architectures such as ARM64 we have no instruction similar to WBINVD but we may have device interfaces in the system that provide a way to ensure a PA range undergoes the write back and invalidate action. This RFC is to find a way to support those cache maintenance device interfaces. The ones I know about are much more flexible than WBINVD, allowing invalidation of particular PA ranges, or a much richer set of flush types (not supported yet as not needed for upstream use cases).
To illustrate how a solution might work, I've taken both a HiSilicon design (slight quirk as registers overlap with existing PMU driver) and more controversially a firmware interface proposal from ARM (wrapped up in made up ACPI) that was dropped from the released spec but for which the alpha spec is still available.
Quoted 4 lines
Why
drivers/cache?
- Mainly because it exists and smells like a reasonable place.
- Conor, you are maintainer for this currently do you mind us putting this stuff in there?
drivers/cache was just something to put the cache controller drivers we have on RISC-V that implement the various arch_dma*() callbacks in non-standard ways that made more sense than drivers/soc/<soc vendor> since the controllers are IP provided by CPU vendors. There's only two drivers here now, but I am aware of another two non-standard CMO mechanisms if the silicon with them so there'll likely be more in the future :) I'm only really maintainer of it to avoid it being another thing for Palmer to look after :)
I've only skimmed this for now, but I think it is reasonable to put them here. Maybe my skim is showing, but it would not surprise me to see a driver providing both non-standard arch_dma*() callbacks as well as dealing with CXL mappings via this new class on RISC-V in the future.. Either way, I think it'd probably be a good idea to add ?you? as a co-maintainer if the directory is going to be used for your proposed interface/drivers, for what I hope is an obvious reason!
Quoted 39 lines · On Fri, 21 Mar 2025 22:32:15 +0000 Conor Dooley <[email protected]> wrote:
Quoted 33 lines · Jonathan Cameron
Note that I've only a vague idea of who will care about this so please do +CC others as needed.
On x86 there is the much loved WBINVD instruction that causes a write back and invalidate of all caches in the system. It is expensive but it is necessary in a few corner cases. These are cases where the contents of Physical Memory may change without any writes from the host. Whilst there are a few reasons this might happen, the one I care about here is when we are adding or removing mappings on CXL. So typically going from there being actual memory at a host Physical Address to nothing there (reads as zero, writes dropped) or visa-versa. That involves the reprogramming of address decoders (HDM Decoders); in the near future it may also include the device offering dynamic capacity extents. The thing that makes it very hard to handle with CPU flushes is that the instructions are normally VA based and not guaranteed to reach beyond the Point of Coherence or similar. You might be able to (ab)use various flush operations intended to ensure persistence memory but in general they don't work either.
So on other architectures such as ARM64 we have no instruction similar to WBINVD but we may have device interfaces in the system that provide a way to ensure a PA range undergoes the write back and invalidate action. This RFC is to find a way to support those cache maintenance device interfaces. The ones I know about are much more flexible than WBINVD, allowing invalidation of particular PA ranges, or a much richer set of flush types (not supported yet as not needed for upstream use cases).
To illustrate how a solution might work, I've taken both a HiSilicon design (slight quirk as registers overlap with existing PMU driver) and more controversially a firmware interface proposal from ARM (wrapped up in made up ACPI) that was dropped from the released spec but for which the alpha spec is still available.
Quoted 4 lines
Why
drivers/cache?
- Mainly because it exists and smells like a reasonable place.
- Conor, you are maintainer for this currently do you mind us putting this stuff in there?
Quoted 8 lines
drivers/cachewas just something to put the cache controller drivers we have on RISC-V that implement the various arch_dma*() callbacks in non-standard ways that made more sense than drivers/soc/<soc vendor> since the controllers are IP provided by CPU vendors. There's only two drivers here now, but I am aware of another two non-standard CMO mechanisms if the silicon with them so there'll likely be more in the future :) I'm only really maintainer of it to avoid it being another thing for Palmer to look after :)
I suspected as much :)
Quoted 5 lines
I've only skimmed this for now, but I think it is reasonable to put them here. Maybe my skim is showing, but it would not surprise me to see a driver providing both non-standard arch_dma*() callbacks as well as dealing with CXL mappings via this new class on RISC-V in the future..
Absolutely. The use of an ARM callback was just a place holder for now (Greg pointed that one out as well as I forgot to mention it in the patch description!)
I think this will turn out to be at least some subset of implementations for other architectures unless they decide to go the route of an instruction (like x86).
Quoted 3 lines
Either way, I think it'd probably be a good idea to add ?you? as a co-maintainer if the directory is going to be used for your proposed interface/drivers, for what I hope is an obvious reason!
Sure. That would make sense.
Jonathan