flash_mmap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include <freertos/FreeRTOS.h>
  19. #include <freertos/task.h>
  20. #include <freertos/semphr.h>
  21. #include "soc/soc.h"
  22. #include "soc/soc_memory_layout.h"
  23. #include "soc/dport_access.h"
  24. #include "sdkconfig.h"
  25. #include "esp_attr.h"
  26. #include "esp_spi_flash.h"
  27. #include "esp_flash_encrypt.h"
  28. #include "esp_log.h"
  29. #include "cache_utils.h"
  30. #if CONFIG_IDF_TARGET_ESP32
  31. #include "soc/dport_reg.h"
  32. #include "esp32/rom/cache.h"
  33. #include "esp32/rom/spi_flash.h"
  34. #include "esp32/spiram.h"
  35. #include "soc/mmu.h"
  36. #elif CONFIG_IDF_TARGET_ESP32S2
  37. #include "esp32s2/rom/cache.h"
  38. #include "esp32s2/rom/spi_flash.h"
  39. #include "esp32s2/spiram.h"
  40. #include "soc/extmem_reg.h"
  41. #include "soc/cache_memory.h"
  42. #include "soc/mmu.h"
  43. #elif CONFIG_IDF_TARGET_ESP32S3
  44. #include "esp32s3/rom/spi_flash.h"
  45. #include "esp32s3/rom/cache.h"
  46. #include "esp32s3/spiram.h"
  47. #include "soc/extmem_reg.h"
  48. #include "soc/cache_memory.h"
  49. #include "soc/mmu.h"
  50. #elif CONFIG_IDF_TARGET_ESP32C3
  51. #include "esp32c3/rom/cache.h"
  52. #include "esp32c3/rom/spi_flash.h"
  53. #include "soc/cache_memory.h"
  54. #include "soc/mmu.h"
  55. #elif CONFIG_IDF_TARGET_ESP32H2
  56. #include "esp32h2/rom/cache.h"
  57. #include "esp32h2/rom/spi_flash.h"
  58. #include "soc/cache_memory.h"
  59. #include "soc/mmu.h"
  60. #endif
  61. #ifndef NDEBUG
  62. // Enable built-in checks in queue.h in debug builds
  63. #define INVARIANTS
  64. #endif
  65. #include "sys/queue.h"
  66. #define IROM0_PAGES_NUM (SOC_MMU_IROM0_PAGES_END - SOC_MMU_IROM0_PAGES_START)
  67. #define DROM0_PAGES_NUM (SOC_MMU_DROM0_PAGES_END - SOC_MMU_DROM0_PAGES_START)
  68. #define PAGES_LIMIT ((SOC_MMU_IROM0_PAGES_END > SOC_MMU_DROM0_PAGES_END) ? SOC_MMU_IROM0_PAGES_END:SOC_MMU_DROM0_PAGES_END)
  69. #if !CONFIG_SPI_FLASH_ROM_IMPL
  70. typedef struct mmap_entry_{
  71. uint32_t handle;
  72. int page;
  73. int count;
  74. LIST_ENTRY(mmap_entry_) entries;
  75. } mmap_entry_t;
  76. static LIST_HEAD(mmap_entries_head, mmap_entry_) s_mmap_entries_head =
  77. LIST_HEAD_INITIALIZER(s_mmap_entries_head);
  78. static uint8_t s_mmap_page_refcnt[SOC_MMU_REGIONS_COUNT * SOC_MMU_PAGES_PER_REGION] = {0};
  79. static uint32_t s_mmap_last_handle = 0;
  80. static void IRAM_ATTR spi_flash_mmap_init(void)
  81. {
  82. if (s_mmap_page_refcnt[SOC_MMU_DROM0_PAGES_START] != 0) {
  83. return; /* mmap data already initialised */
  84. }
  85. DPORT_INTERRUPT_DISABLE();
  86. for (int i = 0; i < SOC_MMU_REGIONS_COUNT * SOC_MMU_PAGES_PER_REGION; ++i) {
  87. uint32_t entry_pro = DPORT_SEQUENCE_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i]);
  88. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  89. uint32_t entry_app = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_APP_FLASH_MMU_TABLE[i]);
  90. if (entry_pro != entry_app) {
  91. // clean up entries used by boot loader
  92. entry_pro = SOC_MMU_INVALID_ENTRY_VAL;
  93. SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i] = SOC_MMU_INVALID_ENTRY_VAL;
  94. }
  95. #endif
  96. if ((entry_pro & SOC_MMU_INVALID_ENTRY_VAL) == 0 && (i == SOC_MMU_DROM0_PAGES_START || i == SOC_MMU_PRO_IRAM0_FIRST_USABLE_PAGE || entry_pro != 0)) {
  97. s_mmap_page_refcnt[i] = 1;
  98. } else {
  99. SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i] = SOC_MMU_INVALID_ENTRY_VAL;
  100. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  101. DPORT_APP_FLASH_MMU_TABLE[i] = SOC_MMU_INVALID_ENTRY_VAL;
  102. #endif
  103. }
  104. }
  105. DPORT_INTERRUPT_RESTORE();
  106. }
  107. static void IRAM_ATTR get_mmu_region(spi_flash_mmap_memory_t memory, int* out_begin, int* out_size,uint32_t* region_addr)
  108. {
  109. if (memory == SPI_FLASH_MMAP_DATA) {
  110. // Vaddr0
  111. *out_begin = SOC_MMU_DROM0_PAGES_START;
  112. *out_size = DROM0_PAGES_NUM;
  113. *region_addr = SOC_MMU_VADDR0_START_ADDR;
  114. } else {
  115. // only part of VAddr1 is usable, so adjust for that
  116. *out_begin = SOC_MMU_PRO_IRAM0_FIRST_USABLE_PAGE;
  117. *out_size = SOC_MMU_IROM0_PAGES_END - *out_begin;
  118. *region_addr = SOC_MMU_VADDR1_FIRST_USABLE_ADDR;
  119. }
  120. }
  121. esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
  122. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  123. {
  124. esp_err_t ret;
  125. if (src_addr & 0xffff) {
  126. return ESP_ERR_INVALID_ARG;
  127. }
  128. if (src_addr + size > g_rom_flashchip.chip_size) {
  129. return ESP_ERR_INVALID_ARG;
  130. }
  131. // region which should be mapped
  132. int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
  133. int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
  134. // prepare a linear pages array to feed into spi_flash_mmap_pages
  135. int *pages = heap_caps_malloc(sizeof(int)*page_count, MALLOC_CAP_INTERNAL);
  136. if (pages == NULL) {
  137. return ESP_ERR_NO_MEM;
  138. }
  139. for (int i = 0; i < page_count; i++) {
  140. pages[i] = (phys_page+i);
  141. }
  142. ret = spi_flash_mmap_pages(pages, page_count, memory, out_ptr, out_handle);
  143. free(pages);
  144. return ret;
  145. }
  146. esp_err_t IRAM_ATTR spi_flash_mmap_pages(const int *pages, size_t page_count, spi_flash_mmap_memory_t memory,
  147. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  148. {
  149. esp_err_t ret;
  150. const void* temp_ptr = *out_ptr = NULL;
  151. spi_flash_mmap_handle_t temp_handle = *out_handle = (spi_flash_mmap_handle_t)NULL;
  152. bool need_flush = false;
  153. if (!page_count) {
  154. return ESP_ERR_INVALID_ARG;
  155. }
  156. if (!esp_ptr_internal(pages)) {
  157. return ESP_ERR_INVALID_ARG;
  158. }
  159. for (int i = 0; i < page_count; i++) {
  160. if (pages[i] < 0 || pages[i]*SPI_FLASH_MMU_PAGE_SIZE >= g_rom_flashchip.chip_size) {
  161. return ESP_ERR_INVALID_ARG;
  162. }
  163. }
  164. mmap_entry_t* new_entry = (mmap_entry_t*) heap_caps_malloc(sizeof(mmap_entry_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  165. if (new_entry == 0) {
  166. return ESP_ERR_NO_MEM;
  167. }
  168. spi_flash_disable_interrupts_caches_and_other_cpu();
  169. spi_flash_mmap_init();
  170. // figure out the memory region where we should look for pages
  171. int region_begin; // first page to check
  172. int region_size; // number of pages to check
  173. uint32_t region_addr; // base address of memory region
  174. get_mmu_region(memory,&region_begin,&region_size,&region_addr);
  175. if (region_size < page_count) {
  176. spi_flash_enable_interrupts_caches_and_other_cpu();
  177. return ESP_ERR_NO_MEM;
  178. }
  179. // The following part searches for a range of MMU entries which can be used.
  180. // Algorithm is essentially naïve strstr algorithm, except that unused MMU
  181. // entries are treated as wildcards.
  182. int start;
  183. // the " + 1" is a fix when loop the MMU table pages, because the last MMU page
  184. // is valid as well if it have not been used
  185. int end = region_begin + region_size - page_count + 1;
  186. for (start = region_begin; start < end; ++start) {
  187. int pageno = 0;
  188. int pos;
  189. DPORT_INTERRUPT_DISABLE();
  190. for (pos = start; pos < start + page_count; ++pos, ++pageno) {
  191. int table_val = (int) DPORT_SEQUENCE_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[pos]);
  192. uint8_t refcnt = s_mmap_page_refcnt[pos];
  193. if (refcnt != 0 && table_val != SOC_MMU_PAGE_IN_FLASH(pages[pageno])) {
  194. break;
  195. }
  196. }
  197. DPORT_INTERRUPT_RESTORE();
  198. // whole mapping range matched, bail out
  199. if (pos - start == page_count) {
  200. break;
  201. }
  202. }
  203. // checked all the region(s) and haven't found anything?
  204. if (start == end) {
  205. ret = ESP_ERR_NO_MEM;
  206. } else {
  207. // set up mapping using pages
  208. uint32_t pageno = 0;
  209. DPORT_INTERRUPT_DISABLE();
  210. for (int i = start; i != start + page_count; ++i, ++pageno) {
  211. // sanity check: we won't reconfigure entries with non-zero reference count
  212. uint32_t entry_pro = DPORT_SEQUENCE_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i]);
  213. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  214. uint32_t entry_app = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_APP_FLASH_MMU_TABLE[i]);
  215. #endif
  216. assert(s_mmap_page_refcnt[i] == 0 ||
  217. (entry_pro == SOC_MMU_PAGE_IN_FLASH(pages[pageno])
  218. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  219. && entry_app == SOC_MMU_PAGE_IN_FLASH(pages[pageno])
  220. #endif
  221. ));
  222. if (s_mmap_page_refcnt[i] == 0) {
  223. if (entry_pro != SOC_MMU_PAGE_IN_FLASH(pages[pageno])
  224. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  225. || entry_app != SOC_MMU_PAGE_IN_FLASH(pages[pageno])
  226. #endif
  227. ) {
  228. SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i] = SOC_MMU_PAGE_IN_FLASH(pages[pageno]);
  229. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  230. DPORT_APP_FLASH_MMU_TABLE[i] = pages[pageno];
  231. #endif
  232. #if !CONFIG_IDF_TARGET_ESP32
  233. Cache_Invalidate_Addr(region_addr + (i - region_begin) * SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMU_PAGE_SIZE);
  234. #endif
  235. need_flush = true;
  236. }
  237. }
  238. ++s_mmap_page_refcnt[i];
  239. }
  240. DPORT_INTERRUPT_RESTORE();
  241. LIST_INSERT_HEAD(&s_mmap_entries_head, new_entry, entries);
  242. new_entry->page = start;
  243. new_entry->count = page_count;
  244. new_entry->handle = ++s_mmap_last_handle;
  245. temp_handle = new_entry->handle;
  246. temp_ptr = (void*) (region_addr + (start - region_begin) * SPI_FLASH_MMU_PAGE_SIZE);
  247. ret = ESP_OK;
  248. }
  249. /* This is a temporary fix for an issue where some
  250. cache reads may see stale data.
  251. Working on a long term fix that doesn't require invalidating
  252. entire cache.
  253. */
  254. if (need_flush) {
  255. #if CONFIG_IDF_TARGET_ESP32
  256. #if CONFIG_SPIRAM
  257. esp_spiram_writeback_cache();
  258. #endif // CONFIG_SPIRAM
  259. Cache_Flush(0);
  260. #if !CONFIG_FREERTOS_UNICORE
  261. Cache_Flush(1);
  262. #endif // !CONFIG_FREERTOS_UNICORE
  263. #endif // CONFIG_IDF_TARGET_ESP32
  264. }
  265. spi_flash_enable_interrupts_caches_and_other_cpu();
  266. if (temp_ptr == NULL) {
  267. free(new_entry);
  268. }
  269. *out_ptr = temp_ptr;
  270. *out_handle = temp_handle;
  271. return ret;
  272. }
  273. void IRAM_ATTR spi_flash_munmap(spi_flash_mmap_handle_t handle)
  274. {
  275. spi_flash_disable_interrupts_caches_and_other_cpu();
  276. mmap_entry_t* it;
  277. // look for handle in linked list
  278. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  279. if (it->handle == handle) {
  280. // for each page, decrement reference counter
  281. // if reference count is zero, disable MMU table entry to
  282. // facilitate debugging of use-after-free conditions
  283. for (int i = it->page; i < it->page + it->count; ++i) {
  284. assert(s_mmap_page_refcnt[i] > 0);
  285. if (--s_mmap_page_refcnt[i] == 0) {
  286. SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i] = SOC_MMU_INVALID_ENTRY_VAL;
  287. #if !CONFIG_FREERTOS_UNICORE && CONFIG_IDF_TARGET_ESP32
  288. DPORT_APP_FLASH_MMU_TABLE[i] = SOC_MMU_INVALID_ENTRY_VAL;
  289. #endif
  290. }
  291. }
  292. LIST_REMOVE(it, entries);
  293. break;
  294. }
  295. }
  296. spi_flash_enable_interrupts_caches_and_other_cpu();
  297. if (it == NULL) {
  298. assert(0 && "invalid handle, or handle already unmapped");
  299. }
  300. free(it);
  301. }
  302. static void IRAM_ATTR NOINLINE_ATTR spi_flash_protected_mmap_init(void)
  303. {
  304. spi_flash_disable_interrupts_caches_and_other_cpu();
  305. spi_flash_mmap_init();
  306. spi_flash_enable_interrupts_caches_and_other_cpu();
  307. }
  308. static uint32_t IRAM_ATTR NOINLINE_ATTR spi_flash_protected_read_mmu_entry(int index)
  309. {
  310. uint32_t value;
  311. spi_flash_disable_interrupts_caches_and_other_cpu();
  312. value = DPORT_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[index]);
  313. spi_flash_enable_interrupts_caches_and_other_cpu();
  314. return value;
  315. }
  316. void spi_flash_mmap_dump(void)
  317. {
  318. spi_flash_protected_mmap_init();
  319. mmap_entry_t* it;
  320. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  321. printf("handle=%d page=%d count=%d\n", it->handle, it->page, it->count);
  322. }
  323. for (int i = 0; i < SOC_MMU_REGIONS_COUNT * SOC_MMU_PAGES_PER_REGION; ++i) {
  324. if (s_mmap_page_refcnt[i] != 0) {
  325. uint32_t paddr = spi_flash_protected_read_mmu_entry(i);
  326. printf("page %d: refcnt=%d paddr=%d\n", i, (int) s_mmap_page_refcnt[i], paddr);
  327. }
  328. }
  329. }
  330. uint32_t IRAM_ATTR spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory)
  331. {
  332. spi_flash_disable_interrupts_caches_and_other_cpu();
  333. spi_flash_mmap_init();
  334. int count = 0;
  335. int region_begin; // first page to check
  336. int region_size; // number of pages to check
  337. uint32_t region_addr; // base address of memory region
  338. get_mmu_region(memory,&region_begin,&region_size,&region_addr);
  339. DPORT_INTERRUPT_DISABLE();
  340. for (int i = region_begin; i < region_begin + region_size; ++i) {
  341. if (s_mmap_page_refcnt[i] == 0 && DPORT_SEQUENCE_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i]) == SOC_MMU_INVALID_ENTRY_VAL) {
  342. count++;
  343. }
  344. }
  345. DPORT_INTERRUPT_RESTORE();
  346. spi_flash_enable_interrupts_caches_and_other_cpu();
  347. return count;
  348. }
  349. size_t spi_flash_cache2phys(const void *cached)
  350. {
  351. intptr_t c = (intptr_t)cached;
  352. size_t cache_page;
  353. int offset = 0;
  354. if (c >= SOC_MMU_VADDR1_START_ADDR && c < SOC_MMU_VADDR1_FIRST_USABLE_ADDR) {
  355. /* IRAM address, doesn't map to flash */
  356. return SPI_FLASH_CACHE2PHYS_FAIL;
  357. }
  358. if (c < SOC_MMU_VADDR1_FIRST_USABLE_ADDR) {
  359. /* expect cache is in DROM */
  360. cache_page = (c - SOC_MMU_VADDR0_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + SOC_MMU_DROM0_PAGES_START;
  361. #if CONFIG_SPIRAM_RODATA
  362. if (c >= (uint32_t)&_rodata_reserved_start && c <= (uint32_t)&_rodata_reserved_end) {
  363. offset = rodata_flash2spiram_offset();
  364. }
  365. #endif
  366. } else {
  367. /* expect cache is in IROM */
  368. cache_page = (c - SOC_MMU_VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + SOC_MMU_IROM0_PAGES_START;
  369. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  370. if (c >= (uint32_t)&_instruction_reserved_start && c <= (uint32_t)&_instruction_reserved_end) {
  371. offset = instruction_flash2spiram_offset();
  372. }
  373. #endif
  374. }
  375. if (cache_page >= PAGES_LIMIT) {
  376. /* cached address was not in IROM or DROM */
  377. return SPI_FLASH_CACHE2PHYS_FAIL;
  378. }
  379. uint32_t phys_page = spi_flash_protected_read_mmu_entry(cache_page);
  380. if (phys_page == SOC_MMU_INVALID_ENTRY_VAL) {
  381. /* page is not mapped */
  382. return SPI_FLASH_CACHE2PHYS_FAIL;
  383. }
  384. uint32_t phys_offs = ((phys_page & SOC_MMU_ADDR_MASK) + offset) * SPI_FLASH_MMU_PAGE_SIZE;
  385. return phys_offs | (c & (SPI_FLASH_MMU_PAGE_SIZE-1));
  386. }
  387. const void *IRAM_ATTR spi_flash_phys2cache(size_t phys_offs, spi_flash_mmap_memory_t memory)
  388. {
  389. uint32_t phys_page = phys_offs / SPI_FLASH_MMU_PAGE_SIZE;
  390. int start, end, page_delta;
  391. intptr_t base;
  392. if (memory == SPI_FLASH_MMAP_DATA) {
  393. start = SOC_MMU_DROM0_PAGES_START;
  394. end = SOC_MMU_DROM0_PAGES_END;
  395. base = SOC_MMU_VADDR0_START_ADDR;
  396. page_delta = SOC_MMU_DROM0_PAGES_START;
  397. } else {
  398. start = SOC_MMU_PRO_IRAM0_FIRST_USABLE_PAGE;
  399. end = SOC_MMU_IROM0_PAGES_END;
  400. base = SOC_MMU_VADDR1_START_ADDR;
  401. page_delta = SOC_MMU_IROM0_PAGES_START;
  402. }
  403. spi_flash_disable_interrupts_caches_and_other_cpu();
  404. DPORT_INTERRUPT_DISABLE();
  405. for (int i = start; i < end; i++) {
  406. uint32_t mmu_value = DPORT_SEQUENCE_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i]);
  407. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  408. if (phys_page >= instruction_flash_start_page_get() && phys_page <= instruction_flash_end_page_get()) {
  409. if (mmu_value & MMU_ACCESS_SPIRAM) {
  410. mmu_value += instruction_flash2spiram_offset();
  411. mmu_value = (mmu_value & SOC_MMU_ADDR_MASK) | MMU_ACCESS_FLASH;
  412. }
  413. }
  414. #endif
  415. #if CONFIG_SPIRAM_RODATA
  416. if (phys_page >= rodata_flash_start_page_get() && phys_page <= rodata_flash_start_page_get()) {
  417. if (mmu_value & MMU_ACCESS_SPIRAM) {
  418. mmu_value += rodata_flash2spiram_offset();
  419. mmu_value = (mmu_value & SOC_MMU_ADDR_MASK) | MMU_ACCESS_FLASH;
  420. }
  421. }
  422. #endif
  423. if (mmu_value == SOC_MMU_PAGE_IN_FLASH(phys_page)) {
  424. i -= page_delta;
  425. intptr_t cache_page = base + (SPI_FLASH_MMU_PAGE_SIZE * i);
  426. DPORT_INTERRUPT_RESTORE();
  427. spi_flash_enable_interrupts_caches_and_other_cpu();
  428. return (const void *) (cache_page | (phys_offs & (SPI_FLASH_MMU_PAGE_SIZE-1)));
  429. }
  430. }
  431. DPORT_INTERRUPT_RESTORE();
  432. spi_flash_enable_interrupts_caches_and_other_cpu();
  433. return NULL;
  434. }
  435. static bool IRAM_ATTR is_page_mapped_in_cache(uint32_t phys_page, const void **out_ptr)
  436. {
  437. int start[2], end[2];
  438. *out_ptr = NULL;
  439. /* SPI_FLASH_MMAP_DATA */
  440. start[0] = SOC_MMU_DROM0_PAGES_START;
  441. end[0] = SOC_MMU_DROM0_PAGES_END;
  442. /* SPI_FLASH_MMAP_INST */
  443. start[1] = SOC_MMU_PRO_IRAM0_FIRST_USABLE_PAGE;
  444. end[1] = SOC_MMU_IROM0_PAGES_END;
  445. DPORT_INTERRUPT_DISABLE();
  446. for (int j = 0; j < 2; j++) {
  447. for (int i = start[j]; i < end[j]; i++) {
  448. if (DPORT_SEQUENCE_REG_READ((uint32_t)&SOC_MMU_DPORT_PRO_FLASH_MMU_TABLE[i]) == SOC_MMU_PAGE_IN_FLASH(phys_page)) {
  449. #if !CONFIG_IDF_TARGET_ESP32
  450. if (j == 0) { /* SPI_FLASH_MMAP_DATA */
  451. *out_ptr = (const void *)(SOC_MMU_VADDR0_START_ADDR + SPI_FLASH_MMU_PAGE_SIZE * (i - start[0]));
  452. } else { /* SPI_FLASH_MMAP_INST */
  453. *out_ptr = (const void *)(SOC_MMU_VADDR1_FIRST_USABLE_ADDR + SPI_FLASH_MMU_PAGE_SIZE * (i - start[1]));
  454. }
  455. #endif
  456. DPORT_INTERRUPT_RESTORE();
  457. return true;
  458. }
  459. }
  460. }
  461. DPORT_INTERRUPT_RESTORE();
  462. return false;
  463. }
  464. /* Validates if given flash address has corresponding cache mapping, if yes, flushes cache memories */
  465. IRAM_ATTR bool spi_flash_check_and_flush_cache(size_t start_addr, size_t length)
  466. {
  467. bool ret = false;
  468. /* align start_addr & length to full MMU pages */
  469. uint32_t page_start_addr = start_addr & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  470. length += (start_addr - page_start_addr);
  471. length = (length + SPI_FLASH_MMU_PAGE_SIZE - 1) & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  472. for (uint32_t addr = page_start_addr; addr < page_start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
  473. uint32_t page = addr / SPI_FLASH_MMU_PAGE_SIZE;
  474. if (page >= 256) {
  475. return false; /* invalid address */
  476. }
  477. const void *vaddr = NULL;
  478. if (is_page_mapped_in_cache(page, &vaddr)) {
  479. #if CONFIG_IDF_TARGET_ESP32
  480. #if CONFIG_SPIRAM
  481. esp_spiram_writeback_cache();
  482. #endif
  483. Cache_Flush(0);
  484. #ifndef CONFIG_FREERTOS_UNICORE
  485. Cache_Flush(1);
  486. #endif
  487. return true;
  488. #else // CONFIG_IDF_TARGET_ESP32
  489. if (vaddr != NULL) {
  490. Cache_Invalidate_Addr((uint32_t)vaddr, SPI_FLASH_MMU_PAGE_SIZE);
  491. ret = true;
  492. }
  493. #endif // CONFIG_IDF_TARGET_ESP32
  494. }
  495. }
  496. return ret;
  497. }
  498. #endif //!CONFIG_SPI_FLASH_ROM_IMPL