flash_mmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 <rom/spi_flash.h>
  22. #include <rom/cache.h>
  23. #include <soc/soc.h>
  24. #include <soc/dport_reg.h>
  25. #include "sdkconfig.h"
  26. #include "esp_ipc.h"
  27. #include "esp_attr.h"
  28. #include "esp_spi_flash.h"
  29. #include "esp_flash_encrypt.h"
  30. #include "esp_log.h"
  31. #include "cache_utils.h"
  32. #include "esp_spiram.h"
  33. #ifndef NDEBUG
  34. // Enable built-in checks in queue.h in debug builds
  35. #define INVARIANTS
  36. #endif
  37. #include "rom/queue.h"
  38. #define REGIONS_COUNT 4
  39. #define PAGES_PER_REGION 64
  40. #define INVALID_ENTRY_VAL 0x100
  41. #define VADDR0_START_ADDR 0x3F400000
  42. #define VADDR1_START_ADDR 0x40000000
  43. #define VADDR1_FIRST_USABLE_ADDR 0x400D0000
  44. #define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64)
  45. typedef struct mmap_entry_{
  46. uint32_t handle;
  47. int page;
  48. int count;
  49. LIST_ENTRY(mmap_entry_) entries;
  50. } mmap_entry_t;
  51. static LIST_HEAD(mmap_entries_head, mmap_entry_) s_mmap_entries_head =
  52. LIST_HEAD_INITIALIZER(s_mmap_entries_head);
  53. static uint8_t s_mmap_page_refcnt[REGIONS_COUNT * PAGES_PER_REGION] = {0};
  54. static uint32_t s_mmap_last_handle = 0;
  55. static void IRAM_ATTR spi_flash_mmap_init()
  56. {
  57. if (s_mmap_page_refcnt[0] != 0) {
  58. return; /* mmap data already initialised */
  59. }
  60. DPORT_INTERRUPT_DISABLE();
  61. for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
  62. uint32_t entry_pro = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]);
  63. uint32_t entry_app = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_APP_FLASH_MMU_TABLE[i]);
  64. if (entry_pro != entry_app) {
  65. // clean up entries used by boot loader
  66. entry_pro = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  67. DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  68. }
  69. if ((entry_pro & INVALID_ENTRY_VAL) == 0 && (i == 0 || i == PRO_IRAM0_FIRST_USABLE_PAGE || entry_pro != 0)) {
  70. s_mmap_page_refcnt[i] = 1;
  71. } else {
  72. DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  73. DPORT_APP_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
  74. }
  75. }
  76. DPORT_INTERRUPT_RESTORE();
  77. }
  78. static void IRAM_ATTR get_mmu_region(spi_flash_mmap_memory_t memory, int* out_begin, int* out_size,uint32_t* region_addr)
  79. {
  80. if (memory == SPI_FLASH_MMAP_DATA) {
  81. // Vaddr0
  82. *out_begin = 0;
  83. *out_size = 64;
  84. *region_addr = VADDR0_START_ADDR;
  85. } else {
  86. // only part of VAddr1 is usable, so adjust for that
  87. *out_begin = PRO_IRAM0_FIRST_USABLE_PAGE;
  88. *out_size = 3 * 64 - *out_begin;
  89. *region_addr = VADDR1_FIRST_USABLE_ADDR;
  90. }
  91. }
  92. esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
  93. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  94. {
  95. esp_err_t ret;
  96. if (src_addr & 0xffff) {
  97. return ESP_ERR_INVALID_ARG;
  98. }
  99. if (src_addr + size > g_rom_flashchip.chip_size) {
  100. return ESP_ERR_INVALID_ARG;
  101. }
  102. // region which should be mapped
  103. int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
  104. int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
  105. // prepare a linear pages array to feed into spi_flash_mmap_pages
  106. int *pages = heap_caps_malloc(sizeof(int)*page_count, MALLOC_CAP_INTERNAL);
  107. if (pages == NULL) {
  108. return ESP_ERR_NO_MEM;
  109. }
  110. for (int i = 0; i < page_count; i++) {
  111. pages[i] = phys_page+i;
  112. }
  113. ret = spi_flash_mmap_pages(pages, page_count, memory, out_ptr, out_handle);
  114. free(pages);
  115. return ret;
  116. }
  117. esp_err_t IRAM_ATTR spi_flash_mmap_pages(const int *pages, size_t page_count, spi_flash_mmap_memory_t memory,
  118. const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
  119. {
  120. esp_err_t ret;
  121. bool need_flush = false;
  122. if (!page_count) {
  123. return ESP_ERR_INVALID_ARG;
  124. }
  125. if (!esp_ptr_internal(pages)) {
  126. return ESP_ERR_INVALID_ARG;
  127. }
  128. for (int i = 0; i < page_count; i++) {
  129. if (pages[i] < 0 || pages[i]*SPI_FLASH_MMU_PAGE_SIZE >= g_rom_flashchip.chip_size) {
  130. return ESP_ERR_INVALID_ARG;
  131. }
  132. }
  133. mmap_entry_t* new_entry = (mmap_entry_t*) heap_caps_malloc(sizeof(mmap_entry_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
  134. if (new_entry == 0) {
  135. return ESP_ERR_NO_MEM;
  136. }
  137. spi_flash_disable_interrupts_caches_and_other_cpu();
  138. spi_flash_mmap_init();
  139. // figure out the memory region where we should look for pages
  140. int region_begin; // first page to check
  141. int region_size; // number of pages to check
  142. uint32_t region_addr; // base address of memory region
  143. get_mmu_region(memory,&region_begin,&region_size,&region_addr);
  144. if (region_size < page_count) {
  145. spi_flash_enable_interrupts_caches_and_other_cpu();
  146. return ESP_ERR_NO_MEM;
  147. }
  148. // The following part searches for a range of MMU entries which can be used.
  149. // Algorithm is essentially naïve strstr algorithm, except that unused MMU
  150. // entries are treated as wildcards.
  151. int start;
  152. // the " + 1" is a fix when loop the MMU table pages, because the last MMU page
  153. // is valid as well if it have not been used
  154. int end = region_begin + region_size - page_count + 1;
  155. for (start = region_begin; start < end; ++start) {
  156. int pageno = 0;
  157. int pos;
  158. DPORT_INTERRUPT_DISABLE();
  159. for (pos = start; pos < start + page_count; ++pos, ++pageno) {
  160. int table_val = (int) DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[pos]);
  161. uint8_t refcnt = s_mmap_page_refcnt[pos];
  162. if (refcnt != 0 && table_val != pages[pageno]) {
  163. break;
  164. }
  165. }
  166. DPORT_INTERRUPT_RESTORE();
  167. // whole mapping range matched, bail out
  168. if (pos - start == page_count) {
  169. break;
  170. }
  171. }
  172. // checked all the region(s) and haven't found anything?
  173. if (start == end) {
  174. *out_handle = 0;
  175. *out_ptr = NULL;
  176. ret = ESP_ERR_NO_MEM;
  177. } else {
  178. // set up mapping using pages
  179. uint32_t pageno = 0;
  180. DPORT_INTERRUPT_DISABLE();
  181. for (int i = start; i != start + page_count; ++i, ++pageno) {
  182. // sanity check: we won't reconfigure entries with non-zero reference count
  183. uint32_t entry_pro = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]);
  184. uint32_t entry_app = DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_APP_FLASH_MMU_TABLE[i]);
  185. assert(s_mmap_page_refcnt[i] == 0 ||
  186. (entry_pro == pages[pageno] &&
  187. entry_app == pages[pageno]));
  188. if (s_mmap_page_refcnt[i] == 0) {
  189. if (entry_pro != pages[pageno] || entry_app != pages[pageno]) {
  190. DPORT_PRO_FLASH_MMU_TABLE[i] = pages[pageno];
  191. DPORT_APP_FLASH_MMU_TABLE[i] = pages[pageno];
  192. need_flush = true;
  193. }
  194. }
  195. ++s_mmap_page_refcnt[i];
  196. }
  197. DPORT_INTERRUPT_RESTORE();
  198. LIST_INSERT_HEAD(&s_mmap_entries_head, new_entry, entries);
  199. new_entry->page = start;
  200. new_entry->count = page_count;
  201. new_entry->handle = ++s_mmap_last_handle;
  202. *out_handle = new_entry->handle;
  203. *out_ptr = (void*) (region_addr + (start - region_begin) * SPI_FLASH_MMU_PAGE_SIZE);
  204. ret = ESP_OK;
  205. }
  206. /* This is a temporary fix for an issue where some
  207. cache reads may see stale data.
  208. Working on a long term fix that doesn't require invalidating
  209. entire cache.
  210. */
  211. if (need_flush) {
  212. #if CONFIG_SPIRAM_SUPPORT
  213. esp_spiram_writeback_cache();
  214. #endif
  215. Cache_Flush(0);
  216. Cache_Flush(1);
  217. }
  218. spi_flash_enable_interrupts_caches_and_other_cpu();
  219. if (*out_ptr == NULL) {
  220. free(new_entry);
  221. }
  222. return ret;
  223. }
  224. void IRAM_ATTR spi_flash_munmap(spi_flash_mmap_handle_t handle)
  225. {
  226. spi_flash_disable_interrupts_caches_and_other_cpu();
  227. mmap_entry_t* it;
  228. // look for handle in linked list
  229. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  230. if (it->handle == handle) {
  231. // for each page, decrement reference counter
  232. // if reference count is zero, disable MMU table entry to
  233. // facilitate debugging of use-after-free conditions
  234. for (int i = it->page; i < it->page + it->count; ++i) {
  235. assert(s_mmap_page_refcnt[i] > 0);
  236. if (--s_mmap_page_refcnt[i] == 0) {
  237. DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  238. DPORT_APP_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
  239. }
  240. }
  241. LIST_REMOVE(it, entries);
  242. break;
  243. }
  244. }
  245. spi_flash_enable_interrupts_caches_and_other_cpu();
  246. if (it == NULL) {
  247. assert(0 && "invalid handle, or handle already unmapped");
  248. }
  249. free(it);
  250. }
  251. static void IRAM_ATTR NOINLINE_ATTR spi_flash_protected_mmap_init()
  252. {
  253. spi_flash_disable_interrupts_caches_and_other_cpu();
  254. spi_flash_mmap_init();
  255. spi_flash_enable_interrupts_caches_and_other_cpu();
  256. }
  257. static uint32_t IRAM_ATTR NOINLINE_ATTR spi_flash_protected_read_mmu_entry(int index)
  258. {
  259. uint32_t value;
  260. spi_flash_disable_interrupts_caches_and_other_cpu();
  261. value = DPORT_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[index]);
  262. spi_flash_enable_interrupts_caches_and_other_cpu();
  263. return value;
  264. }
  265. void spi_flash_mmap_dump()
  266. {
  267. spi_flash_protected_mmap_init();
  268. mmap_entry_t* it;
  269. for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
  270. printf("handle=%d page=%d count=%d\n", it->handle, it->page, it->count);
  271. }
  272. for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
  273. if (s_mmap_page_refcnt[i] != 0) {
  274. uint32_t paddr = spi_flash_protected_read_mmu_entry(i);
  275. printf("page %d: refcnt=%d paddr=%d\n", i, (int) s_mmap_page_refcnt[i], paddr);
  276. }
  277. }
  278. }
  279. uint32_t IRAM_ATTR spi_flash_mmap_get_free_pages(spi_flash_mmap_memory_t memory)
  280. {
  281. spi_flash_disable_interrupts_caches_and_other_cpu();
  282. spi_flash_mmap_init();
  283. int count = 0;
  284. int region_begin; // first page to check
  285. int region_size; // number of pages to check
  286. uint32_t region_addr; // base address of memory region
  287. get_mmu_region(memory,&region_begin,&region_size,&region_addr);
  288. DPORT_INTERRUPT_DISABLE();
  289. for (int i = region_begin; i < region_begin + region_size; ++i) {
  290. if (s_mmap_page_refcnt[i] == 0 && DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]) == INVALID_ENTRY_VAL) {
  291. count++;
  292. }
  293. }
  294. DPORT_INTERRUPT_RESTORE();
  295. spi_flash_enable_interrupts_caches_and_other_cpu();
  296. return count;
  297. }
  298. uint32_t spi_flash_cache2phys(const void *cached)
  299. {
  300. intptr_t c = (intptr_t)cached;
  301. size_t cache_page;
  302. if (c >= VADDR1_START_ADDR && c < VADDR1_FIRST_USABLE_ADDR) {
  303. /* IRAM address, doesn't map to flash */
  304. return SPI_FLASH_CACHE2PHYS_FAIL;
  305. }
  306. else if (c < VADDR1_FIRST_USABLE_ADDR) {
  307. /* expect cache is in DROM */
  308. cache_page = (c - VADDR0_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE;
  309. } else {
  310. /* expect cache is in IROM */
  311. cache_page = (c - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64;
  312. }
  313. if (cache_page >= 256) {
  314. /* cached address was not in IROM or DROM */
  315. return SPI_FLASH_CACHE2PHYS_FAIL;
  316. }
  317. uint32_t phys_page = spi_flash_protected_read_mmu_entry(cache_page);
  318. if (phys_page == INVALID_ENTRY_VAL) {
  319. /* page is not mapped */
  320. return SPI_FLASH_CACHE2PHYS_FAIL;
  321. }
  322. uint32_t phys_offs = phys_page * SPI_FLASH_MMU_PAGE_SIZE;
  323. return phys_offs | (c & (SPI_FLASH_MMU_PAGE_SIZE-1));
  324. }
  325. const void *IRAM_ATTR spi_flash_phys2cache(uint32_t phys_offs, spi_flash_mmap_memory_t memory)
  326. {
  327. uint32_t phys_page = phys_offs / SPI_FLASH_MMU_PAGE_SIZE;
  328. int start, end, page_delta;
  329. intptr_t base;
  330. if (memory == SPI_FLASH_MMAP_DATA) {
  331. start = 0;
  332. end = 64;
  333. base = VADDR0_START_ADDR;
  334. page_delta = 0;
  335. } else {
  336. start = PRO_IRAM0_FIRST_USABLE_PAGE;
  337. end = 256;
  338. base = VADDR1_START_ADDR;
  339. page_delta = 64;
  340. }
  341. spi_flash_disable_interrupts_caches_and_other_cpu();
  342. DPORT_INTERRUPT_DISABLE();
  343. for (int i = start; i < end; i++) {
  344. if (DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]) == phys_page) {
  345. i -= page_delta;
  346. intptr_t cache_page = base + (SPI_FLASH_MMU_PAGE_SIZE * i);
  347. DPORT_INTERRUPT_RESTORE();
  348. spi_flash_enable_interrupts_caches_and_other_cpu();
  349. return (const void *) (cache_page | (phys_offs & (SPI_FLASH_MMU_PAGE_SIZE-1)));
  350. }
  351. }
  352. DPORT_INTERRUPT_RESTORE();
  353. spi_flash_enable_interrupts_caches_and_other_cpu();
  354. return NULL;
  355. }
  356. static bool IRAM_ATTR is_page_mapped_in_cache(uint32_t phys_page)
  357. {
  358. int start[2], end[2];
  359. /* SPI_FLASH_MMAP_DATA */
  360. start[0] = 0;
  361. end[0] = 64;
  362. /* SPI_FLASH_MMAP_INST */
  363. start[1] = PRO_IRAM0_FIRST_USABLE_PAGE;
  364. end[1] = 256;
  365. DPORT_INTERRUPT_DISABLE();
  366. for (int j = 0; j < 2; j++) {
  367. for (int i = start[j]; i < end[j]; i++) {
  368. if (DPORT_SEQUENCE_REG_READ((uint32_t)&DPORT_PRO_FLASH_MMU_TABLE[i]) == phys_page) {
  369. DPORT_INTERRUPT_RESTORE();
  370. return true;
  371. }
  372. }
  373. }
  374. DPORT_INTERRUPT_RESTORE();
  375. return false;
  376. }
  377. /* Validates if given flash address has corresponding cache mapping, if yes, flushes cache memories */
  378. IRAM_ATTR bool spi_flash_check_and_flush_cache(size_t start_addr, size_t length)
  379. {
  380. /* align start_addr & length to full MMU pages */
  381. uint32_t page_start_addr = start_addr & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  382. length += (start_addr - page_start_addr);
  383. length = (length + SPI_FLASH_MMU_PAGE_SIZE - 1) & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
  384. for (uint32_t addr = page_start_addr; addr < page_start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
  385. uint32_t page = addr / SPI_FLASH_MMU_PAGE_SIZE;
  386. if (page >= 256) {
  387. return false; /* invalid address */
  388. }
  389. if (is_page_mapped_in_cache(page)) {
  390. #if CONFIG_SPIRAM_SUPPORT
  391. esp_spiram_writeback_cache();
  392. #endif
  393. Cache_Flush(0);
  394. #ifndef CONFIG_FREERTOS_UNICORE
  395. Cache_Flush(1);
  396. #endif
  397. return true;
  398. }
  399. }
  400. return false;
  401. }