]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/commitdiff
drm/ttm, drm/vmwgfx: Move the lock- and object functionality to the vmwgfx driver
authorThomas Hellstrom <thellstrom@vmware.com>
Wed, 26 Sep 2018 18:15:36 +0000 (20:15 +0200)
committerThomas Hellstrom <thellstrom@vmware.com>
Thu, 27 Sep 2018 13:19:20 +0000 (15:19 +0200)
No other driver is using this functionality so move it out of TTM and
into the vmwgfx driver. Update includes and remove exports.
Also annotate to remove false static analyzer lock balance warnings.

Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
14 files changed:
drivers/gpu/drm/ttm/Makefile
drivers/gpu/drm/ttm/ttm_lock.c [deleted file]
drivers/gpu/drm/ttm/ttm_object.c [deleted file]
drivers/gpu/drm/vmwgfx/Makefile
drivers/gpu/drm/vmwgfx/ttm_lock.c [new file with mode: 0644]
drivers/gpu/drm/vmwgfx/ttm_lock.h [new file with mode: 0644]
drivers/gpu/drm/vmwgfx/ttm_object.c [new file with mode: 0644]
drivers/gpu/drm/vmwgfx/ttm_object.h [new file with mode: 0644]
drivers/gpu/drm/vmwgfx/vmwgfx_bo.c
drivers/gpu/drm/vmwgfx/vmwgfx_drv.c
drivers/gpu/drm/vmwgfx/vmwgfx_drv.h
drivers/gpu/drm/vmwgfx/vmwgfx_prime.c
include/drm/ttm/ttm_lock.h [deleted file]
include/drm/ttm/ttm_object.h [deleted file]

index a60e560804e0ad3c64c6cf2b632a8c5085b59fde..01fc670ce7a21978881c2603034c84142d943bea 100644 (file)
@@ -4,8 +4,8 @@
 
 ttm-y := ttm_memory.o ttm_tt.o ttm_bo.o \
        ttm_bo_util.o ttm_bo_vm.o ttm_module.o \
-       ttm_object.o ttm_lock.o ttm_execbuf_util.o ttm_page_alloc.o \
-       ttm_bo_manager.o ttm_page_alloc_dma.o
+       ttm_execbuf_util.o ttm_page_alloc.o ttm_bo_manager.o \
+       ttm_page_alloc_dma.o
 ttm-$(CONFIG_AGP) += ttm_agp_backend.o
 
 obj-$(CONFIG_DRM_TTM) += ttm.o
diff --git a/drivers/gpu/drm/ttm/ttm_lock.c b/drivers/gpu/drm/ttm/ttm_lock.c
deleted file mode 100644 (file)
index 20694b8..0000000
+++ /dev/null
@@ -1,303 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 OR MIT */
-/**************************************************************************
- *
- * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- **************************************************************************/
-/*
- * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
- */
-
-#include <drm/ttm/ttm_lock.h>
-#include <drm/ttm/ttm_module.h>
-#include <linux/atomic.h>
-#include <linux/errno.h>
-#include <linux/wait.h>
-#include <linux/sched/signal.h>
-#include <linux/module.h>
-
-#define TTM_WRITE_LOCK_PENDING    (1 << 0)
-#define TTM_VT_LOCK_PENDING       (1 << 1)
-#define TTM_SUSPEND_LOCK_PENDING  (1 << 2)
-#define TTM_VT_LOCK               (1 << 3)
-#define TTM_SUSPEND_LOCK          (1 << 4)
-
-void ttm_lock_init(struct ttm_lock *lock)
-{
-       spin_lock_init(&lock->lock);
-       init_waitqueue_head(&lock->queue);
-       lock->rw = 0;
-       lock->flags = 0;
-       lock->kill_takers = false;
-       lock->signal = SIGKILL;
-}
-EXPORT_SYMBOL(ttm_lock_init);
-
-void ttm_read_unlock(struct ttm_lock *lock)
-{
-       spin_lock(&lock->lock);
-       if (--lock->rw == 0)
-               wake_up_all(&lock->queue);
-       spin_unlock(&lock->lock);
-}
-EXPORT_SYMBOL(ttm_read_unlock);
-
-static bool __ttm_read_lock(struct ttm_lock *lock)
-{
-       bool locked = false;
-
-       spin_lock(&lock->lock);
-       if (unlikely(lock->kill_takers)) {
-               send_sig(lock->signal, current, 0);
-               spin_unlock(&lock->lock);
-               return false;
-       }
-       if (lock->rw >= 0 && lock->flags == 0) {
-               ++lock->rw;
-               locked = true;
-       }
-       spin_unlock(&lock->lock);
-       return locked;
-}
-
-int ttm_read_lock(struct ttm_lock *lock, bool interruptible)
-{
-       int ret = 0;
-
-       if (interruptible)
-               ret = wait_event_interruptible(lock->queue,
-                                              __ttm_read_lock(lock));
-       else
-               wait_event(lock->queue, __ttm_read_lock(lock));
-       return ret;
-}
-EXPORT_SYMBOL(ttm_read_lock);
-
-static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
-{
-       bool block = true;
-
-       *locked = false;
-
-       spin_lock(&lock->lock);
-       if (unlikely(lock->kill_takers)) {
-               send_sig(lock->signal, current, 0);
-               spin_unlock(&lock->lock);
-               return false;
-       }
-       if (lock->rw >= 0 && lock->flags == 0) {
-               ++lock->rw;
-               block = false;
-               *locked = true;
-       } else if (lock->flags == 0) {
-               block = false;
-       }
-       spin_unlock(&lock->lock);
-
-       return !block;
-}
-
-int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
-{
-       int ret = 0;
-       bool locked;
-
-       if (interruptible)
-               ret = wait_event_interruptible
-                       (lock->queue, __ttm_read_trylock(lock, &locked));
-       else
-               wait_event(lock->queue, __ttm_read_trylock(lock, &locked));
-
-       if (unlikely(ret != 0)) {
-               BUG_ON(locked);
-               return ret;
-       }
-
-       return (locked) ? 0 : -EBUSY;
-}
-
-void ttm_write_unlock(struct ttm_lock *lock)
-{
-       spin_lock(&lock->lock);
-       lock->rw = 0;
-       wake_up_all(&lock->queue);
-       spin_unlock(&lock->lock);
-}
-EXPORT_SYMBOL(ttm_write_unlock);
-
-static bool __ttm_write_lock(struct ttm_lock *lock)
-{
-       bool locked = false;
-
-       spin_lock(&lock->lock);
-       if (unlikely(lock->kill_takers)) {
-               send_sig(lock->signal, current, 0);
-               spin_unlock(&lock->lock);
-               return false;
-       }
-       if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
-               lock->rw = -1;
-               lock->flags &= ~TTM_WRITE_LOCK_PENDING;
-               locked = true;
-       } else {
-               lock->flags |= TTM_WRITE_LOCK_PENDING;
-       }
-       spin_unlock(&lock->lock);
-       return locked;
-}
-
-int ttm_write_lock(struct ttm_lock *lock, bool interruptible)
-{
-       int ret = 0;
-
-       if (interruptible) {
-               ret = wait_event_interruptible(lock->queue,
-                                              __ttm_write_lock(lock));
-               if (unlikely(ret != 0)) {
-                       spin_lock(&lock->lock);
-                       lock->flags &= ~TTM_WRITE_LOCK_PENDING;
-                       wake_up_all(&lock->queue);
-                       spin_unlock(&lock->lock);
-               }
-       } else
-               wait_event(lock->queue, __ttm_write_lock(lock));
-
-       return ret;
-}
-EXPORT_SYMBOL(ttm_write_lock);
-
-static int __ttm_vt_unlock(struct ttm_lock *lock)
-{
-       int ret = 0;
-
-       spin_lock(&lock->lock);
-       if (unlikely(!(lock->flags & TTM_VT_LOCK)))
-               ret = -EINVAL;
-       lock->flags &= ~TTM_VT_LOCK;
-       wake_up_all(&lock->queue);
-       spin_unlock(&lock->lock);
-
-       return ret;
-}
-
-static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
-{
-       struct ttm_base_object *base = *p_base;
-       struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
-       int ret;
-
-       *p_base = NULL;
-       ret = __ttm_vt_unlock(lock);
-       BUG_ON(ret != 0);
-}
-
-static bool __ttm_vt_lock(struct ttm_lock *lock)
-{
-       bool locked = false;
-
-       spin_lock(&lock->lock);
-       if (lock->rw == 0) {
-               lock->flags &= ~TTM_VT_LOCK_PENDING;
-               lock->flags |= TTM_VT_LOCK;
-               locked = true;
-       } else {
-               lock->flags |= TTM_VT_LOCK_PENDING;
-       }
-       spin_unlock(&lock->lock);
-       return locked;
-}
-
-int ttm_vt_lock(struct ttm_lock *lock,
-               bool interruptible,
-               struct ttm_object_file *tfile)
-{
-       int ret = 0;
-
-       if (interruptible) {
-               ret = wait_event_interruptible(lock->queue,
-                                              __ttm_vt_lock(lock));
-               if (unlikely(ret != 0)) {
-                       spin_lock(&lock->lock);
-                       lock->flags &= ~TTM_VT_LOCK_PENDING;
-                       wake_up_all(&lock->queue);
-                       spin_unlock(&lock->lock);
-                       return ret;
-               }
-       } else
-               wait_event(lock->queue, __ttm_vt_lock(lock));
-
-       /*
-        * Add a base-object, the destructor of which will
-        * make sure the lock is released if the client dies
-        * while holding it.
-        */
-
-       ret = ttm_base_object_init(tfile, &lock->base, false,
-                                  ttm_lock_type, &ttm_vt_lock_remove, NULL);
-       if (ret)
-               (void)__ttm_vt_unlock(lock);
-       else
-               lock->vt_holder = tfile;
-
-       return ret;
-}
-EXPORT_SYMBOL(ttm_vt_lock);
-
-int ttm_vt_unlock(struct ttm_lock *lock)
-{
-       return ttm_ref_object_base_unref(lock->vt_holder,
-                                        lock->base.hash.key, TTM_REF_USAGE);
-}
-EXPORT_SYMBOL(ttm_vt_unlock);
-
-void ttm_suspend_unlock(struct ttm_lock *lock)
-{
-       spin_lock(&lock->lock);
-       lock->flags &= ~TTM_SUSPEND_LOCK;
-       wake_up_all(&lock->queue);
-       spin_unlock(&lock->lock);
-}
-EXPORT_SYMBOL(ttm_suspend_unlock);
-
-static bool __ttm_suspend_lock(struct ttm_lock *lock)
-{
-       bool locked = false;
-
-       spin_lock(&lock->lock);
-       if (lock->rw == 0) {
-               lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
-               lock->flags |= TTM_SUSPEND_LOCK;
-               locked = true;
-       } else {
-               lock->flags |= TTM_SUSPEND_LOCK_PENDING;
-       }
-       spin_unlock(&lock->lock);
-       return locked;
-}
-
-void ttm_suspend_lock(struct ttm_lock *lock)
-{
-       wait_event(lock->queue, __ttm_suspend_lock(lock));
-}
-EXPORT_SYMBOL(ttm_suspend_lock);
diff --git a/drivers/gpu/drm/ttm/ttm_object.c b/drivers/gpu/drm/ttm/ttm_object.c
deleted file mode 100644 (file)
index 74f1b1e..0000000
+++ /dev/null
@@ -1,775 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 OR MIT */
-/**************************************************************************
- *
- * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- **************************************************************************/
-/*
- * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
- *
- * While no substantial code is shared, the prime code is inspired by
- * drm_prime.c, with
- * Authors:
- *      Dave Airlie <airlied@redhat.com>
- *      Rob Clark <rob.clark@linaro.org>
- */
-/** @file ttm_ref_object.c
- *
- * Base- and reference object implementation for the various
- * ttm objects. Implements reference counting, minimal security checks
- * and release on file close.
- */
-
-
-/**
- * struct ttm_object_file
- *
- * @tdev: Pointer to the ttm_object_device.
- *
- * @lock: Lock that protects the ref_list list and the
- * ref_hash hash tables.
- *
- * @ref_list: List of ttm_ref_objects to be destroyed at
- * file release.
- *
- * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
- * for fast lookup of ref objects given a base object.
- */
-
-#define pr_fmt(fmt) "[TTM] " fmt
-
-#include <drm/ttm/ttm_object.h>
-#include <drm/ttm/ttm_module.h>
-#include <linux/list.h>
-#include <linux/spinlock.h>
-#include <linux/slab.h>
-#include <linux/module.h>
-#include <linux/atomic.h>
-
-struct ttm_object_file {
-       struct ttm_object_device *tdev;
-       spinlock_t lock;
-       struct list_head ref_list;
-       struct drm_open_hash ref_hash[TTM_REF_NUM];
-       struct kref refcount;
-};
-
-/**
- * struct ttm_object_device
- *
- * @object_lock: lock that protects the object_hash hash table.
- *
- * @object_hash: hash table for fast lookup of object global names.
- *
- * @object_count: Per device object count.
- *
- * This is the per-device data structure needed for ttm object management.
- */
-
-struct ttm_object_device {
-       spinlock_t object_lock;
-       struct drm_open_hash object_hash;
-       atomic_t object_count;
-       struct ttm_mem_global *mem_glob;
-       struct dma_buf_ops ops;
-       void (*dmabuf_release)(struct dma_buf *dma_buf);
-       size_t dma_buf_size;
-};
-
-/**
- * struct ttm_ref_object
- *
- * @hash: Hash entry for the per-file object reference hash.
- *
- * @head: List entry for the per-file list of ref-objects.
- *
- * @kref: Ref count.
- *
- * @obj: Base object this ref object is referencing.
- *
- * @ref_type: Type of ref object.
- *
- * This is similar to an idr object, but it also has a hash table entry
- * that allows lookup with a pointer to the referenced object as a key. In
- * that way, one can easily detect whether a base object is referenced by
- * a particular ttm_object_file. It also carries a ref count to avoid creating
- * multiple ref objects if a ttm_object_file references the same base
- * object more than once.
- */
-
-struct ttm_ref_object {
-       struct rcu_head rcu_head;
-       struct drm_hash_item hash;
-       struct list_head head;
-       struct kref kref;
-       enum ttm_ref_type ref_type;
-       struct ttm_base_object *obj;
-       struct ttm_object_file *tfile;
-};
-
-static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
-
-static inline struct ttm_object_file *
-ttm_object_file_ref(struct ttm_object_file *tfile)
-{
-       kref_get(&tfile->refcount);
-       return tfile;
-}
-
-static void ttm_object_file_destroy(struct kref *kref)
-{
-       struct ttm_object_file *tfile =
-               container_of(kref, struct ttm_object_file, refcount);
-
-       kfree(tfile);
-}
-
-
-static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
-{
-       struct ttm_object_file *tfile = *p_tfile;
-
-       *p_tfile = NULL;
-       kref_put(&tfile->refcount, ttm_object_file_destroy);
-}
-
-
-int ttm_base_object_init(struct ttm_object_file *tfile,
-                        struct ttm_base_object *base,
-                        bool shareable,
-                        enum ttm_object_type object_type,
-                        void (*refcount_release) (struct ttm_base_object **),
-                        void (*ref_obj_release) (struct ttm_base_object *,
-                                                 enum ttm_ref_type ref_type))
-{
-       struct ttm_object_device *tdev = tfile->tdev;
-       int ret;
-
-       base->shareable = shareable;
-       base->tfile = ttm_object_file_ref(tfile);
-       base->refcount_release = refcount_release;
-       base->ref_obj_release = ref_obj_release;
-       base->object_type = object_type;
-       kref_init(&base->refcount);
-       spin_lock(&tdev->object_lock);
-       ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
-                                           &base->hash,
-                                           (unsigned long)base, 31, 0, 0);
-       spin_unlock(&tdev->object_lock);
-       if (unlikely(ret != 0))
-               goto out_err0;
-
-       ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
-       if (unlikely(ret != 0))
-               goto out_err1;
-
-       ttm_base_object_unref(&base);
-
-       return 0;
-out_err1:
-       spin_lock(&tdev->object_lock);
-       (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
-       spin_unlock(&tdev->object_lock);
-out_err0:
-       return ret;
-}
-EXPORT_SYMBOL(ttm_base_object_init);
-
-static void ttm_release_base(struct kref *kref)
-{
-       struct ttm_base_object *base =
-           container_of(kref, struct ttm_base_object, refcount);
-       struct ttm_object_device *tdev = base->tfile->tdev;
-
-       spin_lock(&tdev->object_lock);
-       (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
-       spin_unlock(&tdev->object_lock);
-
-       /*
-        * Note: We don't use synchronize_rcu() here because it's far
-        * too slow. It's up to the user to free the object using
-        * call_rcu() or ttm_base_object_kfree().
-        */
-
-       ttm_object_file_unref(&base->tfile);
-       if (base->refcount_release)
-               base->refcount_release(&base);
-}
-
-void ttm_base_object_unref(struct ttm_base_object **p_base)
-{
-       struct ttm_base_object *base = *p_base;
-
-       *p_base = NULL;
-
-       kref_put(&base->refcount, ttm_release_base);
-}
-EXPORT_SYMBOL(ttm_base_object_unref);
-
-struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
-                                              uint32_t key)
-{
-       struct ttm_base_object *base = NULL;
-       struct drm_hash_item *hash;
-       struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
-       int ret;
-
-       rcu_read_lock();
-       ret = drm_ht_find_item_rcu(ht, key, &hash);
-
-       if (likely(ret == 0)) {
-               base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
-               if (!kref_get_unless_zero(&base->refcount))
-                       base = NULL;
-       }
-       rcu_read_unlock();
-
-       return base;
-}
-EXPORT_SYMBOL(ttm_base_object_lookup);
-
-struct ttm_base_object *
-ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
-{
-       struct ttm_base_object *base = NULL;
-       struct drm_hash_item *hash;
-       struct drm_open_hash *ht = &tdev->object_hash;
-       int ret;
-
-       rcu_read_lock();
-       ret = drm_ht_find_item_rcu(ht, key, &hash);
-
-       if (likely(ret == 0)) {
-               base = drm_hash_entry(hash, struct ttm_base_object, hash);
-               if (!kref_get_unless_zero(&base->refcount))
-                       base = NULL;
-       }
-       rcu_read_unlock();
-
-       return base;
-}
-EXPORT_SYMBOL(ttm_base_object_lookup_for_ref);
-
-/**
- * ttm_ref_object_exists - Check whether a caller has a valid ref object
- * (has opened) a base object.
- *
- * @tfile: Pointer to a struct ttm_object_file identifying the caller.
- * @base: Pointer to a struct base object.
- *
- * Checks wether the caller identified by @tfile has put a valid USAGE
- * reference object on the base object identified by @base.
- */
-bool ttm_ref_object_exists(struct ttm_object_file *tfile,
-                          struct ttm_base_object *base)
-{
-       struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
-       struct drm_hash_item *hash;
-       struct ttm_ref_object *ref;
-
-       rcu_read_lock();
-       if (unlikely(drm_ht_find_item_rcu(ht, base->hash.key, &hash) != 0))
-               goto out_false;
-
-       /*
-        * Verify that the ref object is really pointing to our base object.
-        * Our base object could actually be dead, and the ref object pointing
-        * to another base object with the same handle.
-        */
-       ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
-       if (unlikely(base != ref->obj))
-               goto out_false;
-
-       /*
-        * Verify that the ref->obj pointer was actually valid!
-        */
-       rmb();
-       if (unlikely(kref_read(&ref->kref) == 0))
-               goto out_false;
-
-       rcu_read_unlock();
-       return true;
-
- out_false:
-       rcu_read_unlock();
-       return false;
-}
-EXPORT_SYMBOL(ttm_ref_object_exists);
-
-int ttm_ref_object_add(struct ttm_object_file *tfile,
-                      struct ttm_base_object *base,
-                      enum ttm_ref_type ref_type, bool *existed,
-                      bool require_existed)
-{
-       struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
-       struct ttm_ref_object *ref;
-       struct drm_hash_item *hash;
-       struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
-       struct ttm_operation_ctx ctx = {
-               .interruptible = false,
-               .no_wait_gpu = false
-       };
-       int ret = -EINVAL;
-
-       if (base->tfile != tfile && !base->shareable)
-               return -EPERM;
-
-       if (existed != NULL)
-               *existed = true;
-
-       while (ret == -EINVAL) {
-               rcu_read_lock();
-               ret = drm_ht_find_item_rcu(ht, base->hash.key, &hash);
-
-               if (ret == 0) {
-                       ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
-                       if (kref_get_unless_zero(&ref->kref)) {
-                               rcu_read_unlock();
-                               break;
-                       }
-               }
-
-               rcu_read_unlock();
-               if (require_existed)
-                       return -EPERM;
-
-               ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
-                                          &ctx);
-               if (unlikely(ret != 0))
-                       return ret;
-               ref = kmalloc(sizeof(*ref), GFP_KERNEL);
-               if (unlikely(ref == NULL)) {
-                       ttm_mem_global_free(mem_glob, sizeof(*ref));
-                       return -ENOMEM;
-               }
-
-               ref->hash.key = base->hash.key;
-               ref->obj = base;
-               ref->tfile = tfile;
-               ref->ref_type = ref_type;
-               kref_init(&ref->kref);
-
-               spin_lock(&tfile->lock);
-               ret = drm_ht_insert_item_rcu(ht, &ref->hash);
-
-               if (likely(ret == 0)) {
-                       list_add_tail(&ref->head, &tfile->ref_list);
-                       kref_get(&base->refcount);
-                       spin_unlock(&tfile->lock);
-                       if (existed != NULL)
-                               *existed = false;
-                       break;
-               }
-
-               spin_unlock(&tfile->lock);
-               BUG_ON(ret != -EINVAL);
-
-               ttm_mem_global_free(mem_glob, sizeof(*ref));
-               kfree(ref);
-       }
-
-       return ret;
-}
-EXPORT_SYMBOL(ttm_ref_object_add);
-
-static void ttm_ref_object_release(struct kref *kref)
-{
-       struct ttm_ref_object *ref =
-           container_of(kref, struct ttm_ref_object, kref);
-       struct ttm_base_object *base = ref->obj;
-       struct ttm_object_file *tfile = ref->tfile;
-       struct drm_open_hash *ht;
-       struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
-
-       ht = &tfile->ref_hash[ref->ref_type];
-       (void)drm_ht_remove_item_rcu(ht, &ref->hash);
-       list_del(&ref->head);
-       spin_unlock(&tfile->lock);
-
-       if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
-               base->ref_obj_release(base, ref->ref_type);
-
-       ttm_base_object_unref(&ref->obj);
-       ttm_mem_global_free(mem_glob, sizeof(*ref));
-       kfree_rcu(ref, rcu_head);
-       spin_lock(&tfile->lock);
-}
-
-int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
-                             unsigned long key, enum ttm_ref_type ref_type)
-{
-       struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
-       struct ttm_ref_object *ref;
-       struct drm_hash_item *hash;
-       int ret;
-
-       spin_lock(&tfile->lock);
-       ret = drm_ht_find_item(ht, key, &hash);
-       if (unlikely(ret != 0)) {
-               spin_unlock(&tfile->lock);
-               return -EINVAL;
-       }
-       ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
-       kref_put(&ref->kref, ttm_ref_object_release);
-       spin_unlock(&tfile->lock);
-       return 0;
-}
-EXPORT_SYMBOL(ttm_ref_object_base_unref);
-
-void ttm_object_file_release(struct ttm_object_file **p_tfile)
-{
-       struct ttm_ref_object *ref;
-       struct list_head *list;
-       unsigned int i;
-       struct ttm_object_file *tfile = *p_tfile;
-
-       *p_tfile = NULL;
-       spin_lock(&tfile->lock);
-
-       /*
-        * Since we release the lock within the loop, we have to
-        * restart it from the beginning each time.
-        */
-
-       while (!list_empty(&tfile->ref_list)) {
-               list = tfile->ref_list.next;
-               ref = list_entry(list, struct ttm_ref_object, head);
-               ttm_ref_object_release(&ref->kref);
-       }
-
-       spin_unlock(&tfile->lock);
-       for (i = 0; i < TTM_REF_NUM; ++i)
-               drm_ht_remove(&tfile->ref_hash[i]);
-
-       ttm_object_file_unref(&tfile);
-}
-EXPORT_SYMBOL(ttm_object_file_release);
-
-struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
-                                            unsigned int hash_order)
-{
-       struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
-       unsigned int i;
-       unsigned int j = 0;
-       int ret;
-
-       if (unlikely(tfile == NULL))
-               return NULL;
-
-       spin_lock_init(&tfile->lock);
-       tfile->tdev = tdev;
-       kref_init(&tfile->refcount);
-       INIT_LIST_HEAD(&tfile->ref_list);
-
-       for (i = 0; i < TTM_REF_NUM; ++i) {
-               ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
-               if (ret) {
-                       j = i;
-                       goto out_err;
-               }
-       }
-
-       return tfile;
-out_err:
-       for (i = 0; i < j; ++i)
-               drm_ht_remove(&tfile->ref_hash[i]);
-
-       kfree(tfile);
-
-       return NULL;
-}
-EXPORT_SYMBOL(ttm_object_file_init);
-
-struct ttm_object_device *
-ttm_object_device_init(struct ttm_mem_global *mem_glob,
-                      unsigned int hash_order,
-                      const struct dma_buf_ops *ops)
-{
-       struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
-       int ret;
-
-       if (unlikely(tdev == NULL))
-               return NULL;
-
-       tdev->mem_glob = mem_glob;
-       spin_lock_init(&tdev->object_lock);
-       atomic_set(&tdev->object_count, 0);
-       ret = drm_ht_create(&tdev->object_hash, hash_order);
-       if (ret != 0)
-               goto out_no_object_hash;
-
-       tdev->ops = *ops;
-       tdev->dmabuf_release = tdev->ops.release;
-       tdev->ops.release = ttm_prime_dmabuf_release;
-       tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
-               ttm_round_pot(sizeof(struct file));
-       return tdev;
-
-out_no_object_hash:
-       kfree(tdev);
-       return NULL;
-}
-EXPORT_SYMBOL(ttm_object_device_init);
-
-void ttm_object_device_release(struct ttm_object_device **p_tdev)
-{
-       struct ttm_object_device *tdev = *p_tdev;
-
-       *p_tdev = NULL;
-
-       drm_ht_remove(&tdev->object_hash);
-
-       kfree(tdev);
-}
-EXPORT_SYMBOL(ttm_object_device_release);
-
-/**
- * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
- *
- * @dma_buf: Non-refcounted pointer to a struct dma-buf.
- *
- * Obtain a file reference from a lookup structure that doesn't refcount
- * the file, but synchronizes with its release method to make sure it has
- * not been freed yet. See for example kref_get_unless_zero documentation.
- * Returns true if refcounting succeeds, false otherwise.
- *
- * Nobody really wants this as a public API yet, so let it mature here
- * for some time...
- */
-static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
-{
-       return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
-}
-
-/**
- * ttm_prime_refcount_release - refcount release method for a prime object.
- *
- * @p_base: Pointer to ttm_base_object pointer.
- *
- * This is a wrapper that calls the refcount_release founction of the
- * underlying object. At the same time it cleans up the prime object.
- * This function is called when all references to the base object we
- * derive from are gone.
- */
-static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
-{
-       struct ttm_base_object *base = *p_base;
-       struct ttm_prime_object *prime;
-
-       *p_base = NULL;
-       prime = container_of(base, struct ttm_prime_object, base);
-       BUG_ON(prime->dma_buf != NULL);
-       mutex_destroy(&prime->mutex);
-       if (prime->refcount_release)
-               prime->refcount_release(&base);
-}
-
-/**
- * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
- *
- * @dma_buf:
- *
- * This function first calls the dma_buf release method the driver
- * provides. Then it cleans up our dma_buf pointer used for lookup,
- * and finally releases the reference the dma_buf has on our base
- * object.
- */
-static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
-{
-       struct ttm_prime_object *prime =
-               (struct ttm_prime_object *) dma_buf->priv;
-       struct ttm_base_object *base = &prime->base;
-       struct ttm_object_device *tdev = base->tfile->tdev;
-
-       if (tdev->dmabuf_release)
-               tdev->dmabuf_release(dma_buf);
-       mutex_lock(&prime->mutex);
-       if (prime->dma_buf == dma_buf)
-               prime->dma_buf = NULL;
-       mutex_unlock(&prime->mutex);
-       ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
-       ttm_base_object_unref(&base);
-}
-
-/**
- * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
- *
- * @tfile: A struct ttm_object_file identifying the caller.
- * @fd: The prime / dmabuf fd.
- * @handle: The returned handle.
- *
- * This function returns a handle to an object that previously exported
- * a dma-buf. Note that we don't handle imports yet, because we simply
- * have no consumers of that implementation.
- */
-int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
-                          int fd, u32 *handle)
-{
-       struct ttm_object_device *tdev = tfile->tdev;
-       struct dma_buf *dma_buf;
-       struct ttm_prime_object *prime;
-       struct ttm_base_object *base;
-       int ret;
-
-       dma_buf = dma_buf_get(fd);
-       if (IS_ERR(dma_buf))
-               return PTR_ERR(dma_buf);
-
-       if (dma_buf->ops != &tdev->ops)
-               return -ENOSYS;
-
-       prime = (struct ttm_prime_object *) dma_buf->priv;
-       base = &prime->base;
-       *handle = base->hash.key;
-       ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
-
-       dma_buf_put(dma_buf);
-
-       return ret;
-}
-EXPORT_SYMBOL_GPL(ttm_prime_fd_to_handle);
-
-/**
- * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
- *
- * @tfile: Struct ttm_object_file identifying the caller.
- * @handle: Handle to the object we're exporting from.
- * @flags: flags for dma-buf creation. We just pass them on.
- * @prime_fd: The returned file descriptor.
- *
- */
-int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
-                          uint32_t handle, uint32_t flags,
-                          int *prime_fd)
-{
-       struct ttm_object_device *tdev = tfile->tdev;
-       struct ttm_base_object *base;
-       struct dma_buf *dma_buf;
-       struct ttm_prime_object *prime;
-       int ret;
-
-       base = ttm_base_object_lookup(tfile, handle);
-       if (unlikely(base == NULL ||
-                    base->object_type != ttm_prime_type)) {
-               ret = -ENOENT;
-               goto out_unref;
-       }
-
-       prime = container_of(base, struct ttm_prime_object, base);
-       if (unlikely(!base->shareable)) {
-               ret = -EPERM;
-               goto out_unref;
-       }
-
-       ret = mutex_lock_interruptible(&prime->mutex);
-       if (unlikely(ret != 0)) {
-               ret = -ERESTARTSYS;
-               goto out_unref;
-       }
-
-       dma_buf = prime->dma_buf;
-       if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
-               DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
-               struct ttm_operation_ctx ctx = {
-                       .interruptible = true,
-                       .no_wait_gpu = false
-               };
-               exp_info.ops = &tdev->ops;
-               exp_info.size = prime->size;
-               exp_info.flags = flags;
-               exp_info.priv = prime;
-
-               /*
-                * Need to create a new dma_buf, with memory accounting.
-                */
-               ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
-                                          &ctx);
-               if (unlikely(ret != 0)) {
-                       mutex_unlock(&prime->mutex);
-                       goto out_unref;
-               }
-
-               dma_buf = dma_buf_export(&exp_info);
-               if (IS_ERR(dma_buf)) {
-                       ret = PTR_ERR(dma_buf);
-                       ttm_mem_global_free(tdev->mem_glob,
-                                           tdev->dma_buf_size);
-                       mutex_unlock(&prime->mutex);
-                       goto out_unref;
-               }
-
-               /*
-                * dma_buf has taken the base object reference
-                */
-               base = NULL;
-               prime->dma_buf = dma_buf;
-       }
-       mutex_unlock(&prime->mutex);
-
-       ret = dma_buf_fd(dma_buf, flags);
-       if (ret >= 0) {
-               *prime_fd = ret;
-               ret = 0;
-       } else
-               dma_buf_put(dma_buf);
-
-out_unref:
-       if (base)
-               ttm_base_object_unref(&base);
-       return ret;
-}
-EXPORT_SYMBOL_GPL(ttm_prime_handle_to_fd);
-
-/**
- * ttm_prime_object_init - Initialize a ttm_prime_object
- *
- * @tfile: struct ttm_object_file identifying the caller
- * @size: The size of the dma_bufs we export.
- * @prime: The object to be initialized.
- * @shareable: See ttm_base_object_init
- * @type: See ttm_base_object_init
- * @refcount_release: See ttm_base_object_init
- * @ref_obj_release: See ttm_base_object_init
- *
- * Initializes an object which is compatible with the drm_prime model
- * for data sharing between processes and devices.
- */
-int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
-                         struct ttm_prime_object *prime, bool shareable,
-                         enum ttm_object_type type,
-                         void (*refcount_release) (struct ttm_base_object **),
-                         void (*ref_obj_release) (struct ttm_base_object *,
-                                                  enum ttm_ref_type ref_type))
-{
-       mutex_init(&prime->mutex);
-       prime->size = PAGE_ALIGN(size);
-       prime->real_type = type;
-       prime->dma_buf = NULL;
-       prime->refcount_release = refcount_release;
-       return ttm_base_object_init(tfile, &prime->base, shareable,
-                                   ttm_prime_type,
-                                   ttm_prime_refcount_release,
-                                   ref_obj_release);
-}
-EXPORT_SYMBOL(ttm_prime_object_init);
index 09b2aa08363e6b5ca7c13c25a72b4b73d080cdde..22fdc07e03ad348a63af6215c84d544662df0ed3 100644 (file)
@@ -7,6 +7,7 @@ vmwgfx-y := vmwgfx_execbuf.o vmwgfx_gmr.o vmwgfx_kms.o vmwgfx_drv.o \
            vmwgfx_surface.o vmwgfx_prime.o vmwgfx_mob.o vmwgfx_shader.o \
            vmwgfx_cmdbuf_res.o vmwgfx_cmdbuf.o vmwgfx_stdu.o \
            vmwgfx_cotable.o vmwgfx_so.o vmwgfx_binding.o vmwgfx_msg.o \
-           vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o
+           vmwgfx_simple_resource.o vmwgfx_va.o vmwgfx_blit.o \
+           ttm_object.o ttm_lock.o
 
 obj-$(CONFIG_DRM_VMWGFX) := vmwgfx.o
diff --git a/drivers/gpu/drm/vmwgfx/ttm_lock.c b/drivers/gpu/drm/vmwgfx/ttm_lock.c
new file mode 100644 (file)
index 0000000..0d59f5a
--- /dev/null
@@ -0,0 +1,294 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/**************************************************************************
+ *
+ * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
+ */
+
+#include <drm/ttm/ttm_module.h>
+#include <linux/atomic.h>
+#include <linux/errno.h>
+#include <linux/wait.h>
+#include <linux/sched/signal.h>
+#include "ttm_lock.h"
+#include "ttm_object.h"
+
+#define TTM_WRITE_LOCK_PENDING    (1 << 0)
+#define TTM_VT_LOCK_PENDING       (1 << 1)
+#define TTM_SUSPEND_LOCK_PENDING  (1 << 2)
+#define TTM_VT_LOCK               (1 << 3)
+#define TTM_SUSPEND_LOCK          (1 << 4)
+
+void ttm_lock_init(struct ttm_lock *lock)
+{
+       spin_lock_init(&lock->lock);
+       init_waitqueue_head(&lock->queue);
+       lock->rw = 0;
+       lock->flags = 0;
+       lock->kill_takers = false;
+       lock->signal = SIGKILL;
+}
+
+void ttm_read_unlock(struct ttm_lock *lock)
+{
+       spin_lock(&lock->lock);
+       if (--lock->rw == 0)
+               wake_up_all(&lock->queue);
+       spin_unlock(&lock->lock);
+}
+
+static bool __ttm_read_lock(struct ttm_lock *lock)
+{
+       bool locked = false;
+
+       spin_lock(&lock->lock);
+       if (unlikely(lock->kill_takers)) {
+               send_sig(lock->signal, current, 0);
+               spin_unlock(&lock->lock);
+               return false;
+       }
+       if (lock->rw >= 0 && lock->flags == 0) {
+               ++lock->rw;
+               locked = true;
+       }
+       spin_unlock(&lock->lock);
+       return locked;
+}
+
+int ttm_read_lock(struct ttm_lock *lock, bool interruptible)
+{
+       int ret = 0;
+
+       if (interruptible)
+               ret = wait_event_interruptible(lock->queue,
+                                              __ttm_read_lock(lock));
+       else
+               wait_event(lock->queue, __ttm_read_lock(lock));
+       return ret;
+}
+
+static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
+{
+       bool block = true;
+
+       *locked = false;
+
+       spin_lock(&lock->lock);
+       if (unlikely(lock->kill_takers)) {
+               send_sig(lock->signal, current, 0);
+               spin_unlock(&lock->lock);
+               return false;
+       }
+       if (lock->rw >= 0 && lock->flags == 0) {
+               ++lock->rw;
+               block = false;
+               *locked = true;
+       } else if (lock->flags == 0) {
+               block = false;
+       }
+       spin_unlock(&lock->lock);
+
+       return !block;
+}
+
+int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
+{
+       int ret = 0;
+       bool locked;
+
+       if (interruptible)
+               ret = wait_event_interruptible
+                       (lock->queue, __ttm_read_trylock(lock, &locked));
+       else
+               wait_event(lock->queue, __ttm_read_trylock(lock, &locked));
+
+       if (unlikely(ret != 0)) {
+               BUG_ON(locked);
+               return ret;
+       }
+
+       return (locked) ? 0 : -EBUSY;
+}
+
+void ttm_write_unlock(struct ttm_lock *lock)
+{
+       spin_lock(&lock->lock);
+       lock->rw = 0;
+       wake_up_all(&lock->queue);
+       spin_unlock(&lock->lock);
+}
+
+static bool __ttm_write_lock(struct ttm_lock *lock)
+{
+       bool locked = false;
+
+       spin_lock(&lock->lock);
+       if (unlikely(lock->kill_takers)) {
+               send_sig(lock->signal, current, 0);
+               spin_unlock(&lock->lock);
+               return false;
+       }
+       if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
+               lock->rw = -1;
+               lock->flags &= ~TTM_WRITE_LOCK_PENDING;
+               locked = true;
+       } else {
+               lock->flags |= TTM_WRITE_LOCK_PENDING;
+       }
+       spin_unlock(&lock->lock);
+       return locked;
+}
+
+int ttm_write_lock(struct ttm_lock *lock, bool interruptible)
+{
+       int ret = 0;
+
+       if (interruptible) {
+               ret = wait_event_interruptible(lock->queue,
+                                              __ttm_write_lock(lock));
+               if (unlikely(ret != 0)) {
+                       spin_lock(&lock->lock);
+                       lock->flags &= ~TTM_WRITE_LOCK_PENDING;
+                       wake_up_all(&lock->queue);
+                       spin_unlock(&lock->lock);
+               }
+       } else
+               wait_event(lock->queue, __ttm_write_lock(lock));
+
+       return ret;
+}
+
+static int __ttm_vt_unlock(struct ttm_lock *lock)
+{
+       int ret = 0;
+
+       spin_lock(&lock->lock);
+       if (unlikely(!(lock->flags & TTM_VT_LOCK)))
+               ret = -EINVAL;
+       lock->flags &= ~TTM_VT_LOCK;
+       wake_up_all(&lock->queue);
+       spin_unlock(&lock->lock);
+
+       return ret;
+}
+
+static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
+{
+       struct ttm_base_object *base = *p_base;
+       struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
+       int ret;
+
+       *p_base = NULL;
+       ret = __ttm_vt_unlock(lock);
+       BUG_ON(ret != 0);
+}
+
+static bool __ttm_vt_lock(struct ttm_lock *lock)
+{
+       bool locked = false;
+
+       spin_lock(&lock->lock);
+       if (lock->rw == 0) {
+               lock->flags &= ~TTM_VT_LOCK_PENDING;
+               lock->flags |= TTM_VT_LOCK;
+               locked = true;
+       } else {
+               lock->flags |= TTM_VT_LOCK_PENDING;
+       }
+       spin_unlock(&lock->lock);
+       return locked;
+}
+
+int ttm_vt_lock(struct ttm_lock *lock,
+               bool interruptible,
+               struct ttm_object_file *tfile)
+{
+       int ret = 0;
+
+       if (interruptible) {
+               ret = wait_event_interruptible(lock->queue,
+                                              __ttm_vt_lock(lock));
+               if (unlikely(ret != 0)) {
+                       spin_lock(&lock->lock);
+                       lock->flags &= ~TTM_VT_LOCK_PENDING;
+                       wake_up_all(&lock->queue);
+                       spin_unlock(&lock->lock);
+                       return ret;
+               }
+       } else
+               wait_event(lock->queue, __ttm_vt_lock(lock));
+
+       /*
+        * Add a base-object, the destructor of which will
+        * make sure the lock is released if the client dies
+        * while holding it.
+        */
+
+       ret = ttm_base_object_init(tfile, &lock->base, false,
+                                  ttm_lock_type, &ttm_vt_lock_remove, NULL);
+       if (ret)
+               (void)__ttm_vt_unlock(lock);
+       else
+               lock->vt_holder = tfile;
+
+       return ret;
+}
+
+int ttm_vt_unlock(struct ttm_lock *lock)
+{
+       return ttm_ref_object_base_unref(lock->vt_holder,
+                                        lock->base.hash.key, TTM_REF_USAGE);
+}
+
+void ttm_suspend_unlock(struct ttm_lock *lock)
+{
+       spin_lock(&lock->lock);
+       lock->flags &= ~TTM_SUSPEND_LOCK;
+       wake_up_all(&lock->queue);
+       spin_unlock(&lock->lock);
+}
+
+static bool __ttm_suspend_lock(struct ttm_lock *lock)
+{
+       bool locked = false;
+
+       spin_lock(&lock->lock);
+       if (lock->rw == 0) {
+               lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
+               lock->flags |= TTM_SUSPEND_LOCK;
+               locked = true;
+       } else {
+               lock->flags |= TTM_SUSPEND_LOCK_PENDING;
+       }
+       spin_unlock(&lock->lock);
+       return locked;
+}
+
+void ttm_suspend_lock(struct ttm_lock *lock)
+{
+       wait_event(lock->queue, __ttm_suspend_lock(lock));
+}
diff --git a/drivers/gpu/drm/vmwgfx/ttm_lock.h b/drivers/gpu/drm/vmwgfx/ttm_lock.h
new file mode 100644 (file)
index 0000000..0c3af98
--- /dev/null
@@ -0,0 +1,248 @@
+/**************************************************************************
+ *
+ * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
+ */
+
+/** @file ttm_lock.h
+ * This file implements a simple replacement for the buffer manager use
+ * of the DRM heavyweight hardware lock.
+ * The lock is a read-write lock. Taking it in read mode and write mode
+ * is relatively fast, and intended for in-kernel use only.
+ *
+ * The vt mode is used only when there is a need to block all
+ * user-space processes from validating buffers.
+ * It's allowed to leave kernel space with the vt lock held.
+ * If a user-space process dies while having the vt-lock,
+ * it will be released during the file descriptor release. The vt lock
+ * excludes write lock and read lock.
+ *
+ * The suspend mode is used to lock out all TTM users when preparing for
+ * and executing suspend operations.
+ *
+ */
+
+#ifndef _TTM_LOCK_H_
+#define _TTM_LOCK_H_
+
+#include <linux/wait.h>
+#include <linux/atomic.h>
+
+#include "ttm_object.h"
+
+/**
+ * struct ttm_lock
+ *
+ * @base: ttm base object used solely to release the lock if the client
+ * holding the lock dies.
+ * @queue: Queue for processes waiting for lock change-of-status.
+ * @lock: Spinlock protecting some lock members.
+ * @rw: Read-write lock counter. Protected by @lock.
+ * @flags: Lock state. Protected by @lock.
+ * @kill_takers: Boolean whether to kill takers of the lock.
+ * @signal: Signal to send when kill_takers is true.
+ */
+
+struct ttm_lock {
+       struct ttm_base_object base;
+       wait_queue_head_t queue;
+       spinlock_t lock;
+       int32_t rw;
+       uint32_t flags;
+       bool kill_takers;
+       int signal;
+       struct ttm_object_file *vt_holder;
+};
+
+
+/**
+ * ttm_lock_init
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * Initializes the lock.
+ */
+extern void ttm_lock_init(struct ttm_lock *lock);
+
+/**
+ * ttm_read_unlock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Releases a read lock.
+ */
+extern void ttm_read_unlock(struct ttm_lock *lock);
+
+/**
+ * ttm_read_lock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * @interruptible: Interruptible sleeping while waiting for a lock.
+ *
+ * Takes the lock in read mode.
+ * Returns:
+ * -ERESTARTSYS If interrupted by a signal and interruptible is true.
+ */
+extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible);
+
+/**
+ * ttm_read_trylock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * @interruptible: Interruptible sleeping while waiting for a lock.
+ *
+ * Tries to take the lock in read mode. If the lock is already held
+ * in write mode, the function will return -EBUSY. If the lock is held
+ * in vt or suspend mode, the function will sleep until these modes
+ * are unlocked.
+ *
+ * Returns:
+ * -EBUSY The lock was already held in write mode.
+ * -ERESTARTSYS If interrupted by a signal and interruptible is true.
+ */
+extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible);
+
+/**
+ * ttm_write_unlock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Releases a write lock.
+ */
+extern void ttm_write_unlock(struct ttm_lock *lock);
+
+/**
+ * ttm_write_lock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * @interruptible: Interruptible sleeping while waiting for a lock.
+ *
+ * Takes the lock in write mode.
+ * Returns:
+ * -ERESTARTSYS If interrupted by a signal and interruptible is true.
+ */
+extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
+
+/**
+ * ttm_lock_downgrade
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Downgrades a write lock to a read lock.
+ */
+extern void ttm_lock_downgrade(struct ttm_lock *lock);
+
+/**
+ * ttm_suspend_lock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Takes the lock in suspend mode. Excludes read and write mode.
+ */
+extern void ttm_suspend_lock(struct ttm_lock *lock);
+
+/**
+ * ttm_suspend_unlock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Releases a suspend lock
+ */
+extern void ttm_suspend_unlock(struct ttm_lock *lock);
+
+/**
+ * ttm_vt_lock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * @interruptible: Interruptible sleeping while waiting for a lock.
+ * @tfile: Pointer to a struct ttm_object_file to register the lock with.
+ *
+ * Takes the lock in vt mode.
+ * Returns:
+ * -ERESTARTSYS If interrupted by a signal and interruptible is true.
+ * -ENOMEM: Out of memory when locking.
+ */
+extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible,
+                      struct ttm_object_file *tfile);
+
+/**
+ * ttm_vt_unlock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Releases a vt lock.
+ * Returns:
+ * -EINVAL If the lock was not held.
+ */
+extern int ttm_vt_unlock(struct ttm_lock *lock);
+
+/**
+ * ttm_write_unlock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ *
+ * Releases a write lock.
+ */
+extern void ttm_write_unlock(struct ttm_lock *lock);
+
+/**
+ * ttm_write_lock
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * @interruptible: Interruptible sleeping while waiting for a lock.
+ *
+ * Takes the lock in write mode.
+ * Returns:
+ * -ERESTARTSYS If interrupted by a signal and interruptible is true.
+ */
+extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
+
+/**
+ * ttm_lock_set_kill
+ *
+ * @lock: Pointer to a struct ttm_lock
+ * @val: Boolean whether to kill processes taking the lock.
+ * @signal: Signal to send to the process taking the lock.
+ *
+ * The kill-when-taking-lock functionality is used to kill processes that keep
+ * on using the TTM functionality when its resources has been taken down, for
+ * example when the X server exits. A typical sequence would look like this:
+ * - X server takes lock in write mode.
+ * - ttm_lock_set_kill() is called with @val set to true.
+ * - As part of X server exit, TTM resources are taken down.
+ * - X server releases the lock on file release.
+ * - Another dri client wants to render, takes the lock and is killed.
+ *
+ */
+static inline void ttm_lock_set_kill(struct ttm_lock *lock, bool val,
+                                    int signal)
+{
+       lock->kill_takers = val;
+       if (val)
+               lock->signal = signal;
+}
+
+#endif
diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.c b/drivers/gpu/drm/vmwgfx/ttm_object.c
new file mode 100644 (file)
index 0000000..190e259
--- /dev/null
@@ -0,0 +1,761 @@
+/* SPDX-License-Identifier: GPL-2.0 OR MIT */
+/**************************************************************************
+ *
+ * Copyright (c) 2009-2013 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
+ *
+ * While no substantial code is shared, the prime code is inspired by
+ * drm_prime.c, with
+ * Authors:
+ *      Dave Airlie <airlied@redhat.com>
+ *      Rob Clark <rob.clark@linaro.org>
+ */
+/** @file ttm_ref_object.c
+ *
+ * Base- and reference object implementation for the various
+ * ttm objects. Implements reference counting, minimal security checks
+ * and release on file close.
+ */
+
+
+/**
+ * struct ttm_object_file
+ *
+ * @tdev: Pointer to the ttm_object_device.
+ *
+ * @lock: Lock that protects the ref_list list and the
+ * ref_hash hash tables.
+ *
+ * @ref_list: List of ttm_ref_objects to be destroyed at
+ * file release.
+ *
+ * @ref_hash: Hash tables of ref objects, one per ttm_ref_type,
+ * for fast lookup of ref objects given a base object.
+ */
+
+#define pr_fmt(fmt) "[TTM] " fmt
+
+#include <drm/ttm/ttm_module.h>
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/slab.h>
+#include <linux/atomic.h>
+#include "ttm_object.h"
+
+struct ttm_object_file {
+       struct ttm_object_device *tdev;
+       spinlock_t lock;
+       struct list_head ref_list;
+       struct drm_open_hash ref_hash[TTM_REF_NUM];
+       struct kref refcount;
+};
+
+/**
+ * struct ttm_object_device
+ *
+ * @object_lock: lock that protects the object_hash hash table.
+ *
+ * @object_hash: hash table for fast lookup of object global names.
+ *
+ * @object_count: Per device object count.
+ *
+ * This is the per-device data structure needed for ttm object management.
+ */
+
+struct ttm_object_device {
+       spinlock_t object_lock;
+       struct drm_open_hash object_hash;
+       atomic_t object_count;
+       struct ttm_mem_global *mem_glob;
+       struct dma_buf_ops ops;
+       void (*dmabuf_release)(struct dma_buf *dma_buf);
+       size_t dma_buf_size;
+};
+
+/**
+ * struct ttm_ref_object
+ *
+ * @hash: Hash entry for the per-file object reference hash.
+ *
+ * @head: List entry for the per-file list of ref-objects.
+ *
+ * @kref: Ref count.
+ *
+ * @obj: Base object this ref object is referencing.
+ *
+ * @ref_type: Type of ref object.
+ *
+ * This is similar to an idr object, but it also has a hash table entry
+ * that allows lookup with a pointer to the referenced object as a key. In
+ * that way, one can easily detect whether a base object is referenced by
+ * a particular ttm_object_file. It also carries a ref count to avoid creating
+ * multiple ref objects if a ttm_object_file references the same base
+ * object more than once.
+ */
+
+struct ttm_ref_object {
+       struct rcu_head rcu_head;
+       struct drm_hash_item hash;
+       struct list_head head;
+       struct kref kref;
+       enum ttm_ref_type ref_type;
+       struct ttm_base_object *obj;
+       struct ttm_object_file *tfile;
+};
+
+static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf);
+
+static inline struct ttm_object_file *
+ttm_object_file_ref(struct ttm_object_file *tfile)
+{
+       kref_get(&tfile->refcount);
+       return tfile;
+}
+
+static void ttm_object_file_destroy(struct kref *kref)
+{
+       struct ttm_object_file *tfile =
+               container_of(kref, struct ttm_object_file, refcount);
+
+       kfree(tfile);
+}
+
+
+static inline void ttm_object_file_unref(struct ttm_object_file **p_tfile)
+{
+       struct ttm_object_file *tfile = *p_tfile;
+
+       *p_tfile = NULL;
+       kref_put(&tfile->refcount, ttm_object_file_destroy);
+}
+
+
+int ttm_base_object_init(struct ttm_object_file *tfile,
+                        struct ttm_base_object *base,
+                        bool shareable,
+                        enum ttm_object_type object_type,
+                        void (*refcount_release) (struct ttm_base_object **),
+                        void (*ref_obj_release) (struct ttm_base_object *,
+                                                 enum ttm_ref_type ref_type))
+{
+       struct ttm_object_device *tdev = tfile->tdev;
+       int ret;
+
+       base->shareable = shareable;
+       base->tfile = ttm_object_file_ref(tfile);
+       base->refcount_release = refcount_release;
+       base->ref_obj_release = ref_obj_release;
+       base->object_type = object_type;
+       kref_init(&base->refcount);
+       spin_lock(&tdev->object_lock);
+       ret = drm_ht_just_insert_please_rcu(&tdev->object_hash,
+                                           &base->hash,
+                                           (unsigned long)base, 31, 0, 0);
+       spin_unlock(&tdev->object_lock);
+       if (unlikely(ret != 0))
+               goto out_err0;
+
+       ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
+       if (unlikely(ret != 0))
+               goto out_err1;
+
+       ttm_base_object_unref(&base);
+
+       return 0;
+out_err1:
+       spin_lock(&tdev->object_lock);
+       (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
+       spin_unlock(&tdev->object_lock);
+out_err0:
+       return ret;
+}
+
+static void ttm_release_base(struct kref *kref)
+{
+       struct ttm_base_object *base =
+           container_of(kref, struct ttm_base_object, refcount);
+       struct ttm_object_device *tdev = base->tfile->tdev;
+
+       spin_lock(&tdev->object_lock);
+       (void)drm_ht_remove_item_rcu(&tdev->object_hash, &base->hash);
+       spin_unlock(&tdev->object_lock);
+
+       /*
+        * Note: We don't use synchronize_rcu() here because it's far
+        * too slow. It's up to the user to free the object using
+        * call_rcu() or ttm_base_object_kfree().
+        */
+
+       ttm_object_file_unref(&base->tfile);
+       if (base->refcount_release)
+               base->refcount_release(&base);
+}
+
+void ttm_base_object_unref(struct ttm_base_object **p_base)
+{
+       struct ttm_base_object *base = *p_base;
+
+       *p_base = NULL;
+
+       kref_put(&base->refcount, ttm_release_base);
+}
+
+struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file *tfile,
+                                              uint32_t key)
+{
+       struct ttm_base_object *base = NULL;
+       struct drm_hash_item *hash;
+       struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
+       int ret;
+
+       rcu_read_lock();
+       ret = drm_ht_find_item_rcu(ht, key, &hash);
+
+       if (likely(ret == 0)) {
+               base = drm_hash_entry(hash, struct ttm_ref_object, hash)->obj;
+               if (!kref_get_unless_zero(&base->refcount))
+                       base = NULL;
+       }
+       rcu_read_unlock();
+
+       return base;
+}
+
+struct ttm_base_object *
+ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key)
+{
+       struct ttm_base_object *base = NULL;
+       struct drm_hash_item *hash;
+       struct drm_open_hash *ht = &tdev->object_hash;
+       int ret;
+
+       rcu_read_lock();
+       ret = drm_ht_find_item_rcu(ht, key, &hash);
+
+       if (likely(ret == 0)) {
+               base = drm_hash_entry(hash, struct ttm_base_object, hash);
+               if (!kref_get_unless_zero(&base->refcount))
+                       base = NULL;
+       }
+       rcu_read_unlock();
+
+       return base;
+}
+
+/**
+ * ttm_ref_object_exists - Check whether a caller has a valid ref object
+ * (has opened) a base object.
+ *
+ * @tfile: Pointer to a struct ttm_object_file identifying the caller.
+ * @base: Pointer to a struct base object.
+ *
+ * Checks wether the caller identified by @tfile has put a valid USAGE
+ * reference object on the base object identified by @base.
+ */
+bool ttm_ref_object_exists(struct ttm_object_file *tfile,
+                          struct ttm_base_object *base)
+{
+       struct drm_open_hash *ht = &tfile->ref_hash[TTM_REF_USAGE];
+       struct drm_hash_item *hash;
+       struct ttm_ref_object *ref;
+
+       rcu_read_lock();
+       if (unlikely(drm_ht_find_item_rcu(ht, base->hash.key, &hash) != 0))
+               goto out_false;
+
+       /*
+        * Verify that the ref object is really pointing to our base object.
+        * Our base object could actually be dead, and the ref object pointing
+        * to another base object with the same handle.
+        */
+       ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
+       if (unlikely(base != ref->obj))
+               goto out_false;
+
+       /*
+        * Verify that the ref->obj pointer was actually valid!
+        */
+       rmb();
+       if (unlikely(kref_read(&ref->kref) == 0))
+               goto out_false;
+
+       rcu_read_unlock();
+       return true;
+
+ out_false:
+       rcu_read_unlock();
+       return false;
+}
+
+int ttm_ref_object_add(struct ttm_object_file *tfile,
+                      struct ttm_base_object *base,
+                      enum ttm_ref_type ref_type, bool *existed,
+                      bool require_existed)
+{
+       struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
+       struct ttm_ref_object *ref;
+       struct drm_hash_item *hash;
+       struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
+       struct ttm_operation_ctx ctx = {
+               .interruptible = false,
+               .no_wait_gpu = false
+       };
+       int ret = -EINVAL;
+
+       if (base->tfile != tfile && !base->shareable)
+               return -EPERM;
+
+       if (existed != NULL)
+               *existed = true;
+
+       while (ret == -EINVAL) {
+               rcu_read_lock();
+               ret = drm_ht_find_item_rcu(ht, base->hash.key, &hash);
+
+               if (ret == 0) {
+                       ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
+                       if (kref_get_unless_zero(&ref->kref)) {
+                               rcu_read_unlock();
+                               break;
+                       }
+               }
+
+               rcu_read_unlock();
+               if (require_existed)
+                       return -EPERM;
+
+               ret = ttm_mem_global_alloc(mem_glob, sizeof(*ref),
+                                          &ctx);
+               if (unlikely(ret != 0))
+                       return ret;
+               ref = kmalloc(sizeof(*ref), GFP_KERNEL);
+               if (unlikely(ref == NULL)) {
+                       ttm_mem_global_free(mem_glob, sizeof(*ref));
+                       return -ENOMEM;
+               }
+
+               ref->hash.key = base->hash.key;
+               ref->obj = base;
+               ref->tfile = tfile;
+               ref->ref_type = ref_type;
+               kref_init(&ref->kref);
+
+               spin_lock(&tfile->lock);
+               ret = drm_ht_insert_item_rcu(ht, &ref->hash);
+
+               if (likely(ret == 0)) {
+                       list_add_tail(&ref->head, &tfile->ref_list);
+                       kref_get(&base->refcount);
+                       spin_unlock(&tfile->lock);
+                       if (existed != NULL)
+                               *existed = false;
+                       break;
+               }
+
+               spin_unlock(&tfile->lock);
+               BUG_ON(ret != -EINVAL);
+
+               ttm_mem_global_free(mem_glob, sizeof(*ref));
+               kfree(ref);
+       }
+
+       return ret;
+}
+
+static void __releases(tfile->lock) __acquires(tfile->lock)
+ttm_ref_object_release(struct kref *kref)
+{
+       struct ttm_ref_object *ref =
+           container_of(kref, struct ttm_ref_object, kref);
+       struct ttm_base_object *base = ref->obj;
+       struct ttm_object_file *tfile = ref->tfile;
+       struct drm_open_hash *ht;
+       struct ttm_mem_global *mem_glob = tfile->tdev->mem_glob;
+
+       ht = &tfile->ref_hash[ref->ref_type];
+       (void)drm_ht_remove_item_rcu(ht, &ref->hash);
+       list_del(&ref->head);
+       spin_unlock(&tfile->lock);
+
+       if (ref->ref_type != TTM_REF_USAGE && base->ref_obj_release)
+               base->ref_obj_release(base, ref->ref_type);
+
+       ttm_base_object_unref(&ref->obj);
+       ttm_mem_global_free(mem_glob, sizeof(*ref));
+       kfree_rcu(ref, rcu_head);
+       spin_lock(&tfile->lock);
+}
+
+int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
+                             unsigned long key, enum ttm_ref_type ref_type)
+{
+       struct drm_open_hash *ht = &tfile->ref_hash[ref_type];
+       struct ttm_ref_object *ref;
+       struct drm_hash_item *hash;
+       int ret;
+
+       spin_lock(&tfile->lock);
+       ret = drm_ht_find_item(ht, key, &hash);
+       if (unlikely(ret != 0)) {
+               spin_unlock(&tfile->lock);
+               return -EINVAL;
+       }
+       ref = drm_hash_entry(hash, struct ttm_ref_object, hash);
+       kref_put(&ref->kref, ttm_ref_object_release);
+       spin_unlock(&tfile->lock);
+       return 0;
+}
+
+void ttm_object_file_release(struct ttm_object_file **p_tfile)
+{
+       struct ttm_ref_object *ref;
+       struct list_head *list;
+       unsigned int i;
+       struct ttm_object_file *tfile = *p_tfile;
+
+       *p_tfile = NULL;
+       spin_lock(&tfile->lock);
+
+       /*
+        * Since we release the lock within the loop, we have to
+        * restart it from the beginning each time.
+        */
+
+       while (!list_empty(&tfile->ref_list)) {
+               list = tfile->ref_list.next;
+               ref = list_entry(list, struct ttm_ref_object, head);
+               ttm_ref_object_release(&ref->kref);
+       }
+
+       spin_unlock(&tfile->lock);
+       for (i = 0; i < TTM_REF_NUM; ++i)
+               drm_ht_remove(&tfile->ref_hash[i]);
+
+       ttm_object_file_unref(&tfile);
+}
+
+struct ttm_object_file *ttm_object_file_init(struct ttm_object_device *tdev,
+                                            unsigned int hash_order)
+{
+       struct ttm_object_file *tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
+       unsigned int i;
+       unsigned int j = 0;
+       int ret;
+
+       if (unlikely(tfile == NULL))
+               return NULL;
+
+       spin_lock_init(&tfile->lock);
+       tfile->tdev = tdev;
+       kref_init(&tfile->refcount);
+       INIT_LIST_HEAD(&tfile->ref_list);
+
+       for (i = 0; i < TTM_REF_NUM; ++i) {
+               ret = drm_ht_create(&tfile->ref_hash[i], hash_order);
+               if (ret) {
+                       j = i;
+                       goto out_err;
+               }
+       }
+
+       return tfile;
+out_err:
+       for (i = 0; i < j; ++i)
+               drm_ht_remove(&tfile->ref_hash[i]);
+
+       kfree(tfile);
+
+       return NULL;
+}
+
+struct ttm_object_device *
+ttm_object_device_init(struct ttm_mem_global *mem_glob,
+                      unsigned int hash_order,
+                      const struct dma_buf_ops *ops)
+{
+       struct ttm_object_device *tdev = kmalloc(sizeof(*tdev), GFP_KERNEL);
+       int ret;
+
+       if (unlikely(tdev == NULL))
+               return NULL;
+
+       tdev->mem_glob = mem_glob;
+       spin_lock_init(&tdev->object_lock);
+       atomic_set(&tdev->object_count, 0);
+       ret = drm_ht_create(&tdev->object_hash, hash_order);
+       if (ret != 0)
+               goto out_no_object_hash;
+
+       tdev->ops = *ops;
+       tdev->dmabuf_release = tdev->ops.release;
+       tdev->ops.release = ttm_prime_dmabuf_release;
+       tdev->dma_buf_size = ttm_round_pot(sizeof(struct dma_buf)) +
+               ttm_round_pot(sizeof(struct file));
+       return tdev;
+
+out_no_object_hash:
+       kfree(tdev);
+       return NULL;
+}
+
+void ttm_object_device_release(struct ttm_object_device **p_tdev)
+{
+       struct ttm_object_device *tdev = *p_tdev;
+
+       *p_tdev = NULL;
+
+       drm_ht_remove(&tdev->object_hash);
+
+       kfree(tdev);
+}
+
+/**
+ * get_dma_buf_unless_doomed - get a dma_buf reference if possible.
+ *
+ * @dma_buf: Non-refcounted pointer to a struct dma-buf.
+ *
+ * Obtain a file reference from a lookup structure that doesn't refcount
+ * the file, but synchronizes with its release method to make sure it has
+ * not been freed yet. See for example kref_get_unless_zero documentation.
+ * Returns true if refcounting succeeds, false otherwise.
+ *
+ * Nobody really wants this as a public API yet, so let it mature here
+ * for some time...
+ */
+static bool __must_check get_dma_buf_unless_doomed(struct dma_buf *dmabuf)
+{
+       return atomic_long_inc_not_zero(&dmabuf->file->f_count) != 0L;
+}
+
+/**
+ * ttm_prime_refcount_release - refcount release method for a prime object.
+ *
+ * @p_base: Pointer to ttm_base_object pointer.
+ *
+ * This is a wrapper that calls the refcount_release founction of the
+ * underlying object. At the same time it cleans up the prime object.
+ * This function is called when all references to the base object we
+ * derive from are gone.
+ */
+static void ttm_prime_refcount_release(struct ttm_base_object **p_base)
+{
+       struct ttm_base_object *base = *p_base;
+       struct ttm_prime_object *prime;
+
+       *p_base = NULL;
+       prime = container_of(base, struct ttm_prime_object, base);
+       BUG_ON(prime->dma_buf != NULL);
+       mutex_destroy(&prime->mutex);
+       if (prime->refcount_release)
+               prime->refcount_release(&base);
+}
+
+/**
+ * ttm_prime_dmabuf_release - Release method for the dma-bufs we export
+ *
+ * @dma_buf:
+ *
+ * This function first calls the dma_buf release method the driver
+ * provides. Then it cleans up our dma_buf pointer used for lookup,
+ * and finally releases the reference the dma_buf has on our base
+ * object.
+ */
+static void ttm_prime_dmabuf_release(struct dma_buf *dma_buf)
+{
+       struct ttm_prime_object *prime =
+               (struct ttm_prime_object *) dma_buf->priv;
+       struct ttm_base_object *base = &prime->base;
+       struct ttm_object_device *tdev = base->tfile->tdev;
+
+       if (tdev->dmabuf_release)
+               tdev->dmabuf_release(dma_buf);
+       mutex_lock(&prime->mutex);
+       if (prime->dma_buf == dma_buf)
+               prime->dma_buf = NULL;
+       mutex_unlock(&prime->mutex);
+       ttm_mem_global_free(tdev->mem_glob, tdev->dma_buf_size);
+       ttm_base_object_unref(&base);
+}
+
+/**
+ * ttm_prime_fd_to_handle - Get a base object handle from a prime fd
+ *
+ * @tfile: A struct ttm_object_file identifying the caller.
+ * @fd: The prime / dmabuf fd.
+ * @handle: The returned handle.
+ *
+ * This function returns a handle to an object that previously exported
+ * a dma-buf. Note that we don't handle imports yet, because we simply
+ * have no consumers of that implementation.
+ */
+int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
+                          int fd, u32 *handle)
+{
+       struct ttm_object_device *tdev = tfile->tdev;
+       struct dma_buf *dma_buf;
+       struct ttm_prime_object *prime;
+       struct ttm_base_object *base;
+       int ret;
+
+       dma_buf = dma_buf_get(fd);
+       if (IS_ERR(dma_buf))
+               return PTR_ERR(dma_buf);
+
+       if (dma_buf->ops != &tdev->ops)
+               return -ENOSYS;
+
+       prime = (struct ttm_prime_object *) dma_buf->priv;
+       base = &prime->base;
+       *handle = base->hash.key;
+       ret = ttm_ref_object_add(tfile, base, TTM_REF_USAGE, NULL, false);
+
+       dma_buf_put(dma_buf);
+
+       return ret;
+}
+
+/**
+ * ttm_prime_handle_to_fd - Return a dma_buf fd from a ttm prime object
+ *
+ * @tfile: Struct ttm_object_file identifying the caller.
+ * @handle: Handle to the object we're exporting from.
+ * @flags: flags for dma-buf creation. We just pass them on.
+ * @prime_fd: The returned file descriptor.
+ *
+ */
+int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
+                          uint32_t handle, uint32_t flags,
+                          int *prime_fd)
+{
+       struct ttm_object_device *tdev = tfile->tdev;
+       struct ttm_base_object *base;
+       struct dma_buf *dma_buf;
+       struct ttm_prime_object *prime;
+       int ret;
+
+       base = ttm_base_object_lookup(tfile, handle);
+       if (unlikely(base == NULL ||
+                    base->object_type != ttm_prime_type)) {
+               ret = -ENOENT;
+               goto out_unref;
+       }
+
+       prime = container_of(base, struct ttm_prime_object, base);
+       if (unlikely(!base->shareable)) {
+               ret = -EPERM;
+               goto out_unref;
+       }
+
+       ret = mutex_lock_interruptible(&prime->mutex);
+       if (unlikely(ret != 0)) {
+               ret = -ERESTARTSYS;
+               goto out_unref;
+       }
+
+       dma_buf = prime->dma_buf;
+       if (!dma_buf || !get_dma_buf_unless_doomed(dma_buf)) {
+               DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
+               struct ttm_operation_ctx ctx = {
+                       .interruptible = true,
+                       .no_wait_gpu = false
+               };
+               exp_info.ops = &tdev->ops;
+               exp_info.size = prime->size;
+               exp_info.flags = flags;
+               exp_info.priv = prime;
+
+               /*
+                * Need to create a new dma_buf, with memory accounting.
+                */
+               ret = ttm_mem_global_alloc(tdev->mem_glob, tdev->dma_buf_size,
+                                          &ctx);
+               if (unlikely(ret != 0)) {
+                       mutex_unlock(&prime->mutex);
+                       goto out_unref;
+               }
+
+               dma_buf = dma_buf_export(&exp_info);
+               if (IS_ERR(dma_buf)) {
+                       ret = PTR_ERR(dma_buf);
+                       ttm_mem_global_free(tdev->mem_glob,
+                                           tdev->dma_buf_size);
+                       mutex_unlock(&prime->mutex);
+                       goto out_unref;
+               }
+
+               /*
+                * dma_buf has taken the base object reference
+                */
+               base = NULL;
+               prime->dma_buf = dma_buf;
+       }
+       mutex_unlock(&prime->mutex);
+
+       ret = dma_buf_fd(dma_buf, flags);
+       if (ret >= 0) {
+               *prime_fd = ret;
+               ret = 0;
+       } else
+               dma_buf_put(dma_buf);
+
+out_unref:
+       if (base)
+               ttm_base_object_unref(&base);
+       return ret;
+}
+
+/**
+ * ttm_prime_object_init - Initialize a ttm_prime_object
+ *
+ * @tfile: struct ttm_object_file identifying the caller
+ * @size: The size of the dma_bufs we export.
+ * @prime: The object to be initialized.
+ * @shareable: See ttm_base_object_init
+ * @type: See ttm_base_object_init
+ * @refcount_release: See ttm_base_object_init
+ * @ref_obj_release: See ttm_base_object_init
+ *
+ * Initializes an object which is compatible with the drm_prime model
+ * for data sharing between processes and devices.
+ */
+int ttm_prime_object_init(struct ttm_object_file *tfile, size_t size,
+                         struct ttm_prime_object *prime, bool shareable,
+                         enum ttm_object_type type,
+                         void (*refcount_release) (struct ttm_base_object **),
+                         void (*ref_obj_release) (struct ttm_base_object *,
+                                                  enum ttm_ref_type ref_type))
+{
+       mutex_init(&prime->mutex);
+       prime->size = PAGE_ALIGN(size);
+       prime->real_type = type;
+       prime->dma_buf = NULL;
+       prime->refcount_release = refcount_release;
+       return ttm_base_object_init(tfile, &prime->base, shareable,
+                                   ttm_prime_type,
+                                   ttm_prime_refcount_release,
+                                   ref_obj_release);
+}
diff --git a/drivers/gpu/drm/vmwgfx/ttm_object.h b/drivers/gpu/drm/vmwgfx/ttm_object.h
new file mode 100644 (file)
index 0000000..1c1b9cc
--- /dev/null
@@ -0,0 +1,353 @@
+/**************************************************************************
+ *
+ * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+ * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+ * USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+/*
+ * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
+ */
+/** @file ttm_object.h
+ *
+ * Base- and reference object implementation for the various
+ * ttm objects. Implements reference counting, minimal security checks
+ * and release on file close.
+ */
+
+#ifndef _TTM_OBJECT_H_
+#define _TTM_OBJECT_H_
+
+#include <linux/list.h>
+#include <drm/drm_hashtab.h>
+#include <linux/kref.h>
+#include <linux/rcupdate.h>
+#include <linux/dma-buf.h>
+#include <drm/ttm/ttm_memory.h>
+
+/**
+ * enum ttm_ref_type
+ *
+ * Describes what type of reference a ref object holds.
+ *
+ * TTM_REF_USAGE is a simple refcount on a base object.
+ *
+ * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
+ * buffer object.
+ *
+ * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
+ * buffer object.
+ *
+ */
+
+enum ttm_ref_type {
+       TTM_REF_USAGE,
+       TTM_REF_SYNCCPU_READ,
+       TTM_REF_SYNCCPU_WRITE,
+       TTM_REF_NUM
+};
+
+/**
+ * enum ttm_object_type
+ *
+ * One entry per ttm object type.
+ * Device-specific types should use the
+ * ttm_driver_typex types.
+ */
+
+enum ttm_object_type {
+       ttm_fence_type,
+       ttm_buffer_type,
+       ttm_lock_type,
+       ttm_prime_type,
+       ttm_driver_type0 = 256,
+       ttm_driver_type1,
+       ttm_driver_type2,
+       ttm_driver_type3,
+       ttm_driver_type4,
+       ttm_driver_type5
+};
+
+struct ttm_object_file;
+struct ttm_object_device;
+
+/**
+ * struct ttm_base_object
+ *
+ * @hash: hash entry for the per-device object hash.
+ * @type: derived type this object is base class for.
+ * @shareable: Other ttm_object_files can access this object.
+ *
+ * @tfile: Pointer to ttm_object_file of the creator.
+ * NULL if the object was not created by a user request.
+ * (kernel object).
+ *
+ * @refcount: Number of references to this object, not
+ * including the hash entry. A reference to a base object can
+ * only be held by a ref object.
+ *
+ * @refcount_release: A function to be called when there are
+ * no more references to this object. This function should
+ * destroy the object (or make sure destruction eventually happens),
+ * and when it is called, the object has
+ * already been taken out of the per-device hash. The parameter
+ * "base" should be set to NULL by the function.
+ *
+ * @ref_obj_release: A function to be called when a reference object
+ * with another ttm_ref_type than TTM_REF_USAGE is deleted.
+ * This function may, for example, release a lock held by a user-space
+ * process.
+ *
+ * This struct is intended to be used as a base struct for objects that
+ * are visible to user-space. It provides a global name, race-safe
+ * access and refcounting, minimal access contol and hooks for unref actions.
+ */
+
+struct ttm_base_object {
+       struct rcu_head rhead;
+       struct drm_hash_item hash;
+       enum ttm_object_type object_type;
+       bool shareable;
+       struct ttm_object_file *tfile;
+       struct kref refcount;
+       void (*refcount_release) (struct ttm_base_object **base);
+       void (*ref_obj_release) (struct ttm_base_object *base,
+                                enum ttm_ref_type ref_type);
+};
+
+
+/**
+ * struct ttm_prime_object - Modified base object that is prime-aware
+ *
+ * @base: struct ttm_base_object that we derive from
+ * @mutex: Mutex protecting the @dma_buf member.
+ * @size: Size of the dma_buf associated with this object
+ * @real_type: Type of the underlying object. Needed since we're setting
+ * the value of @base::object_type to ttm_prime_type
+ * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
+ * object.
+ * @refcount_release: The underlying object's release method. Needed since
+ * we set @base::refcount_release to our own release method.
+ */
+
+struct ttm_prime_object {
+       struct ttm_base_object base;
+       struct mutex mutex;
+       size_t size;
+       enum ttm_object_type real_type;
+       struct dma_buf *dma_buf;
+       void (*refcount_release) (struct ttm_base_object **);
+};
+
+/**
+ * ttm_base_object_init
+ *
+ * @tfile: Pointer to a struct ttm_object_file.
+ * @base: The struct ttm_base_object to initialize.
+ * @shareable: This object is shareable with other applcations.
+ * (different @tfile pointers.)
+ * @type: The object type.
+ * @refcount_release: See the struct ttm_base_object description.
+ * @ref_obj_release: See the struct ttm_base_object description.
+ *
+ * Initializes a struct ttm_base_object.
+ */
+
+extern int ttm_base_object_init(struct ttm_object_file *tfile,
+                               struct ttm_base_object *base,
+                               bool shareable,
+                               enum ttm_object_type type,
+                               void (*refcount_release) (struct ttm_base_object
+                                                         **),
+                               void (*ref_obj_release) (struct ttm_base_object
+                                                        *,
+                                                        enum ttm_ref_type
+                                                        ref_type));
+
+/**
+ * ttm_base_object_lookup
+ *
+ * @tfile: Pointer to a struct ttm_object_file.
+ * @key: Hash key
+ *
+ * Looks up a struct ttm_base_object with the key @key.
+ */
+
+extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
+                                                     *tfile, uint32_t key);
+
+/**
+ * ttm_base_object_lookup_for_ref
+ *
+ * @tdev: Pointer to a struct ttm_object_device.
+ * @key: Hash key
+ *
+ * Looks up a struct ttm_base_object with the key @key.
+ * This function should only be used when the struct tfile associated with the
+ * caller doesn't yet have a reference to the base object.
+ */
+
+extern struct ttm_base_object *
+ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key);
+
+/**
+ * ttm_base_object_unref
+ *
+ * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
+ *
+ * Decrements the base object refcount and clears the pointer pointed to by
+ * p_base.
+ */
+
+extern void ttm_base_object_unref(struct ttm_base_object **p_base);
+
+/**
+ * ttm_ref_object_add.
+ *
+ * @tfile: A struct ttm_object_file representing the application owning the
+ * ref_object.
+ * @base: The base object to reference.
+ * @ref_type: The type of reference.
+ * @existed: Upon completion, indicates that an identical reference object
+ * already existed, and the refcount was upped on that object instead.
+ * @require_existed: Fail with -EPERM if an identical ref object didn't
+ * already exist.
+ *
+ * Checks that the base object is shareable and adds a ref object to it.
+ *
+ * Adding a ref object to a base object is basically like referencing the
+ * base object, but a user-space application holds the reference. When the
+ * file corresponding to @tfile is closed, all its reference objects are
+ * deleted. A reference object can have different types depending on what
+ * it's intended for. It can be refcounting to prevent object destruction,
+ * When user-space takes a lock, it can add a ref object to that lock to
+ * make sure the lock is released if the application dies. A ref object
+ * will hold a single reference on a base object.
+ */
+extern int ttm_ref_object_add(struct ttm_object_file *tfile,
+                             struct ttm_base_object *base,
+                             enum ttm_ref_type ref_type, bool *existed,
+                             bool require_existed);
+
+extern bool ttm_ref_object_exists(struct ttm_object_file *tfile,
+                                 struct ttm_base_object *base);
+
+/**
+ * ttm_ref_object_base_unref
+ *
+ * @key: Key representing the base object.
+ * @ref_type: Ref type of the ref object to be dereferenced.
+ *
+ * Unreference a ref object with type @ref_type
+ * on the base object identified by @key. If there are no duplicate
+ * references, the ref object will be destroyed and the base object
+ * will be unreferenced.
+ */
+extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
+                                    unsigned long key,
+                                    enum ttm_ref_type ref_type);
+
+/**
+ * ttm_object_file_init - initialize a struct ttm_object file
+ *
+ * @tdev: A struct ttm_object device this file is initialized on.
+ * @hash_order: Order of the hash table used to hold the reference objects.
+ *
+ * This is typically called by the file_ops::open function.
+ */
+
+extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
+                                                   *tdev,
+                                                   unsigned int hash_order);
+
+/**
+ * ttm_object_file_release - release data held by a ttm_object_file
+ *
+ * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
+ * *p_tfile will be set to NULL by this function.
+ *
+ * Releases all data associated by a ttm_object_file.
+ * Typically called from file_ops::release. The caller must
+ * ensure that there are no concurrent users of tfile.
+ */
+
+extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
+
+/**
+ * ttm_object device init - initialize a struct ttm_object_device
+ *
+ * @mem_glob: struct ttm_mem_global for memory accounting.
+ * @hash_order: Order of hash table used to hash the base objects.
+ * @ops: DMA buf ops for prime objects of this device.
+ *
+ * This function is typically called on device initialization to prepare
+ * data structures needed for ttm base and ref objects.
+ */
+
+extern struct ttm_object_device *
+ttm_object_device_init(struct ttm_mem_global *mem_glob,
+                      unsigned int hash_order,
+                      const struct dma_buf_ops *ops);
+
+/**
+ * ttm_object_device_release - release data held by a ttm_object_device
+ *
+ * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
+ * *p_tdev will be set to NULL by this function.
+ *
+ * Releases all data associated by a ttm_object_device.
+ * Typically called from driver::unload before the destruction of the
+ * device private data structure.
+ */
+
+extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
+
+#define ttm_base_object_kfree(__object, __base)\
+       kfree_rcu(__object, __base.rhead)
+
+extern int ttm_prime_object_init(struct ttm_object_file *tfile,
+                                size_t size,
+                                struct ttm_prime_object *prime,
+                                bool shareable,
+                                enum ttm_object_type type,
+                                void (*refcount_release)
+                                (struct ttm_base_object **),
+                                void (*ref_obj_release)
+                                (struct ttm_base_object *,
+                                 enum ttm_ref_type ref_type));
+
+static inline enum ttm_object_type
+ttm_base_object_type(struct ttm_base_object *base)
+{
+       return (base->object_type == ttm_prime_type) ?
+               container_of(base, struct ttm_prime_object, base)->real_type :
+               base->object_type;
+}
+extern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
+                                 int fd, u32 *handle);
+extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
+                                 uint32_t handle, uint32_t flags,
+                                 int *prime_fd);
+
+#define ttm_prime_object_kfree(__obj, __prime)         \
+       kfree_rcu(__obj, __prime.base.rhead)
+#endif
index 2dda0334576154ff3c22d56c0a8813dfee375dcd..36e84acee3ea0e6ee015c5245771de7aa375ccde 100644 (file)
@@ -30,7 +30,7 @@
 
 #include <drm/drmP.h>
 #include "vmwgfx_drv.h"
-#include "drm/ttm/ttm_object.h"
+#include "ttm_object.h"
 
 
 /**
index bb6dbbe188358f040c1a1e17425874b9b581e367..d9c178e235b4a1dd76e06c704ce6db067323629c 100644 (file)
@@ -30,9 +30,9 @@
 #include <drm/drmP.h>
 #include "vmwgfx_drv.h"
 #include "vmwgfx_binding.h"
+#include "ttm_object.h"
 #include <drm/ttm/ttm_placement.h>
 #include <drm/ttm/ttm_bo_driver.h>
-#include <drm/ttm/ttm_object.h>
 #include <drm/ttm/ttm_module.h>
 #include <linux/dma_remapping.h>
 
index 1abe21758b0d7523a0b66631f615f55e0d07796a..1aa11c39f07f724e3697da3349299b7eadcad1e2 100644 (file)
 #include <drm/drm_auth.h>
 #include <linux/suspend.h>
 #include <drm/ttm/ttm_bo_driver.h>
-#include <drm/ttm/ttm_object.h>
-#include <drm/ttm/ttm_lock.h>
 #include <drm/ttm/ttm_execbuf_util.h>
 #include <drm/ttm/ttm_module.h>
 #include "vmwgfx_fence.h"
+#include "ttm_object.h"
+#include "ttm_lock.h"
 #include <linux/sync_file.h>
 
 #define VMWGFX_DRIVER_NAME "vmwgfx"
index 0861c821a7fe399540726ec93e54baab5d383152..e420675e8db3ceb19b669fdd1f517dd5f276e899 100644 (file)
@@ -31,8 +31,8 @@
  */
 
 #include "vmwgfx_drv.h"
+#include "ttm_object.h"
 #include <linux/dma-buf.h>
-#include <drm/ttm/ttm_object.h>
 
 /*
  * DMA-BUF attach- and mapping methods. No need to implement
diff --git a/include/drm/ttm/ttm_lock.h b/include/drm/ttm/ttm_lock.h
deleted file mode 100644 (file)
index 0c3af98..0000000
+++ /dev/null
@@ -1,248 +0,0 @@
-/**************************************************************************
- *
- * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- **************************************************************************/
-/*
- * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
- */
-
-/** @file ttm_lock.h
- * This file implements a simple replacement for the buffer manager use
- * of the DRM heavyweight hardware lock.
- * The lock is a read-write lock. Taking it in read mode and write mode
- * is relatively fast, and intended for in-kernel use only.
- *
- * The vt mode is used only when there is a need to block all
- * user-space processes from validating buffers.
- * It's allowed to leave kernel space with the vt lock held.
- * If a user-space process dies while having the vt-lock,
- * it will be released during the file descriptor release. The vt lock
- * excludes write lock and read lock.
- *
- * The suspend mode is used to lock out all TTM users when preparing for
- * and executing suspend operations.
- *
- */
-
-#ifndef _TTM_LOCK_H_
-#define _TTM_LOCK_H_
-
-#include <linux/wait.h>
-#include <linux/atomic.h>
-
-#include "ttm_object.h"
-
-/**
- * struct ttm_lock
- *
- * @base: ttm base object used solely to release the lock if the client
- * holding the lock dies.
- * @queue: Queue for processes waiting for lock change-of-status.
- * @lock: Spinlock protecting some lock members.
- * @rw: Read-write lock counter. Protected by @lock.
- * @flags: Lock state. Protected by @lock.
- * @kill_takers: Boolean whether to kill takers of the lock.
- * @signal: Signal to send when kill_takers is true.
- */
-
-struct ttm_lock {
-       struct ttm_base_object base;
-       wait_queue_head_t queue;
-       spinlock_t lock;
-       int32_t rw;
-       uint32_t flags;
-       bool kill_takers;
-       int signal;
-       struct ttm_object_file *vt_holder;
-};
-
-
-/**
- * ttm_lock_init
- *
- * @lock: Pointer to a struct ttm_lock
- * Initializes the lock.
- */
-extern void ttm_lock_init(struct ttm_lock *lock);
-
-/**
- * ttm_read_unlock
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Releases a read lock.
- */
-extern void ttm_read_unlock(struct ttm_lock *lock);
-
-/**
- * ttm_read_lock
- *
- * @lock: Pointer to a struct ttm_lock
- * @interruptible: Interruptible sleeping while waiting for a lock.
- *
- * Takes the lock in read mode.
- * Returns:
- * -ERESTARTSYS If interrupted by a signal and interruptible is true.
- */
-extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible);
-
-/**
- * ttm_read_trylock
- *
- * @lock: Pointer to a struct ttm_lock
- * @interruptible: Interruptible sleeping while waiting for a lock.
- *
- * Tries to take the lock in read mode. If the lock is already held
- * in write mode, the function will return -EBUSY. If the lock is held
- * in vt or suspend mode, the function will sleep until these modes
- * are unlocked.
- *
- * Returns:
- * -EBUSY The lock was already held in write mode.
- * -ERESTARTSYS If interrupted by a signal and interruptible is true.
- */
-extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible);
-
-/**
- * ttm_write_unlock
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Releases a write lock.
- */
-extern void ttm_write_unlock(struct ttm_lock *lock);
-
-/**
- * ttm_write_lock
- *
- * @lock: Pointer to a struct ttm_lock
- * @interruptible: Interruptible sleeping while waiting for a lock.
- *
- * Takes the lock in write mode.
- * Returns:
- * -ERESTARTSYS If interrupted by a signal and interruptible is true.
- */
-extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
-
-/**
- * ttm_lock_downgrade
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Downgrades a write lock to a read lock.
- */
-extern void ttm_lock_downgrade(struct ttm_lock *lock);
-
-/**
- * ttm_suspend_lock
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Takes the lock in suspend mode. Excludes read and write mode.
- */
-extern void ttm_suspend_lock(struct ttm_lock *lock);
-
-/**
- * ttm_suspend_unlock
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Releases a suspend lock
- */
-extern void ttm_suspend_unlock(struct ttm_lock *lock);
-
-/**
- * ttm_vt_lock
- *
- * @lock: Pointer to a struct ttm_lock
- * @interruptible: Interruptible sleeping while waiting for a lock.
- * @tfile: Pointer to a struct ttm_object_file to register the lock with.
- *
- * Takes the lock in vt mode.
- * Returns:
- * -ERESTARTSYS If interrupted by a signal and interruptible is true.
- * -ENOMEM: Out of memory when locking.
- */
-extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible,
-                      struct ttm_object_file *tfile);
-
-/**
- * ttm_vt_unlock
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Releases a vt lock.
- * Returns:
- * -EINVAL If the lock was not held.
- */
-extern int ttm_vt_unlock(struct ttm_lock *lock);
-
-/**
- * ttm_write_unlock
- *
- * @lock: Pointer to a struct ttm_lock
- *
- * Releases a write lock.
- */
-extern void ttm_write_unlock(struct ttm_lock *lock);
-
-/**
- * ttm_write_lock
- *
- * @lock: Pointer to a struct ttm_lock
- * @interruptible: Interruptible sleeping while waiting for a lock.
- *
- * Takes the lock in write mode.
- * Returns:
- * -ERESTARTSYS If interrupted by a signal and interruptible is true.
- */
-extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
-
-/**
- * ttm_lock_set_kill
- *
- * @lock: Pointer to a struct ttm_lock
- * @val: Boolean whether to kill processes taking the lock.
- * @signal: Signal to send to the process taking the lock.
- *
- * The kill-when-taking-lock functionality is used to kill processes that keep
- * on using the TTM functionality when its resources has been taken down, for
- * example when the X server exits. A typical sequence would look like this:
- * - X server takes lock in write mode.
- * - ttm_lock_set_kill() is called with @val set to true.
- * - As part of X server exit, TTM resources are taken down.
- * - X server releases the lock on file release.
- * - Another dri client wants to render, takes the lock and is killed.
- *
- */
-static inline void ttm_lock_set_kill(struct ttm_lock *lock, bool val,
-                                    int signal)
-{
-       lock->kill_takers = val;
-       if (val)
-               lock->signal = signal;
-}
-
-#endif
diff --git a/include/drm/ttm/ttm_object.h b/include/drm/ttm/ttm_object.h
deleted file mode 100644 (file)
index a98bfeb..0000000
+++ /dev/null
@@ -1,354 +0,0 @@
-/**************************************************************************
- *
- * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
- * All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sub license, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice (including the
- * next paragraph) shall be included in all copies or substantial portions
- * of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- **************************************************************************/
-/*
- * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
- */
-/** @file ttm_object.h
- *
- * Base- and reference object implementation for the various
- * ttm objects. Implements reference counting, minimal security checks
- * and release on file close.
- */
-
-#ifndef _TTM_OBJECT_H_
-#define _TTM_OBJECT_H_
-
-#include <linux/list.h>
-#include <drm/drm_hashtab.h>
-#include <linux/kref.h>
-#include <linux/rcupdate.h>
-#include <linux/dma-buf.h>
-
-#include "ttm_memory.h"
-
-/**
- * enum ttm_ref_type
- *
- * Describes what type of reference a ref object holds.
- *
- * TTM_REF_USAGE is a simple refcount on a base object.
- *
- * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
- * buffer object.
- *
- * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
- * buffer object.
- *
- */
-
-enum ttm_ref_type {
-       TTM_REF_USAGE,
-       TTM_REF_SYNCCPU_READ,
-       TTM_REF_SYNCCPU_WRITE,
-       TTM_REF_NUM
-};
-
-/**
- * enum ttm_object_type
- *
- * One entry per ttm object type.
- * Device-specific types should use the
- * ttm_driver_typex types.
- */
-
-enum ttm_object_type {
-       ttm_fence_type,
-       ttm_buffer_type,
-       ttm_lock_type,
-       ttm_prime_type,
-       ttm_driver_type0 = 256,
-       ttm_driver_type1,
-       ttm_driver_type2,
-       ttm_driver_type3,
-       ttm_driver_type4,
-       ttm_driver_type5
-};
-
-struct ttm_object_file;
-struct ttm_object_device;
-
-/**
- * struct ttm_base_object
- *
- * @hash: hash entry for the per-device object hash.
- * @type: derived type this object is base class for.
- * @shareable: Other ttm_object_files can access this object.
- *
- * @tfile: Pointer to ttm_object_file of the creator.
- * NULL if the object was not created by a user request.
- * (kernel object).
- *
- * @refcount: Number of references to this object, not
- * including the hash entry. A reference to a base object can
- * only be held by a ref object.
- *
- * @refcount_release: A function to be called when there are
- * no more references to this object. This function should
- * destroy the object (or make sure destruction eventually happens),
- * and when it is called, the object has
- * already been taken out of the per-device hash. The parameter
- * "base" should be set to NULL by the function.
- *
- * @ref_obj_release: A function to be called when a reference object
- * with another ttm_ref_type than TTM_REF_USAGE is deleted.
- * This function may, for example, release a lock held by a user-space
- * process.
- *
- * This struct is intended to be used as a base struct for objects that
- * are visible to user-space. It provides a global name, race-safe
- * access and refcounting, minimal access contol and hooks for unref actions.
- */
-
-struct ttm_base_object {
-       struct rcu_head rhead;
-       struct drm_hash_item hash;
-       enum ttm_object_type object_type;
-       bool shareable;
-       struct ttm_object_file *tfile;
-       struct kref refcount;
-       void (*refcount_release) (struct ttm_base_object **base);
-       void (*ref_obj_release) (struct ttm_base_object *base,
-                                enum ttm_ref_type ref_type);
-};
-
-
-/**
- * struct ttm_prime_object - Modified base object that is prime-aware
- *
- * @base: struct ttm_base_object that we derive from
- * @mutex: Mutex protecting the @dma_buf member.
- * @size: Size of the dma_buf associated with this object
- * @real_type: Type of the underlying object. Needed since we're setting
- * the value of @base::object_type to ttm_prime_type
- * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
- * object.
- * @refcount_release: The underlying object's release method. Needed since
- * we set @base::refcount_release to our own release method.
- */
-
-struct ttm_prime_object {
-       struct ttm_base_object base;
-       struct mutex mutex;
-       size_t size;
-       enum ttm_object_type real_type;
-       struct dma_buf *dma_buf;
-       void (*refcount_release) (struct ttm_base_object **);
-};
-
-/**
- * ttm_base_object_init
- *
- * @tfile: Pointer to a struct ttm_object_file.
- * @base: The struct ttm_base_object to initialize.
- * @shareable: This object is shareable with other applcations.
- * (different @tfile pointers.)
- * @type: The object type.
- * @refcount_release: See the struct ttm_base_object description.
- * @ref_obj_release: See the struct ttm_base_object description.
- *
- * Initializes a struct ttm_base_object.
- */
-
-extern int ttm_base_object_init(struct ttm_object_file *tfile,
-                               struct ttm_base_object *base,
-                               bool shareable,
-                               enum ttm_object_type type,
-                               void (*refcount_release) (struct ttm_base_object
-                                                         **),
-                               void (*ref_obj_release) (struct ttm_base_object
-                                                        *,
-                                                        enum ttm_ref_type
-                                                        ref_type));
-
-/**
- * ttm_base_object_lookup
- *
- * @tfile: Pointer to a struct ttm_object_file.
- * @key: Hash key
- *
- * Looks up a struct ttm_base_object with the key @key.
- */
-
-extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
-                                                     *tfile, uint32_t key);
-
-/**
- * ttm_base_object_lookup_for_ref
- *
- * @tdev: Pointer to a struct ttm_object_device.
- * @key: Hash key
- *
- * Looks up a struct ttm_base_object with the key @key.
- * This function should only be used when the struct tfile associated with the
- * caller doesn't yet have a reference to the base object.
- */
-
-extern struct ttm_base_object *
-ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key);
-
-/**
- * ttm_base_object_unref
- *
- * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
- *
- * Decrements the base object refcount and clears the pointer pointed to by
- * p_base.
- */
-
-extern void ttm_base_object_unref(struct ttm_base_object **p_base);
-
-/**
- * ttm_ref_object_add.
- *
- * @tfile: A struct ttm_object_file representing the application owning the
- * ref_object.
- * @base: The base object to reference.
- * @ref_type: The type of reference.
- * @existed: Upon completion, indicates that an identical reference object
- * already existed, and the refcount was upped on that object instead.
- * @require_existed: Fail with -EPERM if an identical ref object didn't
- * already exist.
- *
- * Checks that the base object is shareable and adds a ref object to it.
- *
- * Adding a ref object to a base object is basically like referencing the
- * base object, but a user-space application holds the reference. When the
- * file corresponding to @tfile is closed, all its reference objects are
- * deleted. A reference object can have different types depending on what
- * it's intended for. It can be refcounting to prevent object destruction,
- * When user-space takes a lock, it can add a ref object to that lock to
- * make sure the lock is released if the application dies. A ref object
- * will hold a single reference on a base object.
- */
-extern int ttm_ref_object_add(struct ttm_object_file *tfile,
-                             struct ttm_base_object *base,
-                             enum ttm_ref_type ref_type, bool *existed,
-                             bool require_existed);
-
-extern bool ttm_ref_object_exists(struct ttm_object_file *tfile,
-                                 struct ttm_base_object *base);
-
-/**
- * ttm_ref_object_base_unref
- *
- * @key: Key representing the base object.
- * @ref_type: Ref type of the ref object to be dereferenced.
- *
- * Unreference a ref object with type @ref_type
- * on the base object identified by @key. If there are no duplicate
- * references, the ref object will be destroyed and the base object
- * will be unreferenced.
- */
-extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
-                                    unsigned long key,
-                                    enum ttm_ref_type ref_type);
-
-/**
- * ttm_object_file_init - initialize a struct ttm_object file
- *
- * @tdev: A struct ttm_object device this file is initialized on.
- * @hash_order: Order of the hash table used to hold the reference objects.
- *
- * This is typically called by the file_ops::open function.
- */
-
-extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
-                                                   *tdev,
-                                                   unsigned int hash_order);
-
-/**
- * ttm_object_file_release - release data held by a ttm_object_file
- *
- * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
- * *p_tfile will be set to NULL by this function.
- *
- * Releases all data associated by a ttm_object_file.
- * Typically called from file_ops::release. The caller must
- * ensure that there are no concurrent users of tfile.
- */
-
-extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
-
-/**
- * ttm_object device init - initialize a struct ttm_object_device
- *
- * @mem_glob: struct ttm_mem_global for memory accounting.
- * @hash_order: Order of hash table used to hash the base objects.
- * @ops: DMA buf ops for prime objects of this device.
- *
- * This function is typically called on device initialization to prepare
- * data structures needed for ttm base and ref objects.
- */
-
-extern struct ttm_object_device *
-ttm_object_device_init(struct ttm_mem_global *mem_glob,
-                      unsigned int hash_order,
-                      const struct dma_buf_ops *ops);
-
-/**
- * ttm_object_device_release - release data held by a ttm_object_device
- *
- * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
- * *p_tdev will be set to NULL by this function.
- *
- * Releases all data associated by a ttm_object_device.
- * Typically called from driver::unload before the destruction of the
- * device private data structure.
- */
-
-extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
-
-#define ttm_base_object_kfree(__object, __base)\
-       kfree_rcu(__object, __base.rhead)
-
-extern int ttm_prime_object_init(struct ttm_object_file *tfile,
-                                size_t size,
-                                struct ttm_prime_object *prime,
-                                bool shareable,
-                                enum ttm_object_type type,
-                                void (*refcount_release)
-                                (struct ttm_base_object **),
-                                void (*ref_obj_release)
-                                (struct ttm_base_object *,
-                                 enum ttm_ref_type ref_type));
-
-static inline enum ttm_object_type
-ttm_base_object_type(struct ttm_base_object *base)
-{
-       return (base->object_type == ttm_prime_type) ?
-               container_of(base, struct ttm_prime_object, base)->real_type :
-               base->object_type;
-}
-extern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
-                                 int fd, u32 *handle);
-extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
-                                 uint32_t handle, uint32_t flags,
-                                 int *prime_fd);
-
-#define ttm_prime_object_kfree(__obj, __prime)         \
-       kfree_rcu(__obj, __prime.base.rhead)
-#endif