Repository navigation
ASoC: SOF: reset DMA state in prepare - #1049
Conversation
|
@kv2019i when a pcm is stopped, it is also always followed by a hw_free isnt it? I am confused about why we need to do this twice? |
|
@ranjani wrote:
There's no guarantee about that. HW_FREE is normally called via snd_pcm_close(), but if application is not closing the stream, but rather restarting due to other reasons, HW_FREE is not closed. I agree HW_FREE at stop may not be the right solution in the end. Doing a hwfree->pcmparam cycle in prepare() might be better if this is the right IPC interface to use. |
@kv2019i in our case pcm_params() will only be called in prepare() after a suspend. So it is much safer there |
|
@ranj063 wrote:
Ack, you are right. I think the main open is what mechanism we should use to reset DMA (FYI @lgirdwood ):
First one feels a bit heavy-weight (PCM params are not changing after all), but is by far least intrusive to do. Second is problematic as I believe current PAUSE/UNPAUSE flow relies on TRIG_START/STOP semantics. Third, well, requires a new IPC to be defined. PS I also noticed weird side-effects with the current patch. Aplay -R now survives the test case, but with occurence rate of ~2/10, audio playback is OK control wise but audio content is garbled. OTOH, without the patch, playback fails completely, so it's at least an improvement. |
No.2 sounds like a good idea. @tlauda can you please comment on what you think? |
|
@tlauda wrote:
Ack, so options (2)and (3) (out of #1049 (comment)) are off the table. I today implemented too different approaches: 1.a) full hwfree->hwparams cycle at each prepare() Implemented a cleaned up version of the patch and tested this more. Unfortunately the audio issue persists and can be triggered with many applications. Dma-ptr logs look clean, so this will not be an quick issue to debug. 1.b) add a 'prepare()' method to SOF device ops and perform a HDA stream reset This would be a cleaner option as each SOF device implementation would have a chance to do its own code how to reset the DMA position and for HDA this could be put in HDA specific code. Unfortunately this does not seem to work at all for HDA platforms. If we reset the host side DMA (with a stream reset), FW DMA code will fail upon TRIG_START IPC. -- A bit puzzled how to continue. It seems on HDA platforms, full reprogramming will be needed at prepare(), so probably I'll clean up (1a) and file a new bug to track the audio content issue. |
693519b to
51ce1c1
Compare
|
@kv2019i wrote:
And this is now done. I merged the logic for "hw_params_upon_resume" and new actions needed for prepare() to simplify the resulting code. Patch updated, please review. The garbled audio issue is still there, but I filed a new bug for that (and will be a separate PR, this could be a FW issue as well, not yet rootcaused) -> #1063 |
|
@keyonjie wrote:
This patch does not change the suspend/resume handling at all. So the flows are same as before. I had to touch some existing code as we now have more logic depending on the DSP state (whether PCM is set up or not).
PCM_FREE_IPC is done at suspend, and at resume we recreate the PCM with PCM_PARAMS_IPC. This is same as before.
If by paused you mean ALSA PAUSE sate (SNDRV_PCM_TRIGGER_PAUSE_PUSH, not supported by all drivers), then the flow is exactly as (1). The new flows this patch adds to the drivers:
Upon restart (app calls prepare() and SNDRV_PCM_TRIGGER_START), we reprogram the DSP with PCM_FREE_IPC. This is new, without the patch, we just send a trigger start to the DSP and this led to errors as ALSA assumes the DMA state to be reset at prepare().
Same flow in SOF as (3). |
|
@kv2019i ack |
51ce1c1 to
ec2bb64
Compare
Finally rootcaused the audio issue! One patch added to the series and this fixes #1063 as well. So the series now fixes both #1062 and #1063 . If reviews are ok, this series is good to go. I tested with all the problem causing cases and this series now survives the test. I also did a few S3 stress runs and now new problems were found (as I had to slightly touch the hw_params_upon_resume code). Please review! |
@kv2019i I hope you meant "no new problems were found"? |
ec2bb64 to
c1317f0
Compare
c1317f0 to
ef80f56
Compare
|
SOFCI TEST |
ef80f56 to
7276196
Compare
|
Hmm, the CI got this compiler warning: I didn't get it locally despite using the same settings (the containerof() usage was enough for my gcc-7.4.0). Anyways, fixed this and uploaded a fresh set. Please review, @lyakh @keyonjie @RanderWang @juimonen I'd need two approvals. |
| dev_err(sdev->dev, "error: platform hw free failed\n"); | ||
|
|
||
| spcm->prepared[substream->stream] = false; | ||
|
|
There was a problem hiding this comment.
@kv2019i What happens in the "return ret" case at line 257 above? We abort freeing and leave the stream in a prepared state? But after snd_pcm_lib_free_pages() the stream probably won't work anymore either?
There was a problem hiding this comment.
@lyakh That's really outside this patches scope, I'm not touching that logic. To me it looks that if (!spcm) check fails on line 257, the function has been called with invalid arguments and we cannot proceed (the substream is not correctly set up).
There was a problem hiding this comment.
(!spcm) cannot fail since we dereference spcm above in cancel_work_sync(). Sorry, I meant a different line - I meant "return ret" at the right 257 / left 255, i.e. when IPC fails. I understand and agree, that it's out of scope of this patch set, but maybe it should be double-checked, that in that case we shouldn't call snd_sof_pcm_platform_hw_free(). As you know cleanup paths often ignore errors and actually cleanup functions often return void, so, they often just do as much cleanup as they can... Returning early from a cleanup isn't very common, unless some init steps haven't been completed earlier.
| struct list_head list; /* list in sdev pcm list */ | ||
| struct snd_pcm_hw_params params[2]; | ||
| int hw_params_upon_resume[2]; /* set up hw_params upon resume */ | ||
| int prepared[2]; /* PCM_PARAMS set successfully */ |
There was a problem hiding this comment.
@lyakh I got earlier comment from Pierre that bool should not be used in structs (even if not packed) and I've stopped adding them to structs. Coding style doc seems to now allow them. I also see you now added one snd_sof_dev, but it's really quite messy now when we use 'int', 'u32' and 'bool' for booleans. Is "bool" now ok in structs as well?
UPDATE: ok, there are other bools in sof-priv.h already, so I'll add as well, patch updated
|
|
||
| int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev) | ||
| { | ||
| #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA) |
There was a problem hiding this comment.
maybe better not call this function at all when CONFIG_SND_SOC_SOF_HDA isn't selected? The SOF CONFIG_* graph is too confusing...
There was a problem hiding this comment.
@lyakh I completely agree, but I don't want to start refactoring the SOF_HDA approach in this patch too much. Now the #ifdefs are in hda-dsp.c code in sof/intel/ and not in the caller, and I'd prefer to keep it like this in the patch.
UPDATE: I have added cleaning the ifdef-chaos to this improvement item -> #990 @lyakh ok for you (for this patch)?
| case SNDRV_PCM_TRIGGER_SUSPEND: | ||
| case SNDRV_PCM_TRIGGER_STOP: | ||
| stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP; | ||
| reset_hw_params = true; |
There was a problem hiding this comment.
please add spcm->prepared[substream->stream] = false;
I am debugging issue: #1057. For this case, the S3 is interrupted by RTC event, and ASoC only trigger SUSPEND to SOF in both FE & BE. There is no chance to invoke sof_suspend. Then SOF prepare functions are invoked by ASoC. we need to set spcm->prepared[substream->stream] = false to make prepare function work. And you also set link_dev->link_prepared = 0 in BE trigger function, so just add it here
There was a problem hiding this comment.
@RanderWang The prepared variable will be set to 'false' in this case. We set 'reset_hw_params' here to true, and at tne end of function, we will call sof_pcm_dsp_pcm_free() and this will set prepared[substream]=false. So should be fine, right?
There was a problem hiding this comment.
yes, I got it. I was confused by if(!ret)
| struct hdac_ext_link *link; | ||
| const char *name; | ||
| int stream_tag; | ||
| #endif |
There was a problem hiding this comment.
please also refine the layout of variable declaration.
There was a problem hiding this comment.
@rander Ack, will do in next patch revision.
RanderWang
left a comment
There was a problem hiding this comment.
most part is good for me. please check my comments
When application goes through SUSPEND/STOP->PREPARE->START cycle, we should always reprogram the SOF device to start DMA from a known state so that hw_ptr/appl_ptrs remain valid. This is expected by ALSA core as it resets the buffer state as part of prepare (see snd_pcm_do_prepare()). Fix the issue by forcing reconfiguration of the FW with STREAM_PCM_PARAMS in prepare(). Use combined logic to handle prepare and the existing flow to reprogram hw-params after system suspend. Without the fix, first call to pcm pointer() will return an invalid hw_ptr and application may immediately observe XRUN status, unless "start_threshold" SW parameter is set to maximum value by the application. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
Remove duplicated code by using a common helper function to send the PCM_FREE IPC message to FW. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
When application goes through SUSPEND/STOP->PREPARE->START cycle, we should always reprogram the DAI link DMA to ensure it is in sync with the host PCM DMA. Use same state tracking logic to handle both restart and system resume flows. Use link_prepared field of 'struct hdac_ext_stream' to store the state, instead of adding redundant fields to SOF specific structs. Signed-off-by: Kai Vehmanen <kai.vehmanen@linux.intel.com>
7276196 to
2eff398
Compare
|
Thanks @lyakh and @RanderWang for the comments! I now updated the patchset addressing most (and for others I added a response above). Please check. Thanks! |
RanderWang
left a comment
There was a problem hiding this comment.
It will fix my bug also, good news!
The PCM status for hw_ptr and appl_ptr is reset at prepare
by ALSA core (see snd_pcm_do_prepare()).
When application goes through SUSPEND/STOP->PREPARE->START
cycle, we should always reprogram the SOF device DMA to start
zero offset. Otherwise first call to pcm pointer() will
return an invalid hw_ptr and application may immediately
observe XRUN status, unless "start_threshold" SW parameter
is set to maximum value by the application.
In current SOF, prepare() implementation in SOF core only
reprograms the HW in case system is resuming from S3. So
we are clearly not following ALSA semantics here.
In this RFC patch, enforce correct DMA programming by
freeing the HW params at each stop. This does more than
is needed, but ensures aligned start after prepare() is called.
Fixes #1062 #1057