[PATCH v7 04/12] lib: utils/cache: Add fdt cmo helpers
Anup Patel
anup at brainfault.org
Mon Oct 27 22:54:45 PDT 2025
On Mon, Oct 20, 2025 at 11:56 AM Nick Hu <nick.hu at sifive.com> wrote:
>
> Add the helpers to build up the cache hierarchy via FDT and provide some
> cmo functions for the user who want to flush the entire cache.
>
> Signed-off-by: Nick Hu <nick.hu at sifive.com>
> Reviewed-by: Samuel Holland <samuel.holland at sifive.com>
> Reviewed-by: Anup Patel <anup at brainfault.org>
> ---
> include/sbi_utils/cache/fdt_cmo_helper.h | 40 +++++++++++
> lib/utils/cache/fdt_cmo_helper.c | 112 +++++++++++++++++++++++++++++++
> lib/utils/cache/objects.mk | 1 +
> platform/generic/platform.c | 3 +-
> 4 files changed, 155 insertions(+), 1 deletion(-)
>
> diff --git a/include/sbi_utils/cache/fdt_cmo_helper.h b/include/sbi_utils/cache/fdt_cmo_helper.h
> new file mode 100644
> index 0000000000000000000000000000000000000000..a6a28db9ede2722d8261d2e077eeb7710cff0893
> --- /dev/null
> +++ b/include/sbi_utils/cache/fdt_cmo_helper.h
> @@ -0,0 +1,40 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2025 SiFive Inc.
> + */
> +
> +#ifndef __FDT_CMO_HELPER_H__
> +#define __FDT_CMO_HELPER_H__
> +
> +#ifdef CONFIG_FDT_CACHE
> +/**
> + * Flush the private first level cache of the current hart
> + *
> + * @return 0 on success, or a negative error code on failure
> + */
> +int fdt_cmo_private_flc_flush_all(void);
> +
> +/**
> + * Flush the last level cache of the current hart
> + *
> + * @return 0 on success, or a negative error code on failure
> + */
> +int fdt_cmo_llc_flush_all(void);
> +
> +/**
> + * Initialize the cache devices for each hart
> + *
> + * @param fdt devicetree blob
> + * @param cold_boot cold init or warm init
> + *
> + * @return 0 on success, or a negative error code on failure
> + */
> +int fdt_cmo_init(bool cold_boot);
> +
> +#else
> +
> +static inline int fdt_cmo_init(bool cold_boot) { return 0; }
> +
> +#endif /* CONFIG_FDT_CACHE */
> +#endif /* __FDT_CMO_HELPER_H__ */
> diff --git a/lib/utils/cache/fdt_cmo_helper.c b/lib/utils/cache/fdt_cmo_helper.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..6d2e853dd735735092c57db2398f97a64de3ab5e
> --- /dev/null
> +++ b/lib/utils/cache/fdt_cmo_helper.c
> @@ -0,0 +1,112 @@
> +/*
> + * SPDX-License-Identifier: BSD-2-Clause
> + *
> + * Copyright (c) 2025 SiFive Inc.
> + */
> +
> +#include <libfdt.h>
> +#include <sbi/sbi_error.h>
> +#include <sbi/sbi_scratch.h>
> +#include <sbi_utils/cache/fdt_cache.h>
> +#include <sbi_utils/cache/fdt_cmo_helper.h>
> +#include <sbi_utils/fdt/fdt_helper.h>
> +
> +static unsigned long flc_offset;
> +
> +#define get_hart_flc(_s) \
> + sbi_scratch_read_type(_s, struct cache_device *, flc_offset)
> +#define set_hart_flc(_s, _p) \
> + sbi_scratch_write_type(_s, struct cache_device *, flc_offset, _p)
> +
> +int fdt_cmo_private_flc_flush_all(void)
> +{
> + struct cache_device *flc = get_hart_flc(sbi_scratch_thishart_ptr());
> +
> + if (!flc || !flc->cpu_private)
> + return SBI_ENODEV;
> +
> + return cache_flush_all(flc);
> +}
> +
> +int fdt_cmo_llc_flush_all(void)
> +{
> + struct cache_device *llc = get_hart_flc(sbi_scratch_thishart_ptr());
> +
> + if (!llc)
> + return SBI_ENODEV;
> +
> + while (llc->next)
> + llc = llc->next;
> +
> + return cache_flush_all(llc);
> +}
> +
> +static int fdt_cmo_cold_init(const void *fdt)
> +{
> + struct sbi_scratch *scratch;
> + struct cache_device *dev;
> + int cpu_offset, cpus_offset, rc;
> + u32 hartid;
> +
> + cpus_offset = fdt_path_offset(fdt, "/cpus");
> + if (cpus_offset < 0)
> + return SBI_EINVAL;
> +
> + fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
> + rc = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
> + if (rc)
> + continue;
> +
> + scratch = sbi_hartid_to_scratch(hartid);
> + if (!scratch)
> + continue;
> +
> + rc = fdt_next_cache_get(fdt, cpu_offset, &dev);
> + if (rc)
> + return rc;
The "next-level-cache" property might be absent in CPU DT nodes
so fdt_next_cache_get() must return SBI_ENOENT when it is
absent.
Also, over here we must handle "rc == SBI_ENOENT" as special
case as follows:
diff --git a/lib/utils/cache/fdt_cmo_helper.c b/lib/utils/cache/fdt_cmo_helper.c
index 6d2e853d..d87bab76 100644
--- a/lib/utils/cache/fdt_cmo_helper.c
+++ b/lib/utils/cache/fdt_cmo_helper.c
@@ -62,8 +62,10 @@ static int fdt_cmo_cold_init(const void *fdt)
continue;
rc = fdt_next_cache_get(fdt, cpu_offset, &dev);
- if (rc)
+ if (rc && rc != SBI_ENOENT)
return rc;
+ if (rc == SBI_ENOENT)
+ dev = NULL;
set_hart_flc(scratch, dev);
}
I will take care of the above at the time of merging this series.
Regards,
Anup
More information about the opensbi
mailing list