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);
111 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
113 struct fd_bo *bo = NULL;
115 pthread_mutex_lock(&table_lock);
117 bo = lookup_bo(dev->handle_table, handle);
121 bo = bo_from_handle(dev, size, handle);
126 pthread_mutex_unlock(&table_lock);
132 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
138 pthread_mutex_lock(&table_lock);
139 ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
144 bo = lookup_bo(dev->handle_table, handle);
148 /* lseek() to get bo size */
149 size = lseek(fd, 0, SEEK_END);
150 lseek(fd, 0, SEEK_CUR);
152 bo = bo_from_handle(dev, size, handle);
157 pthread_mutex_unlock(&table_lock);
162 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
164 struct drm_gem_open req = {
169 pthread_mutex_lock(&table_lock);
171 /* check name table first, to see if bo is already open: */
172 bo = lookup_bo(dev->name_table, name);
176 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
177 ERROR_MSG("gem-open failed: %s", strerror(errno));
181 bo = lookup_bo(dev->handle_table, req.handle);
185 bo = bo_from_handle(dev, req.size, req.handle);
192 pthread_mutex_unlock(&table_lock);
197 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
199 atomic_inc(&bo->refcnt);
203 void fd_bo_del(struct fd_bo *bo)
205 struct fd_device *dev = bo->dev;
207 if (!atomic_dec_and_test(&bo->refcnt))
210 pthread_mutex_lock(&table_lock);
212 if (bo->bo_reuse && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
216 fd_device_del_locked(dev);
218 pthread_mutex_unlock(&table_lock);
221 /* Called under table_lock */
222 drm_private void bo_del(struct fd_bo *bo)
227 drm_munmap(bo->map, bo->size);
229 /* TODO probably bo's in bucket list get removed from
234 struct drm_gem_close req = {
235 .handle = bo->handle,
237 drmHashDelete(bo->dev->handle_table, bo->handle);
239 drmHashDelete(bo->dev->name_table, bo->name);
240 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
243 bo->funcs->destroy(bo);
246 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
249 struct drm_gem_flink req = {
250 .handle = bo->handle,
254 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
259 pthread_mutex_lock(&table_lock);
260 set_name(bo, req.name);
261 pthread_mutex_unlock(&table_lock);
262 bo->bo_reuse = FALSE;
270 uint32_t fd_bo_handle(struct fd_bo *bo)
275 int fd_bo_dmabuf(struct fd_bo *bo)
279 ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
282 ERROR_MSG("failed to get dmabuf fd: %d", ret);
286 bo->bo_reuse = FALSE;
291 uint32_t fd_bo_size(struct fd_bo *bo)
296 void * fd_bo_map(struct fd_bo *bo)
302 ret = bo->funcs->offset(bo, &offset);
307 bo->map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
308 bo->dev->fd, offset);
309 if (bo->map == MAP_FAILED) {
310 ERROR_MSG("mmap failed: %s", strerror(errno));
317 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
318 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
320 return bo->funcs->cpu_prep(bo, pipe, op);
323 void fd_bo_cpu_fini(struct fd_bo *bo)
325 bo->funcs->cpu_fini(bo);
328 #ifndef HAVE_FREEDRENO_KGSL
329 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe, int fbfd, uint32_t size)