cpu_start.c 27 KB

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