drm/rockchip: Don't change hdmi reference clock rate
[drm/drm-misc.git] / drivers / base / regmap / regcache-maple.c
blob23da7b31d7153450e1b16528be47216d7fbdd872
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>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/maple_tree.h>
12 #include <linux/slab.h>
14 #include "internal.h"
16 static int regcache_maple_read(struct regmap *map,
17 unsigned int reg, unsigned int *value)
19 struct maple_tree *mt = map->cache;
20 MA_STATE(mas, mt, reg, reg);
21 unsigned long *entry;
23 rcu_read_lock();
25 entry = mas_walk(&mas);
26 if (!entry) {
27 rcu_read_unlock();
28 return -ENOENT;
31 *value = entry[reg - mas.index];
33 rcu_read_unlock();
35 return 0;
38 static int regcache_maple_write(struct regmap *map, unsigned int reg,
39 unsigned int val)
41 struct maple_tree *mt = map->cache;
42 MA_STATE(mas, mt, reg, reg);
43 unsigned long *entry, *upper, *lower;
44 unsigned long index, last;
45 size_t lower_sz, upper_sz;
46 int ret;
48 rcu_read_lock();
50 entry = mas_walk(&mas);
51 if (entry) {
52 entry[reg - mas.index] = val;
53 rcu_read_unlock();
54 return 0;
57 /* Any adjacent entries to extend/merge? */
58 mas_set_range(&mas, reg - 1, reg + 1);
59 index = reg;
60 last = reg;
62 lower = mas_find(&mas, reg - 1);
63 if (lower) {
64 index = mas.index;
65 lower_sz = (mas.last - mas.index + 1) * sizeof(unsigned long);
68 upper = mas_find(&mas, reg + 1);
69 if (upper) {
70 last = mas.last;
71 upper_sz = (mas.last - mas.index + 1) * sizeof(unsigned long);
74 rcu_read_unlock();
76 entry = kmalloc((last - index + 1) * sizeof(unsigned long),
77 map->alloc_flags);
78 if (!entry)
79 return -ENOMEM;
81 if (lower)
82 memcpy(entry, lower, lower_sz);
83 entry[reg - index] = val;
84 if (upper)
85 memcpy(&entry[reg - index + 1], upper, upper_sz);
88 * This is safe because the regmap lock means the Maple lock
89 * is redundant, but we need to take it due to lockdep asserts
90 * in the maple tree code.
92 mas_lock(&mas);
94 mas_set_range(&mas, index, last);
95 ret = mas_store_gfp(&mas, entry, map->alloc_flags);
97 mas_unlock(&mas);
99 if (ret == 0) {
100 kfree(lower);
101 kfree(upper);
104 return ret;
107 static int regcache_maple_drop(struct regmap *map, unsigned int min,
108 unsigned int max)
110 struct maple_tree *mt = map->cache;
111 MA_STATE(mas, mt, min, max);
112 unsigned long *entry, *lower, *upper;
113 /* initialized to work around false-positive -Wuninitialized warning */
114 unsigned long lower_index = 0, lower_last = 0;
115 unsigned long upper_index, upper_last;
116 int ret = 0;
118 lower = NULL;
119 upper = NULL;
121 mas_lock(&mas);
123 mas_for_each(&mas, entry, max) {
125 * This is safe because the regmap lock means the
126 * Maple lock is redundant, but we need to take it due
127 * to lockdep asserts in the maple tree code.
129 mas_unlock(&mas);
131 /* Do we need to save any of this entry? */
132 if (mas.index < min) {
133 lower_index = mas.index;
134 lower_last = min -1;
136 lower = kmemdup_array(entry,
137 min - mas.index, sizeof(*lower),
138 map->alloc_flags);
139 if (!lower) {
140 ret = -ENOMEM;
141 goto out_unlocked;
145 if (mas.last > max) {
146 upper_index = max + 1;
147 upper_last = mas.last;
149 upper = kmemdup_array(&entry[max - mas.index + 1],
150 mas.last - max, sizeof(*upper),
151 map->alloc_flags);
152 if (!upper) {
153 ret = -ENOMEM;
154 goto out_unlocked;
158 kfree(entry);
159 mas_lock(&mas);
160 mas_erase(&mas);
162 /* Insert new nodes with the saved data */
163 if (lower) {
164 mas_set_range(&mas, lower_index, lower_last);
165 ret = mas_store_gfp(&mas, lower, map->alloc_flags);
166 if (ret != 0)
167 goto out;
168 lower = NULL;
171 if (upper) {
172 mas_set_range(&mas, upper_index, upper_last);
173 ret = mas_store_gfp(&mas, upper, map->alloc_flags);
174 if (ret != 0)
175 goto out;
176 upper = NULL;
180 out:
181 mas_unlock(&mas);
182 out_unlocked:
183 kfree(lower);
184 kfree(upper);
186 return ret;
189 static int regcache_maple_sync_block(struct regmap *map, unsigned long *entry,
190 struct ma_state *mas,
191 unsigned int min, unsigned int max)
193 void *buf;
194 unsigned long r;
195 size_t val_bytes = map->format.val_bytes;
196 int ret = 0;
198 mas_pause(mas);
199 rcu_read_unlock();
202 * Use a raw write if writing more than one register to a
203 * device that supports raw writes to reduce transaction
204 * overheads.
206 if (max - min > 1 && regmap_can_raw_write(map)) {
207 buf = kmalloc(val_bytes * (max - min), map->alloc_flags);
208 if (!buf) {
209 ret = -ENOMEM;
210 goto out;
213 /* Render the data for a raw write */
214 for (r = min; r < max; r++) {
215 regcache_set_val(map, buf, r - min,
216 entry[r - mas->index]);
219 ret = _regmap_raw_write(map, min, buf, (max - min) * val_bytes,
220 false);
222 kfree(buf);
223 } else {
224 for (r = min; r < max; r++) {
225 ret = _regmap_write(map, r,
226 entry[r - mas->index]);
227 if (ret != 0)
228 goto out;
232 out:
233 rcu_read_lock();
235 return ret;
238 static int regcache_maple_sync(struct regmap *map, unsigned int min,
239 unsigned int max)
241 struct maple_tree *mt = map->cache;
242 unsigned long *entry;
243 MA_STATE(mas, mt, min, max);
244 unsigned long lmin = min;
245 unsigned long lmax = max;
246 unsigned int r, v, sync_start;
247 int ret = 0;
248 bool sync_needed = false;
250 map->cache_bypass = true;
252 rcu_read_lock();
254 mas_for_each(&mas, entry, max) {
255 for (r = max(mas.index, lmin); r <= min(mas.last, lmax); r++) {
256 v = entry[r - mas.index];
258 if (regcache_reg_needs_sync(map, r, v)) {
259 if (!sync_needed) {
260 sync_start = r;
261 sync_needed = true;
263 continue;
266 if (!sync_needed)
267 continue;
269 ret = regcache_maple_sync_block(map, entry, &mas,
270 sync_start, r);
271 if (ret != 0)
272 goto out;
273 sync_needed = false;
276 if (sync_needed) {
277 ret = regcache_maple_sync_block(map, entry, &mas,
278 sync_start, r);
279 if (ret != 0)
280 goto out;
281 sync_needed = false;
285 out:
286 rcu_read_unlock();
288 map->cache_bypass = false;
290 return ret;
293 static int regcache_maple_exit(struct regmap *map)
295 struct maple_tree *mt = map->cache;
296 MA_STATE(mas, mt, 0, UINT_MAX);
297 unsigned int *entry;
299 /* if we've already been called then just return */
300 if (!mt)
301 return 0;
303 mas_lock(&mas);
304 mas_for_each(&mas, entry, UINT_MAX)
305 kfree(entry);
306 __mt_destroy(mt);
307 mas_unlock(&mas);
309 kfree(mt);
310 map->cache = NULL;
312 return 0;
315 static int regcache_maple_insert_block(struct regmap *map, int first,
316 int last)
318 struct maple_tree *mt = map->cache;
319 MA_STATE(mas, mt, first, last);
320 unsigned long *entry;
321 int i, ret;
323 entry = kcalloc(last - first + 1, sizeof(unsigned long), map->alloc_flags);
324 if (!entry)
325 return -ENOMEM;
327 for (i = 0; i < last - first + 1; i++)
328 entry[i] = map->reg_defaults[first + i].def;
330 mas_lock(&mas);
332 mas_set_range(&mas, map->reg_defaults[first].reg,
333 map->reg_defaults[last].reg);
334 ret = mas_store_gfp(&mas, entry, map->alloc_flags);
336 mas_unlock(&mas);
338 if (ret)
339 kfree(entry);
341 return ret;
344 static int regcache_maple_init(struct regmap *map)
346 struct maple_tree *mt;
347 int i;
348 int ret;
349 int range_start;
351 mt = kmalloc(sizeof(*mt), map->alloc_flags);
352 if (!mt)
353 return -ENOMEM;
354 map->cache = mt;
356 mt_init(mt);
358 if (!mt_external_lock(mt) && map->lock_key)
359 lockdep_set_class_and_subclass(&mt->ma_lock, map->lock_key, 1);
361 if (!map->num_reg_defaults)
362 return 0;
364 range_start = 0;
366 /* Scan for ranges of contiguous registers */
367 for (i = 1; i < map->num_reg_defaults; i++) {
368 if (map->reg_defaults[i].reg !=
369 map->reg_defaults[i - 1].reg + 1) {
370 ret = regcache_maple_insert_block(map, range_start,
371 i - 1);
372 if (ret != 0)
373 goto err;
375 range_start = i;
379 /* Add the last block */
380 ret = regcache_maple_insert_block(map, range_start,
381 map->num_reg_defaults - 1);
382 if (ret != 0)
383 goto err;
385 return 0;
387 err:
388 regcache_maple_exit(map);
389 return ret;
392 struct regcache_ops regcache_maple_ops = {
393 .type = REGCACHE_MAPLE,
394 .name = "maple",
395 .init = regcache_maple_init,
396 .exit = regcache_maple_exit,
397 .read = regcache_maple_read,
398 .write = regcache_maple_write,
399 .drop = regcache_maple_drop,
400 .sync = regcache_maple_sync,