From: Christian Gmeiner <[email protected]>

pm_runtime_suspended(..) only looks at the runtime PM status and takes
no reference. It returns false while a suspend or resume is in flight,
and nothing stops a suspend from starting right after the check. So the
cooling device can write the clock while the GPU turns its clocks off.

Check gpu->state under gpu->lock instead. The state is RESET or higher
only after the runtime resume or the init reset has programmed the
clock. A GPU that is suspended or not reset yet only stores the new
scale, and the next resume or the init reset applies it.

Take gpu->lock in every place that sets the state below RESET:
etnaviv_gpu_rpm_suspend(..), etnaviv_gpu_unbind(..) and the failure
path of etnaviv_gpu_init(..). etnaviv_gpu_init(..) also keeps
the lock from its state change until the init reset is done. Otherwise
a cooling request during the reset is not applied until the next
resume.

Fixes: bcdfb5e56dc5 ("drm/etnaviv: add etnaviv cooling device")
Cc: [email protected] # 6.6+
Signed-off-by: Christian Gmeiner <[email protected]>
---
 drivers/gpu/drm/etnaviv/etnaviv_gpu.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c 
b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
index 0a28e21feb8b..c819f5fc4418 100644
--- a/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
+++ b/drivers/gpu/drm/etnaviv/etnaviv_gpu.c
@@ -886,9 +886,12 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
            (gpu->identity.minor_features10 & chipMinorFeatures10_SECURITY_AHB))
                gpu->sec_mode = ETNA_SEC_KERNEL;
 
+       /* the cooling device may change the clock from here on */
+       mutex_lock(&gpu->lock);
        gpu->state = ETNA_GPU_STATE_IDENTIFIED;
-
        ret = etnaviv_hw_reset(gpu);
+       mutex_unlock(&gpu->lock);
+
        if (ret) {
                dev_err(gpu->dev, "GPU reset failed\n");
                goto fail;
@@ -952,7 +955,9 @@ int etnaviv_gpu_init(struct etnaviv_gpu *gpu)
 
 fail:
        /* the GPU is not used, a resume must not set it up */
+       mutex_lock(&gpu->lock);
        gpu->state = ETNA_GPU_STATE_UNKNOWN;
+       mutex_unlock(&gpu->lock);
        pm_runtime_mark_last_busy(gpu->dev);
 pm_put:
        pm_runtime_put_autosuspend(gpu->dev);
@@ -1792,7 +1797,8 @@ etnaviv_gpu_cooling_set_cur_state(struct 
thermal_cooling_device *cdev,
        }
 
        gpu->freq_scale = state;
-       if (!pm_runtime_suspended(gpu->dev))
+       /* a suspended or not yet reset GPU picks up freq_scale later */
+       if (gpu->state >= ETNA_GPU_STATE_RESET)
                etnaviv_gpu_update_clock(gpu);
        mutex_unlock(&gpu->lock);
 
@@ -1888,7 +1894,9 @@ static void etnaviv_gpu_unbind(struct device *dev, struct 
device *master,
        }
 
        /* the GPU is not initialized any more, a resume must not set it up */
+       mutex_lock(&gpu->lock);
        gpu->state = ETNA_GPU_STATE_UNKNOWN;
+       mutex_unlock(&gpu->lock);
 
        if (gpu->mmu_context)
                etnaviv_iommu_context_put(gpu->mmu_context);
@@ -2031,8 +2039,10 @@ static int etnaviv_gpu_rpm_suspend(struct device *dev)
 
        etnaviv_gpu_hw_suspend(gpu);
 
+       mutex_lock(&gpu->lock);
        if (gpu->state != ETNA_GPU_STATE_UNKNOWN)
                gpu->state = ETNA_GPU_STATE_IDENTIFIED;
+       mutex_unlock(&gpu->lock);
 
        return etnaviv_gpu_clk_disable(gpu);
 }

-- 
2.56.0

Reply via email to