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 drm_private pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
37 drm_private void bo_del(struct fd_bo *bo);
39 /* set buffer name, and add to table, call w/ table_lock held: */
40 static void set_name(struct fd_bo *bo, uint32_t name)
43 /* add ourself into the handle table: */
44 drmHashInsert(bo->dev->name_table, name, bo);
47 /* lookup a buffer, call w/ table_lock held: */
48 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
50 struct fd_bo *bo = NULL;
51 if (!drmHashLookup(tbl, key, (void **)&bo)) {
52 /* found, incr refcnt and return: */
55 /* don't break the bucket if this bo was found in one */
56 list_delinit(&bo->list);
61 /* allocate a new buffer object, call w/ table_lock held */
62 static struct fd_bo * bo_from_handle(struct fd_device *dev,
63 uint32_t size, uint32_t handle)
67 bo = dev->funcs->bo_from_handle(dev, size, handle);
69 struct drm_gem_close req = {
72 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
75 bo->dev = fd_device_ref(dev);
78 atomic_set(&bo->refcnt, 1);
79 list_inithead(&bo->list);
80 /* add ourself into the handle table: */
81 drmHashInsert(dev->handle_table, handle, bo);
86 fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
88 struct fd_bo *bo = NULL;
92 bo = fd_bo_cache_alloc(&dev->bo_cache, &size, flags);
96 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
100 pthread_mutex_lock(&table_lock);
101 bo = bo_from_handle(dev, size, handle);
103 pthread_mutex_unlock(&table_lock);
109 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
111 struct fd_bo *bo = NULL;
113 pthread_mutex_lock(&table_lock);
115 bo = lookup_bo(dev->handle_table, handle);
119 bo = bo_from_handle(dev, size, handle);
122 pthread_mutex_unlock(&table_lock);
128 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
134 pthread_mutex_lock(&table_lock);
135 ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
140 bo = lookup_bo(dev->handle_table, handle);
144 /* lseek() to get bo size */
145 size = lseek(fd, 0, SEEK_END);
146 lseek(fd, 0, SEEK_CUR);
148 bo = bo_from_handle(dev, size, handle);
151 pthread_mutex_unlock(&table_lock);
156 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
158 struct drm_gem_open req = {
163 pthread_mutex_lock(&table_lock);
165 /* check name table first, to see if bo is already open: */
166 bo = lookup_bo(dev->name_table, name);
170 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
171 ERROR_MSG("gem-open failed: %s", strerror(errno));
175 bo = lookup_bo(dev->handle_table, req.handle);
179 bo = bo_from_handle(dev, req.size, req.handle);
184 pthread_mutex_unlock(&table_lock);
189 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
191 atomic_inc(&bo->refcnt);
195 void fd_bo_del(struct fd_bo *bo)
197 struct fd_device *dev = bo->dev;
199 if (!atomic_dec_and_test(&bo->refcnt))
202 pthread_mutex_lock(&table_lock);
204 if (bo->bo_reuse && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
208 fd_device_del_locked(dev);
210 pthread_mutex_unlock(&table_lock);
213 /* Called under table_lock */
214 drm_private void bo_del(struct fd_bo *bo)
217 drm_munmap(bo->map, bo->size);
219 /* TODO probably bo's in bucket list get removed from
224 struct drm_gem_close req = {
225 .handle = bo->handle,
227 drmHashDelete(bo->dev->handle_table, bo->handle);
229 drmHashDelete(bo->dev->name_table, bo->name);
230 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
233 bo->funcs->destroy(bo);
236 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
239 struct drm_gem_flink req = {
240 .handle = bo->handle,
244 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
249 pthread_mutex_lock(&table_lock);
250 set_name(bo, req.name);
251 pthread_mutex_unlock(&table_lock);
252 bo->bo_reuse = FALSE;
260 uint32_t fd_bo_handle(struct fd_bo *bo)
265 int fd_bo_dmabuf(struct fd_bo *bo)
269 ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
272 ERROR_MSG("failed to get dmabuf fd: %d", ret);
276 bo->bo_reuse = FALSE;
281 uint32_t fd_bo_size(struct fd_bo *bo)
286 void * fd_bo_map(struct fd_bo *bo)
292 ret = bo->funcs->offset(bo, &offset);
297 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
298 bo->dev->fd, offset);
299 if (bo->map == MAP_FAILED) {
300 ERROR_MSG("mmap failed: %s", strerror(errno));
307 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
308 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
310 return bo->funcs->cpu_prep(bo, pipe, op);
313 void fd_bo_cpu_fini(struct fd_bo *bo)
315 bo->funcs->cpu_fini(bo);
318 #ifndef HAVE_FREEDRENO_KGSL
319 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe, int fbfd, uint32_t size)