Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Nelson Integration, LLC 2016 |
| 4 | * Author: Eric Nelson<eric@nelint.com> |
| 5 | * |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 6 | */ |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 8 | #include <blk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <part.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 13 | #include <linux/ctype.h> |
| 14 | #include <linux/list.h> |
| 15 | |
| 16 | struct block_cache_node { |
| 17 | struct list_head lh; |
| 18 | int iftype; |
| 19 | int devnum; |
| 20 | lbaint_t start; |
| 21 | lbaint_t blkcnt; |
| 22 | unsigned long blksz; |
| 23 | char *cache; |
| 24 | }; |
| 25 | |
Angelo Durgehello | 2c54cf5 | 2020-01-26 19:31:22 +0100 | [diff] [blame] | 26 | static LIST_HEAD(block_cache); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 27 | |
| 28 | static struct block_cache_stats _stats = { |
Marek Vasut | 0ebd033 | 2018-08-08 13:20:29 +0200 | [diff] [blame] | 29 | .max_blocks_per_entry = 8, |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 30 | .max_entries = 32 |
| 31 | }; |
| 32 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 33 | static struct block_cache_node *cache_find(int iftype, int devnum, |
| 34 | lbaint_t start, lbaint_t blkcnt, |
| 35 | unsigned long blksz) |
| 36 | { |
| 37 | struct block_cache_node *node; |
| 38 | |
| 39 | list_for_each_entry(node, &block_cache, lh) |
| 40 | if ((node->iftype == iftype) && |
| 41 | (node->devnum == devnum) && |
| 42 | (node->blksz == blksz) && |
| 43 | (node->start <= start) && |
| 44 | (node->start + node->blkcnt >= start + blkcnt)) { |
| 45 | if (block_cache.next != &node->lh) { |
| 46 | /* maintain MRU ordering */ |
| 47 | list_del(&node->lh); |
| 48 | list_add(&node->lh, &block_cache); |
| 49 | } |
| 50 | return node; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | int blkcache_read(int iftype, int devnum, |
| 56 | lbaint_t start, lbaint_t blkcnt, |
| 57 | unsigned long blksz, void *buffer) |
| 58 | { |
| 59 | struct block_cache_node *node = cache_find(iftype, devnum, start, |
| 60 | blkcnt, blksz); |
| 61 | if (node) { |
| 62 | const char *src = node->cache + (start - node->start) * blksz; |
| 63 | memcpy(buffer, src, blksz * blkcnt); |
| 64 | debug("hit: start " LBAF ", count " LBAFU "\n", |
| 65 | start, blkcnt); |
| 66 | ++_stats.hits; |
| 67 | return 1; |
| 68 | } |
| 69 | |
| 70 | debug("miss: start " LBAF ", count " LBAFU "\n", |
| 71 | start, blkcnt); |
| 72 | ++_stats.misses; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | void blkcache_fill(int iftype, int devnum, |
| 77 | lbaint_t start, lbaint_t blkcnt, |
| 78 | unsigned long blksz, void const *buffer) |
| 79 | { |
| 80 | lbaint_t bytes; |
| 81 | struct block_cache_node *node; |
| 82 | |
| 83 | /* don't cache big stuff */ |
| 84 | if (blkcnt > _stats.max_blocks_per_entry) |
| 85 | return; |
| 86 | |
| 87 | if (_stats.max_entries == 0) |
| 88 | return; |
| 89 | |
| 90 | bytes = blksz * blkcnt; |
| 91 | if (_stats.max_entries <= _stats.entries) { |
| 92 | /* pop LRU */ |
| 93 | node = (struct block_cache_node *)block_cache.prev; |
| 94 | list_del(&node->lh); |
| 95 | _stats.entries--; |
| 96 | debug("drop: start " LBAF ", count " LBAFU "\n", |
| 97 | node->start, node->blkcnt); |
| 98 | if (node->blkcnt * node->blksz < bytes) { |
| 99 | free(node->cache); |
| 100 | node->cache = 0; |
| 101 | } |
| 102 | } else { |
| 103 | node = malloc(sizeof(*node)); |
| 104 | if (!node) |
| 105 | return; |
| 106 | node->cache = 0; |
| 107 | } |
| 108 | |
| 109 | if (!node->cache) { |
| 110 | node->cache = malloc(bytes); |
| 111 | if (!node->cache) { |
| 112 | free(node); |
| 113 | return; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | debug("fill: start " LBAF ", count " LBAFU "\n", |
| 118 | start, blkcnt); |
| 119 | |
| 120 | node->iftype = iftype; |
| 121 | node->devnum = devnum; |
| 122 | node->start = start; |
| 123 | node->blkcnt = blkcnt; |
| 124 | node->blksz = blksz; |
| 125 | memcpy(node->cache, buffer, bytes); |
| 126 | list_add(&node->lh, &block_cache); |
| 127 | _stats.entries++; |
| 128 | } |
| 129 | |
| 130 | void blkcache_invalidate(int iftype, int devnum) |
| 131 | { |
| 132 | struct list_head *entry, *n; |
| 133 | struct block_cache_node *node; |
| 134 | |
| 135 | list_for_each_safe(entry, n, &block_cache) { |
| 136 | node = (struct block_cache_node *)entry; |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 137 | if (iftype == -1 || |
| 138 | (node->iftype == iftype && node->devnum == devnum)) { |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 139 | list_del(entry); |
| 140 | free(node->cache); |
| 141 | free(node); |
| 142 | --_stats.entries; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void blkcache_configure(unsigned blocks, unsigned entries) |
| 148 | { |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 149 | /* invalidate cache if there is a change */ |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 150 | if ((blocks != _stats.max_blocks_per_entry) || |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 151 | (entries != _stats.max_entries)) |
| 152 | blkcache_invalidate(-1, 0); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 153 | |
| 154 | _stats.max_blocks_per_entry = blocks; |
| 155 | _stats.max_entries = entries; |
| 156 | |
| 157 | _stats.hits = 0; |
| 158 | _stats.misses = 0; |
| 159 | } |
| 160 | |
| 161 | void blkcache_stats(struct block_cache_stats *stats) |
| 162 | { |
| 163 | memcpy(stats, &_stats, sizeof(*stats)); |
| 164 | _stats.hits = 0; |
| 165 | _stats.misses = 0; |
| 166 | } |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 167 | |
| 168 | void blkcache_free(void) |
| 169 | { |
| 170 | blkcache_invalidate(-1, 0); |
| 171 | } |