source: GPL/branches/uniaud32-next/lib32/regcache-lzo.c@ 615

Last change on this file since 615 was 615, checked in by Paul Smedley, 5 years ago

Add source for uniaud32 based on code from linux kernel 5.4.86

File size: 9.0 KB
Line 
1/*
2 * Register cache access API - LZO caching support
3 *
4 * Copyright 2011 Wolfson Microelectronics plc
5 *
6 * Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
7 *
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.
11 */
12/* from 4.14.202 */
13
14#include <linux/device.h>
15#include <linux/lzo.h>
16#include <linux/slab.h>
17#include <linux/module.h>
18#include <linux/workqueue.h>
19#include <linux/byteorder/little_endian.h>
20#include <linux/printk.h>
21
22#include "internal.h"
23
24static int regcache_lzo_exit(struct regmap *map);
25
26struct regcache_lzo_ctx {
27 void *wmem;
28 void *dst;
29 const void *src;
30 size_t src_len;
31 size_t dst_len;
32 size_t decompressed_size;
33 unsigned long *sync_bmp;
34 int sync_bmp_nbits;
35};
36
37#define LZO_BLOCK_NUM 8
38static int regcache_lzo_block_count(struct regmap *map)
39{
40 return LZO_BLOCK_NUM;
41}
42
43static int regcache_lzo_prepare(struct regcache_lzo_ctx *lzo_ctx)
44{
45 lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
46 if (!lzo_ctx->wmem)
47 return -ENOMEM;
48 return 0;
49}
50
51static int regcache_lzo_compress(struct regcache_lzo_ctx *lzo_ctx)
52{
53 size_t compress_size;
54 int ret;
55
56 ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
57 lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
58 if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
59 return -EINVAL;
60 lzo_ctx->dst_len = compress_size;
61 return 0;
62}
63
64static int regcache_lzo_decompress(struct regcache_lzo_ctx *lzo_ctx)
65{
66 size_t dst_len;
67 int ret;
68
69 dst_len = lzo_ctx->dst_len;
70 ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
71 lzo_ctx->dst, &dst_len);
72 if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
73 return -EINVAL;
74 return 0;
75}
76
77static int regcache_lzo_compress_cache_block(struct regmap *map,
78 struct regcache_lzo_ctx *lzo_ctx)
79{
80 int ret;
81
82 lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
83 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
84 if (!lzo_ctx->dst) {
85 lzo_ctx->dst_len = 0;
86 return -ENOMEM;
87 }
88
89 ret = regcache_lzo_compress(lzo_ctx);
90 if (ret < 0)
91 return ret;
92 return 0;
93}
94
95static int regcache_lzo_decompress_cache_block(struct regmap *map,
96 struct regcache_lzo_ctx *lzo_ctx)
97{
98 int ret;
99
100 lzo_ctx->dst_len = lzo_ctx->decompressed_size;
101 lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
102 if (!lzo_ctx->dst) {
103 lzo_ctx->dst_len = 0;
104 return -ENOMEM;
105 }
106
107 ret = regcache_lzo_decompress(lzo_ctx);
108 if (ret < 0)
109 return ret;
110 return 0;
111}
112
113static inline int regcache_lzo_get_blkindex(struct regmap *map,
114 unsigned int reg)
115{
116 return ((reg / map->reg_stride) * map->cache_word_size) /
117 DIV_ROUND_UP(map->cache_size_raw,
118 regcache_lzo_block_count(map));
119}
120
121static inline int regcache_lzo_get_blkpos(struct regmap *map,
122 unsigned int reg)
123{
124 return (reg / map->reg_stride) %
125 (DIV_ROUND_UP(map->cache_size_raw,
126 regcache_lzo_block_count(map)) /
127 map->cache_word_size);
128}
129
130static inline int regcache_lzo_get_blksize(struct regmap *map)
131{
132 return DIV_ROUND_UP(map->cache_size_raw,
133 regcache_lzo_block_count(map));
134}
135
136static int regcache_lzo_init(struct regmap *map)
137{
138 struct regcache_lzo_ctx **lzo_blocks;
139 size_t bmp_size;
140 int ret, i, blksize, blkcount;
141 const char *p, *end;
142 unsigned long *sync_bmp;
143
144 ret = 0;
145
146 blkcount = regcache_lzo_block_count(map);
147 map->cache = kcalloc(blkcount, sizeof(*lzo_blocks),
148 GFP_KERNEL);
149 if (!map->cache)
150 return -ENOMEM;
151 lzo_blocks = map->cache;
152
153 /*
154 * allocate a bitmap to be used when syncing the cache with
155 * the hardware. Each time a register is modified, the corresponding
156 * bit is set in the bitmap, so we know that we have to sync
157 * that register.
158 */
159 bmp_size = map->num_reg_defaults_raw;
160 sync_bmp = kmalloc_array(BITS_TO_LONGS(bmp_size), sizeof(long),
161 GFP_KERNEL);
162 if (!sync_bmp) {
163 ret = -ENOMEM;
164 goto err;
165 }
166 bitmap_zero(sync_bmp, bmp_size);
167
168 /* allocate the lzo blocks and initialize them */
169 for (i = 0; i < blkcount; i++) {
170 lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
171 GFP_KERNEL);
172 if (!lzo_blocks[i]) {
173 kfree(sync_bmp);
174 ret = -ENOMEM;
175 goto err;
176 }
177 lzo_blocks[i]->sync_bmp = sync_bmp;
178 lzo_blocks[i]->sync_bmp_nbits = bmp_size;
179 /* alloc the working space for the compressed block */
180 ret = regcache_lzo_prepare(lzo_blocks[i]);
181 if (ret < 0)
182 goto err;
183 }
184
185 blksize = regcache_lzo_get_blksize(map);
186 p = map->reg_defaults_raw;
187 end = map->reg_defaults_raw + map->cache_size_raw;
188 /* compress the register map and fill the lzo blocks */
189 for (i = 0; i < blkcount; i++, p += blksize) {
190 lzo_blocks[i]->src = p;
191 if (p + blksize > end)
192 lzo_blocks[i]->src_len = end - p;
193 else
194 lzo_blocks[i]->src_len = blksize;
195 ret = regcache_lzo_compress_cache_block(map,
196 lzo_blocks[i]);
197 if (ret < 0)
198 goto err;
199 lzo_blocks[i]->decompressed_size =
200 lzo_blocks[i]->src_len;
201 }
202
203 return 0;
204err:
205 regcache_lzo_exit(map);
206 return ret;
207}
208
209static int regcache_lzo_exit(struct regmap *map)
210{
211 struct regcache_lzo_ctx **lzo_blocks;
212 int i, blkcount;
213
214 lzo_blocks = map->cache;
215 if (!lzo_blocks)
216 return 0;
217
218 blkcount = regcache_lzo_block_count(map);
219 /*
220 * the pointer to the bitmap used for syncing the cache
221 * is shared amongst all lzo_blocks. Ensure it is freed
222 * only once.
223 */
224 if (lzo_blocks[0])
225 kfree(lzo_blocks[0]->sync_bmp);
226 for (i = 0; i < blkcount; i++) {
227 if (lzo_blocks[i]) {
228 kfree(lzo_blocks[i]->wmem);
229 kfree(lzo_blocks[i]->dst);
230 }
231 /* each lzo_block is a pointer returned by kmalloc or NULL */
232 kfree(lzo_blocks[i]);
233 }
234 kfree(lzo_blocks);
235 map->cache = NULL;
236 return 0;
237}
238
239static int regcache_lzo_read(struct regmap *map,
240 unsigned int reg, unsigned int *value)
241{
242 struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
243 int ret, blkindex, blkpos;
244 size_t tmp_dst_len;
245 void *tmp_dst;
246
247 /* index of the compressed lzo block */
248 blkindex = regcache_lzo_get_blkindex(map, reg);
249 /* register index within the decompressed block */
250 blkpos = regcache_lzo_get_blkpos(map, reg);
251 lzo_blocks = map->cache;
252 lzo_block = lzo_blocks[blkindex];
253
254 /* save the pointer and length of the compressed block */
255 tmp_dst = lzo_block->dst;
256 tmp_dst_len = lzo_block->dst_len;
257
258 /* prepare the source to be the compressed block */
259 lzo_block->src = lzo_block->dst;
260 lzo_block->src_len = lzo_block->dst_len;
261
262 /* decompress the block */
263 ret = regcache_lzo_decompress_cache_block(map, lzo_block);
264 if (ret >= 0)
265 /* fetch the value from the cache */
266 *value = regcache_get_val(map, lzo_block->dst, blkpos);
267
268 kfree(lzo_block->dst);
269 /* restore the pointer and length of the compressed block */
270 lzo_block->dst = tmp_dst;
271 lzo_block->dst_len = tmp_dst_len;
272
273 return ret;
274}
275
276static int regcache_lzo_write(struct regmap *map,
277 unsigned int reg, unsigned int value)
278{
279 struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
280 int ret, blkindex, blkpos;
281 size_t tmp_dst_len;
282 void *tmp_dst;
283
284 /* index of the compressed lzo block */
285 blkindex = regcache_lzo_get_blkindex(map, reg);
286 /* register index within the decompressed block */
287 blkpos = regcache_lzo_get_blkpos(map, reg);
288 lzo_blocks = map->cache;
289 lzo_block = lzo_blocks[blkindex];
290
291 /* save the pointer and length of the compressed block */
292 tmp_dst = lzo_block->dst;
293 tmp_dst_len = lzo_block->dst_len;
294
295 /* prepare the source to be the compressed block */
296 lzo_block->src = lzo_block->dst;
297 lzo_block->src_len = lzo_block->dst_len;
298
299 /* decompress the block */
300 ret = regcache_lzo_decompress_cache_block(map, lzo_block);
301 if (ret < 0) {
302 kfree(lzo_block->dst);
303 goto out;
304 }
305
306 /* write the new value to the cache */
307 if (regcache_set_val(map, lzo_block->dst, blkpos, value)) {
308 kfree(lzo_block->dst);
309 goto out;
310 }
311
312 /* prepare the source to be the decompressed block */
313 lzo_block->src = lzo_block->dst;
314 lzo_block->src_len = lzo_block->dst_len;
315
316 /* compress the block */
317 ret = regcache_lzo_compress_cache_block(map, lzo_block);
318 if (ret < 0) {
319 kfree(lzo_block->dst);
320 kfree(lzo_block->src);
321 goto out;
322 }
323
324 /* set the bit so we know we have to sync this register */
325 set_bit(reg / map->reg_stride, lzo_block->sync_bmp);
326 kfree(tmp_dst);
327 kfree(lzo_block->src);
328 return 0;
329out:
330 lzo_block->dst = tmp_dst;
331 lzo_block->dst_len = tmp_dst_len;
332 return ret;
333}
334
335static int regcache_lzo_sync(struct regmap *map, unsigned int min,
336 unsigned int max)
337{
338 struct regcache_lzo_ctx **lzo_blocks;
339 unsigned int val;
340 int i;
341 int ret;
342
343 lzo_blocks = map->cache;
344 i = min;
345 for_each_set_bit_from(i, lzo_blocks[0]->sync_bmp,
346 lzo_blocks[0]->sync_bmp_nbits) {
347 if (i > max)
348 continue;
349
350 ret = regcache_read(map, i, &val);
351 if (ret)
352 return ret;
353
354 /* Is this the hardware default? If so skip. */
355 ret = regcache_lookup_reg(map, i);
356 if (ret > 0 && val == map->reg_defaults[ret].def)
357 continue;
358
359 map->cache_bypass = true;
360 ret = _regmap_write(map, i, val);
361 map->cache_bypass = false;
362 if (ret)
363 return ret;
364 dev_dbg(map->dev, "Synced register %#x, value %#x\n",
365 i, val);
366 }
367
368 return 0;
369}
370
371struct regcache_ops regcache_lzo_ops = {
372 .type = REGCACHE_COMPRESSED,
373 .name = "lzo",
374 .init = regcache_lzo_init,
375 .exit = regcache_lzo_exit,
376 .read = regcache_lzo_read,
377 .write = regcache_lzo_write,
378 .sync = regcache_lzo_sync
379};
Note: See TracBrowser for help on using the repository browser.