OSDN Git Service

freedreno: prevent deadlock in error path
[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                 pthread_mutex_unlock(&table_lock);
142                 return NULL;
143         }
144
145         bo = lookup_bo(dev->handle_table, handle);
146         if (bo)
147                 goto out_unlock;
148
149         /* lseek() to get bo size */
150         size = lseek(fd, 0, SEEK_END);
151         lseek(fd, 0, SEEK_CUR);
152
153         bo = bo_from_handle(dev, size, handle);
154
155         VG_BO_ALLOC(bo);
156
157 out_unlock:
158         pthread_mutex_unlock(&table_lock);
159
160         return bo;
161 }
162
163 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
164 {
165         struct drm_gem_open req = {
166                         .name = name,
167         };
168         struct fd_bo *bo;
169
170         pthread_mutex_lock(&table_lock);
171
172         /* check name table first, to see if bo is already open: */
173         bo = lookup_bo(dev->name_table, name);
174         if (bo)
175                 goto out_unlock;
176
177         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
178                 ERROR_MSG("gem-open failed: %s", strerror(errno));
179                 goto out_unlock;
180         }
181
182         bo = lookup_bo(dev->handle_table, req.handle);
183         if (bo)
184                 goto out_unlock;
185
186         bo = bo_from_handle(dev, req.size, req.handle);
187         if (bo) {
188                 set_name(bo, name);
189                 VG_BO_ALLOC(bo);
190         }
191
192 out_unlock:
193         pthread_mutex_unlock(&table_lock);
194
195         return bo;
196 }
197
198 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
199 {
200         atomic_inc(&bo->refcnt);
201         return bo;
202 }
203
204 void fd_bo_del(struct fd_bo *bo)
205 {
206         struct fd_device *dev = bo->dev;
207
208         if (!atomic_dec_and_test(&bo->refcnt))
209                 return;
210
211         pthread_mutex_lock(&table_lock);
212
213         if (bo->bo_reuse && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
214                 goto out;
215
216         bo_del(bo);
217         fd_device_del_locked(dev);
218 out:
219         pthread_mutex_unlock(&table_lock);
220 }
221
222 /* Called under table_lock */
223 drm_private void bo_del(struct fd_bo *bo)
224 {
225         VG_BO_FREE(bo);
226
227         if (bo->map)
228                 drm_munmap(bo->map, bo->size);
229
230         /* TODO probably bo's in bucket list get removed from
231          * handle table??
232          */
233
234         if (bo->handle) {
235                 struct drm_gem_close req = {
236                                 .handle = bo->handle,
237                 };
238                 drmHashDelete(bo->dev->handle_table, bo->handle);
239                 if (bo->name)
240                         drmHashDelete(bo->dev->name_table, bo->name);
241                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
242         }
243
244         bo->funcs->destroy(bo);
245 }
246
247 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
248 {
249         if (!bo->name) {
250                 struct drm_gem_flink req = {
251                                 .handle = bo->handle,
252                 };
253                 int ret;
254
255                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
256                 if (ret) {
257                         return ret;
258                 }
259
260                 pthread_mutex_lock(&table_lock);
261                 set_name(bo, req.name);
262                 pthread_mutex_unlock(&table_lock);
263                 bo->bo_reuse = FALSE;
264         }
265
266         *name = bo->name;
267
268         return 0;
269 }
270
271 uint32_t fd_bo_handle(struct fd_bo *bo)
272 {
273         return bo->handle;
274 }
275
276 int fd_bo_dmabuf(struct fd_bo *bo)
277 {
278         int ret, prime_fd;
279
280         ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
281                         &prime_fd);
282         if (ret) {
283                 ERROR_MSG("failed to get dmabuf fd: %d", ret);
284                 return ret;
285         }
286
287         bo->bo_reuse = FALSE;
288
289         return prime_fd;
290 }
291
292 uint32_t fd_bo_size(struct fd_bo *bo)
293 {
294         return bo->size;
295 }
296
297 void * fd_bo_map(struct fd_bo *bo)
298 {
299         if (!bo->map) {
300                 uint64_t offset;
301                 int ret;
302
303                 ret = bo->funcs->offset(bo, &offset);
304                 if (ret) {
305                         return NULL;
306                 }
307
308                 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
309                                 bo->dev->fd, offset);
310                 if (bo->map == MAP_FAILED) {
311                         ERROR_MSG("mmap failed: %s", strerror(errno));
312                         bo->map = NULL;
313                 }
314         }
315         return bo->map;
316 }
317
318 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
319 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
320 {
321         return bo->funcs->cpu_prep(bo, pipe, op);
322 }
323
324 void fd_bo_cpu_fini(struct fd_bo *bo)
325 {
326         bo->funcs->cpu_fini(bo);
327 }
328
329 #ifndef HAVE_FREEDRENO_KGSL
330 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe, int fbfd, uint32_t size)
331 {
332     return NULL;
333 }
334 #endif