Skip to content
13 changes: 13 additions & 0 deletions drivers/gpu/drm/asahi/g16/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ pub(crate) struct RenderCommand {
/// Explicit hardware attachments in effect for each render engine.
pub(crate) vertex_attachments: KVec<uapi::drm_asahi_attachment>,
pub(crate) fragment_attachments: KVec<uapi::drm_asahi_attachment>,
/// Whether this pass exceeds the light-TA wave threshold (multi-layer or large tilemap).
pub(crate) heavy_ta: bool,
}

impl RenderCommand {
/// Whether this render pass requires the conservative `TA_WAVE_PASSES` wave window.
pub(crate) fn is_heavy_ta(&self) -> bool {
self.heavy_ta
}
}

/// A validated compute command.
Expand Down Expand Up @@ -1517,6 +1526,9 @@ pub(crate) fn parse_stream(bytes: &[u8], early_tiling: bool) -> Result<ParsedStr
};
let vertex_attachments = take_render(&mut vertex, false)?;
let fragment_attachments = take_render(&mut fragment, true)?;
let heavy_ta = packet.layers > 1
|| super::encode::Geometry::new(&packet, 0).tilemap_bytes
> hw::sizes::TA_LIGHT_TILEMAP_BYTES;
commands.push(Command::Render(KBox::new(
RenderCommand {
cmd: packet,
Expand All @@ -1525,6 +1537,7 @@ pub(crate) fn parse_stream(bytes: &[u8], early_tiling: bool) -> Result<ParsedStr
timestamp_outputs: crate::timestamp::Destinations::empty(),
vertex_attachments,
fragment_attachments,
heavy_ta,
},
GFP_KERNEL,
)?))?;
Expand Down
637 changes: 558 additions & 79 deletions drivers/gpu/drm/asahi/g16/engine.rs

Large diffs are not rendered by default.

34 changes: 26 additions & 8 deletions drivers/gpu/drm/asahi/g16/hw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,19 @@ pub(crate) mod sizes {
/// Worst-case firmware work-ring entries per compute command (`fs2c` barrier + cross-pipe
/// `c2c` barrier + `work`).
pub(crate) const MAX_COMPUTE_RING_ENTRIES: usize = 3;
/// Maximum unfinished TA passes kept in flight at once so multi-pass render bursts stay
/// inside M4 firmware's `<= 4`-pass TA/scene pipeline without throttling single-pass
/// pipelining (where TA finishes well ahead of FS).
/// Maximum unfinished TA passes kept in flight at once for heavy multi-layer or large-tilemap
/// renders so heavy bursts stay inside M4 firmware's `<= 4`-pass TA/scene pipeline.
pub(crate) const TA_WAVE_PASSES: usize = 4;
/// Unfinished TA pass count at or below which a TA-wave-paused render queue wakes to
/// Maximum unfinished TA passes kept in flight at once for light single-layer renders
/// (`tilemap_bytes <= TA_LIGHT_TILEMAP_BYTES`), doubling the wave window for light render
/// bursts and deeper micro-coalesced batches.
pub(crate) const TA_WAVE_PASSES_LIGHT: usize = 8;
/// Tilemap byte threshold at or below which a single-layer render qualifies for
/// `TA_WAVE_PASSES_LIGHT`.
pub(crate) const TA_LIGHT_TILEMAP_BYTES: u64 = 64 * 1024;
/// Headroom below the active TA wave cap at which a TA-wave-paused render queue wakes to
/// launch the next wave, avoiding 1-pass stub drips.
pub(crate) const TA_WAVE_REFILL: usize = TA_WAVE_PASSES / 2;
pub(crate) const TA_WAVE_REFILL_HEADROOM: usize = 2;
/// Base render pass slots allocated on first render (full TPC/tilemap capacity).
pub(crate) const BASE_RENDER_SLOTS: usize = 2;
/// Retained render pass slots per VM at most. A slot is reused only after a later render
Expand Down Expand Up @@ -896,10 +902,22 @@ pub(crate) mod sizes {
);
/// Maximum jobs folded into one barrier-free compute horizon prefix.
pub(crate) const HORIZON_JOBS: u32 = COMPUTE_UNITS as u32;
/// Jobs admitted to the engine per logical queue: enough to keep a queue's commands
/// in flight back to back without scheduler backpressure stalls; exact prior-command
/// Maximum commands accumulated in a busy-state horizon micro-coalescing batch before
/// immediate publication, and maximum hold window (18 us) while a queue already has >= 2
/// active commands in flight.
pub(crate) const COALESCE_MAX_CMDS: usize = 6;
pub(crate) const COALESCE_WINDOW_US: u32 = 18;
pub(crate) const COALESCE_WINDOW_NS: u64 = (COALESCE_WINDOW_US as u64) * 1000;
/// Per-queue retired CDM execution EWMA threshold (microseconds) at or above which
/// barrier-free compute fans out across both CDM pipes instead of staying 1-pipe affine.
pub(crate) const COMPUTE_HEAVY_EMA_US: u32 = 200;
/// Elapsed wall-clock time (nanoseconds) on a sole active single-command compute flight
/// that immediately classifies the queue as heavy before its first retirement updates EWMA.
pub(crate) const COMPUTE_HEAVY_INFLIGHT_NS: u64 = 180_000;
/// Jobs admitted to the engine per logical queue: enough to keep deep queues (`d=64`)
/// queued back to back without scheduler backpressure stalls; exact prior-command
/// dependencies still gate each one.
pub(crate) const SCHEDULER_CREDITS: u32 = 32;
pub(crate) const SCHEDULER_CREDITS: u32 = 64;
/// Unconsumed records per 256-entry channel ring a prefix may be posted behind.
pub(crate) const AHEAD_RECORDS: u32 = 128;
/// Policy bound on logical queues reported in QUEUE_LIMITS (not a firmware limit).
Expand Down
158 changes: 127 additions & 31 deletions drivers/gpu/drm/asahi/g16/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ impl Shared {
self.mod_poll(kernel::time::msecs_to_jiffies((delay_ns / 1_000_000) as u32).max(1));
}

/// Queues the backup poll for idle housekeeping that a pass skipped inside its cadence, at
/// the end of that cadence. It may replace a later idle power poll: that pass runs the
/// housekeeping, including `service_power()`, and arms the power poll again.
fn queue_owed_housekeeping(self: &Arc<Self>, firmware: &Firmware) {
if !firmware.engine.housekeeping_owed || firmware.recovery.failed().is_some() {
return;
}
let elapsed = now_ns().wrapping_sub(firmware.last_housekeeping_ns);
let ms = HOUSEKEEPING_NS.saturating_sub(elapsed).div_ceil(1_000_000) as u32;
self.mod_poll(kernel::time::msecs_to_jiffies(ms).max(1));
}

/// One poll pass (the poll work item).
pub(crate) fn run_poll(self: Arc<Self>) {
// The domain may be off. The sleep's end queues a pass.
Expand Down Expand Up @@ -141,12 +153,24 @@ impl Shared {
) {
Ok(true) if retry_tail => {}
Ok(true) => {
self.rearm(firmware.engine.chan_wait || firmware.engine.short_repoll)
let soon = firmware.engine.chan_wait || firmware.engine.short_repoll;
if firmware.engine.coalesce_deadline_ns != 0 && !soon {
let rem_us = (firmware
.engine
.coalesce_deadline_ns
.saturating_sub(now_ns())
.div_ceil(1000) as u32)
.clamp(1, super::hw::sizes::COALESCE_WINDOW_US);
self.rearm_after(rem_us.min(self.rearm_us(false)));
} else {
self.rearm(soon);
}
}
// Idle: arm a delayed poll while DVFS utilization is decaying or a CLPC
// floor hold is still pending so both expire cleanly to idle power.
Ok(false) => {
self.arm_idle_power_poll(firmware);
self.queue_owed_housekeeping(firmware);
if firmware.recovery.failed().is_some() {
self.cancel_resnap();
} else {
Expand Down Expand Up @@ -271,20 +295,26 @@ impl Shared {
Ok(())
});
}
let mut coalesce_us = None;
if pub_res.is_ok() {
if retired_any || !had_flights {
firmware.reclaim_closed_vm_slots();
}
pub_res = if recovery_pending {
let r = firmware.publish(done);
if r.is_ok() {
firmware.last_housekeeping_ns = now;
firmware.last_aux_drain_ns = now;
}
r
} else {
firmware.publish_checked(done)
};
if !recovery_pending && !retired_any && !restored_event && had_flights {
coalesce_us = firmware.defer_busy_coalesce(now);
}
if coalesce_us.is_none() {
pub_res = if recovery_pending {
let r = firmware.publish(done);
if r.is_ok() {
firmware.last_housekeeping_ns = now;
firmware.last_aux_drain_ns = now;
}
r
} else {
firmware.publish_checked(done)
};
}
}
if pub_res.is_ok()
&& !firmware.engine.flights.is_empty()
Expand All @@ -299,12 +329,24 @@ impl Shared {
if !firmware.engine.flights.is_empty() || firmware.engine.chan_wait {
// If an EP 0x20 event retired an older flight while later flights remain active,
// or arrived before the oldest receipt became visible, arm a short repoll so
// trailing completions never fall back to `resnap_us`.
self.rearm(
firmware.engine.chan_wait
|| restored_event
|| (was_evented && retired_any && !firmware.engine.flights.is_empty()),
);
// trailing completions never fall back to `resnap_us`. A ready job this
// publication left waiting on TA wave or slot headroom repolls as soon as
// `poll_step()` would rearm it, not a full `resnap_us` later.
if let Some(us) = coalesce_us {
self.rearm_after(us.min(self.rearm_us(false)));
} else {
let paced = !firmware.engine.frozen
&& !firmware.engine.pending.is_empty()
&& (firmware.engine.slot_wait
|| firmware.engine.ta_wave_wait
|| firmware.engine.held_wait);
self.rearm(
firmware.engine.chan_wait
|| restored_event
|| paced
|| (was_evented && retired_any && !firmware.engine.flights.is_empty()),
);
}
} else {
firmware.power.report_work(false);
self.arm_idle_power_poll(firmware);
Expand Down Expand Up @@ -527,10 +569,15 @@ impl Shared {
/// Re-checks a published command soon: hrtimer, with a throttled 1-jiffy backup; sooner while
/// a ready prefix waits for record consumption or a post-event receipt tail is settling.
pub(crate) fn rearm(self: &Arc<Self>, soon: bool) {
self.rearm_after(self.rearm_us(soon));
}

/// Arms the poll hrtimer for `us` microseconds (coalescing with an already-armed deadline
/// within 5 us) plus the throttled 1-jiffy backup poll.
pub(crate) fn rearm_after(self: &Arc<Self>, us: u32) {
if self.sleeping.load(Ordering::Acquire) {
return;
}
let us = self.rearm_us(soon);
let now = now_ns();
let target_ns = now.saturating_add(u64::from(us) * 1000);
let armed_ns = self.resnap_deadline_ns.load(Ordering::Acquire);
Expand Down Expand Up @@ -638,14 +685,29 @@ impl Firmware {
// failure of the next prefix. Drain the receive rings before deciding, published or
// not, so the error count is current and idle log/trace/stats traffic is consumed. With
// prefixes published the retirement loop below drains first.
//
// Inside the housekeeping cadence, with no recovery pending, an idle pass drains only
// the event and statistics rings, which carry completion and recovery notifications.
// The session, object, power and auxiliary ring checks then wait for the cadence, so
// back-to-back shallow submissions do not repeat them, under the device lock, between
// one completion and the next SUBMIT. `run_poll()` queues a backup poll for them.
if !had_flights {
self.session.check()?;
self.objects.health()?;
self.service_power()?;
self.last_housekeeping_ns = now;
self.last_aux_drain_ns = now;
ran_housekeeping = true;
self.drain_rings()?;
let full = housekeeping_due || self.recovery.receipt_fault || {
self.drain_events_and_stats()?;
self.recovery_pending()?
};
if full {
self.session.check()?;
self.objects.health()?;
self.service_power()?;
self.last_housekeeping_ns = now;
self.last_aux_drain_ns = now;
ran_housekeeping = true;
self.engine.housekeeping_owed = false;
self.drain_rings()?;
} else {
self.engine.housekeeping_owed = true;
}
self.reclaim_closed_vm_slots();
if self.recovery_idle()? && self.engine.pending.is_empty() {
let _ = self.slots.take_scrub_woke();
Expand Down Expand Up @@ -683,6 +745,8 @@ impl Firmware {
// while `publish_woke` covers external state changes (VM close, unstarted queue
// cancellation) and `ta_wave_ready()` wakes TA-wave-paced renders when a `0x70` TA
// pass finishes while FS is still running.
let coalesce_due = self.engine.coalesce_deadline_ns != 0
&& (!evented || now >= self.engine.coalesce_deadline_ns);
let should_publish = !self.engine.pending.is_empty()
&& (retired_any
|| !had_flights
Expand All @@ -692,6 +756,7 @@ impl Firmware {
|| scrub_woke
|| publish_woke
|| self.ta_wave_ready()
|| coalesce_due
|| housekeeping_due);
let published = if should_publish {
self.publish_checked(done)
Expand All @@ -714,15 +779,17 @@ impl Firmware {
published?;
// A TA-only (0x70) EP 0x20 interrupt while FS is still running does not signal flight
// retirement: avoid burning the receipt-tail spin or arming a short repoll storm for it,
// unless a render queue is actively waiting on TA wave headroom (`ta_wave_wait`).
// unless a render queue is actively waiting on TA wave headroom (`ta_wave_wait`) or a
// ready job waits behind its own VM's flights (`held_wait`).
let non_terminal_only = !retired_any
&& term_before.is_some()
&& self.events.stage_events != stage_before
&& self.oldest_terminal_events() == term_before
&& !self.oldest_stamp_reached()
&& !self.recovery.receipt_fault
&& !recovery_pending
&& !self.engine.ta_wave_wait;
&& !self.engine.ta_wave_wait
&& !self.engine.held_wait;
// After an EP 0x20 event, retry up to `EVENT_RETIRE_POLLS` times (outside the device
// lock) only while no flight has retired yet and flights remain in flight. Once any
// flight retires, stop spinning so deeper queues do not burn tail waits per interrupt.
Expand All @@ -748,13 +815,16 @@ impl Firmware {
}
// If a terminal EP 0x20 event arrived with flights outstanding and nothing retired within
// the trailing polls, or a ready same-VM pending job is blocked on slot/flight/work-ring
// or TA-wave headroom, repoll at `repoll_us` instead of sleeping `resnap_us`.
// or TA-wave headroom, or held behind its own VM's flights, repoll at `repoll_us`
// instead of sleeping `resnap_us`.
self.engine.short_repoll = !self.engine.flights.is_empty()
&& !self.engine.frozen
&& !non_terminal_only
&& ((evented && had_flights && !retired_any)
|| (!self.engine.pending.is_empty()
&& (self.engine.slot_wait || self.engine.ta_wave_wait)));
&& (self.engine.slot_wait
|| self.engine.ta_wave_wait
|| self.engine.held_wait)));
Ok(!self.engine.flights.is_empty()
|| self.engine.chan_wait
|| self.recovery.needs_repoll()
Expand Down Expand Up @@ -782,6 +852,7 @@ impl Firmware {
self.last_aux_drain_ns = now;
}
self.last_housekeeping_ns = now;
self.engine.housekeeping_owed = false;
self.reclaim_closed_vm_slots();
if !self.engine.flights.is_empty() || recovery_pending {
self.check_hangs(done)?;
Expand Down Expand Up @@ -816,10 +887,13 @@ impl Firmware {
Some((compute, render))
}

/// Whether a render job paused by `TA_WAVE_PASSES` pacing has enough TA headroom to publish
/// Whether a render job paused by TA wave pacing has enough TA headroom to publish
/// the next wave without dribbling 1-pass stub flights.
fn ta_wave_ready(&self) -> bool {
self.engine.ta_wave_wait && self.ta_pending_passes() <= super::hw::sizes::TA_WAVE_REFILL
let cap = self.active_ta_wave_cap();
self.engine.ta_wave_wait
&& self.ta_pending_passes()
<= cap.saturating_sub(super::hw::sizes::TA_WAVE_REFILL_HEADROOM)
}

/// Cheap single-stage stamp check for the oldest compute pipe 0/1 and oldest render flights'
Expand Down Expand Up @@ -1836,6 +1910,8 @@ impl Firmware {
};
let (mut lo, mut hi) = (u64::MAX, 0u64);
let (mut first, mut last) = (u64::MAX, 0u64);
let mut cdm_flight_ticks = 0u64;
let mut cdm_counted = 0u32;
let work_ticks = self.init.abi.compute().work_ticks;
let s = &mut self.engine.stats;

Expand Down Expand Up @@ -1872,6 +1948,10 @@ impl Firmware {
&mut s.cdm_ticks,
crate::usage::Engine::Compute,
);
if ticks[1] >= ticks[0] {
cdm_flight_ticks = ticks[1] - ticks[0];
cdm_counted = 1;
}
} else {
// Multi-command prefix fallback: read each member's internal timestamp cells.
let mut stage = |o: &super::object::FwObject,
Expand Down Expand Up @@ -1910,9 +1990,20 @@ impl Firmware {
};
if let Ok((start, end)) = Self::compute_ticks_with(work_ticks, c) {
add_interval(start, end, &mut s.cdm_ticks, crate::usage::Engine::Compute);
if start != 0 && end >= start {
cdm_flight_ticks = cdm_flight_ticks.saturating_add(end - start);
cdm_counted += 1;
}
}
}
}
if cdm_counted != 0 {
let per_cmd_ticks = cdm_flight_ticks / u64::from(cdm_counted);
let per_cmd_us = (per_cmd_ticks.saturating_mul(1_000_000)
/ super::hw::COMMAND_TIMESTAMP_HZ)
.clamp(1, 100_000) as u32;
f.job.queue.note_compute_us(per_cmd_us);
}
if hi > lo {
s.span_ticks = s.span_ticks.saturating_add(hi - lo);
}
Expand Down Expand Up @@ -2177,6 +2268,11 @@ impl Firmware {
s.hold[1],
s.hold[2]
)?;
write!(f, "hold_causes:")?;
for (name, n) in super::engine::HoldCause::NAMES.iter().zip(s.hold_causes) {
write!(f, " {} {}", name, n)?;
}
writeln!(f)?;
}
let p = &self.engine.prof;
writeln!(
Expand Down
Loading