freedreno: add dmabuf import/export helpers
[platform/upstream/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 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
33
34 static void bo_del(struct fd_bo *bo);
35
36 /* set buffer name, and add to table, call w/ table_lock held: */
37 static void set_name(struct fd_bo *bo, uint32_t name)
38 {
39         bo->name = name;
40         /* add ourself into the handle table: */
41         drmHashInsert(bo->dev->name_table, name, bo);
42 }
43
44 /* lookup a buffer, call w/ table_lock held: */
45 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
46 {
47         struct fd_bo *bo = NULL;
48         if (!drmHashLookup(tbl, key, (void **)&bo)) {
49                 /* found, incr refcnt and return: */
50                 bo = fd_bo_ref(bo);
51         }
52         return bo;
53 }
54
55 /* allocate a new buffer object, call w/ table_lock held */
56 static struct fd_bo * bo_from_handle(struct fd_device *dev,
57                 uint32_t size, uint32_t handle)
58 {
59         struct fd_bo *bo;
60
61         bo = dev->funcs->bo_from_handle(dev, size, handle);
62         if (!bo) {
63                 struct drm_gem_close req = {
64                                 .handle = handle,
65                 };
66                 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
67                 return NULL;
68         }
69         bo->dev = fd_device_ref(dev);
70         bo->size = size;
71         bo->handle = handle;
72         atomic_set(&bo->refcnt, 1);
73         list_inithead(&bo->list);
74         /* add ourself into the handle table: */
75         drmHashInsert(dev->handle_table, handle, bo);
76         return bo;
77 }
78
79 /* Frees older cached buffers.  Called under table_lock */
80 void fd_cleanup_bo_cache(struct fd_device *dev, time_t time)
81 {
82         int i;
83
84         if (dev->time == time)
85                 return;
86
87         for (i = 0; i < dev->num_buckets; i++) {
88                 struct fd_bo_bucket *bucket = &dev->cache_bucket[i];
89                 struct fd_bo *bo;
90
91                 while (!LIST_IS_EMPTY(&bucket->list)) {
92                         bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
93
94                         /* keep things in cache for at least 1 second: */
95                         if (time && ((time - bo->free_time) <= 1))
96                                 break;
97
98                         list_del(&bo->list);
99                         bo_del(bo);
100                 }
101         }
102
103         dev->time = time;
104 }
105
106 static struct fd_bo_bucket * get_bucket(struct fd_device *dev, uint32_t size)
107 {
108         int i;
109
110         /* hmm, this is what intel does, but I suppose we could calculate our
111          * way to the correct bucket size rather than looping..
112          */
113         for (i = 0; i < dev->num_buckets; i++) {
114                 struct fd_bo_bucket *bucket = &dev->cache_bucket[i];
115                 if (bucket->size >= size) {
116                         return bucket;
117                 }
118         }
119
120         return NULL;
121 }
122
123 static int is_idle(struct fd_bo *bo)
124 {
125         return fd_bo_cpu_prep(bo, NULL,
126                         DRM_FREEDRENO_PREP_READ |
127                         DRM_FREEDRENO_PREP_WRITE |
128                         DRM_FREEDRENO_PREP_NOSYNC) == 0;
129 }
130
131 static struct fd_bo *find_in_bucket(struct fd_device *dev,
132                 struct fd_bo_bucket *bucket, uint32_t flags)
133 {
134         struct fd_bo *bo = NULL;
135
136         /* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
137          * skip the busy check.. if it is only going to be a render target
138          * then we probably don't need to stall..
139          *
140          * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
141          * (MRU, since likely to be in GPU cache), rather than head (LRU)..
142          */
143         pthread_mutex_lock(&table_lock);
144         while (!LIST_IS_EMPTY(&bucket->list)) {
145                 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
146                 if (0 /* TODO: if madvise tells us bo is gone... */) {
147                         list_del(&bo->list);
148                         bo_del(bo);
149                         bo = NULL;
150                         continue;
151                 }
152                 /* TODO check for compatible flags? */
153                 if (is_idle(bo)) {
154                         list_del(&bo->list);
155                         break;
156                 }
157                 bo = NULL;
158                 break;
159         }
160         pthread_mutex_unlock(&table_lock);
161
162         return bo;
163 }
164
165
166 drm_public struct fd_bo *
167 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
168 {
169         struct fd_bo *bo = NULL;
170         struct fd_bo_bucket *bucket;
171         uint32_t handle;
172         int ret;
173
174         size = ALIGN(size, 4096);
175         bucket = get_bucket(dev, size);
176
177         /* see if we can be green and recycle: */
178         if (bucket) {
179                 size = bucket->size;
180                 bo = find_in_bucket(dev, bucket, flags);
181                 if (bo) {
182                         atomic_set(&bo->refcnt, 1);
183                         fd_device_ref(bo->dev);
184                         return bo;
185                 }
186         }
187
188         ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
189         if (ret)
190                 return NULL;
191
192         pthread_mutex_lock(&table_lock);
193         bo = bo_from_handle(dev, size, handle);
194         bo->bo_reuse = 1;
195         pthread_mutex_unlock(&table_lock);
196
197         return bo;
198 }
199
200 drm_public struct fd_bo *
201 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
202 {
203         struct fd_bo *bo = NULL;
204
205         pthread_mutex_lock(&table_lock);
206         bo = bo_from_handle(dev, size, handle);
207         pthread_mutex_unlock(&table_lock);
208
209         return bo;
210 }
211
212 drm_public struct fd_bo *
213 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
214 {
215         struct drm_prime_handle req = {
216                         .fd = fd,
217         };
218         int ret, size;
219
220         ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
221         if (ret) {
222                 return NULL;
223         }
224
225         /* hmm, would be nice if we had a way to figure out the size.. */
226         size = 0;
227
228         return fd_bo_from_handle(dev, req.handle, size);
229 }
230
231 drm_public struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
232 {
233         struct drm_gem_open req = {
234                         .name = name,
235         };
236         struct fd_bo *bo;
237
238         pthread_mutex_lock(&table_lock);
239
240         /* check name table first, to see if bo is already open: */
241         bo = lookup_bo(dev->name_table, name);
242         if (bo)
243                 goto out_unlock;
244
245         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
246                 ERROR_MSG("gem-open failed: %s", strerror(errno));
247                 goto out_unlock;
248         }
249
250         bo = lookup_bo(dev->handle_table, req.handle);
251         if (bo)
252                 goto out_unlock;
253
254         bo = bo_from_handle(dev, req.size, req.handle);
255         if (bo)
256                 set_name(bo, name);
257
258 out_unlock:
259         pthread_mutex_unlock(&table_lock);
260
261         return bo;
262 }
263
264 drm_public struct fd_bo * fd_bo_ref(struct fd_bo *bo)
265 {
266         atomic_inc(&bo->refcnt);
267         return bo;
268 }
269
270 drm_public void fd_bo_del(struct fd_bo *bo)
271 {
272         struct fd_device *dev = bo->dev;
273
274         if (!atomic_dec_and_test(&bo->refcnt))
275                 return;
276
277         if (bo->fd) {
278                 close(bo->fd);
279                 bo->fd = 0;
280         }
281
282         pthread_mutex_lock(&table_lock);
283
284         if (bo->bo_reuse) {
285                 struct fd_bo_bucket *bucket = get_bucket(dev, bo->size);
286
287                 /* see if we can be green and recycle: */
288                 if (bucket) {
289                         struct timespec time;
290
291                         clock_gettime(CLOCK_MONOTONIC, &time);
292
293                         bo->free_time = time.tv_sec;
294                         list_addtail(&bo->list, &bucket->list);
295                         fd_cleanup_bo_cache(dev, time.tv_sec);
296
297                         /* bo's in the bucket cache don't have a ref and
298                          * don't hold a ref to the dev:
299                          */
300
301                         goto out;
302                 }
303         }
304
305         bo_del(bo);
306 out:
307         fd_device_del_locked(dev);
308         pthread_mutex_unlock(&table_lock);
309 }
310
311 /* Called under table_lock */
312 static void bo_del(struct fd_bo *bo)
313 {
314         if (bo->map)
315                 munmap(bo->map, bo->size);
316
317         /* TODO probably bo's in bucket list get removed from
318          * handle table??
319          */
320
321         if (bo->handle) {
322                 struct drm_gem_close req = {
323                                 .handle = bo->handle,
324                 };
325                 drmHashDelete(bo->dev->handle_table, bo->handle);
326                 if (bo->name)
327                         drmHashDelete(bo->dev->name_table, bo->name);
328                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
329         }
330
331         bo->funcs->destroy(bo);
332 }
333
334 drm_public int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
335 {
336         if (!bo->name) {
337                 struct drm_gem_flink req = {
338                                 .handle = bo->handle,
339                 };
340                 int ret;
341
342                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
343                 if (ret) {
344                         return ret;
345                 }
346
347                 pthread_mutex_lock(&table_lock);
348                 set_name(bo, req.name);
349                 pthread_mutex_unlock(&table_lock);
350         }
351
352         *name = bo->name;
353
354         return 0;
355 }
356
357 drm_public uint32_t fd_bo_handle(struct fd_bo *bo)
358 {
359         return bo->handle;
360 }
361
362 drm_public int fd_bo_dmabuf(struct fd_bo *bo)
363 {
364         if (!bo->fd) {
365                 struct drm_prime_handle req = {
366                                 .handle = bo->handle,
367                                 .flags = DRM_CLOEXEC,
368                 };
369                 int ret;
370
371                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
372                 if (ret) {
373                         return ret;
374                 }
375
376                 bo->fd = req.fd;
377         }
378         return dup(bo->fd);
379 }
380
381 drm_public uint32_t fd_bo_size(struct fd_bo *bo)
382 {
383         return bo->size;
384 }
385
386 drm_public void * fd_bo_map(struct fd_bo *bo)
387 {
388         if (!bo->map) {
389                 uint64_t offset;
390                 int ret;
391
392                 ret = bo->funcs->offset(bo, &offset);
393                 if (ret) {
394                         return NULL;
395                 }
396
397                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
398                                 bo->dev->fd, offset);
399                 if (bo->map == MAP_FAILED) {
400                         ERROR_MSG("mmap failed: %s", strerror(errno));
401                         bo->map = NULL;
402                 }
403         }
404         return bo->map;
405 }
406
407 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
408 drm_public int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
409 {
410         return bo->funcs->cpu_prep(bo, pipe, op);
411 }
412
413 drm_public void fd_bo_cpu_fini(struct fd_bo *bo)
414 {
415         bo->funcs->cpu_fini(bo);
416 }