diff --git a/packages/omamobile-device-tegu/README.md b/packages/omamobile-device-tegu/README.md index 604697628..b53eca32a 100644 --- a/packages/omamobile-device-tegu/README.md +++ b/packages/omamobile-device-tegu/README.md @@ -21,6 +21,7 @@ packaged yet. What is here is what runs on in-tree drivers. | `bin/omamobile-setup-system` | `/usr/bin` | System setup for Omarchy's lifecycle (omamobile-firstboot runs it): UTF-8 locale, timezone, NTP, and enables the sensor proxy and the NFC link opener (`omamobile-nfc-open`). Not the fingerprint service, speaker protection or the GNSS socket yet (see the script) | | `vendor/systemd/system-sleep/omamobile-tegu-wifi` | `/usr/lib/systemd/system-sleep` | Keeps Wi-Fi loaded across s2idle when `/etc/omamobile/wifi-keep-across-sleep` exists (the chip reloads its firmware on resume and iwd reconnects; the hook checks that from a transient unit, `omamobile-tegu-wifi-check`, so the lock screen doesn't wait for it), otherwise unloads and reloads brcmfmac around the sleep; after a sleep that lost Wi-Fi it unloads for the rest of the boot. It reloads the in-tree `brcmfmac`; a phone still running an out-of-tree build of it keeps its own hook until the Wi-Fi series lands | | `vendor/systemd/system-sleep/omamobile-tegu-nfc` | `/usr/lib/systemd/system-sleep` | Stops neard's poll loop before a sleep (polling through s2idle reset the palmtop; with it stopped, 35 of 35 cycles woke), reads `Polling` back and logs it, and starts the loop again after the wake if it was on. Powering the ST54L off as well, the workaround for a controller that did not answer on I2C after a wake (issue #132; fixed by linux-omarchy-mobile #131), runs only while `/etc/omamobile/nfc-power-off-across-sleep` exists: off by default, since its record is one wake that needed the power key and one that worked. Log: `/var/log/omamobile/nfc-sleep.log` | +| `vendor/systemd/system-sleep/omamobile-tegu-cpuidle` | `/usr/lib/systemd/system-sleep` | Keeps the cores out of the core power-down state (C2) through a sleep: with every core in C2 during s2idle the palmtop sometimes never woke (6 to 135 sleeps), and with C2 off for the sleep 300 of 300 came back. Disables every idle state deeper than state0 before the sleep and enables the ones it disabled after the wake, so the cores still power down while awake; does nothing on a kernel without deeper states, or while `/etc/omamobile/cpuidle-keep-across-sleep` exists (for testing C2 in the sleep). A sleep written straight to `/sys/power/state` skips it. [SLEEP-PLAN](../../device/tegu/SLEEP-PLAN.md) S1 | | `vendor/systemd/system/linux-modules-cleanup.service.d/10-omamobile-tegu.conf` | `/usr/lib/systemd/system` | Holds off Arch's old-module cleanup: our kernels come as module tarballs, not pacman packages, and the next-boot and rollback trees must stay | | `vendor/systemd/system.conf.d/10-omamobile-reboot-watchdog.conf`, `sleep.conf.d/10-omamobile.conf` | `/usr/lib/systemd` | A hung shutdown resets the phone after a minute; suspend means s2idle (no SoC sleep sequence yet, and `deep` does not wake) | | `vendor/udev/rules.d/10-omamobile-storage.rules` | `/usr/lib/udev/rules.d` | Continues the initramfs storage guard: Android partitions stay hidden and the whole-disk node unusable | diff --git a/packages/omamobile-device-tegu/test/all b/packages/omamobile-device-tegu/test/all index 121d36bc5..8d9d33193 100755 --- a/packages/omamobile-device-tegu/test/all +++ b/packages/omamobile-device-tegu/test/all @@ -84,6 +84,7 @@ for f in usr/bin/reboot-to usr/bin/tegu-tcpc-switch \ usr/lib/modprobe.d/omamobile-tegu-guards.conf usr/lib/modprobe.d/omamobile-tegu-aoc.conf \ usr/lib/modprobe.d/omamobile-tegu-display.conf usr/lib/modprobe.d/omamobile-tegu-clk-acpm.conf \ usr/lib/systemd/system-sleep/omamobile-tegu-wifi usr/lib/systemd/system-sleep/omamobile-tegu-nfc \ + usr/lib/systemd/system-sleep/omamobile-tegu-cpuidle \ usr/lib/systemd/system/linux-modules-cleanup.service.d/10-omamobile-tegu.conf \ usr/lib/systemd/system.conf.d/10-omamobile-reboot-watchdog.conf \ usr/lib/systemd/sleep.conf.d/10-omamobile.conf \ @@ -98,7 +99,8 @@ for f in usr/bin/reboot-to usr/bin/tegu-tcpc-switch \ etc/pacman.d/omamobile-mirrorlist; do [[ -f $tmp/root/$f ]] || fail "install stages $f" done -[[ -x $tmp/root/usr/lib/systemd/system-sleep/omamobile-tegu-wifi && -x $tmp/root/usr/lib/systemd/system-sleep/omamobile-tegu-nfc ]] \ +[[ -x $tmp/root/usr/lib/systemd/system-sleep/omamobile-tegu-wifi && -x $tmp/root/usr/lib/systemd/system-sleep/omamobile-tegu-nfc && + -x $tmp/root/usr/lib/systemd/system-sleep/omamobile-tegu-cpuidle ]] \ || fail "the sleep hooks are not executable" for unit in omamobile-tegu-graphics omamobile-tegu-usb-rescue; do [[ $(readlink "$tmp/root/usr/lib/systemd/system/multi-user.target.wants/$unit.service") == ../$unit.service ]] \ diff --git a/packages/omamobile-device-tegu/test/sleep-hooks b/packages/omamobile-device-tegu/test/sleep-hooks index 9a927fcf5..f5ee20792 100755 --- a/packages/omamobile-device-tegu/test/sleep-hooks +++ b/packages/omamobile-device-tegu/test/sleep-hooks @@ -4,11 +4,13 @@ # systemctl, logger and busctl: Wi-Fi is left alone when kept and present, # unloaded and reloaded otherwise, and falls back after a lost sleep; NFC's # poll loop is stopped before the sleep and started again after it, and the -# adapter is powered off only behind the flag file. +# adapter is powered off only behind the flag file; the deeper CPU idle +# states are disabled for the sleep and enabled again after it. set -euo pipefail cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." wifi=$PWD/vendor/systemd/system-sleep/omamobile-tegu-wifi nfc=$PWD/vendor/systemd/system-sleep/omamobile-tegu-nfc +cpuidle=$PWD/vendor/systemd/system-sleep/omamobile-tegu-cpuidle fail() { printf 'not ok - sleep-hooks: %s\n' "$1" >&2; exit 1; } tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT export OMAMOBILE_TEGU_ROOT=$tmp/root @@ -116,4 +118,65 @@ rm -f "$power_off" : > "$TEST_LOG"; rm -f "$TEST_NFC"/* "$nfc" pre; "$nfc" post [[ -z $(calls) ]] || fail "an off NFC adapter was touched: $(calls)" -echo 'ok - sleep-hooks: Wi-Fi kept or reloaded as flagged, NFC polling stopped and restored, power-off behind the flag' + +# CPU idle states: a scratch /sys with two CPUs, state0 (WFI) and two deeper +# states each; cpu1's state2 was disabled by hand before +cpus=$OMAMOBILE_TEGU_ROOT/sys/devices/system/cpu +idle_log=$OMAMOBILE_TEGU_ROOT/var/log/omamobile/cpuidle-sleep.log +keep_idle=$OMAMOBILE_TEGU_ROOT/etc/omamobile/cpuidle-keep-across-sleep +for c in 0 1; do for s in 0 1 2; do mkdir -p "$cpus/cpu$c/cpuidle/state$s"; echo 0 > "$cpus/cpu$c/cpuidle/state$s/disable"; done; done +echo 1 > "$cpus/cpu1/cpuidle/state2/disable" +states() { for c in 0 1; do for s in 0 1 2; do printf '%s' "$(cat "$cpus/cpu$c/cpuidle/state$s/disable")"; done; done; } +rm -f "$idle_log" "$keep_idle" +"$cpuidle" pre suspend +[[ $(states) == 011011 ]] || fail "cpuidle pre: the deeper states are not all disabled, or state0 was touched: $(states)" +[[ $(wc -l < "$run/cpuidle-disabled-for-sleep") -eq 3 ]] || fail "cpuidle pre notes the three states it disabled, not the one disabled by hand" +"$cpuidle" post suspend +[[ $(states) == 000001 ]] || fail "cpuidle post: its own states are enabled again and the hand-disabled one stays off: $(states)" +[[ ! -e $run/cpuidle-disabled-for-sleep ]] || fail "cpuidle post left its note" +grep -q ' pre: 3 deeper idle states disabled for the sleep$' "$idle_log" && grep -q ' post: 3 idle states enabled again$' "$idle_log" \ + || fail "cpuidle is not logged: $(cat "$idle_log")" +# A post without a pre (a boot's first hook run, or the keep flag): nothing changes +"$cpuidle" post suspend +[[ $(states) == 000001 ]] || fail "cpuidle post without a note changed something: $(states)" +# A pre whose post never ran (systemd-sleep killed between the passes), then a +# whole sleep: the second pre must not take the first one's states for +# hand-disabled ones, so the post still enables all three +rm -f "$idle_log" +"$cpuidle" pre suspend; "$cpuidle" pre suspend +[[ $(states) == 011011 && $(wc -l < "$run/cpuidle-disabled-for-sleep") -eq 3 ]] \ + || fail "cpuidle pre after a pre without a post: $(states), note $(cat "$run/cpuidle-disabled-for-sleep" 2>&1)" +"$cpuidle" post suspend +[[ $(states) == 000001 && ! -e $run/cpuidle-disabled-for-sleep ]] || fail "cpuidle pre, pre, post left deeper states disabled: $(states)" +grep -q 'the note of a sleep whose post pass never ran' "$idle_log" || fail "the leftover note is not logged: $(cat "$idle_log")" +# The same leftover with the keep flag set since: the states come back, and stay +"$cpuidle" pre suspend; : > "$keep_idle"; "$cpuidle" pre suspend +[[ $(states) == 000001 ]] || fail "cpuidle with the keep flag left an earlier sleep's states disabled: $(states)" +"$cpuidle" post suspend; rm -f "$keep_idle" +[[ $(states) == 000001 ]] || fail "cpuidle post after a kept sleep changed something: $(states)" +# A state that can't be enabled again is logged +"$cpuidle" pre suspend +if [[ $EUID -ne 0 ]]; then + chmod a-w "$cpus/cpu0/cpuidle/state1/disable"; rm -f "$idle_log" + "$cpuidle" post suspend + chmod u+w "$cpus/cpu0/cpuidle/state1/disable" + grep -q ' post: could not enable .*/cpu0/cpuidle/state1/disable$' "$idle_log" && grep -q ' post: 2 idle states enabled again$' "$idle_log" \ + || fail "a failed write in post is not logged: $(cat "$idle_log")" + echo 0 > "$cpus/cpu0/cpuidle/state1/disable" +else + "$cpuidle" post suspend +fi +[[ $(states) == 000001 ]] || fail "cpuidle after the failed write: $(states)" +# The keep flag: the states are left alone, and it is logged +: > "$keep_idle"; rm -f "$idle_log" +"$cpuidle" pre suspend; "$cpuidle" post suspend +[[ $(states) == 000001 ]] || fail "cpuidle with the keep flag touched the states: $(states)" +grep -q 'cpuidle-keep-across-sleep exists' "$idle_log" || fail "the keep flag is not logged" +rm -f "$keep_idle" +# No deeper states (the published device tree): nothing happens, no note, no log line +rm -rf "$cpus"; mkdir -p "$cpus/cpu0/cpuidle/state0"; echo 0 > "$cpus/cpu0/cpuidle/state0/disable"; rm -f "$idle_log" +"$cpuidle" pre suspend; "$cpuidle" post suspend +[[ $(cat "$cpus/cpu0/cpuidle/state0/disable") == 0 && ! -e $run/cpuidle-disabled-for-sleep && ! -s $idle_log ]] \ + || fail "cpuidle without deeper states did something" +rm -rf "$OMAMOBILE_TEGU_ROOT/sys" +echo 'ok - sleep-hooks: Wi-Fi kept or reloaded as flagged, NFC polling stopped and restored, power-off behind the flag, deeper CPU idle states off for the sleep' diff --git a/packages/omamobile-device-tegu/vendor/systemd/system-sleep/omamobile-tegu-cpuidle b/packages/omamobile-device-tegu/vendor/systemd/system-sleep/omamobile-tegu-cpuidle new file mode 100755 index 000000000..cb5c1752c --- /dev/null +++ b/packages/omamobile-device-tegu/vendor/systemd/system-sleep/omamobile-tegu-cpuidle @@ -0,0 +1,68 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# systemd-sleep hook for the Pixel 9a's CPU idle states: the sleep runs in +# WFI. With every core in the core power-down state (C2) through s2idle the +# palmtop sometimes never woke, not from the RTC alarm and not from the +# power key, after 6 to 135 sleeps (2026-10-28 to 2026-11-08; a watchdog kept +# counting through the sleep showed the suspend side complete and nothing +# after it). With C2 off for the sleep, 300 of 300 RTC-woken sleeps came +# back (2026-11-07/08). Why C2 doesn't wake is open: the PMU's wake-up +# masks and the CPU power-down handshake aren't programmed on this SoC yet +# (device/tegu SLEEP-PLAN S1). +# +# So before the sleep: every idle state deeper than state0 that is enabled +# is disabled on every CPU, and noted. After the wake: the noted ones are +# enabled again, so the cores still power down while the palmtop is awake. +# A state someone disabled by hand stays disabled. On a kernel without +# deeper states (the published device tree has none yet) nothing happens. +# +# Like the NFC hook, this covers a systemd sleep only: a sleep written +# straight to /sys/power/state (rtcwake -m freeze) skips it. To test C2 in +# the sleep on purpose, make /etc/omamobile/cpuidle-keep-across-sleep: the +# hook then only logs. Logs to /var/log/omamobile/cpuidle-sleep.log. +# OMAMOBILE_TEGU_ROOT: a scratch root for the package tests. +root=${OMAMOBILE_TEGU_ROOT:-} +C=$root/sys/devices/system/cpu +D=$root/run/omamobile +N=$D/cpuidle-disabled-for-sleep +KEEP=$root/etc/omamobile/cpuidle-keep-across-sleep +L=$root/var/log/omamobile/cpuidle-sleep.log +mkdir -p "$D" "${L%/*}" +log() { echo "$(date -Is) $1: $2" >> "$L"; } +# Enable the noted states again and drop the note. $1: the pass, for the log. +restore() { + local f n=0 + while read -r f; do + [[ -e $f ]] || continue + if { echo 0 > "$f"; } 2>/dev/null; then n=$((n + 1)); else log "$1" "could not enable $f"; fi + done < "$N" + rm -f "$N" + if [[ $1 == pre ]]; then + log pre "$n idle states enabled again (the note of a sleep whose post pass never ran)" + else + log post "$n idle states enabled again" + fi +} +case "$1" in + pre) + # A note still here is from a sleep whose "post" never ran (systemd-sleep + # killed between the two passes): its states are enabled again first, so + # they aren't taken for hand-disabled ones and left off for good. + [[ -e $N ]] && restore pre + if [[ -e $KEEP ]]; then + log pre "$KEEP exists: the idle states are left as they are" + exit 0 + fi + n=0 + for f in "$C"/cpu[0-9]*/cpuidle/state[1-9]*/disable; do + [[ -e $f ]] || continue + [[ $(cat "$f") == 0 ]] || continue + if { echo 1 > "$f"; } 2>/dev/null; then echo "$f" >> "$N"; n=$((n + 1)); else log pre "could not disable $f"; fi + done + ((n)) && log pre "$n deeper idle states disabled for the sleep" + ;; + post) + [[ -e $N ]] && restore post + ;; +esac +exit 0 diff --git a/packages/omamobile-device-tegu/version b/packages/omamobile-device-tegu/version index 13dead7eb..d3b5ba4bf 100644 --- a/packages/omamobile-device-tegu/version +++ b/packages/omamobile-device-tegu/version @@ -1 +1 @@ -0.2.10 +0.2.11