nrf_mpu_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdint.h>
  41. #include "nrf.h"
  42. #include "nrf_mpu_lib.h"
  43. #include "app_util_platform.h"
  44. #include "sdk_config.h"
  45. #if NRF_MPU_LIB_CLI_CMDS
  46. #include "nrf_cli.h"
  47. #endif
  48. #define NRF_LOG_MODULE_NAME nrf_mpu_lib
  49. #if NRF_MPU_LIB_CONFIG_LOG_ENABLED
  50. #define NRF_LOG_LEVEL NRF_MPU_LIB_CONFIG_LOG_LEVEL
  51. #define NRF_LOG_INFO_COLOR NRF_MPU_LIB_CONFIG_INFO_COLOR
  52. #define NRF_LOG_DEBUG_COLOR NRF_MPU_LIB_CONFIG_DEBUG_COLOR
  53. #else
  54. #define NRF_LOG_LEVEL 0
  55. #endif
  56. #include "nrf_log.h"
  57. NRF_LOG_MODULE_REGISTER();
  58. // Check module requirements.
  59. STATIC_ASSERT(__MPU_PRESENT);
  60. STATIC_ASSERT(__CORTEX_M == 4);
  61. /**@brief Return the number of unified regions avaiable in MPU. */
  62. __STATIC_INLINE unsigned int nrf_mpu_lib_get_number_of_regions(void)
  63. {
  64. return (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
  65. }
  66. /**@brief Extract human-readable memory type from attributes. */
  67. static const char *nrf_mpu_lib_mem_type(uint32_t attributes)
  68. {
  69. attributes &= MPU_RASR_TEX_Msk | MPU_RASR_C_Msk | MPU_RASR_B_Msk | MPU_RASR_S_Msk;
  70. switch (attributes)
  71. {
  72. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  73. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  74. return "Strongly-ordered, Shareable";
  75. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  76. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  77. return "Device, Shareable";
  78. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  79. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  80. case (0x01 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  81. case (0x01 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  82. return "Normal";
  83. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  84. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  85. case (0x01 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  86. case (0x01 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  87. return "Normal, Shareable";
  88. case (0x02 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  89. case (0x02 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  90. return "Device";
  91. default:
  92. if (((attributes & MPU_RASR_TEX_Msk) >> MPU_RASR_TEX_Pos) >= 0x04)
  93. {
  94. return (attributes & MPU_RASR_S_Msk) ? "Normal, Shareable" : "Normal";
  95. }
  96. else
  97. {
  98. return "Reserved or Implementation Defined";
  99. }
  100. }
  101. // Not reached.
  102. }
  103. /**@brief Extract human-readable access level from attributes. */
  104. static const char *nrf_mpu_lib_mem_access(uint32_t attributes)
  105. {
  106. switch ((attributes & MPU_RASR_AP_Msk) >> MPU_RASR_AP_Pos)
  107. {
  108. case 0x00:
  109. return "--/--";
  110. case 0x01:
  111. return "RW/--";
  112. case 0x02:
  113. return "RW/RO";
  114. case 0x03:
  115. return "RW/RW";
  116. case 0x04:
  117. return "\?\?/\?\?";
  118. case 0x05:
  119. return "RO/--";
  120. case 0x06:
  121. case 0x07:
  122. return "RO/RO";
  123. default:
  124. return "Unknown";
  125. }
  126. // Not reached.
  127. }
  128. ret_code_t nrf_mpu_lib_init(void)
  129. {
  130. unsigned int i, regions;
  131. // Make sure that MPU uses unified regions. Separate are not supported.
  132. if ((MPU->TYPE & MPU_TYPE_SEPARATE_Msk) >> MPU_TYPE_SEPARATE_Pos)
  133. {
  134. NRF_LOG_ERROR("Non-unified MPU is not supported!");
  135. return NRF_ERROR_NOT_SUPPORTED;
  136. }
  137. // Make sure that we can handle all avaiable regions.
  138. regions = nrf_mpu_lib_get_number_of_regions();
  139. if (regions > ((1 << (8 * sizeof(nrf_mpu_lib_region_t))) - 1))
  140. {
  141. NRF_LOG_ERROR("MPU with %u regions is not supported!", regions);
  142. return NRF_ERROR_NOT_SUPPORTED;
  143. }
  144. // Disable MPU.
  145. MPU->CTRL = 0;
  146. // Clear all regions.
  147. for (i = 0; i < regions; i++)
  148. {
  149. MPU->RNR = i;
  150. MPU->RASR = 0;
  151. MPU->RBAR = 0;
  152. }
  153. /* Enable MPU */
  154. MPU->CTRL = (1 << MPU_CTRL_ENABLE_Pos) |
  155. (1 << MPU_CTRL_PRIVDEFENA_Pos);
  156. // Make sure that configuration changes are active.
  157. __DSB();
  158. NRF_LOG_INFO("MPU Initalized. %u unified regions avaiable.", regions);
  159. return NRF_SUCCESS;
  160. }
  161. ret_code_t nrf_mpu_lib_region_create(nrf_mpu_lib_region_t *p_region,
  162. void *address,
  163. size_t size,
  164. uint32_t attributes)
  165. {
  166. unsigned long addr = (unsigned long)(address);
  167. unsigned int i, regions = nrf_mpu_lib_get_number_of_regions();
  168. // Size must be power of 2, greater than or equal to 32
  169. if ((size & (size - 1)) || (size < 32))
  170. {
  171. NRF_LOG_ERROR("Cannot create MPU region 0x%08X-0x%08X: Region size is not valid!", addr, addr + size - 1);
  172. return NRF_ERROR_INVALID_PARAM;
  173. }
  174. // Region must be aligned to it's size.
  175. if (addr & (size - 1))
  176. {
  177. NRF_LOG_ERROR("Cannot create MPU region 0x%08X-0x%08X: Region alignment is not valid!", addr, addr + size - 1);
  178. return NRF_ERROR_INVALID_PARAM;
  179. }
  180. // Check if attributes are valid.
  181. if (attributes & ~MPU_RASR_ATTRS_Msk)
  182. {
  183. NRF_LOG_ERROR("Cannot create MPU region 0x%08X-0x%08X: Region attributes are not valid!", addr, addr + size - 1);
  184. return NRF_ERROR_INVALID_PARAM;
  185. }
  186. NRF_LOG_DEBUG("MPU region creating (location: 0x%08X-0x%08X)",
  187. addr,
  188. addr + size - 1);
  189. // Create region.
  190. CRITICAL_REGION_ENTER();
  191. for (i = 0; i < regions; i++)
  192. {
  193. MPU->RNR = i;
  194. if (MPU->RASR & MPU_RASR_ENABLE_Msk)
  195. {
  196. continue;
  197. }
  198. MPU->RBAR = addr;
  199. MPU->RASR = attributes |
  200. (1 << MPU_RASR_ENABLE_Pos) |
  201. ((31 - __CLZ(size) - 1) << MPU_RASR_SIZE_Pos);
  202. break;
  203. }
  204. CRITICAL_REGION_EXIT();
  205. if (i >= regions)
  206. {
  207. NRF_LOG_ERROR("Cannot create MPU region 0x%08X-0x%08X: No free region found!", addr, addr + size - 1);
  208. return NRF_ERROR_NO_MEM;
  209. }
  210. // Apply changes.
  211. __DSB();
  212. // Save region number;
  213. *p_region = i;
  214. NRF_LOG_DEBUG("MPU region %u created (location: 0x%08X-0x%08X, access: %s, type: %s, flags: %s).",
  215. i,
  216. addr,
  217. addr + size - 1,
  218. (uint32_t)nrf_mpu_lib_mem_access(attributes),
  219. (uint32_t)nrf_mpu_lib_mem_type(attributes),
  220. (uint32_t)((attributes & MPU_RASR_XN_Msk) ? "XN" : "--"));
  221. return NRF_SUCCESS;
  222. }
  223. ret_code_t nrf_mpu_lib_region_destroy(nrf_mpu_lib_region_t region)
  224. {
  225. if (region >= nrf_mpu_lib_get_number_of_regions())
  226. {
  227. NRF_LOG_ERROR("Cannot destroy MPU region %u: Invaid region!", region);
  228. return NRF_ERROR_INVALID_PARAM;
  229. }
  230. CRITICAL_REGION_ENTER();
  231. MPU->RNR = region;
  232. MPU->RASR = 0;
  233. MPU->RBAR = 0;
  234. CRITICAL_REGION_EXIT();
  235. // Apply changes.
  236. __DSB();
  237. NRF_LOG_DEBUG("MPU region %u destroyed.", region);
  238. return NRF_SUCCESS;
  239. }
  240. #if NRF_MPU_LIB_CLI_CMDS
  241. /**@brief Extract human-readable caching policy from attributes. */
  242. static const char *nrf_mpu_lib_mem_caching(uint32_t attributes)
  243. {
  244. static const char *caching[] =
  245. {
  246. "--/--", "WBWA/--", "WT/--", "WB/--",
  247. "--/WBWA", "WBWA/WBWA","WT/WBWA", "WB/WBWA",
  248. "--/WT", "WBWA/WT", "WT/WT", "WB/WT",
  249. "--/WB", "WBWA/WB", "WT/WB", "WB/WB",
  250. };
  251. uint32_t tex = (attributes & MPU_RASR_TEX_Msk) >> MPU_RASR_TEX_Pos;
  252. uint32_t c = (attributes & MPU_RASR_C_Msk) >> MPU_RASR_C_Pos;
  253. uint32_t b = (attributes & MPU_RASR_B_Msk) >> MPU_RASR_B_Pos;
  254. attributes &= MPU_RASR_TEX_Msk | MPU_RASR_C_Msk | MPU_RASR_B_Msk | MPU_RASR_S_Msk;
  255. switch (attributes)
  256. {
  257. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  258. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  259. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  260. case (0x00 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  261. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  262. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  263. case (0x01 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  264. case (0x01 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  265. case (0x01 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  266. case (0x02 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  267. case (0x02 << MPU_RASR_TEX_Pos) | (0 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (1 << MPU_RASR_S_Pos):
  268. return "--/--";
  269. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (0 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  270. return "WT/WT";
  271. case (0x00 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  272. return "WB/WB";
  273. case (0x01 << MPU_RASR_TEX_Pos) | (1 << MPU_RASR_C_Pos) | (1 << MPU_RASR_B_Pos) | (0 << MPU_RASR_S_Pos):
  274. return "WBWA/WBWA";
  275. default:
  276. if (tex >= 0x04)
  277. {
  278. return caching[((tex & 0x03) << 2) | ((c & 0x01) << 1) | (b & 0x01)];
  279. }
  280. break;
  281. }
  282. return "Unknown";
  283. }
  284. /**@brief Implementation of "mpu info" command. */
  285. static void nrf_mpu_lib_cmd_info(nrf_cli_t const *p_cli, size_t argc, char **argv)
  286. {
  287. if (nrf_cli_help_requested(p_cli))
  288. {
  289. nrf_cli_help_print(p_cli, NULL, 0);
  290. return;
  291. }
  292. unsigned int i, regions = nrf_mpu_lib_get_number_of_regions();
  293. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "MPU State: %s, %u unified regions aviable.\r\n\r\n",
  294. (MPU->CTRL & MPU_CTRL_ENABLE_Msk) ? "Enabled" : "Disabled",
  295. regions);
  296. for (i = 0; i < regions; i++)
  297. {
  298. uint32_t rasr, rbar;
  299. uint32_t size;
  300. CRITICAL_REGION_ENTER();
  301. MPU->RNR = i;
  302. rasr = MPU->RASR;
  303. rbar = MPU->RBAR;
  304. CRITICAL_REGION_EXIT();
  305. if ((rasr & MPU_RASR_ENABLE_Msk) == 0)
  306. {
  307. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "Region %u: Disabled\r\n", i);
  308. continue;
  309. }
  310. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "Region %u: Enabled\r\n", i);
  311. size = 1ul << (((rasr & MPU_RASR_SIZE_Msk) >> MPU_RASR_SIZE_Pos) + 1);
  312. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\t- Location:\t0x%08X-0x%08X (size: %u bytes)\r\n",
  313. rbar,
  314. rbar + size - 1,
  315. size);
  316. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\t- Access:\t%s\r\n", nrf_mpu_lib_mem_access(rasr));
  317. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\t- Type:\t\t%s\r\n", nrf_mpu_lib_mem_type(rasr));
  318. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\t- Caching:\t%s\r\n", nrf_mpu_lib_mem_caching(rasr));
  319. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\t- Flags:\t%s\r\n\r\n", (rasr & MPU_RASR_XN_Msk) ? "XN" : "--");
  320. }
  321. }
  322. /**@brief Implementation of "mpu dump" command. */
  323. static void nrf_mpu_lib_cmd_dump(nrf_cli_t const *p_cli, size_t argc, char **argv)
  324. {
  325. unsigned int i, regions = nrf_mpu_lib_get_number_of_regions();
  326. if (nrf_cli_help_requested(p_cli))
  327. {
  328. nrf_cli_help_print(p_cli, NULL, 0);
  329. return;
  330. }
  331. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "MPU_TYPE:\t0x%08X\r\n", MPU->TYPE);
  332. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "MPU_CTRL:\t0x%08X\r\n", MPU->CTRL);
  333. for (i = 0; i < regions; i++)
  334. {
  335. uint32_t rasr, rbar;
  336. CRITICAL_REGION_ENTER();
  337. MPU->RNR = i;
  338. rasr = MPU->RASR;
  339. rbar = MPU->RBAR;
  340. CRITICAL_REGION_EXIT();
  341. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "\r\nMPU_RBAR[%u]:\t0x%08X\r\n", i, rbar);
  342. nrf_cli_fprintf(p_cli, NRF_CLI_NORMAL, "MPU_RASR[%u]:\t0x%08X\r\n", i, rasr);
  343. }
  344. }
  345. /**@brief Implementation of "mpu" command. */
  346. static void nrf_mpu_lib_cmd_unknown(nrf_cli_t const * p_cli, size_t argc, char **argv)
  347. {
  348. if ((argc == 1) || nrf_cli_help_requested(p_cli))
  349. {
  350. nrf_cli_help_print(p_cli, NULL, 0);
  351. return;
  352. }
  353. nrf_cli_fprintf(p_cli, NRF_CLI_ERROR, "%s: unknown parameter: %s\r\n", argv[0], argv[1]);
  354. }
  355. // Register "mpu" command and it's subcommands in CLI.
  356. NRF_CLI_CREATE_STATIC_SUBCMD_SET(nrf_mpu_lib_commands)
  357. {
  358. NRF_CLI_CMD(dump, NULL, "Dump MPU registers.", nrf_mpu_lib_cmd_dump),
  359. NRF_CLI_CMD(info, NULL, "Print information about MPU state.", nrf_mpu_lib_cmd_info),
  360. NRF_CLI_SUBCMD_SET_END
  361. };
  362. NRF_CLI_CMD_REGISTER(mpu, &nrf_mpu_lib_commands, "Commands for MPU management", nrf_mpu_lib_cmd_unknown);
  363. #endif //NRF_MPU_LIB_CLI_CMDS