fd5cb26cfdaeb4f06d18644fd27fca9016814c33
[mesa.git] / src / gallium / drivers / iris / iris_batch.c
1 /*
2 * Copyright © 2017 Intel Corporation
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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_batch.c
25 *
26 * Batchbuffer and command submission module.
27 *
28 * Every API draw call results in a number of GPU commands, which we
29 * collect into a "batch buffer". Typically, many draw calls are grouped
30 * into a single batch to amortize command submission overhead.
31 *
32 * We submit batches to the kernel using the I915_GEM_EXECBUFFER2 ioctl.
33 * One critical piece of data is the "validation list", which contains a
34 * list of the buffer objects (BOs) which the commands in the GPU need.
35 * The kernel will make sure these are resident and pinned at the correct
36 * virtual memory address before executing our batch. If a BO is not in
37 * the validation list, it effectively does not exist, so take care.
38 */
39
40 #include "iris_batch.h"
41 #include "iris_bufmgr.h"
42 #include "iris_context.h"
43 #include "iris_fence.h"
44
45 #include "drm-uapi/i915_drm.h"
46
47 #include "common/gen_aux_map.h"
48 #include "intel/common/gen_gem.h"
49 #include "util/hash_table.h"
50 #include "util/set.h"
51 #include "main/macros.h"
52
53 #include <errno.h>
54 #include <xf86drm.h>
55
56 #if HAVE_VALGRIND
57 #include <valgrind.h>
58 #include <memcheck.h>
59 #define VG(x) x
60 #else
61 #define VG(x)
62 #endif
63
64 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
65
66 static void
67 iris_batch_reset(struct iris_batch *batch);
68
69 static unsigned
70 num_fences(struct iris_batch *batch)
71 {
72 return util_dynarray_num_elements(&batch->exec_fences,
73 struct drm_i915_gem_exec_fence);
74 }
75
76 /**
77 * Debugging code to dump the fence list, used by INTEL_DEBUG=submit.
78 */
79 static void
80 dump_fence_list(struct iris_batch *batch)
81 {
82 fprintf(stderr, "Fence list (length %u): ", num_fences(batch));
83
84 util_dynarray_foreach(&batch->exec_fences,
85 struct drm_i915_gem_exec_fence, f) {
86 fprintf(stderr, "%s%u%s ",
87 (f->flags & I915_EXEC_FENCE_WAIT) ? "..." : "",
88 f->handle,
89 (f->flags & I915_EXEC_FENCE_SIGNAL) ? "!" : "");
90 }
91
92 fprintf(stderr, "\n");
93 }
94
95 /**
96 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
97 */
98 static void
99 dump_validation_list(struct iris_batch *batch)
100 {
101 fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
102
103 for (int i = 0; i < batch->exec_count; i++) {
104 uint64_t flags = batch->validation_list[i].flags;
105 assert(batch->validation_list[i].handle ==
106 batch->exec_bos[i]->gem_handle);
107 fprintf(stderr, "[%2d]: %2d %-14s @ 0x%016llx (%"PRIu64"B)\t %2d refs %s\n",
108 i,
109 batch->validation_list[i].handle,
110 batch->exec_bos[i]->name,
111 batch->validation_list[i].offset,
112 batch->exec_bos[i]->size,
113 batch->exec_bos[i]->refcount,
114 (flags & EXEC_OBJECT_WRITE) ? " (write)" : "");
115 }
116 }
117
118 /**
119 * Return BO information to the batch decoder (for debugging).
120 */
121 static struct gen_batch_decode_bo
122 decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
123 {
124 struct iris_batch *batch = v_batch;
125
126 assert(ppgtt);
127
128 for (int i = 0; i < batch->exec_count; i++) {
129 struct iris_bo *bo = batch->exec_bos[i];
130 /* The decoder zeroes out the top 16 bits, so we need to as well */
131 uint64_t bo_address = bo->gtt_offset & (~0ull >> 16);
132
133 if (address >= bo_address && address < bo_address + bo->size) {
134 return (struct gen_batch_decode_bo) {
135 .addr = address,
136 .size = bo->size,
137 .map = iris_bo_map(batch->dbg, bo, MAP_READ) +
138 (address - bo_address),
139 };
140 }
141 }
142
143 return (struct gen_batch_decode_bo) { };
144 }
145
146 static unsigned
147 decode_get_state_size(void *v_batch,
148 uint64_t address,
149 UNUSED uint64_t base_address)
150 {
151 struct iris_batch *batch = v_batch;
152 unsigned size = (uintptr_t)
153 _mesa_hash_table_u64_search(batch->state_sizes, address);
154
155 return size;
156 }
157
158 /**
159 * Decode the current batch.
160 */
161 static void
162 decode_batch(struct iris_batch *batch)
163 {
164 void *map = iris_bo_map(batch->dbg, batch->exec_bos[0], MAP_READ);
165 gen_print_batch(&batch->decoder, map, batch->primary_batch_size,
166 batch->exec_bos[0]->gtt_offset, false);
167 }
168
169 void
170 iris_init_batch(struct iris_batch *batch,
171 struct iris_screen *screen,
172 struct pipe_debug_callback *dbg,
173 struct pipe_device_reset_callback *reset,
174 struct hash_table_u64 *state_sizes,
175 struct iris_batch *all_batches,
176 enum iris_batch_name name,
177 int priority)
178 {
179 batch->screen = screen;
180 batch->dbg = dbg;
181 batch->reset = reset;
182 batch->state_sizes = state_sizes;
183 batch->name = name;
184
185 batch->hw_ctx_id = iris_create_hw_context(screen->bufmgr);
186 assert(batch->hw_ctx_id);
187
188 iris_hw_context_set_priority(screen->bufmgr, batch->hw_ctx_id, priority);
189
190 util_dynarray_init(&batch->exec_fences, ralloc_context(NULL));
191 util_dynarray_init(&batch->syncpts, ralloc_context(NULL));
192
193 batch->exec_count = 0;
194 batch->exec_array_size = 100;
195 batch->exec_bos =
196 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
197 batch->validation_list =
198 malloc(batch->exec_array_size * sizeof(batch->validation_list[0]));
199
200 batch->cache.render = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
201 _mesa_key_pointer_equal);
202 batch->cache.depth = _mesa_set_create(NULL, _mesa_hash_pointer,
203 _mesa_key_pointer_equal);
204
205 memset(batch->other_batches, 0, sizeof(batch->other_batches));
206
207 for (int i = 0, j = 0; i < IRIS_BATCH_COUNT; i++) {
208 if (&all_batches[i] != batch)
209 batch->other_batches[j++] = &all_batches[i];
210 }
211
212 if (unlikely(INTEL_DEBUG)) {
213 const unsigned decode_flags =
214 GEN_BATCH_DECODE_FULL |
215 ((INTEL_DEBUG & DEBUG_COLOR) ? GEN_BATCH_DECODE_IN_COLOR : 0) |
216 GEN_BATCH_DECODE_OFFSETS |
217 GEN_BATCH_DECODE_FLOATS;
218
219 gen_batch_decode_ctx_init(&batch->decoder, &screen->devinfo,
220 stderr, decode_flags, NULL,
221 decode_get_bo, decode_get_state_size, batch);
222 batch->decoder.dynamic_base = IRIS_MEMZONE_DYNAMIC_START;
223 batch->decoder.instruction_base = IRIS_MEMZONE_SHADER_START;
224 batch->decoder.max_vbo_decoded_lines = 32;
225 }
226
227 iris_batch_reset(batch);
228 }
229
230 static struct drm_i915_gem_exec_object2 *
231 find_validation_entry(struct iris_batch *batch, struct iris_bo *bo)
232 {
233 unsigned index = READ_ONCE(bo->index);
234
235 if (index < batch->exec_count && batch->exec_bos[index] == bo)
236 return &batch->validation_list[index];
237
238 /* May have been shared between multiple active batches */
239 for (index = 0; index < batch->exec_count; index++) {
240 if (batch->exec_bos[index] == bo)
241 return &batch->validation_list[index];
242 }
243
244 return NULL;
245 }
246
247 static void
248 ensure_exec_obj_space(struct iris_batch *batch, uint32_t count)
249 {
250 while (batch->exec_count + count > batch->exec_array_size) {
251 batch->exec_array_size *= 2;
252 batch->exec_bos =
253 realloc(batch->exec_bos,
254 batch->exec_array_size * sizeof(batch->exec_bos[0]));
255 batch->validation_list =
256 realloc(batch->validation_list,
257 batch->exec_array_size * sizeof(batch->validation_list[0]));
258 }
259 }
260
261 /**
262 * Add a buffer to the current batch's validation list.
263 *
264 * You must call this on any BO you wish to use in this batch, to ensure
265 * that it's resident when the GPU commands execute.
266 */
267 void
268 iris_use_pinned_bo(struct iris_batch *batch,
269 struct iris_bo *bo,
270 bool writable)
271 {
272 assert(bo->kflags & EXEC_OBJECT_PINNED);
273
274 /* Never mark the workaround BO with EXEC_OBJECT_WRITE. We don't care
275 * about the order of any writes to that buffer, and marking it writable
276 * would introduce data dependencies between multiple batches which share
277 * the buffer.
278 */
279 if (bo == batch->screen->workaround_bo)
280 writable = false;
281
282 struct drm_i915_gem_exec_object2 *existing_entry =
283 find_validation_entry(batch, bo);
284
285 if (existing_entry) {
286 /* The BO is already in the validation list; mark it writable */
287 if (writable)
288 existing_entry->flags |= EXEC_OBJECT_WRITE;
289
290 return;
291 }
292
293 if (bo != batch->bo) {
294 /* This is the first time our batch has seen this BO. Before we use it,
295 * we may need to flush and synchronize with other batches.
296 */
297 for (int b = 0; b < ARRAY_SIZE(batch->other_batches); b++) {
298 struct drm_i915_gem_exec_object2 *other_entry =
299 find_validation_entry(batch->other_batches[b], bo);
300
301 /* If the buffer is referenced by another batch, and either batch
302 * intends to write it, then flush the other batch and synchronize.
303 *
304 * Consider these cases:
305 *
306 * 1. They read, we read => No synchronization required.
307 * 2. They read, we write => Synchronize (they need the old value)
308 * 3. They write, we read => Synchronize (we need their new value)
309 * 4. They write, we write => Synchronize (order writes)
310 *
311 * The read/read case is very common, as multiple batches usually
312 * share a streaming state buffer or shader assembly buffer, and
313 * we want to avoid synchronizing in this case.
314 */
315 if (other_entry &&
316 ((other_entry->flags & EXEC_OBJECT_WRITE) || writable)) {
317 iris_batch_flush(batch->other_batches[b]);
318 iris_batch_add_syncpt(batch, batch->other_batches[b]->last_syncpt,
319 I915_EXEC_FENCE_WAIT);
320 }
321 }
322 }
323
324 /* Now, take a reference and add it to the validation list. */
325 iris_bo_reference(bo);
326
327 ensure_exec_obj_space(batch, 1);
328
329 batch->validation_list[batch->exec_count] =
330 (struct drm_i915_gem_exec_object2) {
331 .handle = bo->gem_handle,
332 .offset = bo->gtt_offset,
333 .flags = bo->kflags | (writable ? EXEC_OBJECT_WRITE : 0),
334 };
335
336 bo->index = batch->exec_count;
337 batch->exec_bos[batch->exec_count] = bo;
338 batch->aperture_space += bo->size;
339
340 batch->exec_count++;
341 }
342
343 static void
344 create_batch(struct iris_batch *batch)
345 {
346 struct iris_screen *screen = batch->screen;
347 struct iris_bufmgr *bufmgr = screen->bufmgr;
348
349 batch->bo = iris_bo_alloc(bufmgr, "command buffer",
350 BATCH_SZ + BATCH_RESERVED, IRIS_MEMZONE_OTHER);
351 batch->bo->kflags |= EXEC_OBJECT_CAPTURE;
352 batch->map = iris_bo_map(NULL, batch->bo, MAP_READ | MAP_WRITE);
353 batch->map_next = batch->map;
354
355 iris_use_pinned_bo(batch, batch->bo, false);
356 }
357
358 static void
359 iris_batch_maybe_noop(struct iris_batch *batch)
360 {
361 /* We only insert the NOOP at the beginning of the batch. */
362 assert(iris_batch_bytes_used(batch) == 0);
363
364 if (batch->noop_enabled) {
365 /* Emit MI_BATCH_BUFFER_END to prevent any further command to be
366 * executed.
367 */
368 uint32_t *map = batch->map_next;
369
370 map[0] = (0xA << 23);
371
372 batch->map_next += 4;
373 }
374 }
375
376 static void
377 iris_batch_reset(struct iris_batch *batch)
378 {
379 struct iris_screen *screen = batch->screen;
380
381 iris_bo_unreference(batch->bo);
382 batch->primary_batch_size = 0;
383 batch->total_chained_batch_size = 0;
384 batch->contains_draw = false;
385 batch->decoder.surface_base = batch->last_surface_base_address;
386
387 create_batch(batch);
388 assert(batch->bo->index == 0);
389
390 struct iris_syncpt *syncpt = iris_create_syncpt(screen);
391 iris_batch_add_syncpt(batch, syncpt, I915_EXEC_FENCE_SIGNAL);
392 iris_syncpt_reference(screen, &syncpt, NULL);
393
394 iris_cache_sets_clear(batch);
395
396 iris_batch_maybe_noop(batch);
397 }
398
399 void
400 iris_batch_free(struct iris_batch *batch)
401 {
402 struct iris_screen *screen = batch->screen;
403 struct iris_bufmgr *bufmgr = screen->bufmgr;
404
405 for (int i = 0; i < batch->exec_count; i++) {
406 iris_bo_unreference(batch->exec_bos[i]);
407 }
408 free(batch->exec_bos);
409 free(batch->validation_list);
410
411 ralloc_free(batch->exec_fences.mem_ctx);
412
413 util_dynarray_foreach(&batch->syncpts, struct iris_syncpt *, s)
414 iris_syncpt_reference(screen, s, NULL);
415 ralloc_free(batch->syncpts.mem_ctx);
416
417 iris_syncpt_reference(screen, &batch->last_syncpt, NULL);
418
419 iris_bo_unreference(batch->bo);
420 batch->bo = NULL;
421 batch->map = NULL;
422 batch->map_next = NULL;
423
424 iris_destroy_hw_context(bufmgr, batch->hw_ctx_id);
425
426 _mesa_hash_table_destroy(batch->cache.render, NULL);
427 _mesa_set_destroy(batch->cache.depth, NULL);
428
429 if (unlikely(INTEL_DEBUG))
430 gen_batch_decode_ctx_finish(&batch->decoder);
431 }
432
433 /**
434 * If we've chained to a secondary batch, or are getting near to the end,
435 * then flush. This should only be called between draws.
436 */
437 void
438 iris_batch_maybe_flush(struct iris_batch *batch, unsigned estimate)
439 {
440 if (batch->bo != batch->exec_bos[0] ||
441 iris_batch_bytes_used(batch) + estimate >= BATCH_SZ) {
442 iris_batch_flush(batch);
443 }
444 }
445
446 static void
447 record_batch_sizes(struct iris_batch *batch)
448 {
449 unsigned batch_size = iris_batch_bytes_used(batch);
450
451 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->map, batch_size));
452
453 if (batch->bo == batch->exec_bos[0])
454 batch->primary_batch_size = batch_size;
455
456 batch->total_chained_batch_size += batch_size;
457 }
458
459 void
460 iris_chain_to_new_batch(struct iris_batch *batch)
461 {
462 uint32_t *cmd = batch->map_next;
463 uint64_t *addr = batch->map_next + 4;
464 batch->map_next += 12;
465
466 record_batch_sizes(batch);
467
468 /* No longer held by batch->bo, still held by validation list */
469 iris_bo_unreference(batch->bo);
470 create_batch(batch);
471
472 /* Emit MI_BATCH_BUFFER_START to chain to another batch. */
473 *cmd = (0x31 << 23) | (1 << 8) | (3 - 2);
474 *addr = batch->bo->gtt_offset;
475 }
476
477 static void
478 add_aux_map_bos_to_batch(struct iris_batch *batch)
479 {
480 void *aux_map_ctx = iris_bufmgr_get_aux_map_context(batch->screen->bufmgr);
481 if (!aux_map_ctx)
482 return;
483
484 uint32_t count = gen_aux_map_get_num_buffers(aux_map_ctx);
485 ensure_exec_obj_space(batch, count);
486 gen_aux_map_fill_bos(aux_map_ctx,
487 (void**)&batch->exec_bos[batch->exec_count], count);
488 for (uint32_t i = 0; i < count; i++) {
489 struct iris_bo *bo = batch->exec_bos[batch->exec_count];
490 iris_bo_reference(bo);
491 batch->validation_list[batch->exec_count] =
492 (struct drm_i915_gem_exec_object2) {
493 .handle = bo->gem_handle,
494 .offset = bo->gtt_offset,
495 .flags = bo->kflags,
496 };
497 batch->aperture_space += bo->size;
498 batch->exec_count++;
499 }
500 }
501
502 /**
503 * Terminate a batch with MI_BATCH_BUFFER_END.
504 */
505 static void
506 iris_finish_batch(struct iris_batch *batch)
507 {
508 add_aux_map_bos_to_batch(batch);
509
510 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
511 uint32_t *map = batch->map_next;
512
513 map[0] = (0xA << 23);
514
515 batch->map_next += 4;
516
517 record_batch_sizes(batch);
518 }
519
520 /**
521 * Replace our current GEM context with a new one (in case it got banned).
522 */
523 static bool
524 replace_hw_ctx(struct iris_batch *batch)
525 {
526 struct iris_screen *screen = batch->screen;
527 struct iris_bufmgr *bufmgr = screen->bufmgr;
528
529 uint32_t new_ctx = iris_clone_hw_context(bufmgr, batch->hw_ctx_id);
530 if (!new_ctx)
531 return false;
532
533 iris_destroy_hw_context(bufmgr, batch->hw_ctx_id);
534 batch->hw_ctx_id = new_ctx;
535
536 /* Notify the context that state must be re-initialized. */
537 iris_lost_context_state(batch);
538
539 return true;
540 }
541
542 enum pipe_reset_status
543 iris_batch_check_for_reset(struct iris_batch *batch)
544 {
545 struct iris_screen *screen = batch->screen;
546 enum pipe_reset_status status = PIPE_NO_RESET;
547 struct drm_i915_reset_stats stats = { .ctx_id = batch->hw_ctx_id };
548
549 if (drmIoctl(screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats))
550 DBG("DRM_IOCTL_I915_GET_RESET_STATS failed: %s\n", strerror(errno));
551
552 if (stats.batch_active != 0) {
553 /* A reset was observed while a batch from this hardware context was
554 * executing. Assume that this context was at fault.
555 */
556 status = PIPE_GUILTY_CONTEXT_RESET;
557 } else if (stats.batch_pending != 0) {
558 /* A reset was observed while a batch from this context was in progress,
559 * but the batch was not executing. In this case, assume that the
560 * context was not at fault.
561 */
562 status = PIPE_INNOCENT_CONTEXT_RESET;
563 }
564
565 if (status != PIPE_NO_RESET) {
566 /* Our context is likely banned, or at least in an unknown state.
567 * Throw it away and start with a fresh context. Ideally this may
568 * catch the problem before our next execbuf fails with -EIO.
569 */
570 replace_hw_ctx(batch);
571 }
572
573 return status;
574 }
575
576 /**
577 * Submit the batch to the GPU via execbuffer2.
578 */
579 static int
580 submit_batch(struct iris_batch *batch)
581 {
582 iris_bo_unmap(batch->bo);
583
584 /* The requirement for using I915_EXEC_NO_RELOC are:
585 *
586 * The addresses written in the objects must match the corresponding
587 * reloc.gtt_offset which in turn must match the corresponding
588 * execobject.offset.
589 *
590 * Any render targets written to in the batch must be flagged with
591 * EXEC_OBJECT_WRITE.
592 *
593 * To avoid stalling, execobject.offset should match the current
594 * address of that object within the active context.
595 */
596 struct drm_i915_gem_execbuffer2 execbuf = {
597 .buffers_ptr = (uintptr_t) batch->validation_list,
598 .buffer_count = batch->exec_count,
599 .batch_start_offset = 0,
600 /* This must be QWord aligned. */
601 .batch_len = ALIGN(batch->primary_batch_size, 8),
602 .flags = I915_EXEC_RENDER |
603 I915_EXEC_NO_RELOC |
604 I915_EXEC_BATCH_FIRST |
605 I915_EXEC_HANDLE_LUT,
606 .rsvd1 = batch->hw_ctx_id, /* rsvd1 is actually the context ID */
607 };
608
609 if (num_fences(batch)) {
610 execbuf.flags |= I915_EXEC_FENCE_ARRAY;
611 execbuf.num_cliprects = num_fences(batch);
612 execbuf.cliprects_ptr =
613 (uintptr_t)util_dynarray_begin(&batch->exec_fences);
614 }
615
616 int ret = 0;
617 if (!batch->screen->no_hw &&
618 gen_ioctl(batch->screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf))
619 ret = -errno;
620
621 for (int i = 0; i < batch->exec_count; i++) {
622 struct iris_bo *bo = batch->exec_bos[i];
623
624 bo->idle = false;
625 bo->index = -1;
626
627 iris_bo_unreference(bo);
628 }
629
630 return ret;
631 }
632
633 static const char *
634 batch_name_to_string(enum iris_batch_name name)
635 {
636 const char *names[IRIS_BATCH_COUNT] = {
637 [IRIS_BATCH_RENDER] = "render",
638 [IRIS_BATCH_COMPUTE] = "compute",
639 };
640 return names[name];
641 }
642
643 /**
644 * Flush the batch buffer, submitting it to the GPU and resetting it so
645 * we're ready to emit the next batch.
646 *
647 * \param in_fence_fd is ignored if -1. Otherwise, this function takes
648 * ownership of the fd.
649 *
650 * \param out_fence_fd is ignored if NULL. Otherwise, the caller must
651 * take ownership of the returned fd.
652 */
653 void
654 _iris_batch_flush(struct iris_batch *batch, const char *file, int line)
655 {
656 struct iris_screen *screen = batch->screen;
657
658 if (iris_batch_bytes_used(batch) == 0)
659 return;
660
661 iris_finish_batch(batch);
662
663 if (unlikely(INTEL_DEBUG &
664 (DEBUG_BATCH | DEBUG_SUBMIT | DEBUG_PIPE_CONTROL))) {
665 const char *basefile = strstr(file, "iris/");
666 if (basefile)
667 file = basefile + 5;
668
669 fprintf(stderr, "%19s:%-3d: %s batch [%u] flush with %5db (%0.1f%%) "
670 "(cmds), %4d BOs (%0.1fMb aperture)\n",
671 file, line, batch_name_to_string(batch->name), batch->hw_ctx_id,
672 batch->total_chained_batch_size,
673 100.0f * batch->total_chained_batch_size / BATCH_SZ,
674 batch->exec_count,
675 (float) batch->aperture_space / (1024 * 1024));
676
677 if (INTEL_DEBUG & (DEBUG_BATCH | DEBUG_SUBMIT)) {
678 dump_fence_list(batch);
679 dump_validation_list(batch);
680 }
681
682 if (INTEL_DEBUG & DEBUG_BATCH) {
683 decode_batch(batch);
684 }
685 }
686
687 int ret = submit_batch(batch);
688
689 batch->exec_count = 0;
690 batch->aperture_space = 0;
691
692 struct iris_syncpt *syncpt =
693 ((struct iris_syncpt **) util_dynarray_begin(&batch->syncpts))[0];
694 iris_syncpt_reference(screen, &batch->last_syncpt, syncpt);
695
696 util_dynarray_foreach(&batch->syncpts, struct iris_syncpt *, s)
697 iris_syncpt_reference(screen, s, NULL);
698 util_dynarray_clear(&batch->syncpts);
699
700 util_dynarray_clear(&batch->exec_fences);
701
702 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
703 dbg_printf("waiting for idle\n");
704 iris_bo_wait_rendering(batch->bo); /* if execbuf failed; this is a nop */
705 }
706
707 /* Start a new batch buffer. */
708 iris_batch_reset(batch);
709
710 /* EIO means our context is banned. In this case, try and replace it
711 * with a new logical context, and inform iris_context that all state
712 * has been lost and needs to be re-initialized. If this succeeds,
713 * dubiously claim success...
714 */
715 if (ret == -EIO && replace_hw_ctx(batch)) {
716 if (batch->reset->reset) {
717 /* Tell the state tracker the device is lost and it was our fault. */
718 batch->reset->reset(batch->reset->data, PIPE_GUILTY_CONTEXT_RESET);
719 }
720
721 ret = 0;
722 }
723
724 if (ret < 0) {
725 #ifdef DEBUG
726 const bool color = INTEL_DEBUG & DEBUG_COLOR;
727 fprintf(stderr, "%siris: Failed to submit batchbuffer: %-80s%s\n",
728 color ? "\e[1;41m" : "", strerror(-ret), color ? "\e[0m" : "");
729 #endif
730 abort();
731 }
732 }
733
734 /**
735 * Does the current batch refer to the given BO?
736 *
737 * (In other words, is the BO in the current batch's validation list?)
738 */
739 bool
740 iris_batch_references(struct iris_batch *batch, struct iris_bo *bo)
741 {
742 return find_validation_entry(batch, bo) != NULL;
743 }
744
745 /**
746 * Updates the state of the noop feature.
747 */
748 uint64_t
749 iris_batch_prepare_noop(struct iris_batch *batch, bool noop_enable, uint64_t dirty_flags)
750 {
751 if (batch->noop_enabled == noop_enable)
752 return 0;
753
754 batch->noop_enabled = noop_enable;
755
756 iris_batch_flush(batch);
757
758 /* If the batch was empty, flush had no effect, so insert our noop. */
759 if (iris_batch_bytes_used(batch) == 0)
760 iris_batch_maybe_noop(batch);
761
762 /* We only need to update the entire state if we transition from noop ->
763 * not-noop.
764 */
765 return !batch->noop_enabled ? dirty_flags : 0;
766 }