lib: No need to provide default PMP region using platform callbacks
authorAnup Patel <anup.patel@wdc.com>
Thu, 23 Apr 2020 06:22:30 +0000 (11:52 +0530)
committerAnup Patel <anup@brainfault.org>
Mon, 27 Apr 2020 08:27:27 +0000 (13:57 +0530)
The default (usually last) PMP region provides S-mode access to
complete memory range not covered by other PMP regions.

Currently, the default PMP region is described as platform specific
PMP region which is not appropriate because all platforms need it
and default PMP region should be part of generic library.

Signed-off-by: Anup Patel <anup.patel@wdc.com>
Reviewed-by: Atish Patra <atish.patra@wdc.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
include/sbi/sbi_platform.h
lib/sbi/sbi_hart.c
platform/andes/ae350/platform.c
platform/qemu/virt/platform.c
platform/sifive/fu540/platform.c
platform/spike/platform.c
platform/template/platform.c

index 847e174..fc2913d 100644 (file)
@@ -492,8 +492,9 @@ static inline int sbi_platform_pmp_region_info(const struct sbi_platform *plat,
                                                ulong *log2size)
 {
        if (plat && sbi_platform_ops(plat)->pmp_region_info)
-               return sbi_platform_ops(plat)->pmp_region_info(hartid, index, prot, addr,
-                                                                              log2size);
+               return sbi_platform_ops(plat)->pmp_region_info(hartid, index,
+                                                              prot, addr,
+                                                              log2size);
        return 0;
 }
 
index b5df7c4..24c3637 100644 (file)
@@ -188,7 +188,7 @@ int sbi_hart_pmp_check_addr(struct sbi_scratch *scratch, unsigned long addr,
 
 static int pmp_init(struct sbi_scratch *scratch, u32 hartid)
 {
-       u32 i, count;
+       u32 i, pmp_idx = 0, count;
        unsigned long fw_start, fw_size_log2;
        ulong prot, addr, log2size;
        const struct sbi_platform *plat = sbi_platform_ptr(scratch);
@@ -196,22 +196,28 @@ static int pmp_init(struct sbi_scratch *scratch, u32 hartid)
        if (!sbi_platform_has_pmp(plat))
                return 0;
 
+       /* Firmware PMP region to protect OpenSBI firmware */
        fw_size_log2 = log2roundup(scratch->fw_size);
-       fw_start     = scratch->fw_start & ~((1UL << fw_size_log2) - 1UL);
-
-       pmp_set(0, 0, fw_start, fw_size_log2);
+       fw_start = scratch->fw_start & ~((1UL << fw_size_log2) - 1UL);
+       pmp_set(pmp_idx++, 0, fw_start, fw_size_log2);
 
+       /* Platform specific PMP regions */
        count = sbi_platform_pmp_region_count(plat, hartid);
-       if ((PMP_COUNT - 1) < count)
-               count = (PMP_COUNT - 1);
-
-       for (i = 0; i < count; i++) {
+       for (i = 0; i < count && pmp_idx < (PMP_COUNT - 1); i++) {
                if (sbi_platform_pmp_region_info(plat, hartid, i, &prot, &addr,
                                                 &log2size))
                        continue;
-               pmp_set(i + 1, prot, addr, log2size);
+               pmp_set(pmp_idx++, prot, addr, log2size);
        }
 
+       /*
+        * Default PMP region for allowing S-mode and U-mode access to
+        * memory not covered by:
+        * 1) Firmware PMP region
+        * 2) Platform specific PMP regions
+        */
+       pmp_set(pmp_idx++, PMP_R | PMP_W | PMP_X, 0, __riscv_xlen);
+
        return 0;
 }
 
index c1311d4..69a3cbc 100644 (file)
@@ -53,35 +53,6 @@ static int ae350_final_init(bool cold_boot)
        return 0;
 }
 
-/* Get number of PMP regions for given HART. */
-static u32 ae350_pmp_region_count(u32 hartid)
-{
-       return 1;
-}
-
-/*
- * Get PMP regions details (namely: protection, base address, and size) for
- * a given HART.
- */
-static int ae350_pmp_region_info(u32 hartid, u32 index, ulong *prot,
-                                ulong *addr, ulong *log2size)
-{
-       int ret = 0;
-
-       switch (index) {
-       case 0:
-               *prot     = PMP_R | PMP_W | PMP_X;
-               *addr     = 0;
-               *log2size = __riscv_xlen;
-               break;
-       default:
-               ret = -1;
-               break;
-       };
-
-       return ret;
-}
-
 /* Initialize the platform console. */
 static int ae350_console_init(void)
 {
@@ -159,9 +130,6 @@ static int ae350_system_shutdown(u32 type)
 const struct sbi_platform_operations platform_ops = {
        .final_init = ae350_final_init,
 
-       .pmp_region_count = ae350_pmp_region_count,
-       .pmp_region_info  = ae350_pmp_region_info,
-
        .console_init = ae350_console_init,
        .console_putc = uart8250_putc,
        .console_getc = uart8250_getc,
index 86d1bcc..5c12f53 100644 (file)
@@ -52,30 +52,6 @@ static int virt_final_init(bool cold_boot)
        return 0;
 }
 
-static u32 virt_pmp_region_count(u32 hartid)
-{
-       return 1;
-}
-
-static int virt_pmp_region_info(u32 hartid, u32 index, ulong *prot, ulong *addr,
-                               ulong *log2size)
-{
-       int ret = 0;
-
-       switch (index) {
-       case 0:
-               *prot     = PMP_R | PMP_W | PMP_X;
-               *addr     = 0;
-               *log2size = __riscv_xlen;
-               break;
-       default:
-               ret = -1;
-               break;
-       };
-
-       return ret;
-}
-
 static int virt_console_init(void)
 {
        return uart8250_init(VIRT_UART16550_ADDR, VIRT_UART_SHIFTREG_ADDR,
@@ -135,8 +111,6 @@ static int virt_system_down(u32 type)
 }
 
 const struct sbi_platform_operations platform_ops = {
-       .pmp_region_count       = virt_pmp_region_count,
-       .pmp_region_info        = virt_pmp_region_info,
        .final_init             = virt_final_init,
        .console_putc           = uart8250_putc,
        .console_getc           = uart8250_getc,
index baa0d13..3a9f4b5 100644 (file)
@@ -66,30 +66,6 @@ static int fu540_final_init(bool cold_boot)
        return 0;
 }
 
-static u32 fu540_pmp_region_count(u32 hartid)
-{
-       return 1;
-}
-
-static int fu540_pmp_region_info(u32 hartid, u32 index, ulong *prot,
-                                ulong *addr, ulong *log2size)
-{
-       int ret = 0;
-
-       switch (index) {
-       case 0:
-               *prot     = PMP_R | PMP_W | PMP_X;
-               *addr     = 0;
-               *log2size = __riscv_xlen;
-               break;
-       default:
-               ret = -1;
-               break;
-       };
-
-       return ret;
-}
-
 static int fu540_console_init(void)
 {
        unsigned long peri_in_freq;
@@ -169,8 +145,6 @@ static int fu540_system_down(u32 type)
 }
 
 const struct sbi_platform_operations platform_ops = {
-       .pmp_region_count       = fu540_pmp_region_count,
-       .pmp_region_info        = fu540_pmp_region_info,
        .final_init             = fu540_final_init,
        .console_putc           = sifive_uart_putc,
        .console_getc           = sifive_uart_getc,
index 0883ff6..b09e7c6 100644 (file)
@@ -21,30 +21,6 @@ static int spike_final_init(bool cold_boot)
        return 0;
 }
 
-static u32 spike_pmp_region_count(u32 hartid)
-{
-       return 1;
-}
-
-static int spike_pmp_region_info(u32 hartid, u32 index, ulong *prot, ulong *addr,
-                               ulong *log2size)
-{
-       int ret = 0;
-
-       switch (index) {
-       case 0:
-               *prot     = PMP_R | PMP_W | PMP_X;
-               *addr     = 0;
-               *log2size = __riscv_xlen;
-               break;
-       default:
-               ret = -1;
-               break;
-       };
-
-       return ret;
-}
-
 static int spike_console_init(void)
 {
        return 0;
@@ -84,8 +60,6 @@ static int spike_timer_init(bool cold_boot)
 }
 
 const struct sbi_platform_operations platform_ops = {
-       .pmp_region_count       = spike_pmp_region_count,
-       .pmp_region_info        = spike_pmp_region_info,
        .final_init             = spike_final_init,
        .console_putc           = htif_putc,
        .console_getc           = htif_getc,
index d09b8bd..ef32941 100644 (file)
@@ -34,24 +34,6 @@ static int platform_final_init(bool cold_boot)
 }
 
 /*
- * Get number of PMP regions for given HART.
- */
-static u32 platform_pmp_region_count(u32 hartid)
-{
-       return 0;
-}
-
-/*
- * Get PMP regions details (namely: protection, base address, and size) for
- * a given HART.
- */
-static int platform_pmp_region_info(u32 hartid, u32 index, ulong *prot,
-                                   ulong *addr, ulong *log2size)
-{
-       return 0;
-}
-
-/*
  * Initialize the platform console.
  */
 static int platform_console_init(void)
@@ -201,8 +183,6 @@ static int platform_system_shutdown(u32 type)
 const struct sbi_platform_operations platform_ops = {
        .early_init             = platform_early_init,
        .final_init             = platform_final_init,
-       .pmp_region_count       = platform_pmp_region_count,
-       .pmp_region_info        = platform_pmp_region_info,
        .console_putc           = platform_console_putc,
        .console_getc           = platform_console_getc,
        .console_init           = platform_console_init,