regcache-lzo.c 8.7 KB

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