Skip to contentSkip to conversation

Support TOR mode in pmp_set

PATCH New Pope B.Lei 2 participants 3 messages Sep 26, 2024 – Nov 6, 2024 RISC-V

lore
Share & tools
lore

Pope B.Lei

author
#0 lore raw

PATCH 1/1 Support TOR mode in pmp_set

---
 include/sbi/riscv_encoding.h |  2 ++
 lib/sbi/riscv_asm.c          | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)
include/sbi/riscv_encoding.h +2 -0
index 980abdb..387c011 100644
@@ -154,6 +154,8 @@
 #define PMP_A_NAPOT			_UL(0x18)
 #define PMP_L				_UL(0x80)
 
+#define PMP_USE_TOR			_UL(0x00)
+
 #define PMP_SHIFT			2
 #define PMP_COUNT			64
 #if __riscv_xlen == 64
lib/sbi/riscv_asm.c +16 -3
index c7d75ac..4518b2c 100644
@@ -309,7 +309,7 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
 	unsigned long addrmask, pmpaddr;
 
 	/* check parameters */
-	if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT)
+	if (n >= PMP_COUNT || log2len > __riscv_xlen || (log2len < PMP_SHIFT && log2len != PMP_USE_TOR ))
 		return SBI_EINVAL;
 
 	/* calculate PMP register and offset */
@@ -326,13 +326,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;
+
+	switch(log2len){
+		case PMP_USE_TOR:{
+			prot |= PMP_A_TOR;
+			break;
+		}
+		case PMP_SHIFT:{
+			prot |= PMP_A_NA4;
+			break;
+		}
+		default:
+			prot |= PMP_A_NAPOT;
+	}
+
 	cfgmask = ~(0xffUL << pmpcfg_shift);
 	pmpcfg	= (csr_read_num(pmpcfg_csr) & cfgmask);
 	pmpcfg |= ((prot << pmpcfg_shift) & ~cfgmask);
 
 	/* encode PMP address */
-	if (log2len == PMP_SHIFT) {
+	if (log2len == PMP_SHIFT || log2len == PMP_USE_TOR) {
 		pmpaddr = (addr >> PMP_SHIFT);
 	} else {
 		if (log2len == __riscv_xlen) {
Signature · 1 line
2.35.2.windows.1
author
#1 lore raw

PATCH 1/1 Support TOR mode in pmp_set

Dear all:
I added support for the TOR mode in the pmp_set function. When
the log2len value is 0, we will configure the PMP according to the TOR mode.
Best Regards!

Quoted 65 lines · On Thu, Sep 26, 2024 at 9:17?PM Pope B.Lei <[email protected]> wrote:
---
 include/sbi/riscv_encoding.h |  2 ++
 lib/sbi/riscv_asm.c          | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)
include/sbi/riscv_encoding.h +2 -0
index 980abdb..387c011 100644
@@ -154,6 +154,8 @@
 #define PMP_A_NAPOT                    _UL(0x18)
 #define PMP_L                          _UL(0x80)
 
+#define PMP_USE_TOR                    _UL(0x00)
+
 #define PMP_SHIFT                      2
 #define PMP_COUNT                      64
 #if __riscv_xlen == 64
lib/sbi/riscv_asm.c +16 -4
index c7d75ac..4518b2c 100644
@@ -309,7 +309,7 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
        unsigned long addrmask, pmpaddr;
 
        /* check parameters */
-       if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT)
+       if (n >= PMP_COUNT || log2len > __riscv_xlen || (log2len < PMP_SHIFT && log2len != PMP_USE_TOR ))
                return SBI_EINVAL;
 
        /* calculate PMP register and offset */
@@ -326,13 +326,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;
+
+       switch(log2len){
+               case PMP_USE_TOR:{
+                       prot |= PMP_A_TOR;
+                       break;
+               }
+               case PMP_SHIFT:{
+                       prot |= PMP_A_NA4;
+                       break;
+               }
+               default:
+                       prot |= PMP_A_NAPOT;
+       }
+
        cfgmask = ~(0xffUL << pmpcfg_shift);
        pmpcfg  = (csr_read_num(pmpcfg_csr) & cfgmask);
        pmpcfg |= ((prot << pmpcfg_shift) & ~cfgmask);
 
        /* encode PMP address */
-       if (log2len == PMP_SHIFT) {
+       if (log2len == PMP_SHIFT || log2len == PMP_USE_TOR) {
                pmpaddr = (addr >> PMP_SHIFT);
        } else {
                if (log2len == __riscv_xlen) {
--

2.35.2.windows.1

#2 lore raw

PATCH 1/1 Support TOR mode in pmp_set

In reply to Pope B.Lei

On Thu, Sep 26, 2024 at 6:47?PM Pope B.Lei <[email protected]> wrote:

TOR mode requires two PMP entries to mark the start and end of a region.

Please refer to my response on "[PATCH 0/3] Add support for tor type pmp" thread.

Quoted 49 lines
---
 include/sbi/riscv_encoding.h |  2 ++
 lib/sbi/riscv_asm.c          | 19 ++++++++++++++++---
 2 files changed, 18 insertions(+), 3 deletions(-)
include/sbi/riscv_encoding.h +2 -0
index 980abdb..387c011 100644
@@ -154,6 +154,8 @@
 #define PMP_A_NAPOT                    _UL(0x18)
 #define PMP_L                          _UL(0x80)
 
+#define PMP_USE_TOR                    _UL(0x00)
+
 #define PMP_SHIFT                      2
 #define PMP_COUNT                      64
 #if __riscv_xlen == 64
lib/sbi/riscv_asm.c +12 -2
index c7d75ac..4518b2c 100644
@@ -309,7 +309,7 @@ int pmp_set(unsigned int n, unsigned long prot, unsigned long addr,
        unsigned long addrmask, pmpaddr;
 
        /* check parameters */
-       if (n >= PMP_COUNT || log2len > __riscv_xlen || log2len < PMP_SHIFT)
+       if (n >= PMP_COUNT || log2len > __riscv_xlen || (log2len < PMP_SHIFT && log2len != PMP_USE_TOR ))
                return SBI_EINVAL;
 
        /* calculate PMP register and offset */
@@ -326,13 +326,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;
+
+       switch(log2len){
+               case PMP_USE_TOR:{
+                       prot |= PMP_A_TOR;
+                       break;
+               }
+               case PMP_SHIFT:{
+                       prot |= PMP_A_NA4;
+                       break;
+               }
+               default:

lib/sbi/riscv_asm.c int pmp_set(unsigned int n, unsigned long prot, unsigned long addr, · line 348

Quoted 10 lines
lib/sbi/riscv_asm.c +4 -1
+                       prot |= PMP_A_NAPOT;
+       }
+
        cfgmask = ~(0xffUL << pmpcfg_shift);
        pmpcfg  = (csr_read_num(pmpcfg_csr) & cfgmask);
        pmpcfg |= ((prot << pmpcfg_shift) & ~cfgmask);
 
        /* encode PMP address */
-       if (log2len == PMP_SHIFT) {
+       if (log2len == PMP_SHIFT || log2len == PMP_USE_TOR) {

This is only programming one PMP entry but for TOR we need two PMP entries per-region.

Also, what about pmp_get() ?

In general, let's avoid TOR PMP entries as much as possible.

Regards,
Anup

Quoted 11 lines
lib/sbi/riscv_asm.c +0 -1
                pmpaddr = (addr >> PMP_SHIFT);
        } else {
                if (log2len == __riscv_xlen) {
--

2.35.2.windows.1

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