regcache-lzo.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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/device.h>
  13. #include <linux/lzo.h>
  14. #include <linux/slab.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->reg_stride) * 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 / map->reg_stride) %
  102. (DIV_ROUND_UP(map->cache_size_raw,
  103. regcache_lzo_block_count(map)) /
  104. map->cache_word_size);
  105. }
  106. static inline int regcache_lzo_get_blksize(struct regmap *map)
  107. {
  108. return DIV_ROUND_UP(map->cache_size_raw,
  109. regcache_lzo_block_count(map));
  110. }
  111. static int regcache_lzo_init(struct regmap *map)
  112. {
  113. struct regcache_lzo_ctx **lzo_blocks;
  114. size_t bmp_size;
  115. int ret, i, blksize, blkcount;
  116. const char *p, *end;
  117. unsigned long *sync_bmp;
  118. ret = 0;
  119. blkcount = regcache_lzo_block_count(map);
  120. map->cache = kcalloc(blkcount, sizeof(*lzo_blocks),
  121. GFP_KERNEL);
  122. if (!map->cache)
  123. return -ENOMEM;
  124. lzo_blocks = map->cache;
  125. /*
  126. * allocate a bitmap to be used when syncing the cache with
  127. * the hardware. Each time a register is modified, the corresponding
  128. * bit is set in the bitmap, so we know that we have to sync
  129. * that register.
  130. */
  131. bmp_size = map->num_reg_defaults_raw;
  132. sync_bmp = kmalloc_array(BITS_TO_LONGS(bmp_size), sizeof(long),
  133. GFP_KERNEL);
  134. if (!sync_bmp) {
  135. ret = -ENOMEM;
  136. goto err;
  137. }
  138. bitmap_zero(sync_bmp, bmp_size);
  139. /* allocate the lzo blocks and initialize them */
  140. for (i = 0; i < blkcount; i++) {
  141. lzo_blocks[i] = kzalloc(sizeof **lzo_blocks,
  142. GFP_KERNEL);
  143. if (!lzo_blocks[i]) {
  144. kfree(sync_bmp);
  145. ret = -ENOMEM;
  146. goto err;
  147. }
  148. lzo_blocks[i]->sync_bmp = sync_bmp;
  149. lzo_blocks[i]->sync_bmp_nbits = bmp_size;
  150. /* alloc the working space for the compressed block */
  151. ret = regcache_lzo_prepare(lzo_blocks[i]);
  152. if (ret < 0)
  153. goto err;
  154. }
  155. blksize = regcache_lzo_get_blksize(map);
  156. p = map->reg_defaults_raw;
  157. end = map->reg_defaults_raw + map->cache_size_raw;
  158. /* compress the register map and fill the lzo blocks */
  159. for (i = 0; i < blkcount; i++, p += blksize) {
  160. lzo_blocks[i]->src = p;
  161. if (p + blksize > end)
  162. lzo_blocks[i]->src_len = end - p;
  163. else
  164. lzo_blocks[i]->src_len = blksize;
  165. ret = regcache_lzo_compress_cache_block(map,
  166. lzo_blocks[i]);
  167. if (ret < 0)
  168. goto err;
  169. lzo_blocks[i]->decompressed_size =
  170. lzo_blocks[i]->src_len;
  171. }
  172. return 0;
  173. err:
  174. regcache_lzo_exit(map);
  175. return ret;
  176. }
  177. static int regcache_lzo_exit(struct regmap *map)
  178. {
  179. struct regcache_lzo_ctx **lzo_blocks;
  180. int i, blkcount;
  181. lzo_blocks = map->cache;
  182. if (!lzo_blocks)
  183. return 0;
  184. blkcount = regcache_lzo_block_count(map);
  185. /*
  186. * the pointer to the bitmap used for syncing the cache
  187. * is shared amongst all lzo_blocks. Ensure it is freed
  188. * only once.
  189. */
  190. if (lzo_blocks[0])
  191. kfree(lzo_blocks[0]->sync_bmp);
  192. for (i = 0; i < blkcount; i++) {
  193. if (lzo_blocks[i]) {
  194. kfree(lzo_blocks[i]->wmem);
  195. kfree(lzo_blocks[i]->dst);
  196. }
  197. /* each lzo_block is a pointer returned by kmalloc or NULL */
  198. kfree(lzo_blocks[i]);
  199. }
  200. kfree(lzo_blocks);
  201. map->cache = NULL;
  202. return 0;
  203. }
  204. static int regcache_lzo_read(struct regmap *map,
  205. unsigned int reg, unsigned int *value)
  206. {
  207. struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
  208. int ret, blkindex, blkpos;
  209. size_t tmp_dst_len;
  210. void *tmp_dst;
  211. /* index of the compressed lzo block */
  212. blkindex = regcache_lzo_get_blkindex(map, reg);
  213. /* register index within the decompressed block */
  214. blkpos = regcache_lzo_get_blkpos(map, reg);
  215. lzo_blocks = map->cache;
  216. lzo_block = lzo_blocks[blkindex];
  217. /* save the pointer and length of the compressed block */
  218. tmp_dst = lzo_block->dst;
  219. tmp_dst_len = lzo_block->dst_len;
  220. /* prepare the source to be the compressed block */
  221. lzo_block->src = lzo_block->dst;
  222. lzo_block->src_len = lzo_block->dst_len;
  223. /* decompress the block */
  224. ret = regcache_lzo_decompress_cache_block(map, lzo_block);
  225. if (ret >= 0)
  226. /* fetch the value from the cache */
  227. *value = regcache_get_val(map, lzo_block->dst, blkpos);
  228. kfree(lzo_block->dst);
  229. /* restore the pointer and length of the compressed block */
  230. lzo_block->dst = tmp_dst;
  231. lzo_block->dst_len = tmp_dst_len;
  232. return ret;
  233. }
  234. static int regcache_lzo_write(struct regmap *map,
  235. unsigned int reg, unsigned int value)
  236. {
  237. struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
  238. int ret, blkindex, blkpos;
  239. size_t tmp_dst_len;
  240. void *tmp_dst;
  241. /* index of the compressed lzo block */
  242. blkindex = regcache_lzo_get_blkindex(map, reg);
  243. /* register index within the decompressed block */
  244. blkpos = regcache_lzo_get_blkpos(map, reg);
  245. lzo_blocks = map->cache;
  246. lzo_block = lzo_blocks[blkindex];
  247. /* save the pointer and length of the compressed block */
  248. tmp_dst = lzo_block->dst;
  249. tmp_dst_len = lzo_block->dst_len;
  250. /* prepare the source to be the compressed block */
  251. lzo_block->src = lzo_block->dst;
  252. lzo_block->src_len = lzo_block->dst_len;
  253. /* decompress the block */
  254. ret = regcache_lzo_decompress_cache_block(map, lzo_block);
  255. if (ret < 0) {
  256. kfree(lzo_block->dst);
  257. goto out;
  258. }
  259. /* write the new value to the cache */
  260. if (regcache_set_val(map, lzo_block->dst, blkpos, value)) {
  261. kfree(lzo_block->dst);
  262. goto out;
  263. }
  264. /* prepare the source to be the decompressed block */
  265. lzo_block->src = lzo_block->dst;
  266. lzo_block->src_len = lzo_block->dst_len;
  267. /* compress the block */
  268. ret = regcache_lzo_compress_cache_block(map, lzo_block);
  269. if (ret < 0) {
  270. kfree(lzo_block->dst);
  271. kfree(lzo_block->src);
  272. goto out;
  273. }
  274. /* set the bit so we know we have to sync this register */
  275. set_bit(reg / map->reg_stride, lzo_block->sync_bmp);
  276. kfree(tmp_dst);
  277. kfree(lzo_block->src);
  278. return 0;
  279. out:
  280. lzo_block->dst = tmp_dst;
  281. lzo_block->dst_len = tmp_dst_len;
  282. return ret;
  283. }
  284. static int regcache_lzo_sync(struct regmap *map, unsigned int min,
  285. unsigned int max)
  286. {
  287. struct regcache_lzo_ctx **lzo_blocks;
  288. unsigned int val;
  289. int i;
  290. int ret;
  291. lzo_blocks = map->cache;
  292. i = min;
  293. for_each_set_bit_from(i, lzo_blocks[0]->sync_bmp,
  294. lzo_blocks[0]->sync_bmp_nbits) {
  295. if (i > max)
  296. continue;
  297. ret = regcache_read(map, i, &val);
  298. if (ret)
  299. return ret;
  300. /* Is this the hardware default? If so skip. */
  301. ret = regcache_lookup_reg(map, i);
  302. if (ret > 0 && val == map->reg_defaults[ret].def)
  303. continue;
  304. map->cache_bypass = true;
  305. ret = _regmap_write(map, i, val);
  306. map->cache_bypass = false;
  307. if (ret)
  308. return ret;
  309. dev_dbg(map->dev, "Synced register %#x, value %#x\n",
  310. i, val);
  311. }
  312. return 0;
  313. }
  314. struct regcache_ops regcache_lzo_ops = {
  315. .type = REGCACHE_COMPRESSED,
  316. .name = "lzo",
  317. .init = regcache_lzo_init,
  318. .exit = regcache_lzo_exit,
  319. .read = regcache_lzo_read,
  320. .write = regcache_lzo_write,
  321. .sync = regcache_lzo_sync
  322. };