-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add suspend/resume recovery for T2 Macs (using legacy apple_bce) #5998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjohnson
wants to merge
3
commits into
basecamp:dev
Choose a base branch
from
jjohnson:add-t2-suspend
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Detect T2 MacBook models using PCI IDs | ||
| # Vendor: 106b (Apple), Device IDs: 1801 or 1802 (T2 Security Chip) | ||
| if lspci -nn 2>/dev/null | grep -q "106b:180[12]"; then | ||
|
jjohnson marked this conversation as resolved.
jjohnson marked this conversation as resolved.
|
||
| echo "Configuring T2 suspend/resume service..." | ||
|
|
||
| # Install the suspend helper from the Omarchy repo | ||
| sudo mkdir -p /usr/local/libexec/omarchy/apple | ||
| sudo cp "$OMARCHY_PATH/install/config/hardware/apple/t2-suspend" /usr/local/libexec/omarchy/apple/t2-suspend | ||
| sudo chmod +x /usr/local/libexec/omarchy/apple/t2-suspend | ||
|
|
||
| # Write the systemd service | ||
| sudo tee /etc/systemd/system/omarchy-apple-t2-suspend.service >/dev/null <<'UNIT_EOF' | ||
| [Unit] | ||
| Description=Apple T2 suspend/resume | ||
| Before=sleep.target | ||
| StopWhenUnneeded=yes | ||
|
|
||
| [Service] | ||
| Type=oneshot | ||
| RemainAfterExit=yes | ||
| ExecStart=/usr/local/libexec/omarchy/apple/t2-suspend pre | ||
| ExecStop=/usr/local/libexec/omarchy/apple/t2-suspend post | ||
| TimeoutStopSec=10 | ||
|
|
||
| [Install] | ||
| WantedBy=sleep.target | ||
| UNIT_EOF | ||
|
|
||
| sudo systemctl enable omarchy-apple-t2-suspend.service | ||
|
|
||
| echo "T2 suspend/resume service enabled." | ||
| fi | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Legacy apple_bce suspend/resume helper. Runs as a Before=sleep.target | ||
| # systemd service so the T2 coprocessor is fully active during pre. | ||
|
|
||
| # Pre: | ||
| # - unbinds aaudio (releases ALSA FDs held by userspace) | ||
| # - stops tiny-dfr (prevents spurious wake-ups) | ||
| # - unloads T2 modules (reverse dep order) | ||
| # * on BCM4377b brcmfmac removed before sleep to avoid D3 timeout | ||
| # * on BCM4364 brcmfmac stays bound; kernel PCI PM handles D3cold | ||
| # Post: | ||
| # - reloads apple_bce, then sleeps to let T2 firmware init | ||
| # - reloads T2 modules (forward dep order), restarts tiny-dfr | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| LOG_TAG="t2-suspend" | ||
| BRCMF_D3COLD_PCI_ID=14e4:4464 | ||
| PRE_MODS=(appletbdrm hid_appletb_kbd hid_appletb_bl brcmfmac_wcc brcmfmac hci_bcm4377) | ||
| POST_MODS=(hci_bcm4377 brcmfmac brcmfmac_wcc hid_appletb_bl hid_appletb_kbd appletbdrm) | ||
|
|
||
| log() { logger -t "$LOG_TAG" "$1"; } | ||
|
|
||
| mod_ld() { [[ -d /sys/module/$1 ]]; } | ||
|
|
||
| require_legacy_driver() { | ||
| # apple_bce is intentionally unloaded during pre, so check the kernel's | ||
| # module metadata rather than /sys/module for both pre and post. | ||
| if ! modinfo apple_bce >/dev/null 2>&1; then | ||
| log "Skipping: legacy apple_bce driver is unavailable" | ||
| exit 0 | ||
| fi | ||
| } | ||
|
|
||
| unload() { | ||
| mod_ld "$1" && modprobe -r "$1" 2>/dev/null && | ||
| log "Unloaded $1" || true | ||
| } | ||
|
|
||
| load() { | ||
| mod_ld "$1" || { | ||
| modprobe "$1" 2>/dev/null && log "Loaded $1" || true | ||
| } | ||
| } | ||
|
|
||
| is_bcm4364() { lspci -nn | grep -q "$BRCMF_D3COLD_PCI_ID"; } | ||
|
|
||
| skip() { is_bcm4364 && [[ $1 == "brcmfmac" || $1 == "brcmfmac_wcc" ]]; } | ||
|
|
||
| case "${1:-}" in | ||
| pre) | ||
| require_legacy_driver | ||
|
|
||
| for d in /sys/bus/pci/drivers/aaudio/0000:*; do | ||
| [[ -e $d ]] || continue | ||
| echo "${d##*/}" > /sys/bus/pci/drivers/aaudio/unbind 2>/dev/null && | ||
| log "${d##*/} Unbound" || true | ||
| done | ||
|
|
||
| systemctl stop tiny-dfr 2>/dev/null || true | ||
|
|
||
| for m in "${PRE_MODS[@]}"; do skip "$m" || unload "$m"; done | ||
| unload apple_bce | ||
|
|
||
| if ! udevadm settle --timeout=5; then | ||
| log "Timed out waiting for udev queue to settle" | ||
| fi | ||
|
|
||
| log "Ready to suspend" | ||
| ;; | ||
| post) | ||
| require_legacy_driver | ||
|
|
||
| load apple_bce; sleep 2 | ||
|
|
||
| for m in "${POST_MODS[@]}"; do skip "$m" || load "$m"; done | ||
|
|
||
| systemctl reset-failed tiny-dfr 2>/dev/null && | ||
| systemctl restart tiny-dfr 2>/dev/null || true | ||
|
|
||
| log "Resume complete" | ||
| ;; | ||
| *) | ||
| echo "Usage: $0 {pre|post}"; exit 1 ;; | ||
| esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| echo "Install t2-suspend T2 suspend/resume service" | ||
|
|
||
| if lspci -nn 2>/dev/null | grep -q "106b:180[12]"; then | ||
|
jjohnson marked this conversation as resolved.
jjohnson marked this conversation as resolved.
|
||
| sudo mkdir -p /usr/local/libexec/omarchy/apple | ||
| sudo cp "$OMARCHY_PATH/install/config/hardware/apple/t2-suspend" /usr/local/libexec/omarchy/apple/t2-suspend | ||
| sudo chmod +x /usr/local/libexec/omarchy/apple/t2-suspend | ||
|
|
||
| sudo tee /etc/systemd/system/omarchy-apple-t2-suspend.service >/dev/null <<'UNIT_EOF' | ||
| [Unit] | ||
| Description=Apple T2 suspend/resume | ||
| Before=sleep.target | ||
| StopWhenUnneeded=yes | ||
|
|
||
| [Service] | ||
| Type=oneshot | ||
| RemainAfterExit=yes | ||
| ExecStart=/usr/local/libexec/omarchy/apple/t2-suspend pre | ||
| ExecStop=/usr/local/libexec/omarchy/apple/t2-suspend post | ||
| TimeoutStopSec=10 | ||
|
|
||
| [Install] | ||
| WantedBy=sleep.target | ||
| UNIT_EOF | ||
|
|
||
| sudo systemctl daemon-reload | ||
| sudo systemctl enable omarchy-apple-t2-suspend.service | ||
| echo "T2 suspend/resume service installed and enabled." | ||
| fi | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.