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 drm_private pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
33 drm_private void bo_del(struct fd_bo *bo);
35 /* set buffer name, and add to table, call w/ table_lock held: */
36 static void set_name(struct fd_bo *bo, uint32_t name)
39 /* add ourself into the handle table: */
40 drmHashInsert(bo->dev->name_table, name, bo);
43 /* lookup a buffer, call w/ table_lock held: */
44 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
46 struct fd_bo *bo = NULL;
47 if (!drmHashLookup(tbl, key, (void **)&bo)) {
48 /* found, incr refcnt and return: */
51 /* don't break the bucket if this bo was found in one */
52 list_delinit(&bo->list);
57 /* allocate a new buffer object, call w/ table_lock held */
58 static struct fd_bo * bo_from_handle(struct fd_device *dev,
59 uint32_t size, uint32_t handle)
63 bo = dev->funcs->bo_from_handle(dev, size, handle);
65 struct drm_gem_close req = {
68 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
71 bo->dev = fd_device_ref(dev);
74 atomic_set(&bo->refcnt, 1);
75 list_inithead(&bo->list);
76 /* add ourself into the handle table: */
77 drmHashInsert(dev->handle_table, handle, bo);
82 bo_new(struct fd_device *dev, uint32_t size, uint32_t flags,
83 struct fd_bo_cache *cache)
85 struct fd_bo *bo = NULL;
89 bo = fd_bo_cache_alloc(cache, &size, flags);
93 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
97 pthread_mutex_lock(&table_lock);
98 bo = bo_from_handle(dev, size, handle);
99 pthread_mutex_unlock(&table_lock);
106 drm_public struct fd_bo *
107 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
109 struct fd_bo *bo = bo_new(dev, size, flags, &dev->bo_cache);
111 bo->bo_reuse = BO_CACHE;
115 /* internal function to allocate bo's that use the ringbuffer cache
116 * instead of the normal bo_cache. The purpose is, because cmdstream
117 * bo's get vmap'd on the kernel side, and that is expensive, we want
118 * to re-use cmdstream bo's for cmdstream and not unrelated purposes.
120 drm_private struct fd_bo *
121 fd_bo_new_ring(struct fd_device *dev, uint32_t size, uint32_t flags)
123 struct fd_bo *bo = bo_new(dev, size, flags, &dev->ring_cache);
125 bo->bo_reuse = RING_CACHE;
129 drm_public struct fd_bo *
130 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
132 struct fd_bo *bo = NULL;
134 pthread_mutex_lock(&table_lock);
136 bo = lookup_bo(dev->handle_table, handle);
140 bo = bo_from_handle(dev, size, handle);
145 pthread_mutex_unlock(&table_lock);
150 drm_public struct fd_bo *
151 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
157 pthread_mutex_lock(&table_lock);
158 ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
160 pthread_mutex_unlock(&table_lock);
164 bo = lookup_bo(dev->handle_table, handle);
168 /* lseek() to get bo size */
169 size = lseek(fd, 0, SEEK_END);
170 lseek(fd, 0, SEEK_CUR);
172 bo = bo_from_handle(dev, size, handle);
177 pthread_mutex_unlock(&table_lock);
182 drm_public struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
184 struct drm_gem_open req = {
189 pthread_mutex_lock(&table_lock);
191 /* check name table first, to see if bo is already open: */
192 bo = lookup_bo(dev->name_table, name);
196 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
197 ERROR_MSG("gem-open failed: %s", strerror(errno));
201 bo = lookup_bo(dev->handle_table, req.handle);
205 bo = bo_from_handle(dev, req.size, req.handle);
212 pthread_mutex_unlock(&table_lock);
217 drm_public uint64_t fd_bo_get_iova(struct fd_bo *bo)
219 return bo->funcs->iova(bo);
222 drm_public void fd_bo_put_iova(struct fd_bo *bo)
224 /* currently a no-op */
227 drm_public struct fd_bo * fd_bo_ref(struct fd_bo *bo)
229 atomic_inc(&bo->refcnt);
233 drm_public void fd_bo_del(struct fd_bo *bo)
235 struct fd_device *dev = bo->dev;
237 if (!atomic_dec_and_test(&bo->refcnt))
240 pthread_mutex_lock(&table_lock);
242 if ((bo->bo_reuse == BO_CACHE) && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
244 if ((bo->bo_reuse == RING_CACHE) && (fd_bo_cache_free(&dev->ring_cache, bo) == 0))
248 fd_device_del_locked(dev);
250 pthread_mutex_unlock(&table_lock);
253 /* Called under table_lock */
254 drm_private void bo_del(struct fd_bo *bo)
259 drm_munmap(bo->map, bo->size);
261 /* TODO probably bo's in bucket list get removed from
266 struct drm_gem_close req = {
267 .handle = bo->handle,
269 drmHashDelete(bo->dev->handle_table, bo->handle);
271 drmHashDelete(bo->dev->name_table, bo->name);
272 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
275 bo->funcs->destroy(bo);
278 drm_public int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
281 struct drm_gem_flink req = {
282 .handle = bo->handle,
286 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
291 pthread_mutex_lock(&table_lock);
292 set_name(bo, req.name);
293 pthread_mutex_unlock(&table_lock);
294 bo->bo_reuse = NO_CACHE;
302 drm_public uint32_t fd_bo_handle(struct fd_bo *bo)
307 drm_public int fd_bo_dmabuf(struct fd_bo *bo)
311 ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
314 ERROR_MSG("failed to get dmabuf fd: %d", ret);
318 bo->bo_reuse = NO_CACHE;
323 drm_public uint32_t fd_bo_size(struct fd_bo *bo)
328 drm_public void * fd_bo_map(struct fd_bo *bo)
334 ret = bo->funcs->offset(bo, &offset);
339 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
340 bo->dev->fd, offset);
341 if (bo->map == MAP_FAILED) {
342 ERROR_MSG("mmap failed: %s", strerror(errno));
349 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
350 drm_public int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
352 return bo->funcs->cpu_prep(bo, pipe, op);
355 drm_public void fd_bo_cpu_fini(struct fd_bo *bo)
357 bo->funcs->cpu_fini(bo);
360 #if !HAVE_FREEDRENO_KGSL
361 drm_public struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe, int fbfd, uint32_t size)