ASoC: cs35l56: Fixes to handling of ASP1 config registers
Subsystems (5)
Richard Fitzgerald
maintainercover ASoC: cs35l56: Fixes to handling of ASP1 config registers
This chain fixes some problems with some previous patches for handling the ASP1 config registers. The root of the problem is that the ownership of these registers can be either with the firmware or the driver, and that the chip has to be soft-reset after downloading the firmware.
This chain adds and uses a regmap_read_bypassed() function so that the driver can leave the regmap in cache-only until the chip has rebooted, but still poll a register to detect when the chip has rebooted.
Richard Fitzgerald (4):
regmap: Add regmap_read_bypassed()
ALSA: hda: cs35l56: Exit cache-only after
cs35l56_wait_for_firmware_boot()
ASoC: cs35l56: Fix unintended bus access while resetting amp
ASoC: cs35l56: Prevent overwriting firmware ASP config
drivers/base/regmap/regmap.c | 37 ++++++++++++++ include/linux/regmap.h | 8 +++ include/sound/cs35l56.h | 2 + sound/pci/hda/cs35l56_hda.c | 4 ++ sound/soc/codecs/cs35l56-sdw.c | 2 - sound/soc/codecs/cs35l56-shared.c | 83 ++++++++++++++++++++----------- sound/soc/codecs/cs35l56.c | 26 +++++++++- 7 files changed, 130 insertions(+), 32 deletions(-)
Signature · 1 line
2.39.2
Richard Fitzgerald
maintainerPATCH 1/4 regmap: Add regmap_read_bypassed()
Add a regmap_read_bypassed() to allow reads from the hardware registers while the regmap is in cache-only mode.
This patch is a prerequisite for a bugfix to the ASoC cs35l56 driver.
A typical use for this is to keep the cache in cache-only mode until the hardware has reached a valid state, but one or more status registers must be polled to determine when this state is reached.
For example, firmware download on the cs35l56 can take several seconds if there are multiple amps sharing limited bus bandwidth. This is too long to block in probe() so it is done as a background task. The device must be soft-reset to reboot the firmware and during this time the registers are not accessible, so the cache should be in cache-only. But the driver must poll a register to detect when reboot has completed.
---
I have a kunit test case for this. But it's based on top of a chain of other changes I've made to the regmap kunit test, so I'll send it with that chain.
--- drivers/base/regmap/regmap.c | 37 ++++++++++++++++++++++++++++++++++++ include/linux/regmap.h | 8 ++++++++ 2 files changed, 45 insertions(+)
drivers/base/regmap/regmap.c +37 -0
@@ -2838,6 +2838,43 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val) }EXPORT_SYMBOL_GPL (regmap_read); +/** +* regmap_read_bypassed() - Read a value from a single register direct +* from the device, bypassing the cache +* +* @map: Register map to read from +* @reg: Register to be read from +* @val: Pointer to store read value +* +* A value of zero will be returned on success, a negative errno will +* be returned in error cases. +*/ +int regmap_read_bypassed (struct regmap *map,unsigned int reg,unsigned int *val) +{ +int ret; +bool bypass, cache_only; + +if (!IS_ALIGNED (reg, map->reg_stride)) +return -EINVAL ; + + map->lock (map->lock_arg); + + bypass = map->cache_bypass; + cache_only = map->cache_only; + map->cache_bypass = true; + map->cache_only = false; + + ret =_regmap_read (map, reg, val); + + map->cache_bypass = bypass; + map->cache_only = cache_only; + + map->unlock (map->lock_arg); + +return ret; +} +EXPORT_SYMBOL_GPL (regmap_read_bypassed); +/** * regmap_raw_read() - Read raw data from the device *
include/linux/regmap.h +8 -0
@@ -1230,6 +1230,7 @@ int regmap_multi_reg_write_bypassed(struct regmap *map,int regmap_raw_write_async (struct regmap *map,unsigned int reg,const void *val,size_t val_len);int regmap_read (struct regmap *map,unsigned int reg,unsigned int *val); +int regmap_read_bypassed (struct regmap *map,unsigned int reg,unsigned int *val);int regmap_raw_read (struct regmap *map,unsigned int reg,void *val,size_t val_len); int regmap_noinc_read(struct regmap *map, unsigned int reg,
@@ -1739,6 +1740,13 @@ static inline int regmap_read(struct regmap *map, unsigned int reg,return -EINVAL ; } +static inline int regmap_read_bypassed (struct regmap *map,unsigned int reg, +unsigned int *val) +{ +WARN_ONCE (1 ,"regmap API is disabled" ); +return -EINVAL ; +} +static inline int regmap_raw_read (struct regmap *map,unsigned int reg,void *val,size_t val_len) {
Signature · 1 line
2.39.2
Mark Brown
maintainerRe: [PATCH 1/4] regmap: Add regmap_read_bypassed()
Quoted 3 lines · Richard Fitzgerald
I have a kunit test case for this. But it's based on top of a chain of other changes I've made to the regmap kunit test, so I'll send it with that chain.
The usual term is series or patch series...
Richard Fitzgerald
maintainerPATCH 2/4 ALSA: hda: cs35l56: Exit cache-only after cs35l56_wait_for_firmware_boot()
In reply to Richard FitzgeraldAdds calls to disable regmap cache-only after a successful return from cs35l56_wait_for_firmware_boot().
This is to prepare for a change in the shared ASoC module that will leave regmap in cache-only mode after cs35l56_system_reset(). This is to prevent register accesses going to the hardware while it is rebooting.
---
This will have to go through Mark's tree because it's necessary to make this change to the HDA driver before applying patch #3 in this series.
--- sound/pci/hda/cs35l56_hda.c | 4 ++++ 1 file changed, 4 insertions(+)
sound/pci/hda/cs35l56_hda.c +4 -0
@@ -644,6 +644,8 @@ static int cs35l56_hda_fw_load(struct cs35l56_hda *cs35l56) ret =cs35l56_wait_for_firmware_boot (&cs35l56->base);if (ret) goto err_powered_up; + +regcache_cache_only (cs35l56->base.regmap, false); }/* Disable auto-hibernate so that runtime_pm has control */
@@ -1002,6 +1004,8 @@ int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int hid, int id)if (ret) goto err; +regcache_cache_only (cs35l56->base.regmap, false); + ret =cs35l56_set_patch (&cs35l56->base);if (ret) goto err;
Signature · 1 line
2.39.2
Richard Fitzgerald
maintainerPATCH 3/4 ASoC: cs35l56: Fix unintended bus access while resetting amp
In reply to Richard FitzgeraldUse the new regmap_read_bypassed() so that the regmap can be left in cache-only mode while it is booting, but the driver can still read boot-status and chip-id information during this time.
This fixes race conditions where some writes could be issued to the silicon while it is still rebooting, before the driver has determined that the boot is complete.
This is typically prevented by putting regmap into cache-only until the hardware is ready. But this assumes that the driver does not need to access device registers to determine when it is "ready". For cs35l56 this involves polling a register and the original implementation relied on having special handlers to block racing callbacks until dsp_work() is complete. However, some cases were missed, most notably the ASP DAI functions.
The regmap_read_bypassed() function allows the fix for this to be simplified to putting regmap into cache-only during the reset. The initial boot stages (poll HALO_STATE and read the chip ID) are all done bypassed. Only when the amp is seen to be booted is the cache-only revoked.
Changes are:
- cs35l56_system_reset() now leaves the regmap in cache-only status.
- cs35l56_wait_for_firmware_boot() polls using regmap_read_bypassed().
- cs35l56_init() revokes cache-only either via cs35l56_hw_init() or when firmware has rebooted after a soft reset.
- cs35l56_hw_init() exits cache-only after it has determined that the amp has booted.
- cs35l56_sdw_init() doesn't disable cache-only, since this must be deferred to cs35l56_init().
- cs35l56_runtime_resume_common() waits for firmware boot before exiting cache-only.
These changes cover three situations where the registers are not accessible:
1) SoundWire first-time enumeration. The regmap is kept in cache-only until the chip is fully booted. The original code had to exit cache-only to read chip status in cs35l56_init() and cs35l56_hw_init() but this is now deferred to after the firmware has rebooted.
In this case cs35l56_sdw_probe() leaves regmap in cache-only (unchanged behaviour) and cs35l56_hw_init() exits cache-only after the firmware is booted and the chip identified.
2) Soft reset during first-time initialization. cs35l56_init() calls cs35l56_system_reset(), which puts regmap into cache-only. On I2C/SPI cs35l56_init() then flows through to call cs35l56_wait_for_firmware_boot() and exit cache-only. On SoundWire the re-enumeration will enter cs35l56_init() again, which then drops down to call cs35l56_wait_for_firmware_boot() and exit cache-only.
3) Soft reset after firmware download. dsp_work() calls cs35l56_system_reset(), which puts regmap into cache-only. After this the flow is the same as (2).
--- sound/soc/codecs/cs35l56-sdw.c | 2 -- sound/soc/codecs/cs35l56-shared.c | 20 ++++++++++++-------- sound/soc/codecs/cs35l56.c | 2 ++ 3 files changed, 14 insertions(+), 10 deletions(-)
sound/soc/codecs/cs35l56-sdw.c +0 -2
@@ -188,8 +188,6 @@ static void cs35l56_sdw_init(struct sdw_slave *peripheral) goto out; } -regcache_cache_only (cs35l56->base.regmap, false); - ret =cs35l56_init (cs35l56);if (ret <0 ) {regcache_cache_only (cs35l56->base.regmap, true);
sound/soc/codecs/cs35l56-shared.c +12 -8
@@ -307,10 +307,10 @@ int cs35l56_wait_for_firmware_boot(struct cs35l56_base *cs35l56_base) reg =CS35L56_DSP1_HALO_STATE ;/* -* This can't be a regmap_read_poll_timeout() because cs35l56 will NAK -* I2C until it has booted which would terminate the poll +* The regmap must remain in cache-only until the chip has +* booted, so use a bypassed read of the status register. */ - poll_ret = read_poll_timeout(regmap_read, read_ret, + poll_ret = read_poll_timeout(regmap_read_bypassed, read_ret, (val < 0xFFFF) && (val >= CS35L56_HALO_STATE_BOOT_DONE), CS35L56_HALO_STATE_POLL_US, CS35L56_HALO_STATE_TIMEOUT_US,
@@ -362,7 +362,8 @@ void cs35l56_system_reset(struct cs35l56_base *cs35l56_base, bool is_soundwire)return ;cs35l56_wait_control_port_ready (); -regcache_cache_only (cs35l56_base->regmap, false); + +/* Leave in cache-only. This will be revoked when the chip has rebooted. */ }EXPORT_SYMBOL_NS_GPL (cs35l56_system_reset,SND_SOC_CS35L56_SHARED );
@@ -577,14 +578,14 @@ int cs35l56_runtime_resume_common(struct cs35l56_base *cs35l56_base, bool is_soucs35l56_issue_wake_event (cs35l56_base); out_sync: -regcache_cache_only (cs35l56_base->regmap, false); - ret =cs35l56_wait_for_firmware_boot (cs35l56_base);if (ret) {dev_err (cs35l56_base->dev,"Hibernate wake failed: %d\n" , ret); goto err; } +regcache_cache_only (cs35l56_base->regmap, false); + ret =cs35l56_mbox_send (cs35l56_base,CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE );if (ret) goto err;
@@ -757,7 +758,7 @@ int cs35l56_hw_init(struct cs35l56_base *cs35l56_base)* devices so the REVID needs to be determined before waiting for the * firmware to boot. */ - ret =regmap_read (cs35l56_base->regmap,CS35L56_REVID , &revid); + ret =regmap_read_bypassed (cs35l56_base->regmap,CS35L56_REVID , &revid);if (ret <0 ) {dev_err (cs35l56_base->dev,"Get Revision ID failed\n" );return ret;
@@ -768,7 +769,7 @@ int cs35l56_hw_init(struct cs35l56_base *cs35l56_base)if (ret)return ret; - ret =regmap_read (cs35l56_base->regmap,CS35L56_DEVID , &devid); + ret =regmap_read_bypassed (cs35l56_base->regmap,CS35L56_DEVID , &devid);if (ret <0 ) {dev_err (cs35l56_base->dev,"Get Device ID failed\n" );return ret;
@@ -787,6 +788,9 @@ int cs35l56_hw_init(struct cs35l56_base *cs35l56_base) cs35l56_base->type = devid &0xFF ; +/* Silicon is now identified and booted so exit cache-only */ +regcache_cache_only (cs35l56_base->regmap, false); + ret =regmap_read (cs35l56_base->regmap,CS35L56_DSP_RESTRICT_STS1 , &secured);if (ret) {dev_err (cs35l56_base->dev,"Get Secure status failed\n" );
sound/soc/codecs/cs35l56.c +2 -0
@@ -1531,6 +1531,8 @@ int cs35l56_init(struct cs35l56_private *cs35l56)return ret;dev_dbg (cs35l56->base.dev,"Firmware rebooted after soft reset\n" ); + +regcache_cache_only (cs35l56->base.regmap, false); }/* Disable auto-hibernate so that runtime_pm has control */
Signature · 1 line
2.39.2
Richard Fitzgerald
maintainerPATCH 4/4 ASoC: cs35l56: Prevent overwriting firmware ASP config
In reply to Richard FitzgeraldOnly populate the ASP1 config registers in the regmap cache if the ASP DAI is used. This prevents regcache_sync() from overwriting these registers with their defaults when the firmware owns control of these registers.
On a SoundWire system the ASP could be owned by the firmware to share reference audio with the firmware on other cs35l56. Or it can be used as a normal codec-codec interface owned by the driver. The driver must not overwrite the registers if the firmware has control of them.
The original implementation for this in commit 07f7d6e7a124 ("ASoC: cs35l56: Fix for initializing ASP1 mixer registers") was to still provide defaults for these registers, assuming that if they were never reconfigured from defaults then regcache_sync() would not write them out because they are not dirty. Unfortunately regcache_sync() is not that smart. If the chip has not reset (so the driver has not called regcache_mark_dirty()) a regcache_sync() could write out registers that are not dirty.
To avoid accidental overwriting of the ASP registers, they are removed from the table of defaults and instead are populated with defaults only if one of the ASP DAI configuration functions is called. So if the DAI has never been configured, the firmware is assumed to have ownership of these registers, and the regmap cache will not contain any entries for them.
--- include/sound/cs35l56.h | 2 + sound/soc/codecs/cs35l56-shared.c | 63 ++++++++++++++++++++----------- sound/soc/codecs/cs35l56.c | 24 +++++++++++- 3 files changed, 67 insertions(+), 22 deletions(-)
include/sound/cs35l56.h +2 -0
@@ -267,6 +267,7 @@ struct cs35l56_base {bool fw_patched;bool secured;bool can_hibernate; +bool fw_owns_asp1;bool cal_data_valid;s8 cal_index;struct cirrus_amp_cal_data cal_data;
@@ -283,6 +284,7 @@ extern const char * const cs35l56_tx_input_texts[CS35L56_NUM_INPUT_SRC];extern const unsigned int cs35l56_tx_input_values[CS35L56_NUM_INPUT_SRC ];int cs35l56_set_patch (struct cs35l56_base *cs35l56_base); +int cs35l56_init_asp1_regs_for_driver_control (struct cs35l56_base *cs35l56_base);int cs35l56_force_sync_asp1_registers_from_cache (struct cs35l56_base *cs35l56_base);int cs35l56_mbox_send (struct cs35l56_base *cs35l56_base,unsigned int command);int cs35l56_firmware_shutdown (struct cs35l56_base *cs35l56_base);
sound/soc/codecs/cs35l56-shared.c +42 -21
83 lines not on this page · raw message
sound/soc/codecs/cs35l56.c +23 -1
@@ -454,9 +454,14 @@ static int cs35l56_asp_dai_set_fmt(struct snd_soc_dai *codec_dai, unsigned int f {struct cs35l56_private *cs35l56 =snd_soc_component_get_drvdata (codec_dai->component);unsigned int val; +int ret;dev_dbg (cs35l56->base.dev,"%s: %#x\n" , __func__, fmt); + ret =cs35l56_init_asp1_regs_for_driver_control (&cs35l56->base); +if (ret) +return ret; +switch (fmt &SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK ) {case SND_SOC_DAIFMT_CBC_CFC :break ;
@@ -530,6 +535,11 @@ static int cs35l56_asp_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx unsigned int rx_mask, int slots, int slot_width) { struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); +int ret; + + ret =cs35l56_init_asp1_regs_for_driver_control (&cs35l56->base); +if (ret) +return ret;if ((slots ==0 ) || (slot_width ==0 )) {dev_dbg (cs35l56->base.dev,"tdm config cleared\n" );
@@ -578,6 +588,11 @@ static int cs35l56_asp_dai_hw_params(struct snd_pcm_substream *substream,struct cs35l56_private *cs35l56 =snd_soc_component_get_drvdata (dai->component);unsigned int rate =params_rate (params);u8 asp_width, asp_wl; +int ret; + + ret =cs35l56_init_asp1_regs_for_driver_control (&cs35l56->base); +if (ret) +return ret; asp_wl =params_width (params);if (cs35l56->asp_slot_width)
@@ -634,7 +649,11 @@ static int cs35l56_asp_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id, unsigned int freq, int dir) { struct cs35l56_private *cs35l56 = snd_soc_component_get_drvdata(dai->component); -int freq_id; +int freq_id, ret; + + ret =cs35l56_init_asp1_regs_for_driver_control (&cs35l56->base); +if (ret) +return ret;if (freq ==0 ) { cs35l56->sysclk_set = false;
@@ -1403,6 +1422,9 @@ int cs35l56_common_probe(struct cs35l56_private *cs35l56) cs35l56->base.cal_index =-1 ; cs35l56->speaker_id = -ENOENT ; +/* Assume that the firmware owns ASP1 until we know different */ + cs35l56->base.fw_owns_asp1 = true; +dev_set_drvdata (cs35l56->base.dev, cs35l56);cs35l56_fill_supply_names (cs35l56->supplies);
Signature · 1 line
2.39.2
Mark Brown
maintainerQuoted 10 lines · Richard Fitzgerald
This chain fixes some problems with some previous patches for handling the ASP1 config registers. The root of the problem is that the ownership of these registers can be either with the firmware or the driver, and that the chip has to be soft-reset after downloading the firmware.
This chain adds and uses a
regmap_read_bypassed()function so that the driver can leave the regmap in cache-only until the chip has rebooted, but still poll a register to detect when the chip has rebooted.[...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next
Thanks!
[1/4] regmap: Add regmap_read_bypassed()
commit: 70ee853eec5693fefd8348a2b049d9cb83362e58
[2/4] ALSA: hda: cs35l56: Exit cache-only after cs35l56_wait_for_firmware_boot()
commit: 73580ec607dfe125b140ed30c7c0a074db78c558
[3/4] ASoC: cs35l56: Fix unintended bus access while resetting amp
commit: d4884fd48a44f3d7f0d4d7399b663b69c000233d
[4/4] ASoC: cs35l56: Prevent overwriting firmware ASP config
commit: dfd2ffb373999630a14d7ff614440f1c2fcc704cAll being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark