Skip to content

Commit 9c7f726

Browse files
svanheulebroonie
authored andcommitted
regmap: add flat cache with sparse validity
The flat regcache will always assume the data in the cache is valid. Since the cache is preferred over hardware access, this may shadow the actual state of the device. Add a new containing cache structure with the flat data table and a bitmap indicating cache validity. REGCACHE_FLAT will still behave as before, as the validity is ignored. Define new cache type REGCACHE_FLAT_S: a flat cache with sparse validity. The sparse validity is used to determine if a hardware access should occur to initialize the cache on the fly, vs. at regmap init for REGCACHE_FLAT. Contrary to REGCACHE_FLAT, this allows us to implement regcache_ops.drop. Signed-off-by: Sander Vanheule <sander@svanheule.net> Link: https://patch.msgid.link/20251029081248.52607-2-sander@svanheule.net Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent dcb6fa3 commit 9c7f726

5 files changed

Lines changed: 126 additions & 17 deletions

File tree

drivers/base/regmap/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ enum regmap_endian regmap_get_val_endian(struct device *dev,
288288
const struct regmap_bus *bus,
289289
const struct regmap_config *config);
290290

291+
extern struct regcache_ops regcache_flat_sparse_ops;
291292
extern struct regcache_ops regcache_rbtree_ops;
292293
extern struct regcache_ops regcache_maple_ops;
293294
extern struct regcache_ops regcache_flat_ops;

drivers/base/regmap/regcache-flat.c

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
//
77
// Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
88

9+
#include <linux/bitmap.h>
10+
#include <linux/bitops.h>
911
#include <linux/device.h>
12+
#include <linux/limits.h>
13+
#include <linux/overflow.h>
1014
#include <linux/seq_file.h>
1115
#include <linux/slab.h>
1216

@@ -18,34 +22,62 @@ static inline unsigned int regcache_flat_get_index(const struct regmap *map,
1822
return regcache_get_index_by_order(map, reg);
1923
}
2024

25+
struct regcache_flat_data {
26+
unsigned long *valid;
27+
unsigned int data[];
28+
};
29+
2130
static int regcache_flat_init(struct regmap *map)
2231
{
2332
int i;
24-
unsigned int *cache;
33+
size_t cache_data_size;
34+
unsigned int cache_size;
35+
struct regcache_flat_data *cache;
2536

2637
if (!map || map->reg_stride_order < 0 || !map->max_register_is_set)
2738
return -EINVAL;
2839

29-
map->cache = kcalloc(regcache_flat_get_index(map, map->max_register)
30-
+ 1, sizeof(unsigned int), map->alloc_flags);
31-
if (!map->cache)
40+
cache_size = regcache_flat_get_index(map, map->max_register) + 1;
41+
cache_data_size = struct_size(cache, data, cache_size);
42+
43+
if (cache_data_size == SIZE_MAX) {
44+
dev_err(map->dev, "cannot allocate regmap cache");
3245
return -ENOMEM;
46+
}
3347

34-
cache = map->cache;
48+
cache = kzalloc(cache_data_size, map->alloc_flags);
49+
if (!cache)
50+
return -ENOMEM;
51+
52+
cache->valid = bitmap_zalloc(cache_size, map->alloc_flags);
53+
if (!cache->valid)
54+
goto err_free;
55+
56+
map->cache = cache;
3557

3658
for (i = 0; i < map->num_reg_defaults; i++) {
3759
unsigned int reg = map->reg_defaults[i].reg;
3860
unsigned int index = regcache_flat_get_index(map, reg);
3961

40-
cache[index] = map->reg_defaults[i].def;
62+
cache->data[index] = map->reg_defaults[i].def;
63+
__set_bit(index, cache->valid);
4164
}
4265

4366
return 0;
67+
68+
err_free:
69+
kfree(cache);
70+
return -ENOMEM;
4471
}
4572

4673
static int regcache_flat_exit(struct regmap *map)
4774
{
48-
kfree(map->cache);
75+
struct regcache_flat_data *cache = map->cache;
76+
77+
if (cache)
78+
bitmap_free(cache->valid);
79+
80+
kfree(cache);
4981
map->cache = NULL;
5082

5183
return 0;
@@ -54,21 +86,59 @@ static int regcache_flat_exit(struct regmap *map)
5486
static int regcache_flat_read(struct regmap *map,
5587
unsigned int reg, unsigned int *value)
5688
{
57-
unsigned int *cache = map->cache;
89+
struct regcache_flat_data *cache = map->cache;
5890
unsigned int index = regcache_flat_get_index(map, reg);
5991

60-
*value = cache[index];
92+
*value = cache->data[index];
93+
94+
return 0;
95+
}
96+
97+
static int regcache_flat_sparse_read(struct regmap *map,
98+
unsigned int reg, unsigned int *value)
99+
{
100+
struct regcache_flat_data *cache = map->cache;
101+
unsigned int index = regcache_flat_get_index(map, reg);
102+
103+
if (unlikely(!test_bit(index, cache->valid)))
104+
return -ENOENT;
105+
106+
*value = cache->data[index];
61107

62108
return 0;
63109
}
64110

65111
static int regcache_flat_write(struct regmap *map, unsigned int reg,
66112
unsigned int value)
67113
{
68-
unsigned int *cache = map->cache;
114+
struct regcache_flat_data *cache = map->cache;
69115
unsigned int index = regcache_flat_get_index(map, reg);
70116

71-
cache[index] = value;
117+
cache->data[index] = value;
118+
119+
return 0;
120+
}
121+
122+
static int regcache_flat_sparse_write(struct regmap *map, unsigned int reg,
123+
unsigned int value)
124+
{
125+
struct regcache_flat_data *cache = map->cache;
126+
unsigned int index = regcache_flat_get_index(map, reg);
127+
128+
cache->data[index] = value;
129+
__set_bit(index, cache->valid);
130+
131+
return 0;
132+
}
133+
134+
static int regcache_flat_drop(struct regmap *map, unsigned int min,
135+
unsigned int max)
136+
{
137+
struct regcache_flat_data *cache = map->cache;
138+
unsigned int bitmap_min = regcache_flat_get_index(map, min);
139+
unsigned int bitmap_max = regcache_flat_get_index(map, max);
140+
141+
bitmap_clear(cache->valid, bitmap_min, bitmap_max + 1 - bitmap_min);
72142

73143
return 0;
74144
}
@@ -81,3 +151,13 @@ struct regcache_ops regcache_flat_ops = {
81151
.read = regcache_flat_read,
82152
.write = regcache_flat_write,
83153
};
154+
155+
struct regcache_ops regcache_flat_sparse_ops = {
156+
.type = REGCACHE_FLAT_S,
157+
.name = "flat-sparse",
158+
.init = regcache_flat_init,
159+
.exit = regcache_flat_exit,
160+
.read = regcache_flat_sparse_read,
161+
.write = regcache_flat_sparse_write,
162+
.drop = regcache_flat_drop,
163+
};

drivers/base/regmap/regcache.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "internal.h"
1717

1818
static const struct regcache_ops *cache_types[] = {
19+
&regcache_flat_sparse_ops,
1920
&regcache_rbtree_ops,
2021
&regcache_maple_ops,
2122
&regcache_flat_ops,

drivers/base/regmap/regmap-kunit.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static const char *regcache_type_name(enum regcache_type type)
5454
return "none";
5555
case REGCACHE_FLAT:
5656
return "flat";
57+
case REGCACHE_FLAT_S:
58+
return "flat-sparse";
5759
case REGCACHE_RBTREE:
5860
return "rbtree";
5961
case REGCACHE_MAPLE:
@@ -93,6 +95,8 @@ static const struct regmap_test_param regcache_types_list[] = {
9395
{ .cache = REGCACHE_NONE, .fast_io = true },
9496
{ .cache = REGCACHE_FLAT },
9597
{ .cache = REGCACHE_FLAT, .fast_io = true },
98+
{ .cache = REGCACHE_FLAT_S },
99+
{ .cache = REGCACHE_FLAT_S, .fast_io = true },
96100
{ .cache = REGCACHE_RBTREE },
97101
{ .cache = REGCACHE_RBTREE, .fast_io = true },
98102
{ .cache = REGCACHE_MAPLE },
@@ -104,6 +108,8 @@ KUNIT_ARRAY_PARAM(regcache_types, regcache_types_list, param_to_desc);
104108
static const struct regmap_test_param real_cache_types_only_list[] = {
105109
{ .cache = REGCACHE_FLAT },
106110
{ .cache = REGCACHE_FLAT, .fast_io = true },
111+
{ .cache = REGCACHE_FLAT_S },
112+
{ .cache = REGCACHE_FLAT_S, .fast_io = true },
107113
{ .cache = REGCACHE_RBTREE },
108114
{ .cache = REGCACHE_RBTREE, .fast_io = true },
109115
{ .cache = REGCACHE_MAPLE },
@@ -119,6 +125,12 @@ static const struct regmap_test_param real_cache_types_list[] = {
119125
{ .cache = REGCACHE_FLAT, .from_reg = 0x2002 },
120126
{ .cache = REGCACHE_FLAT, .from_reg = 0x2003 },
121127
{ .cache = REGCACHE_FLAT, .from_reg = 0x2004 },
128+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0 },
129+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0, .fast_io = true },
130+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2001 },
131+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2002 },
132+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2003 },
133+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2004 },
122134
{ .cache = REGCACHE_RBTREE, .from_reg = 0 },
123135
{ .cache = REGCACHE_RBTREE, .from_reg = 0, .fast_io = true },
124136
{ .cache = REGCACHE_RBTREE, .from_reg = 0x2001 },
@@ -136,6 +148,12 @@ static const struct regmap_test_param real_cache_types_list[] = {
136148
KUNIT_ARRAY_PARAM(real_cache_types, real_cache_types_list, param_to_desc);
137149

138150
static const struct regmap_test_param sparse_cache_types_list[] = {
151+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0 },
152+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0, .fast_io = true },
153+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2001 },
154+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2002 },
155+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2003 },
156+
{ .cache = REGCACHE_FLAT_S, .from_reg = 0x2004 },
139157
{ .cache = REGCACHE_RBTREE, .from_reg = 0 },
140158
{ .cache = REGCACHE_RBTREE, .from_reg = 0, .fast_io = true },
141159
{ .cache = REGCACHE_RBTREE, .from_reg = 0x2001 },
@@ -1597,6 +1615,8 @@ static const struct regmap_test_param raw_types_list[] = {
15971615
{ .cache = REGCACHE_NONE, .val_endian = REGMAP_ENDIAN_BIG },
15981616
{ .cache = REGCACHE_FLAT, .val_endian = REGMAP_ENDIAN_LITTLE },
15991617
{ .cache = REGCACHE_FLAT, .val_endian = REGMAP_ENDIAN_BIG },
1618+
{ .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_LITTLE },
1619+
{ .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_BIG },
16001620
{ .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_LITTLE },
16011621
{ .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_BIG },
16021622
{ .cache = REGCACHE_MAPLE, .val_endian = REGMAP_ENDIAN_LITTLE },
@@ -1608,6 +1628,8 @@ KUNIT_ARRAY_PARAM(raw_test_types, raw_types_list, param_to_desc);
16081628
static const struct regmap_test_param raw_cache_types_list[] = {
16091629
{ .cache = REGCACHE_FLAT, .val_endian = REGMAP_ENDIAN_LITTLE },
16101630
{ .cache = REGCACHE_FLAT, .val_endian = REGMAP_ENDIAN_BIG },
1631+
{ .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_LITTLE },
1632+
{ .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_BIG },
16111633
{ .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_LITTLE },
16121634
{ .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_BIG },
16131635
{ .cache = REGCACHE_MAPLE, .val_endian = REGMAP_ENDIAN_LITTLE },

include/linux/regmap.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,23 @@ struct sdw_slave;
5555
#define REGMAP_DOWNSHIFT(s) (s)
5656

5757
/*
58-
* The supported cache types, the default is no cache. Any new caches
59-
* should usually use the maple tree cache unless they specifically
60-
* require that there are never any allocations at runtime and can't
61-
* provide defaults in which case they should use the flat cache. The
62-
* rbtree cache *may* have some performance advantage for very low end
63-
* systems that make heavy use of cache syncs but is mainly legacy.
58+
* The supported cache types, the default is no cache. Any new caches should
59+
* usually use the maple tree cache unless they specifically require that there
60+
* are never any allocations at runtime in which case they should use the sparse
61+
* flat cache. The rbtree cache *may* have some performance advantage for very
62+
* low end systems that make heavy use of cache syncs but is mainly legacy.
63+
* These caches are sparse and entries will be initialized from hardware if no
64+
* default has been provided.
65+
* The non-sparse flat cache is provided for compatibility with existing users
66+
* and will zero-initialize cache entries for which no defaults are provided.
67+
* New users should use the sparse flat cache.
6468
*/
6569
enum regcache_type {
6670
REGCACHE_NONE,
6771
REGCACHE_RBTREE,
6872
REGCACHE_FLAT,
6973
REGCACHE_MAPLE,
74+
REGCACHE_FLAT_S,
7075
};
7176

7277
/**

0 commit comments

Comments
 (0)