core_feature_pmp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 Get PMPCFGx Register by csr index
  75. * \details Return the content of the PMPCFGx Register.
  76. * \param [in] csr_idx PMPCFG CSR index(0-3)
  77. * \return PMPCFGx Register value
  78. * \remark
  79. * - For RV64, only csr_idx = 0 and csr_idx = 2 is allowed.
  80. * pmpcfg0 and pmpcfg2 hold the configurations
  81. * for the 16 PMP entries, pmpcfg1 and pmpcfg3 are illegal
  82. * - For RV32, pmpcfg0–pmpcfg3, hold the configurations
  83. * pmp0cfg–pmp15cfg for the 16 PMP entries
  84. */
  85. __STATIC_INLINE rv_csr_t __get_PMPCFGx(uint32_t csr_idx)
  86. {
  87. switch (csr_idx) {
  88. case 0: return __RV_CSR_READ(CSR_PMPCFG0);
  89. case 1: return __RV_CSR_READ(CSR_PMPCFG1);
  90. case 2: return __RV_CSR_READ(CSR_PMPCFG2);
  91. case 3: return __RV_CSR_READ(CSR_PMPCFG3);
  92. default: return 0;
  93. }
  94. }
  95. /**
  96. * \brief Set PMPCFGx by csr index
  97. * \details Write the given value to the PMPCFGx Register.
  98. * \param [in] csr_idx PMPCFG CSR index(0-3)
  99. * \param [in] pmpcfg PMPCFGx Register value to set
  100. * \remark
  101. * - For RV64, only csr_idx = 0 and csr_idx = 2 is allowed.
  102. * pmpcfg0 and pmpcfg2 hold the configurations
  103. * for the 16 PMP entries, pmpcfg1 and pmpcfg3 are illegal
  104. * - For RV32, pmpcfg0–pmpcfg3, hold the configurations
  105. * pmp0cfg–pmp15cfg for the 16 PMP entries
  106. */
  107. __STATIC_INLINE void __set_PMPCFGx(uint32_t csr_idx, rv_csr_t pmpcfg)
  108. {
  109. switch (csr_idx) {
  110. case 0: __RV_CSR_WRITE(CSR_PMPCFG0, pmpcfg); break;
  111. case 1: __RV_CSR_WRITE(CSR_PMPCFG1, pmpcfg); break;
  112. case 2: __RV_CSR_WRITE(CSR_PMPCFG2, pmpcfg); break;
  113. case 3: __RV_CSR_WRITE(CSR_PMPCFG3, pmpcfg); break;
  114. default: return;
  115. }
  116. }
  117. /**
  118. * \brief Get 8bit PMPxCFG Register by PMP entry index
  119. * \details Return the content of the PMPxCFG Register.
  120. * \param [in] entry_idx PMP region index(0-15)
  121. * \return PMPxCFG Register value
  122. */
  123. __STATIC_INLINE uint8_t __get_PMPxCFG(uint32_t entry_idx)
  124. {
  125. rv_csr_t pmpcfgx = 0;
  126. uint8_t csr_cfg_num = 0;
  127. uint16_t csr_idx = 0;
  128. uint16_t cfg_shift = 0;
  129. if (entry_idx >= __PMP_ENTRY_NUM) return 0;
  130. #if __RISCV_XLEN == 32
  131. csr_cfg_num = 4;
  132. csr_idx = entry_idx >> 2;
  133. #elif __RISCV_XLEN == 64
  134. csr_cfg_num = 8;
  135. /* For RV64, pmpcfg0 and pmpcfg2 each hold 8 PMP entries, align by 2 */
  136. csr_idx = (entry_idx >> 2) & ~1;
  137. #else
  138. // TODO Add RV128 Handling
  139. return 0;
  140. #endif
  141. pmpcfgx = __get_PMPCFGx(csr_idx);
  142. /*
  143. * first get specific pmpxcfg's order in one CSR composed of csr_cfg_num pmpxcfgs,
  144. * then get pmpxcfg's bit position in one CSR by left shift 3(each pmpxcfg size is one byte)
  145. */
  146. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  147. /* read specific pmpxcfg register value */
  148. return (uint8_t)(__RV_EXTRACT_FIELD(pmpcfgx, 0xFF << cfg_shift));
  149. }
  150. /**
  151. * \brief Set 8bit PMPxCFG by pmp entry index
  152. * \details Set the given pmpxcfg value to the PMPxCFG Register.
  153. * \param [in] entry_idx PMPx region index(0-15)
  154. * \param [in] pmpxcfg PMPxCFG register value to set
  155. * \remark
  156. * - For RV32, 4 pmpxcfgs are densely packed into one CSR in order
  157. * For RV64, 8 pmpxcfgs are densely packed into one CSR in order
  158. */
  159. __STATIC_INLINE void __set_PMPxCFG(uint32_t entry_idx, uint8_t pmpxcfg)
  160. {
  161. rv_csr_t pmpcfgx = 0;
  162. uint8_t csr_cfg_num = 0;
  163. uint16_t csr_idx = 0;
  164. uint16_t cfg_shift = 0;
  165. if (entry_idx >= __PMP_ENTRY_NUM) return;
  166. #if __RISCV_XLEN == 32
  167. csr_cfg_num = 4;
  168. csr_idx = entry_idx >> 2;
  169. #elif __RISCV_XLEN == 64
  170. csr_cfg_num = 8;
  171. /* For RV64, pmpcfg0 and pmpcfg2 each hold 8 PMP entries, align by 2 */
  172. csr_idx = (entry_idx >> 2) & ~1;
  173. #else
  174. // TODO Add RV128 Handling
  175. return;
  176. #endif
  177. /* read specific pmpcfgx register value */
  178. pmpcfgx = __get_PMPCFGx(csr_idx);
  179. /*
  180. * first get specific pmpxcfg's order in one CSR composed of csr_cfg_num pmpxcfgs,
  181. * then get pmpxcfg's bit position in one CSR by left shift 3(each pmpxcfg size is one byte)
  182. */
  183. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  184. pmpcfgx = __RV_INSERT_FIELD(pmpcfgx, 0xFFUL << cfg_shift, pmpxcfg);
  185. __set_PMPCFGx(csr_idx, pmpcfgx);
  186. }
  187. /**
  188. * \brief Get PMPADDRx Register by CSR index
  189. * \details Return the content of the PMPADDRx Register.
  190. * \param [in] csr_idx PMP region CSR index(0-15)
  191. * \return PMPADDRx Register value
  192. */
  193. __STATIC_INLINE rv_csr_t __get_PMPADDRx(uint32_t csr_idx)
  194. {
  195. switch (csr_idx) {
  196. case 0: return __RV_CSR_READ(CSR_PMPADDR0);
  197. case 1: return __RV_CSR_READ(CSR_PMPADDR1);
  198. case 2: return __RV_CSR_READ(CSR_PMPADDR2);
  199. case 3: return __RV_CSR_READ(CSR_PMPADDR3);
  200. case 4: return __RV_CSR_READ(CSR_PMPADDR4);
  201. case 5: return __RV_CSR_READ(CSR_PMPADDR5);
  202. case 6: return __RV_CSR_READ(CSR_PMPADDR6);
  203. case 7: return __RV_CSR_READ(CSR_PMPADDR7);
  204. case 8: return __RV_CSR_READ(CSR_PMPADDR8);
  205. case 9: return __RV_CSR_READ(CSR_PMPADDR9);
  206. case 10: return __RV_CSR_READ(CSR_PMPADDR10);
  207. case 11: return __RV_CSR_READ(CSR_PMPADDR11);
  208. case 12: return __RV_CSR_READ(CSR_PMPADDR12);
  209. case 13: return __RV_CSR_READ(CSR_PMPADDR13);
  210. case 14: return __RV_CSR_READ(CSR_PMPADDR14);
  211. case 15: return __RV_CSR_READ(CSR_PMPADDR15);
  212. default: return 0;
  213. }
  214. }
  215. /**
  216. * \brief Set PMPADDRx by CSR index
  217. * \details Write the given value to the PMPADDRx Register.
  218. * \param [in] csr_idx PMP region CSR index(0-15)
  219. * \param [in] pmpaddr PMPADDRx Register value to set
  220. */
  221. __STATIC_INLINE void __set_PMPADDRx(uint32_t csr_idx, rv_csr_t pmpaddr)
  222. {
  223. switch (csr_idx) {
  224. case 0: __RV_CSR_WRITE(CSR_PMPADDR0, pmpaddr); break;
  225. case 1: __RV_CSR_WRITE(CSR_PMPADDR1, pmpaddr); break;
  226. case 2: __RV_CSR_WRITE(CSR_PMPADDR2, pmpaddr); break;
  227. case 3: __RV_CSR_WRITE(CSR_PMPADDR3, pmpaddr); break;
  228. case 4: __RV_CSR_WRITE(CSR_PMPADDR4, pmpaddr); break;
  229. case 5: __RV_CSR_WRITE(CSR_PMPADDR5, pmpaddr); break;
  230. case 6: __RV_CSR_WRITE(CSR_PMPADDR6, pmpaddr); break;
  231. case 7: __RV_CSR_WRITE(CSR_PMPADDR7, pmpaddr); break;
  232. case 8: __RV_CSR_WRITE(CSR_PMPADDR8, pmpaddr); break;
  233. case 9: __RV_CSR_WRITE(CSR_PMPADDR9, pmpaddr); break;
  234. case 10: __RV_CSR_WRITE(CSR_PMPADDR10, pmpaddr); break;
  235. case 11: __RV_CSR_WRITE(CSR_PMPADDR11, pmpaddr); break;
  236. case 12: __RV_CSR_WRITE(CSR_PMPADDR12, pmpaddr); break;
  237. case 13: __RV_CSR_WRITE(CSR_PMPADDR13, pmpaddr); break;
  238. case 14: __RV_CSR_WRITE(CSR_PMPADDR14, pmpaddr); break;
  239. case 15: __RV_CSR_WRITE(CSR_PMPADDR15, pmpaddr); break;
  240. default: return;
  241. }
  242. }
  243. /**
  244. * \brief Set PMP entry by entry idx
  245. * \details Write the given value to the PMPxCFG Register and PMPADDRx.
  246. * \param [in] entry_idx PMP entry index(0-15)
  247. * \param [in] pmp_cfg structure of L, X, W, R field of PMP configuration register, memory region base address
  248. * and size of memory region as power of 2
  249. * \remark
  250. * - If the size of memory region is 2^12(4KB) range, pmp_cfg->order makes 12, and the like.
  251. * - Suppose the size of memory region is 2^X bytes range, if X >=3, the NA4 mode is not selectable, NAPOT is selected.
  252. * - TOR of A field in PMP configuration register is not considered here.
  253. */
  254. __STATIC_INLINE void __set_PMPENTRYx(uint32_t entry_idx, const pmp_config *pmp_cfg)
  255. {
  256. unsigned int cfg_shift, cfg_csr_idx, addr_csr_idx = 0;
  257. unsigned long cfgmask, addrmask = 0;
  258. unsigned long pmpcfg, pmpaddr = 0;
  259. unsigned long protection, csr_cfg_num = 0;
  260. /* check parameters */
  261. if (entry_idx >= __PMP_ENTRY_NUM || pmp_cfg->order > __RISCV_XLEN || pmp_cfg->order < PMP_SHIFT) return;
  262. /* calculate PMP register and offset */
  263. #if __RISCV_XLEN == 32
  264. csr_cfg_num = 4;
  265. cfg_csr_idx = (entry_idx >> 2);
  266. #elif __RISCV_XLEN == 64
  267. csr_cfg_num = 8;
  268. cfg_csr_idx = ((entry_idx >> 2)) & ~1;
  269. #else
  270. // TODO Add RV128 Handling
  271. return;
  272. #endif
  273. /*
  274. * first get specific pmpxcfg's order in one CSR composed of csr_cfg_num pmpxcfgs,
  275. * then get pmpxcfg's bit position in one CSR by left shift 3, each pmpxcfg size is one byte
  276. */
  277. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  278. addr_csr_idx = entry_idx;
  279. /* encode PMP config */
  280. protection = (unsigned long)pmp_cfg->protection;
  281. protection |= (PMP_SHIFT == pmp_cfg->order) ? PMP_A_NA4 : PMP_A_NAPOT;
  282. cfgmask = ~(0xFFUL << cfg_shift);
  283. pmpcfg = (__get_PMPCFGx(cfg_csr_idx) & cfgmask);
  284. pmpcfg |= ((protection << cfg_shift) & ~cfgmask);
  285. /* encode PMP address */
  286. if (PMP_SHIFT == pmp_cfg->order) { /* NA4 */
  287. pmpaddr = (pmp_cfg->base_addr >> PMP_SHIFT);
  288. } else { /* NAPOT */
  289. addrmask = (1UL << (pmp_cfg->order - PMP_SHIFT)) - 1;
  290. pmpaddr = ((pmp_cfg->base_addr >> PMP_SHIFT) & ~addrmask);
  291. pmpaddr |= (addrmask >> 1);
  292. }
  293. /*
  294. * write csrs, update the address first, in case the entry is locked that
  295. * we won't be able to modify it after we set the config csr.
  296. */
  297. __set_PMPADDRx(addr_csr_idx, pmpaddr);
  298. __set_PMPCFGx(cfg_csr_idx, pmpcfg);
  299. }
  300. /**
  301. * \brief Get PMP entry by entry idx
  302. * \details Write the given value to the PMPxCFG Register and PMPADDRx.
  303. * \param [in] entry_idx PMP entry index(0-15)
  304. * \param [out] pmp_cfg structure of L, X, W, R, A field of PMP configuration register, memory region base
  305. * address and size of memory region as power of 2
  306. * \return -1 failure, else 0 success
  307. * \remark
  308. * - If the size of memory region is 2^12(4KB) range, pmp_cfg->order makes 12, and the like.
  309. * - TOR of A field in PMP configuration register is not considered here.
  310. */
  311. __STATIC_INLINE int __get_PMPENTRYx(unsigned int entry_idx, pmp_config *pmp_cfg)
  312. {
  313. unsigned int cfg_shift, cfg_csr_idx, addr_csr_idx = 0;
  314. unsigned long cfgmask, pmpcfg, prot = 0;
  315. unsigned long t1, addr, pmpaddr, len = 0;
  316. uint8_t csr_cfg_num = 0;
  317. /* check parameters */
  318. if (entry_idx >= __PMP_ENTRY_NUM || !pmp_cfg) return -1;
  319. /* calculate PMP register and offset */
  320. #if __RISCV_XLEN == 32
  321. csr_cfg_num = 4;
  322. cfg_csr_idx = entry_idx >> 2;
  323. #elif __RISCV_XLEN == 64
  324. csr_cfg_num = 8;
  325. cfg_csr_idx = (entry_idx>> 2) & ~1;
  326. #else
  327. // TODO Add RV128 Handling
  328. return -1;
  329. #endif
  330. cfg_shift = (entry_idx & (csr_cfg_num - 1)) << 3;
  331. addr_csr_idx = entry_idx;
  332. /* decode PMP config */
  333. cfgmask = (0xFFUL << cfg_shift);
  334. pmpcfg = (__get_PMPCFGx(cfg_csr_idx) & cfgmask);
  335. prot = pmpcfg >> cfg_shift;
  336. /* decode PMP address */
  337. pmpaddr = __get_PMPADDRx(addr_csr_idx);
  338. if (PMP_A_NAPOT == (prot & PMP_A)) {
  339. t1 = __CTZ(~pmpaddr);
  340. addr = (pmpaddr & ~((1UL << t1) - 1)) << PMP_SHIFT;
  341. len = (t1 + PMP_SHIFT + 1);
  342. } else {
  343. addr = pmpaddr << PMP_SHIFT;
  344. len = PMP_SHIFT;
  345. }
  346. /* return details */
  347. pmp_cfg->protection = prot;
  348. pmp_cfg->base_addr = addr;
  349. pmp_cfg->order = len;
  350. return 0;
  351. }
  352. /** @} */ /* End of Doxygen Group NMSIS_Core_PMP_Functions */
  353. #endif /* defined(__PMP_PRESENT) && (__PMP_PRESENT == 1) */
  354. #ifdef __cplusplus
  355. }
  356. #endif
  357. #endif /* __CORE_FEATURE_PMP_H__ */