]> granicus.if.org Git - esp-idf/blob - components/spi_flash/flash_mmap.c
Merge branch 'bugfix/btdm_debug_gatt_attr_table' into 'master'
[esp-idf] / components / spi_flash / flash_mmap.c
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
15 #include <stdlib.h>
16 #include <assert.h>
17 #include <string.h>
18 #include <stdio.h>
19
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>
25 #include <soc/soc.h>
26 #include <soc/dport_reg.h>
27 #include "sdkconfig.h"
28 #include "esp_ipc.h"
29 #include "esp_attr.h"
30 #include "esp_spi_flash.h"
31 #include "esp_log.h"
32 #include "cache_utils.h"
33
34 #ifndef NDEBUG
35 // Enable built-in checks in queue.h in debug builds
36 #define INVARIANTS
37 #endif
38 #include "rom/queue.h"
39
40 #define REGIONS_COUNT 4
41 #define PAGES_PER_REGION 64
42 #define INVALID_ENTRY_VAL 0x100
43 #define VADDR0_START_ADDR 0x3F400000
44 #define VADDR1_START_ADDR 0x40000000
45 #define VADDR1_FIRST_USABLE_ADDR 0x400D0000
46 #define PRO_IRAM0_FIRST_USABLE_PAGE ((VADDR1_FIRST_USABLE_ADDR - VADDR1_START_ADDR) / SPI_FLASH_MMU_PAGE_SIZE + 64)
47
48 /* Ensure pages in a region haven't been marked as written via
49    spi_flash_mark_modified_region(). If the page has
50    been written, flush the entire flash cache before returning.
51
52    This ensures stale cache entries are never read after fresh calls
53    to spi_flash_mmap(), while keeping the number of cache flushes to a
54    minimum.
55 */
56 static void spi_flash_ensure_unmodified_region(size_t start_addr, size_t length);
57
58 typedef struct mmap_entry_{
59     uint32_t handle;
60     int page;
61     int count;
62     LIST_ENTRY(mmap_entry_) entries;
63 } mmap_entry_t;
64
65
66 static LIST_HEAD(mmap_entries_head, mmap_entry_) s_mmap_entries_head =
67         LIST_HEAD_INITIALIZER(s_mmap_entries_head);
68 static uint8_t s_mmap_page_refcnt[REGIONS_COUNT * PAGES_PER_REGION] = {0};
69 static uint32_t s_mmap_last_handle = 0;
70
71
72 static void IRAM_ATTR spi_flash_mmap_init()
73 {
74     for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
75         uint32_t entry_pro = DPORT_PRO_FLASH_MMU_TABLE[i];
76         uint32_t entry_app = DPORT_APP_FLASH_MMU_TABLE[i];
77         if (entry_pro != entry_app) {
78             // clean up entries used by boot loader
79             entry_pro = 0;
80             DPORT_PRO_FLASH_MMU_TABLE[i] = 0;
81         }
82         if ((entry_pro & 0x100) == 0 && (i == 0 || i == PRO_IRAM0_FIRST_USABLE_PAGE || entry_pro != 0)) {
83             s_mmap_page_refcnt[i] = 1;
84         }
85     }
86 }
87
88 esp_err_t IRAM_ATTR spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
89                          const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
90 {
91     esp_err_t ret;
92     mmap_entry_t* new_entry = (mmap_entry_t*) malloc(sizeof(mmap_entry_t));
93     if (new_entry == 0) {
94         return ESP_ERR_NO_MEM;
95     }
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
103     spi_flash_disable_interrupts_caches_and_other_cpu();
104
105     spi_flash_ensure_unmodified_region(src_addr, size);
106
107     if (s_mmap_page_refcnt[0] == 0) {
108         spi_flash_mmap_init();
109     }
110     // figure out the memory region where we should look for pages
111     int region_begin;   // first page to check
112     int region_size;    // number of pages to check
113     uint32_t region_addr;  // base address of memory region
114     if (memory == SPI_FLASH_MMAP_DATA) {
115         // Vaddr0
116         region_begin = 0;
117         region_size = 64;
118         region_addr = VADDR0_START_ADDR;
119     } else {
120         // only part of VAddr1 is usable, so adjust for that
121         region_begin = VADDR1_FIRST_USABLE_ADDR;
122         region_size = 3 * 64 - region_begin;
123         region_addr = VADDR1_FIRST_USABLE_ADDR;
124     }
125     // region which should be mapped
126     int phys_page = src_addr / SPI_FLASH_MMU_PAGE_SIZE;
127     int page_count = (size + SPI_FLASH_MMU_PAGE_SIZE - 1) / SPI_FLASH_MMU_PAGE_SIZE;
128     // The following part searches for a range of MMU entries which can be used.
129     // Algorithm is essentially naïve strstr algorithm, except that unused MMU
130     // entries are treated as wildcards.
131     int start;
132     int end = region_begin + region_size - page_count;
133     for (start = region_begin; start < end; ++start) {
134         int page = phys_page;
135         int pos;
136         for (pos = start; pos < start + page_count; ++pos, ++page) {
137             int table_val = (int) DPORT_PRO_FLASH_MMU_TABLE[pos];
138             uint8_t refcnt = s_mmap_page_refcnt[pos];
139             if (refcnt != 0 && table_val != page) {
140                 break;
141             }
142         }
143         // whole mapping range matched, bail out
144         if (pos - start == page_count) {
145             break;
146         }
147     }
148     // checked all the region(s) and haven't found anything?
149     if (start == end) {
150         *out_handle = 0;
151         *out_ptr = NULL;
152         ret = ESP_ERR_NO_MEM;
153     } else {
154         // set up mapping using pages [start, start + page_count)
155         uint32_t entry_val = (uint32_t) phys_page;
156         for (int i = start; i != start + page_count; ++i, ++entry_val) {
157             // sanity check: we won't reconfigure entries with non-zero reference count
158             assert(s_mmap_page_refcnt[i] == 0 ||
159                     (DPORT_PRO_FLASH_MMU_TABLE[i] == entry_val &&
160                      DPORT_APP_FLASH_MMU_TABLE[i] == entry_val));
161             if (s_mmap_page_refcnt[i] == 0) {
162                 DPORT_PRO_FLASH_MMU_TABLE[i] = entry_val;
163                 DPORT_APP_FLASH_MMU_TABLE[i] = entry_val;
164             }
165             ++s_mmap_page_refcnt[i];
166         }
167
168         LIST_INSERT_HEAD(&s_mmap_entries_head, new_entry, entries);
169         new_entry->page = start;
170         new_entry->count = page_count;
171         new_entry->handle = ++s_mmap_last_handle;
172         *out_handle = new_entry->handle;
173         *out_ptr = (void*) (region_addr + start * SPI_FLASH_MMU_PAGE_SIZE);
174         ret = ESP_OK;
175     }
176     spi_flash_enable_interrupts_caches_and_other_cpu();
177     if (*out_ptr == NULL) {
178         free(new_entry);
179     }
180     return ret;
181 }
182
183 void IRAM_ATTR spi_flash_munmap(spi_flash_mmap_handle_t handle)
184 {
185     spi_flash_disable_interrupts_caches_and_other_cpu();
186     mmap_entry_t* it;
187     // look for handle in linked list
188     for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
189         if (it->handle == handle) {
190             // for each page, decrement reference counter
191             // if reference count is zero, disable MMU table entry to
192             // facilitate debugging of use-after-free conditions
193             for (int i = it->page; i < it->page + it->count; ++i) {
194                 assert(s_mmap_page_refcnt[i] > 0);
195                 if (--s_mmap_page_refcnt[i] == 0) {
196                     DPORT_PRO_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
197                     DPORT_APP_FLASH_MMU_TABLE[i] = INVALID_ENTRY_VAL;
198                 }
199             }
200             LIST_REMOVE(it, entries);
201             break;
202         }
203     }
204     spi_flash_enable_interrupts_caches_and_other_cpu();
205     if (it == NULL) {
206         assert(0 && "invalid handle, or handle already unmapped");
207     }
208     free(it);
209 }
210
211 void spi_flash_mmap_dump()
212 {
213     if (s_mmap_page_refcnt[0] == 0) {
214         spi_flash_mmap_init();
215     }
216     mmap_entry_t* it;
217     for (it = LIST_FIRST(&s_mmap_entries_head); it != NULL; it = LIST_NEXT(it, entries)) {
218         printf("handle=%d page=%d count=%d\n", it->handle, it->page, it->count);
219     }
220     for (int i = 0; i < REGIONS_COUNT * PAGES_PER_REGION; ++i) {
221         if (s_mmap_page_refcnt[i] != 0) {
222             printf("page %d: refcnt=%d paddr=%d\n",
223                     i, (int) s_mmap_page_refcnt[i], DPORT_PRO_FLASH_MMU_TABLE[i]);
224         }
225     }
226 }
227
228 /* 256-bit (up to 16MB of 64KB pages) bitset of all flash pages
229    that have been written to since last cache flush.
230
231    Before mmaping a page, need to flush caches if that page has been
232    written to.
233
234    Note: It's possible to do some additional performance tweaks to
235    this algorithm, as we actually only need to flush caches if a page
236    was first mmapped, then written to, then is about to be mmaped a
237    second time. This is a fair bit more complex though, so unless
238    there's an access pattern that this would significantly boost then
239    it's probably not worth it.
240 */
241 static uint32_t written_pages[256/32];
242
243 static void update_written_pages(size_t start_addr, size_t length, bool mark);
244
245 void IRAM_ATTR spi_flash_mark_modified_region(size_t start_addr, size_t length)
246 {
247     update_written_pages(start_addr, length, true);
248 }
249
250 static void IRAM_ATTR spi_flash_ensure_unmodified_region(size_t start_addr, size_t length)
251 {
252     update_written_pages(start_addr, length, false);
253 }
254
255 /* generic implementation for the previous two functions */
256 static inline IRAM_ATTR void update_written_pages(size_t start_addr, size_t length, bool mark)
257 {
258     for (uint32_t addr = start_addr; addr < start_addr + length; addr += SPI_FLASH_MMU_PAGE_SIZE) {
259         int page = addr / SPI_FLASH_MMU_PAGE_SIZE;
260         if (page >= 256) {
261             return; /* invalid address */
262         }
263
264         int idx = page / 32;
265         uint32_t bit = 1 << (page % 32);
266
267         if (mark) {
268             written_pages[idx] |= bit;
269         } else if (written_pages[idx] & bit) {
270             /* it is tempting to write a version of this that only
271                flushes each CPU's cache as needed. However this is
272                tricky because mmaped memory can be used on un-pinned
273                cores, or the pointer passed between CPUs.
274             */
275             Cache_Flush(0);
276 #ifndef CONFIG_FREERTOS_UNICORE
277             Cache_Flush(1);
278 #endif
279             bzero(written_pages, sizeof(written_pages));
280         }
281     }
282 }