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: */
56 /* don't break the bucket if this bo was found in one */
57 list_delinit(&bo->list);
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)
68 bo = dev->funcs->bo_from_handle(dev, size, handle);
70 struct drm_gem_close req = {
73 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
76 bo->dev = fd_device_ref(dev);
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);
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)
91 if (cache->time == time)
94 for (i = 0; i < cache->num_buckets; i++) {
95 struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
98 while (!LIST_IS_EMPTY(&bucket->list)) {
99 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
101 /* keep things in cache for at least 1 second: */
102 if (time && ((time - bo->free_time) <= 1))
113 static struct fd_bo_bucket * get_bucket(struct fd_bo_cache *cache, uint32_t size)
117 /* hmm, this is what intel does, but I suppose we could calculate our
118 * way to the correct bucket size rather than looping..
120 for (i = 0; i < cache->num_buckets; i++) {
121 struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
122 if (bucket->size >= size) {
130 static int is_idle(struct fd_bo *bo)
132 return fd_bo_cpu_prep(bo, NULL,
133 DRM_FREEDRENO_PREP_READ |
134 DRM_FREEDRENO_PREP_WRITE |
135 DRM_FREEDRENO_PREP_NOSYNC) == 0;
138 static struct fd_bo *find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)
140 struct fd_bo *bo = NULL;
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..
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)..
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... */) {
158 /* TODO check for compatible flags? */
166 pthread_mutex_unlock(&table_lock);
173 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
175 struct fd_bo *bo = NULL;
176 struct fd_bo_bucket *bucket;
180 size = ALIGN(size, 4096);
181 bucket = get_bucket(&dev->bo_cache, size);
183 /* see if we can be green and recycle: */
186 bo = find_in_bucket(bucket, flags);
188 atomic_set(&bo->refcnt, 1);
189 fd_device_ref(bo->dev);
194 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
198 pthread_mutex_lock(&table_lock);
199 bo = bo_from_handle(dev, size, handle);
201 pthread_mutex_unlock(&table_lock);
207 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
209 struct fd_bo *bo = NULL;
211 pthread_mutex_lock(&table_lock);
213 bo = lookup_bo(dev->handle_table, handle);
217 bo = bo_from_handle(dev, size, handle);
220 pthread_mutex_unlock(&table_lock);
226 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
232 pthread_mutex_lock(&table_lock);
233 ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
238 bo = lookup_bo(dev->handle_table, handle);
242 /* lseek() to get bo size */
243 size = lseek(fd, 0, SEEK_END);
244 lseek(fd, 0, SEEK_CUR);
246 bo = bo_from_handle(dev, size, handle);
249 pthread_mutex_unlock(&table_lock);
254 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
256 struct drm_gem_open req = {
261 pthread_mutex_lock(&table_lock);
263 /* check name table first, to see if bo is already open: */
264 bo = lookup_bo(dev->name_table, name);
268 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
269 ERROR_MSG("gem-open failed: %s", strerror(errno));
273 bo = lookup_bo(dev->handle_table, req.handle);
277 bo = bo_from_handle(dev, req.size, req.handle);
282 pthread_mutex_unlock(&table_lock);
287 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
289 atomic_inc(&bo->refcnt);
293 void fd_bo_del(struct fd_bo *bo)
295 struct fd_device *dev = bo->dev;
297 if (!atomic_dec_and_test(&bo->refcnt))
300 pthread_mutex_lock(&table_lock);
303 struct fd_bo_bucket *bucket = get_bucket(&dev->bo_cache, bo->size);
305 /* see if we can be green and recycle: */
307 struct timespec time;
309 clock_gettime(CLOCK_MONOTONIC, &time);
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);
315 /* bo's in the bucket cache don't have a ref and
316 * don't hold a ref to the dev:
325 fd_device_del_locked(dev);
326 pthread_mutex_unlock(&table_lock);
329 /* Called under table_lock */
330 static void bo_del(struct fd_bo *bo)
333 drm_munmap(bo->map, bo->size);
335 /* TODO probably bo's in bucket list get removed from
340 struct drm_gem_close req = {
341 .handle = bo->handle,
343 drmHashDelete(bo->dev->handle_table, bo->handle);
345 drmHashDelete(bo->dev->name_table, bo->name);
346 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
349 bo->funcs->destroy(bo);
352 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
355 struct drm_gem_flink req = {
356 .handle = bo->handle,
360 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
365 pthread_mutex_lock(&table_lock);
366 set_name(bo, req.name);
367 pthread_mutex_unlock(&table_lock);
376 uint32_t fd_bo_handle(struct fd_bo *bo)
381 int fd_bo_dmabuf(struct fd_bo *bo)
385 ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
388 ERROR_MSG("failed to get dmabuf fd: %d", ret);
397 uint32_t fd_bo_size(struct fd_bo *bo)
402 void * fd_bo_map(struct fd_bo *bo)
408 ret = bo->funcs->offset(bo, &offset);
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));
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)
426 return bo->funcs->cpu_prep(bo, pipe, op);
429 void fd_bo_cpu_fini(struct fd_bo *bo)
431 bo->funcs->cpu_fini(bo);
434 #ifndef HAVE_FREEDRENO_KGSL
435 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe, int fbfd, uint32_t size)