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>
31 #include "freedreno_drmif.h"
32 #include "freedreno_priv.h"
33 #include "freedreno_ringbuffer.h"
36 /* because kgsl tries to validate the gpuaddr on kernel side in ISSUEIBCMDS,
37 * we can't use normal gem bo's for ringbuffer.. someday the kernel part
38 * needs to be reworked into a single sane drm driver :-/
47 struct fd_ringmarker {
48 struct fd_ringbuffer *ring;
52 static void fd_rb_bo_del(struct fd_rb_bo *bo)
54 struct kgsl_sharedmem_free req = {
55 .gpuaddr = bo->gpuaddr,
59 munmap(bo->hostptr, bo->size);
61 ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
63 ERROR_MSG("sharedmem free failed: %s", strerror(errno));
69 static struct fd_rb_bo * fd_rb_bo_new(struct fd_pipe *pipe, uint32_t size)
72 struct kgsl_gpumem_alloc req = {
73 .size = ALIGN(size, 4096),
74 .flags = KGSL_MEMFLAGS_GPUREADONLY,
78 bo = calloc(1, sizeof(*bo));
80 ERROR_MSG("allocation failed");
83 ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
85 ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
90 bo->gpuaddr = req.gpuaddr;
92 bo->hostptr = mmap(NULL, size, PROT_WRITE|PROT_READ,
93 MAP_SHARED, pipe->fd, req.gpuaddr);
102 struct fd_ringbuffer * fd_ringbuffer_new(struct fd_pipe *pipe,
105 struct fd_ringbuffer *ring = NULL;
107 ring = calloc(1, sizeof(*ring));
109 ERROR_MSG("allocation failed");
113 ring->bo = fd_rb_bo_new(pipe, size);
115 ERROR_MSG("ringbuffer allocation failed");
121 ring->start = ring->bo->hostptr;
122 ring->end = &(ring->start[size/4]);
124 ring->cur = ring->last_start = ring->start;
129 fd_ringbuffer_del(ring);
133 void fd_ringbuffer_del(struct fd_ringbuffer *ring)
136 fd_rb_bo_del(ring->bo);
140 void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
142 uint32_t *start = ring->start;
143 if (ring->pipe->id == FD_PIPE_2D)
144 start = &ring->start[0x140];
145 ring->cur = ring->last_start = start;
148 static int flush_impl(struct fd_ringbuffer *ring, uint32_t *last_start)
150 uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start;
151 struct kgsl_ibdesc ibdesc = {
152 .gpuaddr = ring->bo->gpuaddr + offset,
153 .hostptr = last_start,
154 .sizedwords = ring->cur - last_start,
156 struct kgsl_ringbuffer_issueibcmds req = {
157 .drawctxt_id = ring->pipe->drawctxt_id,
158 .ibdesc_addr = (unsigned long)&ibdesc,
160 .flags = KGSL_CONTEXT_SUBMIT_IB_LIST,
164 fd_pipe_pre_submit(ring->pipe);
166 /* z180_cmdstream_issueibcmds() is made of fail: */
167 if (ring->pipe->id == FD_PIPE_2D) {
168 /* fix up size field in last cmd packet */
169 uint32_t last_size = (uint32_t)(ring->cur - last_start);
170 /* 5 is length of first packet, 2 for the two 7f000000's */
171 last_start[2] = last_size - (5 + 2);
172 ibdesc.gpuaddr = ring->bo->gpuaddr;
173 ibdesc.hostptr = ring->bo->hostptr;
174 ibdesc.sizedwords = 0x145;
175 req.timestamp = (uint32_t)ring->bo->hostptr;
179 ret = ioctl(ring->pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
180 } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
182 ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno));
184 ring->last_timestamp = req.timestamp;
185 ring->last_start = ring->cur;
187 fd_pipe_post_submit(ring->pipe, req.timestamp);
192 /* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
193 int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
195 return flush_impl(ring, ring->last_start);
198 uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
200 return ring->last_timestamp;
203 void fd_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
204 struct fd_bo *bo, uint32_t offset, uint32_t or)
206 uint32_t addr = fd_bo_gpuaddr(bo, offset);
208 (*ring->cur++) = addr | or;
209 fd_pipe_add_submit(ring->pipe, bo);
212 void fd_ringbuffer_emit_reloc_shift(struct fd_ringbuffer *ring,
213 struct fd_bo *bo, uint32_t offset, uint32_t or, int32_t shift)
215 uint32_t addr = fd_bo_gpuaddr(bo, offset);
221 (*ring->cur++) = addr | or;
222 fd_pipe_add_submit(ring->pipe, bo);
225 void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
226 struct fd_ringmarker *target)
228 (*ring->cur++) = target->ring->bo->gpuaddr +
229 (uint8_t *)target->cur - (uint8_t *)target->ring->start;
232 struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
234 struct fd_ringmarker *marker = NULL;
236 marker = calloc(1, sizeof(*marker));
238 ERROR_MSG("allocation failed");
244 fd_ringmarker_mark(marker);
249 void fd_ringmarker_del(struct fd_ringmarker *marker)
254 void fd_ringmarker_mark(struct fd_ringmarker *marker)
256 marker->cur = marker->ring->cur;
259 uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
260 struct fd_ringmarker *end)
262 return end->cur - start->cur;
265 int fd_ringmarker_flush(struct fd_ringmarker *marker)
267 return flush_impl(marker->ring, marker->cur);