From e4c0b4847713576a192b26b403409738bb9ddefa Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Thu, 26 Nov 2026 04:37:48 -0500 Subject: [PATCH 1/2] drm/asahi: g14_v27_2: enable runtime idle power-off When the firmware power-controller calibration is present (root.has_pwr_controller()), publish the allow-off idle policy after the boot job and after warm restart from deep sleep so firmware powers the GPU down after fender_idle_off_delay_ms of inactivity instead of holding it powered continuously while idle. Track idle_off_allowed in State so sleep_prepare(), sleep_complete(), and stop() avoid redundant allow-off/keep-powered policy flips when runtime idle power-off is already active. Measured on J313 (M1 MacBook Air, T8103): lowers idle SoC power (PHPC) by 186-201 mW (1.074 W -> 0.873-0.888 W) with no regression in Borealis compute, rendering, s2idle, or deep sleep. Signed-off-by: Erik Bourget --- drivers/gpu/drm/asahi/g14_v27_2.rs | 54 +++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/asahi/g14_v27_2.rs b/drivers/gpu/drm/asahi/g14_v27_2.rs index ba1910a1a84be..f043cd6d38938 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2.rs @@ -482,7 +482,12 @@ pub(crate) fn start<'a>( step = "boot job"; root.publish_boot_job(resources.operating_point())?; peer.as_mut().send_message(EP_DOORBELL, gpu::COMPUTE_KICK)?; - wait(&root, 3000, || root.boot_job_complete()) + wait(&root, 3000, || root.boot_job_complete())?; + if root.has_pwr_controller() { + step = "idle power-off policy"; + set_idle_policy(&root, &mut peer, true)?; + } + Ok(()) })(); if let Err(e) = result { dev_err!(dev, "GPU firmware 27.2 did not start: {}: {:?}\n", step, e); @@ -555,6 +560,9 @@ struct State { accepting: bool, submitted: u64, retired: u64, + /// Set when the DT power-controller calibration is present and runtime idle power-off is + /// permitted (firmware powers the GPU off after `fender_idle_off_delay_ms` of inactivity). + idle_off_allowed: bool, /// Set when a sleep/wake failure may have left the GPU domain unpowered or stopped so /// teardown skips live-hardware MMIO and RTKit operations. hardware_unreachable: bool, @@ -1199,9 +1207,15 @@ impl Runtime { if !self.healthy() || state.running() { return Err(self.abort_deep_prepare(state, false, EBUSY)); } - if let Err((published, err)) = try_set_idle_policy(&state.root, &mut state.peer, true) { - dev_err!(self.dev, "GPU: deep sleep refused, allow-off failed: {:?}\n", err); - return Err(self.abort_deep_prepare(state, published, err)); + let mut policy_published = false; + if !state.idle_off_allowed { + match try_set_idle_policy(&state.root, &mut state.peer, true) { + Ok(()) => policy_published = true, + Err((published, err)) => { + dev_err!(self.dev, "GPU: deep sleep refused, allow-off failed: {:?}\n", err); + return Err(self.abort_deep_prepare(state, published, err)); + } + } } if let Err(err) = wait(&state.root, 500, || Ok(gpu_powered_off(state.asc.fender()))) { dev_err!( @@ -1210,7 +1224,7 @@ impl Runtime { state.asc.fender(), err ); - return Err(self.abort_deep_prepare(state, true, err)); + return Err(self.abort_deep_prepare(state, policy_published, err)); } // SAFETY: `ASC_CPU_CONTROL` lies within the mapped ASC page. let asc_ctl = unsafe { state.asc.0.add(ASC_CPU_CONTROL) }; @@ -1222,7 +1236,7 @@ impl Runtime { idle_us: 0, }, GFP_KERNEL) { Ok(c) => c, - Err(err) => return Err(self.abort_deep_prepare(state, true, err.into())), + Err(err) => return Err(self.abort_deep_prepare(state, policy_published, err.into())), }; state.commit_stop = Some(commit); let data = state.commit_stop.as_deref_mut().map_or(core::ptr::null_mut(), |c| { @@ -1239,7 +1253,7 @@ impl Runtime { }; if ret != 0 { state.commit_stop = None; - return Err(self.abort_deep_prepare(state, true, Error::from_errno(ret))); + return Err(self.abort_deep_prepare(state, policy_published, Error::from_errno(ret))); } // SAFETY: `dev` was just registered above. unsafe { apple_sleep_coproc_set_ready(self.dev.as_raw(), true) }; @@ -1271,10 +1285,14 @@ impl Runtime { // SAFETY: Reads a global flag word. let real = unsafe { kernel::bindings::pm_resume_via_firmware() }; if !stopped && !real { - dev_info!(self.dev, "GPU: deep sleep refused before commit; restoring keep-powered\n"); - if let Err(err) = set_idle_policy(&state.root, &mut state.peer, false) { - dev_err!(self.dev, "GPU: failed to restore keep-powered policy: {:?}\n", err); - self.fail_all(state, err); + if !state.idle_off_allowed { + dev_info!(self.dev, "GPU: deep sleep refused before commit; restoring keep-powered\n"); + if let Err(err) = set_idle_policy(&state.root, &mut state.peer, false) { + dev_err!(self.dev, "GPU: failed to restore keep-powered policy: {:?}\n", err); + self.fail_all(state, err); + } + } else { + dev_info!(self.dev, "GPU: deep sleep refused before commit\n"); } self.notify.set_deep_sleeping(self, false); self.asleep.store(false, Ordering::Release); @@ -1316,7 +1334,11 @@ impl Runtime { state.asc.write(ASC_CPU_CONTROL, ctl_before | ASC_RUN); state.peer.as_mut().boot()?; set_idle_policy(&state.root, &mut state.peer, false)?; - wait(&state.root, 500, || Ok(gpu_powered(state.asc.fender()))) + wait(&state.root, 500, || Ok(gpu_powered(state.asc.fender())))?; + if state.idle_off_allowed { + set_idle_policy(&state.root, &mut state.peer, true)?; + } + Ok(()) })(); self.notify.set_deep_sleeping(self, false); self.asleep.store(false, Ordering::Release); @@ -1393,7 +1415,11 @@ impl Runtime { self.notify.messages.load(Ordering::Relaxed) ); } else { - let allowed = set_idle_policy(&state.root, &mut state.peer, true); + let allowed = if state.idle_off_allowed { + Ok(()) + } else { + set_idle_policy(&state.root, &mut state.peer, true) + }; let root = &state.root; let _ = wait(root, 500, || Ok(root.firmware_running()? == 0)); let running = root.firmware_running(); @@ -1489,6 +1515,7 @@ impl Backend { return Err(e); } }; + let idle_off_allowed = root.has_pwr_controller(); *runtime.state.lock() = Some(State { peer, root, pool, asc, fault, queue, last, sequence: u64::from(head), @@ -1510,6 +1537,7 @@ impl Backend { accepting: true, submitted: 0, retired: 0, + idle_off_allowed, hardware_unreachable: false, #[cfg(CONFIG_APPLE_SOC_SLEEP)] commit_stop: None, From c9f59b988c2424e3f533be3c14138be74be0ffc4 Mon Sep 17 00:00:00 2001 From: Erik Bourget Date: Fri, 27 Nov 2026 12:46:24 -0500 Subject: [PATCH 2/2] drm/asahi: g14_v27_2: keep keep-powered mode if idle-off fails pre-publication Once keep-powered mode and the boot job have already succeeded, enabling optional runtime idle power-off uses try_set_idle_policy() and startup_idle_policy() to distinguish pre-publication failures from uncertain post-publication failures: - If publish_idle_policy(true) fails before writing the allow-off record to shared memory (published == false), warn and continue in the confirmed keep-powered mode (idle_off_allowed = false) instead of failing GPU startup or warm restart. - If a failure occurs after publication (send_message or wait), retain controlled teardown since the active firmware policy is uncertain. - Pass the resolved idle_off_allowed flag into Backend::start() and update State.idle_off_allowed across warm restart so sleep_prepare(), sleep_complete(), and stop() stay consistent with the active policy. - Add unit coverage for startup_idle_policy() in g14_v27_2_abi.rs. Signed-off-by: Erik Bourget --- drivers/gpu/drm/asahi/g14_v27_2.rs | 88 +++++++++++++++++++------- drivers/gpu/drm/asahi/g14_v27_2_abi.rs | 66 +++++++++++++++++++ 2 files changed, 132 insertions(+), 22 deletions(-) diff --git a/drivers/gpu/drm/asahi/g14_v27_2.rs b/drivers/gpu/drm/asahi/g14_v27_2.rs index a3a7579ad33a4..d02c3fe171cd0 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2.rs @@ -407,6 +407,43 @@ fn set_idle_policy(root: &RootV27_2, peer: &mut Peer, allow_off: bool) -> Result try_set_idle_policy(root, peer, allow_off).map_err(|(_, e)| e) } +/// Enables optional runtime idle power-off after `keep-powered` has already succeeded. A failure +/// before publication (`published == false`) keeps `keep-powered` active (`Ok(false)`); an +/// uncertain post-publication failure propagates `Err(err)` for controlled teardown. +fn enable_optional_idle_off( + dev: &device::Device, + root: &RootV27_2, + peer: &mut Peer, + want_idle_off: bool, + phase: &str, +) -> Result { + let attempt = if want_idle_off { + try_set_idle_policy(root, peer, true) + } else { + Ok(()) + }; + let status = match &attempt { + Ok(()) => Ok(()), + Err((published, _)) => Err(*published), + }; + match (g14_v27_2_abi::startup_idle_policy(want_idle_off, status), attempt) { + (g14_v27_2_abi::StartupIdlePolicy::AllowOff, Ok(())) => Ok(true), + (g14_v27_2_abi::StartupIdlePolicy::KeepPowered, Err((_, err))) => { + dev_warn!( + dev, + "GPU firmware 27.2: {} idle power-off failed before publication ({:?}); continuing in keep-powered mode\n", + phase, + err + ); + Ok(false) + } + (g14_v27_2_abi::StartupIdlePolicy::KeepPowered, Ok(())) => Ok(false), + (g14_v27_2_abi::StartupIdlePolicy::FailUncertain, Err((_, err))) => Err(err), + (_, Err((_, err))) => Err(err), + (_, Ok(())) => Ok(false), + } +} + /// Stops firmware that no runtime owns: allow power-off, then RTKit power-off, then the ASC /// run bit. Everything it could reach is kept for the rest of the boot. fn halt_unowned(dev: &device::Device, mut peer: Peer, root: crate::fw::types::GpuObject, @@ -479,7 +516,7 @@ pub(crate) fn start<'a>( // From here the firmware may read everything the window names: nothing is freed. let mut booted = false; let mut step = "loader handoff"; - let result = (|| -> Result { + let result = (|| -> Result { let start = Instant::::now(); while !window.answered() { if start.elapsed() >= Delta::from_millis(1000) { return Err(ETIMEDOUT); } @@ -506,23 +543,25 @@ pub(crate) fn start<'a>( root.publish_boot_job(resources.operating_point())?; peer.as_mut().send_message(EP_DOORBELL, gpu::COMPUTE_KICK)?; wait(&root, 3000, || root.boot_job_complete())?; - if root.has_pwr_controller() { - step = "idle power-off policy"; - set_idle_policy(&root, &mut peer, true)?; - } - Ok(()) + step = "idle power-off policy"; + enable_optional_idle_off(dev, &root, &mut peer, root.has_pwr_controller(), "startup") })(); - if let Err(e) = result { - dev_err!(dev, "GPU firmware 27.2 did not start: {}: {:?}\n", step, e); - halt_unowned(dev, peer, root, asc, booted); - core::mem::forget(window); - core::mem::forget(pool); - core::mem::forget(graph); - return Err(e); - } + let idle_off_allowed = match result { + Ok(allowed) => allowed, + Err(e) => { + dev_err!(dev, "GPU firmware 27.2 did not start: {}: {:?}\n", step, e); + halt_unowned(dev, peer, root, asc, booted); + core::mem::forget(window); + core::mem::forget(pool); + core::mem::forget(graph); + return Err(e); + } + }; // The firmware keeps using the window until it is stopped; it stays for the life of the boot. core::mem::forget(window); - let backend = Backend::start(&drm, dev, notify, peer, root, graph, pool, asc, fault, cfg)?; + let backend = Backend::start( + &drm, dev, notify, peer, root, graph, pool, asc, fault, cfg, idle_off_allowed, + )?; Ok(Started { backend, drm, binding, resources }) } @@ -1798,7 +1837,8 @@ impl Runtime { } let started = Instant::::now(); let ctl_before = state.asc.read(ASC_CPU_CONTROL); - let restart = (|| -> Result { + let was_idle_off_allowed = state.idle_off_allowed; + let restart = (|| -> Result { // SAFETY: `peer` is live and the firmware was stopped at the commit. kernel::error::to_result(unsafe { kernel::bindings::apple_rtkit_warm_restart(state.peer.as_raw()) @@ -1807,17 +1847,21 @@ impl Runtime { state.peer.as_mut().boot()?; set_idle_policy(&state.root, &mut state.peer, false)?; wait(&state.root, 500, || Ok(gpu_powered(state.asc.fender())))?; - if state.idle_off_allowed { - set_idle_policy(&state.root, &mut state.peer, true)?; - } - Ok(()) + enable_optional_idle_off( + &self.dev, + &state.root, + &mut state.peer, + was_idle_off_allowed, + "warm restart", + ) })(); self.notify.set_deep_sleeping(self, false); self.asleep.store(false, Ordering::Release); state.waiting_unlink = None; state.waiting_control = None; match restart { - Ok(()) => { + Ok(idle_off_allowed) => { + state.idle_off_allowed = idle_off_allowed; // No round crosses a sleep (sleep_prepare refuses one), and the restarted // firmware may count generations again from its start: forget old rounds. state.ledger = g14_v27_2_abi::RecoveryLedger::default(); @@ -1947,6 +1991,7 @@ impl Backend { asc: AscControl, fault: FaultStatus, cfg: &'static hw::HwConfig, + idle_off_allowed: bool, ) -> Result> { // The runtime is created empty first, so no allocation failure can drop firmware-visible // state. Every failure after the state is moved in goes through Runtime::stop, which @@ -1991,7 +2036,6 @@ impl Backend { return Err(e); } }; - let idle_off_allowed = root.has_pwr_controller(); *runtime.state.lock() = Some(State { peer, root, pool, asc, fault, queue, last, sequence: u64::from(head), diff --git a/drivers/gpu/drm/asahi/g14_v27_2_abi.rs b/drivers/gpu/drm/asahi/g14_v27_2_abi.rs index 96d4992845d68..2d42a0f1f7b7c 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2_abi.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2_abi.rs @@ -395,6 +395,37 @@ pub(crate) fn spare_buffer(published: u64, slot: usize) -> bool { published & (1 << slot) == 0 } +/// Outcome of attempting to enable optional runtime idle power-off after `keep-powered` and +/// the boot job already succeeded. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub(crate) enum StartupIdlePolicy { + /// Runtime idle power-off is active (`idle_off_allowed = true`). + AllowOff, + /// Keep-powered remains active: either DT power-controller calibration is absent or + /// `publish_idle_policy` failed before writing an allow-off record to shared memory. + KeepPowered, + /// An allow-off record was already published (`published = true`) before the failure, + /// leaving the active firmware policy uncertain; fail startup cleanly. + FailUncertain, +} + +/// Decides the startup idle policy from whether DT power-controller calibration is present and +/// the outcome of publishing and confirming `allow_off = true` (`Ok(())`, or `Err(published)` +/// where `published` reports whether the control record was written to shared memory). +pub(crate) const fn startup_idle_policy( + has_pwr_controller: bool, + enable_result: Result<(), bool>, +) -> StartupIdlePolicy { + if !has_pwr_controller { + return StartupIdlePolicy::KeepPowered; + } + match enable_result { + Ok(()) => StartupIdlePolicy::AllowOff, + Err(false) => StartupIdlePolicy::KeepPowered, + Err(true) => StartupIdlePolicy::FailUncertain, + } +} + #[cfg_attr(not(test), kunit_tests(asahi_g14_fw_queues))] #[cfg_attr(test, allow(dead_code))] mod tests { @@ -775,4 +806,39 @@ mod tests { assert_eq!(encode_compute_timestamps(&mut empty, 0x8000, [0; 2]), 0); assert!(empty.iter().all(|&b| b == 0)); } + + #[test] + fn startup_idle_policy_handles_calibration_and_publish_errors() { + // Without DT power-controller calibration, keep-powered always stays active. + assert_eq!( + startup_idle_policy(false, Ok(())), + StartupIdlePolicy::KeepPowered + ); + assert_eq!( + startup_idle_policy(false, Err(false)), + StartupIdlePolicy::KeepPowered + ); + assert_eq!( + startup_idle_policy(false, Err(true)), + StartupIdlePolicy::KeepPowered + ); + + // With calibration, confirmed publication enables runtime idle power-off. + assert_eq!( + startup_idle_policy(true, Ok(())), + StartupIdlePolicy::AllowOff + ); + + // Pre-publication failure leaves the previously confirmed keep-powered mode intact. + assert_eq!( + startup_idle_policy(true, Err(false)), + StartupIdlePolicy::KeepPowered + ); + + // Post-publication failure leaves the active policy uncertain and must fail cleanly. + assert_eq!( + startup_idle_policy(true, Err(true)), + StartupIdlePolicy::FailUncertain + ); + } }