2 * Copyright (C) 2014-2015 Etnaviv Project
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:
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
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
24 * Christian Gmeiner <christian.gmeiner@gmail.com>
33 #include "etnaviv_drmif.h"
34 #include "etnaviv_priv.h"
36 static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
38 static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
40 if ((nr + 1) > *max) {
41 if ((*max * 2) < (nr + 1))
45 ptr = realloc(ptr, *max * sz);
51 #define APPEND(x, name) ({ \
52 (x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
53 (x)->nr_ ## name ++; \
56 static inline struct etna_cmd_stream_priv *
57 etna_cmd_stream_priv(struct etna_cmd_stream *stream)
59 return (struct etna_cmd_stream_priv *)stream;
62 struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe, uint32_t size,
63 void (*reset_notify)(struct etna_cmd_stream *stream, void *priv),
66 struct etna_cmd_stream_priv *stream = NULL;
69 ERROR_MSG("invalid size of 0");
73 stream = calloc(1, sizeof(*stream));
75 ERROR_MSG("allocation failed");
79 /* allocate even number of 32-bit words */
80 size = ALIGN(size, 2);
82 stream->base.buffer = malloc(size * sizeof(uint32_t));
83 if (!stream->base.buffer) {
84 ERROR_MSG("allocation failed");
88 stream->base.size = size;
90 stream->reset_notify = reset_notify;
91 stream->reset_notify_priv = priv;
97 etna_cmd_stream_del(&stream->base);
102 void etna_cmd_stream_del(struct etna_cmd_stream *stream)
104 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
106 free(stream->buffer);
107 free(priv->submit.relocs);
111 static void reset_buffer(struct etna_cmd_stream *stream)
113 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
116 priv->submit.nr_bos = 0;
117 priv->submit.nr_relocs = 0;
120 if (priv->reset_notify)
121 priv->reset_notify(stream, priv->reset_notify_priv);
124 uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)
126 return etna_cmd_stream_priv(stream)->last_timestamp;
129 static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)
131 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
134 idx = APPEND(&priv->submit, bos);
135 idx = APPEND(priv, bos);
137 priv->submit.bos[idx].flags = 0;
138 priv->submit.bos[idx].handle = bo->handle;
140 priv->bos[idx] = etna_bo_ref(bo);
145 /* add (if needed) bo, return idx: */
146 static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
149 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
152 pthread_mutex_lock(&idx_lock);
154 if (!bo->current_stream) {
155 idx = append_bo(stream, bo);
156 bo->current_stream = stream;
158 } else if (bo->current_stream == stream) {
162 for (idx = 0; idx < priv->nr_bos; idx++)
163 if (priv->bos[idx] == bo)
165 if (idx == priv->nr_bos) {
167 idx = append_bo(stream, bo);
170 pthread_mutex_unlock(&idx_lock);
172 if (flags & ETNA_RELOC_READ)
173 priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;
174 if (flags & ETNA_RELOC_WRITE)
175 priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;
180 static void flush(struct etna_cmd_stream *stream, int in_fence_fd,
183 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
184 int ret, id = priv->pipe->id;
185 struct etna_gpu *gpu = priv->pipe->gpu;
187 struct drm_etnaviv_gem_submit req = {
190 .bos = VOID2U64(priv->submit.bos),
191 .nr_bos = priv->submit.nr_bos,
192 .relocs = VOID2U64(priv->submit.relocs),
193 .nr_relocs = priv->submit.nr_relocs,
194 .stream = VOID2U64(stream->buffer),
195 .stream_size = stream->offset * 4, /* in bytes */
198 if (in_fence_fd != -1) {
199 req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;
200 req.fence_fd = in_fence_fd;
204 req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;
207 * Pass the complete submit structure only if flags are set. Otherwise,
208 * only pass the fields up to, but not including the flags field for
209 * backwards compatiblity with older kernels.
211 ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,
212 &req, req.flags ? sizeof(req) :
213 offsetof(struct drm_etnaviv_gem_submit, flags));
216 ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
218 priv->last_timestamp = req.fence;
220 for (uint32_t i = 0; i < priv->nr_bos; i++) {
221 struct etna_bo *bo = priv->bos[i];
223 bo->current_stream = NULL;
228 *out_fence_fd = req.fence_fd;
231 void etna_cmd_stream_flush(struct etna_cmd_stream *stream)
233 flush(stream, -1, NULL);
234 reset_buffer(stream);
237 void etna_cmd_stream_flush2(struct etna_cmd_stream *stream, int in_fence_fd,
240 flush(stream, in_fence_fd, out_fence_fd);
241 reset_buffer(stream);
244 void etna_cmd_stream_finish(struct etna_cmd_stream *stream)
246 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
248 flush(stream, -1, NULL);
249 etna_pipe_wait(priv->pipe, priv->last_timestamp, 5000);
250 reset_buffer(stream);
253 void etna_cmd_stream_reloc(struct etna_cmd_stream *stream, const struct etna_reloc *r)
255 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
256 struct drm_etnaviv_gem_submit_reloc *reloc;
257 uint32_t idx = APPEND(&priv->submit, relocs);
260 reloc = &priv->submit.relocs[idx];
262 reloc->reloc_idx = bo2idx(stream, r->bo, r->flags);
263 reloc->reloc_offset = r->offset;
264 reloc->submit_offset = stream->offset * 4; /* in bytes */
267 etna_cmd_stream_emit(stream, addr);