1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include "freedreno_drmif.h"
38 #include "freedreno_priv.h"
39
40 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
41
42 struct fd_device * kgsl_device_new(int fd);
43 struct fd_device * msm_device_new(int fd);
44
45 static void
add_bucket(struct fd_device * dev,int size)46 add_bucket(struct fd_device *dev, int size)
47 {
48 unsigned int i = dev->num_buckets;
49
50 assert(i < ARRAY_SIZE(dev->cache_bucket));
51
52 list_inithead(&dev->cache_bucket[i].list);
53 dev->cache_bucket[i].size = size;
54 dev->num_buckets++;
55 }
56
57 static void
init_cache_buckets(struct fd_device * dev)58 init_cache_buckets(struct fd_device *dev)
59 {
60 unsigned long size, cache_max_size = 64 * 1024 * 1024;
61
62 /* OK, so power of two buckets was too wasteful of memory.
63 * Give 3 other sizes between each power of two, to hopefully
64 * cover things accurately enough. (The alternative is
65 * probably to just go for exact matching of sizes, and assume
66 * that for things like composited window resize the tiled
67 * width/height alignment and rounding of sizes to pages will
68 * get us useful cache hit rates anyway)
69 */
70 add_bucket(dev, 4096);
71 add_bucket(dev, 4096 * 2);
72 add_bucket(dev, 4096 * 3);
73
74 /* Initialize the linked lists for BO reuse cache. */
75 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
76 add_bucket(dev, size);
77 add_bucket(dev, size + size * 1 / 4);
78 add_bucket(dev, size + size * 2 / 4);
79 add_bucket(dev, size + size * 3 / 4);
80 }
81 }
82
fd_device_new(int fd)83 struct fd_device * fd_device_new(int fd)
84 {
85 struct fd_device *dev;
86 drmVersionPtr version;
87
88 /* figure out if we are kgsl or msm drm driver: */
89 version = drmGetVersion(fd);
90 if (!version) {
91 ERROR_MSG("cannot get version: %s", strerror(errno));
92 return NULL;
93 }
94
95 if (!strcmp(version->name, "msm")) {
96 DEBUG_MSG("msm DRM device");
97 dev = msm_device_new(fd);
98 #ifdef HAVE_FREEDRENO_KGSL
99 } else if (!strcmp(version->name, "kgsl")) {
100 DEBUG_MSG("kgsl DRM device");
101 dev = kgsl_device_new(fd);
102 #endif
103 } else {
104 ERROR_MSG("unknown device: %s", version->name);
105 dev = NULL;
106 }
107 drmFreeVersion(version);
108
109 if (!dev)
110 return NULL;
111
112 atomic_set(&dev->refcnt, 1);
113 dev->fd = fd;
114 dev->handle_table = drmHashCreate();
115 dev->name_table = drmHashCreate();
116 init_cache_buckets(dev);
117
118 return dev;
119 }
120
121 /* like fd_device_new() but creates it's own private dup() of the fd
122 * which is close()d when the device is finalized.
123 */
fd_device_new_dup(int fd)124 struct fd_device * fd_device_new_dup(int fd)
125 {
126 struct fd_device *dev = fd_device_new(dup(fd));
127 if (dev)
128 dev->closefd = 1;
129 return dev;
130 }
131
fd_device_ref(struct fd_device * dev)132 struct fd_device * fd_device_ref(struct fd_device *dev)
133 {
134 atomic_inc(&dev->refcnt);
135 return dev;
136 }
137
fd_device_del_impl(struct fd_device * dev)138 static void fd_device_del_impl(struct fd_device *dev)
139 {
140 fd_cleanup_bo_cache(dev, 0);
141 drmHashDestroy(dev->handle_table);
142 drmHashDestroy(dev->name_table);
143 if (dev->closefd)
144 close(dev->fd);
145 dev->funcs->destroy(dev);
146 }
147
fd_device_del_locked(struct fd_device * dev)148 drm_private void fd_device_del_locked(struct fd_device *dev)
149 {
150 if (!atomic_dec_and_test(&dev->refcnt))
151 return;
152 fd_device_del_impl(dev);
153 }
154
fd_device_del(struct fd_device * dev)155 void fd_device_del(struct fd_device *dev)
156 {
157 if (!atomic_dec_and_test(&dev->refcnt))
158 return;
159 pthread_mutex_lock(&table_lock);
160 fd_device_del_impl(dev);
161 pthread_mutex_unlock(&table_lock);
162 }
163
fd_device_fd(struct fd_device * dev)164 int fd_device_fd(struct fd_device *dev)
165 {
166 return dev->fd;
167 }
168