cpu.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include <stdint.h>
  8. #include <assert.h>
  9. #include "soc/soc.h"
  10. #include "soc/soc_caps.h"
  11. // TODO: IDF-5645
  12. #if CONFIG_IDF_TARGET_ESP32C6
  13. #include "soc/lp_aon_reg.h"
  14. #include "soc/pcr_reg.h"
  15. #define SYSTEM_CPU_PER_CONF_REG PCR_CPU_WAITI_CONF_REG
  16. #define SYSTEM_CPU_WAIT_MODE_FORCE_ON PCR_CPU_WAIT_MODE_FORCE_ON
  17. #else
  18. #include "soc/rtc_cntl_reg.h"
  19. #endif
  20. #include "hal/soc_hal.h"
  21. #include "hal/mpu_hal.h"
  22. #include "esp_bit_defs.h"
  23. #include "esp_attr.h"
  24. #include "esp_err.h"
  25. #include "esp_cpu.h"
  26. #include "esp_memory_utils.h"
  27. #include "esp_fault.h"
  28. #if __XTENSA__
  29. #include "xtensa/config/core-isa.h"
  30. #else
  31. #include "soc/system_reg.h" // For SYSTEM_CPU_PER_CONF_REG
  32. #include "soc/dport_access.h" // For Dport access
  33. #include "riscv/semihosting.h"
  34. #include "riscv/csr.h" // For PMP_ENTRY. [refactor-todo] create PMP abstraction in rv_utils.h
  35. #endif
  36. #if SOC_CPU_HAS_FLEXIBLE_INTC
  37. #include "riscv/instruction_decode.h"
  38. #endif
  39. /* --------------------------------------------------- CPU Control -----------------------------------------------------
  40. *
  41. * ------------------------------------------------------------------------------------------------------------------ */
  42. void esp_cpu_stall(int core_id)
  43. {
  44. assert(core_id >= 0 && core_id < SOC_CPU_CORES_NUM);
  45. #if SOC_CPU_CORES_NUM > 1 // We don't allow stalling of the current core
  46. /*
  47. We need to write the value "0x86" to stall a particular core. The write location is split into two separate
  48. bit fields named "c0" and "c1", and the two fields are located in different registers. Each core has its own pair of
  49. "c0" and "c1" bit fields.
  50. Note: This function can be called when the cache is disabled. We use "ternary if" instead of an array so that the
  51. "rodata" of the register masks/shifts will be stored in this function's "rodata" section, instead of the source
  52. file's "rodata" section (see IDF-5214).
  53. */
  54. int rtc_cntl_c0_m = (core_id == 0) ? RTC_CNTL_SW_STALL_PROCPU_C0_M : RTC_CNTL_SW_STALL_APPCPU_C0_M;
  55. int rtc_cntl_c0_s = (core_id == 0) ? RTC_CNTL_SW_STALL_PROCPU_C0_S : RTC_CNTL_SW_STALL_APPCPU_C0_S;
  56. int rtc_cntl_c1_m = (core_id == 0) ? RTC_CNTL_SW_STALL_PROCPU_C1_M : RTC_CNTL_SW_STALL_APPCPU_C1_M;
  57. int rtc_cntl_c1_s = (core_id == 0) ? RTC_CNTL_SW_STALL_PROCPU_C1_S : RTC_CNTL_SW_STALL_APPCPU_C1_S;
  58. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, rtc_cntl_c0_m);
  59. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, 2 << rtc_cntl_c0_s);
  60. CLEAR_PERI_REG_MASK(RTC_CNTL_SW_CPU_STALL_REG, rtc_cntl_c1_m);
  61. SET_PERI_REG_MASK(RTC_CNTL_SW_CPU_STALL_REG, 0x21 << rtc_cntl_c1_s);
  62. #endif
  63. }
  64. void esp_cpu_unstall(int core_id)
  65. {
  66. assert(core_id >= 0 && core_id < SOC_CPU_CORES_NUM);
  67. #if SOC_CPU_CORES_NUM > 1 // We don't allow stalling of the current core
  68. /*
  69. We need to write clear the value "0x86" to unstall a particular core. The location of this value is split into
  70. two separate bit fields named "c0" and "c1", and the two fields are located in different registers. Each core has
  71. its own pair of "c0" and "c1" bit fields.
  72. Note: This function can be called when the cache is disabled. We use "ternary if" instead of an array so that the
  73. "rodata" of the register masks/shifts will be stored in this function's "rodata" section, instead of the source
  74. file's "rodata" section (see IDF-5214).
  75. */
  76. int rtc_cntl_c0_m = (core_id == 0) ? RTC_CNTL_SW_STALL_PROCPU_C0_M : RTC_CNTL_SW_STALL_APPCPU_C0_M;
  77. int rtc_cntl_c1_m = (core_id == 0) ? RTC_CNTL_SW_STALL_PROCPU_C1_M : RTC_CNTL_SW_STALL_APPCPU_C1_M;
  78. CLEAR_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, rtc_cntl_c0_m);
  79. CLEAR_PERI_REG_MASK(RTC_CNTL_SW_CPU_STALL_REG, rtc_cntl_c1_m);
  80. #endif
  81. }
  82. void esp_cpu_reset(int core_id)
  83. {
  84. #if CONFIG_IDF_TARGET_ESP32C6 // TODO: IDF-5645
  85. SET_PERI_REG_MASK(LP_AON_CPUCORE0_CFG_REG, LP_AON_CPU_CORE0_SW_RESET);
  86. #else
  87. assert(core_id >= 0 && core_id < SOC_CPU_CORES_NUM);
  88. #if SOC_CPU_CORES_NUM > 1
  89. /*
  90. Note: This function can be called when the cache is disabled. We use "ternary if" instead of an array so that the
  91. "rodata" of the register masks/shifts will be stored in this function's "rodata" section, instead of the source
  92. file's "rodata" section (see IDF-5214).
  93. */
  94. int rtc_cntl_rst_m = (core_id == 0) ? RTC_CNTL_SW_PROCPU_RST_M : RTC_CNTL_SW_APPCPU_RST_M;
  95. #else // SOC_CPU_CORES_NUM > 1
  96. int rtc_cntl_rst_m = RTC_CNTL_SW_PROCPU_RST_M;
  97. #endif // SOC_CPU_CORES_NUM > 1
  98. SET_PERI_REG_MASK(RTC_CNTL_OPTIONS0_REG, rtc_cntl_rst_m);
  99. #endif
  100. }
  101. void esp_cpu_wait_for_intr(void)
  102. {
  103. #if __XTENSA__
  104. xt_utils_wait_for_intr();
  105. #else
  106. // TODO: IDF-5645 (better to implement with ll) C6 register names converted in the #include section at the top
  107. if (esp_cpu_dbgr_is_attached() && DPORT_REG_GET_BIT(SYSTEM_CPU_PER_CONF_REG, SYSTEM_CPU_WAIT_MODE_FORCE_ON) == 0) {
  108. /* when SYSTEM_CPU_WAIT_MODE_FORCE_ON is disabled in WFI mode SBA access to memory does not work for debugger,
  109. so do not enter that mode when debugger is connected */
  110. return;
  111. }
  112. rv_utils_wait_for_intr();
  113. #endif // __XTENSA__
  114. }
  115. /* -------------------------------------------------- CPU Registers ----------------------------------------------------
  116. *
  117. * ------------------------------------------------------------------------------------------------------------------ */
  118. /* ------------------------------------------------- CPU Interrupts ----------------------------------------------------
  119. *
  120. * ------------------------------------------------------------------------------------------------------------------ */
  121. // ---------------- Interrupt Descriptors ------------------
  122. #if SOC_CPU_HAS_FLEXIBLE_INTC
  123. static bool is_intr_num_resv(int intr_num)
  124. {
  125. // Workaround to reserve interrupt number 1 for Wi-Fi, 5,8 for Bluetooth, 6 for "permanently disabled interrupt"
  126. // [TODO: IDF-2465]
  127. uint32_t reserved = BIT(1) | BIT(5) | BIT(6) | BIT(8);
  128. // int_num 0,3,4,7 are inavaliable for PULP cpu
  129. #if CONFIG_IDF_TARGET_ESP32C6 // TODO: IDF-5728 replace with a better macro name
  130. reserved |= BIT(0) | BIT(3) | BIT(4) | BIT(7);
  131. #endif
  132. if (reserved & BIT(intr_num)) {
  133. return true;
  134. }
  135. extern int _vector_table;
  136. extern int _interrupt_handler;
  137. const intptr_t pc = (intptr_t)(&_vector_table + intr_num);
  138. /* JAL instructions are relative to the PC there are executed from. */
  139. const intptr_t destination = pc + riscv_decode_offset_from_jal_instruction(pc);
  140. return destination != (intptr_t)&_interrupt_handler;
  141. }
  142. void esp_cpu_intr_get_desc(int core_id, int intr_num, esp_cpu_intr_desc_t *intr_desc_ret)
  143. {
  144. intr_desc_ret->priority = 1; //Todo: We should make this -1
  145. intr_desc_ret->type = ESP_CPU_INTR_TYPE_NA;
  146. #if __riscv
  147. intr_desc_ret->flags = is_intr_num_resv(intr_num) ? ESP_CPU_INTR_DESC_FLAG_RESVD : 0;
  148. #else
  149. intr_desc_ret->flags = 0;
  150. #endif
  151. }
  152. #else // SOC_CPU_HAS_FLEXIBLE_INTC
  153. typedef struct {
  154. int priority;
  155. esp_cpu_intr_type_t type;
  156. uint32_t flags[SOC_CPU_CORES_NUM];
  157. } intr_desc_t;
  158. #if SOC_CPU_CORES_NUM > 1
  159. // Note: We currently only have dual core targets, so the table initializer is hard coded
  160. const static intr_desc_t intr_desc_table [SOC_CPU_INTR_NUM] = {
  161. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //0
  162. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //1
  163. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //2
  164. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //3
  165. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, 0 } }, //4
  166. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //5
  167. #if CONFIG_FREERTOS_CORETIMER_0
  168. { 1, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //6
  169. #else
  170. { 1, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL, ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //6
  171. #endif
  172. { 1, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL, ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //7
  173. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //8
  174. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //9
  175. { 1, ESP_CPU_INTR_TYPE_EDGE, { 0, 0 } }, //10
  176. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL, ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //11
  177. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0} }, //12
  178. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0} }, //13
  179. { 7, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //14, NMI
  180. #if CONFIG_FREERTOS_CORETIMER_1
  181. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //15
  182. #else
  183. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL, ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //15
  184. #endif
  185. { 5, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL, ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //16
  186. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //17
  187. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //18
  188. { 2, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //19
  189. { 2, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //20
  190. { 2, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //21
  191. { 3, ESP_CPU_INTR_TYPE_EDGE, { ESP_CPU_INTR_DESC_FLAG_RESVD, 0 } }, //22
  192. { 3, ESP_CPU_INTR_TYPE_LEVEL, { 0, 0 } }, //23
  193. { 4, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, 0 } }, //24
  194. { 4, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //25
  195. { 5, ESP_CPU_INTR_TYPE_LEVEL, { 0, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //26
  196. { 3, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //27
  197. { 4, ESP_CPU_INTR_TYPE_EDGE, { 0, 0 } }, //28
  198. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL, ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //29
  199. { 4, ESP_CPU_INTR_TYPE_EDGE, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //30
  200. { 5, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD, ESP_CPU_INTR_DESC_FLAG_RESVD } }, //31
  201. };
  202. #else // SOC_CPU_CORES_NUM > 1
  203. const static intr_desc_t intr_desc_table [SOC_CPU_INTR_NUM] = {
  204. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //0
  205. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //1
  206. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //2
  207. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //3
  208. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //4
  209. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //5
  210. #if CONFIG_FREERTOS_CORETIMER_0
  211. { 1, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //6
  212. #else
  213. { 1, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //6
  214. #endif
  215. { 1, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //7
  216. { 1, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //8
  217. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //9
  218. { 1, ESP_CPU_INTR_TYPE_EDGE, { 0 } }, //10
  219. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //11
  220. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //12
  221. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //13
  222. { 7, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //14, NMI
  223. #if CONFIG_FREERTOS_CORETIMER_1
  224. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //15
  225. #else
  226. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //15
  227. #endif
  228. { 5, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //16
  229. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //17
  230. { 1, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //18
  231. { 2, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //19
  232. { 2, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //20
  233. { 2, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //21
  234. { 3, ESP_CPU_INTR_TYPE_EDGE, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //22
  235. { 3, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //23
  236. { 4, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //24
  237. { 4, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //25
  238. { 5, ESP_CPU_INTR_TYPE_LEVEL, { 0 } }, //26
  239. { 3, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //27
  240. { 4, ESP_CPU_INTR_TYPE_EDGE, { 0 } }, //28
  241. { 3, ESP_CPU_INTR_TYPE_NA, { ESP_CPU_INTR_DESC_FLAG_SPECIAL } }, //29
  242. { 4, ESP_CPU_INTR_TYPE_EDGE, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //30
  243. { 5, ESP_CPU_INTR_TYPE_LEVEL, { ESP_CPU_INTR_DESC_FLAG_RESVD } }, //31
  244. };
  245. #endif // SOC_CPU_CORES_NUM > 1
  246. void esp_cpu_intr_get_desc(int core_id, int intr_num, esp_cpu_intr_desc_t *intr_desc_ret)
  247. {
  248. assert(core_id >= 0 && core_id < SOC_CPU_CORES_NUM);
  249. #if SOC_CPU_CORES_NUM == 1
  250. core_id = 0; //If this is a single core target, hard code CPU ID to 0
  251. #endif
  252. intr_desc_ret->priority = intr_desc_table[intr_num].priority;
  253. intr_desc_ret->type = intr_desc_table[intr_num].type;
  254. intr_desc_ret->flags = intr_desc_table[intr_num].flags[core_id];
  255. }
  256. #endif // SOC_CPU_HAS_FLEXIBLE_INTC
  257. /* -------------------------------------------------- Memory Ports -----------------------------------------------------
  258. *
  259. * ------------------------------------------------------------------------------------------------------------------ */
  260. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  261. void esp_cpu_configure_region_protection(void)
  262. {
  263. /* Note: currently this is configured the same on all Xtensa targets
  264. *
  265. * Both chips have the address space divided into 8 regions, 512MB each.
  266. */
  267. const int illegal_regions[] = {0, 4, 5, 6, 7}; // 0x00000000, 0x80000000, 0xa0000000, 0xc0000000, 0xe0000000
  268. for (size_t i = 0; i < sizeof(illegal_regions) / sizeof(illegal_regions[0]); ++i) {
  269. mpu_hal_set_region_access(illegal_regions[i], MPU_REGION_ILLEGAL);
  270. }
  271. mpu_hal_set_region_access(1, MPU_REGION_RW); // 0x20000000
  272. }
  273. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H4
  274. void esp_cpu_configure_region_protection(void)
  275. {
  276. /* Notes on implementation:
  277. *
  278. * 1) Note: ESP32-C3/H4 CPU doesn't support overlapping PMP regions
  279. *
  280. * 2) Therefore, we use TOR (top of range) entries to map the whole address
  281. * space, bottom to top.
  282. *
  283. * 3) There are not enough entries to describe all the memory regions 100% accurately.
  284. *
  285. * 4) This means some gaps (invalid memory) are accessible. Priority for extending regions
  286. * to cover gaps is to extend read-only or read-execute regions or read-only regions only
  287. * (executing unmapped addresses should always fault with invalid instruction, read-only means
  288. * stores will correctly fault even if reads may return some invalid value.)
  289. *
  290. * 5) Entries are grouped in order with some static asserts to try and verify everything is
  291. * correct.
  292. */
  293. const unsigned NONE = PMP_L | PMP_TOR;
  294. const unsigned R = PMP_L | PMP_TOR | PMP_R;
  295. const unsigned RW = PMP_L | PMP_TOR | PMP_R | PMP_W;
  296. const unsigned RX = PMP_L | PMP_TOR | PMP_R | PMP_X;
  297. const unsigned RWX = PMP_L | PMP_TOR | PMP_R | PMP_W | PMP_X;
  298. // 1. Gap at bottom of address space
  299. PMP_ENTRY_SET(0, SOC_DEBUG_LOW, NONE);
  300. // 2. Debug region
  301. PMP_ENTRY_SET(1, SOC_DEBUG_HIGH, RWX);
  302. _Static_assert(SOC_DEBUG_LOW < SOC_DEBUG_HIGH, "Invalid CPU debug region");
  303. // 3. Gap between debug region & DROM (flash cache)
  304. PMP_ENTRY_SET(2, SOC_DROM_LOW, NONE);
  305. _Static_assert(SOC_DEBUG_HIGH < SOC_DROM_LOW, "Invalid PMP entry order");
  306. // 4. DROM (flash cache)
  307. // 5. Gap between DROM & DRAM
  308. // (Note: To save PMP entries these two are merged into one read-only region)
  309. PMP_ENTRY_SET(3, SOC_DRAM_LOW, R);
  310. _Static_assert(SOC_DROM_LOW < SOC_DROM_HIGH, "Invalid DROM region");
  311. _Static_assert(SOC_DROM_HIGH < SOC_DRAM_LOW, "Invalid PMP entry order");
  312. // 6. DRAM
  313. PMP_ENTRY_SET(4, SOC_DRAM_HIGH, RW);
  314. _Static_assert(SOC_DRAM_LOW < SOC_DRAM_HIGH, "Invalid DRAM region");
  315. // 7. Gap between DRAM and Mask DROM
  316. // 8. Mask DROM
  317. // (Note: to save PMP entries these two are merged into one read-only region)
  318. PMP_ENTRY_SET(5, SOC_DROM_MASK_HIGH, R);
  319. _Static_assert(SOC_DRAM_HIGH < SOC_DROM_MASK_LOW, "Invalid PMP entry order");
  320. _Static_assert(SOC_DROM_MASK_LOW < SOC_DROM_MASK_HIGH, "Invalid mask DROM region");
  321. // 9. Gap between mask DROM and mask IROM
  322. // 10. Mask IROM
  323. // (Note: to save PMP entries these two are merged into one RX region)
  324. PMP_ENTRY_SET(6, SOC_IROM_MASK_HIGH, RX);
  325. _Static_assert(SOC_DROM_MASK_HIGH < SOC_IROM_MASK_LOW, "Invalid PMP entry order");
  326. _Static_assert(SOC_IROM_MASK_LOW < SOC_IROM_MASK_HIGH, "Invalid mask IROM region");
  327. // 11. Gap between mask IROM & IRAM
  328. PMP_ENTRY_SET(7, SOC_IRAM_LOW, NONE);
  329. _Static_assert(SOC_IROM_MASK_HIGH < SOC_IRAM_LOW, "Invalid PMP entry order");
  330. // 12. IRAM
  331. PMP_ENTRY_SET(8, SOC_IRAM_HIGH, RWX);
  332. _Static_assert(SOC_IRAM_LOW < SOC_IRAM_HIGH, "Invalid IRAM region");
  333. // 13. Gap between IRAM and IROM
  334. // 14. IROM (flash cache)
  335. // (Note: to save PMP entries these two are merged into one RX region)
  336. PMP_ENTRY_SET(9, SOC_IROM_HIGH, RX);
  337. _Static_assert(SOC_IRAM_HIGH < SOC_IROM_LOW, "Invalid PMP entry order");
  338. _Static_assert(SOC_IROM_LOW < SOC_IROM_HIGH, "Invalid IROM region");
  339. // 15. Gap between IROM & RTC slow memory
  340. PMP_ENTRY_SET(10, SOC_RTC_IRAM_LOW, NONE);
  341. _Static_assert(SOC_IROM_HIGH < SOC_RTC_IRAM_LOW, "Invalid PMP entry order");
  342. // 16. RTC fast memory
  343. PMP_ENTRY_SET(11, SOC_RTC_IRAM_HIGH, RWX);
  344. _Static_assert(SOC_RTC_IRAM_LOW < SOC_RTC_IRAM_HIGH, "Invalid RTC IRAM region");
  345. // 17. Gap between RTC fast memory & peripheral addresses
  346. PMP_ENTRY_SET(12, SOC_PERIPHERAL_LOW, NONE);
  347. _Static_assert(SOC_RTC_IRAM_HIGH < SOC_PERIPHERAL_LOW, "Invalid PMP entry order");
  348. // 18. Peripheral addresses
  349. PMP_ENTRY_SET(13, SOC_PERIPHERAL_HIGH, RW);
  350. _Static_assert(SOC_PERIPHERAL_LOW < SOC_PERIPHERAL_HIGH, "Invalid peripheral region");
  351. // 19. End of address space
  352. PMP_ENTRY_SET(14, UINT32_MAX, NONE); // all but last 4 bytes
  353. PMP_ENTRY_SET(15, UINT32_MAX, PMP_L | PMP_NA4); // last 4 bytes
  354. }
  355. #elif CONFIG_IDF_TARGET_ESP32C2
  356. #if CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT && !BOOTLOADER_BUILD
  357. extern int _iram_end;
  358. extern int _data_start;
  359. #define IRAM_END (int)&_iram_end
  360. #define DRAM_START (int)&_data_start
  361. #else
  362. #define IRAM_END SOC_DIRAM_IRAM_HIGH
  363. #define DRAM_START SOC_DIRAM_DRAM_LOW
  364. #endif
  365. #ifdef BOOTLOADER_BUILD
  366. // Without L bit set
  367. #define CONDITIONAL_NONE 0x0
  368. #define CONDITIONAL_RX PMP_R | PMP_X
  369. #define CONDITIONAL_RW PMP_R | PMP_W
  370. #else
  371. // With L bit set
  372. #define CONDITIONAL_NONE NONE
  373. #define CONDITIONAL_RX RX
  374. #define CONDITIONAL_RW RW
  375. #endif
  376. void esp_cpu_configure_region_protection(void)
  377. {
  378. /* Notes on implementation:
  379. *
  380. * 1) ESP32-C2 CPU support overlapping PMP regions, configuration is based on static priority
  381. * feature(lowest numbered entry has highest priority).
  382. *
  383. * 2) Therefore, we use TOR (top of range) and NAOPT entries to map the effective area.
  384. * Finally, define any address without access permission.
  385. *
  386. * 3) 3-15 PMPADDR entries be hardcoded to fixed value, 0-2 PMPADDR be programmed to split ID SRAM
  387. * as IRAM/DRAM. All PMPCFG entryies be available.
  388. *
  389. * 4) Ideally, PMPADDR 0-2 entries should be configured twice, once during bootloader startup and another during app startup.
  390. * However, the CPU currently always executes in machine mode and to enforce these permissions in machine mode, we need
  391. * to set the Lock (L) bit but if set once, it cannot be reconfigured. So, we only configure 0-2 PMPADDR during app startup.
  392. */
  393. const unsigned NONE = PMP_L ;
  394. const unsigned R = PMP_L | PMP_R;
  395. const unsigned X = PMP_L | PMP_X;
  396. const unsigned RW = PMP_L | PMP_R | PMP_W;
  397. const unsigned RX = PMP_L | PMP_R | PMP_X;
  398. const unsigned RWX = PMP_L | PMP_R | PMP_W | PMP_X;
  399. /* There are 4 configuration scenarios for PMPADDR 0-2
  400. *
  401. * 1. Bootloader build:
  402. * - We cannot set the lock bit as we need to reconfigure it again for the application.
  403. * We configure PMPADDR 0-1 to cover entire valid IRAM range and PMPADDR 2-3 to cover entire valid DRAM range.
  404. *
  405. * 2. Application build with CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT enabled
  406. * - We split the SRAM into IRAM and DRAM such that IRAM region cannot be accessed via DBUS
  407. * and DRAM region cannot be accessed via IBUS. We use _iram_end and _data_start markers to set the boundaries.
  408. * We also lock these entries so the R/W/X permissions are enforced even for machine mode
  409. *
  410. * 3. Application build with CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT disabled
  411. * - The IRAM-DRAM split is not enabled so we just need to ensure that access to only valid address ranges are successful
  412. * so for that we set PMPADDR 0-1 to cover entire valid IRAM range and PMPADDR 2-3 to cover entire DRAM region.
  413. * We also lock these entries so the R/W/X permissions are enforced even for machine mode
  414. *
  415. * 4. CPU is in OCD debug mode
  416. * - The IRAM-DRAM split is not enabled so that OpenOCD can write and execute from IRAM.
  417. * We set PMPADDR 0-1 to cover entire valid IRAM range and PMPADDR 2-3 to cover entire DRAM region.
  418. * We also lock these entries so the R/W/X permissions are enforced even for machine mode
  419. *
  420. * PMPADDR 3-15 are hard-coded and are appicable to both, bootloader and application. So we configure and lock
  421. * these during BOOTLOADER build itself. During application build, reconfiguration of these PMPADDR entries
  422. * are silently ignored by the CPU
  423. */
  424. if (esp_cpu_dbgr_is_attached()) {
  425. // Anti-FI check that cpu is really in ocd mode
  426. ESP_FAULT_ASSERT(esp_cpu_dbgr_is_attached());
  427. // 1. IRAM
  428. PMP_ENTRY_SET(0, SOC_DIRAM_IRAM_LOW, NONE);
  429. PMP_ENTRY_SET(1, SOC_DIRAM_IRAM_HIGH, PMP_TOR | RWX);
  430. // 2. DRAM
  431. PMP_ENTRY_SET(2, SOC_DIRAM_DRAM_LOW, NONE);
  432. PMP_ENTRY_CFG_SET(3, PMP_TOR | RW);
  433. } else {
  434. // 1. IRAM
  435. PMP_ENTRY_SET(0, SOC_DIRAM_IRAM_LOW, CONDITIONAL_NONE);
  436. PMP_ENTRY_SET(1, IRAM_END, PMP_TOR | CONDITIONAL_RX);
  437. // 2. DRAM
  438. PMP_ENTRY_SET(2, DRAM_START, CONDITIONAL_NONE);
  439. PMP_ENTRY_CFG_SET(3, PMP_TOR | CONDITIONAL_RW);
  440. }
  441. // 3. Debug region
  442. PMP_ENTRY_CFG_SET(4, PMP_NAPOT | RWX);
  443. // 4. DROM (flash dcache)
  444. PMP_ENTRY_CFG_SET(5, PMP_NAPOT | R);
  445. // 5. DROM_MASK
  446. PMP_ENTRY_CFG_SET(6, NONE);
  447. PMP_ENTRY_CFG_SET(7, PMP_TOR | R);
  448. // 6. IROM_MASK
  449. PMP_ENTRY_CFG_SET(8, NONE);
  450. PMP_ENTRY_CFG_SET(9, PMP_TOR | RX);
  451. // 7. IROM (flash icache)
  452. PMP_ENTRY_CFG_SET(10, PMP_NAPOT | RX);
  453. // 8. Peripheral addresses
  454. PMP_ENTRY_CFG_SET(11, PMP_NAPOT | RW);
  455. // 9. SRAM (used as ICache)
  456. PMP_ENTRY_CFG_SET(12, PMP_NAPOT | X);
  457. // 10. no access to any address below(0x0-0xFFFF_FFFF)
  458. PMP_ENTRY_CFG_SET(13, PMP_NA4 | NONE);// last 4 bytes(0xFFFFFFFC)
  459. PMP_ENTRY_CFG_SET(14, NONE);
  460. PMP_ENTRY_CFG_SET(15, PMP_TOR | NONE);
  461. }
  462. #elif CONFIG_IDF_TARGET_ESP32C6 // TODO: IDF-5642
  463. void esp_cpu_configure_region_protection(void)
  464. {
  465. /* Notes on implementation:
  466. *
  467. * 1) Note: ESP32-C6 CPU doesn't support overlapping PMP regions
  468. *
  469. * 2) Therefore, we use TOR (top of range) entries to map the whole address
  470. * space, bottom to top.
  471. *
  472. * 3) There are not enough entries to describe all the memory regions 100% accurately.
  473. *
  474. * 4) This means some gaps (invalid memory) are accessible. Priority for extending regions
  475. * to cover gaps is to extend read-only or read-execute regions or read-only regions only
  476. * (executing unmapped addresses should always fault with invalid instruction, read-only means
  477. * stores will correctly fault even if reads may return some invalid value.)
  478. *
  479. * 5) Entries are grouped in order with some static asserts to try and verify everything is
  480. * correct.
  481. */
  482. const unsigned NONE = PMP_L | PMP_TOR;
  483. const unsigned RW = PMP_L | PMP_TOR | PMP_R | PMP_W;
  484. const unsigned RX = PMP_L | PMP_TOR | PMP_R | PMP_X;
  485. const unsigned RWX = PMP_L | PMP_TOR | PMP_R | PMP_W | PMP_X;
  486. // 1. Gap at bottom of address space
  487. PMP_ENTRY_SET(0, SOC_DEBUG_LOW, NONE);
  488. // 2. Debug region
  489. PMP_ENTRY_SET(1, SOC_DEBUG_HIGH, RWX);
  490. _Static_assert(SOC_DEBUG_LOW < SOC_DEBUG_HIGH, "Invalid CPU debug region");
  491. // 3. Gap between debug region & IROM
  492. PMP_ENTRY_SET(2, SOC_IROM_MASK_LOW, NONE);
  493. _Static_assert(SOC_DEBUG_HIGH < SOC_IROM_MASK_LOW, "Invalid PMP entry order");
  494. // 4. ROM
  495. PMP_ENTRY_SET(3, SOC_DROM_MASK_HIGH, RX);
  496. _Static_assert(SOC_IROM_MASK_LOW < SOC_DROM_MASK_HIGH, "Invalid ROM region");
  497. // 5. Gap between ROM & RAM
  498. PMP_ENTRY_SET(4, SOC_IRAM_LOW, NONE);
  499. _Static_assert(SOC_DROM_MASK_HIGH < SOC_IRAM_LOW, "Invalid PMP entry order");
  500. // 6. RAM
  501. PMP_ENTRY_SET(5, SOC_IRAM_HIGH, RWX);
  502. _Static_assert(SOC_IRAM_LOW < SOC_IRAM_HIGH, "Invalid RAM region");
  503. // 7. Gap between DRAM and I_Cache
  504. PMP_ENTRY_SET(6, SOC_IROM_LOW, NONE);
  505. _Static_assert(SOC_IRAM_HIGH < SOC_IROM_LOW, "Invalid PMP entry order");
  506. // 8. I_Cache (flash)
  507. PMP_ENTRY_SET(7, SOC_IROM_HIGH, RWX);
  508. _Static_assert(SOC_IROM_LOW < SOC_IROM_HIGH, "Invalid I_Cache region");
  509. // 9. D_Cache (flash)
  510. PMP_ENTRY_SET(8, SOC_DROM_HIGH, RW);
  511. _Static_assert(SOC_DROM_LOW < SOC_DROM_HIGH, "Invalid D_Cache region");
  512. // 10. Gap between D_Cache & LP_RAM
  513. PMP_ENTRY_SET(9, SOC_RTC_IRAM_LOW, NONE);
  514. _Static_assert(SOC_DROM_HIGH < SOC_RTC_IRAM_LOW, "Invalid PMP entry order");
  515. // 16. LP memory
  516. PMP_ENTRY_SET(10, SOC_RTC_IRAM_HIGH, RWX);
  517. _Static_assert(SOC_RTC_IRAM_LOW < SOC_RTC_IRAM_HIGH, "Invalid RTC IRAM region");
  518. // 17. Gap between LP memory & peripheral addresses
  519. PMP_ENTRY_SET(11, SOC_PERIPHERAL_LOW, NONE);
  520. _Static_assert(SOC_RTC_IRAM_HIGH < SOC_PERIPHERAL_LOW, "Invalid PMP entry order");
  521. // 18. Peripheral addresses
  522. PMP_ENTRY_SET(12, SOC_PERIPHERAL_HIGH, RW);
  523. _Static_assert(SOC_PERIPHERAL_LOW < SOC_PERIPHERAL_HIGH, "Invalid peripheral region");
  524. // 19. End of address space
  525. PMP_ENTRY_SET(13, UINT32_MAX, NONE); // all but last 4 bytes
  526. PMP_ENTRY_SET(14, UINT32_MAX, PMP_L | PMP_NA4); // last 4 bytes
  527. }
  528. #endif
  529. /* ---------------------------------------------------- Debugging ------------------------------------------------------
  530. *
  531. * ------------------------------------------------------------------------------------------------------------------ */
  532. // --------------- Breakpoints/Watchpoints -----------------
  533. #if SOC_CPU_BREAKPOINTS_NUM > 0
  534. esp_err_t esp_cpu_set_breakpoint(int bp_num, const void *bp_addr)
  535. {
  536. /*
  537. Todo:
  538. - Check that bp_num is in range
  539. */
  540. #if __XTENSA__
  541. xt_utils_set_breakpoint(bp_num, (uint32_t)bp_addr);
  542. #else
  543. if (esp_cpu_dbgr_is_attached()) {
  544. /* If we want to set breakpoint which when hit transfers control to debugger
  545. * we need to set `action` in `mcontrol` to 1 (Enter Debug Mode).
  546. * That `action` value is supported only when `dmode` of `tdata1` is set.
  547. * But `dmode` can be modified by debugger only (from Debug Mode).
  548. *
  549. * So when debugger is connected we use special syscall to ask it to set breakpoint for us.
  550. */
  551. long args[] = {true, bp_num, (long)bp_addr};
  552. int ret = semihosting_call_noerrno(ESP_SEMIHOSTING_SYS_BREAKPOINT_SET, args);
  553. if (ret == 0) {
  554. return ESP_ERR_INVALID_RESPONSE;
  555. }
  556. }
  557. rv_utils_set_breakpoint(bp_num, (uint32_t)bp_addr);
  558. #endif // __XTENSA__
  559. return ESP_OK;
  560. }
  561. esp_err_t esp_cpu_clear_breakpoint(int bp_num)
  562. {
  563. /*
  564. Todo:
  565. - Check if the bp_num is valid
  566. */
  567. #if __XTENSA__
  568. xt_utils_clear_breakpoint(bp_num);
  569. #else
  570. if (esp_cpu_dbgr_is_attached()) {
  571. // See description in esp_cpu_set_breakpoint()
  572. long args[] = {false, bp_num};
  573. int ret = semihosting_call_noerrno(ESP_SEMIHOSTING_SYS_BREAKPOINT_SET, args);
  574. if (ret == 0) {
  575. return ESP_ERR_INVALID_RESPONSE;
  576. }
  577. }
  578. rv_utils_clear_breakpoint(bp_num);
  579. #endif // __XTENSA__
  580. return ESP_OK;
  581. }
  582. #endif // SOC_CPU_BREAKPOINTS_NUM > 0
  583. #if SOC_CPU_WATCHPOINTS_NUM > 0
  584. esp_err_t esp_cpu_set_watchpoint(int wp_num, const void *wp_addr, size_t size, esp_cpu_watchpoint_trigger_t trigger)
  585. {
  586. /*
  587. Todo:
  588. - Check that wp_num is in range
  589. - Check if the wp_num is already in use
  590. */
  591. // Check if size is 2^n, where n is in [0...6]
  592. if (size < 1 || size > 64 || (size & (size - 1)) != 0) {
  593. return ESP_ERR_INVALID_ARG;
  594. }
  595. bool on_read = (trigger == ESP_CPU_WATCHPOINT_LOAD || trigger == ESP_CPU_WATCHPOINT_ACCESS);
  596. bool on_write = (trigger == ESP_CPU_WATCHPOINT_STORE || trigger == ESP_CPU_WATCHPOINT_ACCESS);
  597. #if __XTENSA__
  598. xt_utils_set_watchpoint(wp_num, (uint32_t)wp_addr, size, on_read, on_write);
  599. #else
  600. if (esp_cpu_dbgr_is_attached()) {
  601. // See description in esp_cpu_set_breakpoint()
  602. long args[] = {true, wp_num, (long)wp_addr, (long)size,
  603. (long)((on_read ? ESP_SEMIHOSTING_WP_FLG_RD : 0) | (on_write ? ESP_SEMIHOSTING_WP_FLG_WR : 0))
  604. };
  605. int ret = semihosting_call_noerrno(ESP_SEMIHOSTING_SYS_WATCHPOINT_SET, args);
  606. if (ret == 0) {
  607. return ESP_ERR_INVALID_RESPONSE;
  608. }
  609. }
  610. rv_utils_set_watchpoint(wp_num, (uint32_t)wp_addr, size, on_read, on_write);
  611. #endif // __XTENSA__
  612. return ESP_OK;
  613. }
  614. esp_err_t esp_cpu_clear_watchpoint(int wp_num)
  615. {
  616. /*
  617. Todo:
  618. - Check if the wp_num is valid
  619. */
  620. #if __XTENSA__
  621. xt_utils_clear_watchpoint(wp_num);
  622. #else
  623. if (esp_cpu_dbgr_is_attached()) {
  624. // See description in esp_cpu_dbgr_is_attached()
  625. long args[] = {false, wp_num};
  626. int ret = semihosting_call_noerrno(ESP_SEMIHOSTING_SYS_WATCHPOINT_SET, args);
  627. if (ret == 0) {
  628. return ESP_ERR_INVALID_RESPONSE;
  629. }
  630. }
  631. rv_utils_clear_watchpoint(wp_num);
  632. #endif // __XTENSA__
  633. return ESP_OK;
  634. }
  635. #endif // SOC_CPU_WATCHPOINTS_NUM > 0
  636. /* ------------------------------------------------------ Misc ---------------------------------------------------------
  637. *
  638. * ------------------------------------------------------------------------------------------------------------------ */
  639. #if __XTENSA__ && XCHAL_HAVE_S32C1I && CONFIG_SPIRAM
  640. static DRAM_ATTR uint32_t external_ram_cas_lock = 0;
  641. #endif
  642. bool esp_cpu_compare_and_set(volatile uint32_t *addr, uint32_t compare_value, uint32_t new_value)
  643. {
  644. #if __XTENSA__
  645. bool ret;
  646. #if XCHAL_HAVE_S32C1I && CONFIG_SPIRAM
  647. // Check if the target address is in external RAM
  648. if ((uint32_t)addr >= SOC_EXTRAM_DATA_LOW && (uint32_t)addr < SOC_EXTRAM_DATA_HIGH) {
  649. /* The target address is in external RAM, thus the native CAS instruction cannot be used. Instead, we achieve
  650. atomicity by disabling interrupts and then acquiring an external RAM CAS lock. */
  651. uint32_t intr_level;
  652. __asm__ __volatile__ ("rsil %0, " XTSTR(XCHAL_EXCM_LEVEL) "\n"
  653. : "=r"(intr_level));
  654. if (!xt_utils_compare_and_set(&external_ram_cas_lock, 0, 1)) {
  655. // External RAM CAS lock already taken. Exit
  656. ret = false;
  657. goto exit;
  658. }
  659. // Now we compare and set the target address
  660. ret = (*addr == compare_value);
  661. if (ret) {
  662. *addr = new_value;
  663. }
  664. // Release the external RAM CAS lock
  665. external_ram_cas_lock = 0;
  666. exit:
  667. // Reenable interrupts
  668. __asm__ __volatile__ ("memw \n"
  669. "wsr %0, ps\n"
  670. :: "r"(intr_level));
  671. } else
  672. #endif // XCHAL_HAVE_S32C1I && CONFIG_SPIRAM
  673. {
  674. // The target address is in internal RAM. Use the CPU's native CAS instruction
  675. ret = xt_utils_compare_and_set(addr, compare_value, new_value);
  676. }
  677. return ret;
  678. #else // __XTENSA__
  679. // Single core targets don't have atomic CAS instruction. So access method is the same for internal and external RAM
  680. return rv_utils_compare_and_set(addr, compare_value, new_value);
  681. #endif
  682. }