* [PATCH 1/7] lib: sbi: Introduce pmp_t type
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
2026-04-30 4:55 ` [PATCH 2/7] lib: sbi: split PMP encoding and CSR access Nicholas Piggin
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
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
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 2/7] lib: sbi: split PMP encoding and CSR access
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
2026-04-30 4:55 ` [PATCH 3/7] lib: sbi: Move RISC-V PMP encoding functions to sbi_pmp.c Nicholas Piggin
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
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
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 3/7] lib: sbi: Move RISC-V PMP encoding functions to sbi_pmp.c
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 ` Nicholas Piggin
2026-04-30 4:55 ` [PATCH 3/7] lib: sbi: Move non-HART PMP " Nicholas Piggin
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
Create a new file for handling the RISC-V PMP format and the new pmp_t
type, as opposed to hart PMP CSR specific access.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
include/sbi/sbi_pmp.h | 22 ++++++++++
lib/sbi/objects.mk | 1 +
lib/sbi/riscv_asm.c | 89 +--------------------------------------
lib/sbi/sbi_pmp.c | 96 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 120 insertions(+), 88 deletions(-)
create mode 100644 include/sbi/sbi_pmp.h
create mode 100644 lib/sbi/sbi_pmp.c
diff --git a/include/sbi/sbi_pmp.h b/include/sbi/sbi_pmp.h
new file mode 100644
index 00000000..66a5cc75
--- /dev/null
+++ b/include/sbi/sbi_pmp.h
@@ -0,0 +1,22 @@
+/*
+ * SPDX-FileCopyrightText: (c) 2025-2026 Tenstorrent USA, Inc.
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#ifndef __SBI_PMP_H__
+#define __SBI_PMP_H__
+
+#include <sbi/sbi_types.h>
+
+struct pmp {
+ unsigned long addr;
+ u8 cfg;
+};
+typedef struct pmp pmp_t;
+
+int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
+ unsigned long log2len);
+int pmp_decode(pmp_t *pmp, unsigned long *prot, unsigned long *addr,
+ unsigned long *log2len);
+
+#endif
diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk
index 07d13229..0e29a277 100644
--- a/lib/sbi/objects.mk
+++ b/lib/sbi/objects.mk
@@ -87,6 +87,7 @@ libsbi-objs-y += sbi_init.o
libsbi-objs-y += sbi_ipi.o
libsbi-objs-y += sbi_irqchip.o
libsbi-objs-y += sbi_platform.o
+libsbi-objs-y += sbi_pmp.o
libsbi-objs-y += sbi_pmu.o
libsbi-objs-y += sbi_dbtr.o
libsbi-objs-y += sbi_mpxy.o
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index e33e3c36..9b45805c 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -12,6 +12,7 @@
#include <sbi/sbi_error.h>
#include <sbi/sbi_platform.h>
#include <sbi/sbi_console.h>
+#include <sbi/sbi_pmp.h>
/* determine CPU extension, return non-zero support */
int misa_extension_imp(char ext)
@@ -272,27 +273,6 @@ 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;
-
- if (x == 0)
- return 8 * sizeof(x);
-
- while (!(x & 1UL)) {
- ret++;
- x = x >> 1;
- }
-
- return ret;
-}
-
int pmp_disable(unsigned int n)
{
int pmpcfg_csr, pmpcfg_shift;
@@ -336,35 +316,6 @@ int is_pmp_entry_mapped(unsigned long entry)
return false;
}
-static int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
- unsigned long log2len)
-{
- /* check parameters */
- if (log2len > __riscv_xlen || log2len < PMP_SHIFT)
- return SBI_EINVAL;
-
- /* encode PMP config */
- prot &= ~PMP_A;
- prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT;
- pmp->cfg = prot;
-
- /* encode PMP address */
- if (log2len == PMP_SHIFT) {
- pmp->addr = (addr >> PMP_SHIFT);
- } else {
- if (log2len == __riscv_xlen) {
- pmp->addr = -1UL;
- } else {
- unsigned long addrmask;
- addrmask = (1UL << (log2len - PMP_SHIFT)) - 1;
- pmp->addr = ((addr >> PMP_SHIFT) & ~addrmask);
- pmp->addr |= (addrmask >> 1);
- }
- }
-
- return SBI_OK;
-}
-
int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
unsigned long log2len)
{
@@ -403,44 +354,6 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
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;
-
- /* decode PMP address */
- if ((prot & PMP_A) == PMP_A_NAPOT) {
- addr = pmp->addr;
- if (addr == -1UL) {
- addr = 0;
- len = __riscv_xlen;
- } else {
- t1 = ctz(~addr);
- addr = (addr & ~((1UL << t1) - 1)) << PMP_SHIFT;
- len = (t1 + PMP_SHIFT + 1);
- }
- } else {
- addr = pmp->addr << PMP_SHIFT;
- len = PMP_SHIFT;
- }
-
- /* return details */
- *prot_out = prot;
- *addr_out = addr;
- *log2len = len;
-
- return SBI_OK;
-}
-
int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
unsigned long *log2len)
{
diff --git a/lib/sbi/sbi_pmp.c b/lib/sbi/sbi_pmp.c
new file mode 100644
index 00000000..91eab87f
--- /dev/null
+++ b/lib/sbi/sbi_pmp.c
@@ -0,0 +1,96 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ * Anup Patel <anup.patel@wdc.com>
+ */
+
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_pmp.h>
+
+static unsigned long ctz(unsigned long x)
+{
+ unsigned long ret = 0;
+
+ if (x == 0)
+ return 8 * sizeof(x);
+
+ while (!(x & 1UL)) {
+ ret++;
+ x = x >> 1;
+ }
+
+ return ret;
+}
+
+int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
+ unsigned long log2len)
+{
+ /* check parameters */
+ if (log2len > __riscv_xlen || log2len < PMP_SHIFT)
+ return SBI_EINVAL;
+
+ /* encode PMP config */
+ prot &= ~PMP_A;
+ prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT;
+ pmp->cfg = prot;
+
+ /* encode PMP address */
+ if (log2len == PMP_SHIFT) {
+ pmp->addr = (addr >> PMP_SHIFT);
+ } else {
+ if (log2len == __riscv_xlen) {
+ pmp->addr = -1UL;
+ } else {
+ unsigned long addrmask;
+ addrmask = (1UL << (log2len - PMP_SHIFT)) - 1;
+ pmp->addr = ((addr >> PMP_SHIFT) & ~addrmask);
+ pmp->addr |= (addrmask >> 1);
+ }
+ }
+
+ return SBI_OK;
+}
+
+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;
+
+ /* decode PMP address */
+ if ((prot & PMP_A) == PMP_A_NAPOT) {
+ addr = pmp->addr;
+ if (addr == -1UL) {
+ addr = 0;
+ len = __riscv_xlen;
+ } else {
+ t1 = ctz(~addr);
+ addr = (addr & ~((1UL << t1) - 1)) << PMP_SHIFT;
+ len = (t1 + PMP_SHIFT + 1);
+ }
+ } else {
+ addr = pmp->addr << PMP_SHIFT;
+ len = PMP_SHIFT;
+ }
+
+ /* return details */
+ *prot_out = prot;
+ *addr_out = addr;
+ *log2len = len;
+
+ return SBI_OK;
+}
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 3/7] lib: sbi: Move non-HART PMP functions to sbi_pmp.c
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
` (2 preceding siblings ...)
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 ` Nicholas Piggin
2026-04-30 4:55 ` [PATCH 4/7] lib: sbi: Add PMP CSR read and write accessors Nicholas Piggin
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
Create a new file for handling the RISC-V PMP format.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
include/sbi/sbi_pmp.h | 22 ++++++++++
lib/sbi/objects.mk | 1 +
lib/sbi/riscv_asm.c | 89 +--------------------------------------
lib/sbi/sbi_pmp.c | 96 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 120 insertions(+), 88 deletions(-)
create mode 100644 include/sbi/sbi_pmp.h
create mode 100644 lib/sbi/sbi_pmp.c
diff --git a/include/sbi/sbi_pmp.h b/include/sbi/sbi_pmp.h
new file mode 100644
index 00000000..66a5cc75
--- /dev/null
+++ b/include/sbi/sbi_pmp.h
@@ -0,0 +1,22 @@
+/*
+ * SPDX-FileCopyrightText: (c) 2025-2026 Tenstorrent USA, Inc.
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#ifndef __SBI_PMP_H__
+#define __SBI_PMP_H__
+
+#include <sbi/sbi_types.h>
+
+struct pmp {
+ unsigned long addr;
+ u8 cfg;
+};
+typedef struct pmp pmp_t;
+
+int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
+ unsigned long log2len);
+int pmp_decode(pmp_t *pmp, unsigned long *prot, unsigned long *addr,
+ unsigned long *log2len);
+
+#endif
diff --git a/lib/sbi/objects.mk b/lib/sbi/objects.mk
index 07d13229..0e29a277 100644
--- a/lib/sbi/objects.mk
+++ b/lib/sbi/objects.mk
@@ -87,6 +87,7 @@ libsbi-objs-y += sbi_init.o
libsbi-objs-y += sbi_ipi.o
libsbi-objs-y += sbi_irqchip.o
libsbi-objs-y += sbi_platform.o
+libsbi-objs-y += sbi_pmp.o
libsbi-objs-y += sbi_pmu.o
libsbi-objs-y += sbi_dbtr.o
libsbi-objs-y += sbi_mpxy.o
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index e33e3c36..9b45805c 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -12,6 +12,7 @@
#include <sbi/sbi_error.h>
#include <sbi/sbi_platform.h>
#include <sbi/sbi_console.h>
+#include <sbi/sbi_pmp.h>
/* determine CPU extension, return non-zero support */
int misa_extension_imp(char ext)
@@ -272,27 +273,6 @@ 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;
-
- if (x == 0)
- return 8 * sizeof(x);
-
- while (!(x & 1UL)) {
- ret++;
- x = x >> 1;
- }
-
- return ret;
-}
-
int pmp_disable(unsigned int n)
{
int pmpcfg_csr, pmpcfg_shift;
@@ -336,35 +316,6 @@ int is_pmp_entry_mapped(unsigned long entry)
return false;
}
-static int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
- unsigned long log2len)
-{
- /* check parameters */
- if (log2len > __riscv_xlen || log2len < PMP_SHIFT)
- return SBI_EINVAL;
-
- /* encode PMP config */
- prot &= ~PMP_A;
- prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT;
- pmp->cfg = prot;
-
- /* encode PMP address */
- if (log2len == PMP_SHIFT) {
- pmp->addr = (addr >> PMP_SHIFT);
- } else {
- if (log2len == __riscv_xlen) {
- pmp->addr = -1UL;
- } else {
- unsigned long addrmask;
- addrmask = (1UL << (log2len - PMP_SHIFT)) - 1;
- pmp->addr = ((addr >> PMP_SHIFT) & ~addrmask);
- pmp->addr |= (addrmask >> 1);
- }
- }
-
- return SBI_OK;
-}
-
int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
unsigned long log2len)
{
@@ -403,44 +354,6 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
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;
-
- /* decode PMP address */
- if ((prot & PMP_A) == PMP_A_NAPOT) {
- addr = pmp->addr;
- if (addr == -1UL) {
- addr = 0;
- len = __riscv_xlen;
- } else {
- t1 = ctz(~addr);
- addr = (addr & ~((1UL << t1) - 1)) << PMP_SHIFT;
- len = (t1 + PMP_SHIFT + 1);
- }
- } else {
- addr = pmp->addr << PMP_SHIFT;
- len = PMP_SHIFT;
- }
-
- /* return details */
- *prot_out = prot;
- *addr_out = addr;
- *log2len = len;
-
- return SBI_OK;
-}
-
int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
unsigned long *log2len)
{
diff --git a/lib/sbi/sbi_pmp.c b/lib/sbi/sbi_pmp.c
new file mode 100644
index 00000000..91eab87f
--- /dev/null
+++ b/lib/sbi/sbi_pmp.c
@@ -0,0 +1,96 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ *
+ * Authors:
+ * Anup Patel <anup.patel@wdc.com>
+ */
+
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_pmp.h>
+
+static unsigned long ctz(unsigned long x)
+{
+ unsigned long ret = 0;
+
+ if (x == 0)
+ return 8 * sizeof(x);
+
+ while (!(x & 1UL)) {
+ ret++;
+ x = x >> 1;
+ }
+
+ return ret;
+}
+
+int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
+ unsigned long log2len)
+{
+ /* check parameters */
+ if (log2len > __riscv_xlen || log2len < PMP_SHIFT)
+ return SBI_EINVAL;
+
+ /* encode PMP config */
+ prot &= ~PMP_A;
+ prot |= (log2len == PMP_SHIFT) ? PMP_A_NA4 : PMP_A_NAPOT;
+ pmp->cfg = prot;
+
+ /* encode PMP address */
+ if (log2len == PMP_SHIFT) {
+ pmp->addr = (addr >> PMP_SHIFT);
+ } else {
+ if (log2len == __riscv_xlen) {
+ pmp->addr = -1UL;
+ } else {
+ unsigned long addrmask;
+ addrmask = (1UL << (log2len - PMP_SHIFT)) - 1;
+ pmp->addr = ((addr >> PMP_SHIFT) & ~addrmask);
+ pmp->addr |= (addrmask >> 1);
+ }
+ }
+
+ return SBI_OK;
+}
+
+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;
+
+ /* decode PMP address */
+ if ((prot & PMP_A) == PMP_A_NAPOT) {
+ addr = pmp->addr;
+ if (addr == -1UL) {
+ addr = 0;
+ len = __riscv_xlen;
+ } else {
+ t1 = ctz(~addr);
+ addr = (addr & ~((1UL << t1) - 1)) << PMP_SHIFT;
+ len = (t1 + PMP_SHIFT + 1);
+ }
+ } else {
+ addr = pmp->addr << PMP_SHIFT;
+ len = PMP_SHIFT;
+ }
+
+ /* return details */
+ *prot_out = prot;
+ *addr_out = addr;
+ *log2len = len;
+
+ return SBI_OK;
+}
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 4/7] lib: sbi: Add PMP CSR read and write accessors
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
` (3 preceding siblings ...)
2026-04-30 4:55 ` [PATCH 3/7] lib: sbi: Move non-HART PMP " Nicholas Piggin
@ 2026-04-30 4:55 ` Nicholas Piggin
2026-04-30 4:55 ` [PATCH 5/7] lib: sbi: Add pmp_is_enabled() helper Nicholas Piggin
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
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
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 5/7] lib: sbi: Add pmp_is_enabled() helper
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
` (4 preceding siblings ...)
2026-04-30 4:55 ` [PATCH 4/7] lib: sbi: Add PMP CSR read and write accessors Nicholas Piggin
@ 2026-04-30 4:55 ` Nicholas Piggin
2026-04-30 4:55 ` [PATCH 6/7] lib: sbi: Add hart_ prefix to PMP functions Nicholas Piggin
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
include/sbi/sbi_pmp.h | 1 +
lib/sbi/riscv_asm.c | 6 +-----
lib/sbi/sbi_pmp.c | 9 +++++++++
3 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/include/sbi/sbi_pmp.h b/include/sbi/sbi_pmp.h
index 66a5cc75..bce62d22 100644
--- a/include/sbi/sbi_pmp.h
+++ b/include/sbi/sbi_pmp.h
@@ -14,6 +14,7 @@ struct pmp {
};
typedef struct pmp pmp_t;
+bool pmp_is_enabled(pmp_t *pmp);
int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
unsigned long log2len);
int pmp_decode(pmp_t *pmp, unsigned long *prot, unsigned long *addr,
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index 2fb0f585..c0ad4dbe 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -353,11 +353,7 @@ int is_pmp_entry_mapped(unsigned long entry)
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;
+ return pmp_is_enabled(&pmp);
}
int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
diff --git a/lib/sbi/sbi_pmp.c b/lib/sbi/sbi_pmp.c
index 91eab87f..75b18764 100644
--- a/lib/sbi/sbi_pmp.c
+++ b/lib/sbi/sbi_pmp.c
@@ -28,6 +28,15 @@ static unsigned long ctz(unsigned long x)
return ret;
}
+bool pmp_is_enabled(pmp_t *pmp)
+{
+ /* If address matching bits are non-zero, the entry is enable */
+ if (pmp->cfg & PMP_A)
+ return true;
+
+ return false;
+}
+
int pmp_encode(pmp_t *pmp, unsigned long prot, unsigned long addr,
unsigned long log2len)
{
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 6/7] lib: sbi: Add hart_ prefix to PMP functions
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
` (5 preceding siblings ...)
2026-04-30 4:55 ` [PATCH 5/7] lib: sbi: Add pmp_is_enabled() helper Nicholas Piggin
@ 2026-04-30 4:55 ` 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
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
PMP functions that deal with hart PMP CSRs are given a hart_ prefix, to
distinguish from RISC-V PMP encoding functions.
The is_pmp_entry_mapped() function is changed a little more, to align
with other PMP conventions, and made to return a bool to make it more
obvious that it returns a bool and not an SBI_ return code.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
include/sbi/riscv_asm.h | 10 +++++----
lib/sbi/riscv_asm.c | 10 ++++-----
lib/sbi/sbi_hart_pmp.c | 18 ++++++++--------
platform/generic/eswin/eic770x.c | 36 ++++++++++++++++----------------
4 files changed, 38 insertions(+), 36 deletions(-)
diff --git a/include/sbi/riscv_asm.h b/include/sbi/riscv_asm.h
index 0cf3fc37..f845320b 100644
--- a/include/sbi/riscv_asm.h
+++ b/include/sbi/riscv_asm.h
@@ -81,6 +81,8 @@
#ifndef __ASSEMBLER__
+#include <sbi/sbi_types.h>
+
#define csr_swap(csr, val) \
({ \
register unsigned long __v = (unsigned long)(val); \
@@ -210,15 +212,15 @@ int misa_xlen(void);
void misa_string(int xlen, char *out, unsigned int out_sz);
/* Disable pmp entry at a given index */
-int pmp_disable(unsigned int n);
+int hart_pmp_disable(unsigned int n);
/* Check if the matching field is set */
-int is_pmp_entry_mapped(unsigned long entry);
+bool hart_is_pmp_enabled(unsigned int n);
-int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
+int hart_pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
unsigned long log2len);
-int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
+int hart_pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
unsigned long *log2len);
#endif /* !__ASSEMBLER__ */
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index c0ad4dbe..98a93462 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -332,7 +332,7 @@ static int hart_pmp_write(pmp_t *pmp, unsigned int n)
return SBI_OK;
}
-int pmp_disable(unsigned int n)
+int hart_pmp_disable(unsigned int n)
{
pmp_t pmp;
int rc;
@@ -346,17 +346,17 @@ int pmp_disable(unsigned int n)
return hart_pmp_write(&pmp, n);
}
-int is_pmp_entry_mapped(unsigned long entry)
+bool hart_is_pmp_enabled(unsigned int n)
{
pmp_t pmp;
- if (hart_pmp_read(&pmp, entry) != SBI_OK)
+ if (hart_pmp_read(&pmp, n) != SBI_OK)
return false;
return pmp_is_enabled(&pmp);
}
-int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
+int hart_pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
unsigned long log2len)
{
pmp_t pmp;
@@ -369,7 +369,7 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
return hart_pmp_write(&pmp, n);
}
-int pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
+int hart_pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
unsigned long *log2len)
{
pmp_t pmp;
diff --git a/lib/sbi/sbi_hart_pmp.c b/lib/sbi/sbi_hart_pmp.c
index 02a3b3c4..8b7b091c 100644
--- a/lib/sbi/sbi_hart_pmp.c
+++ b/lib/sbi/sbi_hart_pmp.c
@@ -100,7 +100,7 @@ static void sbi_hart_smepmp_set(struct sbi_scratch *scratch,
sbi_platform_pmp_set(sbi_platform_ptr(scratch),
pmp_idx, reg->flags, pmp_flags,
reg->base, reg->order);
- pmp_set(pmp_idx, pmp_flags, reg->base, reg->order);
+ hart_pmp_set(pmp_idx, pmp_flags, reg->base, reg->order);
} else {
sbi_printf("Can not configure pmp for domain %s because"
" memory region address 0x%lx or size 0x%lx "
@@ -139,7 +139,7 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch)
csr_set(CSR_MSECCFG, MSECCFG_RLB);
/* Disable the reserved entry */
- pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+ hart_pmp_disable(SBI_SMEPMP_RESV_ENTRY);
/* Program M-only regions when MML is not set. */
pmp_idx = 0;
@@ -206,7 +206,7 @@ static int sbi_hart_smepmp_configure(struct sbi_scratch *scratch)
}
/* Disable remaining PMP entries */
for(; pmp_idx < pmp_count; pmp_idx++)
- pmp_disable(pmp_idx);
+ hart_pmp_disable(pmp_idx);
/*
* All entries are programmed.
@@ -224,7 +224,7 @@ static int sbi_hart_smepmp_map_range(struct sbi_scratch *scratch,
unsigned int pmp_flags = (PMP_W | PMP_X);
unsigned long order, base = 0;
- if (is_pmp_entry_mapped(SBI_SMEPMP_RESV_ENTRY))
+ if (hart_is_pmp_enabled(SBI_SMEPMP_RESV_ENTRY))
return SBI_ENOSPC;
for (order = MAX(sbi_hart_pmp_log2gran(scratch), log2roundup(size));
@@ -244,7 +244,7 @@ static int sbi_hart_smepmp_map_range(struct sbi_scratch *scratch,
sbi_platform_pmp_set(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY,
SBI_DOMAIN_MEMREGION_SHARED_SURW_MRW,
pmp_flags, base, order);
- pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
+ hart_pmp_set(SBI_SMEPMP_RESV_ENTRY, pmp_flags, base, order);
return SBI_OK;
}
@@ -253,7 +253,7 @@ static int sbi_hart_smepmp_unmap_range(struct sbi_scratch *scratch,
unsigned long addr, unsigned long size)
{
sbi_platform_pmp_disable(sbi_platform_ptr(scratch), SBI_SMEPMP_RESV_ENTRY);
- return pmp_disable(SBI_SMEPMP_RESV_ENTRY);
+ return hart_pmp_disable(SBI_SMEPMP_RESV_ENTRY);
}
static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch)
@@ -281,7 +281,7 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch)
sbi_platform_pmp_set(sbi_platform_ptr(scratch),
pmp_idx, reg->flags, pmp_flags,
reg->base, reg->order);
- pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
+ hart_pmp_set(pmp_idx++, pmp_flags, reg->base, reg->order);
} else {
sbi_printf("Can not configure pmp for domain %s because"
" memory region address 0x%lx or size 0x%lx "
@@ -291,7 +291,7 @@ static int sbi_hart_oldpmp_configure(struct sbi_scratch *scratch)
}
/* Disable remaining PMP entries */
for(; pmp_idx < pmp_count; pmp_idx++)
- pmp_disable(pmp_idx);
+ hart_pmp_disable(pmp_idx);
sbi_hart_pmp_fence();
return 0;
@@ -307,7 +307,7 @@ static void sbi_hart_pmp_unconfigure(struct sbi_scratch *scratch)
continue;
sbi_platform_pmp_disable(sbi_platform_ptr(scratch), i);
- pmp_disable(i);
+ hart_pmp_disable(i);
}
}
diff --git a/platform/generic/eswin/eic770x.c b/platform/generic/eswin/eic770x.c
index 7330df9f..dfca0c41 100644
--- a/platform/generic/eswin/eic770x.c
+++ b/platform/generic/eswin/eic770x.c
@@ -254,21 +254,21 @@ static int eswin_eic7700_final_init(bool cold_boot)
__func__);
return SBI_EFAIL;
}
- pmp_set(pmp_idx++, sbi_domain_get_oldpmp_flags(reg),
- reg->base, reg->order);
+ hart_pmp_set(pmp_idx++, sbi_domain_get_oldpmp_flags(reg),
+ reg->base, reg->order);
}
- pmp_set(PMP_RESERVED_A, PMP_L, EIC770X_L3_ZERO_REMOTE,
- log2roundup(EIC770X_L3_ZERO_SIZE));
+ hart_pmp_set(PMP_RESERVED_A, PMP_L, EIC770X_L3_ZERO_REMOTE,
+ log2roundup(EIC770X_L3_ZERO_SIZE));
/**
* Enable P550 internal + System Port, so OpenSBI can access
* CLINT/PLIC/UART. Might be overwritten in pmp_configure.
*/
- pmp_set(PMP_FREE_A_START + PMP_FREE_A_COUNT - 1, 0, 0,
- log2roundup(EIC770X_MEMPORT_BASE));
+ hart_pmp_set(PMP_FREE_A_START + PMP_FREE_A_COUNT - 1, 0, 0,
+ log2roundup(EIC770X_MEMPORT_BASE));
- pmp_set(PMP_RESERVED_B, PMP_L, 0,
- log2roundup(EIC770X_MEMPORT_LIMIT));
+ hart_pmp_set(PMP_RESERVED_B, PMP_L, 0,
+ log2roundup(EIC770X_MEMPORT_LIMIT));
/**
* These must come after the setup of PMP, as we are about to
* enable speculation and HW prefetcher bits
@@ -321,13 +321,13 @@ static int eswin_eic7700_pmp_configure(struct sbi_scratch *scratch)
if (pmp_idx >= pmp_max)
goto no_more_pmp;
- pmp_set(pmp_idx++, sbi_domain_get_oldpmp_flags(reg),
- reg->base, reg->order);
+ hart_pmp_set(pmp_idx++, sbi_domain_get_oldpmp_flags(reg),
+ reg->base, reg->order);
prev = reg;
}
/* Disable the rest */
while (pmp_idx < pmp_max)
- pmp_disable(pmp_idx++);
+ hart_pmp_disable(pmp_idx++);
/* Process the second free range B [7-7] */
pmp_idx = PMP_FREE_B_START,
@@ -340,12 +340,12 @@ static int eswin_eic7700_pmp_configure(struct sbi_scratch *scratch)
if (pmp_idx >= pmp_max)
goto no_more_pmp;
- pmp_set(pmp_idx++, sbi_domain_get_oldpmp_flags(reg),
- reg->base, reg->order);
+ hart_pmp_set(pmp_idx++, sbi_domain_get_oldpmp_flags(reg),
+ reg->base, reg->order);
}
/* Disable the rest */
while (pmp_idx < pmp_max)
- pmp_disable(pmp_idx++);
+ hart_pmp_disable(pmp_idx++);
sbi_hart_pmp_fence();
return 0;
@@ -357,14 +357,14 @@ no_more_pmp:
static void eswin_eic7700_pmp_unconfigure(struct sbi_scratch *scratch)
{
/* Enable P550 internal + System Port */
- pmp_set(PMP_FREE_A_START + PMP_FREE_A_COUNT - 1, 0, 0,
- log2roundup(EIC770X_MEMPORT_BASE));
+ hart_pmp_set(PMP_FREE_A_START + PMP_FREE_A_COUNT - 1, 0, 0,
+ log2roundup(EIC770X_MEMPORT_BASE));
for (unsigned int i = 0; i < PMP_FREE_A_COUNT - 1; i++)
- pmp_disable(i + PMP_FREE_A_START);
+ hart_pmp_disable(i + PMP_FREE_A_START);
for (unsigned int i = 0; i < PMP_FREE_B_COUNT; i++)
- pmp_disable(i + PMP_FREE_B_START);
+ hart_pmp_disable(i + PMP_FREE_B_START);
}
static struct sbi_hart_protection eswin_eic7700_pmp_protection = {
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 7/7] lib: sbi: Move hart PMP functions to sbi_hart_pmp.c
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
` (6 preceding siblings ...)
2026-04-30 4:55 ` [PATCH 6/7] lib: sbi: Add hart_ prefix to PMP functions Nicholas Piggin
@ 2026-04-30 4:55 ` Nicholas Piggin
2026-06-10 12:35 ` [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Anup Patel
8 siblings, 0 replies; 10+ messages in thread
From: Nicholas Piggin @ 2026-04-30 4:55 UTC (permalink / raw)
To: opensbi; +Cc: Nicholas Piggin
The sbi_hart_pmp.c looks like a good place for the hart PMP CSR
access functions.
Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
include/sbi/riscv_asm.h | 14 -----
include/sbi/sbi_hart_pmp.h | 12 ++++
lib/sbi/riscv_asm.c | 109 ------------------------------------
lib/sbi/sbi_hart_pmp.c | 110 +++++++++++++++++++++++++++++++++++++
4 files changed, 122 insertions(+), 123 deletions(-)
diff --git a/include/sbi/riscv_asm.h b/include/sbi/riscv_asm.h
index f845320b..7765edb7 100644
--- a/include/sbi/riscv_asm.h
+++ b/include/sbi/riscv_asm.h
@@ -81,8 +81,6 @@
#ifndef __ASSEMBLER__
-#include <sbi/sbi_types.h>
-
#define csr_swap(csr, val) \
({ \
register unsigned long __v = (unsigned long)(val); \
@@ -211,18 +209,6 @@ int misa_xlen(void);
/* Get RISC-V ISA string representation */
void misa_string(int xlen, char *out, unsigned int out_sz);
-/* Disable pmp entry at a given index */
-int hart_pmp_disable(unsigned int n);
-
-/* Check if the matching field is set */
-bool hart_is_pmp_enabled(unsigned int n);
-
-int hart_pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
- unsigned long log2len);
-
-int hart_pmp_get(unsigned int n, unsigned long *prot_out, unsigned long *addr_out,
- unsigned long *log2len);
-
#endif /* !__ASSEMBLER__ */
#endif
diff --git a/include/sbi/sbi_hart_pmp.h b/include/sbi/sbi_hart_pmp.h
index a7765d17..3e0219ac 100644
--- a/include/sbi/sbi_hart_pmp.h
+++ b/include/sbi/sbi_hart_pmp.h
@@ -9,6 +9,18 @@
#include <sbi/sbi_types.h>
+/* Disable pmp entry at a given index */
+int hart_pmp_disable(unsigned int n);
+
+/* Check if the matching field is set */
+bool hart_is_pmp_enabled(unsigned int n);
+
+int hart_pmp_set(unsigned int n, unsigned long prot,
+ unsigned long addr, unsigned long log2len);
+
+int hart_pmp_get(unsigned int n, unsigned long *prot_out,
+ unsigned long *addr_out, unsigned long *log2len);
+
struct sbi_scratch;
unsigned int sbi_hart_pmp_count(struct sbi_scratch *scratch);
diff --git a/lib/sbi/riscv_asm.c b/lib/sbi/riscv_asm.c
index 98a93462..a1e0f2f8 100644
--- a/lib/sbi/riscv_asm.c
+++ b/lib/sbi/riscv_asm.c
@@ -272,112 +272,3 @@ void csr_write_num(int csr_num, unsigned long val)
#undef switchcase_csr_write_2
#undef switchcase_csr_write
}
-
-static int hart_pmp_read(pmp_t *pmp, unsigned int n)
-{
- 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;
-#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 SBI_OK;
-}
-
-static int hart_pmp_write(pmp_t *pmp, unsigned int n)
-{
- int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
- unsigned long cfgmask, pmpcfg;
-
- /* 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;
-
- /* 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 SBI_OK;
-}
-
-int hart_pmp_disable(unsigned int n)
-{
- pmp_t pmp;
- int rc;
-
- rc = hart_pmp_read(&pmp, n);
- if (rc)
- return rc;
-
- pmp.cfg = 0;
-
- return hart_pmp_write(&pmp, n);
-}
-
-bool hart_is_pmp_enabled(unsigned int n)
-{
- pmp_t pmp;
-
- if (hart_pmp_read(&pmp, n) != SBI_OK)
- return false;
-
- return pmp_is_enabled(&pmp);
-}
-
-int hart_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 hart_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);
-}
diff --git a/lib/sbi/sbi_hart_pmp.c b/lib/sbi/sbi_hart_pmp.c
index 8b7b091c..54cfd0cb 100644
--- a/lib/sbi/sbi_hart_pmp.c
+++ b/lib/sbi/sbi_hart_pmp.c
@@ -12,9 +12,119 @@
#include <sbi/sbi_hfence.h>
#include <sbi/sbi_math.h>
#include <sbi/sbi_platform.h>
+#include <sbi/sbi_pmp.h>
#include <sbi/sbi_tlb.h>
#include <sbi/riscv_asm.h>
+static int hart_pmp_read(pmp_t *pmp, unsigned int n)
+{
+ 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;
+#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 SBI_OK;
+}
+
+static int hart_pmp_write(pmp_t *pmp, unsigned int n)
+{
+ int pmpcfg_csr, pmpcfg_shift, pmpaddr_csr;
+ unsigned long cfgmask, pmpcfg;
+
+ /* 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;
+
+ /* 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 SBI_OK;
+}
+
+int hart_pmp_disable(unsigned int n)
+{
+ pmp_t pmp;
+ int rc;
+
+ rc = hart_pmp_read(&pmp, n);
+ if (rc)
+ return rc;
+
+ pmp.cfg = 0;
+
+ return hart_pmp_write(&pmp, n);
+}
+
+bool hart_is_pmp_enabled(unsigned int n)
+{
+ pmp_t pmp;
+
+ if (hart_pmp_read(&pmp, n) != SBI_OK)
+ return false;
+
+ return pmp_is_enabled(&pmp);
+}
+
+int hart_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 hart_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);
+}
+
/*
* Smepmp enforces access boundaries between M-mode and
* S/U-mode. When it is enabled, the PMPs are programmed
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH 0/7] Make PMP encoding usable for non-hart PMPs
2026-04-30 4:55 [PATCH 0/7] Make PMP encoding usable for non-hart PMPs Nicholas Piggin
` (7 preceding siblings ...)
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 ` Anup Patel
8 siblings, 0 replies; 10+ messages in thread
From: Anup Patel @ 2026-06-10 12:35 UTC (permalink / raw)
To: Nicholas Piggin; +Cc: opensbi
On Thu, Apr 30, 2026 at 10:25 AM Nicholas Piggin <npiggin@gmail.com> wrote:
>
> The Tenstorrent RISC-V IOMMU implements PMPs with MMRs in the same
> format as specified in the RISC-V ISA for the hart CSRs. This series
> splits out the PMP encoding functions so it can be shared by both
> users.
>
> I moved this series out of the Tenstorrent Atlantis platform series
> that included IOMMU support, tried to do better at splitting it up,
> tidied and improved code.
>
> The new user of the API can be seen here for reference (will not
> quite compile with this new series):
>
> https://lore.kernel.org/opensbi/20260310005000.3837512-4-npiggin@gmail.com/
>
> I will post that one out again after the base Atlantis platform and
> this series is merged.
>
> Thanks,
> Nick
>
> Nicholas Piggin (7):
> lib: sbi: Introduce pmp_t type
> lib: sbi: split PMP encoding and CSR access
> lib: sbi: Move RISC-V PMP encoding functions to sbi_pmp.c
> lib: sbi: Add PMP CSR read and write accessors
> lib: sbi: Add pmp_is_enabled() helper
> lib: sbi: Add hart_ prefix to PMP functions
> lib: sbi: Move hart PMP functions to sbi_hart_pmp.c
The only minor comment is to use "sbi_" prefix for various
public functions in sbi_pmp.h and sbi_hart_pmp.h otherwise
this looks good to me. I will take care of it at the time of
merging.
Reviewed-by: Anup Patel <anup@brainfault.org>
Applied this series to the riscv/opensbi repo.
Thanks,
Anup
>
> include/sbi/riscv_asm.h | 12 ---
> include/sbi/sbi_hart_pmp.h | 12 +++
> include/sbi/sbi_pmp.h | 23 +++++
> lib/sbi/objects.mk | 1 +
> lib/sbi/riscv_asm.c | 162 +------------------------------
> lib/sbi/sbi_hart_pmp.c | 128 ++++++++++++++++++++++--
> lib/sbi/sbi_pmp.c | 105 ++++++++++++++++++++
> platform/generic/eswin/eic770x.c | 36 +++----
> 8 files changed, 279 insertions(+), 200 deletions(-)
> create mode 100644 include/sbi/sbi_pmp.h
> create mode 100644 lib/sbi/sbi_pmp.c
>
> --
> 2.53.0
>
>
> --
> opensbi mailing list
> opensbi@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply [flat|nested] 10+ messages in thread