1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2013 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>
36 #include "freedreno_ringbuffer.h"
39 /* represents a single cmd buffer in the submit ioctl. Each cmd buffer has
40 * a backing bo, and a reloc table.
43 struct fd_ringbuffer *ring;
44 struct fd_bo *ring_bo;
47 struct drm_msm_gem_submit_reloc *relocs;
48 uint32_t nr_relocs, max_relocs;
51 struct msm_ringbuffer {
52 struct fd_ringbuffer base;
54 /* submit ioctl related tables:
55 * Note that bos and cmds are tracked by the parent ringbuffer, since
56 * that is global to the submit ioctl call. The reloc's table is tracked
61 struct drm_msm_gem_submit_bo *bos;
62 uint32_t nr_bos, max_bos;
65 struct drm_msm_gem_submit_cmd *cmds;
66 uint32_t nr_cmds, max_cmds;
69 /* should have matching entries in submit.bos: */
70 /* Note, only in parent ringbuffer */
72 uint32_t nr_bos, max_bos;
74 /* should have matching entries in submit.cmds: */
75 struct msm_cmd **cmds;
76 uint32_t nr_cmds, max_cmds;
78 /* current cmd-buffer: */
82 static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
83 drm_private extern pthread_mutex_t table_lock;
85 static void ring_bo_del(struct fd_device *dev, struct fd_bo *bo)
89 pthread_mutex_lock(&table_lock);
90 ret = fd_bo_cache_free(&to_msm_device(dev)->ring_cache, bo);
91 pthread_mutex_unlock(&table_lock);
99 static struct fd_bo * ring_bo_new(struct fd_device *dev, uint32_t size)
103 bo = fd_bo_cache_alloc(&to_msm_device(dev)->ring_cache, &size, 0);
107 bo = fd_bo_new(dev, size, 0);
111 /* keep ringbuffer bo's out of the normal bo cache: */
112 bo->bo_reuse = FALSE;
117 static void ring_cmd_del(struct msm_cmd *cmd)
120 ring_bo_del(cmd->ring->pipe->dev, cmd->ring_bo);
125 static struct msm_cmd * ring_cmd_new(struct fd_ringbuffer *ring, uint32_t size)
127 struct msm_cmd *cmd = calloc(1, sizeof(*cmd));
133 cmd->ring_bo = ring_bo_new(ring->pipe->dev, size);
144 static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
146 if ((nr + 1) > *max) {
147 if ((*max * 2) < (nr + 1))
151 ptr = realloc(ptr, *max * sz);
156 #define APPEND(x, name) ({ \
157 (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
158 (x)->nr_ ## name ++; \
161 static inline struct msm_ringbuffer * to_msm_ringbuffer(struct fd_ringbuffer *x)
163 return (struct msm_ringbuffer *)x;
166 static uint32_t append_bo(struct fd_ringbuffer *ring, struct fd_bo *bo)
168 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
171 idx = APPEND(&msm_ring->submit, bos);
172 idx = APPEND(msm_ring, bos);
174 msm_ring->submit.bos[idx].flags = 0;
175 msm_ring->submit.bos[idx].handle = bo->handle;
176 msm_ring->submit.bos[idx].presumed = to_msm_bo(bo)->presumed;
178 msm_ring->bos[idx] = fd_bo_ref(bo);
183 /* add (if needed) bo, return idx: */
184 static uint32_t bo2idx(struct fd_ringbuffer *ring, struct fd_bo *bo, uint32_t flags)
186 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
187 struct msm_bo *msm_bo = to_msm_bo(bo);
189 pthread_mutex_lock(&idx_lock);
190 if (!msm_bo->current_ring) {
191 idx = append_bo(ring, bo);
192 msm_bo->current_ring = ring;
194 } else if (msm_bo->current_ring == ring) {
198 for (idx = 0; idx < msm_ring->nr_bos; idx++)
199 if (msm_ring->bos[idx] == bo)
201 if (idx == msm_ring->nr_bos) {
203 idx = append_bo(ring, bo);
206 pthread_mutex_unlock(&idx_lock);
207 if (flags & FD_RELOC_READ)
208 msm_ring->submit.bos[idx].flags |= MSM_SUBMIT_BO_READ;
209 if (flags & FD_RELOC_WRITE)
210 msm_ring->submit.bos[idx].flags |= MSM_SUBMIT_BO_WRITE;
214 static int check_cmd_bo(struct fd_ringbuffer *ring,
215 struct drm_msm_gem_submit_cmd *cmd, struct fd_bo *bo)
217 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
218 return msm_ring->submit.bos[cmd->submit_idx].handle == bo->handle;
221 /* Ensure that submit has corresponding entry in cmds table for the
222 * target cmdstream buffer:
224 static void get_cmd(struct fd_ringbuffer *ring, struct msm_cmd *target_cmd,
225 uint32_t submit_offset, uint32_t size, uint32_t type)
227 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
228 struct drm_msm_gem_submit_cmd *cmd;
231 /* figure out if we already have a cmd buf: */
232 for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
233 cmd = &msm_ring->submit.cmds[i];
234 if ((cmd->submit_offset == submit_offset) &&
235 (cmd->size == size) &&
236 (cmd->type == type) &&
237 check_cmd_bo(ring, cmd, target_cmd->ring_bo))
241 /* create cmd buf if not: */
242 i = APPEND(&msm_ring->submit, cmds);
243 APPEND(msm_ring, cmds);
244 msm_ring->cmds[i] = target_cmd;
245 cmd = &msm_ring->submit.cmds[i];
247 cmd->submit_idx = bo2idx(ring, target_cmd->ring_bo, FD_RELOC_READ);
248 cmd->submit_offset = submit_offset;
253 static void * msm_ringbuffer_hostptr(struct fd_ringbuffer *ring)
255 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
256 return fd_bo_map(msm_ring->cmd->ring_bo);
259 static uint32_t find_next_reloc_idx(struct msm_cmd *msm_cmd,
260 uint32_t start, uint32_t offset)
264 /* a binary search would be more clever.. */
265 for (i = start; i < msm_cmd->nr_relocs; i++) {
266 struct drm_msm_gem_submit_reloc *reloc = &msm_cmd->relocs[i];
267 if (reloc->submit_offset >= offset)
274 static void flush_reset(struct fd_ringbuffer *ring)
276 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
279 for (i = 0; i < msm_ring->nr_bos; i++) {
280 struct msm_bo *msm_bo = to_msm_bo(msm_ring->bos[i]);
281 msm_bo->current_ring = NULL;
282 fd_bo_del(&msm_bo->base);
285 /* for each of the cmd buffers, clear their reloc's: */
286 for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
287 struct msm_cmd *target_cmd = msm_ring->cmds[i];
288 target_cmd->nr_relocs = 0;
291 msm_ring->cmd->nr_relocs = 0;
292 msm_ring->submit.nr_cmds = 0;
293 msm_ring->submit.nr_bos = 0;
294 msm_ring->nr_cmds = 0;
295 msm_ring->nr_bos = 0;
298 static void dump_submit(struct msm_ringbuffer *msm_ring)
302 for (i = 0; i < msm_ring->submit.nr_bos; i++) {
303 struct drm_msm_gem_submit_bo *bo = &msm_ring->submit.bos[i];
304 ERROR_MSG(" bos[%d]: handle=%u, flags=%x", i, bo->handle, bo->flags);
306 for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
307 struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
308 struct drm_msm_gem_submit_reloc *relocs = U642VOID(cmd->relocs);
309 ERROR_MSG(" cmd[%d]: type=%u, submit_idx=%u, submit_offset=%u, size=%u",
310 i, cmd->type, cmd->submit_idx, cmd->submit_offset, cmd->size);
311 for (j = 0; j < cmd->nr_relocs; j++) {
312 struct drm_msm_gem_submit_reloc *r = &relocs[j];
313 ERROR_MSG(" reloc[%d]: submit_offset=%u, or=%08x, shift=%d, reloc_idx=%u"
314 ", reloc_offset=%"PRIu64, j, r->submit_offset, r->or, r->shift,
315 r->reloc_idx, r->reloc_offset);
320 static int msm_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start)
322 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
323 struct drm_msm_gem_submit req = {
324 .pipe = to_msm_pipe(ring->pipe)->pipe,
326 uint32_t i, submit_offset, size;
329 submit_offset = offset_bytes(last_start, ring->start);
330 size = offset_bytes(ring->cur, last_start);
332 get_cmd(ring, msm_ring->cmd, submit_offset, size, MSM_SUBMIT_CMD_BUF);
334 /* needs to be after get_cmd() as that could create bos/cmds table: */
335 req.bos = VOID2U64(msm_ring->submit.bos),
336 req.nr_bos = msm_ring->submit.nr_bos;
337 req.cmds = VOID2U64(msm_ring->submit.cmds),
338 req.nr_cmds = msm_ring->submit.nr_cmds;
340 /* for each of the cmd's fix up their reloc's: */
341 for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
342 struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
343 struct msm_cmd *msm_cmd = msm_ring->cmds[i];
344 uint32_t a = find_next_reloc_idx(msm_cmd, 0, cmd->submit_offset);
345 uint32_t b = find_next_reloc_idx(msm_cmd, a, cmd->submit_offset + cmd->size);
346 cmd->relocs = VOID2U64(&msm_cmd->relocs[a]);
347 cmd->nr_relocs = (b > a) ? b - a : 0;
350 DEBUG_MSG("nr_cmds=%u, nr_bos=%u", req.nr_cmds, req.nr_bos);
352 ret = drmCommandWriteRead(ring->pipe->dev->fd, DRM_MSM_GEM_SUBMIT,
355 ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
356 dump_submit(msm_ring);
358 /* update timestamp on all rings associated with submit: */
359 for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
360 struct msm_cmd *msm_cmd = msm_ring->cmds[i];
361 msm_cmd->ring->last_timestamp = req.fence;
370 static void msm_ringbuffer_reset(struct fd_ringbuffer *ring)
375 static void msm_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
376 const struct fd_reloc *r)
378 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
379 struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring;
380 struct msm_bo *msm_bo = to_msm_bo(r->bo);
381 struct drm_msm_gem_submit_reloc *reloc;
382 uint32_t idx = APPEND(msm_ring->cmd, relocs);
385 reloc = &msm_ring->cmd->relocs[idx];
387 reloc->reloc_idx = bo2idx(parent, r->bo, r->flags);
388 reloc->reloc_offset = r->offset;
390 reloc->shift = r->shift;
391 reloc->submit_offset = offset_bytes(ring->cur, ring->start);
393 addr = msm_bo->presumed;
398 (*ring->cur++) = addr | r->or;
401 static void msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
402 struct fd_ringbuffer *target,
403 uint32_t submit_offset, uint32_t size)
405 struct msm_cmd *cmd = to_msm_ringbuffer(target)->cmd;
407 get_cmd(ring, cmd, submit_offset, size, MSM_SUBMIT_CMD_IB_TARGET_BUF);
409 msm_ringbuffer_emit_reloc(ring, &(struct fd_reloc){
411 .flags = FD_RELOC_READ,
412 .offset = submit_offset,
416 static void msm_ringbuffer_destroy(struct fd_ringbuffer *ring)
418 struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
420 ring_cmd_del(msm_ring->cmd);
421 free(msm_ring->submit.cmds);
422 free(msm_ring->submit.bos);
424 free(msm_ring->cmds);
428 static const struct fd_ringbuffer_funcs funcs = {
429 .hostptr = msm_ringbuffer_hostptr,
430 .flush = msm_ringbuffer_flush,
431 .reset = msm_ringbuffer_reset,
432 .emit_reloc = msm_ringbuffer_emit_reloc,
433 .emit_reloc_ring = msm_ringbuffer_emit_reloc_ring,
434 .destroy = msm_ringbuffer_destroy,
437 drm_private struct fd_ringbuffer * msm_ringbuffer_new(struct fd_pipe *pipe,
440 struct msm_ringbuffer *msm_ring;
441 struct fd_ringbuffer *ring = NULL;
443 msm_ring = calloc(1, sizeof(*msm_ring));
445 ERROR_MSG("allocation failed");
449 ring = &msm_ring->base;
450 ring->funcs = &funcs;
451 ring->pipe = pipe; /* needed in ring_cmd_new() */
453 msm_ring->cmd = ring_cmd_new(ring, size);
454 if (!msm_ring->cmd) {
455 ERROR_MSG("command buffer allocation failed");
462 fd_ringbuffer_del(ring);