[RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache

Sheetal . posted 2 patches 1 month ago
[RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache
Posted by Sheetal . 1 month ago
From: Sheetal <sheetal@nvidia.com>

Commit e062bdfdd6ad ("regmap: warn users about uninitialized flat
cache") added a warning for drivers using REGCACHE_FLAT when reading
registers not present in reg_defaults.

For hardware where registers have a power-on-reset value of zero
or drivers that wish to treat zero as a valid cache default, adding
all such registers to reg_defaults has drawbacks:

1. Maintenance burden: Drivers must list every readable register
   regardless of its reset value.

2. No functional benefit: Entries like { REG, 0x0 } only set the
   validity bit; the cache value is already zero.

3. Code bloat: Large reg_defaults arrays increase driver size.

Add a cache_default_is_zero flag to struct regmap_config. When set,
the flat cache marks registers as valid on first read instead of
warning. This ensures only accessed registers are marked valid,
keeping sync scope minimal and avoiding writes to unused registers
or holes.

Signed-off-by: Sheetal <sheetal@nvidia.com>
---
 drivers/base/regmap/internal.h      |  2 ++
 drivers/base/regmap/regcache-flat.c | 12 ++++++++----
 drivers/base/regmap/regcache.c      |  1 +
 include/linux/regmap.h              |  1 +
 4 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/drivers/base/regmap/internal.h b/drivers/base/regmap/internal.h
index 1477329410ec..8e805046526a 100644
--- a/drivers/base/regmap/internal.h
+++ b/drivers/base/regmap/internal.h
@@ -157,6 +157,8 @@ struct regmap {
 	bool cache_dirty;
 	/* if set, the HW registers are known to match map->reg_defaults */
 	bool no_sync_defaults;
+	/* if set, zero is a valid default for registers not in reg_defaults */
+	bool cache_default_is_zero;
 
 	struct reg_sequence *patch;
 	int patch_regs;
diff --git a/drivers/base/regmap/regcache-flat.c b/drivers/base/regmap/regcache-flat.c
index 53cc59c84e2f..ea12e13e1365 100644
--- a/drivers/base/regmap/regcache-flat.c
+++ b/drivers/base/regmap/regcache-flat.c
@@ -88,10 +88,14 @@ static int regcache_flat_read(struct regmap *map,
 	struct regcache_flat_data *cache = map->cache;
 	unsigned int index = regcache_flat_get_index(map, reg);
 
-	/* legacy behavior: ignore validity, but warn the user */
-	if (unlikely(!test_bit(index, cache->valid)))
-		dev_warn_once(map->dev,
-			"using zero-initialized flat cache, this may cause unexpected behavior");
+	/* legacy behavior: ignore validity, but warn the user if zero is not a valid default */
+	if (unlikely(!test_bit(index, cache->valid))) {
+		if (map->cache_default_is_zero)
+			set_bit(index, cache->valid);
+		else
+			dev_warn_once(map->dev,
+				      "using zero-initialized flat cache, this may cause unexpected behavior");
+	}
 
 	*value = cache->data[index];
 
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index 319c342bf5a0..2d0e5c9ba51c 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -177,6 +177,7 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
 	map->reg_defaults_raw = config->reg_defaults_raw;
 	map->cache_word_size = BITS_TO_BYTES(config->val_bits);
 	map->cache_size_raw = map->cache_word_size * config->num_reg_defaults_raw;
+	map->cache_default_is_zero = config->cache_default_is_zero;
 
 	map->cache = NULL;
 	map->cache_ops = cache_types[i];
diff --git a/include/linux/regmap.h b/include/linux/regmap.h
index b0b9be750d93..bf918f88bfd3 100644
--- a/include/linux/regmap.h
+++ b/include/linux/regmap.h
@@ -452,6 +452,7 @@ struct regmap_config {
 	enum regcache_type cache_type;
 	const void *reg_defaults_raw;
 	unsigned int num_reg_defaults_raw;
+	bool cache_default_is_zero;
 
 	unsigned long read_flag_mask;
 	unsigned long write_flag_mask;
-- 
2.34.1
Re: [RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache
Posted by Sander Vanheule 1 month ago
Hi Sheetal,

On Tue, 2026-01-06 at 19:38 +0530, Sheetal . wrote:
> From: Sheetal <sheetal@nvidia.com>
> 
> Commit e062bdfdd6ad ("regmap: warn users about uninitialized flat
> cache") added a warning for drivers using REGCACHE_FLAT when reading
> registers not present in reg_defaults.
> 
> For hardware where registers have a power-on-reset value of zero
> or drivers that wish to treat zero as a valid cache default, adding
> all such registers to reg_defaults has drawbacks:
> 
> 1. Maintenance burden: Drivers must list every readable register
>    regardless of its reset value.
> 
> 2. No functional benefit: Entries like { REG, 0x0 } only set the
>    validity bit; the cache value is already zero.

This is only true because REGCACHE_FLAT just so happens to zero-initialize its
cache, which IMHO should be considered an implementation detail. If you were to
switch to another cache type, you would also need these defaults to maintain the
current behavior.

> 3. Code bloat: Large reg_defaults arrays increase driver size.

> Add a cache_default_is_zero flag to struct regmap_config. When set,
> the flat cache marks registers as valid on first read instead of
> warning. This ensures only accessed registers are marked valid,
> keeping sync scope minimal and avoiding writes to unused registers
> or holes.

A special flag only used in the flat cache is exactly the type of config I think
is non-intuitive and should be avoided. It needs an explanation, which implies
documentation that may go out of sync.

If your device has a single contiguous register space that you want to
initialize to zero, all you really need to provide is something like the ranges
used for readable/writable/... registers:

	(struct regcache_defaults_range) {
		.range_min	= REG_MIN,
		.range_max	= REG_MAX,
		.value		= 0,
	}

Instead of a bool, you could add a pointer to a defaults table in the config
(which can be loaded together with the current flat list), just like how
rd_table works.

This would allow others to use the same table for multiple contiguous block,
with zero or non-zero default values. It would work the same for all cache
types, thus avoiding potential confusion, and limit the size increase of your
drivers. Then you could even safely switch to REGCACHE_FLAT_S.

Best,
Sander
Re: [RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache
Posted by Sheetal . 1 month ago

On 07-01-2026 02:49, Sander Vanheule wrote:
> External email: Use caution opening links or attachments
> 
> 
> Hi Sheetal,
> 
> On Tue, 2026-01-06 at 19:38 +0530, Sheetal . wrote:
>> From: Sheetal <sheetal@nvidia.com>
>>
>> Commit e062bdfdd6ad ("regmap: warn users about uninitialized flat
>> cache") added a warning for drivers using REGCACHE_FLAT when reading
>> registers not present in reg_defaults.
>>
>> For hardware where registers have a power-on-reset value of zero
>> or drivers that wish to treat zero as a valid cache default, adding
>> all such registers to reg_defaults has drawbacks:
>>
>> 1. Maintenance burden: Drivers must list every readable register
>>     regardless of its reset value.
>>
>> 2. No functional benefit: Entries like { REG, 0x0 } only set the
>>     validity bit; the cache value is already zero.
> 
> This is only true because REGCACHE_FLAT just so happens to zero-initialize its
> cache, which IMHO should be considered an implementation detail. If you were to
> switch to another cache type, you would also need these defaults to maintain the
> current behavior.


The warning itself only exists in REGCACHE_FLAT not in other cache 
types. So this fix addresses a REGCACHE_FLAT specific warning with a 
REGCACHE_FLAT-specific flag.

I feel that perhaps we could avoid warning the user when they have
explicitly indicated that zero is a valid default for their hardware.
Since the driver author understands their device requirements, this
flag would allow them to opt out of the warning for cases where it
may not be helpful.

> 
>> 3. Code bloat: Large reg_defaults arrays increase driver size.
> 
>> Add a cache_default_is_zero flag to struct regmap_config. When set,
>> the flat cache marks registers as valid on first read instead of
>> warning. This ensures only accessed registers are marked valid,
>> keeping sync scope minimal and avoiding writes to unused registers
>> or holes.
> 
> A special flag only used in the flat cache is exactly the type of config I think
> is non-intuitive and should be avoided. It needs an explanation, which implies
> documentation that may go out of sync.
> 
> If your device has a single contiguous register space that you want to
> initialize to zero, all you really need to provide is something like the ranges
> used for readable/writable/... registers:
> 
>          (struct regcache_defaults_range) {
>                  .range_min      = REG_MIN,
>                  .range_max      = REG_MAX,
>                  .value          = 0,
>          }
> 
> Instead of a bool, you could add a pointer to a defaults table in the config
> (which can be loaded together with the current flat list), just like how
> rd_table works.
> 
> This would allow others to use the same table for multiple contiguous block,
> with zero or non-zero default values. It would work the same for all cache
> types, thus avoiding potential confusion, and limit the size increase of your
> drivers. Then you could even safely switch to REGCACHE_FLAT_S.
> 

The range-based approach is a good idea for contiguous register
blocks. However, if registers with zero defaults are not contiguous
(scattered across the address space), it would need multiple range
entries or multiple reg default entries.


> Best,
> Sander
Re: [RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache
Posted by Mark Brown 1 month ago
On Tue, Jan 06, 2026 at 07:38:26PM +0530, Sheetal . wrote:

> 2. No functional benefit: Entries like { REG, 0x0 } only set the
>    validity bit; the cache value is already zero.

For sparse caches specifying the register also allocates the cache
entry.

> Add a cache_default_is_zero flag to struct regmap_config. When set,
> the flat cache marks registers as valid on first read instead of
> warning. This ensures only accessed registers are marked valid,

Why do this on first read rather than than just fill the valid flags
during initialisation?

> index b0b9be750d93..bf918f88bfd3 100644
> --- a/include/linux/regmap.h
> +++ b/include/linux/regmap.h
> @@ -452,6 +452,7 @@ struct regmap_config {
>  	enum regcache_type cache_type;
>  	const void *reg_defaults_raw;
>  	unsigned int num_reg_defaults_raw;
> +	bool cache_default_is_zero;

It would be better if this were something specific to the flat cache
since otherwise we have to consider what this means for the other cache
types.
Re: [RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache
Posted by Sheetal . 1 month ago

On 06-01-2026 20:42, Mark Brown wrote:
> On Tue, Jan 06, 2026 at 07:38:26PM +0530, Sheetal . wrote:
> 
>> 2. No functional benefit: Entries like { REG, 0x0 } only set the
>>     validity bit; the cache value is already zero.
> 
> For sparse caches specifying the register also allocates the cache
> entry.

ACK

> 
>> Add a cache_default_is_zero flag to struct regmap_config. When set,
>> the flat cache marks registers as valid on first read instead of
>> warning. This ensures only accessed registers are marked valid,
> 
> Why do this on first read rather than than just fill the valid flags
> during initialisation?
> 


Setting valid bits on first read rather than bitmap_fill() at init ensures:
- Only accessed registers are marked valid
- regcache_sync() only syncs registers that were actually used
- Avoids writes to holes or unused registers during sync
- Safer for drivers without writeable_reg callback


>> index b0b9be750d93..bf918f88bfd3 100644
>> --- a/include/linux/regmap.h
>> +++ b/include/linux/regmap.h
>> @@ -452,6 +452,7 @@ struct regmap_config {
>>   	enum regcache_type cache_type;
>>   	const void *reg_defaults_raw;
>>   	unsigned int num_reg_defaults_raw;
>> +	bool cache_default_is_zero;
> 
> It would be better if this were something specific to the flat cache
> since otherwise we have to consider what this means for the other cache
> types.

I can address this by rename to flat_cache_default_is_zero.
Re: [RFC PATCH 1/2] regmap: Add cache_default_is_zero flag for flat cache
Posted by Mark Brown 1 month ago
On Wed, Jan 07, 2026 at 12:25:01PM +0530, Sheetal . wrote:
> On 06-01-2026 20:42, Mark Brown wrote:

> > Why do this on first read rather than than just fill the valid flags
> > during initialisation?

> Setting valid bits on first read rather than bitmap_fill() at init ensures:
> - Only accessed registers are marked valid
> - regcache_sync() only syncs registers that were actually used
> - Avoids writes to holes or unused registers during sync
> - Safer for drivers without writeable_reg callback

Seems reasonable, put that in the changelog please.

BTW I forgot in my initial review but please add KUnit coverage for
this, we've got good coverage of the cache code.