Skip to content

ASoC/dmaengine: wait for stopped capture streams (t8140-aop-audio sync_stop, apple-admac synchronize) - #259

Merged
RyanTheTide merged 2 commits into
aurora-wipfrom
j700/audio-sync-stop
Oct 11, 2026
Merged

RyanTheTide merged 2 commits into
aurora-wipfrom
j700/audio-sync-stop

Conversation

@RyanTheTide

Copy link
Copy Markdown
Contributor

Summary

Stopping a capture stream did not wait for the stream to have really stopped before ALSA
went on to hw_params, hw_free or prepare, which reallocate or free the buffer.

dmaengine: apple-admac: Wait for the channel to stop in synchronize

device_synchronize() must not return while the controller can still access the memory of
terminated descriptors, but admac_synchronize() only killed the tasklet and freed the
descriptors: terminate_all() posts the STOP write and resets the rings without waiting.
Synchronize now polls the channel's enable bit (the START register; the STOP register
clears it) until it reads back clear, which also pushes out the posted STOP write. The
wait is bounded at 100 ms and warns instead of hanging if a channel never stops.

ASoC: apple: t8140-aop-audio: wait for stopped streams in sync_stop

None of the driver's PCMs had a sync_stop op.

  • The microphone array and the DPCM front-ends stop their ADMAC channel from the trigger
    with dmaengine_terminate_async() and were only synchronized at close, after hw_free
    had already released the buffer these PCMs allocate at every hw_params. They now use
    snd_dmaengine_pcm_sync_stop(), like the generic dmaengine PCM.
  • The low-power microphone has no DMA: its report handler copies into the ALSA buffer under
    the stream state lock after checking the run flag, which the trigger clears. Its
    sync_stop takes that lock once, so a copy that was already under way finishes before
    the buffer can go away, and later reports see the flag clear.

Testing

Written for this tree; tested on a MacBook Neo (J700) and a MacBook Air (M2), capture only:

  • J700: 50 one-second captures each on the low-power microphone, the microphone array and the
    headset input (plain, closed early, different buffer sizes): 0 failed, 0 short. Five forced
    overruns per device recover. 20 captures per DMA-backed device were terminated 10-300 ms after
    open; every one closed cleanly within 204 ms. The final captures carry signal. The base-ns ADMAC RX
    enable register reads back the running channel's bit during a headset-input capture. No
    still enabled, ADMAC, DART or kernel warnings.

  • M2: capture churn on the internal microphones and the headset input (45 captures, a third of them
    closed early, plus forced overruns): 0 failures and no kernel warnings, the same as the stock kernel.

  • Build: J700 configuration (arm64, LLVM, Rust), no new warnings; W=1 clean for
    drivers/dma/apple-admac.o and sound/soc/apple/t8140_aop_audio.o.

  • Capture-only churn on the low-power microphone, the microphone array and the headset
    input: open/close, close while running, buffer-size changes and forced overruns, then a
    final capture with signal on both microphones; no ADMAC, DART or kernel warnings.

🤖 Generated with Claude Code

device_synchronize() must not return while the controller can still
access the memory of terminated descriptors, but admac_synchronize()
only kills the tasklet and frees the descriptors. terminate_all() posts
the channel's STOP write and resets the rings without waiting for the
channel to wind down, so a client that reuses or frees its buffer right
after dmaengine_synchronize() races the channel's last burst, and
nothing even ensures that the STOP write has reached the controller by
then.

The START registers hold the per-channel enable bits that the STOP
registers clear (m1n1's register map calls them TX_EN/RX_EN and
TX_EN_CLR/RX_EN_CLR). Poll the channel's bit until it reads back clear
before synchronize goes on; the read also pushes out the posted STOP
write. A channel only has a burst to finish, so bound the wait
generously and warn instead of hanging the caller if it never clears.

Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
None of the driver's PCMs has a sync_stop op, so after a stop ALSA moves
on to hw_params, hw_free or prepare without waiting for the stream to
have really stopped.

The microphone array and the front-ends stop their ADMAC channel from
the trigger with dmaengine_terminate_async(), and the channel is only
synchronized at close. hw_free comes first and releases the buffer,
which these PCMs allocate at every hw_params, so the channel's last
burst and a pending period callback can land after the buffer is gone.
Give them the dmaengine helper's sync_stop, as the generic dmaengine
PCM does.

The low-power microphone has no DMA: its report handler copies each
reported span into the ALSA buffer under the stream state lock, after
checking the run flag, and the trigger only clears that flag. A report
that passed the check before the stop can still be copying when hw_free
releases a buffer larger than the preallocated one. Its sync_stop takes
the state lock once, which waits for such a copy; any later report sees
the flag clear.

Fixes: eeb1126 ("ASoC: apple: Add T8140 AOP audio driver")
Assisted-by: LLM
Signed-off-by: Ryan Murray <[email protected]>
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.

1 participant