msm_ringbuffer.c revision e88f27b3
1e88f27b3Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */ 2e88f27b3Smrg 3e88f27b3Smrg/* 4e88f27b3Smrg * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org> 5e88f27b3Smrg * 6e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7e88f27b3Smrg * copy of this software and associated documentation files (the "Software"), 8e88f27b3Smrg * to deal in the Software without restriction, including without limitation 9e88f27b3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10e88f27b3Smrg * and/or sell copies of the Software, and to permit persons to whom the 11e88f27b3Smrg * Software is furnished to do so, subject to the following conditions: 12e88f27b3Smrg * 13e88f27b3Smrg * The above copyright notice and this permission notice (including the next 14e88f27b3Smrg * paragraph) shall be included in all copies or substantial portions of the 15e88f27b3Smrg * Software. 16e88f27b3Smrg * 17e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18e88f27b3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19e88f27b3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20e88f27b3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22e88f27b3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23e88f27b3Smrg * SOFTWARE. 24e88f27b3Smrg * 25e88f27b3Smrg * Authors: 26e88f27b3Smrg * Rob Clark <robclark@freedesktop.org> 27e88f27b3Smrg */ 28e88f27b3Smrg 29e88f27b3Smrg#include <assert.h> 30e88f27b3Smrg 31e88f27b3Smrg#include "freedreno_ringbuffer.h" 32e88f27b3Smrg#include "msm_priv.h" 33e88f27b3Smrg 34e88f27b3Smrgstruct msm_ringbuffer { 35e88f27b3Smrg struct fd_ringbuffer base; 36e88f27b3Smrg struct fd_bo *ring_bo; 37e88f27b3Smrg 38e88f27b3Smrg struct list_head submit_list; 39e88f27b3Smrg 40e88f27b3Smrg /* bo's table: */ 41e88f27b3Smrg struct drm_msm_gem_submit_bo *bos; 42e88f27b3Smrg uint32_t nr_bos, max_bos; 43e88f27b3Smrg 44e88f27b3Smrg /* cmd's table: */ 45e88f27b3Smrg struct drm_msm_gem_submit_cmd *cmds; 46e88f27b3Smrg uint32_t nr_cmds, max_cmds; 47e88f27b3Smrg struct fd_ringbuffer **rings; 48e88f27b3Smrg uint32_t nr_rings, max_rings; 49e88f27b3Smrg 50e88f27b3Smrg /* reloc's table: */ 51e88f27b3Smrg struct drm_msm_gem_submit_reloc *relocs; 52e88f27b3Smrg uint32_t nr_relocs, max_relocs; 53e88f27b3Smrg}; 54e88f27b3Smrg 55e88f27b3Smrgstatic void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz) 56e88f27b3Smrg{ 57e88f27b3Smrg if ((nr + 1) > *max) { 58e88f27b3Smrg if ((*max * 2) < (nr + 1)) 59e88f27b3Smrg *max = nr + 5; 60e88f27b3Smrg else 61e88f27b3Smrg *max = *max * 2; 62e88f27b3Smrg ptr = realloc(ptr, *max * sz); 63e88f27b3Smrg } 64e88f27b3Smrg return ptr; 65e88f27b3Smrg} 66e88f27b3Smrg 67e88f27b3Smrg#define APPEND(x, name) ({ \ 68e88f27b3Smrg (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \ 69e88f27b3Smrg (x)->nr_ ## name ++; \ 70e88f27b3Smrg}) 71e88f27b3Smrg 72e88f27b3Smrgstatic inline struct msm_ringbuffer * to_msm_ringbuffer(struct fd_ringbuffer *x) 73e88f27b3Smrg{ 74e88f27b3Smrg return (struct msm_ringbuffer *)x; 75e88f27b3Smrg} 76e88f27b3Smrg 77e88f27b3Smrg/* add (if needed) bo, return idx: */ 78e88f27b3Smrgstatic uint32_t bo2idx(struct fd_ringbuffer *ring, struct fd_bo *bo, uint32_t flags) 79e88f27b3Smrg{ 80e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 81e88f27b3Smrg struct msm_bo *msm_bo = to_msm_bo(bo); 82e88f27b3Smrg int id = ring->pipe->id; 83e88f27b3Smrg uint32_t idx; 84e88f27b3Smrg if (!msm_bo->indexp1[id]) { 85e88f27b3Smrg struct list_head *list = &msm_bo->list[id]; 86e88f27b3Smrg idx = APPEND(msm_ring, bos); 87e88f27b3Smrg msm_ring->bos[idx].flags = 0; 88e88f27b3Smrg msm_ring->bos[idx].handle = bo->handle; 89e88f27b3Smrg msm_ring->bos[idx].presumed = msm_bo->presumed; 90e88f27b3Smrg msm_bo->indexp1[id] = idx + 1; 91e88f27b3Smrg 92e88f27b3Smrg assert(LIST_IS_EMPTY(list)); 93e88f27b3Smrg fd_bo_ref(bo); 94e88f27b3Smrg list_addtail(list, &msm_ring->submit_list); 95e88f27b3Smrg } else { 96e88f27b3Smrg idx = msm_bo->indexp1[id] - 1; 97e88f27b3Smrg } 98e88f27b3Smrg if (flags & FD_RELOC_READ) 99e88f27b3Smrg msm_ring->bos[idx].flags |= MSM_SUBMIT_BO_READ; 100e88f27b3Smrg if (flags & FD_RELOC_WRITE) 101e88f27b3Smrg msm_ring->bos[idx].flags |= MSM_SUBMIT_BO_WRITE; 102e88f27b3Smrg return idx; 103e88f27b3Smrg} 104e88f27b3Smrg 105e88f27b3Smrgstatic int check_cmd_bo(struct fd_ringbuffer *ring, 106e88f27b3Smrg struct drm_msm_gem_submit_cmd *cmd, struct fd_bo *bo) 107e88f27b3Smrg{ 108e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 109e88f27b3Smrg return msm_ring->bos[cmd->submit_idx].handle == bo->handle; 110e88f27b3Smrg} 111e88f27b3Smrg 112e88f27b3Smrgstatic uint32_t offset_bytes(void *end, void *start) 113e88f27b3Smrg{ 114e88f27b3Smrg return ((char *)end) - ((char *)start); 115e88f27b3Smrg} 116e88f27b3Smrg 117e88f27b3Smrgstatic struct drm_msm_gem_submit_cmd * get_cmd(struct fd_ringbuffer *ring, 118e88f27b3Smrg struct fd_ringbuffer *target_ring, struct fd_bo *target_bo, 119e88f27b3Smrg uint32_t submit_offset, uint32_t size, uint32_t type) 120e88f27b3Smrg{ 121e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 122e88f27b3Smrg struct drm_msm_gem_submit_cmd *cmd = NULL; 123e88f27b3Smrg uint32_t i; 124e88f27b3Smrg 125e88f27b3Smrg /* figure out if we already have a cmd buf: */ 126e88f27b3Smrg for (i = 0; i < msm_ring->nr_cmds; i++) { 127e88f27b3Smrg cmd = &msm_ring->cmds[i]; 128e88f27b3Smrg if ((cmd->submit_offset == submit_offset) && 129e88f27b3Smrg (cmd->size == size) && 130e88f27b3Smrg (cmd->type == type) && 131e88f27b3Smrg check_cmd_bo(ring, cmd, target_bo)) 132e88f27b3Smrg break; 133e88f27b3Smrg cmd = NULL; 134e88f27b3Smrg } 135e88f27b3Smrg 136e88f27b3Smrg /* create cmd buf if not: */ 137e88f27b3Smrg if (!cmd) { 138e88f27b3Smrg uint32_t idx = APPEND(msm_ring, cmds); 139e88f27b3Smrg APPEND(msm_ring, rings); 140e88f27b3Smrg msm_ring->rings[idx] = target_ring; 141e88f27b3Smrg cmd = &msm_ring->cmds[idx]; 142e88f27b3Smrg cmd->type = type; 143e88f27b3Smrg cmd->submit_idx = bo2idx(ring, target_bo, FD_RELOC_READ); 144e88f27b3Smrg cmd->submit_offset = submit_offset; 145e88f27b3Smrg cmd->size = size; 146e88f27b3Smrg } 147e88f27b3Smrg 148e88f27b3Smrg return cmd; 149e88f27b3Smrg} 150e88f27b3Smrg 151e88f27b3Smrgstatic void * msm_ringbuffer_hostptr(struct fd_ringbuffer *ring) 152e88f27b3Smrg{ 153e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 154e88f27b3Smrg return fd_bo_map(msm_ring->ring_bo); 155e88f27b3Smrg} 156e88f27b3Smrg 157e88f27b3Smrgstatic uint32_t find_next_reloc_idx(struct msm_ringbuffer *msm_ring, 158e88f27b3Smrg uint32_t start, uint32_t offset) 159e88f27b3Smrg{ 160e88f27b3Smrg uint32_t i; 161e88f27b3Smrg 162e88f27b3Smrg /* a binary search would be more clever.. */ 163e88f27b3Smrg for (i = start; i < msm_ring->nr_relocs; i++) { 164e88f27b3Smrg struct drm_msm_gem_submit_reloc *reloc = &msm_ring->relocs[i]; 165e88f27b3Smrg if (reloc->submit_offset >= offset) 166e88f27b3Smrg return i; 167e88f27b3Smrg } 168e88f27b3Smrg 169e88f27b3Smrg return i; 170e88f27b3Smrg} 171e88f27b3Smrg 172e88f27b3Smrgstatic int msm_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start) 173e88f27b3Smrg{ 174e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 175e88f27b3Smrg struct fd_bo *ring_bo = msm_ring->ring_bo; 176e88f27b3Smrg struct drm_msm_gem_submit req = { 177e88f27b3Smrg .pipe = to_msm_pipe(ring->pipe)->pipe, 178e88f27b3Smrg }; 179e88f27b3Smrg struct msm_bo *msm_bo = NULL, *tmp; 180e88f27b3Smrg uint32_t i, submit_offset, size; 181e88f27b3Smrg int ret, id = ring->pipe->id; 182e88f27b3Smrg 183e88f27b3Smrg submit_offset = offset_bytes(last_start, ring->start); 184e88f27b3Smrg size = offset_bytes(ring->cur, last_start); 185e88f27b3Smrg 186e88f27b3Smrg get_cmd(ring, ring, ring_bo, submit_offset, size, MSM_SUBMIT_CMD_BUF); 187e88f27b3Smrg 188e88f27b3Smrg /* needs to be after get_cmd() as that could create bos/cmds table: */ 189e88f27b3Smrg req.bos = VOID2U64(msm_ring->bos), 190e88f27b3Smrg req.nr_bos = msm_ring->nr_bos; 191e88f27b3Smrg req.cmds = VOID2U64(msm_ring->cmds), 192e88f27b3Smrg req.nr_cmds = msm_ring->nr_cmds; 193e88f27b3Smrg 194e88f27b3Smrg /* for each of the cmd's fix up their reloc's: */ 195e88f27b3Smrg for (i = 0; i < msm_ring->nr_cmds; i++) { 196e88f27b3Smrg struct drm_msm_gem_submit_cmd *cmd = &msm_ring->cmds[i]; 197e88f27b3Smrg struct msm_ringbuffer *target_ring = to_msm_ringbuffer(msm_ring->rings[i]); 198e88f27b3Smrg uint32_t a = find_next_reloc_idx(target_ring, 0, cmd->submit_offset); 199e88f27b3Smrg uint32_t b = find_next_reloc_idx(target_ring, a, cmd->submit_offset + cmd->size); 200e88f27b3Smrg cmd->relocs = VOID2U64(&target_ring->relocs[a]); 201e88f27b3Smrg cmd->nr_relocs = (b > a) ? b - a : 0; 202e88f27b3Smrg } 203e88f27b3Smrg 204e88f27b3Smrg DEBUG_MSG("nr_cmds=%u, nr_bos=%u\n", req.nr_cmds, req.nr_bos); 205e88f27b3Smrg 206e88f27b3Smrg ret = drmCommandWriteRead(ring->pipe->dev->fd, DRM_MSM_GEM_SUBMIT, 207e88f27b3Smrg &req, sizeof(req)); 208e88f27b3Smrg if (ret) 209e88f27b3Smrg ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno)); 210e88f27b3Smrg 211e88f27b3Smrg LIST_FOR_EACH_ENTRY_SAFE(msm_bo, tmp, &msm_ring->submit_list, list[id]) { 212e88f27b3Smrg struct list_head *list = &msm_bo->list[id]; 213e88f27b3Smrg list_delinit(list); 214e88f27b3Smrg msm_bo->indexp1[id] = 0; 215e88f27b3Smrg fd_bo_del(&msm_bo->base); 216e88f27b3Smrg } 217e88f27b3Smrg 218e88f27b3Smrg /* for each of the cmd buffers, clear their reloc's: */ 219e88f27b3Smrg for (i = 0; i < msm_ring->nr_cmds; i++) { 220e88f27b3Smrg struct msm_ringbuffer *target_ring = to_msm_ringbuffer(msm_ring->rings[i]); 221e88f27b3Smrg target_ring->nr_relocs = 0; 222e88f27b3Smrg } 223e88f27b3Smrg 224e88f27b3Smrg msm_ring->nr_cmds = 0; 225e88f27b3Smrg msm_ring->nr_bos = 0; 226e88f27b3Smrg 227e88f27b3Smrg return ret; 228e88f27b3Smrg} 229e88f27b3Smrg 230e88f27b3Smrgstatic void msm_ringbuffer_emit_reloc(struct fd_ringbuffer *ring, 231e88f27b3Smrg const struct fd_reloc *r) 232e88f27b3Smrg{ 233e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 234e88f27b3Smrg struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring; 235e88f27b3Smrg struct msm_bo *msm_bo = to_msm_bo(r->bo); 236e88f27b3Smrg struct drm_msm_gem_submit_reloc *reloc; 237e88f27b3Smrg uint32_t idx = APPEND(msm_ring, relocs); 238e88f27b3Smrg uint32_t addr; 239e88f27b3Smrg 240e88f27b3Smrg reloc = &msm_ring->relocs[idx]; 241e88f27b3Smrg 242e88f27b3Smrg reloc->reloc_idx = bo2idx(parent, r->bo, r->flags); 243e88f27b3Smrg reloc->reloc_offset = r->offset; 244e88f27b3Smrg reloc->or = r->or; 245e88f27b3Smrg reloc->shift = r->shift; 246e88f27b3Smrg reloc->submit_offset = offset_bytes(ring->cur, ring->start); 247e88f27b3Smrg 248e88f27b3Smrg addr = msm_bo->presumed; 249e88f27b3Smrg if (r->shift < 0) 250e88f27b3Smrg addr >>= -r->shift; 251e88f27b3Smrg else 252e88f27b3Smrg addr <<= r->shift; 253e88f27b3Smrg (*ring->cur++) = addr | r->or; 254e88f27b3Smrg} 255e88f27b3Smrg 256e88f27b3Smrgstatic void msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring, 257e88f27b3Smrg struct fd_ringmarker *target, struct fd_ringmarker *end) 258e88f27b3Smrg{ 259e88f27b3Smrg struct fd_bo *target_bo = to_msm_ringbuffer(target->ring)->ring_bo; 260e88f27b3Smrg struct drm_msm_gem_submit_cmd *cmd; 261e88f27b3Smrg uint32_t submit_offset, size; 262e88f27b3Smrg 263e88f27b3Smrg submit_offset = offset_bytes(target->cur, target->ring->start); 264e88f27b3Smrg size = offset_bytes(end->cur, target->cur); 265e88f27b3Smrg 266e88f27b3Smrg cmd = get_cmd(ring, target->ring, target_bo, submit_offset, size, 267e88f27b3Smrg MSM_SUBMIT_CMD_IB_TARGET_BUF); 268e88f27b3Smrg assert(cmd); 269e88f27b3Smrg 270e88f27b3Smrg msm_ringbuffer_emit_reloc(ring, &(struct fd_reloc){ 271e88f27b3Smrg .bo = target_bo, 272e88f27b3Smrg .flags = FD_RELOC_READ, 273e88f27b3Smrg .offset = submit_offset, 274e88f27b3Smrg }); 275e88f27b3Smrg} 276e88f27b3Smrg 277e88f27b3Smrgstatic void msm_ringbuffer_destroy(struct fd_ringbuffer *ring) 278e88f27b3Smrg{ 279e88f27b3Smrg struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring); 280e88f27b3Smrg if (msm_ring->ring_bo) 281e88f27b3Smrg fd_bo_del(msm_ring->ring_bo); 282e88f27b3Smrg free(msm_ring); 283e88f27b3Smrg} 284e88f27b3Smrg 285e88f27b3Smrgstatic struct fd_ringbuffer_funcs funcs = { 286e88f27b3Smrg .hostptr = msm_ringbuffer_hostptr, 287e88f27b3Smrg .flush = msm_ringbuffer_flush, 288e88f27b3Smrg .emit_reloc = msm_ringbuffer_emit_reloc, 289e88f27b3Smrg .emit_reloc_ring = msm_ringbuffer_emit_reloc_ring, 290e88f27b3Smrg .destroy = msm_ringbuffer_destroy, 291e88f27b3Smrg}; 292e88f27b3Smrg 293e88f27b3Smrgstruct fd_ringbuffer * msm_ringbuffer_new(struct fd_pipe *pipe, 294e88f27b3Smrg uint32_t size) 295e88f27b3Smrg{ 296e88f27b3Smrg struct msm_ringbuffer *msm_ring; 297e88f27b3Smrg struct fd_ringbuffer *ring = NULL; 298e88f27b3Smrg 299e88f27b3Smrg msm_ring = calloc(1, sizeof(*msm_ring)); 300e88f27b3Smrg if (!msm_ring) { 301e88f27b3Smrg ERROR_MSG("allocation failed"); 302e88f27b3Smrg goto fail; 303e88f27b3Smrg } 304e88f27b3Smrg 305e88f27b3Smrg ring = &msm_ring->base; 306e88f27b3Smrg ring->funcs = &funcs; 307e88f27b3Smrg 308e88f27b3Smrg list_inithead(&msm_ring->submit_list); 309e88f27b3Smrg 310e88f27b3Smrg msm_ring->ring_bo = fd_bo_new(pipe->dev, size, 0); 311e88f27b3Smrg if (!msm_ring->ring_bo) { 312e88f27b3Smrg ERROR_MSG("ringbuffer allocation failed"); 313e88f27b3Smrg goto fail; 314e88f27b3Smrg } 315e88f27b3Smrg 316e88f27b3Smrg return ring; 317e88f27b3Smrgfail: 318e88f27b3Smrg if (ring) 319e88f27b3Smrg fd_ringbuffer_del(ring); 320e88f27b3Smrg return NULL; 321e88f27b3Smrg} 322