1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) Nelson Integration, LLC 2016
4 * Author: Eric Nelson<eric@nelint.com>
12 #include <asm/global_data.h>
13 #include <linux/ctype.h>
14 #include <linux/list.h>
16 #ifdef CONFIG_NEEDS_MANUAL_RELOC
17 DECLARE_GLOBAL_DATA_PTR;
20 struct block_cache_node {
30 static LIST_HEAD(block_cache);
32 static struct block_cache_stats _stats = {
33 .max_blocks_per_entry = 8,
37 #ifdef CONFIG_NEEDS_MANUAL_RELOC
38 int blkcache_init(void)
40 struct list_head *head = &block_cache;
42 head->next = (uintptr_t)head->next + gd->reloc_off;
43 head->prev = (uintptr_t)head->prev + gd->reloc_off;
49 static struct block_cache_node *cache_find(int iftype, int devnum,
50 lbaint_t start, lbaint_t blkcnt,
53 struct block_cache_node *node;
55 list_for_each_entry(node, &block_cache, lh)
56 if ((node->iftype == iftype) &&
57 (node->devnum == devnum) &&
58 (node->blksz == blksz) &&
59 (node->start <= start) &&
60 (node->start + node->blkcnt >= start + blkcnt)) {
61 if (block_cache.next != &node->lh) {
62 /* maintain MRU ordering */
64 list_add(&node->lh, &block_cache);
71 int blkcache_read(int iftype, int devnum,
72 lbaint_t start, lbaint_t blkcnt,
73 unsigned long blksz, void *buffer)
75 struct block_cache_node *node = cache_find(iftype, devnum, start,
78 const char *src = node->cache + (start - node->start) * blksz;
79 memcpy(buffer, src, blksz * blkcnt);
80 debug("hit: start " LBAF ", count " LBAFU "\n",
86 debug("miss: start " LBAF ", count " LBAFU "\n",
92 void blkcache_fill(int iftype, int devnum,
93 lbaint_t start, lbaint_t blkcnt,
94 unsigned long blksz, void const *buffer)
97 struct block_cache_node *node;
99 /* don't cache big stuff */
100 if (blkcnt > _stats.max_blocks_per_entry)
103 if (_stats.max_entries == 0)
106 bytes = blksz * blkcnt;
107 if (_stats.max_entries <= _stats.entries) {
109 node = (struct block_cache_node *)block_cache.prev;
112 debug("drop: start " LBAF ", count " LBAFU "\n",
113 node->start, node->blkcnt);
114 if (node->blkcnt * node->blksz < bytes) {
119 node = malloc(sizeof(*node));
126 node->cache = malloc(bytes);
133 debug("fill: start " LBAF ", count " LBAFU "\n",
136 node->iftype = iftype;
137 node->devnum = devnum;
139 node->blkcnt = blkcnt;
141 memcpy(node->cache, buffer, bytes);
142 list_add(&node->lh, &block_cache);
146 void blkcache_invalidate(int iftype, int devnum)
148 struct list_head *entry, *n;
149 struct block_cache_node *node;
151 list_for_each_safe(entry, n, &block_cache) {
152 node = (struct block_cache_node *)entry;
153 if ((node->iftype == iftype) &&
154 (node->devnum == devnum)) {
163 void blkcache_configure(unsigned blocks, unsigned entries)
165 struct block_cache_node *node;
166 if ((blocks != _stats.max_blocks_per_entry) ||
167 (entries != _stats.max_entries)) {
168 /* invalidate cache */
169 while (!list_empty(&block_cache)) {
170 node = (struct block_cache_node *)block_cache.next;
178 _stats.max_blocks_per_entry = blocks;
179 _stats.max_entries = entries;
185 void blkcache_stats(struct block_cache_stats *stats)
187 memcpy(stats, &_stats, sizeof(*stats));