regcache-maple.c 7.8 KB

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