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 static int regcache_lzo_exit(struct regmap
*map
);
20 struct regcache_lzo_ctx
{
26 size_t decompressed_size
;
27 unsigned long *sync_bmp
;
31 #define LZO_BLOCK_NUM 8
32 static int regcache_lzo_block_count(struct regmap
*map
)
37 static int regcache_lzo_prepare(struct regcache_lzo_ctx
*lzo_ctx
)
39 lzo_ctx
->wmem
= kmalloc(LZO1X_MEM_COMPRESS
, GFP_KERNEL
);
45 static int regcache_lzo_compress(struct regcache_lzo_ctx
*lzo_ctx
)
50 ret
= lzo1x_1_compress(lzo_ctx
->src
, lzo_ctx
->src_len
,
51 lzo_ctx
->dst
, &compress_size
, lzo_ctx
->wmem
);
52 if (ret
!= LZO_E_OK
|| compress_size
> lzo_ctx
->dst_len
)
54 lzo_ctx
->dst_len
= compress_size
;
58 static int regcache_lzo_decompress(struct regcache_lzo_ctx
*lzo_ctx
)
63 dst_len
= lzo_ctx
->dst_len
;
64 ret
= lzo1x_decompress_safe(lzo_ctx
->src
, lzo_ctx
->src_len
,
65 lzo_ctx
->dst
, &dst_len
);
66 if (ret
!= LZO_E_OK
|| dst_len
!= lzo_ctx
->dst_len
)
71 static int regcache_lzo_compress_cache_block(struct regmap
*map
,
72 struct regcache_lzo_ctx
*lzo_ctx
)
76 lzo_ctx
->dst_len
= lzo1x_worst_compress(PAGE_SIZE
);
77 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
83 ret
= regcache_lzo_compress(lzo_ctx
);
89 static int regcache_lzo_decompress_cache_block(struct regmap
*map
,
90 struct regcache_lzo_ctx
*lzo_ctx
)
94 lzo_ctx
->dst_len
= lzo_ctx
->decompressed_size
;
95 lzo_ctx
->dst
= kmalloc(lzo_ctx
->dst_len
, GFP_KERNEL
);
101 ret
= regcache_lzo_decompress(lzo_ctx
);
107 static inline int regcache_lzo_get_blkindex(struct regmap
*map
,
110 return (reg
* map
->cache_word_size
) /
111 DIV_ROUND_UP(map
->cache_size_raw
,
112 regcache_lzo_block_count(map
));
115 static inline int regcache_lzo_get_blkpos(struct regmap
*map
,
118 return reg
% (DIV_ROUND_UP(map
->cache_size_raw
,
119 regcache_lzo_block_count(map
)) /
120 map
->cache_word_size
);
123 static inline int regcache_lzo_get_blksize(struct regmap
*map
)
125 return DIV_ROUND_UP(map
->cache_size_raw
,
126 regcache_lzo_block_count(map
));
129 static int regcache_lzo_init(struct regmap
*map
)
131 struct regcache_lzo_ctx
**lzo_blocks
;
133 int ret
, i
, blksize
, blkcount
;
135 unsigned long *sync_bmp
;
139 blkcount
= regcache_lzo_block_count(map
);
140 map
->cache
= kzalloc(blkcount
* sizeof *lzo_blocks
,
144 lzo_blocks
= map
->cache
;
147 * allocate a bitmap to be used when syncing the cache with
148 * the hardware. Each time a register is modified, the corresponding
149 * bit is set in the bitmap, so we know that we have to sync
152 bmp_size
= map
->num_reg_defaults_raw
;
153 sync_bmp
= kmalloc(BITS_TO_LONGS(bmp_size
) * sizeof(long),
159 bitmap_zero(sync_bmp
, bmp_size
);
161 /* allocate the lzo blocks and initialize them */
162 for (i
= 0; i
< blkcount
; i
++) {
163 lzo_blocks
[i
] = kzalloc(sizeof **lzo_blocks
,
165 if (!lzo_blocks
[i
]) {
170 lzo_blocks
[i
]->sync_bmp
= sync_bmp
;
171 lzo_blocks
[i
]->sync_bmp_nbits
= bmp_size
;
172 /* alloc the working space for the compressed block */
173 ret
= regcache_lzo_prepare(lzo_blocks
[i
]);
178 blksize
= regcache_lzo_get_blksize(map
);
179 p
= map
->reg_defaults_raw
;
180 end
= map
->reg_defaults_raw
+ map
->cache_size_raw
;
181 /* compress the register map and fill the lzo blocks */
182 for (i
= 0; i
< blkcount
; i
++, p
+= blksize
) {
183 lzo_blocks
[i
]->src
= p
;
184 if (p
+ blksize
> end
)
185 lzo_blocks
[i
]->src_len
= end
- p
;
187 lzo_blocks
[i
]->src_len
= blksize
;
188 ret
= regcache_lzo_compress_cache_block(map
,
192 lzo_blocks
[i
]->decompressed_size
=
193 lzo_blocks
[i
]->src_len
;
198 regcache_lzo_exit(map
);
202 static int regcache_lzo_exit(struct regmap
*map
)
204 struct regcache_lzo_ctx
**lzo_blocks
;
207 lzo_blocks
= map
->cache
;
211 blkcount
= regcache_lzo_block_count(map
);
213 * the pointer to the bitmap used for syncing the cache
214 * is shared amongst all lzo_blocks. Ensure it is freed
218 kfree(lzo_blocks
[0]->sync_bmp
);
219 for (i
= 0; i
< blkcount
; i
++) {
221 kfree(lzo_blocks
[i
]->wmem
);
222 kfree(lzo_blocks
[i
]->dst
);
224 /* each lzo_block is a pointer returned by kmalloc or NULL */
225 kfree(lzo_blocks
[i
]);
232 static int regcache_lzo_read(struct regmap
*map
,
233 unsigned int reg
, unsigned int *value
)
235 struct regcache_lzo_ctx
*lzo_block
, **lzo_blocks
;
236 int ret
, blkindex
, blkpos
;
237 size_t blksize
, tmp_dst_len
;
240 /* index of the compressed lzo block */
241 blkindex
= regcache_lzo_get_blkindex(map
, reg
);
242 /* register index within the decompressed block */
243 blkpos
= regcache_lzo_get_blkpos(map
, reg
);
244 /* size of the compressed block */
245 blksize
= regcache_lzo_get_blksize(map
);
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(lzo_block
->dst
, blkpos
,
262 map
->cache_word_size
);
264 kfree(lzo_block
->dst
);
265 /* restore the pointer and length of the compressed block */
266 lzo_block
->dst
= tmp_dst
;
267 lzo_block
->dst_len
= tmp_dst_len
;
272 static int regcache_lzo_write(struct regmap
*map
,
273 unsigned int reg
, unsigned int value
)
275 struct regcache_lzo_ctx
*lzo_block
, **lzo_blocks
;
276 int ret
, blkindex
, blkpos
;
277 size_t blksize
, tmp_dst_len
;
280 /* index of the compressed lzo block */
281 blkindex
= regcache_lzo_get_blkindex(map
, reg
);
282 /* register index within the decompressed block */
283 blkpos
= regcache_lzo_get_blkpos(map
, reg
);
284 /* size of the compressed block */
285 blksize
= regcache_lzo_get_blksize(map
);
286 lzo_blocks
= map
->cache
;
287 lzo_block
= lzo_blocks
[blkindex
];
289 /* save the pointer and length of the compressed block */
290 tmp_dst
= lzo_block
->dst
;
291 tmp_dst_len
= lzo_block
->dst_len
;
293 /* prepare the source to be the compressed block */
294 lzo_block
->src
= lzo_block
->dst
;
295 lzo_block
->src_len
= lzo_block
->dst_len
;
297 /* decompress the block */
298 ret
= regcache_lzo_decompress_cache_block(map
, lzo_block
);
300 kfree(lzo_block
->dst
);
304 /* write the new value to the cache */
305 if (regcache_set_val(lzo_block
->dst
, blkpos
, value
,
306 map
->cache_word_size
)) {
307 kfree(lzo_block
->dst
);
311 /* prepare the source to be the decompressed block */
312 lzo_block
->src
= lzo_block
->dst
;
313 lzo_block
->src_len
= lzo_block
->dst_len
;
315 /* compress the block */
316 ret
= regcache_lzo_compress_cache_block(map
, lzo_block
);
318 kfree(lzo_block
->dst
);
319 kfree(lzo_block
->src
);
323 /* set the bit so we know we have to sync this register */
324 set_bit(reg
, lzo_block
->sync_bmp
);
326 kfree(lzo_block
->src
);
329 lzo_block
->dst
= tmp_dst
;
330 lzo_block
->dst_len
= tmp_dst_len
;
334 static int regcache_lzo_sync(struct regmap
*map
)
336 struct regcache_lzo_ctx
**lzo_blocks
;
341 lzo_blocks
= map
->cache
;
342 for_each_set_bit(i
, lzo_blocks
[0]->sync_bmp
, lzo_blocks
[0]->sync_bmp_nbits
) {
343 ret
= regcache_read(map
, i
, &val
);
346 map
->cache_bypass
= 1;
347 ret
= _regmap_write(map
, i
, val
);
348 map
->cache_bypass
= 0;
351 dev_dbg(map
->dev
, "Synced register %#x, value %#x\n",
358 struct regcache_ops regcache_lzo_ops
= {
359 .type
= REGCACHE_COMPRESSED
,
361 .init
= regcache_lzo_init
,
362 .exit
= regcache_lzo_exit
,
363 .read
= regcache_lzo_read
,
364 .write
= regcache_lzo_write
,
365 .sync
= regcache_lzo_sync