mpu_armv8.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /******************************************************************************
  2. * @file mpu_armv8.h
  3. * @brief CMSIS MPU API for Armv8-M and Armv8.1-M MPU
  4. * @version V5.9.0
  5. * @date 11. April 2023
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2017-2022 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 cacheability.
  36. * \param NT Non-Transient: Set to 1 for Non-transient data. Set to 0 for Transient data.
  37. * \param WB Write-Back: Set to 1 to use a Write-Back policy. Set to 0 to use a Write-Through policy.
  38. * \param RA Read Allocation: Set to 1 to enable cache allocation on read miss. Set to 0 to disable cache allocation on read miss.
  39. * \param WA Write Allocation: Set to 1 to enable cache allocation on write miss. Set to 0 to disable 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 Normal memory outer-cacheable and inner-cacheable attributes
  52. * WT = Write Through, WB = Write Back, TR = Transient, RA = Read-Allocate, WA = Write Allocate
  53. */
  54. #define MPU_ATTR_NORMAL_OUTER_NON_CACHEABLE (0b0100)
  55. #define MPU_ATTR_NORMAL_OUTER_WT_TR_RA (0b0010)
  56. #define MPU_ATTR_NORMAL_OUTER_WT_TR_WA (0b0001)
  57. #define MPU_ATTR_NORMAL_OUTER_WT_TR_RA_WA (0b0011)
  58. #define MPU_ATTR_NORMAL_OUTER_WT_RA (0b1010)
  59. #define MPU_ATTR_NORMAL_OUTER_WT_WA (0b1001)
  60. #define MPU_ATTR_NORMAL_OUTER_WT_RA_WA (0b1011)
  61. #define MPU_ATTR_NORMAL_OUTER_WB_TR_RA (0b0101)
  62. #define MPU_ATTR_NORMAL_OUTER_WB_TR_WA (0b0110)
  63. #define MPU_ATTR_NORMAL_OUTER_WB_TR_RA_WA (0b0111)
  64. #define MPU_ATTR_NORMAL_OUTER_WB_RA (0b1101)
  65. #define MPU_ATTR_NORMAL_OUTER_WB_WA (0b1110)
  66. #define MPU_ATTR_NORMAL_OUTER_WB_RA_WA (0b1111)
  67. #define MPU_ATTR_NORMAL_INNER_NON_CACHEABLE (0b0100)
  68. #define MPU_ATTR_NORMAL_INNER_WT_TR_RA (0b0010)
  69. #define MPU_ATTR_NORMAL_INNER_WT_TR_WA (0b0001)
  70. #define MPU_ATTR_NORMAL_INNER_WT_TR_RA_WA (0b0011)
  71. #define MPU_ATTR_NORMAL_INNER_WT_RA (0b1010)
  72. #define MPU_ATTR_NORMAL_INNER_WT_WA (0b1001)
  73. #define MPU_ATTR_NORMAL_INNER_WT_RA_WA (0b1011)
  74. #define MPU_ATTR_NORMAL_INNER_WB_TR_RA (0b0101)
  75. #define MPU_ATTR_NORMAL_INNER_WB_TR_WA (0b0110)
  76. #define MPU_ATTR_NORMAL_INNER_WB_TR_RA_WA (0b0111)
  77. #define MPU_ATTR_NORMAL_INNER_WB_RA (0b1101)
  78. #define MPU_ATTR_NORMAL_INNER_WB_WA (0b1110)
  79. #define MPU_ATTR_NORMAL_INNER_WB_RA_WA (0b1111)
  80. /** \brief Memory Attribute
  81. * \param O Outer memory attributes
  82. * \param I O == ARM_MPU_ATTR_DEVICE: Device memory attributes, else: Inner memory attributes
  83. */
  84. #define ARM_MPU_ATTR(O, I) ((((O) & 0xFU) << 4U) | ((((O) & 0xFU) != 0U) ? ((I) & 0xFU) : (((I) & 0x3U) << 2U)))
  85. /* \brief Specifies MAIR_ATTR number */
  86. #define MAIR_ATTR(x) ((x > 7 || x < 0) ? 0 : x)
  87. /**
  88. * Shareability
  89. */
  90. /** \brief Normal memory, non-shareable */
  91. #define ARM_MPU_SH_NON (0U)
  92. /** \brief Normal memory, outer shareable */
  93. #define ARM_MPU_SH_OUTER (2U)
  94. /** \brief Normal memory, inner shareable */
  95. #define ARM_MPU_SH_INNER (3U)
  96. /**
  97. * Access permissions
  98. * AP = Access permission, RO = Read-only, RW = Read/Write, NP = Any privilege, PO = Privileged code only
  99. */
  100. /** \brief Normal memory, read/write */
  101. #define ARM_MPU_AP_RW (0U)
  102. /** \brief Normal memory, read-only */
  103. #define ARM_MPU_AP_RO (1U)
  104. /** \brief Normal memory, any privilege level */
  105. #define ARM_MPU_AP_NP (1U)
  106. /** \brief Normal memory, privileged access only */
  107. #define ARM_MPU_AP_PO (0U)
  108. /*
  109. * Execute-never
  110. * XN = Execute-never, EX = Executable
  111. */
  112. /** \brief Normal memory, Execution only permitted if read permitted */
  113. #define ARM_MPU_XN (1U)
  114. /** \brief Normal memory, Execution only permitted if read permitted */
  115. #define ARM_MPU_EX (0U)
  116. /** \brief Memory access permissions
  117. * \param RO Read-Only: Set to 1 for read-only memory. Set to 0 for a read/write memory.
  118. * \param NP Non-Privileged: Set to 1 for non-privileged memory. Set to 0 for privileged memory.
  119. */
  120. #define ARM_MPU_AP_(RO, NP) ((((RO) & 1U) << 1U) | ((NP) & 1U))
  121. /** \brief Region Base Address Register value
  122. * \param BASE The base address bits [31:5] of a memory region. The value is zero extended. Effective address gets 32 byte aligned.
  123. * \param SH Defines the Shareability domain for this memory region.
  124. * \param RO Read-Only: Set to 1 for a read-only memory region. Set to 0 for a read/write memory region.
  125. * \param NP Non-Privileged: Set to 1 for a non-privileged memory region. Set to 0 for privileged memory region.
  126. * \param XN eXecute Never: Set to 1 for a non-executable memory region. Set to 0 for an executable memory region.
  127. */
  128. #define ARM_MPU_RBAR(BASE, SH, RO, NP, XN) \
  129. (((BASE) & MPU_RBAR_BASE_Msk) | \
  130. (((SH) << MPU_RBAR_SH_Pos) & MPU_RBAR_SH_Msk) | \
  131. ((ARM_MPU_AP_(RO, NP) << MPU_RBAR_AP_Pos) & MPU_RBAR_AP_Msk) | \
  132. (((XN) << MPU_RBAR_XN_Pos) & MPU_RBAR_XN_Msk))
  133. /** \brief Region Limit Address Register value
  134. * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
  135. * \param IDX The attribute index to be associated with this memory region.
  136. */
  137. #define ARM_MPU_RLAR(LIMIT, IDX) \
  138. (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
  139. (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
  140. (MPU_RLAR_EN_Msk))
  141. #if defined(MPU_RLAR_PXN_Pos)
  142. /** \brief Region Limit Address Register with PXN value
  143. * \param LIMIT The limit address bits [31:5] for this memory region. The value is one extended.
  144. * \param PXN Privileged execute never. Defines whether code can be executed from this privileged region.
  145. * \param IDX The attribute index to be associated with this memory region.
  146. */
  147. #define ARM_MPU_RLAR_PXN(LIMIT, PXN, IDX) \
  148. (((LIMIT) & MPU_RLAR_LIMIT_Msk) | \
  149. (((PXN) << MPU_RLAR_PXN_Pos) & MPU_RLAR_PXN_Msk) | \
  150. (((IDX) << MPU_RLAR_AttrIndx_Pos) & MPU_RLAR_AttrIndx_Msk) | \
  151. (MPU_RLAR_EN_Msk))
  152. #endif
  153. /**
  154. * Struct for a single MPU Region
  155. */
  156. typedef struct {
  157. uint32_t RBAR; /*!< Region Base Address Register value */
  158. uint32_t RLAR; /*!< Region Limit Address Register value */
  159. } ARM_MPU_Region_t;
  160. /**
  161. \brief Read MPU Type Register
  162. \return Number of MPU regions
  163. */
  164. __STATIC_INLINE uint32_t ARM_MPU_TYPE()
  165. {
  166. return ((MPU->TYPE) >> 8);
  167. }
  168. /** Enable the MPU.
  169. * \param MPU_Control Default access permissions for unconfigured regions.
  170. */
  171. __STATIC_INLINE void ARM_MPU_Enable(uint32_t MPU_Control)
  172. {
  173. __DMB();
  174. MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
  175. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  176. SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
  177. #endif
  178. __DSB();
  179. __ISB();
  180. }
  181. /** Disable the MPU.
  182. */
  183. __STATIC_INLINE void ARM_MPU_Disable(void)
  184. {
  185. __DMB();
  186. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  187. SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
  188. #endif
  189. MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
  190. __DSB();
  191. __ISB();
  192. }
  193. #ifdef MPU_NS
  194. /** Enable the Non-secure MPU.
  195. * \param MPU_Control Default access permissions for unconfigured regions.
  196. */
  197. __STATIC_INLINE void ARM_MPU_Enable_NS(uint32_t MPU_Control)
  198. {
  199. __DMB();
  200. MPU_NS->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
  201. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  202. SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
  203. #endif
  204. __DSB();
  205. __ISB();
  206. }
  207. /** Disable the Non-secure MPU.
  208. */
  209. __STATIC_INLINE void ARM_MPU_Disable_NS(void)
  210. {
  211. __DMB();
  212. #ifdef SCB_SHCSR_MEMFAULTENA_Msk
  213. SCB_NS->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
  214. #endif
  215. MPU_NS->CTRL &= ~MPU_CTRL_ENABLE_Msk;
  216. __DSB();
  217. __ISB();
  218. }
  219. #endif
  220. /** Set the memory attribute encoding to the given MPU.
  221. * \param mpu Pointer to the MPU to be configured.
  222. * \param idx The attribute index to be set [0-7]
  223. * \param attr The attribute value to be set.
  224. */
  225. __STATIC_INLINE void ARM_MPU_SetMemAttrEx(MPU_Type* mpu, uint8_t idx, uint8_t attr)
  226. {
  227. const uint8_t reg = idx / 4U;
  228. const uint32_t pos = ((idx % 4U) * 8U);
  229. const uint32_t mask = 0xFFU << pos;
  230. const uint32_t val = (uint32_t)attr << pos;
  231. if (reg >= (sizeof(mpu->MAIR) / sizeof(mpu->MAIR[0]))) {
  232. return; // invalid index
  233. }
  234. mpu->MAIR[reg] = ((mpu->MAIR[reg] & ~mask) | (val & mask));
  235. }
  236. /** Set the memory attribute encoding.
  237. * \param idx The attribute index to be set [0-7]
  238. * \param attr The attribute value to be set.
  239. */
  240. __STATIC_INLINE void ARM_MPU_SetMemAttr(uint8_t idx, uint8_t attr)
  241. {
  242. ARM_MPU_SetMemAttrEx(MPU, idx, attr);
  243. }
  244. #ifdef MPU_NS
  245. /** Set the memory attribute encoding to the Non-secure MPU.
  246. * \param idx The attribute index to be set [0-7]
  247. * \param attr The attribute value to be set.
  248. */
  249. __STATIC_INLINE void ARM_MPU_SetMemAttr_NS(uint8_t idx, uint8_t attr)
  250. {
  251. ARM_MPU_SetMemAttrEx(MPU_NS, idx, attr);
  252. }
  253. #endif
  254. /** Clear and disable the given MPU region of the given MPU.
  255. * \param mpu Pointer to MPU to be used.
  256. * \param rnr Region number to be cleared.
  257. */
  258. __STATIC_INLINE void ARM_MPU_ClrRegionEx(MPU_Type* mpu, uint32_t rnr)
  259. {
  260. mpu->RNR = rnr;
  261. mpu->RLAR = 0U;
  262. }
  263. /** Clear and disable the given MPU region.
  264. * \param rnr Region number to be cleared.
  265. */
  266. __STATIC_INLINE void ARM_MPU_ClrRegion(uint32_t rnr)
  267. {
  268. ARM_MPU_ClrRegionEx(MPU, rnr);
  269. }
  270. #ifdef MPU_NS
  271. /** Clear and disable the given Non-secure MPU region.
  272. * \param rnr Region number to be cleared.
  273. */
  274. __STATIC_INLINE void ARM_MPU_ClrRegion_NS(uint32_t rnr)
  275. {
  276. ARM_MPU_ClrRegionEx(MPU_NS, rnr);
  277. }
  278. #endif
  279. /** Configure the given MPU region of the given MPU.
  280. * \param mpu Pointer to MPU to be used.
  281. * \param rnr Region number to be configured.
  282. * \param rbar Value for RBAR register.
  283. * \param rlar Value for RLAR register.
  284. */
  285. __STATIC_INLINE void ARM_MPU_SetRegionEx(MPU_Type* mpu, uint32_t rnr, uint32_t rbar, uint32_t rlar)
  286. {
  287. mpu->RNR = rnr;
  288. mpu->RBAR = rbar;
  289. mpu->RLAR = rlar;
  290. }
  291. /** Configure the given MPU region.
  292. * \param rnr Region number to be configured.
  293. * \param rbar Value for RBAR register.
  294. * \param rlar Value for RLAR register.
  295. */
  296. __STATIC_INLINE void ARM_MPU_SetRegion(uint32_t rnr, uint32_t rbar, uint32_t rlar)
  297. {
  298. ARM_MPU_SetRegionEx(MPU, rnr, rbar, rlar);
  299. }
  300. #ifdef MPU_NS
  301. /** Configure the given Non-secure MPU region.
  302. * \param rnr Region number to be configured.
  303. * \param rbar Value for RBAR register.
  304. * \param rlar Value for RLAR register.
  305. */
  306. __STATIC_INLINE void ARM_MPU_SetRegion_NS(uint32_t rnr, uint32_t rbar, uint32_t rlar)
  307. {
  308. ARM_MPU_SetRegionEx(MPU_NS, rnr, rbar, rlar);
  309. }
  310. #endif
  311. /** Memcpy with strictly ordered memory access, e.g. used by code in ARM_MPU_LoadEx()
  312. * \param dst Destination data is copied to.
  313. * \param src Source data is copied from.
  314. * \param len Amount of data words to be copied.
  315. */
  316. __STATIC_INLINE void ARM_MPU_OrderedMemcpy(volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
  317. {
  318. uint32_t i;
  319. for (i = 0U; i < len; ++i)
  320. {
  321. dst[i] = src[i];
  322. }
  323. }
  324. /** Load the given number of MPU regions from a table to the given MPU.
  325. * \param mpu Pointer to the MPU registers to be used.
  326. * \param rnr First region number to be configured.
  327. * \param table Pointer to the MPU configuration table.
  328. * \param cnt Amount of regions to be configured.
  329. */
  330. __STATIC_INLINE void ARM_MPU_LoadEx(MPU_Type* mpu, uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
  331. {
  332. const uint32_t rowWordSize = sizeof(ARM_MPU_Region_t)/4U;
  333. if (cnt == 1U) {
  334. mpu->RNR = rnr;
  335. ARM_MPU_OrderedMemcpy(&(mpu->RBAR), &(table->RBAR), rowWordSize);
  336. } else {
  337. uint32_t rnrBase = rnr & ~(MPU_TYPE_RALIASES-1U);
  338. uint32_t rnrOffset = rnr % MPU_TYPE_RALIASES;
  339. mpu->RNR = rnrBase;
  340. while ((rnrOffset + cnt) > MPU_TYPE_RALIASES) {
  341. uint32_t c = MPU_TYPE_RALIASES - rnrOffset;
  342. ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), c*rowWordSize);
  343. table += c;
  344. cnt -= c;
  345. rnrOffset = 0U;
  346. rnrBase += MPU_TYPE_RALIASES;
  347. mpu->RNR = rnrBase;
  348. }
  349. ARM_MPU_OrderedMemcpy(&(mpu->RBAR)+(rnrOffset*2U), &(table->RBAR), cnt*rowWordSize);
  350. }
  351. }
  352. /** Load the given number of MPU regions from a table.
  353. * \param rnr First region number to be configured.
  354. * \param table Pointer to the MPU configuration table.
  355. * \param cnt Amount of regions to be configured.
  356. */
  357. __STATIC_INLINE void ARM_MPU_Load(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
  358. {
  359. ARM_MPU_LoadEx(MPU, rnr, table, cnt);
  360. }
  361. #ifdef MPU_NS
  362. /** Load the given number of MPU regions from a table to the Non-secure MPU.
  363. * \param rnr First region number to be configured.
  364. * \param table Pointer to the MPU configuration table.
  365. * \param cnt Amount of regions to be configured.
  366. */
  367. __STATIC_INLINE void ARM_MPU_Load_NS(uint32_t rnr, ARM_MPU_Region_t const* table, uint32_t cnt)
  368. {
  369. ARM_MPU_LoadEx(MPU_NS, rnr, table, cnt);
  370. }
  371. #endif
  372. #endif