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>
29 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
32 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
34 static void bo_del(struct fd_bo *bo);
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)
40 /* add ourself into the handle table: */
41 drmHashInsert(bo->dev->name_table, name, bo);
44 /* lookup a buffer, call w/ table_lock held: */
45 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
47 struct fd_bo *bo = NULL;
48 if (!drmHashLookup(tbl, key, (void **)&bo)) {
49 /* found, incr refcnt and return: */
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)
61 bo = dev->funcs->bo_from_handle(dev, size, handle);
63 struct drm_gem_close req = {
66 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
69 bo->dev = fd_device_ref(dev);
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);
79 /* Frees older cached buffers. Called under table_lock */
80 void fd_cleanup_bo_cache(struct fd_device *dev, time_t time)
84 if (dev->time == time)
87 for (i = 0; i < dev->num_buckets; i++) {
88 struct fd_bo_bucket *bucket = &dev->cache_bucket[i];
91 while (!LIST_IS_EMPTY(&bucket->list)) {
92 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
94 /* keep things in cache for at least 1 second: */
95 if (time && ((time - bo->free_time) <= 1))
106 static struct fd_bo_bucket * get_bucket(struct fd_device *dev, uint32_t size)
110 /* hmm, this is what intel does, but I suppose we could calculate our
111 * way to the correct bucket size rather than looping..
113 for (i = 0; i < dev->num_buckets; i++) {
114 struct fd_bo_bucket *bucket = &dev->cache_bucket[i];
115 if (bucket->size >= size) {
123 static int is_idle(struct fd_bo *bo)
125 return fd_bo_cpu_prep(bo, NULL,
126 DRM_FREEDRENO_PREP_READ |
127 DRM_FREEDRENO_PREP_WRITE |
128 DRM_FREEDRENO_PREP_NOSYNC) == 0;
131 static struct fd_bo *find_in_bucket(struct fd_device *dev,
132 struct fd_bo_bucket *bucket, uint32_t flags)
134 struct fd_bo *bo = NULL;
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..
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)..
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... */) {
152 /* TODO check for compatible flags? */
160 pthread_mutex_unlock(&table_lock);
166 drm_public struct fd_bo *
167 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
169 struct fd_bo *bo = NULL;
170 struct fd_bo_bucket *bucket;
174 size = ALIGN(size, 4096);
175 bucket = get_bucket(dev, size);
177 /* see if we can be green and recycle: */
180 bo = find_in_bucket(dev, bucket, flags);
182 atomic_set(&bo->refcnt, 1);
183 fd_device_ref(bo->dev);
188 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
192 pthread_mutex_lock(&table_lock);
193 bo = bo_from_handle(dev, size, handle);
195 pthread_mutex_unlock(&table_lock);
200 drm_public struct fd_bo *
201 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
203 struct fd_bo *bo = NULL;
205 pthread_mutex_lock(&table_lock);
206 bo = bo_from_handle(dev, size, handle);
207 pthread_mutex_unlock(&table_lock);
212 drm_public struct fd_bo *
213 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
215 struct drm_prime_handle req = {
220 ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
225 /* hmm, would be nice if we had a way to figure out the size.. */
228 return fd_bo_from_handle(dev, req.handle, size);
231 drm_public struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
233 struct drm_gem_open req = {
238 pthread_mutex_lock(&table_lock);
240 /* check name table first, to see if bo is already open: */
241 bo = lookup_bo(dev->name_table, name);
245 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
246 ERROR_MSG("gem-open failed: %s", strerror(errno));
250 bo = lookup_bo(dev->handle_table, req.handle);
254 bo = bo_from_handle(dev, req.size, req.handle);
259 pthread_mutex_unlock(&table_lock);
264 drm_public struct fd_bo * fd_bo_ref(struct fd_bo *bo)
266 atomic_inc(&bo->refcnt);
270 drm_public void fd_bo_del(struct fd_bo *bo)
272 struct fd_device *dev = bo->dev;
274 if (!atomic_dec_and_test(&bo->refcnt))
282 pthread_mutex_lock(&table_lock);
285 struct fd_bo_bucket *bucket = get_bucket(dev, bo->size);
287 /* see if we can be green and recycle: */
289 struct timespec time;
291 clock_gettime(CLOCK_MONOTONIC, &time);
293 bo->free_time = time.tv_sec;
294 list_addtail(&bo->list, &bucket->list);
295 fd_cleanup_bo_cache(dev, time.tv_sec);
297 /* bo's in the bucket cache don't have a ref and
298 * don't hold a ref to the dev:
307 fd_device_del_locked(dev);
308 pthread_mutex_unlock(&table_lock);
311 /* Called under table_lock */
312 static void bo_del(struct fd_bo *bo)
315 munmap(bo->map, bo->size);
317 /* TODO probably bo's in bucket list get removed from
322 struct drm_gem_close req = {
323 .handle = bo->handle,
325 drmHashDelete(bo->dev->handle_table, bo->handle);
327 drmHashDelete(bo->dev->name_table, bo->name);
328 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
331 bo->funcs->destroy(bo);
334 drm_public int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
337 struct drm_gem_flink req = {
338 .handle = bo->handle,
342 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
347 pthread_mutex_lock(&table_lock);
348 set_name(bo, req.name);
349 pthread_mutex_unlock(&table_lock);
357 drm_public uint32_t fd_bo_handle(struct fd_bo *bo)
362 drm_public int fd_bo_dmabuf(struct fd_bo *bo)
365 struct drm_prime_handle req = {
366 .handle = bo->handle,
367 .flags = DRM_CLOEXEC,
371 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
381 drm_public uint32_t fd_bo_size(struct fd_bo *bo)
386 drm_public void * fd_bo_map(struct fd_bo *bo)
392 ret = bo->funcs->offset(bo, &offset);
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));
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)
410 return bo->funcs->cpu_prep(bo, pipe, op);
413 drm_public void fd_bo_cpu_fini(struct fd_bo *bo)
415 bo->funcs->cpu_fini(bo);