8cea4ded9a207a24ea95e0dde924ecce506f9d8d
[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 struct fd_bo * fd_bo_new(struct fd_device *dev,
167                 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 struct fd_bo *fd_bo_from_handle(struct fd_device *dev,
201                 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 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
213 {
214         struct drm_gem_open req = {
215                         .name = name,
216         };
217         struct fd_bo *bo;
218
219         pthread_mutex_lock(&table_lock);
220
221         /* check name table first, to see if bo is already open: */
222         bo = lookup_bo(dev->name_table, name);
223         if (bo)
224                 goto out_unlock;
225
226         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
227                 ERROR_MSG("gem-open failed: %s", strerror(errno));
228                 goto out_unlock;
229         }
230
231         bo = lookup_bo(dev->handle_table, req.handle);
232         if (bo)
233                 goto out_unlock;
234
235         bo = bo_from_handle(dev, req.size, req.handle);
236         if (bo)
237                 set_name(bo, name);
238
239 out_unlock:
240         pthread_mutex_unlock(&table_lock);
241
242         return bo;
243 }
244
245 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
246 {
247         atomic_inc(&bo->refcnt);
248         return bo;
249 }
250
251 void fd_bo_del(struct fd_bo *bo)
252 {
253         struct fd_device *dev = bo->dev;
254
255         if (!atomic_dec_and_test(&bo->refcnt))
256                 return;
257
258         pthread_mutex_lock(&table_lock);
259
260         if (bo->bo_reuse) {
261                 struct fd_bo_bucket *bucket = get_bucket(dev, bo->size);
262
263                 /* see if we can be green and recycle: */
264                 if (bucket) {
265                         struct timespec time;
266
267                         clock_gettime(CLOCK_MONOTONIC, &time);
268
269                         bo->free_time = time.tv_sec;
270                         list_addtail(&bo->list, &bucket->list);
271                         fd_cleanup_bo_cache(dev, time.tv_sec);
272
273                         /* bo's in the bucket cache don't have a ref and
274                          * don't hold a ref to the dev:
275                          */
276
277                         goto out;
278                 }
279         }
280
281         bo_del(bo);
282 out:
283         fd_device_del_locked(dev);
284         pthread_mutex_unlock(&table_lock);
285 }
286
287 /* Called under table_lock */
288 static void bo_del(struct fd_bo *bo)
289 {
290         if (bo->map)
291                 munmap(bo->map, bo->size);
292
293         /* TODO probably bo's in bucket list get removed from
294          * handle table??
295          */
296
297         if (bo->handle) {
298                 struct drm_gem_close req = {
299                                 .handle = bo->handle,
300                 };
301                 drmHashDelete(bo->dev->handle_table, bo->handle);
302                 if (bo->name)
303                         drmHashDelete(bo->dev->name_table, bo->name);
304                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
305         }
306
307         bo->funcs->destroy(bo);
308 }
309
310 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
311 {
312         if (!bo->name) {
313                 struct drm_gem_flink req = {
314                                 .handle = bo->handle,
315                 };
316                 int ret;
317
318                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
319                 if (ret) {
320                         return ret;
321                 }
322
323                 pthread_mutex_lock(&table_lock);
324                 set_name(bo, req.name);
325                 pthread_mutex_unlock(&table_lock);
326         }
327
328         *name = bo->name;
329
330         return 0;
331 }
332
333 uint32_t fd_bo_handle(struct fd_bo *bo)
334 {
335         return bo->handle;
336 }
337
338 uint32_t fd_bo_size(struct fd_bo *bo)
339 {
340         return bo->size;
341 }
342
343 void * fd_bo_map(struct fd_bo *bo)
344 {
345         if (!bo->map) {
346                 uint64_t offset;
347                 int ret;
348
349                 ret = bo->funcs->offset(bo, &offset);
350                 if (ret) {
351                         return NULL;
352                 }
353
354                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
355                                 bo->dev->fd, offset);
356                 if (bo->map == MAP_FAILED) {
357                         ERROR_MSG("mmap failed: %s", strerror(errno));
358                         bo->map = NULL;
359                 }
360         }
361         return bo->map;
362 }
363
364 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
365 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
366 {
367         return bo->funcs->cpu_prep(bo, pipe, op);
368 }
369
370 void fd_bo_cpu_fini(struct fd_bo *bo)
371 {
372         bo->funcs->cpu_fini(bo);
373 }