regmap: Add cache_default_is_zero flag for flat cache
Subsystems (5)
cover regmap: Add cache_default_is_zero flag for flat cache
From: Sheetal <[email protected]>
This patch series adds a cache_default_is_zero flag for REGCACHE_FLAT that marks cache entries as valid on first read.
For hardware with zero power-on-reset registers, this avoids the need to add all such registers to reg_defaults (maintenance burden, code bloat, no functional benefit) just to set the validity bits.
Sheetal (2):
regmap: Add cache_default_is_zero flag for flat cache
ASoC: tegra: Enable cache_default_is_zero for audio drivers
drivers/base/regmap/internal.h | 2 ++ drivers/base/regmap/regcache-flat.c | 11 +++++++---- drivers/base/regmap/regcache.c | 1 + include/linux/regmap.h | 3 +++ sound/soc/tegra/tegra186_asrc.c | 1 + sound/soc/tegra/tegra186_dspk.c | 1 + sound/soc/tegra/tegra210_admaif.c | 3 +++ sound/soc/tegra/tegra210_adx.c | 2 ++ sound/soc/tegra/tegra210_ahub.c | 3 +++ sound/soc/tegra/tegra210_amx.c | 3 +++ sound/soc/tegra/tegra210_dmic.c | 1 + sound/soc/tegra/tegra210_i2s.c | 2 ++ sound/soc/tegra/tegra210_mbdrc.c | 1 + sound/soc/tegra/tegra210_mixer.c | 1 + sound/soc/tegra/tegra210_mvc.c | 1 + sound/soc/tegra/tegra210_ope.c | 1 + sound/soc/tegra/tegra210_peq.c | 1 + sound/soc/tegra/tegra210_sfc.c | 1 + 18 files changed, 35 insertions(+), 4 deletions(-)
Signature · 1 line
2.17.1
RFC PATCH 1/2 regmap: Add cache_default_is_zero flag for flat cache
From: Sheetal <[email protected]>
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.
--- 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(-)
drivers/base/regmap/internal.h +2 -0
@@ -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;
drivers/base/regmap/regcache-flat.c +8 -4
@@ -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];
drivers/base/regmap/regcache.c +1 -0
@@ -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];
include/linux/regmap.h +1 -0
@@ -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;
Signature · 1 line
2.34.1
Mark Brown
maintainerQuoted 2 lines · Sheetal .
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.
Quoted 3 lines
Add a
cache_default_is_zeroflag to structregmap_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?
include/linux/regmap.h struct regmap_config { · line 455
Quoted 8 lines
index
b0b9be750d93..bf918f88bfd3100644include/linux/regmap.h +1
-0@@ -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.
Quoted 7 lines · Mark Brown
Quoted 2 lines · Sheetal .
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
Quoted 8 lines
Quoted 4 lines
Add a
cache_default_is_zeroflag to structregmap_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
Quoted 4 lines
Quoted 4 lines
index
b0b9be750d93..bf918f88bfd3100644include/linux/regmap.h +0
-0@@ -452,6 +452,7 @@ struct regmap_config {
Quoted 8 lines
include/linux/regmap.h struct regmap_config { · line 455
Quoted 4 lines
include/linux/regmap.h +1
-0enum 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.
Mark Brown
maintainerQuoted 10 lines · Sheetal .
Quoted 2 lines · Mark Brown
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 withoutwriteable_regcallback
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.
Hi Sheetal,
Quoted 10 lines · Sheetal .
From: Sheetal <[email protected]>
Commit
e062bdfdd6ad("regmap: warn users about uninitialized flat cache") added a warning for drivers usingREGCACHE_FLATwhen reading registers not present inreg_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_defaultshas drawbacks:
Quoted 5 lines
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.
Quoted 7 lines
3. Code bloat: Large
reg_defaultsarrays increase driver size.Add a
cache_default_is_zeroflag to structregmap_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
Quoted 19 lines · Sander Vanheule
External email: Use caution opening links or attachments
Hi Sheetal,
Quoted 13 lines · Sheetal .
From: Sheetal <[email protected]>
Commit
e062bdfdd6ad("regmap: warn users about uninitialized flat cache") added a warning for drivers usingREGCACHE_FLATwhen reading registers not present inreg_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_defaultshas drawbacks:1. Maintenance burden: Drivers must list every readable register regardless of its reset value.
Quoted 7 lines
Quoted 2 lines
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_FLATjust 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.
Quoted 23 lines
Quoted 8 lines
3. Code bloat: Large
reg_defaultsarrays increase driver size.Add a
cache_default_is_zeroflag to structregmap_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,
}
Quoted 9 lines
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_tableworks.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.
Quoted 2 lines
Best,
Sander
RFC PATCH 2/2 ASoC: tegra: Enable cache_default_is_zero for audio drivers
In reply to Sheetal .From: Sheetal <[email protected]>
Set cache_default_is_zero flag in Tegra audio driver regmap configurations.
Tegra APE hardware has numerous registers with zero power-on-reset values. Use cache_default_is_zero to mark cache entries as valid instead of adding all these registers to reg_defaults arrays.
This patch depends on:
https://patchwork.ozlabs.org/project/linux-tegra/patch/[email protected]/
--- sound/soc/tegra/tegra186_asrc.c | 1 + sound/soc/tegra/tegra186_dspk.c | 1 + sound/soc/tegra/tegra210_admaif.c | 3 +++ sound/soc/tegra/tegra210_adx.c | 2 ++ sound/soc/tegra/tegra210_ahub.c | 3 +++ sound/soc/tegra/tegra210_amx.c | 3 +++ sound/soc/tegra/tegra210_dmic.c | 1 + sound/soc/tegra/tegra210_i2s.c | 2 ++ sound/soc/tegra/tegra210_mbdrc.c | 1 + sound/soc/tegra/tegra210_mixer.c | 1 + sound/soc/tegra/tegra210_mvc.c | 1 + sound/soc/tegra/tegra210_ope.c | 1 + sound/soc/tegra/tegra210_peq.c | 1 + sound/soc/tegra/tegra210_sfc.c | 1 + 14 files changed, 22 insertions(+)
sound/soc/tegra/tegra186_asrc.c +1 -0
@@ -951,6 +951,7 @@ static const struct regmap_config tegra186_asrc_regmap_config = { .reg_defaults = tegra186_asrc_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra186_asrc_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct tegra_asrc_soc_data soc_data_tegra186 = {
sound/soc/tegra/tegra186_dspk.c +1 -0
@@ -468,6 +468,7 @@ static const struct regmap_config tegra186_dspk_regmap = { .reg_defaults = tegra186_dspk_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra186_dspk_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct of_device_id tegra186_dspk_of_match[] = {
sound/soc/tegra/tegra210_admaif.c +3 -0
@@ -242,6 +242,7 @@ static const struct regmap_config tegra210_admaif_regmap_config = { .reg_defaults = tegra210_admaif_reg_defaults, .num_reg_defaults = TEGRA210_ADMAIF_CHANNEL_COUNT * 6 + 1, .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra186_admaif_regmap_config = {
@@ -255,6 +256,7 @@ static const struct regmap_config tegra186_admaif_regmap_config = { .reg_defaults = tegra186_admaif_reg_defaults, .num_reg_defaults = TEGRA186_ADMAIF_CHANNEL_COUNT * 6 + 1, .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra264_admaif_regmap_config = {
@@ -268,6 +270,7 @@ static const struct regmap_config tegra264_admaif_regmap_config = { .reg_defaults = tegra264_admaif_reg_defaults, .num_reg_defaults = TEGRA264_ADMAIF_CHANNEL_COUNT * 6 + 1, .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static int tegra_admaif_runtime_suspend (struct device *dev)
sound/soc/tegra/tegra210_adx.c +2 -0
@@ -626,6 +626,7 @@ static const struct regmap_config tegra210_adx_regmap_config = { .reg_defaults = tegra210_adx_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_adx_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra264_adx_regmap_config = {
@@ -639,6 +640,7 @@ static const struct regmap_config tegra264_adx_regmap_config = { .reg_defaults = tegra264_adx_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra264_adx_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct tegra210_adx_soc_data soc_data_tegra210 = {
sound/soc/tegra/tegra210_ahub.c +3 -0
@@ -2078,6 +2078,7 @@ static const struct regmap_config tegra210_ahub_regmap_config = { .reg_stride = 4, .max_register = TEGRA210_MAX_REGISTER_ADDR, .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra186_ahub_regmap_config = {
@@ -2086,6 +2087,7 @@ static const struct regmap_config tegra186_ahub_regmap_config = { .reg_stride = 4, .max_register = TEGRA186_MAX_REGISTER_ADDR, .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra264_ahub_regmap_config = {
@@ -2095,6 +2097,7 @@ static const struct regmap_config tegra264_ahub_regmap_config = { .writeable_reg = tegra264_ahub_wr_reg, .max_register = TEGRA264_MAX_REGISTER_ADDR, .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct tegra_ahub_soc_data soc_data_tegra210 = {
sound/soc/tegra/tegra210_amx.c +3 -0
@@ -655,6 +655,7 @@ static const struct regmap_config tegra210_amx_regmap_config = { .reg_defaults = tegra210_amx_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_amx_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra194_amx_regmap_config = {
@@ -668,6 +669,7 @@ static const struct regmap_config tegra194_amx_regmap_config = { .reg_defaults = tegra210_amx_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_amx_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct regmap_config tegra264_amx_regmap_config = {
@@ -681,6 +683,7 @@ static const struct regmap_config tegra264_amx_regmap_config = { .reg_defaults = tegra264_amx_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra264_amx_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct tegra210_amx_soc_data soc_data_tegra210 = {
sound/soc/tegra/tegra210_dmic.c +1 -0
@@ -484,6 +484,7 @@ static const struct regmap_config tegra210_dmic_regmap_config = { .reg_defaults = tegra210_dmic_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_dmic_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static int tegra210_dmic_probe (struct platform_device *pdev)
sound/soc/tegra/tegra210_i2s.c +2 -0
@@ -998,6 +998,7 @@ static const struct regmap_config tegra210_regmap_conf = { .reg_defaults = tegra210_i2s_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_i2s_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };/*
@@ -1045,6 +1046,7 @@ static const struct regmap_config tegra264_regmap_conf = { .reg_defaults = tegra264_i2s_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra264_i2s_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static int tegra210_i2s_probe (struct platform_device *pdev)
sound/soc/tegra/tegra210_mbdrc.c +1 -0
@@ -764,6 +764,7 @@ static const struct regmap_config tegra210_mbdrc_regmap_cfg = { .reg_defaults = tegra210_mbdrc_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_mbdrc_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };int tegra210_mbdrc_hw_params (struct snd_soc_component *cmpnt)
sound/soc/tegra/tegra210_mixer.c +1 -0
@@ -609,6 +609,7 @@ static const struct regmap_config tegra210_mixer_regmap_config = { .reg_defaults = tegra210_mixer_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_mixer_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct of_device_id tegra210_mixer_of_match[] = {
sound/soc/tegra/tegra210_mvc.c +1 -0
@@ -700,6 +700,7 @@ static const struct regmap_config tegra210_mvc_regmap_config = { .reg_defaults = tegra210_mvc_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_mvc_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct of_device_id tegra210_mvc_of_match[] = {
sound/soc/tegra/tegra210_ope.c +1 -0
@@ -298,6 +298,7 @@ static const struct regmap_config tegra210_ope_regmap_config = { .reg_defaults = tegra210_ope_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_ope_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static int tegra210_ope_probe (struct platform_device *pdev)
sound/soc/tegra/tegra210_peq.c +1 -0
@@ -307,6 +307,7 @@ static const struct regmap_config tegra210_peq_regmap_config = { .reg_defaults = tegra210_peq_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_peq_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, }; void tegra210_peq_restore(struct regmap *regmap, u32 *biquad_gains,
sound/soc/tegra/tegra210_sfc.c +1 -0
@@ -3570,6 +3570,7 @@ static const struct regmap_config tegra210_sfc_regmap_config = { .reg_defaults = tegra210_sfc_reg_defaults, .num_reg_defaults = ARRAY_SIZE(tegra210_sfc_reg_defaults), .cache_type = REGCACHE_FLAT, + .cache_default_is_zero = true, };static const struct of_device_id tegra210_sfc_of_match[] = {
Signature · 1 line
2.34.1