1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2013 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 <assert.h>
30
31 #include "freedreno_ringbuffer.h"
32 #include "kgsl_priv.h"
33
34
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 :-/
38 */
39 struct kgsl_rb_bo {
40 struct kgsl_pipe *pipe;
41 void *hostptr;
42 uint32_t gpuaddr;
43 uint32_t size;
44 };
45
46 struct kgsl_ringbuffer {
47 struct fd_ringbuffer base;
48 struct kgsl_rb_bo *bo;
49 };
50
to_kgsl_ringbuffer(struct fd_ringbuffer * x)51 static inline struct kgsl_ringbuffer * to_kgsl_ringbuffer(struct fd_ringbuffer *x)
52 {
53 return (struct kgsl_ringbuffer *)x;
54 }
55
kgsl_rb_bo_del(struct kgsl_rb_bo * bo)56 static void kgsl_rb_bo_del(struct kgsl_rb_bo *bo)
57 {
58 struct kgsl_sharedmem_free req = {
59 .gpuaddr = bo->gpuaddr,
60 };
61 int ret;
62
63 drm_munmap(bo->hostptr, bo->size);
64
65 ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
66 if (ret) {
67 ERROR_MSG("sharedmem free failed: %s", strerror(errno));
68 }
69
70 free(bo);
71 }
72
kgsl_rb_bo_new(struct kgsl_pipe * pipe,uint32_t size)73 static struct kgsl_rb_bo * kgsl_rb_bo_new(struct kgsl_pipe *pipe, uint32_t size)
74 {
75 struct kgsl_rb_bo *bo;
76 struct kgsl_gpumem_alloc req = {
77 .size = ALIGN(size, 4096),
78 .flags = KGSL_MEMFLAGS_GPUREADONLY,
79 };
80 int ret;
81
82 bo = calloc(1, sizeof(*bo));
83 if (!bo) {
84 ERROR_MSG("allocation failed");
85 return NULL;
86 }
87 ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
88 if (ret) {
89 ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
90 goto fail;
91 }
92
93 bo->pipe = pipe;
94 bo->gpuaddr = req.gpuaddr;
95 bo->size = size;
96 bo->hostptr = drm_mmap(NULL, size, PROT_WRITE|PROT_READ,
97 MAP_SHARED, pipe->fd, req.gpuaddr);
98
99 return bo;
100 fail:
101 if (bo)
102 kgsl_rb_bo_del(bo);
103 return NULL;
104 }
105
kgsl_ringbuffer_hostptr(struct fd_ringbuffer * ring)106 static void * kgsl_ringbuffer_hostptr(struct fd_ringbuffer *ring)
107 {
108 struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
109 return kgsl_ring->bo->hostptr;
110 }
111
kgsl_ringbuffer_flush(struct fd_ringbuffer * ring,uint32_t * last_start,int in_fence_fd,int * out_fence_fd)112 static int kgsl_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start,
113 int in_fence_fd, int *out_fence_fd)
114 {
115 struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
116 struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(ring->pipe);
117 uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start;
118 struct kgsl_ibdesc ibdesc = {
119 .gpuaddr = kgsl_ring->bo->gpuaddr + offset,
120 .hostptr = last_start,
121 .sizedwords = ring->cur - last_start,
122 };
123 struct kgsl_ringbuffer_issueibcmds req = {
124 .drawctxt_id = kgsl_pipe->drawctxt_id,
125 .ibdesc_addr = (unsigned long)&ibdesc,
126 .numibs = 1,
127 .flags = KGSL_CONTEXT_SUBMIT_IB_LIST,
128 };
129 int ret;
130
131 assert(in_fence_fd == -1);
132 assert(out_fence_fd == NULL);
133
134 kgsl_pipe_pre_submit(kgsl_pipe);
135
136 /* z180_cmdstream_issueibcmds() is made of fail: */
137 if (ring->pipe->id == FD_PIPE_2D) {
138 /* fix up size field in last cmd packet */
139 uint32_t last_size = (uint32_t)(ring->cur - last_start);
140 /* 5 is length of first packet, 2 for the two 7f000000's */
141 last_start[2] = last_size - (5 + 2);
142 ibdesc.gpuaddr = kgsl_ring->bo->gpuaddr;
143 ibdesc.hostptr = kgsl_ring->bo->hostptr;
144 ibdesc.sizedwords = 0x145;
145 req.timestamp = (uintptr_t)kgsl_ring->bo->hostptr;
146 }
147
148 do {
149 ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
150 } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
151 if (ret)
152 ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno));
153
154 ring->last_timestamp = req.timestamp;
155 ring->last_start = ring->cur;
156
157 kgsl_pipe_post_submit(kgsl_pipe, req.timestamp);
158
159 return ret;
160 }
161
kgsl_ringbuffer_emit_reloc(struct fd_ringbuffer * ring,const struct fd_reloc * r)162 static void kgsl_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
163 const struct fd_reloc *r)
164 {
165 struct kgsl_bo *kgsl_bo = to_kgsl_bo(r->bo);
166 uint32_t addr = kgsl_bo_gpuaddr(kgsl_bo, r->offset);
167 assert(addr);
168 if (r->shift < 0)
169 addr >>= -r->shift;
170 else
171 addr <<= r->shift;
172 (*ring->cur++) = addr | r->or;
173 kgsl_pipe_add_submit(to_kgsl_pipe(ring->pipe), kgsl_bo);
174 }
175
kgsl_ringbuffer_emit_reloc_ring(struct fd_ringbuffer * ring,struct fd_ringbuffer * target,uint32_t cmd_idx,uint32_t submit_offset,uint32_t size)176 static uint32_t kgsl_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
177 struct fd_ringbuffer *target, uint32_t cmd_idx,
178 uint32_t submit_offset, uint32_t size)
179 {
180 struct kgsl_ringbuffer *target_ring = to_kgsl_ringbuffer(target);
181 assert(cmd_idx == 0);
182 (*ring->cur++) = target_ring->bo->gpuaddr + submit_offset;
183 return size;
184 }
185
kgsl_ringbuffer_destroy(struct fd_ringbuffer * ring)186 static void kgsl_ringbuffer_destroy(struct fd_ringbuffer *ring)
187 {
188 struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
189 if (ring->last_timestamp)
190 fd_pipe_wait(ring->pipe, ring->last_timestamp);
191 if (kgsl_ring->bo)
192 kgsl_rb_bo_del(kgsl_ring->bo);
193 free(kgsl_ring);
194 }
195
196 static const struct fd_ringbuffer_funcs funcs = {
197 .hostptr = kgsl_ringbuffer_hostptr,
198 .flush = kgsl_ringbuffer_flush,
199 .emit_reloc = kgsl_ringbuffer_emit_reloc,
200 .emit_reloc_ring = kgsl_ringbuffer_emit_reloc_ring,
201 .destroy = kgsl_ringbuffer_destroy,
202 };
203
kgsl_ringbuffer_new(struct fd_pipe * pipe,uint32_t size)204 drm_private struct fd_ringbuffer * kgsl_ringbuffer_new(struct fd_pipe *pipe,
205 uint32_t size)
206 {
207 struct kgsl_ringbuffer *kgsl_ring;
208 struct fd_ringbuffer *ring = NULL;
209
210 kgsl_ring = calloc(1, sizeof(*kgsl_ring));
211 if (!kgsl_ring) {
212 ERROR_MSG("allocation failed");
213 goto fail;
214 }
215
216 ring = &kgsl_ring->base;
217 ring->funcs = &funcs;
218 ring->size = size;
219
220 kgsl_ring->bo = kgsl_rb_bo_new(to_kgsl_pipe(pipe), size);
221 if (!kgsl_ring->bo) {
222 ERROR_MSG("ringbuffer allocation failed");
223 goto fail;
224 }
225
226 return ring;
227 fail:
228 if (ring)
229 fd_ringbuffer_del(ring);
230 return NULL;
231 }
232