Skip to content
Closed
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
3 changes: 3 additions & 0 deletions drivers/gpu/drm/asahi/driver.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Top-level GPU driver implementation.
Expand Down Expand Up @@ -231,6 +231,9 @@
_pdev: &'bound platform::Device<Core<'_>>,
this: Pin<&Self::Data<'bound>>,
) {
// SAFETY: Takes no arguments, process context. Clears the T6001 AGX request in the PMP report if the pmp_report.agx_vote
// parameter set it; a no-op everywhere else.
unsafe { bindings::apple_pmp_agx_activity_release() };
if let Some(gpu) = this.g17.as_ref() {
// Driver data is dropped after devres, so CPU control must happen in unbind.
gpu.shutdown();
Expand Down
5 changes: 5 additions & 0 deletions drivers/gpu/drm/asahi/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// SPDX-License-Identifier: GPL-2.0-only OR MIT

//! Top-level GPU manager
Expand Down Expand Up @@ -1284,6 +1284,9 @@
.with(|raw, _inner| raw.pending_submissions.fetch_add(1, Ordering::Acquire));

mod_dev_dbg!(self.dev, "OP start (pending: {})\n", val + 1);
// SAFETY: Takes no arguments and does nothing unless pmp_report.agx_vote is set on a T6001. The matching end call is made
// by OpGuard's Drop, and the guard is created right below, so every path out of here (including a failed kick) pairs it.
unsafe { kernel::bindings::apple_pmp_agx_activity_begin() };
let guard = OpGuard(self.clone());
self.kick_firmware()?;
Ok(guard)
Expand Down Expand Up @@ -1804,5 +1807,7 @@
.with(|raw, _inner| raw.pending_submissions.fetch_sub(1, Ordering::Release));

mod_dev_dbg!(self.dev, "OP end (pending: {})\n", val - 1);
// SAFETY: Pairs with the begin call in start_op(); callable from any context.
unsafe { kernel::bindings::apple_pmp_agx_activity_end() };
}
}
126 changes: 126 additions & 0 deletions drivers/pmdomain/apple/pmp-agx-vote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
/*
* When the T6001 AGX request (PS-REQ bit 9 of the PMP report) is set and cleared.
*
* The GPU driver counts operations in flight (begin/end). The request is set when the first operation starts and cleared
* idle_ms after the last one ends, so a decode stream (a burst every ~10 ms) holds one vote and an idle GPU drops it. All state
* changes are in these functions; the caller serializes them with one spinlock and does the register write between
* agx_vote_decide() and agx_vote_applied(). Times are milliseconds from any wrapping clock.
*/
#ifndef PMP_AGX_VOTE_H
#define PMP_AGX_VOTE_H

#ifdef __KERNEL__
#include <linux/types.h>
/* jiffies_to_msecs() returns u32: the clock wraps every 49.7 days, and every compare below is done in 32 bits. */
typedef u32 agx_ms_t;
typedef s32 agx_sms_t;
#else
#include <stdbool.h>
#include <stdint.h>
typedef uint32_t agx_ms_t;
typedef int32_t agx_sms_t;
#endif

/* How long the request is held after the last operation ends. w7X measured idle gaps of up to 111.5 ms inside one 9B generation, so 100 ms
* was not enough; 250 ms is about 2.2 times the largest gap seen. The parameter pmp_report.agx_vote_idle_ms overrides it at run time. */
#define AGX_VOTE_IDLE_MS_DEFAULT 250

/* After a set that was not acknowledged (or a PMP that is not running), wait this long before trying again. */
#define AGX_VOTE_RETRY_MS 1000

struct agx_vote {
bool enabled; /* the module parameter */
bool voted; /* bit 9 is set and was acknowledged */
unsigned int users; /* GPU operations in flight */
agx_ms_t idle_ms; /* keep the vote this long after the last operation ends */
agx_ms_t idle_at; /* when users last fell to 0 */
unsigned int failures; /* consecutive failed sets; the backoff applies only while this is nonzero */
agx_ms_t retry_at; /* with failures: no new set before this */
};

enum agx_vote_action {
AGX_VOTE_NONE,
AGX_VOTE_SET,
AGX_VOTE_CLEAR,
};

static inline bool agx_vote_reached(agx_ms_t now, agx_ms_t t)
{
return (agx_sms_t)(now - t) >= 0;
}

/* An operation starts. True: run the reconcile step now. */
static inline bool agx_vote_begin(struct agx_vote *v)
{
v->users++;
return v->enabled && !v->voted;
}

/* An operation ends. Returns the delay in ms after which the reconcile step must run, 0 when none is needed. */
static inline agx_ms_t agx_vote_end(struct agx_vote *v, agx_ms_t now)
{
if (!v->users) /* unbalanced end: ignored */
return 0;
if (--v->users)
return 0;
v->idle_at = now;
if (!v->enabled || !v->voted)
return 0;
return v->idle_ms ? v->idle_ms : 1;
}

/* What the reconcile step must do now. */
static inline enum agx_vote_action agx_vote_decide(const struct agx_vote *v, agx_ms_t now)
{
if (!v->enabled)
return v->voted ? AGX_VOTE_CLEAR : AGX_VOTE_NONE;
if (v->users)
return !v->voted && (!v->failures || agx_vote_reached(now, v->retry_at)) ? AGX_VOTE_SET : AGX_VOTE_NONE;
if (v->voted && agx_vote_reached(now, v->idle_at + v->idle_ms))
return AGX_VOTE_CLEAR;
return AGX_VOTE_NONE;
}

/* Delay in ms until the reconcile step must run again, 0 when no timer is needed. */
static inline agx_ms_t agx_vote_next(const struct agx_vote *v, agx_ms_t now)
{
agx_ms_t at;

if (!v->enabled)
return 0;
if (v->users && !v->voted && v->failures)
at = v->retry_at;
else if (!v->users && v->voted)
at = v->idle_at + v->idle_ms;
else
return 0;
return agx_vote_reached(now, at) ? 0 : at - now;
}

/* Record the outcome of the register write for `action` (ret 0: acknowledged). */
static inline void agx_vote_applied(struct agx_vote *v, enum agx_vote_action action, int ret, agx_ms_t now)
{
if (action == AGX_VOTE_SET) {
if (ret) {
v->failures++;
v->retry_at = now + AGX_VOTE_RETRY_MS;
} else {
v->voted = true;
v->failures = 0;
}
} else if (action == AGX_VOTE_CLEAR) {
v->voted = false; /* the write was made; the caller logs an unacknowledged clear */
}
}

/* The GPU driver unbinds: drop the vote now. True when bit 9 is set and the caller must write the clear. */
static inline bool agx_vote_release(struct agx_vote *v)
{
bool was_voted = v->voted;

v->voted = false;
return was_voted;
}

#endif /* PMP_AGX_VOTE_H */
Loading
Loading