2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include <sys/types.h>
31 #include "freedreno_drmif.h"
32 #include "freedreno_priv.h"
34 static pthread_mutex_t table_lock
= PTHREAD_MUTEX_INITIALIZER
;
37 u32_hash(const void *key
)
39 return _mesa_hash_data(key
, sizeof(uint32_t));
43 u32_equals(const void *key1
, const void *key2
)
45 return *(const uint32_t *)key1
== *(const uint32_t *)key2
;
49 struct fd_device
* kgsl_device_new(int fd
);
50 struct fd_device
* msm_device_new(int fd
);
52 struct fd_device
* fd_device_new(int fd
)
54 struct fd_device
*dev
;
55 drmVersionPtr version
;
57 /* figure out if we are kgsl or msm drm driver: */
58 version
= drmGetVersion(fd
);
60 ERROR_MSG("cannot get version: %s", strerror(errno
));
64 if (!strcmp(version
->name
, "msm")) {
65 DEBUG_MSG("msm DRM device");
66 if (version
->version_major
!= 1) {
67 ERROR_MSG("unsupported version: %u.%u.%u", version
->version_major
,
68 version
->version_minor
, version
->version_patchlevel
);
73 dev
= msm_device_new(fd
);
74 dev
->version
= version
->version_minor
;
75 #if HAVE_FREEDRENO_KGSL
76 } else if (!strcmp(version
->name
, "kgsl")) {
77 DEBUG_MSG("kgsl DRM device");
78 dev
= kgsl_device_new(fd
);
81 ERROR_MSG("unknown device: %s", version
->name
);
86 drmFreeVersion(version
);
91 p_atomic_set(&dev
->refcnt
, 1);
93 dev
->handle_table
= _mesa_hash_table_create(NULL
, u32_hash
, u32_equals
);
94 dev
->name_table
= _mesa_hash_table_create(NULL
, u32_hash
, u32_equals
);
95 fd_bo_cache_init(&dev
->bo_cache
, FALSE
);
96 fd_bo_cache_init(&dev
->ring_cache
, TRUE
);
101 /* like fd_device_new() but creates it's own private dup() of the fd
102 * which is close()d when the device is finalized.
104 struct fd_device
* fd_device_new_dup(int fd
)
106 int dup_fd
= dup(fd
);
107 struct fd_device
*dev
= fd_device_new(dup_fd
);
115 struct fd_device
* fd_device_ref(struct fd_device
*dev
)
117 p_atomic_inc(&dev
->refcnt
);
121 static void fd_device_del_impl(struct fd_device
*dev
)
123 int close_fd
= dev
->closefd
? dev
->fd
: -1;
124 fd_bo_cache_cleanup(&dev
->bo_cache
, 0);
125 _mesa_hash_table_destroy(dev
->handle_table
, NULL
);
126 _mesa_hash_table_destroy(dev
->name_table
, NULL
);
127 dev
->funcs
->destroy(dev
);
132 void fd_device_del_locked(struct fd_device
*dev
)
134 if (!atomic_dec_and_test(&dev
->refcnt
))
136 fd_device_del_impl(dev
);
139 void fd_device_del(struct fd_device
*dev
)
141 if (!atomic_dec_and_test(&dev
->refcnt
))
143 pthread_mutex_lock(&table_lock
);
144 fd_device_del_impl(dev
);
145 pthread_mutex_unlock(&table_lock
);
148 int fd_device_fd(struct fd_device
*dev
)
153 enum fd_version
fd_device_version(struct fd_device
*dev
)