mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Vladimir Bely <vlwwwwww@gmail.com>
To: Srinivas Kandagatla <srini@kernel.org>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Mark Brown <broonie@kernel.org>
Cc: Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	linux-sound@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	linux-kernel@vger.kernel.org, Vladimir Bely <vlwwwwww@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v3 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN
Date: Sun, 29 Nov 2026 12:20:28 +0300	[thread overview]
Message-ID: <20261129092028.239529-3-vlwwwwww@gmail.com> (raw)
In-Reply-To: <20261129011802.1025539-1-vlwwwwww@gmail.com>

q6asm_dai_ack() sends one ASM_DATA_CMD_WRITE_V2 APR packet per available
period in a tight loop.  On SoCs with a small SMD FIFO (such as MSM8916),
an initial playback write burst or a buffer with many periods can fill the
FIFO, making rpmsg_trysend() return -EAGAIN.  q6asm_dai_ack() currently
aborts and propagates -EAGAIN to ALSA, causing userspace writes to fail
and playback to stall.

Move playback period queuing into a helper q6asm_dai_queue_playback()
called with the PCM stream lock held.  Handle appl_ptr wrap around
runtime->boundary, guard against appl_ptr moving behind queue_ptr (e.g.
on SNDRV_PCM_IOCTL_RESET or appl_ptr rollback) by requiring
avail <= runtime->buffer_size, and stop cleanly on -EAGAIN without
returning an error.

Defer playback queuing in q6asm_dai_ack() until snd_pcm_running(substream)
and queue initial periods in q6asm_dai_trigger() after q6asm_run_nowait()
succeeds so a pre-start burst cannot exhaust the SMD FIFO and fail
SNDRV_PCM_TRIGGER_START with -EAGAIN.  Retry any remaining periods in
event_handler() on ASM_CLIENT_EVENT_CMD_RUN_DONE and under a single
pcm_stream_lock_irqsave critical section with
snd_pcm_period_elapsed_under_stream_lock() on
ASM_CLIENT_EVENT_DATA_WRITE_DONE.

Fixes: e386c2cf3df2 ("ASoC: qcom: q6asm-dai: schedule all available frames to avoid dsp under-runs")
Cc: stable@vger.kernel.org
Signed-off-by: Vladimir Bely <vlwwwwww@gmail.com>
---
Changes in v3:
- Only queue in q6asm_dai_ack() when snd_pcm_running(substream), queue
  initial playback periods in q6asm_dai_trigger() after q6asm_run_nowait()
  succeeds, and retry on ASM_CLIENT_EVENT_CMD_RUN_DONE so a full SMD FIFO
  cannot fail SNDRV_PCM_TRIGGER_START with -EAGAIN (reported by Sashiko AI).
- Use snd_pcm_period_elapsed_under_stream_lock() inside the same
  pcm_stream_lock_irqsave guard in event_handler().
- Fix the Fixes: trailer subject.

Changes in v2:
- Bound avail <= runtime->buffer_size and serialize event_handler() via
  pcm_stream_lock_irqsave + snd_pcm_running(substream).

 sound/soc/qcom/qdsp6/q6asm-dai.c | 75 ++++++++++++++++++++++++++------
 1 file changed, 61 insertions(+), 14 deletions(-)

diff --git a/sound/soc/qcom/qdsp6/q6asm-dai.c b/sound/soc/qcom/qdsp6/q6asm-dai.c
index 57cbce2bb..b88f431de 100644
--- a/sound/soc/qcom/qdsp6/q6asm-dai.c
+++ b/sound/soc/qcom/qdsp6/q6asm-dai.c
@@ -179,6 +179,40 @@ static const struct snd_compr_codec_caps q6asm_compr_caps = {
 	.descriptor[0].formats = 0,
 };
 
+/*
+ * Queue the periods the application has written so far. Each period is one
+ * APR packet sent with rpmsg_trysend(), and a burst can fill the small SMD
+ * FIFO (-EAGAIN). That is not an error: stop and retry when the DSP signals
+ * CMD_RUN_DONE or DATA_WRITE_DONE. Caller must hold the PCM stream lock.
+ */
+static int q6asm_dai_queue_playback(struct q6asm_dai_rtd *prtd)
+{
+	struct snd_pcm_runtime *runtime = prtd->substream->runtime;
+	int ret;
+
+	while (prtd->state == Q6ASM_STREAM_RUNNING) {
+		snd_pcm_sframes_t avail = runtime->control->appl_ptr - prtd->queue_ptr;
+
+		if (avail < 0)
+			avail += runtime->boundary;
+		if (avail < runtime->period_size || avail > runtime->buffer_size)
+			break;
+
+		ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
+					prtd->pcm_count, 0, 0, 0);
+		if (ret == -EAGAIN)
+			break;
+		if (ret < 0)
+			return ret;
+
+		prtd->queue_ptr += runtime->period_size;
+		if (prtd->queue_ptr >= runtime->boundary)
+			prtd->queue_ptr -= runtime->boundary;
+	}
+
+	return 0;
+}
+
 static void event_handler(uint32_t opcode, uint32_t token,
 			  void *payload, void *priv)
 {
@@ -187,11 +221,29 @@ static void event_handler(uint32_t opcode, uint32_t token,
 
 	switch (opcode) {
 	case ASM_CLIENT_EVENT_CMD_RUN_DONE:
+		if (substream &&
+		    substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+			scoped_guard(pcm_stream_lock_irqsave, substream) {
+				if (substream->runtime &&
+				    snd_pcm_running(substream) &&
+				    q6asm_dai_queue_playback(prtd) < 0)
+					pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
+			}
+		}
 		break;
 	case ASM_CLIENT_EVENT_CMD_EOS_DONE:
 		break;
 	case ASM_CLIENT_EVENT_DATA_WRITE_DONE:
-		snd_pcm_period_elapsed(substream);
+		if (substream) {
+			scoped_guard(pcm_stream_lock_irqsave, substream) {
+				snd_pcm_period_elapsed_under_stream_lock(substream);
+				/* Push any periods left behind by a full SMD FIFO */
+				if (substream->runtime &&
+				    snd_pcm_running(substream) &&
+				    q6asm_dai_queue_playback(prtd) < 0)
+					pr_err_ratelimited("q6asm-dai: Error queuing playback buffer\n");
+			}
+		}
 		break;
 	case ASM_CLIENT_EVENT_DATA_READ_DONE:
 		snd_pcm_period_elapsed(substream);
@@ -321,20 +373,13 @@ static int q6asm_dai_ack(struct snd_soc_component *component, struct snd_pcm_sub
 {
 	struct snd_pcm_runtime *runtime = substream->runtime;
 	struct q6asm_dai_rtd *prtd = runtime->private_data;
-	int i, ret = 0, avail_periods;
-
-	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && prtd->state == Q6ASM_STREAM_RUNNING) {
-		avail_periods = (runtime->control->appl_ptr - prtd->queue_ptr)/runtime->period_size;
-		for (i = 0; i < avail_periods; i++) {
-			ret = q6asm_write_async(prtd->audio_client, prtd->stream_id,
-					   prtd->pcm_count, 0, 0, 0);
+	int ret = 0;
 
-			if (ret < 0) {
-				dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
-				return ret;
-			}
-			prtd->queue_ptr += runtime->period_size;
-		}
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
+	    snd_pcm_running(substream)) {
+		ret = q6asm_dai_queue_playback(prtd);
+		if (ret < 0)
+			dev_err(component->dev, "Error queuing playback buffer %d\n", ret);
 	}
 
 	return ret;
@@ -353,6 +398,8 @@ static int q6asm_dai_trigger(struct snd_soc_component *component,
 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 		ret = q6asm_run_nowait(prtd->audio_client, prtd->stream_id,
 				       0, 0, 0);
+		if (!ret && substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+			ret = q6asm_dai_queue_playback(prtd);
 		break;
 	case SNDRV_PCM_TRIGGER_STOP:
 		ret = q6asm_cmd_nowait(prtd->audio_client, prtd->stream_id,
-- 
2.43.0


  parent reply	other threads:[~2026-11-29  9:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  0:19 [PATCH 0/2] ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback Vladimir Bely
2026-11-29  0:19 ` [PATCH 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read Vladimir Bely
2026-11-29  0:19 ` [PATCH 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN Vladimir Bely
2026-11-29  1:18 ` [PATCH v2 0/2] ASoC: qcom: qdsp6: handle SMD FIFO -EAGAIN in q6asm playback Vladimir Bely
2026-11-29  9:20   ` [PATCH v3 " Vladimir Bely
2026-11-29  9:20   ` [PATCH v3 1/2] ASoC: qcom: q6asm: don't advance dsp_buf on failed write or read Vladimir Bely
2026-11-29  9:20   ` Vladimir Bely [this message]
2026-11-29  1:18 ` [PATCH v2 " Vladimir Bely
2026-11-29  1:18 ` [PATCH v2 2/2] ASoC: qcom: q6asm-dai: retry playback queuing on -EAGAIN Vladimir Bely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129092028.239529-3-vlwwwwww@gmail.com \
    --to=vlwwwwww@gmail.com \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=srini@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®