drm/vc4: Add helpers for pm get/put.
authorEric Anholt <eric@anholt.net>
Wed, 20 Feb 2019 21:03:42 +0000 (13:03 -0800)
committerEric Anholt <eric@anholt.net>
Mon, 1 Apr 2019 17:44:34 +0000 (10:44 -0700)
This makes sure the vc4_reset doesn't hit an obscure race with the
GET_PARAM ioctl, fixes a decrement outside of the lock, and prevents
future code from making mistakes with the weird return value of
pm_runtime_get_sync().

Signed-off-by: Eric Anholt <eric@anholt.net>
Link: https://patchwork.freedesktop.org/patch/msgid/20190220210343.28157-6-eric@anholt.net
Reviewed-by: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
drivers/gpu/drm/vc4/vc4_drv.c
drivers/gpu/drm/vc4/vc4_drv.h
drivers/gpu/drm/vc4/vc4_gem.c
drivers/gpu/drm/vc4/vc4_v3d.c

index 4daf44fd454826843e2d3d5f02eb084778a122dd..f2d0fb3aa72988b074dddb9e82b1c6a2c3aec747 100644 (file)
@@ -74,28 +74,25 @@ static int vc4_get_param_ioctl(struct drm_device *dev, void *data,
 
        switch (args->param) {
        case DRM_VC4_PARAM_V3D_IDENT0:
-               ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
-               if (ret < 0)
+               ret = vc4_v3d_pm_get(vc4);
+               if (ret)
                        return ret;
                args->value = V3D_READ(V3D_IDENT0);
-               pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
-               pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
+               vc4_v3d_pm_put(vc4);
                break;
        case DRM_VC4_PARAM_V3D_IDENT1:
-               ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
-               if (ret < 0)
+               ret = vc4_v3d_pm_get(vc4);
+               if (ret)
                        return ret;
                args->value = V3D_READ(V3D_IDENT1);
-               pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
-               pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
+               vc4_v3d_pm_put(vc4);
                break;
        case DRM_VC4_PARAM_V3D_IDENT2:
-               ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
-               if (ret < 0)
+               ret = vc4_v3d_pm_get(vc4);
+               if (ret)
                        return ret;
                args->value = V3D_READ(V3D_IDENT2);
-               pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
-               pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
+               vc4_v3d_pm_put(vc4);
                break;
        case DRM_VC4_PARAM_SUPPORTS_BRANCHES:
        case DRM_VC4_PARAM_SUPPORTS_ETC1:
index 5156ede0f7b7dfd4621c2a5db1c49f604f3fd038..f6c9de75d46563e7c83a31fb0cad11b366df7fda 100644 (file)
@@ -815,6 +815,8 @@ extern struct platform_driver vc4_v3d_driver;
 int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
 int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
 int vc4_v3d_get_bin_slot(struct vc4_dev *vc4);
+int vc4_v3d_pm_get(struct vc4_dev *vc4);
+void vc4_v3d_pm_put(struct vc4_dev *vc4);
 
 /* vc4_validate.c */
 int
index 5ee5bf7fedf7bdd63477a26711fe8e9491a4d61d..5e9496d477bf75ee0d1eb0c5900b04139ebc74b5 100644 (file)
@@ -964,12 +964,7 @@ vc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
        /* Release the reference we had on the perf monitor. */
        vc4_perfmon_put(exec->perfmon);
 
-       mutex_lock(&vc4->power_lock);
-       if (--vc4->power_refcount == 0) {
-               pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
-               pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
-       }
-       mutex_unlock(&vc4->power_lock);
+       vc4_v3d_pm_put(vc4);
 
        kfree(exec);
 }
@@ -1143,17 +1138,11 @@ vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
                return -ENOMEM;
        }
 
-       mutex_lock(&vc4->power_lock);
-       if (vc4->power_refcount++ == 0) {
-               ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
-               if (ret < 0) {
-                       mutex_unlock(&vc4->power_lock);
-                       vc4->power_refcount--;
-                       kfree(exec);
-                       return ret;
-               }
+       ret = vc4_v3d_pm_get(vc4);
+       if (ret) {
+               kfree(exec);
+               return ret;
        }
-       mutex_unlock(&vc4->power_lock);
 
        exec->args = args;
        INIT_LIST_HEAD(&exec->unref_list);
index e1e728e95562a061e2cdddca34127fd8241bcf75..b2b974f20e232e0596eee53ba52e4466a12dc446 100644 (file)
@@ -138,6 +138,39 @@ int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused)
 }
 #endif /* CONFIG_DEBUG_FS */
 
+/**
+ * Wraps pm_runtime_get_sync() in a refcount, so that we can reliably
+ * get the pm_runtime refcount to 0 in vc4_reset().
+ */
+int
+vc4_v3d_pm_get(struct vc4_dev *vc4)
+{
+       mutex_lock(&vc4->power_lock);
+       if (vc4->power_refcount++ == 0) {
+               int ret = pm_runtime_get_sync(&vc4->v3d->pdev->dev);
+
+               if (ret < 0) {
+                       vc4->power_refcount--;
+                       mutex_unlock(&vc4->power_lock);
+                       return ret;
+               }
+       }
+       mutex_unlock(&vc4->power_lock);
+
+       return 0;
+}
+
+void
+vc4_v3d_pm_put(struct vc4_dev *vc4)
+{
+       mutex_lock(&vc4->power_lock);
+       if (--vc4->power_refcount == 0) {
+               pm_runtime_mark_last_busy(&vc4->v3d->pdev->dev);
+               pm_runtime_put_autosuspend(&vc4->v3d->pdev->dev);
+       }
+       mutex_unlock(&vc4->power_lock);
+}
+
 static void vc4_v3d_init_hw(struct drm_device *dev)
 {
        struct vc4_dev *vc4 = to_vc4_dev(dev);