[RFC PATCH 2/5] lib: utils/pma: set PMA regions from the device tree
Randolph Lin
randolph at andestech.com
Fri Nov 13 01:32:09 PST 2026
Add fdt_pma_init(), which reads the PMA regions from a
"riscv,pma-regions" DT node, so that a board describes them without
its own platform code.
Each subnode describes a region with "base", "order" and "attr". As with
PMP regions, the order gives the size, so a region is always NAPOT.
"attr" holds the PMA_* attributes. The cold boot hart parses the node
and then deletes it, like the other OpenSBI configuration nodes. Every
hart then sets the regions.
The generic platform calls it from generic_final_init() on every hart.
Without the node, it does nothing. With the node but no PMA device, the
boot fails with a message that says so, because S-mode would otherwise
use the regions with the wrong attributes. The cold boot hart prints
each region and what its attributes mean, because S-mode cannot see the
node. A hart that fails to set the regions prints its hart ID and the
error.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Randolph Lin <randolph at andestech.com>
---
include/sbi_utils/pma/fdt_pma.h | 33 ++++++
lib/utils/pma/Kconfig | 6 ++
lib/utils/pma/fdt_pma.c | 167 +++++++++++++++++++++++++++++
lib/utils/pma/objects.mk | 2 +
platform/generic/configs/defconfig | 1 +
platform/generic/platform.c | 6 ++
6 files changed, 215 insertions(+)
create mode 100644 include/sbi_utils/pma/fdt_pma.h
create mode 100644 lib/utils/pma/fdt_pma.c
diff --git a/include/sbi_utils/pma/fdt_pma.h b/include/sbi_utils/pma/fdt_pma.h
new file mode 100644
index 00000000..3db12087
--- /dev/null
+++ b/include/sbi_utils/pma/fdt_pma.h
@@ -0,0 +1,33 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Andes Technology Corporation
+ */
+
+#ifndef __FDT_PMA_H__
+#define __FDT_PMA_H__
+
+#include <sbi/sbi_types.h>
+
+#ifdef CONFIG_FDT_PMA
+
+/**
+ * Set the PMA regions in the "riscv,pma-regions" DT node
+ *
+ * The cold boot hart parses the node and then deletes it from the DT. The
+ * PMA settings are per hart, so every hart must call it.
+ *
+ * @param cold_boot cold init or warm init
+ *
+ * @return 0 on success or if the DT has no such node, or a negative error
+ * code on failure
+ */
+int fdt_pma_init(bool cold_boot);
+
+#else
+
+static inline int fdt_pma_init(bool cold_boot) { return 0; }
+
+#endif
+
+#endif
diff --git a/lib/utils/pma/Kconfig b/lib/utils/pma/Kconfig
index 73523ab0..a7666942 100644
--- a/lib/utils/pma/Kconfig
+++ b/lib/utils/pma/Kconfig
@@ -2,6 +2,12 @@
menu "PMA Support"
+config FDT_PMA
+ bool "FDT based PMA regions"
+ depends on FDT
+ select PMA
+ default n
+
config PMA
bool "PMA support"
default n
diff --git a/lib/utils/pma/fdt_pma.c b/lib/utils/pma/fdt_pma.c
new file mode 100644
index 00000000..420678ef
--- /dev/null
+++ b/lib/utils/pma/fdt_pma.c
@@ -0,0 +1,167 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Andes Technology Corporation
+ */
+
+#include <libfdt.h>
+#include <sbi/riscv_asm.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_error.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/pma/fdt_pma.h>
+#include <sbi_utils/pma/pma.h>
+
+/*
+ * Each subnode describes a NAPOT region with "base" (the 64-bit physical
+ * address), "order" (the size as log2, i.e. size = BIT(order)) and "attr"
+ * (the PMA_* attributes).
+ */
+static int fdt_pma_parse_region(const void *fdt, int node,
+ struct pma_region *region)
+{
+ const fdt32_t *val;
+ u32 order;
+ u64 base;
+ int len;
+
+ val = fdt_getprop(fdt, node, "base", &len);
+ if (!val || len != 8)
+ return SBI_EINVAL;
+
+ base = fdt32_to_cpu(val[0]);
+ base = (base << 32) | fdt32_to_cpu(val[1]);
+ region->base = base;
+ if (region->base != base)
+ return SBI_EINVAL;
+
+ val = fdt_getprop(fdt, node, "order", &len);
+ if (!val || len != 4)
+ return SBI_EINVAL;
+
+ order = fdt32_to_cpu(*val);
+ if (order >= __riscv_xlen)
+ return SBI_EINVAL;
+
+ region->size = BIT(order);
+
+ val = fdt_getprop(fdt, node, "attr", &len);
+ if (!val || len != 4)
+ return SBI_EINVAL;
+
+ region->attr = fdt32_to_cpu(*val);
+
+ return 0;
+}
+
+static void fdt_pma_print_region(unsigned int i,
+ const struct pma_region *region)
+{
+ u32 attr = region->attr;
+
+ sbi_printf("PMA Region%02u : 0x%" PRILX "-0x%" PRILX
+ " attr 0x%x\n", i, region->base,
+ region->base + region->size - 1, attr);
+
+ sbi_printf("PMA Region%02u Type : ", i);
+ if (attr & PMA_UNSAFE) {
+ sbi_printf("unsafe, accesses fault\n");
+ return;
+ }
+
+ switch (attr & PMA_ORDER_MASK) {
+ case PMA_ORDER_MEMORY:
+ sbi_printf("memory");
+ if (!(attr & PMA_READ_IDEMPOTENT))
+ sbi_printf(", non-idempotent reads");
+ if (!(attr & PMA_WRITE_IDEMPOTENT))
+ sbi_printf(", non-idempotent writes");
+ break;
+ default:
+ sbi_printf("I/O, %s ordering",
+ (attr & PMA_ORDER_MASK) == PMA_ORDER_IO_STRONG ?
+ "strong" : "relaxed");
+ if (attr & PMA_READ_IDEMPOTENT)
+ sbi_printf(", idempotent reads");
+ if (attr & PMA_WRITE_IDEMPOTENT)
+ sbi_printf(", idempotent writes");
+ break;
+ }
+
+ if (!(attr & PMA_CACHEABLE)) {
+ sbi_printf(", non-cacheable, %s\n",
+ (attr & PMA_BUFFERABLE) ? "bufferable" : "non-bufferable");
+ return;
+ }
+
+ sbi_printf(", cacheable, %s, %s",
+ (attr & PMA_COHERENT) ? "coherent" : "non-coherent",
+ (attr & PMA_WRITE_THROUGH) ? "write-through" : "write-back");
+
+ switch (attr & PMA_NO_ALLOC) {
+ case 0:
+ sbi_printf(", read and write allocate\n");
+ break;
+ case PMA_NO_WRITE_ALLOC:
+ sbi_printf(", read allocate\n");
+ break;
+ case PMA_NO_READ_ALLOC:
+ sbi_printf(", write allocate\n");
+ break;
+ default:
+ sbi_printf(", no allocate\n");
+ break;
+ }
+}
+
+static int fdt_pma_parse(void *fdt)
+{
+ struct pma_region region;
+ int parent, node, rc;
+ unsigned int i = 0;
+
+ parent = fdt_node_offset_by_compatible(fdt, -1, "riscv,pma-regions");
+ if (parent < 0)
+ return 0;
+
+ fdt_for_each_subnode(node, fdt, parent) {
+ rc = fdt_pma_parse_region(fdt, node, ®ion);
+ if (!rc)
+ rc = pma_add_region(®ion);
+ if (rc == SBI_ENODEV) {
+ sbi_printf("%s: no PMA device, cannot set region %s\n",
+ __func__, fdt_get_name(fdt, node, NULL));
+ return rc;
+ }
+ if (rc) {
+ sbi_printf("%s: failed to add region %s (error %d)\n",
+ __func__, fdt_get_name(fdt, node, NULL), rc);
+ return rc;
+ }
+
+ fdt_pma_print_region(i++, ®ion);
+ }
+
+ /* Like the other OpenSBI configuration nodes, hide it from S-mode */
+ fdt_nop_node(fdt, parent);
+
+ return 0;
+}
+
+int fdt_pma_init(bool cold_boot)
+{
+ int rc;
+
+ if (cold_boot) {
+ rc = fdt_pma_parse(fdt_get_address_rw());
+ if (rc)
+ return rc;
+ }
+
+ rc = pma_setup_regions();
+ if (rc)
+ sbi_printf("%s: hart%u failed to set the PMA regions (error %d)\n",
+ __func__, current_hartid(), rc);
+
+ return rc;
+}
diff --git a/lib/utils/pma/objects.mk b/lib/utils/pma/objects.mk
index a778d0b6..72cdd233 100644
--- a/lib/utils/pma/objects.mk
+++ b/lib/utils/pma/objects.mk
@@ -4,4 +4,6 @@
# Copyright (c) 2026 Andes Technology Corporation
#
+libsbiutils-objs-$(CONFIG_FDT_PMA) += pma/fdt_pma.o
+
libsbiutils-objs-$(CONFIG_PMA) += pma/pma.o
diff --git a/platform/generic/configs/defconfig b/platform/generic/configs/defconfig
index 687b4c35..80188a32 100644
--- a/platform/generic/configs/defconfig
+++ b/platform/generic/configs/defconfig
@@ -46,6 +46,7 @@ CONFIG_FDT_IRQCHIP_PLIC=y
CONFIG_FDT_MAILBOX=y
CONFIG_RPMI_MAILBOX=y
CONFIG_FDT_MAILBOX_RPMI_SHMEM=y
+CONFIG_FDT_PMA=y
CONFIG_FDT_REGMAP=y
CONFIG_FDT_REGMAP_SYSCON=y
CONFIG_FDT_RESET=y
diff --git a/platform/generic/platform.c b/platform/generic/platform.c
index 1df0280d..bb0682b6 100644
--- a/platform/generic/platform.c
+++ b/platform/generic/platform.c
@@ -26,6 +26,7 @@
#include <sbi_utils/irqchip/fdt_irqchip.h>
#include <sbi_utils/irqchip/imsic.h>
#include <sbi_utils/mpxy/fdt_mpxy.h>
+#include <sbi_utils/pma/fdt_pma.h>
#include <sbi_utils/serial/fdt_serial.h>
#include <sbi_utils/serial/semihosting.h>
#include <sbi_utils/timer/fdt_timer.h>
@@ -237,6 +238,11 @@ int generic_early_init(bool cold_boot)
int generic_final_init(bool cold_boot)
{
void *fdt = fdt_get_address_rw();
+ int rc;
+
+ rc = fdt_pma_init(cold_boot);
+ if (rc)
+ return rc;
if (!cold_boot)
return 0;
--
2.34.1
More information about the opensbi
mailing list