Skip to content

cpuidle: apple: fix T8140 s2idle timekeeping with a selectable WFI state - #298

Merged
RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
niteria:fix/t8140-s2idle-timekeeping
Oct 13, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
niteria:fix/t8140-s2idle-timekeeping

Conversation

@niteria

@niteria niteria commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Problem

On T8140, apple_cpuidle_probe() reduces state_count to one to avoid the unsupported CPU-PD/CYC_OVRD path. This leaves only the index-0 WFI fallback.

The generic find_deepest_state() search starts at index 1, and cpuidle_enter_s2idle() calls enter_s2idle_proper() only for an index greater than zero. With the current T8140 registration, s2idle falls back without reaching tick_freeze()/tick_unfreeze() and timekeeping suspension.

On an Apple MacBook Neo (T8140/J700), all six CPUs consequently reported zero s2idle/usage after completed RTC sleeps. CLOCK_MONOTONIC advanced through the entire suspended interval, just like CLOCK_BOOTTIME.

Fix

Keep the existing state count and replace the T8140 index-1 power-down entry with a copy of the safe WFI entry. This gives the generic s2idle selector an eligible nonzero entry without executing the unsupported deep-WFI register sequence.

  • Both normal-idle callbacks on T8140 remain safe WFI.
  • The fallback state and generic CPU-idle core are unchanged.
  • Other Apple SoCs retain their existing power-down entry.
  • This does not enable deep CPU power-down or claim reduced standby power.

Behavioral hardware A/B

The same runner and pass/fail criterion were used before and after the patch. For each RTC sleep:

accounted_suspend = ΔCLOCK_BOOTTIME - ΔCLOCK_MONOTONIC

For a requested 20-second sleep, the test accepts 17–23 seconds of accounted suspended time. The tolerance is a test allowance for RTC rounding/device transitions, not an API-specified bound. Each measurement also requires a matching RTC interrupt/deadline, cleared alarm, exactly one completed suspend, unchanged kernel failure counters, successful thaw, automatic Wi-Fi recovery, and unchanged monitored service PIDs/restart counts. An early or unrelated failed wake is not counted as a clock reproduction.

Kernel BOOTTIME delta MONOTONIC delta Accounted suspend Result
Before, run 1 20.278776 s 20.278776 s ~0 s FAIL
Before, run 2 20.822336 s 20.822336 s ~0 s FAIL
Before, run 3 21.039468 s 21.039468 s ~0 s FAIL
After, run 1 20.716105 s 0.518469 s 20.197636 s PASS
After, run 2 20.308580 s 0.500235 s 19.808345 s PASS
After, run 3 20.916300 s 0.497085 s 20.419216 s PASS

All three patched runs completed RTC wake/thaw and automatic Wi-Fi recovery without logind, timesyncd or NetworkManager restarting. All six CPUs recorded nonzero index-1 s2idle usage. CPU-idle counters are diagnostic only; the clock oracle does not depend on this patch's state layout.

The hardware A/B was performed on 3b2bd35a8591bcad766c60d0681e7f060cfd8cc7 with our existing matched platform integration. The patched driver and generic cpuidle selector/wrapper are byte-identical to the corresponding files used by this PR. I also checked the current aurora-wip base (ea2e6a147844998a489da8e06fb176f2894b736f): these relevant files are unchanged from the original PR preparation base (30812f491575ede27e5be059fd6afc4e985166f1). The complete newer branch has not been separately hardware-tested.

This patch was first prepared in the fork-local PR niteria#1; this submission targets Aurora's aurora-wip branch.

Reproduction procedure

Use a boot with suspend/RTC wake support already qualified and no bound driver that vetoes sleep. On our test unit: AC connected, lid open, console target, Bluetooth unadmitted, encoder unloaded; RTC1 is the Apple PMU wake-alarm device. Run the measuring process as a root system.slice service so it is outside the userspace slice frozen by systemd.

  1. Record BOOTTIME and MONOTONIC close together, the boot ID, /sys/power/suspend_stats, RTC IRQ/activation counts and per-CPU cpuidle/state*/s2idle/usage.
  2. Arm the verified RTC with rtcwake --device /dev/rtc1 --mode no --seconds 20, then request systemctl suspend --no-block. Leave inputs/cables untouched.
  3. When the kernel suspend-success counter increments, immediately record both clocks and RTC evidence again, then require suspend-service completion and thaw. Reject an early wake or any failed_* counter increment.
  4. Evaluate the formula above. On the original kernel, both clocks advance together and the difference is near zero; after the patch, the difference accounts for the RTC sleep.

No watchdog crash is needed to reproduce the clock defect. In a separate unpatched 40-minute trial, logind and timesyncd hit their three-minute service watchdogs immediately upon resume, and NetworkManager remained asleep; a manual Sleep(false) restored networking without a reboot or firmware reload. That longer failure is motivation, not a claim that extended suspend is fully qualified by the short A/B.

Other validation

  • Matched ARM64 kernel and FUSE/AVE/RTC module build, unchanged DTB, boot admission checks.
  • Actual-source host replay of the driver table/probe, WFI callback, generic selector and s2idle wrapper under ASan/UBSan. It reproduces the original no-freeze path and verifies balanced freeze/unfreeze after the patch; hardware primitives are mocked.
  • Negative controls: disabled state, missing s2idle callback and latency limit; unchanged non-T8140 selection and WFI-only normal idle on T8140.
  • scripts/checkpatch.pl: 0 errors, 0 warnings.

Clock semantics and boot-format scope

Hardware testing used raw booting. The current driver selects the T8140 restriction by DT compatibility, not boot format, so the same selection issue applies with this driver under Mach-O. Future qualified deep-idle support may supersede this WFI alias; Mach-O/deep-idle, battery drain, Bluetooth-enabled extended suspend and the earlier unexplained reset are not qualified by this patch's short-cycle tests.

T8140 exposes only the index-0 WFI fallback to avoid the unsupported CPU power-down path. The generic s2idle selector skips index 0, so this prevents tick_freeze() and timekeeping suspension from being reached.

Copy the safe WFI state over the T8140 power-down entry instead of reducing state_count. This preserves an eligible nonzero s2idle entry without using CYC_OVRD, while leaving other Apple SoCs unchanged.

On MacBook Neo, three 20-second RTC sleeps accounted for approximately zero suspended time before this change and 20.198, 19.808 and 20.419 seconds afterward using the identical BOOTTIME-minus-MONOTONIC test.

Signed-off-by: Bartosz Nitka <[email protected]>
@RyanTheTide
RyanTheTide merged commit 1b6f659 into aurora-silicon:aurora-wip Oct 13, 2026

@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.

lgtm!

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.

2 participants