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