You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Distorted "metallic" sound after snd_pcm_drain snd_pcm_prepare cycle #5198
Describe the bug
Intermittently sounds becomes distorted after snd_pcm_drain / snd_pcm_prepare ALSA call. Does not happen if the device is reopened after snd_pcm_drain with snd_pcm_close / snd_pcm_open.
It seems it's the snd_pcm_drain() that triggers a bug in SOF stack. Just commeting out the drain between iterations, and the bug is not triggerred (but obviously this changes the behaviour so not a solution). Replacing snd_pcm_drain() with snd_pcm_drop() does not solve.
@ujfalusi has a potential fix, would this be a kernel side or do we keep on FW side?
The proposed fix is submitted: zephyrproject-rtos/zephyr#79275 @madenvel, unfortunately you cannot test this as you would need a signed firmware binary, we will do extended validation (with the PR your repro.cpp passes hundreds of iterations w/o metallic sound) and will be included in the next sof-bin release.
@ujfalusi@madenvel@kv2019i I have an alternate proposal to fix this in the kernel. Basically cleaning up the link DMA during stop always also seems to help. Could you please verify if this works?
diff --git a/sound/soc/sof/intel/hda-dai.c b/sound/soc/sof/intel/hda-dai.c
index f1a8491bba9e..df8bf497e224 100644
--- a/sound/soc/sof/intel/hda-dai.c
+++ b/sound/soc/sof/intel/hda-dai.c
@@ -303,6 +303,7 @@ static int __maybe_unused hda_dai_trigger(struct snd_pcm_substream *substream, i
switch (cmd) {
case SNDRV_PCM_TRIGGER_SUSPEND:
+ case SNDRV_PCM_TRIGGER_STOP:
ret = hda_link_dma_cleanup(substream, hext_stream, dai);
if (ret < 0) {
dev_err(sdev->dev, "%s: failed to clean up link DMA\n", __func__);
@ranj063 I confirm that your patch fixes the issue for the test code (repro.cpp). I ran 20 iterations and did not hear any distortion while without the patch distortion is present at least in 2 runs out of 10 (usually on 5th and 6th iteration).
I applied it on top of 6.11 coming with Fedora 41 Beta.
Would you have time to contribute it to the kernel?
@ranj063 I confirm that your patch fixes the issue for the test code (repro.cpp). I ran 20 iterations and did not hear any distortion while without the patch distortion is present at least in 2 runs out of 10 (usually on 5th and 6th iteration). I applied it on top of 6.11 coming with Fedora 41 Beta.
Would you have time to contribute it to the kernel?
Let me move this to kernel. Looking at this from pure FW point of view, the problematic sequence seems to be this (running repro.cpp modified to have 3sec delay between calls to play()):
# 1. at end of each test iteration, DAI pipeline is put to PAUSED state
[ 122.155748] <inf> ipc: ipc_cmd: rx : 0x13010003|0x0
[ 122.155765] <inf> pipe: pipeline_trigger: pipe:1 0x0 pipe trigger cmd 2
# 2. a modified repro.cpp adds a 3sec delay which shows here, when next play() iteration
# occurs, DAI pipeline is put to RESET state
[ 125.165575] <inf> ipc: ipc_cmd: rx : 0x13000002|0x1
[ 125.165593] <inf> pipe: pipeline_trigger: pipe:1 0x0 pipe trigger cmd 0
[ 125.165648] <inf> pipe: pipeline_trigger: pipe:0 0x0 pipe trigger cmd 0
[ 125.165668] <wrn> copier: copier_comp_trigger: comp:0 0x4 dai is not ready
# 3. MOD_UNBIND and DELETE_PIPELINE are sent next
[ 125.166176] <inf> ipc: ipc_cmd: rx : 0x46000002|0x3
[ 125.166938] <inf> ipc: ipc_cmd: rx : 0x12000000|0x0
So on Intel platforms with HD-link-DMA, the host must clear the stream-tag in step3 (or arguably already for step2), as the FW can no longer setup the link DMA again for streaming as there is stale data from previous use and the DSP cannot reset this state. zephyrproject-rtos/zephyr#79275 brings the HW to a state that does not cause artifacts upon next iteration, but it's really not correct as some state is retained when there should be none.
Let me move this to kernel. Looking at this from pure FW point of view, the problematic sequence seems to be this (running repro.cpp modified to have 3sec delay between calls to play()):
# 1. at end of each test iteration, DAI pipeline is put to PAUSED state
[ 122.155748] <inf> ipc: ipc_cmd: rx : 0x13010003|0x0
[ 122.155765] <inf> pipe: pipeline_trigger: pipe:1 0x0 pipe trigger cmd 2
# 2. a modified repro.cpp adds a 3sec delay which shows here, when next play() iteration
# occurs, DAI pipeline is put to RESET state
[ 125.165575] <inf> ipc: ipc_cmd: rx : 0x13000002|0x1
[ 125.165593] <inf> pipe: pipeline_trigger: pipe:1 0x0 pipe trigger cmd 0
[ 125.165648] <inf> pipe: pipeline_trigger: pipe:0 0x0 pipe trigger cmd 0
[ 125.165668] <wrn> copier: copier_comp_trigger: comp:0 0x4 dai is not ready
# 3. MOD_UNBIND and DELETE_PIPELINE are sent next
[ 125.166176] <inf> ipc: ipc_cmd: rx : 0x46000002|0x3
[ 125.166938] <inf> ipc: ipc_cmd: rx : 0x12000000|0x0
So on Intel platforms with HD-link-DMA, the host must clear the stream-tag in step3 (or arguably already for step2), as the FW can no longer setup the link DMA again for streaming as there is stale data from previous use and the DSP cannot reset this state. zephyrproject-rtos/zephyr#79275 brings the HW to a state that does not cause artifacts upon next iteration, but it's really not correct as some state is retained when there should be none.
You have omitted what is done on start (and what is done in case of stop and then close).
The reason for the unbind in start here is that for IPC4 we do a delayed stop, on stop the pipeliens are paused and they will be stopped on hw_free time, so if the hw_free is skipped and we go to prepare we do the cleanup, but indeed it looks like it is partial as the hw_free is not done, because the be is not handling this in a similar way how the fe is.
@ujfalusi Not disagreeing with any of that, but point remains, if DAI pipe/copier is unbound and deleted, host needs to do the complementary actions or behaviour of next pipeline will be undefined.
@madenvel the PR with the fix for this issue has been merged to the topic/sof-dev branch. We will upstream it after a few cycles of daily tests in our CI.
I can't reproduce the issue on Fedora 41 (Release).
Kernel 6.11.6-300.fc41.x86_64 , SOF version 2024.09.
@ranj063 as I understand the patch at the end went to the firmware. I haven't checked the kernel sources but I assume your suggestion above also went through or is this something else?
Activity
kv2019i commentedon Sep 30, 2024
Thank you @madenvel for the repro app in https://bugzilla.kernel.org/attachment.cgi?id=306866 -- we can reproduce the problem.
It seems it's the snd_pcm_drain() that triggers a bug in SOF stack. Just commeting out the drain between iterations, and the bug is not triggerred (but obviously this changes the behaviour so not a solution). Replacing snd_pcm_drain() with snd_pcm_drop() does not solve.
@ujfalusi has a potential fix, would this be a kernel side or do we keep on FW side?
ujfalusi commentedon Sep 30, 2024
I would keep this in firmware for now.
The 'fix' in kernel is too intrusive for my liking and it results with IPC error on pcm_free.
ujfalusi commentedon Sep 30, 2024
@kv2019i, dropping the snd_pcm_drain() do changes the behavior since there will be no stop and no prepare happening, just continuous audio playback.
kv2019i commentedon Sep 30, 2024
Note on scope - affects cases where HD-DMA used on the link side.
ujfalusi commentedon Oct 1, 2024
The proposed fix is submitted: zephyrproject-rtos/zephyr#79275
@madenvel, unfortunately you cannot test this as you would need a signed firmware binary, we will do extended validation (with the PR your
repro.cpppasses hundreds of iterations w/o metallic sound) and will be included in the next sof-bin release.ranj063 commentedon Oct 1, 2024
@ujfalusi @madenvel @kv2019i I have an alternate proposal to fix this in the kernel. Basically cleaning up the link DMA during stop always also seems to help. Could you please verify if this works?
ranj063 commentedon Oct 1, 2024
I have restricted this to IPC4 and playback only to address this specific issue in my PR #5197
madenvel commentedon Oct 1, 2024
@ranj063 I confirm that your patch fixes the issue for the test code (
repro.cpp). I ran 20 iterations and did not hear any distortion while without the patch distortion is present at least in 2 runs out of 10 (usually on 5th and 6th iteration).I applied it on top of 6.11 coming with Fedora 41 Beta.
Would you have time to contribute it to the kernel?
@ujfalusi to comment on this.
ranj063 commentedon Oct 2, 2024
@madenvel my PR still needs to be reviewed and tested in our CI before we can upstream it. We will keep you updated.
kv2019i commentedon Oct 2, 2024
Let me move this to kernel. Looking at this from pure FW point of view, the problematic sequence seems to be this (running repro.cpp modified to have 3sec delay between calls to play()):
So on Intel platforms with HD-link-DMA, the host must clear the stream-tag in step3 (or arguably already for step2), as the FW can no longer setup the link DMA again for streaming as there is stale data from previous use and the DSP cannot reset this state. zephyrproject-rtos/zephyr#79275 brings the HW to a state that does not cause artifacts upon next iteration, but it's really not correct as some state is retained when there should be none.
ujfalusi commentedon Oct 2, 2024
You have omitted what is done on start (and what is done in case of stop and then close).
The reason for the unbind in start here is that for IPC4 we do a delayed stop, on stop the pipeliens are paused and they will be stopped on hw_free time, so if the hw_free is skipped and we go to prepare we do the cleanup, but indeed it looks like it is partial as the hw_free is not done, because the be is not handling this in a similar way how the fe is.
kv2019i commentedon Oct 2, 2024
@ujfalusi Not disagreeing with any of that, but point remains, if DAI pipe/copier is unbound and deleted, host needs to do the complementary actions or behaviour of next pipeline will be undefined.
ranj063 commentedon Oct 10, 2024
@madenvel the PR with the fix for this issue has been merged to the topic/sof-dev branch. We will upstream it after a few cycles of daily tests in our CI.
madenvel commentedon Nov 8, 2024
I can't reproduce the issue on Fedora 41 (Release).
Kernel 6.11.6-300.fc41.x86_64 , SOF version 2024.09.
@ranj063 as I understand the patch at the end went to the firmware. I haven't checked the kernel sources but I assume your suggestion above also went through or is this something else?
Thanks!
ujfalusi commentedon Nov 15, 2024
@madenvel, the patches from #5197 were backported to 6.11 and are part of 6.11.6.
If the issue is fixed with the patches, can you close the issue? Thank you.