Skip to content

ASoC: SOF: reset DMA state in prepare - #1049

Merged
ranj063 merged 3 commits into
thesofproject:topic/sof-devfrom
kv2019i:topic/pcm-startstop-flood
Jul 5, 2019
Merged

ranj063 merged 3 commits into
thesofproject:topic/sof-devfrom
kv2019i:topic/pcm-startstop-flood

Conversation

@kv2019i

@kv2019i kv2019i commented Jun 19, 2019 •

Copy link
Copy Markdown
Collaborator

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

@kv2019i
kv2019i requested a review from ranj063 June 19, 2019 14:26
@ranj063

ranj063 commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator

@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?

@kv2019i

kv2019i commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator Author

@ranjani wrote:

@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?

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.

@ranj063

ranj063 commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator

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

@kv2019i

kv2019i commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator Author

@ranj063 wrote:

@kv2019i in our case pcm_params() will only be called in prepare() after a suspend. So it is much safer there

Ack, you are right. I think the main open is what mechanism we should use to reset DMA (FYI @lgirdwood ):

  1. the full free_params()-free_params() cycle at prepare() (unless the very first one, driver change only)
  2. modify FW to reset DMA position at TRIG_START (no change to driver)
  3. add new IPC that only resets the DMA position (TRIG_PREPARE or some such, canges to driver+fw)

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.

@ranj063

ranj063 commented Jun 19, 2019

Copy link
Copy Markdown
Collaborator

@ranj063 wrote:

@kv2019i in our case pcm_params() will only be called in prepare() after a suspend. So it is much safer there

Ack, you are right. I think the main open is what mechanism we should use to reset DMA (FYI @lgirdwood ):

  1. the full free_params()-free_params() cycle at prepare() (unless the very first one, driver change only)
  2. modify FW to reset DMA position at TRIG_START (no change to driver)
  3. add new IPC that only resets the DMA position (TRIG_PREPARE or some such, canges to driver+fw)

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?

Comment thread sound/soc/sof/pcm.c Resolved
@tlauda

tlauda commented Jun 25, 2019

Copy link
Copy Markdown

@ranj063 @kv2019i FW cannot reset DMA position. Only kernel has such possibility by resetting Host DMA.

@kv2019i

kv2019i commented Jun 26, 2019

Copy link
Copy Markdown
Collaborator Author

@tlauda wrote:

@ranj063 @kv2019i FW cannot reset DMA position. Only kernel has such possibility by resetting Host DMA.

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.

@kv2019i
kv2019i force-pushed the topic/pcm-startstop-flood branch from 693519b to 51ce1c1 Compare June 27, 2019 15:21
@kv2019i kv2019i changed the title [RFC] ASoC: SOF: reset DMA state in prepare ASoC: SOF: reset DMA state in prepare Jun 27, 2019
@kv2019i
kv2019i marked this pull request as ready for review June 27, 2019 16:54
@kv2019i
kv2019i requested a review from plbossart June 27, 2019 16:54
@kv2019i

kv2019i commented Jun 27, 2019

Copy link
Copy Markdown
Collaborator Author

@kv2019i wrote:

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.

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

ranj063
ranj063 previously approved these changes Jun 27, 2019

@ranj063 ranj063 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

@keyonjie

Copy link
Copy Markdown

Can @ranj063 or @kv2019i help illustrate how we are handling suspend/resume now? e.g.

  1. suspend/resume at PCM running.
    What is the sequence?
  2. suspend/resume at PCM paused.
    What is the sequence here?

@kv2019i

kv2019i commented Jun 28, 2019

Copy link
Copy Markdown
Collaborator Author

@keyonjie wrote:

Can @ranj063 or @kv2019i help illustrate how we are handling suspend/resume now? e.g.

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).

  1. suspend/resume at PCM running.

PCM_FREE_IPC is done at suspend, and at resume we recreate the PCM with PCM_PARAMS_IPC. This is same as before.

  1. suspend/resume at PCM paused.

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:

  1. stream is stopped with SNDRV_PCM_TRIGGER_STOP (XRUN occured and ALSA is configured to stop on XRUNs)

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().

  1. stream is stopped with SNDRV_PCM_TRIGGER_STOP and then restarted (application calling snd_pcm_drop() or snd_pcm_drain(), and the snd_pcm_prepare() again)

Same flow in SOF as (3).

@kv2019i

kv2019i commented Jun 28, 2019

Copy link
Copy Markdown
Collaborator Author

@ranj063 Please hold off merging. It seems with this patch, corrupt audio is generated even if start_threshold is set to maximum. E.g. you will trigger #1063 with "aplay foo.wav" and doign a ctrl-Z and "fg" repeatedly.

In short, I need to find a way to fix #1063 first. :(

@ranj063

ranj063 commented Jul 1, 2019

Copy link
Copy Markdown
Collaborator

@kv2019i ack

@kv2019i
kv2019i force-pushed the topic/pcm-startstop-flood branch from 51ce1c1 to ec2bb64 Compare July 2, 2019 11:57
@kv2019i
kv2019i requested a review from lgirdwood as a code owner July 2, 2019 11:57
@kv2019i

kv2019i commented Jul 2, 2019

Copy link
Copy Markdown
Collaborator Author

@ranj063

@kv2019i ack

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!

@ranj063

ranj063 commented Jul 2, 2019

Copy link
Copy Markdown
Collaborator

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).

@kv2019i I hope you meant "no new problems were found"?

Comment thread sound/soc/sof/intel/hda.h Resolved
@kv2019i
kv2019i requested a review from RanderWang July 3, 2019 10:26
@kv2019i
kv2019i force-pushed the topic/pcm-startstop-flood branch from ec2bb64 to c1317f0 Compare July 3, 2019 10:26
@kv2019i
kv2019i force-pushed the topic/pcm-startstop-flood branch from c1317f0 to ef80f56 Compare July 3, 2019 15:21
@kv2019i

kv2019i commented Jul 3, 2019

Copy link
Copy Markdown
Collaborator Author

@ranj063

@kv2019i looks good now. Could you please rebase and resubmit? And we need 2 approvals. @lyakh could you please review?

Ack, this was already on top of your upstream merge, but did another rebase for the recent HDA patches. Now updated and ready to go (pending the other approvals).

ranj063
ranj063 previously approved these changes Jul 3, 2019
@wenqingfu

Copy link
Copy Markdown

SOFCI TEST

@kv2019i

kv2019i commented Jul 4, 2019

Copy link
Copy Markdown
Collaborator Author

Hmm, the CI got this compiler warning:

sound/soc/sof/intel/hda-dsp.c: In function ‘hda_dsp_set_hw_params_upon_resume’:
sound/soc/sof/intel/hda-dsp.c:427:31: error: variable ‘hda_stream’ set but not used [-Werror=unused-but-set-variable]
  struct sof_intel_hda_stream *hda_stream;
                               ^~~~~~~~~~

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.

@kv2019i
kv2019i requested a review from ranj063 July 4, 2019 09:21
ranj063
ranj063 previously approved these changes Jul 4, 2019
RanderWang
RanderWang previously approved these changes Jul 5, 2019

@RanderWang RanderWang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread sound/soc/sof/pcm.c Outdated
dev_err(sdev->dev, "error: platform hw free failed\n");

spcm->prepared[substream->stream] = false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(!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.

Comment thread sound/soc/sof/sof-priv.h Outdated
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 */

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kv2019i bool

@kv2019i kv2019i Jul 5, 2019 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better not call this function at all when CONFIG_SND_SOC_SOF_HDA isn't selected? The SOF CONFIG_* graph is too confusing...

@kv2019i kv2019i Jul 5, 2019 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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)?

Comment thread sound/soc/sof/pcm.c
case SNDRV_PCM_TRIGGER_SUSPEND:
case SNDRV_PCM_TRIGGER_STOP:
stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
reset_hw_params = true;

@RanderWang RanderWang Jul 5, 2019 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I got it. I was confused by if(!ret)

struct hdac_ext_link *link;
const char *name;
int stream_tag;
#endif

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also refine the layout of variable declaration.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rander Ack, will do in next patch revision.

@RanderWang RanderWang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

most part is good for me. please check my comments

kv2019i added 3 commits July 5, 2019 12:09
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>
@kv2019i
kv2019i dismissed stale reviews from RanderWang and ranj063 via 2eff398 July 5, 2019 09:11
@kv2019i
kv2019i force-pushed the topic/pcm-startstop-flood branch from 7276196 to 2eff398 Compare July 5, 2019 09:11
@kv2019i

kv2019i commented Jul 5, 2019

Copy link
Copy Markdown
Collaborator Author

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 RanderWang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fix my bug also, good news!

@ranj063
ranj063 merged commit fdfcca4 into thesofproject:topic/sof-dev Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

start/stop floods with applications using start threshold, e.g. with aplay+S3

7 participants