[RFC PATCH 21/23] platform: generic: sifive: add Smepmp MMIO regions in sifive_pmp_init

Yu-Chien Peter Lin peter.lin at sifive.com
Mon Nov 30 02:11:45 PST 2026


Move and rename sifive_smepmp_setup() to static
sifive_pmp_add_mmio_regions() in sifive_pmp.c, and invoke it from
sifive_pmp_init() before parsing or enforcing static PMP regions. The
helper belongs with sifive_pmp_init() because both prepare SiFive PMP
and domain configuration.

The helper now runs after generic_early_init() instead of before it.
Thus, MMIO ranges are registered later, and helper errors are returned
after generic_early_init() rather than before it.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 platform/generic/sifive/sifive_dev_platform.c | 41 -----------------
 platform/generic/sifive/sifive_pmp.c          | 46 +++++++++++++++++--
 2 files changed, 43 insertions(+), 44 deletions(-)

diff --git a/platform/generic/sifive/sifive_dev_platform.c b/platform/generic/sifive/sifive_dev_platform.c
index 6a1b5ed3..c38daa7d 100644
--- a/platform/generic/sifive/sifive_dev_platform.c
+++ b/platform/generic/sifive/sifive_dev_platform.c
@@ -5,54 +5,13 @@
  */
 
 #include <platform_override.h>
-#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
- * to peripheral and system ports for all privilege modes.
- * Finer-grained access control is enforced by wgChecker rules.
- */
-static int sifive_smepmp_setup(void)
-{
-	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
-	int rc;
-
-	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
-		return 0;
-
-	/* Peripheral port region: 0 to 2GiB */
-	rc = sbi_domain_root_add_memrange(0x0, 0x80000000, 0x80000000,
-					  SBI_DOMAIN_MEMREGION_MMIO |
-					  SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW);
-	if (rc)
-		return rc;
-
-#if __riscv_xlen > 32
-	/* System port region: 512GiB to 1TiB */
-	rc = sbi_domain_root_add_memrange(0x8000000000ULL, 0x8000000000ULL, 0x8000000000ULL,
-					  SBI_DOMAIN_MEMREGION_MMIO |
-					  SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW);
-	if (rc)
-		return rc;
-#endif
-
-	return 0;
-}
-
 static int sifive_early_init(bool cold_boot)
 {
 	int rc;
 
-	if (cold_boot) {
-		rc = sifive_smepmp_setup();
-		if (rc)
-			return rc;
-	}
-
 	rc = generic_early_init(cold_boot);
 	if (rc)
 		return rc;
diff --git a/platform/generic/sifive/sifive_pmp.c b/platform/generic/sifive/sifive_pmp.c
index 1717b471..2a4b08d2 100644
--- a/platform/generic/sifive/sifive_pmp.c
+++ b/platform/generic/sifive/sifive_pmp.c
@@ -399,19 +399,59 @@ static struct sbi_hart_protection sifive_pmp_protection = {
 	.temp_unmap_range = sifive_pmp_temp_unmap_range,
 };
 
+/*
+ * Instead of allocating individual PMP entries for each M-mode
+ * MMIO driver, configure SMEPMP regions to grant R/W permission
+ * to peripheral and system ports for all privilege modes.
+ * Finer-grained access control is enforced by wgChecker rules.
+ */
+static int sifive_pmp_add_mmio_regions(void)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	int rc;
+
+	if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+		return 0;
+
+	/* Peripheral port region: 0 to 2GiB */
+	rc = sbi_domain_root_add_memrange(
+		0x0, 0x80000000, 0x80000000,
+		SBI_DOMAIN_MEMREGION_MMIO |
+			SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW);
+	if (rc)
+		return rc;
+
+#if __riscv_xlen > 32
+	/* System port region: 512GiB to 1TiB */
+	rc = sbi_domain_root_add_memrange(
+		0x8000000000ULL, 0x8000000000ULL, 0x8000000000ULL,
+		SBI_DOMAIN_MEMREGION_MMIO |
+			SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW);
+	if (rc)
+		return rc;
+#endif
+
+	return 0;
+}
+
 int sifive_pmp_init(bool cold_boot)
 {
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 	int rc;
 
 	if (cold_boot) {
+		rc = sifive_pmp_add_mmio_regions();
+		if (rc)
+			return rc;
+
 		rc = sifive_pmp_parse_regions(fdt_get_address());
-		if (rc || !unsafe_region_entries)
+		if (rc)
 			return rc;
-	} else if (!unsafe_region_entries) {
-		return 0;
 	}
 
+	if (!unsafe_region_entries)
+		return 0;
+
 	rc = static_pmp_enforce(scratch);
 	if (rc)
 		return rc;
-- 
2.43.7




More information about the opensbi mailing list