mpu_armv8.h 11 KB

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