OSDN Git Service

freedreno: add freedreno DRM
[android-x86/external-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         /* z180_cmdstream_issueibcmds() is made of fail: */
163         if (ring->pipe->id == FD_PIPE_2D) {
164                 /* fix up size field in last cmd packet */
165                 uint32_t last_size = (uint32_t)(ring->cur - last_start);
166                 /* 5 is length of first packet, 2 for the two 7f000000's */
167                 last_start[2] = last_size - (5 + 2);
168                 ibdesc.gpuaddr = ring->bo->gpuaddr;
169                 ibdesc.hostptr = ring->bo->hostptr;
170                 ibdesc.sizedwords = 0x145;
171                 req.timestamp = (uint32_t)ring->bo->hostptr;
172         }
173
174         do {
175                 ret = ioctl(ring->pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
176         } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
177         if (ret)
178                 ERROR_MSG("issueibcmds failed!  %d (%s)", ret, strerror(errno));
179
180         ring->last_timestamp = req.timestamp;
181         ring->last_start = ring->cur;
182
183         fd_pipe_process_submit(ring->pipe, req.timestamp);
184
185         return ret;
186 }
187
188 /* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
189 int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
190 {
191         return flush_impl(ring, ring->last_start);
192 }
193
194 uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
195 {
196         return ring->last_timestamp;
197 }
198
199 void fd_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
200                 struct fd_bo *bo, uint32_t offset, uint32_t or)
201 {
202         (*ring->cur++) = fd_bo_gpuaddr(bo, offset) | or;
203         fd_pipe_add_submit(ring->pipe, bo);
204 }
205
206 void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
207                 struct fd_ringmarker *target)
208 {
209         (*ring->cur++) = target->ring->bo->gpuaddr +
210                         (uint8_t *)target->cur - (uint8_t *)target->ring->start;
211 }
212
213 struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
214 {
215         struct fd_ringmarker *marker = NULL;
216
217         marker = calloc(1, sizeof(*marker));
218         if (!marker) {
219                 ERROR_MSG("allocation failed");
220                 return NULL;
221         }
222
223         marker->ring = ring;
224
225         fd_ringmarker_mark(marker);
226
227         return marker;
228 }
229
230 void fd_ringmarker_del(struct fd_ringmarker *marker)
231 {
232         free(marker);
233 }
234
235 void fd_ringmarker_mark(struct fd_ringmarker *marker)
236 {
237         marker->cur = marker->ring->cur;
238 }
239
240 uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
241                 struct fd_ringmarker *end)
242 {
243         return end->cur - start->cur;
244 }
245
246 int fd_ringmarker_flush(struct fd_ringmarker *marker)
247 {
248         return flush_impl(marker->ring, marker->cur);
249 }