Overload the already existing pm_runtime_active_auto_try_enabled guard
with our custom guard that force the state to suspened (and thus clear
the runtime_error) in case the resume fails.

Once done, we can replace the existing places where manual
panthor_device_resume_and_get()+pm_runtime_put_autosuspend() were
done by guards.

Signed-off-by: Boris Brezillon <[email protected]>
---
 drivers/gpu/drm/panthor/panthor_device.h | 10 +++++++---
 drivers/gpu/drm/panthor/panthor_drv.c    |  4 ++--
 drivers/gpu/drm/panthor/panthor_sched.c  | 11 ++++-------
 3 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/gpu/drm/panthor/panthor_device.h 
b/drivers/gpu/drm/panthor/panthor_device.h
index 1aaf06df875b..51527d2e2f77 100644
--- a/drivers/gpu/drm/panthor/panthor_device.h
+++ b/drivers/gpu/drm/panthor/panthor_device.h
@@ -407,9 +407,10 @@ int panthor_device_mmap_io(struct panthor_device *ptdev,
 int panthor_device_resume(struct device *dev);
 int panthor_device_suspend(struct device *dev);
 
-static inline int panthor_device_resume_and_get(struct panthor_device *ptdev)
+static inline int panthor_device_resume_and_get(struct device *dev)
 {
-       int ret = pm_runtime_resume_and_get(ptdev->base.dev);
+       struct panthor_device *ptdev = dev_get_drvdata(dev);
+       int ret = pm_runtime_resume_and_get(dev);
 
        /* If the resume failed, we need to clear the runtime_error, which
         * can done by forcing the RPM state to suspended. If multiple
@@ -424,11 +425,14 @@ static inline int panthor_device_resume_and_get(struct 
panthor_device *ptdev)
         * something we can live with.
         */
        if (ret && atomic_cmpxchg(&ptdev->pm.recovery_needed, 1, 0) == 1)
-               pm_runtime_set_suspended(ptdev->base.dev);
+               pm_runtime_set_suspended(dev);
 
        return ret;
 }
 
+DEFINE_GUARD_COND(pm_runtime_active_auto, _try_enabled_or_suspend,
+                 panthor_device_resume_and_get(_T), _RET == 0)
+
 enum drm_panthor_exception_type {
        DRM_PANTHOR_EXCEPTION_OK = 0x00,
        DRM_PANTHOR_EXCEPTION_TERMINATED = 0x04,
diff --git a/drivers/gpu/drm/panthor/panthor_drv.c 
b/drivers/gpu/drm/panthor/panthor_drv.c
index 789ddc0ff7ef..f2d60ff00896 100644
--- a/drivers/gpu/drm/panthor/panthor_drv.c
+++ b/drivers/gpu/drm/panthor/panthor_drv.c
@@ -824,7 +824,8 @@ static int panthor_query_timestamp_info(struct 
panthor_device *ptdev,
                (flags & DRM_PANTHOR_TIMESTAMP_DURATION) ||
                (timestamp_types >= 2);
 
-       ret = panthor_device_resume_and_get(ptdev);
+       ACQUIRE(pm_runtime_active_auto_try_enabled_or_suspend, 
pm_guard)(ptdev->base.dev);
+       ret = ACQUIRE_ERR(pm_runtime_active_auto_try_enabled_or_suspend, 
&pm_guard);
        if (ret)
                return ret;
 
@@ -894,7 +895,6 @@ static int panthor_query_timestamp_info(struct 
panthor_device *ptdev,
                arg->cpu_timestamp_nsec = 0;
        }
 
-       pm_runtime_put(ptdev->base.dev);
        return 0;
 }
 
diff --git a/drivers/gpu/drm/panthor/panthor_sched.c 
b/drivers/gpu/drm/panthor/panthor_sched.c
index 9aa9941d2309..9afa38e87fc9 100644
--- a/drivers/gpu/drm/panthor/panthor_sched.c
+++ b/drivers/gpu/drm/panthor/panthor_sched.c
@@ -2630,13 +2630,12 @@ static void tick_work(struct work_struct *work)
        if (ACQUIRE_ERR(drm_dev_access, &dev_guard))
                return;
 
-       ret = panthor_device_resume_and_get(ptdev);
+       ACQUIRE(pm_runtime_active_auto_try_enabled_or_suspend, 
pm_guard)(ptdev->base.dev);
+       ret = ACQUIRE_ERR(pm_runtime_active_auto_try_enabled_or_suspend, 
&pm_guard);
        if (drm_WARN_ON(&ptdev->base, ret))
                return;
 
        tick(sched);
-       pm_runtime_mark_last_busy(ptdev->base.dev);
-       pm_runtime_put_autosuspend(ptdev->base.dev);
 }
 
 static int panthor_queue_eval_syncwait(struct panthor_group *group, u8 
queue_idx)
@@ -3359,7 +3358,8 @@ queue_run_job(struct drm_sched_job *sched_job)
                return dma_fence_get(job->done_fence);
        }
 
-       ret = panthor_device_resume_and_get(ptdev);
+       ACQUIRE(pm_runtime_active_auto_try_enabled_or_suspend, 
pm_guard)(ptdev->base.dev);
+       ret = ACQUIRE_ERR(pm_runtime_active_auto_try_enabled_or_suspend, 
&pm_guard);
        if (drm_WARN_ON(&ptdev->base, ret))
                return ERR_PTR(ret);
 
@@ -3367,9 +3367,6 @@ queue_run_job(struct drm_sched_job *sched_job)
                done_fence = queue_run_job_locked(job);
        }
 
-       pm_runtime_mark_last_busy(ptdev->base.dev);
-       pm_runtime_put_autosuspend(ptdev->base.dev);
-
        return done_fence;
 }
 

-- 
2.54.0

Reply via email to