etnaviv: Rework resource status tracking
[mesa.git] / src / etnaviv / drm / etnaviv_cmd_stream.c
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
32 static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
33
34 static 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
52 static inline struct etna_cmd_stream_priv *
53 etna_cmd_stream_priv(struct etna_cmd_stream *stream)
54 {
55 return (struct etna_cmd_stream_priv *)stream;
56 }
57
58 struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe,
59 uint32_t size,
60 void (*force_flush)(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->force_flush = force_flush;
88 stream->force_flush_priv = priv;
89
90 return &stream->base;
91
92 fail:
93 if (stream)
94 etna_cmd_stream_del(&stream->base);
95
96 return NULL;
97 }
98
99 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
109 void etna_cmd_stream_force_flush(struct etna_cmd_stream *stream)
110 {
111 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
112
113 if (priv->force_flush)
114 priv->force_flush(stream, priv->force_flush_priv);
115 }
116
117 uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)
118 {
119 return etna_cmd_stream_priv(stream)->last_timestamp;
120 }
121
122 static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)
123 {
124 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
125 uint32_t idx;
126
127 idx = APPEND(&priv->submit, bos);
128 idx = APPEND(priv, bos);
129
130 priv->submit.bos[idx].flags = 0;
131 priv->submit.bos[idx].handle = bo->handle;
132
133 priv->bos[idx] = etna_bo_ref(bo);
134
135 return idx;
136 }
137
138 /* add (if needed) bo, return idx: */
139 static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
140 uint32_t flags)
141 {
142 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
143 uint32_t idx;
144
145 pthread_mutex_lock(&idx_lock);
146
147 if (bo->current_stream == stream) {
148 idx = bo->idx;
149 } else {
150 void *val;
151
152 if (!priv->bo_table)
153 priv->bo_table = drmHashCreate();
154
155 if (!drmHashLookup(priv->bo_table, bo->handle, &val)) {
156 /* found */
157 idx = (uint32_t)(uintptr_t)val;
158 } else {
159 idx = append_bo(stream, bo);
160 val = (void *)(uintptr_t)idx;
161 drmHashInsert(priv->bo_table, bo->handle, val);
162 }
163
164 bo->current_stream = stream;
165 bo->idx = idx;
166 }
167 pthread_mutex_unlock(&idx_lock);
168
169 if (flags & ETNA_RELOC_READ)
170 priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;
171 if (flags & ETNA_RELOC_WRITE)
172 priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;
173
174 return idx;
175 }
176
177 void etna_cmd_stream_flush(struct etna_cmd_stream *stream, int in_fence_fd,
178 int *out_fence_fd)
179 {
180 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
181 int ret, id = priv->pipe->id;
182 struct etna_gpu *gpu = priv->pipe->gpu;
183
184 struct drm_etnaviv_gem_submit req = {
185 .pipe = gpu->core,
186 .exec_state = id,
187 .bos = VOID2U64(priv->submit.bos),
188 .nr_bos = priv->submit.nr_bos,
189 .relocs = VOID2U64(priv->submit.relocs),
190 .nr_relocs = priv->submit.nr_relocs,
191 .pmrs = VOID2U64(priv->submit.pmrs),
192 .nr_pmrs = priv->submit.nr_pmrs,
193 .stream = VOID2U64(stream->buffer),
194 .stream_size = stream->offset * 4, /* in bytes */
195 };
196
197 if (in_fence_fd != -1) {
198 req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;
199 req.fence_fd = in_fence_fd;
200 }
201
202 if (out_fence_fd)
203 req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;
204
205 ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,
206 &req, sizeof(req));
207
208 if (ret)
209 ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
210 else
211 priv->last_timestamp = req.fence;
212
213 for (uint32_t i = 0; i < priv->nr_bos; i++) {
214 struct etna_bo *bo = priv->bos[i];
215
216 bo->current_stream = NULL;
217 etna_bo_del(bo);
218 }
219
220 if (priv->bo_table) {
221 drmHashDestroy(priv->bo_table);
222 priv->bo_table = NULL;
223 }
224
225 if (out_fence_fd)
226 *out_fence_fd = req.fence_fd;
227
228 stream->offset = 0;
229 priv->submit.nr_bos = 0;
230 priv->submit.nr_relocs = 0;
231 priv->submit.nr_pmrs = 0;
232 priv->nr_bos = 0;
233 }
234
235 void etna_cmd_stream_reloc(struct etna_cmd_stream *stream,
236 const struct etna_reloc *r)
237 {
238 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
239 struct drm_etnaviv_gem_submit_reloc *reloc;
240 uint32_t idx = APPEND(&priv->submit, relocs);
241 uint32_t addr = 0;
242
243 reloc = &priv->submit.relocs[idx];
244
245 reloc->reloc_idx = bo2idx(stream, r->bo, r->flags);
246 reloc->reloc_offset = r->offset;
247 reloc->submit_offset = stream->offset * 4; /* in bytes */
248 reloc->flags = 0;
249
250 etna_cmd_stream_emit(stream, addr);
251 }
252
253 void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p)
254 {
255 struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
256 struct drm_etnaviv_gem_submit_pmr *pmr;
257 uint32_t idx = APPEND(&priv->submit, pmrs);
258
259 pmr = &priv->submit.pmrs[idx];
260
261 pmr->flags = p->flags;
262 pmr->sequence = p->sequence;
263 pmr->read_offset = p->offset;
264 pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE);
265 pmr->domain = p->signal->domain->id;
266 pmr->signal = p->signal->signal;
267 }