regcache-lzo.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register cache access API - LZO caching support
  4. //
  5. // Copyright 2011 Wolfson Microelectronics plc
  6. //
  7. // Author: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
  8. #include <linux/device.h>
  9. #include <linux/lzo.h>
  10. #include <linux/slab.h>
  11. #include "internal.h"
  12. static int regcache_lzo_exit(struct regmap *map);
  13. struct regcache_lzo_ctx {
  14. void *wmem;
  15. void *dst;
  16. const void *src;
  17. size_t src_len;
  18. size_t dst_len;
  19. size_t decompressed_size;
  20. unsigned long *sync_bmp;
  21. int sync_bmp_nbits;
  22. };
  23. #define LZO_BLOCK_NUM 8
  24. static int regcache_lzo_block_count(struct regmap *map)
  25. {
  26. return LZO_BLOCK_NUM;
  27. }
  28. static int regcache_lzo_prepare(struct regcache_lzo_ctx *lzo_ctx)
  29. {
  30. lzo_ctx->wmem = kmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
  31. if (!lzo_ctx->wmem)
  32. return -ENOMEM;
  33. return 0;
  34. }
  35. static int regcache_lzo_compress(struct regcache_lzo_ctx *lzo_ctx)
  36. {
  37. size_t compress_size;
  38. int ret;
  39. ret = lzo1x_1_compress(lzo_ctx->src, lzo_ctx->src_len,
  40. lzo_ctx->dst, &compress_size, lzo_ctx->wmem);
  41. if (ret != LZO_E_OK || compress_size > lzo_ctx->dst_len)
  42. return -EINVAL;
  43. lzo_ctx->dst_len = compress_size;
  44. return 0;
  45. }
  46. static int regcache_lzo_decompress(struct regcache_lzo_ctx *lzo_ctx)
  47. {
  48. size_t dst_len;
  49. int ret;
  50. dst_len = lzo_ctx->dst_len;
  51. ret = lzo1x_decompress_safe(lzo_ctx->src, lzo_ctx->src_len,
  52. lzo_ctx->dst, &dst_len);
  53. if (ret != LZO_E_OK || dst_len != lzo_ctx->dst_len)
  54. return -EINVAL;
  55. return 0;
  56. }
  57. static int regcache_lzo_compress_cache_block(struct regmap *map,
  58. struct regcache_lzo_ctx *lzo_ctx)
  59. {
  60. int ret;
  61. lzo_ctx->dst_len = lzo1x_worst_compress(PAGE_SIZE);
  62. lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
  63. if (!lzo_ctx->dst) {
  64. lzo_ctx->dst_len = 0;
  65. return -ENOMEM;
  66. }
  67. ret = regcache_lzo_compress(lzo_ctx);
  68. if (ret < 0)
  69. return ret;
  70. return 0;
  71. }
  72. static int regcache_lzo_decompress_cache_block(struct regmap *map,
  73. struct regcache_lzo_ctx *lzo_ctx)
  74. {
  75. int ret;
  76. lzo_ctx->dst_len = lzo_ctx->decompressed_size;
  77. lzo_ctx->dst = kmalloc(lzo_ctx->dst_len, GFP_KERNEL);
  78. if (!lzo_ctx->dst) {
  79. lzo_ctx->dst_len = 0;
  80. return -ENOMEM;
  81. }
  82. ret = regcache_lzo_decompress(lzo_ctx);
  83. if (ret < 0)
  84. return ret;
  85. return 0;
  86. }
  87. static inline int regcache_lzo_get_blkindex(struct regmap *map,
  88. unsigned int reg)
  89. {
  90. return ((reg / map->reg_stride) * map->cache_word_size) /
  91. DIV_ROUND_UP(map->cache_size_raw,
  92. regcache_lzo_block_count(map));
  93. }
  94. static inline int regcache_lzo_get_blkpos(struct regmap *map,
  95. unsigned int reg)
  96. {
  97. return (reg / map->reg_stride) %
  98. (DIV_ROUND_UP(map->cache_size_raw,
  99. regcache_lzo_block_count(map)) /
  100. map->cache_word_size);
  101. }
  102. static inline int regcache_lzo_get_blksize(struct regmap *map)
  103. {
  104. return DIV_ROUND_UP(map->cache_size_raw,
  105. regcache_lzo_block_count(map));
  106. }
  107. static int regcache_lzo_init(struct regmap *map)
  108. {
  109. struct regcache_lzo_ctx **lzo_blocks;
  110. size_t bmp_size;
  111. int ret, i, blksize, blkcount;
  112. const char *p, *end;
  113. unsigned long *sync_bmp;
  114. ret = 0;
  115. blkcount = regcache_lzo_block_count(map);
  116. map->cache = kcalloc(blkcount, sizeof(*lzo_blocks),
  117. GFP_KERNEL);
  118. if (!map->cache)
  119. return -ENOMEM;
  120. lzo_blocks = map->cache;
  121. /*
  122. * allocate a bitmap to be used when syncing the cache with
  123. * the hardware. Each time a register is modified, the corresponding
  124. * bit is set in the bitmap, so we know that we have to sync
  125. * that register.
  126. */
  127. bmp_size = map->num_reg_defaults_raw;
  128. sync_bmp = bitmap_zalloc(bmp_size, GFP_KERNEL);
  129. if (!sync_bmp) {
  130. ret = -ENOMEM;
  131. goto err;
  132. }
  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. bitmap_free(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_lzo_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(map);
  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. bitmap_free(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 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. lzo_blocks = map->cache;
  210. lzo_block = lzo_blocks[blkindex];
  211. /* save the pointer and length of the compressed block */
  212. tmp_dst = lzo_block->dst;
  213. tmp_dst_len = lzo_block->dst_len;
  214. /* prepare the source to be the compressed block */
  215. lzo_block->src = lzo_block->dst;
  216. lzo_block->src_len = lzo_block->dst_len;
  217. /* decompress the block */
  218. ret = regcache_lzo_decompress_cache_block(map, lzo_block);
  219. if (ret >= 0)
  220. /* fetch the value from the cache */
  221. *value = regcache_get_val(map, lzo_block->dst, blkpos);
  222. kfree(lzo_block->dst);
  223. /* restore the pointer and length of the compressed block */
  224. lzo_block->dst = tmp_dst;
  225. lzo_block->dst_len = tmp_dst_len;
  226. return ret;
  227. }
  228. static int regcache_lzo_write(struct regmap *map,
  229. unsigned int reg, unsigned int value)
  230. {
  231. struct regcache_lzo_ctx *lzo_block, **lzo_blocks;
  232. int ret, blkindex, blkpos;
  233. size_t tmp_dst_len;
  234. void *tmp_dst;
  235. /* index of the compressed lzo block */
  236. blkindex = regcache_lzo_get_blkindex(map, reg);
  237. /* register index within the decompressed block */
  238. blkpos = regcache_lzo_get_blkpos(map, reg);
  239. lzo_blocks = map->cache;
  240. lzo_block = lzo_blocks[blkindex];
  241. /* save the pointer and length of the compressed block */
  242. tmp_dst = lzo_block->dst;
  243. tmp_dst_len = lzo_block->dst_len;
  244. /* prepare the source to be the compressed block */
  245. lzo_block->src = lzo_block->dst;
  246. lzo_block->src_len = lzo_block->dst_len;
  247. /* decompress the block */
  248. ret = regcache_lzo_decompress_cache_block(map, lzo_block);
  249. if (ret < 0) {
  250. kfree(lzo_block->dst);
  251. goto out;
  252. }
  253. /* write the new value to the cache */
  254. if (regcache_set_val(map, lzo_block->dst, blkpos, value)) {
  255. kfree(lzo_block->dst);
  256. goto out;
  257. }
  258. /* prepare the source to be the decompressed block */
  259. lzo_block->src = lzo_block->dst;
  260. lzo_block->src_len = lzo_block->dst_len;
  261. /* compress the block */
  262. ret = regcache_lzo_compress_cache_block(map, lzo_block);
  263. if (ret < 0) {
  264. kfree(lzo_block->dst);
  265. kfree(lzo_block->src);
  266. goto out;
  267. }
  268. /* set the bit so we know we have to sync this register */
  269. set_bit(reg / map->reg_stride, lzo_block->sync_bmp);
  270. kfree(tmp_dst);
  271. kfree(lzo_block->src);
  272. return 0;
  273. out:
  274. lzo_block->dst = tmp_dst;
  275. lzo_block->dst_len = tmp_dst_len;
  276. return ret;
  277. }
  278. static int regcache_lzo_sync(struct regmap *map, unsigned int min,
  279. unsigned int max)
  280. {
  281. struct regcache_lzo_ctx **lzo_blocks;
  282. unsigned int val;
  283. int i;
  284. int ret;
  285. lzo_blocks = map->cache;
  286. i = min;
  287. for_each_set_bit_from(i, lzo_blocks[0]->sync_bmp,
  288. lzo_blocks[0]->sync_bmp_nbits) {
  289. if (i > max)
  290. continue;
  291. ret = regcache_read(map, i, &val);
  292. if (ret)
  293. return ret;
  294. /* Is this the hardware default? If so skip. */
  295. ret = regcache_lookup_reg(map, i);
  296. if (ret > 0 && val == map->reg_defaults[ret].def)
  297. continue;
  298. map->cache_bypass = true;
  299. ret = _regmap_write(map, i, val);
  300. map->cache_bypass = false;
  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_COMPRESSED,
  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. };