diskcache.c 6.7 KB

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