Skip to content

Commit 416a805

Browse files
committed
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 <[email protected]>
1 parent ca85e76 commit 416a805

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

arch/riscv/core/pmp.c

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,53 @@ static void print_pmp_entries(unsigned int pmp_start, unsigned int pmp_end,
104104
}
105105
}
106106

107-
static void dump_pmp_regs(const char *banner)
107+
/**
108+
* @brief Reads the PMP configuration CSRs (pmpcfgX) based on architecture and slot count.
109+
*
110+
* This helper function abstracts the logic required to read the correct CSRs
111+
* (pmpcfg0, pmpcfg1, pmpcfg2, pmpcfg3) depending on whether the system is
112+
* 32-bit or 64-bit and the total number of PMP slots configured.
113+
*
114+
* @param pmp_cfg Pointer to the array where the CSR contents will be stored.
115+
* @param pmp_cfg_size The size of the pmp_cfg array, measured in unsigned long entries.
116+
*/
117+
static inline void z_riscv_pmp_read_config(unsigned long *pmp_cfg, size_t pmp_cfg_size)
108118
{
109-
unsigned long pmp_addr[CONFIG_PMP_SLOTS];
110-
unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE];
111-
112-
#define PMPADDR_READ(x) pmp_addr[x] = csr_read(pmpaddr##x)
113-
114-
FOR_EACH(PMPADDR_READ, (;), 0, 1, 2, 3, 4, 5, 6, 7);
115-
#if CONFIG_PMP_SLOTS > 8
116-
FOR_EACH(PMPADDR_READ, (;), 8, 9, 10, 11, 12, 13, 14, 15);
117-
#endif
118-
119-
#undef PMPADDR_READ
119+
__ASSERT(num_slots == (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE),
120+
"Invalid PMP config array size");
120121

121122
#ifdef CONFIG_64BIT
123+
/* RV64: pmpcfg0 holds entries 0-7; pmpcfg2 holds entries 8-15. */
122124
pmp_cfg[0] = csr_read(pmpcfg0);
123125
#if CONFIG_PMP_SLOTS > 8
124126
pmp_cfg[1] = csr_read(pmpcfg2);
125127
#endif
126128
#else
129+
/* RV32: Each pmpcfg register holds 4 entries. */
127130
pmp_cfg[0] = csr_read(pmpcfg0);
128131
pmp_cfg[1] = csr_read(pmpcfg1);
129132
#if CONFIG_PMP_SLOTS > 8
130133
pmp_cfg[2] = csr_read(pmpcfg2);
131134
pmp_cfg[3] = csr_read(pmpcfg3);
132135
#endif
133136
#endif
137+
}
138+
139+
static void dump_pmp_regs(const char *banner)
140+
{
141+
unsigned long pmp_addr[CONFIG_PMP_SLOTS];
142+
unsigned long pmp_cfg[CONFIG_PMP_SLOTS / PMPCFG_STRIDE];
143+
144+
#define PMPADDR_READ(x) pmp_addr[x] = csr_read(pmpaddr##x)
145+
146+
FOR_EACH(PMPADDR_READ, (;), 0, 1, 2, 3, 4, 5, 6, 7);
147+
#if CONFIG_PMP_SLOTS > 8
148+
FOR_EACH(PMPADDR_READ, (;), 8, 9, 10, 11, 12, 13, 14, 15);
149+
#endif
150+
151+
#undef PMPADDR_READ
134152

153+
z_riscv_pmp_read_config(pmp_cfg, (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE));
135154
print_pmp_entries(0, CONFIG_PMP_SLOTS, pmp_addr, pmp_cfg, banner);
136155
}
137156

0 commit comments

Comments
 (0)