Skip to contentSkip to conversation

Add Andes PMA related SBI calls

PATCH New Ben Zong-You Xie 2 participants 9 messages Jul 23, 2024 – Aug 23, 2024

lore
Share & tools
lore
#0 lore raw

cover Add Andes PMA related SBI calls

Programmable physical memory attributes(PPMA) is a PMA-related feature for Andes. PPMA allows dynamic adjustment of memory attributes in the runtime. It contains a configurable amount of PMA entries implemented as CSRs to control the attributes of memory locations in interest. For example, device drivers can set memory regions as non-cacheable if necessary. For more information, refer to the section 10.3 in [1].

This patch series adds three Andes PPMA SBI calls. These SBI calls provides a way to dynamically set/reset memory attributes in the runtime. For more technical details, see the individual commit messages.

[1] https://www.andestech.com/wp-content/uploads/AX45MP-1C-Rev.-5.0.0-Datasheet.pdf

Ben Zong-You Xie (4):
platform: generic: Kconfig: add the description for Andes PMA feature
platform: generic: andes: add a new Andes SBI call to probe Andes PMA
feature
platform: generic: andes: add a new Andes SBI call to set up a PMA
entry
platform: generic: andes: add a new Andes SBI call to free a PMA entry

 platform/generic/Kconfig                   |   1 +
 platform/generic/andes/Kconfig             |   5 +
 platform/generic/andes/ae350.c             |   2 +
 platform/generic/andes/andes_pma.c         | 181 +++++++++++++++++++--
 platform/generic/andes/andes_sbi.c         |  18 +-
 platform/generic/include/andes/andes_pma.h |  43 ++++-
 6 files changed, 228 insertions(+), 22 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 platform: generic: Kconfig: add the description for Andes PMA feature

Describe Andes PPMA in the config option, and select it for AE350 platform.

---
 platform/generic/Kconfig       | 1 +
 platform/generic/andes/Kconfig | 5 +++++
 2 files changed, 6 insertions(+)
platform/generic/Kconfig +1 -0
index 6642a6e..688da3f 100644
@@ -33,6 +33,7 @@ config PLATFORM_ANDES_AE350
 	bool "Andes AE350 support"
 	select SYS_ATCSMU
 	select ANDES_PMU
+	select ANDES_PMA
 	default n
 
 config PLATFORM_RENESAS_RZFIVE
platform/generic/andes/Kconfig +5 -0
index 138710b..6edd67d 100644
@@ -3,6 +3,11 @@
 config ANDES_PMA
 	bool "Andes PMA support"
 	default n
+	help
+	  Programmable PMA(PPMA) is a feature for Andes. PPMA allows
+	  dynamic adjustment of memory attributes in the runtime.
+	  It contains a configurable amount of PMA entries implemented
+	  as CSRs to control the attributes of memory locations in interest.
 
 config ANDES_SBI
 	bool "Andes SBI support"
Signature · 1 line
2.34.1
#2 lore raw

PATCH 1/4 platform: generic: Kconfig: add the description for Andes PMA feature

Quoted 5 lines · On Tue, Jul 23, 2024 at 1:27?PM Ben Zong-You Xie <[email protected]> wrote:

Describe Andes PPMA in the config option, and select it for AE350 platform.

LGTM.

Thanks,
Anup

Quoted 41 lines
---
 platform/generic/Kconfig       | 1 +
 platform/generic/andes/Kconfig | 5 +++++
 2 files changed, 6 insertions(+)
platform/generic/Kconfig +1 -0
index 6642a6e..688da3f 100644
@@ -33,6 +33,7 @@ config PLATFORM_ANDES_AE350
        bool "Andes AE350 support"
        select SYS_ATCSMU
        select ANDES_PMU
+       select ANDES_PMA
        default n
 
 config PLATFORM_RENESAS_RZFIVE
platform/generic/andes/Kconfig +5 -1
index 138710b..6edd67d 100644
@@ -3,6 +3,11 @@
 config ANDES_PMA
        bool "Andes PMA support"
        default n
+       help
+         Programmable PMA(PPMA) is a feature for Andes. PPMA allows
+         dynamic adjustment of memory attributes in the runtime.
+         It contains a configurable amount of PMA entries implemented
+         as CSRs to control the attributes of memory locations in interest.
 
 config ANDES_SBI
        bool "Andes SBI support"
--

2.34.1

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

#3 lore raw

PATCH 2/4 platform: generic: andes: add a new Andes SBI call to probe Andes PMA feature

In reply to Ben Zong-You Xie

Add a new Andes SBI call to check whether PPMA is supported by hardware or not.

---
 platform/generic/andes/ae350.c             |  2 ++
 platform/generic/andes/andes_pma.c         |  8 ++++++--
 platform/generic/andes/andes_sbi.c         |  5 +++++
 platform/generic/include/andes/andes_pma.h | 12 ++++++++++++
 4 files changed, 25 insertions(+), 2 deletions(-)
platform/generic/andes/ae350.c +2 -0
index 63c10bc..a1ab6b9 100644
@@ -19,6 +19,7 @@
 #include <sbi/sbi_ipi.h>
 #include <sbi/sbi_init.h>
 #include <andes/andes.h>
+#include <andes/andes_sbi.h>
 
 static struct smu_data smu = { 0 };
 extern void __ae350_enable_coherency_warmboot(void);
@@ -120,4 +121,5 @@ const struct platform_override andes_ae350 = {
 	.final_init  = ae350_final_init,
 	.extensions_init = andes_pmu_extensions_init,
 	.pmu_init = andes_pmu_init,
+	.vendor_ext_provider = andes_sbi_vendor_ext_provider,
 };
platform/generic/andes/andes_pma.c +6 -2
index 9c37720..a884bf7 100644
@@ -241,7 +241,6 @@ static int andes_fdt_reserved_memory_fixup(void *fdt,
 int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
 			    unsigned int pma_regions_count)
 {
-	unsigned long mmsc = csr_read(CSR_MMSC_CFG);
 	unsigned int dt_populate_cnt;
 	unsigned int i, j;
 	unsigned long pa;
@@ -254,7 +253,7 @@ int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
 	if (pma_regions_count > ANDES_MAX_PMA_REGIONS)
 		return SBI_EINVAL;
 
-	if ((mmsc & MMSC_CFG_PPMA_MASK) == 0)
+	if (!andes_sbi_probe_pma())
 		return SBI_ENOTSUPP;
 
 	/* Configure the PMA regions */
@@ -290,3 +289,8 @@ int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
 
 	return 0;
 }
+
+bool andes_sbi_probe_pma(void)
+{
+	return (csr_read(CSR_MMSC_CFG) & MMSC_CFG_PPMA_MASK) ? true : false;
+}
platform/generic/andes/andes_sbi.c +5 -0
index 43bafdc..a7ca4a5 100644
@@ -5,12 +5,14 @@
  */
 #include <andes/andes.h>
 #include <andes/andes_sbi.h>
+#include <andes/andes_pma.h>
 #include <sbi/riscv_asm.h>
 #include <sbi/sbi_error.h>
 
 enum sbi_ext_andes_fid {
 	SBI_EXT_ANDES_FID0 = 0, /* Reserved for future use */
 	SBI_EXT_ANDES_IOCP_SW_WORKAROUND,
+	SBI_EXT_ANDES_PMA_PROBE,
 };
 
 static bool andes_cache_controllable(void)
@@ -41,6 +43,9 @@ int andes_sbi_vendor_ext_provider(long funcid,
 	case SBI_EXT_ANDES_IOCP_SW_WORKAROUND:
 		out->value = andes_apply_iocp_sw_workaround();
 		break;
+	case SBI_EXT_ANDES_PMA_PROBE:
+		out->value = andes_sbi_probe_pma();
+		break;
 
 	default:
 		return SBI_EINVAL;
platform/generic/include/andes/andes_pma.h +12 -0
index 5ea1247..147dca1 100644
@@ -47,4 +47,16 @@ struct andes_pma_region {
 int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
 			    unsigned int pma_regions_count);
 
+/**
+ * Programmable PMA(PPMA) is a feature for Andes. PPMA allows dynamic adjustment
+ * of memory attributes in the runtime. It contains a configurable amount of PMA
+ * entries implemented as CSRs to control the attributes of memory locations.
+ *
+ * Check if hardware supports PPMA
+ *
+ * @return true if PPMA is supported
+ * @return false if PPMA is not supported
+ */
+bool andes_sbi_probe_pma(void);
+
 #endif /* _ANDES_PMA_H_ */
Signature · 1 line
2.34.1
#4 lore raw

PATCH 2/4 platform: generic: andes: add a new Andes SBI call to probe Andes PMA feature

Quoted 5 lines · On Tue, Jul 23, 2024 at 1:27?PM Ben Zong-You Xie <[email protected]> wrote:

Add a new Andes SBI call to check whether PPMA is supported by hardware or not.

LGTM.

Thanks,
Anup

Quoted 113 lines
---
 platform/generic/andes/ae350.c             |  2 ++
 platform/generic/andes/andes_pma.c         |  8 ++++++--
 platform/generic/andes/andes_sbi.c         |  5 +++++
 platform/generic/include/andes/andes_pma.h | 12 ++++++++++++
 4 files changed, 25 insertions(+), 2 deletions(-)
platform/generic/andes/ae350.c +2 -0
index 63c10bc..a1ab6b9 100644
@@ -19,6 +19,7 @@
 #include <sbi/sbi_ipi.h>
 #include <sbi/sbi_init.h>
 #include <andes/andes.h>
+#include <andes/andes_sbi.h>
 
 static struct smu_data smu = { 0 };
 extern void __ae350_enable_coherency_warmboot(void);
@@ -120,4 +121,5 @@ const struct platform_override andes_ae350 = {
        .final_init  = ae350_final_init,
        .extensions_init = andes_pmu_extensions_init,
        .pmu_init = andes_pmu_init,
+       .vendor_ext_provider = andes_sbi_vendor_ext_provider,
 };
platform/generic/andes/andes_pma.c +6 -2
index 9c37720..a884bf7 100644
@@ -241,7 +241,6 @@ static int andes_fdt_reserved_memory_fixup(void *fdt,
 int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
                            unsigned int pma_regions_count)
 {
-       unsigned long mmsc = csr_read(CSR_MMSC_CFG);
        unsigned int dt_populate_cnt;
        unsigned int i, j;
        unsigned long pa;
@@ -254,7 +253,7 @@ int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
        if (pma_regions_count > ANDES_MAX_PMA_REGIONS)
                return SBI_EINVAL;
 
-       if ((mmsc & MMSC_CFG_PPMA_MASK) == 0)
+       if (!andes_sbi_probe_pma())
                return SBI_ENOTSUPP;
 
        /* Configure the PMA regions */
@@ -290,3 +289,8 @@ int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
 
        return 0;
 }
+
+bool andes_sbi_probe_pma(void)
+{
+       return (csr_read(CSR_MMSC_CFG) & MMSC_CFG_PPMA_MASK) ? true : false;
+}
platform/generic/andes/andes_sbi.c +5 -0
index 43bafdc..a7ca4a5 100644
@@ -5,12 +5,14 @@
  */
 #include <andes/andes.h>
 #include <andes/andes_sbi.h>
+#include <andes/andes_pma.h>
 #include <sbi/riscv_asm.h>
 #include <sbi/sbi_error.h>
 
 enum sbi_ext_andes_fid {
        SBI_EXT_ANDES_FID0 = 0, /* Reserved for future use */
        SBI_EXT_ANDES_IOCP_SW_WORKAROUND,
+       SBI_EXT_ANDES_PMA_PROBE,
 };
 
 static bool andes_cache_controllable(void)
@@ -41,6 +43,9 @@ int andes_sbi_vendor_ext_provider(long funcid,
        case SBI_EXT_ANDES_IOCP_SW_WORKAROUND:
                out->value = andes_apply_iocp_sw_workaround();
                break;
+       case SBI_EXT_ANDES_PMA_PROBE:
+               out->value = andes_sbi_probe_pma();
+               break;
 
        default:
                return SBI_EINVAL;
platform/generic/include/andes/andes_pma.h +12 -1
index 5ea1247..147dca1 100644
@@ -47,4 +47,16 @@ struct andes_pma_region {
 int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
                            unsigned int pma_regions_count);
 
+/**
+ * Programmable PMA(PPMA) is a feature for Andes. PPMA allows dynamic adjustment
+ * of memory attributes in the runtime. It contains a configurable amount of PMA
+ * entries implemented as CSRs to control the attributes of memory locations.
+ *
+ * Check if hardware supports PPMA
+ *
+ * @return true if PPMA is supported
+ * @return false if PPMA is not supported
+ */
+bool andes_sbi_probe_pma(void);
+
 #endif /* _ANDES_PMA_H_ */
--

2.34.1

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

#5 lore raw

PATCH 3/4 platform: generic: andes: add a new Andes SBI call to set up a PMA entry

In reply to Ben Zong-You Xie

Implement a new Andes SBI call, which is to set up a NAPOT region with given memory attributes.

---
 platform/generic/andes/andes_pma.c         | 110 +++++++++++++++++++++
 platform/generic/andes/andes_sbi.c         |   9 +-
 platform/generic/include/andes/andes_pma.h |  22 ++++-
 3 files changed, 137 insertions(+), 4 deletions(-)
platform/generic/andes/andes_pma.c +110 -0
index a884bf7..70039ca 100644
@@ -94,6 +94,74 @@ static inline bool not_napot(unsigned long addr, unsigned long size)
 	return ((size & (size - 1)) || (addr & (size - 1)));
 }
 
+static inline bool is_pma_entry_disable(char pmaxcfg)
+{
+	return (pmaxcfg & ANDES_PMACFG_ETYP_MASK) == ANDES_PMACFG_ETYP_OFF ?
+	       true : false;
+}
+
+static char get_pmaxcfg(int entry_id)
+{
+	unsigned int pmacfg_addr;
+	unsigned long pmacfg_val;
+	char *pmaxcfg;
+
+#if __riscv_xlen == 64
+	pmacfg_addr = CSR_PMACFG0 + ((entry_id / 8) ? 2 : 0);
+	pmacfg_val = andes_pma_read_num(pmacfg_addr);
+	pmaxcfg = (char *)&pmacfg_val + (entry_id % 8);
+#elif __riscv_xlen == 32
+	pmacfg_addr = CSR_PMACFG0 + (entry_id / 4);
+	pmacfg_val = andes_pma_read_num(pmacfg_addr);
+	pmaxcfg = (char *)&pmacfg_val + (entry_id % 4);
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+	return *pmaxcfg;
+}
+
+static void decode_pmaaddrx(int entry_id, unsigned long *start,
+			    unsigned long *size)
+{
+	unsigned long pmaaddr;
+	int k;
+
+	/**
+	 * Given $pmaaddr, let k = # of trailing 1s of $pmaaddr
+	 * size = 2 ^ (k + 3)
+	 * start = 4 * ($pmaaddr - (size / 8) + 1)
+	 */
+	pmaaddr = andes_pma_read_num(CSR_PMAADDR0 + entry_id);
+	k = sbi_ffz(pmaaddr);
+	*size = 1 << (k + 3);
+	*start = (pmaaddr - (1 << k) + 1) << 2;
+}
+
+static bool has_pma_region_overlap(unsigned long start, unsigned long size)
+{
+	unsigned long _start, _size, _end, end;
+	char pmaxcfg;
+
+	end = start + size - 1;
+	for (int i = 0; i < ANDES_MAX_PMA_REGIONS; i++) {
+		pmaxcfg = get_pmaxcfg(i);
+		if (is_pma_entry_disable(pmaxcfg))
+			continue;
+
+		decode_pmaaddrx(i, &_start, &_size);
+		_end = _start + _size - 1;
+
+		if (MAX(start, _start) <= MIN(end, _end)) {
+			sbi_printf(
+				"ERROR %s(): %#lx ~ %#lx overlaps with PMA%d: %#lx ~ %#lx\n",
+				__func__, start, end, i, _start, _end);
+			return true;
+		}
+	}
+
+	return false;
+}
+
 static unsigned long andes_pma_setup(const struct andes_pma_region *pma_region,
 				     unsigned int entry_id)
 {
@@ -294,3 +362,45 @@ bool andes_sbi_probe_pma(void)
 {
 	return (csr_read(CSR_MMSC_CFG) & MMSC_CFG_PPMA_MASK) ? true : false;
 }
+
+int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags)
+{
+	unsigned int entry_id;
+	unsigned long rc;
+	char pmaxcfg;
+	struct andes_pma_region region;
+
+	if (!andes_sbi_probe_pma()) {
+		sbi_printf("ERROR %s(): Platform does not support PPMA.\n",
+			   __func__);
+		return SBI_ERR_NOT_SUPPORTED;
+	}
+
+	if (has_pma_region_overlap(pa, size))
+		return SBI_ERR_INVALID_PARAM;
+
+	for (entry_id = 0; entry_id < ANDES_MAX_PMA_REGIONS; entry_id++) {
+		pmaxcfg = get_pmaxcfg(entry_id);
+		if (is_pma_entry_disable(pmaxcfg)) {
+			region.pa = pa;
+			region.size = size;
+			region.flags = flags;
+			break;
+		}
+	}
+
+	if (entry_id == ANDES_MAX_PMA_REGIONS) {
+		sbi_printf("ERROR %s(): All PMA entries have run out\n",
+			   __func__);
+		return SBI_ERR_FAILED;
+	}
+
+	rc = andes_pma_setup(&region, entry_id);
+	if (rc == SBI_EINVAL) {
+		sbi_printf("ERROR %s(): Failed to set PMAADDR%d\n",
+			   __func__, entry_id);
+		return SBI_ERR_FAILED;
+	}
+
+	return SBI_SUCCESS;
+}
platform/generic/andes/andes_sbi.c +7 -2
index a7ca4a5..0e4a43d 100644
@@ -13,6 +13,7 @@ enum sbi_ext_andes_fid {
 	SBI_EXT_ANDES_FID0 = 0, /* Reserved for future use */
 	SBI_EXT_ANDES_IOCP_SW_WORKAROUND,
 	SBI_EXT_ANDES_PMA_PROBE,
+	SBI_EXT_ANDES_PMA_SET,
 };
 
 static bool andes_cache_controllable(void)
@@ -39,6 +40,7 @@ int andes_sbi_vendor_ext_provider(long funcid,
 				  struct sbi_ecall_return *out,
 				  const struct fdt_match *match)
 {
+	int ret = 0;
 	switch (funcid) {
 	case SBI_EXT_ANDES_IOCP_SW_WORKAROUND:
 		out->value = andes_apply_iocp_sw_workaround();
@@ -46,10 +48,13 @@ int andes_sbi_vendor_ext_provider(long funcid,
 	case SBI_EXT_ANDES_PMA_PROBE:
 		out->value = andes_sbi_probe_pma();
 		break;
+	case SBI_EXT_ANDES_PMA_SET:
+		ret = andes_sbi_set_pma(regs->a0, regs->a1, regs->a2);
+		break;
 
 	default:
-		return SBI_EINVAL;
+		ret = SBI_ENOTSUPP;
 	}
 
-	return 0;
+	return ret;
 }
platform/generic/include/andes/andes_pma.h +20 -2
index 147dca1..487d9bf 100644
@@ -12,11 +12,15 @@
 
 #define ANDES_PMA_GRANULARITY			(1 << 12)
 
+#define ANDES_PMACFG_ETYP_OFFSET		0
+#define ANDES_PMACFG_ETYP_MASK			(3 << ANDES_PMACFG_ETYP_OFFSET)
+#define ANDES_PMACFG_ETYP_OFF			(0 << ANDES_PMACFG_ETYP_OFFSET)
 /* Naturally aligned power of 2 region */
-#define ANDES_PMACFG_ETYP_NAPOT			3
+#define ANDES_PMACFG_ETYP_NAPOT			(3 << ANDES_PMACFG_ETYP_OFFSET)
 
+#define ANDES_PMACFG_MTYP_OFFSET		2
 /* Memory, Non-cacheable, Bufferable */
-#define ANDES_PMACFG_MTYP_MEM_NON_CACHE_BUF	(3 << 2)
+#define ANDES_PMACFG_MTYP_MEM_NON_CACHE_BUF	(3 << ANDES_PMACFG_MTYP_OFFSET)
 
 /**
  * struct andes_pma_region - Describes PMA regions
@@ -59,4 +63,18 @@ int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
  */
 bool andes_sbi_probe_pma(void);
 
+/**
+ * Set a NAPOT region with given memory attributes
+ * @param pa: Start address of the NAPOT region
+ * @param size: Size of the NAPOT region
+ * @param flags: Memory attributes set to the NAPOT region
+ *
+ * @return SBI_SUCCESS on success
+ * @return SBI_ERR_NOT_SUPPORTED if hardware does not support PPMA features
+ * @return SBI_ERR_INVALID_PARAM if the given region is overlapped with the
+ *	   region that has been set already
+ * @return SBI_ERR_FAILED if available entries have run out or setup fails
+ */
+int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags);
+
 #endif /* _ANDES_PMA_H_ */
Signature · 1 line
2.34.1
#6 lore raw

PATCH 3/4 platform: generic: andes: add a new Andes SBI call to set up a PMA entry

Quoted 5 lines · On Tue, Jul 23, 2024 at 1:27?PM Ben Zong-You Xie <[email protected]> wrote:

Implement a new Andes SBI call, which is to set up a NAPOT region with given memory attributes.

LGTM.

Thanks,
Anup

Quoted 216 lines
---
 platform/generic/andes/andes_pma.c         | 110 +++++++++++++++++++++
 platform/generic/andes/andes_sbi.c         |   9 +-
 platform/generic/include/andes/andes_pma.h |  22 ++++-
 3 files changed, 137 insertions(+), 4 deletions(-)
platform/generic/andes/andes_pma.c +110 -0
index a884bf7..70039ca 100644
@@ -94,6 +94,74 @@ static inline bool not_napot(unsigned long addr, unsigned long size)
        return ((size & (size - 1)) || (addr & (size - 1)));
 }
 
+static inline bool is_pma_entry_disable(char pmaxcfg)
+{
+       return (pmaxcfg & ANDES_PMACFG_ETYP_MASK) == ANDES_PMACFG_ETYP_OFF ?
+              true : false;
+}
+
+static char get_pmaxcfg(int entry_id)
+{
+       unsigned int pmacfg_addr;
+       unsigned long pmacfg_val;
+       char *pmaxcfg;
+
+#if __riscv_xlen == 64
+       pmacfg_addr = CSR_PMACFG0 + ((entry_id / 8) ? 2 : 0);
+       pmacfg_val = andes_pma_read_num(pmacfg_addr);
+       pmaxcfg = (char *)&pmacfg_val + (entry_id % 8);
+#elif __riscv_xlen == 32
+       pmacfg_addr = CSR_PMACFG0 + (entry_id / 4);
+       pmacfg_val = andes_pma_read_num(pmacfg_addr);
+       pmaxcfg = (char *)&pmacfg_val + (entry_id % 4);
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+       return *pmaxcfg;
+}
+
+static void decode_pmaaddrx(int entry_id, unsigned long *start,
+                           unsigned long *size)
+{
+       unsigned long pmaaddr;
+       int k;
+
+       /**
+        * Given $pmaaddr, let k = # of trailing 1s of $pmaaddr
+        * size = 2 ^ (k + 3)
+        * start = 4 * ($pmaaddr - (size / 8) + 1)
+        */
+       pmaaddr = andes_pma_read_num(CSR_PMAADDR0 + entry_id);
+       k = sbi_ffz(pmaaddr);
+       *size = 1 << (k + 3);
+       *start = (pmaaddr - (1 << k) + 1) << 2;
+}
+
+static bool has_pma_region_overlap(unsigned long start, unsigned long size)
+{
+       unsigned long _start, _size, _end, end;
+       char pmaxcfg;
+
+       end = start + size - 1;
+       for (int i = 0; i < ANDES_MAX_PMA_REGIONS; i++) {
+               pmaxcfg = get_pmaxcfg(i);
+               if (is_pma_entry_disable(pmaxcfg))
+                       continue;
+
+               decode_pmaaddrx(i, &_start, &_size);
+               _end = _start + _size - 1;
+
+               if (MAX(start, _start) <= MIN(end, _end)) {
+                       sbi_printf(
+                               "ERROR %s(): %#lx ~ %#lx overlaps with PMA%d: %#lx ~ %#lx\n",
+                               __func__, start, end, i, _start, _end);
+                       return true;
+               }
+       }
+
+       return false;
+}
+
 static unsigned long andes_pma_setup(const struct andes_pma_region *pma_region,
                                     unsigned int entry_id)
 {
@@ -294,3 +362,45 @@ bool andes_sbi_probe_pma(void)
 {
        return (csr_read(CSR_MMSC_CFG) & MMSC_CFG_PPMA_MASK) ? true : false;
 }
+
+int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags)
+{
+       unsigned int entry_id;
+       unsigned long rc;
+       char pmaxcfg;
+       struct andes_pma_region region;
+
+       if (!andes_sbi_probe_pma()) {
+               sbi_printf("ERROR %s(): Platform does not support PPMA.\n",
+                          __func__);
+               return SBI_ERR_NOT_SUPPORTED;
+       }
+
+       if (has_pma_region_overlap(pa, size))
+               return SBI_ERR_INVALID_PARAM;
+
+       for (entry_id = 0; entry_id < ANDES_MAX_PMA_REGIONS; entry_id++) {
+               pmaxcfg = get_pmaxcfg(entry_id);
+               if (is_pma_entry_disable(pmaxcfg)) {
+                       region.pa = pa;
+                       region.size = size;
+                       region.flags = flags;
+                       break;
+               }
+       }
+
+       if (entry_id == ANDES_MAX_PMA_REGIONS) {
+               sbi_printf("ERROR %s(): All PMA entries have run out\n",
+                          __func__);
+               return SBI_ERR_FAILED;
+       }
+
+       rc = andes_pma_setup(&region, entry_id);
+       if (rc == SBI_EINVAL) {
+               sbi_printf("ERROR %s(): Failed to set PMAADDR%d\n",
+                          __func__, entry_id);
+               return SBI_ERR_FAILED;
+       }
+
+       return SBI_SUCCESS;
+}
platform/generic/andes/andes_sbi.c +7 -2
index a7ca4a5..0e4a43d 100644
@@ -13,6 +13,7 @@ enum sbi_ext_andes_fid {
        SBI_EXT_ANDES_FID0 = 0, /* Reserved for future use */
        SBI_EXT_ANDES_IOCP_SW_WORKAROUND,
        SBI_EXT_ANDES_PMA_PROBE,
+       SBI_EXT_ANDES_PMA_SET,
 };
 
 static bool andes_cache_controllable(void)
@@ -39,6 +40,7 @@ int andes_sbi_vendor_ext_provider(long funcid,
                                  struct sbi_ecall_return *out,
                                  const struct fdt_match *match)
 {
+       int ret = 0;
        switch (funcid) {
        case SBI_EXT_ANDES_IOCP_SW_WORKAROUND:
                out->value = andes_apply_iocp_sw_workaround();
@@ -46,10 +48,13 @@ int andes_sbi_vendor_ext_provider(long funcid,
        case SBI_EXT_ANDES_PMA_PROBE:
                out->value = andes_sbi_probe_pma();
                break;
+       case SBI_EXT_ANDES_PMA_SET:
+               ret = andes_sbi_set_pma(regs->a0, regs->a1, regs->a2);
+               break;
 
        default:
-               return SBI_EINVAL;
+               ret = SBI_ENOTSUPP;
        }
 
-       return 0;
+       return ret;
 }
platform/generic/include/andes/andes_pma.h +20 -3
index 147dca1..487d9bf 100644
@@ -12,11 +12,15 @@
 
 #define ANDES_PMA_GRANULARITY                  (1 << 12)
 
+#define ANDES_PMACFG_ETYP_OFFSET               0
+#define ANDES_PMACFG_ETYP_MASK                 (3 << ANDES_PMACFG_ETYP_OFFSET)
+#define ANDES_PMACFG_ETYP_OFF                  (0 << ANDES_PMACFG_ETYP_OFFSET)
 /* Naturally aligned power of 2 region */
-#define ANDES_PMACFG_ETYP_NAPOT                        3
+#define ANDES_PMACFG_ETYP_NAPOT                        (3 << ANDES_PMACFG_ETYP_OFFSET)
 
+#define ANDES_PMACFG_MTYP_OFFSET               2
 /* Memory, Non-cacheable, Bufferable */
-#define ANDES_PMACFG_MTYP_MEM_NON_CACHE_BUF    (3 << 2)
+#define ANDES_PMACFG_MTYP_MEM_NON_CACHE_BUF    (3 << ANDES_PMACFG_MTYP_OFFSET)
 
 /**
  * struct andes_pma_region - Describes PMA regions
@@ -59,4 +63,18 @@ int andes_pma_setup_regions(const struct andes_pma_region *pma_regions,
  */
 bool andes_sbi_probe_pma(void);
 
+/**
+ * Set a NAPOT region with given memory attributes
+ * @param pa: Start address of the NAPOT region
+ * @param size: Size of the NAPOT region
+ * @param flags: Memory attributes set to the NAPOT region
+ *
+ * @return SBI_SUCCESS on success
+ * @return SBI_ERR_NOT_SUPPORTED if hardware does not support PPMA features
+ * @return SBI_ERR_INVALID_PARAM if the given region is overlapped with the
+ *        region that has been set already
+ * @return SBI_ERR_FAILED if available entries have run out or setup fails
+ */
+int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags);
+
 #endif /* _ANDES_PMA_H_ */
--

2.34.1

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

#7 lore raw

PATCH 4/4 platform: generic: andes: add a new Andes SBI call to free a PMA entry

In reply to Ben Zong-You Xie

Add a new Andes SBI call to free a PMA entry, and reset the memory attributes for the corresponding NAPOT region.

---
 platform/generic/andes/andes_pma.c         | 65 ++++++++++++++++------
 platform/generic/andes/andes_sbi.c         |  4 ++
 platform/generic/include/andes/andes_pma.h |  9 +++
 3 files changed, 61 insertions(+), 17 deletions(-)
platform/generic/andes/andes_pma.c +48 -17
index 70039ca..be61005 100644
@@ -120,6 +120,27 @@ static char get_pmaxcfg(int entry_id)
 	return *pmaxcfg;
 }
 
+static void set_pmaxcfg(int entry_id, char flags)
+{
+	unsigned int pmacfg_addr;
+	unsigned long pmacfg_val;
+	char *pmaxcfg;
+
+#if __riscv_xlen == 64
+	pmacfg_addr = CSR_PMACFG0 + ((entry_id / 8) ? 2 : 0);
+	pmacfg_val = andes_pma_read_num(pmacfg_addr);
+	pmaxcfg = (char *)&pmacfg_val + (entry_id % 8);
+#elif __riscv_xlen == 32
+	pmacfg_addr = CSR_PMACFG0 + (entry_id / 4);
+	pmacfg_val = andes_pma_read_num(pmacfg_addr);
+	pmaxcfg = (char *)&pmacfg_val + (entry_id % 4);
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+	*pmaxcfg = flags;
+	andes_pma_write_num(pmacfg_addr, pmacfg_val);
+}
+
 static void decode_pmaaddrx(int entry_id, unsigned long *start,
 			    unsigned long *size)
 {
@@ -167,30 +188,14 @@ static unsigned long andes_pma_setup(const struct andes_pma_region *pma_region,
 {
 	unsigned long size = pma_region->size;
 	unsigned long addr = pma_region->pa;
-	unsigned int pma_cfg_addr;
-	unsigned long pmacfg_val;
 	unsigned long pmaaddr;
-	char *pmaxcfg;
 
 	/* Check for a 4KiB granularity NAPOT region*/
 	if (size < ANDES_PMA_GRANULARITY || not_napot(addr, size) ||
 	    !(pma_region->flags & ANDES_PMACFG_ETYP_NAPOT))
 		return SBI_EINVAL;
 
-#if __riscv_xlen == 64
-	pma_cfg_addr = CSR_PMACFG0 + ((entry_id / 8) ? 2 : 0);
-	pmacfg_val = andes_pma_read_num(pma_cfg_addr);
-	pmaxcfg = (char *)&pmacfg_val + (entry_id % 8);
-#elif __riscv_xlen == 32
-	pma_cfg_addr = CSR_PMACFG0 + (entry_id / 4);
-	pmacfg_val = andes_pma_read_num(pma_cfg_addr);
-	pmaxcfg = (char *)&pmacfg_val + (entry_id % 4);
-#else
-#error "Unexpected __riscv_xlen"
-#endif
-	*pmaxcfg = pma_region->flags;
-
-	andes_pma_write_num(pma_cfg_addr, pmacfg_val);
+	set_pmaxcfg(entry_id, pma_region->flags);
 
 	pmaaddr = (addr >> 2) + (size >> 3) - 1;
 
@@ -404,3 +409,29 @@ int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags)
 
 	return SBI_SUCCESS;
 }
+
+int andes_sbi_free_pma(unsigned long pa)
+{
+	unsigned long start, size;
+	char pmaxcfg;
+
+	for (int i = 0; i < ANDES_MAX_PMA_REGIONS; i++) {
+		pmaxcfg = get_pmaxcfg(i);
+		if (is_pma_entry_disable(pmaxcfg))
+			continue;
+
+		decode_pmaaddrx(i, &start, &size);
+		if (start != pa)
+			continue;
+
+		set_pmaxcfg(i, ANDES_PMACFG_ETYP_OFF);
+		andes_pma_write_num(CSR_PMAADDR0 + i, 0);
+
+		return SBI_SUCCESS;
+	}
+
+	sbi_printf("ERROR %s(): Failed to find the entry with PA %#lx\n",
+		   __func__, pa);
+
+	return SBI_ERR_FAILED;
+}
platform/generic/andes/andes_sbi.c +4 -0
index 0e4a43d..0e1e59e 100644
@@ -14,6 +14,7 @@ enum sbi_ext_andes_fid {
 	SBI_EXT_ANDES_IOCP_SW_WORKAROUND,
 	SBI_EXT_ANDES_PMA_PROBE,
 	SBI_EXT_ANDES_PMA_SET,
+	SBI_EXT_ANDES_PMA_FREE,
 };
 
 static bool andes_cache_controllable(void)
@@ -51,6 +52,9 @@ int andes_sbi_vendor_ext_provider(long funcid,
 	case SBI_EXT_ANDES_PMA_SET:
 		ret = andes_sbi_set_pma(regs->a0, regs->a1, regs->a2);
 		break;
+	case SBI_EXT_ANDES_PMA_FREE:
+		ret = andes_sbi_free_pma(regs->a0);
+		break;
 
 	default:
 		ret = SBI_ENOTSUPP;
platform/generic/include/andes/andes_pma.h +9 -0
index 487d9bf..e2d5279 100644
@@ -77,4 +77,13 @@ bool andes_sbi_probe_pma(void);
  */
 int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags);
 
+/**
+ * Reset the memory attribute of a NAPOT region
+ * @param pa Start address of the NAPOT region
+ *
+ * @return SBI_SUCCESS on success
+ * @return SBI_ERR_FAILED if the given region is not set before
+ */
+int andes_sbi_free_pma(unsigned long pa);
+
 #endif /* _ANDES_PMA_H_ */
Signature · 1 line
2.34.1
#8 lore raw

PATCH 4/4 platform: generic: andes: add a new Andes SBI call to free a PMA entry

Quoted 76 lines · On Tue, Jul 23, 2024 at 1:27?PM Ben Zong-You Xie <[email protected]> wrote:

Add a new Andes SBI call to free a PMA entry, and reset the memory attributes for the corresponding NAPOT region.

---
 platform/generic/andes/andes_pma.c         | 65 ++++++++++++++++------
 platform/generic/andes/andes_sbi.c         |  4 ++
 platform/generic/include/andes/andes_pma.h |  9 +++
 3 files changed, 61 insertions(+), 17 deletions(-)
platform/generic/andes/andes_pma.c +22 -17
index 70039ca..be61005 100644
@@ -120,6 +120,27 @@ static char get_pmaxcfg(int entry_id)
        return *pmaxcfg;
 }
 
+static void set_pmaxcfg(int entry_id, char flags)
+{
+       unsigned int pmacfg_addr;
+       unsigned long pmacfg_val;
+       char *pmaxcfg;
+
+#if __riscv_xlen == 64
+       pmacfg_addr = CSR_PMACFG0 + ((entry_id / 8) ? 2 : 0);
+       pmacfg_val = andes_pma_read_num(pmacfg_addr);
+       pmaxcfg = (char *)&pmacfg_val + (entry_id % 8);
+#elif __riscv_xlen == 32
+       pmacfg_addr = CSR_PMACFG0 + (entry_id / 4);
+       pmacfg_val = andes_pma_read_num(pmacfg_addr);
+       pmaxcfg = (char *)&pmacfg_val + (entry_id % 4);
+#else
+#error "Unexpected __riscv_xlen"
+#endif
+       *pmaxcfg = flags;
+       andes_pma_write_num(pmacfg_addr, pmacfg_val);
+}
+
 static void decode_pmaaddrx(int entry_id, unsigned long *start,
                            unsigned long *size)
 {
@@ -167,30 +188,14 @@ static unsigned long andes_pma_setup(const struct andes_pma_region *pma_region,
 {
        unsigned long size = pma_region->size;
        unsigned long addr = pma_region->pa;
-       unsigned int pma_cfg_addr;
-       unsigned long pmacfg_val;
        unsigned long pmaaddr;
-       char *pmaxcfg;
 
        /* Check for a 4KiB granularity NAPOT region*/
        if (size < ANDES_PMA_GRANULARITY || not_napot(addr, size) ||
            !(pma_region->flags & ANDES_PMACFG_ETYP_NAPOT))
                return SBI_EINVAL;
 
-#if __riscv_xlen == 64
-       pma_cfg_addr = CSR_PMACFG0 + ((entry_id / 8) ? 2 : 0);
-       pmacfg_val = andes_pma_read_num(pma_cfg_addr);
-       pmaxcfg = (char *)&pmacfg_val + (entry_id % 8);
-#elif __riscv_xlen == 32
-       pma_cfg_addr = CSR_PMACFG0 + (entry_id / 4);
-       pmacfg_val = andes_pma_read_num(pma_cfg_addr);
-       pmaxcfg = (char *)&pmacfg_val + (entry_id % 4);
-#else
-#error "Unexpected __riscv_xlen"
-#endif
-       *pmaxcfg = pma_region->flags;
-
-       andes_pma_write_num(pma_cfg_addr, pmacfg_val);
+       set_pmaxcfg(entry_id, pma_region->flags);
 
        pmaaddr = (addr >> 2) + (size >> 3) - 1;
 

platform/generic/andes/andes_pma.c int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags) · line 417

Quoted 9 lines
platform/generic/andes/andes_pma.c +6 -0
@@ -404,3 +409,29 @@ int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags)
 
        return SBI_SUCCESS;
 }
+
+int andes_sbi_free_pma(unsigned long pa)
+{
+       unsigned long start, size;
+       char pmaxcfg;
+

Need a check on andes_sbi_probe_pma() here. I will take care of it at the time of merging this patch.

Quoted 64 lines
platform/generic/andes/andes_pma.c +20 -0
+       for (int i = 0; i < ANDES_MAX_PMA_REGIONS; i++) {
+               pmaxcfg = get_pmaxcfg(i);
+               if (is_pma_entry_disable(pmaxcfg))
+                       continue;
+
+               decode_pmaaddrx(i, &start, &size);
+               if (start != pa)
+                       continue;
+
+               set_pmaxcfg(i, ANDES_PMACFG_ETYP_OFF);
+               andes_pma_write_num(CSR_PMAADDR0 + i, 0);
+
+               return SBI_SUCCESS;
+       }
+
+       sbi_printf("ERROR %s(): Failed to find the entry with PA %#lx\n",
+                  __func__, pa);
+
+       return SBI_ERR_FAILED;
+}
platform/generic/andes/andes_sbi.c +4 -0
index 0e4a43d..0e1e59e 100644
@@ -14,6 +14,7 @@ enum sbi_ext_andes_fid {
        SBI_EXT_ANDES_IOCP_SW_WORKAROUND,
        SBI_EXT_ANDES_PMA_PROBE,
        SBI_EXT_ANDES_PMA_SET,
+       SBI_EXT_ANDES_PMA_FREE,
 };
 
 static bool andes_cache_controllable(void)
@@ -51,6 +52,9 @@ int andes_sbi_vendor_ext_provider(long funcid,
        case SBI_EXT_ANDES_PMA_SET:
                ret = andes_sbi_set_pma(regs->a0, regs->a1, regs->a2);
                break;
+       case SBI_EXT_ANDES_PMA_FREE:
+               ret = andes_sbi_free_pma(regs->a0);
+               break;
 
        default:
                ret = SBI_ENOTSUPP;
platform/generic/include/andes/andes_pma.h +9 -1
index 487d9bf..e2d5279 100644
@@ -77,4 +77,13 @@ bool andes_sbi_probe_pma(void);
  */
 int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags);
 
+/**
+ * Reset the memory attribute of a NAPOT region
+ * @param pa Start address of the NAPOT region
+ *
+ * @return SBI_SUCCESS on success
+ * @return SBI_ERR_FAILED if the given region is not set before
+ */
+int andes_sbi_free_pma(unsigned long pa);
+
 #endif /* _ANDES_PMA_H_ */
--

2.34.1

Quoted 4 lines

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

Otherwise, looks good to me.

Thanks,
Anup