From fefbbe4b63d6149db185ab2c0bf2d6424a42f738 Mon Sep 17 00:00:00 2001 From: AxxzyWasTaken Date: Fri, 20 Nov 2026 11:29:48 +0000 Subject: [PATCH 1/2] drm/asahi: g17: notify every tiling completion again Commit 1a890ba49266 ("drm/asahi: g17: drop the 25G83 tiling notification for queued renders") clears the tiling descriptor's first-kick word on a kick to a pair that has renders in flight. That word also makes the firmware notify the host when the pass's tiling stage completes. Without it, a render queued beyond the 32-render in-flight window raises no firmware event when it completes, so it retires only on the next poll, a jiffy later: 4 ms at HZ=250 and 1 ms at HZ=1000. Set the word on every kick again, as 25G83 did before 1a890ba49266, and do it on every firmware build that uses TaDescriptor::write (25G83, 25F84, 26B123, and 26A428; M5 uses write_m5_commands and is unaffected): whether to ask for a tiling-completion notification is a retirement policy, not part of the descriptor layout that follows the build, so 26A428 no longer sets it on the first kick only. Each queued pass's notification raises an event pass, which retires the renders finished by then and publishes the next ones, so retirement follows the firmware's events instead of the poll. The render kick entry's event mask is no substitute: it lists events the kick waits on (fw::m5::compute::kick_event_mask), not one its completion raises. On a J616C (T6041, 25G83, HZ=250), a Vulkan client with two frames in flight and N near-empty render passes per frame: passes/frame 100 200 notification idle pair only 8.8 ms 18.3 ms + 100 us rescan after a poll 3.1-3.7 ms notification on every kick 1.6 ms 3.2 ms (32 passes 0.34 ms, 48 passes 0.86 ms.) Setting it only on an idle pair or on the kick that fills the window gave 4.2 ms for 100 passes. The rescan row is a timed rescan 100 us after each poll request, tried first and dropped for this. CS2 in a fixed scene (offline de_dust2, -nosound, 1728x1117, MSAA 4x, FSR): ~102 fps without the notification on queued kicks, 158 fps with the rescan, 178.6-179.2 fps with this (two runs of three windows). Firmware interrupts during the run rise from ~1.4k/s to ~17.7k/s. No GPU faults, hangs or recoveries. On an M4 Pro (J616s, T6040, 25G83, HZ=1000; 8 A/B boots, medians) the same kind of client takes 36-39% less time per frame from 32 to 200 passes (100 passes 3.63 -> 2.25 ms, p99 frame interval 4.73 -> 2.54 ms), with firmware interrupts at 100 passes up from ~3.5k/s to ~42.6k/s. The cost is the one that commit avoided: a single queue of one-render submissions is slower at every queued depth, 3.8% at d4, 7.5% at d16, 8.5% at d32 and 5.7% at d64 (us/submit, ranges not overlapping from d4). Depth-1 latency, two processes, two queues, fill and compute stay within run-to-run noise. On an A18 Pro (J700, T8140, 26A428, HZ=1000; 8 A/B boots, medians) the multi-pass stall is real too: frames of 32-200 passes take 13.1-24.1% less time (200-pass p99 frame interval 8.189 -> 6.498 ms, -20.7%), with 802 Pass / 0 Fail (337 NotSupported) on the 1139-case dEQP-VK subset and no GPU faults. The cost on 26A428 is larger than on the M4 Pro: 16-pass frames are 10.8% slower on the mean (0.390 -> 0.431 ms) with p99 frame interval 0.495 -> 1.378 ms (+178%); one-render submissions are 5.9-9.0% slower at depths d4-d64 (non-overlapping ranges); two and four processes are 7.6% and 8.2% slower, two queues 6.7%; and firmware IRQs at 100 passes rise 14x (~2.7k -> ~37.6k/s). Depth-1 latency, heavy fill, and compute stay within noise. Signed-off-by: AxxzyWasTaken --- drivers/gpu/drm/asahi/g17/fw/render.rs | 33 ++++++++++++++--------- drivers/gpu/drm/asahi/g17/queue/render.rs | 1 - 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/fw/render.rs b/drivers/gpu/drm/asahi/g17/fw/render.rs index 023a633230c3c..bf3bb0c5170b8 100644 --- a/drivers/gpu/drm/asahi/g17/fw/render.rs +++ b/drivers/gpu/drm/asahi/g17/fw/render.rs @@ -388,8 +388,6 @@ 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, /// Tiling stage. pub(crate) ta: RenderStage, /// Fragment stage. @@ -1203,11 +1201,26 @@ 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. Set on every kick, the firmware also notifies the host of each pass's tiling + /// completion. That is a retirement policy, not a descriptor layout, so every firmware build + /// that uses [`TaDescriptor::write`] (25G83, 25F84, 26B123, and 26A428; M5 does not use this + /// descriptor) keeps it set on every kick. A render queued beyond the 32-render in-flight + /// window raises no other firmware event when it completes, so these notifications are the + /// events that retire the queued renders and publish the next ones; without them those renders + /// wait for the poll, a jiffy later. On the J616C (HZ=250), 100 near-empty passes per frame + /// with two frames in flight take 1.6 ms with it, 8.8 ms without it on queued kicks and 4.2 ms + /// with it only on an idle pair or the kick that fills the window. A render submitted to an + /// idle pair also completes later without it (M4 Pro: render d1 +12%, two clients at depth 1 + /// +18-24%). The cost of the notification on queued kicks: on the M4 Pro (25G83, HZ=1000) a + /// single queue of one-render submissions is 4-8% slower at depths 4-64 while frames of 32-200 + /// passes take 36-39% less time; on the A18 Pro (J700, 26A428, HZ=1000) frames of 32-200 + /// passes take 13-24% less time (200-pass p99 -20.7%), while 16-pass frames are 10.8% slower + /// on the mean with p99 frame interval 0.495 to 1.378 ms (+178%), one-render submissions are + /// 6-9% slower at depths 4-64, two and four processes are 7.6% and 8.2% slower, two queues are + /// 6.7% slower, and firmware IRQs rise 14x. 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`]). @@ -1314,10 +1327,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 = 1; self.current_id = resources.ta_ids.current.into(); self.ordinal = args.ordinal as u32; self.timestamp_start_va = resources.timestamp_va; @@ -1883,7 +1893,6 @@ mod ta_mode_tests { resources, ray_query: [ray; 2], ordinal: 0, - pair_idle: true, ta: stage, fragment: stage, programs: RegisterPrograms::V25G83, diff --git a/drivers/gpu/drm/asahi/g17/queue/render.rs b/drivers/gpu/drm/asahi/g17/queue/render.rs index 27dbcaab8ce7e..6477b18de21fb 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render.rs @@ -617,7 +617,6 @@ impl Pair { ray_query: *ray_query, resources: &resources, ordinal, - pair_idle: self.active.len() == 0, ta: abi::RenderStage { qid: qids[0], descriptor_va: self.memory.graph.descriptor_client(0, ordinal)?, From 82783e2782d3bbe09a6ced07872cc6356bd229c8 Mon Sep 17 00:00:00 2001 From: AxxzyWasTaken Date: Wed, 25 Nov 2026 08:53:14 +0000 Subject: [PATCH 2/2] drm/asahi: g17: stride tiling notifications to deep multi-pass renders Setting TaDescriptor::first_kick unconditionally on every kick eliminated multi-pass poll stalls past the 32-render in-flight window on every TaDescriptor::write firmware build (25G83, 25F84, 26B123, and 26A428; M5 uses write_m5_commands and is unaffected), but regressed small multi-pass frames (A18 Pro 16-pass +10.8% mean, p99 0.495 -> 1.378 ms) and single-pass streams (M4 Pro +4-8%, A18 Pro +6-9% at d4-d64, 2/4 processes +7.6%/+8.2%, 2 queues +6.7%) while raising firmware IRQs 12-14x. Replace the unconditional first_kick = 1 with a single workload-driven retirement policy shared identically across all TaDescriptor::write builds (no chip, board, or firmware-build gating): - Keep tiling notification enabled on a pair's first kick (ordinal == 0) and on any kick submitted to an idle pair (in_flight == 0), preserving depth-1 / idle-pair latency. - Keep queued single-pass submissions (batch_renders <= 1) free of tiling notifications at all depths (d4-d64): each one-render ioctl owns its own work-state node and already raises a fragment completion event without stalling. - Keep small multi-pass frames that fit two frames in flight within RENDER_DEPTH (batch_renders <= RENDER_DEPTH / 2 and in_flight + backlog <= RENDER_DEPTH, e.g. 16-pass double-buffered frames at RENDER_DEPTH = 32) free of extra tiling notifications so each frame retires on its own fragment completion without extra IRQs. - Enable periodic tiling notifications for deep multi-pass batches (batch_renders > RENDER_DEPTH / 2) and multi-pass streams overflowing the active ring (batch_renders > 1 with in_flight + backlog > RENDER_DEPTH) once the active ring is at least half full (in_flight >= RENDER_DEPTH / 2), firing every TA_NOTIFY_STRIDE (4) ordinals and on the window-filling kick (in_flight + 1 >= RENDER_DEPTH) so a 1-3-pass trickle refill near RENDER_DEPTH arms a tiling notification ahead of queue_poll() (which remains the backstop). Note that idle-pair kicks still set first_kick, so single-depth multi- client behavior on 26A428 (A18 Pro 2/4 procs and 2 queues) still needs a fresh A18 Pro A/B run alongside 16-pass, 17-pass, and triple-buffered frames. Measured on J616C (T6041 M4 Max, 25G83, HZ=1000; 7.3.0-j616c-main25nov+ vs this change, 0 GPU faults): vkstress (2 frames in flight, 398 frames): 16 passes: 0.099 -> 0.096 ms mean (-3.0%), p99 0.300 -> 0.200 ms, fw IRQs 2.7k -> 2.7k/s (401 -> 401, 1.00x) 24 passes: 0.462 -> 0.127 ms mean (-72.5%), p99 0.900 -> 0.500 ms 32 passes: 0.701 -> 0.229 ms mean (-67.3%), p99 1.600 -> 0.700 ms 48 passes: 0.992 -> 0.768 ms mean (-22.6%), p99 1.500 -> 1.000 ms 64 passes: 1.308 -> 1.008 ms mean (-22.9%), p99 1.600 -> 1.200 ms 100 passes: 2.036 -> 1.544 ms mean (-24.2%), p99 2.400 -> 1.800 ms, fw IRQs 2.9k -> 16.6k/s (5.7x vs 12-14x on every-kick) 200 passes: 3.651 -> 2.847 ms mean (-22.0%), p99 4.500 -> 3.900 ms gpubench-vk (medians, 5 runs, us/submit): render.light d1: 109.99 -> 110.77 (+0.7%) render.light d4: 31.34 -> 32.18 (+2.7%) render.light d16: 25.40 -> 25.43 (+0.1%) render.light d32: 25.73 -> 25.86 (+0.5%) render.light d64: 23.38 -> 24.17 (+3.4%) render.light 2p: 68.10 -> 66.97 (-1.7%) render.light 4p: 39.44 -> 39.36 (-0.2%) render.light 2q: 66.56 -> 65.77 (-1.2%) render.heavy d16: 80.13 -> 80.13 ( 0.0%) compute.light d64: 11.50 -> 11.46 (-0.4%) Add KUnit tests covering cold start, single-pass streams across all depths, 16-pass single- and double-buffered frames, 32-200-pass strided and window-filling retirement, multi-ioctl overflow, floor oscillation, non-power-of-two depths, and uniform TaDescriptor::first_kick writing across both RegisterPrograms variants. Signed-off-by: AxxzyWasTaken --- drivers/gpu/drm/asahi/g17/fw/render.rs | 71 ++++-- drivers/gpu/drm/asahi/g17/job.rs | 8 + drivers/gpu/drm/asahi/g17/queue/mod.rs | 14 +- drivers/gpu/drm/asahi/g17/queue/render.rs | 224 ++++++++++++++++++ .../drm/asahi/g17/runtime/render/direct.rs | 7 +- .../drm/asahi/g17/runtime/render/prepare.rs | 7 +- .../drm/asahi/g17/runtime/render/replay.rs | 2 +- 7 files changed, 307 insertions(+), 26 deletions(-) diff --git a/drivers/gpu/drm/asahi/g17/fw/render.rs b/drivers/gpu/drm/asahi/g17/fw/render.rs index bf3bb0c5170b8..04c32ffca1b43 100644 --- a/drivers/gpu/drm/asahi/g17/fw/render.rs +++ b/drivers/gpu/drm/asahi/g17/fw/render.rs @@ -388,6 +388,9 @@ pub(crate) struct RenderArgs<'a> { pub(crate) resources: &'a RenderResources, /// Submission ordinal of the render queue pair, from zero. pub(crate) ordinal: u64, + /// 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. @@ -1202,25 +1205,23 @@ pub(crate) struct TaDescriptor { 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. That is a retirement policy, not a descriptor layout, so every firmware build - /// that uses [`TaDescriptor::write`] (25G83, 25F84, 26B123, and 26A428; M5 does not use this - /// descriptor) keeps it set on every kick. A render queued beyond the 32-render in-flight - /// window raises no other firmware event when it completes, so these notifications are the - /// events that retire the queued renders and publish the next ones; without them those renders - /// wait for the poll, a jiffy later. On the J616C (HZ=250), 100 near-empty passes per frame - /// with two frames in flight take 1.6 ms with it, 8.8 ms without it on queued kicks and 4.2 ms - /// with it only on an idle pair or the kick that fills the window. A render submitted to an - /// idle pair also completes later without it (M4 Pro: render d1 +12%, two clients at depth 1 - /// +18-24%). The cost of the notification on queued kicks: on the M4 Pro (25G83, HZ=1000) a - /// single queue of one-render submissions is 4-8% slower at depths 4-64 while frames of 32-200 - /// passes take 36-39% less time; on the A18 Pro (J700, 26A428, HZ=1000) frames of 32-200 - /// passes take 13-24% less time (200-pass p99 -20.7%), while 16-pass frames are 10.8% slower - /// on the mean with p99 frame interval 0.495 to 1.378 ms (+178%), one-render submissions are - /// 6-9% slower at depths 4-64, two and four processes are 7.6% and 8.2% slower, two queues are - /// 6.7% slower, and firmware IRQs rise 14x. 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. + /// 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`]). @@ -1327,7 +1328,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 = 1; + 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; @@ -1893,6 +1894,7 @@ mod ta_mode_tests { resources, ray_query: [ray; 2], ordinal: 0, + ta_notify: true, ta: stage, fragment: stage, programs: RegisterPrograms::V25G83, @@ -2000,4 +2002,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 213d40b3e46eb..4dbe663be12d9 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, @@ -141,12 +143,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> { @@ -166,6 +173,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 4df522c98daf9..9788611a645fa 100644 --- a/drivers/gpu/drm/asahi/g17/queue/mod.rs +++ b/drivers/gpu/drm/asahi/g17/queue/mod.rs @@ -234,6 +234,7 @@ impl Queue { aggregate: &super::fence::Aggregate, inputs: &[file::SyncItem], destinations: Destinations, + batch_renders: u16, ) -> Result { let engine = Engine::of(&command); let index = engine.index(); @@ -421,6 +422,7 @@ impl Queue { command, order, completion, + batch_renders, checked_inputs, firmware, )?; @@ -591,9 +593,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; if has_render { self.context.note_render(); } @@ -646,6 +653,7 @@ impl crate::queue::Queue for Queue { &aggregate, 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 6477b18de21fb..fa0e390fe72eb 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render.rs @@ -514,6 +514,7 @@ impl Pair { alloc: &Allocator<'_>, packet: &Arc, addresses: RenderAddresses, + backlog: usize, ) -> Result { if !self.room() || self @@ -617,6 +618,7 @@ impl Pair { ray_query: *ray_query, resources: &resources, ordinal, + 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)?, @@ -3569,6 +3571,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 @@ -3705,3 +3772,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 7fa602e0b2c9f..45af0648efba0 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/render/direct.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/render/direct.rs @@ -413,7 +413,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 1c57ed12adcef..4683e6ea37f84 100644 --- a/drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs +++ b/drivers/gpu/drm/asahi/g17/runtime/render/prepare.rs @@ -955,7 +955,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(()) => {}