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.0
  5. * @date 08. March 2019
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2017-2019 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. {
  104. uint32_t RBAR; /*!< Region Base Address Register value */
  105. uint32_t RLAR; /*!< Region Limit Address Register value */
  106. } ARM_MPU_Region_t;
  107. /** Enable the MPU.
  108. * \param MPU_Control Default access permissions for unconfigured regions.
  109. */
  110. __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
  111. {
  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. }
  129. #ifdef MPU_NS
  130. /** Enable the Non-secure MPU.
  131. * \param MPU_Control Default access permissions for unconfigured regions.
  132. */
  133. __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
  134. {
  135. MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
  136. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  137. SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
  138. #endif
  139. __DSB();
  140. __ISB();
  141. }
  142. /** Disable the Non-secure MPU.
  143. */
  144. __STATIC_INLINE void ARM_MPU_Disable_NS(void)
  145. {
  146. __DMB();
  147. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  148. SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
  149. #endif
  150. MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
  151. }
  152. #endif
  153. /** Set the memory attribute encoding to the given MPU.
  154. * \param mpu Pointer to the MPU to be configured.
  155. * \param idx The attribute index to be set [0-7]
  156. * \param attr The attribute value to be set.
  157. */
  158. __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type *mpu, uint8_t idx, uint8_t attr)
  159. {
  160. const uint8_t reg = idx / 4U;
  161. const uint32_t pos = ((idx % 4U) * 8U);
  162. const uint32_t mask = 0xFFU << pos;
  163. if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0])))
  164. {
  165. return; // invalid index
  166. }
  167. mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | ((attr << pos) & mask));
  168. }
  169. /** Set the memory attribute encoding.
  170. * \param idx The attribute index to be set [0-7]
  171. * \param attr The attribute value to be set.
  172. */
  173. __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
  174. {
  175. ARM_MPU_SetMemAttrEx(MPU, idx, attr);
  176. }
  177. #ifdef MPU_NS
  178. /** Set the memory attribute encoding to the Non-secure MPU.
  179. * \param idx The attribute index to be set [0-7]
  180. * \param attr The attribute value to be set.
  181. */
  182. __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
  183. {
  184. ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
  185. }
  186. #endif
  187. /** Clear and disable the given MPU region of the given MPU.
  188. * \param mpu Pointer to MPU to be used.
  189. * \param rnr Region number to be cleared.
  190. */
  191. __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type *mpu, uint32_t rnr)
  192. {
  193. mpu->RNR = rnr;
  194. mpu->RLAR = 0U;
  195. }
  196. /** Clear and disable the given MPU region.
  197. * \param rnr Region number to be cleared.
  198. */
  199. __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
  200. {
  201. ARM_MPU_ClrRegionEx(MPU, rnr);
  202. }
  203. #ifdef MPU_NS
  204. /** Clear and disable the given Non-secure MPU region.
  205. * \param rnr Region number to be cleared.
  206. */
  207. __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
  208. {
  209. ARM_MPU_ClrRegionEx(MPU_NS, rnr);
  210. }
  211. #endif
  212. /** Configure the given MPU region of the given MPU.
  213. * \param mpu Pointer to MPU to be used.
  214. * \param rnr Region number to be configured.
  215. * \param rbar Value for RBAR register.
  216. * \param rlar Value for RLAR register.
  217. */
  218. __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type *mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
  219. {
  220. mpu->RNR = rnr;
  221. mpu->RBAR = rbar;
  222. mpu->RLAR = rlar;
  223. }
  224. /** Configure the given MPU region.
  225. * \param rnr Region number to be configured.
  226. * \param rbar Value for RBAR register.
  227. * \param rlar Value for RLAR register.
  228. */
  229. __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
  230. {
  231. ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
  232. }
  233. #ifdef MPU_NS
  234. /** Configure the given Non-secure MPU region.
  235. * \param rnr Region number to be configured.
  236. * \param rbar Value for RBAR register.
  237. * \param rlar Value for RLAR register.
  238. */
  239. __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
  240. {
  241. ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
  242. }
  243. #endif
  244. /** Memcopy with strictly ordered memory access, e.g. for register targets.
  245. * \param dst Destination data is copied to.
  246. * \param src Source data is copied from.
  247. * \param len Amount of data words to be copied.
  248. */
  249. __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t *dst, const uint32_t *__RESTRICT src, uint32_t len)
  250. {
  251. uint32_t i;
  252. for (i = 0U; i < len; ++i)
  253. {
  254. dst[i] = src[i];
  255. }
  256. }
  257. /** Load the given number of MPU regions from a table to the given MPU.
  258. * \param mpu Pointer to the MPU registers to be used.
  259. * \param rnr First region number to be configured.
  260. * \param table Pointer to the MPU configuration table.
  261. * \param cnt Amount of regions to be configured.
  262. */
  263. __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type *mpu, uint32_t rnr, ARM_MPU_Region_t const *table, uint32_t cnt)
  264. {
  265. const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t) / 4U;
  266. if (cnt == 1U)
  267. {
  268. mpu->RNR = rnr;
  269. ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
  270. }
  271. else
  272. {
  273. uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES - 1U);
  274. uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
  275. mpu->RNR = rnrBase;
  276. while ((rnrOffset + cnt) > MPU_TYPE_RALIASES)
  277. {
  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