OSDN Git Service

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