From: Nicholas Piggin <npiggin@gmail.com>
To: opensbi@lists.infradead.org
Cc: Nicholas Piggin <npiggin@gmail.com>
Subject: [PATCH 2/7] lib: sbi: split PMP encoding and CSR access
Date: Thu, 30 Apr 2026 14:55:19 +1000 [thread overview]
Message-ID: <20260430045528.420437-3-npiggin@gmail.com> (raw)
In-Reply-To: <20260430045528.420437-1-npiggin@gmail.com>
Allow PMP encoding functions to be shared with non-hart PMP manipulation
by splitting encoding / decoding and hart PMP CSR access into their own
functions.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
lib/sbi/riscv_asm.c | 119 +++++++++++++++++++++++++++-----------------
1 file changed, 73 insertions(+), 46 deletions(-)
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index acb6b5e6..e33e3c36 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -336,70 +336,50 @@ int is_pmp_entry_mapped(unsigned long entry)
return false;
}
-int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
- unsigned long log2len)
+static int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
+ unsigned long log2len)
{
- int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
- unsigned long cfgmask, pmpcfg;
- unsigned long addrmask;
- pmp_t pmp;
-
/* check parameters */
- if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT)
+ if (log2len > __riscv_xlen || log2len < PMP_SHIFT)
return SBI_EINVAL;
- /* calculate PMP register and offset */
-#if __riscv_xlen == 32
- pmpcfg_csr = CSR_PMPCFG0 + (n >> 2);
- pmpcfg_shift = (n & 3) << 3;
-#elif __riscv_xlen == 64
- pmpcfg_csr = (CSR_PMPCFG0 + (n >> 2)) & ~1;
- pmpcfg_shift = (n & 7) << 3;
-#else
-# error "Unexpected __riscv_xlen"
-#endif
- pmpaddr_csr = CSR_PMPADDR0 + n;
-
/* encode PMP config */
prot &= ~PMP_A;
prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT;
- pmp.cfg = prot;
+ pmp->cfg = prot;
/* encode PMP address */
if (log2len == PMP_SHIFT) {
- pmp.addr = (addr >> PMP_SHIFT);
+ pmp->addr = (addr >> PMP_SHIFT);
} else {
if (log2len == __riscv_xlen) {
- pmp.addr = -1UL;
+ pmp->addr = -1UL;
} else {
+ unsigned long addrmask;
addrmask = (1UL << (log2len - PMP_SHIFT)) - 1;
- pmp.addr = ((addr >> PMP_SHIFT) & ~addrmask);
- pmp.addr |= (addrmask >> 1);
+ pmp->addr = ((addr >> PMP_SHIFT) & ~addrmask);
+ pmp->addr |= (addrmask >> 1);
}
}
- /* write csrs */
- csr_write_num(pmpaddr_csr, pmp.addr);
- cfgmask = ~(0xffUL << pmpcfg_shift);
- pmpcfg = (csr_read_num(pmpcfg_csr) & cfgmask);
- pmpcfg |= (((unsigned long)pmp.cfg << pmpcfg_shift) & ~cfgmask);
- csr_write_num(pmpcfg_csr, pmpcfg);
-
- return 0;
+ return SBI_OK;
}
-int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
- unsigned long *log2len)
+int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
+ unsigned long log2len)
{
int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
- unsigned long cfgmask, prot;
- unsigned long t1, addr, len;
+ unsigned long cfgmask, pmpcfg;
pmp_t pmp;
+ int rc;
/* check parameters */
- if (n >= PMP_COUNT || !prot_out || !addr_out || !log2len)
+ if (n >= PMP_COUNT)
return SBI_EINVAL;
- *prot_out = *addr_out = *log2len = 0;
+
+ rc = pmp_encode(&pmp, prot, addr, log2len);
+ if (rc)
+ return rc;
/* calculate PMP register and offset */
#if __riscv_xlen == 32
@@ -413,16 +393,33 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
#endif
pmpaddr_csr = CSR_PMPADDR0 + n;
- cfgmask = (0xffUL << pmpcfg_shift);
- pmp.cfg = (csr_read_num(pmpcfg_csr) & cfgmask) >> pmpcfg_shift;
- pmp.addr = csr_read_num(pmpaddr_csr);
+ /* write csrs */
+ csr_write_num(pmpaddr_csr, pmp.addr);
+ cfgmask = ~(0xffUL << pmpcfg_shift);
+ pmpcfg = (csr_read_num(pmpcfg_csr) & cfgmask);
+ pmpcfg |= (((unsigned long)pmp.cfg << pmpcfg_shift) & ~cfgmask);
+ csr_write_num(pmpcfg_csr, pmpcfg);
+
+ return 0;
+}
+
+static int pmp_decode(pmp_t *pmp, unsigned long *prot_out, unsigned long *addr_out,
+ unsigned long *log2len)
+{
+ unsigned long prot;
+ unsigned long t1, addr, len;
+
+ /* check parameters */
+ if (!prot_out || !addr_out || !log2len)
+ return SBI_EINVAL;
+ *prot_out = *addr_out = *log2len = 0;
/* decode PMP config */
- prot = pmp.cfg;
+ prot = pmp->cfg;
/* decode PMP address */
if ((prot & PMP_A) == PMP_A_NAPOT) {
- addr = pmp.addr;
+ addr = pmp->addr;
if (addr == -1UL) {
addr = 0;
len = __riscv_xlen;
@@ -432,7 +429,7 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
len = (t1 + PMP_SHIFT + 1);
}
} else {
- addr = pmp.addr << PMP_SHIFT;
+ addr = pmp->addr << PMP_SHIFT;
len = PMP_SHIFT;
}
@@ -441,5 +438,35 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
*addr_out = addr;
*log2len = len;
- return 0;
+ return SBI_OK;
+}
+
+int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
+ unsigned long *log2len)
+{
+ int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
+ unsigned long cfgmask;
+ pmp_t pmp;
+
+ /* check parameters */
+ if (n >= PMP_COUNT)
+ return SBI_EINVAL;
+
+ /* calculate PMP register and offset */
+#if __riscv_xlen == 32
+ pmpcfg_csr = CSR_PMPCFG0 + (n >> 2);
+ pmpcfg_shift = (n & 3) << 3;
+#elif __riscv_xlen == 64
+ pmpcfg_csr = (CSR_PMPCFG0 + (n >> 2)) & ~1;
+ pmpcfg_shift = (n & 7) << 3;
+#else
+# error "Unexpected __riscv_xlen"
+#endif
+ pmpaddr_csr = CSR_PMPADDR0 + n;
+
+ cfgmask = (0xffUL << pmpcfg_shift);
+ pmp.cfg = (csr_read_num(pmpcfg_csr) & cfgmask) >> pmpcfg_shift;
+ pmp.addr = csr_read_num(pmpaddr_csr);
+
+ return pmp_decode(&pmp, prot_out, addr_out, log2len);
}
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
next prev parent reply other threads:[~2026-04-30 4:55 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
2026-04-30 4:55 ` [PATCH 1/7] lib: sbi: Introduce pmp_t type Nicholas Piggin
2026-04-30 4:55 ` Nicholas Piggin [this message]
2026-04-30 4:55 ` [PATCH 3/7] lib: sbi: Move RISC-V PMP encoding functions to sbi_pmp.c Nicholas Piggin
2026-04-30 4:55 ` [PATCH 3/7] lib: sbi: Move non-HART PMP " Nicholas Piggin
2026-04-30 4:55 ` [PATCH 4/7] lib: sbi: Add PMP CSR read and write accessors Nicholas Piggin
2026-04-30 4:55 ` [PATCH 5/7] lib: sbi: Add pmp_is_enabled() helper Nicholas Piggin
2026-04-30 4:55 ` [PATCH 6/7] lib: sbi: Add hart_ prefix to PMP functions Nicholas Piggin
2026-04-30 4:55 ` [PATCH 7/7] lib: sbi: Move hart PMP functions to sbi_hart_pmp.c Nicholas Piggin
2026-06-10 12:35 ` [PATCH 0/7] Make PMP encoding usable for non-hart PMPs 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=20260430045528.420437-3-npiggin@gmail.com \
--to=npiggin@gmail.com \
--cc=opensbi@lists.infradead.org \
/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