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 "util/os_file.h"
32
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
35
36 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
37
38 struct fd_device * kgsl_device_new(int fd);
39 struct fd_device * msm_device_new(int fd);
40
fd_device_new(int fd)41 struct fd_device * fd_device_new(int fd)
42 {
43 struct fd_device *dev;
44 drmVersionPtr version;
45
46 /* figure out if we are kgsl or msm drm driver: */
47 version = drmGetVersion(fd);
48 if (!version) {
49 ERROR_MSG("cannot get version: %s", strerror(errno));
50 return NULL;
51 }
52
53 if (!strcmp(version->name, "msm")) {
54 DEBUG_MSG("msm DRM device");
55 if (version->version_major != 1) {
56 ERROR_MSG("unsupported version: %u.%u.%u", version->version_major,
57 version->version_minor, version->version_patchlevel);
58 dev = NULL;
59 goto out;
60 }
61
62 dev = msm_device_new(fd);
63 dev->version = version->version_minor;
64 #if HAVE_FREEDRENO_KGSL
65 } else if (!strcmp(version->name, "kgsl")) {
66 DEBUG_MSG("kgsl DRM device");
67 dev = kgsl_device_new(fd);
68 #endif
69 } else {
70 ERROR_MSG("unknown device: %s", version->name);
71 dev = NULL;
72 }
73
74 out:
75 drmFreeVersion(version);
76
77 if (!dev)
78 return NULL;
79
80 p_atomic_set(&dev->refcnt, 1);
81 dev->fd = fd;
82 dev->handle_table = _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
83 dev->name_table = _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
84 fd_bo_cache_init(&dev->bo_cache, false);
85 fd_bo_cache_init(&dev->ring_cache, true);
86
87 return dev;
88 }
89
90 /* like fd_device_new() but creates it's own private dup() of the fd
91 * which is close()d when the device is finalized.
92 */
fd_device_new_dup(int fd)93 struct fd_device * fd_device_new_dup(int fd)
94 {
95 int dup_fd = os_dupfd_cloexec(fd);
96 struct fd_device *dev = fd_device_new(dup_fd);
97 if (dev)
98 dev->closefd = 1;
99 else
100 close(dup_fd);
101 return dev;
102 }
103
fd_device_ref(struct fd_device * dev)104 struct fd_device * fd_device_ref(struct fd_device *dev)
105 {
106 p_atomic_inc(&dev->refcnt);
107 return dev;
108 }
109
fd_device_del_impl(struct fd_device * dev)110 static void fd_device_del_impl(struct fd_device *dev)
111 {
112 int close_fd = dev->closefd ? dev->fd : -1;
113 fd_bo_cache_cleanup(&dev->bo_cache, 0);
114 fd_bo_cache_cleanup(&dev->ring_cache, 0);
115 _mesa_hash_table_destroy(dev->handle_table, NULL);
116 _mesa_hash_table_destroy(dev->name_table, NULL);
117 dev->funcs->destroy(dev);
118 if (close_fd >= 0)
119 close(close_fd);
120 }
121
fd_device_del_locked(struct fd_device * dev)122 void fd_device_del_locked(struct fd_device *dev)
123 {
124 if (!atomic_dec_and_test(&dev->refcnt))
125 return;
126 fd_device_del_impl(dev);
127 }
128
fd_device_del(struct fd_device * dev)129 void fd_device_del(struct fd_device *dev)
130 {
131 if (!atomic_dec_and_test(&dev->refcnt))
132 return;
133 pthread_mutex_lock(&table_lock);
134 fd_device_del_impl(dev);
135 pthread_mutex_unlock(&table_lock);
136 }
137
fd_device_fd(struct fd_device * dev)138 int fd_device_fd(struct fd_device *dev)
139 {
140 return dev->fd;
141 }
142
fd_device_version(struct fd_device * dev)143 enum fd_version fd_device_version(struct fd_device *dev)
144 {
145 return dev->version;
146 }
147
fd_dbg(void)148 bool fd_dbg(void)
149 {
150 static int dbg;
151
152 if (!dbg)
153 dbg = getenv("LIBGL_DEBUG") ? 1 : -1;
154
155 return dbg == 1;
156 }
157
fd_has_syncobj(struct fd_device * dev)158 bool fd_has_syncobj(struct fd_device *dev)
159 {
160 uint64_t value;
161 if (drmGetCap(dev->fd, DRM_CAP_SYNCOBJ, &value))
162 return false;
163 return value && dev->version >= FD_VERSION_FENCE_FD;
164 }
165