|
@@ -1,28 +1,55 @@
|
|
|
/*
|
|
|
- * Simple disk cache
|
|
|
+ * Simple disk cache, dynamically sharing space with the sbrk heap.
|
|
|
*/
|
|
|
|
|
|
#include "common.h"
|
|
|
#include "list.h"
|
|
|
#include "sdcard.h"
|
|
|
+#include "sys.h"
|
|
|
#include "systime.h"
|
|
|
#include "console.h"
|
|
|
|
|
|
-#define CACHE_BLOCKS 1024
|
|
|
+/*
|
|
|
+ * Maximum possible arena size; metadata structures and sbrk
|
|
|
+ * thresholds are sized based on this value. A reasonable overestimate
|
|
|
+ * is fine.
|
|
|
+ */
|
|
|
+#define MAX_ARENA_SIZE SDRAM_SIZE
|
|
|
+
|
|
|
#define CACHE_BLOCK_BITS 5
|
|
|
+#define CACHE_BLOCK_SHIFT (CACHE_BLOCK_BITS+SECTOR_SHIFT)
|
|
|
#define CACHE_BLOCK_SECTORS (1 << CACHE_BLOCK_BITS)
|
|
|
#define CACHE_BLOCK_SIZE (SECTOR_SIZE << CACHE_BLOCK_BITS)
|
|
|
|
|
|
-#define CACHE_HASH_SIZE (CACHE_BLOCKS*2)
|
|
|
+#define MAX_CACHE_BLOCKS (MAX_ARENA_SIZE/CACHE_BLOCK_SIZE)
|
|
|
+#define CACHE_HASH_SIZE (MAX_CACHE_BLOCKS*2)
|
|
|
+
|
|
|
+/*
|
|
|
+ * Minimum cache size; fail sbrk allocation beyond this point.
|
|
|
+ */
|
|
|
+#define MIN_CACHE_SIZE (MAX_ARENA_SIZE >> 4)
|
|
|
+#define MIN_CACHE_BLOCKS (MIN_CACHE_SIZE/CACHE_BLOCK_SIZE)
|
|
|
+
|
|
|
+/*
|
|
|
+ * When reclaiming storage from the brk heap, free up this much space
|
|
|
+ * in addition to the requested allocation (if possible.)
|
|
|
+ */
|
|
|
+#define BRK_SLACK (MAX_ARENA_SIZE >> 6)
|
|
|
+
|
|
|
+/* Minimum alignment for brk */
|
|
|
+#define HEAP_ALIGN 32
|
|
|
|
|
|
typedef sector_t block_t;
|
|
|
#define NO_BLOCK ((block_t)(-1))
|
|
|
|
|
|
enum block_status {
|
|
|
- FL_INVALID = 0,
|
|
|
- FL_VALID = 1,
|
|
|
- FL_DIRTY_BIT = 2,
|
|
|
- FL_DIRTY = FL_VALID|FL_DIRTY_BIT
|
|
|
+ FL_NOTCACHE = 0, /* heap or not present */
|
|
|
+ FL_CACHE_BIT = 1,
|
|
|
+ FL_FREE = FL_CACHE_BIT,
|
|
|
+ FL_VALID_BIT = 2,
|
|
|
+ FL_VALID = FL_CACHE_BIT | FL_VALID_BIT,
|
|
|
+ FL_DIRTY_BIT = 4,
|
|
|
+ FL_DIRTY = FL_VALID | FL_DIRTY_BIT
|
|
|
};
|
|
|
|
|
|
struct cache_block {
|
|
@@ -30,40 +57,32 @@ struct cache_block {
|
|
|
struct dll lru; /* Link in LRU chain */
|
|
|
block_t block; /* Physical block index */
|
|
|
enum block_status flags; /* Status flags */
|
|
|
- char data[CACHE_BLOCK_SIZE] __attribute__((aligned(4)));
|
|
|
};
|
|
|
|
|
|
-static struct dll __dram_noinit cache_hash[CACHE_HASH_SIZE];
|
|
|
-static struct cache_block __dram_noinit disk_cache[CACHE_BLOCKS];
|
|
|
+static struct dll __dram_bss cache_hash[CACHE_HASH_SIZE];
|
|
|
+static struct cache_block __dram_bss cache_blocks[MAX_CACHE_BLOCKS];
|
|
|
|
|
|
-static struct dll lru_list;
|
|
|
-static struct dll free_list;
|
|
|
+extern char __heap_start[], __heap_end[];
|
|
|
+#define CACHE_START align_up(&__heap_start[0], CACHE_BLOCK_SIZE)
|
|
|
+#define CACHE_END __heap_end
|
|
|
|
|
|
-void disk_cache_init(void)
|
|
|
+/* Convert between data and metadata pointers */
|
|
|
+static inline __constfunc char *bp_to_data(const struct cache_block *block)
|
|
|
{
|
|
|
- struct cache_block *bp, *bep;
|
|
|
- struct dll *hp, *hep;
|
|
|
-
|
|
|
- dll_init(&free_list);
|
|
|
- dll_init(&lru_list);
|
|
|
+ return CACHE_START + ((block - cache_blocks) << CACHE_BLOCK_SHIFT);
|
|
|
+}
|
|
|
+static inline __constfunc struct cache_block *data_to_bp(void *data)
|
|
|
+{
|
|
|
+ return &cache_blocks[((char *)data - CACHE_START) >> CACHE_BLOCK_SHIFT];
|
|
|
+}
|
|
|
|
|
|
- bp = disk_cache;
|
|
|
- bep = bp + CACHE_BLOCKS;
|
|
|
- while (bp < bep) {
|
|
|
- dll_insert_head(&free_list, &bp->hash);
|
|
|
- dll_insert_head(&lru_list, &bp->lru);
|
|
|
- bp->block = NO_BLOCK;
|
|
|
- bp->flags = FL_INVALID;
|
|
|
- bp++;
|
|
|
- }
|
|
|
+static struct dll lru_list;
|
|
|
+static struct dll free_list;
|
|
|
|
|
|
- hp = cache_hash;
|
|
|
- hep = hp + CACHE_HASH_SIZE;
|
|
|
- while (hp < hep)
|
|
|
- dll_init(hp++);
|
|
|
-}
|
|
|
+static size_t cur_brk;
|
|
|
+static size_t cache_base;
|
|
|
|
|
|
-static inline __attribute__((const)) struct dll *hash_slot(block_t block)
|
|
|
+static inline __constfunc struct dll *hash_slot(block_t block)
|
|
|
{
|
|
|
uint64_t m;
|
|
|
uint32_t hash;
|
|
@@ -71,7 +90,7 @@ static inline __attribute__((const)) struct dll *hash_slot(block_t block)
|
|
|
m = UINT64_C(0x34f1f85d) * block;
|
|
|
hash = (m >> 32) + m;
|
|
|
|
|
|
- return &cache_hash[hash % CACHE_BLOCKS];
|
|
|
+ return &cache_hash[hash % CACHE_HASH_SIZE];
|
|
|
}
|
|
|
|
|
|
static struct cache_block *disk_cache_find(block_t block)
|
|
@@ -92,11 +111,13 @@ static struct cache_block *disk_cache_find(block_t block)
|
|
|
|
|
|
static void invalidate_block(struct cache_block *bp)
|
|
|
{
|
|
|
- dll_remove(&bp->hash);
|
|
|
- dll_insert_head(&free_list, &bp->hash);
|
|
|
- bp->block = NO_BLOCK;
|
|
|
- bp->flags = FL_INVALID;
|
|
|
- dll_demote(&lru_list, &bp->lru);
|
|
|
+ if (bp->flags & FL_VALID_BIT) {
|
|
|
+ dll_remove(&bp->hash);
|
|
|
+ dll_insert_head(&free_list, &bp->hash);
|
|
|
+ bp->block = NO_BLOCK;
|
|
|
+ bp->flags = FL_FREE;
|
|
|
+ dll_demote(&lru_list, &bp->lru);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
static DRESULT sync_block(struct cache_block *bp)
|
|
@@ -106,11 +127,10 @@ static DRESULT sync_block(struct cache_block *bp)
|
|
|
sector_t size = sdc.lbasize;
|
|
|
sector_t sectors = min(CACHE_BLOCK_SECTORS, size - sector);
|
|
|
|
|
|
- if (sdcard_write_sectors(bp->data, sector, sectors) != (int)sectors) {
|
|
|
+ if (sdcard_write_sectors(bp_to_data(bp), sector, sectors) != (int)sectors) {
|
|
|
invalidate_block(bp); /* Or...? */
|
|
|
return RES_ERROR;
|
|
|
}
|
|
|
-
|
|
|
bp->flags = FL_VALID;
|
|
|
}
|
|
|
|
|
@@ -145,6 +165,35 @@ static DRESULT sync_all(void)
|
|
|
return rv;
|
|
|
}
|
|
|
|
|
|
+static void invalidate_all(void)
|
|
|
+{
|
|
|
+ struct cache_block *bp = data_to_bp((void *)cache_base);
|
|
|
+ const struct cache_block *ebp = data_to_bp(CACHE_END);
|
|
|
+
|
|
|
+ while (bp < ebp)
|
|
|
+ invalidate_block(bp++);
|
|
|
+}
|
|
|
+
|
|
|
+static DRESULT sync_kill_block(struct cache_block *bp)
|
|
|
+{
|
|
|
+ DRESULT rv = RES_OK;
|
|
|
+
|
|
|
+ if (!(bp->flags & FL_CACHE_BIT))
|
|
|
+ return;
|
|
|
+
|
|
|
+ if (bp->flags == FL_DIRTY)
|
|
|
+ rv = sync_block(bp);
|
|
|
+
|
|
|
+ if (!rv) {
|
|
|
+ dll_remove(&bp->hash);
|
|
|
+ dll_remove(&bp->lru);
|
|
|
+ bp->flags = FL_NOTCACHE;
|
|
|
+ bp->block = NO_BLOCK;
|
|
|
+ }
|
|
|
+
|
|
|
+ return rv;
|
|
|
+}
|
|
|
+
|
|
|
static struct cache_block *disk_cache_get(block_t block, bool do_read)
|
|
|
{
|
|
|
const sector_t size = sdc.lbasize;
|
|
@@ -168,7 +217,7 @@ static struct cache_block *disk_cache_get(block_t block, bool do_read)
|
|
|
clear_block(bp);
|
|
|
|
|
|
if (do_read) {
|
|
|
- if (sdcard_read_sectors(bp->data, sector, sectors) != sectors)
|
|
|
+ if (sdcard_read_sectors(bp_to_data(bp), sector, sectors) != sectors)
|
|
|
return NULL;
|
|
|
|
|
|
bp->flags = FL_VALID;
|
|
@@ -182,6 +231,100 @@ static struct cache_block *disk_cache_get(block_t block, bool do_read)
|
|
|
return bp;
|
|
|
}
|
|
|
|
|
|
+/* --------------------------------------------------------------------------
|
|
|
+ * Heap management
|
|
|
+ * ------------------------------------------------------------------------- */
|
|
|
+
|
|
|
+#define ARENA_START ((size_t)&__heap_start)
|
|
|
+#define ARENA_END ((size_t)&__heap_end)
|
|
|
+
|
|
|
+static size_t disk_cache_adjust(size_t newbrk)
|
|
|
+{
|
|
|
+ size_t new_base = align_up(newbrk + BRK_SLACK, CACHE_BLOCK_SIZE);
|
|
|
+
|
|
|
+ if (new_base > ARENA_END - MIN_CACHE_SIZE)
|
|
|
+ new_base = ARENA_END - MIN_CACHE_SIZE;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Add new entries to the tail of the free list in reverse order,
|
|
|
+ * so higher addresses get preferred.
|
|
|
+ */
|
|
|
+ struct cache_block * const obp = data_to_bp((void *)cache_base);
|
|
|
+ struct cache_block * const nbp = data_to_bp((void *)new_base);
|
|
|
+ struct cache_block *bp;
|
|
|
+
|
|
|
+ if (obp < nbp) {
|
|
|
+ /* Shrink cache */
|
|
|
+ for (bp = obp; bp < nbp; bp++) {
|
|
|
+ if (sync_kill_block(bp))
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ /* Grow cache */
|
|
|
+ for (bp = nbp; bp < obp; bp++) {
|
|
|
+ bp->block = NO_BLOCK;
|
|
|
+ bp->flags = FL_FREE;
|
|
|
+ dll_insert_tail(&free_list, &bp->hash);
|
|
|
+ dll_insert_tail(&lru_list, &bp->lru);
|
|
|
+ }
|
|
|
+ bp = nbp;
|
|
|
+ }
|
|
|
+
|
|
|
+ new_base = (size_t)bp_to_data(bp);
|
|
|
+
|
|
|
+ if (cache_base != new_base) {
|
|
|
+ con_printf("heap: start %p, brk %p, cache %p, %u blocks\n",
|
|
|
+ __heap_start, (void *)newbrk, (void *)new_base,
|
|
|
+ (ARENA_END - new_base) >> CACHE_BLOCK_SHIFT);
|
|
|
+ }
|
|
|
+
|
|
|
+ return cache_base = new_base;
|
|
|
+}
|
|
|
+
|
|
|
+void * __hot _sbrk(ptrdiff_t increment)
|
|
|
+{
|
|
|
+ const size_t heap_start = align_up(ARENA_START, HEAP_ALIGN);
|
|
|
+ size_t new_brk = align_up(cur_brk + increment, HEAP_ALIGN);
|
|
|
+
|
|
|
+ if (unlikely(new_brk > ARENA_END - MIN_CACHE_SIZE))
|
|
|
+ goto err;
|
|
|
+
|
|
|
+ if (unlikely(new_brk < heap_start))
|
|
|
+ new_brk = heap_start;
|
|
|
+
|
|
|
+ if (new_brk > cache_base || new_brk <= cache_base - BRK_SLACK) {
|
|
|
+ if (disk_cache_adjust(new_brk) < new_brk)
|
|
|
+ goto err;
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t old_brk = cur_brk;
|
|
|
+ cur_brk = new_brk;
|
|
|
+ return (void *)old_brk;
|
|
|
+
|
|
|
+err:
|
|
|
+ errno = ENOMEM;
|
|
|
+ return (void *)(-1);
|
|
|
+}
|
|
|
+
|
|
|
+void heap_init(void)
|
|
|
+{
|
|
|
+ const size_t heap_start = align_up(ARENA_START, HEAP_ALIGN);
|
|
|
+
|
|
|
+ cur_brk = heap_start;
|
|
|
+ cache_base = ARENA_END;
|
|
|
+
|
|
|
+ con_printf("heap_init: start %p, end %p\n", cur_brk, (void *)cache_base);
|
|
|
+
|
|
|
+ dll_init(&free_list);
|
|
|
+ dll_init(&lru_list);
|
|
|
+
|
|
|
+ struct dll * const hep = cache_hash + CACHE_HASH_SIZE;
|
|
|
+ for (struct dll *hp = cache_hash; hp < hep; hp++)
|
|
|
+ dll_init(hp);
|
|
|
+
|
|
|
+ disk_cache_adjust(cur_brk);
|
|
|
+}
|
|
|
+
|
|
|
/* --------------------------------------------------------------------------
|
|
|
* Interface to fatfs
|
|
|
* ------------------------------------------------------------------------- */
|
|
@@ -196,7 +339,7 @@ DSTATUS disk_initialize(BYTE drive)
|
|
|
old_status = sdc.status;
|
|
|
status = sdcard_init();
|
|
|
if ((status ^ old_status) & STA_NOINIT)
|
|
|
- disk_cache_init();
|
|
|
+ invalidate_all();
|
|
|
|
|
|
return sdc.fsstatus = status;
|
|
|
}
|
|
@@ -249,7 +392,7 @@ DRESULT disk_read(BYTE drive, BYTE *buffer,
|
|
|
|
|
|
size_t bytes = min(CACHE_BLOCK_SIZE - offset, len);
|
|
|
|
|
|
- memcpy(buffer, bp->data + offset, bytes);
|
|
|
+ memcpy(buffer, bp_to_data(bp) + offset, bytes);
|
|
|
len -= bytes;
|
|
|
block++;
|
|
|
offset = 0;
|
|
@@ -286,7 +429,7 @@ DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
|
|
|
if (!bp)
|
|
|
return RES_ERROR;
|
|
|
|
|
|
- memcpy(bp->data + offset, buffer, bytes);
|
|
|
+ memcpy(bp_to_data(bp) + offset, buffer, bytes);
|
|
|
bp->flags = FL_DIRTY;
|
|
|
|
|
|
len -= bytes;
|
|
@@ -296,8 +439,3 @@ DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
|
|
|
|
|
|
return RES_OK;
|
|
|
}
|
|
|
-
|
|
|
-DWORD get_fattime(void)
|
|
|
-{
|
|
|
- return SYSCLOCK_DATETIME; /* Already in FAT format */
|
|
|
-}
|