Skip to contentSkip to conversation

Extend the reserved PMP entries

RFC PATCH Superseded Yu-Chien Peter Lin 2 participants 9 messages Aug 15, 2025 – Oct 6, 2025 RISC-V

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Extend the reserved PMP entries

This series extends the number of reserved PMP entry. The capabilities of the reserved PMP entries include:
- Highest priority
- Allow TOR mode for platform-specific use cases
- Can be consistent across domain context switches
- Allow runtime PMP allocation

A potential use case for reserved PMP entries is parsing gaps in memory ports marked as PMA_UNSAFE [1] in device-tree. These gaps may not be able to exactly covered by 1 or 2 NAPOT entries, so TOR mode PMP can be allocated to save PMP usage. Besides, they don't need reconfiguration across domains.

This series depends on:
1) https://lore.kernel.org/all/[email protected]/
2) https://lore.kernel.org/all/[email protected]/

[1] https://patchwork.kernel.org/project/linux-riscv/patch/[email protected]/

Yu-Chien Peter Lin (7):
lib: utils: fdt_helper: add fdt_has_isa_extension() helper
include: sbi: sbi_platform: add reserved_pmp_count field
platform: generic: platform: introduce
fw_platform_get_reserved_pmp_count()
lib: sbi_init: print total and reserved PMP counts
lib: sbi: riscv_asm: add reserved_pmp_alloc() to allocate a reserved
PMP
lib: sbi: sbi_hart: extend PMP handling to support multiple reserved
entries
docs: opensbi_config: add description for reserved-pmp-count

 docs/opensbi_config.md             |  4 +++
 include/sbi/riscv_asm.h            |  2 ++
 include/sbi/sbi_hart.h             | 15 --------
 include/sbi/sbi_platform.h         | 15 +++++---
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/riscv_asm.c                | 25 +++++++++++++
 lib/sbi/sbi_domain_context.c       |  6 +++-
 lib/sbi/sbi_hart.c                 | 56 ++++++++++++++++++++++--------
 lib/sbi/sbi_init.c                 |  6 ++--
 lib/utils/fdt/fdt_helper.c         | 35 +++++++++++++++++++
 platform/generic/platform.c        | 34 ++++++++++++++++++
 11 files changed, 163 insertions(+), 37 deletions(-)
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

RFC PATCH 1/7 lib: utils: fdt_helper: add fdt_has_isa_extension() helper

Add a helper function to search for a given ISA string in riscv,isa-extensions or riscv,isa properties within the FDT.

---
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/utils/fdt/fdt_helper.c         | 35 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 24ad1a94..8aa216c7 100644
@@ -56,6 +56,8 @@ int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts);
 
+bool fdt_has_isa_extension(const void *fdt, const char *ext);
+
 int fdt_parse_gaisler_uart_node(const void *fdt, int nodeoffset,
 				struct platform_uart_data *uart);
 
lib/utils/fdt/fdt_helper.c +35 -0
index b0b18b1c..de72f34a 100644
@@ -447,6 +447,41 @@ int fdt_parse_isa_extensions(const void *fdt, unsigned long *hart_exts)
 	return 0;
 }
 
+bool fdt_has_isa_extension(const void *fdt, const char *ext)
+{
+	const struct sbi_hart_ext_data *ext_data = NULL;
+	DECLARE_BITMAP(hart_exts, SBI_HART_EXT_MAX);
+	int rc, i;
+
+	if (!fdt || !ext)
+		return false;
+
+	for (i = 0; i < SBI_HART_EXT_MAX; i++) {
+		if (!sbi_strncmp(sbi_hart_ext[i].name, ext,
+	                         sbi_strlen(ext))) {
+			ext_data = &sbi_hart_ext[i];
+			break;
+		}
+	}
+
+	if (!ext_data) {
+		sbi_printf("%s: extension %s not found in sbi_hart_ext[]\n",
+			   __func__, ext);
+		return false;
+	}
+
+	bitmap_zero(hart_exts, SBI_HART_EXT_MAX);
+
+	rc = fdt_parse_isa_extensions(fdt, hart_exts);
+	if (rc) {
+		sbi_printf("%s: failed to parse isa extensions\n",
+			   __func__);
+		return false;
+	}
+
+	return bitmap_test(hart_exts, ext_data->id);
+}
+
 static int fdt_parse_uart_node_common(const void *fdt, int nodeoffset,
 				      struct platform_uart_data *uart,
 				      unsigned long default_freq,
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#2 lore raw

RFC PATCH 2/7 include: sbi: sbi_platform: add reserved_pmp_count field

In reply to Yu-Chien Peter Lin

Add reserved_pmp_count field to struct sbi_platform to specify the configurable number of high-priority PMP entries reserved.

---
 include/sbi/sbi_platform.h | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
include/sbi/sbi_platform.h +10 -5
index c6d30080..e2bf9b77 100644
@@ -31,16 +31,18 @@
 #define SBI_PLATFORM_HART_STACK_SIZE_OFFSET (0x54)
 /** Offset of heap_size in struct sbi_platform */
 #define SBI_PLATFORM_HEAP_SIZE_OFFSET (0x58)
+/** Offset of reserved PMP entry count in struct sbi_platform */
+#define SBI_PLATFORM_RESERVED_PMP_COUNT_OFFSET (0x5c)
 /** Offset of reserved in struct sbi_platform */
-#define SBI_PLATFORM_RESERVED_OFFSET (0x5c)
+#define SBI_PLATFORM_RESERVED_OFFSET (0x60)
 /** Offset of platform_ops_addr in struct sbi_platform */
-#define SBI_PLATFORM_OPS_OFFSET (0x60)
+#define SBI_PLATFORM_OPS_OFFSET (0x68)
 /** Offset of firmware_context in struct sbi_platform */
-#define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x60 + __SIZEOF_POINTER__)
+#define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x68 + __SIZEOF_POINTER__)
 /** Offset of hart_index2id in struct sbi_platform */
-#define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x60 + (__SIZEOF_POINTER__ * 2))
+#define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x68 + (__SIZEOF_POINTER__ * 2))
 /** Offset of cbom_block_size in struct sbi_platform */
-#define SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET (0x60 + (__SIZEOF_POINTER__ * 3))
+#define SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET (0x68 + (__SIZEOF_POINTER__ * 3))
 
 #define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT		(1UL << 12)
 
@@ -182,6 +184,8 @@ struct sbi_platform {
 	u32 hart_stack_size;
 	/** Size of heap shared by all HARTs */
 	u32 heap_size;
+	/** Reserved PMP entry count */
+	u8 reserved_pmp_count;
 	/** Reserved for future use */
 	u32 reserved;
 	/** Pointer to sbi platform operations */
@@ -214,6 +218,7 @@ assert_member_offset(struct sbi_platform, features, SBI_PLATFORM_FEATURES_OFFSET
 assert_member_offset(struct sbi_platform, hart_count, SBI_PLATFORM_HART_COUNT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_stack_size, SBI_PLATFORM_HART_STACK_SIZE_OFFSET);
 assert_member_offset(struct sbi_platform, heap_size, SBI_PLATFORM_HEAP_SIZE_OFFSET);
+assert_member_offset(struct sbi_platform, reserved_pmp_count, SBI_PLATFORM_RESERVED_PMP_COUNT_OFFSET);
 assert_member_offset(struct sbi_platform, reserved, SBI_PLATFORM_RESERVED_OFFSET);
 assert_member_offset(struct sbi_platform, platform_ops_addr, SBI_PLATFORM_OPS_OFFSET);
 assert_member_offset(struct sbi_platform, firmware_context, SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET);
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#3 lore raw

RFC PATCH 3/7 platform: generic: platform: introduce fw_platform_get_reserved_pmp_count()

In reply to Yu-Chien Peter Lin

Introduce fw_platform_get_reserved_pmp_count() to parse the reserved-pmp-count property from the opensbi-config node.

Since mseccfg.{RLB,MMWP,MML} are sticky bits, we can't poll them to detect the presence of Smempp. Therefore, the only way is to parse it from FDT riscv,isa-extensions or riscv,isa.

---
 platform/generic/platform.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
platform/generic/platform.c +34 -0
index 2e8c6051..10bc3137 100644
@@ -67,6 +67,39 @@ default_config:
 	return fw_platform_calculate_heap_size(hart_count);
 }
 
+static u32 fw_platform_get_reserved_pmp_count(const void *fdt)
+{
+	int chosen_offset, config_offset, len;
+	u32 reserved_pmp_count;
+	const fdt32_t *val;
+
+	/*
+	 * Reserve one default entry if smepmp is enabned.
+	 * This entry is required for sbi_hart_map_saddr().
+	 */
+	if (fdt_has_isa_extension(fdt, "smepmp"))
+		reserved_pmp_count = 1;
+	else
+		reserved_pmp_count = 0;
+
+
+	/* Get the heap size from device tree */
+	chosen_offset = fdt_path_offset(fdt, "/chosen");
+	if (chosen_offset < 0)
+		goto out;
+
+	config_offset = fdt_node_offset_by_compatible(fdt, chosen_offset, "opensbi,config");
+	if (config_offset < 0)
+		goto out;
+
+	val = (fdt32_t *)fdt_getprop(fdt, config_offset, "reserved-pmp-count", &len);
+	if (len > 0 && val)
+		return fdt32_to_cpu(*val) + reserved_pmp_count;
+
+out:
+	return reserved_pmp_count;
+}
+
 extern struct sbi_platform platform;
 static bool platform_has_mlevel_imsic = false;
 static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
@@ -185,6 +218,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
+	platform.reserved_pmp_count = fw_platform_get_reserved_pmp_count(fdt);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
 	platform.cbom_block_size = cbom_block_size;
 
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

RFC PATCH 4/7 lib: sbi_init: print total and reserved PMP counts

In reply to Yu-Chien Peter Lin

Show both total and reserved PMP counts in boot log.

---
 lib/sbi/sbi_init.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/sbi/sbi_init.c +4 -2
index 84a63748..367476f0 100644
@@ -163,6 +163,7 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
 	int xlen;
 	char str[256];
 	const struct sbi_domain *dom = sbi_domain_thishart_ptr();
+	const struct sbi_platform *plat = sbi_platform_ptr(scratch);
 
 	if (scratch->options & SBI_SCRATCH_NO_BOOT_PRINTS)
 		return;
@@ -183,8 +184,9 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
 	sbi_printf("Boot HART Base ISA          : %s\n", str);
 	sbi_hart_get_extensions_str(scratch, str, sizeof(str));
 	sbi_printf("Boot HART ISA Extensions    : %s\n", str);
-	sbi_printf("Boot HART PMP Count         : %d\n",
-		   sbi_hart_pmp_count(scratch));
+	sbi_printf("Boot HART PMP Count         : "
+		   "%d (total), %d (reserved)\n",
+		   sbi_hart_pmp_count(scratch), plat->reserved_pmp_count);
 	sbi_printf("Boot HART PMP Granularity   : %u bits\n",
 		   sbi_hart_pmp_log2gran(scratch));
 	sbi_printf("Boot HART PMP Address Bits  : %d\n",
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#5 lore raw

RFC PATCH 5/7 lib: sbi: riscv_asm: add reserved_pmp_alloc() to allocate a reserved PMP

In reply to Yu-Chien Peter Lin

Add a function that allocates unused PMP entries from the high-priority reserved pool.

---
 include/sbi/riscv_asm.h |  2 ++
 lib/sbi/riscv_asm.c     | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)
include/sbi/riscv_asm.h +2 -0
index ef48dc89..5fff2196 100644
@@ -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
lib/sbi/riscv_asm.c +25 -0
index c7d75ac0..daa5087a 100644
@@ -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;
+}
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#6 lore raw

RFC PATCH 6/7 lib: sbi: sbi_hart: extend PMP handling to support multiple reserved entries

In reply to Yu-Chien Peter Lin

Previously, OpenSBI supported only a single reserved PMP entry. This change adds support for multiple reserved PMP entries, configurable via the reserved-pmp-count DT property in the opensbi-config.

---
 include/sbi/sbi_hart.h       | 15 ----------
 lib/sbi/sbi_domain_context.c |  6 +++-
 lib/sbi/sbi_hart.c           | 56 +++++++++++++++++++++++++++---------
 3 files changed, 47 insertions(+), 30 deletions(-)
include/sbi/sbi_hart.h +0 -15
index 82b19dcf..86c2675b 100644
@@ -101,21 +101,6 @@ enum sbi_hart_csrs {
 	SBI_HART_CSR_MAX,
 };
 
-/*
- * Smepmp enforces access boundaries between M-mode and
- * S/U-mode. When it is enabled, the PMPs are programmed
- * such that M-mode doesn't have access to S/U-mode memory.
- *
- * To give M-mode R/W access to the shared memory between M and
- * S/U-mode, first entry is reserved. It is disabled at boot.
- * When shared memory access is required, the physical address
- * should be programmed into the first PMP entry with R/W
- * permissions to the M-mode. Once the work is done, it should be
- * unmapped. sbi_hart_map_saddr/sbi_hart_unmap_saddr function
- * pair should be used to map/unmap the shared memory.
- */
-#define SBI_SMEPMP_RESV_ENTRY		0
-
 struct sbi_hart_features {
 	bool detected;
 	int priv_version;
lib/sbi/sbi_domain_context.c +5 -1
index fb04d81d..a78bd28c 100644
@@ -101,6 +101,7 @@ static void switch_to_next_domain_context(struct hart_context *ctx,
 	struct sbi_domain *current_dom = ctx->dom;
 	struct sbi_domain *target_dom = dom_ctx->dom;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	const struct sbi_platform *plat = sbi_platform_thishart_ptr();
 	unsigned int pmp_count = sbi_hart_pmp_count(scratch);
 
 	/* Assign current hart to target domain */
@@ -115,7 +116,10 @@ static void switch_to_next_domain_context(struct hart_context *ctx,
 	spin_unlock(&target_dom->assigned_harts_lock);
 
 	/* Reconfigure PMP settings for the new domain */
-	for (int i = 0; i < pmp_count; i++) {
+	for (int i = plat->reserved_pmp_count; i < pmp_count; i++) {
+		if (pmp_is_fw_region(i, current_dom))
+			continue;
+
 		sbi_platform_pmp_disable(sbi_platform_thishart_ptr(), i);
 		pmp_disable(i);
 	}
lib/sbi/sbi_hart.c +42 -14

130 lines not on this page · raw message

Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#7 lore raw

RFC PATCH 7/7 docs: opensbi_config: add description for reserved-pmp-count

In reply to Yu-Chien Peter Lin

Add documentation for the reserved-pmp-count property, which specifies the number of additional high-priority PMP entries.

---
 docs/opensbi_config.md | 4 ++++
 1 file changed, 4 insertions(+)
docs/opensbi_config.md +4 -0
index 104d82c9..02481800 100644
@@ -26,6 +26,9 @@ The DT properties of a domain configuration DT node are as follows:
 * **heap-size** (Optional) - When present, the specified value is used
   as the size of the heap in bytes.
 
+* **reserved-pmp-count** (Optional) - When present, the specified value
+  is used as the additional number of reserved high-priority PMP entries.
+
 * **system-suspend-test** (Optional) - When present, enable a system
   suspend test implementation which simply waits five seconds and issues a WFI.
 
@@ -40,6 +43,7 @@ The OpenSBI Configuration Node will be deleted at the end of cold boot
             compatible = "opensbi,config";
             cold-boot-harts = <&cpu1 &cpu2 &cpu3 &cpu4>;
             heap-size = <0x400000>;
+            reserved-pmp-count = <2>;
             system-suspend-test;
         };
     };
Signature · 7 lines
2.48.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#8 lore raw
In reply to Yu-Chien Peter Lin
Quoted 20 lines · Yu-Chien Peter Lin

This series extends the number of reserved PMP entry. The capabilities of the reserved PMP entries include:
- Highest priority
- Allow TOR mode for platform-specific use cases
- Can be consistent across domain context switches
- Allow runtime PMP allocation

A potential use case for reserved PMP entries is parsing gaps in memory ports marked as PMA_UNSAFE [1] in device-tree. These gaps may not be able to exactly covered by 1 or 2 NAPOT entries, so TOR mode PMP can be allocated to save PMP usage. Besides, they don't need reconfiguration across domains.

This series depends on:
1) https://lore.kernel.org/all/[email protected]/
2) https://lore.kernel.org/all/[email protected]/

[1] https://patchwork.kernel.org/project/linux-riscv/patch/[email protected]/

Quoted 11 lines

Yu-Chien Peter Lin (7):
lib: utils: fdt_helper: add fdt_has_isa_extension() helper
include: sbi: sbi_platform: add reserved_pmp_count field
platform: generic: platform: introduce
fw_platform_get_reserved_pmp_count()
lib: sbi_init: print total and reserved PMP counts
lib: sbi: riscv_asm: add reserved_pmp_alloc() to allocate a reserved
PMP
lib: sbi: sbi_hart: extend PMP handling to support multiple reserved
entries
docs: opensbi_config: add description for reserved-pmp-count

Same issue as the other series, it is not showing up in OpenSBI patchwork (https://patchwork.ozlabs.org/project/opensbi/list/).

Can you please rebase and resend this series ?

Regards,
Anup

Quoted 22 lines
 docs/opensbi_config.md             |  4 +++
 include/sbi/riscv_asm.h            |  2 ++
 include/sbi/sbi_hart.h             | 15 --------
 include/sbi/sbi_platform.h         | 15 +++++---
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/riscv_asm.c                | 25 +++++++++++++
 lib/sbi/sbi_domain_context.c       |  6 +++-
 lib/sbi/sbi_hart.c                 | 56 ++++++++++++++++++++++--------
 lib/sbi/sbi_init.c                 |  6 ++--
 lib/utils/fdt/fdt_helper.c         | 35 +++++++++++++++++++
 platform/generic/platform.c        | 34 ++++++++++++++++++
 11 files changed, 163 insertions(+), 37 deletions(-)

--
2.48.0

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi