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
5 changes: 5 additions & 0 deletions drivers/gpu/drm/asahi/g17/clpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ void asahi_clpc_gpu_set_busy(struct apple_clpc_gpu *gpu, unsigned int permille)
apple_clpc_gpu_set_busy(gpu, permille);
}

void asahi_clpc_gpu_clear_busy(struct apple_clpc_gpu *gpu)
{
apple_clpc_gpu_clear_busy(gpu);
}

void asahi_clpc_gpu_put(struct apple_clpc_gpu *gpu)
{
apple_clpc_gpu_put(gpu);
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/clpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ struct apple_clpc_gpu;
int asahi_clpc_gpu_get(void *dev, struct apple_clpc_gpu **out);
void asahi_clpc_gpu_set_active(struct apple_clpc_gpu *gpu, bool active);
void asahi_clpc_gpu_set_busy(struct apple_clpc_gpu *gpu, unsigned int permille);
void asahi_clpc_gpu_clear_busy(struct apple_clpc_gpu *gpu);
void asahi_clpc_gpu_put(struct apple_clpc_gpu *gpu);
#endif
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ impl Drop for Firmware {
secondary.stop();
}
self.primary.stop();
self.port.set_effort_full(false);
self.port.clear_effort_and_busy();
self.primary.state.shared.set_full_effort_published(false);
self.pool.stop_after_firmware();
// RTKit destruction releases the mailbox power references. Clear the bridge while
Expand Down
47 changes: 44 additions & 3 deletions drivers/gpu/drm/asahi/g17/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub(super) struct Port {
/// Mirrored from `busy` and `qos` for a lock-free fast path in `note_gpu_activity()`.
busy_window_start: AtomicU64,
busy_below_top: AtomicBool,
busy_inactive_since: AtomicU64,
oldest_in_flight_any: AtomicU64,
statistics_epoch: AtomicU64,
last_submission_ns: AtomicU64,
Expand Down Expand Up @@ -236,6 +237,7 @@ impl Port {
busy <- new_spinlock!(super::power::BusyShare::new(), "g17::Port::busy"),
busy_window_start: AtomicU64::new(0),
busy_below_top: AtomicBool::new(false),
busy_inactive_since: AtomicU64::new(0),
oldest_in_flight_any: AtomicU64::new(0),
statistics_epoch: AtomicU64::new(0),
last_submission_ns: AtomicU64::new(0),
Expand Down Expand Up @@ -937,19 +939,50 @@ impl Port {
self.submissions.load(Ordering::SeqCst)
}

fn resume_busy_if_inactive(&self, now_ns: u64) {
if self.busy_inactive_since.load(Ordering::Acquire) == 0 {
return;
}
let now_ns = super::now_ns().max(now_ns);
let mut busy = self.busy.lock();
if busy.resume(now_ns) {
self.demand.clear_busy();
self.busy_window_start.store(0, Ordering::Relaxed);
self.busy_below_top.store(false, Ordering::Relaxed);
}
self.busy_inactive_since.store(0, Ordering::Release);
}

pub(super) fn set_effort_full(&self, full: bool) {
self.effort_full.store(full, Ordering::SeqCst);
if full {
self.resume_busy_if_inactive(super::now_ns());
self.demand.set_active(true);
} else {
let now = super::now_ns();
let mut busy = self.busy.lock();
self.demand.set_active(false);
busy.clear_active();
self.busy_window_start.store(0, Ordering::Relaxed);
self.busy_below_top.store(false, Ordering::Relaxed);
if self.demand.has_provider() {
busy.note_inactive(now);
self.busy_inactive_since.store(
busy.inactive_since().map_or(0, |s| s.max(1)),
Ordering::Release,
);
}
}
}

pub(super) fn clear_effort_and_busy(&self) {
self.effort_full.store(false, Ordering::SeqCst);
let mut busy = self.busy.lock();
self.demand.set_active(false);
self.demand.clear_busy();
busy.clear_active();
self.busy_window_start.store(0, Ordering::Relaxed);
self.busy_below_top.store(false, Ordering::Relaxed);
self.busy_inactive_since.store(0, Ordering::Release);
}

/// The next statistics drain and reader discard what the decoder holds.
pub(super) fn statistics_epoch_bump(&self) {
self.statistics_epoch.fetch_add(1, Ordering::Release);
Expand Down Expand Up @@ -988,6 +1021,12 @@ impl Port {
let oldest = self.qos.lock().oldest_in_flight();
let now_ns = super::now_ns().max(now_ns);
let mut busy = self.busy.lock();
if busy.inactive_since().is_some() {
if busy.resume(now_ns) {
self.demand.clear_busy();
}
self.busy_inactive_since.store(0, Ordering::Release);
}
if let Some(permille) = busy.sample(now_ns, retired, oldest) {
self.demand.set_busy(permille);
}
Expand All @@ -1012,6 +1051,7 @@ impl Port {
let mut busy = self.busy.lock();
if let Some(permille) = busy.lift_saturated(now_ns, retired, oldest) {
self.demand.set_busy(permille);
self.busy_inactive_since.store(0, Ordering::Release);
}
self.busy_window_start
.store(busy.window_start().map_or(0, |s| s.max(1)), Ordering::Relaxed);
Expand Down Expand Up @@ -1051,6 +1091,7 @@ impl Port {
self.statistics_epoch.fetch_add(1, Ordering::Release);
}
self.last_submission_ns.store(now, Ordering::Relaxed);
self.resume_busy_if_inactive(now);
self.demand.set_active(true);
self.report_busy(now);
}
Expand Down
86 changes: 82 additions & 4 deletions drivers/gpu/drm/asahi/g17/power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ pub(super) const BUSY_WINDOW_NS: u64 = 200_000_000;
/// A longer gap between reports with no long command in flight was idle: start over
/// rather than average it in.
pub(super) const BUSY_WINDOW_MAX_NS: u64 = 2_000_000_000;
/// Inactive gap (after [`IDLE_HOLD_NS`] ends effort) long enough to forget the prior
/// busy share so a new burst starts at the top CLPC column. Short enough to avoid
/// diluting new bursts after real idle, yet longer than inter-frame gaps of paced
/// low-fps workloads (e.g. 24-30 fps video or VRR, 25-42 ms).
pub(super) const BUSY_IDLE_RESET_NS: u64 = 100_000_000;
/// An unretired command that has been running continuously for this long saturates
/// its engine and immediately lifts the busy share to 1000 permille, without waiting
/// for it to retire or for the current window to end.
Expand All @@ -491,6 +496,7 @@ pub(super) const BUSY_LONG_COMMAND_NS: u64 = 50_000_000;
/// stages retiring together, err toward a higher share, which is the safe direction.
pub(super) struct BusyShare {
start_ns: Option<u64>,
inactive_since: Option<u64>,
retired_ns: [u64; crate::usage::NUM_ENGINES],
in_flight_ns: [u64; crate::usage::NUM_ENGINES],
below_top: bool,
Expand All @@ -500,22 +506,49 @@ impl BusyShare {
pub(super) const fn new() -> Self {
Self {
start_ns: None,
inactive_since: None,
retired_ns: [0; crate::usage::NUM_ENGINES],
in_flight_ns: [0; crate::usage::NUM_ENGINES],
below_top: false,
}
}

/// CLPC cleared GPU activity (forgetting its last share so a new burst starts
/// at the top column). Only called once the GPU is idle: restart the window so
/// the first report of the next burst re-baselines instead of closing a window
/// diluted by the idle gap, which would drop the burst off the top column.
/// Unconditionally resets the window and forgets any prior busy share (for
/// genuine idle expiry or teardown).
pub(super) fn clear_active(&mut self) {
self.start_ns = None;
self.inactive_since = None;
self.in_flight_ns = [0; crate::usage::NUM_ENGINES];
self.below_top = false;
}

/// Marks the GPU inactive after [`IDLE_HOLD_NS`] expires without wiping the
/// measurement window yet. Paced 24-30 fps streams resume well inside
/// [`BUSY_IDLE_RESET_NS`] and keep their window; longer idle gaps clear on
/// [`Self::resume`]. Idempotent while already inactive.
pub(super) fn note_inactive(&mut self, now_ns: u64) {
self.inactive_since.get_or_insert(now_ns);
}

pub(super) fn inactive_since(&self) -> Option<u64> {
self.inactive_since
}

/// Resumes activity at `now_ns` after [`Self::note_inactive`]. Returns `true`
/// when the inactive gap reached [`BUSY_IDLE_RESET_NS`] and the window was
/// cleared so the caller clears CLPC's cached share before reactivating.
pub(super) fn resume(&mut self, now_ns: u64) -> bool {
let Some(since) = self.inactive_since.take() else {
return false;
};
if now_ns.saturating_sub(since) >= BUSY_IDLE_RESET_NS {
self.clear_active();
true
} else {
false
}
}

pub(super) fn below_top(&self) -> bool {
self.below_top
}
Expand All @@ -534,6 +567,7 @@ impl BusyShare {
if !self.below_top {
return None;
}
self.inactive_since = None;
self.start_ns = Some(now_ns);
self.retired_ns = retired_ns;
for (slot, &since) in self.in_flight_ns.iter_mut().zip(oldest_in_flight.iter()) {
Expand Down Expand Up @@ -577,6 +611,7 @@ impl BusyShare {
retired_ns: [u64; crate::usage::NUM_ENGINES],
oldest_in_flight: [u64; crate::usage::NUM_ENGINES],
) -> Option<u32> {
let _ = self.resume(now_ns);
let long_in_flight = Self::is_long_in_flight(now_ns, oldest_in_flight);
let Some(start) = self.start_ns else {
self.start_ns = Some(now_ns);
Expand Down Expand Up @@ -956,6 +991,43 @@ mod tests {
assert!(share.below_top());
}

#[test]
fn paced_low_fps_preserves_window_across_effort_clears() {
const NS_MS: u64 = 1_000_000;
let mut share = BusyShare::new();
let mut retired = 0u64;
let mut reported = None;

// 24 fps light video/compositor (~42 ms period, 0.5 ms fragment work/frame):
// Effort::idle fires 16 ms after each frame, leaving a ~26 ms inactive gap
// (< BUSY_IDLE_RESET_NS). The 200 ms window must survive and close cleanly.
for frame in 0..8u64 {
let t = 1000 * NS_MS + frame * 42 * NS_MS;
assert!(!share.resume(t));
if let Some(p) = share.sample(t, [0, retired, 0], [0, 0, 0]) {
reported = Some((frame, p));
}
retired += 500_000;
// Idempotent note_inactive (Effort::idle calls set_effort_full(false) twice).
share.note_inactive(t + 16 * NS_MS);
share.note_inactive(t + 20 * NS_MS);
assert_eq!(share.inactive_since(), Some(t + 16 * NS_MS));
}
// Frame 5 is at t = 1210 ms (210 ms elapsed, 2.5 ms retired -> 11 permille).
assert_eq!(reported, Some((5, 11)));
assert!(share.below_top());

// A genuine idle gap (>= BUSY_IDLE_RESET_NS) clears the window so the next
// burst starts fresh at the top CLPC column instead of diluting the window.
let last_inactive = 1000 * NS_MS + 7 * 42 * NS_MS + 16 * NS_MS;
let next_burst = last_inactive + BUSY_IDLE_RESET_NS;
assert!(share.resume(next_burst));
assert!(!share.below_top());
assert_eq!(share.window_start(), None);
assert_eq!(share.sample(next_burst, [0, retired, 0], [0, 0, 0]), None);
assert_eq!(share.window_start(), Some(next_burst));
}

#[test]
fn compute_waiters_lift_immediately_and_expire_while_streaming() {
const NS_MS: u64 = 1_000_000;
Expand Down Expand Up @@ -990,6 +1062,7 @@ unsafe extern "C" {
fn asahi_clpc_gpu_set_active(gpu: *mut core::ffi::c_void, active: bool);
fn asahi_clpc_gpu_put(gpu: *mut core::ffi::c_void);
fn asahi_clpc_gpu_set_busy(gpu: *mut core::ffi::c_void, permille: core::ffi::c_uint);
fn asahi_clpc_gpu_clear_busy(gpu: *mut core::ffi::c_void);
}

/// DT-selected memory-demand provider. Its independent allocation outlives a
Expand Down Expand Up @@ -1026,6 +1099,11 @@ impl Demand {
// SAFETY: the retained reference is live; NULL denotes no DT provider.
unsafe { asahi_clpc_gpu_set_busy(self.0, permille.min(1000) as core::ffi::c_uint) };
}

pub(super) fn clear_busy(&self) {
// SAFETY: the retained reference is live; NULL denotes no DT provider.
unsafe { asahi_clpc_gpu_clear_busy(self.0) };
}
}

impl Drop for Demand {
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/g17/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ impl Shared {
}
firmware.rings.invalidate_statistics();
firmware.port.statistics_epoch_bump();
firmware.port.clear_effort_and_busy();
}
result
}
Expand Down
72 changes: 69 additions & 3 deletions drivers/soc/apple/clpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,6 @@ void apple_clpc_gpu_set_active(struct apple_clpc_gpu *gpu, bool active)
if (active && !gpu->active && gpu->wake)
mod_delayed_work(system_freezable_power_efficient_wq, gpu->wake, 0);
gpu->active = active;
/* A new burst after idle starts from the top, not from an old share. */
if (!active)
gpu->busy_valid = false;
gpu->updated_ns = ktime_get_mono_fast_ns();
gpu->sequence++;
spin_unlock_irqrestore(&gpu->lock, flags);
Expand All @@ -333,10 +330,30 @@ void apple_clpc_gpu_set_busy(struct apple_clpc_gpu *gpu, unsigned int permille)
}
EXPORT_SYMBOL_GPL(apple_clpc_gpu_set_busy);

/**
* apple_clpc_gpu_clear_busy() - Forget the GPU busy share after genuine idle
* @gpu: the GPU input, from apple_clpc_gpu_get()
*
* Returns reported GPU work to the top column of &struct apple_clpc_hw.gpu_busy
* until the driver reports a fresh window via apple_clpc_gpu_set_busy().
*/
void apple_clpc_gpu_clear_busy(struct apple_clpc_gpu *gpu)
{
unsigned long flags;

if (!gpu)
return;
spin_lock_irqsave(&gpu->lock, flags);
gpu->busy_valid = false;
spin_unlock_irqrestore(&gpu->lock, flags);
}
EXPORT_SYMBOL_GPL(apple_clpc_gpu_clear_busy);

void apple_clpc_gpu_put(struct apple_clpc_gpu *gpu)
{
if (gpu) {
apple_clpc_gpu_set_active(gpu, false);
apple_clpc_gpu_clear_busy(gpu);
kref_put(&gpu->ref, apple_clpc_gpu_free);
}
}
Expand Down Expand Up @@ -2852,11 +2869,60 @@ static void apple_clpc_floor_test(struct kunit *test)
KUNIT_EXPECT_EQ(test, apple_clpc_floor(clpc, state), 0xf00u);
}

static void apple_clpc_gpu_busy_clear_test(struct kunit *test)
{
struct apple_clpc_hw hw = apple_clpc_hw_t6041;
struct apple_clpc_gpu *gpu = kzalloc_obj(*gpu);
unsigned int demand[APPLE_CLPC_RAILS] = {};

KUNIT_ASSERT_NOT_NULL(test, gpu);
kref_init(&gpu->ref);
spin_lock_init(&gpu->lock);
hw.gpu_busy[APPLE_CLPC_DCS][0] = 1;
hw.gpu_busy[APPLE_CLPC_DCS][1] = 2;
hw.gpu_busy[APPLE_CLPC_DCS][2] = 4;
hw.gpu_busy[APPLE_CLPC_DCS][3] = 5;
hw.gpu_busy[APPLE_CLPC_SOC][3] = 3;
hw.gpu_busy_mid = 160;
hw.gpu_busy_high = 500;
hw.gpu_busy_top = 800;

/* Brief inactivity preserves the reported busy share across paced frames. */
apple_clpc_gpu_set_active(gpu, true);
apple_clpc_gpu_set_busy(gpu, 15);
gpu->level = 0;
gpu->level_since = jiffies - msecs_to_jiffies(APPLE_CLPC_GPU_DOWN_MS);
apple_clpc_gpu_set_active(gpu, false);
KUNIT_EXPECT_TRUE(test, gpu->busy_valid);
KUNIT_EXPECT_EQ(test, gpu->busy, 15u);
apple_clpc_gpu_set_active(gpu, true);
KUNIT_EXPECT_TRUE(test, apple_clpc_gpu_input_demand(&hw, gpu, demand));
KUNIT_EXPECT_EQ(test, demand[APPLE_CLPC_DCS], 1u);
KUNIT_EXPECT_EQ(test, demand[APPLE_CLPC_SOC], 0u);

/* Explicit clear_busy returns the next burst to the top column at once. */
apple_clpc_gpu_clear_busy(gpu);
KUNIT_EXPECT_FALSE(test, gpu->busy_valid);
memset(demand, 0, sizeof(demand));
KUNIT_EXPECT_TRUE(test, apple_clpc_gpu_input_demand(&hw, gpu, demand));
KUNIT_EXPECT_EQ(test, demand[APPLE_CLPC_DCS], 5u);
KUNIT_EXPECT_EQ(test, demand[APPLE_CLPC_SOC], 3u);

/* Releasing the consumer clears both activity and busy_valid. */
kref_get(&gpu->ref);
apple_clpc_gpu_set_busy(gpu, 15);
apple_clpc_gpu_put(gpu);
KUNIT_EXPECT_FALSE(test, gpu->active);
KUNIT_EXPECT_FALSE(test, gpu->busy_valid);
kref_put(&gpu->ref, apple_clpc_gpu_free);
}

static struct kunit_case apple_clpc_policy_cases[] = {
KUNIT_CASE(apple_clpc_step_khz_test),
KUNIT_CASE(apple_clpc_steps_first_test),
KUNIT_CASE(apple_clpc_vote_test),
KUNIT_CASE(apple_clpc_floor_test),
KUNIT_CASE(apple_clpc_gpu_busy_clear_test),
{}
};
static struct kunit_suite apple_clpc_policy_suite = {
Expand Down
Loading
Loading