Skip to contentSkip to conversation

lib: utils/cache: Add SiFive PL2 and EC cache drivers

PATCH Applied Nick Hu 2 participants 7 messages Nov 14, 2025 – Dec 8, 2025 SiFive drivers

lore
Share & tools
lore

Nick Hu

author
#0 lore raw

cover lib: utils/cache: Add SiFive PL2 and EC cache drivers

Add the SiFive PL2 and EC cache drivers to support the external cache maintenance operations.

---
Nick Hu (3):
lib: utils/cache: Handle last-level cache correctly in fdt_cache_add()
lib: utils/cache: Add SiFive PL2 controller
lib: utils/cache: Add SiFive Extensible Cache (EC) driver

 lib/utils/cache/Kconfig            |   8 ++
 lib/utils/cache/fdt_cache.c        |   4 +-
 lib/utils/cache/fdt_sifive_ec.c    | 194 +++++++++++++++++++++++++++++++++++++
 lib/utils/cache/fdt_sifive_pl2.c   | 139 ++++++++++++++++++++++++++
 lib/utils/cache/objects.mk         |   6 ++
 platform/generic/configs/defconfig |   2 +
 6 files changed, 352 insertions(+), 1 deletion(-)

---
base-commit: fade4399d2a47a3ff9c5ecd5dd4c3cba7a4eace7
change-id: 20251029-sifive-cache-drivers-bf918d1337ea

Best regards,

Signature · 7 lines
Nick Hu <[email protected]>


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#1 lore raw

PATCH 1/3 lib: utils/cache: Handle last-level cache correctly in fdt_cache_add()

The fdt_cache_add() helper attempts to retrieve the next-level cache and returns SBI_ENOENT when there is none. Since this condition only indicates that the current cache is the last-level cache, the helper should not treat it as an error.

---
 lib/utils/cache/fdt_cache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/utils/cache/fdt_cache.c +3 -1
index aeaa613830960b67bf5f512a101f0242d8692872..2a9e8a6dd53c008eec8f319d0e19c56ea1d11a1a 100644
@@ -23,7 +23,9 @@ int fdt_cache_add(const void *fdt, int noff, struct cache_device *dev)
 	sbi_dprintf("%s: %s\n", __func__, dev->name);
 
 	rc = fdt_next_cache_get(fdt, noff, &dev->next);
-	if (rc)
+	if (rc == SBI_ENOENT)
+		dev->next = NULL;
+	else if (rc)
 		return rc;
 
 	return cache_add(dev);
Signature · 7 lines
2.34.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/3 Applied
#2 lore raw

Re: [PATCH 1/3] lib: utils/cache: Handle last-level cache correctly in fdt_cache_add()

Quoted 7 lines · Nick Hu

The fdt_cache_add() helper attempts to retrieve the next-level cache and returns SBI_ENOENT when there is none. Since this condition only indicates that the current cache is the last-level cache, the helper should not treat it as an error.

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 28 lines
---
 lib/utils/cache/fdt_cache.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/utils/cache/fdt_cache.c +3 -2
index aeaa613830960b67bf5f512a101f0242d8692872..2a9e8a6dd53c008eec8f319d0e19c56ea1d11a1a 100644
@@ -23,7 +23,9 @@ int fdt_cache_add(const void *fdt, int noff, struct cache_device *dev)
        sbi_dprintf("%s: %s\n", __func__, dev->name);
 
        rc = fdt_next_cache_get(fdt, noff, &dev->next);
-       if (rc)
+       if (rc == SBI_ENOENT)
+               dev->next = NULL;
+       else if (rc)
                return rc;
 
        return cache_add(dev);
 
--

2.34.1

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#3 lore raw

PATCH 2/3 lib: utils/cache: Add SiFive PL2 controller

In reply to Nick Hu

SiFive Private L2(PL2) cache is a private cache owned by each hart. Add this driver to support private cache flush operations via the MMIO registers.

---
 lib/utils/cache/Kconfig            |   4 ++
 lib/utils/cache/fdt_sifive_pl2.c   | 139 +++++++++++++++++++++++++++++++++++++
 lib/utils/cache/objects.mk         |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 147 insertions(+)
lib/utils/cache/Kconfig +4 -0
index 1c7abdc9ad93319569807b4e25623cac871c6ce3..e28262fa2e81328460c8ee9a34b841454d03d776 100644
@@ -14,6 +14,10 @@ config FDT_CACHE_SIFIVE_CCACHE
 	bool "SiFive CCACHE FDT cache driver"
 	default n
 
+config FDT_CACHE_SIFIVE_PL2
+	bool "SiFive PL2 FDT cache driver"
+	default n
+
 endif
 
 config CACHE
lib/utils/cache/fdt_sifive_pl2.c new file +139 -0

139 lines not on this page · raw message

lib/utils/cache/objects.mk +3 -0
index 6829a966784732127d93180b3eb46ed24de5bc7d..37d250d1001bc647e3ae05304b2278e5dc608cae 100644
@@ -11,4 +11,7 @@ libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cmo_helper.o
 carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += fdt_sifive_ccache
 libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += cache/fdt_sifive_ccache.o
 
+carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += fdt_sifive_pl2
+libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += cache/fdt_sifive_pl2.o
+
 libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
platform/generic/configs/defconfig +1 -0
index 3896b0e03268ad3520a3d1586b652e38c0b54ceb..1bb14c78b9f0fc98493961023703a1ed2a24eea8 100644
@@ -13,6 +13,7 @@ CONFIG_PLATFORM_MIPS_P8700=y
 CONFIG_PLATFORM_SPACEMIT_K1=y
 CONFIG_FDT_CACHE=y
 CONFIG_FDT_CACHE_SIFIVE_CCACHE=y
+CONFIG_FDT_CACHE_SIFIVE_PL2=y
 CONFIG_FDT_CPPC=y
 CONFIG_FDT_CPPC_RPMI=y
 CONFIG_FDT_GPIO=y
Signature · 7 lines
2.34.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 2/3 Applied
#4 lore raw

Re: [PATCH 2/3] lib: utils/cache: Add SiFive PL2 controller

Quoted 95 lines · Nick Hu

SiFive Private L2(PL2) cache is a private cache owned by each hart. Add this driver to support private cache flush operations via the MMIO registers.

---
 lib/utils/cache/Kconfig            |   4 ++
 lib/utils/cache/fdt_sifive_pl2.c   | 139 +++++++++++++++++++++++++++++++++++++
 lib/utils/cache/objects.mk         |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 147 insertions(+)
lib/utils/cache/Kconfig +4 -0
index 1c7abdc9ad93319569807b4e25623cac871c6ce3..e28262fa2e81328460c8ee9a34b841454d03d776 100644
@@ -14,6 +14,10 @@ config FDT_CACHE_SIFIVE_CCACHE
        bool "SiFive CCACHE FDT cache driver"
        default n
 
+config FDT_CACHE_SIFIVE_PL2
+       bool "SiFive PL2 FDT cache driver"
+       default n
+
 endif
 
 config CACHE
lib/utils/cache/fdt_sifive_pl2.c new file +53 -0
new file mode 100644
index 0000000000000000000000000000000000000000..afe5d6796dbf3504604642643b49cbb4ccc858c6
@@ -0,0 +1,139 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi_utils/cache/fdt_cache.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+
+#define FLUSH64_CMD_TARGET_ALL         (0x2 << 3)
+#define FLUSH64_CMD_TYPE_FLUSH         0x3ULL
+
+#define SIFIVE_PL2CACHE_CMD_QLEN       0xff
+
+#define SIFIVE_PL2CACHE_FLUSH64_OFF    0x200ULL
+#define SIFIVE_PL2CACHE_STATUS_OFF     0x208ULL
+#define SIFIVE_PL2CACHE_CONFIG1_OFF    0x1000ULL
+#define SIFIVE_PL2CACHE_CONFIG0_OFF    0x1008ULL
+
+#define FLUSH64_CMD_POS                        56
+#define REGIONCLOCKDISABLE_MASK                BIT(3)
+
+#define CONFIG0_ACCEPT_DIRTY_DATA_ENABLE       BIT(24)
+
+struct sifive_pl2_quirks {
+       bool no_dirty_fill;
+};
+
+struct sifive_pl2 {
+       struct cache_device dev;
+       void *addr;
+       bool no_dirty_fill;
+};
+
+#define to_pl2(_dev) container_of(_dev, struct sifive_pl2, dev)
+
+static int sifive_pl2_flush_all(struct cache_device *dev)
+{
+       struct sifive_pl2 *pl2_dev = to_pl2(dev);
+       char *addr = pl2_dev->addr;
+       u64 cmd = (FLUSH64_CMD_TARGET_ALL | FLUSH64_CMD_TYPE_FLUSH) << FLUSH64_CMD_POS;
+       u32 config0;
+
+       /*
+        * While flushing pl2 cache, a speculative load might causes a dirty line pull into PL2. It
+        * will cause the SiFive SMC0 refuse to enter the power gating. Disable the
+        * ACCEPT_DIRTY_DATA_ENABLE to avoid the issue.
+        */
+       if (pl2_dev->no_dirty_fill) {

lib/utils/cache/fdt_sifive_pl2.c line 63

Quoted 10 lines
lib/utils/cache/fdt_sifive_pl2.c new file +10 -0
+               config0 = readl((void *)addr + SIFIVE_PL2CACHE_CONFIG0_OFF);
+               config0 &= ~CONFIG0_ACCEPT_DIRTY_DATA_ENABLE;
+               writel(config0, (void *)addr + SIFIVE_PL2CACHE_CONFIG0_OFF);
+       }
+
+#if __riscv_xlen != 32
+       writeq(cmd, addr + SIFIVE_PL2CACHE_FLUSH64_OFF);
+#else
+       writel((u32)cmd, addr + SIFIVE_PL2CACHE_FLUSH64_OFF);
+       writel((u32)cmd >> 32, addr + SIFIVE_PL2CACHE_FLUSH64_OFF + sizeof(u32));

This does not compile on RV32. I will fix it at the time of merging.

Quoted 105 lines
lib/utils/cache/fdt_sifive_pl2.c new file +76 -0
+#endif
+       do {} while (readl(addr + SIFIVE_PL2CACHE_STATUS_OFF) & SIFIVE_PL2CACHE_CMD_QLEN);
+
+       return 0;
+}
+
+static int sifive_pl2_warm_init(struct cache_device *dev)
+{
+       struct sifive_pl2 *pl2_dev = to_pl2(dev);
+       char *addr = pl2_dev->addr;
+       u32 val;
+
+       /* Enabling the clock gating */
+       val = readl(addr + SIFIVE_PL2CACHE_CONFIG1_OFF);
+       val &= (~REGIONCLOCKDISABLE_MASK);
+       writel(val, addr + SIFIVE_PL2CACHE_CONFIG1_OFF);
+
+       return 0;
+}
+
+static struct cache_ops sifive_pl2_ops = {
+       .warm_init = sifive_pl2_warm_init,
+       .cache_flush_all = sifive_pl2_flush_all,
+};
+
+static int sifive_pl2_cold_init(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       const struct sifive_pl2_quirks *quirk = match->data;
+       struct sifive_pl2 *pl2_dev;
+       struct cache_device *dev;
+       u64 reg_addr;
+       int rc;
+
+       /* find the pl2 control base address */
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &reg_addr, NULL);
+       if (rc < 0 && reg_addr)
+               return SBI_ENODEV;
+
+       pl2_dev = sbi_zalloc(sizeof(*pl2_dev));
+       if (!pl2_dev)
+               return SBI_ENOMEM;
+
+       dev = &pl2_dev->dev;
+       dev->ops = &sifive_pl2_ops;
+       dev->cpu_private = true;
+
+       rc = fdt_cache_add(fdt, nodeoff, dev);
+       if (rc)
+               return rc;
+
+       pl2_dev->addr = (void *)(uintptr_t)reg_addr;
+       if (quirk)
+               pl2_dev->no_dirty_fill = quirk->no_dirty_fill;
+
+       return 0;
+}
+
+static const struct sifive_pl2_quirks pl2cache2_quirks = {
+       .no_dirty_fill = true,
+};
+
+static const struct sifive_pl2_quirks pl2cache0_quirks = {
+       .no_dirty_fill = false,
+};
+
+static const struct fdt_match sifive_pl2_match[] = {
+       { .compatible = "sifive,pl2cache2", .data = &pl2cache2_quirks },
+       { .compatible = "sifive,pl2cache1", .data = &pl2cache0_quirks },
+       { .compatible = "sifive,pl2cache0", .data = &pl2cache0_quirks },
+       {},
+};
+
+struct fdt_driver fdt_sifive_pl2 = {
+       .match_table = sifive_pl2_match,
+       .init = sifive_pl2_cold_init,
+};
lib/utils/cache/objects.mk +3 -0
index 6829a966784732127d93180b3eb46ed24de5bc7d..37d250d1001bc647e3ae05304b2278e5dc608cae 100644
@@ -11,4 +11,7 @@ libsbiutils-objs-$(CONFIG_FDT_CACHE) += cache/fdt_cmo_helper.o
 carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += fdt_sifive_ccache
 libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += cache/fdt_sifive_ccache.o
 
+carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += fdt_sifive_pl2
+libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += cache/fdt_sifive_pl2.o
+
 libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
platform/generic/configs/defconfig +1 -1
index 3896b0e03268ad3520a3d1586b652e38c0b54ceb..1bb14c78b9f0fc98493961023703a1ed2a24eea8 100644
@@ -13,6 +13,7 @@ CONFIG_PLATFORM_MIPS_P8700=y
 CONFIG_PLATFORM_SPACEMIT_K1=y
 CONFIG_FDT_CACHE=y
 CONFIG_FDT_CACHE_SIFIVE_CCACHE=y
+CONFIG_FDT_CACHE_SIFIVE_PL2=y
 CONFIG_FDT_CPPC=y
 CONFIG_FDT_CPPC_RPMI=y
 CONFIG_FDT_GPIO=y
 
--

2.34.1

Quoted 4 lines

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

Otherwise, LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
author
#5 lore raw

PATCH 3/3 lib: utils/cache: Add SiFive Extensible Cache (EC) driver

In reply to Nick Hu

Add support for SiFive Extensible Cache (EC) controller with multi-slice architecture. The driver implements cache maintenance operations through MMIO register interface.

---
 lib/utils/cache/Kconfig            |   4 +
 lib/utils/cache/fdt_sifive_ec.c    | 194 +++++++++++++++++++++++++++++++++++++
 lib/utils/cache/objects.mk         |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 202 insertions(+)
lib/utils/cache/Kconfig +4 -0
index e28262fa2e81328460c8ee9a34b841454d03d776..be7d57c37d2477e60fb793d42bd6cf2cd830b846 100644
@@ -14,6 +14,10 @@ config FDT_CACHE_SIFIVE_CCACHE
 	bool "SiFive CCACHE FDT cache driver"
 	default n
 
+config FDT_CACHE_SIFIVE_EC
+	bool "SiFive EC FDT cache driver"
+	default n
+
 config FDT_CACHE_SIFIVE_PL2
 	bool "SiFive PL2 FDT cache driver"
 	default n
lib/utils/cache/fdt_sifive_ec.c new file +194 -0

194 lines not on this page · raw message

lib/utils/cache/objects.mk +3 -0
index 37d250d1001bc647e3ae05304b2278e5dc608cae..aa76adc2e3bc5efbaa32b4ead9849d8b62bb7747 100644
@@ -14,4 +14,7 @@ libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += cache/fdt_sifive_ccache.o
 carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += fdt_sifive_pl2
 libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += cache/fdt_sifive_pl2.o
 
+carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_EC) += fdt_sifive_ec
+libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_EC) += cache/fdt_sifive_ec.o
+
 libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
platform/generic/configs/defconfig +1 -0
index 1bb14c78b9f0fc98493961023703a1ed2a24eea8..d0e2ec26bd60ad9394f631dd4d2870f5f2b90fd0 100644
@@ -13,6 +13,7 @@ CONFIG_PLATFORM_MIPS_P8700=y
 CONFIG_PLATFORM_SPACEMIT_K1=y
 CONFIG_FDT_CACHE=y
 CONFIG_FDT_CACHE_SIFIVE_CCACHE=y
+CONFIG_FDT_CACHE_SIFIVE_EC=y
 CONFIG_FDT_CACHE_SIFIVE_PL2=y
 CONFIG_FDT_CPPC=y
 CONFIG_FDT_CPPC_RPMI=y
Signature · 7 lines
2.34.1


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 3/3 Applied
#6 lore raw

Re: [PATCH 3/3] lib: utils/cache: Add SiFive Extensible Cache (EC) driver

Quoted 5 lines · Nick Hu

Add support for SiFive Extensible Cache (EC) controller with multi-slice architecture. The driver implements cache maintenance operations through MMIO register interface.

Quoted 7 lines

LGTM.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 255 lines
---
 lib/utils/cache/Kconfig            |   4 +
 lib/utils/cache/fdt_sifive_ec.c    | 194 +++++++++++++++++++++++++++++++++++++
 lib/utils/cache/objects.mk         |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 202 insertions(+)
lib/utils/cache/Kconfig +4 -0
index e28262fa2e81328460c8ee9a34b841454d03d776..be7d57c37d2477e60fb793d42bd6cf2cd830b846 100644
@@ -14,6 +14,10 @@ config FDT_CACHE_SIFIVE_CCACHE
        bool "SiFive CCACHE FDT cache driver"
        default n
 
+config FDT_CACHE_SIFIVE_EC
+       bool "SiFive EC FDT cache driver"
+       default n
+
 config FDT_CACHE_SIFIVE_PL2
        bool "SiFive PL2 FDT cache driver"
        default n
lib/utils/cache/fdt_sifive_ec.c new file +194 -0
new file mode 100644
index 0000000000000000000000000000000000000000..53ef6edb24aee316652cbaf83fd34c7c159aba59
@@ -0,0 +1,194 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2025 SiFive Inc.
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_platform.h>
+#include <sbi_utils/cache/fdt_cache.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+
+#define SIFIVE_EC_FEATURE_DISABLE_OFF  0x100UL
+#define SIFIVE_EC_FLUSH_CMD_OFF                0x800UL
+#define SIFIVE_EC_FLUSH_STATUS_OFF     0x808UL
+#define SIFIVE_EC_FLUSH_ADDR_OFF       0x810UL
+#define SIFIVE_EC_MODE_CTRL            0xa00UL
+
+#define SIFIVE_EC_FLUSH_COMPLETION_MASK        BIT(0)
+
+#define SIFIVE_EC_CLEANINV_ALL_CMD     0x3
+
+#define SIFIVE_EC_FEATURE_DISABLE_VAL  0
+
+struct sifive_ec_quirks {
+       bool two_mode;
+       char *reg_name;
+};
+
+struct sifive_ec_slice {
+       void *addr;
+       bool last_slice;
+};
+
+struct sifive_ec {
+       struct cache_device dev;
+       struct sifive_ec_slice *slices;
+};
+
+#define to_ec(_dev) container_of(_dev, struct sifive_ec, dev)
+
+static int sifive_ec_flush_all(struct cache_device *dev)
+{
+       struct sifive_ec *ec_dev = to_ec(dev);
+       struct sifive_ec_slice *slices = ec_dev->slices;
+       u32 cmd = SIFIVE_EC_CLEANINV_ALL_CMD, i = 0;
+       void *addr;
+
+       do {
+               addr = slices[i].addr;
+
+               writel((int)-1, addr + SIFIVE_EC_FLUSH_ADDR_OFF);
+               writel((int)-1, addr + SIFIVE_EC_FLUSH_ADDR_OFF + sizeof(u32));
+               writel(cmd, addr + SIFIVE_EC_FLUSH_CMD_OFF);
+       } while (!slices[i++].last_slice);
+
+       i = 0;
+       do {
+               addr = slices[i].addr;
+               do {} while (!(readl(addr + SIFIVE_EC_FLUSH_STATUS_OFF) &
+                               SIFIVE_EC_FLUSH_COMPLETION_MASK));
+       } while (!slices[i++].last_slice);
+
+       return 0;
+}
+
+int sifive_ec_warm_init(struct cache_device *dev)
+{
+       struct sifive_ec *ec_dev = to_ec(dev);
+       struct sifive_ec_slice *slices = ec_dev->slices;
+       struct sbi_domain *dom = sbi_domain_thishart_ptr();
+       int i = 0;
+
+       if (dom->boot_hartid == current_hartid()) {
+               do {
+                       writel(SIFIVE_EC_FEATURE_DISABLE_VAL,
+                              slices[i].addr + SIFIVE_EC_FEATURE_DISABLE_OFF);
+               } while (!slices[i++].last_slice);
+       }
+
+       return SBI_OK;
+}
+
+static struct cache_ops sifive_ec_ops = {
+       .warm_init = sifive_ec_warm_init,
+       .cache_flush_all = sifive_ec_flush_all,
+};
+
+static int sifive_ec_slices_cold_init(const void *fdt, int nodeoff, struct sifive_ec_slice *slices,
+                                     const struct sifive_ec_quirks *quirks)
+{
+       int rc, subnode, slice_idx = -1;
+       u64 reg_addr, size, start_addr = -1, end_addr = 0;
+
+       fdt_for_each_subnode(subnode, fdt, nodeoff) {
+               rc = fdt_get_node_addr_size_by_name(fdt, subnode, quirks->reg_name, &reg_addr,
+                                                   &size);
+               if (rc < 0)
+                       return SBI_ENODEV;
+
+               if (reg_addr < start_addr)
+                       start_addr = reg_addr;
+
+               if (reg_addr + size > end_addr)
+                       end_addr = reg_addr + size;
+
+               slices[++slice_idx].addr = (void *)(uintptr_t)reg_addr;
+       }
+       slices[slice_idx].last_slice = true;
+
+       /* Only enable the pmp to protect the EC m-mode region when it support two mode */
+       if (quirks->two_mode) {
+               rc = sbi_domain_root_add_memrange((unsigned long)start_addr,
+                                                 (unsigned long)(end_addr - start_addr),
+                                                 BIT(12),
+                                                 (SBI_DOMAIN_MEMREGION_MMIO |
+                                                  SBI_DOMAIN_MEMREGION_M_READABLE |
+                                                  SBI_DOMAIN_MEMREGION_M_WRITABLE));
+               if (rc)
+                       return rc;
+       }
+
+       return SBI_OK;
+}
+
+static int sifive_ec_cold_init(const void *fdt, int nodeoff, const struct fdt_match *match)
+{
+       const struct sifive_ec_quirks *quirks = match->data;
+       struct sifive_ec_slice *slices;
+       struct sifive_ec *ec_dev;
+       struct cache_device *dev;
+       int subnode, rc = SBI_ENOMEM;
+       u32 slice_count = 0;
+
+       /* Count the number of slices */
+       fdt_for_each_subnode(subnode, fdt, nodeoff)
+               slice_count++;
+
+       /* Need at least one slice */
+       if (!slice_count)
+               return SBI_EINVAL;
+
+       ec_dev = sbi_zalloc(sizeof(*ec_dev));
+       if (!ec_dev)
+               return SBI_ENOMEM;
+
+       slices = sbi_zalloc(slice_count * sizeof(*slices));
+       if (!slices)
+               goto free_ec;
+
+       rc = sifive_ec_slices_cold_init(fdt, nodeoff, slices, quirks);
+       if (rc)
+               goto free_slice;
+
+       dev = &ec_dev->dev;
+       dev->ops = &sifive_ec_ops;
+       rc = fdt_cache_add(fdt, nodeoff, dev);
+       if (rc)
+               goto free_slice;
+
+       ec_dev->slices = slices;
+
+       return SBI_OK;
+
+free_slice:
+       sbi_free(slices);
+free_ec:
+       sbi_free(ec_dev);
+       return rc;
+}
+
+static const struct sifive_ec_quirks sifive_extensiblecache0_quirks = {
+       .two_mode = false,
+       .reg_name = "control",
+};
+
+static const struct sifive_ec_quirks sifive_extensiblecache4_quirks = {
+       .two_mode = true,
+       .reg_name = "m_mode",
+};
+
+static const struct fdt_match sifive_ec_match[] = {
+       { .compatible = "sifive,extensiblecache4", .data = &sifive_extensiblecache4_quirks },
+       { .compatible = "sifive,extensiblecache3", .data = &sifive_extensiblecache0_quirks },
+       { .compatible = "sifive,extensiblecache2", .data = &sifive_extensiblecache0_quirks },
+       { .compatible = "sifive,extensiblecache0", .data = &sifive_extensiblecache0_quirks },
+       {},
+};
+
+struct fdt_driver fdt_sifive_ec = {
+       .match_table = sifive_ec_match,
+       .init = sifive_ec_cold_init,
+};
lib/utils/cache/objects.mk +3 -0
index 37d250d1001bc647e3ae05304b2278e5dc608cae..aa76adc2e3bc5efbaa32b4ead9849d8b62bb7747 100644
@@ -14,4 +14,7 @@ libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_CCACHE) += cache/fdt_sifive_ccache.o
 carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += fdt_sifive_pl2
 libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_PL2) += cache/fdt_sifive_pl2.o
 
+carray-fdt_cache_drivers-$(CONFIG_FDT_CACHE_SIFIVE_EC) += fdt_sifive_ec
+libsbiutils-objs-$(CONFIG_FDT_CACHE_SIFIVE_EC) += cache/fdt_sifive_ec.o
+
 libsbiutils-objs-$(CONFIG_CACHE) += cache/cache.o
platform/generic/configs/defconfig +1 -1
index 1bb14c78b9f0fc98493961023703a1ed2a24eea8..d0e2ec26bd60ad9394f631dd4d2870f5f2b90fd0 100644
@@ -13,6 +13,7 @@ CONFIG_PLATFORM_MIPS_P8700=y
 CONFIG_PLATFORM_SPACEMIT_K1=y
 CONFIG_FDT_CACHE=y
 CONFIG_FDT_CACHE_SIFIVE_CCACHE=y
+CONFIG_FDT_CACHE_SIFIVE_EC=y
 CONFIG_FDT_CACHE_SIFIVE_PL2=y
 CONFIG_FDT_CPPC=y
 CONFIG_FDT_CPPC_RPMI=y
 
--

2.34.1

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi