diff --git a/drivers/gpu/drm/asahi/g14_v27_2.rs b/drivers/gpu/drm/asahi/g14_v27_2.rs index a29b80086177c..9b043c63364c0 100644 --- a/drivers/gpu/drm/asahi/g14_v27_2.rs +++ b/drivers/gpu/drm/asahi/g14_v27_2.rs @@ -445,6 +445,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, @@ -517,7 +554,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); } @@ -543,21 +580,28 @@ 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())?; + 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); + } + }; // Retain the firmware window on the kernel UAT so dynamic kernel-window mappings past // the boot-time L2 tables mirror new L2 entries into `shared_l2` (and `Runtime::stop()` // retains `graph` for the life of the boot). graph.kernel_uat().attach_firmware_window(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 }) } @@ -666,6 +710,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 while a PMGR fabric/DCS busy reference is held for active GPU work. gpu_busy_held: bool, /// A fault or timeout the firmware reported (error, named slot), answered by `recover`. @@ -1868,9 +1915,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!( @@ -1879,7 +1932,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) }; @@ -1891,7 +1944,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| { @@ -1908,7 +1961,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) }; @@ -1940,10 +1993,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); @@ -1978,7 +2035,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()) @@ -1986,7 +2044,14 @@ 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())))?; + 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); @@ -1994,7 +2059,8 @@ impl Runtime { state.waiting_control = None; state.waiting_bind = 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(); @@ -2068,7 +2134,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(); @@ -2126,6 +2196,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 @@ -2193,6 +2264,7 @@ impl Backend { accepting: true, submitted: 0, retired: 0, + idle_off_allowed, gpu_busy_held: false, firmware_error: None, recoveries: 0, 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 + ); + } } diff --git a/tools/asahi/g14-v27-2-try-bind-tests.py b/tools/asahi/g14-v27-2-try-bind-tests.py index ad994ed148990..2cf836b11d988 100755 --- a/tools/asahi/g14-v27-2-try-bind-tests.py +++ b/tools/asahi/g14-v27-2-try-bind-tests.py @@ -1109,6 +1109,7 @@ def build_harness(mutation=None): kept_compute: KVec, kept_renders: KVec, hardware_unreachable: bool, + idle_off_allowed: bool, gpu_busy_held: bool, }} @@ -1183,6 +1184,7 @@ def build_harness(mutation=None): kept_compute: KVec::new(), kept_renders: KVec::new(), hardware_unreachable: false, + idle_off_allowed: false, gpu_busy_held: false, }})), idle: CondVar::new(),