cpu_start.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 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. #elif CONFIG_IDF_TARGET_ESP32C3
  40. #include "esp32c3/rtc.h"
  41. #include "esp32c3/rom/cache.h"
  42. #include "esp32c3/rom/secure_boot.h"
  43. #include "esp_memprot.h"
  44. #elif CONFIG_IDF_TARGET_ESP32H2
  45. #include "esp32h2/rtc.h"
  46. #include "esp32h2/rom/cache.h"
  47. #include "esp32h2/rom/secure_boot.h"
  48. #include "esp_memprot.h"
  49. #elif CONFIG_IDF_TARGET_ESP32C2
  50. #include "esp32c2/rtc.h"
  51. #include "esp32c2/rom/cache.h"
  52. #include "esp32c2/rom/rtc.h"
  53. #include "esp32c2/rom/secure_boot.h"
  54. #include "esp32c2/memprot.h"
  55. #endif
  56. #if CONFIG_SPIRAM
  57. #include "esp_psram.h"
  58. #include "esp_private/esp_psram_extram.h"
  59. #endif
  60. #include "esp_private/spi_flash_os.h"
  61. #include "bootloader_flash_config.h"
  62. #include "bootloader_flash.h"
  63. #include "esp_private/crosscore_int.h"
  64. #include "esp_flash_encrypt.h"
  65. #include "hal/rtc_io_hal.h"
  66. #include "hal/gpio_hal.h"
  67. #include "hal/wdt_hal.h"
  68. #include "soc/rtc.h"
  69. #include "hal/efuse_ll.h"
  70. #include "soc/periph_defs.h"
  71. #include "esp_cpu.h"
  72. #include "esp_private/esp_clk.h"
  73. #include "spi_flash_mmap.h"
  74. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
  75. #include "esp_private/trax.h"
  76. #endif
  77. #include "bootloader_mem.h"
  78. #if CONFIG_APP_BUILD_TYPE_ELF_RAM
  79. #include "esp_rom_spiflash.h"
  80. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  81. //This dependency will be removed in the future
  82. #include "soc/ext_mem_defs.h"
  83. #include "esp_private/startup_internal.h"
  84. #include "esp_private/system_internal.h"
  85. extern int _bss_start;
  86. extern int _bss_end;
  87. extern int _rtc_bss_start;
  88. extern int _rtc_bss_end;
  89. extern int _vector_table;
  90. static const char *TAG = "cpu_start";
  91. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  92. extern int _ext_ram_bss_start;
  93. extern int _ext_ram_bss_end;
  94. #endif
  95. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  96. extern int _iram_bss_start;
  97. extern int _iram_bss_end;
  98. #endif
  99. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  100. static volatile bool s_cpu_up[SOC_CPU_CORES_NUM] = { false };
  101. static volatile bool s_cpu_inited[SOC_CPU_CORES_NUM] = { false };
  102. static volatile bool s_resume_cores;
  103. #endif
  104. static void core_intr_matrix_clear(void)
  105. {
  106. uint32_t core_id = esp_cpu_get_core_id();
  107. for (int i = 0; i < ETS_MAX_INTR_SOURCE; i++) {
  108. esp_rom_route_intr_matrix(core_id, i, ETS_INVALID_INUM);
  109. }
  110. }
  111. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  112. void startup_resume_other_cores(void)
  113. {
  114. s_resume_cores = true;
  115. }
  116. void IRAM_ATTR call_start_cpu1(void)
  117. {
  118. esp_cpu_intr_set_ivt_addr(&_vector_table);
  119. ets_set_appcpu_boot_addr(0);
  120. bootloader_init_mem();
  121. #if CONFIG_ESP_CONSOLE_NONE
  122. esp_rom_install_channel_putc(1, NULL);
  123. esp_rom_install_channel_putc(2, NULL);
  124. #else // CONFIG_ESP_CONSOLE_NONE
  125. esp_rom_install_uart_printf();
  126. esp_rom_uart_set_as_console(CONFIG_ESP_CONSOLE_UART_NUM);
  127. #endif
  128. #if CONFIG_IDF_TARGET_ESP32
  129. DPORT_REG_SET_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE);
  130. DPORT_REG_CLR_BIT(DPORT_APP_CPU_RECORD_CTRL_REG, DPORT_APP_CPU_RECORD_ENABLE);
  131. #else
  132. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG, 1);
  133. REG_WRITE(ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG, 1);
  134. #endif
  135. s_cpu_up[1] = true;
  136. ESP_EARLY_LOGI(TAG, "App cpu up.");
  137. // Clear interrupt matrix for APP CPU core
  138. core_intr_matrix_clear();
  139. //Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
  140. //has started, but it isn't active *on this CPU* yet.
  141. esp_cache_err_int_init();
  142. #if (CONFIG_IDF_TARGET_ESP32 && CONFIG_ESP32_TRAX_TWOBANKS) || \
  143. (CONFIG_IDF_TARGET_ESP32S3 && CONFIG_ESP32S3_TRAX_TWOBANKS)
  144. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  145. #endif
  146. s_cpu_inited[1] = true;
  147. while (!s_resume_cores) {
  148. esp_rom_delay_us(100);
  149. }
  150. SYS_STARTUP_FN();
  151. }
  152. static void start_other_core(void)
  153. {
  154. esp_chip_info_t chip_info;
  155. esp_chip_info(&chip_info);
  156. // If not the single core variant of a target - check this since there is
  157. // no separate soc_caps.h for the single core variant.
  158. if (!(chip_info.cores > 1)) {
  159. ESP_EARLY_LOGE(TAG, "Running on single core variant of a chip, but app is built with multi-core support.");
  160. ESP_EARLY_LOGE(TAG, "Check that CONFIG_FREERTOS_UNICORE is enabled in menuconfig");
  161. abort();
  162. }
  163. ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1);
  164. #if CONFIG_IDF_TARGET_ESP32
  165. Cache_Flush(1);
  166. Cache_Read_Enable(1);
  167. #endif
  168. esp_cpu_unstall(1);
  169. // Enable clock and reset APP CPU. Note that OpenOCD may have already
  170. // enabled clock and taken APP CPU out of reset. In this case don't reset
  171. // APP CPU again, as that will clear the breakpoints which may have already
  172. // been set.
  173. #if CONFIG_IDF_TARGET_ESP32
  174. if (!DPORT_GET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN)) {
  175. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN);
  176. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL);
  177. DPORT_SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  178. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING);
  179. }
  180. #elif CONFIG_IDF_TARGET_ESP32S3
  181. if (!REG_GET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN)) {
  182. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  183. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  184. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  185. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  186. }
  187. #endif
  188. ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1);
  189. bool cpus_up = false;
  190. while (!cpus_up) {
  191. cpus_up = true;
  192. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  193. cpus_up &= s_cpu_up[i];
  194. }
  195. esp_rom_delay_us(100);
  196. }
  197. }
  198. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  199. /*
  200. * We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
  201. * and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
  202. */
  203. void IRAM_ATTR call_start_cpu0(void)
  204. {
  205. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  206. soc_reset_reason_t rst_reas[SOC_CPU_CORES_NUM];
  207. #else
  208. soc_reset_reason_t __attribute__((unused)) rst_reas[1];
  209. #endif
  210. #ifdef __riscv
  211. if (esp_cpu_dbgr_is_attached()) {
  212. /* Let debugger some time to detect that target started, halt it, enable ebreaks and resume.
  213. 500ms should be enough. */
  214. for (uint32_t ms_num = 0; ms_num < 2; ms_num++) {
  215. esp_rom_delay_us(100000);
  216. }
  217. }
  218. // Configure the global pointer register
  219. // (This should be the first thing IDF app does, as any other piece of code could be
  220. // relaxed by the linker to access something relative to __global_pointer$)
  221. __asm__ __volatile__ (
  222. ".option push\n"
  223. ".option norelax\n"
  224. "la gp, __global_pointer$\n"
  225. ".option pop"
  226. );
  227. #endif
  228. // Move exception vectors to IRAM
  229. esp_cpu_intr_set_ivt_addr(&_vector_table);
  230. rst_reas[0] = esp_rom_get_reset_reason(0);
  231. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  232. rst_reas[1] = esp_rom_get_reset_reason(1);
  233. #endif
  234. #ifndef CONFIG_BOOTLOADER_WDT_ENABLE
  235. // from panic handler we can be reset by RWDT or TG0WDT
  236. if (rst_reas[0] == RESET_REASON_CORE_RTC_WDT || rst_reas[0] == RESET_REASON_CORE_MWDT0
  237. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  238. || rst_reas[1] == RESET_REASON_CORE_RTC_WDT || rst_reas[1] == RESET_REASON_CORE_MWDT0
  239. #endif
  240. ) {
  241. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  242. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  243. wdt_hal_disable(&rtc_wdt_ctx);
  244. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  245. }
  246. #endif
  247. //Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
  248. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  249. #if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
  250. // Clear IRAM BSS
  251. memset(&_iram_bss_start, 0, (&_iram_bss_end - &_iram_bss_start) * sizeof(_iram_bss_start));
  252. #endif
  253. #if SOC_RTC_FAST_MEM_SUPPORTED || SOC_RTC_SLOW_MEM_SUPPORTED
  254. /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
  255. if (rst_reas[0] != RESET_REASON_CORE_DEEP_SLEEP) {
  256. memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
  257. }
  258. #endif
  259. #if CONFIG_IDF_TARGET_ESP32S2
  260. /* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
  261. extern void esp_config_instruction_cache_mode(void);
  262. esp_config_instruction_cache_mode();
  263. /* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
  264. Configure the mode of data : cache size, cache associated ways, cache line size.
  265. Enable data cache, so if we don't use SPIRAM, it just works. */
  266. #if CONFIG_SPIRAM_BOOT_INIT
  267. extern void esp_config_data_cache_mode(void);
  268. esp_config_data_cache_mode();
  269. Cache_Enable_DCache(0);
  270. #endif
  271. #endif
  272. #if CONFIG_IDF_TARGET_ESP32S3
  273. /* Configure the mode of instruction cache : cache size, cache line size. */
  274. extern void rom_config_instruction_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  275. rom_config_instruction_cache_mode(CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE, CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE);
  276. /* If we need use SPIRAM, we should use data cache.
  277. Configure the mode of data : cache size, cache line size.*/
  278. Cache_Suspend_DCache();
  279. extern void rom_config_data_cache_mode(uint32_t cfg_cache_size, uint8_t cfg_cache_ways, uint8_t cfg_cache_line_size);
  280. rom_config_data_cache_mode(CONFIG_ESP32S3_DATA_CACHE_SIZE, CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS, CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE);
  281. Cache_Resume_DCache(0);
  282. #endif // CONFIG_IDF_TARGET_ESP32S3
  283. if (esp_efuse_check_errors() != ESP_OK) {
  284. esp_restart();
  285. }
  286. #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  287. /* Configure the Cache MMU size for instruction and rodata in flash. */
  288. extern uint32_t Cache_Set_IDROM_MMU_Size(uint32_t irom_size, uint32_t drom_size);
  289. extern int _rodata_reserved_start;
  290. uint32_t rodata_reserved_start_align = (uint32_t)&_rodata_reserved_start & ~(SPI_FLASH_MMU_PAGE_SIZE - 1);
  291. uint32_t cache_mmu_irom_size = ((rodata_reserved_start_align - SOC_DROM_LOW) / SPI_FLASH_MMU_PAGE_SIZE) * sizeof(uint32_t);
  292. #if CONFIG_IDF_TARGET_ESP32S3
  293. extern int _rodata_reserved_end;
  294. uint32_t cache_mmu_drom_size = (((uint32_t)&_rodata_reserved_end - rodata_reserved_start_align + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE) * sizeof(uint32_t);
  295. #endif
  296. Cache_Set_IDROM_MMU_Size(cache_mmu_irom_size, CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size);
  297. #endif // CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  298. #if CONFIG_ESPTOOLPY_OCT_FLASH
  299. bool efuse_opflash_en = efuse_ll_get_flash_type();
  300. if (!efuse_opflash_en) {
  301. ESP_EARLY_LOGE(TAG, "Octal Flash option selected, but EFUSE not configured!");
  302. abort();
  303. }
  304. #endif
  305. esp_mspi_pin_init();
  306. // For Octal flash, it's hard to implement a read_id function in OPI mode for all vendors.
  307. // So we have to read it here in SPI mode, before entering the OPI mode.
  308. bootloader_flash_update_id();
  309. /**
  310. * This function initialise the Flash chip to the user-defined settings.
  311. *
  312. * In bootloader, we only init Flash (and MSPI) to a preliminary state, for being flexible to
  313. * different chips.
  314. * In this stage, we re-configure the Flash (and MSPI) to required configuration
  315. */
  316. spi_flash_init_chip_state();
  317. #if CONFIG_IDF_TARGET_ESP32S3
  318. //On other chips, this feature is not provided by HW, or hasn't been tested yet.
  319. spi_timing_flash_tuning();
  320. #endif
  321. bootloader_init_mem();
  322. #if CONFIG_SPIRAM_BOOT_INIT
  323. if (esp_psram_init() != ESP_OK) {
  324. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  325. ESP_EARLY_LOGE(TAG, "Failed to init external RAM, needed for external .bss segment");
  326. abort();
  327. #endif
  328. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  329. ESP_EARLY_LOGI(TAG, "Failed to init external RAM; continuing without it.");
  330. #else
  331. ESP_EARLY_LOGE(TAG, "Failed to init external RAM!");
  332. abort();
  333. #endif
  334. }
  335. #endif
  336. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  337. s_cpu_up[0] = true;
  338. #endif
  339. ESP_EARLY_LOGI(TAG, "Pro cpu up.");
  340. #if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
  341. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  342. start_other_core();
  343. #else
  344. ESP_EARLY_LOGI(TAG, "Single core mode");
  345. #if CONFIG_IDF_TARGET_ESP32
  346. DPORT_CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); // stop the other core
  347. #elif CONFIG_IDF_TARGET_ESP32S3
  348. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_CLKGATE_EN);
  349. #if SOC_APPCPU_HAS_CLOCK_GATING_BUG
  350. /* The clock gating signal of the App core is invalid. We use RUNSTALL and RESETING
  351. signals to ensure that the App core stops running in single-core mode. */
  352. REG_SET_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RUNSTALL);
  353. REG_CLR_BIT(SYSTEM_CORE_1_CONTROL_0_REG, SYSTEM_CONTROL_CORE_1_RESETING);
  354. #endif
  355. #endif // CONFIG_IDF_TARGET_ESP32
  356. #endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  357. #endif // SOC_CPU_CORES_NUM > 1
  358. #if CONFIG_SPIRAM_MEMTEST
  359. if (esp_psram_is_initialized()) {
  360. bool ext_ram_ok = esp_psram_extram_test();
  361. if (!ext_ram_ok) {
  362. ESP_EARLY_LOGE(TAG, "External RAM failed memory test!");
  363. abort();
  364. }
  365. }
  366. #endif //CONFIG_SPIRAM_MEMTEST
  367. //TODO: IDF-5023, replace with MMU driver
  368. #if CONFIG_IDF_TARGET_ESP32S3
  369. int s_instr_flash2spiram_off = 0;
  370. int s_rodata_flash2spiram_off = 0;
  371. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  372. extern int instruction_flash2spiram_offset(void);
  373. s_instr_flash2spiram_off = instruction_flash2spiram_offset();
  374. #endif
  375. #if CONFIG_SPIRAM_RODATA
  376. extern int rodata_flash2spiram_offset(void);
  377. s_rodata_flash2spiram_off = rodata_flash2spiram_offset();
  378. #endif
  379. extern void Cache_Set_IDROM_MMU_Info(uint32_t instr_page_num, uint32_t rodata_page_num, uint32_t rodata_start, uint32_t rodata_end, int i_off, int ro_off);
  380. Cache_Set_IDROM_MMU_Info(cache_mmu_irom_size / sizeof(uint32_t), \
  381. cache_mmu_drom_size / sizeof(uint32_t), \
  382. (uint32_t)&_rodata_reserved_start, \
  383. (uint32_t)&_rodata_reserved_end, \
  384. s_instr_flash2spiram_off, \
  385. s_rodata_flash2spiram_off);
  386. #endif
  387. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP || \
  388. CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
  389. uint32_t icache_wrap_enable = 0, dcache_wrap_enable = 0;
  390. #if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP
  391. icache_wrap_enable = 1;
  392. #endif
  393. #if CONFIG_ESP32S2_DATA_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
  394. dcache_wrap_enable = 1;
  395. #endif
  396. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable, uint32_t dcache_wrap_enable);
  397. esp_enable_cache_wrap(icache_wrap_enable, dcache_wrap_enable);
  398. #endif
  399. #if CONFIG_ESP32S3_DATA_CACHE_16KB
  400. Cache_Invalidate_DCache_All();
  401. Cache_Occupy_Addr(SOC_DROM_LOW, 0x4000);
  402. #endif
  403. #if CONFIG_IDF_TARGET_ESP32C2
  404. // TODO : IDF-4194
  405. #if CONFIG_ESP32C2_INSTRUCTION_CACHE_WRAP
  406. extern void esp_enable_cache_wrap(uint32_t icache_wrap_enable);
  407. esp_enable_cache_wrap(1);
  408. #endif
  409. #endif
  410. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  411. memset(&_ext_ram_bss_start, 0, (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
  412. #endif
  413. //Enable trace memory and immediately start trace.
  414. #if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
  415. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S3
  416. #if CONFIG_ESP32_TRAX_TWOBANKS || CONFIG_ESP32S3_TRAX_TWOBANKS
  417. trax_enable(TRAX_ENA_PRO_APP);
  418. #else
  419. trax_enable(TRAX_ENA_PRO);
  420. #endif
  421. #elif CONFIG_IDF_TARGET_ESP32S2
  422. trax_enable(TRAX_ENA_PRO);
  423. #endif
  424. trax_start_trace(TRAX_DOWNCOUNT_WORDS);
  425. #endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
  426. esp_clk_init();
  427. esp_perip_clk_init();
  428. // Now that the clocks have been set-up, set the startup time from RTC
  429. // and default RTC-backed system time provider.
  430. g_startup_time = esp_rtc_get_time_us();
  431. // Clear interrupt matrix for PRO CPU core
  432. core_intr_matrix_clear();
  433. #ifndef CONFIG_IDF_ENV_FPGA // TODO: on FPGA it should be possible to configure this, not currently working with APB_CLK_FREQ changed
  434. #ifdef CONFIG_ESP_CONSOLE_UART
  435. uint32_t clock_hz = esp_clk_apb_freq();
  436. #if ESP_ROM_UART_CLK_IS_XTAL
  437. clock_hz = esp_clk_xtal_freq(); // From esp32-s3 on, UART clock source is selected to XTAL in ROM
  438. #endif
  439. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  440. esp_rom_uart_set_clock_baudrate(CONFIG_ESP_CONSOLE_UART_NUM, clock_hz, CONFIG_ESP_CONSOLE_UART_BAUDRATE);
  441. #endif
  442. #endif
  443. #if SOC_RTCIO_HOLD_SUPPORTED
  444. rtcio_hal_unhold_all();
  445. #else
  446. gpio_hal_force_unhold_all();
  447. #endif
  448. esp_cache_err_int_init();
  449. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE && !CONFIG_ESP_SYSTEM_MEMPROT_TEST
  450. // Memprot cannot be locked during OS startup as the lock-on prevents any PMS changes until a next reboot
  451. // If such a situation appears, it is likely an malicious attempt to bypass the system safety setup -> print error & reset
  452. #if CONFIG_IDF_TARGET_ESP32S2
  453. if (esp_memprot_is_locked_any()) {
  454. #else
  455. bool is_locked = false;
  456. if (esp_mprot_is_conf_locked_any(&is_locked) != ESP_OK || is_locked) {
  457. #endif
  458. ESP_EARLY_LOGE(TAG, "Memprot feature locked after the system reset! Potential safety corruption, rebooting.");
  459. esp_restart_noos_dig();
  460. }
  461. //default configuration of PMS Memprot
  462. esp_err_t memp_err = ESP_OK;
  463. #if CONFIG_IDF_TARGET_ESP32S2 //specific for ESP32S2 unless IDF-3024 is merged
  464. #if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  465. memp_err = esp_memprot_set_prot(PANIC_HNDL_ON, MEMPROT_LOCK, NULL);
  466. #else
  467. memp_err = esp_memprot_set_prot(PANIC_HNDL_ON, MEMPROT_UNLOCK, NULL);
  468. #endif
  469. #else //CONFIG_IDF_TARGET_ESP32S2 specific end
  470. esp_memp_config_t memp_cfg = ESP_MEMPROT_DEFAULT_CONFIG();
  471. #if !CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
  472. memp_cfg.lock_feature = false;
  473. #endif
  474. memp_err = esp_mprot_set_prot(&memp_cfg);
  475. #endif //other IDF_TARGETS end
  476. if (memp_err != ESP_OK) {
  477. ESP_EARLY_LOGE(TAG, "Failed to set Memprot feature (0x%08X: %s), rebooting.", memp_err, esp_err_to_name(memp_err));
  478. esp_restart_noos_dig();
  479. }
  480. #endif //CONFIG_ESP_SYSTEM_MEMPROT_FEATURE && !CONFIG_ESP_SYSTEM_MEMPROT_TEST
  481. // Read the application binary image header. This will also decrypt the header if the image is encrypted.
  482. __attribute__((unused)) esp_image_header_t fhdr = {0};
  483. #ifdef CONFIG_APP_BUILD_TYPE_ELF_RAM
  484. fhdr.spi_mode = ESP_IMAGE_SPI_MODE_DIO;
  485. fhdr.spi_speed = ESP_IMAGE_SPI_SPEED_DIV_2;
  486. fhdr.spi_size = ESP_IMAGE_FLASH_SIZE_4MB;
  487. extern void esp_rom_spiflash_attach(uint32_t, bool);
  488. #if !CONFIG_IDF_TARGET_ESP32C2
  489. esp_rom_spiflash_attach(esp_rom_efuse_get_flash_gpio_info(), false);
  490. #else
  491. // ESP32C2 cannot get flash_gpio_info from efuse
  492. esp_rom_spiflash_attach(0, false);
  493. #endif // CONFIG_IDF_TARGET_ESP32C2
  494. bootloader_flash_unlock();
  495. #else
  496. // This assumes that DROM is the first segment in the application binary, i.e. that we can read
  497. // the binary header through cache by accessing SOC_DROM_LOW address.
  498. #pragma GCC diagnostic push
  499. #if __GNUC__ >= 11
  500. #pragma GCC diagnostic ignored "-Wstringop-overread"
  501. #endif
  502. #pragma GCC diagnostic ignored "-Warray-bounds"
  503. memcpy(&fhdr, (void *) SOC_DROM_LOW, sizeof(fhdr));
  504. #pragma GCC diagnostic pop
  505. #endif // CONFIG_APP_BUILD_TYPE_ELF_RAM
  506. #if CONFIG_IDF_TARGET_ESP32
  507. #if !CONFIG_SPIRAM_BOOT_INIT
  508. // If psram is uninitialized, we need to improve some flash configuration.
  509. bootloader_flash_clock_config(&fhdr);
  510. bootloader_flash_gpio_config(&fhdr);
  511. bootloader_flash_dummy_config(&fhdr);
  512. bootloader_flash_cs_timing_config();
  513. #endif //!CONFIG_SPIRAM_BOOT_INIT
  514. #endif //CONFIG_IDF_TARGET_ESP32
  515. #if CONFIG_SPI_FLASH_SIZE_OVERRIDE
  516. int app_flash_size = esp_image_get_flash_size(fhdr.spi_size);
  517. if (app_flash_size < 1 * 1024 * 1024) {
  518. ESP_EARLY_LOGE(TAG, "Invalid flash size in app image header.");
  519. abort();
  520. }
  521. bootloader_flash_update_size(app_flash_size);
  522. #endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
  523. #if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
  524. s_cpu_inited[0] = true;
  525. volatile bool cpus_inited = false;
  526. while (!cpus_inited) {
  527. cpus_inited = true;
  528. for (int i = 0; i < SOC_CPU_CORES_NUM; i++) {
  529. cpus_inited &= s_cpu_inited[i];
  530. }
  531. esp_rom_delay_us(100);
  532. }
  533. #endif
  534. SYS_STARTUP_FN();
  535. }