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