etnaviv_cmd_stream.c revision d8807b2f
1037b3c26Smrg/* 2037b3c26Smrg * Copyright (C) 2014-2015 Etnaviv Project 3037b3c26Smrg * 4037b3c26Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5037b3c26Smrg * copy of this software and associated documentation files (the "Software"), 6037b3c26Smrg * to deal in the Software without restriction, including without limitation 7037b3c26Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8037b3c26Smrg * and/or sell copies of the Software, and to permit persons to whom the 9037b3c26Smrg * Software is furnished to do so, subject to the following conditions: 10037b3c26Smrg * 11037b3c26Smrg * The above copyright notice and this permission notice (including the next 12037b3c26Smrg * paragraph) shall be included in all copies or substantial portions of the 13037b3c26Smrg * Software. 14037b3c26Smrg * 15037b3c26Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16037b3c26Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17037b3c26Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18037b3c26Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19037b3c26Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20037b3c26Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21037b3c26Smrg * SOFTWARE. 22037b3c26Smrg * 23037b3c26Smrg * Authors: 24037b3c26Smrg * Christian Gmeiner <christian.gmeiner@gmail.com> 25037b3c26Smrg */ 26037b3c26Smrg 27037b3c26Smrg#ifdef HAVE_CONFIG_H 28037b3c26Smrg# include <config.h> 29037b3c26Smrg#endif 30037b3c26Smrg 31037b3c26Smrg#include <assert.h> 32037b3c26Smrg 33037b3c26Smrg#include "etnaviv_drmif.h" 34037b3c26Smrg#include "etnaviv_priv.h" 35037b3c26Smrg 36037b3c26Smrgstatic pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER; 37037b3c26Smrg 38037b3c26Smrgstatic void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz) 39037b3c26Smrg{ 40037b3c26Smrg if ((nr + 1) > *max) { 41037b3c26Smrg if ((*max * 2) < (nr + 1)) 42037b3c26Smrg *max = nr + 5; 43037b3c26Smrg else 44037b3c26Smrg *max = *max * 2; 45037b3c26Smrg ptr = realloc(ptr, *max * sz); 46037b3c26Smrg } 47037b3c26Smrg 48037b3c26Smrg return ptr; 49037b3c26Smrg} 50037b3c26Smrg 51037b3c26Smrg#define APPEND(x, name) ({ \ 52037b3c26Smrg (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \ 53037b3c26Smrg (x)->nr_ ## name ++; \ 54037b3c26Smrg}) 55037b3c26Smrg 56037b3c26Smrgstatic inline struct etna_cmd_stream_priv * 57037b3c26Smrgetna_cmd_stream_priv(struct etna_cmd_stream *stream) 58037b3c26Smrg{ 59037b3c26Smrg return (struct etna_cmd_stream_priv *)stream; 60037b3c26Smrg} 61037b3c26Smrg 62037b3c26Smrgstruct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe, uint32_t size, 63037b3c26Smrg void (*reset_notify)(struct etna_cmd_stream *stream, void *priv), 64037b3c26Smrg void *priv) 65037b3c26Smrg{ 66037b3c26Smrg struct etna_cmd_stream_priv *stream = NULL; 67037b3c26Smrg 68037b3c26Smrg if (size == 0) { 69037b3c26Smrg ERROR_MSG("invalid size of 0"); 70037b3c26Smrg goto fail; 71037b3c26Smrg } 72037b3c26Smrg 73037b3c26Smrg stream = calloc(1, sizeof(*stream)); 74037b3c26Smrg if (!stream) { 75037b3c26Smrg ERROR_MSG("allocation failed"); 76037b3c26Smrg goto fail; 77037b3c26Smrg } 78037b3c26Smrg 79037b3c26Smrg /* allocate even number of 32-bit words */ 80037b3c26Smrg size = ALIGN(size, 2); 81037b3c26Smrg 82037b3c26Smrg stream->base.buffer = malloc(size * sizeof(uint32_t)); 83037b3c26Smrg if (!stream->base.buffer) { 84037b3c26Smrg ERROR_MSG("allocation failed"); 85037b3c26Smrg goto fail; 86037b3c26Smrg } 87037b3c26Smrg 88037b3c26Smrg stream->base.size = size; 89037b3c26Smrg stream->pipe = pipe; 90037b3c26Smrg stream->reset_notify = reset_notify; 91037b3c26Smrg stream->reset_notify_priv = priv; 92037b3c26Smrg 93037b3c26Smrg return &stream->base; 94037b3c26Smrg 95037b3c26Smrgfail: 96037b3c26Smrg if (stream) 97037b3c26Smrg etna_cmd_stream_del(&stream->base); 98037b3c26Smrg 99037b3c26Smrg return NULL; 100037b3c26Smrg} 101037b3c26Smrg 102037b3c26Smrgvoid etna_cmd_stream_del(struct etna_cmd_stream *stream) 103037b3c26Smrg{ 104037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 105037b3c26Smrg 106037b3c26Smrg free(stream->buffer); 107037b3c26Smrg free(priv->submit.relocs); 108037b3c26Smrg free(priv); 109037b3c26Smrg} 110037b3c26Smrg 111037b3c26Smrgstatic void reset_buffer(struct etna_cmd_stream *stream) 112037b3c26Smrg{ 113037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 114037b3c26Smrg 115037b3c26Smrg stream->offset = 0; 116037b3c26Smrg priv->submit.nr_bos = 0; 117037b3c26Smrg priv->submit.nr_relocs = 0; 118037b3c26Smrg priv->nr_bos = 0; 119037b3c26Smrg 120037b3c26Smrg if (priv->reset_notify) 121037b3c26Smrg priv->reset_notify(stream, priv->reset_notify_priv); 122037b3c26Smrg} 123037b3c26Smrg 124037b3c26Smrguint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream) 125037b3c26Smrg{ 126037b3c26Smrg return etna_cmd_stream_priv(stream)->last_timestamp; 127037b3c26Smrg} 128037b3c26Smrg 129037b3c26Smrgstatic uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo) 130037b3c26Smrg{ 131037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 132037b3c26Smrg uint32_t idx; 133037b3c26Smrg 134037b3c26Smrg idx = APPEND(&priv->submit, bos); 135037b3c26Smrg idx = APPEND(priv, bos); 136037b3c26Smrg 137037b3c26Smrg priv->submit.bos[idx].flags = 0; 138037b3c26Smrg priv->submit.bos[idx].handle = bo->handle; 139037b3c26Smrg 140037b3c26Smrg priv->bos[idx] = etna_bo_ref(bo); 141037b3c26Smrg 142037b3c26Smrg return idx; 143037b3c26Smrg} 144037b3c26Smrg 145037b3c26Smrg/* add (if needed) bo, return idx: */ 146037b3c26Smrgstatic uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo, 147037b3c26Smrg uint32_t flags) 148037b3c26Smrg{ 149037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 150037b3c26Smrg uint32_t idx; 151037b3c26Smrg 152037b3c26Smrg pthread_mutex_lock(&idx_lock); 153037b3c26Smrg 154037b3c26Smrg if (!bo->current_stream) { 155037b3c26Smrg idx = append_bo(stream, bo); 156037b3c26Smrg bo->current_stream = stream; 157037b3c26Smrg bo->idx = idx; 158037b3c26Smrg } else if (bo->current_stream == stream) { 159037b3c26Smrg idx = bo->idx; 160037b3c26Smrg } else { 161037b3c26Smrg /* slow-path: */ 162037b3c26Smrg for (idx = 0; idx < priv->nr_bos; idx++) 163037b3c26Smrg if (priv->bos[idx] == bo) 164037b3c26Smrg break; 165037b3c26Smrg if (idx == priv->nr_bos) { 166037b3c26Smrg /* not found */ 167037b3c26Smrg idx = append_bo(stream, bo); 168037b3c26Smrg } 169037b3c26Smrg } 170037b3c26Smrg pthread_mutex_unlock(&idx_lock); 171037b3c26Smrg 172037b3c26Smrg if (flags & ETNA_RELOC_READ) 173037b3c26Smrg priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ; 174037b3c26Smrg if (flags & ETNA_RELOC_WRITE) 175037b3c26Smrg priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE; 176037b3c26Smrg 177037b3c26Smrg return idx; 178037b3c26Smrg} 179037b3c26Smrg 180d8807b2fSmrgstatic void flush(struct etna_cmd_stream *stream, int in_fence_fd, 181d8807b2fSmrg int *out_fence_fd) 182037b3c26Smrg{ 183037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 184037b3c26Smrg int ret, id = priv->pipe->id; 185037b3c26Smrg struct etna_gpu *gpu = priv->pipe->gpu; 186037b3c26Smrg 187037b3c26Smrg struct drm_etnaviv_gem_submit req = { 188037b3c26Smrg .pipe = gpu->core, 189037b3c26Smrg .exec_state = id, 190037b3c26Smrg .bos = VOID2U64(priv->submit.bos), 191037b3c26Smrg .nr_bos = priv->submit.nr_bos, 192037b3c26Smrg .relocs = VOID2U64(priv->submit.relocs), 193037b3c26Smrg .nr_relocs = priv->submit.nr_relocs, 194037b3c26Smrg .stream = VOID2U64(stream->buffer), 195037b3c26Smrg .stream_size = stream->offset * 4, /* in bytes */ 196037b3c26Smrg }; 197037b3c26Smrg 198d8807b2fSmrg if (in_fence_fd != -1) { 199d8807b2fSmrg req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT; 200d8807b2fSmrg req.fence_fd = in_fence_fd; 201d8807b2fSmrg } 202d8807b2fSmrg 203d8807b2fSmrg if (out_fence_fd) 204d8807b2fSmrg req.flags |= ETNA_SUBMIT_FENCE_FD_OUT; 205d8807b2fSmrg 206037b3c26Smrg ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT, 207037b3c26Smrg &req, sizeof(req)); 208037b3c26Smrg 209037b3c26Smrg if (ret) 210037b3c26Smrg ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno)); 211037b3c26Smrg else 212037b3c26Smrg priv->last_timestamp = req.fence; 213037b3c26Smrg 214037b3c26Smrg for (uint32_t i = 0; i < priv->nr_bos; i++) { 215037b3c26Smrg struct etna_bo *bo = priv->bos[i]; 216037b3c26Smrg 217037b3c26Smrg bo->current_stream = NULL; 218037b3c26Smrg etna_bo_del(bo); 219037b3c26Smrg } 220d8807b2fSmrg 221d8807b2fSmrg if (out_fence_fd) 222d8807b2fSmrg *out_fence_fd = req.fence_fd; 223037b3c26Smrg} 224037b3c26Smrg 225037b3c26Smrgvoid etna_cmd_stream_flush(struct etna_cmd_stream *stream) 226037b3c26Smrg{ 227d8807b2fSmrg flush(stream, -1, NULL); 228d8807b2fSmrg reset_buffer(stream); 229d8807b2fSmrg} 230d8807b2fSmrg 231d8807b2fSmrgvoid etna_cmd_stream_flush2(struct etna_cmd_stream *stream, int in_fence_fd, 232d8807b2fSmrg int *out_fence_fd) 233d8807b2fSmrg{ 234d8807b2fSmrg flush(stream, in_fence_fd, out_fence_fd); 235037b3c26Smrg reset_buffer(stream); 236037b3c26Smrg} 237037b3c26Smrg 238037b3c26Smrgvoid etna_cmd_stream_finish(struct etna_cmd_stream *stream) 239037b3c26Smrg{ 240037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 241037b3c26Smrg 242d8807b2fSmrg flush(stream, -1, NULL); 243037b3c26Smrg etna_pipe_wait(priv->pipe, priv->last_timestamp, 5000); 244037b3c26Smrg reset_buffer(stream); 245037b3c26Smrg} 246037b3c26Smrg 247037b3c26Smrgvoid etna_cmd_stream_reloc(struct etna_cmd_stream *stream, const struct etna_reloc *r) 248037b3c26Smrg{ 249037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 250037b3c26Smrg struct drm_etnaviv_gem_submit_reloc *reloc; 251037b3c26Smrg uint32_t idx = APPEND(&priv->submit, relocs); 252037b3c26Smrg uint32_t addr = 0; 253037b3c26Smrg 254037b3c26Smrg reloc = &priv->submit.relocs[idx]; 255037b3c26Smrg 256037b3c26Smrg reloc->reloc_idx = bo2idx(stream, r->bo, r->flags); 257037b3c26Smrg reloc->reloc_offset = r->offset; 258037b3c26Smrg reloc->submit_offset = stream->offset * 4; /* in bytes */ 259037b3c26Smrg reloc->flags = 0; 260037b3c26Smrg 261037b3c26Smrg etna_cmd_stream_emit(stream, addr); 262037b3c26Smrg} 263