[RFC PATCH 1/5] lib: utils: add a PMA framework

Randolph Lin randolph at andestech.com
Fri Nov 13 01:32:08 PST 2026


Several platforms set physical memory attributes (PMA) at boot, each in
its own way: RZ/Five and MT2824 from C tables, and MIPS and SpacemiT K3
by writing their PMA CSRs. Add a small framework, so that a platform
registers a PMA device and common code decides the regions.

The cold boot hart adds the regions. The framework checks that each
region is naturally aligned to its power-of-2 size and does not overlap
another region, and the device checks the rest. Every hart then sets the
regions, because the PMA settings are per hart.

The attributes of a region use the encoding of the flags in the
"riscv,physical-memory-regions" DT binding proposed for Linux, limited
to the memory type: ordering, idempotency, cacheability, coherence and
the unsafe flag. Each vendor encodes PMAs differently, so the device
maps the attributes to its own format, and it rejects a region whose
attributes it cannot set.

Bits 19:16 of the attributes, which that encoding does not use, choose
among the memory types that a device may offer for the same attributes:
bufferable, write-through, and no read or no write allocate. Zero means
non-bufferable, and a cacheable region is write-back with read and write
allocate.

Link: https://lore.kernel.org/all/20251113014656.2605447-20-samuel.holland@sifive.com/
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Randolph Lin <randolph at andestech.com>
---
 include/sbi_utils/pma/pma.h | 119 ++++++++++++++++++++++++++++++++++++
 lib/utils/Kconfig           |   2 +
 lib/utils/pma/Kconfig       |   9 +++
 lib/utils/pma/objects.mk    |   7 +++
 lib/utils/pma/pma.c         |  80 ++++++++++++++++++++++++
 5 files changed, 217 insertions(+)
 create mode 100644 include/sbi_utils/pma/pma.h
 create mode 100644 lib/utils/pma/Kconfig
 create mode 100644 lib/utils/pma/objects.mk
 create mode 100644 lib/utils/pma/pma.c

diff --git a/include/sbi_utils/pma/pma.h b/include/sbi_utils/pma/pma.h
new file mode 100644
index 00000000..eee02c4a
--- /dev/null
+++ b/include/sbi_utils/pma/pma.h
@@ -0,0 +1,119 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Andes Technology Corporation
+ */
+
+#ifndef __PMA_H__
+#define __PMA_H__
+
+#include <sbi/sbi_types.h>
+
+/*
+ * Region attributes. Bits 15:8 use the encoding of the flags in the
+ * "riscv,physical-memory-regions" DT binding proposed for Linux, which
+ * describe the memory type. The access types and the alias fields of that
+ * encoding do not apply.
+ */
+#define PMA_ORDER_MASK			(3 << 8)
+#define PMA_ORDER_IO_RELAXED		(0 << 8)
+#define PMA_ORDER_IO_STRONG		(1 << 8)
+#define PMA_ORDER_MEMORY		(2 << 8)
+#define PMA_READ_IDEMPOTENT		(1 << 10)
+#define PMA_WRITE_IDEMPOTENT		(1 << 11)
+#define PMA_CACHEABLE			(1 << 12)
+#define PMA_COHERENT			(1 << 13)
+#define PMA_UNSAFE			(1 << 15)
+
+#define PMA_IO				(PMA_ORDER_IO_RELAXED)
+#define PMA_NONCACHEABLE_MEMORY		(PMA_ORDER_MEMORY | PMA_READ_IDEMPOTENT | \
+					 PMA_WRITE_IDEMPOTENT)
+#define PMA_NONCOHERENT_MEMORY		(PMA_NONCACHEABLE_MEMORY | PMA_CACHEABLE)
+#define PMA_NORMAL_MEMORY		(PMA_NONCOHERENT_MEMORY | PMA_COHERENT)
+
+/*
+ * Bits 19:16 choose among the memory types that a device may offer for the
+ * same attributes. Zero means non-bufferable, and for a cacheable region,
+ * write-back with read and write allocate.
+ */
+#define PMA_BUFFERABLE			(1 << 16)
+#define PMA_WRITE_THROUGH		(1 << 17)
+#define PMA_NO_READ_ALLOC		(1 << 18)
+#define PMA_NO_WRITE_ALLOC		(1 << 19)
+
+#define PMA_NO_ALLOC			(PMA_NO_READ_ALLOC | PMA_NO_WRITE_ALLOC)
+
+#define PMA_ATTR_MASK			(PMA_ORDER_MASK | PMA_READ_IDEMPOTENT | \
+					 PMA_WRITE_IDEMPOTENT | PMA_CACHEABLE | \
+					 PMA_COHERENT | PMA_UNSAFE | \
+					 PMA_BUFFERABLE | PMA_WRITE_THROUGH | \
+					 PMA_NO_ALLOC)
+
+/** A region whose physical memory attributes (PMA) are set at boot **/
+struct pma_region {
+	/** Start address, aligned to the size **/
+	unsigned long base;
+	/** Size in bytes, a power of 2 **/
+	unsigned long size;
+	/** PMA_* attributes **/
+	u32 attr;
+};
+
+struct pma_device;
+
+struct pma_ops {
+	/**
+	 * Check whether the device can set a region (optional). A device
+	 * that cannot set the attributes of the region must reject it.
+	 **/
+	int (*region_check)(struct pma_device *dev,
+			    const struct pma_region *region);
+	/**
+	 * Set the regions on the calling hart. The regions must stay set
+	 * until the hart resets: the device must not let other users, such
+	 * as a vendor SBI extension, change or free them.
+	 **/
+	int (*regions_set)(struct pma_device *dev,
+			   const struct pma_region *regions,
+			   unsigned int count);
+};
+
+struct pma_device {
+	/** Maximum number of regions **/
+	unsigned int max_regions;
+	/** PMA operations **/
+	const struct pma_ops *ops;
+};
+
+/**
+ * Set the PMA device of the platform
+ *
+ * @param dev the PMA device
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int pma_set_device(struct pma_device *dev);
+
+/**
+ * Add a region to the regions that every hart sets at boot
+ *
+ * Call it on the cold boot hart only, before any hart calls
+ * pma_setup_regions().
+ *
+ * @param region the region to add
+ *
+ * @return 0 on success, or a negative error code on failure
+ */
+int pma_add_region(const struct pma_region *region);
+
+/**
+ * Set the added regions on the calling hart
+ *
+ * The PMA settings are per hart, so every hart must call it.
+ *
+ * @return 0 on success or if no region was added, or a negative error code
+ *	   on failure
+ */
+int pma_setup_regions(void);
+
+#endif
diff --git a/lib/utils/Kconfig b/lib/utils/Kconfig
index 5a5b3b1e..465d10ac 100644
--- a/lib/utils/Kconfig
+++ b/lib/utils/Kconfig
@@ -22,6 +22,8 @@ source "$(OPENSBI_SRC_DIR)/lib/utils/libfdt/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/mailbox/Kconfig"
 
+source "$(OPENSBI_SRC_DIR)/lib/utils/pma/Kconfig"
+
 source "$(OPENSBI_SRC_DIR)/lib/utils/regmap/Kconfig"
 
 source "$(OPENSBI_SRC_DIR)/lib/utils/reset/Kconfig"
diff --git a/lib/utils/pma/Kconfig b/lib/utils/pma/Kconfig
new file mode 100644
index 00000000..73523ab0
--- /dev/null
+++ b/lib/utils/pma/Kconfig
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "PMA Support"
+
+config PMA
+	bool "PMA support"
+	default n
+
+endmenu
diff --git a/lib/utils/pma/objects.mk b/lib/utils/pma/objects.mk
new file mode 100644
index 00000000..a778d0b6
--- /dev/null
+++ b/lib/utils/pma/objects.mk
@@ -0,0 +1,7 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 Andes Technology Corporation
+#
+
+libsbiutils-objs-$(CONFIG_PMA) += pma/pma.o
diff --git a/lib/utils/pma/pma.c b/lib/utils/pma/pma.c
new file mode 100644
index 00000000..f3f47ea0
--- /dev/null
+++ b/lib/utils/pma/pma.c
@@ -0,0 +1,80 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Andes Technology Corporation
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi_utils/pma/pma.h>
+
+static struct pma_device *pma_dev;
+static struct pma_region *pma_regions;
+static unsigned int pma_regions_count;
+
+int pma_set_device(struct pma_device *dev)
+{
+	if (!dev || !dev->ops || !dev->ops->regions_set || !dev->max_regions)
+		return SBI_EINVAL;
+
+	if (pma_dev)
+		return SBI_EALREADY;
+
+	pma_dev = dev;
+
+	return 0;
+}
+
+int pma_add_region(const struct pma_region *region)
+{
+	const struct pma_region *r;
+	unsigned int i;
+	int rc;
+
+	if (!pma_dev)
+		return SBI_ENODEV;
+
+	if (!region->size || (region->size & (region->size - 1)) ||
+	    (region->base & (region->size - 1)))
+		return SBI_EINVAL;
+
+	if ((region->attr & ~PMA_ATTR_MASK) ||
+	    (region->attr & PMA_ORDER_MASK) == PMA_ORDER_MASK)
+		return SBI_EINVAL;
+
+	for (i = 0; i < pma_regions_count; i++) {
+		r = &pma_regions[i];
+		if (region->base <= r->base + r->size - 1 &&
+		    r->base <= region->base + region->size - 1)
+			return SBI_EINVAL;
+	}
+
+	if (pma_regions_count == pma_dev->max_regions)
+		return SBI_ENOSPC;
+
+	if (pma_dev->ops->region_check) {
+		rc = pma_dev->ops->region_check(pma_dev, region);
+		if (rc)
+			return rc;
+	}
+
+	if (!pma_regions) {
+		pma_regions = sbi_calloc(pma_dev->max_regions,
+					 sizeof(*pma_regions));
+		if (!pma_regions)
+			return SBI_ENOMEM;
+	}
+
+	pma_regions[pma_regions_count++] = *region;
+
+	return 0;
+}
+
+int pma_setup_regions(void)
+{
+	if (!pma_regions_count)
+		return 0;
+
+	return pma_dev->ops->regions_set(pma_dev, pma_regions,
+					 pma_regions_count);
+}
-- 
2.34.1




More information about the opensbi mailing list