diff --git a/drivers/gpu/drm/asahi/g17/fw/render.rs b/drivers/gpu/drm/asahi/g17/fw/render.rs index 4061aba57da6b..1aa23f3c23338 100644 --- a/drivers/gpu/drm/asahi/g17/fw/render.rs +++ b/drivers/gpu/drm/asahi/g17/fw/render.rs @@ -380,8 +380,9 @@ pub(crate) struct RenderArgs<'a> { pub(crate) resources: &'a RenderResources, /// Submission ordinal of the render queue pair, from zero. pub(crate) ordinal: u64, - /// No earlier render of the queue pair is in flight at submit. - pub(crate) pair_idle: bool, + /// Whether the tiling descriptor requests a firmware tiling-completion + /// notification ([`TaDescriptor::first_kick`]). + pub(crate) ta_notify: bool, /// Tiling stage. pub(crate) ta: RenderStage, /// Fragment stage. @@ -1195,11 +1196,24 @@ pub(crate) struct TaDescriptor { /// QID of the tiling queue. pub(crate) qid: u32, pub(crate) unk_8be: u32, - /// Non-zero on the first kick of a queue pair, like the fragment descriptor's word at the - /// same position. Set on every kick, the firmware also notifies the host of each pass's - /// tiling completion, which no retirement needs. On 25G83 a render submitted to an idle - /// pair keeps it set: without the notification such a render completes later (render d1 - /// +12%, two clients at depth 1 +18-24%), while queued renders gain 5-7% without it. + /// Non-zero on the first kick of a queue pair, like the fragment descriptor's word at the same + /// position. When set on a subsequent kick, the firmware also notifies the host when that + /// pass's tiling stage completes. That is a retirement policy, not a descriptor layout, so + /// every firmware build that uses [`TaDescriptor::write`] (25G83, 25F84, 26B123, and 26A428; + /// M5 uses `write_m5_commands` and is unaffected) follows [`RenderArgs::ta_notify`] + /// (`crate::g17::queue::render::ta_notify`). + /// + /// Setting it on every kick eliminated multi-pass poll stalls past the 32-render in-flight + /// window (J616C HZ=250 100-pass 8.8 -> 1.6 ms; M4 Pro HZ=1000 32-200 passes -36 to -39%; + /// A18 Pro 26A428 HZ=1000 32-200 passes -13 to -24%), at the cost of extra firmware IRQs on + /// single-pass streams (M4 Pro +4-8%, A18 Pro +6-9% at d4-d64) and small multi-pass frames + /// (A18 Pro 16-pass mean +10.8%, p99 0.495 -> 1.378 ms, IRQs 14x). Setting it on an idle + /// pair keeps depth-1 latency (M4 Pro d1 +12% without it), while deep multi-pass batches + /// request strided notifications once the active ring is at least half full plus on the kick + /// that fills the window (J616C HZ=1000: 16-pass unchanged at 0.099 -> 0.096 ms and 1.00x + /// firmware IRQs, 24-200 passes -22% to -72%, render.light d16 +0.1%, d32 +0.5%). The render + /// kick entry's event mask ([`RENDER_KICK_EVENT_MASK`]) is no substitute: it lists events + /// the kick waits on ([`super::m5::compute::kick_event_mask`]), not one its completion raises. pub(crate) first_kick: u32, /// Completion value of the tiling kick (see /// [`KickTimestamp::completion_value`]). @@ -1306,10 +1320,7 @@ impl TaDescriptor { self.aux_stamp_va = args.ta.aux_stamp_va; self.stamp_value_2 = stamp_value; self.qid = args.ta.qid.into(); - self.first_kick = match args.programs == RegisterPrograms::V25G83 { - true => u32::from(args.ordinal == 0 || args.pair_idle), - false => u32::from(args.ordinal == 0), - }; + self.first_kick = u32::from(args.ta_notify); self.current_id = resources.ta_ids.current.into(); self.ordinal = args.ordinal as u32; self.timestamp_start_va = resources.timestamp_va; @@ -1831,7 +1842,7 @@ mod ta_mode_tests { resources, ray_query: [ray; 2], ordinal: 0, - pair_idle: true, + ta_notify: true, ta: stage, fragment: stage, programs: RegisterPrograms::V25G83, @@ -1939,4 +1950,33 @@ mod ta_mode_tests { } } } + + #[test] + fn ta_descriptor_first_kick_follows_ta_notify_across_all_programs() { + let (pass, resources) = fixture(); + let mut desc = KBox::::new_uninit(GFP_KERNEL).unwrap(); + // SAFETY: `TaDescriptor` implements `Zeroable` and is rewritten by `write()`. + unsafe { + core::ptr::write_bytes(desc.as_mut_ptr(), 0, 1); + } + // SAFETY: The descriptor was zero-initialized above. + let mut desc = unsafe { desc.assume_init() }; + for programs in [RegisterPrograms::V25G83, RegisterPrograms::G17P] { + for (ordinal, ta_notify) in [(0, true), (1, false), (16, true), (31, false), (31, true)] + { + let mut args = args(&pass, &resources, false); + args.programs = programs; + args.ordinal = ordinal; + args.ta_notify = ta_notify; + desc.write(&args).unwrap(); + assert_eq!({ desc.first_kick }, u32::from(ta_notify)); + let expected_93e = if ordinal == 0 { + TaDescriptor::UNK_93E_FIRST_KICK + } else { + [0, 0] + }; + assert_eq!({ desc.unk_93e }, expected_93e); + } + } + } } diff --git a/drivers/gpu/drm/asahi/g17/job.rs b/drivers/gpu/drm/asahi/g17/job.rs index 4278f8b9be256..83b8333cacbe7 100644 --- a/drivers/gpu/drm/asahi/g17/job.rs +++ b/drivers/gpu/drm/asahi/g17/job.rs @@ -129,6 +129,8 @@ pub(crate) struct Packet { pub(crate) order: Order, pub(crate) completion: Arc, pub(crate) footprint: Option>, + /// Number of render passes in the submit ioctl that created this packet. + pub(crate) batch_renders: u16, inputs: KVec, firmware: KVec, publication: AtomicU8, @@ -151,12 +153,17 @@ pub(crate) struct Packet { } impl Packet { + #[expect( + clippy::too_many_arguments, + reason = "Packet captures the validated command, ordering, batch size, and fence inputs." + )] pub(crate) fn new( id: u64, context: Arc, command: Validated, order: Order, completion: Arc, + batch_renders: u16, inputs: KVec, firmware: KVec, ) -> Result> { @@ -176,6 +183,7 @@ impl Packet { order, completion, footprint, + batch_renders, inputs, firmware, publication: AtomicU8::new(0), diff --git a/drivers/gpu/drm/asahi/g17/queue/mod.rs b/drivers/gpu/drm/asahi/g17/queue/mod.rs index fc333ebe5d411..f45c26672bb5d 100644 --- a/drivers/gpu/drm/asahi/g17/queue/mod.rs +++ b/drivers/gpu/drm/asahi/g17/queue/mod.rs @@ -248,6 +248,7 @@ impl Queue { work: Option>, inputs: &[file::SyncItem], destinations: Destinations, + batch_renders: u16, ) -> Result { let engine = Engine::of(&command); let index = engine.index(); @@ -439,6 +440,7 @@ impl Queue { command, order, completion, + batch_renders, checked_inputs, firmware, )?; @@ -609,9 +611,14 @@ impl crate::queue::Queue for Queue { } self.prepare_submission(bytes, objects, &mut scratch)?; let Scratch { pending, guards } = &mut scratch; - let has_render = pending - .iter() - .any(|(command, _, _)| matches!(command, Validated::Render { .. })); + let batch_renders = u16::try_from( + pending + .iter() + .filter(|(command, _, _)| matches!(command, Validated::Render { .. })) + .count(), + ) + .unwrap_or(u16::MAX); + let has_render = batch_renders != 0; let compute_only = !has_render && !pending.is_empty(); if super::timeout::select_submission(pending.iter().map(|(cmd, _, _)| cmd.long_job())) { for (command, _, _) in pending.iter_mut() { @@ -713,6 +720,7 @@ impl crate::queue::Queue for Queue { work_states.next(), outputs.inputs(), destinations, + batch_renders, )?; } Ok(()) diff --git a/drivers/gpu/drm/asahi/g17/queue/render.rs b/drivers/gpu/drm/asahi/g17/queue/render.rs index fb5711ad1274d..e60baf2d299a2 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render.rs @@ -516,6 +516,7 @@ impl Pair { alloc: &Allocator<'_>, packet: &Arc, addresses: RenderAddresses, + backlog: usize, ) -> Result { if !self.room() || self @@ -618,7 +619,7 @@ impl Pair { ray_query: *ray_query, resources: &resources, ordinal, - pair_idle: self.active.len() == 0, + ta_notify: ta_notify(ordinal, self.active.len(), packet.batch_renders, backlog), ta: abi::RenderStage { qid: qids[0], descriptor_va: self.memory.graph.descriptor_client(0, ordinal)?, @@ -3682,6 +3683,71 @@ impl Pair { } } +/// Stride between periodic tiling-completion notifications once a deep +/// multi-pass render stream fills the upper half of a pair's active window. +pub(crate) const TA_NOTIFY_STRIDE: u64 = 4; + +const _: () = assert!(queues::RENDER_DEPTH >= 2); +const _: () = assert!(TA_NOTIFY_STRIDE > 0); + +/// Decides whether a tiling kick sets [`abi::TaDescriptor::first_kick`] to +/// request a firmware tiling-completion notification for a pair of capacity +/// `depth`. +/// +/// - The first kick of a pair (`ordinal == 0`) and any kick to an idle pair +/// (`in_flight == 0`) always notify, preserving idle-pair depth-1 latency. +/// - Single-pass submissions (`batch_renders <= 1`) already complete their own +/// work-state node and raise a fragment completion event without stalling at +/// any queued depth (`d4`-`d64`), so queued kicks add no tiling IRQs. +/// - Small multi-pass frames that fit two frames in flight inside `depth` +/// (`batch_renders <= depth / 2` and `in_flight + backlog <= depth`, such as +/// 16-pass frames at `RENDER_DEPTH == 32`) retire cleanly on each frame's +/// fragment completion without blocking on mid-frame slots. Note that +/// `backlog` is tracked per logical owner while `in_flight` is per pair. +/// - Larger multi-pass batches (`batch_renders > depth / 2`) or multi-pass +/// streams overflowing the active window (`batch_renders > 1` with +/// `in_flight + backlog > depth`) notify once the active ring reaches its +/// halfway mark (`in_flight >= depth / 2`), on every [`TA_NOTIFY_STRIDE`]-th +/// ordinal and on the kick that fills the window (`in_flight + 1 >= depth`) +/// so a 1-3-pass trickle refill near `depth` arms a tiling notification +/// ahead of `queue_poll()` (which remains the fallback for any edge case). +pub(crate) const fn ta_notify_for_depth( + ordinal: u64, + in_flight: usize, + batch_renders: u16, + backlog: usize, + depth: usize, +) -> bool { + if ordinal == 0 || in_flight == 0 { + return true; + } + if batch_renders <= 1 || depth < 2 { + return false; + } + let floor = depth / 2; + let deep_batch = + (batch_renders as usize) > floor || in_flight.saturating_add(backlog) > depth; + deep_batch + && in_flight >= floor + && (ordinal % TA_NOTIFY_STRIDE == 0 || in_flight.saturating_add(1) >= depth) +} + +/// Tiling-completion notification policy for [`queues::RENDER_DEPTH`]. +pub(crate) const fn ta_notify( + ordinal: u64, + in_flight: usize, + batch_renders: u16, + backlog: usize, +) -> bool { + ta_notify_for_depth( + ordinal, + in_flight, + batch_renders, + backlog, + queues::RENDER_DEPTH as usize, + ) +} + /// Whether `masks`, the stamp notifications collected for a pair whose tiling and fragment /// queues are `qids`, can mean a finished pass: the fragment queue's bit. A fragment kick starts /// only after its fragment barrier saw the tiling stamp, and retirement still checks both stamps @@ -3820,3 +3886,160 @@ mod drain_retry_tests { assert!(total < 10_000_000_000); } } + +#[kunit_tests(asahi_g17_render_ta_notify)] +mod ta_notify_tests { + use super::{queues::RENDER_DEPTH, ta_notify, ta_notify_for_depth, TA_NOTIFY_STRIDE}; + + #[test] + fn cold_start_and_idle_pair_always_notify() { + for batch in [0, 1, 16, 32, 64] { + for in_flight in [0, 1, 16, usize::from(RENDER_DEPTH) - 1] { + assert!(ta_notify(0, in_flight, batch, 0)); + } + for ordinal in [0, 1, 2, 3, 4, 15, 31, 100, u64::MAX - 1] { + assert!(ta_notify(ordinal, 0, batch, 0)); + } + } + } + + #[test] + fn single_pass_streams_never_notify_while_busy() { + let depth = usize::from(RENDER_DEPTH); + for ordinal in 1..256 { + for in_flight in 1..depth { + for backlog in [0, 1, 16, 32, 64] { + assert!(!ta_notify(ordinal, in_flight, 1, backlog)); + } + } + } + } + + #[test] + fn sixteen_pass_frames_add_no_extra_notifications() { + let depth = usize::from(RENDER_DEPTH); + let frame_passes: u16 = 16; + // Single 16-pass frame from cold start: only ordinal 0 notifies. + let mut notified = 0; + for pass in 0..usize::from(frame_passes) { + if ta_notify(pass as u64, pass, frame_passes, 0) { + notified += 1; + } + } + assert_eq!(notified, 1); + + // Two 16-pass frames in flight (frame N already occupies 16 slots, + // frame N+1 fills slots 16..31 with total active + backlog <= 32): + // zero extra TA notifications across all 16 passes, even under fallback. + for pass in 0..usize::from(frame_passes) { + let ordinal = 16 + pass as u64; + let in_flight = 16 + pass; + let remaining = usize::from(frame_passes) - pass; + assert!(in_flight + remaining <= depth); + assert!(!ta_notify(ordinal, in_flight, frame_passes, 0)); + assert!(!ta_notify(ordinal, in_flight, frame_passes, remaining)); + } + } + + #[test] + fn deep_multi_pass_frames_notify_on_stride_and_window_fill() { + let depth = usize::from(RENDER_DEPTH); + let floor = depth / 2; + for batch in [32u16, 48, 64] { + // Below floor (in_flight 1..15): no premature notifications. + for pass in 1..floor { + assert!(!ta_notify(pass as u64, pass, batch, 0)); + } + // Upper half (in_flight 16..31): stride + window-fill only. + let mut count = 0; + for pass in floor..depth { + let want = (pass as u64) % TA_NOTIFY_STRIDE == 0 || pass + 1 == depth; + let got = ta_notify(pass as u64, pass, batch, 0); + assert_eq!(got, want); + if got { + count += 1; + } + } + // At depth=32, stride=4: passes 16, 20, 24, 28, and 31 notify (5 of 16). + assert_eq!(count, 5); + } + } + + #[test] + fn saturated_window_never_refills_without_an_armed_notification() { + let depth = usize::from(RENDER_DEPTH); + // Simulate 100-pass and 200-pass frames draining 1, 2, 3, 4, or 8 passes + // per event wakeup while keeping the window saturated at `depth`. + for retire in [1usize, 2, 3, 4, 8] { + let mut ordinal = 32u64; + for _ in 0..200 { + let mut armed = false; + for i in 0..retire { + let in_flight = depth - retire + i; + // Also cover the 8-pass tail chunk of a 200-pass frame while + // the next frame sits in backlog (`in_flight + backlog > depth`). + let batch = if ordinal % 200 >= 192 { 8 } else { 64 }; + let backlog = 64; + if ta_notify(ordinal, in_flight, batch, backlog) { + armed = true; + } + ordinal += 1; + } + assert!(armed); + } + } + } + + #[test] + fn multi_ioctl_overflow_and_floor_oscillation_stay_live() { + let depth = usize::from(RENDER_DEPTH); + let floor = depth / 2; + // Smaller multi-pass ioctls (e.g. 12 passes) that overflow RENDER_DEPTH + // via backlog enable strided + window-fill notifications. + assert!(!ta_notify(20, 20, 12, 0)); + assert!(ta_notify(20, 20, 12, 13)); + assert!(ta_notify(21, depth - 1, 12, 13)); + + // Contiguous runs at or above floor notify within every TA_NOTIFY_STRIDE + // kicks, and rapid oscillation around floor (15 <-> 16) continues to + // deliver periodic notifications whenever a stride lands at/above floor. + let mut contiguous_gap = 0u64; + for step in 0..32u64 { + let got = ta_notify(101 + step, floor, 64, 32); + if got { + contiguous_gap = 0; + } else { + contiguous_gap += 1; + assert!(contiguous_gap < TA_NOTIFY_STRIDE); + } + } + let mut oscillating_notifies = 0u32; + for step in 0..64u64 { + let in_flight = if step % 3 == 0 { floor - 1 } else { floor }; + let got = ta_notify(100 + step, in_flight, 64, 32); + if in_flight < floor { + assert!(!got); + } else if got { + oscillating_notifies += 1; + } + } + assert!(oscillating_notifies >= 10); + } + + #[test] + fn non_power_of_two_depths_and_boundaries_are_safe() { + for depth in [15usize, 24, 30] { + let floor = depth / 2; + assert!(ta_notify_for_depth(0, depth - 1, 1, 0, depth)); + assert!(ta_notify_for_depth(7, 0, 1, 0, depth)); + assert!(!ta_notify_for_depth(8, floor, 1, 64, depth)); + assert!(!ta_notify_for_depth(8, floor, floor as u16, 0, depth)); + assert!(ta_notify_for_depth(8, floor, floor as u16 + 1, 0, depth)); + assert!(ta_notify_for_depth(9, depth - 1, floor as u16 + 1, 0, depth)); + } + // Degenerate depth and saturation boundaries never overflow. + assert!(!ta_notify_for_depth(4, 1, 32, 32, 1)); + assert!(ta_notify_for_depth(u64::MAX - 3, usize::MAX, 64, usize::MAX, 32)); + assert!(!ta_notify_for_depth(u64::MAX, 1, 1, usize::MAX, 32)); + } +} diff --git a/drivers/gpu/drm/asahi/g17/runtime/render/direct.rs b/drivers/gpu/drm/asahi/g17/runtime/render/direct.rs index ed6d477891cc8..b3bbfc62b3d16 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/render/direct.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/render/direct.rs @@ -480,7 +480,12 @@ impl Backend { dev: &dev, uat: &loan.uat, }; - pair.prepare(&alloc, packet, loan.addresses)?; + pair.prepare( + &alloc, + packet, + loan.addresses, + self.backlog.load(Ordering::Acquire) as usize, + )?; let render = port.render_lock(); if !lease.is_current() { return Err(EBUSY); diff --git a/drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs b/drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs index 004c686763441..b05c91b64406f 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs @@ -971,7 +971,12 @@ impl super::super::Backend { Err(error) => return Err(error), } } - pair.prepare(&alloc, packet, addresses) + pair.prepare( + &alloc, + packet, + addresses, + self.backlog.load(core::sync::atomic::Ordering::Acquire) as usize, + ) })(); let mut returned = Some(pair); let mut state = self.shared.state.lock(); diff --git a/drivers/gpu/drm/asahi/g17/runtime/render/replay.rs b/drivers/gpu/drm/asahi/g17/runtime/render/replay.rs index a37ad6b2c8ab8..2a8b406f288c7 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/render/replay.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/render/replay.rs @@ -128,7 +128,7 @@ impl Firmware { dev: &dev, uat: &uat, }; - pair.prepare(&alloc, &packet, addresses) + pair.prepare(&alloc, &packet, addresses, 0) }); match prepared { Ok(()) => {}