Skip to content

Commit 3a48d21

Browse files
committed
regmap: Load register defaults in blocks rather than register by register
Currently we use the normal single register write function to load the default values into the cache, resulting in a large number of reallocations when there are blocks of registers as we extend the memory region we are using to store the values. Instead scan through the list of defaults for blocks of adjacent registers and do a single allocation and insert for each such block. No functional change. We do not take advantage of the maple tree preallocation, this is purely at the regcache level. It is not clear to me yet if the maple tree level would help much here or if we'd have more overhead from overallocating and then freeing maple tree data. Signed-off-by: Mark Brown <broonie@kernel.org> Link: https://lore.kernel.org/r/20230523-regcache-maple-load-defaults-v1-1-0c04336f005d@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 90d0d60 commit 3a48d21

1 file changed

Lines changed: 52 additions & 6 deletions

File tree

drivers/base/regmap/regcache-maple.c

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,41 @@ static int regcache_maple_exit(struct regmap *map)
239239
return 0;
240240
}
241241

242+
static int regcache_maple_insert_block(struct regmap *map, int first,
243+
int last)
244+
{
245+
struct maple_tree *mt = map->cache;
246+
MA_STATE(mas, mt, first, last);
247+
unsigned long *entry;
248+
int i, ret;
249+
250+
entry = kcalloc(last - first + 1, sizeof(unsigned long), GFP_KERNEL);
251+
if (!entry)
252+
return -ENOMEM;
253+
254+
for (i = 0; i < last - first + 1; i++)
255+
entry[i] = map->reg_defaults[first + i].def;
256+
257+
mas_lock(&mas);
258+
259+
mas_set_range(&mas, map->reg_defaults[first].reg,
260+
map->reg_defaults[last].reg);
261+
ret = mas_store_gfp(&mas, entry, GFP_KERNEL);
262+
263+
mas_unlock(&mas);
264+
265+
if (ret)
266+
kfree(entry);
267+
268+
return ret;
269+
}
270+
242271
static int regcache_maple_init(struct regmap *map)
243272
{
244273
struct maple_tree *mt;
245274
int i;
246275
int ret;
276+
int range_start;
247277

248278
mt = kmalloc(sizeof(*mt), GFP_KERNEL);
249279
if (!mt)
@@ -252,14 +282,30 @@ static int regcache_maple_init(struct regmap *map)
252282

253283
mt_init(mt);
254284

255-
for (i = 0; i < map->num_reg_defaults; i++) {
256-
ret = regcache_maple_write(map,
257-
map->reg_defaults[i].reg,
258-
map->reg_defaults[i].def);
259-
if (ret)
260-
goto err;
285+
if (!map->num_reg_defaults)
286+
return 0;
287+
288+
range_start = 0;
289+
290+
/* Scan for ranges of contiguous registers */
291+
for (i = 1; i < map->num_reg_defaults; i++) {
292+
if (map->reg_defaults[i].reg !=
293+
map->reg_defaults[i - 1].reg + 1) {
294+
ret = regcache_maple_insert_block(map, range_start,
295+
i - 1);
296+
if (ret != 0)
297+
goto err;
298+
299+
range_start = i;
300+
}
261301
}
262302

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+
263309
return 0;
264310

265311
err:

0 commit comments

Comments
 (0)