[RFC PATCH v3 1/6] include: sbi: sbi_platform: add sbi_platform_reserved_pmp_count()

Yu-Chien Peter Lin peter.lin at sifive.com
Sun Nov 30 03:16:38 PST 2025


Add sbi_platform_reserved_pmp_count() function to calculate
the total number of reserved PMP entries for a platform.
Also add get_reserved_pmp_count() callback to allow platforms
specifying their additional reserved PMP requirements.

Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
 include/sbi/sbi_platform.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/include/sbi/sbi_platform.h b/include/sbi/sbi_platform.h
index d75c12de..0deeca9f 100644
--- a/include/sbi/sbi_platform.h
+++ b/include/sbi/sbi_platform.h
@@ -48,6 +48,7 @@
 
 #include <sbi/sbi_ecall_interface.h>
 #include <sbi/sbi_error.h>
+#include <sbi/sbi_hart.h>
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_version.h>
 #include <sbi/sbi_trap_ldst.h>
@@ -146,6 +147,8 @@ struct sbi_platform_operations {
 			unsigned long log2len);
 	/** platform specific pmp disable on current HART */
 	void (*pmp_disable)(unsigned int n);
+	/** Get number of additional reserved PMP entries. */
+	u32 (*get_reserved_pmp_count)(void);
 };
 
 /** Platform default per-HART stack size for exception/interrupt handling */
@@ -302,6 +305,38 @@ static inline u32 sbi_platform_tlb_fifo_num_entries(const struct sbi_platform *p
 	return sbi_hart_count();
 }
 
+/**
+ * Get total number of reserved PMP entries for the platform.
+ *
+ * This includes:
+ * - One default PMP entry for sbi_hart_map_saddr() when smepmp is enabled
+ * - Additional platform-specific reserved PMP entries from get_reserved_pmp_count()
+ *
+ * @param plat pointer to struct sbi_platform
+ *
+ * @return total reserved PMP entry count for the platform
+ */
+static inline u32 sbi_platform_reserved_pmp_count(const struct sbi_platform *plat)
+{
+	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	u32 pmp_count = 0;
+
+	if (!plat)
+		return 0;
+
+	/*
++	 * If smepmp is enabled, reserve at least one PMP entry
++	 * for sbi_hart_map_saddr().
++	 */
+	if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMEPMP))
+		pmp_count += 1;
+
+	if (sbi_platform_ops(plat)->get_reserved_pmp_count)
+		pmp_count += sbi_platform_ops(plat)->get_reserved_pmp_count();
+
+	return pmp_count;
+}
+
 /**
  * Get total number of HARTs supported by the platform
  *
-- 
2.39.3




More information about the opensbi mailing list