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#include <assert.h> 28037b3c26Smrg 29037b3c26Smrg#include "etnaviv_drmif.h" 30037b3c26Smrg#include "etnaviv_priv.h" 31037b3c26Smrg 32037b3c26Smrgstatic pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER; 33037b3c26Smrg 34037b3c26Smrgstatic void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz) 35037b3c26Smrg{ 36037b3c26Smrg if ((nr + 1) > *max) { 37037b3c26Smrg if ((*max * 2) < (nr + 1)) 38037b3c26Smrg *max = nr + 5; 39037b3c26Smrg else 40037b3c26Smrg *max = *max * 2; 41037b3c26Smrg ptr = realloc(ptr, *max * sz); 42037b3c26Smrg } 43037b3c26Smrg 44037b3c26Smrg return ptr; 45037b3c26Smrg} 46037b3c26Smrg 47037b3c26Smrg#define APPEND(x, name) ({ \ 48037b3c26Smrg (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \ 49037b3c26Smrg (x)->nr_ ## name ++; \ 50037b3c26Smrg}) 51037b3c26Smrg 52037b3c26Smrgstatic inline struct etna_cmd_stream_priv * 53037b3c26Smrgetna_cmd_stream_priv(struct etna_cmd_stream *stream) 54037b3c26Smrg{ 55037b3c26Smrg return (struct etna_cmd_stream_priv *)stream; 56037b3c26Smrg} 57037b3c26Smrg 587cdc0497Smrgdrm_public struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe, 597cdc0497Smrg uint32_t size, 60037b3c26Smrg void (*reset_notify)(struct etna_cmd_stream *stream, void *priv), 61037b3c26Smrg void *priv) 62037b3c26Smrg{ 63037b3c26Smrg struct etna_cmd_stream_priv *stream = NULL; 64037b3c26Smrg 65037b3c26Smrg if (size == 0) { 66037b3c26Smrg ERROR_MSG("invalid size of 0"); 67037b3c26Smrg goto fail; 68037b3c26Smrg } 69037b3c26Smrg 70037b3c26Smrg stream = calloc(1, sizeof(*stream)); 71037b3c26Smrg if (!stream) { 72037b3c26Smrg ERROR_MSG("allocation failed"); 73037b3c26Smrg goto fail; 74037b3c26Smrg } 75037b3c26Smrg 76037b3c26Smrg /* allocate even number of 32-bit words */ 77037b3c26Smrg size = ALIGN(size, 2); 78037b3c26Smrg 79037b3c26Smrg stream->base.buffer = malloc(size * sizeof(uint32_t)); 80037b3c26Smrg if (!stream->base.buffer) { 81037b3c26Smrg ERROR_MSG("allocation failed"); 82037b3c26Smrg goto fail; 83037b3c26Smrg } 84037b3c26Smrg 85037b3c26Smrg stream->base.size = size; 86037b3c26Smrg stream->pipe = pipe; 87037b3c26Smrg stream->reset_notify = reset_notify; 88037b3c26Smrg stream->reset_notify_priv = priv; 89037b3c26Smrg 90037b3c26Smrg return &stream->base; 91037b3c26Smrg 92037b3c26Smrgfail: 93037b3c26Smrg if (stream) 94037b3c26Smrg etna_cmd_stream_del(&stream->base); 95037b3c26Smrg 96037b3c26Smrg return NULL; 97037b3c26Smrg} 98037b3c26Smrg 997cdc0497Smrgdrm_public void etna_cmd_stream_del(struct etna_cmd_stream *stream) 100037b3c26Smrg{ 101037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 102037b3c26Smrg 103037b3c26Smrg free(stream->buffer); 104037b3c26Smrg free(priv->submit.relocs); 10500a23bdaSmrg free(priv->submit.pmrs); 106037b3c26Smrg free(priv); 107037b3c26Smrg} 108037b3c26Smrg 109037b3c26Smrgstatic void reset_buffer(struct etna_cmd_stream *stream) 110037b3c26Smrg{ 111037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 112037b3c26Smrg 113037b3c26Smrg stream->offset = 0; 114037b3c26Smrg priv->submit.nr_bos = 0; 115037b3c26Smrg priv->submit.nr_relocs = 0; 11600a23bdaSmrg priv->submit.nr_pmrs = 0; 117037b3c26Smrg priv->nr_bos = 0; 118037b3c26Smrg 119037b3c26Smrg if (priv->reset_notify) 120037b3c26Smrg priv->reset_notify(stream, priv->reset_notify_priv); 121037b3c26Smrg} 122037b3c26Smrg 1237cdc0497Smrgdrm_public uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream) 124037b3c26Smrg{ 125037b3c26Smrg return etna_cmd_stream_priv(stream)->last_timestamp; 126037b3c26Smrg} 127037b3c26Smrg 128037b3c26Smrgstatic uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo) 129037b3c26Smrg{ 130037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 131037b3c26Smrg uint32_t idx; 132037b3c26Smrg 133037b3c26Smrg idx = APPEND(&priv->submit, bos); 134037b3c26Smrg idx = APPEND(priv, bos); 135037b3c26Smrg 136037b3c26Smrg priv->submit.bos[idx].flags = 0; 137037b3c26Smrg priv->submit.bos[idx].handle = bo->handle; 138037b3c26Smrg 139037b3c26Smrg priv->bos[idx] = etna_bo_ref(bo); 140037b3c26Smrg 141037b3c26Smrg return idx; 142037b3c26Smrg} 143037b3c26Smrg 144037b3c26Smrg/* add (if needed) bo, return idx: */ 145037b3c26Smrgstatic uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo, 146037b3c26Smrg uint32_t flags) 147037b3c26Smrg{ 148037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 149037b3c26Smrg uint32_t idx; 150037b3c26Smrg 151037b3c26Smrg pthread_mutex_lock(&idx_lock); 152037b3c26Smrg 1535324fb0dSmrg if (bo->current_stream == stream) { 154037b3c26Smrg idx = bo->idx; 155037b3c26Smrg } else { 156037b3c26Smrg /* slow-path: */ 157037b3c26Smrg for (idx = 0; idx < priv->nr_bos; idx++) 158037b3c26Smrg if (priv->bos[idx] == bo) 159037b3c26Smrg break; 160037b3c26Smrg if (idx == priv->nr_bos) { 161037b3c26Smrg /* not found */ 162037b3c26Smrg idx = append_bo(stream, bo); 163037b3c26Smrg } 1645324fb0dSmrg bo->current_stream = stream; 1655324fb0dSmrg bo->idx = idx; 166037b3c26Smrg } 167037b3c26Smrg pthread_mutex_unlock(&idx_lock); 168037b3c26Smrg 169037b3c26Smrg if (flags & ETNA_RELOC_READ) 170037b3c26Smrg priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ; 171037b3c26Smrg if (flags & ETNA_RELOC_WRITE) 172037b3c26Smrg priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE; 173037b3c26Smrg 174037b3c26Smrg return idx; 175037b3c26Smrg} 176037b3c26Smrg 177d8807b2fSmrgstatic void flush(struct etna_cmd_stream *stream, int in_fence_fd, 178d8807b2fSmrg int *out_fence_fd) 179037b3c26Smrg{ 180037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 181037b3c26Smrg int ret, id = priv->pipe->id; 182037b3c26Smrg struct etna_gpu *gpu = priv->pipe->gpu; 183037b3c26Smrg 184037b3c26Smrg struct drm_etnaviv_gem_submit req = { 185037b3c26Smrg .pipe = gpu->core, 186037b3c26Smrg .exec_state = id, 187037b3c26Smrg .bos = VOID2U64(priv->submit.bos), 188037b3c26Smrg .nr_bos = priv->submit.nr_bos, 189037b3c26Smrg .relocs = VOID2U64(priv->submit.relocs), 190037b3c26Smrg .nr_relocs = priv->submit.nr_relocs, 19100a23bdaSmrg .pmrs = VOID2U64(priv->submit.pmrs), 19200a23bdaSmrg .nr_pmrs = priv->submit.nr_pmrs, 193037b3c26Smrg .stream = VOID2U64(stream->buffer), 194037b3c26Smrg .stream_size = stream->offset * 4, /* in bytes */ 195037b3c26Smrg }; 196037b3c26Smrg 197d8807b2fSmrg if (in_fence_fd != -1) { 198d8807b2fSmrg req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT; 199d8807b2fSmrg req.fence_fd = in_fence_fd; 200d8807b2fSmrg } 201d8807b2fSmrg 202d8807b2fSmrg if (out_fence_fd) 203d8807b2fSmrg req.flags |= ETNA_SUBMIT_FENCE_FD_OUT; 204d8807b2fSmrg 205037b3c26Smrg ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT, 206037b3c26Smrg &req, sizeof(req)); 207037b3c26Smrg 208037b3c26Smrg if (ret) 209037b3c26Smrg ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno)); 210037b3c26Smrg else 211037b3c26Smrg priv->last_timestamp = req.fence; 212037b3c26Smrg 213037b3c26Smrg for (uint32_t i = 0; i < priv->nr_bos; i++) { 214037b3c26Smrg struct etna_bo *bo = priv->bos[i]; 215037b3c26Smrg 216037b3c26Smrg bo->current_stream = NULL; 217037b3c26Smrg etna_bo_del(bo); 218037b3c26Smrg } 219d8807b2fSmrg 220d8807b2fSmrg if (out_fence_fd) 221d8807b2fSmrg *out_fence_fd = req.fence_fd; 222037b3c26Smrg} 223037b3c26Smrg 2247cdc0497Smrgdrm_public void etna_cmd_stream_flush(struct etna_cmd_stream *stream) 225037b3c26Smrg{ 226d8807b2fSmrg flush(stream, -1, NULL); 227d8807b2fSmrg reset_buffer(stream); 228d8807b2fSmrg} 229d8807b2fSmrg 2307cdc0497Smrgdrm_public void etna_cmd_stream_flush2(struct etna_cmd_stream *stream, 2317cdc0497Smrg int in_fence_fd, 2327cdc0497Smrg int *out_fence_fd) 233d8807b2fSmrg{ 234d8807b2fSmrg flush(stream, in_fence_fd, out_fence_fd); 235037b3c26Smrg reset_buffer(stream); 236037b3c26Smrg} 237037b3c26Smrg 2387cdc0497Smrgdrm_public void 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 2477cdc0497Smrgdrm_public void etna_cmd_stream_reloc(struct etna_cmd_stream *stream, 2487cdc0497Smrg const struct etna_reloc *r) 249037b3c26Smrg{ 250037b3c26Smrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 251037b3c26Smrg struct drm_etnaviv_gem_submit_reloc *reloc; 252037b3c26Smrg uint32_t idx = APPEND(&priv->submit, relocs); 253037b3c26Smrg uint32_t addr = 0; 254037b3c26Smrg 255037b3c26Smrg reloc = &priv->submit.relocs[idx]; 256037b3c26Smrg 257037b3c26Smrg reloc->reloc_idx = bo2idx(stream, r->bo, r->flags); 258037b3c26Smrg reloc->reloc_offset = r->offset; 259037b3c26Smrg reloc->submit_offset = stream->offset * 4; /* in bytes */ 260037b3c26Smrg reloc->flags = 0; 261037b3c26Smrg 262037b3c26Smrg etna_cmd_stream_emit(stream, addr); 263037b3c26Smrg} 26400a23bdaSmrg 2657cdc0497Smrgdrm_public void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p) 26600a23bdaSmrg{ 26700a23bdaSmrg struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 26800a23bdaSmrg struct drm_etnaviv_gem_submit_pmr *pmr; 26900a23bdaSmrg uint32_t idx = APPEND(&priv->submit, pmrs); 27000a23bdaSmrg 27100a23bdaSmrg pmr = &priv->submit.pmrs[idx]; 27200a23bdaSmrg 27300a23bdaSmrg pmr->flags = p->flags; 27400a23bdaSmrg pmr->sequence = p->sequence; 27500a23bdaSmrg pmr->read_offset = p->offset; 27600a23bdaSmrg pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE); 27700a23bdaSmrg pmr->domain = p->signal->domain->id; 27800a23bdaSmrg pmr->signal = p->signal->signal; 27900a23bdaSmrg} 280