blob: 92c7dd797d3c390325444fddbd80494ede7bbb9f [file] [log] [blame]
Rob Clark41fc2cc2012-10-07 18:57:31 -05001/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29#include "freedreno_drmif.h"
30#include "freedreno_priv.h"
31
Rob Clark0b89e272013-05-15 13:18:02 -040032static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
33
34/* set buffer name, and add to table, call w/ table_lock held: */
35static void set_name(struct fd_bo *bo, uint32_t name)
36{
37 bo->name = name;
38 /* add ourself into the handle table: */
39 drmHashInsert(bo->dev->name_table, name, bo);
40}
41
42/* lookup a buffer, call w/ table_lock held: */
43static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
44{
45 struct fd_bo *bo = NULL;
46 if (!drmHashLookup(tbl, key, (void **)&bo)) {
47 /* found, incr refcnt and return: */
48 bo = fd_bo_ref(bo);
49 }
50 return bo;
51}
52
53/* allocate a new buffer object, call w/ table_lock held */
Rob Clark41fc2cc2012-10-07 18:57:31 -050054static struct fd_bo * bo_from_handle(struct fd_device *dev,
55 uint32_t size, uint32_t handle)
56{
Rob Clarkb2b18852013-07-10 15:20:04 -040057 struct fd_bo *bo;
58
59 bo = dev->funcs->bo_from_handle(dev, size, handle);
Rob Clark0b89e272013-05-15 13:18:02 -040060 if (!bo) {
61 struct drm_gem_close req = {
62 .handle = handle,
63 };
64 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
Rob Clark41fc2cc2012-10-07 18:57:31 -050065 return NULL;
Rob Clark0b89e272013-05-15 13:18:02 -040066 }
67 bo->dev = fd_device_ref(dev);
Rob Clark41fc2cc2012-10-07 18:57:31 -050068 bo->size = size;
69 bo->handle = handle;
70 atomic_set(&bo->refcnt, 1);
Rob Clark0b89e272013-05-15 13:18:02 -040071 /* add ourself into the handle table: */
72 drmHashInsert(dev->handle_table, handle, bo);
Rob Clark41fc2cc2012-10-07 18:57:31 -050073 return bo;
74}
75
Rob Clark41fc2cc2012-10-07 18:57:31 -050076struct fd_bo * fd_bo_new(struct fd_device *dev,
77 uint32_t size, uint32_t flags)
78{
Rob Clark41fc2cc2012-10-07 18:57:31 -050079 struct fd_bo *bo = NULL;
Rob Clarkb2b18852013-07-10 15:20:04 -040080 uint32_t handle;
81 int ret;
Rob Clark41fc2cc2012-10-07 18:57:31 -050082
Rob Clarkb2b18852013-07-10 15:20:04 -040083 ret = dev->funcs->bo_new_handle(dev, ALIGN(size, 4096), flags, &handle);
84 if (ret)
Rob Clark41fc2cc2012-10-07 18:57:31 -050085 return NULL;
Rob Clark41fc2cc2012-10-07 18:57:31 -050086
Rob Clark0b89e272013-05-15 13:18:02 -040087 pthread_mutex_lock(&table_lock);
Rob Clarkb2b18852013-07-10 15:20:04 -040088 bo = bo_from_handle(dev, size, handle);
Rob Clark0b89e272013-05-15 13:18:02 -040089 pthread_mutex_unlock(&table_lock);
Rob Clark41fc2cc2012-10-07 18:57:31 -050090
91 return bo;
Rob Clark41fc2cc2012-10-07 18:57:31 -050092}
93
Rob Clarkb2b18852013-07-10 15:20:04 -040094struct fd_bo *fd_bo_from_handle(struct fd_device *dev,
95 uint32_t handle, uint32_t size)
Rob Clark41fc2cc2012-10-07 18:57:31 -050096{
Rob Clarkb2b18852013-07-10 15:20:04 -040097 struct fd_bo *bo = NULL;
Rob Clark41fc2cc2012-10-07 18:57:31 -050098
Rob Clark0b89e272013-05-15 13:18:02 -040099 pthread_mutex_lock(&table_lock);
Rob Clarkb2b18852013-07-10 15:20:04 -0400100 bo = bo_from_handle(dev, size, handle);
Rob Clark0b89e272013-05-15 13:18:02 -0400101 pthread_mutex_unlock(&table_lock);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500102
103 return bo;
Rob Clark41fc2cc2012-10-07 18:57:31 -0500104}
105
106struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
107{
108 struct drm_gem_open req = {
109 .name = name,
110 };
Rob Clarkb3a3a772013-04-25 16:36:15 -0400111 struct fd_bo *bo;
Rob Clark41fc2cc2012-10-07 18:57:31 -0500112
Rob Clark0b89e272013-05-15 13:18:02 -0400113 pthread_mutex_lock(&table_lock);
114
115 /* check name table first, to see if bo is already open: */
116 bo = lookup_bo(dev->name_table, name);
117 if (bo)
118 goto out_unlock;
119
Rob Clark41fc2cc2012-10-07 18:57:31 -0500120 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
Rob Clark0b89e272013-05-15 13:18:02 -0400121 ERROR_MSG("gem-open failed: %s", strerror(errno));
122 goto out_unlock;
Rob Clark41fc2cc2012-10-07 18:57:31 -0500123 }
124
Rob Clark0b89e272013-05-15 13:18:02 -0400125 bo = lookup_bo(dev->handle_table, req.handle);
126 if (bo)
127 goto out_unlock;
128
Rob Clarkb3a3a772013-04-25 16:36:15 -0400129 bo = bo_from_handle(dev, req.size, req.handle);
130 if (bo)
Rob Clark0b89e272013-05-15 13:18:02 -0400131 set_name(bo, name);
132
133out_unlock:
134 pthread_mutex_unlock(&table_lock);
Rob Clarkb3a3a772013-04-25 16:36:15 -0400135
136 return bo;
Rob Clark41fc2cc2012-10-07 18:57:31 -0500137}
138
139struct fd_bo * fd_bo_ref(struct fd_bo *bo)
140{
141 atomic_inc(&bo->refcnt);
142 return bo;
143}
144
145void fd_bo_del(struct fd_bo *bo)
146{
Rob Clarkb2b18852013-07-10 15:20:04 -0400147 struct fd_device *dev;
148
Rob Clark41fc2cc2012-10-07 18:57:31 -0500149 if (!atomic_dec_and_test(&bo->refcnt))
150 return;
151
152 if (bo->map)
153 munmap(bo->map, bo->size);
154
155 if (bo->handle) {
156 struct drm_gem_close req = {
157 .handle = bo->handle,
158 };
Rob Clark0b89e272013-05-15 13:18:02 -0400159 pthread_mutex_lock(&table_lock);
160 drmHashDelete(bo->dev->handle_table, bo->handle);
Rob Clark35863372013-05-17 16:13:02 -0400161 if (bo->name)
162 drmHashDelete(bo->dev->name_table, bo->name);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500163 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
Rob Clark0b89e272013-05-15 13:18:02 -0400164 pthread_mutex_unlock(&table_lock);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500165 }
166
Rob Clarkb2b18852013-07-10 15:20:04 -0400167 dev = bo->dev;
168 bo->funcs->destroy(bo);
169
170 fd_device_del(dev);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500171}
172
173int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
174{
175 if (!bo->name) {
176 struct drm_gem_flink req = {
177 .handle = bo->handle,
178 };
179 int ret;
180
181 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
182 if (ret) {
183 return ret;
184 }
185
Rob Clark0b89e272013-05-15 13:18:02 -0400186 pthread_mutex_lock(&table_lock);
187 set_name(bo, req.name);
188 pthread_mutex_unlock(&table_lock);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500189 }
190
191 *name = bo->name;
192
193 return 0;
194}
195
196uint32_t fd_bo_handle(struct fd_bo *bo)
197{
198 return bo->handle;
199}
200
201uint32_t fd_bo_size(struct fd_bo *bo)
202{
203 return bo->size;
204}
205
206void * fd_bo_map(struct fd_bo *bo)
207{
208 if (!bo->map) {
Rob Clarkb2b18852013-07-10 15:20:04 -0400209 uint64_t offset;
Rob Clark41fc2cc2012-10-07 18:57:31 -0500210 int ret;
211
Rob Clarkb2b18852013-07-10 15:20:04 -0400212 ret = bo->funcs->offset(bo, &offset);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500213 if (ret) {
214 return NULL;
215 }
216
217 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
Rob Clarkb2b18852013-07-10 15:20:04 -0400218 bo->dev->fd, offset);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500219 if (bo->map == MAP_FAILED) {
220 ERROR_MSG("mmap failed: %s", strerror(errno));
221 bo->map = NULL;
222 }
223 }
224 return bo->map;
225}
226
Rob Clarkb2b18852013-07-10 15:20:04 -0400227/* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
228int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
Rob Clark41fc2cc2012-10-07 18:57:31 -0500229{
Rob Clarkb2b18852013-07-10 15:20:04 -0400230 return bo->funcs->cpu_prep(bo, pipe, op);
Rob Clark41fc2cc2012-10-07 18:57:31 -0500231}
Rob Clarkb3a3a772013-04-25 16:36:15 -0400232
Rob Clarkb2b18852013-07-10 15:20:04 -0400233void fd_bo_cpu_fini(struct fd_bo *bo)
Rob Clarkb3a3a772013-04-25 16:36:15 -0400234{
Rob Clarkb2b18852013-07-10 15:20:04 -0400235 bo->funcs->cpu_fini(bo);
Rob Clarkb3a3a772013-04-25 16:36:15 -0400236}