Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# imac20-amdgpu-patch

> Reliable GPU acceleration for **Apple iMac20,1 / iMac20,2** (Radeon Pro 5300/5500, Navi 14) on Arch Linux / Omarchy with the `linux-t2` kernel.
> Reliable GPU acceleration for **Apple iMac20,1 / iMac20,2 with Radeon Pro 5300** (Navi 14, `1002:7340`, subsystem `106b:0219`) on Arch Linux / Omarchy with the `linux-t2` kernel.
> A rebuilt `amdgpu.ko` with the deferred-UCLK SMU fix, plus a two-entry Limine boot (hardware-accelerated / safe) that survives kernel upgrades.

This repository is **not** a fork of the Linux kernel. It holds:
Expand Down Expand Up @@ -37,6 +37,15 @@ It installs to `/usr/lib/modules/<krel>/updates/`, so the package's own module i

## Install

The installer checks PCI IDs before installing files or changing boot settings.
The bundled patch targets `1002:7340` / `106b:0219`; the `106b:0218` variant
requires a different patch and is rejected. The check uses sysfs, so a working
graphics driver is not required. Rebuild and accelerated-entry commands repeat
the check; an unsupported host's pacman hook exits successfully without changes.
Diagnostics and cleanup commands remain available for recovery. On unsupported
hardware, `unpatch` restores the package module, rebuilds the safe UKI and removes
the default hardware-accelerated entry.

```bash
git clone https://github.com/McoreD/imac20-amdgpu-patch.git
cd imac20-amdgpu-patch
Expand Down
33 changes: 28 additions & 5 deletions scripts/imac20-hwaccel
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ KERNEL_CMDLINE[default]+=" $flags"
EOF
}

rebuild_safe_uki() {
log "building safe UKI"
write_dropin safe
limine-mkinitcpio >/dev/null
uki_cmdline "$DEFAULT_UKI" | grep -q 'nomodeset' || die "safe cmdline not in default UKI"
}

rebuild_ukis() {
local krel=$1 desc
if [[ $PATCH_AMDGPU == 1 ]]; then
Expand All @@ -194,11 +201,8 @@ rebuild_ukis() {
uki_cmdline "$DEFAULT_UKI" | grep -q 'amdgpu.modeset=1' || die "hwaccel cmdline not in built UKI"
cp "$DEFAULT_UKI" "$HWA_UKI.new" && mv "$HWA_UKI.new" "$HWA_UKI"

log "building safe UKI"
write_dropin safe
rebuild_safe_uki
trap - EXIT
limine-mkinitcpio >/dev/null
uki_cmdline "$DEFAULT_UKI" | grep -q 'nomodeset' || die "safe cmdline not in default UKI"

limine-entry-tool --add-efi "$ENTRY_NAME" "$HWA_UKI" --overwrite \
--comment "Hardware acceleration ($desc, $krel). Black screen? Boot 'Omarchy' = safe mode." >/dev/null
Expand Down Expand Up @@ -405,11 +409,30 @@ status() {
}

cmd=${1:-status}
# Guard module builds and accelerated-entry setup. Keep diagnostics and
# recovery available if hardware changes.
case $cmd in
repatch|rebuild-uki|ab-test|test-entry|_testentry|_hookwork|hook)
if ! source "$SHARE/hardware.sh" || ! require_supported_gpu; then
# A pacman hook must not fail the transaction or rewrite another GPU's
# boot configuration, including the usual failure/disable handler.
[[ $cmd == hook ]] && exit 0
exit 1
fi ;;
esac
case $cmd in
status) require_root "$@"; status ;;
repatch) require_root "$@"; k=${2:-$(target_krel)}; build_module "$k"; rebuild_ukis "$k" ;;
rebuild-uki) require_root "$@"; rebuild_ukis "$(target_krel)" ;;
unpatch) require_root "$@"; PATCH_AMDGPU=0; k=$(target_krel); unpatch_module "$k"; rebuild_ukis "$k" ;;
unpatch) require_root "$@"; PATCH_AMDGPU=0; k=$(target_krel); unpatch_module "$k"
if source "$SHARE/hardware.sh" && require_supported_gpu; then
rebuild_ukis "$k"
else
# Recovery must not create or select an accelerated entry
# for hardware outside the bundled patch's supported IDs.
rebuild_safe_uki
disable_hwaccel unsupported-gpu
fi ;;
disable) require_root "$@"; disable_hwaccel manual ;;
ab-test) require_root "$@"; ab_test ;;
ab-result) require_root "$@"; ab_result ;;
Expand Down
5 changes: 4 additions & 1 deletion scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
# - sets PATCH_AMDGPU=1 and builds the deferred-UCLK amdgpu for the newest
# linux-t2, then both UKIs
set -euo pipefail
[[ $EUID -eq 0 ]] || exec sudo "$0" "$@"
REPO=$(cd "$(dirname "$(readlink -f "$0")")/.." && pwd)
source "$REPO/scripts/lib/hardware.sh"
require_supported_gpu
[[ $EUID -eq 0 ]] || exec sudo "$0" "$@"

install -Dm755 "$REPO/scripts/imac20-hwaccel" /usr/local/sbin/imac20-hwaccel
install -Dm644 "$REPO/scripts/lib/hardware.sh" /usr/local/share/imac20-hwaccel/hardware.sh
for p in 6001-drm-amd-pm-Fix-boot-problems-in-5300.patch amdgpu-defer-uclk-5300.patch; do
install -Dm644 "$REPO/src/$p" "/usr/local/share/imac20-hwaccel/$p"
done
Expand Down
20 changes: 20 additions & 0 deletions scripts/lib/hardware.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Shared hardware preflight for the installer and imac20-hwaccel.
# The optional directory argument is for fixture tests; production callers use
# the kernel's PCI inventory, which works even when amdgpu failed to bind.
require_supported_gpu() {
local pci_root=${1:-/sys/bus/pci/devices} device vendor id subvendor subdevice
for device in "$pci_root"/*; do
[[ -d $device ]] || continue
vendor=$(cat "$device/vendor" 2>/dev/null) || continue
id=$(cat "$device/device" 2>/dev/null) || continue
subvendor=$(cat "$device/subsystem_vendor" 2>/dev/null) || continue
subdevice=$(cat "$device/subsystem_device" 2>/dev/null) || continue
# Match the bundled deferred-UCLK patch, not the marketing GPU name.
if [[ $vendor == 0x1002 && $id == 0x7340 &&
$subvendor == 0x106b && $subdevice == 0x0219 ]]; then
return 0
fi
done
echo 'imac20-hwaccel: unsupported GPU: expected 1002:7340 with subsystem 106b:0219; the bundled patch does not cover 106b:0218.' >&2
return 1
}
140 changes: 140 additions & 0 deletions tests/test_hardware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
"""Hardware gate tests: temporary sysfs fixtures, no root or host writes.

Run with: python3 -m unittest discover -s tests -v
"""
import pathlib
import subprocess
import tempfile
import unittest

REPO = pathlib.Path(__file__).resolve().parents[1]
HARDWARE = REPO / "scripts/lib/hardware.sh"
TOOL = (REPO / "scripts/imac20-hwaccel").read_text()


class HardwareTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = pathlib.Path(self.temp.name)
self.pci = self.root / "pci"
self.pci.mkdir()

def gpu(self, subdevice="0x0219", **overrides):
device = self.pci / f"0000:03:{len(list(self.pci.iterdir())):02d}.0"
device.mkdir()
values = dict(vendor="0x1002", device="0x7340",
subsystem_vendor="0x106b", subsystem_device=subdevice)
values.update(overrides)
for name, value in values.items():
if value is not None:
(device / name).write_text(value + "\n")
return device

def check(self):
return subprocess.run(["bash", "-c", 'set -eu; source "$1"; require_supported_gpu "$2"',
"_", str(HARDWARE), str(self.pci)], capture_output=True, text=True)

def test_supported_gpu_without_driver_or_revision(self):
self.gpu()
self.assertEqual(self.check().returncode, 0)

def test_unsupported_and_incomplete_inventory(self):
self.assertNotEqual(self.check().returncode, 0)
for changes in ({"subdevice": "0x0218"}, {"vendor": "0x1234"},
{"device": "0x7341"}, {"subsystem_vendor": "0x1002"},
{"subsystem_device": None}):
self.gpu(**changes)
result = self.check()
self.assertNotEqual(result.returncode, 0, changes)
self.assertIn("106b:0219", result.stderr)

def test_scans_all_pci_devices(self):
self.gpu("0x0218")
self.gpu()
self.assertEqual(self.check().returncode, 0)

def dispatch(self, command, missing_helper=False):
# Load real function definitions and real dispatch, but replace all
# work functions with markers. Never invoke system mutation commands.
prefix, dispatch = TOOL.split("cmd=${1:-status}\n", 1)
prefix = prefix.replace("CONF=/etc/imac20-hwaccel.conf", f"CONF={self.root}/absent.conf")
helper = HARDWARE.read_text().replace("/sys/bus/pci/devices", str(self.pci))
if not missing_helper:
(self.root / "hardware.sh").write_text(helper)
stubs = '\nSHARE="$2"\n'
for name in ("require_root", "status", "build_module", "rebuild_ukis", "unpatch_module",
"disable_hwaccel", "ab_test", "ab_result", "ab_clean", "build_test_entry",
"remove_test_entry", "module_is_patched"):
stubs += f'{name}() {{ echo "called:{name}"; }}\n'
stubs += 'target_krel() { echo fixture-kernel; }\n'
stubs += 'write_dropin() { echo "called:write_dropin:$1"; }\n'
stubs += 'limine-mkinitcpio() { :; }\n'
stubs += 'uki_cmdline() { echo "nomodeset modprobe.blacklist=amdgpu"; }\n'
script = prefix + stubs + "cmd=${1:-status}\n" + dispatch
return subprocess.run(["bash", "-c", script, "_", command, str(self.root)],
capture_output=True, text=True)

def test_unsupported_build_and_entry_commands_do_no_work(self):
self.gpu("0x0218")
for command in ("repatch", "rebuild-uki", "ab-test", "test-entry", "_testentry", "_hookwork", "hook"):
result = self.dispatch(command)
self.assertEqual(result.returncode, 0 if command == "hook" else 1, command)
self.assertNotIn("called:", result.stdout, command)
self.assertIn("unsupported GPU", result.stderr, command)

def test_hook_missing_helper_does_not_fail_transaction(self):
result = self.dispatch("hook", missing_helper=True)
self.assertEqual(result.returncode, 0)
self.assertNotIn("called:", result.stdout)

def test_supported_repatch_reaches_build(self):
self.gpu()
result = self.dispatch("repatch")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("called:build_module", result.stdout)
self.assertIn("called:rebuild_ukis", result.stdout)

def test_diagnostics_and_recovery_remain_available(self):
self.gpu("0x0218")
for command in ("status", "ab-result", "disable", "unpatch", "ab-clean", "test-entry-remove"):
result = self.dispatch(command)
self.assertEqual(result.returncode, 0, (command, result.stderr))
self.assertIn("called:", result.stdout)

def test_unsupported_unpatch_rebuilds_safe_boot_without_acceleration(self):
self.gpu("0x0218")
result = self.dispatch("unpatch")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("called:unpatch_module", result.stdout)
self.assertIn("called:write_dropin:safe", result.stdout)
self.assertIn("called:disable_hwaccel", result.stdout)
self.assertNotIn("called:rebuild_ukis", result.stdout)
self.assertNotIn("called:write_dropin:hwaccel", result.stdout)

def test_supported_unpatch_preserves_package_module_boot(self):
self.gpu()
result = self.dispatch("unpatch")
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("called:unpatch_module", result.stdout)
self.assertIn("called:rebuild_ukis", result.stdout)
self.assertNotIn("called:disable_hwaccel", result.stdout)

def test_installer_rejects_before_privilege_or_writes(self):
self.gpu("0x0218")
scripts = self.root / "scripts"
(scripts / "lib").mkdir(parents=True)
installer = (REPO / "scripts/install.sh").read_text()
# Exit if the gate is bypassed, before executing *any* installer work.
before, _ = installer.split('[[ $EUID -eq 0 ]]', 1)
(scripts / "install.sh").write_text(before + 'echo UNEXPECTED-WORK; exit 99\n')
(scripts / "lib/hardware.sh").write_text(
HARDWARE.read_text().replace("/sys/bus/pci/devices", str(self.pci)))
result = subprocess.run(["bash", str(scripts / "install.sh")], capture_output=True, text=True)
self.assertEqual(result.returncode, 1)
self.assertIn("unsupported GPU", result.stderr)
self.assertNotIn("UNEXPECTED-WORK", result.stdout)


if __name__ == "__main__":
unittest.main()