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 <unistd.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 
31 #include "util/os_file.h"
32 
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
35 
36 struct fd_device *msm_device_new(int fd, drmVersionPtr version);
37 
38 struct fd_device *
fd_device_new(int fd)39 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, version);
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 =
81       _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
82    dev->name_table =
83       _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    list_inithead(&dev->deferred_submits);
88    simple_mtx_init(&dev->submit_lock, mtx_plain);
89 
90    return dev;
91 }
92 
93 /* like fd_device_new() but creates it's own private dup() of the fd
94  * which is close()d when the device is finalized.
95  */
96 struct fd_device *
fd_device_new_dup(int fd)97 fd_device_new_dup(int fd)
98 {
99    int dup_fd = os_dupfd_cloexec(fd);
100    struct fd_device *dev = fd_device_new(dup_fd);
101    if (dev)
102       dev->closefd = 1;
103    else
104       close(dup_fd);
105    return dev;
106 }
107 
108 struct fd_device *
fd_device_ref(struct fd_device * dev)109 fd_device_ref(struct fd_device *dev)
110 {
111    p_atomic_inc(&dev->refcnt);
112    return dev;
113 }
114 
115 void
fd_device_purge(struct fd_device * dev)116 fd_device_purge(struct fd_device *dev)
117 {
118    simple_mtx_lock(&table_lock);
119    fd_bo_cache_cleanup(&dev->bo_cache, 0);
120    fd_bo_cache_cleanup(&dev->ring_cache, 0);
121    simple_mtx_unlock(&table_lock);
122 }
123 
124 static void
fd_device_del_impl(struct fd_device * dev)125 fd_device_del_impl(struct fd_device *dev)
126 {
127    int close_fd = dev->closefd ? dev->fd : -1;
128 
129    simple_mtx_assert_locked(&table_lock);
130 
131    assert(list_is_empty(&dev->deferred_submits));
132 
133    fd_bo_cache_cleanup(&dev->bo_cache, 0);
134    fd_bo_cache_cleanup(&dev->ring_cache, 0);
135    _mesa_hash_table_destroy(dev->handle_table, NULL);
136    _mesa_hash_table_destroy(dev->name_table, NULL);
137    dev->funcs->destroy(dev);
138    if (close_fd >= 0)
139       close(close_fd);
140 }
141 
142 void
fd_device_del_locked(struct fd_device * dev)143 fd_device_del_locked(struct fd_device *dev)
144 {
145    if (!p_atomic_dec_zero(&dev->refcnt))
146       return;
147    fd_device_del_impl(dev);
148 }
149 
150 void
fd_device_del(struct fd_device * dev)151 fd_device_del(struct fd_device *dev)
152 {
153    if (!p_atomic_dec_zero(&dev->refcnt))
154       return;
155    simple_mtx_lock(&table_lock);
156    fd_device_del_impl(dev);
157    simple_mtx_unlock(&table_lock);
158 }
159 
160 int
fd_device_fd(struct fd_device * dev)161 fd_device_fd(struct fd_device *dev)
162 {
163    return dev->fd;
164 }
165 
166 enum fd_version
fd_device_version(struct fd_device * dev)167 fd_device_version(struct fd_device *dev)
168 {
169    return dev->version;
170 }
171 
172 bool
fd_dbg(void)173 fd_dbg(void)
174 {
175    static int dbg;
176 
177    if (!dbg)
178       dbg = getenv("LIBGL_DEBUG") ? 1 : -1;
179 
180    return dbg == 1;
181 }
182 
183 bool
fd_has_syncobj(struct fd_device * dev)184 fd_has_syncobj(struct fd_device *dev)
185 {
186    uint64_t value;
187    if (drmGetCap(dev->fd, DRM_CAP_SYNCOBJ, &value))
188       return false;
189    return value && dev->version >= FD_VERSION_FENCE_FD;
190 }
191