freedreno/batch: always update last_fence
[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 * XXX I think we can just require new enough kernel for this?
53 */
54 if ((fd_device_version(ctx->screen->dev) < FD_VERSION_UNLIMITED_CMDS) ||
55 (fd_mesa_debug & FD_DBG_NOGROW)){
56 size = 0x100000;
57 }
58
59 batch->submit = fd_submit_new(ctx->pipe);
60 if (batch->nondraw) {
61 batch->draw = fd_submit_new_ringbuffer(batch->submit, size,
62 FD_RINGBUFFER_PRIMARY | FD_RINGBUFFER_GROWABLE);
63 } else {
64 batch->gmem = fd_submit_new_ringbuffer(batch->submit, size,
65 FD_RINGBUFFER_PRIMARY | FD_RINGBUFFER_GROWABLE);
66 batch->draw = fd_submit_new_ringbuffer(batch->submit, size,
67 FD_RINGBUFFER_GROWABLE);
68
69 if (ctx->screen->gpu_id < 600) {
70 batch->binning = fd_submit_new_ringbuffer(batch->submit,
71 size, FD_RINGBUFFER_GROWABLE);
72 }
73 }
74
75 batch->in_fence_fd = -1;
76 batch->fence = fd_fence_create(batch);
77
78 batch->cleared = 0;
79 batch->fast_cleared = 0;
80 batch->invalidated = 0;
81 batch->restore = batch->resolve = 0;
82 batch->needs_flush = false;
83 batch->flushed = false;
84 batch->gmem_reason = 0;
85 batch->num_draws = 0;
86 batch->num_vertices = 0;
87 batch->stage = FD_STAGE_NULL;
88
89 fd_reset_wfi(batch);
90
91 util_dynarray_init(&batch->draw_patches, NULL);
92 util_dynarray_init(&batch->fb_read_patches, NULL);
93
94 if (is_a2xx(ctx->screen)) {
95 util_dynarray_init(&batch->shader_patches, NULL);
96 util_dynarray_init(&batch->gmem_patches, NULL);
97 }
98
99 if (is_a3xx(ctx->screen))
100 util_dynarray_init(&batch->rbrc_patches, NULL);
101
102 assert(batch->resources->entries == 0);
103
104 util_dynarray_init(&batch->samples, NULL);
105 }
106
107 struct fd_batch *
108 fd_batch_create(struct fd_context *ctx, bool nondraw)
109 {
110 struct fd_batch *batch = CALLOC_STRUCT(fd_batch);
111
112 if (!batch)
113 return NULL;
114
115 DBG("%p", batch);
116
117 pipe_reference_init(&batch->reference, 1);
118 batch->ctx = ctx;
119 batch->nondraw = nondraw;
120
121 batch->resources = _mesa_set_create(NULL, _mesa_hash_pointer,
122 _mesa_key_pointer_equal);
123
124 batch_init(batch);
125
126 return batch;
127 }
128
129 static void
130 batch_fini(struct fd_batch *batch)
131 {
132 DBG("%p", batch);
133
134 pipe_resource_reference(&batch->query_buf, NULL);
135
136 if (batch->in_fence_fd != -1)
137 close(batch->in_fence_fd);
138
139 /* in case batch wasn't flushed but fence was created: */
140 fd_fence_populate(batch->fence, 0, -1);
141
142 fd_fence_ref(&batch->fence, NULL);
143
144 fd_ringbuffer_del(batch->draw);
145 if (!batch->nondraw) {
146 if (batch->binning)
147 fd_ringbuffer_del(batch->binning);
148 fd_ringbuffer_del(batch->gmem);
149 } else {
150 debug_assert(!batch->binning);
151 debug_assert(!batch->gmem);
152 }
153
154 if (batch->lrz_clear) {
155 fd_ringbuffer_del(batch->lrz_clear);
156 batch->lrz_clear = NULL;
157 }
158
159 if (batch->tile_setup) {
160 fd_ringbuffer_del(batch->tile_setup);
161 batch->tile_setup = NULL;
162 }
163
164 if (batch->tile_fini) {
165 fd_ringbuffer_del(batch->tile_fini);
166 batch->tile_fini = NULL;
167 }
168
169 fd_submit_del(batch->submit);
170
171 util_dynarray_fini(&batch->draw_patches);
172 util_dynarray_fini(&batch->fb_read_patches);
173
174 if (is_a2xx(batch->ctx->screen)) {
175 util_dynarray_fini(&batch->shader_patches);
176 util_dynarray_fini(&batch->gmem_patches);
177 }
178
179 if (is_a3xx(batch->ctx->screen))
180 util_dynarray_fini(&batch->rbrc_patches);
181
182 while (batch->samples.size > 0) {
183 struct fd_hw_sample *samp =
184 util_dynarray_pop(&batch->samples, struct fd_hw_sample *);
185 fd_hw_sample_reference(batch->ctx, &samp, NULL);
186 }
187 util_dynarray_fini(&batch->samples);
188
189 if (batch->ctx->screen->reorder)
190 util_queue_fence_destroy(&batch->flush_fence);
191 }
192
193 static void
194 batch_flush_reset_dependencies(struct fd_batch *batch, bool flush)
195 {
196 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
197 struct fd_batch *dep;
198
199 foreach_batch(dep, cache, batch->dependents_mask) {
200 if (flush)
201 fd_batch_flush(dep, false);
202 fd_batch_reference(&dep, NULL);
203 }
204
205 batch->dependents_mask = 0;
206 }
207
208 static void
209 batch_reset_resources_locked(struct fd_batch *batch)
210 {
211 pipe_mutex_assert_locked(batch->ctx->screen->lock);
212
213 set_foreach(batch->resources, entry) {
214 struct fd_resource *rsc = (struct fd_resource *)entry->key;
215 _mesa_set_remove(batch->resources, entry);
216 debug_assert(rsc->batch_mask & (1 << batch->idx));
217 rsc->batch_mask &= ~(1 << batch->idx);
218 if (rsc->write_batch == batch)
219 fd_batch_reference_locked(&rsc->write_batch, NULL);
220 }
221 }
222
223 static void
224 batch_reset_resources(struct fd_batch *batch)
225 {
226 mtx_lock(&batch->ctx->screen->lock);
227 batch_reset_resources_locked(batch);
228 mtx_unlock(&batch->ctx->screen->lock);
229 }
230
231 static void
232 batch_reset(struct fd_batch *batch)
233 {
234 DBG("%p", batch);
235
236 fd_batch_sync(batch);
237
238 batch_flush_reset_dependencies(batch, false);
239 batch_reset_resources(batch);
240
241 batch_fini(batch);
242 batch_init(batch);
243 }
244
245 void
246 fd_batch_reset(struct fd_batch *batch)
247 {
248 if (batch->needs_flush)
249 batch_reset(batch);
250 }
251
252 void
253 __fd_batch_destroy(struct fd_batch *batch)
254 {
255 struct fd_context *ctx = batch->ctx;
256
257 DBG("%p", batch);
258
259 fd_context_assert_locked(batch->ctx);
260
261 fd_bc_invalidate_batch(batch, true);
262
263 batch_reset_resources_locked(batch);
264 debug_assert(batch->resources->entries == 0);
265 _mesa_set_destroy(batch->resources, NULL);
266
267 fd_context_unlock(ctx);
268 batch_flush_reset_dependencies(batch, false);
269 debug_assert(batch->dependents_mask == 0);
270
271 util_copy_framebuffer_state(&batch->framebuffer, NULL);
272 batch_fini(batch);
273 free(batch);
274 fd_context_lock(ctx);
275 }
276
277 void
278 __fd_batch_describe(char* buf, const struct fd_batch *batch)
279 {
280 sprintf(buf, "fd_batch<%u>", batch->seqno);
281 }
282
283 void
284 fd_batch_sync(struct fd_batch *batch)
285 {
286 if (!batch->ctx->screen->reorder)
287 return;
288 util_queue_fence_wait(&batch->flush_fence);
289 }
290
291 static void
292 batch_flush_func(void *job, int id)
293 {
294 struct fd_batch *batch = job;
295
296 DBG("%p", batch);
297
298 fd_gmem_render_tiles(batch);
299 batch_reset_resources(batch);
300 }
301
302 static void
303 batch_cleanup_func(void *job, int id)
304 {
305 struct fd_batch *batch = job;
306 fd_batch_reference(&batch, NULL);
307 }
308
309 static void
310 batch_flush(struct fd_batch *batch)
311 {
312 DBG("%p: needs_flush=%d", batch, batch->needs_flush);
313
314 if (batch->flushed)
315 return;
316
317 batch->needs_flush = false;
318
319 /* close out the draw cmds by making sure any active queries are
320 * paused:
321 */
322 fd_batch_set_stage(batch, FD_STAGE_NULL);
323
324 batch_flush_reset_dependencies(batch, true);
325
326 batch->flushed = true;
327
328 fd_fence_ref(&batch->ctx->last_fence, batch->fence);
329
330 if (batch->ctx->screen->reorder) {
331 struct fd_batch *tmp = NULL;
332 fd_batch_reference(&tmp, batch);
333
334 if (!util_queue_is_initialized(&batch->ctx->flush_queue))
335 util_queue_init(&batch->ctx->flush_queue, "flush_queue", 16, 1, 0);
336
337 util_queue_add_job(&batch->ctx->flush_queue,
338 batch, &batch->flush_fence,
339 batch_flush_func, batch_cleanup_func);
340 } else {
341 fd_gmem_render_tiles(batch);
342 batch_reset_resources(batch);
343 }
344
345 debug_assert(batch->reference.count > 0);
346
347 mtx_lock(&batch->ctx->screen->lock);
348 fd_bc_invalidate_batch(batch, false);
349 mtx_unlock(&batch->ctx->screen->lock);
350 }
351
352 /* NOTE: could drop the last ref to batch
353 *
354 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
355 * to kernel before this returns, as opposed to just being queued to be
356 * flushed
357 * @force: force a flush even if no rendering, mostly useful if you need
358 * a fence to sync on
359 */
360 void
361 fd_batch_flush(struct fd_batch *batch, bool sync)
362 {
363 struct fd_batch *tmp = NULL;
364 bool newbatch = false;
365
366 /* NOTE: we need to hold an extra ref across the body of flush,
367 * since the last ref to this batch could be dropped when cleaning
368 * up used_resources
369 */
370 fd_batch_reference(&tmp, batch);
371
372 if (batch == batch->ctx->batch) {
373 batch->ctx->batch = NULL;
374 newbatch = true;
375 }
376
377 batch_flush(tmp);
378
379 if (newbatch) {
380 struct fd_context *ctx = batch->ctx;
381 struct fd_batch *new_batch;
382
383 if (ctx->screen->reorder) {
384 /* defer allocating new batch until one is needed for rendering
385 * to avoid unused batches for apps that create many contexts
386 */
387 new_batch = NULL;
388 } else {
389 new_batch = fd_bc_alloc_batch(&ctx->screen->batch_cache, ctx, false);
390 util_copy_framebuffer_state(&new_batch->framebuffer, &batch->framebuffer);
391 }
392
393 fd_batch_reference(&batch, NULL);
394 ctx->batch = new_batch;
395 fd_context_all_dirty(ctx);
396 }
397
398 if (sync)
399 fd_batch_sync(tmp);
400
401 fd_batch_reference(&tmp, NULL);
402 }
403
404 /* does 'batch' depend directly or indirectly on 'other' ? */
405 static bool
406 batch_depends_on(struct fd_batch *batch, struct fd_batch *other)
407 {
408 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
409 struct fd_batch *dep;
410
411 if (batch->dependents_mask & (1 << other->idx))
412 return true;
413
414 foreach_batch(dep, cache, other->dependents_mask)
415 if (batch_depends_on(batch, dep))
416 return true;
417
418 return false;
419 }
420
421 void
422 fd_batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
423 {
424 if (batch->dependents_mask & (1 << dep->idx))
425 return;
426
427 /* a loop should not be possible */
428 debug_assert(!batch_depends_on(dep, batch));
429
430 struct fd_batch *other = NULL;
431 fd_batch_reference_locked(&other, dep);
432 batch->dependents_mask |= (1 << dep->idx);
433 DBG("%p: added dependency on %p", batch, dep);
434 }
435
436 static void
437 flush_write_batch(struct fd_resource *rsc)
438 {
439 struct fd_batch *b = NULL;
440 fd_batch_reference_locked(&b, rsc->write_batch);
441
442 mtx_unlock(&b->ctx->screen->lock);
443 fd_batch_flush(b, true);
444 mtx_lock(&b->ctx->screen->lock);
445
446 fd_bc_invalidate_batch(b, false);
447 fd_batch_reference_locked(&b, NULL);
448 }
449
450 void
451 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool write)
452 {
453 pipe_mutex_assert_locked(batch->ctx->screen->lock);
454
455 if (rsc->stencil)
456 fd_batch_resource_used(batch, rsc->stencil, write);
457
458 DBG("%p: %s %p", batch, write ? "write" : "read", rsc);
459
460 if (write)
461 rsc->valid = true;
462
463 /* note, invalidate write batch, to avoid further writes to rsc
464 * resulting in a write-after-read hazard.
465 */
466
467 if (write) {
468 /* if we are pending read or write by any other batch: */
469 if (rsc->batch_mask & ~(1 << batch->idx)) {
470 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
471 struct fd_batch *dep;
472
473 if (rsc->write_batch && rsc->write_batch != batch)
474 flush_write_batch(rsc);
475
476 foreach_batch(dep, cache, rsc->batch_mask) {
477 struct fd_batch *b = NULL;
478 if (dep == batch)
479 continue;
480 /* note that batch_add_dep could flush and unref dep, so
481 * we need to hold a reference to keep it live for the
482 * fd_bc_invalidate_batch()
483 */
484 fd_batch_reference(&b, dep);
485 fd_batch_add_dep(batch, b);
486 fd_bc_invalidate_batch(b, false);
487 fd_batch_reference_locked(&b, NULL);
488 }
489 }
490 fd_batch_reference_locked(&rsc->write_batch, batch);
491 } else {
492 /* If reading a resource pending a write, go ahead and flush the
493 * writer. This avoids situations where we end up having to
494 * flush the current batch in _resource_used()
495 */
496 if (rsc->write_batch && rsc->write_batch != batch)
497 flush_write_batch(rsc);
498 }
499
500 if (rsc->batch_mask & (1 << batch->idx)) {
501 debug_assert(_mesa_set_search(batch->resources, rsc));
502 return;
503 }
504
505 debug_assert(!_mesa_set_search(batch->resources, rsc));
506
507 _mesa_set_add(batch->resources, rsc);
508 rsc->batch_mask |= (1 << batch->idx);
509 }
510
511 void
512 fd_batch_check_size(struct fd_batch *batch)
513 {
514 debug_assert(!batch->flushed);
515
516 if (unlikely(fd_mesa_debug & FD_DBG_FLUSH)) {
517 fd_batch_flush(batch, true);
518 return;
519 }
520
521 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
522 return;
523
524 struct fd_ringbuffer *ring = batch->draw;
525 if ((ring->cur - ring->start) > (ring->size/4 - 0x1000))
526 fd_batch_flush(batch, true);
527 }
528
529 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
530 * been one since last draw:
531 */
532 void
533 fd_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
534 {
535 if (batch->needs_wfi) {
536 if (batch->ctx->screen->gpu_id >= 500)
537 OUT_WFI5(ring);
538 else
539 OUT_WFI(ring);
540 batch->needs_wfi = false;
541 }
542 }