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