etnaviv_cmd_stream.c revision 7cdc0497
1/* 2 * Copyright (C) 2014-2015 Etnaviv Project 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 * 23 * Authors: 24 * Christian Gmeiner <christian.gmeiner@gmail.com> 25 */ 26 27#include <assert.h> 28 29#include "etnaviv_drmif.h" 30#include "etnaviv_priv.h" 31 32static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER; 33 34static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz) 35{ 36 if ((nr + 1) > *max) { 37 if ((*max * 2) < (nr + 1)) 38 *max = nr + 5; 39 else 40 *max = *max * 2; 41 ptr = realloc(ptr, *max * sz); 42 } 43 44 return ptr; 45} 46 47#define APPEND(x, name) ({ \ 48 (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \ 49 (x)->nr_ ## name ++; \ 50}) 51 52static inline struct etna_cmd_stream_priv * 53etna_cmd_stream_priv(struct etna_cmd_stream *stream) 54{ 55 return (struct etna_cmd_stream_priv *)stream; 56} 57 58drm_public struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe, 59 uint32_t size, 60 void (*reset_notify)(struct etna_cmd_stream *stream, void *priv), 61 void *priv) 62{ 63 struct etna_cmd_stream_priv *stream = NULL; 64 65 if (size == 0) { 66 ERROR_MSG("invalid size of 0"); 67 goto fail; 68 } 69 70 stream = calloc(1, sizeof(*stream)); 71 if (!stream) { 72 ERROR_MSG("allocation failed"); 73 goto fail; 74 } 75 76 /* allocate even number of 32-bit words */ 77 size = ALIGN(size, 2); 78 79 stream->base.buffer = malloc(size * sizeof(uint32_t)); 80 if (!stream->base.buffer) { 81 ERROR_MSG("allocation failed"); 82 goto fail; 83 } 84 85 stream->base.size = size; 86 stream->pipe = pipe; 87 stream->reset_notify = reset_notify; 88 stream->reset_notify_priv = priv; 89 90 return &stream->base; 91 92fail: 93 if (stream) 94 etna_cmd_stream_del(&stream->base); 95 96 return NULL; 97} 98 99drm_public void etna_cmd_stream_del(struct etna_cmd_stream *stream) 100{ 101 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 102 103 free(stream->buffer); 104 free(priv->submit.relocs); 105 free(priv->submit.pmrs); 106 free(priv); 107} 108 109static void reset_buffer(struct etna_cmd_stream *stream) 110{ 111 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 112 113 stream->offset = 0; 114 priv->submit.nr_bos = 0; 115 priv->submit.nr_relocs = 0; 116 priv->submit.nr_pmrs = 0; 117 priv->nr_bos = 0; 118 119 if (priv->reset_notify) 120 priv->reset_notify(stream, priv->reset_notify_priv); 121} 122 123drm_public uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream) 124{ 125 return etna_cmd_stream_priv(stream)->last_timestamp; 126} 127 128static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo) 129{ 130 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 131 uint32_t idx; 132 133 idx = APPEND(&priv->submit, bos); 134 idx = APPEND(priv, bos); 135 136 priv->submit.bos[idx].flags = 0; 137 priv->submit.bos[idx].handle = bo->handle; 138 139 priv->bos[idx] = etna_bo_ref(bo); 140 141 return idx; 142} 143 144/* add (if needed) bo, return idx: */ 145static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo, 146 uint32_t flags) 147{ 148 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 149 uint32_t idx; 150 151 pthread_mutex_lock(&idx_lock); 152 153 if (!bo->current_stream) { 154 idx = append_bo(stream, bo); 155 bo->current_stream = stream; 156 bo->idx = idx; 157 } else if (bo->current_stream == stream) { 158 idx = bo->idx; 159 } else { 160 /* slow-path: */ 161 for (idx = 0; idx < priv->nr_bos; idx++) 162 if (priv->bos[idx] == bo) 163 break; 164 if (idx == priv->nr_bos) { 165 /* not found */ 166 idx = append_bo(stream, bo); 167 } 168 } 169 pthread_mutex_unlock(&idx_lock); 170 171 if (flags & ETNA_RELOC_READ) 172 priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ; 173 if (flags & ETNA_RELOC_WRITE) 174 priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE; 175 176 return idx; 177} 178 179static void flush(struct etna_cmd_stream *stream, int in_fence_fd, 180 int *out_fence_fd) 181{ 182 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 183 int ret, id = priv->pipe->id; 184 struct etna_gpu *gpu = priv->pipe->gpu; 185 186 struct drm_etnaviv_gem_submit req = { 187 .pipe = gpu->core, 188 .exec_state = id, 189 .bos = VOID2U64(priv->submit.bos), 190 .nr_bos = priv->submit.nr_bos, 191 .relocs = VOID2U64(priv->submit.relocs), 192 .nr_relocs = priv->submit.nr_relocs, 193 .pmrs = VOID2U64(priv->submit.pmrs), 194 .nr_pmrs = priv->submit.nr_pmrs, 195 .stream = VOID2U64(stream->buffer), 196 .stream_size = stream->offset * 4, /* in bytes */ 197 }; 198 199 if (in_fence_fd != -1) { 200 req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT; 201 req.fence_fd = in_fence_fd; 202 } 203 204 if (out_fence_fd) 205 req.flags |= ETNA_SUBMIT_FENCE_FD_OUT; 206 207 ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT, 208 &req, sizeof(req)); 209 210 if (ret) 211 ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno)); 212 else 213 priv->last_timestamp = req.fence; 214 215 for (uint32_t i = 0; i < priv->nr_bos; i++) { 216 struct etna_bo *bo = priv->bos[i]; 217 218 bo->current_stream = NULL; 219 etna_bo_del(bo); 220 } 221 222 if (out_fence_fd) 223 *out_fence_fd = req.fence_fd; 224} 225 226drm_public void etna_cmd_stream_flush(struct etna_cmd_stream *stream) 227{ 228 flush(stream, -1, NULL); 229 reset_buffer(stream); 230} 231 232drm_public void etna_cmd_stream_flush2(struct etna_cmd_stream *stream, 233 int in_fence_fd, 234 int *out_fence_fd) 235{ 236 flush(stream, in_fence_fd, out_fence_fd); 237 reset_buffer(stream); 238} 239 240drm_public void etna_cmd_stream_finish(struct etna_cmd_stream *stream) 241{ 242 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 243 244 flush(stream, -1, NULL); 245 etna_pipe_wait(priv->pipe, priv->last_timestamp, 5000); 246 reset_buffer(stream); 247} 248 249drm_public void etna_cmd_stream_reloc(struct etna_cmd_stream *stream, 250 const struct etna_reloc *r) 251{ 252 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 253 struct drm_etnaviv_gem_submit_reloc *reloc; 254 uint32_t idx = APPEND(&priv->submit, relocs); 255 uint32_t addr = 0; 256 257 reloc = &priv->submit.relocs[idx]; 258 259 reloc->reloc_idx = bo2idx(stream, r->bo, r->flags); 260 reloc->reloc_offset = r->offset; 261 reloc->submit_offset = stream->offset * 4; /* in bytes */ 262 reloc->flags = 0; 263 264 etna_cmd_stream_emit(stream, addr); 265} 266 267drm_public void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p) 268{ 269 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream); 270 struct drm_etnaviv_gem_submit_pmr *pmr; 271 uint32_t idx = APPEND(&priv->submit, pmrs); 272 273 pmr = &priv->submit.pmrs[idx]; 274 275 pmr->flags = p->flags; 276 pmr->sequence = p->sequence; 277 pmr->read_offset = p->offset; 278 pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE); 279 pmr->domain = p->signal->domain->id; 280 pmr->signal = p->signal->signal; 281} 282