]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
regmap: add flat cache with sparse validity
authorSander Vanheule <sander@svanheule.net>
Wed, 29 Oct 2025 08:12:47 +0000 (09:12 +0100)
committerMark Brown <broonie@kernel.org>
Wed, 29 Oct 2025 12:54:04 +0000 (12:54 +0000)
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>
drivers/base/regmap/internal.h
drivers/base/regmap/regcache-flat.c
drivers/base/regmap/regcache.c
drivers/base/regmap/regmap-kunit.c
include/linux/regmap.h

index 6f31240ee4a9a22ea1d5e7fe554977b545b70598..8d19a1414d5b2acb40386783f5480c431b243ac8 100644 (file)
@@ -288,6 +288,7 @@ enum regmap_endian regmap_get_val_endian(struct device *dev,
                                         const struct regmap_bus *bus,
                                         const struct regmap_config *config);
 
+extern struct regcache_ops regcache_flat_sparse_ops;
 extern struct regcache_ops regcache_rbtree_ops;
 extern struct regcache_ops regcache_maple_ops;
 extern struct regcache_ops regcache_flat_ops;
index f36d3618b67c1a1e190f051a81c1b5ba0bc59073..86f7679175b16fc366bbaf09a8af1a9340df58cd 100644 (file)
@@ -6,7 +6,11 @@
 //
 // Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
 
+#include <linux/bitmap.h>
+#include <linux/bitops.h>
 #include <linux/device.h>
+#include <linux/limits.h>
+#include <linux/overflow.h>
 #include <linux/seq_file.h>
 #include <linux/slab.h>
 
@@ -18,34 +22,62 @@ static inline unsigned int regcache_flat_get_index(const struct regmap *map,
        return regcache_get_index_by_order(map, reg);
 }
 
+struct regcache_flat_data {
+       unsigned long *valid;
+       unsigned int data[];
+};
+
 static int regcache_flat_init(struct regmap *map)
 {
        int i;
-       unsigned int *cache;
+       size_t cache_data_size;
+       unsigned int cache_size;
+       struct regcache_flat_data *cache;
 
        if (!map || map->reg_stride_order < 0 || !map->max_register_is_set)
                return -EINVAL;
 
-       map->cache = kcalloc(regcache_flat_get_index(map, map->max_register)
-                            + 1, sizeof(unsigned int), map->alloc_flags);
-       if (!map->cache)
+       cache_size = regcache_flat_get_index(map, map->max_register) + 1;
+       cache_data_size = struct_size(cache, data, cache_size);
+
+       if (cache_data_size == SIZE_MAX) {
+               dev_err(map->dev, "cannot allocate regmap cache");
                return -ENOMEM;
+       }
 
-       cache = map->cache;
+       cache = kzalloc(cache_data_size, map->alloc_flags);
+       if (!cache)
+               return -ENOMEM;
+
+       cache->valid = bitmap_zalloc(cache_size, map->alloc_flags);
+       if (!cache->valid)
+               goto err_free;
+
+       map->cache = cache;
 
        for (i = 0; i < map->num_reg_defaults; i++) {
                unsigned int reg = map->reg_defaults[i].reg;
                unsigned int index = regcache_flat_get_index(map, reg);
 
-               cache[index] = map->reg_defaults[i].def;
+               cache->data[index] = map->reg_defaults[i].def;
+               __set_bit(index, cache->valid);
        }
 
        return 0;
+
+err_free:
+       kfree(cache);
+       return -ENOMEM;
 }
 
 static int regcache_flat_exit(struct regmap *map)
 {
-       kfree(map->cache);
+       struct regcache_flat_data *cache = map->cache;
+
+       if (cache)
+               bitmap_free(cache->valid);
+
+       kfree(cache);
        map->cache = NULL;
 
        return 0;
@@ -54,10 +86,24 @@ static int regcache_flat_exit(struct regmap *map)
 static int regcache_flat_read(struct regmap *map,
                              unsigned int reg, unsigned int *value)
 {
-       unsigned int *cache = map->cache;
+       struct regcache_flat_data *cache = map->cache;
        unsigned int index = regcache_flat_get_index(map, reg);
 
-       *value = cache[index];
+       *value = cache->data[index];
+
+       return 0;
+}
+
+static int regcache_flat_sparse_read(struct regmap *map,
+                                    unsigned int reg, unsigned int *value)
+{
+       struct regcache_flat_data *cache = map->cache;
+       unsigned int index = regcache_flat_get_index(map, reg);
+
+       if (unlikely(!test_bit(index, cache->valid)))
+               return -ENOENT;
+
+       *value = cache->data[index];
 
        return 0;
 }
@@ -65,10 +111,34 @@ static int regcache_flat_read(struct regmap *map,
 static int regcache_flat_write(struct regmap *map, unsigned int reg,
                               unsigned int value)
 {
-       unsigned int *cache = map->cache;
+       struct regcache_flat_data *cache = map->cache;
        unsigned int index = regcache_flat_get_index(map, reg);
 
-       cache[index] = value;
+       cache->data[index] = value;
+
+       return 0;
+}
+
+static int regcache_flat_sparse_write(struct regmap *map, unsigned int reg,
+                                     unsigned int value)
+{
+       struct regcache_flat_data *cache = map->cache;
+       unsigned int index = regcache_flat_get_index(map, reg);
+
+       cache->data[index] = value;
+       __set_bit(index, cache->valid);
+
+       return 0;
+}
+
+static int regcache_flat_drop(struct regmap *map, unsigned int min,
+                             unsigned int max)
+{
+       struct regcache_flat_data *cache = map->cache;
+       unsigned int bitmap_min = regcache_flat_get_index(map, min);
+       unsigned int bitmap_max = regcache_flat_get_index(map, max);
+
+       bitmap_clear(cache->valid, bitmap_min, bitmap_max + 1 - bitmap_min);
 
        return 0;
 }
@@ -81,3 +151,13 @@ struct regcache_ops regcache_flat_ops = {
        .read = regcache_flat_read,
        .write = regcache_flat_write,
 };
+
+struct regcache_ops regcache_flat_sparse_ops = {
+       .type = REGCACHE_FLAT_S,
+       .name = "flat-sparse",
+       .init = regcache_flat_init,
+       .exit = regcache_flat_exit,
+       .read = regcache_flat_sparse_read,
+       .write = regcache_flat_sparse_write,
+       .drop = regcache_flat_drop,
+};
index c7650fa434add7d682b485b47211d20e0b7e22f4..0392f5525cf307e9c7428e1108cdf08b16315a50 100644 (file)
@@ -16,6 +16,7 @@
 #include "internal.h"
 
 static const struct regcache_ops *cache_types[] = {
+       &regcache_flat_sparse_ops,
        &regcache_rbtree_ops,
        &regcache_maple_ops,
        &regcache_flat_ops,
index 95c5bf2a78eebb8a74256aa5b6878beae2b2ca97..f6fc5ed016da15a5a3902177f17e8ed8dcb21740 100644 (file)
@@ -54,6 +54,8 @@ static const char *regcache_type_name(enum regcache_type type)
                return "none";
        case REGCACHE_FLAT:
                return "flat";
+       case REGCACHE_FLAT_S:
+               return "flat-sparse";
        case REGCACHE_RBTREE:
                return "rbtree";
        case REGCACHE_MAPLE:
@@ -93,6 +95,8 @@ static const struct regmap_test_param regcache_types_list[] = {
        { .cache = REGCACHE_NONE, .fast_io = true },
        { .cache = REGCACHE_FLAT },
        { .cache = REGCACHE_FLAT, .fast_io = true },
+       { .cache = REGCACHE_FLAT_S },
+       { .cache = REGCACHE_FLAT_S, .fast_io = true },
        { .cache = REGCACHE_RBTREE },
        { .cache = REGCACHE_RBTREE, .fast_io = true },
        { .cache = REGCACHE_MAPLE },
@@ -104,6 +108,8 @@ KUNIT_ARRAY_PARAM(regcache_types, regcache_types_list, param_to_desc);
 static const struct regmap_test_param real_cache_types_only_list[] = {
        { .cache = REGCACHE_FLAT },
        { .cache = REGCACHE_FLAT, .fast_io = true },
+       { .cache = REGCACHE_FLAT_S },
+       { .cache = REGCACHE_FLAT_S, .fast_io = true },
        { .cache = REGCACHE_RBTREE },
        { .cache = REGCACHE_RBTREE, .fast_io = true },
        { .cache = REGCACHE_MAPLE },
@@ -119,6 +125,12 @@ static const struct regmap_test_param real_cache_types_list[] = {
        { .cache = REGCACHE_FLAT,   .from_reg = 0x2002 },
        { .cache = REGCACHE_FLAT,   .from_reg = 0x2003 },
        { .cache = REGCACHE_FLAT,   .from_reg = 0x2004 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0, .fast_io = true },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2001 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2002 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2003 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2004 },
        { .cache = REGCACHE_RBTREE, .from_reg = 0 },
        { .cache = REGCACHE_RBTREE, .from_reg = 0, .fast_io = true },
        { .cache = REGCACHE_RBTREE, .from_reg = 0x2001 },
@@ -136,6 +148,12 @@ static const struct regmap_test_param real_cache_types_list[] = {
 KUNIT_ARRAY_PARAM(real_cache_types, real_cache_types_list, param_to_desc);
 
 static const struct regmap_test_param sparse_cache_types_list[] = {
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0, .fast_io = true },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2001 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2002 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2003 },
+       { .cache = REGCACHE_FLAT_S, .from_reg = 0x2004 },
        { .cache = REGCACHE_RBTREE, .from_reg = 0 },
        { .cache = REGCACHE_RBTREE, .from_reg = 0, .fast_io = true },
        { .cache = REGCACHE_RBTREE, .from_reg = 0x2001 },
@@ -1597,6 +1615,8 @@ static const struct regmap_test_param raw_types_list[] = {
        { .cache = REGCACHE_NONE,   .val_endian = REGMAP_ENDIAN_BIG },
        { .cache = REGCACHE_FLAT,   .val_endian = REGMAP_ENDIAN_LITTLE },
        { .cache = REGCACHE_FLAT,   .val_endian = REGMAP_ENDIAN_BIG },
+       { .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_LITTLE },
+       { .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_BIG },
        { .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_LITTLE },
        { .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_BIG },
        { .cache = REGCACHE_MAPLE,  .val_endian = REGMAP_ENDIAN_LITTLE },
@@ -1608,6 +1628,8 @@ KUNIT_ARRAY_PARAM(raw_test_types, raw_types_list, param_to_desc);
 static const struct regmap_test_param raw_cache_types_list[] = {
        { .cache = REGCACHE_FLAT,   .val_endian = REGMAP_ENDIAN_LITTLE },
        { .cache = REGCACHE_FLAT,   .val_endian = REGMAP_ENDIAN_BIG },
+       { .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_LITTLE },
+       { .cache = REGCACHE_FLAT_S, .val_endian = REGMAP_ENDIAN_BIG },
        { .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_LITTLE },
        { .cache = REGCACHE_RBTREE, .val_endian = REGMAP_ENDIAN_BIG },
        { .cache = REGCACHE_MAPLE,  .val_endian = REGMAP_ENDIAN_LITTLE },
index 4e1ac1fbcec4398bb877a1e5e67ee998d92f8390..17bed25dc4e38ff346a44e9ad632b257bdec662a 100644 (file)
@@ -55,18 +55,23 @@ struct sdw_slave;
 #define REGMAP_DOWNSHIFT(s)    (s)
 
 /*
- * The supported cache types, the default is no cache.  Any new caches
- * should usually use the maple tree cache unless they specifically
- * require that there are never any allocations at runtime and can't
- * provide defaults in which case they should use the flat cache.  The
- * rbtree cache *may* have some performance advantage for very low end
- * systems that make heavy use of cache syncs but is mainly legacy.
+ * The supported cache types, the default is no cache.  Any new caches should
+ * usually use the maple tree cache unless they specifically require that there
+ * are never any allocations at runtime in which case they should use the sparse
+ * flat cache.  The rbtree cache *may* have some performance advantage for very
+ * low end systems that make heavy use of cache syncs but is mainly legacy.
+ * These caches are sparse and entries will be initialized from hardware if no
+ * default has been provided.
+ * The non-sparse flat cache is provided for compatibility with existing users
+ * and will zero-initialize cache entries for which no defaults are provided.
+ * New users should use the sparse flat cache.
  */
 enum regcache_type {
        REGCACHE_NONE,
        REGCACHE_RBTREE,
        REGCACHE_FLAT,
        REGCACHE_MAPLE,
+       REGCACHE_FLAT_S,
 };
 
 /**