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