Skip to content
Merged
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
10 changes: 9 additions & 1 deletion drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,8 @@ struct Shared {
sleeping: AtomicBool,
/// A sleep holds preparations closed, so a recovery settling meanwhile doesn't reopen them.
sleep_gate: AtomicBool,
/// Deadline to reopen an uncommitted sleep after a late preparation drain; zero otherwise.
sleep_cancel_deadline: AtomicU64,
/// The power state word as it was before the firmware stopped.
sleep_ps: AtomicU32,
/// The stop the sleep's commit makes, from `.prepare` to `.complete`; null otherwise.
Expand Down Expand Up @@ -1361,6 +1363,7 @@ impl WorkItem<POLL_WORK> for Shared {
type Pointer = Arc<Shared>;
fn run(this: Arc<Shared>) {
if this.sleeping() {
this.sleep_cancel_poll();
return;
}
let result = {
Expand Down Expand Up @@ -1509,8 +1512,12 @@ impl Shared {
}

fn queue_poll(self: &Arc<Self>) {
self.queue_poll_after(1);
}

fn queue_poll_after(self: &Arc<Self>, delay_ms: u32) {
let _ = workqueue::system_highpri()
.enqueue_delayed::<Arc<Self>, POLL_WORK>(self.clone(), msecs_to_jiffies(1));
.enqueue_delayed::<Arc<Self>, POLL_WORK>(self.clone(), msecs_to_jiffies(delay_ms));
}

fn queue_governor(self: &Arc<Self>) {
Expand Down Expand Up @@ -1766,6 +1773,7 @@ impl Gpu {
crashed: AtomicBool::new(false),
sleeping: AtomicBool::new(false),
sleep_gate: AtomicBool::new(false),
sleep_cancel_deadline: AtomicU64::new(0),
sleep_ps: AtomicU32::new(0),
sleep_commit: core::sync::atomic::AtomicPtr::new(core::ptr::null_mut()),
feed,
Expand Down
5 changes: 5 additions & 0 deletions drivers/gpu/drm/asahi/g17/preparation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ impl Gate {
}
}

/// A canceled sleep may only retry its own temporary closed epoch.
pub(super) fn closed_and_recoverable(&self) -> bool {
self.state.load(Ordering::Acquire) & (CLOSED | FAILED | REMOVED) == CLOSED
}

/// Reopen only an empty, temporary closure. Failed or removed devices remain closed.
pub(super) fn reopen(&self) -> bool {
let wait = self.wait.lock();
Expand Down
7 changes: 4 additions & 3 deletions drivers/gpu/drm/asahi/g17/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@
#define ASC_CPU_CONTROL 0x44
#define ASC_CPU_CONTROL_RUN BIT(4)

/* Only the supported M5 board needs a wrapped provider translation here.
* Other G17 clients retain their established direct-domain methods below.
/* Only the M5 boards with a deep sleep need a wrapped provider translation here: the J714s
* and the J716c. Other G17 clients retain their established direct-domain methods below.
*/
static bool asahi_g17_m5_provider(struct device *dev)
{
return of_machine_is_compatible("apple,j714s") &&
return (of_machine_is_compatible("apple,j714s") ||
of_machine_is_compatible("apple,j716c")) &&
of_machine_is_compatible("apple,t6050") &&
of_device_is_compatible(dev->of_node, "apple,agx-g17s");
}
Expand Down
87 changes: 79 additions & 8 deletions drivers/gpu/drm/asahi/g17/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ use kernel::{
/// Time work in flight gets to finish before the sleep is refused.
const DRAIN_NS: u64 = 3_000_000_000;
const DRAIN_TICK_MS: i64 = 2;
/// Match the existing maximum compute admission wait before declaring a late builder stalled.
const CANCEL_RETRY_NS: u64 = super::preparation::QUEUE_WAIT_NS;
const CANCEL_CHECK_MS: u32 = 20;

unsafe extern "C" {
fn asahi_g17_pmp_request(dev: *mut core::ffi::c_void, enable: bool) -> core::ffi::c_int;
Expand Down Expand Up @@ -345,9 +348,68 @@ impl Shared {
}
}

/// Undoes everything short of a stopped firmware. A builder that never finishes would keep
/// publication closed for good, and that is a lost device.
fn sleep_cancel(self: &Arc<Self>) {
fn sleep_cancel_reopenable(&self) -> bool {
self.sleeping()
&& self.sleep_gate.load(Ordering::Acquire)
&& !self.crashed.load(Ordering::Acquire)
&& self.sleep_commit.load(Ordering::Acquire).is_null()
&& self.preparations.closed_and_recoverable()
}

/// Only used when the commit never stopped firmware. The gate must join the entire
/// invalidated preparation epoch before new builders can publish into the same session.
fn reopen_cancelled_sleep(self: &Arc<Self>) -> bool {
if !self.sleep_cancel_reopenable() || !self.preparations.reopen() {
return false;
}
self.sleep_cancel_deadline.store(0, Ordering::Release);
// Clear ownership first: another PM prepare cannot pass sleeping() until the gate
// flag is settled, and the retry worker makes no further state changes after that.
self.sleep_gate.store(false, Ordering::Release);
self.sleeping.store(false, Ordering::Release);
self.queue_events();
true
}

/// This poll only retries software admission. It never runs the normal poll or touches
/// registers while asleep, even if the uncommitted session cannot be reopened.
pub(super) fn sleep_cancel_poll(self: &Arc<Self>) {
let deadline = self.sleep_cancel_deadline.load(Ordering::Acquire);
if deadline == 0 {
return;
}
if super::now_ns() >= deadline || !self.sleep_cancel_reopenable() {
self.sleep_cancel_deadline.store(0, Ordering::Release);
dev_warn!(self.dev.as_ref(), "GPU sleep cancellation could not reopen its session\n");
return;
}
if !self.reopen_cancelled_sleep() {
self.queue_poll_after(CANCEL_CHECK_MS);
}
}

/// Undoes everything short of a stopped firmware. T6050 keeps an uncertain session owned:
/// marking it lost here would let the runtime worker stop firmware before the commit.
fn sleep_cancel(self: &Arc<Self>, commit_only: bool) {
if commit_only {
self.sleeping.store(true, Ordering::Release);
self.join_event_workers();
let drained = self
.preparations
.wait_drained_until(super::now_ns().saturating_add(DRAIN_NS));
if drained.is_ok() && self.reopen_cancelled_sleep() {
return;
}
if drained.is_err() && self.sleep_cancel_reopenable() {
self.sleep_cancel_deadline.store(
super::now_ns().saturating_add(CANCEL_RETRY_NS),
Ordering::Release,
);
self.queue_poll_after(CANCEL_CHECK_MS);
}
dev_warn!(self.dev.as_ref(), "GPU sleep cancellation retains its session\n");
return;
}
self.sleeping.store(false, Ordering::Release);
self.sleep_gate.store(false, Ordering::Release);
let drained = self
Expand Down Expand Up @@ -424,8 +486,11 @@ impl Shared {

/// Closes publication, waits for quiet, and leaves the stop to the sleep's commit. A refusal
/// changes nothing.
fn sleep_down(self: &Arc<Self>) -> Result {
fn sleep_down(self: &Arc<Self>, commit_only: bool) -> Result {
if self.sleeping() {
if commit_only {
return Err(EBUSY);
}
let mut state = self.state.lock();
if let Some(firmware) = (*state).as_deref_mut() {
lower_domain(firmware);
Expand All @@ -449,7 +514,8 @@ impl Shared {
});
match result {
Ok(()) => Ok(()),
Err(error) if error == ENODEV || self.crashed.load(Ordering::Acquire) => {
Err(error) if !commit_only &&
(error == ENODEV || self.crashed.load(Ordering::Acquire)) => {
// Nothing to save: let the worker finish detaching what's left, and then the
// domain only has to be off for the sleep.
self.sleeping.store(false, Ordering::Release);
Expand All @@ -464,7 +530,7 @@ impl Shared {
Ok(())
}
Err(error) => {
self.sleep_cancel();
self.sleep_cancel(commit_only);
Err(error)
}
}
Expand Down Expand Up @@ -540,7 +606,7 @@ impl super::Gpu {
);
return Err(EBUSY);
}
let result = self.shared.sleep_down();
let result = self.shared.sleep_down(self.cfg.chip_id == 0x6050);
if let Err(error) = result {
dev_warn!(self.shared.dev.as_ref(), "GPU refused sleep: {:?}\n", error);
}
Expand All @@ -560,11 +626,16 @@ impl super::Gpu {
self.shared.dev.as_ref(),
"sleep refused before the commit: GPU firmware never stopped\n"
);
self.shared.sleep_cancel();
self.shared.sleep_cancel(self.cfg.chip_id == 0x6050);
return;
}
self.shared.sleep_settle_commit();
}
if self.cfg.chip_id == 0x6050 && !real {
// With no physical wake, even an uncertain or stopped session stays owned.
// Neither a refusal nor a missing commit receipt authorizes power or restart.
return;
}
if !real && self.shared.sleep_hold() {
return;
}
Expand Down
Loading