Path: blob/21.2-virgl/src/etnaviv/drm/etnaviv_cmd_stream.c
4564 views
/*1* Copyright (C) 2014-2015 Etnaviv Project2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors:23* Christian Gmeiner <[email protected]>24*/2526#include <assert.h>27#include <stdlib.h>2829#include "etnaviv_drmif.h"30#include "etnaviv_priv.h"3132static simple_mtx_t idx_lock = _SIMPLE_MTX_INITIALIZER_NP;3334static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)35{36if ((nr + 1) > *max) {37if ((*max * 2) < (nr + 1))38*max = nr + 5;39else40*max = *max * 2;41ptr = realloc(ptr, *max * sz);42}4344return ptr;45}4647#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})5152void etna_cmd_stream_realloc(struct etna_cmd_stream *stream, size_t n)53{54size_t size;55void *buffer;5657/*58* Increase the command buffer size by 1 kiB. Here we pick 1 kiB59* increment to prevent it from growing too much too quickly.60*/61size = ALIGN(stream->size + n, 1024);6263/* Command buffer is too big for older kernel versions */64if (size >= 32768)65goto error;6667buffer = realloc(stream->buffer, size * 4);68if (!buffer)69goto error;7071stream->buffer = buffer;72stream->size = size;7374return;7576error:77WARN_MSG("command buffer too long, forcing flush.");78etna_cmd_stream_force_flush(stream);79}8081static inline struct etna_cmd_stream_priv *82etna_cmd_stream_priv(struct etna_cmd_stream *stream)83{84return (struct etna_cmd_stream_priv *)stream;85}8687struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe,88uint32_t size,89void (*force_flush)(struct etna_cmd_stream *stream, void *priv),90void *priv)91{92struct etna_cmd_stream_priv *stream = NULL;9394if (size == 0) {95ERROR_MSG("invalid size of 0");96goto fail;97}9899stream = calloc(1, sizeof(*stream));100if (!stream) {101ERROR_MSG("allocation failed");102goto fail;103}104105/* allocate even number of 32-bit words */106size = ALIGN(size, 2);107108stream->base.buffer = malloc(size * sizeof(uint32_t));109if (!stream->base.buffer) {110ERROR_MSG("allocation failed");111goto fail;112}113114stream->base.size = size;115stream->pipe = pipe;116stream->force_flush = force_flush;117stream->force_flush_priv = priv;118119return &stream->base;120121fail:122if (stream)123etna_cmd_stream_del(&stream->base);124125return NULL;126}127128void etna_cmd_stream_del(struct etna_cmd_stream *stream)129{130struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);131132free(stream->buffer);133free(priv->submit.relocs);134free(priv->submit.pmrs);135free(priv);136}137138void etna_cmd_stream_force_flush(struct etna_cmd_stream *stream)139{140struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);141142if (priv->force_flush)143priv->force_flush(stream, priv->force_flush_priv);144}145146uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)147{148return etna_cmd_stream_priv(stream)->last_timestamp;149}150151static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)152{153struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);154uint32_t idx;155156simple_mtx_assert_locked(&idx_lock);157158idx = APPEND(&priv->submit, bos);159idx = APPEND(priv, bos);160161priv->submit.bos[idx].flags = 0;162priv->submit.bos[idx].handle = bo->handle;163priv->submit.bos[idx].presumed = bo->va;164165priv->bos[idx] = etna_bo_ref(bo);166167return idx;168}169170/* add (if needed) bo, return idx: */171static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,172uint32_t flags)173{174struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);175uint32_t idx;176177simple_mtx_lock(&idx_lock);178179if (bo->current_stream == stream) {180idx = bo->idx;181} else {182void *val;183184if (!priv->bo_table)185priv->bo_table = drmHashCreate();186187if (!drmHashLookup(priv->bo_table, bo->handle, &val)) {188/* found */189idx = (uint32_t)(uintptr_t)val;190} else {191idx = append_bo(stream, bo);192val = (void *)(uintptr_t)idx;193drmHashInsert(priv->bo_table, bo->handle, val);194}195196bo->current_stream = stream;197bo->idx = idx;198}199simple_mtx_unlock(&idx_lock);200201if (flags & ETNA_RELOC_READ)202priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;203if (flags & ETNA_RELOC_WRITE)204priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;205206return idx;207}208209void etna_cmd_stream_flush(struct etna_cmd_stream *stream, int in_fence_fd,210int *out_fence_fd)211{212struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);213int ret, id = priv->pipe->id;214struct etna_gpu *gpu = priv->pipe->gpu;215216struct drm_etnaviv_gem_submit req = {217.pipe = gpu->core,218.exec_state = id,219.bos = VOID2U64(priv->submit.bos),220.nr_bos = priv->submit.nr_bos,221.relocs = VOID2U64(priv->submit.relocs),222.nr_relocs = priv->submit.nr_relocs,223.pmrs = VOID2U64(priv->submit.pmrs),224.nr_pmrs = priv->submit.nr_pmrs,225.stream = VOID2U64(stream->buffer),226.stream_size = stream->offset * 4, /* in bytes */227};228229if (in_fence_fd != -1) {230req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;231req.fence_fd = in_fence_fd;232}233234if (out_fence_fd)235req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;236237if (gpu->dev->use_softpin)238req.flags |= ETNA_SUBMIT_SOFTPIN;239240ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,241&req, sizeof(req));242243if (ret)244ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));245else246priv->last_timestamp = req.fence;247248for (uint32_t i = 0; i < priv->nr_bos; i++) {249struct etna_bo *bo = priv->bos[i];250251bo->current_stream = NULL;252etna_bo_del(bo);253}254255if (priv->bo_table) {256drmHashDestroy(priv->bo_table);257priv->bo_table = NULL;258}259260if (out_fence_fd)261*out_fence_fd = req.fence_fd;262263stream->offset = 0;264priv->submit.nr_bos = 0;265priv->submit.nr_relocs = 0;266priv->submit.nr_pmrs = 0;267priv->nr_bos = 0;268}269270void etna_cmd_stream_reloc(struct etna_cmd_stream *stream,271const struct etna_reloc *r)272{273struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);274struct drm_etnaviv_gem_submit_reloc *reloc;275uint32_t addr = r->bo->va + r->offset;276uint32_t bo_idx = bo2idx(stream, r->bo, r->flags);277uint32_t idx;278279if (!priv->pipe->gpu->dev->use_softpin) {280idx = APPEND(&priv->submit, relocs);281reloc = &priv->submit.relocs[idx];282283reloc->reloc_idx = bo_idx;284reloc->reloc_offset = r->offset;285reloc->submit_offset = stream->offset * 4; /* in bytes */286reloc->flags = 0;287}288289etna_cmd_stream_emit(stream, addr);290}291292void etna_cmd_stream_ref_bo(struct etna_cmd_stream *stream, struct etna_bo *bo,293uint32_t flags)294{295bo2idx(stream, bo, flags);296}297298void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p)299{300struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);301struct drm_etnaviv_gem_submit_pmr *pmr;302uint32_t idx = APPEND(&priv->submit, pmrs);303304pmr = &priv->submit.pmrs[idx];305306pmr->flags = p->flags;307pmr->sequence = p->sequence;308pmr->read_offset = p->offset;309pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE);310pmr->domain = p->signal->domain->id;311pmr->signal = p->signal->signal;312}313314315