Skip to content

ASoC: apple: t8152-aop-audio: serve speaker sense from a driver-owned ring - #3084

Merged
DjDeveloperr merged 5 commits into
mainfrom
fix/t8152-aop-sense-realign
Nov 26, 2026
Merged

DjDeveloperr merged 5 commits into
mainfrom
fix/t8152-aop-sense-realign

Conversation

@erikwb

@erikwb erikwb commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What: Move multi-amp speaker V/I sense capture in t8152_aop_audio onto a driver-owned cyclic ADMAC RX ring (SenseRing, matching aop_spkr.rs) armed only on a verified pw0 serializer stop before every pwrd transition and failing closed on unconfirmed power transitions, fixing arbitrary mid-frame channel slot rotation modulo 8/12 when userspace stops and restarts Speaker Sense while the speaker serializer is already running.
Tested: Arm64 QEMU KUnit snd_soc_apple_t8152_aop_audio* at exact head 5f1321879f34: 5/5 PASS (sense_ring_lap_detection, sense_ring_geometry_covers_8ch_and_12ch, sense_ring_fe_space_guards_overrun, spkr_state_requires_verified_pw0_before_rearm, spkr_state_suspend_pw0_failure_and_resume); arm64 defconfig + asahi.config sound/soc/apple/t8152_aop_audio.o build at 5f1321879f34: clean (0 warnings); tools/asahi/hardware-defaults-tests.py 6/6 PASS; scripts/checkpatch.pl --strict --git cf330a1c264c..5f1321879f34 0 errors/warnings/checks; git merge-tree --write-tree origin/main 5f1321879f34 clean (1e115101031b). J716c (16-inch M5 Max, T6050) one-shot trial boot of composed candidate a60c110f35e0 (1b09b76b53e9 + #3084 5f1321879f34 unchanged + #3085 31a8aca83305 unchanged + landed #2185/#2187, Stage 2 1.0.0b17-gae94218e, default cmdline): booted cleanly, GPU up, 18/18 gpubench-vk ok, 6/6 dEQP-VK.api.smoke.* PASS, speakerguardd v2 active & silent selfcheck PASS for that kernel, 0 new kernel errors (3 boot err lines also present on default), 1 pre-existing failed unit (aurora-peripherals.service), 0 AVC (SELinux N/A); speaker playback not run. Prior J716c live speaker validation ran at 24f591f91eff (matching the initial edf11ddd623b revision before b7028feb2f7a, cf330a1c264c, and 5f1321879f34, composed with #3085 and Stage 2 handoff: speakerguardd-selfcheck verify 4/4 PASS, 4/4 L/R 200 Hz bursts aligned with shift == 0, health_or = 0x0, 0,0,0 faults across all 6 amps, 0 SELinux denials, 0 new kernel errors).
Risk: two-way door · T8142 (M5 Air) and T6050 (M5 Pro/Max) multi-amp Macs (sense_ring = true); mono T8152 (sense_ring = false) keeps its direct DMA path unchanged.
Not yet: Live protected speaker playback (4/4 L/R 200 Hz bursts with shift == 0, health_or = 0x0, and live Speaker Sense stop/restart + serializer idle cycle under speakerguardd v2) at 5f1321879f34 — not run yet because speaker playback requires explicit machine-owner permission and active speaker sink routing. External T8142 (8-ch/12-ch) and T8152 (mono) regression rows on boards not in this lab (J716c T6050 stands in for the shared multi-amp SenseRing path; requesting T8142 and T8152 rows).

Details

Root cause

On T8142 and T6050 multi-amp speaker streams, the AOP firmware's serializer streams 8 or 12 16-bit V/I sense slots back over base-ns ADMAC RX with no hardware frame-start marker. Previously, the sense front-end owned the ADMAC RX channel directly and only realigned it when speaker playback prepared a stopped serializer. In practice, PipeWire stops, prepares, and starts Speaker Sense while the speaker serializer is already running in pwrd (both on normal stream start and on capture XRUN recovery), causing ADMAC RX to resume mid-frame at an arbitrary 16-bit word offset modulo the channel count and rotating V/I samples across amplifiers.

Design

Matches sound/soc/apple/aop_spkr.rs:

  • Cyclic SenseRing: 2.5 ms (120-frame) × 32 periods = 80 ms cyclic ADMAC RX ring, dynamically sized from cfg.sense_channels * 2 (1920 B period for 8-ch T8142, 2880 B period for 12-ch T8142-6amp/T6050 — both exact multiples of the ADMAC's 48 B RX FIFO threshold).
  • Stop vs callback synchronization (active guard): SenseRing::start() sets active (Ordering::Release) right before device_issue_pending(), SenseRing::stop() clears active (Ordering::Release) before device_terminate_all(), and sense_ring_done() checks active.load(Ordering::Acquire) both before and after device_tx_status(). Through adchan->lock release/acquire between admac_terminate_all() and admac_tx_status(), any callback that observes a terminated channel (current_tx == NULL, residue == 0) sees active == false and exits without delivering stale ring bytes.
  • Verified-pw0 serializer synchronization (SpkrState mutex):
    • SpkrState tracks pw0_verified alongside open and running. pw0_verified is set only after set_power(&self.spkr, POWER_STATE_PW0) (which checks PROP_POWER_STATE readback) succeeds in spkr_startup(), sense_startup(), spkr_stop_locked(), or spkr_prepare(), and is cleared before every pw0 or pwrd transition attempt.
    • SpkrState::start_with_ring() refuses to arm SenseRing (ring.start()) unless !running && pw0_verified, failing closed with -EIO otherwise so neither spkr_prepare() nor spkr_resume() can ever arm RX on a running or unconfirmed serializer.
    • If spkr_stop_locked() fails to verify pw0 , it leaves running set and pw0_verified = false and does not stop SenseRing DMA while the stream stays awake. be_hw_free() returns the error, though snd_soc_dai_hw_free() (sound/soc/soc-dai.c) returns void and discards it: DAPM PRE_PMD still proceeds to be_shutdown() -> spkr_shutdown(), which runs ring.release() while keeping running set on failure so the next spkr_startup() must verify pw0 , and an open stream that stays active re-verifies pw0 on the next spkr_prepare() or spkr_suspend().
    • asoc_suspend() (SpkrState::suspend()) also returns the pw0 error, which snd_soc_component_suspend() (sound/soc/soc-component.c) discards before marking the component suspended and proceeding with sleep. To prevent ADMAC registers cleared by firmware sleep from leaving RX un-rebuilt on wake, SpkrState::suspend() stops and synchronizes SenseRing DMA (ring.stop()) and records resume_run = was_running even when pw0 fails (keeping running set and pw0_verified = false). On wake, asoc_resume() (SpkrState::resume_with_ring()) runs ensure_pw0() before start_with_ring() so resume retries pw0 if running || !pw0_verified and either cleanly rearms RX or fails closed (propagating the pw0 errno, also discarded by snd_soc_component_resume(), without rearming RX or requesting pwrd).
    • On playback re-prepare while spkr is already running or !pw0_verified, spkr_prepare() calls mute_codecs() (propagating any error to abort before cutting BCLK) before cycling pw0 -> ring.start() -> pwrd. Playback drops SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME so playback DMA never resumes mid-frame.
  • DMA lifetime & PM: be_hw_free and be_shutdown stop the ring after verified pw0 ; be_shutdown releases the ring and ADMAC channel while the fabric is still powered; asoc_suspend / asoc_resume stop and restart cleanly.
  • VMALLOC sense FE & guards: Sense capture (fe == 2) uses SNDRV_DMA_TYPE_VMALLOC with BLOCK_TRANSFER | BATCH. sense_pcm_hw() constrains period_bytes_min to at least ring.period_bytes() (1920 B / 2880 B, 120 frames per period, with periods_min = 2 -> minimum 240-frame buffer) and updates periods_max to match, preventing ALSA from negotiating a buffer of one ring batch or less (such as two 60-frame periods). deliver() copies whole frames under the stream lock, checks strict free space (unread_alsa + pending + incoming < buffer_frames) to signal PCM_STATE_XRUN before overrunning unread data or creating an ambiguous full-buffer modulo wrap, and validates sense_ring_may_lap() both before and after the copy (with an acquire fence) before publishing fe_pos.
  • Mono T8152 unchanged: sense_ring: false preserves the existing direct-DMA path.

J716c candidate test composition

Because t6050-j716c.dts speaker/jack/mic/PMP enablement and apple,j716-macaudio v2-only protection live in #3085 and require a compatible Stage 2 pmp0/GPU/audio handoff, exact-head J716c hardware testing ran a composed candidate (a60c110f35e0) combining:

  • Base 1b09b76b53e9 with aurora-silicon/linux#3084 at 5f1321879f34 unchanged (edf11ddd623b, b7028feb2f7a, cf330a1c264c, 5f1321879f34)
  • aurora-silicon/linux#3085 at 31a8aca83305 unchanged (J716c DT, apple,j716-macaudio, apple-pmp)
  • Landed #2185 (58df51d0d5e5) and #2187 (af93a6e2d079) for GPU/display on 27.2 firmware, booted with Stage 2 1.0.0b17-gae94218e and default cmdline (note: aurora-silicon/aura#506 at f7f690fb6091 / 1.0.0b15-gf7f690fb predated the GPU/pmp0 handoff on this firmware, so Stage 2 1.0.0b17-gae94218e was used)
Evidence
  • Head SHA: 5f1321879f348f5c636f9b208ee816b29241b6a4 (4 commits on main d420ae2bd61a; git merge-tree --write-tree origin/main 5f1321879f34 clean at 1e115101031b)
  • arm64 QEMU KUnit (snd_soc_apple_t8152_aop_audio* at 5f1321879f34):
    • [PASSED] sense_ring_lap_detection
    • [PASSED] sense_ring_geometry_covers_8ch_and_12ch
    • [PASSED] sense_ring_fe_space_guards_overrun
    • [PASSED] spkr_state_requires_verified_pw0_before_rearm
    • [PASSED] spkr_state_suspend_pw0_failure_and_resume (5/5 passed, 0 warnings in t8152_aop_audio.rs)
  • Host checks at 5f1321879f34:
    • make ARCH=arm64 LLVM=1 ... sound/soc/apple/t8152_aop_audio.o (defconfig + asahi.config): clean (0 warnings)
    • ./scripts/checkpatch.pl --strict --git cf330a1c264c..HEAD: 0 errors, 0 warnings, 0 checks
    • python3 -I tools/asahi/hardware-defaults-tests.py: 6/6 passed
  • J716c (16-inch M5 Max, T6050, G17C B1, GPU fw 27.2) one-shot trial boot of composed candidate a60c110f35e0 (1b09b76b53e9 + #3084 5f1321879f34 unchanged + #3085 31a8aca83305 unchanged + landed #2185/#2187, Stage 2 1.0.0b17-gae94218e, default cmdline):
    • Booted cleanly (aurora,trial), GPU up (/dev/dri/renderD128), Hyprland up, gpubench-vk 18/18 ok, dEQP-VK.api.smoke.* 6/6 Pass, 0 GPU-error lines
    • speakerguardd v2: speakerguardd-v2.service active (AppleJ716: v2 interface only, V/I sense initialized: 12 switches enabled and read back, protection ready: every check holds; the protect stage idles until a stream starts), silent selfcheck (zero samples) PASS for that kernel (j716c, wiring cbba18ec53f0a442, params bfbbd9fc8008)
    • Regression checks: 0 new kernel errors (3 boot err+ lines also present on default: aurora 2302600000.gpu: M5 bare-metal bootstrap failed at session: EPROBE_DEFER and 2x systemd-gpt-auto-generator), 1 pre-existing failed unit (aurora-peripherals.service), 0 AVC (SELinux N/A)
    • Not run: live protected speaker playback (4/4 L/R 200 Hz bursts with shift == 0, health_or = 0x0, and live Speaker Sense stop/restart + serializer idle cycle under speakerguardd v2) at 5f1321879f34 (requires explicit machine-owner permission and active speaker sink routing)
  • Prior live J716c (16-inch M5 Max, T6050) at 24f591f91eff (initial edf11ddd623b revision before b7028feb2f7a, cf330a1c264c, and 5f1321879f34, composed with ASoC: apple: enable J716c (16-inch M5 Max) AOP audio, v2-only speakers, jack and mic array #3085 + Stage 2 handoff):
    • speakerguardd-selfcheck verify: 4/4 PASS (AppleJ716: v2 interface only, wiring cbba18ec53f0a442, 5 silent pilot starts, no xrun)
    • 4/4 L/R 200 Hz bursts aligned (shift == 0, health_or = 0x0) including a 13 s idle pwrd -> pw0 -> pwrd cycle; 0,0,0 faults across all 6 amps, 0 SELinux AVCs
  • Earlier J716c AID41 playback ladder at c3feb10d6265 and 6-burst + kill -9 probe at 26f1ac62bcbd (default cmdline, MTE on):
    • 10/10 L/R 200 Hz bursts aligned (shift == 0, sense_delay = 1600, health_or = 0x0); 0.3 / 0.6 / 1.0 (60 / 60 / 300 s, -6 dBFS pink noise, kill -9 at +150 s of 1.0): 39,762 rows, lost = 0, health_or = 0x0, 0,0,0 faults on all 6 amps
    • Aligned coil resistance across all 6 amps before kill -9 (3.895 / 3.884 / 3.917 / 3.928 / 3.421 / 3.428 ohm) and after kill -9 (4.008 / 3.996 / 4.015 / 4.040 / 3.426 / 3.452 ohm); re-granted in 1.254 s

… ring

On T8142 and T6050 multi-amp speaker streams, the AOP firmware's
serializer carries 8 or 12 16-bit V/I sense slots back on base-ns ADMAC
RX with no hardware frame-start marker. Previously, the sense front-end
opened the ADMAC channel directly and realigned it only when speaker
playback prepared a stopped serializer. When PipeWire stops, prepares
and restarts the sense front-end while the speaker serializer is already
running in pwrd (which happens deterministically on stream start and on
any capture XRUN recovery), the ADMAC RX channel restarts mid-frame at an
arbitrary 16-bit word offset modulo the channel count, misattributing V/I
sense samples across amplifiers.

Move multi-amp sense capture onto a driver-owned cyclic ring (SenseRing,
matching aop_spkr.rs):

- Arm the ADMAC RX ring on the stopped serializer immediately before
  every spkr pwrd transition so ring offset 0 is always slot 0 of a
  frame, and stop it after pw0 (releasing the ring and channel on spkr
  shutdown while the fabric is still powered).
- On a playback re-prepare while spkr is already running, mute the
  speaker codecs before cycling pw0 -> ring start -> pwrd (propagating
  any mute error to abort the clock transition so amps never see BCLK
  stop while active), and drop PAUSE/RESUME on playback so the playback
  DMA never resumes mid-frame either.
- Deliver whole-frame chunks in 2.5 ms (120-frame) periods from the
  80 ms ring to the attached VMALLOC sense front-end; signal XRUN before
  copying if the incoming frames would overrun unread capture data in
  the ALSA buffer, and re-check ring lap timing after the copy before
  publishing the updated front-end pointer.
- Keep the mono T8152 (sense_ring = false) path unchanged.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb
erikwb requested review from a team as code owners November 24, 2026 15:57
@erikwb
erikwb requested review from LiamRay11 and malik-na November 24, 2026 15:57
@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) blast:m5 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 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Merge readiness at cec55b37: not ready yet

Check Detail
CI at head running running: pr-text / pr-text
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head no no line with the head SHA cec55b37, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · T8142 (M5 Air) and T6050 (M5 Pro/Max) multi-amp Macs (sensering = true); mono T8152 (sensering = false) keeps its direct DMA path unchanged.

Areas: area:audio

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

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: HOLD at edf11ddd623b

Blockers:

  1. SenseRing::stop() vs sense_ring_done() stale-sense delivery race (sound/soc/apple/t8152_aop_audio.rs:419-435, 620-669): SenseRing::stop() calls device_terminate_all before device_synchronize kills the ADMAC tasklet. In drivers/dma/apple-admac.c, admac_chan_tasklet() drops adchan->lock before invoking sense_ring_done(), while admac_terminate_all() clears adchan->current_tx = NULL under adchan->lock. If sense_ring_done() calls admac_tx_status() in that window, admac_tx_status() takes the else branch (lines 674–682) and returns DMA_IN_PROGRESS with residue = 0. sense_ring_done() accepts residue == 0, computes now = (ring_bytes - 0) % ring_bytes = 0, and treats (ring_bytes - from) as newly captured whole frames — either silently copying stale/unwritten tail-of-ring data into the Speaker Sense buffer for speakerguardd whenever (ring_bytes - from) fits in the FE buffer (e.g. when from is near the end of the ring or on larger capture buffers) or raising a spurious XRUN. Fix: guard the ring with an active: AtomicBool set true before device_issue_pending and cleared false (Ordering::Release) in SenseRing::stop() before device_terminate_all, and bail out of sense_ring_done() when !ring.active.load(Ordering::Acquire) both before and after device_tx_status() (the adchan->lock release/acquire handoff guarantees sense_ring_done() sees active == false whenever admac_tx_status() observed the terminated channel).
  2. §5 exact-head hardware row: As noted in Not yet:, live J716c validation was run at 26f1ac62bcbd and c3feb10d6265 rather than at exact head edf11ddd623b. After fixing blocker 1, please record an exact-head J716c run at the new head commit including the full regression line (0 SELinux denials, 0 failed units, 0 new kernel errors).

Checked:

  • Full diff (sound/soc/apple/t8152_aop_audio.rs, +835/-89) against sound/soc/apple/aop_spkr.rs, drivers/dma/apple-admac.c, sound/soc/apple/macaudio.c, sound/soc/soc-pcm.c, and sound/soc/soc-core.c.
  • SenseRing geometry (120 frames × 32 periods = 80 ms; 1920 B for 8-ch T8142 and 2880 B for 12-ch T8142-6amp/T6050, both multiples of 48 B RX FIFO threshold), sense_ring_may_lap(), strict < sense_ring_has_fe_space() overrun/modulo-wrap guard, boundary wrap arithmetic, pre/post-copy lap checks, and KUnit coverage: sound.
  • SpkrState mutex serialization across sense_startup / spkr_startup / spkr_prepare / spkr_hw_free / spkr_shutdown / spkr_suspend / spkr_resume: clean lock ordering, mute_codecs() propagates mute errors before stopping BCLK on re-prepare, be_hw_free runs after ASoC digital mute, be_shutdown releases the ADMAC channel and ring before mca_down(), and playback drops PAUSE/RESUME on sense_ring = true boards.
  • Cross-SoC impact: mono T8152 (sense_ring = false) keeps its direct DMA path unchanged; T8142 (ap_mca = true) and T6050 (ap_mca = false) share the SenseRing path cleanly.
  • Policy & attribution: human author/committer (Erik Bourget) with valid Signed-off-by:, zero AI attribution, no banned names, no model gating or hardcoded safety limits, default cmdline, mergeable: MERGEABLE, CI green (host-tests, pr-text, readiness).
Evidence
  • Head SHA: edf11ddd623bf71e8d044148025eab45b949ef8c (state: OPEN, isDraft: false, mergeable: MERGEABLE)
  • CI at head: host-tests (success), pr-text / pr-text (success), readiness / readiness (success)
  • Race trace:
    • drivers/dma/apple-admac.c: admac_chan_tasklet drops adchan->lock before invoking sense_ring_done; admac_terminate_all sets adchan->current_tx = NULL and splices issued to to_free before admac_synchronize runs tasklet_kill; admac_tx_status then returns DMA_IN_PROGRESS with residue = 0 (active cyclic transfers always return residue in [1, buf_len] from admac_cyclic_read_residue).
    • sound/soc/apple/t8152_aop_audio.rs:645-668: residue == 0 passes residue > ring_bytes, yields now = 0, and delivers (ring_bytes - from) bytes from unwritten/prior-lap ring memory when (ring_bytes - from) / frame_bytes fits in the capture FE buffer.

@github-actions github-actions Bot added the changes-requested A reviewer requested changes (bot) label Nov 24, 2026
Clear SenseRing's new active flag (Release) before calling
device_terminate_all() in SenseRing::stop(), and check it (Acquire) both
before and after device_tx_status() in sense_ring_done().

admac_chan_tasklet() drops adchan->lock before invoking the callback,
while admac_terminate_all() clears adchan->current_tx under the same
lock before device_synchronize() kills the tasklet. If sense_ring_done()
called admac_tx_status() in that window, admac_tx_status() returned
DMA_IN_PROGRESS with residue = 0, which sense_ring_done() treated as a
wrap to ring offset 0 and delivered stale tail-of-ring frames (or a
spurious XRUN). Checking active after device_tx_status() pairs through
adchan->lock with the store in stop(), ensuring sense_ring_done() bails
out whenever admac_tx_status() saw the terminated channel.

Signed-off-by: Erik Bourget <erik@ebourget.net>
Require at least one SenseRing period (SENSE_RING_PERIOD_FRAMES) per
ALSA period on the VMALLOC sense front-end, updating periods_max to
match. With periods_min = 2, ALSA negotiation rejects buffers too small
to fit a single ring batch (such as two 60-frame periods), preventing
immediate XRUNs on the first callback, and covers the smallest admitted
buffer in KUnit.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb

erikwb commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Note

🤖 Gemini 4 Hydrogen responding on behalf of erikwb

Fixed the review findings across b7028feb2f7a and cf330a1c264c (sound/soc/apple/t8152_aop_audio.rs), and updated the PR body.

  1. SenseRing::active guard (b7028feb2f7a): Added active: AtomicBool (initialized false), set true (Ordering::Release) immediately before device_issue_pending() in SenseRing::start(), cleared false (Ordering::Release) before device_terminate_all() in SenseRing::stop(), and checked with Ordering::Acquire in sense_ring_done() both before and after device_tx_status(). Because admac_terminate_all() and admac_tx_status() serialize on adchan->lock in drivers/dma/apple-admac.c, any sense_ring_done() call whose status read observes the terminated channel (current_tx == NULL, residue == 0) now sees active == false and exits before touching done or delivering frames.
  2. Sense FE buffer geometry constraint (cf330a1c264c): Added sense_pcm_hw() so sense_open() sets period_bytes_min to at least ring.period_bytes() (1920 B for 8-ch, 2880 B for 12-ch — 120 frames per period) and updates periods_max to match. With periods_min = 2, ALSA negotiation requires at least a 240-frame buffer and rejects geometries too small to fit a single 120-frame ring batch (such as two 60-frame periods). Updated sense_ring_fe_space_guards_overrun in KUnit to cover the smallest admitted buffer across HW_CFG_T8142, HW_CFG_T8142_6AMP, and HW_CFG_T6050.
  3. Tested-SHA attribution in PR body: Updated **Tested:**, **Not yet:**, and Evidence to cite 24f591f91eff as the archived J716c build SHA (noting that it carries the matching sound/soc/apple/t8152_aop_audio.rs revision of edf11ddd623b, without b7028feb2f7a or cf330a1c264c), and made clear that exact-head J716c verification at cf330a1c264c is pending.
  4. Checks at cf330a1c264c: arm64 QEMU KUnit snd_soc_apple_t8152_aop_audio* 3/3 PASS (0 warnings in t8152_aop_audio.rs), arm64 sound/soc/apple/t8152_aop_audio.o build clean, tools/asahi/hardware-defaults-tests.py 6/6 PASS, and scripts/checkpatch.pl --strict --git edf11ddd623b..HEAD 0 errors/warnings/checks.

@github-actions github-actions Bot added the ci-failing A check failed at the PR head (bot) label Nov 24, 2026
DjDeveloperr
DjDeveloperr previously approved these changes Nov 25, 2026

@DjDeveloperr DjDeveloperr left a comment

Copy link
Copy Markdown
Contributor

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 cf330a1c264c. The code finding is fixed.

Rows before merging: A protected J716c (T6050) run at this head and a T8142 run for the shared speaker-sense path. The posted J716c run predates both fixes.

Checked: The ring is marked inactive before terminate, and the callback checks again after reading DMA status. It drops the terminated channel’s zero-residue result. The second commit makes the minimum capture buffer large enough to hold a full DMA ring period.

@DjDeveloperr
DjDeveloperr dismissed their stale review November 25, 2026 01:10

Superseded by the review at cf330a1

@github-actions github-actions Bot removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Nov 25, 2026
…nse ring

Do not clear spkr running state or stop SenseRing DMA when a pw0
transition fails in spkr_stop_locked() (except on shutdown, which still
releases DMA resources while keeping running set). On suspend, still
stop and synchronize SenseRing DMA and record resume_run even when pw0
fails, because snd_soc_component_suspend() discards the callback error
and proceeds with sleep.

Track pw0_verified in SpkrState, clear it before every pwrd attempt so
an unconfirmed pwrd failure forces a fresh verified pw0 before rearming,
have resume_with_ring() verify pw0 via ensure_pw0() when running or
unverified before rearming SenseRing, and fail closed in both
resume_with_ring() and start_with_ring() if pw0 still fails so neither
prepare nor resume can arm RX on a running or unverified serializer.

Add KUnit coverage in spkr_state_requires_verified_pw0_before_rearm and
spkr_state_suspend_pw0_failure_and_resume.

Signed-off-by: Erik Bourget <erik@ebourget.net>
@erikwb

erikwb commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Note

🤖 Gemini 4 Hydrogen responding on behalf of erikwb

Addressed all findings on top of cf330a1c264c in 5f1321879f34 (sound/soc/apple/t8152_aop_audio.rs) and updated the PR description:

  1. Require verified pw0 before rearming SenseRing and handle suspend / hw_free error paths (5f1321879f34):
    • Added pw0_verified to SpkrState and extracted the state-machine transitions onto SpkrState (ensure_pw0(), start_with_ring(), stop(), prepare_with_ring(), suspend(), resume_with_ring()), with spkr_pw0() logging pw0 failures.
    • SpkrState::suspend() now stops and synchronizes SenseRing DMA (ring.stop()) and records resume_run = was_running even when pw0 fails (while keeping running set and pw0_verified = false), because snd_soc_component_suspend() (sound/soc/soc-component.c) returns void and discards asoc_suspend()'s errno before proceeding with sleep.
    • SpkrState::resume_with_ring() calls ensure_pw0() before start_with_ring(), so after a failed pw0 on suspend it retries pw0 on resume: cleanly rearming RX if pw0 succeeds, or failing closed (propagating the pw0 errno, also discarded by snd_soc_component_resume()) without rearming RX or requesting pwrd if pw0 still fails.
    • spkr_stop_locked() (SpkrState::stop()) keeps running set, pw0_verified = false, and SenseRing untouched when pw0 fails; be_hw_free() returns the errno, which snd_soc_dai_hw_free() (sound/soc/soc-dai.c) returns void and discards (DAPM PRE_PMD still proceeds to be_shutdown() -> spkr_shutdown(), which calls ring.release() while keeping running set on failure, and an open stream re-verifies pw0 on the next spkr_prepare() or spkr_suspend()).
    • SpkrState::start_with_ring() clears pw0_verified before set_power(..., POWER_STATE_PWRD) and refuses (-EIO) to arm SenseRing whenever running || !pw0_verified.
    • Added KUnit coverage in spkr_state_requires_verified_pw0_before_rearm and spkr_state_suspend_pw0_failure_and_resume.
  2. Test composition & J716c candidate boot in PR body:
    • Updated **Tested:**, **Not yet:**, Details, and Evidence to record the J716c one-shot trial boot of composed candidate a60c110f35e0 (1b09b76b53e9 + aurora-silicon/linux#3084 5f1321879f34 unchanged + aurora-silicon/linux#3085 31a8aca83305 unchanged + landed #2185/#2187, booted with Stage 2 1.0.0b17-gae94218e and default cmdline), and to keep live protected speaker playback (4/4 L/R 200 Hz bursts with shift == 0, health_or = 0x0, and live Speaker Sense stop/restart + serializer idle cycle under speakerguardd v2) at 5f1321879f34 in **Not yet:** (requires explicit machine-owner permission and active speaker sink routing) alongside external T8142/T8152 rows.
  3. Checks at 5f1321879f34:
    • arm64 QEMU KUnit snd_soc_apple_t8152_aop_audio*: 5/5 PASS (sense_ring_lap_detection, sense_ring_geometry_covers_8ch_and_12ch, sense_ring_fe_space_guards_overrun, spkr_state_requires_verified_pw0_before_rearm, spkr_state_suspend_pw0_failure_and_resume).
    • arm64 defconfig + asahi.config sound/soc/apple/t8152_aop_audio.o: clean (0 warnings).
    • tools/asahi/hardware-defaults-tests.py: 6/6 PASS; scripts/checkpatch.pl --strict --git cf330a1c264c..HEAD: 0 errors, 0 warnings, 0 checks; git merge-tree --write-tree origin/main 5f1321879f34: clean (1e115101031b).
    • J716c (16-inch M5 Max, T6050) one-shot trial boot of composed candidate a60c110f35e0 (Stage 2 1.0.0b17-gae94218e, default cmdline): booted cleanly, GPU up, 18/18 gpubench-vk ok, 6/6 dEQP-VK.api.smoke.* PASS, speakerguardd v2 active & silent selfcheck PASS for that kernel, 0 new kernel errors (3 boot err lines also present on default), 1 pre-existing failed unit (aurora-peripherals.service), 0 AVC (SELinux N/A); live speaker playback not run (requires explicit machine-owner permission and active speaker sink routing).

@github-actions github-actions Bot added the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 26, 2026
@erikwb

erikwb commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

PASS: J716c (16-inch M5 Max, T6050), live protected speaker playback at #3084 head 5f1321879f34, one-shot trial boot of composed candidate a60c110f35e0 (1b09b76b53e9 + #3084 5f1321879f34 unchanged + #3085 31a8aca83305 unchanged + landed #2185/#2187), Stage 2 1.0.0b17-gae94218e, default cmdline, speakerguardd v2 active and gating throughout.

  • speakerguardd-selfcheck verify: 4/4 PASS (kernel, hardware, voicing, graph)
  • L/R 200 Hz bursts: 4/4 aligned, shift == 0 (identical repeat run: 4/4 again, 8/8 total)
  • health_or = 0x0; 0,0,0 faults on all 6 amps before and after; 0 protection events
  • Live Speaker Sense stop/restart with the speaker serializer already up, then a serializer idle cycle; bursts after both stay aligned (shift == 0)
  • 0 SELinux denials (SELinux N/A, 0 AVC); 0 new kernel errors (same 3 err lines as the default boot); failed units same as the default boot: aurora-peripherals.service (known) and systemd-networkd-wait-online.service (also on the default boot)
  • Default restored afterwards: default kernel and Stage 2, no one-shot pending

Not covered here: T8142 and mono T8152 rows.

Evidence
  • Build: a60c110f35e0 rebuilt from the same tree and kernel config as the earlier candidate boot. 5f1321879f34 is an ancestor, and git diff 5f1321879f34 a60c110f35e0 -- sound/soc/apple/t8152_aop_audio.rs is empty. /proc/version on the Mac matches the built image, aurora,trial was set, and the default Stage 2 was unchanged.
  • verify: kernel pass (AppleJ716: v2 interface only); hardware pass (wiring cbba18ec53f0a442); voicing pass; graph pass (512-frame quantum locked; 5 silent pilot starts, no xrun on sgv2.sense.j716, sgv2.speaker.j716).
  • Burst plan and levels (same as the 24f591f91eff run): 200 Hz sine, amplitude 0.20, 2.2 s; L (0 s), R (+1 s), L (+13 s idle), R (+1 s); default protected sink sgv2.voicing.j716 at slider 0.45, put back to its previous setting afterwards. Each burst has 345 to 377 rows of V/I telemetry from the speakerguardd engine, with health 0x0 on every row in both runs; run 1 engine summaries show lost = 0, gaps = 0.
run burst side wait V/I energy on other amps health
1 b1 L 0 s left woofers (amps 0, 2) 0.0 0x0
1 b2 R 1 s right woofers (amps 1, 3) 0.0 0x0
1 b3 L 13 s left woofers (amps 0, 2) 0.0 0x0
1 b4 R 1 s right woofers (amps 1, 3) 0.0 0x0
2 b1 to b4 L, R, L, R 0, 1, 13, 1 s same split 0.0 0x0
  • Speaker Sense restart timing (ALSA substream status sampled every 0.2 ms, repeat run):
    • b1: Speaker Sense starts with the speaker closed (trigger 213.2643 s). The speaker substream reaches PREPARED, and its prepare brings the serializer up. Then Speaker Sense stops and restarts (trigger 213.2751 s) while the speaker is still PREPARED, and the speaker starts at 213.2858 s.
    • b3, after the idle cycle: the same sequence. Speaker Sense restarts at 235.5357 s, within one sample of the speaker reaching PREPARED, and the speaker starts at 235.5464 s.
    • b2 and b4 run on the streams that b1 and b3 left running.
    • Idle cycle: between b2 and b3 both substreams were closed for about 9.8 s, and protection was released and re-granted.
  • Kernel log: the 3 err lines are the same on the trial and the default boot (aurora 2302600000.gpu: M5 bare-metal bootstrap failed at session: EPROBE_DEFER, 2x systemd-gpt-auto-generator). 0 AVC on both boots.
  • No audio was recorded, and no microphone or camera was used.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 5f1321879f34 once one T8142 row covers the shared speaker-sense path (Dj's remaining item)

Blockers: none in the code.

Checked (delta cf330a1c..5f132187, the pw0 commit):

  • The ring is armed only from start_with_ring(), and only when !running && pw0_verified. Otherwise it returns EIO and never arms RX on a running or unconfirmed serializer.
  • pw0_verified is cleared before every pwrd attempt and every pw0 attempt, and set only after a pw0 succeeds. A failed pwrd leaves the ring stopped and forces a fresh pw0. In prepare_with_ring() that path calls quiesce() (amps muted) first.
  • A failed pw0 in stop() keeps running set and leaves the sense ring running. That is the safe direction: if the serializer may still be clocking, protection keeps seeing V/I.
  • resume_with_ring() only re-arms when the stream was running at suspend and is still open. It re-verifies pw0 first.
  • The two new KUnit cases drive the failure orders: a failed pwrd, then a re-prepare; a failed pw0 at suspend, then resume. hw_free and suspend errors now reach ASoC instead of being dropped.
  • Exact-head J716c run with protection active and gating throughout: speakerguardd-selfcheck verify 4/4, L/R bursts aligned with shift == 0.

Non-blocking:

  • In suspend(), a failed pw0 still stops the sense ring while running stays set, and nothing mutes the amps. ALSA has already suspended the PCM by then, so samples have stopped. A quiesce() (or amp mute) in that error branch, before ring_stop(), would still remove any window where the serializer runs without sense.
  • spkr_shutdown() discards the stop error and then releases the ring. The commit message says why. A dev_warn there would make the case visible in logs.

Still needed: a T8142 run of the shared sense path at this head: protected playback with speakerguardd v2 gating, selfcheck PASS, and a sense stop/restart while the serializer runs (the case this PR fixes). Our lab has no M5 Air. @m0d16l14n1, could your J815, or a J813, run it if it fits your next batch?

@github-actions github-actions Bot removed the needs-review No approving review from Core or Omarchy Mac yet (bot) label Nov 26, 2026
@m0d16l14n1

Copy link
Copy Markdown
Contributor

PASS: J815 (MacBook Air 15" M5, T8142, 6 amps / 12 sense lanes), live protected speaker playback with #3084 at 5f1321879f34. Only t8152_aop_audio.rs differs from the running kernel (that file in the running kernel is identical to this PR's base d420ae2b), so the stock module is the "before" arm. speakerguardd v2 was active and gating throughout, on the default cmdline.

stock (base) #3084 5f1321879f34
bursts with V and I on the same amps, every lit amp 2.5–6.5 Ω 7/14 14/15
lit patterns across the run 8 different ones ([0,2], [1,3], [3,5], [2,4], …) 2, disjoint: L [0,4], R [1,5]
V/I of lit amps 3.1 Ω up to 197 Ω 3.2–5.2 Ω
health_or 0x0 0x0
  • Before: the lane rotation reproduces at stream starts on the J815. Bursts land on the wrong amps, and with an odd slot shift V is paired with another amp's I (16–197 Ω on a ~4 Ω coil).
  • After: every burst lands on the same amp pair (L 0+4, R 1+5). This covers 4 cold starts after 8 s of idle (serializer released, so PipeWire restarts the speaker PCM and then Speaker Sense), 1 warm start, and a 300 ms PipeWire freeze in the middle of a playback (both PCMs xrun and restart, and speakerguardd drops and re-grants protection). Bursts before and after the freeze are on the same amps.
  • The one non-matching burst (start 3, R) is a resolution artifact, not a rotation. V is on [1,5] at 4.6/4.5 Ω. Amp 4's current reads 2e-5 against a peak of 4e-5 on a quiet burst, at the engine page's quantisation floor, so it crossed the 35 % "lit" threshold.
  • speakerguardd-selfcheck verify: 4/4 PASS on the ASoC: apple: t8152-aop-audio: serve speaker sense from a driver-owned ring #3084 module.
  • 0 failed units; 0 new kernel errors (the run logs only protection granted/released lines); SELinux N/A on this image.
  • Non-blocking: on the very first stream after the live module swap, the engine page showed ~30 incomplete rows (NaN V/I) for under a second, then normal. Later starts did not show it.

Not covered here: mono T8152; suspend/resume with a stream open (the pw0 rearm path).

Evidence
  • Build: sound/soc/apple/t8152_aop_audio.rs from 5f1321879f34 built against the running J815 kernel tree (same config). Module build-id 2c51d532490f; stock module 3c8c8184baa4. The swap was live: PipeWire, speakerguardd v2 and snd-soc-macaudio stopped, the module replaced, everything restarted, and speakerguardd re-ran its gate and self-check.
  • Method: each playback is a 200 Hz burst at -20 dBFS, 0.6 s on the left only and then on the right only, at volume 0.25. Sense is read from speakerguardd's engine page (per-amp V and I mean squares, health), not from PipeWire, because a recorder linked to the sense node makes speakerguardd withdraw protection. For each burst: which amps carry V, which carry I, and √(V/I) per lit amp. Lanes rotated by an odd slot count pair V with the wrong I (ohms far from ~4); an even count moves the burst to other amps.
  • Phases: start1–4 (each after 8 s idle), warm (1 s after), xrun (L/R bursts, SIGSTOP of pipewire for 300 ms under a -50 dBFS pilot, then L/R again).
  • sense_delay seen during the ASoC: apple: t8152-aop-audio: serve speaker sense from a driver-owned ring #3084 run: -1, 192, 320.

@github-actions github-actions Bot added needs-hw-row No hardware row at the PR head (bot) and removed ci-failing A check failed at the PR head (bot) labels Nov 26, 2026
@DjDeveloperr
DjDeveloperr merged commit 71806e8 into main Nov 26, 2026
3 of 7 checks passed
@RyanTheTide
RyanTheTide deleted the fix/t8152-aop-sense-realign branch November 27, 2026 13:21
@betizzel

Copy link
Copy Markdown
Contributor

J714s (MacBook Pro 14" M5 Pro, T6050) row for this PR, under speakerguardd v2 (1.0.0-alpha.1, spp3, 12 sense words).

Kernel: our pin 7f00dc6bc52c with the four commits of this PR applied unchanged (edf11ddd623b, b7028feb2f7a, cf330a1c264c, 5f1321879f34), so t8152_aop_audio.rs is byte-identical to main's. Built natively with clang 23.1.5 and Rust 1.98.1. On first boot, speakerguardd's silent switch test passed in 3.4 s.

How measured: a 4 kHz tone at -22 dBFS on one side, through the normal protected sink. I recorded the protect stage's input from its monitor (6 speaker words + 12 sense words) and found which V/I pair carries the tone, compared with the self-check's wiring (shift 0 is right). To force restarts, I stalled PipeWire for 150 ms (SIGSTOP/SIGCONT). That makes the speaker PCM xrun, speakerguardd releases and re-grants, and PipeWire restarts both PCMs.

Kernel Sense shift in steady stretches Amp fault status
before (our pin) 2, 3, 4, 6, 10; each held until the next restart 0 on all six
with this PR 0 in every steady stretch, two runs, 6 forced restarts 0 on all six

Before, this was audible in normal use: audio from a video sounded dull and one side dropped out, because the protection was modelling each speaker from another amplifier's V/I.

One leftover: after 2 of the 6 forced restarts, the sense read a wrong shift for under a second (4 for about 0.5 s, then 2 for about 0.8 s). It then returned to 0 with no further PCM trigger: trigger_time of pcm1p/pcm2c didn't change. Whether the sense or the speaker started first didn't predict it (once the capture started 93 ms ahead, once both started within 0.2 ms). A short burst of stale or misaligned frames delivered right after the restart, before the ring settles, would explain it, but I haven't traced it. Our analysis windows are 0.1-0.25 s, so a shorter event could be missed.

Thanks for this. The protection on our J714s is reading its own speakers again.

@TripleU613

Copy link
Copy Markdown
Contributor

Follow-up J714s row, now that these four commits ship in our package (kernel v272-27, still on pin 7f00dc6bc52c with the commits unchanged). Booted 2026-11-29, under speakerguardd v2, measured the same way as above but with a 2.5 kHz tone at -34 dBFS and 0.25 s windows:

Case Sense shift Amp fault status
10 s tone, PipeWire stalled 150 ms at 3 s and 6.5 s (two forced restarts) 0 in every window from 0.50 s to 9.75 s 0 on all six
three fresh starts (amps idled out first), left side 0 in every steady stretch 0 on all six
right side 0 0 on all six

On the leftover from my earlier row (a wrong shift for under a second after 2 of 6 forced restarts): this run showed none. Both forced restarts stayed inside one unbroken shift-0 stretch at 0.25 s resolution. That's only two restarts, so it doesn't rule the transient out, and anything shorter than a window would still be missed.

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:m5 Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants