regcache-lzo.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 blksize, 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. /* size of the compressed block */
  216. blksize = regcache_lzo_get_blksize(map);
  217. lzo_blocks = map->cache;
  218. lzo_block = lzo_blocks[blkindex];
  219. /* save the pointer and length of the compressed block */
  220. tmp_dst = lzo_block->dst;
  221. tmp_dst_len = lzo_block->dst_len;
  222. /* prepare the source to be the compressed block */
  223. lzo_block->src = lzo_block->dst;
  224. lzo_block->src_len = lzo_block->dst_len;
  225. /* decompress the block */
  226. ret = regcache_lzo_decompress_cache_block(map, lzo_block);
  227. if (ret >= 0)
  228. /* fetch the value from the cache */
  229. *value = regcache_get_val(map, lzo_block->dst, blkpos);
  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(map, lzo_block->dst, blkpos, value)) {
  265. kfree(lzo_block->dst);
  266. goto out;
  267. }
  268. /* prepare the source to be the decompressed block */
  269. lzo_block->src = lzo_block->dst;
  270. lzo_block->src_len = lzo_block->dst_len;
  271. /* compress the block */
  272. ret = regcache_lzo_compress_cache_block(map, lzo_block);
  273. if (ret < 0) {
  274. kfree(lzo_block->dst);
  275. kfree(lzo_block->src);
  276. goto out;
  277. }
  278. /* set the bit so we know we have to sync this register */
  279. set_bit(reg / map->reg_stride, lzo_block->sync_bmp);
  280. kfree(tmp_dst);
  281. kfree(lzo_block->src);
  282. return 0;
  283. out:
  284. lzo_block->dst = tmp_dst;
  285. lzo_block->dst_len = tmp_dst_len;
  286. return ret;
  287. }
  288. static int regcache_lzo_sync(struct regmap *map, unsigned int min,
  289. unsigned int max)
  290. {
  291. struct regcache_lzo_ctx **lzo_blocks;
  292. unsigned int val;
  293. int i;
  294. int ret;
  295. lzo_blocks = map->cache;
  296. i = min;
  297. for_each_set_bit_from(i, lzo_blocks[0]->sync_bmp,
  298. lzo_blocks[0]->sync_bmp_nbits) {
  299. if (i > max)
  300. continue;
  301. ret = regcache_read(map, i, &val);
  302. if (ret)
  303. return ret;
  304. /* Is this the hardware default? If so skip. */
  305. ret = regcache_lookup_reg(map, i);
  306. if (ret > 0 && val == map->reg_defaults[ret].def)
  307. continue;
  308. map->cache_bypass = true;
  309. ret = _regmap_write(map, i, val);
  310. map->cache_bypass = false;
  311. if (ret)
  312. return ret;
  313. dev_dbg(map->dev, "Synced register %#x, value %#x\n",
  314. i, val);
  315. }
  316. return 0;
  317. }
  318. struct regcache_ops regcache_lzo_ops = {
  319. .type = REGCACHE_COMPRESSED,
  320. .name = "lzo",
  321. .init = regcache_lzo_init,
  322. .exit = regcache_lzo_exit,
  323. .read = regcache_lzo_read,
  324. .write = regcache_lzo_write,
  325. .sync = regcache_lzo_sync
  326. };