core_feature_pmp.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright (c) 2019 Nuclei Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef __CORE_FEATURE_PMP_H__
  19. #define __CORE_FEATURE_PMP_H__
  20. /*!
  21. * @file core_feature_pmp.h
  22. * @brief PMP feature API header file for Nuclei N/NX Core
  23. */
  24. /*
  25. * PMP Feature Configuration Macro:
  26. * 1. __PMP_PRESENT: Define whether Physical Memory Protection(PMP) is present or not
  27. * * 0: Not present
  28. * * 1: Present
  29. * 2. __PMP_ENTRY_NUM: Define the number of PMP entries, only 8 or 16 is configurable.
  30. */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. #include "core_feature_base.h"
  35. #include "core_compatiable.h"
  36. #if defined(__PMP_PRESENT) && (__PMP_PRESENT == 1)
  37. /* ===== PMP Operations ===== */
  38. /**
  39. * \defgroup NMSIS_Core_PMP_Functions PMP Functions
  40. * \ingroup NMSIS_Core
  41. * \brief Functions that related to the RISCV Phyiscal Memory Protection.
  42. * \details
  43. * Optional physical memory protection (PMP) unit provides per-hart machine-mode
  44. * control registers to allow physical memory access privileges (read, write, execute)
  45. * to be specified for each physical memory region.
  46. *
  47. * The PMP can supports region access control settings as small as four bytes.
  48. *
  49. * @{
  50. */
  51. #ifndef __PMP_ENTRY_NUM
  52. /* numbers of PMP entries(__PMP_ENTRY_NUM) should be defined in <Device.h> */
  53. #error "__PMP_ENTRY_NUM is not defined, please check!"
  54. #endif
  55. typedef struct PMP_CONFIG {
  56. /**
  57. * set locking bit, addressing mode, read, write, and instruction execution permissions,
  58. * see \ref PMP_L, \ref PMP_R, \ref PMP_W, \ref PMP_X, .etc in <riscv_encoding.h>
  59. */
  60. unsigned int protection;
  61. /**
  62. * Size of memory region as power of 2, it has to be minimum 2 and maxium \ref __RISCV_XLEN according to the
  63. * hard-wired granularity 2^N bytes, if N = 12, then order has to be at least 12; if not, the order read out
  64. * is N though you configure less than N.
  65. */
  66. unsigned long order;
  67. /**
  68. * Base address of memory region
  69. * It must be 2^order aligned address
  70. */
  71. unsigned long base_addr;
  72. } pmp_config;
  73. /**
  74. * @brief PMPCFG list for RV32 (indices 0-7)
  75. *
  76. * For RV32, each PMPCFG register holds 4 PMP entries.
  77. * This list covers the first 32 entries (registers 0-7).
  78. */
  79. #define PMPCFG_LIST_RV32_0_7 \
  80. X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7)
  81. /**
  82. * @brief PMPCFG list for RV32 (indices 8-15)
  83. *
  84. * For RV32, each PMPCFG register holds 4 PMP entries.
  85. * This list covers entries 33-64 (registers 8-15).
  86. */
  87. #define PMPCFG_LIST_RV32_8_15 \
  88. X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15)
  89. /**
  90. * @brief PMPCFG list for RV64 (even indices 0-6)
  91. *
  92. * For RV64, each PMPCFG register holds 8 PMP entries.
  93. * This list covers the first 32 entries (registers 0,2,4,6).
  94. */
  95. #define PMPCFG_LIST_RV64_0_6 \
  96. X(0) X(2) X(4) X(6)
  97. /**
  98. * @brief PMPCFG list for RV64 (even indices 8-14)
  99. *
  100. * For RV64, each PMPCFG register holds 8 PMP entries.
  101. * This list covers entries 33-64 (registers 8,10,12,14).
  102. */
  103. #define PMPCFG_LIST_RV64_8_14 \
  104. X(8) X(10) X(12) X(14)
  105. /**
  106. * @brief Select appropriate PMPCFG list based on architecture and PMP entry count
  107. *
  108. * This macro defines the complete PMPCFG list according to:
  109. * - RISC-V architecture (RV32/RV64)
  110. * - Number of PMP entries (__PMP_ENTRY_NUM)
  111. *
  112. * @note Valid indices depend on configuration:
  113. * - RV32:
  114. * - <=32 entries: indices 0-7
  115. * - <=64 entries: indices 0-15
  116. * - RV64:
  117. * - <=32 entries: indices 0,2,4,6
  118. * - <=64 entries: indices 0,2,4,6,8,10,12,14
  119. */
  120. #if __RISCV_XLEN == 32
  121. #if __PMP_ENTRY_NUM <= 32
  122. #define PMPCFG_LIST PMPCFG_LIST_RV32_0_7
  123. #elif __PMP_ENTRY_NUM <= 64
  124. #define PMPCFG_LIST PMPCFG_LIST_RV32_0_7 PMPCFG_LIST_RV32_8_15
  125. #else
  126. #error "Unsupported PMP_ENTRY_NUM value for RV32"
  127. #endif
  128. #elif __RISCV_XLEN == 64
  129. #if __PMP_ENTRY_NUM <= 32
  130. #define PMPCFG_LIST PMPCFG_LIST_RV64_0_6
  131. #elif __PMP_ENTRY_NUM <= 64
  132. #define PMPCFG_LIST PMPCFG_LIST_RV64_0_6 PMPCFG_LIST_RV64_8_14
  133. #else
  134. #error "Unsupported PMP_ENTRY_NUM value for RV64"
  135. #endif
  136. #else
  137. #error "Unsupported RISC-V architecture"
  138. #endif
  139. /**
  140. * @brief Get PMPCFGx Register by CSR index
  141. * @details Return the content of the PMPCFGx Register.
  142. * @param [in] csr_idx PMPCFG CSR index (0-15 for RV32, 0,2,4,6,8,10,12,14 for RV64)
  143. * @return PMPCFGx Register value
  144. *
  145. * @note Architecture-specific behavior:
  146. * - RV32:
  147. * - Each register holds 4 PMP entries
  148. * - Valid indices: 0 to ceil(__PMP_ENTRY_NUM/4)-1
  149. * - 64 entries require indices 0-15
  150. * - RV64:
  151. * - Each register holds 8 PMP entries
  152. * - Only even indices are valid
  153. * - 64 entries require indices 0,2,4,6,8,10,12,14
  154. *
  155. * @remark The function returns 0 for invalid indices to prevent illegal accesses.
  156. */
  157. __STATIC_INLINE rv_csr_t __get_PMPCFGx(uint32_t csr_idx)
  158. {
  159. switch (csr_idx) {
  160. #define X(n) case n: return __RV_CSR_READ(CSR_PMPCFG##n);
  161. PMPCFG_LIST
  162. #undef X
  163. default: return 0;
  164. }
  165. }
  166. /**
  167. * @brief Set PMPCFGx by CSR index
  168. * @details Write the given value to the PMPCFGx Register.
  169. * @param [in] csr_idx PMPCFG CSR index (0-15 for RV32, 0,2,4,6,8,10,12,14 for RV64)
  170. * @param [in] pmpcfg PMPCFGx Register value to set
  171. *
  172. * @note Architecture-specific behavior:
  173. * - RV32:
  174. * - Each register holds 4 PMP entries
  175. * - Valid indices: 0 to ceil(__PMP_ENTRY_NUM/4)-1
  176. * - 64 entries require indices 0-15
  177. * - RV64:
  178. * - Each register holds 8 PMP entries
  179. * - Only even indices are valid
  180. * - 64 entries require indices 0,2,4,6,8,10,12,14
  181. *
  182. * @remark The function does nothing for invalid indices to prevent illegal accesses.
  183. */
  184. __STATIC_INLINE void __set_PMPCFGx(uint32_t csr_idx, rv_csr_t pmpcfg)
  185. {
  186. switch (csr_idx) {
  187. #define X(n) case n: __RV_CSR_WRITE(CSR_PMPCFG##n, pmpcfg); break;
  188. PMPCFG_LIST
  189. #undef X
  190. default: return;
  191. }
  192. }
  193. /**
  194. * \brief Get 8bit PMPxCFG Register by PMP entry index
  195. * \details Return the content of the PMPxCFG Register.
  196. * \param [in] entry_idx PMP region index(0-63)
  197. * \return PMPxCFG Register value
  198. */
  199. __STATIC_INLINE uint8_t __get_PMPxCFG(uint32_t entry_idx)
  200. {
  201. rv_csr_t pmpcfgx = 0;
  202. uint8_t csr_cfg_num = 0;
  203. uint16_t csr_idx = 0;
  204. uint16_t cfg_shift = 0;
  205. if (entry_idx >= __PMP_ENTRY_NUM) return 0;
  206. #if __RISCV_XLEN == 32
  207. csr_cfg_num = 4;
  208. csr_idx = entry_idx >> 2;
  209. #elif __RISCV_XLEN == 64
  210. csr_cfg_num = 8;
  211. /* For RV64, each PMPCFG register (pmpcfg0, pmpcfg2, etc.) holds 8 PMP entries */
  212. /* Only even-numbered CSRs are used, so we align the index by clearing the LSB */
  213. csr_idx = (entry_idx >> 2) & ~1;
  214. #else
  215. // TODO Add RV128 Handling
  216. return 0;
  217. #endif
  218. pmpcfgx = __get_PMPCFGx(csr_idx);
  219. /*
  220. * first get specific pmpxcfg's order in one CSR composed of csr_cfg_num pmpxcfgs,
  221. * then get pmpxcfg's bit position in one CSR by left shift 3(each pmpxcfg size is one byte)
  222. */
  223. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  224. /* read specific pmpxcfg register value */
  225. return (uint8_t)(__RV_EXTRACT_FIELD(pmpcfgx, 0xFF << cfg_shift));
  226. }
  227. /**
  228. * \brief Set 8bit PMPxCFG by pmp entry index
  229. * \details Set the given pmpxcfg value to the PMPxCFG Register.
  230. * \param [in] entry_idx PMPx region index(0-63)
  231. * \param [in] pmpxcfg PMPxCFG register value to set
  232. * \remark
  233. * - For RV32, 4 pmpxcfgs are densely packed into one CSR in order
  234. * For RV64, 8 pmpxcfgs are densely packed into one CSR in order
  235. */
  236. __STATIC_INLINE void __set_PMPxCFG(uint32_t entry_idx, uint8_t pmpxcfg)
  237. {
  238. rv_csr_t pmpcfgx = 0;
  239. uint8_t csr_cfg_num = 0;
  240. uint16_t csr_idx = 0;
  241. uint16_t cfg_shift = 0;
  242. if (entry_idx >= __PMP_ENTRY_NUM) return;
  243. #if __RISCV_XLEN == 32
  244. csr_cfg_num = 4;
  245. csr_idx = entry_idx >> 2;
  246. #elif __RISCV_XLEN == 64
  247. csr_cfg_num = 8;
  248. /* For RV64, pmpcfg0 and pmpcfg2 each hold 8 PMP entries, align by 2 */
  249. csr_idx = (entry_idx >> 2) & ~1;
  250. #else
  251. // TODO Add RV128 Handling
  252. return;
  253. #endif
  254. /* read specific pmpcfgx register value */
  255. pmpcfgx = __get_PMPCFGx(csr_idx);
  256. /*
  257. * first get specific pmpxcfg's order in one CSR composed of csr_cfg_num pmpxcfgs,
  258. * then get pmpxcfg's bit position in one CSR by left shift 3(each pmpxcfg size is one byte)
  259. */
  260. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  261. pmpcfgx = __RV_INSERT_FIELD(pmpcfgx, 0xFFUL << cfg_shift, pmpxcfg);
  262. __set_PMPCFGx(csr_idx, pmpcfgx);
  263. }
  264. /**
  265. * @brief Base PMPADDR list (indices 0-7)
  266. */
  267. #define PMPADDR_LIST_BASE \
  268. X(0) X(1) X(2) X(3) X(4) X(5) X(6) X(7)
  269. /**
  270. * @brief Extended PMPADDR list (indices 8-15)
  271. */
  272. #define PMPADDR_LIST_8_15 \
  273. X(8) X(9) X(10) X(11) X(12) X(13) X(14) X(15)
  274. /**
  275. * @brief Extended PMPADDR list (indices 16-31)
  276. */
  277. #define PMPADDR_LIST_16_31 \
  278. X(16) X(17) X(18) X(19) X(20) X(21) X(22) X(23) \
  279. X(24) X(25) X(26) X(27) X(28) X(29) X(30) X(31)
  280. /**
  281. * @brief Extended PMPADDR list (indices 32-63)
  282. */
  283. #define PMPADDR_LIST_32_63 \
  284. X(32) X(33) X(34) X(35) X(36) X(37) X(38) X(39) \
  285. X(40) X(41) X(42) X(43) X(44) X(45) X(46) X(47) \
  286. X(48) X(49) X(50) X(51) X(52) X(53) X(54) X(55) \
  287. X(56) X(57) X(58) X(59) X(60) X(61) X(62) X(63)
  288. /**
  289. * @brief Select appropriate PMPADDR list based on PMP_ENTRY_NUM
  290. *
  291. * This macro defines the complete PMPADDR list by combining base and
  292. * extended lists according to the configured number of PMP entries.
  293. *
  294. * @note The actual list is determined by the __PMP_ENTRY_NUM configuration:
  295. * - <=8: Only base list (0-7)
  296. * - <=16: Base + 8-15
  297. * - <=32: Base + 8-15 + 16-31
  298. * - <=64: Base + 8-15 + 16-31 + 32-63
  299. */
  300. #if __PMP_ENTRY_NUM <= 8
  301. #define PMPADDR_LIST PMPADDR_LIST_BASE
  302. #elif __PMP_ENTRY_NUM <= 16
  303. #define PMPADDR_LIST PMPADDR_LIST_BASE PMPADDR_LIST_8_15
  304. #elif __PMP_ENTRY_NUM <= 32
  305. #define PMPADDR_LIST PMPADDR_LIST_BASE PMPADDR_LIST_8_15 PMPADDR_LIST_16_31
  306. #elif __PMP_ENTRY_NUM <= 64
  307. #define PMPADDR_LIST PMPADDR_LIST_BASE PMPADDR_LIST_8_15 PMPADDR_LIST_16_31 PMPADDR_LIST_32_63
  308. #else
  309. #error "Unsupported PMP_ENTRY_NUM value"
  310. #endif
  311. /**
  312. * \brief Get PMPADDRx Register by CSR index
  313. * \details Return the content of the PMPADDRx Register.
  314. * \param [in] csr_idx PMP region CSR index(0-63)
  315. * \return PMPADDRx Register value
  316. */
  317. __STATIC_INLINE rv_csr_t __get_PMPADDRx(uint32_t csr_idx)
  318. {
  319. switch (csr_idx) {
  320. #define X(n) case n: return __RV_CSR_READ(CSR_PMPADDR##n);
  321. PMPADDR_LIST
  322. #undef X
  323. default: return 0;
  324. }
  325. }
  326. /**
  327. * \brief Set PMPADDRx by CSR index
  328. * \details Write the given value to the PMPADDRx Register.
  329. * \param [in] csr_idx PMP region CSR index(0-63)
  330. * \param [in] pmpaddr PMPADDRx Register value to set
  331. */
  332. __STATIC_INLINE void __set_PMPADDRx(uint32_t csr_idx, rv_csr_t pmpaddr)
  333. {
  334. switch (csr_idx) {
  335. #define X(n) case n: __RV_CSR_WRITE(CSR_PMPADDR##n, pmpaddr); break;
  336. PMPADDR_LIST
  337. #undef X
  338. default: return;
  339. }
  340. }
  341. /**
  342. * \brief Set PMP entry by entry idx
  343. * \details Write the given value to the PMPxCFG Register and PMPADDRx.
  344. * \param [in] entry_idx PMP entry index(0-63)
  345. * \param [in] pmp_cfg structure of L, X, W, R field of PMP configuration register, memory region base address
  346. * and size of memory region as power of 2
  347. * \remark
  348. * - If the size of memory region is 2^12(4KB) range, pmp_cfg->order makes 12, and the like.
  349. * - Suppose the size of memory region is 2^X bytes range, if X >=3, the NA4 mode is not selectable, NAPOT is selected.
  350. * - TOR of A field in PMP configuration register is not considered here.
  351. */
  352. __STATIC_INLINE void __set_PMPENTRYx(uint32_t entry_idx, const pmp_config *pmp_cfg)
  353. {
  354. unsigned int cfg_shift, cfg_csr_idx, addr_csr_idx = 0;
  355. unsigned long cfgmask, addrmask = 0;
  356. unsigned long pmpcfg, pmpaddr = 0;
  357. unsigned long protection, csr_cfg_num = 0;
  358. /* check parameters */
  359. if (entry_idx >= __PMP_ENTRY_NUM || pmp_cfg->order > __RISCV_XLEN || pmp_cfg->order < PMP_SHIFT) return;
  360. /* calculate PMP register and offset */
  361. #if __RISCV_XLEN == 32
  362. csr_cfg_num = 4;
  363. cfg_csr_idx = (entry_idx >> 2);
  364. #elif __RISCV_XLEN == 64
  365. csr_cfg_num = 8;
  366. cfg_csr_idx = ((entry_idx >> 2)) & ~1;
  367. #else
  368. // TODO Add RV128 Handling
  369. return;
  370. #endif
  371. /*
  372. * first get specific pmpxcfg's order in one CSR composed of csr_cfg_num pmpxcfgs,
  373. * then get pmpxcfg's bit position in one CSR by left shift 3, each pmpxcfg size is one byte
  374. */
  375. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  376. addr_csr_idx = entry_idx;
  377. /* encode PMP config */
  378. protection = (unsigned long)pmp_cfg->protection;
  379. protection |= (PMP_SHIFT == pmp_cfg->order) ? PMP_A_NA4 : PMP_A_NAPOT;
  380. cfgmask = ~(0xFFUL << cfg_shift);
  381. pmpcfg = (__get_PMPCFGx(cfg_csr_idx) & cfgmask);
  382. pmpcfg |= ((protection << cfg_shift) & ~cfgmask);
  383. /* encode PMP address */
  384. if (PMP_SHIFT == pmp_cfg->order) { /* NA4 */
  385. pmpaddr = (pmp_cfg->base_addr >> PMP_SHIFT);
  386. } else { /* NAPOT */
  387. addrmask = (1UL << (pmp_cfg->order - PMP_SHIFT)) - 1;
  388. pmpaddr = ((pmp_cfg->base_addr >> PMP_SHIFT) & ~addrmask);
  389. pmpaddr |= (addrmask >> 1);
  390. }
  391. /*
  392. * write csrs, update the address first, in case the entry is locked that
  393. * we won't be able to modify it after we set the config csr.
  394. */
  395. __set_PMPADDRx(addr_csr_idx, pmpaddr);
  396. __set_PMPCFGx(cfg_csr_idx, pmpcfg);
  397. }
  398. /**
  399. * \brief Get PMP entry by entry idx
  400. * \details Write the given value to the PMPxCFG Register and PMPADDRx.
  401. * \param [in] entry_idx PMP entry index(0-63)
  402. * \param [out] pmp_cfg structure of L, X, W, R, A field of PMP configuration register, memory region base
  403. * address and size of memory region as power of 2
  404. * \return -1 failure, else 0 success
  405. * \remark
  406. * - If the size of memory region is 2^12(4KB) range, pmp_cfg->order makes 12, and the like.
  407. * - TOR of A field in PMP configuration register is not considered here.
  408. */
  409. __STATIC_INLINE int __get_PMPENTRYx(unsigned int entry_idx, pmp_config *pmp_cfg)
  410. {
  411. unsigned int cfg_shift, cfg_csr_idx, addr_csr_idx = 0;
  412. unsigned long cfgmask, pmpcfg, prot = 0;
  413. unsigned long t1, addr, pmpaddr, len = 0;
  414. uint8_t csr_cfg_num = 0;
  415. /* check parameters */
  416. if (entry_idx >= __PMP_ENTRY_NUM || !pmp_cfg) return -1;
  417. /* calculate PMP register and offset */
  418. #if __RISCV_XLEN == 32
  419. csr_cfg_num = 4;
  420. cfg_csr_idx = entry_idx >> 2;
  421. #elif __RISCV_XLEN == 64
  422. csr_cfg_num = 8;
  423. cfg_csr_idx = (entry_idx>> 2) & ~1;
  424. #else
  425. // TODO Add RV128 Handling
  426. return -1;
  427. #endif
  428. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  429. addr_csr_idx = entry_idx;
  430. /* decode PMP config */
  431. cfgmask = (0xFFUL << cfg_shift);
  432. pmpcfg = (__get_PMPCFGx(cfg_csr_idx) & cfgmask);
  433. prot = pmpcfg >> cfg_shift;
  434. /* decode PMP address */
  435. pmpaddr = __get_PMPADDRx(addr_csr_idx);
  436. if (PMP_A_NAPOT == (prot & PMP_A)) {
  437. t1 = __CTZ(~pmpaddr);
  438. addr = (pmpaddr & ~((1UL << t1) - 1)) << PMP_SHIFT;
  439. len = (t1 + PMP_SHIFT + 1);
  440. } else {
  441. addr = pmpaddr << PMP_SHIFT;
  442. len = PMP_SHIFT;
  443. }
  444. /* return details */
  445. pmp_cfg->protection = prot;
  446. pmp_cfg->base_addr = addr;
  447. pmp_cfg->order = len;
  448. return 0;
  449. }
  450. /** @} */ /* End of Doxygen Group NMSIS_Core_PMP_Functions */
  451. #endif /* defined(__PMP_PRESENT) && (__PMP_PRESENT == 1) */
  452. #ifdef __cplusplus
  453. }
  454. #endif
  455. #endif /* __CORE_FEATURE_PMP_H__ */