2
0

diskcache.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Simple disk cache
  3. */
  4. #include "fw.h"
  5. #include "list.h"
  6. #include "sdcard.h"
  7. #include "systime.h"
  8. #include "console.h"
  9. #define CACHE_BLOCKS 1024
  10. #define CACHE_BLOCK_BITS 5
  11. #define CACHE_BLOCK_SECTORS (1 << CACHE_BLOCK_BITS)
  12. #define CACHE_BLOCK_SIZE (SECTOR_SIZE << CACHE_BLOCK_BITS)
  13. #define CACHE_HASH_SIZE (CACHE_BLOCKS*2)
  14. typedef sector_t block_t;
  15. #define NO_BLOCK ((block_t)(-1))
  16. enum block_status {
  17. FL_INVALID = 0,
  18. FL_VALID = 1,
  19. FL_DIRTY_BIT = 2,
  20. FL_DIRTY = FL_VALID|FL_DIRTY_BIT
  21. };
  22. struct cache_block {
  23. struct dll hash; /* Link in hash chain or free list */
  24. struct dll lru; /* Link in LRU chain */
  25. block_t block; /* Physical block index */
  26. enum block_status flags; /* Status flags */
  27. char data[CACHE_BLOCK_SIZE] __attribute__((aligned(4)));
  28. };
  29. static struct dll __dram_noinit cache_hash[CACHE_HASH_SIZE];
  30. static struct cache_block __dram_noinit disk_cache[CACHE_BLOCKS];
  31. static struct dll lru_list;
  32. static struct dll free_list;
  33. void disk_cache_init(void)
  34. {
  35. struct cache_block *bp, *bep;
  36. struct dll *hp, *hep;
  37. dll_init(&free_list);
  38. dll_init(&lru_list);
  39. bp = disk_cache;
  40. bep = bp + CACHE_BLOCKS;
  41. while (bp < bep) {
  42. dll_insert_head(&free_list, &bp->hash);
  43. dll_insert_head(&lru_list, &bp->lru);
  44. bp->block = NO_BLOCK;
  45. bp->flags = FL_INVALID;
  46. bp++;
  47. }
  48. hp = cache_hash;
  49. hep = hp + CACHE_HASH_SIZE;
  50. while (hp < hep)
  51. dll_init(hp++);
  52. }
  53. static inline __attribute__((const)) struct dll *hash_slot(block_t block)
  54. {
  55. uint64_t m;
  56. uint32_t hash;
  57. m = UINT64_C(0x34f1f85d) * block;
  58. hash = (m >> 32) + m;
  59. return &cache_hash[hash % CACHE_BLOCKS];
  60. }
  61. static struct cache_block *disk_cache_find(block_t block)
  62. {
  63. struct dll *hp, *bhp;
  64. struct cache_block *bp;
  65. hp = hash_slot(block);
  66. for (bhp = hp->next; bhp != hp; bhp = bhp->next) {
  67. bp = container_of(bhp, struct cache_block, hash);
  68. if (bp->block == block)
  69. return bp;
  70. }
  71. return NULL;
  72. }
  73. static void invalidate_block(struct cache_block *bp)
  74. {
  75. dll_remove(&bp->hash);
  76. dll_insert_head(&free_list, &bp->hash);
  77. bp->block = NO_BLOCK;
  78. bp->flags = FL_INVALID;
  79. dll_demote(&lru_list, &bp->lru);
  80. }
  81. static DRESULT sync_block(struct cache_block *bp)
  82. {
  83. if (bp->flags == FL_DIRTY) {
  84. sector_t sector = bp->block << CACHE_BLOCK_BITS;
  85. sector_t size = sdc.lbasize;
  86. sector_t sectors = min(CACHE_BLOCK_SECTORS, size - sector);
  87. if (sdcard_write_sectors(bp->data, sector, sectors) != (int)sectors) {
  88. invalidate_block(bp); /* Or...? */
  89. return RES_ERROR;
  90. }
  91. bp->flags = FL_VALID;
  92. }
  93. return RES_OK;
  94. }
  95. static DRESULT clear_block(struct cache_block *bp)
  96. {
  97. DRESULT rv;
  98. rv = sync_block(bp);
  99. if (rv != RES_OK)
  100. return rv;
  101. invalidate_block(bp);
  102. return RES_OK;
  103. }
  104. static DRESULT sync_all(void)
  105. {
  106. DRESULT rv = RES_OK;
  107. struct dll *bhp;
  108. struct cache_block *bp;
  109. for (bhp = lru_list.next; bhp != &lru_list; bhp = bhp->next) {
  110. bp = container_of(bhp, struct cache_block, lru);
  111. if (bp->flags == FL_DIRTY)
  112. rv |= sync_block(bp);
  113. }
  114. return rv;
  115. }
  116. static struct cache_block *disk_cache_get(block_t block, bool do_read)
  117. {
  118. const sector_t size = sdc.lbasize;
  119. struct cache_block *bp;
  120. bp = disk_cache_find(block);
  121. if (!bp) {
  122. /* Block not in cache, need to get it */
  123. sector_t sector = block << CACHE_BLOCK_BITS;
  124. int sectors = CACHE_BLOCK_SECTORS;
  125. if (sector >= size)
  126. return NULL;
  127. if (sector + sectors > size)
  128. sectors = size - sectors; /* Truncated final block */
  129. /* Get the oldest block */
  130. bp = container_of(lru_list.prev, struct cache_block, lru);
  131. clear_block(bp);
  132. if (do_read) {
  133. if (sdcard_read_sectors(bp->data, sector, sectors) != sectors)
  134. return NULL;
  135. bp->flags = FL_VALID;
  136. }
  137. bp->block = block;
  138. dll_insert_head(hash_slot(block), &bp->hash);
  139. }
  140. dll_promote(&lru_list, &bp->lru);
  141. return bp;
  142. }
  143. /* --------------------------------------------------------------------------
  144. * Interface to fatfs
  145. * ------------------------------------------------------------------------- */
  146. DSTATUS disk_initialize(BYTE drive)
  147. {
  148. DSTATUS status, old_status;
  149. if (drive != 0)
  150. return STA_NOINIT | STA_NODISK;
  151. old_status = sdc.status;
  152. status = sdcard_init();
  153. if ((status ^ old_status) & STA_NOINIT)
  154. disk_cache_init();
  155. return sdc.fsstatus = status;
  156. }
  157. DRESULT disk_ioctl(BYTE drive, BYTE command, void *buffer)
  158. {
  159. if (drive != 0)
  160. return STA_NOINIT | STA_NODISK;
  161. if (sdc.status & STA_NOINIT)
  162. return RES_NOTRDY;
  163. switch (command) {
  164. case CTRL_SYNC:
  165. return sync_all();
  166. case GET_SECTOR_SIZE:
  167. *(WORD *)buffer = SECTOR_SIZE;
  168. return RES_OK;
  169. case GET_SECTOR_COUNT:
  170. *(DWORD *)buffer = sdc.lbasize;
  171. return RES_OK;
  172. case GET_BLOCK_SIZE:
  173. *(DWORD *)buffer = CACHE_BLOCK_SECTORS;
  174. return RES_OK;
  175. default:
  176. return RES_PARERR;
  177. }
  178. }
  179. DRESULT disk_read(BYTE drive, BYTE *buffer,
  180. LBA_t sectornumber, UINT sectorcount)
  181. {
  182. (void)drive;
  183. if (sdc.status & STA_NOINIT)
  184. return RES_NOTRDY;
  185. if (!sectorcount)
  186. return RES_OK;
  187. block_t block = sectornumber >> CACHE_BLOCK_BITS;
  188. block_t last = (sectornumber + sectorcount - 1) >> CACHE_BLOCK_BITS;
  189. size_t offset = (sectornumber & (CACHE_BLOCK_SECTORS-1)) << SECTOR_SHIFT;
  190. size_t len = sectorcount << SECTOR_SHIFT;
  191. while (block <= last) {
  192. struct cache_block *bp = disk_cache_get(block, true);
  193. if (!bp)
  194. return RES_ERROR;
  195. size_t bytes = min(CACHE_BLOCK_SIZE - offset, len);
  196. memcpy(buffer, bp->data + offset, bytes);
  197. len -= bytes;
  198. block++;
  199. offset = 0;
  200. }
  201. return RES_OK;
  202. }
  203. DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
  204. UINT sectorcount)
  205. {
  206. (void)drive;
  207. if (sdc.status & STA_NOINIT)
  208. return RES_NOTRDY;
  209. if (!sectorcount)
  210. return RES_OK;
  211. block_t block = sectornumber >> CACHE_BLOCK_BITS;
  212. block_t last = (sectornumber + sectorcount - 1) >> CACHE_BLOCK_BITS;
  213. size_t offset = (sectornumber & (CACHE_BLOCK_SECTORS-1)) << SECTOR_SHIFT;
  214. size_t len = sectorcount << SECTOR_SHIFT;
  215. size_t size = sdc.lbasize;
  216. while (block <= last) {
  217. sector_t sector = block << CACHE_BLOCK_BITS;
  218. sector_t sectors = min(CACHE_BLOCK_SECTORS, size - sector);
  219. size_t block_bytes = sectors << SECTOR_SHIFT;
  220. size_t bytes = min(block_bytes - offset, len);
  221. struct cache_block *bp;
  222. bp = disk_cache_get(block, bytes < block_bytes);
  223. if (!bp)
  224. return RES_ERROR;
  225. memcpy(bp->data + offset, buffer, bytes);
  226. bp->flags = FL_DIRTY;
  227. len -= bytes;
  228. block++;
  229. offset = 0;
  230. }
  231. return RES_OK;
  232. }
  233. DWORD get_fattime(void)
  234. {
  235. return SYSCLOCK_DATETIME; /* Already in FAT format */
  236. }