OSDN Git Service

freedreno: valgrind support
[android-x86/external-libdrm.git] / freedreno / freedreno_bo.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 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
35
36 drm_private pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
37 drm_private void bo_del(struct fd_bo *bo);
38
39 /* set buffer name, and add to table, call w/ table_lock held: */
40 static void set_name(struct fd_bo *bo, uint32_t name)
41 {
42         bo->name = name;
43         /* add ourself into the handle table: */
44         drmHashInsert(bo->dev->name_table, name, bo);
45 }
46
47 /* lookup a buffer, call w/ table_lock held: */
48 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
49 {
50         struct fd_bo *bo = NULL;
51         if (!drmHashLookup(tbl, key, (void **)&bo)) {
52                 /* found, incr refcnt and return: */
53                 bo = fd_bo_ref(bo);
54
55                 /* don't break the bucket if this bo was found in one */
56                 list_delinit(&bo->list);
57         }
58         return bo;
59 }
60
61 /* allocate a new buffer object, call w/ table_lock held */
62 static struct fd_bo * bo_from_handle(struct fd_device *dev,
63                 uint32_t size, uint32_t handle)
64 {
65         struct fd_bo *bo;
66
67         bo = dev->funcs->bo_from_handle(dev, size, handle);
68         if (!bo) {
69                 struct drm_gem_close req = {
70                                 .handle = handle,
71                 };
72                 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
73                 return NULL;
74         }
75         bo->dev = fd_device_ref(dev);
76         bo->size = size;
77         bo->handle = handle;
78         atomic_set(&bo->refcnt, 1);
79         list_inithead(&bo->list);
80         /* add ourself into the handle table: */
81         drmHashInsert(dev->handle_table, handle, bo);
82         return bo;
83 }
84
85 struct fd_bo *
86 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
87 {
88         struct fd_bo *bo = NULL;
89         uint32_t handle;
90         int ret;
91
92         bo = fd_bo_cache_alloc(&dev->bo_cache, &size, flags);
93         if (bo)
94                 return bo;
95
96         ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
97         if (ret)
98                 return NULL;
99
100         pthread_mutex_lock(&table_lock);
101         bo = bo_from_handle(dev, size, handle);
102         bo->bo_reuse = TRUE;
103         pthread_mutex_unlock(&table_lock);
104
105         VG_BO_ALLOC(bo);
106
107         return bo;
108 }
109
110 struct fd_bo *
111 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
112 {
113         struct fd_bo *bo = NULL;
114
115         pthread_mutex_lock(&table_lock);
116
117         bo = lookup_bo(dev->handle_table, handle);
118         if (bo)
119                 goto out_unlock;
120
121         bo = bo_from_handle(dev, size, handle);
122
123         VG_BO_ALLOC(bo);
124
125 out_unlock:
126         pthread_mutex_unlock(&table_lock);
127
128         return bo;
129 }
130
131 struct fd_bo *
132 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
133 {
134         int ret, size;
135         uint32_t handle;
136         struct fd_bo *bo;
137
138         pthread_mutex_lock(&table_lock);
139         ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
140         if (ret) {
141                 return NULL;
142         }
143
144         bo = lookup_bo(dev->handle_table, handle);
145         if (bo)
146                 goto out_unlock;
147
148         /* lseek() to get bo size */
149         size = lseek(fd, 0, SEEK_END);
150         lseek(fd, 0, SEEK_CUR);
151
152         bo = bo_from_handle(dev, size, handle);
153
154         VG_BO_ALLOC(bo);
155
156 out_unlock:
157         pthread_mutex_unlock(&table_lock);
158
159         return bo;
160 }
161
162 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
163 {
164         struct drm_gem_open req = {
165                         .name = name,
166         };
167         struct fd_bo *bo;
168
169         pthread_mutex_lock(&table_lock);
170
171         /* check name table first, to see if bo is already open: */
172         bo = lookup_bo(dev->name_table, name);
173         if (bo)
174                 goto out_unlock;
175
176         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
177                 ERROR_MSG("gem-open failed: %s", strerror(errno));
178                 goto out_unlock;
179         }
180
181         bo = lookup_bo(dev->handle_table, req.handle);
182         if (bo)
183                 goto out_unlock;
184
185         bo = bo_from_handle(dev, req.size, req.handle);
186         if (bo) {
187                 set_name(bo, name);
188                 VG_BO_ALLOC(bo);
189         }
190
191 out_unlock:
192         pthread_mutex_unlock(&table_lock);
193
194         return bo;
195 }
196
197 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
198 {
199         atomic_inc(&bo->refcnt);
200         return bo;
201 }
202
203 void fd_bo_del(struct fd_bo *bo)
204 {
205         struct fd_device *dev = bo->dev;
206
207         if (!atomic_dec_and_test(&bo->refcnt))
208                 return;
209
210         pthread_mutex_lock(&table_lock);
211
212         if (bo->bo_reuse && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
213                 goto out;
214
215         bo_del(bo);
216         fd_device_del_locked(dev);
217 out:
218         pthread_mutex_unlock(&table_lock);
219 }
220
221 /* Called under table_lock */
222 drm_private void bo_del(struct fd_bo *bo)
223 {
224         VG_BO_FREE(bo);
225
226         if (bo->map)
227                 drm_munmap(bo->map, bo->size);
228
229         /* TODO probably bo's in bucket list get removed from
230          * handle table??
231          */
232
233         if (bo->handle) {
234                 struct drm_gem_close req = {
235                                 .handle = bo->handle,
236                 };
237                 drmHashDelete(bo->dev->handle_table, bo->handle);
238                 if (bo->name)
239                         drmHashDelete(bo->dev->name_table, bo->name);
240                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
241         }
242
243         bo->funcs->destroy(bo);
244 }
245
246 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
247 {
248         if (!bo->name) {
249                 struct drm_gem_flink req = {
250                                 .handle = bo->handle,
251                 };
252                 int ret;
253
254                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
255                 if (ret) {
256                         return ret;
257                 }
258
259                 pthread_mutex_lock(&table_lock);
260                 set_name(bo, req.name);
261                 pthread_mutex_unlock(&table_lock);
262                 bo->bo_reuse = FALSE;
263         }
264
265         *name = bo->name;
266
267         return 0;
268 }
269
270 uint32_t fd_bo_handle(struct fd_bo *bo)
271 {
272         return bo->handle;
273 }
274
275 int fd_bo_dmabuf(struct fd_bo *bo)
276 {
277         int ret, prime_fd;
278
279         ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
280                         &prime_fd);
281         if (ret) {
282                 ERROR_MSG("failed to get dmabuf fd: %d", ret);
283                 return ret;
284         }
285
286         bo->bo_reuse = FALSE;
287
288         return prime_fd;
289 }
290
291 uint32_t fd_bo_size(struct fd_bo *bo)
292 {
293         return bo->size;
294 }
295
296 void * fd_bo_map(struct fd_bo *bo)
297 {
298         if (!bo->map) {
299                 uint64_t offset;
300                 int ret;
301
302                 ret = bo->funcs->offset(bo, &offset);
303                 if (ret) {
304                         return NULL;
305                 }
306
307                 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
308                                 bo->dev->fd, offset);
309                 if (bo->map == MAP_FAILED) {
310                         ERROR_MSG("mmap failed: %s", strerror(errno));
311                         bo->map = NULL;
312                 }
313         }
314         return bo->map;
315 }
316
317 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
318 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
319 {
320         return bo->funcs->cpu_prep(bo, pipe, op);
321 }
322
323 void fd_bo_cpu_fini(struct fd_bo *bo)
324 {
325         bo->funcs->cpu_fini(bo);
326 }
327
328 #ifndef HAVE_FREEDRENO_KGSL
329 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe, int fbfd, uint32_t size)
330 {
331     return NULL;
332 }
333 #endif