From: Nicholas Piggin <npiggin@gmail.com>
To: opensbi@lists.infradead.org
Cc: Nicholas Piggin <npiggin@gmail.com>
Subject: [PATCH 4/7] lib: sbi: Add PMP CSR read and write accessors
Date: Thu, 30 Apr 2026 14:55:22 +1000 [thread overview]
Message-ID: <20260430045528.420437-6-npiggin@gmail.com> (raw)
In-Reply-To: <20260430045528.420437-1-npiggin@gmail.com>
PMPCFG CSR access is non-trivial as it requires shifting and masking, it
makes PMP manipulation code simpler if this basic CSR read/write access is
abstracted away.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
lib/sbi/riscv_asm.c | 114 ++++++++++++++++++++++----------------------
1 file changed, 58 insertions(+), 56 deletions(-)
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index 9b45805c..2fb0f585 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -273,14 +273,16 @@ void csr_write_num(int csr_num, unsigned long val)
#undef switchcase_csr_write
}
-int pmp_disable(unsigned int n)
+static int hart_pmp_read(pmp_t *pmp, unsigned int n)
{
- int pmpcfg_csr, pmpcfg_shift;
- unsigned long cfgmask, pmpcfg;
+ int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
+ unsigned long cfgmask;
+ /* 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;
@@ -290,48 +292,24 @@ int pmp_disable(unsigned int n)
#else
# error "Unexpected __riscv_xlen"
#endif
+ pmpaddr_csr = CSR_PMPADDR0 + n;
- /* Clear the address matching bits to disable the pmp entry */
- cfgmask = ~(0xffUL << pmpcfg_shift);
- pmpcfg = (csr_read_num(pmpcfg_csr) & cfgmask);
-
- csr_write_num(pmpcfg_csr, pmpcfg);
+ cfgmask = (0xffUL << pmpcfg_shift);
+ pmp->cfg = (csr_read_num(pmpcfg_csr) & cfgmask) >> pmpcfg_shift;
+ pmp->addr = csr_read_num(pmpaddr_csr);
return SBI_OK;
}
-int is_pmp_entry_mapped(unsigned long entry)
-{
- unsigned long prot;
- unsigned long addr;
- unsigned long log2len;
-
- if (pmp_get(entry, &prot, &addr, &log2len) != 0)
- return false;
-
- /* If address matching bits are non-zero, the entry is enable */
- if (prot & PMP_A)
- return true;
-
- return false;
-}
-
-int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
- unsigned long log2len)
+static int hart_pmp_write(pmp_t *pmp, unsigned int n)
{
int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
unsigned long cfgmask, pmpcfg;
- pmp_t pmp;
- int rc;
/* check parameters */
if (n >= PMP_COUNT)
return SBI_EINVAL;
- rc = pmp_encode(&pmp, prot, addr, log2len);
- if (rc)
- return rc;
-
/* calculate PMP register and offset */
#if __riscv_xlen == 32
pmpcfg_csr = CSR_PMPCFG0 + (n >> 2);
@@ -345,41 +323,65 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
pmpaddr_csr = CSR_PMPADDR0 + n;
/* write csrs */
- csr_write_num(pmpaddr_csr, pmp.addr);
+ 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);
+ 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_disable(unsigned int n)
{
- int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
- unsigned long cfgmask;
pmp_t pmp;
+ int rc;
- /* check parameters */
- if (n >= PMP_COUNT)
- return SBI_EINVAL;
+ rc = hart_pmp_read(&pmp, n);
+ if (rc)
+ return rc;
- /* 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;
+ pmp.cfg = 0;
- cfgmask = (0xffUL << pmpcfg_shift);
- pmp.cfg = (csr_read_num(pmpcfg_csr) & cfgmask) >> pmpcfg_shift;
- pmp.addr = csr_read_num(pmpaddr_csr);
+ return hart_pmp_write(&pmp, n);
+}
+
+int is_pmp_entry_mapped(unsigned long entry)
+{
+ pmp_t pmp;
+
+ if (hart_pmp_read(&pmp, entry) != SBI_OK)
+ return false;
+
+ /* If address matching bits are non-zero, the entry is enable */
+ if (pmp.cfg & PMP_A)
+ return true;
+
+ return false;
+}
+
+int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
+ unsigned long log2len)
+{
+ pmp_t pmp;
+ int rc;
+
+ rc = pmp_encode(&pmp, prot, addr, log2len);
+ if (rc)
+ return rc;
+
+ return hart_pmp_write(&pmp, n);
+}
+
+int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
+ unsigned long *log2len)
+{
+ pmp_t pmp;
+ int rc;
+
+ rc = hart_pmp_read(&pmp, n);
+ if (rc)
+ return rc;
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:56 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 ` [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 ` Nicholas Piggin [this message]
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-6-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