Skip to contentSkip to conversation

drm/tyr: use pin-init self-referential support

PATCH In review Danilo Krummrich 4 participants 11 messages Nov 30 – Dec 3

Subsystems (4)
lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

v2 cover drm/tyr: use pin-init self-referential support

This series uses pin-init's self-referential support to replace Arcs for resources whose lifetimes are bounded by their owners.

It also cleans up lifetime names and simplifies firmware loading cleanup with ScopeGuard.

Changes in v2:
- Use generic names for covariant lifetime parameters and resource names for invariant ones.
- Include VmAsData, GpuVmData, the VM operation types and PtUpdateContext in the lifetime cleanup.
- Drop the pin-init lint suppression patch and temporarily allow clippy::multiple_bound_locations in Tyr instead.

Danilo Krummrich (3):
drm/tyr: clarify resource lifetime names drm/tyr: use pin-init self-referential support drm/tyr: use ScopeGuard for firmware loading cleanup

 drivers/gpu/drm/tyr/driver.rs            |  38 +++--
 drivers/gpu/drm/tyr/fw.rs                | 151 +++++++++----------
 drivers/gpu/drm/tyr/gem.rs               |  15 +-
 drivers/gpu/drm/tyr/mmu.rs               |  56 ++++----
 drivers/gpu/drm/tyr/mmu/address_space.rs |  54 +++----
 drivers/gpu/drm/tyr/tyr.rs               |   5 +
 drivers/gpu/drm/tyr/vm.rs                | 175 +++++++++++------------
 7 files changed, 247 insertions(+), 247 deletions(-)

base-commit: abfb640b2374e26de8ff81d488c39d8254db538e

Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH v2 1/3 drm/tyr: clarify resource lifetime names

Use generic names for covariant lifetime parameters and 'res for invariant device resource lifetimes throughout Tyr's firmware, buffer, VM and MMU code.

Apply this convention consistently in preparation for introducing additional borrow lifetimes with self-referential pin-init.

---
 drivers/gpu/drm/tyr/fw.rs                | 26 +++----
 drivers/gpu/drm/tyr/gem.rs               |  8 +-
 drivers/gpu/drm/tyr/mmu.rs               | 24 +++---
 drivers/gpu/drm/tyr/mmu/address_space.rs | 44 +++++------
 drivers/gpu/drm/tyr/vm.rs                | 96 ++++++++++++------------
 5 files changed, 99 insertions(+), 99 deletions(-)
drivers/gpu/drm/tyr/fw.rs +13 -13
index aea151ff1f93..6521c5c1b63a 100644
@@ -131,7 +131,7 @@ fn try_from_fw(value: u32) -> Result<Self> {
 }
 
 /// A parsed section of the firmware binary.
-struct Section<'drm> {
+struct Section<'res> {
     // Raw firmware section data for reset purposes
     #[expect(dead_code)]
     data: KVec<u8>,
@@ -139,23 +139,23 @@ struct Section<'drm> {
     // Keep the BO backing this firmware section so that both the
     // GPU mapping and CPU mapping remain valid until the Section is dropped.
     #[expect(dead_code)]
-    mem: gem::KernelBo<'drm>,
+    mem: gem::KernelBo<'res>,
 }
 
 /// Loaded firmware with sections mapped into MCU VM.
-pub(crate) struct Firmware<'drm> {
+pub(crate) struct Firmware<'res> {
     /// Iomem need to access registers.
-    iomem: Arc<IoMem<'drm>>,
+    iomem: Arc<IoMem<'res>>,
 
     /// MCU VM.
-    vm: Arc<Vm<'drm>>,
+    vm: Arc<Vm<'res>>,
 
     /// List of firmware sections.
     #[expect(dead_code)]
-    sections: KVec<Section<'drm>>,
+    sections: KVec<Section<'res>>,
 }
 
-impl<'drm> Drop for Firmware<'drm> {
+impl<'res> Drop for Firmware<'res> {
     fn drop(&mut self) {
         // Stop the MCU before releasing its firmware mappings and memory.
         let _ = self.stop();
@@ -165,8 +165,8 @@ fn drop(&mut self) {
     }
 }
 
-impl<'drm> Firmware<'drm> {
-    fn init_section_mem(dev: &Device, mem: &mut KernelBo<'drm>, data: &KVec<u8>) -> Result {
+impl<'res> Firmware<'res> {
+    fn init_section_mem(dev: &Device, mem: &mut KernelBo<'res>, data: &KVec<u8>) -> Result {
         if data.is_empty() {
             return Ok(());
         }
@@ -213,12 +213,12 @@ fn load(
 
     /// Load firmware and map sections into MCU VM.
     pub(crate) fn new(
-        dev: &'drm Device<Bound>,
-        iomem: Arc<IoMem<'drm>>,
+        dev: &'res Device<Bound>,
+        iomem: Arc<IoMem<'res>>,
         ddev: &TyrDrmDevice,
-        mmu: ArcBorrow<'_, Mmu<'drm>>,
+        mmu: ArcBorrow<'_, Mmu<'res>>,
         gpu_info: &GpuInfo,
-    ) -> Result<Firmware<'drm>> {
+    ) -> Result<Firmware<'res>> {
         let vm = Vm::new(dev, ddev, mmu, gpu_info)?;
         vm.activate()?;
 
drivers/gpu/drm/tyr/gem.rs +4 -4
index 3bf3787f5c3f..c9f9e5592e41 100644
@@ -82,16 +82,16 @@ pub(crate) enum KernelBoVaAlloc {
 /// a GEM object with automatic GPU virtual address (VA) space mapping and cleanup.
 ///
 /// When dropped, the buffer is automatically unmapped from the GPU VA space.
-pub(crate) struct KernelBo<'drm> {
+pub(crate) struct KernelBo<'res> {
     /// The underlying GEM buffer object.
     bo: ARef<Bo>,
     /// The GPU VM this buffer is mapped into.
-    vm: Arc<Vm<'drm>>,
+    vm: Arc<Vm<'res>>,
     /// The GPU VA range occupied by this buffer.
     va_range: Range<u64>,
 }
 
-impl<'drm> KernelBo<'drm> {
+impl<'res> KernelBo<'res> {
     /// Creates a new kernel-owned buffer object and maps it into GPU VA space.
     ///
     /// This function allocates a new shmem-backed GEM object and immediately maps
@@ -99,7 +99,7 @@ impl<'drm> KernelBo<'drm> {
     /// cleaned up when the [`KernelBo`] is dropped.
     pub(crate) fn new(
         ddev: &TyrDrmDevice,
-        vm: Arc<Vm<'drm>>,
+        vm: Arc<Vm<'res>>,
         size: u64,
         va_alloc: KernelBoVaAlloc,
         flags: VmMapFlags,
drivers/gpu/drm/tyr/mmu.rs +12 -12
index 5e88c9e6a4cd..e708a181aedb 100644
@@ -43,23 +43,23 @@
 
 pub(crate) mod address_space;
 
-pub(crate) type AsSlotManager<'drm> = SlotManager<AddressSpaceManager<'drm>, MAX_AS>;
+pub(crate) type AsSlotManager<'res> = SlotManager<AddressSpaceManager<'res>, MAX_AS>;
 
 /// Locked wrapper for carrying out virtual memory (VM) operations on the MMU.
 #[pin_data]
-pub(crate) struct Mmu<'drm> {
+pub(crate) struct Mmu<'res> {
     /// Slot Manager instance used to allocate hardware slots and write to MMU registers.
     #[pin]
-    pub(crate) as_manager: Mutex<AsSlotManager<'drm>>,
+    pub(crate) as_manager: Mutex<AsSlotManager<'res>>,
 }
 
-impl<'drm> Mmu<'drm> {
+impl<'res> Mmu<'res> {
     /// Create an MMU component for this device.
     pub(crate) fn new(
-        dev: &'drm Device<Bound>,
-        iomem: ArcBorrow<'_, IoMem<'drm>>,
+        dev: &'res Device<Bound>,
+        iomem: ArcBorrow<'_, IoMem<'res>>,
         gpu_info: &GpuInfo,
-    ) -> Result<Arc<Mmu<'drm>>> {
+    ) -> Result<Arc<Mmu<'res>>> {
         let present = AS_PRESENT::from_raw(gpu_info.as_present).present().get();
         let slot_count = present.count_ones().try_into()?;
 
@@ -81,17 +81,17 @@ pub(crate) fn new(
 
     /// Assign a VM to an AS slot, provide a translation table,
     /// and update the MMU to make the VM resident.
-    pub(crate) fn activate_vm(&self, vm_as_data: ArcBorrow<'_, VmAsData<'drm>>) -> Result {
+    pub(crate) fn activate_vm(&self, vm_as_data: ArcBorrow<'_, VmAsData<'res>>) -> Result {
         self.as_manager.lock().activate_vm(vm_as_data)
     }
 
     /// Evict a VM from its AS slot and flush the MMU.
-    pub(crate) fn deactivate_vm(&self, vm_as_data: &VmAsData<'drm>) -> Result {
+    pub(crate) fn deactivate_vm(&self, vm_as_data: &VmAsData<'res>) -> Result {
         self.as_manager.lock().deactivate_vm(vm_as_data)
     }
 
     /// Flush MMU translation caches after a VM update.
-    pub(crate) fn flush_vm(&self, vm_as_data: &VmAsData<'drm>) -> Result {
+    pub(crate) fn flush_vm(&self, vm_as_data: &VmAsData<'res>) -> Result {
         self.as_manager.lock().flush_vm(vm_as_data)
     }
 
@@ -103,7 +103,7 @@ pub(crate) fn flush_vm(&self, vm_as_data: &VmAsData<'drm>) -> Result {
     /// If the VM is not resident, this is a NOP.
     pub(crate) fn start_vm_update(
         &self,
-        vm_as_data: &VmAsData<'drm>,
+        vm_as_data: &VmAsData<'res>,
         region: &Range<u64>,
     ) -> Result {
         self.as_manager.lock().start_vm_update(vm_as_data, region)
@@ -114,7 +114,7 @@ pub(crate) fn start_vm_update(
     /// If the VM is resident, this will let GPU accesses on the updated
     /// range go through, in case any of them were blocked.
     /// If the VM is not resident, this is a NOP.
-    pub(crate) fn end_vm_update(&self, vm_as_data: &VmAsData<'drm>) -> Result {
+    pub(crate) fn end_vm_update(&self, vm_as_data: &VmAsData<'res>) -> Result {
         self.as_manager.lock().end_vm_update(vm_as_data)
     }
 }
drivers/gpu/drm/tyr/mmu/address_space.rs +22 -22

122 lines not on this page · raw message

drivers/gpu/drm/tyr/vm.rs +48 -48

239 lines not on this page · raw message

Signature · 1 line
2.56.1
maintainer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] drm/tyr: clarify resource lifetime names

Quoted 9 lines · Danilo Krummrich

Use generic names for covariant lifetime parameters and 'res for invariant device resource lifetimes throughout Tyr's firmware, buffer, VM and MMU code.

Apply this convention consistently in preparation for introducing additional borrow lifetimes with self-referential pin-init.

reviewer Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] drm/tyr: clarify resource lifetime names

In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

Use generic names for covariant lifetime parameters and 'res for invariant device resource lifetimes throughout Tyr's firmware, buffer, VM and MMU code.

Apply this convention consistently in preparation for introducing additional borrow lifetimes with self-referential pin-init.

Quoted 7 lines
---
 drivers/gpu/drm/tyr/fw.rs                | 26 +++----
 drivers/gpu/drm/tyr/gem.rs               |  8 +-
 drivers/gpu/drm/tyr/mmu.rs               | 24 +++---
 drivers/gpu/drm/tyr/mmu/address_space.rs | 44 +++++------
 drivers/gpu/drm/tyr/vm.rs                | 96 ++++++++++++------------
 5 files changed, 99 insertions(+), 99 deletions(-)
author maintainer
#4 lore raw

PATCH v2 2/3 drm/tyr: use pin-init self-referential support

In reply to Danilo Krummrich

Now that pin-init supports self-referential structs, Arc is no longer needed just to share resources whose lifetimes are bounded by their owners.

Use pin-init's self-referential support to share the MMIO mapping, MMU and firmware VM through borrows instead of Arc, avoiding their separate allocations and reference counts.

Hide the invariant resource lifetime shared by the VM and its sections behind an existential lifetime that outlives 'mmu, so Firmware only needs a single lifetime parameter.

---
 drivers/gpu/drm/tyr/driver.rs            |  38 +++---
 drivers/gpu/drm/tyr/fw.rs                | 152 ++++++++++++-----------
 drivers/gpu/drm/tyr/gem.rs               |  15 +--
 drivers/gpu/drm/tyr/mmu.rs               |  36 +++---
 drivers/gpu/drm/tyr/mmu/address_space.rs |  14 +--
 drivers/gpu/drm/tyr/tyr.rs               |   5 +
 drivers/gpu/drm/tyr/vm.rs                |  93 +++++++-------
 7 files changed, 179 insertions(+), 174 deletions(-)
drivers/gpu/drm/tyr/driver.rs +18 -20
index 557f2ec0a767..a2fa7350f144 100644
@@ -29,7 +29,7 @@
     regulator::Regulator,
     sizes::SZ_2M,
     sync::{
-        Arc,
+        aref::ARef,
         Mutex, //
     },
     time, //
@@ -71,7 +71,14 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
     pub(crate) pdev: &'drm platform::Device<Bound>,
 
     /// Firmware sections.
-    pub(crate) fw: Firmware<'drm>,
+    #[pin]
+    #[uses('mmu: invariant)]
+    pub(crate) fw: Firmware<'mmu>,
+
+    /// MMU shared by the firmware and its buffer objects.
+    #[pin]
+    #[uses('iomem: invariant)]
+    mmu: Mmu<'iomem>,
 
     #[pin]
     clks: Mutex<Clocks>,
@@ -80,7 +87,7 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
     regulators: Mutex<Regulators>,
 
     /// GPU MMIO register mapping.
-    pub(crate) iomem: Arc<IoMem<'drm>>,
+    pub(crate) iomem: IoMem<'drm>,
 
     /// GPU information read from hardware during probe.
     pub(crate) gpu_info: GpuInfo,
@@ -135,7 +142,7 @@ fn probe<'bound>(
 
             let request = pdev.io_request_by_index(0).ok_or(ENODEV)?;
 
-            let iomem = Arc::new(request.iomap_sized::<SZ_2M>()?, GFP_KERNEL)?;
+            let iomem = request.iomap_sized::<SZ_2M>()?;
 
             issue_soft_reset(pdev.as_ref(), &iomem)?;
             gpu::l2_power_on(pdev.as_ref(), &iomem)?;
@@ -152,18 +159,7 @@ fn probe<'bound>(
             unsafe { pdev.dma_set_mask_and_coherent(DmaMask::try_new(pa_bits)?)? };
 
             let unreg_dev = drm::UnregisteredDevice::<TyrDrmDriver>::new(pdev)?;
-
-            let mmu = Mmu::new(pdev.as_ref(), iomem.as_arc_borrow(), &gpu_info)?;
-
-            let firmware = Firmware::new(
-                pdev.as_ref(),
-                iomem.clone(),
-                &unreg_dev,
-                mmu.as_arc_borrow(),
-                &gpu_info,
-            )?;
-
-            firmware.boot()?;
+            let ddev = ARef::from(&*unreg_dev);
 
             Ok(try_pin_init!(TyrPlatformDriverData {
                 // SAFETY: `_reg` is stored in `TyrPlatformDriverData` and dropped when the
@@ -171,9 +167,13 @@ fn probe<'bound>(
                 _reg <- unsafe { drm::Registration::new(
                     pdev.as_ref(),
                     unreg_dev,
-                    pin_init!(TyrDrmRegistrationData {
+                    try_pin_init!(TyrDrmRegistrationData {
+                        iomem,
+                        gpu_info,
+                        mmu <- Mmu::new(pdev.as_ref(), iomem, gpu_info),
+                        fw <- Firmware::new(pdev.as_ref(), iomem, &ddev, mmu.get_ref(), gpu_info),
+                        _: { fw.boot()?; },
                         pdev,
-                        fw: firmware,
                         clks <- new_mutex!(Clocks {
                             core: core_clk,
                             stacks: stacks_clk,
@@ -183,8 +183,6 @@ fn probe<'bound>(
                             _mali: mali_regulator,
                             _sram: sram_regulator,
                         }),
-                        iomem,
-                        gpu_info,
                     }),
                 )},
                 _: { dev_dbg!(pdev, "Tyr initialized correctly.") },
drivers/gpu/drm/tyr/fw.rs +79 -73

213 lines not on this page · raw message

drivers/gpu/drm/tyr/gem.rs +6 -9
index c9f9e5592e41..ecf22a277fc0 100644
@@ -12,10 +12,7 @@
         shmem, //
     },
     prelude::*,
-    sync::{
-        aref::ARef,
-        Arc, //
-    }, //
+    sync::aref::ARef, //
 };
 
 use crate::{
@@ -82,16 +79,16 @@ pub(crate) enum KernelBoVaAlloc {
 /// a GEM object with automatic GPU virtual address (VA) space mapping and cleanup.
 ///
 /// When dropped, the buffer is automatically unmapped from the GPU VA space.
-pub(crate) struct KernelBo<'res> {
+pub(crate) struct KernelBo<'a, 'res> {
     /// The underlying GEM buffer object.
     bo: ARef<Bo>,
     /// The GPU VM this buffer is mapped into.
-    vm: Arc<Vm<'res>>,
+    vm: &'a Vm<'a, 'res>,
     /// The GPU VA range occupied by this buffer.
     va_range: Range<u64>,
 }
 
-impl<'res> KernelBo<'res> {
+impl<'a, 'res> KernelBo<'a, 'res> {
     /// Creates a new kernel-owned buffer object and maps it into GPU VA space.
     ///
     /// This function allocates a new shmem-backed GEM object and immediately maps
@@ -99,7 +96,7 @@ impl<'res> KernelBo<'res> {
     /// cleaned up when the [`KernelBo`] is dropped.
     pub(crate) fn new(
         ddev: &TyrDrmDevice,
-        vm: Arc<Vm<'res>>,
+        vm: &'a Vm<'a, 'res>,
         size: u64,
         va_alloc: KernelBoVaAlloc,
         flags: VmMapFlags,
@@ -138,7 +135,7 @@ pub(crate) fn bo(&self) -> &Bo {
     }
 }
 
-impl Drop for KernelBo<'_> {
+impl Drop for KernelBo<'_, '_> {
     fn drop(&mut self) {
         let va = self.va_range.start;
         let size = self.va_range.end - self.va_range.start;
drivers/gpu/drm/tyr/mmu.rs +18 -18
index e708a181aedb..23daa7ad81d6 100644
@@ -21,7 +21,6 @@
     new_mutex,
     prelude::*,
     sync::{
-        Arc,
         ArcBorrow,
         Mutex, //
     }, //
@@ -57,26 +56,27 @@ impl<'res> Mmu<'res> {
     /// Create an MMU component for this device.
     pub(crate) fn new(
         dev: &'res Device<Bound>,
-        iomem: ArcBorrow<'_, IoMem<'res>>,
+        iomem: &'res IoMem<'res>,
         gpu_info: &GpuInfo,
-    ) -> Result<Arc<Mmu<'res>>> {
+    ) -> impl PinInit<Self, Error> + 'res {
         let present = AS_PRESENT::from_raw(gpu_info.as_present).present().get();
-        let slot_count = present.count_ones().try_into()?;
+        try_pin_init!(Self {
+            as_manager <- {
+                let slot_count = present.count_ones().try_into()?;
 
-        let address_space_manager = AddressSpaceManager::new(dev, iomem.into(), present)?;
-        let as_slot_manager =
-            SlotManager::new(address_space_manager, slot_count).inspect_err(|e| {
-                dev_err!(
-                    dev,
-                    "Failed to initialize MMU slot manager with {} slots: {:?}",
-                    slot_count,
-                    e
-                );
-            })?;
-        let mmu_init = try_pin_init!(Self{
-            as_manager <- new_mutex!(as_slot_manager),
-        });
-        Arc::pin_init(mmu_init, GFP_KERNEL)
+                let address_space_manager = AddressSpaceManager::new(dev, iomem, present)?;
+                let as_slot_manager =
+                    SlotManager::new(address_space_manager, slot_count).inspect_err(|e| {
+                        dev_err!(
+                            dev,
+                            "Failed to initialize MMU slot manager with {} slots: {:?}",
+                            slot_count,
+                            e
+                        );
+                    })?;
+                new_mutex!(as_slot_manager)
+            },
+        })
     }
 
     /// Assign a VM to an AS slot, provide a translation table,
drivers/gpu/drm/tyr/mmu/address_space.rs +7 -7
index 49899d395249..f6fdc70a7c58 100644
@@ -152,7 +152,7 @@ pub(crate) struct AddressSpaceManager<'a> {
     dev: &'a Device<Bound>,
 
     /// Memory-mapped I/O region for GPU register access.
-    iomem: Arc<IoMem<'a>>,
+    iomem: &'a IoMem<'a>,
 
     /// Bitmask of present address space slots from GPU_AS_PRESENT register.
     as_present: u32,
@@ -165,7 +165,7 @@ impl<'a> AddressSpaceManager<'a> {
     /// I/O memory region, along with the bitmask of available AS slots.
     pub(super) fn new(
         dev: &'a Device<Bound>,
-        iomem: Arc<IoMem<'a>>,
+        iomem: &'a IoMem<'a>,
         as_present: u32,
     ) -> Result<AddressSpaceManager<'a>> {
         if as_present.trailing_ones() != as_present.count_ones() {
@@ -216,7 +216,7 @@ fn validate_as_slot(&self, as_nr: usize) -> Result {
     ///
     /// Returns an error if polling times out after 10ms or if register access fails.
     fn as_wait_ready(&self, as_nr: usize) -> Result {
-        let io = &*self.iomem;
+        let io = self.iomem;
         let op = || {
             let status_reg = STATUS::try_at(as_nr).ok_or(EINVAL)?;
             Ok(io.read(status_reg))
@@ -232,7 +232,7 @@ fn as_wait_ready(&self, as_nr: usize) -> Result {
     /// Returns an error if waiting for ready times out or if register write fails.
     fn as_send_cmd(&mut self, as_nr: usize, cmd: MmuCommand) -> Result {
         self.as_wait_ready(as_nr)?;
-        let io = &*self.iomem;
+        let io = self.iomem;
         let command_reg = COMMAND::try_at(as_nr).ok_or(EINVAL)?;
         io.write(command_reg, COMMAND::zeroed().with_command(cmd));
         Ok(())
@@ -253,7 +253,7 @@ fn as_send_cmd_and_wait(&mut self, as_nr: usize, cmd: MmuCommand) -> Result {
     fn as_enable(&mut self, as_nr: usize, as_config: &AddressSpaceConfig) -> Result {
         self.validate_as_slot(as_nr)?;
 
-        let io = &*self.iomem;
+        let io = self.iomem;
 
         let transtab = as_config.transtab;
         io.write(
@@ -299,7 +299,7 @@ fn as_disable(&mut self, as_nr: usize) -> Result {
         // Flush AS before disabling
         self.as_send_cmd_and_wait(as_nr, MmuCommand::FlushMem)?;
 
-        let io = &*self.iomem;
+        let io = self.iomem;
 
         io.write(
             TRANSTAB_LO::try_at(as_nr).ok_or(EINVAL)?,
@@ -394,7 +394,7 @@ fn as_start_update(&mut self, as_nr: usize, region: &Range<u64>) -> Result {
         // because log2(32 KiB) = 15.
         let lockaddr_size = lock_region_log2 - 1;
 
-        let io = &*self.iomem;
+        let io = self.iomem;
 
         // The LOCKADDR base field stores address bits 63:12, so remove the low 12 bits
         // before passing this value to the register macro helper.
drivers/gpu/drm/tyr/tyr.rs +5 -0
index e7ec450bdc9c..7575a5365353 100644
@@ -5,6 +5,11 @@
 //! The name "Tyr" is inspired by Norse mythology, reflecting Arm's tradition of
 //! naming their GPUs after Nordic mythological figures and places.
 
+#![allow(
+    clippy::multiple_bound_locations,
+    reason = "pin_data generates additional outlives bounds for borrowed fields"
+)]
+
 use crate::driver::TyrPlatformDriver;
 
 mod driver;
drivers/gpu/drm/tyr/vm.rs +46 -47

148 lines not on this page · raw message

Signature · 1 line
2.56.1
maintainer Reviewed-by on v2 2/3
#5 lore raw
Quoted 9 lines · Danilo Krummrich

Now that pin-init supports self-referential structs, Arc is no longer needed just to share resources whose lifetimes are bounded by their owners.

Use pin-init's self-referential support to share the MMIO mapping, MMU and firmware VM through borrows instead of Arc, avoiding their separate allocations and reference counts.

Quoted 6 lines

Hide the invariant resource lifetime shared by the VM and its sections behind an existential lifetime that outlives 'mmu, so Firmware only needs a single lifetime parameter.

reviewer Reviewed-by on v2 2/3
#6 lore raw
In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

Now that pin-init supports self-referential structs, Arc is no longer needed just to share resources whose lifetimes are bounded by their owners.

Use pin-init's self-referential support to share the MMIO mapping, MMU and firmware VM through borrows instead of Arc, avoiding their separate allocations and reference counts.

Quoted 6 lines

Hide the invariant resource lifetime shared by the VM and its sections behind an existential lifetime that outlives 'mmu, so Firmware only needs a single lifetime parameter.

drivers/gpu/drm/tyr/driver.rs pub(crate) struct TyrDrmRegistrationData<'drm> {

Quoted 40 lines
---
 drivers/gpu/drm/tyr/driver.rs            |  38 +++---
 drivers/gpu/drm/tyr/fw.rs                | 152 ++++++++++++-----------
 drivers/gpu/drm/tyr/gem.rs               |  15 +--
 drivers/gpu/drm/tyr/mmu.rs               |  36 +++---
 drivers/gpu/drm/tyr/mmu/address_space.rs |  14 +--
 drivers/gpu/drm/tyr/tyr.rs               |   5 +
 drivers/gpu/drm/tyr/vm.rs                |  93 +++++++-------
 7 files changed, 179 insertions(+), 174 deletions(-)
drivers/gpu/drm/tyr/driver.rs +9 -2
index 557f2ec0a767..a2fa7350f144 100644
@@ -29,7 +29,7 @@
     regulator::Regulator,
     sizes::SZ_2M,
     sync::{
-        Arc,
+        aref::ARef,
         Mutex, //
     },
     time, //
@@ -71,7 +71,14 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
     pub(crate) pdev: &'drm platform::Device<Bound>,
 
     /// Firmware sections.
-    pub(crate) fw: Firmware<'drm>,
+    #[pin]
+    #[uses('mmu: invariant)]
+    pub(crate) fw: Firmware<'mmu>,
+
+    /// MMU shared by the firmware and its buffer objects.
+    #[pin]
+    #[uses('iomem: invariant)]
+    mmu: Mmu<'iomem>,
 
     #[pin]
     clks: Mutex<Clocks>,
@@ -80,7 +87,7 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {

This isn't renamed because it's top-level I suppose?

Best,
Gary

Quoted 8 lines
drivers/gpu/drm/tyr/driver.rs +1 -1
     regulators: Mutex<Regulators>,
 
     /// GPU MMIO register mapping.
-    pub(crate) iomem: Arc<IoMem<'drm>>,
+    pub(crate) iomem: IoMem<'drm>,
 
     /// GPU information read from hardware during probe.
     pub(crate) gpu_info: GpuInfo,
author maintainer
#7 lore raw

PATCH v2 3/3 drm/tyr: use ScopeGuard for firmware loading cleanup

In reply to Danilo Krummrich

Use ScopeGuard to call vm.kill() when firmware section loading fails. Dismiss it on success, replacing the nested closure and explicit error check.

---
 drivers/gpu/drm/tyr/fw.rs | 59 ++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 32 deletions(-)
drivers/gpu/drm/tyr/fw.rs +27 -32
index 698c3e9b6845..5505cfb6a916 100644
@@ -29,7 +29,8 @@
     num::Bounded,
     prelude::*,
     str::CString,
-    time, //
+    time,
+    types::ScopeGuard, //
 };
 
 use crate::{
@@ -228,46 +229,40 @@ pub(crate) fn new<'a, 'res>(
             sections: {
                 let vm = vm.get_ref();
                 vm.activate()?;
+                let guard = ScopeGuard::new(|| vm.kill());
 
-                let result: Result<_> = (|| {
-                    let (fw, parsed_sections) = Self::load(dev, ddev, gpu_info)?;
-                    let mut sections = KVec::new();
-                    for parsed in parsed_sections {
-                        let size =
-                            u64::from(parsed.va.end.checked_sub(parsed.va.start).ok_or(EINVAL)?);
+                let (fw, parsed_sections) = Self::load(dev, ddev, gpu_info)?;
+                let mut sections = KVec::new();
+                for parsed in parsed_sections {
+                    let size =
+                        u64::from(parsed.va.end.checked_sub(parsed.va.start).ok_or(EINVAL)?);
 
-                        let va = u64::from(parsed.va.start);
+                    let va = u64::from(parsed.va.start);
 
-                        let mut mem = KernelBo::new(
-                            ddev,
-                            vm,
-                            size,
-                            KernelBoVaAlloc::Explicit(va),
-                            parsed.vm_map_flags,
-                        )?;
+                    let mut mem = KernelBo::new(
+                        ddev,
+                        vm,
+                        size,
+                        KernelBoVaAlloc::Explicit(va),
+                        parsed.vm_map_flags,
+                    )?;
 
-                        let section_start = parsed.data_range.start as usize;
-                        let section_end = parsed.data_range.end as usize;
-                        let mut data = KVec::new();
+                    let section_start = parsed.data_range.start as usize;
+                    let section_end = parsed.data_range.end as usize;
+                    let mut data = KVec::new();
 
-                        // Ensure that the firmware slice is not out of bounds.
-                        let fw_data = fw.data();
-                        let bytes = fw_data.get(section_start..section_end).ok_or(EINVAL)?;
-                        data.extend_from_slice(bytes, GFP_KERNEL)?;
+                    // Ensure that the firmware slice is not out of bounds.
+                    let fw_data = fw.data();
+                    let bytes = fw_data.get(section_start..section_end).ok_or(EINVAL)?;
+                    data.extend_from_slice(bytes, GFP_KERNEL)?;
 
-                        Self::init_section_mem(dev, &mut mem, &data)?;
+                    Self::init_section_mem(dev, &mut mem, &data)?;
 
-                        sections.push(Section { data, mem }, GFP_KERNEL)?;
-                    }
-
-                    Ok(sections)
-                })();
-
-                if result.is_err() {
-                    vm.kill();
+                    sections.push(Section { data, mem }, GFP_KERNEL)?;
                 }
 
-                result?
+                guard.dismiss();
+                sections
             },
         })
     }
Signature · 1 line
2.56.1
reviewer Reviewed-by on v2 3/3
#9 lore raw

Re: [PATCH v2 3/3] drm/tyr: use ScopeGuard for firmware loading cleanup

In reply to Danilo Krummrich
Quoted 5 lines · Danilo Krummrich

Use ScopeGuard to call vm.kill() when firmware section loading fails. Dismiss it on success, replacing the nested closure and explicit error check.

Looks much cleaner!

Quoted 3 lines
---
 drivers/gpu/drm/tyr/fw.rs | 59 ++++++++++++++++++---------------------
 1 file changed, 27 insertions(+), 32 deletions(-)
maintainer
#10 lore raw
In reply to Danilo Krummrich

Hi folks,

Quoted 31 lines · Danilo Krummrich

This series uses pin-init's self-referential support to replace Arcs for resources whose lifetimes are bounded by their owners.

It also cleans up lifetime names and simplifies firmware loading cleanup with ScopeGuard.

Changes in v2:
- Use generic names for covariant lifetime parameters and resource names for invariant ones.
- Include VmAsData, GpuVmData, the VM operation types and PtUpdateContext in the lifetime cleanup.
- Drop the pin-init lint suppression patch and temporarily allow clippy::multiple_bound_locations in Tyr instead.

Danilo Krummrich (3):
drm/tyr: clarify resource lifetime names drm/tyr: use pin-init self-referential support drm/tyr: use ScopeGuard for firmware loading cleanup

drivers/gpu/drm/tyr/driver.rs            |  38 +++--
drivers/gpu/drm/tyr/fw.rs                | 151 +++++++++----------
drivers/gpu/drm/tyr/gem.rs               |  15 +-
drivers/gpu/drm/tyr/mmu.rs               |  56 ++++----
drivers/gpu/drm/tyr/mmu/address_space.rs |  54 +++----
drivers/gpu/drm/tyr/tyr.rs               |   5 +
drivers/gpu/drm/tyr/vm.rs                | 175 +++++++++++------------
7 files changed, 247 insertions(+), 247 deletions(-)
Quoted 4 lines

base-commit: abfb640b2374e26de8ff81d488c39d8254db538e

Signature · 1 line
2.56.1

I am actively trying to rebase our downstream work on this.

Please wait for a tested-by tag, it may or may not reveal something relevant.

— Daniel