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 = {
169 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
173 return bo_from_handle(dev, req.size, req.handle);
176 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
178 atomic_inc(&bo->refcnt);
182 void fd_bo_del(struct fd_bo *bo)
184 if (!atomic_dec_and_test(&bo->refcnt))
188 munmap(bo->map, bo->size);
191 struct drm_gem_close req = {
192 .handle = bo->handle,
194 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
200 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
203 struct drm_gem_flink req = {
204 .handle = bo->handle,
208 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
221 uint32_t fd_bo_handle(struct fd_bo *bo)
226 uint32_t fd_bo_size(struct fd_bo *bo)
231 void * fd_bo_map(struct fd_bo *bo)
241 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
242 bo->dev->fd, bo->offset);
243 if (bo->map == MAP_FAILED) {
244 ERROR_MSG("mmap failed: %s", strerror(errno));
251 uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset)
254 struct drm_kgsl_gem_bufinfo req = {
255 .handle = bo->handle,
264 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
267 ERROR_MSG("get bufinfo failed: %s", strerror(errno));
271 bo->gpuaddr = req.gpuaddr[0];
273 return bo->gpuaddr + offset;