flash_mmap.c 19 KB

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