Skip to content

ASoC: apple: mca: stop and sync capture DMA before freeing the buffer - #3196

Merged
RyanTheTide merged 2 commits into
mainfrom
fix/mca-capture-sync-stop
Nov 30, 2026
Merged

RyanTheTide merged 2 commits into
mainfrom
fix/mca-capture-sync-stop

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What: The MCA capture buffer is allocated at hw_params and freed at hw_free. The STOP trigger only asks ADMAC to stop (dmaengine_terminate_async()), and the component had no sync_stop, so ALSA freed the capture pages without waiting for the channel to finish its last burst. This adds a sync_stop that calls snd_dmaengine_pcm_sync_stop(), as the AOP audio components already do. ALSA calls it before hw_free, hw_params and prepare, and at the end of a suspend. The second commit covers one path that gets no STOP: a stream suspended while running and then dropped or prepared. Its paused transfer stayed on the channel, still pointing into the old buffer, and the next START queued behind it and never ran. prepare now ends any transfer left on the channel.

Tested:

  • scripts/checkpatch.pl --strict passes on both commits at 5ddd33a809df (0 errors, 0 warnings, 0 checks). sound/soc/apple/mca.o builds for arm64 (defconfig, LLVM=1, W=1) with rc=0 and no warnings.
  • J714s (MacBook Pro 14" M5 Pro), silent capture only, with every frame sent to /dev/null (row). It ran in a combined candidate that carries both commits unchanged.
    • 200 open, hw_params, start, stop and hw_free cycles: pass, 0 stalls, 0 DART or ADMAC lines, and no new warnings.
    • One deep sleep with resume and one deep sleep followed by drop and a new start: both pass, and frames arrive after the sleep.
    • On the default kernel, a deep sleep with a capture running never returns and the Mac needs a reset.

Risk: Two-way · every Mac whose built-in audio uses MCA. Only the stop path changes: hw_free, hw_params and prepare now wait for the channel to stop, normally a single register poll. A stream that is resumed after a sleep is not affected.

Not yet

  • The published J714s and J516s silent-capture runs cover this revision. Refresh affected rows after a base merge and obtain independent review.

The J516s silent capture row: the same 200 cycles, without a sleep, since s2idle doesn't return on that Mac. The row is required before merge.

Details
  • snd_dmaengine_pcm_sync_stop() calls dmaengine_synchronize(), which for ADMAC waits until the channel's enable bit clears and kills its tasklet before the descriptors are freed. ADMAC never reports DMA_PAUSED, so the wait also runs after a suspend.
  • No KUnit case: what changes is how the ALSA core and the DMA engine interact, and a unit test would only check that a function pointer is set. The rows exercise the paths themselves.
  • Playback keeps its preallocated buffer, so only capture frees pages at hw_free. The prepare change applies to both directions, because a stale paused transfer blocks playback the same way.

Fixes: 79af0ba ("ASoC: apple: mca: Make the capture buffers cacheable")

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: 79af0ba ("ASoC: apple: mca: Make the capture buffers cacheable")
Signed-off-by: Dj <[email protected]>
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 <[email protected]>
@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

Verdict at 5ddd33a809df: ready (code); the capture rows listed under "Not yet" are still owed before merge

Blockers: none

Checked

  • mca_sync_stop() → snd_dmaengine_pcm_sync_stop(). ADMAC never reports DMA_PAUSED, so this always reaches admac_synchronize(). That function polls until the channel is disabled, kills the tasklet, and frees the terminated descriptors. ALSA calls sync_stop before hw_free, hw_params and prepare, and after a suspend, so the capture pages are freed only after the channel stops writing.
  • mca_pcm_prepare() → dmaengine_terminate_sync(). prepare always runs in process context (snd_pcm_action_nonatomic), so the sleeping wait is allowed there. From SUSPENDED, snd_pcm_prepare() and snd_pcm_drop() send no STOP trigger, because a suspended stream isn't "running". So the paused transfer really did stay on the channel, and ending it here is the only place that catches it. After an ordinary stop the channel is already idle, and the terminate just stops an idle channel. The DMA channel is held from open, so the register writes don't hit a powered-down controller.
  • Back-end (no_pcm) links skip both callbacks, and the front-end substream does the work, as in the AOP components.
  • No runtime switches, no SoC or board gates, and no new constants.
Evidence
  • make W=1 sound/soc/apple/mca.o (asahi profile, LLVM=1, arm64) at 5ddd33a809df: builds, no warnings.
  • checkpatch.pl --strict on both commits: clean.
  • No KUnit suite covers this component. The open/start/stop/hw_free and suspend-then-drop capture rows listed in the PR are what test it.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J516s (MacBook Pro 16" M3 Pro, 27.2 stub) row for 5ddd33a809df, from one boot of main 9a9862253576 + #3180, #3195, #3196, #3021, #3210 (no file overlap). mca-capture-cycle.py --cycles 200 on hw:CARD=AppleJ516,DEV=0, capture only with frames to /dev/null: pass:true, 200/200 cycles, 0 xruns, no stalled cycles, and empty dma_klog and warn_klog (buffers of 128/64 and 1,572,864/256 frames). Main alone gives the same result. The sleep step was skipped as asked. 0 failed units on this boot. An earlier batch boot that also carried #3212 failed the speakerguardd switch test; without #3212 it passes, so that failure isn't this PR's.

@DjDeveloperr

Copy link
Copy Markdown
Contributor Author

J714s (MacBook Pro 14" M5 Pro) row at 5ddd33a809df: PASS, silent capture only.

  • Kernel: 7.3.0-m5-rows-sec2, a warm native build of combined candidate 3e2c44440978. That's main 40e5e29cc915 plus drm/asahi: hold an object's backing for every holder of its pages #3193, accel/ane: h16: check admission before importing the HWX and account the copy #3194, dmaengine: apple-sio: hold tags and ended transfers until the firmware is done with them #3195, drm/asahi: g17: bound submit syncs, install repeated timeline outputs once, return closed queues' identities at close #3200, wip/media-bounds-rows and this PR. It carries both 5ddd33a809df commits unchanged, and sound/soc/apple/mca.c doesn't differ from the PR head in the candidate. Default command line, headless; the default kernel was restored afterwards.

  • Silent: no playback anywhere, and every captured frame went to /dev/null. The device was hw:CARD=AppleJ714,DEV=0 (Primary capture). Speaker Sense was left alone.

  • mca-capture-cycle.py --cycles 200: pass:true, 200 cycles, 0 xruns, stalled_cycles empty, dma_klog empty, warn_klog empty.

  • Sleep cases on the candidate: two separate runs 30 s apart, each one deep mem sleep woken by RTC after 20 s:

    case pass frames before frames after xruns dma_klog
    resume true 9,664 23,744 1 empty
    drop true 9,664 24,064 0 empty

    warn_klog holds only the platform's own suspend/resume lines (mmc power, genpd_prepare -11, display crossbar link-down, USB re-enumeration). All of them appear the same way in the default kernel's earlier deep-sleep evidence, and none come from MCA, ADMAC or a DART.

  • Default kernel 7.3.0-m5-cdirect16, optional drop case: the deep sleep with capture running never returned. There was no JSON after 180 s, and the Mac needed a reset. The same happened to the resume-then-drop pair on the default kernel. So on this Mac, without this PR, a capture left running across the suspend doesn't just give frames_after:0; the sleep hangs.

  • Caveat: on the candidate, running both cases in one invocation started the second suspend 0.7 s after the first resume, while USB and the display were still re-enumerating. That suspend never reached the freezer either. With 30 s between the sleeps, both pass.

@DjDeveloperr
DjDeveloperr marked this pull request as ready for review November 30, 2026 20:45
@DjDeveloperr
DjDeveloperr requested review from a team as code owners November 30, 2026 20:45
@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Merge readiness at 5ddd33a8: not ready yet

Check Detail
CI at head ok 7 checks passed
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head ok comment at 5ddd33a8
Conflicts ok none
PR text ok Risk: two-way · every Mac whose built-in audio uses MCA. Only the stop path changes: hwfree, hwparams and prepare now wait for the channel to stop, normally a single register poll. A stream that is resumed after a sleep is not affected.

Areas: area:audio

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@aurora-silicon-review aurora-silicon-review Bot 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.

Verdict: MERGE at 5ddd33a8

Blockers: none

Checked:

  • Full 1-file diff (sound/soc/apple/mca.c, +38/-0), MERGEABLE against main, Fixes: 79af0bae4d0d.
  • mca_sync_stop() guards rtd->dai_link->no_pcm and calls snd_dmaengine_pcm_sync_stop(substream), ensuring ADMAC finishes any in-flight capture burst and kills its tasklet before ALSA frees or reallocates buffers at hw_free/hw_params/prepare or post-suspend.
  • mca_pcm_prepare() guards rtd->dai_link->no_pcm and calls dmaengine_terminate_sync(snd_dmaengine_pcm_get_chan(substream)) in sleepable process context (snd_pcm_action_nonatomic), clearing any stale paused transfer left after suspend-then-drop/prepare.
  • CI at 5ddd33a8: Host tests, pr-text, and pr-readiness passed.
  • Dual-generation exact-head hardware proof at 5ddd33a809df:
    • J714s (M5 Pro, T6050): 200 capture open/hw_params/start/stop/hw_free cycles passed (0 stalls, 0 DART/ADMAC errors) plus deep sleep resume and deep sleep drop-and-restart passed (fixing stock-kernel hang).
    • J516s (M3 Pro, T6030): 200/200 mca-capture-cycle.py cycles passed (0 xruns, 0 stalls, empty dma_klog/warn_klog, 0 failed units).
Evidence
  • Checked sound/soc/apple/mca.c:1069-1115 and mca_component ops registration at 5ddd33a8.
  • Both J714s (M5 Pro) and J516s (M3 Pro) silent-capture rows ran cleanly with exact commits unchanged.

@RyanTheTide

Copy link
Copy Markdown
Contributor

code-owner ping <@422957901716652033>: merge train at 5ddd33a8. App approved (NEON-PR-REVIEW2); silent capture rows on J714s + J516s. Merges under the 6-point rule 1 h from now once hosted CI (and the KUnit proof) is green.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Merging under the 6-point rule (Ryan 09:29, permanent), at 5ddd33a8. (1) The review App approved at this head. (2) Full CI is green. (3/4) Rows: J714s+J516s-silent-capture. (5) There's no blocking review. (6) Dj was pinged here 68 min ago with no objection. Revert: git revert -m 1 <merge>.

@RyanTheTide
RyanTheTide merged commit ae29c65 into main Nov 30, 2026
12 of 18 checks passed
@RyanTheTide
RyanTheTide deleted the fix/mca-capture-sync-stop branch November 30, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:audio Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants