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"
31 #include "freedreno_ringbuffer.h"
34 /* because kgsl tries to validate the gpuaddr on kernel side in ISSUEIBCMDS,
35 * we can't use normal gem bo's for ringbuffer.. someday the kernel part
36 * needs to be reworked into a single sane drm driver :-/
45 struct fd_ringmarker {
46 struct fd_ringbuffer *ring;
50 static void fd_rb_bo_del(struct fd_rb_bo *bo)
52 struct kgsl_sharedmem_free req = {
53 .gpuaddr = bo->gpuaddr,
57 munmap(bo->hostptr, bo->size);
59 ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
61 ERROR_MSG("sharedmem free failed: %s", strerror(errno));
67 static struct fd_rb_bo * fd_rb_bo_new(struct fd_pipe *pipe, uint32_t size)
70 struct kgsl_gpumem_alloc req = {
71 .size = ALIGN(size, 4096),
72 .flags = KGSL_MEMFLAGS_GPUREADONLY,
76 bo = calloc(1, sizeof(*bo));
78 ERROR_MSG("allocation failed");
81 ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
83 ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
88 bo->gpuaddr = req.gpuaddr;
90 bo->hostptr = mmap(NULL, size, PROT_WRITE|PROT_READ,
91 MAP_SHARED, pipe->fd, req.gpuaddr);
100 struct fd_ringbuffer * fd_ringbuffer_new(struct fd_pipe *pipe,
103 struct fd_ringbuffer *ring = NULL;
105 ring = calloc(1, sizeof(*ring));
107 ERROR_MSG("allocation failed");
111 ring->bo = fd_rb_bo_new(pipe, size);
113 ERROR_MSG("ringbuffer allocation failed");
119 ring->start = ring->bo->hostptr;
120 ring->end = &(ring->start[size/4]);
122 ring->cur = ring->last_start = ring->start;
127 fd_ringbuffer_del(ring);
131 void fd_ringbuffer_del(struct fd_ringbuffer *ring)
134 fd_rb_bo_del(ring->bo);
138 void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
140 uint32_t *start = ring->start;
141 if (ring->pipe->id == FD_PIPE_2D)
142 start = &ring->start[0x140];
143 ring->cur = ring->last_start = start;
146 static int flush_impl(struct fd_ringbuffer *ring, uint32_t *last_start)
148 uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start;
149 struct kgsl_ibdesc ibdesc = {
150 .gpuaddr = ring->bo->gpuaddr + offset,
151 .hostptr = last_start,
152 .sizedwords = ring->cur - last_start,
154 struct kgsl_ringbuffer_issueibcmds req = {
155 .drawctxt_id = ring->pipe->drawctxt_id,
156 .ibdesc_addr = (unsigned long)&ibdesc,
158 .flags = KGSL_CONTEXT_SUBMIT_IB_LIST,
162 fd_pipe_pre_submit(ring->pipe);
164 /* z180_cmdstream_issueibcmds() is made of fail: */
165 if (ring->pipe->id == FD_PIPE_2D) {
166 /* fix up size field in last cmd packet */
167 uint32_t last_size = (uint32_t)(ring->cur - last_start);
168 /* 5 is length of first packet, 2 for the two 7f000000's */
169 last_start[2] = last_size - (5 + 2);
170 ibdesc.gpuaddr = ring->bo->gpuaddr;
171 ibdesc.hostptr = ring->bo->hostptr;
172 ibdesc.sizedwords = 0x145;
173 req.timestamp = (uint32_t)ring->bo->hostptr;
177 ret = ioctl(ring->pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
178 } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
180 ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno));
182 ring->last_timestamp = req.timestamp;
183 ring->last_start = ring->cur;
185 fd_pipe_post_submit(ring->pipe, req.timestamp);
190 /* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
191 int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
193 return flush_impl(ring, ring->last_start);
196 uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
198 return ring->last_timestamp;
201 void fd_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
202 struct fd_bo *bo, uint32_t offset, uint32_t or)
204 (*ring->cur++) = fd_bo_gpuaddr(bo, offset) | or;
205 fd_pipe_add_submit(ring->pipe, bo);
208 void fd_ringbuffer_emit_reloc_shift(struct fd_ringbuffer *ring,
209 struct fd_bo *bo, uint32_t offset, uint32_t or, int32_t shift)
211 uint32_t addr = fd_bo_gpuaddr(bo, offset);
216 (*ring->cur++) = addr | or;
217 fd_pipe_add_submit(ring->pipe, bo);
220 void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
221 struct fd_ringmarker *target)
223 (*ring->cur++) = target->ring->bo->gpuaddr +
224 (uint8_t *)target->cur - (uint8_t *)target->ring->start;
227 struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
229 struct fd_ringmarker *marker = NULL;
231 marker = calloc(1, sizeof(*marker));
233 ERROR_MSG("allocation failed");
239 fd_ringmarker_mark(marker);
244 void fd_ringmarker_del(struct fd_ringmarker *marker)
249 void fd_ringmarker_mark(struct fd_ringmarker *marker)
251 marker->cur = marker->ring->cur;
254 uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
255 struct fd_ringmarker *end)
257 return end->cur - start->cur;
260 int fd_ringmarker_flush(struct fd_ringmarker *marker)
262 return flush_impl(marker->ring, marker->cur);