mpu_armv8.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /******************************************************************************
  2. * @file mpu_armv8.h
  3. * @brief CMSIS MPU API for Armv8-M and Armv8.1-M MPU
  4. * @version V5.1.2
  5. * @date 10. February 2020
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2017-2020 Arm Limited. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #if defined ( __ICCARM__ )
  25. #pragma system_include /* treat file as system include file for MISRA check */
  26. #elif defined (__clang__)
  27. #pragma clang system_header /* treat file as system include file */
  28. #endif
  29. #ifndef ARM_MPU_ARMV8_H
  30. #define ARM_MPU_ARMV8_H
  31. /** \brief Attribute for device memory (outer only) */
  32. #define ARM_MPU_ATTR_DEVICE ( 0U )
  33. /** \brief Attribute for non-cacheable, normal memory */
  34. #define ARM_MPU_ATTR_NON_CACHEABLE ( 4U )
  35. /** \brief Attribute for normal memory (outer and inner)
  36. * \param NT Non-Transient: Set to 1 for non-transient data.
  37. * \param WB Write-Back: Set to 1 to use write-back update policy.
  38. * \param RA Read Allocation: Set to 1 to use cache allocation on read miss.
  39. * \param WA Write Allocation: Set to 1 to use cache allocation on write miss.
  40. */
  41. #define ARM_MPU_ATTR_MEMORY_(NT, WB, RA, WA) \
  42. ((((NT) & 1U) << 3U) | (((WB) & 1U) << 2U) | (((RA) & 1U) << 1U) | ((WA) & 1U))
  43. /** \brief Device memory type non Gathering, non Re-ordering, non Early Write Acknowledgement */
  44. #define ARM_MPU_ATTR_DEVICE_nGnRnE (0U)
  45. /** \brief Device memory type non Gathering, non Re-ordering, Early Write Acknowledgement */
  46. #define ARM_MPU_ATTR_DEVICE_nGnRE (1U)
  47. /** \brief Device memory type non Gathering, Re-ordering, Early Write Acknowledgement */
  48. #define ARM_MPU_ATTR_DEVICE_nGRE (2U)
  49. /** \brief Device memory type Gathering, Re-ordering, Early Write Acknowledgement */
  50. #define ARM_MPU_ATTR_DEVICE_GRE (3U)
  51. /** \brief Memory Attribute
  52. * \param O Outer memory attributes
  53. * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes
  54. */
  55. #define ARM_MPU_ATTR(O, I) ((((O) & 0xFU) << 4U) | ((((O) & 0xFU) != 0U) ? ((I) & 0xFU) : (((I) & 0x3U) << 2U)))
  56. /** \brief Normal memory non-shareable */
  57. #define ARM_MPU_SH_NON (0U)
  58. /** \brief Normal memory outer shareable */
  59. #define ARM_MPU_SH_OUTER (2U)
  60. /** \brief Normal memory inner shareable */
  61. #define ARM_MPU_SH_INNER (3U)
  62. /** \brief Memory access permissions
  63. * \param RO Read-Only: Set to 1 for read-only memory.
  64. * \param NP Non-Privileged: Set to 1 for non-privileged memory.
  65. */
  66. #define ARM_MPU_AP_(RO, NP) ((((RO) & 1U) << 1U) | ((NP) & 1U))
  67. /** \brief Region Base Address Register value
  68. * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.
  69. * \param SH Defines the Shareability domain for this memory region.
  70. * \param RO Read-Only: Set to 1 for a read-only memory region.
  71. * \param NP Non-Privileged: Set to 1 for a non-privileged memory region.
  72. * \oaram XN eXecute Never: Set to 1 for a non-executable memory region.
  73. */
  74. #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
  75. (((BASE) & MPU_RBAR_BASE_Msk) | \
  76. (((SH) << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
  77. ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
  78. (((XN) << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
  79. /** \brief Region Limit Address Register value
  80. * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
  81. * \param IDX The attribute index to be associated with this memory region.
  82. */
  83. #define ARM_MPU_RLAR(LIMIT, IDX) \
  84. (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
  85. (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
  86. (MPU_RLAR_EN_Msk))
  87. #if defined(MPU_RLAR_PXN_Pos)
  88. /** \brief Region Limit Address Register with PXN value
  89. * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
  90. * \param PXN Privileged execute never. Defines whether code can be executed from this privileged region.
  91. * \param IDX The attribute index to be associated with this memory region.
  92. */
  93. #define ARM_MPU_RLAR_PXN(LIMIT, PXN, IDX) \
  94. (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
  95. (((PXN) << MPU_RLAR_PXN_Pos) & MPU_RLAR_PXN_Msk) | \
  96. (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
  97. (MPU_RLAR_EN_Msk))
  98. #endif
  99. /**
  100. * Struct for a single MPU Region
  101. */
  102. typedef struct {
  103. uint32_t RBAR; /*!< Region Base Address Register value */
  104. uint32_t RLAR; /*!< Region Limit Address Register value */
  105. } ARM_MPU_Region_t;
  106. /** Enable the MPU.
  107. * \param MPU_Control Default access permissions for unconfigured regions.
  108. */
  109. __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
  110. {
  111. __DMB();
  112. MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
  113. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  114. SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
  115. #endif
  116. __DSB();
  117. __ISB();
  118. }
  119. /** Disable the MPU.
  120. */
  121. __STATIC_INLINE void ARM_MPU_Disable(void)
  122. {
  123. __DMB();
  124. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  125. SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
  126. #endif
  127. MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
  128. __DSB();
  129. __ISB();
  130. }
  131. #ifdef MPU_NS
  132. /** Enable the Non-secure MPU.
  133. * \param MPU_Control Default access permissions for unconfigured regions.
  134. */
  135. __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
  136. {
  137. __DMB();
  138. MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
  139. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  140. SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
  141. #endif
  142. __DSB();
  143. __ISB();
  144. }
  145. /** Disable the Non-secure MPU.
  146. */
  147. __STATIC_INLINE void ARM_MPU_Disable_NS(void)
  148. {
  149. __DMB();
  150. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  151. SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
  152. #endif
  153. MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
  154. __DSB();
  155. __ISB();
  156. }
  157. #endif
  158. /** Set the memory attribute encoding to the given MPU.
  159. * \param mpu Pointer to the MPU to be configured.
  160. * \param idx The attribute index to be set [0-7]
  161. * \param attr The attribute value to be set.
  162. */
  163. __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
  164. {
  165. const uint8_t reg = idx / 4U;
  166. const uint32_t pos = ((idx % 4U) * 8U);
  167. const uint32_t mask = 0xFFU << pos;
  168. if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
  169. return; // invalid index
  170. }
  171. mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask));
  172. }
  173. /** Set the memory attribute encoding.
  174. * \param idx The attribute index to be set [0-7]
  175. * \param attr The attribute value to be set.
  176. */
  177. __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
  178. {
  179. ARM_MPU_SetMemAttrEx(MPU, idx, attr);
  180. }
  181. #ifdef MPU_NS
  182. /** Set the memory attribute encoding to the Non-secure MPU.
  183. * \param idx The attribute index to be set [0-7]
  184. * \param attr The attribute value to be set.
  185. */
  186. __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
  187. {
  188. ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
  189. }
  190. #endif
  191. /** Clear and disable the given MPU region of the given MPU.
  192. * \param mpu Pointer to MPU to be used.
  193. * \param rnr Region number to be cleared.
  194. */
  195. __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
  196. {
  197. mpu->RNR = rnr;
  198. mpu->RLAR = 0U;
  199. }
  200. /** Clear and disable the given MPU region.
  201. * \param rnr Region number to be cleared.
  202. */
  203. __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
  204. {
  205. ARM_MPU_ClrRegionEx(MPU, rnr);
  206. }
  207. #ifdef MPU_NS
  208. /** Clear and disable the given Non-secure MPU region.
  209. * \param rnr Region number to be cleared.
  210. */
  211. __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
  212. {
  213. ARM_MPU_ClrRegionEx(MPU_NS, rnr);
  214. }
  215. #endif
  216. /** Configure the given MPU region of the given MPU.
  217. * \param mpu Pointer to MPU to be used.
  218. * \param rnr Region number to be configured.
  219. * \param rbar Value for RBAR register.
  220. * \param rlar Value for RLAR register.
  221. */
  222. __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
  223. {
  224. mpu->RNR = rnr;
  225. mpu->RBAR = rbar;
  226. mpu->RLAR = rlar;
  227. }
  228. /** Configure the given MPU region.
  229. * \param rnr Region number to be configured.
  230. * \param rbar Value for RBAR register.
  231. * \param rlar Value for RLAR register.
  232. */
  233. __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
  234. {
  235. ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
  236. }
  237. #ifdef MPU_NS
  238. /** Configure the given Non-secure MPU region.
  239. * \param rnr Region number to be configured.
  240. * \param rbar Value for RBAR register.
  241. * \param rlar Value for RLAR register.
  242. */
  243. __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
  244. {
  245. ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
  246. }
  247. #endif
  248. /** Memcopy with strictly ordered memory access, e.g. for register targets.
  249. * \param dst Destination data is copied to.
  250. * \param src Source data is copied from.
  251. * \param len Amount of data words to be copied.
  252. */
  253. __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
  254. {
  255. uint32_t i;
  256. for (i = 0U; i < len; ++i)
  257. {
  258. dst[i] = src[i];
  259. }
  260. }
  261. /** Load the given number of MPU regions from a table to the given MPU.
  262. * \param mpu Pointer to the MPU registers to be used.
  263. * \param rnr First region number to be configured.
  264. * \param table Pointer to the MPU configuration table.
  265. * \param cnt Amount of regions to be configured.
  266. */
  267. __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
  268. {
  269. const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
  270. if (cnt == 1U) {
  271. mpu->RNR = rnr;
  272. ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
  273. } else {
  274. uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
  275. uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
  276. mpu->RNR = rnrBase;
  277. while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
  278. uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
  279. ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
  280. table += c;
  281. cnt -= c;
  282. rnrOffset = 0U;
  283. rnrBase += MPU_TYPE_RALIASES;
  284. mpu->RNR = rnrBase;
  285. }
  286. ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
  287. }
  288. }
  289. /** Load the given number of MPU regions from a table.
  290. * \param rnr First region number to be configured.
  291. * \param table Pointer to the MPU configuration table.
  292. * \param cnt Amount of regions to be configured.
  293. */
  294. __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
  295. {
  296. ARM_MPU_LoadEx(MPU, rnr, table, cnt);
  297. }
  298. #ifdef MPU_NS
  299. /** Load the given number of MPU regions from a table to the Non-secure MPU.
  300. * \param rnr First region number to be configured.
  301. * \param table Pointer to the MPU configuration table.
  302. * \param cnt Amount of regions to be configured.
  303. */
  304. __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
  305. {
  306. ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
  307. }
  308. #endif
  309. #endif