Skip to content

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

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

RyanTheTide wants to merge 3 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 <[email protected]>
…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 <[email protected]>
@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 <[email protected]>
@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.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant