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