1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2013 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 "kgsl_priv.h"
33 static int set_memtype(struct fd_device *dev, uint32_t handle, uint32_t flags)
35 struct drm_kgsl_gem_memtype req = {
37 .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
40 return drmCommandWrite(dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
44 static int bo_alloc(struct kgsl_bo *kgsl_bo)
46 struct fd_bo *bo = &kgsl_bo->base;
47 if (!kgsl_bo->offset) {
48 struct drm_kgsl_gem_alloc req = {
53 /* if the buffer is already backed by pages then this
54 * doesn't actually do anything (other than giving us
57 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
60 ERROR_MSG("alloc failed: %s", strerror(errno));
64 kgsl_bo->offset = req.offset;
70 static int kgsl_bo_offset(struct fd_bo *bo, uint64_t *offset)
72 struct kgsl_bo *kgsl_bo = to_kgsl_bo(bo);
73 int ret = bo_alloc(kgsl_bo);
76 *offset = kgsl_bo->offset;
80 static int kgsl_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
82 uint32_t timestamp = kgsl_bo_get_timestamp(to_kgsl_bo(bo));
84 if (op & DRM_FREEDRENO_PREP_NOSYNC) {
88 /* special case for is_idle().. we can't really handle that
89 * properly in kgsl (perhaps we need a way to just disable
90 * the bo-cache for kgsl?)
95 ret = kgsl_pipe_timestamp(to_kgsl_pipe(pipe), ¤t);
99 if (timestamp > current)
106 fd_pipe_wait(pipe, timestamp);
111 static void kgsl_bo_cpu_fini(struct fd_bo *bo)
115 static void kgsl_bo_destroy(struct fd_bo *bo)
117 struct kgsl_bo *kgsl_bo = to_kgsl_bo(bo);
122 static struct fd_bo_funcs funcs = {
123 .offset = kgsl_bo_offset,
124 .cpu_prep = kgsl_bo_cpu_prep,
125 .cpu_fini = kgsl_bo_cpu_fini,
126 .destroy = kgsl_bo_destroy,
129 /* allocate a buffer handle: */
130 int kgsl_bo_new_handle(struct fd_device *dev,
131 uint32_t size, uint32_t flags, uint32_t *handle)
133 struct drm_kgsl_gem_create req = {
138 ret = drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
143 // TODO make flags match msm driver, since kgsl is legacy..
144 // translate flags in kgsl..
146 set_memtype(dev, req.handle, flags);
148 *handle = req.handle;
153 /* allocate a new buffer object */
154 struct fd_bo * kgsl_bo_from_handle(struct fd_device *dev,
155 uint32_t size, uint32_t handle)
157 struct kgsl_bo *kgsl_bo;
161 kgsl_bo = calloc(1, sizeof(*kgsl_bo));
168 for (i = 0; i < ARRAY_SIZE(kgsl_bo->list); i++)
169 list_inithead(&kgsl_bo->list[i]);
174 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
175 int fbfd, uint32_t size)
177 struct drm_kgsl_gem_create_fd req = {
181 struct kgsl_bo *kgsl_bo;
183 if (!is_kgsl_pipe(pipe))
186 if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
187 &req, sizeof(req))) {
191 bo = fd_bo_from_handle(pipe->dev, req.handle, size);
192 kgsl_bo = to_kgsl_bo(bo);
194 /* this is fugly, but works around a bug in the kernel..
195 * priv->memdesc.size never gets set, so getbufinfo ioctl
196 * thinks the buffer hasn't be allocate and fails
198 if (bo && !kgsl_bo_gpuaddr(kgsl_bo, 0)) {
199 void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
200 MAP_SHARED, fbfd, 0);
201 struct kgsl_map_user_mem req = {
202 .memtype = KGSL_USER_MEM_TYPE_ADDR,
205 .hostptr = (unsigned long)fbmem,
208 ret = ioctl(to_kgsl_pipe(pipe)->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
210 ERROR_MSG("mapping user mem failed: %s",
214 kgsl_bo->gpuaddr = req.gpuaddr;
226 uint32_t kgsl_bo_gpuaddr(struct kgsl_bo *kgsl_bo, uint32_t offset)
228 struct fd_bo *bo = &kgsl_bo->base;
229 if (!kgsl_bo->gpuaddr) {
230 struct drm_kgsl_gem_bufinfo req = {
231 .handle = bo->handle,
235 ret = bo_alloc(kgsl_bo);
240 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
243 ERROR_MSG("get bufinfo failed: %s", strerror(errno));
247 kgsl_bo->gpuaddr = req.gpuaddr[0];
249 return kgsl_bo->gpuaddr + offset;
253 * Super-cheezy way to synchronization between mesa and ddx.. the
254 * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
255 * GET_BUFINFO gives us a way to retrieve it. We use this to stash
256 * the timestamp of the last ISSUEIBCMDS on the buffer.
258 * To avoid an obscene amount of syscalls, we:
259 * 1) Only set the timestamp for buffers w/ an flink name, ie.
260 * only buffers shared across processes. This is enough to
261 * catch the DRI2 buffers.
262 * 2) Only set the timestamp for buffers submitted to the 3d ring
263 * and only check the timestamps on buffers submitted to the
264 * 2d ring. This should be enough to handle synchronizing of
265 * presentation blit. We could do synchronization in the other
266 * direction too, but that would be problematic if we are using
267 * the 3d ring from DDX, since client side wouldn't know this.
269 * The waiting on timestamp happens before flush, and setting of
270 * timestamp happens after flush. It is transparent to the user
271 * of libdrm_freedreno as all the tracking of buffers happens via
275 void kgsl_bo_set_timestamp(struct kgsl_bo *kgsl_bo, uint32_t timestamp)
277 struct fd_bo *bo = &kgsl_bo->base;
279 struct drm_kgsl_gem_active req = {
280 .handle = bo->handle,
285 ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
288 ERROR_MSG("set active failed: %s", strerror(errno));
293 uint32_t kgsl_bo_get_timestamp(struct kgsl_bo *kgsl_bo)
295 struct fd_bo *bo = &kgsl_bo->base;
296 uint32_t timestamp = 0;
298 struct drm_kgsl_gem_bufinfo req = {
299 .handle = bo->handle,
303 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
306 ERROR_MSG("get bufinfo failed: %s", strerror(errno));
310 timestamp = req.active;