etnaviv_cmd_stream.c revision 00a23bda
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); 10800a23bdaSmrg free(priv->submit.pmrs); 109037b3c26Smrg free(priv); 110037b3c26Smrg} 111037b3c26Smrg 112037b3c26Smrgstatic void reset_buffer(struct etna_cmd_stream *stream) 113037b3c26Smrg{ 114037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 115037b3c26Smrg 116037b3c26Smrg stream->offset = 0; 117037b3c26Smrg priv->submit.nr_bos = 0; 118037b3c26Smrg priv->submit.nr_relocs = 0; 11900a23bdaSmrg priv->submit.nr_pmrs = 0; 120037b3c26Smrg priv->nr_bos = 0; 121037b3c26Smrg 122037b3c26Smrg if (priv->reset_notify) 123037b3c26Smrg priv->reset_notify(stream, priv->reset_notify_priv); 124037b3c26Smrg} 125037b3c26Smrg 126037b3c26Smrguint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream) 127037b3c26Smrg{ 128037b3c26Smrg return etna_cmd_stream_priv(stream)->last_timestamp; 129037b3c26Smrg} 130037b3c26Smrg 131037b3c26Smrgstatic uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo) 132037b3c26Smrg{ 133037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 134037b3c26Smrg uint32_t idx; 135037b3c26Smrg 136037b3c26Smrg idx = APPEND(&priv->submit, bos); 137037b3c26Smrg idx = APPEND(priv, bos); 138037b3c26Smrg 139037b3c26Smrg priv->submit.bos[idx].flags = 0; 140037b3c26Smrg priv->submit.bos[idx].handle = bo->handle; 141037b3c26Smrg 142037b3c26Smrg priv->bos[idx] = etna_bo_ref(bo); 143037b3c26Smrg 144037b3c26Smrg return idx; 145037b3c26Smrg} 146037b3c26Smrg 147037b3c26Smrg/* add (if needed) bo, return idx: */ 148037b3c26Smrgstatic uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo, 149037b3c26Smrg uint32_t flags) 150037b3c26Smrg{ 151037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 152037b3c26Smrg uint32_t idx; 153037b3c26Smrg 154037b3c26Smrg pthread_mutex_lock(&idx_lock); 155037b3c26Smrg 156037b3c26Smrg if (!bo->current_stream) { 157037b3c26Smrg idx = append_bo(stream, bo); 158037b3c26Smrg bo->current_stream = stream; 159037b3c26Smrg bo->idx = idx; 160037b3c26Smrg } else if (bo->current_stream == stream) { 161037b3c26Smrg idx = bo->idx; 162037b3c26Smrg } else { 163037b3c26Smrg /* slow-path: */ 164037b3c26Smrg for (idx = 0; idx < priv->nr_bos; idx++) 165037b3c26Smrg if (priv->bos[idx] == bo) 166037b3c26Smrg break; 167037b3c26Smrg if (idx == priv->nr_bos) { 168037b3c26Smrg /* not found */ 169037b3c26Smrg idx = append_bo(stream, bo); 170037b3c26Smrg } 171037b3c26Smrg } 172037b3c26Smrg pthread_mutex_unlock(&idx_lock); 173037b3c26Smrg 174037b3c26Smrg if (flags & ETNA_RELOC_READ) 175037b3c26Smrg priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ; 176037b3c26Smrg if (flags & ETNA_RELOC_WRITE) 177037b3c26Smrg priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE; 178037b3c26Smrg 179037b3c26Smrg return idx; 180037b3c26Smrg} 181037b3c26Smrg 182d8807b2fSmrgstatic void flush(struct etna_cmd_stream *stream, int in_fence_fd, 183d8807b2fSmrg int *out_fence_fd) 184037b3c26Smrg{ 185037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 186037b3c26Smrg int ret, id = priv->pipe->id; 187037b3c26Smrg struct etna_gpu *gpu = priv->pipe->gpu; 188037b3c26Smrg 189037b3c26Smrg struct drm_etnaviv_gem_submit req = { 190037b3c26Smrg .pipe = gpu->core, 191037b3c26Smrg .exec_state = id, 192037b3c26Smrg .bos = VOID2U64(priv->submit.bos), 193037b3c26Smrg .nr_bos = priv->submit.nr_bos, 194037b3c26Smrg .relocs = VOID2U64(priv->submit.relocs), 195037b3c26Smrg .nr_relocs = priv->submit.nr_relocs, 19600a23bdaSmrg .pmrs = VOID2U64(priv->submit.pmrs), 19700a23bdaSmrg .nr_pmrs = priv->submit.nr_pmrs, 198037b3c26Smrg .stream = VOID2U64(stream->buffer), 199037b3c26Smrg .stream_size = stream->offset * 4, /* in bytes */ 200037b3c26Smrg }; 201037b3c26Smrg 202d8807b2fSmrg if (in_fence_fd != -1) { 203d8807b2fSmrg req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT; 204d8807b2fSmrg req.fence_fd = in_fence_fd; 205d8807b2fSmrg } 206d8807b2fSmrg 207d8807b2fSmrg if (out_fence_fd) 208d8807b2fSmrg req.flags |= ETNA_SUBMIT_FENCE_FD_OUT; 209d8807b2fSmrg 210037b3c26Smrg ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT, 211037b3c26Smrg &req, sizeof(req)); 212037b3c26Smrg 213037b3c26Smrg if (ret) 214037b3c26Smrg ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno)); 215037b3c26Smrg else 216037b3c26Smrg priv->last_timestamp = req.fence; 217037b3c26Smrg 218037b3c26Smrg for (uint32_t i = 0; i < priv->nr_bos; i++) { 219037b3c26Smrg struct etna_bo *bo = priv->bos[i]; 220037b3c26Smrg 221037b3c26Smrg bo->current_stream = NULL; 222037b3c26Smrg etna_bo_del(bo); 223037b3c26Smrg } 224d8807b2fSmrg 225d8807b2fSmrg if (out_fence_fd) 226d8807b2fSmrg *out_fence_fd = req.fence_fd; 227037b3c26Smrg} 228037b3c26Smrg 229037b3c26Smrgvoid etna_cmd_stream_flush(struct etna_cmd_stream *stream) 230037b3c26Smrg{ 231d8807b2fSmrg flush(stream, -1, NULL); 232d8807b2fSmrg reset_buffer(stream); 233d8807b2fSmrg} 234d8807b2fSmrg 235d8807b2fSmrgvoid etna_cmd_stream_flush2(struct etna_cmd_stream *stream, int in_fence_fd, 236d8807b2fSmrg int *out_fence_fd) 237d8807b2fSmrg{ 238d8807b2fSmrg flush(stream, in_fence_fd, out_fence_fd); 239037b3c26Smrg reset_buffer(stream); 240037b3c26Smrg} 241037b3c26Smrg 242037b3c26Smrgvoid etna_cmd_stream_finish(struct etna_cmd_stream *stream) 243037b3c26Smrg{ 244037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 245037b3c26Smrg 246d8807b2fSmrg flush(stream, -1, NULL); 247037b3c26Smrg etna_pipe_wait(priv->pipe, priv->last_timestamp, 5000); 248037b3c26Smrg reset_buffer(stream); 249037b3c26Smrg} 250037b3c26Smrg 251037b3c26Smrgvoid etna_cmd_stream_reloc(struct etna_cmd_stream *stream, const struct etna_reloc *r) 252037b3c26Smrg{ 253037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 254037b3c26Smrg struct drm_etnaviv_gem_submit_reloc *reloc; 255037b3c26Smrg uint32_t idx = APPEND(&priv->submit, relocs); 256037b3c26Smrg uint32_t addr = 0; 257037b3c26Smrg 258037b3c26Smrg reloc = &priv->submit.relocs[idx]; 259037b3c26Smrg 260037b3c26Smrg reloc->reloc_idx = bo2idx(stream, r->bo, r->flags); 261037b3c26Smrg reloc->reloc_offset = r->offset; 262037b3c26Smrg reloc->submit_offset = stream->offset * 4; /* in bytes */ 263037b3c26Smrg reloc->flags = 0; 264037b3c26Smrg 265037b3c26Smrg etna_cmd_stream_emit(stream, addr); 266037b3c26Smrg} 26700a23bdaSmrg 26800a23bdaSmrgvoid etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p) 26900a23bdaSmrg{ 27000a23bdaSmrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 27100a23bdaSmrg struct drm_etnaviv_gem_submit_pmr *pmr; 27200a23bdaSmrg uint32_t idx = APPEND(&priv->submit, pmrs); 27300a23bdaSmrg 27400a23bdaSmrg pmr = &priv->submit.pmrs[idx]; 27500a23bdaSmrg 27600a23bdaSmrg pmr->flags = p->flags; 27700a23bdaSmrg pmr->sequence = p->sequence; 27800a23bdaSmrg pmr->read_offset = p->offset; 27900a23bdaSmrg pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE); 28000a23bdaSmrg pmr->domain = p->signal->domain->id; 28100a23bdaSmrg pmr->signal = p->signal->signal; 28200a23bdaSmrg} 283