freedreno: flush immediately when reading a pending batch
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch.c
1 /*
2 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
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 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/list.h"
28 #include "util/set.h"
29 #include "util/hash_table.h"
30 #include "util/u_string.h"
31
32 #include "freedreno_batch.h"
33 #include "freedreno_context.h"
34 #include "freedreno_fence.h"
35 #include "freedreno_resource.h"
36 #include "freedreno_query_hw.h"
37
38 static void
39 batch_init(struct fd_batch *batch)
40 {
41 struct fd_context *ctx = batch->ctx;
42 unsigned size = 0;
43
44 if (ctx->screen->reorder)
45 util_queue_fence_init(&batch->flush_fence);
46
47 /* if kernel is too old to support unlimited # of cmd buffers, we
48 * have no option but to allocate large worst-case sizes so that
49 * we don't need to grow the ringbuffer. Performance is likely to
50 * suffer, but there is no good alternative.
51 */
52 if ((fd_device_version(ctx->screen->dev) < FD_VERSION_UNLIMITED_CMDS) ||
53 (fd_mesa_debug & FD_DBG_NOGROW)){
54 size = 0x100000;
55 }
56
57 batch->draw = fd_ringbuffer_new(ctx->pipe, size);
58 if (!batch->nondraw) {
59 batch->binning = fd_ringbuffer_new(ctx->pipe, size);
60 batch->gmem = fd_ringbuffer_new(ctx->pipe, size);
61
62 fd_ringbuffer_set_parent(batch->gmem, NULL);
63 fd_ringbuffer_set_parent(batch->draw, batch->gmem);
64 fd_ringbuffer_set_parent(batch->binning, batch->gmem);
65 } else {
66 fd_ringbuffer_set_parent(batch->draw, NULL);
67 }
68
69 batch->in_fence_fd = -1;
70 batch->fence = fd_fence_create(batch);
71
72 batch->cleared = batch->partial_cleared = 0;
73 batch->restore = batch->resolve = 0;
74 batch->needs_flush = false;
75 batch->gmem_reason = 0;
76 batch->num_draws = 0;
77 batch->stage = FD_STAGE_NULL;
78
79 fd_reset_wfi(batch);
80
81 /* reset maximal bounds: */
82 batch->max_scissor.minx = batch->max_scissor.miny = ~0;
83 batch->max_scissor.maxx = batch->max_scissor.maxy = 0;
84
85 util_dynarray_init(&batch->draw_patches, NULL);
86
87 if (is_a3xx(ctx->screen))
88 util_dynarray_init(&batch->rbrc_patches, NULL);
89
90 assert(batch->resources->entries == 0);
91
92 util_dynarray_init(&batch->samples, NULL);
93 }
94
95 struct fd_batch *
96 fd_batch_create(struct fd_context *ctx, bool nondraw)
97 {
98 struct fd_batch *batch = CALLOC_STRUCT(fd_batch);
99
100 if (!batch)
101 return NULL;
102
103 DBG("%p", batch);
104
105 pipe_reference_init(&batch->reference, 1);
106 batch->ctx = ctx;
107 batch->nondraw = nondraw;
108
109 batch->resources = _mesa_set_create(NULL, _mesa_hash_pointer,
110 _mesa_key_pointer_equal);
111
112 batch_init(batch);
113
114 return batch;
115 }
116
117 static void
118 batch_fini(struct fd_batch *batch)
119 {
120 pipe_resource_reference(&batch->query_buf, NULL);
121
122 if (batch->in_fence_fd != -1)
123 close(batch->in_fence_fd);
124
125 /* in case batch wasn't flushed but fence was created: */
126 fd_fence_populate(batch->fence, 0, -1);
127
128 fd_fence_ref(NULL, &batch->fence, NULL);
129
130 fd_ringbuffer_del(batch->draw);
131 if (!batch->nondraw) {
132 fd_ringbuffer_del(batch->binning);
133 fd_ringbuffer_del(batch->gmem);
134 } else {
135 debug_assert(!batch->binning);
136 debug_assert(!batch->gmem);
137 }
138 if (batch->lrz_clear) {
139 fd_ringbuffer_del(batch->lrz_clear);
140 batch->lrz_clear = NULL;
141 }
142
143 util_dynarray_fini(&batch->draw_patches);
144
145 if (is_a3xx(batch->ctx->screen))
146 util_dynarray_fini(&batch->rbrc_patches);
147
148 while (batch->samples.size > 0) {
149 struct fd_hw_sample *samp =
150 util_dynarray_pop(&batch->samples, struct fd_hw_sample *);
151 fd_hw_sample_reference(batch->ctx, &samp, NULL);
152 }
153 util_dynarray_fini(&batch->samples);
154
155 if (batch->ctx->screen->reorder)
156 util_queue_fence_destroy(&batch->flush_fence);
157 }
158
159 static void
160 batch_flush_reset_dependencies(struct fd_batch *batch, bool flush)
161 {
162 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
163 struct fd_batch *dep;
164
165 foreach_batch(dep, cache, batch->dependents_mask) {
166 if (flush)
167 fd_batch_flush(dep, false, false);
168 fd_batch_reference(&dep, NULL);
169 }
170
171 batch->dependents_mask = 0;
172 }
173
174 static void
175 batch_reset_resources_locked(struct fd_batch *batch)
176 {
177 struct set_entry *entry;
178
179 pipe_mutex_assert_locked(batch->ctx->screen->lock);
180
181 set_foreach(batch->resources, entry) {
182 struct fd_resource *rsc = (struct fd_resource *)entry->key;
183 _mesa_set_remove(batch->resources, entry);
184 debug_assert(rsc->batch_mask & (1 << batch->idx));
185 rsc->batch_mask &= ~(1 << batch->idx);
186 if (rsc->write_batch == batch)
187 fd_batch_reference_locked(&rsc->write_batch, NULL);
188 }
189 }
190
191 static void
192 batch_reset_resources(struct fd_batch *batch)
193 {
194 mtx_lock(&batch->ctx->screen->lock);
195 batch_reset_resources_locked(batch);
196 mtx_unlock(&batch->ctx->screen->lock);
197 }
198
199 static void
200 batch_reset(struct fd_batch *batch)
201 {
202 DBG("%p", batch);
203
204 fd_batch_sync(batch);
205
206 batch_flush_reset_dependencies(batch, false);
207 batch_reset_resources(batch);
208
209 batch_fini(batch);
210 batch_init(batch);
211 }
212
213 void
214 fd_batch_reset(struct fd_batch *batch)
215 {
216 if (batch->needs_flush)
217 batch_reset(batch);
218 }
219
220 void
221 __fd_batch_destroy(struct fd_batch *batch)
222 {
223 DBG("%p", batch);
224
225 util_copy_framebuffer_state(&batch->framebuffer, NULL);
226
227 mtx_lock(&batch->ctx->screen->lock);
228 fd_bc_invalidate_batch(batch, true);
229 mtx_unlock(&batch->ctx->screen->lock);
230
231 batch_fini(batch);
232
233 batch_reset_resources(batch);
234 debug_assert(batch->resources->entries == 0);
235 _mesa_set_destroy(batch->resources, NULL);
236
237 batch_flush_reset_dependencies(batch, false);
238 debug_assert(batch->dependents_mask == 0);
239
240 free(batch);
241 }
242
243 void
244 __fd_batch_describe(char* buf, const struct fd_batch *batch)
245 {
246 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
247 }
248
249 void
250 fd_batch_sync(struct fd_batch *batch)
251 {
252 if (!batch->ctx->screen->reorder)
253 return;
254 util_queue_fence_wait(&batch->flush_fence);
255 }
256
257 static void
258 batch_flush_func(void *job, int id)
259 {
260 struct fd_batch *batch = job;
261
262 fd_gmem_render_tiles(batch);
263 batch_reset_resources(batch);
264 }
265
266 static void
267 batch_cleanup_func(void *job, int id)
268 {
269 struct fd_batch *batch = job;
270 fd_batch_reference(&batch, NULL);
271 }
272
273 static void
274 batch_flush(struct fd_batch *batch, bool force)
275 {
276 DBG("%p: needs_flush=%d", batch, batch->needs_flush);
277
278 if (!batch->needs_flush) {
279 return;
280 }
281
282 batch->needs_flush = false;
283
284 /* close out the draw cmds by making sure any active queries are
285 * paused:
286 */
287 fd_batch_set_stage(batch, FD_STAGE_NULL);
288
289 fd_context_all_dirty(batch->ctx);
290 batch_flush_reset_dependencies(batch, true);
291
292 if (batch->ctx->screen->reorder) {
293 struct fd_batch *tmp = NULL;
294 fd_batch_reference(&tmp, batch);
295
296 if (!util_queue_is_initialized(&batch->ctx->flush_queue))
297 util_queue_init(&batch->ctx->flush_queue, "flush_queue", 16, 1, 0);
298
299 util_queue_add_job(&batch->ctx->flush_queue,
300 batch, &batch->flush_fence,
301 batch_flush_func, batch_cleanup_func);
302 } else {
303 fd_gmem_render_tiles(batch);
304 batch_reset_resources(batch);
305 }
306
307 debug_assert(batch->reference.count > 0);
308
309 if (batch == batch->ctx->batch) {
310 batch_reset(batch);
311 } else {
312 mtx_lock(&batch->ctx->screen->lock);
313 fd_bc_invalidate_batch(batch, false);
314 mtx_unlock(&batch->ctx->screen->lock);
315 }
316 }
317
318 /* NOTE: could drop the last ref to batch
319 *
320 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
321 * to kernel before this returns, as opposed to just being queued to be
322 * flushed
323 * @force: force a flush even if no rendering, mostly useful if you need
324 * a fence to sync on
325 */
326 void
327 fd_batch_flush(struct fd_batch *batch, bool sync, bool force)
328 {
329 /* NOTE: we need to hold an extra ref across the body of flush,
330 * since the last ref to this batch could be dropped when cleaning
331 * up used_resources
332 */
333 struct fd_batch *tmp = NULL;
334
335 fd_batch_reference(&tmp, batch);
336 batch_flush(tmp, force);
337 if (sync)
338 fd_batch_sync(tmp);
339 fd_batch_reference(&tmp, NULL);
340 }
341
342 /* does 'batch' depend directly or indirectly on 'other' ? */
343 static bool
344 batch_depends_on(struct fd_batch *batch, struct fd_batch *other)
345 {
346 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
347 struct fd_batch *dep;
348
349 if (batch->dependents_mask & (1 << other->idx))
350 return true;
351
352 foreach_batch(dep, cache, batch->dependents_mask)
353 if (batch_depends_on(batch, dep))
354 return true;
355
356 return false;
357 }
358
359 void
360 fd_batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
361 {
362 if (batch->dependents_mask & (1 << dep->idx))
363 return;
364
365 /* a loop should not be possible */
366 debug_assert(!batch_depends_on(dep, batch));
367
368 struct fd_batch *other = NULL;
369 fd_batch_reference_locked(&other, dep);
370 batch->dependents_mask |= (1 << dep->idx);
371 DBG("%p: added dependency on %p", batch, dep);
372 }
373
374 static void
375 flush_write_batch(struct fd_resource *rsc)
376 {
377 struct fd_batch *b = NULL;
378 fd_batch_reference(&b, rsc->write_batch);
379
380 mtx_unlock(&b->ctx->screen->lock);
381 fd_batch_flush(b, true, false);
382 mtx_lock(&b->ctx->screen->lock);
383
384 fd_bc_invalidate_batch(b, false);
385 fd_batch_reference_locked(&b, NULL);
386 }
387
388 void
389 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool write)
390 {
391 pipe_mutex_assert_locked(batch->ctx->screen->lock);
392
393 if (rsc->stencil)
394 fd_batch_resource_used(batch, rsc->stencil, write);
395
396 DBG("%p: %s %p", batch, write ? "write" : "read", rsc);
397
398 if (write)
399 rsc->valid = true;
400
401 /* note, invalidate write batch, to avoid further writes to rsc
402 * resulting in a write-after-read hazard.
403 */
404
405 if (write) {
406 /* if we are pending read or write by any other batch: */
407 if (rsc->batch_mask & ~(1 << batch->idx)) {
408 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
409 struct fd_batch *dep;
410
411 if (rsc->write_batch && rsc->write_batch != batch)
412 flush_write_batch(rsc);
413
414 foreach_batch(dep, cache, rsc->batch_mask) {
415 struct fd_batch *b = NULL;
416 if (dep == batch)
417 continue;
418 /* note that batch_add_dep could flush and unref dep, so
419 * we need to hold a reference to keep it live for the
420 * fd_bc_invalidate_batch()
421 */
422 fd_batch_reference(&b, dep);
423 fd_batch_add_dep(batch, b);
424 fd_bc_invalidate_batch(b, false);
425 fd_batch_reference_locked(&b, NULL);
426 }
427 }
428 fd_batch_reference_locked(&rsc->write_batch, batch);
429 } else {
430 /* If reading a resource pending a write, go ahead and flush the
431 * writer. This avoids situations where we end up having to
432 * flush the current batch in _resource_used()
433 */
434 if (rsc->write_batch && rsc->write_batch != batch)
435 flush_write_batch(rsc);
436 }
437
438 if (rsc->batch_mask & (1 << batch->idx))
439 return;
440
441 debug_assert(!_mesa_set_search(batch->resources, rsc));
442
443 _mesa_set_add(batch->resources, rsc);
444 rsc->batch_mask |= (1 << batch->idx);
445 }
446
447 void
448 fd_batch_check_size(struct fd_batch *batch)
449 {
450 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
451 return;
452
453 struct fd_ringbuffer *ring = batch->draw;
454 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
455 (fd_mesa_debug & FD_DBG_FLUSH))
456 fd_batch_flush(batch, true, false);
457 }
458
459 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
460 * been one since last draw:
461 */
462 void
463 fd_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
464 {
465 if (batch->needs_wfi) {
466 if (batch->ctx->screen->gpu_id >= 500)
467 OUT_WFI5(ring);
468 else
469 OUT_WFI(ring);
470 batch->needs_wfi = false;
471 }
472 }