regcache-lzo.c 8.7 KB

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