Store the BAR reference in the TLB mutex and use a with_bar_locked()
helper to serialize register access. This makes the locking requirement
more explicit (e.g. when adding operations such as fault replay and
cancellation).

Signed-off-by: Danilo Krummrich <[email protected]>
---
 drivers/gpu/nova-core/mm/tlb.rs | 70 ++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 31 deletions(-)

diff --git a/drivers/gpu/nova-core/mm/tlb.rs b/drivers/gpu/nova-core/mm/tlb.rs
index fff66dc03549..eb32b95623be 100644
--- a/drivers/gpu/nova-core/mm/tlb.rs
+++ b/drivers/gpu/nova-core/mm/tlb.rs
@@ -64,57 +64,65 @@ pub(crate) enum TlbAckMode with TryFrom<Bounded<u32, 2>> {
 
 /// TLB manager for GPU translation buffer operations.
 #[pin_data]
-pub(crate) struct Tlb<'gpu> {
-    bar: Bar0<'gpu>,
+pub(crate) struct Tlb<'tlb>
+where
+    // Hide the mutex's invariant resource lifetime so `Tlb` remains covariant.
+    exists<'res>: 'tlb,
+{
     /// TLB flush serialization lock.
     ///
     /// Do not hold this lock across reclaimable memory allocations; see the
     /// [MM locking rules](crate::mm#locking).
     #[pin]
-    lock: Mutex<()>,
+    #[uses('res: invariant)]
+    bar: Mutex<Bar0<'res>>,
 }
 
-impl<'gpu> Tlb<'gpu> {
+impl<'tlb> Tlb<'tlb> {
     /// Create a new TLB manager.
-    pub(super) fn new(bar: Bar0<'gpu>) -> impl PinInit<Self> {
+    pub(super) fn new(bar: Bar0<'tlb>) -> impl PinInit<Self> {
         pin_init!(Self {
-            bar,
-            lock <- new_mutex!((), "tlb_flush"),
+            bar <- new_mutex!(bar, "tlb_flush"),
         })
     }
 
+    /// Runs `f` with the TLB locked.
+    fn with_bar_locked<R>(&self, f: impl FnOnce(Bar0<'_>) -> R) -> R {
+        self.with_bar(|bar| f(*bar.lock()))
+    }
+
     /// Flush the GPU TLB for a specific page directory base.
     ///
     /// This invalidates all TLB entries associated with the given PDB address.
     /// Must be called after modifying page table entries to ensure the GPU 
sees
     /// the updated mappings.
     pub(super) fn flush(&self, pdb_addr: VramAddress) -> Result {
-        let _guard = self.lock.lock();
+        self.with_bar_locked(|bar| {
+            // Write PDB address.
+            bar.write_reg(regs::NV_TLB_FLUSH_PDB_LO::from_pdb_addr(
+                pdb_addr.into_raw(),
+            ));
+            bar.write_reg(regs::NV_TLB_FLUSH_PDB_HI::from_pdb_addr(
+                pdb_addr.into_raw(),
+            ));
 
-        // Write PDB address.
-        self.bar.write_reg(regs::NV_TLB_FLUSH_PDB_LO::from_pdb_addr(
-            pdb_addr.into_raw(),
-        ));
-        self.bar.write_reg(regs::NV_TLB_FLUSH_PDB_HI::from_pdb_addr(
-            pdb_addr.into_raw(),
-        ));
+            // Trigger flush.
+            bar.write_reg(
+                regs::NV_TLB_FLUSH_CTRL::zeroed()
+                    .with_all_va(true)
+                    .with_ack(TlbAckMode::None)
+                    .with_trigger(true),
+            );
 
-        // Trigger flush.
-        self.bar.write_reg(
-            regs::NV_TLB_FLUSH_CTRL::zeroed()
-                .with_all_va(true)
-                .with_ack(TlbAckMode::None)
-                .with_trigger(true),
-        );
+            // Poll for completion.
+            read_poll_timeout(
+                || Ok(bar.read(regs::NV_TLB_FLUSH_CTRL)),
+                |ctrl: &regs::NV_TLB_FLUSH_CTRL| !ctrl.trigger(),
+                Delta::ZERO,
+                Delta::from_secs(2),
+            )?;
 
-        // Poll for completion.
-        read_poll_timeout(
-            || Ok(self.bar.read(regs::NV_TLB_FLUSH_CTRL)),
-            |ctrl: &regs::NV_TLB_FLUSH_CTRL| !ctrl.trigger(),
-            Delta::ZERO,
-            Delta::from_secs(2),
-        )?;
-
-        Ok(())
+            Ok(())
+        })
     }
 }
-- 
2.56.1

Reply via email to