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/slab.h>
14 #include <linux/lzo.h>
18 struct regcache_lzo_ctx {
24 size_t decompressed_size;
25 unsigned long *sync_bmp;
29 #define LZO_BLOCK_NUM 8
30 static int regcache_lzo_block_count(void)
35 static int regcache_lzo_prepare(struct regcache_lzo_ctx *lzo_ctx)
37 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
43 static int regcache_lzo_compress(struct regcache_lzo_ctx *lzo_ctx)
48 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
49 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
50 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
52 lzo_ctx->dst_len = compress_size;
56 static int regcache_lzo_decompress(struct regcache_lzo_ctx *lzo_ctx)
61 dst_len = lzo_ctx->dst_len;
62 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
63 lzo_ctx->dst, &dst_len);
64 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
69 static int regcache_lzo_compress_cache_block(struct regmap *map,
70 struct regcache_lzo_ctx *lzo_ctx)
74 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
75 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
81 ret = regcache_lzo_compress(lzo_ctx);
87 static int regcache_lzo_decompress_cache_block(struct regmap *map,
88 struct regcache_lzo_ctx *lzo_ctx)
92 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
93 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
99 ret = regcache_lzo_decompress(lzo_ctx);
105 static inline int regcache_lzo_get_blkindex(struct regmap *map,
108 return (reg * map->cache_word_size) /
109 DIV_ROUND_UP(map->cache_size_raw, regcache_lzo_block_count());
112 static inline int regcache_lzo_get_blkpos(struct regmap *map,
115 return reg % (DIV_ROUND_UP(map->cache_size_raw, regcache_lzo_block_count()) /
116 map->cache_word_size);
119 static inline int regcache_lzo_get_blksize(struct regmap *map)
121 return DIV_ROUND_UP(map->cache_size_raw, regcache_lzo_block_count());
124 static int regcache_lzo_init(struct regmap *map)
126 struct regcache_lzo_ctx **lzo_blocks;
128 int ret, i, blksize, blkcount;
130 unsigned long *sync_bmp;
134 blkcount = regcache_lzo_block_count();
135 map->cache = kzalloc(blkcount * sizeof *lzo_blocks,
139 lzo_blocks = map->cache;
142 * allocate a bitmap to be used when syncing the cache with
143 * the hardware. Each time a register is modified, the corresponding
144 * bit is set in the bitmap, so we know that we have to sync
147 bmp_size = map->num_reg_defaults_raw;
148 sync_bmp = kmalloc(BITS_TO_LONGS(bmp_size) * sizeof(long),
154 bitmap_zero(sync_bmp, bmp_size);
156 /* allocate the lzo blocks and initialize them */
157 for (i = 0; i < blkcount; i++) {
158 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
160 if (!lzo_blocks[i]) {
165 lzo_blocks[i]->sync_bmp = sync_bmp;
166 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
167 /* alloc the working space for the compressed block */
168 ret = regcache_lzo_prepare(lzo_blocks[i]);
173 blksize = regcache_lzo_get_blksize(map);
174 p = map->reg_defaults_raw;
175 end = map->reg_defaults_raw + map->cache_size_raw;
176 /* compress the register map and fill the lzo blocks */
177 for (i = 0; i < blkcount; i++, p += blksize) {
178 lzo_blocks[i]->src = p;
179 if (p + blksize > end)
180 lzo_blocks[i]->src_len = end - p;
182 lzo_blocks[i]->src_len = blksize;
183 ret = regcache_lzo_compress_cache_block(map,
187 lzo_blocks[i]->decompressed_size =
188 lzo_blocks[i]->src_len;
197 static int regcache_lzo_exit(struct regmap *map)
199 struct regcache_lzo_ctx **lzo_blocks;
202 lzo_blocks = map->cache;
206 blkcount = regcache_lzo_block_count();
208 * the pointer to the bitmap used for syncing the cache
209 * is shared amongst all lzo_blocks. Ensure it is freed
213 kfree(lzo_blocks[0]->sync_bmp);
214 for (i = 0; i < blkcount; i++) {
216 kfree(lzo_blocks[i]->wmem);
217 kfree(lzo_blocks[i]->dst);
219 /* each lzo_block is a pointer returned by kmalloc or NULL */
220 kfree(lzo_blocks[i]);
227 static int regcache_lzo_read(struct regmap *map,
228 unsigned int reg, unsigned int *value)
230 struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
231 int ret, blkindex, blkpos;
232 size_t blksize, tmp_dst_len;
235 /* index of the compressed lzo block */
236 blkindex = regcache_lzo_get_blkindex(map, reg);
237 /* register index within the decompressed block */
238 blkpos = regcache_lzo_get_blkpos(map, reg);
239 /* size of the compressed block */
240 blksize = regcache_lzo_get_blksize(map);
241 lzo_blocks = map->cache;
242 lzo_block = lzo_blocks[blkindex];
244 /* save the pointer and length of the compressed block */
245 tmp_dst = lzo_block->dst;
246 tmp_dst_len = lzo_block->dst_len;
248 /* prepare the source to be the compressed block */
249 lzo_block->src = lzo_block->dst;
250 lzo_block->src_len = lzo_block->dst_len;
252 /* decompress the block */
253 ret = regcache_lzo_decompress_cache_block(map, lzo_block);
255 /* fetch the value from the cache */
256 *value = regcache_get_val(lzo_block->dst, blkpos,
257 map->cache_word_size);
259 kfree(lzo_block->dst);
260 /* restore the pointer and length of the compressed block */
261 lzo_block->dst = tmp_dst;
262 lzo_block->dst_len = tmp_dst_len;
267 static int regcache_lzo_write(struct regmap *map,
268 unsigned int reg, unsigned int value)
270 struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
271 int ret, blkindex, blkpos;
272 size_t blksize, tmp_dst_len;
275 /* index of the compressed lzo block */
276 blkindex = regcache_lzo_get_blkindex(map, reg);
277 /* register index within the decompressed block */
278 blkpos = regcache_lzo_get_blkpos(map, reg);
279 /* size of the compressed block */
280 blksize = regcache_lzo_get_blksize(map);
281 lzo_blocks = map->cache;
282 lzo_block = lzo_blocks[blkindex];
284 /* save the pointer and length of the compressed block */
285 tmp_dst = lzo_block->dst;
286 tmp_dst_len = lzo_block->dst_len;
288 /* prepare the source to be the compressed block */
289 lzo_block->src = lzo_block->dst;
290 lzo_block->src_len = lzo_block->dst_len;
292 /* decompress the block */
293 ret = regcache_lzo_decompress_cache_block(map, lzo_block);
295 kfree(lzo_block->dst);
299 /* write the new value to the cache */
300 if (regcache_set_val(lzo_block->dst, blkpos, value,
301 map->cache_word_size)) {
302 kfree(lzo_block->dst);
306 /* prepare the source to be the decompressed block */
307 lzo_block->src = lzo_block->dst;
308 lzo_block->src_len = lzo_block->dst_len;
310 /* compress the block */
311 ret = regcache_lzo_compress_cache_block(map, lzo_block);
313 kfree(lzo_block->dst);
314 kfree(lzo_block->src);
318 /* set the bit so we know we have to sync this register */
319 set_bit(reg, lzo_block->sync_bmp);
321 kfree(lzo_block->src);
324 lzo_block->dst = tmp_dst;
325 lzo_block->dst_len = tmp_dst_len;
329 static int regcache_lzo_sync(struct regmap *map)
331 struct regcache_lzo_ctx **lzo_blocks;
336 lzo_blocks = map->cache;
337 for_each_set_bit(i, lzo_blocks[0]->sync_bmp, lzo_blocks[0]->sync_bmp_nbits) {
338 ret = regcache_read(map, i, &val);
341 map->cache_bypass = 1;
342 ret = _regmap_write(map, i, val);
343 map->cache_bypass = 0;
346 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
353 struct regcache_ops regcache_lzo_ops = {
354 .type = REGCACHE_LZO,
356 .init = regcache_lzo_init,
357 .exit = regcache_lzo_exit,
358 .read = regcache_lzo_read,
359 .write = regcache_lzo_write,
360 .sync = regcache_lzo_sync