Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / m5stack-tft / repo / components / spiffs / spiffs_cache.c
1 /*
2  * spiffs_cache.c
3  *
4  *  Created on: Jun 23, 2013
5  *      Author: petera
6  */
7
8 #include "spiffs.h"
9 #include "spiffs_nucleus.h"
10
11 #if SPIFFS_CACHE
12
13 // returns cached page for give page index, or null if no such cached page
14 static spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix) {
15   spiffs_cache *cache = spiffs_get_cache(fs);
16   if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) return 0;
17   int i;
18   for (i = 0; i < cache->cpage_count; i++) {
19     spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
20     if ((cache->cpage_use_map & (1<<i)) &&
21         (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
22         cp->pix == pix ) {
23       SPIFFS_CACHE_DBG("CACHE_GET: have cache page "_SPIPRIi" for "_SPIPRIpg"\n", i, pix);
24       cp->last_access = cache->last_access;
25       return cp;
26     }
27   }
28   //SPIFFS_CACHE_DBG("CACHE_GET: no cache for "_SPIPRIpg"\n", pix);
29   return 0;
30 }
31
32 // frees cached page
33 static s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back) {
34   s32_t res = SPIFFS_OK;
35   spiffs_cache *cache = spiffs_get_cache(fs);
36   spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, ix);
37   if (cache->cpage_use_map & (1<<ix)) {
38     if (write_back &&
39         (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
40         (cp->flags & SPIFFS_CACHE_FLAG_DIRTY)) {
41       u8_t *mem =  spiffs_get_cache_page(fs, cache, ix);
42       res = SPIFFS_HAL_WRITE(fs, SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);
43     }
44
45     cp->flags = 0;
46     cache->cpage_use_map &= ~(1 << ix);
47
48     if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) {
49       SPIFFS_CACHE_DBG("CACHE_FREE: free cache page "_SPIPRIi" objid "_SPIPRIid"\n", ix, cp->obj_id);
50     } else {
51       SPIFFS_CACHE_DBG("CACHE_FREE: free cache page "_SPIPRIi" pix "_SPIPRIpg"\n", ix, cp->pix);
52     }
53   }
54
55   return res;
56 }
57
58 // removes the oldest accessed cached page
59 static s32_t spiffs_cache_page_remove_oldest(spiffs *fs, u8_t flag_mask, u8_t flags) {
60   s32_t res = SPIFFS_OK;
61   spiffs_cache *cache = spiffs_get_cache(fs);
62
63   if ((cache->cpage_use_map & cache->cpage_use_mask) != cache->cpage_use_mask) {
64     // at least one free cpage
65     return SPIFFS_OK;
66   }
67
68   // all busy, scan thru all to find the cpage which has oldest access
69   int i;
70   int cand_ix = -1;
71   u32_t oldest_val = 0;
72   for (i = 0; i < cache->cpage_count; i++) {
73     spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
74     if ((cache->last_access - cp->last_access) > oldest_val &&
75         (cp->flags & flag_mask) == flags) {
76       oldest_val = cache->last_access - cp->last_access;
77       cand_ix = i;
78     }
79   }
80
81   if (cand_ix >= 0) {
82     res = spiffs_cache_page_free(fs, cand_ix, 1);
83   }
84
85   return res;
86 }
87
88 // allocates a new cached page and returns it, or null if all cache pages are busy
89 static spiffs_cache_page *spiffs_cache_page_allocate(spiffs *fs) {
90   spiffs_cache *cache = spiffs_get_cache(fs);
91   if (cache->cpage_use_map == 0xffffffff) {
92     // out of cache memory
93     return 0;
94   }
95   int i;
96   for (i = 0; i < cache->cpage_count; i++) {
97     if ((cache->cpage_use_map & (1<<i)) == 0) {
98       spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
99       cache->cpage_use_map |= (1<<i);
100       cp->last_access = cache->last_access;
101       SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page "_SPIPRIi"\n", i);
102       return cp;
103     }
104   }
105   // out of cache entries
106   return 0;
107 }
108
109 // drops the cache page for give page index
110 void spiffs_cache_drop_page(spiffs *fs, spiffs_page_ix pix) {
111   spiffs_cache_page *cp =  spiffs_cache_page_get(fs, pix);
112   if (cp) {
113     spiffs_cache_page_free(fs, cp->ix, 0);
114   }
115 }
116
117 // ------------------------------
118
119 // reads from spi flash or the cache
120 s32_t spiffs_phys_rd(
121     spiffs *fs,
122     u8_t op,
123     spiffs_file fh,
124     u32_t addr,
125     u32_t len,
126     u8_t *dst) {
127   (void)fh;
128   s32_t res = SPIFFS_OK;
129   spiffs_cache *cache = spiffs_get_cache(fs);
130   spiffs_cache_page *cp =  spiffs_cache_page_get(fs, SPIFFS_PADDR_TO_PAGE(fs, addr));
131   cache->last_access++;
132   if (cp) {
133     // we've already got one, you see
134 #if SPIFFS_CACHE_STATS
135     fs->cache_hits++;
136 #endif
137     cp->last_access = cache->last_access;
138     u8_t *mem =  spiffs_get_cache_page(fs, cache, cp->ix);
139     memcpy(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
140   } else {
141     if ((op & SPIFFS_OP_TYPE_MASK) == SPIFFS_OP_T_OBJ_LU2) {
142       // for second layer lookup functions, we do not cache in order to prevent shredding
143       return SPIFFS_HAL_READ(fs, addr, len, dst);
144     }
145 #if SPIFFS_CACHE_STATS
146     fs->cache_misses++;
147 #endif
148     // this operation will always free one cache page (unless all already free),
149     // the result code stems from the write operation of the possibly freed cache page
150     res = spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);
151
152     cp = spiffs_cache_page_allocate(fs);
153     if (cp) {
154       cp->flags = SPIFFS_CACHE_FLAG_WRTHRU;
155       cp->pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
156
157       s32_t res2 = SPIFFS_HAL_READ(fs,
158           addr - SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr),
159           SPIFFS_CFG_LOG_PAGE_SZ(fs),
160           spiffs_get_cache_page(fs, cache, cp->ix));
161       if (res2 != SPIFFS_OK) {
162         // honor read failure before possible write failure (bad idea?)
163         res = res2;
164       }
165       u8_t *mem =  spiffs_get_cache_page(fs, cache, cp->ix);
166       memcpy(dst, &mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], len);
167     } else {
168       // this will never happen, last resort for sake of symmetry
169       s32_t res2 = SPIFFS_HAL_READ(fs, addr, len, dst);
170       if (res2 != SPIFFS_OK) {
171         // honor read failure before possible write failure (bad idea?)
172         res = res2;
173       }
174     }
175   }
176   return res;
177 }
178
179 // writes to spi flash and/or the cache
180 s32_t spiffs_phys_wr(
181     spiffs *fs,
182     u8_t op,
183     spiffs_file fh,
184     u32_t addr,
185     u32_t len,
186     u8_t *src) {
187   (void)fh;
188   spiffs_page_ix pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
189   spiffs_cache *cache = spiffs_get_cache(fs);
190   spiffs_cache_page *cp =  spiffs_cache_page_get(fs, pix);
191
192   if (cp && (op & SPIFFS_OP_COM_MASK) != SPIFFS_OP_C_WRTHRU) {
193     // have a cache page
194     // copy in data to cache page
195
196     if ((op & SPIFFS_OP_COM_MASK) == SPIFFS_OP_C_DELE &&
197         (op & SPIFFS_OP_TYPE_MASK) != SPIFFS_OP_T_OBJ_LU) {
198       // page is being deleted, wipe from cache - unless it is a lookup page
199       spiffs_cache_page_free(fs, cp->ix, 0);
200       return SPIFFS_HAL_WRITE(fs, addr, len, src);
201     }
202
203     u8_t *mem =  spiffs_get_cache_page(fs, cache, cp->ix);
204     memcpy(&mem[SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr)], src, len);
205
206     cache->last_access++;
207     cp->last_access = cache->last_access;
208
209     if (cp->flags & SPIFFS_CACHE_FLAG_WRTHRU) {
210       // page is being updated, no write-cache, just pass thru
211       return SPIFFS_HAL_WRITE(fs, addr, len, src);
212     } else {
213       return SPIFFS_OK;
214     }
215   } else {
216     // no cache page, no write cache - just write thru
217     return SPIFFS_HAL_WRITE(fs, addr, len, src);
218   }
219 }
220
221 #if SPIFFS_CACHE_WR
222 // returns the cache page that this fd refers, or null if no cache page
223 spiffs_cache_page *spiffs_cache_page_get_by_fd(spiffs *fs, spiffs_fd *fd) {
224   spiffs_cache *cache = spiffs_get_cache(fs);
225
226   if ((cache->cpage_use_map & cache->cpage_use_mask) == 0) {
227     // all cpages free, no cpage cannot be assigned to obj_id
228     return 0;
229   }
230
231   int i;
232   for (i = 0; i < cache->cpage_count; i++) {
233     spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
234     if ((cache->cpage_use_map & (1<<i)) &&
235         (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) &&
236         cp->obj_id == fd->obj_id) {
237       return cp;
238     }
239   }
240
241   return 0;
242 }
243
244 // allocates a new cache page and refers this to given fd - flushes an old cache
245 // page if all cache is busy
246 spiffs_cache_page *spiffs_cache_page_allocate_by_fd(spiffs *fs, spiffs_fd *fd) {
247   // before this function is called, it is ensured that there is no already existing
248   // cache page with same object id
249   spiffs_cache_page_remove_oldest(fs, SPIFFS_CACHE_FLAG_TYPE_WR, 0);
250   spiffs_cache_page *cp = spiffs_cache_page_allocate(fs);
251   if (cp == 0) {
252     // could not get cache page
253     return 0;
254   }
255
256   cp->flags = SPIFFS_CACHE_FLAG_TYPE_WR;
257   cp->obj_id = fd->obj_id;
258   fd->cache_page = cp;
259   return cp;
260 }
261
262 // unrefers all fds that this cache page refers to and releases the cache page
263 void spiffs_cache_fd_release(spiffs *fs, spiffs_cache_page *cp) {
264   if (cp == 0) return;
265   u32_t i;
266   spiffs_fd *fds = (spiffs_fd *)fs->fd_space;
267   for (i = 0; i < fs->fd_count; i++) {
268     spiffs_fd *cur_fd = &fds[i];
269     if (cur_fd->file_nbr != 0 && cur_fd->cache_page == cp) {
270       cur_fd->cache_page = 0;
271     }
272   }
273   spiffs_cache_page_free(fs, cp->ix, 0);
274
275   cp->obj_id = 0;
276 }
277
278 #endif
279
280 // initializes the cache
281 void spiffs_cache_init(spiffs *fs) {
282   if (fs->cache == 0) return;
283   u32_t sz = fs->cache_size;
284   u32_t cache_mask = 0;
285   int i;
286   int cache_entries =
287       (sz - sizeof(spiffs_cache)) / (SPIFFS_CACHE_PAGE_SIZE(fs));
288   if (cache_entries <= 0) return;
289
290   for (i = 0; i < cache_entries; i++) {
291     cache_mask <<= 1;
292     cache_mask |= 1;
293   }
294
295   spiffs_cache cache;
296   memset(&cache, 0, sizeof(spiffs_cache));
297   cache.cpage_count = cache_entries;
298   cache.cpages = (u8_t *)((u8_t *)fs->cache + sizeof(spiffs_cache));
299
300   cache.cpage_use_map = 0xffffffff;
301   cache.cpage_use_mask = cache_mask;
302   memcpy(fs->cache, &cache, sizeof(spiffs_cache));
303
304   spiffs_cache *c = spiffs_get_cache(fs);
305
306   memset(c->cpages, 0, c->cpage_count * SPIFFS_CACHE_PAGE_SIZE(fs));
307
308   c->cpage_use_map &= ~(c->cpage_use_mask);
309   for (i = 0; i < cache.cpage_count; i++) {
310     spiffs_get_cache_page_hdr(fs, c, i)->ix = i;
311   }
312 }
313
314 #endif // SPIFFS_CACHE