Skip to content

ASoC: apple: aop-spkr: keep speaker TX DMA fed via driver-owned ring - #3474

Closed
AxxzyWasTaken wants to merge 1 commit into
aurora-silicon:mainfrom
AxxzyWasTaken:pr/aop-spkr-txring-2188
Closed

AxxzyWasTaken wants to merge 1 commit into
aurora-silicon:mainfrom
AxxzyWasTaken:pr/aop-spkr-txring-2188

Conversation

@AxxzyWasTaken

Copy link
Copy Markdown
Contributor

Fixes #2188

What: Replace direct snd_dmaengine_pcm TX0 ownership on j616-pcm-1 in sound/soc/apple/aop_spkr.rs with a symmetric driver-owned TxRing (matching SenseRing, pure ring state machine extracted to aop_spkr_ring.rs) that keeps cyclic TX0 continuously fed with digital silence whenever spkr is in pwrd. This closes all four pwrd-without-TX0 windows that crash AOP RTKit firmware (initial prepare rollback, TRIGGER_STOP/TRIGGER_SUSPEND, stop_locked/put/suspend, and resume), and fast-paths healthy already-running streams in Service::run() (with live power_state() == STATE_PWRD readback and DMA_IN_PROGRESS + 20 ms callback freshness checks) to eliminate the 60 ms mute_codecs() stall in be_prepare. Leaves j616-jack-pcm-0 (cout/cin) untouched.

Tested:

  • rustfmt --check clean; host unit tests run-host-tests.py --only aop-spkr-ring-tests: 12/12 PASS (ring wrap, silence planning, live pointer/delay, backward-wrap glitch clamp, 10 ms softirq stall survival).
  • ARM64 cross-compile (ARCH=arm64 LLVM=1): sound/soc/apple/aop_audio.o and snd-soc-aop.o build with 0 warnings / 0 errors under both CONFIG_KUNIT=n and CONFIG_KUNIT=y; scripts/checkpatch.pl --strict: 0 errors.
  • J616C (MacBook Pro 16" M4 Max, T6041, AppleJ616 hw:1,1 S24_LE 6-ch + hw:1,2 S16_LE 12-ch Speaker Sense) integrated boot (7.3.0-j616c-parity5+ carrying this commit, strict /dev/zero digital silence):
    • 60/60 rapid aplay open/prepare/play-zeros/close cycles (hw:1,1): PASS, 0 AOP crashes.
    • 50/50 rapid snd_pcm_drop -> snd_pcm_prepare -> snd_pcm_writei cycles on hw:1,1 concurrently reading 24,000 live SenseRing frames from hw:1,2 (12-ch S16_LE): PASS, prepare_first = 0.003 ms, reprepare_mean = 0.006 ms (min 0.005 ms, max 0.012 ms, down from ~60 ms), 0 AOP crashes, 0 kernel warnings/errors.

Risk: two-way · Apple AOP speaker playback path (sound/soc/apple/aop_spkr.rs, aop_spkr_ring.rs); headphone jack path (j616-jack-pcm-0) is unchanged.

Not yet: Standalone exact-head bare-metal boot row on f043d55dda9c, audible playback check, and second-board / suspend-resume speaker verification.

The AOP `spkr` firmware service (MCA cluster 0, base-ns ADMAC1 TX0/RX0)
crashes with RTKit global IRQ status 0x1 whenever the serializer sits in
`pwrd` while host playback DMA (TX0) is stopped or starved. Previously,
driving the `j616-pcm-1` playback front-end directly with
`snd_dmaengine_pcm` left four windows where `spkr` stayed in `pwrd`
without TX0 running:

1. Playback STOP / DROP / XRUN / speakerguard lease withdrawal until
   `be_hw_free` muted all six SN012776 amps (~60 ms) and sent `pw1`.
2. System suspend between `TRIGGER_STOP`/`TRIGGER_SUSPEND` and
   `spkr_suspend`.
3. `be_prepare` (`pwrd`) until `TRIGGER_START` (including `-EPERM` lease
   waits in macaudio).
4. `spkr_resume` (`pwrd`) until userspace prepared and started again.

In addition, because stopping TX0 on `STOP` broke slot alignment with the
running serializer, `Service::run()` unconditionally muted all six amps
sequentially (~60 ms synchronously inside `prepare()`), sent `pw1`, and
restarted `SenseRing` and `pwrd`, pushing PipeWire past speakerguard's
heartbeat window and dropping sense frames.

Mirror `SenseRing` with a driver-owned `TxRing` on ADMAC1 TX0:

- `Service` now owns `SpeakerRings { sense, tx }`. `start_with_rings()`
  prepares a zeroed 36 KiB coherent cyclic TX ring (32 x 1.0 ms periods
  of 48 6-channel S24_LE frames, 12.0 ms / 576-frame lookahead), arms
  `SenseRing`, requests `pwrd` (rolling back to `pw1` on error),
  immediately issues TX0 on digital silence, and then verifies `pwrd`.
- Teardown (`stop_locked`, `put`, `suspend`) always transitions `spkr`
  to `pw1` and verifies it before terminating TX0 and RX0 (leaving rings
  running if `pw1` fails in `stop_locked` so a later retry recovers);
  `resume()` brings both rings and `pwrd` back together.
- `j616-pcm-1` uses a `SNDRV_DMA_TYPE_VMALLOC` buffer with
  `SNDRV_PCM_INFO_NO_REWINDS | SNDRV_PCM_INFO_SYNC_APPLPTR` and an `ack`
  callback. `TRIGGER_STOP` and `TRIGGER_SUSPEND` detach audio by zeroing
  only the queued lookahead slice ahead of `dma_now + 2` first (with
  `dma_mb(Write)` for non-coherent arm64 NC buffers) while TX0 keeps
  streaming digital silence.
- Because both TX0 and RX0 stay continuously frame-aligned across stops,
  XRUNs, and resumes, `Service::run()` returns immediately when `spkr`
  is already running, both rings report healthy `DMA_IN_PROGRESS` status
  (with a fresh TX callback timestamp), and AOP still reports `pwrd`,
  falling back to `quiesce()` + restart only on a DMA fault or
  unexpected power state.
- Replace `SenseRing`'s `fence(SeqCst)` with `dma_mb(Read)` and track
  DMA faults on both rings.
- Extract pure ring geometry and cursor arithmetic into `aop_spkr_ring.rs`
  with shared KUnit and host (`tools/asahi/aop-spkr-ring-tests.rs`)
  unit test coverage.

Signed-off-by: AxxzyWasTaken <[email protected]>
@AxxzyWasTaken
AxxzyWasTaken requested review from a team and DjDeveloperr as code owners November 28, 2026 04:32

@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: FINISH, then merge at f043d55d

Blockers:

  1. CI has not finished yet (host-tests, pr-text, readiness are queued; full build + KUnit + host-tests must be green at f043d55d).
  2. Confirm that the J616C 7.3.0-j616c-parity5+ integrated boot row carries f043d55d byte-identically (or run an exact-head boot), and add a second-board / suspend-resume verification row for the modified spkr_suspend()/spkr_resume() error-propagation and TxRing restart paths (plus protected playback verification when applicable).

Checked: no Apple material, no AI attribution, no chip/board/firmware gates, no cmdline hacks, human Signed-off-by, speaker protection integrity (keeps SenseRing continuous and never weakens protection or touches amp limits), TxRing clean-ring silence invariant and dma_mb(Write) barriers, Service::start_with_rings() / stop_locked() / suspend() ordering (pw1 before DMA teardown, pwrd followed immediately by TX0 issue), tx_ring_advance()/tx_ring_live_progress()/tx_ring_silence_plan() wrap and glitch clamping, and j616-jack-pcm-0 isolation.

Evidence
  • Inspected sound/soc/apple/aop_audio.rs, sound/soc/apple/aop_spkr.rs, and sound/soc/apple/aop_spkr_ring.rs.
  • Verified TxRing lifecycle: prepare() zeroes coherent DMA ring + dma_mb(Write) while channel is stopped; start_with_rings() arms SenseRing, sends pwrd, issues cyclic TX0 immediately, and rolls back (pw1 -> stop TX0 -> stop RX0) on any failure; stop_locked() transitions to pw1 before stopping TX0/RX0.
  • Verified Service::run() fast path checks rings.tx.is_healthy() (faulted flag, DMA_IN_PROGRESS, residue bounds, done_at staleness <= 20 ms), rings.sense.is_healthy(), and live power_state() == STATE_PWRD before skipping quiesce() + restart.
  • Verified tx_ring_advance() handles wrap, backward-wrap glitch clamping (>= TX_RING_FRAMES - TX_PERIOD_FRAMES), underrun/drain overtake resynchronization, and tx_ring_silence_plan() zeroes ahead of dma_now + 2 frames before the prefix.
  • J616C integrated boot row (7.3.0-j616c-parity5+, digital silence): 60/60 rapid aplay cycles and 50/50 snd_pcm_drop -> snd_pcm_prepare -> snd_pcm_writei cycles with live SenseRing capture passed with 0 AOP crashes and reprepare_mean = 0.006 ms.

@AxxzyWasTaken

Copy link
Copy Markdown
Contributor Author

Confirmed against the booted 7.3.0-j616c-parity5+ (3969120aab727) tree: all four driver and test files (sound/soc/apple/aop_audio.rs, sound/soc/apple/aop_spkr.rs, sound/soc/apple/aop_spkr_ring.rs, and tools/asahi/aop-spkr-ring-tests.rs) are byte-identical to f043d55dda9c (git diff empty; only unrelated test-runner entries differ in tools/asahi/run-host-tests.py). Standalone exact-head J616C verification and suspend-resume / second-board rows remain queued.

@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) area:gpu Area from CODEOWNERS (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 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Merge readiness at f043d55d: not ready yet

Check Detail
CI at head ok 2 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 f043d55d
Conflicts ok not computed yet
PR text ok Risk: two-way · Apple AOP speaker playback path (sound/soc/apple/aopspkr.rs, aopspkrring.rs); headphone jack path (j616-jack-pcm-0) is unchanged.

Areas: area:audio, area:gpu

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

@AxxzyWasTaken

Copy link
Copy Markdown
Contributor Author

Converting to draft. On J616C, steady playback with this patch applied produces audible pops, some of them loud even at low volume. PipeWire reports no xruns and the browser's output stream is clean, so the glitch is added below PipeWire, in the TX path. My earlier verification only used silent cycles, which could not catch this. Until I've found and fixed it, please don't merge this or try it on speakers. J616C is back on the pre-patch driver meanwhile.

@AxxzyWasTaken
AxxzyWasTaken marked this pull request as draft November 28, 2026 22:15
@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 29, 2026
@AxxzyWasTaken

Copy link
Copy Markdown
Contributor Author

Closing in favour of #3619. On J616C this driver-owned TX ring caused audible pops during steady playback, which the muted cycle tests never showed. #3619 keeps the existing dmaengine playback path and fixes #2188 by stopping the TX DMA only after pw1 .

Assisted-by: LLM (Anthropic:Claude Opus 5.5)

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) area:gpu Area from CODEOWNERS (bot) door:two-way Revertible or not, from Merge risk (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

J614c: AOP firmware crashes at speaker start/stop, speakers dead until reboot

1 participant