zink: always allow transfer to/from buffers
[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 /* Terminating the batch takes either 4 bytes for MI_BATCH_BUFFER_END
67 * or 12 bytes for MI_BATCH_BUFFER_START (when chaining). Plus, we may
68 * need an extra 4 bytes to pad out to the nearest QWord. So reserve 16.
69 */
70 #define BATCH_RESERVED 16
71
72 static void
73 iris_batch_reset(struct iris_batch *batch);
74
75 static unsigned
76 num_fences(struct iris_batch *batch)
77 {
78 return util_dynarray_num_elements(&batch->exec_fences,
79 struct drm_i915_gem_exec_fence);
80 }
81
82 /**
83 * Debugging code to dump the fence list, used by INTEL_DEBUG=submit.
84 */
85 static void
86 dump_fence_list(struct iris_batch *batch)
87 {
88 fprintf(stderr, "Fence list (length %u): ", num_fences(batch));
89
90 util_dynarray_foreach(&batch->exec_fences,
91 struct drm_i915_gem_exec_fence, f) {
92 fprintf(stderr, "%s%u%s ",
93 (f->flags & I915_EXEC_FENCE_WAIT) ? "..." : "",
94 f->handle,
95 (f->flags & I915_EXEC_FENCE_SIGNAL) ? "!" : "");
96 }
97
98 fprintf(stderr, "\n");
99 }
100
101 /**
102 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
103 */
104 static void
105 dump_validation_list(struct iris_batch *batch)
106 {
107 fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
108
109 for (int i = 0; i < batch->exec_count; i++) {
110 uint64_t flags = batch->validation_list[i].flags;
111 assert(batch->validation_list[i].handle ==
112 batch->exec_bos[i]->gem_handle);
113 fprintf(stderr, "[%2d]: %2d %-14s @ 0x%016llx (%"PRIu64"B)\t %2d refs %s\n",
114 i,
115 batch->validation_list[i].handle,
116 batch->exec_bos[i]->name,
117 batch->validation_list[i].offset,
118 batch->exec_bos[i]->size,
119 batch->exec_bos[i]->refcount,
120 (flags & EXEC_OBJECT_WRITE) ? " (write)" : "");
121 }
122 }
123
124 /**
125 * Return BO information to the batch decoder (for debugging).
126 */
127 static struct gen_batch_decode_bo
128 decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
129 {
130 struct iris_batch *batch = v_batch;
131
132 assert(ppgtt);
133
134 for (int i = 0; i < batch->exec_count; i++) {
135 struct iris_bo *bo = batch->exec_bos[i];
136 /* The decoder zeroes out the top 16 bits, so we need to as well */
137 uint64_t bo_address = bo->gtt_offset & (~0ull >> 16);
138
139 if (address >= bo_address && address < bo_address + bo->size) {
140 return (struct gen_batch_decode_bo) {
141 .addr = address,
142 .size = bo->size,
143 .map = iris_bo_map(batch->dbg, bo, MAP_READ) +
144 (address - bo_address),
145 };
146 }
147 }
148
149 return (struct gen_batch_decode_bo) { };
150 }
151
152 static unsigned
153 decode_get_state_size(void *v_batch, uint32_t offset_from_base)
154 {
155 struct iris_batch *batch = v_batch;
156
157 /* The decoder gives us offsets from a base address, which is not great.
158 * Binding tables are relative to surface state base address, and other
159 * state is relative to dynamic state base address. These could alias,
160 * but in practice it's unlikely because surface offsets are always in
161 * the [0, 64K) range, and we assign dynamic state addresses starting at
162 * the top of the 4GB range. We should fix this but it's likely good
163 * enough for now.
164 */
165 unsigned size = (uintptr_t)
166 _mesa_hash_table_u64_search(batch->state_sizes, offset_from_base);
167
168 return size;
169 }
170
171 /**
172 * Decode the current batch.
173 */
174 static void
175 decode_batch(struct iris_batch *batch)
176 {
177 void *map = iris_bo_map(batch->dbg, batch->exec_bos[0], MAP_READ);
178 gen_print_batch(&batch->decoder, map, batch->primary_batch_size,
179 batch->exec_bos[0]->gtt_offset, false);
180 }
181
182 void
183 iris_init_batch(struct iris_batch *batch,
184 struct iris_screen *screen,
185 struct iris_vtable *vtbl,
186 struct pipe_debug_callback *dbg,
187 struct pipe_device_reset_callback *reset,
188 struct hash_table_u64 *state_sizes,
189 struct iris_batch *all_batches,
190 enum iris_batch_name name,
191 uint8_t engine,
192 int priority)
193 {
194 batch->screen = screen;
195 batch->vtbl = vtbl;
196 batch->dbg = dbg;
197 batch->reset = reset;
198 batch->state_sizes = state_sizes;
199 batch->name = name;
200
201 /* engine should be one of I915_EXEC_RENDER, I915_EXEC_BLT, etc. */
202 assert((engine & ~I915_EXEC_RING_MASK) == 0);
203 assert(util_bitcount(engine) == 1);
204 batch->engine = engine;
205
206 batch->hw_ctx_id = iris_create_hw_context(screen->bufmgr);
207 assert(batch->hw_ctx_id);
208
209 iris_hw_context_set_priority(screen->bufmgr, batch->hw_ctx_id, priority);
210
211 util_dynarray_init(&batch->exec_fences, ralloc_context(NULL));
212 util_dynarray_init(&batch->syncpts, ralloc_context(NULL));
213
214 batch->exec_count = 0;
215 batch->exec_array_size = 100;
216 batch->exec_bos =
217 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
218 batch->validation_list =
219 malloc(batch->exec_array_size * sizeof(batch->validation_list[0]));
220
221 batch->cache.render = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
222 _mesa_key_pointer_equal);
223 batch->cache.depth = _mesa_set_create(NULL, _mesa_hash_pointer,
224 _mesa_key_pointer_equal);
225
226 memset(batch->other_batches, 0, sizeof(batch->other_batches));
227
228 for (int i = 0, j = 0; i < IRIS_BATCH_COUNT; i++) {
229 if (&all_batches[i] != batch)
230 batch->other_batches[j++] = &all_batches[i];
231 }
232
233 if (unlikely(INTEL_DEBUG)) {
234 const unsigned decode_flags =
235 GEN_BATCH_DECODE_FULL |
236 ((INTEL_DEBUG & DEBUG_COLOR) ? GEN_BATCH_DECODE_IN_COLOR : 0) |
237 GEN_BATCH_DECODE_OFFSETS |
238 GEN_BATCH_DECODE_FLOATS;
239
240 gen_batch_decode_ctx_init(&batch->decoder, &screen->devinfo,
241 stderr, decode_flags, NULL,
242 decode_get_bo, decode_get_state_size, batch);
243 batch->decoder.dynamic_base = IRIS_MEMZONE_DYNAMIC_START;
244 batch->decoder.instruction_base = IRIS_MEMZONE_SHADER_START;
245 batch->decoder.max_vbo_decoded_lines = 32;
246 }
247
248 iris_batch_reset(batch);
249 }
250
251 static struct drm_i915_gem_exec_object2 *
252 find_validation_entry(struct iris_batch *batch, struct iris_bo *bo)
253 {
254 unsigned index = READ_ONCE(bo->index);
255
256 if (index < batch->exec_count && batch->exec_bos[index] == bo)
257 return &batch->validation_list[index];
258
259 /* May have been shared between multiple active batches */
260 for (index = 0; index < batch->exec_count; index++) {
261 if (batch->exec_bos[index] == bo)
262 return &batch->validation_list[index];
263 }
264
265 return NULL;
266 }
267
268 static void
269 ensure_exec_obj_space(struct iris_batch *batch, uint32_t count)
270 {
271 while (batch->exec_count + count > batch->exec_array_size) {
272 batch->exec_array_size *= 2;
273 batch->exec_bos =
274 realloc(batch->exec_bos,
275 batch->exec_array_size * sizeof(batch->exec_bos[0]));
276 batch->validation_list =
277 realloc(batch->validation_list,
278 batch->exec_array_size * sizeof(batch->validation_list[0]));
279 }
280 }
281
282 /**
283 * Add a buffer to the current batch's validation list.
284 *
285 * You must call this on any BO you wish to use in this batch, to ensure
286 * that it's resident when the GPU commands execute.
287 */
288 void
289 iris_use_pinned_bo(struct iris_batch *batch,
290 struct iris_bo *bo,
291 bool writable)
292 {
293 assert(bo->kflags & EXEC_OBJECT_PINNED);
294
295 /* Never mark the workaround BO with EXEC_OBJECT_WRITE. We don't care
296 * about the order of any writes to that buffer, and marking it writable
297 * would introduce data dependencies between multiple batches which share
298 * the buffer.
299 */
300 if (bo == batch->screen->workaround_bo)
301 writable = false;
302
303 struct drm_i915_gem_exec_object2 *existing_entry =
304 find_validation_entry(batch, bo);
305
306 if (existing_entry) {
307 /* The BO is already in the validation list; mark it writable */
308 if (writable)
309 existing_entry->flags |= EXEC_OBJECT_WRITE;
310
311 return;
312 }
313
314 if (bo != batch->bo) {
315 /* This is the first time our batch has seen this BO. Before we use it,
316 * we may need to flush and synchronize with other batches.
317 */
318 for (int b = 0; b < ARRAY_SIZE(batch->other_batches); b++) {
319 struct drm_i915_gem_exec_object2 *other_entry =
320 find_validation_entry(batch->other_batches[b], bo);
321
322 /* If the buffer is referenced by another batch, and either batch
323 * intends to write it, then flush the other batch and synchronize.
324 *
325 * Consider these cases:
326 *
327 * 1. They read, we read => No synchronization required.
328 * 2. They read, we write => Synchronize (they need the old value)
329 * 3. They write, we read => Synchronize (we need their new value)
330 * 4. They write, we write => Synchronize (order writes)
331 *
332 * The read/read case is very common, as multiple batches usually
333 * share a streaming state buffer or shader assembly buffer, and
334 * we want to avoid synchronizing in this case.
335 */
336 if (other_entry &&
337 ((other_entry->flags & EXEC_OBJECT_WRITE) || writable)) {
338 iris_batch_flush(batch->other_batches[b]);
339 iris_batch_add_syncpt(batch, batch->other_batches[b]->last_syncpt,
340 I915_EXEC_FENCE_WAIT);
341 }
342 }
343 }
344
345 /* Now, take a reference and add it to the validation list. */
346 iris_bo_reference(bo);
347
348 ensure_exec_obj_space(batch, 1);
349
350 batch->validation_list[batch->exec_count] =
351 (struct drm_i915_gem_exec_object2) {
352 .handle = bo->gem_handle,
353 .offset = bo->gtt_offset,
354 .flags = bo->kflags | (writable ? EXEC_OBJECT_WRITE : 0),
355 };
356
357 bo->index = batch->exec_count;
358 batch->exec_bos[batch->exec_count] = bo;
359 batch->aperture_space += bo->size;
360
361 batch->exec_count++;
362 }
363
364 static void
365 create_batch(struct iris_batch *batch)
366 {
367 struct iris_screen *screen = batch->screen;
368 struct iris_bufmgr *bufmgr = screen->bufmgr;
369
370 batch->bo = iris_bo_alloc(bufmgr, "command buffer",
371 BATCH_SZ + BATCH_RESERVED, IRIS_MEMZONE_OTHER);
372 batch->bo->kflags |= EXEC_OBJECT_CAPTURE;
373 batch->map = iris_bo_map(NULL, batch->bo, MAP_READ | MAP_WRITE);
374 batch->map_next = batch->map;
375
376 iris_use_pinned_bo(batch, batch->bo, false);
377 }
378
379 static void
380 iris_batch_reset(struct iris_batch *batch)
381 {
382 struct iris_screen *screen = batch->screen;
383
384 iris_bo_unreference(batch->bo);
385 batch->primary_batch_size = 0;
386 batch->contains_draw = false;
387 batch->decoder.surface_base = batch->last_surface_base_address;
388
389 create_batch(batch);
390 assert(batch->bo->index == 0);
391
392 struct iris_syncpt *syncpt = iris_create_syncpt(screen);
393 iris_batch_add_syncpt(batch, syncpt, I915_EXEC_FENCE_SIGNAL);
394 iris_syncpt_reference(screen, &syncpt, NULL);
395
396 iris_cache_sets_clear(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 void
447 iris_chain_to_new_batch(struct iris_batch *batch)
448 {
449 /* We only support chaining a single time. */
450 assert(batch->bo == batch->exec_bos[0]);
451
452 VG(void *map = batch->map);
453 uint32_t *cmd = batch->map_next;
454 uint64_t *addr = batch->map_next + 4;
455 batch->map_next += 12;
456
457 /* No longer held by batch->bo, still held by validation list */
458 iris_bo_unreference(batch->bo);
459 batch->primary_batch_size = iris_batch_bytes_used(batch);
460 create_batch(batch);
461
462 /* Emit MI_BATCH_BUFFER_START to chain to another batch. */
463 *cmd = (0x31 << 23) | (1 << 8) | (3 - 2);
464 *addr = batch->bo->gtt_offset;
465
466 VG(VALGRIND_CHECK_MEM_IS_DEFINED(map, batch->primary_batch_size));
467 }
468
469
470 static void
471 add_aux_map_bos_to_batch(struct iris_batch *batch)
472 {
473 void *aux_map_ctx = iris_bufmgr_get_aux_map_context(batch->screen->bufmgr);
474 if (!aux_map_ctx)
475 return;
476
477 uint32_t count = gen_aux_map_get_num_buffers(aux_map_ctx);
478 ensure_exec_obj_space(batch, count);
479 gen_aux_map_fill_bos(aux_map_ctx,
480 (void**)&batch->exec_bos[batch->exec_count], count);
481 for (uint32_t i = 0; i < count; i++) {
482 struct iris_bo *bo = batch->exec_bos[batch->exec_count];
483 iris_bo_reference(bo);
484 batch->validation_list[batch->exec_count] =
485 (struct drm_i915_gem_exec_object2) {
486 .handle = bo->gem_handle,
487 .offset = bo->gtt_offset,
488 .flags = bo->kflags,
489 };
490 batch->aperture_space += bo->size;
491 batch->exec_count++;
492 }
493 }
494
495 /**
496 * Terminate a batch with MI_BATCH_BUFFER_END.
497 */
498 static void
499 iris_finish_batch(struct iris_batch *batch)
500 {
501 add_aux_map_bos_to_batch(batch);
502
503 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
504 uint32_t *map = batch->map_next;
505
506 map[0] = (0xA << 23);
507
508 batch->map_next += 4;
509 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->map, iris_batch_bytes_used(batch)));
510
511 if (batch->bo == batch->exec_bos[0])
512 batch->primary_batch_size = iris_batch_bytes_used(batch);
513 }
514
515 /**
516 * Replace our current GEM context with a new one (in case it got banned).
517 */
518 static bool
519 replace_hw_ctx(struct iris_batch *batch)
520 {
521 struct iris_screen *screen = batch->screen;
522 struct iris_bufmgr *bufmgr = screen->bufmgr;
523
524 uint32_t new_ctx = iris_clone_hw_context(bufmgr, batch->hw_ctx_id);
525 if (!new_ctx)
526 return false;
527
528 iris_destroy_hw_context(bufmgr, batch->hw_ctx_id);
529 batch->hw_ctx_id = new_ctx;
530
531 /* Notify the context that state must be re-initialized. */
532 iris_lost_context_state(batch);
533
534 return true;
535 }
536
537 enum pipe_reset_status
538 iris_batch_check_for_reset(struct iris_batch *batch)
539 {
540 struct iris_screen *screen = batch->screen;
541 enum pipe_reset_status status = PIPE_NO_RESET;
542 struct drm_i915_reset_stats stats = { .ctx_id = batch->hw_ctx_id };
543
544 if (drmIoctl(screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats))
545 DBG("DRM_IOCTL_I915_GET_RESET_STATS failed: %s\n", strerror(errno));
546
547 if (stats.batch_active != 0) {
548 /* A reset was observed while a batch from this hardware context was
549 * executing. Assume that this context was at fault.
550 */
551 status = PIPE_GUILTY_CONTEXT_RESET;
552 } else if (stats.batch_pending != 0) {
553 /* A reset was observed while a batch from this context was in progress,
554 * but the batch was not executing. In this case, assume that the
555 * context was not at fault.
556 */
557 status = PIPE_INNOCENT_CONTEXT_RESET;
558 }
559
560 if (status != PIPE_NO_RESET) {
561 /* Our context is likely banned, or at least in an unknown state.
562 * Throw it away and start with a fresh context. Ideally this may
563 * catch the problem before our next execbuf fails with -EIO.
564 */
565 replace_hw_ctx(batch);
566 }
567
568 return status;
569 }
570
571 /**
572 * Submit the batch to the GPU via execbuffer2.
573 */
574 static int
575 submit_batch(struct iris_batch *batch)
576 {
577 iris_bo_unmap(batch->bo);
578
579 /* The requirement for using I915_EXEC_NO_RELOC are:
580 *
581 * The addresses written in the objects must match the corresponding
582 * reloc.gtt_offset which in turn must match the corresponding
583 * execobject.offset.
584 *
585 * Any render targets written to in the batch must be flagged with
586 * EXEC_OBJECT_WRITE.
587 *
588 * To avoid stalling, execobject.offset should match the current
589 * address of that object within the active context.
590 */
591 struct drm_i915_gem_execbuffer2 execbuf = {
592 .buffers_ptr = (uintptr_t) batch->validation_list,
593 .buffer_count = batch->exec_count,
594 .batch_start_offset = 0,
595 /* This must be QWord aligned. */
596 .batch_len = ALIGN(batch->primary_batch_size, 8),
597 .flags = batch->engine |
598 I915_EXEC_NO_RELOC |
599 I915_EXEC_BATCH_FIRST |
600 I915_EXEC_HANDLE_LUT,
601 .rsvd1 = batch->hw_ctx_id, /* rsvd1 is actually the context ID */
602 };
603
604 if (num_fences(batch)) {
605 execbuf.flags |= I915_EXEC_FENCE_ARRAY;
606 execbuf.num_cliprects = num_fences(batch);
607 execbuf.cliprects_ptr =
608 (uintptr_t)util_dynarray_begin(&batch->exec_fences);
609 }
610
611 int ret = 0;
612 if (!batch->screen->no_hw &&
613 gen_ioctl(batch->screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf))
614 ret = -errno;
615
616 for (int i = 0; i < batch->exec_count; i++) {
617 struct iris_bo *bo = batch->exec_bos[i];
618
619 bo->idle = false;
620 bo->index = -1;
621
622 iris_bo_unreference(bo);
623 }
624
625 return ret;
626 }
627
628 static const char *
629 batch_name_to_string(enum iris_batch_name name)
630 {
631 const char *names[IRIS_BATCH_COUNT] = {
632 [IRIS_BATCH_RENDER] = "render",
633 [IRIS_BATCH_COMPUTE] = "compute",
634 };
635 return names[name];
636 }
637
638 /**
639 * Flush the batch buffer, submitting it to the GPU and resetting it so
640 * we're ready to emit the next batch.
641 *
642 * \param in_fence_fd is ignored if -1. Otherwise, this function takes
643 * ownership of the fd.
644 *
645 * \param out_fence_fd is ignored if NULL. Otherwise, the caller must
646 * take ownership of the returned fd.
647 */
648 void
649 _iris_batch_flush(struct iris_batch *batch, const char *file, int line)
650 {
651 struct iris_screen *screen = batch->screen;
652
653 if (iris_batch_bytes_used(batch) == 0)
654 return;
655
656 iris_finish_batch(batch);
657
658 if (unlikely(INTEL_DEBUG &
659 (DEBUG_BATCH | DEBUG_SUBMIT | DEBUG_PIPE_CONTROL))) {
660 int bytes_for_commands = iris_batch_bytes_used(batch);
661 int second_bytes = 0;
662 if (batch->bo != batch->exec_bos[0]) {
663 second_bytes = bytes_for_commands;
664 bytes_for_commands += batch->primary_batch_size;
665 }
666 fprintf(stderr, "%19s:%-3d: %s batch [%u] flush with %5d+%5db (%0.1f%%) "
667 "(cmds), %4d BOs (%0.1fMb aperture)\n",
668 file, line, batch_name_to_string(batch->name), batch->hw_ctx_id,
669 batch->primary_batch_size, second_bytes,
670 100.0f * bytes_for_commands / BATCH_SZ,
671 batch->exec_count,
672 (float) batch->aperture_space / (1024 * 1024));
673
674 if (INTEL_DEBUG & (DEBUG_BATCH | DEBUG_SUBMIT)) {
675 dump_fence_list(batch);
676 dump_validation_list(batch);
677 }
678
679 if (INTEL_DEBUG & DEBUG_BATCH) {
680 decode_batch(batch);
681 }
682 }
683
684 int ret = submit_batch(batch);
685
686 batch->exec_count = 0;
687 batch->aperture_space = 0;
688
689 struct iris_syncpt *syncpt =
690 ((struct iris_syncpt **) util_dynarray_begin(&batch->syncpts))[0];
691 iris_syncpt_reference(screen, &batch->last_syncpt, syncpt);
692
693 util_dynarray_foreach(&batch->syncpts, struct iris_syncpt *, s)
694 iris_syncpt_reference(screen, s, NULL);
695 util_dynarray_clear(&batch->syncpts);
696
697 util_dynarray_clear(&batch->exec_fences);
698
699 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
700 dbg_printf("waiting for idle\n");
701 iris_bo_wait_rendering(batch->bo); /* if execbuf failed; this is a nop */
702 }
703
704 /* Start a new batch buffer. */
705 iris_batch_reset(batch);
706
707 /* EIO means our context is banned. In this case, try and replace it
708 * with a new logical context, and inform iris_context that all state
709 * has been lost and needs to be re-initialized. If this succeeds,
710 * dubiously claim success...
711 */
712 if (ret == -EIO && replace_hw_ctx(batch)) {
713 if (batch->reset->reset) {
714 /* Tell the state tracker the device is lost and it was our fault. */
715 batch->reset->reset(batch->reset->data, PIPE_GUILTY_CONTEXT_RESET);
716 }
717
718 ret = 0;
719 }
720
721 if (ret < 0) {
722 #ifdef DEBUG
723 const bool color = INTEL_DEBUG & DEBUG_COLOR;
724 fprintf(stderr, "%siris: Failed to submit batchbuffer: %-80s%s\n",
725 color ? "\e[1;41m" : "", strerror(-ret), color ? "\e[0m" : "");
726 #endif
727 abort();
728 }
729 }
730
731 /**
732 * Does the current batch refer to the given BO?
733 *
734 * (In other words, is the BO in the current batch's validation list?)
735 */
736 bool
737 iris_batch_references(struct iris_batch *batch, struct iris_bo *bo)
738 {
739 return find_validation_entry(batch, bo) != NULL;
740 }