Skip to content

ASoC: apple: AOP LPAI: xrun on lost samples, wait for the first report on a completion - #2350

Open
RyanTheTide wants to merge 6 commits into
mainfrom
hack/aop-audio-b
Open

RyanTheTide wants to merge 6 commits into
mainfrom
hack/aop-audio-b

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What: ASoC: apple: AOP LPAI: xrun on lost samples, wait for the first report on a completion. This PR now carries these drafts as one feature (merge commits, the commits themselves unchanged):

Tested: each part as tested on its own PR (folded bodies below; no hardware row on this head yet). Combined with current main (ba29027) together with the other folds of 11-25: full arm64 build (Image, modules, dtbs; fleet89 config, clang 22, rustc 1.97.1) clean.

Risk: two-way · Neo (T8140 AOP audio) and AOP mic Macs

Not yet: a silent low-power mic capture row on the Neo on this head.

#2350 original description

Problem

The low-power microphone (lpai, capture PCM 1 of the AOP card) kept capturing when its producer could not be followed (sound/soc/apple/aop_lpai.rs at 0abc2f3):

  • T6041 ring: a producer that lapped the consumer or stepped back was taken as the new position (lpai ring position lost, continuing at the producer, :835).
  • T8112/T6000 inline reports: a report that started after the last copied byte was copied as if it were contiguous (lpai stream gap, continuing at the new position, :907).
  • A report larger than the ALSA buffer was dropped (:914).

In all three cases the application got a stream with a hole in it while the PCM state stayed RUNNING. The only trace was a one-time warning per driver instance.

Change

Lost capture samples are an overrun. Each of these cases now calls snd_pcm_stop_xrun(). The application sees -EPIPE (arecord prints overrun!!!) and recovers with prepare, which already resets the consumer so it restarts at the next producer position. These cases are unchanged:

  • The first report after a start still only sets the position.
  • A whole stale inline report is still skipped, since it carries nothing new.
  • The warnings stay, once per class, and now give the producer and consumer positions.

Locking: the xrun is raised under the stream-state spinlock. The trigger callback, which runs under the ALSA stream lock, never takes that spinlock. snd_pcm_period_elapsed() already uses the same order on this path, and sync_stop takes the state lock without the stream lock, so the change adds no new lock dependency.

The consumer steps now name the loss (RingStep::Start/Lost, Continuity::Gap), and the KUnit cases follow.

The T8140 low-power microphone is in aop_t8140.rs and is not affected.

Testing

  • make O=... sound/soc/apple/aop_audio.o (arm64, LLVM, release config) with CONFIG_KUNIT=y, so the snd_soc_apple_aop_lpai KUnit suite is compiled: no warnings. The suite was not run: it needs an Apple arm64 kernel booted under KUnit, which is not cheap on the build host.
  • Lab captures on T6000, T8112 and T6041 (11-10 and 11-12) delivered exactly 10 s of samples with none of these warnings, so normal captures never reach the changed paths.

Still to run

Capture only, nothing kept:

  • M1 Pro (T6000 J316s): arecord -D hw:AppleJ316HPAI,1 -f S16_LE -r 16000 -c 3 -d 10 -t raw | wc -c returns 960000, with no overrun!!!.
  • M2 (T8112 J413): the same with hw:AppleJ413HPAI,1, which returns 960000.
  • J616s (T6040): arecord -D hw:AppleJ616HPAI,1 -f S32_LE -r 16000 -c 3 -d 10 -t raw | wc -c returns 1920000.
  • On each Mac, dmesg | grep -i 'lpai.*overrun' is empty. A/B: before and after are both 3/3 clean runs of 10 s. Any overrun line now matches an xrun that arecord reports, where it used to be a silent hole.
Hardware rows (combined test kernel, 2026-11-20)

This change was booted in a combined test kernel: integration/aurora 0abc2f3 plus the open gate-sweep drafts plus this and the related drafts (7.3.0-aurora-hk2/pwr1/hk3). It ran once on each lab Mac (M1 Pro J316s, M2 J413, J616s, Neo J700) and was compared against the production kernel on the same Mac, using the same silent row script:

  • kernel errors, failed units and SELinux denials;
  • microphone capture byte counts;
  • camera frame counts;
  • USB speeds;
  • panel brightness steps;
  • every retry/refusal message these drafts touch;
  • 2 s2idle cycles on M1 Pro, M2 and J616s.

M1 Pro, M2 and J616s matched production on every row, apart from the new counters and lines the drafts add. The Neo row found the old-DTB display regression that #2358's follow-up commit fixes.

This PR: lpai lost-sample and overrun lines: 0 on all four Macs, before and after. Low-power mic capture byte counts identical.

#2404 original description

Problem

sound/soc/apple/aop_t8140.rs:867 (07084c3): before a leaf power request, the driver runs the low-power microphone to raise the audio fabric. It then polls a flag every 5 ms, up to 100 times, until the first report shows the fabric is up. That report is a firmware message the driver already handles.

Change

The report path signals a Completion on the first report after a run request, and the raise waits on it with the same 500 ms timeout. If the report has already arrived, the raise returns at once. A raise that receives the completion hands it on to any raise waiting beside it, and the next run request drops any token left over. This is T8140 only, because the file is the T8140 backend.

Testing

  • make sound/soc/apple/aop_audio.o (Rust, release config): builds, no warnings.

Still to run

  • Neo (T8140 J700): play on the speakers and record from the internal mic and the jack, three times each, from a cold start of each service. dmesg must show no lpai: no report while raising the fabric and no PMGR leaf timeout. The audio must start no later than before.
  • Neo: repeat after s2idle three times (rtcwake -d rtc0 -m freeze -s 20).

…pletion

Before a leaf power request, the low-power microphone is run to raise
the audio fabric, and its first report proves the fabric is up. The
driver waited for that report by polling a flag every 5 ms, 100 times.

The report is a firmware message the driver handles. Complete a
completion from the report path on the first report after a run
request, and wait on it for the same 500 ms. A raise that finds the
report already in returns at once; one that gets the completion hands
it on to a raise waiting beside it, and the next run request drops any
left over.

Fixes: 07084c3 ("ASoC: apple: t8140-aop-audio: raise the fabric before every leaf request")
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
…lost samples

When the low-power microphone's producer could not be followed, the
capture went on as if nothing happened: a T6041 ring producer that
lapped the consumer or stepped back was taken as the new position
("lpai ring position lost, continuing at the producer"), an inline
T8112/T6000 report that started after the last copied byte was copied
on as if contiguous ("lpai stream gap, continuing at the new
position"), and a report larger than the ALSA buffer was dropped. The
application got a stream with a hole in it, the PCM status showed
nothing, and the one-time warning was all that remained.

Lost capture samples are an overrun. Stop the stream with
snd_pcm_stop_xrun() in each of these cases, so the application sees
-EPIPE and restarts it with prepare, which already resets the consumer
to start at the next producer position. The first report after a start
still only sets the position. A whole stale inline report is still
skipped: nothing is lost there.

The xrun is raised under the stream state lock, which the trigger
callback never takes, the same order snd_pcm_period_elapsed() already
uses on this path. The consumer steps now name the loss (RingStep::Lost,
Continuity::Gap) and the KUnit cases follow.

Fixes: fbec62e ("ASoC: apple: aop_audio: capture the low-power microphone on T8112 and T6000 (#1493)")
Fixes: 6f17cbf ("ASoC: apple: aop_audio: capture the low-power microphone on T6041 (#1505)")
Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Blockers: 1.

  1. sound/soc/apple/aop_lpai.rs:852-870: the T6041 ring path still loses samples silently in one case. ring_step() returns Copy(len) for any len < ring_bytes, but nothing compares len with st.buffer_bytes. When one report announces at least a full ALSA buffer, which the 768000-byte ring easily allows against a small buffer, the loop at :861 wraps dst modulo buffer_bytes and overwrites what it just copied. advance() then moves the pointer by len % buffer_bytes, so the core never sees the lost whole buffers and the PCM stays RUNNING with a hole in it. That is exactly the failure this PR turns into an xrun everywhere else. The inline path already checks for it at :939. Add the same check before the copy: if len >= st.buffer_bytes { warn_once(WARN_LOST); Self::overrun(&st); return Ok(()); }. Then add a KUnit case for it, or move the decision into ring_step() so the existing test covers it.

The rest is correct:

  • pcm_trigger (:1039) and pcm_pointer (:1064) never take stream. That makes snd_pcm_stop_xrun() under the state lock the same order as the existing snd_pcm_period_elapsed() (:892).
  • The STOP that snd_pcm_stop_xrun() runs clears running, so later reports return at :828/:914 until prepare resets copied (:1022).
  • Start and Stale are unchanged.
  • The commit carries only a human Signed-off-by, and the Fixes tags are correct.
  1. Testing: the edited snd_soc_apple_aop_lpai KUnit suite was compiled but not run. Run it under tools/testing/kunit/kunit.py run --arch=arm64 (a 16K-page arm64 config runs in about 90 s on the build host). The changed decisions (Lost, Gap) are only proven there, because no hardware capture reached them.

HW row: the combined-kernel row (0 overrun lines, byte counts unchanged on M1 Pro, M2 and J616s) proves no regression on clean captures, so it covers the normal path. After the fix, add the KUnit run (all cases pass, including the new full-buffer ring case). Also rerun the J616s 3 x 10 s low-power capture with a 20 ms ALSA buffer (arecord --buffer-time=20000): 1920000 bytes each and no overrun!!!, or an overrun!!! that arecord recovers from, matched by one dmesg line.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review at 0f7dc0d. Reporting an xrun rather than treating a gap as contiguous fixes the capture boundary. Please verify prepare/start after that xrun clears the missed-sample state, with both late inline T8112 reports and the separate DMA format. The ordinary 10-second capture remains the positive control. No playback is needed.

…rst lpai report on a completion

Folds #2404 into this PR unchanged.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Row: Neo (J700, T8140 AOP audio), silent capture. Combined one-shot kernel 7.3.0-aurora-dsw2 (main 2a173ca + this PR's head c225627 + 14 other drafts; none of the others touches sound/soc/apple/). The low-power mic (plughw:0,1, 16 kHz mono S16) was captured for 3 s before and after one 15 s s2idle: rc 0, 96000 bytes, 0 overruns, 67735/67702 non-zero bytes. The same as on the edge kernel (96000 bytes, 0 overruns). No lpai/aop error lines, 0 WARN/BUG. The xrun-on-lost-samples paths need a lapped or gapped producer, which a normal capture doesn't make. The Neo went back to its saved edge kernel, verified.

On the T6041 ring path, ring_step() accepted any producer step shorter
than the 768000-byte ring. A step of a whole ALSA buffer or more then
wrapped the copy over the bytes it had just written, and advance()
moved the pointer by the step modulo the buffer, so the core never saw
the lost buffers and the PCM stayed RUNNING with a hole in it. The
inline report path already turns that case into an overrun.

Decide it in ring_step(): a step of at least the ALSA buffer is Lost,
like a lapped or backwards producer, so the stream stops with an xrun.
The overrun message gives the buffer size too. A KUnit case covers a
step just under, at, and well over a 640-byte buffer.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Draft sweep 12-05: the 11-21 blocker is fixed and the branch is current.

  • Merged main @ f02bd68 (bd1a89b, clean).
  • c01ff28: the T6041 ring path now treats a producer step of a whole ALSA buffer or more as lost, decided in ring_step() (new buffer_bytes argument), so it stops with an xrun like a lapped/backwards producer instead of wrapping the copy over itself. The overrun line now also prints the buffer size. New KUnit case a_step_of_a_full_alsa_buffer_is_lost (639/640/38400 bytes against a 640-byte buffer).
  • KUnit run (ask 2 of the review): arm64 QEMU, LLVM, Rust with the layout-corrected bindgen, aop.kunitconfig + SND_SOC_APPLE_AOP_AUDIO: snd_soc_apple_aop* 18/18 pass, including snd_soc_apple_aop_lpai with the new case and the existing Lost/Gap decisions.
  • The 11-23 review's prepare/start-after-xrun check and the J616s 3 x 10 s capture with a 20 ms buffer are queued as one silent-capture row in HWROWS-QUEUE (no playback). The Neo row from 11-29 covers the normal path.

Out of draft for review; the queued row is the remaining evidence.

@RyanTheTide
RyanTheTide marked this pull request as ready for review December 5, 2026 01:20
@RyanTheTide
RyanTheTide requested review from a team as code owners December 5, 2026 01:20
@github-actions github-actions Bot added area:audio Area from CODEOWNERS (bot) blast:neo Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 5, 2026
@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 Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Merge readiness at 5f18911e: not ready yet

Check Detail
CI at head running running: apple-build, KUnit macaudio
Review no approved by aurora-silicon-review, but GitHub does not count it yet (code owner from Core, Omarchy Mac or Aurora Maintainers needed, or approval went stale)
Requested changes ok none
Hardware row at head ok comment at 5f18911e
Conflicts ok none
PR text ok Risk: two-way · Neo (T8140 AOP audio) and AOP mic Macs

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 5f18911
Checked: The exact capture predicates passed all three shipped cases under host Rust, including first-report anchoring, stale/overlapping inline reports, gaps, producer rollback, and a full PCM buffer. Read through the stop and prepare callbacks. The existing report-to-ALSA lock order is preserved and prepare resets the consumer after an xrun. A fabric waiter holds a run reference while waiting, and one completion wakes the next waiter without crossing an idle transition. Exact Host, Apple and KUnit jobs are queued.

@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Hardware row at exact head 5f18911ef525 on the Neo (J700): PASS (silent capture).

  • On this kernel the low-power mic is card 0 device 0 (lpai); device 1 is hpai, so I captured plughw:0,0 rather than the plughw:0,1 in the row text.
  • 3x 10 s arecord --buffer-time=20000 (S16_LE 48 kHz stereo): 1920044 bytes each, 0 overrun!!!; a normal 10 s capture as control gave the same size. No WARN/BUG. The overrun-recovery path did not trigger in these runs.
  • Not covered: the J616s row (other Mac).

How: one combined test kernel for eight PRs, not this PR alone. Kernel 7.3.0-aurora-ci = main d0684a06 + merge commits of linux#2712 b6887f88, #2300 c1d351d2, #1990 83ef8b0f, #3742 798c2cde, #2350 5f18911e, #2398 75b30c6c, #1814 775f31d2, #2402 07660961 (all merged clean, test commit 03a571ef, cloud build https://github.com/aurora-silicon/linux/actions/runs/47474056726). Booted as a one-shot on the lab Neo (J700, T8140, 26.6.2 stub) with this kernel's own t8140-j700.dtb in a trial Stage 2 (live aura 1.0.0b19 + U-Boot, only the DTB swapped), default cmdline, silent (no audio playback). Boot health on the test kernel: 0 error-level kernel lines, 0 AVC, 0 failed units. Saved default restored after.

This branch has not been deployed

No deployments
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:neo 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.

1 participant