From 7429405ab674348850b485fee3e36612ed4d4706 Mon Sep 17 00:00:00 2001 From: Firas Sammoura Date: Mon, 6 Oct 2025 23:46:46 +0000 Subject: [PATCH] arch/riscv: Refactor PMP configuration reading Factor out the logic for reading PMP configuration registers (pmpcfgX) from dump_pmp_regs into a new static inline helper function, z_riscv_pmp_read_config. This new function encapsulates the architecture-specific (RV32/RV64) and slot-count-specific reads of the pmpcfg CSRs, improving code organization and potential reusability. Signed-off-by: Firas Sammoura --- arch/riscv/core/pmp.c | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/arch/riscv/core/pmp.c b/arch/riscv/core/pmp.c index ca5e5a49d5a74..751227737e16f 100644 --- a/arch/riscv/core/pmp.c +++ b/arch/riscv/core/pmp.c @@ -104,26 +104,29 @@ static void print_pmp_entries(unsigned int pmp_start, unsigned int pmp_end, } } -static void dump_pmp_regs(const char *banner) +/** + * @brief Reads the PMP configuration CSRs (pmpcfgX) based on architecture and slot count. + * + * This helper function abstracts the logic required to read the correct CSRs + * (pmpcfg0, pmpcfg1, pmpcfg2, pmpcfg3) depending on whether the system is + * 32-bit or 64-bit and the total number of PMP slots configured. + * + * @param pmp_cfg Pointer to the array where the CSR contents will be stored. + * @param pmp_cfg_size The size of the pmp_cfg array, measured in unsigned long entries. + */ +static inline void z_riscv_pmp_read_config(unsigned long *pmp_cfg, size_t pmp_cfg_size) { - unsigned long pmp_addr[CONFIG_PMP_SLOTS]; - unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE]; - -#define PMPADDR_READ(x) pmp_addr[x] = csr_read(pmpaddr##x) - - FOR_EACH(PMPADDR_READ, (;), 0, 1, 2, 3, 4, 5, 6, 7); -#if CONFIG_PMP_SLOTS > 8 - FOR_EACH(PMPADDR_READ, (;), 8, 9, 10, 11, 12, 13, 14, 15); -#endif - -#undef PMPADDR_READ + __ASSERT(pmp_cfg_size == (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE), + "Invalid PMP config array size"); #ifdef CONFIG_64BIT + /* RV64: pmpcfg0 holds entries 0-7; pmpcfg2 holds entries 8-15. */ pmp_cfg[0] = csr_read(pmpcfg0); #if CONFIG_PMP_SLOTS > 8 pmp_cfg[1] = csr_read(pmpcfg2); #endif #else + /* RV32: Each pmpcfg register holds 4 entries. */ pmp_cfg[0] = csr_read(pmpcfg0); pmp_cfg[1] = csr_read(pmpcfg1); #if CONFIG_PMP_SLOTS > 8 @@ -131,7 +134,23 @@ static void dump_pmp_regs(const char *banner) pmp_cfg[3] = csr_read(pmpcfg3); #endif #endif +} + +static void dump_pmp_regs(const char *banner) +{ + unsigned long pmp_addr[CONFIG_PMP_SLOTS]; + unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE]; + +#define PMPADDR_READ(x) pmp_addr[x] = csr_read(pmpaddr##x) + + FOR_EACH(PMPADDR_READ, (;), 0, 1, 2, 3, 4, 5, 6, 7); +#if CONFIG_PMP_SLOTS > 8 + FOR_EACH(PMPADDR_READ, (;), 8, 9, 10, 11, 12, 13, 14, 15); +#endif + +#undef PMPADDR_READ + z_riscv_pmp_read_config(pmp_cfg, (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE)); print_pmp_entries(0, CONFIG_PMP_SLOTS, pmp_addr, pmp_cfg, banner); }