freedreno: add fd_device_new_dup()
[platform/upstream/libdrm.git] / freedreno / freedreno_device.c
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 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
35
36 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
37 static void * dev_table;
38
39 struct fd_device * kgsl_device_new(int fd);
40 struct fd_device * msm_device_new(int fd);
41
42 static void
43 add_bucket(struct fd_device *dev, int size)
44 {
45         unsigned int i = dev->num_buckets;
46
47         assert(i < ARRAY_SIZE(dev->cache_bucket));
48
49         list_inithead(&dev->cache_bucket[i].list);
50         dev->cache_bucket[i].size = size;
51         dev->num_buckets++;
52 }
53
54 static void
55 init_cache_buckets(struct fd_device *dev)
56 {
57         unsigned long size, cache_max_size = 64 * 1024 * 1024;
58
59         /* OK, so power of two buckets was too wasteful of memory.
60          * Give 3 other sizes between each power of two, to hopefully
61          * cover things accurately enough.  (The alternative is
62          * probably to just go for exact matching of sizes, and assume
63          * that for things like composited window resize the tiled
64          * width/height alignment and rounding of sizes to pages will
65          * get us useful cache hit rates anyway)
66          */
67         add_bucket(dev, 4096);
68         add_bucket(dev, 4096 * 2);
69         add_bucket(dev, 4096 * 3);
70
71         /* Initialize the linked lists for BO reuse cache. */
72         for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
73                 add_bucket(dev, size);
74                 add_bucket(dev, size + size * 1 / 4);
75                 add_bucket(dev, size + size * 2 / 4);
76                 add_bucket(dev, size + size * 3 / 4);
77         }
78 }
79
80 static struct fd_device * fd_device_new_impl(int fd)
81 {
82         struct fd_device *dev;
83         drmVersionPtr version;
84
85         /* figure out if we are kgsl or msm drm driver: */
86         version = drmGetVersion(fd);
87         if (!version) {
88                 ERROR_MSG("cannot get version: %s", strerror(errno));
89                 return NULL;
90         }
91
92         if (!strcmp(version->name, "kgsl")) {
93                 DEBUG_MSG("kgsl DRM device");
94                 dev = kgsl_device_new(fd);
95         } else if (!strcmp(version->name, "msm")) {
96                 DEBUG_MSG("msm DRM device");
97                 dev = msm_device_new(fd);
98         } else {
99                 ERROR_MSG("unknown device: %s", version->name);
100                 dev = NULL;
101         }
102
103         if (!dev)
104                 return NULL;
105
106         atomic_set(&dev->refcnt, 1);
107         dev->fd = fd;
108         dev->handle_table = drmHashCreate();
109         dev->name_table = drmHashCreate();
110         init_cache_buckets(dev);
111
112         return dev;
113 }
114
115 struct fd_device * fd_device_new(int fd)
116 {
117         struct fd_device *dev = NULL;
118         int key = fd;
119
120         pthread_mutex_lock(&table_lock);
121
122         if (!dev_table)
123                 dev_table = drmHashCreate();
124
125         if (drmHashLookup(dev_table, key, (void **)&dev)) {
126                 dev = fd_device_new_impl(fd);
127                 if (dev)
128                         drmHashInsert(dev_table, key, dev);
129         } else {
130                 dev = fd_device_ref(dev);
131         }
132
133         pthread_mutex_unlock(&table_lock);
134
135         return dev;
136 }
137
138 /* like fd_device_new() but creates it's own private dup() of the fd
139  * which is close()d when the device is finalized.
140  */
141 struct fd_device * fd_device_new_dup(int fd)
142 {
143         struct fd_device *dev = fd_device_new(dup(fd));
144         dev->closefd = 1;
145         return dev;
146 }
147
148 struct fd_device * fd_device_ref(struct fd_device *dev)
149 {
150         atomic_inc(&dev->refcnt);
151         return dev;
152 }
153
154 static void fd_device_del_impl(struct fd_device *dev)
155 {
156         fd_cleanup_bo_cache(dev, 0);
157         drmHashDestroy(dev->handle_table);
158         drmHashDestroy(dev->name_table);
159         drmHashDelete(dev_table, dev->fd);
160         if (dev->closefd)
161                 close(dev->fd);
162         dev->funcs->destroy(dev);
163 }
164
165 void fd_device_del_locked(struct fd_device *dev)
166 {
167         if (!atomic_dec_and_test(&dev->refcnt))
168                 return;
169         fd_device_del_impl(dev);
170 }
171
172 void fd_device_del(struct fd_device *dev)
173 {
174         if (!atomic_dec_and_test(&dev->refcnt))
175                 return;
176         pthread_mutex_lock(&table_lock);
177         fd_device_del_impl(dev);
178         pthread_mutex_unlock(&table_lock);
179 }