Re: [RFC v5.1 9/9] [DON'T APPLY] cache: sifive-ccache: add cache flushing capability

Ben Dooks
2023-01-04T09:50:41Z

On 03/01/2023 21:04, Conor Dooley wrote:
> From: Daire McNamara <daire.mcnamara@microchip.com>
>
> SiFive L2 cache controller can flush L2 cache. Expose this capability via
> driver.
>
> Signed-off-by: Daire McNamara <daire.mcnamara@microchip.com>
> [Conor: rebase on top of move to cache subsystem]
> Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
> ---
> This commit needs more work, and a way to enable it from errata. I've
> not gone and done this as PolarFire SoC has archid etc all set to zero.
> So we need to go figure out a workaround for this, before adding in
> errata enabling code for this. I've included it here as a second user of
> the cache management stuff, since what's currently upstream for the
> ccache driver does not do any cache management.

I think errata isn't the right word here, it's more of a system
requirement for anything that isn't coherent. All the SiFive systems
I have are coherent so won't need this.

> ---
> drivers/cache/sifive_ccache.c | 45 +++++++++++++++++++++++++++++++++++
> 1 file changed, 45 insertions(+)
>
> diff --git a/drivers/cache/sifive_ccache.c b/drivers/cache/sifive_ccache.c
> index 47e7d6557f85..3c00f205bace 100644
> --- a/drivers/cache/sifive_ccache.c
> +++ b/drivers/cache/sifive_ccache.c
> @@ -9,12 +9,14 @@
> #define pr_fmt(fmt) "CCACHE: " fmt
>
> #include <linux/debugfs.h>
> +#include <linux/dma-direction.h>
> #include <linux/interrupt.h>
> #include <linux/of_irq.h>
> #include <linux/of_address.h>
> #include <linux/device.h>
> #include <linux/bitfield.h>
> #include <asm/cacheinfo.h>
> +#include <asm/cacheflush.h>
> #include <cache/sifive_ccache.h>
>
> #define SIFIVE_CCACHE_DIRECCFIX_LOW 0x100
> @@ -42,11 +44,15 @@
> #define SIFIVE_CCACHE_WAYENABLE 0x08
> #define SIFIVE_CCACHE_ECCINJECTERR 0x40
>
> +#define SIFIVE_CCACHE_FLUSH64 0x200
> +#define SIFIVE_CCACHE_FLUSH32 0x240
> +
> #define SIFIVE_CCACHE_MAX_ECCINTR 4
>
> static void __iomem *ccache_base;
> static int g_irq[SIFIVE_CCACHE_MAX_ECCINTR];
> static struct riscv_cacheinfo_ops ccache_cache_ops;
> +static struct riscv_cache_maint_ops ccache_cmos;
> static int level;
>
> enum {
> @@ -205,6 +211,42 @@ static irqreturn_t ccache_int_handler(int irq, void *device)
> return IRQ_HANDLED;
> }
>
> +static void sifive_ccache_dma_wback_inv(void* vaddr, unsigned long size)
> +{
> + void * __iomem flush = ccache_base + SIFIVE_CCACHE_FLUSH64;
> + phys_addr_t start = virt_to_phys(vaddr);
> + phys_addr_t aligned_start = start & ~0x3f;
> + u64 addr;
> + u64 end;
> + u64 aligned_end;
> +
> + size += start - aligned_start;
> + end = start + size;
> + aligned_end = end += 0x3f;

I think you meant + 0x3f here. There is an align macro in the kernel
headers, and I'm not sure by inspection if you'd miss the last line
with this code.

> + aligned_end &= ~0x3f;
> +
> + for (addr = aligned_start; addr < aligned_end; addr += 64)
> + writeq(addr, flush);
> +}

The p550 manual states that the zero device flush method is quicker for
any large area flush. However not sure what that level is and whether it
is worth dealing with here? If so we need to have the L3 zero are mapped.

> +
> +static void sifive_ccache_cmo(unsigned int cache_size, void *vaddr, size_t size,
> + int dir, int ops)
> +{

technically dir should have been of type "enum dma_data_direction"

> + switch (dir) {
> + case DMA_TO_DEVICE:
> + sifive_ccache_dma_wback_inv(vaddr, size);
> + break;
> + case DMA_FROM_DEVICE:
> + sifive_ccache_dma_wback_inv(vaddr, size);
> + break;
> + case DMA_BIDIRECTIONAL:
> + sifive_ccache_dma_wback_inv(vaddr, size);
> + break;
> + default:
> + break;
> + }
> +}

I'm not sure why you'd bother checking the dir here, the cache can
only be flushed (I hope DMA_FROM_DEVICE is done /before/ the DMA op).

You could have saved yourself an include if just ignoring dir.

> +
> static int __init sifive_ccache_init(void)
> {
> struct device_node *np;
> @@ -254,6 +296,9 @@ static int __init sifive_ccache_init(void)
> ccache_cache_ops.get_priv_group = ccache_get_priv_group;
> riscv_set_cacheinfo_ops(&ccache_cache_ops);
>
> + ccache_cmos.cmo_patchfunc = sifive_ccache_cmo;
> + riscv_set_cache_maint_ops(&ccache_cmos);
> +
> #ifdef CONFIG_DEBUG_FS
> setup_sifive_debug();
> #endif

--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius