Skip to content

Commit 5df5f88

Browse files
committed
riscv: pmp: Add helper to write PMP configuration CSRs
Introduce `z_riscv_pmp_write_config` to abstract writing to pmpcfg registers. This function handles the differing register layouts and slot counts between RV32 and RV64 architectures, writing to the appropriate pmpcfg0, pmpcfg1, pmpcfg2, or pmpcfg3 CSRs as needed based on CONFIG_PMP_SLOTS. Signed-off-by: Firas Sammoura <[email protected]>
1 parent e83d8d9 commit 5df5f88

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

arch/riscv/core/pmp.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,40 @@ static inline void z_riscv_pmp_read_config(unsigned long *pmp_cfg, size_t pmp_cf
136136
#endif
137137
}
138138

139+
/**
140+
* @brief Writes the PMP configuration CSRs (pmpcfgX) based on architecture and slot count.
141+
*
142+
* This helper function abstracts the logic required to write the correct Control and Status
143+
* Registers (CSRs)—pmpcfg0, pmpcfg1, etc.—by accounting for whether the system is 32-bit or 64-bit
144+
* and the total number of PMP slots configured. It handles the different register packing schemes
145+
* between RV32 and RV64.
146+
*
147+
* @param pmp_cfg Pointer to the array containing the PMP configuration values to be written to the
148+
* CSRs.
149+
* @param pmp_cfg_size The size of the pmp_cfg array, measured in unsigned long entries.
150+
*/
151+
static inline void z_riscv_pmp_write_config(unsigned long *pmp_cfg, size_t pmp_cfg_size)
152+
{
153+
__ASSERT(pmp_cfg_size == (size_t)(CONFIG_PMP_SLOTS / PMPCFG_STRIDE),
154+
"Invalid PMP config array size");
155+
156+
#ifdef CONFIG_64BIT
157+
/* RV64: pmpcfg0 holds entries 0-7; pmpcfg2 holds entries 8-15. */
158+
csr_write(pmpcfg0, pmp_cfg[0]);
159+
#if CONFIG_PMP_SLOTS > 8
160+
csr_write(pmpcfg2, pmp_cfg[1]);
161+
#endif
162+
#else
163+
/* RV32: Each pmpcfg register holds 4 entries. */
164+
csr_write(pmpcfg0, pmp_cfg[0]);
165+
csr_write(pmpcfg1, pmp_cfg[1]);
166+
#if CONFIG_PMP_SLOTS > 8
167+
csr_write(pmpcfg2, pmp_cfg[2]);
168+
csr_write(pmpcfg3, pmp_cfg[3]);
169+
#endif
170+
#endif
171+
}
172+
139173
static void dump_pmp_regs(const char *banner)
140174
{
141175
unsigned long pmp_addr[CONFIG_PMP_SLOTS];

0 commit comments

Comments
 (0)