diff --git a/drivers/gpu/drm/asahi/g17/fw/queue.rs b/drivers/gpu/drm/asahi/g17/fw/queue.rs index 44073e2e63fda..4f213109d9db2 100644 --- a/drivers/gpu/drm/asahi/g17/fw/queue.rs +++ b/drivers/gpu/drm/asahi/g17/fw/queue.rs @@ -586,6 +586,103 @@ impl QueueConfig { unk_86: [0; 0x3a], }) } + + /// Whether this cached compute configuration already matches a steady-state + /// publication (no install, seed, QoS, or context update flags) with the + /// given dynamic fields. + pub(crate) fn matches_steady_compute( + &self, + context_id: u16, + free_list: &FreeListBinding, + scheduler_va: u64, + policy: Policy, + qos_slot: u8, + owner_pid: u32, + ) -> bool { + let (install, seed_valid, qos_update, context_update) = ( + self.install, + self.completion_seed_valid, + self.qos_update, + self.context_update, + ); + if install != 0 || seed_valid != 0 || qos_update != 0 || context_update != 0 { + return false; + } + let (ctx, fl_va, fl_gen, fl_slot, sched, prio, pol, qos, pid) = ( + self.context_id, + self.free_list_va, + self.free_list_generation, + self.free_list_slot, + self.scheduler_va, + self.priority, + self.policy, + self.qos_slot, + self.owner_pid, + ); + ctx == context_id + && fl_va == free_list.va + && fl_gen == free_list.generation + && fl_slot == free_list.slot + && sched == scheduler_va + && prio == u16::from(policy.priority()) + && pol == policy.policy() + && qos == u16::from(qos_slot) + && pid == owner_pid + } + + /// Whether this cached render configuration's durable identity matches a + /// steady-state publication (caller has already verified no install, seed, + /// QoS, or context update is needed). + #[expect( + clippy::too_many_arguments, + reason = "Compares independent fields of QueueConfig without constructing a 192-byte record." + )] + pub(crate) fn matches_steady_render_identity( + &self, + context_id: u16, + free_list: &FreeListBinding, + scheduler_va: u64, + policy: Policy, + qos_slot: u8, + owner_pid: u32, + tiling: Option<(&TilingBinding, Option)>, + ) -> bool { + let (ctx, fl_va, fl_gen, fl_slot, sched, prio, pol, qos, pid) = ( + self.context_id, + self.free_list_va, + self.free_list_generation, + self.free_list_slot, + self.scheduler_va, + self.priority, + self.policy, + self.qos_slot, + self.owner_pid, + ); + if ctx != context_id + || fl_va != free_list.va + || fl_gen != free_list.generation + || fl_slot != free_list.slot + || sched != scheduler_va + || prio != u16::from(policy.priority()) + || pol != policy.policy() + || qos != u16::from(qos_slot) + || pid != owner_pid + { + return false; + } + match tiling { + Some((binding, parameter_manager)) => { + let (pb_va, pb_token, pb_slot, fragment_qid) = + (self.pb_va, self.pb_token, self.pb_slot, self.fragment_qid); + let expected_frag = parameter_manager.unwrap_or(u32::from(binding.fragment_qid)); + pb_va == binding.pb_va + && pb_token == binding.pb_token + && pb_slot == binding.pb_slot + && fragment_qid == expected_frag + } + None => true, + } + } } /// The 25G83 firmware's buffer-manager initialization (tag 5), before the first tiling @@ -703,3 +800,75 @@ impl KickPredecessor { }) } } + +#[kunit_tests(asahi_g17_queue_config_steady)] +mod queue_config_steady_tests { + use super::*; + + #[test] + fn steady_compute_and_render_matchers_track_all_dynamic_fields() { + let policy = Policy::new(2, 2, 4, 8192); + let free_list = FreeListBinding { + va: 0x1_0000_0000, + generation: 7, + slot: 3, + }; + let compute_args = QueueConfigArgs { + target: QueueConfigTarget::Compute, + kick_ring_va: 0x2_0000_0000, + kick_ring_fw_va: 0xf_0000_0000, + qid: 5, + install: false, + completion_seed: None, + context_id: 11, + free_list, + scheduler_va: 0x3_0000_0000, + policy, + qos_slot: 2, + qos_update: false, + owner_pid: 4242, + context_update: false, + }; + let cfg = QueueConfig::new(&compute_args).unwrap(); + assert!(cfg.matches_steady_compute(11, &free_list, 0x3_0000_0000, policy, 2, 4242)); + assert!(!cfg.matches_steady_compute(12, &free_list, 0x3_0000_0000, policy, 2, 4242)); + assert!(!cfg.matches_steady_compute(11, &FreeListBinding { generation: 8, ..free_list }, 0x3_0000_0000, policy, 2, 4242)); + assert!(!cfg.matches_steady_compute(11, &free_list, 0x3_0000_4000, policy, 2, 4242)); + assert!(!cfg.matches_steady_compute(11, &free_list, 0x3_0000_0000, Policy::new(1, 0, 20, 8192), 2, 4242)); + assert!(!cfg.matches_steady_compute(11, &free_list, 0x3_0000_0000, policy, 3, 4242)); + assert!(!cfg.matches_steady_compute(11, &free_list, 0x3_0000_0000, policy, 2, 4243)); + + let mut flagged = cfg; + flagged.install = 1; + assert!(!flagged.matches_steady_compute(11, &free_list, 0x3_0000_0000, policy, 2, 4242)); + + let tiling = TilingBinding { + pb_va: 0x4_0000_0000, + pb_token: 0xdead_beef, + pb_slot: 1, + fragment_qid: 9, + }; + let render_args = QueueConfigArgs { + target: QueueConfigTarget::Tiling(Some(tiling)), + install: true, + ..compute_args + }; + let mut rcfg = QueueConfig::new(&render_args).unwrap(); + // Durable identity ignores transient install/update/seed flags. + assert!(rcfg.matches_steady_render_identity( + 11, &free_list, 0x3_0000_0000, policy, 2, 4242, Some((&tiling, None)) + )); + assert!(!rcfg.matches_steady_render_identity( + 11, &free_list, 0x3_0000_0000, policy, 2, 4242, + Some((&TilingBinding { pb_token: 0xbad, ..tiling }, None)) + )); + // Parameter manager override (M5 non-J813 TA config). + rcfg.fragment_qid = 1; + assert!(rcfg.matches_steady_render_identity( + 11, &free_list, 0x3_0000_0000, policy, 2, 4242, Some((&tiling, Some(1))) + )); + assert!(!rcfg.matches_steady_render_identity( + 11, &free_list, 0x3_0000_0000, policy, 2, 4242, Some((&tiling, Some(2))) + )); + } +} diff --git a/drivers/gpu/drm/asahi/g17/queue/compute.rs b/drivers/gpu/drm/asahi/g17/queue/compute.rs index 4354c34027864..0b010a7a51bca 100644 --- a/drivers/gpu/drm/asahi/g17/queue/compute.rs +++ b/drivers/gpu/drm/asahi/g17/queue/compute.rs @@ -2822,6 +2822,16 @@ impl Graph { offset: usize, window: usize, value: T, + ) -> Result { + self.record_ref(slot, offset, window, &value) + } + + fn record_ref( + &mut self, + slot: Option, + offset: usize, + window: usize, + value: &T, ) -> Result { let (object, start) = if let Some(slot) = slot { let (object, stride): (&mut KernelObject, usize) = if offset == ANNOUNCE { @@ -2834,14 +2844,19 @@ impl Graph { let start = record_start(0, offset, window, GRAPH_SIZE - 0x6800)?; (&mut self.graph, 0x6800 + start) }; - if core::mem::size_of::() > window { + let len = core::mem::size_of::(); + if len > window { return Err(EINVAL); } let pointer = object.pointer(start, window)?; // SAFETY: The checked record window is not named by any unconsumed item pointer. The - // device mutex excludes other host publishers, and no Rust reference to it is formed. - unsafe { pointer.write_bytes(0, window) }; - object.write(start, value)?; + // queue lock excludes other host publishers, and no Rust reference to it is formed. + unsafe { + core::ptr::copy_nonoverlapping(core::ptr::from_ref(value).cast::(), pointer, len); + if window > len { + pointer.add(len).write_bytes(0, window - len); + } + } fence(Ordering::SeqCst); Ok(object.gpu_va() + start as u64) } @@ -2980,7 +2995,11 @@ impl Queue { &[][..] }; let mcache_context: u8 = packet.context.id().try_into()?; - let mcache = ComputeDescriptor::mcache_table(descriptor_low, mcache_writes.len())?; + let mcache = if self.graph.m5 { + None + } else { + ComputeDescriptor::mcache_table(descriptor_low, mcache_writes.len())? + }; let first = self.ordinal == 0 && (self.graph.m5 || !self.installed); // The first prototype only waits on an installed consumer channel. // A retry without a published source falls back to completion on CPU. @@ -3031,7 +3050,14 @@ impl Queue { self.graph.m5_timestamps.as_ref().ok_or(EINVAL)?, record_slot.ok_or(EINVAL)? * 16)? } else { packet.completion.compute_timestamp_va()? }; - let args = ComputeArgs { + let context_id: u16 = packet.context.id().try_into()?; + let stamp_va = self.graph.support.gpu_va() + STAMP as u64; + let aux_stamp_va = stamp_va + 8; + let status_va = [ + self.graph.support.gpu_va() + 0x310, + self.graph.support.gpu_va() + 0x318, + ]; + let legacy_args = (!self.graph.m5).then(|| ComputeArgs { program: self.graph.program, intersection: *intersection, scratch: *scratch, @@ -3041,7 +3067,7 @@ impl Queue { sampler_heap_va: *sampler_heap_va, sampler_count: *sampler_count, descriptor_va: descriptor_low, - context_id: packet.context.id().try_into()?, + context_id, context_generation: packet.context.generation(), work_state: work_word, work_state_va: work_node, @@ -3055,14 +3081,11 @@ impl Queue { usage_fw_va: self.graph.usage.gpu_va(), free_list_slot: u32::from(self.pool.id()), free_list_control_va: self.pool.control_va(), - stamp_va: self.graph.support.gpu_va() + STAMP as u64, - aux_stamp_va: self.graph.support.gpu_va() + STAMP as u64 + 8, - status_va: [ - self.graph.support.gpu_va() + 0x310, - self.graph.support.gpu_va() + 0x318, - ], + stamp_va, + aux_stamp_va, + status_va, timestamp_va, - }; + }); let identity = ConfigIdentity { scheduler: packet.context.scheduler_va(), qos: packet.context.qos_id(), @@ -3072,15 +3095,43 @@ impl Queue { let policy = packet.context.policy(); let activation = activation::Publication::new( self.outer_started, policy.priority(), self.graph.m5); - let config = self.queue_config(&identity, first, activation.refresh, - args.context_id, policy, &packet)?; let cached = self .config_cache .and_then(|(config, at)| (at == epoch).then_some(config)); - let send_config = self.graph.m5 || first || super::render::publication::required(cached, config); - let announce = - KickAnnounce::new(qid, DataMaster::Compute, kick_timestamp, policy.priority())?; - let predecessor = KickPredecessor::new(qid, kick_parent)?; + let free_list_binding = FreeListBinding { + va: self.pool.control_va(), + generation: self.free_list_generation, + slot: u32::from(self.pool.id()), + }; + let steady_config = !first + && !activation.refresh + && self.published_config == Some(identity) + && cached.as_ref().is_some_and(|c| { + c.matches_steady_compute( + context_id, + &free_list_binding, + identity.scheduler, + policy, + identity.qos, + packet.context.owner_pid(), + ) + }); + let built_config = if steady_config { + None + } else { + Some(self.queue_config( + &identity, + first, + activation.refresh, + context_id, + policy, + &packet, + )?) + }; + let config_ref = built_config.as_ref().or(cached.as_ref()).ok_or(EIO)?; + let send_config = self.graph.m5 + || first + || (!steady_config && super::render::publication::required(cached, *config_ref)); let matrix_ops = self.graph.m5 && *requested_matrix_ops; let m5_board = if self.graph.m5 { Some(self.graph.board.ok_or(EINVAL)?) } else { None }; // Each physical QID owns a single preemption/operand/usage/status/stamp set: only take the @@ -3109,7 +3160,7 @@ impl Queue { auxiliary_allocation: *cdm_auxiliary_allocation, preempt_va: addresses.preempt, cdm_va: *cdm_va, operand_va: addresses.operand, usage_va: addresses.usage, free_list_slot: u32::from(self.pool.id()), - context_id: args.context_id, work_state: work_word, + context_id, work_state: work_word, object_ids: m5_object_ids, resource_policy: plan.map_or(0xf5_0040_0020, |p| u64::from(p.pressure) << 32 | 0x0040_0020), preempt_state_offset: tuple.preempt_state_offset, @@ -3136,12 +3187,12 @@ impl Queue { 0 }, work_state_va: work_node, - cdm_end_va: *cdm_end_va, stamp_va: [args.stamp_va, args.aux_stamp_va], + cdm_end_va: *cdm_end_va, stamp_va: [stamp_va, aux_stamp_va], stamp_value: crate::g17::fw::stamp_value(kick_timestamp.get() as u32), qid: u32::from(qid), channel_index: if multi_channel { u32::from(self.channel_index) } else { 0 }, kick_timestamp: kick_timestamp.get(), completion_ordinal: crate::g17::fw::m5::compute::completion_ordinal(board, m5_ordinal), - status_va: args.status_va, timestamp_va: [timestamp_va, timestamp_va + 8], + status_va, timestamp_va: [timestamp_va, timestamp_va + 8], shared_control_va: self.pool.control_va(), event_metadata: crate::g17::fw::m5::compute::completion_forms(board).1, usage_fw_va: self.graph.usage.gpu_va(), @@ -3150,12 +3201,17 @@ impl Queue { // Explicit dependencies take the board's class // ([`crate::g17::fw::m5::compute::explicit_dependency`]); the implicit parent, last, // takes its own ([`crate::g17::fw::m5::compute::parent_dependency`]). - let mut m5_dependency_types = [m5_board.map_or( - crate::g17::fw::m5::compute::COMPUTE_PIPELINED_DEPENDENCY, - crate::g17::fw::m5::compute::explicit_dependency); - crate::g17::fw::kick::QID_MAX as usize + 1]; - if let (Some(class), Some(parent)) = (parent_class, m5_dependency_types.get_mut(dependencies.len())) { - *parent = class; + let mut m5_dependency_types = [0u8; crate::g17::fw::kick::KICK_DEPENDENCIES]; + let m5_dep_count = dependencies.len().checked_add(1).ok_or(EINVAL)?; + if let (Some(board), Some(class)) = (m5_board, parent_class) { + if m5_dep_count > m5_dependency_types.len() { + return Err(EINVAL); + } + if !dependencies.is_empty() { + m5_dependency_types[..dependencies.len()] + .fill(crate::g17::fw::m5::compute::explicit_dependency(board)); + } + m5_dependency_types[dependencies.len()] = class; } let kick_args = KickArgs { qid, @@ -3214,8 +3270,9 @@ impl Queue { object.edit::(offset, |descriptor| descriptor.write_in_place(m5_args))?; } else { + let args = legacy_args.as_ref().ok_or(EIO)?; object.initialize::(offset, |descriptor| { - descriptor.write_zeroed(&args)?; + descriptor.write_zeroed(args)?; descriptor.write_mcache(mcache_writes, mcache_context) })?; } @@ -3236,7 +3293,7 @@ impl Queue { } if let Some(cursors) = snapshot { self.graph.recycle(cursors)?; } let config_va = if send_config { - Some(self.graph.record(record_slot, CONFIG, 0xc0, config)?) + Some(self.graph.record_ref(record_slot, CONFIG, 0xc0, config_ref)?) } else { None }; @@ -3370,9 +3427,13 @@ impl Queue { prepared_kick.commit(); self.submitted = self.submitted.checked_add(1).ok_or(EOVERFLOW)?; let announce_va = if self.graph.m5 && !crate::g17::J813_FORMS.load(Ordering::Relaxed) { - self.graph.record(record_slot, ANNOUNCE, 0x40, - crate::g17::fw::m5::queue::notification(qid, kick_timestamp)?)? - } else { self.graph.record(record_slot, ANNOUNCE, 0x40, announce)? }; + let notification = crate::g17::fw::m5::queue::notification(qid, kick_timestamp)?; + self.graph.record_ref(record_slot, ANNOUNCE, 0x40, ¬ification)? + } else { + let announce = + KickAnnounce::new(qid, DataMaster::Compute, kick_timestamp, policy.priority())?; + self.graph.record_ref(record_slot, ANNOUNCE, 0x40, &announce)? + }; producer = match snapshot { // The snapshot's consumer only lags the firmware's, and the free-room check // above covered both M5 items; the producer is the one this publication set. @@ -3384,9 +3445,10 @@ impl Queue { // Only the first kick installs a predecessor record. A later kick that names another // queue's kick depends on it through the kick entry alone. if first && !self.graph.m5 { + let predecessor = KickPredecessor::new(qid, kick_parent)?; let predecessor_va = self.graph - .record(record_slot, PREDECESSOR, 0x400, predecessor)?; + .record_ref(record_slot, PREDECESSOR, 0x400, &predecessor)?; producer = self.graph.append(producer, predecessor_va, || {})?; } if let Some(slot) = record_slot { @@ -3408,7 +3470,9 @@ impl Queue { self.installed = true; self.published_config = Some(identity); if send_config { - self.config_cache = Some((config, epoch)); + if let Some(config) = built_config { + self.config_cache = Some((config, epoch)); + } } self.ordinal = ordinal; let (accepted, state) = host.epoch()?; diff --git a/drivers/gpu/drm/asahi/g17/queue/render.rs b/drivers/gpu/drm/asahi/g17/queue/render.rs index 72857a0a4e581..563eec3b9959a 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render.rs @@ -1010,53 +1010,115 @@ impl Pair { data_master: DataMaster::Fragment as u8, }, ]; + // The J813's (common 26A428) items carry configuration, descriptor and announcement in + // one publication; macOS 27.0 never publishes a configuration alone there. + let j813 = crate::g17::J813_FORMS.load(Ordering::Relaxed); + let host_only_render_kicks = + m5 && !j813 && self.board.is_some_and(|board| board.host_only_render_kicks); + let cold_m5_config = m5 && !j813 && self.registered.is_none(); + if cold_m5_config && self.installed == [true; 2] { + // The cold-install configuration pair must be consumed first. + let cursors = self.memory.graph.cursors()?; + if cursors.iter().zip(self.cursors).any(|(c, end)| c.read != end) { + return Err(EBUSY); + } + } // M5 carries the identity update bits only on a changed identity. let identity_update = if m5 { self.identity_update } else { self.context_update }; + let cache = if self.recovery_generation == epoch { + self.configs + } else { + [None; 2] + }; + // A covered queue holds the completion value of its newest kick. When that kick is the + // previous publication of this pair in this recovery epoch, the seed would reload the + // value already held, so the configuration record is needed only for its identity. + // Each record costs the firmware time before it starts the kick. + let held = if cold_m5_config && self.installed != [true; 2] { + None + } else { + self.published_completions + .take() + .filter(|_| self.recovery_generation == epoch) + }; + let config_context_id: u16 = context.id().try_into()?; + let free_list_binding = FreeListBinding { + va: self.free_list.control_va(), + generation: lease.free_list_generation, + slot: self.free_list.id().into(), + }; + let tiling_binding = TilingBinding { + pb_va: self.memory.manager.state_va(), + pb_token: lease.buffer_token, + pb_slot: self.buffer_id().into(), + fragment_qid: qids[1], + }; + let parameter_manager = (m5 && !j813).then(|| u32::from(self.buffer_id())); + let scheduler_va = context.scheduler_va(); + let owner_pid = context.owner_pid(); let mut configs = [None; 2]; + let mut present = [false; 2]; for stage in 0..2 { + let need_install = !self.installed[stage]; + let completion_seed = (covered[stage] + && (!m5 || need_install || self.context_update)) + .then(|| { + // The predecessor's timestamp with the stage's scratch payload: the + // previous pass's on fragment; with the G17D forms 0 on tiling, as the + // G17D's tiling queues are seeded (its fragment checks read the paired + // fragment queue's payload, not this one). + let tiling_zero = stage == 0 && m5 && self.g17d_forms(); + kicks[stage].prev().completion_value(if tiling_zero { + 0 + } else { + payload.wrapping_sub(1) + }) + }); + let reload = kicks[stage] + .prev() + .completion_value(payload.wrapping_sub(1)); + let need_seed = completion_seed.is_some() + && !(self.installed[stage] && held.is_some_and(|h| h[stage] == reload)); + let skip_steady = !cold_m5_config + && !j813 + && !need_install + && !identity_update + && !need_seed + && cache[stage].as_ref().is_some_and(|cached| { + cached.matches_steady_render_identity( + config_context_id, + &free_list_binding, + scheduler_va, + policy, + qos, + owner_pid, + (stage == 0).then_some((&tiling_binding, parameter_manager)), + ) + }); + if skip_steady { + continue; + } let args = QueueConfigArgs { target: if stage == 0 { - QueueConfigTarget::Tiling(Some(TilingBinding { - pb_va: self.memory.manager.state_va(), - pb_token: lease.buffer_token, - pb_slot: self.buffer_id().into(), - fragment_qid: qids[1], - })) + QueueConfigTarget::Tiling(Some(tiling_binding)) } else { QueueConfigTarget::Fragment }, kick_ring_va: self.kicks[stage].low_va(), kick_ring_fw_va: self.kicks[stage].firmware_va(), qid: qids[stage], - install: !self.installed[stage], - completion_seed: (covered[stage] - && (!m5 || !self.installed[stage] || self.context_update)) - .then(|| { - // The predecessor's timestamp with the stage's scratch payload: the - // previous pass's on fragment; with the G17D forms 0 on tiling, as the - // G17D's tiling queues are seeded (its fragment checks read the paired - // fragment queue's payload, not this one). - let tiling_zero = stage == 0 && m5 && self.g17d_forms(); - kicks[stage].prev().completion_value(if tiling_zero { - 0 - } else { - payload.wrapping_sub(1) - }) - }), - context_id: context.id().try_into()?, - free_list: FreeListBinding { - va: self.free_list.control_va(), - generation: lease.free_list_generation, - slot: self.free_list.id().into(), - }, - scheduler_va: context.scheduler_va(), + install: need_install, + completion_seed, + context_id: config_context_id, + free_list: free_list_binding, + scheduler_va, policy, qos_slot: qos, qos_update: identity_update, - owner_pid: context.owner_pid(), + owner_pid, context_update: identity_update, }; - configs[stage] = Some(if m5 { + let config = if m5 { crate::g17::fw::m5::queue::config( &args, crate::g17::fw::m5::queue::ConfigProfile { @@ -1064,62 +1126,36 @@ impl Pair { extended: false, // macOS 27.0's J813 TA configuration names the paired 3D queue at // +0x82 (qid 1 beside pb_slot 0), not the parameter manager. - parameter_manager: (stage == 0 - && !crate::g17::J813_FORMS.load(Ordering::Relaxed)) - .then(|| u32::from(self.buffer_id())), + parameter_manager: if stage == 0 { parameter_manager } else { None }, }, )? } else { QueueConfig::new(&args)? - }); - } - let configs = [configs[0].ok_or(EIO)?, configs[1].ok_or(EIO)?]; - // The J813's (common 26A428) items carry configuration, descriptor and announcement in - // one publication; macOS 27.0 never publishes a configuration alone there. - let j813 = crate::g17::J813_FORMS.load(Ordering::Relaxed); - let host_only_render_kicks = - m5 && !j813 && self.board.is_some_and(|board| board.host_only_render_kicks); - if m5 && !j813 && self.registered.is_none() { - if self.installed == [true; 2] { - // The cold-install configuration pair must be consumed first. - let cursors = self.memory.graph.cursors()?; - if cursors.iter().zip(self.cursors).any(|(c, end)| c.read != end) { - return Err(EBUSY); - } - } else { - return self.publish_m5_configuration(&packet, configs, kicks, policy, ring, host); + }; + if cold_m5_config && self.installed != [true; 2] { + configs[stage] = Some(config); + continue; } - } - let cache = if self.recovery_generation == epoch { - self.configs - } else { - [None; 2] - }; - // A covered queue holds the completion value of its newest kick. When that kick is the - // previous publication of this pair in this recovery epoch, the seed would reload the - // value already held, so the configuration record is needed only for its identity. - // Each record costs the firmware time before it starts the kick. - let held = self - .published_completions - .take() - .filter(|_| self.recovery_generation == epoch); - let present = [0, 1].map(|stage| { - let mut config = configs[stage]; - let reload = kicks[stage] - .prev() - .completion_value(payload.wrapping_sub(1)); - if config.completion_seed_valid != 0 + let mut check = config; + if check.completion_seed_valid != 0 && self.installed[stage] - && held.is_some_and(|held| held[stage] == reload) + && held.is_some_and(|h| h[stage] == reload) { - config.completion_seed_valid = 0; - config.completion_seed = 0; + check.completion_seed_valid = 0; + check.completion_seed = 0; } // macOS 27.0 sends a configuration with every J813 kick, so the firmware always // runs a kick under the context its own record names; leaving it out let a fragment // run under another client's (or no) context under parallel load. - j813 || publication::required(cache[stage], config) - }); + if j813 || publication::required(cache[stage], check) { + present[stage] = true; + configs[stage] = Some(config); + } + } + if cold_m5_config && self.installed != [true; 2] { + let cold = [configs[0].ok_or(EIO)?, configs[1].ok_or(EIO)?]; + return self.publish_m5_configuration(&packet, cold, kicks, policy, ring, host); + } // An idle pair's next command starts from the rings' cursors; behind a command in // flight it starts from that command's windows, and only the producers are checked. let cursors = match self.active.front() { @@ -1308,8 +1344,9 @@ impl Pair { 0, ordinal, self.cursors[0], - configs[0], - present[0], + qids[0], + policy.priority(), + configs[0].as_ref(), kicks[0], leads[0], sparse_inner, @@ -1319,8 +1356,9 @@ impl Pair { 1, ordinal, self.cursors[1], - configs[1], - present[1], + qids[1], + policy.priority(), + configs[1].as_ref(), kicks[1], leads[1], sparse_inner, @@ -1487,8 +1525,8 @@ impl Pair { } self.cursors = windows.map(|window| window.target()); for stage in 0..2 { - if present[stage] { - self.configs[stage] = Some(configs[stage]); + if let Some(config) = configs[stage] { + self.configs[stage] = Some(config); } } self.recovery_generation = epoch; @@ -1545,8 +1583,9 @@ impl Pair { 0, ordinal, self.cursors[0], - configs[0], - true, + qids[0], + policy.priority(), + Some(&configs[0]), kicks[0], false, false, @@ -1556,8 +1595,9 @@ impl Pair { 1, ordinal, self.cursors[1], - configs[1], - true, + qids[1], + policy.priority(), + Some(&configs[1]), kicks[1], false, false, diff --git a/drivers/gpu/drm/asahi/g17/queue/render/publication.rs b/drivers/gpu/drm/asahi/g17/queue/render/publication.rs index 53707fb0b9fcd..4d2ff0fa4ad80 100644 --- a/drivers/gpu/drm/asahi/g17/queue/render/publication.rs +++ b/drivers/gpu/drm/asahi/g17/queue/render/publication.rs @@ -38,9 +38,8 @@ pub(super) struct Lane<'a> { items: [&'a AtomicU64; 4], producer: &'a AtomicU32, values: [u64; 4], - config: QueueConfig, + config: Option<&'a QueueConfig>, config_destination: *mut u64, - config_present: bool, announce: Announcement, announce_destination: *mut u8, prefix: usize, @@ -58,8 +57,9 @@ impl<'a> Lane<'a> { stage: usize, ordinal: u64, base: u32, - config: QueueConfig, - present: bool, + qid: u8, + priority: u8, + config: Option<&'a QueueConfig>, timestamp: KickTimestamp, lead: bool, retained: bool, @@ -69,6 +69,7 @@ impl<'a> Lane<'a> { // records, never the M5 Pro's descriptor-less tag-16 notification. let j813 = crate::g17::J813_FORMS.load(core::sync::atomic::Ordering::Relaxed); let m5 = graph.is_m5() && !j813; + let present = config.is_some(); // M5 items carry no descriptor: [configuration], notification. let count = if retained { 0 @@ -78,13 +79,18 @@ impl<'a> Lane<'a> { let window = Window::new(base, count, ITEM_RING_ENTRIES)?; let config_offset = graph.config_offset(stage, ordinal); let announce_offset = graph.announce_offset(stage, ordinal); - let config_destination = graph - .config - .pointer(config_offset, size_of::())? - .cast::(); - if config_destination.align_offset(align_of::()) != 0 { - return Err(EINVAL); - } + let config_destination = if present { + let ptr = graph + .config + .pointer(config_offset, size_of::())? + .cast::(); + if ptr.align_offset(align_of::()) != 0 { + return Err(EINVAL); + } + ptr + } else { + core::ptr::null_mut() + }; let announce_destination = graph .queues .pointer(announce_offset, if m5 { size_of::() } @@ -127,19 +133,18 @@ impl<'a> Lane<'a> { values, config, config_destination, - config_present: present, announce: if m5 { - Announcement::M5(m5::queue::notification(config.qid.try_into()?, timestamp)?) + Announcement::M5(m5::queue::notification(qid, timestamp)?) } else { Announcement::Neo(KickAnnounce::new( - config.qid.try_into()?, + qid, if stage == 0 { DataMaster::Tiling } else { DataMaster::Fragment }, timestamp, - config.priority as u8, + priority, )?) }, announce_destination, @@ -155,10 +160,10 @@ impl<'a> Lane<'a> { } } fn config(&self) { - if !self.config_present { + let Some(config) = self.config else { return; - } - let source = core::ptr::from_ref(&self.config).cast::(); + }; + let source = core::ptr::from_ref(config).cast::(); for index in 0..size_of::() / 8 { // SAFETY: QueueConfig is packed integer storage with no padding; // every source qword is read unaligned. The destination is a diff --git a/drivers/gpu/drm/asahi/g17/runtime.rs b/drivers/gpu/drm/asahi/g17/runtime.rs index c6bd61b75b3c0..429e5429e30bf 100644 --- a/drivers/gpu/drm/asahi/g17/runtime.rs +++ b/drivers/gpu/drm/asahi/g17/runtime.rs @@ -3392,45 +3392,47 @@ impl Backend { if let Err(error) = packet.check_firmware_producers() { return Some(Err(error)); } - let cells = self.direct.lock().clone(); - let cached_count = cells.iter().filter(|cell| cell.is_some()).count(); - if cached_count != bound_count || cells[0].is_none() { - return None; - } - let target_qid = cells[0].as_ref()?.qid()?; let lease = self.shared.preparations.try_enter().ok()?; let port = self.shared.port(&lease)?; if self.shared.crashed.load(Ordering::Acquire) || port.recovery_state().ok()? != 0 { return None; } let generation = port.generation(); - let (mut direct_frontier, direct_native, direct_holds, cross_qid_mask) = - if let Some(sources) = packet.completion.sources() { - self.resolve_direct_sources(port, generation, target_qid, m5, sources)? - } else { - (super::dependency::Frontier::new(), KVec::new(), KVec::new(), 0) - }; // Mixed-engine contexts resolve any active render prefix briefly under the device mutex - // (and skip the mutex altogether when no render of this context is in flight), then keep + // before locking `self.direct` (preserving `shared.state -> direct` lock order), then keep // compute descriptor encoding, cache maintenance, and doorbell rings off the device mutex. - if self.context.render_seen() + let render_frontier = if self.context.render_seen() && packet.order.wait_through[0].is_some() && self.context.render_active() != 0 { - let render_frontier = { - let state = self.shared.state.lock(); - let firmware = (*state).as_deref()?; - if !lease.is_current() - || firmware.recovery.pending() - || firmware.queues.render.replay_parked() - { - return None; - } - match firmware.queues.render_prefix(self.owner, packet) { - Ok(frontier) => frontier, - Err(error) => return Some(Err(error)), - } + let state = self.shared.state.lock(); + let firmware = (*state).as_deref()?; + if !lease.is_current() + || firmware.recovery.pending() + || firmware.queues.render.replay_parked() + { + return None; + } + match firmware.queues.render_prefix(self.owner, packet) { + Ok(frontier) => Some(frontier), + Err(error) => return Some(Err(error)), + } + } else { + None + }; + let cells = self.direct.lock(); + let cached_count = cells.iter().filter(|cell| cell.is_some()).count(); + if cached_count != bound_count || cells[0].is_none() { + return None; + } + let target_qid = cells[0].as_ref()?.qid()?; + let (mut direct_frontier, direct_native, direct_holds, cross_qid_mask) = + if let Some(sources) = packet.completion.sources() { + self.resolve_direct_sources(port, generation, target_qid, m5, sources)? + } else { + (super::dependency::Frontier::new(), KVec::new(), KVec::new(), 0) }; + if let Some(render_frontier) = render_frontier { for dep in render_frontier.as_slice() { if direct_frontier.insert(0, *dep).is_err() { return None; @@ -3574,6 +3576,7 @@ impl Backend { ) }) })(); + drop(cells); drop(direct); if packet.is_published() { if let Some(sources) = packet.completion.sources() { @@ -3791,63 +3794,70 @@ impl Backend { if bound_count != 0 && bound_count <= super::pairs::COMPUTE_CHANNELS_MAX { - let cells = self.direct.lock().clone(); - let cached_count = cells.iter().filter(|cell| cell.is_some()).count(); - if cached_count == bound_count { - if let Some(primary) = cells[0].clone() { - // Validate siblings first so primary is claimed only once every channel passes. - let mut all_ok = true; - let mut outer_slots = 1u32; - for cell in cells[1..].iter().flatten() { - match cell.lock() { - Some(queue) - if queue.installed() + let bound = { + let cells = self.direct.lock(); + let cached_count = cells.iter().filter(|cell| cell.is_some()).count(); + if cached_count == bound_count { + if let Some(primary) = cells[0].as_ref() { + // Validate siblings first so primary is claimed only once every channel passes. + let mut all_ok = true; + let mut outer_slots = 1u32; + for cell in cells[1..].iter().flatten() { + match cell.lock() { + Some(queue) + if queue.installed() + && !queue.spared_pending() + && queue.pool_fits(required) + && matches!( + queue.matches(self.owner, queue.qid(), &self.context), + Ok(true) + ) => + { + outer_slots = outer_slots.max(queue.outer_slots()); + } + _ => { + all_ok = false; + break; + } + } + } + if all_ok { + primary.lock().and_then(|mut queue| { + let ok = queue.installed() && !queue.spared_pending() && queue.pool_fits(required) && matches!( queue.matches(self.owner, queue.qid(), &self.context), Ok(true) - ) => - { - outer_slots = outer_slots.max(queue.outer_slots()); - } - _ => { - all_ok = false; - break; - } + ) + && self.context.compute_binding_count() as usize + == cached_count; + if ok { + queue.claim(); + Some((primary.clone(), outer_slots.max(queue.outer_slots()))) + } else { + None + } + }) + } else { + None } - } - let bound = if all_ok { - primary.lock().and_then(|mut queue| { - let ok = queue.installed() - && !queue.spared_pending() - && queue.pool_fits(required) - && matches!( - queue.matches(self.owner, queue.qid(), &self.context), - Ok(true) - ) - && self.context.compute_binding_count() as usize == cached_count; - if ok { - queue.claim(); - Some(outer_slots.max(queue.outer_slots())) - } else { - None - } - }) } else { None - }; - if let Some(outer_slots) = bound { - self.hold_claim(Some(primary)); - if !self.context.is_m5() { - return Readiness::Ready; - } - return match self.claim_compute_outer_direct(outer_slots) { - Ok(false) => Readiness::Outer, - _ => Readiness::Ready, - }; } + } else { + None + } + }; + if let Some((primary, outer_slots)) = bound { + self.hold_claim(Some(primary)); + if !self.context.is_m5() { + return Readiness::Ready; } + return match self.claim_compute_outer_direct(outer_slots) { + Ok(false) => Readiness::Outer, + _ => Readiness::Ready, + }; } } if !sources_checked {