regcache-lzo.c 9.0 KB

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