diskcache.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. static 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;
  149. if (drive != 0)
  150. return STA_NOINIT | STA_NODISK;
  151. status = sdcard_init();
  152. if (!(status & STA_NOINIT))
  153. disk_cache_init();
  154. return sdc.fsstatus = status;
  155. }
  156. DRESULT disk_ioctl(BYTE drive, BYTE command, void *buffer)
  157. {
  158. if (drive != 0)
  159. return STA_NOINIT;
  160. switch (command) {
  161. case CTRL_SYNC:
  162. return sync_all();
  163. case GET_SECTOR_SIZE:
  164. *(WORD *)buffer = SECTOR_SIZE;
  165. return RES_OK;
  166. case GET_SECTOR_COUNT:
  167. *(DWORD *)buffer = sdc.lbasize;
  168. return RES_OK;
  169. case GET_BLOCK_SIZE:
  170. *(DWORD *)buffer = CACHE_BLOCK_SECTORS;
  171. return RES_OK;
  172. default:
  173. return RES_PARERR;
  174. }
  175. }
  176. DRESULT disk_read(BYTE drive, BYTE *buffer,
  177. LBA_t sectornumber, UINT sectorcount)
  178. {
  179. (void)drive;
  180. if (!sectorcount)
  181. return RES_OK;
  182. block_t block = sectornumber >> CACHE_BLOCK_BITS;
  183. block_t last = (sectornumber + sectorcount - 1) >> CACHE_BLOCK_BITS;
  184. size_t offset = (sectornumber & (CACHE_BLOCK_SECTORS-1)) << SECTOR_SHIFT;
  185. size_t len = sectorcount << SECTOR_SHIFT;
  186. while (block <= last) {
  187. struct cache_block *bp = disk_cache_get(block, true);
  188. if (!bp)
  189. return RES_ERROR;
  190. size_t bytes = min(CACHE_BLOCK_SIZE - offset, len);
  191. memcpy(buffer, bp->data + offset, bytes);
  192. len -= bytes;
  193. block++;
  194. offset = 0;
  195. }
  196. return RES_OK;
  197. }
  198. DRESULT disk_write(BYTE drive, const BYTE *buffer, LBA_t sectornumber,
  199. UINT sectorcount)
  200. {
  201. (void)drive;
  202. if (!sectorcount)
  203. return RES_OK;
  204. block_t block = sectornumber >> CACHE_BLOCK_BITS;
  205. block_t last = (sectornumber + sectorcount - 1) >> CACHE_BLOCK_BITS;
  206. size_t offset = (sectornumber & (CACHE_BLOCK_SECTORS-1)) << SECTOR_SHIFT;
  207. size_t len = sectorcount << SECTOR_SHIFT;
  208. size_t size = sdc.lbasize;
  209. while (block <= last) {
  210. sector_t sector = block << CACHE_BLOCK_BITS;
  211. sector_t sectors = min(CACHE_BLOCK_SECTORS, size - sector);
  212. size_t block_bytes = sectors << SECTOR_SHIFT;
  213. size_t bytes = min(block_bytes - offset, len);
  214. struct cache_block *bp;
  215. bp = disk_cache_get(block, bytes < block_bytes);
  216. if (!bp)
  217. return RES_ERROR;
  218. memcpy(bp->data + offset, buffer, bytes);
  219. bp->flags = FL_DIRTY;
  220. len -= bytes;
  221. block++;
  222. offset = 0;
  223. }
  224. return RES_OK;
  225. }
  226. DWORD get_fattime(void)
  227. {
  228. return SYSCLOCK_DATETIME; /* Already in FAT format */
  229. }