1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
36 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
38 static void bo_del(struct fd_bo *bo);
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)
44 /* add ourself into the handle table: */
45 drmHashInsert(bo->dev->name_table, name, bo);
48 /* lookup a buffer, call w/ table_lock held: */
49 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
51 struct fd_bo *bo = NULL;
52 if (!drmHashLookup(tbl, key, (void **)&bo)) {
53 /* found, incr refcnt and return: */
59 /* allocate a new buffer object, call w/ table_lock held */
60 static struct fd_bo * bo_from_handle(struct fd_device *dev,
61 uint32_t size, uint32_t handle)
65 bo = dev->funcs->bo_from_handle(dev, size, handle);
67 struct drm_gem_close req = {
70 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
73 bo->dev = fd_device_ref(dev);
76 atomic_set(&bo->refcnt, 1);
77 list_inithead(&bo->list);
78 /* add ourself into the handle table: */
79 drmHashInsert(dev->handle_table, handle, bo);
83 /* Frees older cached buffers. Called under table_lock */
84 void fd_cleanup_bo_cache(struct fd_device *dev, time_t time)
88 if (dev->time == time)
91 for (i = 0; i < dev->num_buckets; i++) {
92 struct fd_bo_bucket *bucket = &dev->cache_bucket[i];
95 while (!LIST_IS_EMPTY(&bucket->list)) {
96 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
98 /* keep things in cache for at least 1 second: */
99 if (time && ((time - bo->free_time) <= 1))
110 static struct fd_bo_bucket * get_bucket(struct fd_device *dev, uint32_t size)
114 /* hmm, this is what intel does, but I suppose we could calculate our
115 * way to the correct bucket size rather than looping..
117 for (i = 0; i < dev->num_buckets; i++) {
118 struct fd_bo_bucket *bucket = &dev->cache_bucket[i];
119 if (bucket->size >= size) {
127 static int is_idle(struct fd_bo *bo)
129 return fd_bo_cpu_prep(bo, NULL,
130 DRM_FREEDRENO_PREP_READ |
131 DRM_FREEDRENO_PREP_WRITE |
132 DRM_FREEDRENO_PREP_NOSYNC) == 0;
135 static struct fd_bo *find_in_bucket(struct fd_device *dev,
136 struct fd_bo_bucket *bucket, uint32_t flags)
138 struct fd_bo *bo = NULL;
140 /* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
141 * skip the busy check.. if it is only going to be a render target
142 * then we probably don't need to stall..
144 * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
145 * (MRU, since likely to be in GPU cache), rather than head (LRU)..
147 pthread_mutex_lock(&table_lock);
148 while (!LIST_IS_EMPTY(&bucket->list)) {
149 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
150 if (0 /* TODO: if madvise tells us bo is gone... */) {
156 /* TODO check for compatible flags? */
164 pthread_mutex_unlock(&table_lock);
170 drm_public struct fd_bo *
171 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
173 struct fd_bo *bo = NULL;
174 struct fd_bo_bucket *bucket;
178 size = ALIGN(size, 4096);
179 bucket = get_bucket(dev, size);
181 /* see if we can be green and recycle: */
184 bo = find_in_bucket(dev, bucket, flags);
186 atomic_set(&bo->refcnt, 1);
187 fd_device_ref(bo->dev);
192 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
196 pthread_mutex_lock(&table_lock);
197 bo = bo_from_handle(dev, size, handle);
199 pthread_mutex_unlock(&table_lock);
204 drm_public struct fd_bo *
205 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
207 struct fd_bo *bo = NULL;
209 pthread_mutex_lock(&table_lock);
210 bo = bo_from_handle(dev, size, handle);
211 pthread_mutex_unlock(&table_lock);
216 drm_public struct fd_bo *
217 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
219 struct drm_prime_handle req = {
224 ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
229 /* hmm, would be nice if we had a way to figure out the size.. */
232 return fd_bo_from_handle(dev, req.handle, size);
235 drm_public struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
237 struct drm_gem_open req = {
242 pthread_mutex_lock(&table_lock);
244 /* check name table first, to see if bo is already open: */
245 bo = lookup_bo(dev->name_table, name);
249 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
250 ERROR_MSG("gem-open failed: %s", strerror(errno));
254 bo = lookup_bo(dev->handle_table, req.handle);
258 bo = bo_from_handle(dev, req.size, req.handle);
263 pthread_mutex_unlock(&table_lock);
268 drm_public struct fd_bo * fd_bo_ref(struct fd_bo *bo)
270 atomic_inc(&bo->refcnt);
274 drm_public void fd_bo_del(struct fd_bo *bo)
276 struct fd_device *dev = bo->dev;
278 if (!atomic_dec_and_test(&bo->refcnt))
286 pthread_mutex_lock(&table_lock);
289 struct fd_bo_bucket *bucket = get_bucket(dev, bo->size);
291 /* see if we can be green and recycle: */
293 struct timespec time;
295 clock_gettime(CLOCK_MONOTONIC, &time);
297 bo->free_time = time.tv_sec;
298 list_addtail(&bo->list, &bucket->list);
299 fd_cleanup_bo_cache(dev, time.tv_sec);
301 /* bo's in the bucket cache don't have a ref and
302 * don't hold a ref to the dev:
311 fd_device_del_locked(dev);
312 pthread_mutex_unlock(&table_lock);
315 /* Called under table_lock */
316 static void bo_del(struct fd_bo *bo)
319 drm_munmap(bo->map, bo->size);
321 /* TODO probably bo's in bucket list get removed from
326 struct drm_gem_close req = {
327 .handle = bo->handle,
329 drmHashDelete(bo->dev->handle_table, bo->handle);
331 drmHashDelete(bo->dev->name_table, bo->name);
332 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
335 bo->funcs->destroy(bo);
338 drm_public int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
341 struct drm_gem_flink req = {
342 .handle = bo->handle,
346 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
351 pthread_mutex_lock(&table_lock);
352 set_name(bo, req.name);
353 pthread_mutex_unlock(&table_lock);
361 drm_public uint32_t fd_bo_handle(struct fd_bo *bo)
366 drm_public int fd_bo_dmabuf(struct fd_bo *bo)
369 struct drm_prime_handle req = {
370 .handle = bo->handle,
371 .flags = DRM_CLOEXEC,
375 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
385 drm_public uint32_t fd_bo_size(struct fd_bo *bo)
390 drm_public void * fd_bo_map(struct fd_bo *bo)
396 ret = bo->funcs->offset(bo, &offset);
401 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
402 bo->dev->fd, offset);
403 if (bo->map == MAP_FAILED) {
404 ERROR_MSG("mmap failed: %s", strerror(errno));
411 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
412 drm_public int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
414 return bo->funcs->cpu_prep(bo, pipe, op);
417 drm_public void fd_bo_cpu_fini(struct fd_bo *bo)
419 bo->funcs->cpu_fini(bo);