Merge tag 'topic/core-stuff-2014-08-15' of git://anongit.freedesktop.org/drm-intel...
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / drm_modeset_lock.c
index 73e6534fd0aa5cc2c58b62cdb5238cb3ca38f9f1..8749fc06570ebb62540dc4df99393d6c5e7daf57 100644 (file)
 
 
 /**
- * drm_modeset_lock_all - take all modeset locks
- * @dev: drm device
+ * __drm_modeset_lock_all - internal helper to grab all modeset locks
+ * @dev: DRM device
+ * @trylock: trylock mode for atomic contexts
  *
- * This function takes all modeset locks, suitable where a more fine-grained
- * scheme isn't (yet) implemented. Locks must be dropped with
- * drm_modeset_unlock_all.
+ * This is a special version of drm_modeset_lock_all() which can also be used in
+ * atomic contexts. Then @trylock must be set to true.
+ *
+ * Returns:
+ * 0 on success or negative error code on failure.
  */
-void drm_modeset_lock_all(struct drm_device *dev)
+int __drm_modeset_lock_all(struct drm_device *dev,
+                          bool trylock)
 {
        struct drm_mode_config *config = &dev->mode_config;
        struct drm_modeset_acquire_ctx *ctx;
        int ret;
 
-       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
-       if (WARN_ON(!ctx))
-               return;
+       ctx = kzalloc(sizeof(*ctx),
+                     trylock ? GFP_ATOMIC : GFP_KERNEL);
+       if (!ctx)
+               return -ENOMEM;
 
-       mutex_lock(&config->mutex);
+       if (trylock) {
+               if (!mutex_trylock(&config->mutex))
+                       return -EBUSY;
+       } else {
+               mutex_lock(&config->mutex);
+       }
 
        drm_modeset_acquire_init(ctx, 0);
+       ctx->trylock_only = trylock;
 
 retry:
        ret = drm_modeset_lock(&config->connection_mutex, ctx);
@@ -95,13 +106,29 @@ retry:
 
        drm_warn_on_modeset_not_all_locked(dev);
 
-       return;
+       return 0;
 
 fail:
        if (ret == -EDEADLK) {
                drm_modeset_backoff(ctx);
                goto retry;
        }
+
+       return ret;
+}
+EXPORT_SYMBOL(__drm_modeset_lock_all);
+
+/**
+ * drm_modeset_lock_all - take all modeset locks
+ * @dev: drm device
+ *
+ * This function takes all modeset locks, suitable where a more fine-grained
+ * scheme isn't (yet) implemented. Locks must be dropped with
+ * drm_modeset_unlock_all.
+ */
+void drm_modeset_lock_all(struct drm_device *dev)
+{
+       WARN_ON(__drm_modeset_lock_all(dev, false) != 0);
 }
 EXPORT_SYMBOL(drm_modeset_lock_all);
 
@@ -129,6 +156,90 @@ void drm_modeset_unlock_all(struct drm_device *dev)
 }
 EXPORT_SYMBOL(drm_modeset_unlock_all);
 
+/**
+ * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx
+ * @crtc: drm crtc
+ *
+ * This function locks the given crtc using a hidden acquire context. This is
+ * necessary so that drivers internally using the atomic interfaces can grab
+ * further locks with the lock acquire context.
+ */
+void drm_modeset_lock_crtc(struct drm_crtc *crtc)
+{
+       struct drm_modeset_acquire_ctx *ctx;
+       int ret;
+
+       ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+       if (WARN_ON(!ctx))
+               return;
+
+       drm_modeset_acquire_init(ctx, 0);
+
+retry:
+       ret = drm_modeset_lock(&crtc->mutex, ctx);
+       if (ret)
+               goto fail;
+
+       WARN_ON(crtc->acquire_ctx);
+
+       /* now we hold the locks, so now that it is safe, stash the
+        * ctx for drm_modeset_unlock_crtc():
+        */
+       crtc->acquire_ctx = ctx;
+
+       return;
+
+fail:
+       if (ret == -EDEADLK) {
+               drm_modeset_backoff(ctx);
+               goto retry;
+       }
+}
+EXPORT_SYMBOL(drm_modeset_lock_crtc);
+
+/**
+ * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
+ * @crtc: drm crtc
+ *
+ * Legacy ioctl operations like cursor updates or page flips only have per-crtc
+ * locking, and store the acquire ctx in the corresponding crtc. All other
+ * legacy operations take all locks and use a global acquire context. This
+ * function grabs the right one.
+ */
+struct drm_modeset_acquire_ctx *
+drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
+{
+       if (crtc->acquire_ctx)
+               return crtc->acquire_ctx;
+
+       WARN_ON(!crtc->dev->mode_config.acquire_ctx);
+
+       return crtc->dev->mode_config.acquire_ctx;
+}
+EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
+
+/**
+ * drm_modeset_unlock_crtc - drop crtc lock
+ * @crtc: drm crtc
+ *
+ * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
+ * locks acquired through the hidden context.
+ */
+void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
+{
+       struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
+
+       if (WARN_ON(!ctx))
+               return;
+
+       crtc->acquire_ctx = NULL;
+       drm_modeset_drop_locks(ctx);
+       drm_modeset_acquire_fini(ctx);
+
+       kfree(ctx);
+}
+EXPORT_SYMBOL(drm_modeset_unlock_crtc);
+
 /**
  * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  * @dev: device
@@ -203,7 +314,12 @@ static inline int modeset_lock(struct drm_modeset_lock *lock,
 
        WARN_ON(ctx->contended);
 
-       if (interruptible && slow) {
+       if (ctx->trylock_only) {
+               if (!ww_mutex_trylock(&lock->mutex))
+                       return -EBUSY;
+               else
+                       return 0;
+       } else if (interruptible && slow) {
                ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
        } else if (interruptible) {
                ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);