1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include <unistd.h>
28bf215546Sopenharmony_ci#include <sys/stat.h>
29bf215546Sopenharmony_ci#include <sys/types.h>
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "util/os_file.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "freedreno_drmif.h"
34bf215546Sopenharmony_ci#include "freedreno_priv.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistruct fd_device *msm_device_new(int fd, drmVersionPtr version);
37bf215546Sopenharmony_ci#if HAVE_FREEDRENO_VIRTIO
38bf215546Sopenharmony_cistruct fd_device *virtio_device_new(int fd, drmVersionPtr version);
39bf215546Sopenharmony_ci#endif
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistruct fd_device *
42bf215546Sopenharmony_cifd_device_new(int fd)
43bf215546Sopenharmony_ci{
44bf215546Sopenharmony_ci   struct fd_device *dev = NULL;
45bf215546Sopenharmony_ci   drmVersionPtr version;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   /* figure out if we are kgsl or msm drm driver: */
48bf215546Sopenharmony_ci   version = drmGetVersion(fd);
49bf215546Sopenharmony_ci   if (!version) {
50bf215546Sopenharmony_ci      ERROR_MSG("cannot get version: %s", strerror(errno));
51bf215546Sopenharmony_ci      return NULL;
52bf215546Sopenharmony_ci   }
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   if (!strcmp(version->name, "msm")) {
55bf215546Sopenharmony_ci      DEBUG_MSG("msm DRM device");
56bf215546Sopenharmony_ci      if (version->version_major != 1) {
57bf215546Sopenharmony_ci         ERROR_MSG("unsupported version: %u.%u.%u", version->version_major,
58bf215546Sopenharmony_ci                   version->version_minor, version->version_patchlevel);
59bf215546Sopenharmony_ci         goto out;
60bf215546Sopenharmony_ci      }
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci      dev = msm_device_new(fd, version);
63bf215546Sopenharmony_ci#if HAVE_FREEDRENO_VIRTIO
64bf215546Sopenharmony_ci   } else if (!strcmp(version->name, "virtio_gpu")) {
65bf215546Sopenharmony_ci      DEBUG_MSG("virtio_gpu DRM device");
66bf215546Sopenharmony_ci      dev = virtio_device_new(fd, version);
67bf215546Sopenharmony_ci#endif
68bf215546Sopenharmony_ci#if HAVE_FREEDRENO_KGSL
69bf215546Sopenharmony_ci   } else if (!strcmp(version->name, "kgsl")) {
70bf215546Sopenharmony_ci      DEBUG_MSG("kgsl DRM device");
71bf215546Sopenharmony_ci      dev = kgsl_device_new(fd);
72bf215546Sopenharmony_ci#endif
73bf215546Sopenharmony_ci   }
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci   if (!dev) {
76bf215546Sopenharmony_ci      INFO_MSG("unsupported device: %s", version->name);
77bf215546Sopenharmony_ci      goto out;
78bf215546Sopenharmony_ci   }
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ciout:
81bf215546Sopenharmony_ci   drmFreeVersion(version);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   if (!dev)
84bf215546Sopenharmony_ci      return NULL;
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   p_atomic_set(&dev->refcnt, 1);
87bf215546Sopenharmony_ci   dev->fd = fd;
88bf215546Sopenharmony_ci   dev->handle_table =
89bf215546Sopenharmony_ci      _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
90bf215546Sopenharmony_ci   dev->name_table =
91bf215546Sopenharmony_ci      _mesa_hash_table_create(NULL, _mesa_hash_u32, _mesa_key_u32_equal);
92bf215546Sopenharmony_ci   fd_bo_cache_init(&dev->bo_cache, false);
93bf215546Sopenharmony_ci   fd_bo_cache_init(&dev->ring_cache, true);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   list_inithead(&dev->deferred_submits);
96bf215546Sopenharmony_ci   simple_mtx_init(&dev->submit_lock, mtx_plain);
97bf215546Sopenharmony_ci   simple_mtx_init(&dev->suballoc_lock, mtx_plain);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   return dev;
100bf215546Sopenharmony_ci}
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci/* like fd_device_new() but creates it's own private dup() of the fd
103bf215546Sopenharmony_ci * which is close()d when the device is finalized.
104bf215546Sopenharmony_ci */
105bf215546Sopenharmony_cistruct fd_device *
106bf215546Sopenharmony_cifd_device_new_dup(int fd)
107bf215546Sopenharmony_ci{
108bf215546Sopenharmony_ci   int dup_fd = os_dupfd_cloexec(fd);
109bf215546Sopenharmony_ci   struct fd_device *dev = fd_device_new(dup_fd);
110bf215546Sopenharmony_ci   if (dev)
111bf215546Sopenharmony_ci      dev->closefd = 1;
112bf215546Sopenharmony_ci   else
113bf215546Sopenharmony_ci      close(dup_fd);
114bf215546Sopenharmony_ci   return dev;
115bf215546Sopenharmony_ci}
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci/* Convenience helper to open the drm device and return new fd_device:
118bf215546Sopenharmony_ci */
119bf215546Sopenharmony_cistruct fd_device *
120bf215546Sopenharmony_cifd_device_open(void)
121bf215546Sopenharmony_ci{
122bf215546Sopenharmony_ci   int fd;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   fd = drmOpenWithType("msm", NULL, DRM_NODE_RENDER);
125bf215546Sopenharmony_ci#if HAVE_FREEDRENO_VIRTIO
126bf215546Sopenharmony_ci   if (fd < 0)
127bf215546Sopenharmony_ci      fd = drmOpenWithType("virtio_gpu", NULL, DRM_NODE_RENDER);
128bf215546Sopenharmony_ci#endif
129bf215546Sopenharmony_ci   if (fd < 0)
130bf215546Sopenharmony_ci      return NULL;
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci   return fd_device_new(fd);
133bf215546Sopenharmony_ci}
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_cistruct fd_device *
136bf215546Sopenharmony_cifd_device_ref(struct fd_device *dev)
137bf215546Sopenharmony_ci{
138bf215546Sopenharmony_ci   p_atomic_inc(&dev->refcnt);
139bf215546Sopenharmony_ci   return dev;
140bf215546Sopenharmony_ci}
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_civoid
143bf215546Sopenharmony_cifd_device_purge(struct fd_device *dev)
144bf215546Sopenharmony_ci{
145bf215546Sopenharmony_ci   simple_mtx_lock(&table_lock);
146bf215546Sopenharmony_ci   fd_bo_cache_cleanup(&dev->bo_cache, 0);
147bf215546Sopenharmony_ci   fd_bo_cache_cleanup(&dev->ring_cache, 0);
148bf215546Sopenharmony_ci   simple_mtx_unlock(&table_lock);
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_cistatic void
152bf215546Sopenharmony_cifd_device_del_impl(struct fd_device *dev)
153bf215546Sopenharmony_ci{
154bf215546Sopenharmony_ci   simple_mtx_assert_locked(&table_lock);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   assert(list_is_empty(&dev->deferred_submits));
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci   if (dev->suballoc_bo)
159bf215546Sopenharmony_ci      fd_bo_del_locked(dev->suballoc_bo);
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   fd_bo_cache_cleanup(&dev->bo_cache, 0);
162bf215546Sopenharmony_ci   fd_bo_cache_cleanup(&dev->ring_cache, 0);
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   /* Needs to be after bo cache cleanup in case backend has a
165bf215546Sopenharmony_ci    * util_vma_heap that it destroys:
166bf215546Sopenharmony_ci    */
167bf215546Sopenharmony_ci   dev->funcs->destroy(dev);
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci   _mesa_hash_table_destroy(dev->handle_table, NULL);
170bf215546Sopenharmony_ci   _mesa_hash_table_destroy(dev->name_table, NULL);
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   if (util_queue_is_initialized(&dev->submit_queue))
173bf215546Sopenharmony_ci      util_queue_destroy(&dev->submit_queue);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   if (dev->closefd)
176bf215546Sopenharmony_ci      close(dev->fd);
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   free(dev);
179bf215546Sopenharmony_ci}
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_civoid
182bf215546Sopenharmony_cifd_device_del_locked(struct fd_device *dev)
183bf215546Sopenharmony_ci{
184bf215546Sopenharmony_ci   if (!p_atomic_dec_zero(&dev->refcnt))
185bf215546Sopenharmony_ci      return;
186bf215546Sopenharmony_ci   fd_device_del_impl(dev);
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_civoid
190bf215546Sopenharmony_cifd_device_del(struct fd_device *dev)
191bf215546Sopenharmony_ci{
192bf215546Sopenharmony_ci   if (!p_atomic_dec_zero(&dev->refcnt))
193bf215546Sopenharmony_ci      return;
194bf215546Sopenharmony_ci   simple_mtx_lock(&table_lock);
195bf215546Sopenharmony_ci   fd_device_del_impl(dev);
196bf215546Sopenharmony_ci   simple_mtx_unlock(&table_lock);
197bf215546Sopenharmony_ci}
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ciint
200bf215546Sopenharmony_cifd_device_fd(struct fd_device *dev)
201bf215546Sopenharmony_ci{
202bf215546Sopenharmony_ci   return dev->fd;
203bf215546Sopenharmony_ci}
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_cienum fd_version
206bf215546Sopenharmony_cifd_device_version(struct fd_device *dev)
207bf215546Sopenharmony_ci{
208bf215546Sopenharmony_ci   return dev->version;
209bf215546Sopenharmony_ci}
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ciDEBUG_GET_ONCE_BOOL_OPTION(libgl, "LIBGL_DEBUG", false)
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_cibool
214bf215546Sopenharmony_cifd_dbg(void)
215bf215546Sopenharmony_ci{
216bf215546Sopenharmony_ci   return debug_get_option_libgl();
217bf215546Sopenharmony_ci}
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_cibool
220bf215546Sopenharmony_cifd_has_syncobj(struct fd_device *dev)
221bf215546Sopenharmony_ci{
222bf215546Sopenharmony_ci   uint64_t value;
223bf215546Sopenharmony_ci   if (drmGetCap(dev->fd, DRM_CAP_SYNCOBJ, &value))
224bf215546Sopenharmony_ci      return false;
225bf215546Sopenharmony_ci   return value && dev->version >= FD_VERSION_FENCE_FD;
226bf215546Sopenharmony_ci}
227