From c1665e385e0bd666e89a42caf2c9bfa1cfac54dc Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:03:00 -0500 Subject: [PATCH 1/2] ASoC: apple: mca: stop and sync capture DMA before freeing the buffer The capture buffer is a managed buffer: ALSA allocates it at hw_params and frees it at hw_free. The STOP trigger only asks the DMA engine to stop (dmaengine_terminate_async()), and ADMAC can still write the burst in flight after it returns. ALSA waits for the DMA before it frees or reallocates a buffer only through the sync_stop callback, and the component has none, so hw_free could free the capture pages while the channel was still writing to them. Add a sync_stop that waits for the channel with snd_dmaengine_pcm_sync_stop(), as the AOP audio components already do. ALSA calls it after a stop or a suspend, before hw_free, hw_params and prepare. The channel is then idle whenever the buffer is freed, reallocated or handed to the DMA again, and before a system sleep saves and powers down the clusters. Fixes: 79af0bae4d0d ("ASoC: apple: mca: Make the capture buffers cacheable") Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- sound/soc/apple/mca.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/sound/soc/apple/mca.c b/sound/soc/apple/mca.c index a36266b1aa080..511d94b99a473 100644 --- a/sound/soc/apple/mca.c +++ b/sound/soc/apple/mca.c @@ -1077,6 +1077,23 @@ static int mca_close(struct snd_soc_component *component, return snd_dmaengine_pcm_close(substream); } +/* + * The STOP trigger only asks the DMA to stop: it may still write the burst in + * flight. ALSA calls this after a stop or a suspend, before it frees or + * reallocates the buffer (hw_free, hw_params) or prepares the stream again. + * Wait here until the DMA no longer touches the buffer. + */ +static int mca_sync_stop(struct snd_soc_component *component, + struct snd_pcm_substream *substream) +{ + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); + + if (rtd->dai_link->no_pcm) + return 0; + + return snd_dmaengine_pcm_sync_stop(substream); +} + /* * The capture buffers are cacheable (SNDRV_DMA_TYPE_NONCOHERENT): reading * them through an uncached mapping costs the reader several times the @@ -1304,6 +1321,7 @@ static const struct snd_soc_component_driver mca_component = { .close = mca_close, .hw_params = mca_hw_params, .trigger = mca_trigger, + .sync_stop = mca_sync_stop, .pointer = mca_pointer, .pcm_new = mca_pcm_new, .pcm_free = mca_pcm_free, From 5ddd33a809dfa3732d0005a5f9906c6037369f93 Mon Sep 17 00:00:00 2001 From: Dj <43033058+DjDeveloperr@users.noreply.github.com> Date: Thu, 26 Nov 2026 01:03:12 -0500 Subject: [PATCH 2/2] ASoC: apple: mca: end a transfer left paused by a suspend before prepare A stream suspended while running keeps its DMA transfer, paused, so that RESUME can continue it. If the stream is dropped or prepared from the suspended state instead, ALSA sends no STOP trigger and the paused transfer stays the channel's current one. It still points into the old buffer, which hw_free or a new hw_params may then free. ADMAC starts a new transfer only once the current one has ended, so the next START queues behind it and the stream never runs, and a later PAUSE_RELEASE or RESUME would restart the old transfer instead. ALSA prepares a stream before every START: end any transfer left on the channel there, with dmaengine_terminate_sync(). After a stop the channel has none, so in the usual case there is nothing to end. Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com> --- sound/soc/apple/mca.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sound/soc/apple/mca.c b/sound/soc/apple/mca.c index 511d94b99a473..7517c8fe61f7f 100644 --- a/sound/soc/apple/mca.c +++ b/sound/soc/apple/mca.c @@ -1094,6 +1094,25 @@ static int mca_sync_stop(struct snd_soc_component *component, return snd_dmaengine_pcm_sync_stop(substream); } +/* + * A stream suspended while running keeps its transfer, paused, for RESUME. + * Dropped or prepared from the suspended state instead, it gets no STOP: the + * paused transfer would stay the channel's current one, still pointing into + * the old buffer, and the next START would queue behind it and never run. + * ALSA prepares before every START: end such a transfer here. After a stop + * the channel has no transfer left, and there is nothing to end. + */ +static int mca_pcm_prepare(struct snd_soc_component *component, + struct snd_pcm_substream *substream) +{ + struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); + + if (rtd->dai_link->no_pcm) + return 0; + + return dmaengine_terminate_sync(snd_dmaengine_pcm_get_chan(substream)); +} + /* * The capture buffers are cacheable (SNDRV_DMA_TYPE_NONCOHERENT): reading * them through an uncached mapping costs the reader several times the @@ -1320,6 +1339,7 @@ static const struct snd_soc_component_driver mca_component = { .open = mca_pcm_open, .close = mca_close, .hw_params = mca_hw_params, + .prepare = mca_pcm_prepare, .trigger = mca_trigger, .sync_stop = mca_sync_stop, .pointer = mca_pointer,