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 struct fd_bo * fd_bo_new(struct fd_device *dev,
167 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 struct fd_bo *fd_bo_from_handle(struct fd_device *dev,
201 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 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
214 struct drm_gem_open req = {
219 pthread_mutex_lock(&table_lock);
221 /* check name table first, to see if bo is already open: */
222 bo = lookup_bo(dev->name_table, name);
226 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
227 ERROR_MSG("gem-open failed: %s", strerror(errno));
231 bo = lookup_bo(dev->handle_table, req.handle);
235 bo = bo_from_handle(dev, req.size, req.handle);
240 pthread_mutex_unlock(&table_lock);
245 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
247 atomic_inc(&bo->refcnt);
251 void fd_bo_del(struct fd_bo *bo)
253 struct fd_device *dev = bo->dev;
255 if (!atomic_dec_and_test(&bo->refcnt))
258 pthread_mutex_lock(&table_lock);
261 struct fd_bo_bucket *bucket = get_bucket(dev, bo->size);
263 /* see if we can be green and recycle: */
265 struct timespec time;
267 clock_gettime(CLOCK_MONOTONIC, &time);
269 bo->free_time = time.tv_sec;
270 list_addtail(&bo->list, &bucket->list);
271 fd_cleanup_bo_cache(dev, time.tv_sec);
273 /* bo's in the bucket cache don't have a ref and
274 * don't hold a ref to the dev:
283 fd_device_del_locked(dev);
284 pthread_mutex_unlock(&table_lock);
287 /* Called under table_lock */
288 static void bo_del(struct fd_bo *bo)
291 munmap(bo->map, bo->size);
293 /* TODO probably bo's in bucket list get removed from
298 struct drm_gem_close req = {
299 .handle = bo->handle,
301 drmHashDelete(bo->dev->handle_table, bo->handle);
303 drmHashDelete(bo->dev->name_table, bo->name);
304 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
307 bo->funcs->destroy(bo);
310 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
313 struct drm_gem_flink req = {
314 .handle = bo->handle,
318 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
323 pthread_mutex_lock(&table_lock);
324 set_name(bo, req.name);
325 pthread_mutex_unlock(&table_lock);
333 uint32_t fd_bo_handle(struct fd_bo *bo)
338 uint32_t fd_bo_size(struct fd_bo *bo)
343 void * fd_bo_map(struct fd_bo *bo)
349 ret = bo->funcs->offset(bo, &offset);
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));
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)
367 return bo->funcs->cpu_prep(bo, pipe, op);
370 void fd_bo_cpu_fini(struct fd_bo *bo)
372 bo->funcs->cpu_fini(bo);