diff --git a/drivers/gpu/drm/asahi/g16/boot.rs b/drivers/gpu/drm/asahi/g16/boot.rs index 5864452f42fa3..8debfbc5ea1e9 100644 --- a/drivers/gpu/drm/asahi/g16/boot.rs +++ b/drivers/gpu/drm/asahi/g16/boot.rs @@ -904,7 +904,12 @@ fn run( scheduler.w32(scheduler_word, 1)?; } regs::wmb(); - devctl_op(session, channels, events, init, devctl.start)?; + let mut policy = DevCtl::op(devctl.start); + if session.adt.layout.powered.is_some() { + // Let the firmware manage idle at the normal boot policy boundary (Globals +0x30 = 1). + policy.words[1] = 1; + } + channel::devctl_submit(session, channels, events, init, &policy, true)?; wait_boot_control(session, channels, events, init, base + 2, 2000)?; let receive_kick = if g15s_channels { hw::engine::G15S_RECEIVE_DOORBELL diff --git a/drivers/gpu/drm/asahi/g16/glue.c b/drivers/gpu/drm/asahi/g16/glue.c index 0658e29266371..93fb11de55a7c 100644 --- a/drivers/gpu/drm/asahi/g16/glue.c +++ b/drivers/gpu/drm/asahi/g16/glue.c @@ -276,23 +276,23 @@ struct asahi_g16_cooling *asahi_g16_cooling_register(struct device *dev) cooling->pmp_node = NULL; } if (cooling->pmp_node) { - atomic_set(&cooling->state, 8); cooling->pmp = apple_pmp_gpu_get(dev); - if (IS_ERR(cooling->pmp)) { + if (PTR_ERR(cooling->pmp) == -ENODEV) { + of_node_put(cooling->pmp_node); + cooling->pmp_node = NULL; + cooling->pmp = NULL; + } else if (IS_ERR(cooling->pmp)) { ret = dev_err_probe(dev, PTR_ERR(cooling->pmp), "PMP telemetry from %pOF unavailable\n", cooling->pmp_node); goto put_node; } - } else { - /* - * Without an available apple,pmp supplier, thermal-zone - * telemetry drives the fail-closed backstop (starting at maximum - * cooling) while the cooling device remains available to thermal - * policy. - */ - atomic_set(&cooling->state, 0); } + /* + * Without PMP telemetry, thermal-zone telemetry drives the fail-closed + * backstop while the cooling device remains available to thermal policy. + */ + atomic_set(&cooling->state, cooling->pmp ? 8 : 0); cooling->cdev = thermal_of_cooling_device_register(dev->of_node, 0, "aurora-g16g", cooling, &asahi_g16_cooling_ops); @@ -412,6 +412,9 @@ int asahi_g16_cooling_sample(struct asahi_g16_cooling *cooling, u64 now, bool id /* * genpd never turned the GFX domain's TVM gate off, so a sleep that lost it goes * unnoticed: the domain then doesn't come on. Enabled here if it's found clear. + * + * Only a GFX domain whose parent is such a gate has one. T6030's GFX hangs off two + * PMGR words instead (GPX and AFR), which the sleep controller lowers and raises. */ int asahi_g16_tvm_enable(struct device *dev); int asahi_g16_tvm_enable(struct device *dev) @@ -428,6 +431,10 @@ int asahi_g16_tvm_enable(struct device *dev) of_node_put(gfx); if (!tvm) return -EINVAL; + if (!of_device_is_compatible(tvm, "apple,t8132-gfx-tvm")) { + of_node_put(tvm); + return 0; + } reg = of_iomap(tvm, 0); of_node_put(tvm); if (!reg) diff --git a/drivers/gpu/drm/asahi/g16/hw.rs b/drivers/gpu/drm/asahi/g16/hw.rs index 79d200e59d810..790cb677526eb 100644 --- a/drivers/gpu/drm/asahi/g16/hw.rs +++ b/drivers/gpu/drm/asahi/g16/hw.rs @@ -88,6 +88,13 @@ pub(crate) struct Config { pub(crate) host_block_ready: bool, /// Whether the TVM gate and firmware warm restart are supported. pub(crate) deep_sleep: bool, + /// Where a deep sleep stops the firmware. + pub(crate) sleep_stop: SleepStop, + /// The stop at the commit touches neither the ASC's page nor the GFX word after the idle + /// (G15S, where the stop is left to the commit). + pub(crate) sleep_quiet_stop: bool, + /// The SRAM a stop in `.prepare` saves before the idle and the restart writes back. + pub(crate) sleep_sram: Option, /// Whether render work accepts scratch for ray queries. pub(crate) supports_render_scs: bool, /// Whether FS->compute cross-engine barriers require `BarrierType::Hold` (type 1), as on G15. @@ -98,6 +105,29 @@ pub(crate) struct Config { pub(crate) max_tile_buffer: u32, } +/// SRAM in the "sgx" window that a stop in `.prepare` saves with the firmware running and the +/// restart writes back with GFX on, before the run bit. Offsets in bytes from the window. +#[derive(Clone, Copy, Debug)] +pub(crate) struct SleepSram { + /// Where the saved range starts. + pub(crate) start: usize, + /// Bytes saved. + pub(crate) save: usize, + /// Bytes written back, from `start`. + pub(crate) restore: usize, +} + +/// When a deep sleep stops the GPU's firmware and powers its domain off. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum SleepStop { + /// In the sleep's commit, with interrupts off and every refusal behind it. A sleep refused + /// before then finds the firmware running; only a real wake restarts it. + Commit, + /// In `.prepare`, before any other device suspends, as macOS does: the firmware restarts + /// warm after every sleep, real or refused. + Prepare, +} + impl Config { pub(crate) const fn core_slots(&self) -> u32 { self.num_clusters * self.num_cores_per_cluster @@ -244,6 +274,9 @@ pub(crate) mod t8132 { host_block_zero_order: &super::start_27_2::HOST_BLOCK_ZERO_ORDER, host_block_ready: true, deep_sleep: true, + sleep_stop: super::SleepStop::Commit, + sleep_quiet_stop: false, + sleep_sram: None, supports_render_scs: true, cross_engine_hold: false, // macOS 26B123 programs 48 KiB tiles (MSAA4, 32x16 utiles) on this GPU. @@ -404,7 +437,27 @@ pub(crate) mod t6030 { // per-pipe host-block control words cleared before endpoints 0x20/0x21 start. host_block_zero_order: &[0, 0x1808, 0x3010], host_block_ready: false, - deep_sleep: false, + // macOS 27.2 on J516s stops the GPU in its device sleep, before any display request + // goes, 4.1 to 4.5 s before the commit: AP and IOP IDLE, the mailbox's receive off, the + // PMP's AGX request withdrawn, then the GFX word off. Its wake raises GFX, makes the + // request again and takes the firmware's HELLO. The same in all three full records + // (s2r-trace ref/t6030-macos-s2r-{entry,wake}-full-{1,2,3}.tsv). There is no TVM gate: + // AFR and the GPU PMGR's other blocks are the sleep controller's. + // + // With the ASC page and sgx hooked as well (lane A, mac-gpustop-1, + // ref/t6030-macos-gpu-stop-asc-sgx.tsv): before the idle macOS saves sgx reg[1] + // +0x60000..+0x80000, in order, 64 bits at a time; after the idle it clears the ASC's + // CPU_CONTROL (never reading CPU_STATUS) before GFX goes off. Its restart writes back + // +0x60000..+0x74000 with GFX on, then sets the run bit and takes the firmware's HELLO. + // sgx reg[1] is at "sgx" +0xd00000 (ADT /arm-io/sgx reg[1], 0x290d00000). + deep_sleep: true, + sleep_stop: super::SleepStop::Prepare, + sleep_quiet_stop: true, + sleep_sram: Some(super::SleepSram { + start: 0xd60000, + save: 0x20000, + restore: 0x14000, + }), supports_render_scs: false, cross_engine_hold: true, max_tile_buffer: super::sizes::TILE_BUFFER_32K, @@ -546,6 +599,9 @@ pub(crate) mod t8122 { host_block_zero_order: &super::start_27_2::HOST_BLOCK_ZERO_ORDER, host_block_ready: true, deep_sleep: false, + sleep_stop: super::SleepStop::Commit, + sleep_quiet_stop: false, + sleep_sram: None, // Measured on the J615 (26B123): vertex-, geometry- and fragment-stage ray // queries with render scratch run clean. T6030 is not measured yet. supports_render_scs: true, diff --git a/drivers/gpu/drm/asahi/g16/mod.rs b/drivers/gpu/drm/asahi/g16/mod.rs index 36d94f0d67e6f..84f68c6f583f0 100644 --- a/drivers/gpu/drm/asahi/g16/mod.rs +++ b/drivers/gpu/drm/asahi/g16/mod.rs @@ -293,6 +293,8 @@ pub(crate) struct Shared { /// From a system sleep's `.prepare` until the firmware restarted: nothing reads its /// registers and nothing is published. pub(crate) sleeping: AtomicBool, + /// Kept stopped after a real dark wake while the system suspends again. + sleep_dark_held: AtomicBool, /// Firmware recovery is active under `Shared::state` (may sleep up to 1000 ms). pub(crate) recovering: AtomicBool, /// Whether any base compute unit (`0..BASE_COMPUTE_UNITS`) has been quarantined. @@ -689,6 +691,7 @@ impl Gpu { rtkit_buffer: AtomicU64::new(0), orphaned: AtomicBool::new(false), sleeping: AtomicBool::new(false), + sleep_dark_held: AtomicBool::new(false), recovering: AtomicBool::new(false), base_compute_quarantined: AtomicBool::new(false), quarantined_compute_mask: AtomicU32::new(0), @@ -727,6 +730,9 @@ impl Gpu { let firmware = Firmware::start_backend(dev, cfg, booted, &shared, power)?; let vms = firmware.vms.clone(); *shared.state.lock() = Some(firmware); + // The PMP's request for the GPU while its firmware runs, where the device tree names + // one: macOS holds it from its GPU's start (T6030 PTD request bit 4). + shared.pmp_request(true); // The engine's statistics file (a debugfs failure does not fail the probe). if let Err(error) = shared.start_engine() { dev_warn!(dev.as_ref(), "G16: no aurora-g16 debugfs ({:?})\n", error); @@ -745,6 +751,11 @@ impl Gpu { GFP_KERNEL, )?; gpu.retain()?; + if cfg.deep_sleep { + if let Err(error) = sleep::register_pm_notify(&gpu.shared) { + dev_warn!(dev.as_ref(), "G16: no sleep PM notifier ({:?})\n", error); + } + } Ok(gpu) } diff --git a/drivers/gpu/drm/asahi/g16/sleep.rs b/drivers/gpu/drm/asahi/g16/sleep.rs index b51556822b075..f571b1e608052 100644 --- a/drivers/gpu/drm/asahi/g16/sleep.rs +++ b/drivers/gpu/drm/asahi/g16/sleep.rs @@ -10,10 +10,14 @@ //! So `.prepare` only drains and closes publication, and the stop is left to the sleep's commit, //! which runs it with interrupts off and every refusal behind it. A sleep refused before then //! finds the firmware never stopped. The order is G17's, which macOS's power words on T8132 -//! agree with. The PMP vote stays through the sleep. +//! agree with. The configured stop determines when the PMP vote is withdrawn. //! //! Only a real wake restarts the firmware. A sleep refused after a stop leaves it stopped and //! publication closed until the next real sleep, with the domain on. +//! +//! G15S snapshots SRAM and withdraws its PMP request in `.prepare`, while the provider can +//! acknowledge. The quiet commit idles firmware. A canceled registration discards the snapshot +//! and reasserts the running request. A real wake restores SRAM before RUN. use core::sync::atomic::Ordering; @@ -25,6 +29,7 @@ use kernel::{ workqueue::{DelayedWork, Work}, }; +use super::hw::{SleepSram, SleepStop}; use super::recovery::now_ns; use super::regs::{self, ASC_RUN}; use super::{Firmware, Gpu, Shared}; @@ -36,15 +41,33 @@ const DRAIN_TICK_MS: i64 = 2; extern "C" { fn asahi_g16_tvm_enable(dev: *mut core::ffi::c_void) -> core::ffi::c_int; - fn asahi_g16_commit_prepare( + fn asahi_g16_commit_prepare_ex( + dev: *mut core::ffi::c_void, + rtk: *mut bindings::apple_rtkit, + quiet: bool, + sram_offset: u64, + save_len: usize, + restore_len: usize, + ) -> *mut core::ffi::c_void; + fn asahi_g16_stop_now( dev: *mut core::ffi::c_void, rtk: *mut bindings::apple_rtkit, + sram_offset: u64, + save_len: usize, + restore_len: usize, ) -> *mut core::ffi::c_void; + fn asahi_g16_sram_restore() -> core::ffi::c_int; + fn asahi_g17_pmp_request(dev: *mut core::ffi::c_void, enable: bool) -> core::ffi::c_int; + fn apple_rtkit_resume_rx(rtk: *mut bindings::apple_rtkit); fn asahi_g17_commit_finish(commit: *mut core::ffi::c_void, saved: *mut u32) -> bool; fn asahi_g17_power_auto_off(dev: *mut core::ffi::c_void, saved: *mut u32) -> core::ffi::c_int; fn asahi_g17_power_lower(dev: *mut core::ffi::c_void) -> core::ffi::c_int; fn asahi_g17_power_raise(dev: *mut core::ffi::c_void, saved: u32) -> core::ffi::c_int; fn asahi_g17_power_auto_on(dev: *mut core::ffi::c_void, saved: u32) -> core::ffi::c_int; + fn asahi_g16_register_pm_notify( + dev: *mut core::ffi::c_void, + post_suspend: unsafe extern "C" fn(*mut bindings::device), + ) -> core::ffi::c_int; } impl Firmware { @@ -58,12 +81,24 @@ impl Firmware { && self.recovery_idle()?) } - /// The run bit and the RTKit handshake, with the firmware's state as it was left. - fn sleep_restart(&mut self) -> Result { + /// The run bit and the RTKit handshake, with the firmware's state as it was left. `held`: + /// the stop masked the mailbox's receive interrupt, which comes back with the receive path. + fn sleep_restart(&mut self, held: bool) -> Result { // Start the next render on each pass slot fresh after the firmware restarts. self.pools.invalidate(); self.session.rtkit.warm_restart()?; + if held { + // SAFETY: The RTKit handle lasts until reboot; the stop masked the interrupt once. + unsafe { apple_rtkit_resume_rx(self.session.rtkit.as_raw()) }; + } let control = self.session.regs.asc_control()?; + if held { + dev_info!( + self.session.dev.as_ref(), + "GPU firmware restart: ASC control {:#x} with GFX back on\n", + control + ); + } self.session.regs.set_asc_control(control | ASC_RUN)?; regs::mb(); self.session.rtkit.boot() @@ -71,7 +106,29 @@ impl Firmware { } impl Shared { + /// The PMP's request for the GPU, where the device tree names one. The PMP may use a device + /// whose request is set, so it's only ever set with the domain on. + pub(crate) fn pmp_request(&self, enable: bool) { + let _ = self.set_pmp_request(enable); + } + + fn set_pmp_request(&self, enable: bool) -> Result { + // SAFETY: `dev` is the live GPU device. + let ret = unsafe { asahi_g17_pmp_request(self.dev.as_raw().cast(), enable) }; + if ret != 0 { + dev_warn!( + self.dev.as_ref(), + "PMP request for the GPU, {}: {}\n", + if enable { "on" } else { "off" }, + ret + ); + } + kernel::error::to_result(ret) + } + fn lower_domain(&self) { + // Before the domain goes: the PMP reads the GPU's counters while its request is set. + self.pmp_request(false); let mut saved = 0; // SAFETY: `dev` is the live GPU device and `saved` is writable. let ret = unsafe { asahi_g17_power_auto_off(self.dev.as_raw().cast(), &mut saved) }; @@ -153,12 +210,27 @@ impl Shared { } } - /// Leaves the stop to the sleep's commit. False, with the reason logged, if it can't be. - fn leave_for_commit(&self, firmware: &mut Firmware) -> bool { - // SAFETY: `dev` is the live GPU device. The RTKit handle lasts until reboot, and - // `.complete` frees the result. + /// Leaves the stop to the sleep's commit, or with [`SleepStop::Prepare`] stops the firmware + /// now. False, with the reason logged, if neither can be. + fn leave_for_commit( + &self, + firmware: &mut Firmware, + stop: SleepStop, + quiet: bool, + sram: Option, + ) -> bool { + let dev = self.dev.as_raw().cast(); + let rtk = firmware.session.rtkit.as_raw(); + let (start, save, restore) = + sram.map_or((0, 0, 0), |r| (r.start as u64, r.save, r.restore)); + // SAFETY: `dev` and RTKit stay live, and `.complete` releases the registration. let commit = unsafe { - asahi_g16_commit_prepare(self.dev.as_raw().cast(), firmware.session.rtkit.as_raw()) + match stop { + SleepStop::Commit => { + asahi_g16_commit_prepare_ex(dev, rtk, quiet, start, save, restore) + } + SleepStop::Prepare => asahi_g16_stop_now(dev, rtk, start, save, restore), + } }; match kernel::error::from_err_ptr(commit) { Ok(commit) => { @@ -168,7 +240,7 @@ impl Shared { Err(error) => { dev_warn!( self.dev.as_ref(), - "no GPU stop at the commit ({:?}): refusing the sleep\n", + "no GPU stop for the sleep ({:?}): refusing the sleep\n", error ); false @@ -183,7 +255,7 @@ impl Shared { return None; } let mut saved = self.sleep_ps.load(Ordering::Relaxed); - // SAFETY: `commit` came from `asahi_g16_commit_prepare` and is finished only once. + // SAFETY: `commit` came from prepare or stop_now and is finished only once. let stopped = unsafe { asahi_g17_commit_finish(commit, &mut saved) }; self.sleep_ps.store(saved, Ordering::Relaxed); Some(stopped) @@ -192,7 +264,25 @@ impl Shared { /// Waits for quiet, closes publication and leaves the stop to the commit. Refuses if the /// commit can't take it: a stop here loses the GPU after a refused sleep. A refusal /// changes nothing. - fn sleep_down(self: &Arc) -> Result { + fn sleep_down( + self: &Arc, + stop: SleepStop, + quiet: bool, + sram: Option, + ) -> Result { + // The commit's G15S stop: the PMP's request goes in `.prepare`. The stop in `.prepare` + // withdraws it itself, after the run bit, as macOS does. + let early = quiet && stop == SleepStop::Commit; + if self.sleep_dark_held.load(Ordering::Acquire) { + // Still parked from a dark wake: firmware is stopped, SRAM is saved and + // `sleep_ps` holds the awake word. Keep GFX off without overwriting it. + // SAFETY: `dev` is the live GPU device. + let ret = unsafe { asahi_g17_power_lower(self.dev.as_raw().cast()) }; + if ret != 0 { + dev_warn!(self.dev.as_ref(), "GPU domain slow to go off: {}\n", ret); + } + return Ok(()); + } if self.sleeping.load(Ordering::Acquire) { // After a sleep refused past the stop, only the domain is left to lower. self.lower_domain(); @@ -215,7 +305,18 @@ impl Shared { .sleep_quiet() .and_then(|quiet| if quiet { Ok(()) } else { Err(EBUSY) }) .and_then(|_| { - if self.leave_for_commit(firmware) { + // G15S: the PMP's request goes now, with the work drained, as macOS + // withdraws it at its GPU stop 4.4 s before the commit: at the commit + // the PMP is in its own sleep steps and its acknowledgment doesn't come + // (J516s: the commit's withdrawal failed, PMU +4 0xfe). + if self.leave_for_commit(firmware, stop, quiet, sram) { + if early { + if let Err(error) = self.set_pmp_request(false) { + let _ = self.sleep_commit_finish(); + self.pmp_request(true); + return Err(error); + } + } firmware.power.release_clpc_now(); self.active_flights.store(0, Ordering::Release); Ok(()) @@ -250,14 +351,31 @@ impl Shared { true } - /// Raises the domain and restarts the firmware. A failure loses the device. - fn sleep_up(self: &Arc) -> Result { + /// Raises the domain and restarts the firmware. A failure loses the device. `held`: the + /// stop was made in `.prepare`, with the mailbox's receive interrupt masked. + fn sleep_up(self: &Arc, held: bool, restore_sram: bool) -> Result { let started = now_ns(); let result = { let mut state = self.state.lock(); match state.as_deref_mut() { Some(firmware) if !self.crashed.load(Ordering::Acquire) => { - self.raise_domain().and_then(|_| firmware.sleep_restart()) + self.raise_domain().and_then(|_| { + // As macOS: the request once the domain is on, before the firmware's + // mailbox and handshake. + self.pmp_request(true); + if restore_sram { + // The SRAM the stop saved, back before the run bit, as macOS does. + // SAFETY: Writes back what the stop saved, with GFX on. + let ret = unsafe { asahi_g16_sram_restore() }; + if ret != 0 { + dev_warn!( + self.dev.as_ref(), + "GPU SRAM not written back: {}\n", ret + ); + } + } + firmware.sleep_restart(held) + }) } _ => self.raise_domain(), } @@ -289,7 +407,11 @@ impl Gpu { if !self.cfg.deep_sleep { return Err(EBUSY); } - let result = self.shared.sleep_down(); + let result = self.shared.sleep_down( + self.cfg.sleep_stop, + self.cfg.sleep_quiet_stop, + self.cfg.sleep_sram, + ); if let Err(error) = result { dev_warn!(self.shared.dev.as_ref(), "GPU refused sleep: {:?}\n", error); } @@ -301,22 +423,61 @@ impl Gpu { if !self.shared.sleeping.load(Ordering::Acquire) { return; } - // SAFETY: Reads a global flag word. + // SAFETY: Reads global flag words. let real = unsafe { bindings::pm_resume_via_firmware() }; - if self.shared.sleep_commit_finish() == Some(false) && !real { + // SAFETY: Reads global flag words. + let dark = unsafe { bindings::pm_dark_wake() }; + let stopped = self.shared.sleep_commit_finish(); + let held = self.cfg.sleep_stop == SleepStop::Prepare; + let was_dark = self.shared.sleep_dark_held.load(Ordering::Acquire); + if stopped == Some(false) && !real && !was_dark { dev_info!( self.shared.dev.as_ref(), "sleep refused before the commit: GPU firmware never stopped\n" ); + if self.cfg.sleep_quiet_stop { + self.shared.pmp_request(true); + } self.shared.feed.resume(); self.shared.sleeping.store(false, Ordering::Release); self.shared.poll_now(); return; } - if !real && self.shared.sleep_hold() { + if dark && (real || was_dark) { + self.shared.sleep_dark_held.store(true, Ordering::Release); + dev_info!( + self.shared.dev.as_ref(), + "dark wake: GPU firmware stays stopped\n" + ); + return; + } + self.shared.sleep_dark_held.store(false, Ordering::Release); + let woken = real || was_dark; + if !woken && self.shared.sleep_hold() { return; } - let _ = self.shared.sleep_up(); + let restore_sram = woken + && (was_dark || stopped == Some(true) || (stopped.is_none() && held)) + && self.cfg.sleep_sram.is_some(); + let _ = self.shared.sleep_up(held, restore_sram); + } + + /// Runs at `PM_POST_SUSPEND` if a dark-wake re-entry failed before `.prepare` reached us. + fn sleep_dark_finish(&self) { + // SAFETY: Reads a global flag word. + if !self.shared.sleep_dark_held.load(Ordering::Acquire) + || unsafe { bindings::pm_dark_wake() } + { + return; + } + self.shared.sleep_dark_held.store(false, Ordering::Release); + let held = self.cfg.sleep_stop == SleepStop::Prepare; + let restore_sram = self.cfg.sleep_sram.is_some(); + dev_info!( + self.shared.dev.as_ref(), + "dark wake over without a resume: restarting GPU firmware\n" + ); + let _ = self.shared.sleep_up(held, restore_sram); } } @@ -362,6 +523,20 @@ static SLEEP_OPS: SleepOps = SleepOps(bindings::dev_pm_ops { ..unsafe { core::mem::zeroed() } }); +unsafe extern "C" fn sleep_post_suspend(dev: *mut bindings::device) { + // SAFETY: Called by the PM notifier while `dev` is bound. + if let Some(gpu) = unsafe { sleep_gpu(dev) } { + gpu.sleep_dark_finish(); + } +} + +pub(super) fn register_pm_notify(shared: &Shared) -> Result { + // SAFETY: `dev` is the live GPU device; devm unregisters on unbind. + kernel::error::to_result(unsafe { + asahi_g16_register_pm_notify(shared.dev.as_raw().cast(), sleep_post_suspend) + }) +} + /// The G16 driver's sleep callbacks. pub(super) fn ops() -> &'static bindings::dev_pm_ops { &SLEEP_OPS.0 diff --git a/drivers/gpu/drm/asahi/g17/sleep.c b/drivers/gpu/drm/asahi/g17/sleep.c index 49fa7153c3475..6b8c03bea798b 100644 --- a/drivers/gpu/drm/asahi/g17/sleep.c +++ b/drivers/gpu/drm/asahi/g17/sleep.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include "sleep.h" @@ -369,6 +371,7 @@ struct asahi_g17_commit { struct device_node *request; bool ran, request_owned; + bool g16_sram_saved; int idle[2]; unsigned int idle_us[2]; u32 status[2]; @@ -573,8 +576,13 @@ static void __iomem *asahi_g17_map(struct device_node *np, const char *name, int return ioremap(res.start + offset, size); } +static size_t asahi_g16_sram_restore_len; +static int asahi_g16_sram_save(struct device *dev, u64 offset, size_t save_len, size_t restore_len); + static void asahi_g17_commit_free(struct asahi_g17_commit *c) { + if (c->g16_sram_saved && !c->ran) + asahi_g16_sram_restore_len = 0; if (c->request) { if (c->request_owned) apple_pmp_report_entry_commit_owned(c->request, false); @@ -693,15 +701,15 @@ bool asahi_g17_commit_required(void) } /* - * The same stop for G16: one firmware instance, whose control registers are the ASC's own, no - * bridge and no PMP request to withdraw, as its vote stays through the sleep. + * What a G16 stop touches: one firmware instance, whose control registers are the ASC's own, + * no bridge, and the GFX word. */ -struct asahi_g17_commit *asahi_g16_commit_prepare(struct device *dev, struct apple_rtkit *rtk) +static struct asahi_g17_commit *asahi_g16_commit_alloc(struct device *dev, + struct apple_rtkit *rtk) { struct asahi_g17_commit *c; struct device_node *np; u32 offset; - int ret; c = kzalloc_obj(*c); if (!c) @@ -711,18 +719,78 @@ struct asahi_g17_commit *asahi_g16_commit_prepare(struct device *dev, struct app c->count = 1; c->raise = asahi_g17_commit_raises(dev); - ret = -ENODEV; c->cpu[0] = asahi_g17_map(dev->of_node, "asc", 0, ASC_CPU_CONTROL, 8); np = of_parse_phandle(dev->of_node, "power-domains", 0); - if (!np) - goto err; - if (!of_property_read_u32(np, "reg", &offset) && np->parent) + if (np && !of_property_read_u32(np, "reg", &offset) && np->parent) c->ps = asahi_g17_map(np->parent, NULL, 0, offset, 4); of_node_put(np); - if (!c->cpu[0] || !c->ps) - goto err; + if (!c->cpu[0] || !c->ps) { + asahi_g17_commit_free(c); + return ERR_PTR(-ENODEV); + } + + return c; +} + +/* + * G15S's stop at the commit, as far as Linux can make it (J516s, pm_test=devices): AP IDLE and + * IOP IDLE, the mailbox's receive off, the PMP request withdrawn. After the idle the ASC's own + * page doesn't answer, and every AP write taking GFX out of automatic power reset the SoC, so + * neither is touched; the sleep controller takes AFR, GFX's parent, down. An idled G15S + * firmware answers neither IOP INIT nor IOP ON without a reset, so only the commit, which + * only a real wake follows, may idle it. + */ +static int asahi_g16_commit_stop_quiet(void *data) +{ + struct asahi_g17_commit *c = data; + + c->ran = true; + c->entry = readl(c->ps); + c->saved = c->entry; + c->idle[0] = apple_rtkit_sleep_idle_polled(c->rtk[0], NULL, &c->idle_us[0]); + apple_rtkit_mailbox_recv_off(c->rtk[0]); + c->words[0] = apple_rtkit_mailbox_status(c->rtk[0]); + c->request_ret = c->request ? apple_pmp_report_entry_withdraw_polled(c->request) : 0; + c->lowered = readl(c->ps); + + /* Return which of the idle exchange and PMP request failed. */ + return (c->idle[0] ? 1 : 0) | (c->request_ret ? 2 : 0); +} + +/* + * The same stop for G16, made by the sleep's commit. @quiet: G15S's, which touches neither the + * ASC's page nor GFX. The mailbox's interrupt stays live until the commit, which turns its + * receive off (J516s: masked from .prepare instead, the firmware's messages went unanswered + * through the suspend and its stop failed at the commit, PMU +4 0xff). + */ +struct asahi_g17_commit *asahi_g16_commit_prepare_ex(struct device *dev, struct apple_rtkit *rtk, + bool quiet, u64 sram_offset, + size_t save_len, size_t restore_len) +{ + struct asahi_g17_commit *c; + int ret; - ret = apple_sleep_coproc_register_first(dev, asahi_g17_commit_stop, c); + c = asahi_g16_commit_alloc(dev, rtk); + if (IS_ERR(c)) + return c; + if (save_len) { + ret = asahi_g16_sram_save(dev, sram_offset, save_len, restore_len); + if (ret) + goto err; + c->g16_sram_saved = true; + } + /* The default stop leaves the PMP vote held. */ + if (quiet || save_len) + c->request = of_parse_phandle(dev->of_node, "apple,pmp-report-entry", 0); + if (c->request) { + ret = apple_pmp_report_entry_commit_owned(c->request, true); + if (ret) + goto err; + c->request_owned = true; + } + + ret = apple_sleep_coproc_register_first(dev, quiet ? asahi_g16_commit_stop_quiet : + asahi_g17_commit_stop, c); if (ret) goto err; apple_sleep_coproc_set_ready(dev, true); @@ -734,6 +802,135 @@ struct asahi_g17_commit *asahi_g16_commit_prepare(struct device *dev, struct app return ERR_PTR(ret); } +struct asahi_g17_commit *asahi_g16_commit_prepare(struct device *dev, struct apple_rtkit *rtk) +{ + return asahi_g16_commit_prepare_ex(dev, rtk, false, 0, 0, 0); +} + +/* + * The SRAM a G15S stop saves while its firmware still runs and its restart writes back before the + * run bit, as macOS does (lane A, mac-gpustop-1): one buffer, the GPU being one device. + */ +static u64 *asahi_g16_sram; +static void __iomem *asahi_g16_sram_io; +static size_t asahi_g16_sram_save_len; + +static int asahi_g16_sram_save(struct device *dev, u64 offset, size_t save_len, size_t restore_len) +{ + size_t i; + + if (!save_len || restore_len > save_len || (save_len | restore_len | offset) & 7) + return -EINVAL; + if (!asahi_g16_sram) { + asahi_g16_sram = kvmalloc(save_len, GFP_KERNEL); + asahi_g16_sram_io = asahi_g17_map(dev->of_node, "sgx", 0, offset, save_len); + if (!asahi_g16_sram || !asahi_g16_sram_io) { + kvfree(asahi_g16_sram); + if (asahi_g16_sram_io) + iounmap(asahi_g16_sram_io); + asahi_g16_sram = NULL; + asahi_g16_sram_io = NULL; + return -ENOMEM; + } + asahi_g16_sram_save_len = save_len; + } + if (save_len != asahi_g16_sram_save_len) + return -EINVAL; + /* In order, 64 bits at a time, as macOS reads it */ + for (i = 0; i < save_len / 8; i++) + asahi_g16_sram[i] = readq(asahi_g16_sram_io + 8 * i); + asahi_g16_sram_restore_len = restore_len; + return 0; +} + +/* The restart's write-back, with GFX on and before the run bit: the first @restore_len only. */ +int asahi_g16_sram_restore(void) +{ + size_t i; + + if (!asahi_g16_sram || !asahi_g16_sram_restore_len) + return -ENODATA; + for (i = 0; i < asahi_g16_sram_restore_len / 8; i++) + writeq(asahi_g16_sram[i], asahi_g16_sram_io + 8 * i); + asahi_g16_sram_restore_len = 0; + return 0; +} + +/* The mailbox +0x0 bit macOS waits for after the receive goes off (0x2c -> 0x2e) */ +#define G16_MBOX_STOPPED BIT(1) + +/* + * The G15S stop as macOS 27.2 makes it on J516s, in its device sleep before any display request + * goes, about 4.2 s before the commit (lane A's mac-gpustop-1 with the ASC page and sgx hooked, + * s2r-trace ref/t6030-macos-gpu-stop-asc-sgx.tsv; ms before the commit): + * 1. -4398 to -4240: the SRAM in sgx reg[1] read in order, 64 bits at a time (@save_len at + * @sram_offset in "sgx"), with the firmware running; + * 2. -4239.87: AP IDLE (0x201), echoed; IOP IDLE, acknowledged at -4239.58; + * 3. -4239.56: the mailbox's receive interrupt off (I2A control 0x22201 -> 0x22200), then its + * +0x0 polled until bit 1 (0x2c -> 0x2e, 1.26 ms); + * 4. -4238.29: ASC CPU_CONTROL read (0x10), written 0, read, written 0; CPU_STATUS is never read; + * 5. -4238.27: the PMP's AGX request (PTD request bit 4) withdrawn; + * 6. -4238.04: the GFX word out of automatic power with its target on (0x0f0000ff), then its + * target off (0x0f0003f0); it reads 0x0f000100. + * The restart is the reverse (asahi_g16_sram_restore() and the driver's warm restart): GFX on, + * automatic power, the AGX request, the SRAM's first @restore_len written back, the receive on, the + * run bit, and the firmware's own HELLO. + */ +struct asahi_g17_commit *asahi_g16_stop_now(struct device *dev, struct apple_rtkit *rtk, + u64 sram_offset, size_t save_len, size_t restore_len) +{ + struct asahi_g17_commit *c; + unsigned int i; + u32 val; + + c = asahi_g16_commit_alloc(dev, rtk); + if (IS_ERR(c)) + return c; + + c->ran = true; + + c->request = of_parse_phandle(dev->of_node, "apple,pmp-report-entry", 0); + c->raise_ret = save_len ? asahi_g16_sram_save(dev, sram_offset, save_len, restore_len) : 0; + + c->entry = readl(c->ps); + c->saved = c->entry; + c->idle[0] = apple_rtkit_sleep_idle_polled(c->rtk[0], NULL, &c->idle_us[0]); + + apple_rtkit_mailbox_recv_off(c->rtk[0]); + for (i = 0; i < 1000; i++) { + c->words[0] = apple_rtkit_mailbox_status(c->rtk[0]); + if (c->words[0] & G16_MBOX_STOPPED) + break; + usleep_range(10, 20); + } + apple_rtkit_hold_rx(c->rtk[0]); + + c->status[0] = readl(c->cpu[0]); + writel(0, c->cpu[0]); + readl(c->cpu[0]); + writel(0, c->cpu[0]); + + c->request_ret = c->request ? apple_pmp_report_entry_request(c->request, false) : 0; + + val = readl(c->ps); + writel(val & ~(PS_AUTO_ENABLE | PS_BUSY | PS_FLAGS), c->ps); + val = readl(c->ps); + c->words[1] = val; + + writel((val & ~(PS_AUTO_ENABLE | PS_BUSY | PS_FLAGS | PS_TARGET)) | PS_FLAGS, c->ps); + + c->lower_ret = readl_poll_timeout(c->ps, c->lowered, + !FIELD_GET(PS_ACTUAL, c->lowered) && + !(c->lowered & PS_BUSY), + 10, PS_TIMEOUT_US); + + dev_info(dev, "GPU stopped for the sleep: SRAM %d, idle %d in %u us, mailbox %#x, control %#x, PMP request %d, GFX %#x, %#x then %#x (%d)\n", + c->raise_ret, c->idle[0], c->idle_us[0], c->words[0], c->status[0], + c->request_ret, c->entry, c->words[1], c->lowered, c->lower_ret); + + return c; +} + /* * After the sleep, refused or not: whether the commit stopped the GPU, and in @saved the * domain's word from before, if it did. Frees @c. @@ -760,3 +957,45 @@ bool asahi_g17_commit_finish(struct asahi_g17_commit *c, u32 *saved) return stopped; } + +struct asahi_g16_pm_notify { + struct notifier_block nb; + struct device *dev; + void (*post_suspend)(struct device *dev); +}; + +static int asahi_g16_pm_notify_cb(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct asahi_g16_pm_notify *n = + container_of(nb, struct asahi_g16_pm_notify, nb); + + if (action == PM_POST_SUSPEND) + n->post_suspend(n->dev); + return NOTIFY_DONE; +} + +static void asahi_g16_pm_unregister(void *data) +{ + struct asahi_g16_pm_notify *n = data; + + unregister_pm_notifier(&n->nb); +} + +int asahi_g16_register_pm_notify(struct device *dev, + void (*post_suspend)(struct device *dev)) +{ + struct asahi_g16_pm_notify *n; + int ret; + + n = devm_kzalloc(dev, sizeof(*n), GFP_KERNEL); + if (!n) + return -ENOMEM; + n->dev = dev; + n->post_suspend = post_suspend; + n->nb.notifier_call = asahi_g16_pm_notify_cb; + ret = register_pm_notifier(&n->nb); + if (ret) + return ret; + return devm_add_action_or_reset(dev, asahi_g16_pm_unregister, n); +} diff --git a/drivers/gpu/drm/asahi/g17/sleep.h b/drivers/gpu/drm/asahi/g17/sleep.h index a8a304fd86144..9fb8ed38b2858 100644 --- a/drivers/gpu/drm/asahi/g17/sleep.h +++ b/drivers/gpu/drm/asahi/g17/sleep.h @@ -15,6 +15,12 @@ struct asahi_g17_commit *asahi_g17_commit_prepare(struct device *dev, struct apple_rtkit *secondary_rtk, struct apple_mbox *secondary_mbox, bool bridge); struct asahi_g17_commit *asahi_g16_commit_prepare(struct device *dev, struct apple_rtkit *rtk); +struct asahi_g17_commit *asahi_g16_commit_prepare_ex(struct device *dev, struct apple_rtkit *rtk, + bool quiet, u64 sram_offset, + size_t save_len, size_t restore_len); +struct asahi_g17_commit *asahi_g16_stop_now(struct device *dev, struct apple_rtkit *rtk, + u64 sram_offset, size_t save_len, size_t restore_len); +int asahi_g16_sram_restore(void); bool asahi_g17_commit_finish(struct asahi_g17_commit *c, u32 *saved); bool asahi_g17_commit_required(void); bool asahi_g17_commit_stops_firmware(void); @@ -22,4 +28,6 @@ int asahi_g17_power_auto_off(struct device *dev, u32 *saved); int asahi_g17_power_lower(struct device *dev); int asahi_g17_power_raise(struct device *dev, u32 saved); int asahi_g17_power_auto_on(struct device *dev, u32 saved); +int asahi_g16_register_pm_notify(struct device *dev, + void (*post_suspend)(struct device *dev)); #endif diff --git a/drivers/soc/apple/pmp-telemetry.c b/drivers/soc/apple/pmp-telemetry.c index 0e9e0d078fe8d..4b8501a4d7a72 100644 --- a/drivers/soc/apple/pmp-telemetry.c +++ b/drivers/soc/apple/pmp-telemetry.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -147,6 +148,7 @@ struct apple_pmp_gpu { struct apple_pmp_gpu *apple_pmp_gpu_get(struct device *dev) { struct apple_pmp_telemetry *sensors; + struct platform_device *pdev; struct apple_pmp_gpu *gpu; int ret = -EPROBE_DEFER; @@ -158,10 +160,22 @@ struct apple_pmp_gpu *apple_pmp_gpu_get(struct device *dev) ret = -EINVAL; goto fail; } + pdev = of_find_device_by_node(gpu->node); + if (!pdev) + goto fail; + + /* Probe registers telemetry before binding completes. */ + device_lock(&pdev->dev); + if (device_is_bound(&pdev->dev)) + ret = -ENODEV; rcu_read_lock(); list_for_each_entry_rcu(sensors, &apple_pmp_telemetry_list, list) { - if (sensors->dev->of_node != gpu->node || !READ_ONCE(sensors->online)) + if (sensors->dev != &pdev->dev) continue; + if (!READ_ONCE(sensors->online)) { + ret = -EPROBE_DEFER; + continue; + } spin_lock(&sensors->gpu_lock); gpu->epoch = apple_pmp_gpu_lease_attach(&sensors->gpu); spin_unlock(&sensors->gpu_lock); @@ -174,6 +188,8 @@ struct apple_pmp_gpu *apple_pmp_gpu_get(struct device *dev) break; } rcu_read_unlock(); + device_unlock(&pdev->dev); + platform_device_put(pdev); if (!ret) return gpu; fail: diff --git a/rust/helpers/suspend.c b/rust/helpers/suspend.c index a616907ecc0b5..3f40a5d0687c4 100644 --- a/rust/helpers/suspend.c +++ b/rust/helpers/suspend.c @@ -11,3 +11,8 @@ __rust_helper bool rust_helper_pm_resume_via_firmware(void) { return pm_resume_via_firmware(); } + +__rust_helper bool rust_helper_pm_dark_wake(void) +{ + return pm_dark_wake(); +}