1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
33 #include <sys/types.h>
37 #include "freedreno_drmif.h"
38 #include "freedreno_priv.h"
40 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
42 struct fd_device * kgsl_device_new(int fd);
43 struct fd_device * msm_device_new(int fd);
45 struct fd_device * fd_device_new(int fd)
47 struct fd_device *dev;
48 drmVersionPtr version;
50 /* figure out if we are kgsl or msm drm driver: */
51 version = drmGetVersion(fd);
53 ERROR_MSG("cannot get version: %s", strerror(errno));
57 if (!strcmp(version->name, "msm")) {
58 DEBUG_MSG("msm DRM device");
59 dev = msm_device_new(fd);
60 #ifdef HAVE_FREEDRENO_KGSL
61 } else if (!strcmp(version->name, "kgsl")) {
62 DEBUG_MSG("kgsl DRM device");
63 dev = kgsl_device_new(fd);
66 ERROR_MSG("unknown device: %s", version->name);
69 drmFreeVersion(version);
74 atomic_set(&dev->refcnt, 1);
76 dev->handle_table = drmHashCreate();
77 dev->name_table = drmHashCreate();
78 fd_bo_cache_init(&dev->bo_cache);
83 /* like fd_device_new() but creates it's own private dup() of the fd
84 * which is close()d when the device is finalized.
86 struct fd_device * fd_device_new_dup(int fd)
88 struct fd_device *dev = fd_device_new(dup(fd));
94 struct fd_device * fd_device_ref(struct fd_device *dev)
96 atomic_inc(&dev->refcnt);
100 static void fd_device_del_impl(struct fd_device *dev)
102 fd_bo_cache_cleanup(&dev->bo_cache, 0);
103 drmHashDestroy(dev->handle_table);
104 drmHashDestroy(dev->name_table);
107 dev->funcs->destroy(dev);
110 drm_private void fd_device_del_locked(struct fd_device *dev)
112 if (!atomic_dec_and_test(&dev->refcnt))
114 fd_device_del_impl(dev);
117 void fd_device_del(struct fd_device *dev)
119 if (!atomic_dec_and_test(&dev->refcnt))
121 pthread_mutex_lock(&table_lock);
122 fd_device_del_impl(dev);
123 pthread_mutex_unlock(&table_lock);
126 int fd_device_fd(struct fd_device *dev)