OSDN Git Service

freedreno: expose kernel driver version
[android-x86/external-libdrm.git] / freedreno / freedreno_device.c
index 03553b9..b99bce2 100644 (file)
  *    Rob Clark <robclark@freedesktop.org>
  */
 
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include "freedreno_priv.h"
 
 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
-static void * dev_table;
 
 struct fd_device * kgsl_device_new(int fd);
+struct fd_device * msm_device_new(int fd);
 
-static struct fd_device * fd_device_new_impl(int fd)
+struct fd_device * fd_device_new(int fd)
 {
        struct fd_device *dev;
        drmVersionPtr version;
@@ -50,14 +54,30 @@ static struct fd_device * fd_device_new_impl(int fd)
                return NULL;
        }
 
-       if (!strcmp(version->name, "kgsl")) {
+       if (!strcmp(version->name, "msm")) {
+               DEBUG_MSG("msm DRM device");
+               if (version->version_major != 1) {
+                       ERROR_MSG("unsupported version: %u.%u.%u", version->version_major,
+                               version->version_minor, version->version_patchlevel);
+                       dev = NULL;
+                       goto out;
+               }
+
+               dev = msm_device_new(fd);
+               dev->version = version->version_minor;
+#ifdef HAVE_FREEDRENO_KGSL
+       } else if (!strcmp(version->name, "kgsl")) {
                DEBUG_MSG("kgsl DRM device");
                dev = kgsl_device_new(fd);
+#endif
        } else {
                ERROR_MSG("unknown device: %s", version->name);
                dev = NULL;
        }
 
+out:
+       drmFreeVersion(version);
+
        if (!dev)
                return NULL;
 
@@ -65,30 +85,22 @@ static struct fd_device * fd_device_new_impl(int fd)
        dev->fd = fd;
        dev->handle_table = drmHashCreate();
        dev->name_table = drmHashCreate();
+       fd_bo_cache_init(&dev->bo_cache);
 
        return dev;
 }
 
-struct fd_device * fd_device_new(int fd)
+/* like fd_device_new() but creates it's own private dup() of the fd
+ * which is close()d when the device is finalized.
+ */
+struct fd_device * fd_device_new_dup(int fd)
 {
-       struct fd_device *dev = NULL;
-       int key = fd;
-
-       pthread_mutex_lock(&table_lock);
-
-       if (!dev_table)
-               dev_table = drmHashCreate();
-
-       if (drmHashLookup(dev_table, key, (void **)&dev)) {
-               dev = fd_device_new_impl(fd);
-               if (dev)
-                       drmHashInsert(dev_table, key, dev);
-       } else {
-               dev = fd_device_ref(dev);
-       }
-
-       pthread_mutex_unlock(&table_lock);
-
+       int dup_fd = dup(fd);
+       struct fd_device *dev = fd_device_new(dup_fd);
+       if (dev)
+               dev->closefd = 1;
+       else
+               close(dup_fd);
        return dev;
 }
 
@@ -98,14 +110,38 @@ struct fd_device * fd_device_ref(struct fd_device *dev)
        return dev;
 }
 
+static void fd_device_del_impl(struct fd_device *dev)
+{
+       fd_bo_cache_cleanup(&dev->bo_cache, 0);
+       drmHashDestroy(dev->handle_table);
+       drmHashDestroy(dev->name_table);
+       if (dev->closefd)
+               close(dev->fd);
+       dev->funcs->destroy(dev);
+}
+
+drm_private void fd_device_del_locked(struct fd_device *dev)
+{
+       if (!atomic_dec_and_test(&dev->refcnt))
+               return;
+       fd_device_del_impl(dev);
+}
+
 void fd_device_del(struct fd_device *dev)
 {
        if (!atomic_dec_and_test(&dev->refcnt))
                return;
        pthread_mutex_lock(&table_lock);
-       drmHashDestroy(dev->handle_table);
-       drmHashDestroy(dev->name_table);
-       drmHashDelete(dev_table, dev->fd);
+       fd_device_del_impl(dev);
        pthread_mutex_unlock(&table_lock);
-       dev->funcs->destroy(dev);
+}
+
+int fd_device_fd(struct fd_device *dev)
+{
+       return dev->fd;
+}
+
+enum fd_version fd_device_version(struct fd_device *dev)
+{
+       return dev->version;
 }