From: Nicholas Piggin <npiggin@gmail.com>
To: opensbi@lists.infradead.org
Cc: Nicholas Piggin <npiggin@gmail.com>
Subject: [PATCH 1/7] lib: sbi: Introduce pmp_t type
Date: Thu, 30 Apr 2026 14:55:18 +1000 [thread overview]
Message-ID: <20260430045528.420437-2-npiggin@gmail.com> (raw)
In-Reply-To: <20260430045528.420437-1-npiggin@gmail.com>
To help abstract details of PMP encoding and access, add a new pmp_t
type which contains address and cfg in the format of the riscv CSRs.
There is no functional change.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
lib/sbi/riscv_asm.c | 41 ++++++++++++++++++++++++++---------------
1 file changed, 26 insertions(+), 15 deletions(-)
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index 3e44320f..acb6b5e6 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -272,6 +272,12 @@ void csr_write_num(int csr_num, unsigned long val)
#undef switchcase_csr_write
}
+struct pmp {
+ unsigned long addr;
+ u8 cfg;
+};
+typedef struct pmp pmp_t;
+
static unsigned long ctz(unsigned long x)
{
unsigned long ret = 0;
@@ -335,7 +341,8 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
{
int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
unsigned long cfgmask, pmpcfg;
- unsigned long addrmask, pmpaddr;
+ unsigned long addrmask;
+ pmp_t pmp;
/* check parameters */
if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT)
@@ -356,25 +363,26 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
/* encode PMP config */
prot &= ~PMP_A;
prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT;
- cfgmask = ~(0xffUL << pmpcfg_shift);
- pmpcfg = (csr_read_num(pmpcfg_csr) & cfgmask);
- pmpcfg |= ((prot << pmpcfg_shift) & ~cfgmask);
+ pmp.cfg = prot;
/* encode PMP address */
if (log2len == PMP_SHIFT) {
- pmpaddr = (addr >> PMP_SHIFT);
+ pmp.addr = (addr >> PMP_SHIFT);
} else {
if (log2len == __riscv_xlen) {
- pmpaddr = -1UL;
+ pmp.addr = -1UL;
} else {
addrmask = (1UL << (log2len - PMP_SHIFT)) - 1;
- pmpaddr = ((addr >> PMP_SHIFT) & ~addrmask);
- pmpaddr |= (addrmask >> 1);
+ pmp.addr = ((addr >> PMP_SHIFT) & ~addrmask);
+ pmp.addr |= (addrmask >> 1);
}
}
/* write csrs */
- csr_write_num(pmpaddr_csr, pmpaddr);
+ 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;
@@ -384,8 +392,9 @@ 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, pmpcfg, prot;
+ unsigned long cfgmask, prot;
unsigned long t1, addr, len;
+ pmp_t pmp;
/* check parameters */
if (n >= PMP_COUNT || !prot_out || !addr_out || !log2len)
@@ -404,14 +413,16 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
#endif
pmpaddr_csr = CSR_PMPADDR0 + n;
- /* decode PMP config */
cfgmask = (0xffUL << pmpcfg_shift);
- pmpcfg = csr_read_num(pmpcfg_csr) & cfgmask;
- prot = pmpcfg >> pmpcfg_shift;
+ pmp.cfg = (csr_read_num(pmpcfg_csr) & cfgmask) >> pmpcfg_shift;
+ pmp.addr = csr_read_num(pmpaddr_csr);
+
+ /* decode PMP config */
+ prot = pmp.cfg;
/* decode PMP address */
if ((prot & PMP_A) == PMP_A_NAPOT) {
- addr = csr_read_num(pmpaddr_csr);
+ addr = pmp.addr;
if (addr == -1UL) {
addr = 0;
len = __riscv_xlen;
@@ -421,7 +432,7 @@ int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
len = (t1 + PMP_SHIFT + 1);
}
} else {
- addr = csr_read_num(pmpaddr_csr) << PMP_SHIFT;
+ addr = pmp.addr << PMP_SHIFT;
len = PMP_SHIFT;
}
--
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 ` Nicholas Piggin [this message]
2026-04-30 4:55 ` [PATCH 2/7] lib: sbi: split PMP encoding and CSR access Nicholas Piggin
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-2-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