OSDN Git Service

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