public inbox for opensbi@lists.infradead.org
 help / color / mirror / Atom feed
From: Yu-Chien Peter Lin <peter.lin@sifive.com>
To: opensbi@lists.infradead.org
Cc: greentime.hu@sifive.com,
	Yu-Chien Peter Lin <peter.lin@sifive.com>,
	zong.li@sifive.com
Subject: [RFC PATCH 5/7] lib: sbi: riscv_asm: add reserved_pmp_alloc() to allocate a reserved PMP
Date: Fri, 15 Aug 2025 18:01:13 +0800	[thread overview]
Message-ID: <20250815100116.27776-6-peter.lin@sifive.com> (raw)
In-Reply-To: <20250815100116.27776-1-peter.lin@sifive.com>

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

Signed-off-by: Yu-Chien Peter Lin <peter.lin@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


-- 
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

  parent reply	other threads:[~2025-08-15 13:14 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-15 10:01 [RFC PATCH 0/7] Extend the reserved PMP entries Yu-Chien Peter Lin
2025-08-15 10:01 ` [RFC PATCH 1/7] lib: utils: fdt_helper: add fdt_has_isa_extension() helper Yu-Chien Peter Lin
2025-08-15 10:01 ` [RFC PATCH 2/7] include: sbi: sbi_platform: add reserved_pmp_count field Yu-Chien Peter Lin
2025-08-15 10:01 ` [RFC PATCH 3/7] platform: generic: platform: introduce fw_platform_get_reserved_pmp_count() Yu-Chien Peter Lin
2025-08-15 10:01 ` [RFC PATCH 4/7] lib: sbi_init: print total and reserved PMP counts Yu-Chien Peter Lin
2025-08-15 10:01 ` Yu-Chien Peter Lin [this message]
2025-08-15 10:01 ` [RFC PATCH 6/7] lib: sbi: sbi_hart: extend PMP handling to support multiple reserved entries Yu-Chien Peter Lin
2025-08-15 10:01 ` [RFC PATCH 7/7] docs: opensbi_config: add description for reserved-pmp-count Yu-Chien Peter Lin
2025-10-06  5:21 ` [RFC PATCH 0/7] Extend the reserved PMP entries Anup Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250815100116.27776-6-peter.lin@sifive.com \
    --to=peter.lin@sifive.com \
    --cc=greentime.hu@sifive.com \
    --cc=opensbi@lists.infradead.org \
    --cc=zong.li@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox