d20a7f55b1de512c7e10e290d37ef197111a023e
[platform/upstream/libdrm.git] / freedreno / freedreno_ringbuffer.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
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:
12  *
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
15  * Software.
16  *
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
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
31 #include "freedreno_ringbuffer.h"
32
33
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 :-/
37  */
38 struct fd_rb_bo {
39         struct fd_pipe *pipe;
40         void    *hostptr;
41         uint32_t gpuaddr;
42         uint32_t size;
43 };
44
45 struct fd_ringmarker {
46         struct fd_ringbuffer *ring;
47         uint32_t *cur;
48 };
49
50 static void fd_rb_bo_del(struct fd_rb_bo *bo)
51 {
52         struct kgsl_sharedmem_free req = {
53                         .gpuaddr = bo->gpuaddr,
54         };
55         int ret;
56
57         munmap(bo->hostptr, bo->size);
58
59         ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
60         if (ret) {
61                 ERROR_MSG("sharedmem free failed: %s", strerror(errno));
62         }
63
64         free(bo);
65 }
66
67 static struct fd_rb_bo * fd_rb_bo_new(struct fd_pipe *pipe, uint32_t size)
68 {
69         struct fd_rb_bo *bo;
70         struct kgsl_gpumem_alloc req = {
71                         .size = ALIGN(size, 4096),
72                         .flags = KGSL_MEMFLAGS_GPUREADONLY,
73         };
74         int ret;
75
76         bo = calloc(1, sizeof(*bo));
77         if (!bo) {
78                 ERROR_MSG("allocation failed");
79                 return NULL;
80         }
81         ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
82         if (ret) {
83                 ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
84                 goto fail;
85         }
86
87         bo->pipe = pipe;
88         bo->gpuaddr = req.gpuaddr;
89         bo->size = size;
90         bo->hostptr = mmap(NULL, size, PROT_WRITE|PROT_READ,
91                                 MAP_SHARED, pipe->fd, req.gpuaddr);
92
93         return bo;
94 fail:
95         if (bo)
96                 fd_rb_bo_del(bo);
97         return NULL;
98 }
99
100 struct fd_ringbuffer * fd_ringbuffer_new(struct fd_pipe *pipe,
101                 uint32_t size)
102 {
103         struct fd_ringbuffer *ring = NULL;
104
105         ring = calloc(1, sizeof(*ring));
106         if (!ring) {
107                 ERROR_MSG("allocation failed");
108                 goto fail;
109         }
110
111         ring->bo = fd_rb_bo_new(pipe, size);
112         if (!ring->bo) {
113                 ERROR_MSG("ringbuffer allocation failed");
114                 goto fail;
115         }
116
117         ring->size = size;
118         ring->pipe = pipe;
119         ring->start = ring->bo->hostptr;
120         ring->end = &(ring->start[size/4]);
121
122         ring->cur = ring->last_start = ring->start;
123
124         return ring;
125 fail:
126         if (ring)
127                 fd_ringbuffer_del(ring);
128         return NULL;
129 }
130
131 void fd_ringbuffer_del(struct fd_ringbuffer *ring)
132 {
133         if (ring->bo)
134                 fd_rb_bo_del(ring->bo);
135         free(ring);
136 }
137
138 void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
139 {
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;
144 }
145
146 static int flush_impl(struct fd_ringbuffer *ring, uint32_t *last_start)
147 {
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,
153         };
154         struct kgsl_ringbuffer_issueibcmds req = {
155                         .drawctxt_id = ring->pipe->drawctxt_id,
156                         .ibdesc_addr = (unsigned long)&ibdesc,
157                         .numibs      = 1,
158                         .flags       = KGSL_CONTEXT_SUBMIT_IB_LIST,
159         };
160         int ret;
161
162         fd_pipe_pre_submit(ring->pipe);
163
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;
174         }
175
176         do {
177                 ret = ioctl(ring->pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
178         } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
179         if (ret)
180                 ERROR_MSG("issueibcmds failed!  %d (%s)", ret, strerror(errno));
181
182         ring->last_timestamp = req.timestamp;
183         ring->last_start = ring->cur;
184
185         fd_pipe_post_submit(ring->pipe, req.timestamp);
186
187         return ret;
188 }
189
190 /* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
191 int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
192 {
193         return flush_impl(ring, ring->last_start);
194 }
195
196 uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
197 {
198         return ring->last_timestamp;
199 }
200
201 void fd_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
202                 struct fd_bo *bo, uint32_t offset, uint32_t or)
203 {
204         (*ring->cur++) = fd_bo_gpuaddr(bo, offset) | or;
205         fd_pipe_add_submit(ring->pipe, bo);
206 }
207
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)
210 {
211         uint32_t addr = fd_bo_gpuaddr(bo, offset);
212         if (shift < 0)
213                 addr >>= -shift;
214         else
215                 addr <<= shift;
216         (*ring->cur++) = addr | or;
217         fd_pipe_add_submit(ring->pipe, bo);
218 }
219
220 void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
221                 struct fd_ringmarker *target)
222 {
223         (*ring->cur++) = target->ring->bo->gpuaddr +
224                         (uint8_t *)target->cur - (uint8_t *)target->ring->start;
225 }
226
227 struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
228 {
229         struct fd_ringmarker *marker = NULL;
230
231         marker = calloc(1, sizeof(*marker));
232         if (!marker) {
233                 ERROR_MSG("allocation failed");
234                 return NULL;
235         }
236
237         marker->ring = ring;
238
239         fd_ringmarker_mark(marker);
240
241         return marker;
242 }
243
244 void fd_ringmarker_del(struct fd_ringmarker *marker)
245 {
246         free(marker);
247 }
248
249 void fd_ringmarker_mark(struct fd_ringmarker *marker)
250 {
251         marker->cur = marker->ring->cur;
252 }
253
254 uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
255                 struct fd_ringmarker *end)
256 {
257         return end->cur - start->cur;
258 }
259
260 int fd_ringmarker_flush(struct fd_ringmarker *marker)
261 {
262         return flush_impl(marker->ring, marker->cur);
263 }