2 * Register cache access API - LZO caching support
4 * Copyright 2011 Wolfson Microelectronics plc
6 * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/device.h>
14 #include <linux/lzo.h>
15 #include <linux/slab.h>
19 static int regcache_lzo_exit(struct regmap *map);
21 struct regcache_lzo_ctx {
27 size_t decompressed_size;
28 unsigned long *sync_bmp;
32 #define LZO_BLOCK_NUM 8
33 static int regcache_lzo_block_count(struct regmap *map)
38 static int regcache_lzo_prepare(struct regcache_lzo_ctx *lzo_ctx)
40 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
46 static int regcache_lzo_compress(struct regcache_lzo_ctx *lzo_ctx)
51 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
52 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
53 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
55 lzo_ctx->dst_len = compress_size;
59 static int regcache_lzo_decompress(struct regcache_lzo_ctx *lzo_ctx)
64 dst_len = lzo_ctx->dst_len;
65 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
66 lzo_ctx->dst, &dst_len);
67 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
72 static int regcache_lzo_compress_cache_block(struct regmap *map,
73 struct regcache_lzo_ctx *lzo_ctx)
77 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
78 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
84 ret = regcache_lzo_compress(lzo_ctx);
90 static int regcache_lzo_decompress_cache_block(struct regmap *map,
91 struct regcache_lzo_ctx *lzo_ctx)
95 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
96 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
102 ret = regcache_lzo_decompress(lzo_ctx);
108 static inline int regcache_lzo_get_blkindex(struct regmap *map,
111 return ((reg / map->reg_stride) * map->cache_word_size) /
112 DIV_ROUND_UP(map->cache_size_raw,
113 regcache_lzo_block_count(map));
116 static inline int regcache_lzo_get_blkpos(struct regmap *map,
119 return (reg / map->reg_stride) %
120 (DIV_ROUND_UP(map->cache_size_raw,
121 regcache_lzo_block_count(map)) /
122 map->cache_word_size);
125 static inline int regcache_lzo_get_blksize(struct regmap *map)
127 return DIV_ROUND_UP(map->cache_size_raw,
128 regcache_lzo_block_count(map));
131 static int regcache_lzo_init(struct regmap *map)
133 struct regcache_lzo_ctx **lzo_blocks;
135 int ret, i, blksize, blkcount;
137 unsigned long *sync_bmp;
141 blkcount = regcache_lzo_block_count(map);
142 map->cache = kcalloc(blkcount, sizeof(*lzo_blocks),
146 lzo_blocks = map->cache;
149 * allocate a bitmap to be used when syncing the cache with
150 * the hardware. Each time a register is modified, the corresponding
151 * bit is set in the bitmap, so we know that we have to sync
154 bmp_size = map->num_reg_defaults_raw;
155 sync_bmp = kmalloc_array(BITS_TO_LONGS(bmp_size), sizeof(long),
161 bitmap_zero(sync_bmp, bmp_size);
163 /* allocate the lzo blocks and initialize them */
164 for (i = 0; i < blkcount; i++) {
165 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
167 if (!lzo_blocks[i]) {
172 lzo_blocks[i]->sync_bmp = sync_bmp;
173 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
174 /* alloc the working space for the compressed block */
175 ret = regcache_lzo_prepare(lzo_blocks[i]);
180 blksize = regcache_lzo_get_blksize(map);
181 p = map->reg_defaults_raw;
182 end = map->reg_defaults_raw + map->cache_size_raw;
183 /* compress the register map and fill the lzo blocks */
184 for (i = 0; i < blkcount; i++, p += blksize) {
185 lzo_blocks[i]->src = p;
186 if (p + blksize > end)
187 lzo_blocks[i]->src_len = end - p;
189 lzo_blocks[i]->src_len = blksize;
190 ret = regcache_lzo_compress_cache_block(map,
194 lzo_blocks[i]->decompressed_size =
195 lzo_blocks[i]->src_len;
200 regcache_lzo_exit(map);
204 static int regcache_lzo_exit(struct regmap *map)
206 struct regcache_lzo_ctx **lzo_blocks;
209 lzo_blocks = map->cache;
213 blkcount = regcache_lzo_block_count(map);
215 * the pointer to the bitmap used for syncing the cache
216 * is shared amongst all lzo_blocks. Ensure it is freed
220 kfree(lzo_blocks[0]->sync_bmp);
221 for (i = 0; i < blkcount; i++) {
223 kfree(lzo_blocks[i]->wmem);
224 kfree(lzo_blocks[i]->dst);
226 /* each lzo_block is a pointer returned by kmalloc or NULL */
227 kfree(lzo_blocks[i]);
234 static int regcache_lzo_read(struct regmap *map,
235 unsigned int reg, unsigned int *value)
237 struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
238 int ret, blkindex, blkpos;
242 /* index of the compressed lzo block */
243 blkindex = regcache_lzo_get_blkindex(map, reg);
244 /* register index within the decompressed block */
245 blkpos = regcache_lzo_get_blkpos(map, reg);
246 lzo_blocks = map->cache;
247 lzo_block = lzo_blocks[blkindex];
249 /* save the pointer and length of the compressed block */
250 tmp_dst = lzo_block->dst;
251 tmp_dst_len = lzo_block->dst_len;
253 /* prepare the source to be the compressed block */
254 lzo_block->src = lzo_block->dst;
255 lzo_block->src_len = lzo_block->dst_len;
257 /* decompress the block */
258 ret = regcache_lzo_decompress_cache_block(map, lzo_block);
260 /* fetch the value from the cache */
261 *value = regcache_get_val(map, lzo_block->dst, blkpos);
263 kfree(lzo_block->dst);
264 /* restore the pointer and length of the compressed block */
265 lzo_block->dst = tmp_dst;
266 lzo_block->dst_len = tmp_dst_len;
271 static int regcache_lzo_write(struct regmap *map,
272 unsigned int reg, unsigned int value)
274 struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
275 int ret, blkindex, blkpos;
279 /* index of the compressed lzo block */
280 blkindex = regcache_lzo_get_blkindex(map, reg);
281 /* register index within the decompressed block */
282 blkpos = regcache_lzo_get_blkpos(map, reg);
283 lzo_blocks = map->cache;
284 lzo_block = lzo_blocks[blkindex];
286 /* save the pointer and length of the compressed block */
287 tmp_dst = lzo_block->dst;
288 tmp_dst_len = lzo_block->dst_len;
290 /* prepare the source to be the compressed block */
291 lzo_block->src = lzo_block->dst;
292 lzo_block->src_len = lzo_block->dst_len;
294 /* decompress the block */
295 ret = regcache_lzo_decompress_cache_block(map, lzo_block);
297 kfree(lzo_block->dst);
301 /* write the new value to the cache */
302 if (regcache_set_val(map, lzo_block->dst, blkpos, value)) {
303 kfree(lzo_block->dst);
307 /* prepare the source to be the decompressed block */
308 lzo_block->src = lzo_block->dst;
309 lzo_block->src_len = lzo_block->dst_len;
311 /* compress the block */
312 ret = regcache_lzo_compress_cache_block(map, lzo_block);
314 kfree(lzo_block->dst);
315 kfree(lzo_block->src);
319 /* set the bit so we know we have to sync this register */
320 set_bit(reg / map->reg_stride, lzo_block->sync_bmp);
322 kfree(lzo_block->src);
325 lzo_block->dst = tmp_dst;
326 lzo_block->dst_len = tmp_dst_len;
330 static int regcache_lzo_sync(struct regmap *map, unsigned int min,
333 struct regcache_lzo_ctx **lzo_blocks;
338 lzo_blocks = map->cache;
340 for_each_set_bit_from(i, lzo_blocks[0]->sync_bmp,
341 lzo_blocks[0]->sync_bmp_nbits) {
345 ret = regcache_read(map, i, &val);
349 /* Is this the hardware default? If so skip. */
350 ret = regcache_lookup_reg(map, i);
351 if (ret > 0 && val == map->reg_defaults[ret].def)
354 map->cache_bypass = true;
355 ret = _regmap_write(map, i, val);
356 map->cache_bypass = false;
359 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
366 struct regcache_ops regcache_lzo_ops = {
367 .type = REGCACHE_COMPRESSED,
369 .init = regcache_lzo_init,
370 .exit = regcache_lzo_exit,
371 .read = regcache_lzo_read,
372 .write = regcache_lzo_write,
373 .sync = regcache_lzo_sync