Skip to contentSkip to conversation

drm/tyr: GPU reset infrastructure

PATCH In review Onur Özkan 4 participants 8 messages Nov 22

Subsystems (4)
lore
Share & tools
lore

Onur Özkan

author reviewer
#0 lore raw

v9 cover drm/tyr: GPU reset infrastructure

Add support for scheduling GPU resets on a dedicated workqueue. Track the reset state to avoid queueing another reset while one is already pending or in progress.

Use an SRCU based gate with mutex-protected reader admission to block hardware accesses while reset work runs and wait for current users before resetting.

Stop new reset requests during teardown and drain any queued or running reset work before releasing the device resources.

This is the initial reset infrastructure only. It is not wired to a reset source yet as those will follow in separate work.

Based on drm-rust-next commit 0694e2390c5b ("rust: drm: remove DRM device private data").

Changes since v8:
- Use workqueue v4 [1].
- Drop "drm/tyr: clear stale IRQ state before soft reset" (applied on drm-rust-next already).
- Minor fixes (Alice's notes on v8).

V8: https://lore.kernel.org/all/[email protected]/

---
Onur Özkan (2):
drm/tyr: add GPU reset infrastructure
drm/tyr: put iomem behind the hardware gate

 drivers/gpu/drm/tyr/driver.rs            |  67 ++++-----
 drivers/gpu/drm/tyr/fw.rs                |  16 +-
 drivers/gpu/drm/tyr/mmu.rs               |   6 +-
 drivers/gpu/drm/tyr/mmu/address_space.rs |  87 ++++++-----
 drivers/gpu/drm/tyr/reset.rs             | 243 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/tyr/reset/hw_gate.rs     | 106 ++++++++++++++
 drivers/gpu/drm/tyr/tyr.rs               |   1 +
 7 files changed, 440 insertions(+), 86 deletions(-)

---
base-commit: 0694e2390c5be63aae9d713a173df18d82d3b94b
change-id: 20260813-tyr-reset-impl-93e951f996b4
prerequisite-change-id: 20261118-ordered-wq-cancel-63b02e0feac4:v1
prerequisite-patch-id: 7a1020bc70ad38e61214d85202e04b1db3c95443
prerequisite-patch-id: dbf1c8ccc147075be256faf6a547d9723c347a93
prerequisite-change-id: 20261012-rust-workqueue-658cd0fbe770:v4
prerequisite-patch-id: 2423b22dfde216dce30af3deb7eb7177505f5b96
prerequisite-patch-id: f5e24f7b3717f2ab0445b5395c7f12b554861d78
prerequisite-patch-id: 0bf6ae4abcef7090d0e48921d6ec627a59dc6a7a
prerequisite-patch-id: cba4cd096a4df20f4597e9ea639881213c18a41b
prerequisite-patch-id: 97b26feddfa5b675204c9e8e75f3ad2958ec268a

author reviewer
#1 lore raw

PATCH v9 1/2 drm/tyr: add GPU reset infrastructure

Add support for scheduling GPU resets on a dedicated workqueue. Track the reset state to avoid queueing another reset while one is already pending or in progress.

Use an SRCU based gate with mutex-protected reader admission to block hardware accesses while reset work runs and wait for current users before resetting.

Stop new reset requests during teardown and drain any queued or running reset work before releasing the device resources.

This is the initial reset infrastructure only. It is not wired to a reset source yet as those will follow in separate work.

---
 drivers/gpu/drm/tyr/driver.rs        |  43 ++----
 drivers/gpu/drm/tyr/reset.rs         | 250 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/tyr/reset/hw_gate.rs |  80 +++++++++++
 drivers/gpu/drm/tyr/tyr.rs           |   1 +
 4 files changed, 345 insertions(+), 29 deletions(-)
drivers/gpu/drm/tyr/driver.rs +14 -29
index 557f2ec0a767..370ab512617c 100644
@@ -8,7 +8,6 @@
     device::{
         Bound,
         Core,
-        Device,
         DeviceContext, //
     },
     dma::{
@@ -17,13 +16,9 @@
     },
     drm,
     drm::ioctl,
-    io::{
-        poll,
-        Io, //
-    },
     new_mutex,
     of,
-    platform,
+    platform, //
     prelude::*,
     regulator,
     regulator::Regulator,
@@ -32,17 +27,16 @@
         Arc,
         Mutex, //
     },
-    time, //
 };
 
 use crate::{
     file::TyrDrmFileData,
     fw::Firmware,
     gem::Bo,
-    gpu,
     gpu::GpuInfo,
     mmu::Mmu,
-    regs::gpu_control::*, //
+    regs::gpu_control::*,
+    reset, //
 };
 
 pub(crate) type IoMem<'a> = kernel::io::mem::IoMem<'a, SZ_2M>;
@@ -70,6 +64,12 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
     /// Parent platform device.
     pub(crate) pdev: &'drm platform::Device<Bound>,
 
+    // `ResetHandle::drop()` drains queued/running works and this must happen
+    // before clocks/regulators are dropped. So keep this field before them to
+    // ensure the correct drop order.
+    #[pin]
+    pub(crate) reset: reset::ResetHandle<'drm>,
+
     /// Firmware sections.
     pub(crate) fw: Firmware<'drm>,
 
@@ -86,23 +86,6 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
     pub(crate) gpu_info: GpuInfo,
 }
 
-fn issue_soft_reset(dev: &Device, iomem: &IoMem<'_>) -> Result {
-    // Clear any stale reset IRQ state before issuing a new soft reset.
-    iomem.write_reg(GPU_IRQ_CLEAR::zeroed().with_reset_completed(true));
-
-    iomem.write_reg(GPU_COMMAND::reset(ResetMode::SoftReset));
-
-    poll::read_poll_timeout(
-        || Ok(iomem.read(GPU_IRQ_RAWSTAT)),
-        |status| status.reset_completed(),
-        time::Delta::from_millis(1),
-        time::Delta::from_millis(100),
-    )
-    .inspect_err(|_| dev_err!(dev, "GPU reset failed."))?;
-
-    Ok(())
-}
-
 kernel::of_device_table!(
     OF_TABLE,
     <TyrPlatformDriver as platform::Driver>::IdInfo,
@@ -137,8 +120,7 @@ fn probe<'bound>(
 
             let iomem = Arc::new(request.iomap_sized::<SZ_2M>()?, GFP_KERNEL)?;
 
-            issue_soft_reset(pdev.as_ref(), &iomem)?;
-            gpu::l2_power_on(pdev.as_ref(), &iomem)?;
+            reset::run_reset(pdev.as_ref(), &iomem)?;
 
             let gpu_info = GpuInfo::new(&iomem);
             gpu_info.log(pdev.as_ref());
@@ -171,8 +153,11 @@ fn probe<'bound>(
                 _reg <- unsafe { drm::Registration::new(
                     pdev.as_ref(),
                     unreg_dev,
-                    pin_init!(TyrDrmRegistrationData {
+                    try_pin_init!(TyrDrmRegistrationData {
                         pdev,
+                        // SAFETY: The registration is owned by platform driver data,
+                        // so reset work is drained before borrowed data expires.
+                        reset <- reset::ResetHandle::new(pdev, iomem.as_arc_borrow()),
                         fw: firmware,
                         clks <- new_mutex!(Clocks {
                             core: core_clk,
drivers/gpu/drm/tyr/reset.rs new file +250 -0

250 lines not on this page · raw message

drivers/gpu/drm/tyr/reset/hw_gate.rs new file +80 -0

80 lines not on this page · raw message

drivers/gpu/drm/tyr/tyr.rs +1 -0
index e7ec450bdc9c..9d995650be81 100644
@@ -14,6 +14,7 @@
 mod gpu;
 mod mmu;
 mod regs;
+mod reset;
 mod slot;
 mod vm;
 
Signature · 1 line
2.51.2
#2 lore raw

Re: [PATCH v9 1/2] drm/tyr: add GPU reset infrastructure

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] drm/tyr: prepared clocks leaked on error in TyrPlatformDriver::probe() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] drm/tyr: prepared clocks leaked on error in TyrPlatformDriver::probe()
--

commit 4717fa42d015d47181768a9ad839f18566adf1c5
Author: Onur Özkan <[email protected]>

drm/tyr: add GPU reset infrastructure

This commit adds asynchronous GPU reset handling on a dedicated ordered workqueue with an SRCU-based hardware access gate, and wires ResetHandle into TyrDrmRegistrationData.

Quoted 4 lines
drivers/gpu/drm/tyr/driver.rs +0 -0
index 557f2ec0a7673..370ab512617c9 100644

[ ... ]

drivers/gpu/drm/tyr/driver.rs fn probe<'bound>( · line 163

Quoted 13 lines
drivers/gpu/drm/tyr/driver.rs +4 -1
@@ -171,8 +153,11 @@ fn probe<'bound>(
                 _reg <- unsafe { drm::Registration::new(
                     pdev.as_ref(),
                     unreg_dev,
-                    pin_init!(TyrDrmRegistrationData {
+                    try_pin_init!(TyrDrmRegistrationData {
                         pdev,
+                        // SAFETY: The registration is owned by platform driver data,
+                        // so reset work is drained before borrowed data expires.
+                        reset <- reset::ResetHandle::new(pdev, iomem.as_arc_borrow()),
                         fw: firmware,
                         clks <- new_mutex!(Clocks {
                             core: core_clk,

[Severity: Medium]
Does this leak the prepared and enabled clocks if reset::ResetHandle::new() fails?

Clk::drop() only calls clk_put(), and disable_unprepare() is only called by Clocks::drop(). Because core_clk, stacks_clk and coregroup_clk are held as bare Clk and OptionalClk variables until clks is initialized after reset, a failure in ResetHandle::new() (or earlier in probe() after prepare_enable()) drops the clocks before Clocks is constructed, leaving them prepared and enabled and triggering a warning in clk_put(). Wrapping the clocks in Clocks right after prepare_enable() would ensure they are disabled and unprepared on any subsequent probe error.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v9 1/2
#3 lore raw

Re: [PATCH v9 1/2] drm/tyr: add GPU reset infrastructure

In reply to Onur Özkan
Quoted 11 lines · Onur Özkan

Add support for scheduling GPU resets on a dedicated workqueue. Track the reset state to avoid queueing another reset while one is already pending or in progress.

Use an SRCU based gate with mutex-protected reader admission to block hardware accesses while reset work runs and wait for current users before resetting.

Stop new reset requests during teardown and drain any queued or running reset work before releasing the device resources.

Quoted 7 lines

This is the initial reset infrastructure only. It is not wired to a reset source yet as those will follow in separate work.

author reviewer
#4 lore raw

PATCH v9 2/2 drm/tyr: put iomem behind the hardware gate

In reply to Onur Özkan

Move iomem mapping into HwGate and pass Arc<HwGate> to components that access hardware. Callers obtain HwAccessGuard before accessing the iomem so the reset worker waits for ongoing accesses.

Acquire the gate once per address-space operation and pass the guarded iomem reference to its helpers. This prevents nested gate acquisition from deadlocking with reset and keeps each operation protected throughout. Clone the gate Arc before acquiring the guard so mutating helpers can retain their mutable self receivers.

---
 drivers/gpu/drm/tyr/driver.rs            | 28 +++++-----
 drivers/gpu/drm/tyr/fw.rs                | 16 +++---
 drivers/gpu/drm/tyr/mmu.rs               |  6 +--
 drivers/gpu/drm/tyr/mmu/address_space.rs | 87 ++++++++++++++++++--------------
 drivers/gpu/drm/tyr/reset.rs             | 25 ++++-----
 drivers/gpu/drm/tyr/reset/hw_gate.rs     | 44 ++++++++++++----
 6 files changed, 122 insertions(+), 84 deletions(-)
drivers/gpu/drm/tyr/driver.rs +16 -12
index 370ab512617c..9aa536b16972 100644
@@ -79,9 +79,6 @@ pub(crate) struct TyrDrmRegistrationData<'drm> {
     #[pin]
     regulators: Mutex<Regulators>,
 
-    /// GPU MMIO register mapping.
-    pub(crate) iomem: Arc<IoMem<'drm>>,
-
     /// GPU information read from hardware during probe.
     pub(crate) gpu_info: GpuInfo,
 }
@@ -118,12 +115,20 @@ 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)?;
-
-            reset::run_reset(pdev.as_ref(), &iomem)?;
+            // TODO: Consider embedding HwGate in platform data and using self referential
+            // pin-init instead of Arc.
+            let hw = Arc::pin_init(
+                reset::HwGate::new(request.iomap_sized::<SZ_2M>()?),
+                GFP_KERNEL,
+            )?;
 
-            let gpu_info = GpuInfo::new(&iomem);
-            gpu_info.log(pdev.as_ref());
+            let gpu_info = {
+                let hw_guard = hw.access();
+                reset::run_reset(pdev.as_ref(), hw_guard.iomem())?;
+                let gpu_info = GpuInfo::new(hw_guard.iomem());
+                gpu_info.log(pdev.as_ref());
+                gpu_info
+            };
 
             let pa_bits = MMU_FEATURES::from_raw(gpu_info.mmu_features)
                 .pa_bits()
@@ -135,11 +140,11 @@ fn probe<'bound>(
 
             let unreg_dev = drm::UnregisteredDevice::<TyrDrmDriver>::new(pdev)?;
 
-            let mmu = Mmu::new(pdev.as_ref(), iomem.as_arc_borrow(), &gpu_info)?;
+            let mmu = Mmu::new(pdev.as_ref(), hw.clone(), &gpu_info)?;
 
             let firmware = Firmware::new(
                 pdev.as_ref(),
-                iomem.clone(),
+                hw.clone(),
                 &unreg_dev,
                 mmu.as_arc_borrow(),
                 &gpu_info,
@@ -157,7 +162,7 @@ fn probe<'bound>(
                         pdev,
                         // SAFETY: The registration is owned by platform driver data,
                         // so reset work is drained before borrowed data expires.
-                        reset <- reset::ResetHandle::new(pdev, iomem.as_arc_borrow()),
+                        reset <- reset::ResetHandle::new(pdev, hw.clone()),
                         fw: firmware,
                         clks <- new_mutex!(Clocks {
                             core: core_clk,
@@ -168,7 +173,6 @@ fn probe<'bound>(
                             _mali: mali_regulator,
                             _sram: sram_regulator,
                         }),
-                        iomem,
                         gpu_info,
                     }),
                 )},
drivers/gpu/drm/tyr/fw.rs +9 -7
index aea151ff1f93..f2e0404addf2 100644
@@ -38,7 +38,6 @@
 
 use crate::{
     driver::{
-        IoMem,
         TyrDrmDevice, //
     },
     fw::parser::{
@@ -66,6 +65,7 @@
             JOB_IRQ_RAWSTAT, //
         }, //
     },
+    reset::HwGate,
     vm::Vm, //
 };
 
@@ -144,8 +144,8 @@ struct Section<'drm> {
 
 /// Loaded firmware with sections mapped into MCU VM.
 pub(crate) struct Firmware<'drm> {
-    /// Iomem need to access registers.
-    iomem: Arc<IoMem<'drm>>,
+    /// Shared gate that coordinates hardware access with GPU reset.
+    hw: Arc<HwGate<'drm>>,
 
     /// MCU VM.
     vm: Arc<Vm<'drm>>,
@@ -214,7 +214,7 @@ fn load(
     /// Load firmware and map sections into MCU VM.
     pub(crate) fn new(
         dev: &'drm Device<Bound>,
-        iomem: Arc<IoMem<'drm>>,
+        hw: Arc<HwGate<'drm>>,
         ddev: &TyrDrmDevice,
         mmu: ArcBorrow<'_, Mmu<'drm>>,
         gpu_info: &GpuInfo,
@@ -253,7 +253,7 @@ pub(crate) fn new(
             }
 
             Ok(Firmware {
-                iomem,
+                hw,
                 vm: vm.clone(),
                 sections,
             })
@@ -267,7 +267,8 @@ pub(crate) fn new(
     }
 
     pub(crate) fn boot(&self) -> Result {
-        let io = &self.iomem;
+        let hw_guard = self.hw.access();
+        let io = hw_guard.iomem();
 
         // Discard any stale global interrupt.
         io.write_reg(JOB_IRQ_CLEAR::zeroed().with_glb(true));
@@ -297,7 +298,8 @@ pub(crate) fn boot(&self) -> Result {
     }
 
     fn stop(&self) -> Result {
-        let io = &self.iomem;
+        let hw_guard = self.hw.access();
+        let io = hw_guard.iomem();
         io.write_reg(MCU_CONTROL::zeroed().with_req(McuControlMode::Disable));
 
         if let Err(e) = poll::read_poll_timeout(
drivers/gpu/drm/tyr/mmu.rs +3 -3
index 5e88c9e6a4cd..adac036744a3 100644
@@ -28,7 +28,6 @@
 };
 
 use crate::{
-    driver::IoMem,
     gpu::GpuInfo,
     mmu::address_space::{
         AddressSpaceManager,
@@ -38,6 +37,7 @@
         gpu_control::AS_PRESENT,
         MAX_AS, //
     },
+    reset::HwGate,
     slot::SlotManager, //
 };
 
@@ -57,13 +57,13 @@ impl<'drm> Mmu<'drm> {
     /// Create an MMU component for this device.
     pub(crate) fn new(
         dev: &'drm Device<Bound>,
-        iomem: ArcBorrow<'_, IoMem<'drm>>,
+        hw: Arc<HwGate<'drm>>,
         gpu_info: &GpuInfo,
     ) -> Result<Arc<Mmu<'drm>>> {
         let present = AS_PRESENT::from_raw(gpu_info.as_present).present().get();
         let slot_count = present.count_ones().try_into()?;
 
-        let address_space_manager = AddressSpaceManager::new(dev, iomem.into(), present)?;
+        let address_space_manager = AddressSpaceManager::new(dev, hw, present)?;
         let as_slot_manager =
             SlotManager::new(address_space_manager, slot_count).inspect_err(|e| {
                 dev_err!(
drivers/gpu/drm/tyr/mmu/address_space.rs +50 -37

223 lines not on this page · raw message

drivers/gpu/drm/tyr/reset.rs +9 -16
index 30a668722d3f..559d8cad1297 100644
@@ -21,7 +21,7 @@
 
 mod hw_gate;
 
-use hw_gate::HwGate;
+pub(crate) use hw_gate::HwGate;
 
 use kernel::{
     device::{
@@ -41,8 +41,7 @@
             Full,
             Release, //
         },
-        Arc,
-        ArcBorrow, //
+        Arc, //
     },
     time,
     workqueue::{
@@ -84,13 +83,10 @@ unsafe impl AtomicType for ResetState {
 struct Controller<'ctrl> {
     /// Parent platform device.
     pdev: &'ctrl platform::Device<Bound>,
-    /// Mapped register space needed for reset operations.
-    iomem: Arc<IoMem<'ctrl>>,
     /// State shared by reset schedulers and the worker.
     state: Atomic<ResetState>,
-    /// Drains reset-sensitive hardware accesses before a reset.
-    #[pin]
-    hw: HwGate,
+    /// Shared gate that coordinates hardware access with GPU reset.
+    hw: Arc<HwGate<'ctrl>>,
 }
 
 impl<'ctrl> ScopedWorkItem for Controller<'ctrl> {
@@ -103,13 +99,12 @@ impl<'ctrl> Controller<'ctrl> {
     /// Creates a reset controller.
     fn new(
         pdev: &'ctrl platform::Device<Bound>,
-        iomem: Arc<IoMem<'ctrl>>,
+        hw: Arc<HwGate<'ctrl>>,
     ) -> impl PinInit<Self, Error> {
         try_pin_init!(Self {
             pdev,
-            iomem,
             state: Atomic::new(ResetState::Idle),
-            hw <- HwGate::new(),
+            hw,
         })
     }
 
@@ -137,7 +132,7 @@ fn reset_work(&self) {
         dev_dbg!(self.pdev, "Starting GPU reset.\n");
 
         let reset_guard = self.hw.close();
-        let reset_result = run_reset(self.pdev.as_ref(), &self.iomem);
+        let reset_result = run_reset(self.pdev.as_ref(), reset_guard.iomem());
         let _ = self.try_transition(ResetState::InProgress, ResetState::Idle);
         drop(reset_guard);
 
@@ -173,16 +168,14 @@ impl<'reset> ResetHandle<'reset> {
     /// running [`Drop`], since it owns work that may borrow from `'reset`.
     pub(crate) unsafe fn new(
         pdev: &'reset platform::Device<Bound>,
-        iomem: ArcBorrow<'_, IoMem<'reset>>,
+        hw: Arc<HwGate<'reset>>,
     ) -> impl PinInit<Self, Error> {
-        let iomem = iomem.into();
-
         try_pin_init!(Self {
             wq: Queue::new_ordered().build(c"tyr-reset-wq")?,
             // SAFETY: The caller guarantees that the handle is dropped and `controller`
             // is declared before `wq`, so its destructor runs before the queue expires.
             controller <- unsafe {
-                kernel::new_scoped_work!(wq, "tyr::reset", Controller::new(pdev, iomem))
+                kernel::new_scoped_work!(wq, "tyr::reset", Controller::new(pdev, hw))
             },
         })
     }
drivers/gpu/drm/tyr/reset/hw_gate.rs +35 -9

93 lines not on this page · raw message

Signature · 1 line
2.51.2
maintainer Reviewed-by on v9 2/2
#5 lore raw

Re: [PATCH v9 2/2] drm/tyr: put iomem behind the hardware gate

Quoted 10 lines · Onur Özkan

Move iomem mapping into HwGate and pass Arc<HwGate> to components that access hardware. Callers obtain HwAccessGuard before accessing the iomem so the reset worker waits for ongoing accesses.

Acquire the gate once per address-space operation and pass the guarded iomem reference to its helpers. This prevents nested gate acquisition from deadlocking with reset and keeps each operation protected throughout. Clone the gate Arc before acquiring the guard so mutating helpers can retain their mutable self receivers.

Quoted 3 lines
maintainer
#6 lore raw
In reply to Onur Özkan
Quoted 5 lines · Onur Özkan

Changes since v8:
- Use workqueue v4 [1].
- Drop "drm/tyr: clear stale IRQ state before soft reset" (applied on drm-rust-next already).
- Minor fixes (Alice's notes on v8).

Thanks for the updates! Both patches look good to me now; I've left my Reviewed-by on them and we can apply this series to drm-rust-next once the OwnedQueue/ScopedWork workqueue dependency lands.

Alice

maintainer
#7 lore raw
In reply to Onur Özkan

Hi Onur,

We can probably fix the sashiko findings once the Clk typestate patchset lands. In any case, what we have so far is good for the time being.

— Daniel