[RFC PATCH 18/23] platform: generic: sifive: parse static PMP regions from DT
Yu-Chien Peter Lin
peter.lin at sifive.com
Mon Nov 30 02:11:42 PST 2026
Add sifive_pmp_init() and call it from the existing SiFive early-init
hook after generic initialization succeeds.
On cold boot, parse the root riscv,physical-memory-regions [1]
property and collect PMA_UNSAFE ranges. Reject malformed properties
and zero-sized or overflowing unsafe ranges that cannot be represented
by unsigned long. Sort and merge overlapping or adjacent ranges.
Prepare NAPOT or TOR entry counts and report the required total. This
commit records the layout but does not program PMP entries.
Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
[1] https://lore.kernel.org/linux-riscv/20251113014656.2605447-20-samuel.holland@sifive.com/
---
platform/generic/sifive/objects.mk | 1 +
platform/generic/sifive/sifive_dev_platform.c | 8 +-
platform/generic/sifive/sifive_pmp.c | 179 ++++++++++++++++++
3 files changed, 187 insertions(+), 1 deletion(-)
create mode 100644 platform/generic/sifive/sifive_pmp.c
diff --git a/platform/generic/sifive/objects.mk b/platform/generic/sifive/objects.mk
index d32e1273..f7d8c5f8 100644
--- a/platform/generic/sifive/objects.mk
+++ b/platform/generic/sifive/objects.mk
@@ -4,6 +4,7 @@
carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive_dev_platform
platform-objs-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive/sifive_dev_platform.o
+platform-objs-$(CONFIG_PLATFORM_SIFIVE_DEV) += sifive/sifive_pmp.o
carray-platform_override_modules-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive_fu540
platform-objs-$(CONFIG_PLATFORM_SIFIVE_FU540) += sifive/fu540.o
diff --git a/platform/generic/sifive/sifive_dev_platform.c b/platform/generic/sifive/sifive_dev_platform.c
index 579d4651..6a1b5ed3 100644
--- a/platform/generic/sifive/sifive_dev_platform.c
+++ b/platform/generic/sifive/sifive_dev_platform.c
@@ -8,6 +8,8 @@
#include <sbi/sbi_hart.h>
#include <sbi/sbi_scratch.h>
+int sifive_pmp_init(bool cold_boot);
+
/*
* Instead of allocating individual PMP entries for each M-mode
* MMIO driver, configure SMEPMP regions to grant R/W permission
@@ -51,7 +53,11 @@ static int sifive_early_init(bool cold_boot)
return rc;
}
- return generic_early_init(cold_boot);
+ rc = generic_early_init(cold_boot);
+ if (rc)
+ return rc;
+
+ return sifive_pmp_init(cold_boot);
}
static int sifive_platform_init(const void *fdt, int nodeoff,
diff --git a/platform/generic/sifive/sifive_pmp.c b/platform/generic/sifive/sifive_pmp.c
new file mode 100644
index 00000000..8300c4f9
--- /dev/null
+++ b/platform/generic/sifive/sifive_pmp.c
@@ -0,0 +1,179 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 SiFive Inc.
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_math.h>
+#include <sbi/sbi_pmp.h>
+#include <sbi/sbi_string.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+
+#define SIFIVE_PHYMEM_PMA_UNSAFE BIT(15)
+
+struct sifive_pmp_unsafe_region {
+ /* RV32 cannot represent ranges above ULONG_MAX. */
+ unsigned long base;
+ unsigned long end;
+ unsigned long order;
+ unsigned int entries;
+ bool napot;
+};
+
+static struct sifive_pmp_unsafe_region unsafe_regions[PMP_COUNT];
+static unsigned int unsafe_region_count;
+static unsigned int unsafe_region_entries;
+
+static int sifive_pmp_set_regions(const struct sifive_pmp_unsafe_region *list,
+ unsigned long count)
+{
+ unsigned long i;
+
+ unsafe_region_count = 0;
+ unsafe_region_entries = 0;
+ for (i = 0; i < count; i++) {
+ struct sifive_pmp_unsafe_region *region;
+ unsigned long size;
+
+ if (unsafe_region_count == PMP_COUNT) {
+ sbi_printf("sifive_pmp: too many regions, range "
+ "%#lx-%#lx\n",
+ list[i].base, list[i].end - 1);
+ return SBI_EFAIL;
+ }
+ region = &unsafe_regions[unsafe_region_count];
+ *region = list[i];
+ size = region->end - region->base;
+ region->napot = size >= 8 && !(size & (size - 1)) &&
+ !(region->base & (size - 1));
+ region->order = log2roundup(size);
+ /*
+ * A NAPOT range or a range starting at zero uses one entry.
+ * Other TOR ranges need a lower-bound entry and an upper-bound
+ * entry.
+ */
+ region->entries = (region->napot || !region->base) ? 1 : 2;
+ unsafe_region_entries += region->entries;
+ unsafe_region_count++;
+ }
+
+ return 0;
+}
+
+static int sifive_pmp_parse_regions(const void *fdt)
+{
+ const fdt32_t *prop;
+ struct sifive_pmp_unsafe_region *parsed;
+ unsigned long addr_cells, size_cells, entry_cells;
+ unsigned long unsafe_count = 0, merged_count = 0;
+ unsigned long i, j;
+ int len, rc = 0;
+
+ prop = fdt_getprop(fdt, 0, "riscv,physical-memory-regions", &len);
+ if (!prop) {
+ if (len == -FDT_ERR_NOTFOUND)
+ return sifive_pmp_set_regions(NULL, 0);
+ goto malformed;
+ }
+
+ addr_cells = fdt_address_cells(fdt, 0);
+ size_cells = fdt_size_cells(fdt, 0);
+ if (!addr_cells || !size_cells || addr_cells > 2 || size_cells > 2)
+ goto malformed;
+
+ entry_cells = addr_cells + size_cells + 2;
+ if (len < 0 || (len % (entry_cells * sizeof(fdt32_t))))
+ goto malformed;
+
+ parsed = sbi_malloc((len / (entry_cells * sizeof(fdt32_t))) *
+ sizeof(*parsed));
+ if (!parsed && len)
+ return SBI_ENOMEM;
+
+ for (i = 0; i < len / (entry_cells * sizeof(fdt32_t)); i++) {
+ const fdt32_t *entry = prop + i * entry_cells;
+ uint64_t base = 0, size = 0;
+ unsigned long k;
+
+ for (k = 0; k < addr_cells; k++)
+ base = (base << 32) | fdt32_to_cpu(entry[k]);
+ for (k = 0; k < size_cells; k++)
+ size = (size << 32) |
+ fdt32_to_cpu(entry[addr_cells + k]);
+
+ if (!(fdt32_to_cpu(entry[addr_cells + size_cells]) &
+ SIFIVE_PHYMEM_PMA_UNSAFE))
+ continue;
+
+ if (!size || base > ~0UL || size > ~0UL ||
+ size > ~0UL - (unsigned long)base) {
+ sbi_printf("sifive_pmp: invalid region range "
+ "%#llx+%#llx\n",
+ (unsigned long long)base,
+ (unsigned long long)size);
+ rc = SBI_EFAIL;
+ goto out;
+ }
+
+ parsed[unsafe_count].base = (unsigned long)base;
+ parsed[unsafe_count].end = (unsigned long)(base + size);
+ unsafe_count++;
+ }
+
+ /* Insertion sort the unsafe ranges by ascending base. */
+ for (i = 1; i < unsafe_count; i++) {
+ struct sifive_pmp_unsafe_region item = parsed[i];
+
+ for (j = i; j && parsed[j - 1].base > item.base; j--)
+ parsed[j] = parsed[j - 1];
+ parsed[j] = item;
+ }
+
+ /*
+ * Merge overlapping or adjacent ranges in place. Since the ranges
+ * are sorted, only the last merged range needs to be checked.
+ */
+ for (i = 0; i < unsafe_count; i++) {
+ struct sifive_pmp_unsafe_region *last =
+ merged_count ? &parsed[merged_count - 1] : NULL;
+
+ if (last && parsed[i].base <= last->end) {
+ /* Overlaps or touches the previous range: extend it. */
+ if (parsed[i].end > last->end)
+ last->end = parsed[i].end;
+ } else {
+ parsed[merged_count++] = parsed[i];
+ }
+ }
+
+ rc = sifive_pmp_set_regions(parsed, merged_count);
+
+out:
+ sbi_free(parsed);
+ return rc;
+
+malformed:
+ sbi_printf("sifive_pmp: malformed riscv,physical-memory-regions\n");
+ return SBI_EFAIL;
+}
+
+int sifive_pmp_init(bool cold_boot)
+{
+ int rc;
+
+ if (!cold_boot)
+ return 0;
+
+ rc = sifive_pmp_parse_regions(fdt_get_address());
+ if (rc)
+ return rc;
+
+ sbi_printf("SiFive static PMP : %u PMP entries needed\n",
+ unsafe_region_entries);
+
+ return 0;
+}
--
2.43.7
More information about the opensbi
mailing list