1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <freertos/FreeRTOS.h>
21 #include <freertos/task.h>
22 #include <freertos/semphr.h>
23 #include <rom/spi_flash.h>
24 #include <rom/cache.h>
26 #include <soc/dport_reg.h>
27 #include "sdkconfig.h"
30 #include "esp_spi_flash.h"
31 #include "esp_flash_encrypt.h"
33 #include "cache_utils.h"
34 #include "esp_spiram.h"
37 // Enable built-in checks in queue.h in debug builds
40 #include "rom/queue.h"
42 #define REGIONS_COUNT 4
43 #define PAGES_PER_REGION 64
44 #define INVALID_ENTRY_VAL 0x100
45 #define VADDR0_START_ADDR 0x3F400000
46 #define VADDR1_START_ADDR 0x40000000
47 #define VADDR1_FIRST_USABLE_ADDR 0x400D0000
48 #define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64)
50 /* Ensure pages in a region haven't been marked as written via
51 spi_flash_mark_modified_region(). If the page has
52 been written, flush the entire flash cache before returning.
54 This ensures stale cache entries are never read after fresh calls
55 to spi_flash_mmap(), while keeping the number of cache flushes to a
58 Returns true if cache was flushed.
60 static bool spi_flash_ensure_unmodified_region(size_t start_addr, size_t length);
62 typedef struct mmap_entry_{
66 LIST_ENTRY(mmap_entry_) entries;
70 static LIST_HEAD(mmap_entries_head, mmap_entry_) s_mmap_entries_head =
71 LIST_HEAD_INITIALIZER(s_mmap_entries_head);
72 static uint8_t s_mmap_page_refcnt[REGIONS_COUNT * PAGES_PER_REGION] = {0};
73 static uint32_t s_mmap_last_handle = 0;
76 static void IRAM_ATTR spi_flash_mmap_init()
78 if (s_mmap_page_refcnt[0] != 0) {
79 return; /* mmap data already initialised */
82 for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
83 uint32_t entry_pro = DPORT_PRO_FLASH_MMU_TABLE[i];
84 uint32_t entry_app = DPORT_APP_FLASH_MMU_TABLE[i];
85 if (entry_pro != entry_app) {
86 // clean up entries used by boot loader
87 entry_pro = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
88 DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
90 if ((entry_pro & INVALID_ENTRY_VAL) == 0 && (i == 0 || i == PRO_IRAM0_FIRST_USABLE_PAGE || entry_pro != 0)) {
91 s_mmap_page_refcnt[i] = 1;
93 DPORT_PRO_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
94 DPORT_APP_FLASH_MMU_TABLE[i] = DPORT_FLASH_MMU_TABLE_INVALID_VAL;
99 esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
100 const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
103 if (src_addr & 0xffff) {
104 return ESP_ERR_INVALID_ARG;
106 if (src_addr + size > g_rom_flashchip.chip_size) {
107 return ESP_ERR_INVALID_ARG;
109 // region which should be mapped
110 int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
111 int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
112 //prepare a linear pages array to feed into spi_flash_mmap_pages
113 int *pages=malloc(sizeof(int)*page_count);
115 return ESP_ERR_NO_MEM;
117 for (int i = 0; i < page_count; i++) {
118 pages[i] = phys_page+i;
120 ret=spi_flash_mmap_pages(pages, page_count, memory, out_ptr, out_handle);
125 esp_err_t IRAM_ATTR spi_flash_mmap_pages(int *pages, size_t page_count, spi_flash_mmap_memory_t memory,
126 const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
129 bool did_flush, need_flush = false;
131 return ESP_ERR_INVALID_ARG;
133 for (int i = 0; i < page_count; i++) {
134 if (pages[i] < 0 || pages[i]*SPI_FLASH_MMU_PAGE_SIZE >= g_rom_flashchip.chip_size) {
135 return ESP_ERR_INVALID_ARG;
138 mmap_entry_t* new_entry = (mmap_entry_t*) heap_caps_malloc(sizeof(mmap_entry_t), MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT);
139 if (new_entry == 0) {
140 return ESP_ERR_NO_MEM;
143 spi_flash_disable_interrupts_caches_and_other_cpu();
146 for (int i = 0; i < page_count; i++) {
147 if (spi_flash_ensure_unmodified_region(pages[i]*SPI_FLASH_MMU_PAGE_SIZE, SPI_FLASH_MMU_PAGE_SIZE)) {
151 spi_flash_mmap_init();
152 // figure out the memory region where we should look for pages
153 int region_begin; // first page to check
154 int region_size; // number of pages to check
155 uint32_t region_addr; // base address of memory region
156 if (memory == SPI_FLASH_MMAP_DATA) {
160 region_addr = VADDR0_START_ADDR;
162 // only part of VAddr1 is usable, so adjust for that
163 region_begin = PRO_IRAM0_FIRST_USABLE_PAGE;
164 region_size = 3 * 64 - region_begin;
165 region_addr = VADDR1_FIRST_USABLE_ADDR;
167 if (region_size < page_count) {
168 return ESP_ERR_NO_MEM;
170 // The following part searches for a range of MMU entries which can be used.
171 // Algorithm is essentially naïve strstr algorithm, except that unused MMU
172 // entries are treated as wildcards.
174 int end = region_begin + region_size - page_count;
175 for (start = region_begin; start < end; ++start) {
178 for (pos = start; pos < start + page_count; ++pos, ++pageno) {
179 int table_val = (int) DPORT_PRO_FLASH_MMU_TABLE[pos];
180 uint8_t refcnt = s_mmap_page_refcnt[pos];
181 if (refcnt != 0 && table_val != pages[pageno]) {
185 // whole mapping range matched, bail out
186 if (pos - start == page_count) {
190 // checked all the region(s) and haven't found anything?
194 ret = ESP_ERR_NO_MEM;
196 // set up mapping using pages
198 for (int i = start; i != start + page_count; ++i, ++pageno) {
199 // sanity check: we won't reconfigure entries with non-zero reference count
200 assert(s_mmap_page_refcnt[i] == 0 ||
201 (DPORT_PRO_FLASH_MMU_TABLE[i] == pages[pageno] &&
202 DPORT_APP_FLASH_MMU_TABLE[i] == pages[pageno]));
203 if (s_mmap_page_refcnt[i] == 0) {
204 if (DPORT_PRO_FLASH_MMU_TABLE[i] != pages[pageno] || DPORT_APP_FLASH_MMU_TABLE[i] != pages[pageno]) {
205 DPORT_PRO_FLASH_MMU_TABLE[i] = pages[pageno];
206 DPORT_APP_FLASH_MMU_TABLE[i] = pages[pageno];
210 ++s_mmap_page_refcnt[i];
213 LIST_INSERT_HEAD(&s_mmap_entries_head, new_entry, entries);
214 new_entry->page = start;
215 new_entry->count = page_count;
216 new_entry->handle = ++s_mmap_last_handle;
217 *out_handle = new_entry->handle;
218 *out_ptr = (void*) (region_addr + (start - region_begin) * SPI_FLASH_MMU_PAGE_SIZE);
222 /* This is a temporary fix for an issue where some
223 cache reads may see stale data.
225 Working on a long term fix that doesn't require invalidating
228 if (!did_flush && need_flush) {
229 #if CONFIG_SPIRAM_SUPPORT
230 esp_spiram_writeback_cache();
236 spi_flash_enable_interrupts_caches_and_other_cpu();
237 if (*out_ptr == NULL) {
243 void IRAM_ATTR spi_flash_munmap(spi_flash_mmap_handle_t handle)
245 spi_flash_disable_interrupts_caches_and_other_cpu();
247 // look for handle in linked list
248 for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
249 if (it->handle == handle) {
250 // for each page, decrement reference counter
251 // if reference count is zero, disable MMU table entry to
252 // facilitate debugging of use-after-free conditions
253 for (int i = it->page; i < it->page + it->count; ++i) {
254 assert(s_mmap_page_refcnt[i] > 0);
255 if (--s_mmap_page_refcnt[i] == 0) {
256 DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
257 DPORT_APP_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
260 LIST_REMOVE(it, entries);
264 spi_flash_enable_interrupts_caches_and_other_cpu();
266 assert(0 && "invalid handle, or handle already unmapped");
271 void spi_flash_mmap_dump()
273 spi_flash_mmap_init();
275 for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
276 printf("handle=%d page=%d count=%d\n", it->handle, it->page, it->count);
278 for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
279 if (s_mmap_page_refcnt[i] != 0) {
280 printf("page %d: refcnt=%d paddr=%d\n",
281 i, (int) s_mmap_page_refcnt[i], DPORT_PRO_FLASH_MMU_TABLE[i]);
286 /* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages
287 that have been written to since last cache flush.
289 Before mmaping a page, need to flush caches if that page has been
292 Note: It's possible to do some additional performance tweaks to
293 this algorithm, as we actually only need to flush caches if a page
294 was first mmapped, then written to, then is about to be mmaped a
295 second time. This is a fair bit more complex though, so unless
296 there's an access pattern that this would significantly boost then
297 it's probably not worth it.
299 static uint32_t written_pages[256/32];
301 static bool update_written_pages(size_t start_addr, size_t length, bool mark);
303 void IRAM_ATTR spi_flash_mark_modified_region(size_t start_addr, size_t length)
305 update_written_pages(start_addr, length, true);
308 static IRAM_ATTR bool spi_flash_ensure_unmodified_region(size_t start_addr, size_t length)
310 return update_written_pages(start_addr, length, false);
313 /* generic implementation for the previous two functions */
314 static inline IRAM_ATTR bool update_written_pages(size_t start_addr, size_t length, bool mark)
316 /* align start_addr & length to full MMU pages */
317 uint32_t page_start_addr = start_addr & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
318 length += (start_addr - page_start_addr);
319 length = (length + SPI_FLASH_MMU_PAGE_SIZE - 1) & ~(SPI_FLASH_MMU_PAGE_SIZE-1);
320 for (uint32_t addr = page_start_addr; addr < page_start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
321 int page = addr / SPI_FLASH_MMU_PAGE_SIZE;
323 return false; /* invalid address */
327 uint32_t bit = 1 << (page % 32);
330 written_pages[idx] |= bit;
331 } else if (written_pages[idx] & bit) {
332 /* it is tempting to write a version of this that only
333 flushes each CPU's cache as needed. However this is
334 tricky because mmaped memory can be used on un-pinned
335 cores, or the pointer passed between CPUs.
337 #if CONFIG_SPIRAM_SUPPORT
338 esp_spiram_writeback_cache();
341 #ifndef CONFIG_FREERTOS_UNICORE
344 bzero(written_pages, sizeof(written_pages));
352 uint32_t spi_flash_cache2phys(const void *cached)
354 intptr_t c = (intptr_t)cached;
356 if (c >= VADDR1_START_ADDR && c < VADDR1_FIRST_USABLE_ADDR) {
357 /* IRAM address, doesn't map to flash */
358 return SPI_FLASH_CACHE2PHYS_FAIL;
360 else if (c < VADDR1_FIRST_USABLE_ADDR) {
361 /* expect cache is in DROM */
362 cache_page = (c - VADDR0_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE;
364 /* expect cache is in IROM */
365 cache_page = (c - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64;
368 if (cache_page >= 256) {
369 /* cached address was not in IROM or DROM */
370 return SPI_FLASH_CACHE2PHYS_FAIL;
372 uint32_t phys_page = DPORT_PRO_FLASH_MMU_TABLE[cache_page];
373 if (phys_page == INVALID_ENTRY_VAL) {
374 /* page is not mapped */
375 return SPI_FLASH_CACHE2PHYS_FAIL;
377 uint32_t phys_offs = phys_page * SPI_FLASH_MMU_PAGE_SIZE;
378 return phys_offs | (c & (SPI_FLASH_MMU_PAGE_SIZE-1));
382 const void *spi_flash_phys2cache(uint32_t phys_offs, spi_flash_mmap_memory_t memory)
384 uint32_t phys_page = phys_offs / SPI_FLASH_MMU_PAGE_SIZE;
385 int start, end, page_delta;
388 if (memory == SPI_FLASH_MMAP_DATA) {
391 base = VADDR0_START_ADDR;
394 start = PRO_IRAM0_FIRST_USABLE_PAGE;
396 base = VADDR1_START_ADDR;
400 for (int i = start; i < end; i++) {
401 if (DPORT_PRO_FLASH_MMU_TABLE[i] == phys_page) {
403 intptr_t cache_page = base + (SPI_FLASH_MMU_PAGE_SIZE * i);
404 return (const void *) (cache_page | (phys_offs & (SPI_FLASH_MMU_PAGE_SIZE-1)));