1031aaf1beff719b96ebc01bf32be0c45012d697
[mesa.git] / src / freedreno / drm / freedreno_device.c
1 /*
2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include "freedreno_drmif.h"
32 #include "freedreno_priv.h"
33
34 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
35
36 struct fd_device * kgsl_device_new(int fd);
37 struct fd_device * msm_device_new(int fd);
38
39 struct fd_device * fd_device_new(int fd)
40 {
41 struct fd_device *dev;
42 drmVersionPtr version;
43
44 /* figure out if we are kgsl or msm drm driver: */
45 version = drmGetVersion(fd);
46 if (!version) {
47 ERROR_MSG("cannot get version: %s", strerror(errno));
48 return NULL;
49 }
50
51 if (!strcmp(version->name, "msm")) {
52 DEBUG_MSG("msm DRM device");
53 if (version->version_major != 1) {
54 ERROR_MSG("unsupported version: %u.%u.%u", version->version_major,
55 version->version_minor, version->version_patchlevel);
56 dev = NULL;
57 goto out;
58 }
59
60 dev = msm_device_new(fd);
61 dev->version = version->version_minor;
62 #if HAVE_FREEDRENO_KGSL
63 } else if (!strcmp(version->name, "kgsl")) {
64 DEBUG_MSG("kgsl DRM device");
65 dev = kgsl_device_new(fd);
66 #endif
67 } else {
68 ERROR_MSG("unknown device: %s", version->name);
69 dev = NULL;
70 }
71
72 out:
73 drmFreeVersion(version);
74
75 if (!dev)
76 return NULL;
77
78 p_atomic_set(&dev->refcnt, 1);
79 dev->fd = fd;
80 dev->handle_table = _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
81 dev->name_table = _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
82 fd_bo_cache_init(&dev->bo_cache, FALSE);
83 fd_bo_cache_init(&dev->ring_cache, TRUE);
84
85 return dev;
86 }
87
88 /* like fd_device_new() but creates it's own private dup() of the fd
89 * which is close()d when the device is finalized.
90 */
91 struct fd_device * fd_device_new_dup(int fd)
92 {
93 int dup_fd = dup(fd);
94 struct fd_device *dev = fd_device_new(dup_fd);
95 if (dev)
96 dev->closefd = 1;
97 else
98 close(dup_fd);
99 return dev;
100 }
101
102 struct fd_device * fd_device_ref(struct fd_device *dev)
103 {
104 p_atomic_inc(&dev->refcnt);
105 return dev;
106 }
107
108 static void fd_device_del_impl(struct fd_device *dev)
109 {
110 int close_fd = dev->closefd ? dev->fd : -1;
111 fd_bo_cache_cleanup(&dev->bo_cache, 0);
112 _mesa_hash_table_destroy(dev->handle_table, NULL);
113 _mesa_hash_table_destroy(dev->name_table, NULL);
114 dev->funcs->destroy(dev);
115 if (close_fd >= 0)
116 close(close_fd);
117 }
118
119 void fd_device_del_locked(struct fd_device *dev)
120 {
121 if (!atomic_dec_and_test(&dev->refcnt))
122 return;
123 fd_device_del_impl(dev);
124 }
125
126 void fd_device_del(struct fd_device *dev)
127 {
128 if (!atomic_dec_and_test(&dev->refcnt))
129 return;
130 pthread_mutex_lock(&table_lock);
131 fd_device_del_impl(dev);
132 pthread_mutex_unlock(&table_lock);
133 }
134
135 int fd_device_fd(struct fd_device *dev)
136 {
137 return dev->fd;
138 }
139
140 enum fd_version fd_device_version(struct fd_device *dev)
141 {
142 return dev->version;
143 }
144
145 bool fd_dbg(void)
146 {
147 static int dbg;
148
149 if (!dbg)
150 dbg = getenv("LIBGL_DEBUG") ? 1 : -1;
151
152 return dbg == 1;
153 }