[RFC PATCH 5/7] lib: sbi: riscv_asm: add reserved_pmp_alloc() to allocate a reserved PMP
Yu-Chien Peter Lin
peter.lin at sifive.com
Fri Aug 15 03:01:13 PDT 2025
Add a function that allocates unused PMP entries from the high-priority
reserved pool.
Signed-off-by: Yu-Chien Peter Lin <peter.lin at sifive.com>
---
include/sbi/riscv_asm.h | 2 ++
lib/sbi/riscv_asm.c | 25 +++++++++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/include/sbi/riscv_asm.h b/include/sbi/riscv_asm.h
index ef48dc89..5fff2196 100644
--- a/include/sbi/riscv_asm.h
+++ b/include/sbi/riscv_asm.h
@@ -221,6 +221,8 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
unsigned long *log2len);
+int reserved_pmp_alloc(unsigned int *pmp_id);
+
#endif /* !__ASSEMBLER__ */
#endif
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index c7d75ac0..daa5087a 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -403,3 +403,28 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
return 0;
}
+
+/**
+ * reserved_pmp_alloc() - Allocate an unused reserved PMP entry
+ * @pmp_id: Pointer to store the allocated PMP entry ID
+ *
+ * Returns: 0 on success, negative error code on failure
+ *
+ * The caller is responsible for configuring the allocated entry
+ * using pmp_set() and pmp_disable().
+ */
+int reserved_pmp_alloc(unsigned int *pmp_id)
+{
+ const struct sbi_platform *plat = sbi_platform_thishart_ptr();
+
+ for (int n = 0; n < plat->reserved_pmp_count; n++) {
+ if (is_pmp_entry_mapped(n))
+ continue;
+ *pmp_id = n;
+ return SBI_SUCCESS;
+ }
+
+ sbi_printf("%s: Failed to allocate PMP entry. "
+ "Please increase reserved-pmp-count\n", __func__);
+ return SBI_EFAIL;
+}
--
2.48.0
More information about the opensbi
mailing list