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"
34 static struct fd_bo * bo_from_handle(struct fd_device *dev,
35 uint32_t size, uint32_t handle)
38 struct fd_bo *bo = calloc(1, sizeof(*bo));
44 atomic_set(&bo->refcnt, 1);
45 for (i = 0; i < ARRAY_SIZE(bo->list); i++)
46 list_inithead(&bo->list[i]);
50 static int set_memtype(struct fd_bo *bo, uint32_t flags)
52 struct drm_kgsl_gem_memtype req = {
54 .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
57 return drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
61 static int bo_alloc(struct fd_bo *bo)
64 struct drm_kgsl_gem_alloc req = {
69 /* if the buffer is already backed by pages then this
70 * doesn't actually do anything (other than giving us
73 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
76 ERROR_MSG("alloc failed: %s", strerror(errno));
80 bo->offset = req.offset;
85 struct fd_bo * fd_bo_new(struct fd_device *dev,
86 uint32_t size, uint32_t flags)
88 struct drm_kgsl_gem_create req = {
89 .size = ALIGN(size, 4096),
91 struct fd_bo *bo = NULL;
93 if (drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
98 bo = bo_from_handle(dev, size, req.handle);
103 if (set_memtype(bo, flags)) {
114 /* don't use this... it is just needed to get a bo from the
115 * framebuffer (pre-dmabuf)
117 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
118 int fbfd, uint32_t size)
120 struct drm_kgsl_gem_create_fd req = {
125 if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
126 &req, sizeof(req))) {
130 bo = bo_from_handle(pipe->dev, size, req.handle);
132 /* this is fugly, but works around a bug in the kernel..
133 * priv->memdesc.size never gets set, so getbufinfo ioctl
134 * thinks the buffer hasn't be allocate and fails
136 if (bo && !fd_bo_gpuaddr(bo, 0)) {
137 void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
138 MAP_SHARED, fbfd, 0);
139 struct kgsl_map_user_mem req = {
140 .memtype = KGSL_USER_MEM_TYPE_ADDR,
143 .hostptr = (unsigned long)fbmem,
146 ret = ioctl(pipe->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
148 ERROR_MSG("mapping user mem failed: %s",
152 bo->gpuaddr = req.gpuaddr;
163 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
165 struct drm_gem_open req = {
170 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
174 bo = bo_from_handle(dev, req.size, req.handle);
181 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
183 atomic_inc(&bo->refcnt);
187 void fd_bo_del(struct fd_bo *bo)
189 if (!atomic_dec_and_test(&bo->refcnt))
193 munmap(bo->map, bo->size);
196 struct drm_gem_close req = {
197 .handle = bo->handle,
199 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
205 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
208 struct drm_gem_flink req = {
209 .handle = bo->handle,
213 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
226 uint32_t fd_bo_handle(struct fd_bo *bo)
231 uint32_t fd_bo_size(struct fd_bo *bo)
236 void * fd_bo_map(struct fd_bo *bo)
246 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
247 bo->dev->fd, bo->offset);
248 if (bo->map == MAP_FAILED) {
249 ERROR_MSG("mmap failed: %s", strerror(errno));
256 uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset)
259 struct drm_kgsl_gem_bufinfo req = {
260 .handle = bo->handle,
269 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
272 ERROR_MSG("get bufinfo failed: %s", strerror(errno));
276 bo->gpuaddr = req.gpuaddr[0];
278 return bo->gpuaddr + offset;
282 * Super-cheezy way to synchronization between mesa and ddx.. the
283 * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
284 * GET_BUFINFO gives us a way to retrieve it. We use this to stash
285 * the timestamp of the last ISSUEIBCMDS on the buffer.
287 * To avoid an obscene amount of syscalls, we:
288 * 1) Only set the timestamp for buffers w/ an flink name, ie.
289 * only buffers shared across processes. This is enough to
290 * catch the DRI2 buffers.
291 * 2) Only set the timestamp for buffers submitted to the 3d ring
292 * and only check the timestamps on buffers submitted to the
293 * 2d ring. This should be enough to handle synchronizing of
294 * presentation blit. We could do synchronization in the other
295 * direction too, but that would be problematic if we are using
296 * the 3d ring from DDX, since client side wouldn't know this.
298 * The waiting on timestamp happens before flush, and setting of
299 * timestamp happens after flush. It is transparent to the user
300 * of libdrm_freedreno as all the tracking of buffers happens via
304 void fb_bo_set_timestamp(struct fd_bo *bo, uint32_t timestamp)
307 struct drm_kgsl_gem_active req = {
308 .handle = bo->handle,
313 ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
316 ERROR_MSG("set active failed: %s", strerror(errno));
321 uint32_t fd_bo_get_timestamp(struct fd_bo *bo)
323 uint32_t timestamp = 0;
325 struct drm_kgsl_gem_bufinfo req = {
326 .handle = bo->handle,
330 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
333 ERROR_MSG("get bufinfo failed: %s", strerror(errno));
337 timestamp = req.active;