hpm_pmp_drv.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (c) 2021-2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PMP_DRV_H
  8. #define HPM_PMP_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_soc_feature.h"
  11. /**
  12. * @brief PMP Entry structure
  13. */
  14. typedef struct pmp_entry_struct {
  15. union {
  16. struct {
  17. uint8_t read_access_ctrl: 1;
  18. uint8_t write_access_ctrl: 1;
  19. uint8_t execution_ctrl: 1;
  20. uint8_t addr_matching_mode: 2;
  21. uint8_t reserved: 2;
  22. uint8_t lock: 1;
  23. };
  24. uint8_t val;
  25. } pmp_cfg;
  26. uint8_t reserved0[3];
  27. uint32_t pmp_addr;
  28. #if (!defined(PMP_SUPPORT_PMA)) || (defined(PMP_SUPPORT_PMA) && (PMP_SUPPORT_PMA == 1))
  29. union {
  30. struct {
  31. uint8_t entry_addr_matching_mode: 2;
  32. uint8_t mem_type_attribute: 4;
  33. uint8_t automic_mem_operation_ctrl: 1;
  34. uint8_t reserved: 1;
  35. };
  36. uint8_t val;
  37. } pma_cfg;
  38. uint8_t reserved1[3];
  39. uint32_t pma_addr;
  40. #endif
  41. } pmp_entry_t;
  42. /**
  43. * @brief PMP Configuration definitions
  44. */
  45. #define READ_EN (1U)
  46. #define READ_DIS (0U)
  47. #define WRITE_EN (1U)
  48. #define WRITE_DIS (0U)
  49. #define EXECUTE_EN (1U)
  50. #define EXECUTE_DIS (0U)
  51. #define ADDR_MATCH_MODE_OFF (0U)
  52. #define ADDR_MATCH_TOR (1U)
  53. #define ADDR_MATCH_NAPOT (3U)
  54. #define REG_LOCK (1U)
  55. #define REG_UNLOCK (0U)
  56. /**
  57. * @brief PMA Configuration definitions
  58. */
  59. #define MEM_TYPE_DEV_NON_BUF (0U)
  60. #define MEM_TYPE_DEV_BUF (1U)
  61. #define MEM_TYPE_MEM_NON_CACHE_NON_BUF (2U)
  62. #define MEM_TYPE_MEM_NON_CACHE_BUF (3U)
  63. #define MEM_TYPE_MEM_WT_NO_ALLOC (4U)
  64. #define MEM_TYPE_MEM_WT_READ_ALLOC (5U)
  65. #define MEM_TYPE_MEM_WB_NO_ALLOC (8U)
  66. #define MEM_TYPE_MEM_WB_READ_ALLOC (9U)
  67. #define MEM_TYPE_MEM_WB_WRITE_ALLOC (10U)
  68. #define MEM_TYPE_MEM_WB_READ_WRITE_ALLOC (11U)
  69. #define MEM_TYPE_EMPTY_HOLE (15U)
  70. #define AMO_EN (0U)
  71. #define AMO_DIS (1U)
  72. /**
  73. * @brief PMP Configuration
  74. * @param r - READ Access control, valid value: READ_EN, READ_DIS
  75. * @param w - Write access control, valid value: WRITE_EN, WRITE_DIS
  76. * @param x - Instruction Execution control, valid value: EXECUTE_EN, EXECUTE_DIS
  77. * @param m - Address matching mode, valid value:
  78. * ADDR_MATCH_MODE_OFF - Null region
  79. * ADDR_MATCH_TOR - Top of range. For pmp_addr0, any address < pmp_addr0 matches, for other regions,
  80. * any address which meets ( pmp_addr[i-1] <= addr < pmp_addr) matches.
  81. * ADDR_MATCH_NAPOT - Naturally aligned power-of-2 region, minimal size must be 8 bytes
  82. * @param l - Write lock and permission enforcement bit for Machine mode, valid value: REG_LOCK, REG_UNLOCK
  83. */
  84. #define PMP_CFG(r, w, x, m, l) ((r) | ((w) << 1) | ((x) << 2) | ((m) << 3) | ((l) << 7))
  85. /**
  86. * @brief PMA Configuration
  87. * @param m - Entry address matching mode, valid value:
  88. * ADDR_MATCH_MODE_OFF - This PMA entry is disabled
  89. * ADDR_MATCH_NAPOT - Naturally aligned power-of-2 region, the granularity is 4K bytes
  90. * @param t - Memory type attributes, valid value:
  91. * MEM_TYPE_DEV_NON_BUF - Device, Non-bufferable
  92. * MEM_TYPE_DEV_BUF - Device, bufferable
  93. * MEM_TYPE_MEM_NON_CACHE_NON_BUF - Memory, Non-cacheable, Non-bufferable
  94. * MEM_TYPE_MEM_NON_CACHE_BUF - Memory, Non-cacheable, bufferable
  95. * MEM_TYPE_MEM_WT_NO_ALLOC - Memory, Write-through, No-allocate
  96. * MEM_TYPE_MEM_WT_READ_ALLOC - Memory, Write-through, read-allocate
  97. * MEM_TYPE_MEM_WB_NO_ALLOC - Memory, Write-back, No-allocate
  98. * MEM_TYPE_MEM_WB_READ_ALLOC - Memory, Write-back, Read-allocate
  99. * MEM_TYPE_MEM_WB_READ_WRITE_ALLOC - Memory, Write-back, Write-Allocate, Read-Allocate
  100. * MEM_TYPE_EMPTY_HOLE - Empty hole, nothing exists
  101. *
  102. * @param n - Indicate Whether Atomic Memory Operation instructions are not supported in this region, valid value:
  103. * AMO_EN - Atomic Memory Operations are supported
  104. * AMO_DIS - Atomic Memory Operations are not supported
  105. */
  106. #define PMA_CFG(m, t, n) ((m) | ((t) << 2) | ((n) << 6))
  107. /**
  108. * @brief Format Top Address Region
  109. */
  110. #define PMP_TOR_ADDR(addr) ((addr) >> 2)
  111. /**
  112. * @brief Format PMP Natural Aligned Region
  113. * @param x - start address
  114. * @param n - power-of-2 aligned length
  115. */
  116. #define PMP_NAPOT_ADDR(x, n) (((uint32_t)(x) >> 2) | (((uint32_t)(n)-1U) >> 3))
  117. /**
  118. * @brief Format PMA Natural Aligned Region
  119. * @param x - start address
  120. * @param n - power-of-2 aligned length
  121. */
  122. #define PMA_NAPOT_ADDR(x, n) (((uint32_t)(x) >> 2) | ((((uint32_t)(n)-1U) >> 3)))
  123. #ifdef __cplusplus
  124. extern "C" {
  125. #endif
  126. /**
  127. * @brief Write PMP Configuration to corresponding PMP_CFG register
  128. * @param value PMP configuration
  129. * @param idx PMP entry index, valid value is 0-15
  130. */
  131. void write_pmp_cfg(uint32_t value, uint32_t idx);
  132. /**
  133. * @brief Read PMP configuration
  134. * @param idx PMP entry index
  135. * @return PMP configuration
  136. */
  137. uint32_t read_pmp_cfg(uint32_t idx);
  138. /**
  139. * @brief Write PMP address to corresponding PMP_ADDR register
  140. * @param value PMP address
  141. * @param idx PMP address entry index, valid value is 0-15
  142. */
  143. void write_pmp_addr(uint32_t value, uint32_t idx);
  144. /**
  145. * @brief Read PMP address entry
  146. * @param idx PMP address entry index
  147. * @return PMP address
  148. */
  149. uint32_t read_pmp_addr(uint32_t idx);
  150. #if (!defined(PMP_SUPPORT_PMA)) || (defined(PMP_SUPPORT_PMA) && (PMP_SUPPORT_PMA == 1))
  151. /**
  152. * @brief Read PMA configuration
  153. * @param idx PMA entry index
  154. * @return PMA configuration
  155. */
  156. uint32_t read_pma_cfg(uint32_t idx);
  157. /**
  158. * @brief Write PMA Configuration to corresponding PMA_CFG register
  159. * @param value PMA configuration
  160. * @param idx PMA entry index, valid value is 0-15
  161. */
  162. void write_pma_cfg(uint32_t value, uint32_t idx);
  163. /**
  164. * @brief Write PMA address to corresponding PMA_ADDR register
  165. * @param value PMA address
  166. * @param idx PMA address entry index, valid value is 0-15
  167. */
  168. void write_pma_addr(uint32_t value, uint32_t idx);
  169. /**
  170. * @brief Read PMA address entry
  171. * @param idx PMA address entry index, valid value is 0-15
  172. * @return PMA address
  173. */
  174. uint32_t read_pma_addr(uint32_t idx);
  175. #endif /* #if (!defined(PMP_SUPPORT_PMA)) || (defined(PMP_SUPPORT_PMA) && (PMP_SUPPORT_PMA == 1)) */
  176. /**
  177. * @brief Configure PMP and PMA for specified PMP/PMA entry
  178. *
  179. * @param[in] entry PMP entry
  180. * @param entry_index PMP/PMA entry index
  181. * @retval status_invalid_argument Invalid Arguments were detected
  182. * @retval status_success Configuration completed without errors
  183. */
  184. hpm_stat_t pmp_config_entry(const pmp_entry_t *entry, uint32_t entry_index);
  185. /**
  186. * @brief Configure PMP and PMA based on the PMP entry list
  187. * @param entry start of the PMP entry list
  188. * @param num_of_entries Number of entries in the PMP entry list
  189. * @retval status_invalid_argument Invalid Arguments were detected
  190. * @retval status_success Configuration completed without errors
  191. */
  192. hpm_stat_t pmp_config(const pmp_entry_t *entry, uint32_t num_of_entries);
  193. /**
  194. * @brief Disable PMP and PMA
  195. */
  196. void pmp_disable(void);
  197. #ifdef __cplusplus
  198. }
  199. #endif
  200. #endif /* HPM_PMP_DRV_H */