All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4] drm/imagination: re-check firmware idle state before runtime suspend
@ 2026-12-03 10:15 Baihui Liang
  2026-12-03 10:45 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Baihui Liang @ 2026-12-03 10:15 UTC (permalink / raw)
  To: Alessio Belle, Luigi Santivetti, Maarten Lankhorst, Maxime Ripard,
	Thomas Zimmermann, David Airlie, Simona Vetter
  Cc: imagination, dri-devel, linux-kernel, gye976, Troy Mitchell,
	Brajesh.Gupta, Baihui Liang

pvr_power_device_idle() rejects runtime suspend while the firmware
hasn't reached ROGUE_FWIF_POW_IDLE. The PM core doesn't reschedule
idle notification after rejection, so no further pm_runtime_put()
arrives on an idle GPU and the device stays powered indefinitely.

Move the idle check into pvr_power_device_suspend() and return -EBUSY
after pm_runtime_mark_last_busy() so the PM core reschedules the
autosuspend timer and retries until the firmware is idle.

Factor the power-off sequence into a helper and call it from the
removal path with runtime PM disabled, bypassing the idle check.
Power off before destroying the job and free-list ID arrays, since
the IRQ handler can still process firmware commands during shutdown
and look up free-list IDs.

Fixes: 727538a4bbff ("drm/imagination: Implement power management")
Signed-off-by: Baihui Liang <liangbaihui@linux.spacemit.com>
---
Tested on SpacemiT K3 (PowerVR Rogue BXM-4-64) by running vulkaninfo
repeatedly in a loop. Without this change the device intermittently
stayed runtime-active after the last user went away, because the
firmware had not yet reached ROGUE_FWIF_POW_IDLE when the idle check
ran. With the change the device reliably suspends.

Changes in v4:
- Inline pvr_power_device_shutdown() into pvr_remove() and drop the
  interface; move drm_dev_enter() into pvr_power_device_off().
- Destroy the job and free-list ID arrays after pvr_device_fini(), so
  they outlive the IRQ handler when power-off fails.
- Guard the fwif_sysdata access in pvr_power_is_idle() with initialised.
- Add the missing Fixes tag and the K3 reproduction steps.
- Link to v3: https://patch.msgid.link/20261127-img-pm-autosuspend-v3-1-3a65ee22d9b2@linux.spacemit.com

Changes in v3:
- Factor the shared power-off sequence out of the runtime suspend callback.
- On driver removal, disable runtime PM and bypass the firmware idle check.
  Power off the GPU before destroying the job and free-list ID arrays.
- Link to v2: https://patch.msgid.link/20261111-img-pm-autosuspend-v2-1-427689f6f2f9@linux.spacemit.com

Changes in v2:
- Move the pvr_power_is_idle() check into pvr_power_device_suspend()
  and return -EBUSY after pm_runtime_mark_last_busy().
- Drop pvr_power_device_idle() and pass NULL in RUNTIME_PM_OPS().
- Link to v1: https://patch.msgid.link/20261029-img-pm-autosuspend-v1-1-26851840a34e@linux.spacemit.com
---
 drivers/gpu/drm/imagination/pvr_drv.c   | 28 +++++++++++++++++---
 drivers/gpu/drm/imagination/pvr_power.c | 47 +++++++++++++++++++++------------
 drivers/gpu/drm/imagination/pvr_power.h |  2 +-
 3 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c
index 5c965ef0274..36ba30f859b 100644
--- a/drivers/gpu/drm/imagination/pvr_drv.c
+++ b/drivers/gpu/drm/imagination/pvr_drv.c
@@ -24,6 +24,7 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_print.h>
 
 #include <linux/err.h>
 #include <linux/export.h>
@@ -1469,19 +1470,38 @@ static void pvr_remove(struct platform_device *plat_dev)
 	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
 	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
 
+	pm_runtime_disable(drm_dev->dev);
+
 	WARN_ON(!xa_empty(&pvr_dev->job_ids));
 	WARN_ON(!xa_empty(&pvr_dev->free_list_ids));
 
-	xa_destroy(&pvr_dev->job_ids);
-	xa_destroy(&pvr_dev->free_list_ids);
+	/*
+	 * Power the device off before teardown. If this fails the IRQ may remain
+	 * enabled and its handler can still process firmware commands and look
+	 * up free-list IDs, so the ID arrays must outlive pvr_device_fini(),
+	 * which releases the IRQ.
+	 */
+	if (!pm_runtime_status_suspended(drm_dev->dev)) {
+		int err = pvr_power_device_off(pvr_dev);
+
+		if (!err)
+			pm_runtime_set_suspended(drm_dev->dev);
+		else
+			drm_err(drm_dev, "Failed to power the device off (%d)\n", err);
+	}
 
-	pm_runtime_suspend(drm_dev->dev);
 	pvr_device_fini(pvr_dev);
 	drm_dev_unplug(drm_dev);
 	pvr_watchdog_fini(pvr_dev);
 	pvr_queue_device_fini(pvr_dev);
 	pvr_context_device_fini(pvr_dev);
 	pvr_power_domains_fini(pvr_dev);
+
+	/* The IRQ is gone now, so the handlers can no longer look these up. */
+	xa_destroy(&pvr_dev->job_ids);
+	xa_destroy(&pvr_dev->free_list_ids);
+
+	pm_runtime_enable(drm_dev->dev);
 }
 
 static const struct pvr_device_data pvr_device_data_manual = {
@@ -1516,7 +1536,7 @@ static const struct of_device_id dt_match[] = {
 MODULE_DEVICE_TABLE(of, dt_match);
 
 static const struct dev_pm_ops pvr_pm_ops = {
-	RUNTIME_PM_OPS(pvr_power_device_suspend, pvr_power_device_resume, pvr_power_device_idle)
+	RUNTIME_PM_OPS(pvr_power_device_suspend, pvr_power_device_resume, NULL)
 };
 
 static struct platform_driver pvr_driver = {
diff --git a/drivers/gpu/drm/imagination/pvr_power.c b/drivers/gpu/drm/imagination/pvr_power.c
index eb4b6ecdf4f..857e0c5cc0e 100644
--- a/drivers/gpu/drm/imagination/pvr_power.c
+++ b/drivers/gpu/drm/imagination/pvr_power.c
@@ -152,6 +152,10 @@ pvr_power_fw_enable(struct pvr_device *pvr_dev, bool rpm_resume)
 bool
 pvr_power_is_idle(struct pvr_device *pvr_dev)
 {
+	/* Nothing to drain before the firmware is up, or after it is gone. */
+	if (!READ_ONCE(pvr_dev->fw_dev.initialised))
+		return true;
+
 	/*
 	 * FW power state can be out of date if a KCCB command has been submitted but the FW hasn't
 	 * started processing it yet. So also check the KCCB status.
@@ -367,12 +371,10 @@ const struct pvr_power_sequence_ops pvr_power_sequence_ops_pwrseq = {
 };
 
 int
-pvr_power_device_suspend(struct device *dev)
+pvr_power_device_off(struct pvr_device *pvr_dev)
 {
-	struct platform_device *plat_dev = to_platform_device(dev);
-	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
-	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
-	int err = 0;
+	struct drm_device *drm_dev = from_pvr_device(pvr_dev);
+	int err;
 	int idx;
 
 	if (!drm_dev_enter(drm_dev, &idx))
@@ -381,17 +383,38 @@ pvr_power_device_suspend(struct device *dev)
 	if (READ_ONCE(pvr_dev->fw_dev.initialised)) {
 		err = pvr_power_fw_disable(pvr_dev, false, true);
 		if (err)
-			goto err_drm_dev_exit;
+			goto out;
 	}
 
 	err = pvr_dev->device_data->pwr_ops->power_off(pvr_dev);
 
-err_drm_dev_exit:
+out:
 	drm_dev_exit(idx);
 
 	return err;
 }
 
+int
+pvr_power_device_suspend(struct device *dev)
+{
+	struct platform_device *plat_dev = to_platform_device(dev);
+	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
+	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
+
+	if (!pvr_power_is_idle(pvr_dev)) {
+		/*
+		 * The firmware has not drained the GPU yet. Refresh last_busy so
+		 * the PM core reschedules the autosuspend and retries this callback
+		 * until the firmware becomes idle.
+		 */
+		pm_runtime_mark_last_busy(dev);
+
+		return -EBUSY;
+	}
+
+	return pvr_power_device_off(pvr_dev);
+}
+
 int
 pvr_power_device_resume(struct device *dev)
 {
@@ -427,16 +450,6 @@ pvr_power_device_resume(struct device *dev)
 	return err;
 }
 
-int
-pvr_power_device_idle(struct device *dev)
-{
-	struct platform_device *plat_dev = to_platform_device(dev);
-	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
-	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
-
-	return pvr_power_is_idle(pvr_dev) ? 0 : -EBUSY;
-}
-
 static int
 pvr_power_clear_error(struct pvr_device *pvr_dev)
 {
diff --git a/drivers/gpu/drm/imagination/pvr_power.h b/drivers/gpu/drm/imagination/pvr_power.h
index c34252bda07..54c3ebd7fcd 100644
--- a/drivers/gpu/drm/imagination/pvr_power.h
+++ b/drivers/gpu/drm/imagination/pvr_power.h
@@ -16,9 +16,9 @@ void pvr_device_lost(struct pvr_device *pvr_dev);
 
 bool pvr_power_is_idle(struct pvr_device *pvr_dev);
 
+int pvr_power_device_off(struct pvr_device *pvr_dev);
 int pvr_power_device_suspend(struct device *dev);
 int pvr_power_device_resume(struct device *dev);
-int pvr_power_device_idle(struct device *dev);
 
 int pvr_power_reset(struct pvr_device *pvr_dev, bool hard_reset);
 

---
base-commit: 067c5c24cef23d913b67eaaf71ae67e7169a6620
change-id: 20261029-img-pm-autosuspend-7772d83416f7

Best regards,
--  
Baihui Liang <liangbaihui@linux.spacemit.com>


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v4] drm/imagination: re-check firmware idle state before runtime suspend
  2026-12-03 10:15 [PATCH v4] drm/imagination: re-check firmware idle state before runtime suspend Baihui Liang
@ 2026-12-03 10:45 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-03 10:45 UTC (permalink / raw)
  To: Baihui Liang; +Cc: dri-devel

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] drm/imagination: unsynchronized power-off race and deadlock in
  pvr_remove()
- [Medium] drm/imagination: infinite autosuspend retry loop on lost GPU
--

commit 07c0fd1fa75337c7be88e0e0451b0831ac6bfbeb
Author: Baihui Liang <liangbaihui@linux.spacemit.com>

drm/imagination: re-check firmware idle state before runtime suspend

This commit moves the firmware idle check from the runtime_idle callback
into pvr_power_device_suspend(), returning -EBUSY after refreshing
last_busy so the PM core retries autosuspend.  It also extracts
pvr_power_device_off() and calls it directly from pvr_remove() with
runtime PM disabled.

> diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c
> index 5c965ef0274ff..36ba30f859be1 100644
> --- a/drivers/gpu/drm/imagination/pvr_drv.c
> +++ b/drivers/gpu/drm/imagination/pvr_drv.c

[ ... ]

> @@ -1469,19 +1470,38 @@ static void pvr_remove(struct platform_device *plat_dev)
>  	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
>  	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
>  
> +	pm_runtime_disable(drm_dev->dev);
> +
>  	WARN_ON(!xa_empty(&pvr_dev->job_ids));
>  	WARN_ON(!xa_empty(&pvr_dev->free_list_ids));
>  
> -	xa_destroy(&pvr_dev->job_ids);
> -	xa_destroy(&pvr_dev->free_list_ids);
> +	/*
> +	 * Power the device off before teardown. If this fails the IRQ may remain
> +	 * enabled and its handler can still process firmware commands and look
> +	 * up free-list IDs, so the ID arrays must outlive pvr_device_fini(),
> +	 * which releases the IRQ.
> +	 */
> +	if (!pm_runtime_status_suspended(drm_dev->dev)) {
> +		int err = pvr_power_device_off(pvr_dev);
> +
> +		if (!err)
> +			pm_runtime_set_suspended(drm_dev->dev);
> +		else
> +			drm_err(drm_dev, "Failed to power the device off (%d)\n", err);
> +	}
>  
> -	pm_runtime_suspend(drm_dev->dev);
>  	pvr_device_fini(pvr_dev);
>  	drm_dev_unplug(drm_dev);
>  	pvr_watchdog_fini(pvr_dev);
>  	pvr_queue_device_fini(pvr_dev);

[Severity: High]
Can calling pvr_power_device_off() directly in pvr_remove() deadlock with
a concurrent GPU reset or power off the hardware while it is still in use?

Previously, pm_runtime_suspend() refused to power off the device while
usage_count was non-zero.  pm_runtime_disable() only waits for in-flight
PM callbacks, and pvr_remove() now calls pvr_power_device_off() without
holding reset_sem and before freeing the IRQ or unplugging the DRM device.

Because __device_release_driver() resumes the device right before calling
pvr_remove(), IRQs and watchdog.work can be active even with no open DRM
FDs.  If the threaded IRQ handler (on a firmware restart request) enters
pvr_power_reset() and hits err_device_lost, it deadlocks:

CPU0                              CPU1
pvr_remove()                      pvr_device_irq_thread_handler()
  pm_runtime_disable()              pvr_power_reset()
  pvr_power_device_off()              pvr_power_get_clear() -> -EACCES
    drm_dev_enter()                   ...
    pvr_power_fw_disable()            err_device_lost:
      disable_irq()                     pvr_device_lost()
        (waits for IRQ thread)            drm_dev_unplug()
                                            synchronize_srcu() <-- deadlock

Without reset_sem, pvr_remove() can also race with a hard reset in
pvr_power_reset() and run power_off() twice while runtime status stays
RPM_ACTIVE, underflowing clock or pwrseq refcounts, or leave the GPU
powered back on after pm_runtime_set_suspended().

During sysfs unbind with active workloads, pvr_power_device_off() can also
gate clocks and assert reset while pvr_queue_run_job() or an ioctl holds a
runtime PM reference and performs MMIO writes, risking an external abort.

[ ... ]

> diff --git a/drivers/gpu/drm/imagination/pvr_power.c b/drivers/gpu/drm/imagination/pvr_power.c
> index eb4b6ecdf4f4d..857e0c5cc0e2a 100644
> --- a/drivers/gpu/drm/imagination/pvr_power.c
> +++ b/drivers/gpu/drm/imagination/pvr_power.c

[ ... ]

> @@ -367,12 +371,10 @@ const struct pvr_power_sequence_ops pvr_power_sequence_ops_pwrseq = {
>  };
>  
>  int
> -pvr_power_device_suspend(struct device *dev)
> +pvr_power_device_off(struct pvr_device *pvr_dev)
>  {
> -	struct platform_device *plat_dev = to_platform_device(dev);
> -	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
> -	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
> -	int err = 0;
> +	struct drm_device *drm_dev = from_pvr_device(pvr_dev);
> +	int err;
>  	int idx;
>  
>  	if (!drm_dev_enter(drm_dev, &idx))
> @@ -381,17 +383,38 @@ pvr_power_device_suspend(struct device *dev)
>  	if (READ_ONCE(pvr_dev->fw_dev.initialised)) {
>  		err = pvr_power_fw_disable(pvr_dev, false, true);
>  		if (err)
> -			goto err_drm_dev_exit;
> +			goto out;
>  	}
>  
>  	err = pvr_dev->device_data->pwr_ops->power_off(pvr_dev);
>  
> -err_drm_dev_exit:
> +out:
>  	drm_dev_exit(idx);
>  
>  	return err;
>  }
>  
> +int
> +pvr_power_device_suspend(struct device *dev)
> +{
> +	struct platform_device *plat_dev = to_platform_device(dev);
> +	struct drm_device *drm_dev = platform_get_drvdata(plat_dev);
> +	struct pvr_device *pvr_dev = to_pvr_device(drm_dev);
> +
> +	if (!pvr_power_is_idle(pvr_dev)) {
> +		/*
> +		 * The firmware has not drained the GPU yet. Refresh last_busy so
> +		 * the PM core reschedules the autosuspend and retries this callback
> +		 * until the firmware becomes idle.
> +		 */
> +		pm_runtime_mark_last_busy(dev);
> +
> +		return -EBUSY;
> +	}
> +
> +	return pvr_power_device_off(pvr_dev);
> +}

[Severity: Medium]
Does checking pvr_power_is_idle() before drm_dev_enter() put a lost GPU
into an infinite autosuspend retry loop?

pvr_device_lost() unplugs the DRM device without clearing
fw_dev.initialised or disabling runtime PM.  If pvr_power_reset() fails
after pvr_fw_hard_reset() zeroes fwif_sysdata, fw_dev.initialised stays
true while pow_state remains ROGUE_FWIF_POW_OFF (not ROGUE_FWIF_POW_IDLE).

Once usage_count hits zero, pvr_power_is_idle() stays false, so this
refreshes last_busy and returns -EBUSY before drm_dev_enter() runs.
rpm_suspend() leaves runtime_error clear on -EBUSY and re-arms the 50 ms
autosuspend timer, spinning in a permanent 20 Hz wakeup loop.

Checking drm_dev_is_unplugged() (or drm_dev_enter()) before
pvr_power_is_idle() in pvr_power_device_suspend() would return -EIO
immediately as before and stop retries.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203-img-pm-autosuspend-v4-1-dfc0874198e5@linux.spacemit.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03 10:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:15 [PATCH v4] drm/imagination: re-check firmware idle state before runtime suspend Baihui Liang
2026-12-03 10:45 ` sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.