[RFC PATCH 3/5] platform: generic/andes: add a PMA device for PPMA
Randolph Lin
randolph at andestech.com
Fri Nov 13 01:32:10 PST 2026
Add andes_pma_device, so that Andes platforms with programmable PMA
(PPMA) can register it with the PMA framework.
A table indexed by MTYP gives the attributes that select each memory
type of pmaxcfg. I/O selects a device type, non-cacheable memory a
non-cacheable memory type, cacheable memory a write-through or
write-back type, and the unsafe flag an empty hole. Bits 19:16 choose
bufferable or not, write-through or write-back, and the allocation.
MTYP does not encode the I/O ordering or the coherence, so the device
ignores them. When bits 19:16 are zero, I/O is MTYP 0 and cacheable
memory MTYP 11. On the 27, 45 and 46-series, these are also the types
of the device and memory regions that no PMA entry covers. The device
rejects a region whose attributes select no MTYP, and the entry is
always NAPOT.
The device sets the regions with andes_pma_setup_regions(), like the
RZ/Five and MT2824 tables, so they take the first PMA entries. The
PMA_FREE SBI call refuses to free them, because the platform needs them
for the whole boot. If a hart has no PPMA, or its PMA entries cannot
hold the regions, the device says so.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Randolph Lin <randolph at andestech.com>
---
platform/generic/andes/Kconfig | 1 +
platform/generic/andes/andes_pma.c | 111 +++++++++++++++++++++
platform/generic/include/andes/andes_pma.h | 11 ++
3 files changed, 123 insertions(+)
diff --git a/platform/generic/andes/Kconfig b/platform/generic/andes/Kconfig
index 6edd67de..dd368fd8 100644
--- a/platform/generic/andes/Kconfig
+++ b/platform/generic/andes/Kconfig
@@ -2,6 +2,7 @@
config ANDES_PMA
bool "Andes PMA support"
+ select PMA
default n
help
Programmable PMA(PPMA) is a feature for Andes. PPMA allows
diff --git a/platform/generic/andes/andes_pma.c b/platform/generic/andes/andes_pma.c
index 28d33a2f..ae8a0cad 100644
--- a/platform/generic/andes/andes_pma.c
+++ b/platform/generic/andes/andes_pma.c
@@ -289,6 +289,111 @@ int andes_pma_setup_regions(void *fdt,
return 0;
}
+/* The PMA framework puts its regions in the first PMA entries. */
+static unsigned int andes_pma_fixed_regions;
+
+/* The attributes that select each MTYP. MTYP 12 to 14 are reserved. */
+static const u32 andes_pma_mtyps[16] = {
+ [0] = PMA_IO,
+ [1] = PMA_IO | PMA_BUFFERABLE,
+ [2] = PMA_NONCACHEABLE_MEMORY,
+ [3] = PMA_NONCACHEABLE_MEMORY | PMA_BUFFERABLE,
+ [4] = PMA_NONCOHERENT_MEMORY | PMA_WRITE_THROUGH | PMA_NO_ALLOC,
+ [5] = PMA_NONCOHERENT_MEMORY | PMA_WRITE_THROUGH | PMA_NO_WRITE_ALLOC,
+ [6] = PMA_NONCOHERENT_MEMORY | PMA_WRITE_THROUGH | PMA_NO_READ_ALLOC,
+ [7] = PMA_NONCOHERENT_MEMORY | PMA_WRITE_THROUGH,
+ [8] = PMA_NONCOHERENT_MEMORY | PMA_NO_ALLOC,
+ [9] = PMA_NONCOHERENT_MEMORY | PMA_NO_WRITE_ALLOC,
+ [10] = PMA_NONCOHERENT_MEMORY | PMA_NO_READ_ALLOC,
+ [11] = PMA_NONCOHERENT_MEMORY,
+ [15] = PMA_UNSAFE,
+};
+
+static int andes_pma_attr_to_mtyp(u32 attr, u8 *mtyp)
+{
+ unsigned int i;
+
+ /* MTYP does not encode the I/O ordering or the coherence. */
+ if ((attr & PMA_ORDER_MASK) == PMA_ORDER_IO_STRONG)
+ attr &= ~PMA_ORDER_MASK;
+ if (attr & PMA_CACHEABLE)
+ attr &= ~PMA_COHERENT;
+
+ for (i = 0; i < array_size(andes_pma_mtyps); i++) {
+ if (i >= 12 && i < 15)
+ continue;
+
+ if (andes_pma_mtyps[i] == attr) {
+ *mtyp = i << ANDES_PMACFG_MTYP_OFFSET;
+ return 0;
+ }
+ }
+
+ return SBI_EINVAL;
+}
+
+static int andes_pma_region_check(struct pma_device *dev,
+ const struct pma_region *region)
+{
+ u8 mtyp;
+
+ if (region->size < ANDES_PMA_GRANULARITY)
+ return SBI_EINVAL;
+
+ return andes_pma_attr_to_mtyp(region->attr, &mtyp);
+}
+
+static int andes_pma_regions_set(struct pma_device *dev,
+ const struct pma_region *regions,
+ unsigned int count)
+{
+ struct andes_pma_region pma[ANDES_MAX_PMA_REGIONS] = { 0 };
+ unsigned int i;
+ u8 mtyp;
+ int rc;
+
+ if (count > ANDES_MAX_PMA_REGIONS)
+ return SBI_EINVAL;
+
+ if (!andes_sbi_probe_pma()) {
+ sbi_printf("%s: hart%u has no PPMA\n", __func__,
+ current_hartid());
+ return SBI_ENOTSUPP;
+ }
+
+ for (i = 0; i < count; i++) {
+ rc = andes_pma_attr_to_mtyp(regions[i].attr, &mtyp);
+ if (rc)
+ return rc;
+
+ pma[i].pa = regions[i].base;
+ pma[i].size = regions[i].size;
+ pma[i].flags = ANDES_PMACFG_ETYP_NAPOT | mtyp;
+ }
+
+ /* No region asks for a reserved-memory node, so pass no DT. */
+ rc = andes_pma_setup_regions(NULL, pma, count);
+ if (rc == SBI_EINVAL)
+ sbi_printf("%s: hart%u has too few PMA entries or address bits for the regions\n",
+ __func__, current_hartid());
+ if (rc)
+ return rc;
+
+ andes_pma_fixed_regions = count;
+
+ return 0;
+}
+
+static const struct pma_ops andes_pma_ops = {
+ .region_check = andes_pma_region_check,
+ .regions_set = andes_pma_regions_set,
+};
+
+struct pma_device andes_pma_device = {
+ .max_regions = ANDES_MAX_PMA_REGIONS,
+ .ops = &andes_pma_ops,
+};
+
bool andes_sbi_probe_pma(void)
{
return (csr_read(CSR_MMSC_CFG) & MMSC_CFG_PPMA_MASK) ? true : false;
@@ -356,6 +461,12 @@ int andes_sbi_free_pma(unsigned long pa)
if (start != pa)
continue;
+ if (i < andes_pma_fixed_regions) {
+ sbi_printf("ERROR %s(): PMA%d is set at boot\n",
+ __func__, i);
+ return SBI_ERR_DENIED;
+ }
+
set_pmaxcfg(i, ANDES_PMACFG_ETYP_OFF);
csr_write_num(CSR_PMAADDR0 + i, 0);
diff --git a/platform/generic/include/andes/andes_pma.h b/platform/generic/include/andes/andes_pma.h
index b0dbeb22..07480057 100644
--- a/platform/generic/include/andes/andes_pma.h
+++ b/platform/generic/include/andes/andes_pma.h
@@ -7,6 +7,7 @@
#define _ANDES_PMA_H_
#include <sbi/sbi_types.h>
+#include <sbi_utils/pma/pma.h>
#define ANDES_MAX_PMA_REGIONS 16
@@ -53,6 +54,15 @@ int andes_pma_setup_regions(void *fdt,
const struct andes_pma_region *pma_regions,
unsigned int pma_regions_count);
+/**
+ * The PMA device of the PMA framework for PPMA
+ *
+ * It sets the MTYP that the attributes of a region select, and it rejects a
+ * region whose attributes select no MTYP. It puts the regions in the first
+ * PMA entries, and andes_sbi_free_pma() refuses to free them.
+ */
+extern struct pma_device andes_pma_device;
+
/**
* 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
@@ -85,6 +95,7 @@ int andes_sbi_set_pma(unsigned long pa, unsigned long size, u8 flags);
*
* @return SBI_SUCCESS on success
* @return SBI_ERR_FAILED if the given region is not set before
+ * @return SBI_ERR_DENIED if the PMA framework set the given region at boot
*/
int andes_sbi_free_pma(unsigned long pa);
--
2.34.1
More information about the opensbi
mailing list