regcache-maple.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Register cache access API - maple tree based cache
  4. //
  5. // Copyright 2023 Arm, Ltd
  6. //
  7. // Author: Mark Brown <broonie@kernel.org>
  8. #include <linux/debugfs.h>
  9. #include <linux/device.h>
  10. #include <linux/maple_tree.h>
  11. #include <linux/slab.h>
  12. #include "internal.h"
  13. static int regcache_maple_read(struct regmap *map,
  14. unsigned int reg, unsigned int *value)
  15. {
  16. struct maple_tree *mt = map->cache;
  17. MA_STATE(mas, mt, reg, reg);
  18. unsigned long *entry;
  19. rcu_read_lock();
  20. entry = mas_walk(&mas);
  21. if (!entry) {
  22. rcu_read_unlock();
  23. return -ENOENT;
  24. }
  25. *value = entry[reg - mas.index];
  26. rcu_read_unlock();
  27. return 0;
  28. }
  29. static int regcache_maple_write(struct regmap *map, unsigned int reg,
  30. unsigned int val)
  31. {
  32. struct maple_tree *mt = map->cache;
  33. MA_STATE(mas, mt, reg, reg);
  34. unsigned long *entry, *upper, *lower;
  35. unsigned long index, last;
  36. size_t lower_sz, upper_sz;
  37. int ret;
  38. rcu_read_lock();
  39. entry = mas_walk(&mas);
  40. if (entry) {
  41. entry[reg - mas.index] = val;
  42. rcu_read_unlock();
  43. return 0;
  44. }
  45. /* Any adjacent entries to extend/merge? */
  46. mas_set_range(&mas, reg - 1, reg + 1);
  47. index = reg;
  48. last = reg;
  49. lower = mas_find(&mas, reg - 1);
  50. if (lower) {
  51. index = mas.index;
  52. lower_sz = (mas.last - mas.index + 1) * sizeof(unsigned long);
  53. }
  54. upper = mas_find(&mas, reg + 1);
  55. if (upper) {
  56. last = mas.last;
  57. upper_sz = (mas.last - mas.index + 1) * sizeof(unsigned long);
  58. }
  59. rcu_read_unlock();
  60. entry = kmalloc((last - index + 1) * sizeof(unsigned long),
  61. map->alloc_flags);
  62. if (!entry)
  63. return -ENOMEM;
  64. if (lower)
  65. memcpy(entry, lower, lower_sz);
  66. entry[reg - index] = val;
  67. if (upper)
  68. memcpy(&entry[reg - index + 1], upper, upper_sz);
  69. /*
  70. * This is safe because the regmap lock means the Maple lock
  71. * is redundant, but we need to take it due to lockdep asserts
  72. * in the maple tree code.
  73. */
  74. mas_lock(&mas);
  75. mas_set_range(&mas, index, last);
  76. ret = mas_store_gfp(&mas, entry, map->alloc_flags);
  77. mas_unlock(&mas);
  78. if (ret == 0) {
  79. kfree(lower);
  80. kfree(upper);
  81. }
  82. return ret;
  83. }
  84. static int regcache_maple_drop(struct regmap *map, unsigned int min,
  85. unsigned int max)
  86. {
  87. struct maple_tree *mt = map->cache;
  88. MA_STATE(mas, mt, min, max);
  89. unsigned long *entry, *lower, *upper;
  90. /* initialized to work around false-positive -Wuninitialized warning */
  91. unsigned long lower_index = 0, lower_last = 0;
  92. unsigned long upper_index, upper_last;
  93. int ret = 0;
  94. lower = NULL;
  95. upper = NULL;
  96. mas_lock(&mas);
  97. mas_for_each(&mas, entry, max) {
  98. /*
  99. * This is safe because the regmap lock means the
  100. * Maple lock is redundant, but we need to take it due
  101. * to lockdep asserts in the maple tree code.
  102. */
  103. mas_unlock(&mas);
  104. /* Do we need to save any of this entry? */
  105. if (mas.index < min) {
  106. lower_index = mas.index;
  107. lower_last = min -1;
  108. lower = kmemdup_array(entry,
  109. min - mas.index, sizeof(*lower),
  110. map->alloc_flags);
  111. if (!lower) {
  112. ret = -ENOMEM;
  113. goto out_unlocked;
  114. }
  115. }
  116. if (mas.last > max) {
  117. upper_index = max + 1;
  118. upper_last = mas.last;
  119. upper = kmemdup_array(&entry[max - mas.index + 1],
  120. mas.last - max, sizeof(*upper),
  121. map->alloc_flags);
  122. if (!upper) {
  123. ret = -ENOMEM;
  124. goto out_unlocked;
  125. }
  126. }
  127. kfree(entry);
  128. mas_lock(&mas);
  129. mas_erase(&mas);
  130. /* Insert new nodes with the saved data */
  131. if (lower) {
  132. mas_set_range(&mas, lower_index, lower_last);
  133. ret = mas_store_gfp(&mas, lower, map->alloc_flags);
  134. if (ret != 0)
  135. goto out;
  136. lower = NULL;
  137. }
  138. if (upper) {
  139. mas_set_range(&mas, upper_index, upper_last);
  140. ret = mas_store_gfp(&mas, upper, map->alloc_flags);
  141. if (ret != 0)
  142. goto out;
  143. upper = NULL;
  144. }
  145. }
  146. out:
  147. mas_unlock(&mas);
  148. out_unlocked:
  149. kfree(lower);
  150. kfree(upper);
  151. return ret;
  152. }
  153. static int regcache_maple_sync_block(struct regmap *map, unsigned long *entry,
  154. struct ma_state *mas,
  155. unsigned int min, unsigned int max)
  156. {
  157. void *buf;
  158. unsigned long r;
  159. size_t val_bytes = map->format.val_bytes;
  160. int ret = 0;
  161. mas_pause(mas);
  162. rcu_read_unlock();
  163. /*
  164. * Use a raw write if writing more than one register to a
  165. * device that supports raw writes to reduce transaction
  166. * overheads.
  167. */
  168. if (max - min > 1 && regmap_can_raw_write(map)) {
  169. buf = kmalloc(val_bytes * (max - min), map->alloc_flags);
  170. if (!buf) {
  171. ret = -ENOMEM;
  172. goto out;
  173. }
  174. /* Render the data for a raw write */
  175. for (r = min; r < max; r++) {
  176. regcache_set_val(map, buf, r - min,
  177. entry[r - mas->index]);
  178. }
  179. ret = _regmap_raw_write(map, min, buf, (max - min) * val_bytes,
  180. false);
  181. kfree(buf);
  182. } else {
  183. for (r = min; r < max; r++) {
  184. ret = _regmap_write(map, r,
  185. entry[r - mas->index]);
  186. if (ret != 0)
  187. goto out;
  188. }
  189. }
  190. out:
  191. rcu_read_lock();
  192. return ret;
  193. }
  194. static int regcache_maple_sync(struct regmap *map, unsigned int min,
  195. unsigned int max)
  196. {
  197. struct maple_tree *mt = map->cache;
  198. unsigned long *entry;
  199. MA_STATE(mas, mt, min, max);
  200. unsigned long lmin = min;
  201. unsigned long lmax = max;
  202. unsigned int r, v, sync_start;
  203. int ret = 0;
  204. bool sync_needed = false;
  205. map->cache_bypass = true;
  206. rcu_read_lock();
  207. mas_for_each(&mas, entry, max) {
  208. for (r = max(mas.index, lmin); r <= min(mas.last, lmax); r++) {
  209. v = entry[r - mas.index];
  210. if (regcache_reg_needs_sync(map, r, v)) {
  211. if (!sync_needed) {
  212. sync_start = r;
  213. sync_needed = true;
  214. }
  215. continue;
  216. }
  217. if (!sync_needed)
  218. continue;
  219. ret = regcache_maple_sync_block(map, entry, &mas,
  220. sync_start, r);
  221. if (ret != 0)
  222. goto out;
  223. sync_needed = false;
  224. }
  225. if (sync_needed) {
  226. ret = regcache_maple_sync_block(map, entry, &mas,
  227. sync_start, r);
  228. if (ret != 0)
  229. goto out;
  230. sync_needed = false;
  231. }
  232. }
  233. out:
  234. rcu_read_unlock();
  235. map->cache_bypass = false;
  236. return ret;
  237. }
  238. static int regcache_maple_exit(struct regmap *map)
  239. {
  240. struct maple_tree *mt = map->cache;
  241. MA_STATE(mas, mt, 0, UINT_MAX);
  242. unsigned int *entry;
  243. /* if we've already been called then just return */
  244. if (!mt)
  245. return 0;
  246. mas_lock(&mas);
  247. mas_for_each(&mas, entry, UINT_MAX)
  248. kfree(entry);
  249. __mt_destroy(mt);
  250. mas_unlock(&mas);
  251. kfree(mt);
  252. map->cache = NULL;
  253. return 0;
  254. }
  255. static int regcache_maple_insert_block(struct regmap *map, int first,
  256. int last)
  257. {
  258. struct maple_tree *mt = map->cache;
  259. MA_STATE(mas, mt, first, last);
  260. unsigned long *entry;
  261. int i, ret;
  262. entry = kcalloc(last - first + 1, sizeof(unsigned long), map->alloc_flags);
  263. if (!entry)
  264. return -ENOMEM;
  265. for (i = 0; i < last - first + 1; i++)
  266. entry[i] = map->reg_defaults[first + i].def;
  267. mas_lock(&mas);
  268. mas_set_range(&mas, map->reg_defaults[first].reg,
  269. map->reg_defaults[last].reg);
  270. ret = mas_store_gfp(&mas, entry, map->alloc_flags);
  271. mas_unlock(&mas);
  272. if (ret)
  273. kfree(entry);
  274. return ret;
  275. }
  276. static int regcache_maple_init(struct regmap *map)
  277. {
  278. struct maple_tree *mt;
  279. int i;
  280. int ret;
  281. int range_start;
  282. mt = kmalloc(sizeof(*mt), map->alloc_flags);
  283. if (!mt)
  284. return -ENOMEM;
  285. map->cache = mt;
  286. mt_init(mt);
  287. if (!mt_external_lock(mt) && map->lock_key)
  288. lockdep_set_class_and_subclass(&mt->ma_lock, map->lock_key, 1);
  289. if (!map->num_reg_defaults)
  290. return 0;
  291. range_start = 0;
  292. /* Scan for ranges of contiguous registers */
  293. for (i = 1; i < map->num_reg_defaults; i++) {
  294. if (map->reg_defaults[i].reg !=
  295. map->reg_defaults[i - 1].reg + 1) {
  296. ret = regcache_maple_insert_block(map, range_start,
  297. i - 1);
  298. if (ret != 0)
  299. goto err;
  300. range_start = i;
  301. }
  302. }
  303. /* Add the last block */
  304. ret = regcache_maple_insert_block(map, range_start,
  305. map->num_reg_defaults - 1);
  306. if (ret != 0)
  307. goto err;
  308. return 0;
  309. err:
  310. regcache_maple_exit(map);
  311. return ret;
  312. }
  313. struct regcache_ops regcache_maple_ops = {
  314. .type = REGCACHE_MAPLE,
  315. .name = "maple",
  316. .init = regcache_maple_init,
  317. .exit = regcache_maple_exit,
  318. .read = regcache_maple_read,
  319. .write = regcache_maple_write,
  320. .drop = regcache_maple_drop,
  321. .sync = regcache_maple_sync,
  322. };