cpu_start.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include "esp_attr.h"
  10. #include "esp_err.h"
  11. #include "esp_log.h"
  12. #include "esp_chip_info.h"
  13. #include "esp_efuse.h"
  14. #include "esp_private/cache_err_int.h"
  15. #include "esp_clk_internal.h"
  16. #include "esp_rom_efuse.h"
  17. #include "esp_rom_uart.h"
  18. #include "esp_rom_sys.h"
  19. #include "esp_rom_caps.h"
  20. #include "sdkconfig.h"
  21. #if CONFIG_IDF_TARGET_ESP32
  22. #include "soc/dport_reg.h"
  23. #include "esp32/rtc.h"
  24. #include "esp32/rom/cache.h"
  25. #include "esp32/rom/secure_boot.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S2
  27. #include "esp32s2/rtc.h"
  28. #include "esp32s2/rom/cache.h"
  29. #include "esp32s2/rom/secure_boot.h"
  30. #include "esp32s2/memprot.h"
  31. #elif CONFIG_IDF_TARGET_ESP32S3
  32. #include "esp32s3/rtc.h"
  33. #include "esp32s3/rom/cache.h"
  34. #include "esp32s3/rom/secure_boot.h"
  35. #include "esp_memprot.h"
  36. #include "soc/assist_debug_reg.h"
  37. #include "soc/system_reg.h"
  38. #include "esp32s3/rom/opi_flash.h"
  39. #include "hal/cache_hal.h"
  40. #elif CONFIG_IDF_TARGET_ESP32C3
  41. #include "esp32c3/rtc.h"
  42. #include "esp32c3/rom/cache.h"
  43. #include "esp32c3/rom/secure_boot.h"
  44. #include "esp_memprot.h"
  45. #elif CONFIG_IDF_TARGET_ESP32C6
  46. #include "esp32c6/rtc.h"
  47. #include "esp32c6/rom/cache.h"
  48. #include "esp_memprot.h"
  49. #elif CONFIG_IDF_TARGET_ESP32H2
  50. #include "esp32h2/rtc.h"
  51. #include "esp32h2/rom/cache.h"
  52. #include "esp_memprot.h"
  53. #elif CONFIG_IDF_TARGET_ESP32C2
  54. #include "esp32c2/rtc.h"
  55. #include "esp32c2/rom/cache.h"
  56. #include "esp32c2/rom/rtc.h"
  57. #include "esp32c2/rom/secure_boot.h"
  58. #elif CONFIG_IDF_TARGET_ESP32P4
  59. #include "esp32p4/rtc.h"
  60. #include "soc/hp_sys_clkrst_reg.h"
  61. #include "soc/interrupt_core0_reg.h"
  62. #include "soc/interrupt_core1_reg.h"
  63. #endif
  64. #include "esp_private/esp_mmu_map_private.h"
  65. #if CONFIG_SPIRAM
  66. #include "esp_psram.h"
  67. #include "esp_private/mmu_psram_flash.h"
  68. #include "esp_private/esp_psram_extram.h"
  69. #endif
  70. #include "esp_private/spi_flash_os.h"
  71. #include "esp_private/mspi_timing_tuning.h"
  72. #include "bootloader_flash_config.h"
  73. #include "bootloader_flash.h"
  74. #include "esp_private/crosscore_int.h"
  75. #include "esp_flash_encrypt.h"
  76. #include "esp_private/sleep_gpio.h"
  77. #include "hal/wdt_hal.h"
  78. #include "soc/rtc.h"
  79. #include "hal/cache_ll.h"
  80. #include "hal/efuse_ll.h"
  81. #include "soc/periph_defs.h"
  82. #include "esp_cpu.h"
  83. #include "esp_private/esp_clk.h"
  84. #include "spi_flash_mmap.h"
  85. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
  86. #include "esp_private/trax.h"
  87. #endif
  88. #include "bootloader_mem.h"
  89. #if CONFIG_APP_BUILD_TYPE_RAM
  90. #include "esp_rom_spiflash.h"
  91. #include "bootloader_init.h"
  92. #include "esp_private/bootloader_flash_internal.h"
  93. #endif // CONFIG_APP_BUILD_TYPE_RAM
  94. //This dependency will be removed in the future
  95. #include "soc/ext_mem_defs.h"
  96. #include "esp_private/startup_internal.h"
  97. #include "esp_private/system_internal.h"
  98. extern int _bss_start;
  99. extern int _bss_end;
  100. extern int _rtc_bss_start;
  101. extern int _rtc_bss_end;
  102. #if CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
  103. extern int _bss_bt_start;
  104. extern int _bss_bt_end;
  105. #endif // CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
  106. extern int _instruction_reserved_start;
  107. extern int _instruction_reserved_end;
  108. extern int _rodata_reserved_start;
  109. extern int _rodata_reserved_end;
  110. extern int _vector_table;
  111. #if SOC_INT_CLIC_SUPPORTED
  112. extern int _mtvt_table;
  113. #endif
  114. static const char *TAG = "cpu_start";
  115. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  116. extern int _ext_ram_bss_start;
  117. extern int _ext_ram_bss_end;
  118. #endif
  119. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  120. extern int _iram_bss_start;
  121. extern int _iram_bss_end;
  122. #endif
  123. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  124. static volatile bool s_cpu_up[SOC_CPU_CORES_NUM] = { false };
  125. static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false };
  126. static volatile bool s_resume_cores;
  127. #endif
  128. static void core_intr_matrix_clear(void)
  129. {
  130. uint32_t core_id = esp_cpu_get_core_id();
  131. for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
  132. #if CONFIG_IDF_TARGET_ESP32P4
  133. if (core_id == 0) {
  134. REG_WRITE(INTERRUPT_CORE0_LP_RTC_INT_MAP_REG + 4 * i, ETS_INVALID_INUM);
  135. } else {
  136. REG_WRITE(INTERRUPT_CORE1_LP_RTC_INT_MAP_REG + 4 * i, ETS_INVALID_INUM);
  137. }
  138. #else
  139. esp_rom_route_intr_matrix(core_id, i, ETS_INVALID_INUM);
  140. #endif // CONFIG_IDF_TARGET_ESP32P4
  141. }
  142. #if SOC_INT_CLIC_SUPPORTED
  143. for (int i = 0; i < 32; i++) {
  144. /* Set all the CPU interrupt lines to vectored by default, as it is on other RISC-V targets */
  145. esprv_intc_int_set_vectored(i, true);
  146. }
  147. #endif // SOC_INT_CLIC_SUPPORTED
  148. }
  149. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  150. void startup_resume_other_cores(void)
  151. {
  152. s_resume_cores = true;
  153. }
  154. void IRAM_ATTR call_start_cpu1(void)
  155. {
  156. #ifdef __riscv
  157. // Configure the global pointer register
  158. // (This should be the first thing IDF app does, as any other piece of code could be
  159. // relaxed by the linker to access something relative to __global_pointer$)
  160. __asm__ __volatile__ (
  161. ".option push\n"
  162. ".option norelax\n"
  163. "la gp, __global_pointer$\n"
  164. ".option pop"
  165. );
  166. #endif //#ifdef __riscv
  167. #if SOC_BRANCH_PREDICTOR_SUPPORTED
  168. esp_cpu_branch_prediction_enable();
  169. #endif //#if SOC_BRANCH_PREDICTOR_SUPPORTED
  170. esp_cpu_intr_set_ivt_addr(&_vector_table);
  171. #if SOC_INT_CLIC_SUPPORTED
  172. /* When hardware vectored interrupts are enabled in CLIC,
  173. * the CPU jumps to this base address + 4 * interrupt_id.
  174. */
  175. esp_cpu_intr_set_mtvt_addr(&_mtvt_table);
  176. #endif
  177. ets_set_appcpu_boot_addr(0);
  178. bootloader_init_mem();
  179. #if CONFIG_ESP_CONSOLE_NONE
  180. esp_rom_install_channel_putc(1, NULL);
  181. esp_rom_install_channel_putc(2, NULL);
  182. #else // CONFIG_ESP_CONSOLE_NONE
  183. esp_rom_install_uart_printf();
  184. esp_rom_uart_set_as_console(CONFIG_ESP_CONSOLE_UART_NUM);
  185. #endif
  186. #if CONFIG_IDF_TARGET_ESP32
  187. DPORT_REG_SET_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE);
  188. DPORT_REG_CLR_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_RECORD_ENABLE);
  189. #elif CONFIG_IDF_TARGET_ESP32P4
  190. //TODO: IDF-7688
  191. #else
  192. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG, 1);
  193. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG, 1);
  194. #endif
  195. s_cpu_up[1] = true;
  196. ESP_EARLY_LOGD(TAG, "App cpu up");
  197. // Clear interrupt matrix for APP CPU core
  198. core_intr_matrix_clear();
  199. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  200. //Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
  201. //has started, but it isn't active *on this CPU* yet.
  202. esp_cache_err_int_init();
  203. #endif
  204. #if (CONFIG_IDF_TARGET_ESP32 && CONFIG_ESP32_TRAX_TWOBANKS) || \
  205. (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_ESP32S3_TRAX_TWOBANKS)
  206. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  207. #endif
  208. s_cpu_inited[1] = true;
  209. while (!s_resume_cores) {
  210. esp_rom_delay_us(100);
  211. }
  212. SYS_STARTUP_FN();
  213. }
  214. static void start_other_core(void)
  215. {
  216. esp_chip_info_t chip_info;
  217. esp_chip_info(&chip_info);
  218. // If not the single core variant of a target - check this since there is
  219. // no separate soc_caps.h for the single core variant.
  220. if (!(chip_info.cores > 1)) {
  221. ESP_EARLY_LOGE(TAG, "Running on single core variant of a chip, but app is built with multi-core support.");
  222. ESP_EARLY_LOGE(TAG, "Check that CONFIG_FREERTOS_UNICORE is enabled in menuconfig");
  223. abort();
  224. }
  225. ESP_EARLY_LOGD(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  226. #if CONFIG_IDF_TARGET_ESP32 && !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  227. Cache_Flush(1);
  228. Cache_Read_Enable(1);
  229. #endif // #if CONFIG_IDF_TARGET_ESP32 && !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  230. esp_cpu_unstall(1);
  231. // Enable clock and reset APP CPU. Note that OpenOCD may have already
  232. // enabled clock and taken APP CPU out of reset. In this case don't reset
  233. // APP CPU again, as that will clear the breakpoints which may have already
  234. // been set.
  235. #if CONFIG_IDF_TARGET_ESP32
  236. if (!DPORT_GET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN)) {
  237. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  238. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  239. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  240. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  241. }
  242. #elif CONFIG_IDF_TARGET_ESP32S3
  243. if (!REG_GET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN)) {
  244. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  245. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  246. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  247. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  248. }
  249. #elif CONFIG_IDF_TARGET_ESP32P4
  250. if (!REG_GET_BIT(HP_SYS_CLKRST_SOC_CLK_CTRL0_REG, HP_SYS_CLKRST_REG_CORE1_CPU_CLK_EN)) {
  251. REG_SET_BIT(HP_SYS_CLKRST_SOC_CLK_CTRL0_REG, HP_SYS_CLKRST_REG_CORE1_CPU_CLK_EN);
  252. }
  253. if(REG_GET_BIT(HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_CORE1_GLOBAL)){
  254. REG_CLR_BIT(HP_SYS_CLKRST_HP_RST_EN0_REG, HP_SYS_CLKRST_REG_RST_EN_CORE1_GLOBAL);
  255. }
  256. #endif
  257. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  258. bool cpus_up = false;
  259. while (!cpus_up) {
  260. cpus_up = true;
  261. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  262. cpus_up &= s_cpu_up[i];
  263. }
  264. esp_rom_delay_us(100);
  265. }
  266. }
  267. #if !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
  268. // This function is needed to make the multicore app runnable on a unicore bootloader (built with FREERTOS UNICORE).
  269. // It does some cache settings for other CPUs.
  270. void IRAM_ATTR do_multicore_settings(void)
  271. {
  272. // We intentionally do not check the cache settings before changing them,
  273. // because it helps to get the application to run on older bootloaders.
  274. #ifdef CONFIG_IDF_TARGET_ESP32
  275. if (!efuse_ll_get_disable_app_cpu()) {
  276. Cache_Read_Disable(1);
  277. Cache_Flush(1);
  278. DPORT_REG_SET_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  279. DPORT_REG_CLR_BIT(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MMU_IA_CLR);
  280. // We do not enable cache for CPU1 now because it will be done later in start_other_core().
  281. }
  282. #endif
  283. cache_bus_mask_t cache_bus_mask_core0 = cache_ll_l1_get_enabled_bus(0);
  284. #ifndef CONFIG_IDF_TARGET_ESP32
  285. // 1. disable the cache before changing its settings.
  286. cache_hal_disable(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
  287. #endif
  288. for (unsigned core = 1; core < SOC_CPU_CORES_NUM; core++) {
  289. // 2. change cache settings. All cores must have the same settings.
  290. cache_ll_l1_enable_bus(core, cache_bus_mask_core0);
  291. }
  292. #ifndef CONFIG_IDF_TARGET_ESP32
  293. // 3. enable the cache after changing its settings.
  294. cache_hal_enable(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_ALL);
  295. #endif
  296. }
  297. #endif // !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
  298. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  299. /*
  300. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  301. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  302. */
  303. void IRAM_ATTR call_start_cpu0(void)
  304. {
  305. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  306. soc_reset_reason_t rst_reas[SOC_CPU_CORES_NUM];
  307. #else
  308. soc_reset_reason_t __attribute__((unused)) rst_reas[1];
  309. #endif
  310. #ifdef __riscv
  311. if (esp_cpu_dbgr_is_attached()) {
  312. /* Let debugger some time to detect that target started, halt it, enable ebreaks and resume.
  313. 500ms should be enough. */
  314. for (uint32_t ms_num = 0; ms_num < 2; ms_num++) {
  315. esp_rom_delay_us(100000);
  316. }
  317. }
  318. // Configure the global pointer register
  319. // (This should be the first thing IDF app does, as any other piece of code could be
  320. // relaxed by the linker to access something relative to __global_pointer$)
  321. __asm__ __volatile__ (
  322. ".option push\n"
  323. ".option norelax\n"
  324. "la gp, __global_pointer$\n"
  325. ".option pop"
  326. );
  327. #endif
  328. #if SOC_BRANCH_PREDICTOR_SUPPORTED
  329. esp_cpu_branch_prediction_enable();
  330. #endif
  331. // Move exception vectors to IRAM
  332. esp_cpu_intr_set_ivt_addr(&_vector_table);
  333. #if SOC_INT_CLIC_SUPPORTED
  334. /* When hardware vectored interrupts are enabled in CLIC,
  335. * the CPU jumps to this base address + 4 * interrupt_id.
  336. */
  337. esp_cpu_intr_set_mtvt_addr(&_mtvt_table);
  338. #endif
  339. rst_reas[0] = esp_rom_get_reset_reason(0);
  340. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  341. rst_reas[1] = esp_rom_get_reset_reason(1);
  342. #endif
  343. //Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
  344. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  345. #if CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
  346. // Clear Bluetooth bss
  347. memset(&_bss_bt_start, 0, (&_bss_bt_end - &_bss_bt_start) * sizeof(_bss_bt_start));
  348. #endif // CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
  349. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
  350. // Clear IRAM BSS
  351. memset(&_iram_bss_start, 0, (&_iram_bss_end - &_iram_bss_start) * sizeof(_iram_bss_start));
  352. #endif
  353. #if SOC_RTC_FAST_MEM_SUPPORTED || SOC_RTC_SLOW_MEM_SUPPORTED
  354. /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
  355. if (rst_reas[0] != RESET_REASON_CORE_DEEP_SLEEP) {
  356. memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
  357. }
  358. #endif
  359. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  360. #if CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  361. ESP_EARLY_LOGI(TAG, "Unicore app");
  362. #else
  363. ESP_EARLY_LOGI(TAG, "Multicore app");
  364. #if !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
  365. // It helps to fix missed cache settings for other cores. It happens when bootloader is unicore.
  366. do_multicore_settings();
  367. #endif // !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
  368. #endif
  369. #endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  370. // When the APP is loaded into ram for execution, some hardware initialization behaviors
  371. // in the bootloader are still necessary
  372. #if CONFIG_APP_BUILD_TYPE_RAM
  373. bootloader_init();
  374. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  375. bootloader_flash_hardware_init();
  376. #endif //#if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  377. #endif //#if CONFIG_APP_BUILD_TYPE_RAM
  378. #ifndef CONFIG_BOOTLOADER_WDT_ENABLE
  379. // from panic handler we can be reset by RWDT or TG0WDT
  380. if (rst_reas[0] == RESET_REASON_CORE_RTC_WDT || rst_reas[0] == RESET_REASON_CORE_MWDT0
  381. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  382. || rst_reas[1] == RESET_REASON_CORE_RTC_WDT || rst_reas[1] == RESET_REASON_CORE_MWDT0
  383. #endif
  384. ) {
  385. wdt_hal_context_t rtc_wdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
  386. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  387. wdt_hal_disable(&rtc_wdt_ctx);
  388. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  389. }
  390. #endif
  391. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  392. #if CONFIG_IDF_TARGET_ESP32S2
  393. /* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
  394. extern void esp_config_instruction_cache_mode(void);
  395. esp_config_instruction_cache_mode();
  396. /* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
  397. Configure the mode of data : cache size, cache associated ways, cache line size.
  398. Enable data cache, so if we don't use SPIRAM, it just works. */
  399. extern void esp_config_data_cache_mode(void);
  400. esp_config_data_cache_mode();
  401. Cache_Enable_DCache(0);
  402. #endif
  403. #if CONFIG_IDF_TARGET_ESP32S3
  404. /* Configure the mode of instruction cache : cache size, cache line size. */
  405. extern void rom_config_instruction_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  406. rom_config_instruction_cache_mode(CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE, CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE);
  407. /* If we need use SPIRAM, we should use data cache.
  408. Configure the mode of data : cache size, cache line size.*/
  409. Cache_Suspend_DCache();
  410. extern void rom_config_data_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  411. rom_config_data_cache_mode(CONFIG_ESP32S3_DATA_CACHE_SIZE, CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE);
  412. Cache_Resume_DCache(0);
  413. #endif // CONFIG_IDF_TARGET_ESP32S3
  414. #if CONFIG_IDF_TARGET_ESP32P4
  415. //TODO: IDF-5670, add cache init API
  416. extern void esp_config_l2_cache_mode(void);
  417. esp_config_l2_cache_mode();
  418. #endif
  419. if (esp_efuse_check_errors() != ESP_OK) {
  420. esp_restart();
  421. }
  422. #if ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
  423. #if CONFIG_APP_BUILD_TYPE_ELF_RAM
  424. // For RAM loadable ELF case, we don't need to reserve IROM/DROM as instructions and data
  425. // are all in internal RAM. If the RAM loadable ELF has any requirement to memory map the
  426. // external flash then it should use flash or partition mmap APIs.
  427. uint32_t cache_mmu_irom_size = 0;
  428. __attribute__((unused)) uint32_t cache_mmu_drom_size = 0;
  429. #else // CONFIG_APP_BUILD_TYPE_ELF_RAM
  430. uint32_t _instruction_size = (uint32_t)&_instruction_reserved_end - (uint32_t)&_instruction_reserved_start;
  431. uint32_t cache_mmu_irom_size = ((_instruction_size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE) * sizeof(uint32_t);
  432. uint32_t _rodata_size = (uint32_t)&_rodata_reserved_end - (uint32_t)&_rodata_reserved_start;
  433. __attribute__((unused)) uint32_t cache_mmu_drom_size = ((_rodata_size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE) * sizeof(uint32_t);
  434. #endif // !CONFIG_APP_BUILD_TYPE_ELF_RAM
  435. /* Configure the Cache MMU size for instruction and rodata in flash. */
  436. Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
  437. #endif // ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
  438. #if CONFIG_ESPTOOLPY_OCT_FLASH && !CONFIG_ESPTOOLPY_FLASH_MODE_AUTO_DETECT
  439. bool efuse_opflash_en = efuse_ll_get_flash_type();
  440. if (!efuse_opflash_en) {
  441. ESP_EARLY_LOGE(TAG, "Octal Flash option selected, but EFUSE not configured!");
  442. abort();
  443. }
  444. #endif
  445. esp_mspi_pin_init();
  446. // For Octal flash, it's hard to implement a read_id function in OPI mode for all vendors.
  447. // So we have to read it here in SPI mode, before entering the OPI mode.
  448. bootloader_flash_update_id();
  449. /**
  450. * This function initialise the Flash chip to the user-defined settings.
  451. *
  452. * In bootloader, we only init Flash (and MSPI) to a preliminary state, for being flexible to
  453. * different chips.
  454. * In this stage, we re-configure the Flash (and MSPI) to required configuration
  455. */
  456. spi_flash_init_chip_state();
  457. #if SOC_MEMSPI_SRC_FREQ_120M
  458. mspi_timing_flash_tuning();
  459. #endif
  460. esp_mmu_map_init();
  461. #if CONFIG_SPIRAM_BOOT_INIT
  462. if (esp_psram_init() != ESP_OK) {
  463. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  464. ESP_EARLY_LOGE(TAG, "Failed to init external RAM, needed for external .bss segment");
  465. abort();
  466. #endif
  467. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  468. ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
  469. #else
  470. ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
  471. abort();
  472. #endif
  473. }
  474. #endif
  475. #endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  476. bootloader_init_mem();
  477. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  478. s_cpu_up[0] = true;
  479. #endif
  480. ESP_EARLY_LOGD(TAG, "Pro cpu up");
  481. #if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
  482. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  483. start_other_core();
  484. #else
  485. ESP_EARLY_LOGI(TAG, "Single core mode");
  486. #if CONFIG_IDF_TARGET_ESP32
  487. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); // stop the other core
  488. #elif CONFIG_IDF_TARGET_ESP32S3
  489. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  490. #if SOC_APPCPU_HAS_CLOCK_GATING_BUG
  491. /* The clock gating signal of the App core is invalid. We use RUNSTALL and RESETING
  492. signals to ensure that the App core stops running in single-core mode. */
  493. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  494. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  495. #endif
  496. #endif // CONFIG_IDF_TARGET_ESP32
  497. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  498. #endif // SOC_CPU_CORES_NUM > 1
  499. #if CONFIG_SPIRAM_MEMTEST
  500. if (esp_psram_is_initialized()) {
  501. bool ext_ram_ok = esp_psram_extram_test();
  502. if (!ext_ram_ok) {
  503. ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
  504. abort();
  505. }
  506. }
  507. #endif //CONFIG_SPIRAM_MEMTEST
  508. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  509. //TODO: IDF-5023, replace with MMU driver
  510. #if CONFIG_IDF_TARGET_ESP32S3
  511. int s_instr_flash2spiram_off = 0;
  512. int s_rodata_flash2spiram_off = 0;
  513. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  514. s_instr_flash2spiram_off = instruction_flash2spiram_offset();
  515. #endif
  516. #if CONFIG_SPIRAM_RODATA
  517. s_rodata_flash2spiram_off = rodata_flash2spiram_offset();
  518. #endif
  519. Cache_Set_IDROM_MMU_Info(cache_mmu_irom_size / sizeof(uint32_t), \
  520. cache_mmu_drom_size / sizeof(uint32_t), \
  521. (uint32_t)&_rodata_reserved_start, \
  522. (uint32_t)&_rodata_reserved_end, \
  523. s_instr_flash2spiram_off, \
  524. s_rodata_flash2spiram_off);
  525. #endif // CONFIG_IDF_TARGET_ESP32S3
  526. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP || \
  527. CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
  528. uint32_t icache_wrap_enable = 0, dcache_wrap_enable = 0;
  529. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP
  530. icache_wrap_enable = 1;
  531. #endif
  532. #if CONFIG_ESP32S2_DATA_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
  533. dcache_wrap_enable = 1;
  534. #endif
  535. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
  536. esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
  537. #endif
  538. #if CONFIG_ESP32S3_DATA_CACHE_16KB
  539. Cache_Invalidate_DCache_All();
  540. Cache_Occupy_Addr(SOC_DROM_LOW, 0x4000);
  541. #endif
  542. #if CONFIG_IDF_TARGET_ESP32C2
  543. // TODO : IDF-5020
  544. #if CONFIG_ESP32C2_INSTRUCTION_CACHE_WRAP
  545. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable);
  546. esp_enable_cache_wrap(1);
  547. #endif
  548. #endif
  549. #endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  550. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  551. memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
  552. #endif
  553. //Enable trace memory and immediately start trace.
  554. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
  555. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S3
  556. #if CONFIG_ESP32_TRAX_TWOBANKS || CONFIG_ESP32S3_TRAX_TWOBANKS
  557. trax_enable(TRAX_ENA_PRO_APP);
  558. #else
  559. trax_enable(TRAX_ENA_PRO);
  560. #endif
  561. #elif CONFIG_IDF_TARGET_ESP32S2
  562. trax_enable(TRAX_ENA_PRO);
  563. #endif
  564. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  565. #endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
  566. esp_clk_init();
  567. esp_perip_clk_init();
  568. // Now that the clocks have been set-up, set the startup time from RTC
  569. // and default RTC-backed system time provider.
  570. g_startup_time = esp_rtc_get_time_us();
  571. // Clear interrupt matrix for PRO CPU core
  572. core_intr_matrix_clear();
  573. #ifndef CONFIG_IDF_ENV_FPGA // TODO: on FPGA it should be possible to configure this, not currently working with APB_CLK_FREQ changed
  574. #ifdef CONFIG_ESP_CONSOLE_UART
  575. uint32_t clock_hz = esp_clk_apb_freq();
  576. #if ESP_ROM_UART_CLK_IS_XTAL
  577. clock_hz = esp_clk_xtal_freq(); // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  578. #endif
  579. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  580. // In a single thread mode, the freertos is not started yet. So don't have to use a critical section.
  581. int __DECLARE_RCC_ATOMIC_ENV __attribute__ ((unused)); // To avoid build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV
  582. esp_rom_uart_set_clock_baudrate(CONFIG_ESP_CONSOLE_UART_NUM, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  583. #endif
  584. #endif
  585. #if !CONFIG_IDF_TARGET_ESP32P4 //TODO: IDF-7529
  586. // Need to unhold the IOs that were hold right before entering deep sleep, which are used as wakeup pins
  587. if (rst_reas[0] == RESET_REASON_CORE_DEEP_SLEEP) {
  588. esp_deep_sleep_wakeup_io_reset();
  589. }
  590. #endif //#if !CONFIG_IDF_TARGET_ESP32P4
  591. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  592. esp_cache_err_int_init();
  593. #endif
  594. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE && !CONFIG_ESP_SYSTEM_MEMPROT_TEST
  595. // Memprot cannot be locked during OS startup as the lock-on prevents any PMS changes until a next reboot
  596. // If such a situation appears, it is likely an malicious attempt to bypass the system safety setup -> print error & reset
  597. #if CONFIG_IDF_TARGET_ESP32S2
  598. if (esp_memprot_is_locked_any()) {
  599. #else
  600. bool is_locked = false;
  601. if (esp_mprot_is_conf_locked_any(&is_locked) != ESP_OK || is_locked) {
  602. #endif
  603. ESP_EARLY_LOGE(TAG, "Memprot feature locked after the system reset! Potential safety corruption, rebooting.");
  604. esp_restart_noos();
  605. }
  606. //default configuration of PMS Memprot
  607. esp_err_t memp_err = ESP_OK;
  608. #if CONFIG_IDF_TARGET_ESP32S2 //specific for ESP32S2 unless IDF-3024 is merged
  609. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  610. memp_err = esp_memprot_set_prot(PANIC_HNDL_ON, MEMPROT_LOCK, NULL);
  611. #else
  612. memp_err = esp_memprot_set_prot(PANIC_HNDL_ON, MEMPROT_UNLOCK, NULL);
  613. #endif
  614. #else //CONFIG_IDF_TARGET_ESP32S2 specific end
  615. esp_memp_config_t memp_cfg = ESP_MEMPROT_DEFAULT_CONFIG();
  616. #if !CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  617. memp_cfg.lock_feature = false;
  618. #endif
  619. memp_err = esp_mprot_set_prot(&memp_cfg);
  620. #endif //other IDF_TARGETS end
  621. if (memp_err != ESP_OK) {
  622. ESP_EARLY_LOGE(TAG, "Failed to set Memprot feature (0x%08X: %s), rebooting.", memp_err, esp_err_to_name(memp_err));
  623. esp_restart_noos();
  624. }
  625. #endif //CONFIG_ESP_SYSTEM_MEMPROT_FEATURE && !CONFIG_ESP_SYSTEM_MEMPROT_TEST
  626. #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  627. // External devices (including SPI0/1, cache) should be initialized
  628. #if !CONFIG_APP_BUILD_TYPE_RAM
  629. // Normal startup flow. We arrive here with the help of 1st, 2nd bootloader. There are valid headers (app/bootloader)
  630. // Read the application binary image header. This will also decrypt the header if the image is encrypted.
  631. __attribute__((unused)) esp_image_header_t fhdr = {0};
  632. // This assumes that DROM is the first segment in the application binary, i.e. that we can read
  633. // the binary header through cache by accessing SOC_DROM_LOW address.
  634. hal_memcpy(&fhdr, (void *) SOC_DROM_LOW, sizeof(fhdr));
  635. #if CONFIG_IDF_TARGET_ESP32
  636. #if !CONFIG_SPIRAM_BOOT_INIT
  637. // If psram is uninitialized, we need to improve some flash configuration.
  638. bootloader_flash_clock_config(&fhdr);
  639. bootloader_flash_gpio_config(&fhdr);
  640. bootloader_flash_dummy_config(&fhdr);
  641. bootloader_flash_cs_timing_config();
  642. #endif //!CONFIG_SPIRAM_BOOT_INIT
  643. #endif //CONFIG_IDF_TARGET_ESP32
  644. #if CONFIG_SPI_FLASH_SIZE_OVERRIDE
  645. int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
  646. if (app_flash_size < 1 * 1024 * 1024) {
  647. ESP_EARLY_LOGE(TAG, "Invalid flash size in app image header.");
  648. abort();
  649. }
  650. bootloader_flash_update_size(app_flash_size);
  651. #endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
  652. #else
  653. // CONFIG_APP_BUILD_TYPE_RAM && !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  654. bootloader_flash_unlock();
  655. #endif
  656. #endif //!CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
  657. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  658. s_cpu_inited[0] = true;
  659. volatile bool cpus_inited = false;
  660. while (!cpus_inited) {
  661. cpus_inited = true;
  662. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  663. cpus_inited &= s_cpu_inited[i];
  664. }
  665. esp_rom_delay_us(100);
  666. }
  667. #endif
  668. SYS_STARTUP_FN();
  669. }