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>
31 #include "freedreno_ringbuffer.h"
32 #include "kgsl_priv.h"
35 /* because kgsl tries to validate the gpuaddr on kernel side in ISSUEIBCMDS,
36 * we can't use normal gem bo's for ringbuffer.. someday the kernel part
37 * needs to be reworked into a single sane drm driver :-/
40 struct kgsl_pipe *pipe;
46 struct kgsl_ringbuffer {
47 struct fd_ringbuffer base;
48 struct kgsl_rb_bo *bo;
51 static inline struct kgsl_ringbuffer * to_kgsl_ringbuffer(struct fd_ringbuffer *x)
53 return (struct kgsl_ringbuffer *)x;
56 static void kgsl_rb_bo_del(struct kgsl_rb_bo *bo)
58 struct kgsl_sharedmem_free req = {
59 .gpuaddr = bo->gpuaddr,
63 drm_munmap(bo->hostptr, bo->size);
65 ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
67 ERROR_MSG("sharedmem free failed: %s", strerror(errno));
73 static struct kgsl_rb_bo * kgsl_rb_bo_new(struct kgsl_pipe *pipe, uint32_t size)
75 struct kgsl_rb_bo *bo;
76 struct kgsl_gpumem_alloc req = {
77 .size = ALIGN(size, 4096),
78 .flags = KGSL_MEMFLAGS_GPUREADONLY,
82 bo = calloc(1, sizeof(*bo));
84 ERROR_MSG("allocation failed");
87 ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
89 ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
94 bo->gpuaddr = req.gpuaddr;
96 bo->hostptr = drm_mmap(NULL, size, PROT_WRITE|PROT_READ,
97 MAP_SHARED, pipe->fd, req.gpuaddr);
106 static void * kgsl_ringbuffer_hostptr(struct fd_ringbuffer *ring)
108 struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
109 return kgsl_ring->bo->hostptr;
112 static int kgsl_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start)
114 struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
115 struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(ring->pipe);
116 uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start;
117 struct kgsl_ibdesc ibdesc = {
118 .gpuaddr = kgsl_ring->bo->gpuaddr + offset,
119 .hostptr = last_start,
120 .sizedwords = ring->cur - last_start,
122 struct kgsl_ringbuffer_issueibcmds req = {
123 .drawctxt_id = kgsl_pipe->drawctxt_id,
124 .ibdesc_addr = (unsigned long)&ibdesc,
126 .flags = KGSL_CONTEXT_SUBMIT_IB_LIST,
130 kgsl_pipe_pre_submit(kgsl_pipe);
132 /* z180_cmdstream_issueibcmds() is made of fail: */
133 if (ring->pipe->id == FD_PIPE_2D) {
134 /* fix up size field in last cmd packet */
135 uint32_t last_size = (uint32_t)(ring->cur - last_start);
136 /* 5 is length of first packet, 2 for the two 7f000000's */
137 last_start[2] = last_size - (5 + 2);
138 ibdesc.gpuaddr = kgsl_ring->bo->gpuaddr;
139 ibdesc.hostptr = kgsl_ring->bo->hostptr;
140 ibdesc.sizedwords = 0x145;
141 req.timestamp = (uint32_t)kgsl_ring->bo->hostptr;
145 ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
146 } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
148 ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno));
150 ring->last_timestamp = req.timestamp;
151 ring->last_start = ring->cur;
153 kgsl_pipe_post_submit(kgsl_pipe, req.timestamp);
158 static void kgsl_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
159 const struct fd_reloc *r)
161 struct kgsl_bo *kgsl_bo = to_kgsl_bo(r->bo);
162 uint32_t addr = kgsl_bo_gpuaddr(kgsl_bo, r->offset);
168 (*ring->cur++) = addr | r->or;
169 kgsl_pipe_add_submit(to_kgsl_pipe(ring->pipe), kgsl_bo);
172 static void kgsl_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
173 struct fd_ringmarker *target, struct fd_ringmarker *end)
175 struct kgsl_ringbuffer *target_ring = to_kgsl_ringbuffer(target->ring);
176 (*ring->cur++) = target_ring->bo->gpuaddr +
177 (uint8_t *)target->cur - (uint8_t *)target->ring->start;
180 static void kgsl_ringbuffer_destroy(struct fd_ringbuffer *ring)
182 struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
183 if (ring->last_timestamp)
184 fd_pipe_wait(ring->pipe, ring->last_timestamp);
186 kgsl_rb_bo_del(kgsl_ring->bo);
190 static struct fd_ringbuffer_funcs funcs = {
191 .hostptr = kgsl_ringbuffer_hostptr,
192 .flush = kgsl_ringbuffer_flush,
193 .emit_reloc = kgsl_ringbuffer_emit_reloc,
194 .emit_reloc_ring = kgsl_ringbuffer_emit_reloc_ring,
195 .destroy = kgsl_ringbuffer_destroy,
198 struct fd_ringbuffer * kgsl_ringbuffer_new(struct fd_pipe *pipe,
201 struct kgsl_ringbuffer *kgsl_ring;
202 struct fd_ringbuffer *ring = NULL;
204 kgsl_ring = calloc(1, sizeof(*kgsl_ring));
206 ERROR_MSG("allocation failed");
210 ring = &kgsl_ring->base;
211 ring->funcs = &funcs;
213 kgsl_ring->bo = kgsl_rb_bo_new(to_kgsl_pipe(pipe), size);
214 if (!kgsl_ring->bo) {
215 ERROR_MSG("ringbuffer allocation failed");
222 fd_ringbuffer_del(ring);