18 #ifndef __CORE_FEATURE_PMP_H__
19 #define __CORE_FEATURE_PMP_H__
35 #include "core_feature_base.h"
36 #include "core_compatiable.h"
38 #if defined(__PMP_PRESENT) && (__PMP_PRESENT == 1)
53 #ifndef __PMP_ENTRY_NUM
55 #error "__PMP_ENTRY_NUM is not defined, please check!"
132 uint8_t csr_cfg_num = 0;
133 uint16_t csr_idx = 0;
134 uint16_t cfg_shift = 0;
136 if (entry_idx >= __PMP_ENTRY_NUM)
return 0;
138 #if __RISCV_XLEN == 32
140 csr_idx = entry_idx >> 2;
141 #elif __RISCV_XLEN == 64
144 csr_idx = (entry_idx >> 2) & ~1;
154 cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
157 return (uint8_t)(__RV_EXTRACT_FIELD(pmpcfgx, 0xFF << cfg_shift));
172 uint8_t csr_cfg_num = 0;
173 uint16_t csr_idx = 0;
174 uint16_t cfg_shift = 0;
175 if (entry_idx >= __PMP_ENTRY_NUM)
return;
177 #if __RISCV_XLEN == 32
179 csr_idx = entry_idx >> 2;
180 #elif __RISCV_XLEN == 64
183 csr_idx = (entry_idx >> 2) & ~1;
194 cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
196 pmpcfgx = __RV_INSERT_FIELD(pmpcfgx, 0xFFUL << cfg_shift, pmpxcfg);
271 unsigned int cfg_shift, cfg_csr_idx, addr_csr_idx = 0;
272 unsigned long cfgmask, addrmask = 0;
273 unsigned long pmpcfg, pmpaddr = 0;
274 unsigned long protection, csr_cfg_num = 0;
276 if (entry_idx >= __PMP_ENTRY_NUM || pmp_cfg->order >
__RISCV_XLEN || pmp_cfg->order <
PMP_SHIFT)
return;
279 #if __RISCV_XLEN == 32
281 cfg_csr_idx = (entry_idx >> 2);
282 #elif __RISCV_XLEN == 64
284 cfg_csr_idx = ((entry_idx >> 2)) & ~1;
293 cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
294 addr_csr_idx = entry_idx;
297 protection = (
unsigned long)pmp_cfg->protection;
299 cfgmask = ~(0xFFUL << cfg_shift);
301 pmpcfg |= ((protection << cfg_shift) & ~cfgmask);
305 pmpaddr = (pmp_cfg->base_addr >>
PMP_SHIFT);
307 addrmask = (1UL << (pmp_cfg->order -
PMP_SHIFT)) - 1;
308 pmpaddr = ((pmp_cfg->base_addr >>
PMP_SHIFT) & ~addrmask);
309 pmpaddr |= (addrmask >> 1);
332 unsigned int cfg_shift, cfg_csr_idx, addr_csr_idx = 0;
333 unsigned long cfgmask, pmpcfg, prot = 0;
334 unsigned long t1, addr, pmpaddr, len = 0;
335 uint8_t csr_cfg_num = 0;
337 if (entry_idx >= __PMP_ENTRY_NUM || !pmp_cfg)
return -1;
340 #if __RISCV_XLEN == 32
342 cfg_csr_idx = entry_idx >> 2;
343 #elif __RISCV_XLEN == 64
345 cfg_csr_idx = (entry_idx>> 2) & ~1;
351 cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
352 addr_csr_idx = entry_idx;
355 cfgmask = (0xFFUL << cfg_shift);
357 prot = pmpcfg >> cfg_shift;
362 t1 =
__CTZ(~pmpaddr);
363 addr = (pmpaddr & ~((1UL << t1) - 1)) <<
PMP_SHIFT;
371 pmp_cfg->protection = prot;
372 pmp_cfg->base_addr = addr;
373 pmp_cfg->order = len;