2 * Copyright (C) 2014 Etnaviv Project
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
27 #include "os/os_mman.h"
29 #include "etnaviv_priv.h"
30 #include "etnaviv_drmif.h"
32 pthread_mutex_t table_lock
= PTHREAD_MUTEX_INITIALIZER
;
33 void bo_del(struct etna_bo
*bo
);
35 /* set buffer name, and add to table, call w/ table_lock held: */
36 static void set_name(struct etna_bo
*bo
, uint32_t name
)
39 /* add ourself into the name table: */
40 drmHashInsert(bo
->dev
->name_table
, name
, bo
);
43 /* Called under table_lock */
44 void bo_del(struct etna_bo
*bo
)
47 os_munmap(bo
->map
, bo
->size
);
50 drmHashDelete(bo
->dev
->name_table
, bo
->name
);
53 struct drm_gem_close req
= {
57 drmHashDelete(bo
->dev
->handle_table
, bo
->handle
);
58 drmIoctl(bo
->dev
->fd
, DRM_IOCTL_GEM_CLOSE
, &req
);
64 /* lookup a buffer from it's handle, call w/ table_lock held: */
65 static struct etna_bo
*lookup_bo(void *tbl
, uint32_t handle
)
67 struct etna_bo
*bo
= NULL
;
69 if (!drmHashLookup(tbl
, handle
, (void **)&bo
)) {
70 /* found, incr refcnt and return: */
73 /* don't break the bucket if this bo was found in one */
74 list_delinit(&bo
->list
);
80 /* allocate a new buffer object, call w/ table_lock held */
81 static struct etna_bo
*bo_from_handle(struct etna_device
*dev
,
82 uint32_t size
, uint32_t handle
, uint32_t flags
)
84 struct etna_bo
*bo
= calloc(sizeof(*bo
), 1);
87 struct drm_gem_close req
= {
91 drmIoctl(dev
->fd
, DRM_IOCTL_GEM_CLOSE
, &req
);
96 bo
->dev
= etna_device_ref(dev
);
100 p_atomic_set(&bo
->refcnt
, 1);
101 list_inithead(&bo
->list
);
102 /* add ourselves to the handle table: */
103 drmHashInsert(dev
->handle_table
, handle
, bo
);
108 /* allocate a new (un-tiled) buffer object */
109 struct etna_bo
*etna_bo_new(struct etna_device
*dev
, uint32_t size
,
114 struct drm_etnaviv_gem_new req
= {
118 bo
= etna_bo_cache_alloc(&dev
->bo_cache
, &size
, flags
);
123 ret
= drmCommandWriteRead(dev
->fd
, DRM_ETNAVIV_GEM_NEW
,
128 pthread_mutex_lock(&table_lock
);
129 bo
= bo_from_handle(dev
, size
, req
.handle
, flags
);
131 pthread_mutex_unlock(&table_lock
);
136 struct etna_bo
*etna_bo_ref(struct etna_bo
*bo
)
138 p_atomic_inc(&bo
->refcnt
);
143 /* get buffer info */
144 static int get_buffer_info(struct etna_bo
*bo
)
147 struct drm_etnaviv_gem_info req
= {
148 .handle
= bo
->handle
,
151 ret
= drmCommandWriteRead(bo
->dev
->fd
, DRM_ETNAVIV_GEM_INFO
,
157 /* really all we need for now is mmap offset */
158 bo
->offset
= req
.offset
;
163 /* import a buffer object from DRI2 name */
164 struct etna_bo
*etna_bo_from_name(struct etna_device
*dev
,
168 struct drm_gem_open req
= {
172 pthread_mutex_lock(&table_lock
);
174 /* check name table first, to see if bo is already open: */
175 bo
= lookup_bo(dev
->name_table
, name
);
179 if (drmIoctl(dev
->fd
, DRM_IOCTL_GEM_OPEN
, &req
)) {
180 ERROR_MSG("gem-open failed: %s", strerror(errno
));
184 bo
= lookup_bo(dev
->handle_table
, req
.handle
);
188 bo
= bo_from_handle(dev
, req
.size
, req
.handle
, 0);
193 pthread_mutex_unlock(&table_lock
);
198 /* import a buffer from dmabuf fd, does not take ownership of the
199 * fd so caller should close() the fd when it is otherwise done
200 * with it (even if it is still using the 'struct etna_bo *')
202 struct etna_bo
*etna_bo_from_dmabuf(struct etna_device
*dev
, int fd
)
208 /* take the lock before calling drmPrimeFDToHandle to avoid
209 * racing against etna_bo_del, which might invalidate the
212 pthread_mutex_lock(&table_lock
);
214 ret
= drmPrimeFDToHandle(dev
->fd
, fd
, &handle
);
216 pthread_mutex_unlock(&table_lock
);
220 bo
= lookup_bo(dev
->handle_table
, handle
);
224 /* lseek() to get bo size */
225 size
= lseek(fd
, 0, SEEK_END
);
226 lseek(fd
, 0, SEEK_CUR
);
228 bo
= bo_from_handle(dev
, size
, handle
, 0);
231 pthread_mutex_unlock(&table_lock
);
236 /* destroy a buffer object */
237 void etna_bo_del(struct etna_bo
*bo
)
239 struct etna_device
*dev
= bo
->dev
;
244 if (!p_atomic_dec_zero(&bo
->refcnt
))
247 pthread_mutex_lock(&table_lock
);
249 if (bo
->reuse
&& (etna_bo_cache_free(&dev
->bo_cache
, bo
) == 0))
253 etna_device_del_locked(dev
);
255 pthread_mutex_unlock(&table_lock
);
258 /* get the global flink/DRI2 buffer name */
259 int etna_bo_get_name(struct etna_bo
*bo
, uint32_t *name
)
262 struct drm_gem_flink req
= {
263 .handle
= bo
->handle
,
267 ret
= drmIoctl(bo
->dev
->fd
, DRM_IOCTL_GEM_FLINK
, &req
);
272 pthread_mutex_lock(&table_lock
);
273 set_name(bo
, req
.name
);
274 pthread_mutex_unlock(&table_lock
);
283 uint32_t etna_bo_handle(struct etna_bo
*bo
)
288 /* caller owns the dmabuf fd that is returned and is responsible
289 * to close() it when done
291 int etna_bo_dmabuf(struct etna_bo
*bo
)
295 ret
= drmPrimeHandleToFD(bo
->dev
->fd
, bo
->handle
, DRM_CLOEXEC
,
298 ERROR_MSG("failed to get dmabuf fd: %d", ret
);
307 uint32_t etna_bo_size(struct etna_bo
*bo
)
312 void *etna_bo_map(struct etna_bo
*bo
)
319 bo
->map
= os_mmap(0, bo
->size
, PROT_READ
| PROT_WRITE
,
320 MAP_SHARED
, bo
->dev
->fd
, bo
->offset
);
321 if (bo
->map
== MAP_FAILED
) {
322 ERROR_MSG("mmap failed: %s", strerror(errno
));
330 int etna_bo_cpu_prep(struct etna_bo
*bo
, uint32_t op
)
332 struct drm_etnaviv_gem_cpu_prep req
= {
333 .handle
= bo
->handle
,
337 get_abs_timeout(&req
.timeout
, 5000000000);
339 return drmCommandWrite(bo
->dev
->fd
, DRM_ETNAVIV_GEM_CPU_PREP
,
343 void etna_bo_cpu_fini(struct etna_bo
*bo
)
345 struct drm_etnaviv_gem_cpu_fini req
= {
346 .handle
= bo
->handle
,
349 drmCommandWrite(bo
->dev
->fd
, DRM_ETNAVIV_GEM_CPU_FINI
,