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 #include "iris_kmd_backend.h"
45 #include "iris_utrace.h"
46 #include "i915/iris_batch.h"
47 #include "xe/iris_batch.h"
48
49 #include "common/intel_aux_map.h"
50 #include "intel/common/intel_gem.h"
51 #include "intel/compiler/brw_compiler.h"
52 #include "intel/compiler/elk/elk_compiler.h"
53 #include "intel/ds/intel_tracepoints.h"
54 #include "util/hash_table.h"
55 #include "util/u_debug.h"
56 #include "util/set.h"
57 #include "util/u_upload_mgr.h"
58
59 #include <errno.h>
60 #include <xf86drm.h>
61
62 #ifdef HAVE_VALGRIND
63 #include <valgrind.h>
64 #include <memcheck.h>
65 #define VG(x) x
66 #else
67 #define VG(x)
68 #endif
69
70 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
71
72 static void
73 iris_batch_reset(struct iris_batch *batch);
74
75 unsigned
iris_batch_num_fences(struct iris_batch * batch)76 iris_batch_num_fences(struct iris_batch *batch)
77 {
78 return util_dynarray_num_elements(&batch->exec_fences,
79 struct iris_batch_fence);
80 }
81
82 /**
83 * Debugging code to dump the fence list, used by INTEL_DEBUG=submit.
84 */
85 void
iris_dump_fence_list(struct iris_batch * batch)86 iris_dump_fence_list(struct iris_batch *batch)
87 {
88 fprintf(stderr, "Fence list (length %u): ", iris_batch_num_fences(batch));
89
90 util_dynarray_foreach(&batch->exec_fences, struct iris_batch_fence, f) {
91 fprintf(stderr, "%s%u%s ",
92 (f->flags & IRIS_BATCH_FENCE_WAIT) ? "..." : "",
93 f->handle,
94 (f->flags & IRIS_BATCH_FENCE_SIGNAL) ? "!" : "");
95 }
96
97 fprintf(stderr, "\n");
98 }
99
100 /**
101 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
102 */
103 void
iris_dump_bo_list(struct iris_batch * batch)104 iris_dump_bo_list(struct iris_batch *batch)
105 {
106 fprintf(stderr, "BO list (length %d):\n", batch->exec_count);
107
108 for (int i = 0; i < batch->exec_count; i++) {
109 struct iris_bo *bo = batch->exec_bos[i];
110 struct iris_bo *backing = iris_get_backing_bo(bo);
111 bool written = BITSET_TEST(batch->bos_written, i);
112 bool exported = iris_bo_is_exported(bo);
113 bool imported = iris_bo_is_imported(bo);
114
115 fprintf(stderr, "[%2d]: %3d (%3d) %-14s @ 0x%016"PRIx64" (%-15s %8"PRIu64"B) %2d refs %s%s%s\n",
116 i,
117 bo->gem_handle,
118 backing->gem_handle,
119 bo->name,
120 bo->address,
121 iris_heap_to_string[backing->real.heap],
122 bo->size,
123 bo->refcount,
124 written ? " write" : "",
125 exported ? " exported" : "",
126 imported ? " imported" : "");
127 }
128 }
129
130 /**
131 * Return BO information to the batch decoder (for debugging).
132 */
133 static struct intel_batch_decode_bo
decode_get_bo(void * v_batch,bool ppgtt,uint64_t address)134 decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
135 {
136 struct iris_batch *batch = v_batch;
137
138 assert(ppgtt);
139
140 for (int i = 0; i < batch->exec_count; i++) {
141 struct iris_bo *bo = batch->exec_bos[i];
142 /* The decoder zeroes out the top 16 bits, so we need to as well */
143 uint64_t bo_address = bo->address & (~0ull >> 16);
144
145 if (address >= bo_address && address < bo_address + bo->size) {
146 if (bo->real.mmap_mode == IRIS_MMAP_NONE)
147 return (struct intel_batch_decode_bo) { };
148
149 return (struct intel_batch_decode_bo) {
150 .addr = bo_address,
151 .size = bo->size,
152 .map = iris_bo_map(batch->dbg, bo, MAP_READ | MAP_ASYNC),
153 };
154 }
155 }
156
157 return (struct intel_batch_decode_bo) { };
158 }
159
160 static unsigned
decode_get_state_size(void * v_batch,uint64_t address,UNUSED uint64_t base_address)161 decode_get_state_size(void *v_batch,
162 uint64_t address,
163 UNUSED uint64_t base_address)
164 {
165 struct iris_batch *batch = v_batch;
166 unsigned size = (uintptr_t)
167 _mesa_hash_table_u64_search(batch->state_sizes, address);
168
169 return size;
170 }
171
172 /**
173 * Decode the current batch.
174 */
175 void
iris_batch_decode_batch(struct iris_batch * batch)176 iris_batch_decode_batch(struct iris_batch *batch)
177 {
178 void *map = iris_bo_map(batch->dbg, batch->exec_bos[0], MAP_READ);
179 intel_print_batch(&batch->decoder, map, batch->primary_batch_size,
180 batch->exec_bos[0]->address, false);
181 }
182
183 static void
iris_init_batch(struct iris_context * ice,enum iris_batch_name name)184 iris_init_batch(struct iris_context *ice,
185 enum iris_batch_name name)
186 {
187 struct iris_batch *batch = &ice->batches[name];
188 struct iris_screen *screen = (void *) ice->ctx.screen;
189
190 /* Note: screen, ctx_id, exec_flags and has_engines_context fields are
191 * initialized at an earlier phase when contexts are created.
192 *
193 * See iris_init_batches(), which calls either iris_init_engines_context()
194 * or iris_init_non_engine_contexts().
195 */
196
197 batch->dbg = &ice->dbg;
198 batch->reset = &ice->reset;
199 batch->state_sizes = ice->state.sizes;
200 batch->name = name;
201 batch->ice = ice;
202 batch->screen = screen;
203 batch->contains_fence_signal = false;
204
205 batch->fine_fences.uploader =
206 u_upload_create(&ice->ctx, 4096, PIPE_BIND_CUSTOM,
207 PIPE_USAGE_STAGING, 0);
208 iris_fine_fence_init(batch);
209
210 util_dynarray_init(&batch->exec_fences, ralloc_context(NULL));
211 util_dynarray_init(&batch->syncobjs, ralloc_context(NULL));
212
213 batch->exec_count = 0;
214 batch->max_gem_handle = 0;
215 batch->exec_array_size = 128;
216 batch->exec_bos =
217 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
218 batch->bos_written =
219 rzalloc_array(NULL, BITSET_WORD, BITSET_WORDS(batch->exec_array_size));
220
221 batch->bo_aux_modes = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
222 _mesa_key_pointer_equal);
223
224 batch->num_other_batches = 0;
225 memset(batch->other_batches, 0, sizeof(batch->other_batches));
226
227 iris_foreach_batch(ice, other_batch) {
228 if (batch != other_batch)
229 batch->other_batches[batch->num_other_batches++] = other_batch;
230 }
231
232 if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_BATCH_STATS)) {
233 const unsigned decode_flags = INTEL_BATCH_DECODE_DEFAULT_FLAGS |
234 (INTEL_DEBUG(DEBUG_COLOR) ? INTEL_BATCH_DECODE_IN_COLOR : 0);
235
236 if (screen->brw) {
237 intel_batch_decode_ctx_init_brw(&batch->decoder, &screen->brw->isa,
238 screen->devinfo,
239 stderr, decode_flags, NULL,
240 decode_get_bo, decode_get_state_size, batch);
241 } else {
242 assert(screen->elk);
243 intel_batch_decode_ctx_init_elk(&batch->decoder, &screen->elk->isa,
244 screen->devinfo,
245 stderr, decode_flags, NULL,
246 decode_get_bo, decode_get_state_size, batch);
247 }
248 batch->decoder.dynamic_base = IRIS_MEMZONE_DYNAMIC_START;
249 batch->decoder.instruction_base = IRIS_MEMZONE_SHADER_START;
250 batch->decoder.surface_base = IRIS_MEMZONE_BINDER_START;
251 batch->decoder.max_vbo_decoded_lines = 32;
252 if (batch->name == IRIS_BATCH_BLITTER)
253 batch->decoder.engine = INTEL_ENGINE_CLASS_COPY;
254 }
255
256 iris_init_batch_measure(ice, batch);
257
258 u_trace_init(&batch->trace, &ice->ds.trace_context);
259
260 iris_batch_reset(batch);
261 }
262
263 void
iris_init_batches(struct iris_context * ice)264 iris_init_batches(struct iris_context *ice)
265 {
266 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
267 struct iris_bufmgr *bufmgr = screen->bufmgr;
268 const struct intel_device_info *devinfo = iris_bufmgr_get_device_info(bufmgr);
269
270 switch (devinfo->kmd_type) {
271 case INTEL_KMD_TYPE_I915:
272 iris_i915_init_batches(ice);
273 break;
274 case INTEL_KMD_TYPE_XE:
275 iris_xe_init_batches(ice);
276 break;
277 default:
278 unreachable("missing");
279 }
280
281 iris_foreach_batch(ice, batch)
282 iris_init_batch(ice, batch - &ice->batches[0]);
283 }
284
285 static int
find_exec_index(struct iris_batch * batch,struct iris_bo * bo)286 find_exec_index(struct iris_batch *batch, struct iris_bo *bo)
287 {
288 unsigned index = READ_ONCE(bo->index);
289
290 if (index == -1)
291 return -1;
292
293 if (index < batch->exec_count && batch->exec_bos[index] == bo)
294 return index;
295
296 /* May have been shared between multiple active batches */
297 for (index = 0; index < batch->exec_count; index++) {
298 if (batch->exec_bos[index] == bo)
299 return index;
300 }
301
302 return -1;
303 }
304
305 static void
ensure_exec_obj_space(struct iris_batch * batch,uint32_t count)306 ensure_exec_obj_space(struct iris_batch *batch, uint32_t count)
307 {
308 while (batch->exec_count + count > batch->exec_array_size) {
309 unsigned old_size = batch->exec_array_size;
310
311 batch->exec_array_size *= 2;
312 batch->exec_bos =
313 realloc(batch->exec_bos,
314 batch->exec_array_size * sizeof(batch->exec_bos[0]));
315 batch->bos_written =
316 rerzalloc(NULL, batch->bos_written, BITSET_WORD,
317 BITSET_WORDS(old_size),
318 BITSET_WORDS(batch->exec_array_size));
319 }
320 }
321
322 static void
add_bo_to_batch(struct iris_batch * batch,struct iris_bo * bo,bool writable)323 add_bo_to_batch(struct iris_batch *batch, struct iris_bo *bo, bool writable)
324 {
325 assert(batch->exec_array_size > batch->exec_count);
326
327 iris_bo_reference(bo);
328
329 batch->exec_bos[batch->exec_count] = bo;
330
331 if (writable)
332 BITSET_SET(batch->bos_written, batch->exec_count);
333
334 bo->index = batch->exec_count;
335 batch->exec_count++;
336 batch->aperture_space += bo->size;
337
338 batch->max_gem_handle =
339 MAX2(batch->max_gem_handle, iris_get_backing_bo(bo)->gem_handle);
340 }
341
342 static void
flush_for_cross_batch_dependencies(struct iris_batch * batch,struct iris_bo * bo,bool writable)343 flush_for_cross_batch_dependencies(struct iris_batch *batch,
344 struct iris_bo *bo,
345 bool writable)
346 {
347 if (batch->measure && bo == batch->measure->bo)
348 return;
349
350 /* When a batch uses a buffer for the first time, or newly writes a buffer
351 * it had already referenced, we may need to flush other batches in order
352 * to correctly synchronize them.
353 */
354 for (int b = 0; b < batch->num_other_batches; b++) {
355 struct iris_batch *other_batch = batch->other_batches[b];
356 int other_index = find_exec_index(other_batch, bo);
357
358 /* If the buffer is referenced by another batch, and either batch
359 * intends to write it, then flush the other batch and synchronize.
360 *
361 * Consider these cases:
362 *
363 * 1. They read, we read => No synchronization required.
364 * 2. They read, we write => Synchronize (they need the old value)
365 * 3. They write, we read => Synchronize (we need their new value)
366 * 4. They write, we write => Synchronize (order writes)
367 *
368 * The read/read case is very common, as multiple batches usually
369 * share a streaming state buffer or shader assembly buffer, and
370 * we want to avoid synchronizing in this case.
371 */
372 if (other_index != -1 &&
373 (writable || BITSET_TEST(other_batch->bos_written, other_index)))
374 iris_batch_flush(other_batch);
375 }
376 }
377
378 /**
379 * Add a buffer to the current batch's validation list.
380 *
381 * You must call this on any BO you wish to use in this batch, to ensure
382 * that it's resident when the GPU commands execute.
383 */
384 void
iris_use_pinned_bo(struct iris_batch * batch,struct iris_bo * bo,bool writable,enum iris_domain access)385 iris_use_pinned_bo(struct iris_batch *batch,
386 struct iris_bo *bo,
387 bool writable, enum iris_domain access)
388 {
389 assert(bo != batch->bo);
390
391 /* Never mark the workaround BO with EXEC_OBJECT_WRITE. We don't care
392 * about the order of any writes to that buffer, and marking it writable
393 * would introduce data dependencies between multiple batches which share
394 * the buffer. It is added directly to the batch using add_bo_to_batch()
395 * during batch reset time.
396 */
397 if (bo == batch->screen->workaround_bo)
398 return;
399
400 if (access < NUM_IRIS_DOMAINS) {
401 assert(batch->sync_region_depth);
402 iris_bo_bump_seqno(bo, batch->next_seqno, access);
403 }
404
405 int existing_index = find_exec_index(batch, bo);
406
407 if (existing_index == -1) {
408 flush_for_cross_batch_dependencies(batch, bo, writable);
409
410 ensure_exec_obj_space(batch, 1);
411 add_bo_to_batch(batch, bo, writable);
412 } else if (writable && !BITSET_TEST(batch->bos_written, existing_index)) {
413 flush_for_cross_batch_dependencies(batch, bo, writable);
414
415 /* The BO is already in the list; mark it writable */
416 BITSET_SET(batch->bos_written, existing_index);
417 }
418 }
419
420 static void
create_batch(struct iris_batch * batch)421 create_batch(struct iris_batch *batch)
422 {
423 struct iris_screen *screen = batch->screen;
424 struct iris_bufmgr *bufmgr = screen->bufmgr;
425
426 /* TODO: We probably could suballocate batches... */
427 batch->bo = iris_bo_alloc(bufmgr, "command buffer",
428 BATCH_SZ + BATCH_RESERVED, 8,
429 IRIS_MEMZONE_OTHER,
430 BO_ALLOC_NO_SUBALLOC | BO_ALLOC_CAPTURE);
431 batch->map = iris_bo_map(NULL, batch->bo, MAP_READ | MAP_WRITE);
432 batch->map_next = batch->map;
433
434 ensure_exec_obj_space(batch, 1);
435 add_bo_to_batch(batch, batch->bo, false);
436 }
437
438 static void
iris_batch_maybe_noop(struct iris_batch * batch)439 iris_batch_maybe_noop(struct iris_batch *batch)
440 {
441 /* We only insert the NOOP at the beginning of the batch. */
442 assert(iris_batch_bytes_used(batch) == 0);
443
444 if (batch->noop_enabled) {
445 /* Emit MI_BATCH_BUFFER_END to prevent any further command to be
446 * executed.
447 */
448 uint32_t *map = batch->map_next;
449
450 map[0] = (0xA << 23);
451
452 batch->map_next += 4;
453 }
454 }
455
456 static void
iris_batch_reset(struct iris_batch * batch)457 iris_batch_reset(struct iris_batch *batch)
458 {
459 struct iris_screen *screen = batch->screen;
460 struct iris_bufmgr *bufmgr = screen->bufmgr;
461 const struct intel_device_info *devinfo = screen->devinfo;
462
463 u_trace_fini(&batch->trace);
464
465 iris_bo_unreference(batch->bo);
466 batch->primary_batch_size = 0;
467 batch->total_chained_batch_size = 0;
468 batch->contains_draw = false;
469 batch->contains_fence_signal = false;
470 if (devinfo->ver < 11)
471 batch->decoder.surface_base = batch->last_binder_address;
472 else
473 batch->decoder.bt_pool_base = batch->last_binder_address;
474
475 create_batch(batch);
476 assert(batch->bo->index == 0);
477
478 memset(batch->bos_written, 0,
479 sizeof(BITSET_WORD) * BITSET_WORDS(batch->exec_array_size));
480
481 struct iris_syncobj *syncobj = iris_create_syncobj(bufmgr);
482 iris_batch_add_syncobj(batch, syncobj, IRIS_BATCH_FENCE_SIGNAL);
483 iris_syncobj_reference(bufmgr, &syncobj, NULL);
484
485 assert(!batch->sync_region_depth);
486 iris_batch_sync_boundary(batch);
487 iris_batch_mark_reset_sync(batch);
488
489 /* Always add the workaround BO, it contains a driver identifier at the
490 * beginning quite helpful to debug error states.
491 */
492 add_bo_to_batch(batch, screen->workaround_bo, false);
493
494 iris_batch_maybe_noop(batch);
495
496 u_trace_init(&batch->trace, &batch->ice->ds.trace_context);
497 batch->begin_trace_recorded = false;
498 }
499
500 static void
iris_batch_free(const struct iris_context * ice,struct iris_batch * batch)501 iris_batch_free(const struct iris_context *ice, struct iris_batch *batch)
502 {
503 struct iris_screen *screen = batch->screen;
504 struct iris_bufmgr *bufmgr = screen->bufmgr;
505 const struct intel_device_info *devinfo = iris_bufmgr_get_device_info(bufmgr);
506
507 for (int i = 0; i < batch->exec_count; i++) {
508 iris_bo_unreference(batch->exec_bos[i]);
509 }
510 free(batch->exec_bos);
511 ralloc_free(batch->bos_written);
512
513 ralloc_free(batch->exec_fences.mem_ctx);
514
515 pipe_resource_reference(&batch->fine_fences.ref.res, NULL);
516
517 util_dynarray_foreach(&batch->syncobjs, struct iris_syncobj *, s)
518 iris_syncobj_reference(bufmgr, s, NULL);
519 ralloc_free(batch->syncobjs.mem_ctx);
520
521 iris_fine_fence_reference(batch->screen, &batch->last_fence, NULL);
522 u_upload_destroy(batch->fine_fences.uploader);
523
524 iris_bo_unreference(batch->bo);
525 batch->bo = NULL;
526 batch->map = NULL;
527 batch->map_next = NULL;
528
529 switch (devinfo->kmd_type) {
530 case INTEL_KMD_TYPE_I915:
531 iris_i915_destroy_batch(batch);
532 break;
533 case INTEL_KMD_TYPE_XE:
534 iris_xe_destroy_batch(batch);
535 break;
536 default:
537 unreachable("missing");
538 }
539
540 iris_destroy_batch_measure(batch->measure);
541 batch->measure = NULL;
542
543 u_trace_fini(&batch->trace);
544
545 _mesa_hash_table_destroy(batch->bo_aux_modes, NULL);
546
547 if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_BATCH_STATS))
548 intel_batch_decode_ctx_finish(&batch->decoder);
549 }
550
551 void
iris_destroy_batches(struct iris_context * ice)552 iris_destroy_batches(struct iris_context *ice)
553 {
554 iris_foreach_batch(ice, batch)
555 iris_batch_free(ice, batch);
556 }
557
iris_batch_maybe_begin_frame(struct iris_batch * batch)558 void iris_batch_maybe_begin_frame(struct iris_batch *batch)
559 {
560 struct iris_context *ice = batch->ice;
561
562 if (ice->utrace.begin_frame != ice->frame) {
563 trace_intel_begin_frame(&batch->trace, batch);
564 ice->utrace.begin_frame = ice->utrace.end_frame = ice->frame;
565 }
566 }
567
568 /**
569 * If we've chained to a secondary batch, or are getting near to the end,
570 * then flush. This should only be called between draws.
571 */
572 void
iris_batch_maybe_flush(struct iris_batch * batch,unsigned estimate)573 iris_batch_maybe_flush(struct iris_batch *batch, unsigned estimate)
574 {
575 if (batch->bo != batch->exec_bos[0] ||
576 iris_batch_bytes_used(batch) + estimate >= BATCH_SZ) {
577 iris_batch_flush(batch);
578 }
579 }
580
581 static void
record_batch_sizes(struct iris_batch * batch)582 record_batch_sizes(struct iris_batch *batch)
583 {
584 unsigned batch_size = iris_batch_bytes_used(batch);
585
586 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->map, batch_size));
587
588 if (batch->bo == batch->exec_bos[0])
589 batch->primary_batch_size = batch_size;
590
591 batch->total_chained_batch_size += batch_size;
592 }
593
594 void
iris_chain_to_new_batch(struct iris_batch * batch)595 iris_chain_to_new_batch(struct iris_batch *batch)
596 {
597 uint32_t *cmd = batch->map_next;
598 uint64_t *addr = batch->map_next + 4;
599 batch->map_next += 12;
600
601 record_batch_sizes(batch);
602
603 /* No longer held by batch->bo, still held by validation list */
604 iris_bo_unreference(batch->bo);
605 create_batch(batch);
606
607 /* Emit MI_BATCH_BUFFER_START to chain to another batch. */
608 *cmd = (0x31 << 23) | (1 << 8) | (3 - 2);
609 *addr = batch->bo->address;
610 }
611
612 static void
add_aux_map_bos_to_batch(struct iris_batch * batch)613 add_aux_map_bos_to_batch(struct iris_batch *batch)
614 {
615 void *aux_map_ctx = iris_bufmgr_get_aux_map_context(batch->screen->bufmgr);
616 if (!aux_map_ctx)
617 return;
618
619 uint32_t count = intel_aux_map_get_num_buffers(aux_map_ctx);
620 ensure_exec_obj_space(batch, count);
621 intel_aux_map_fill_bos(aux_map_ctx,
622 (void**)&batch->exec_bos[batch->exec_count], count);
623 for (uint32_t i = 0; i < count; i++) {
624 struct iris_bo *bo = batch->exec_bos[batch->exec_count];
625 add_bo_to_batch(batch, bo, false);
626 }
627 }
628
629 static void
finish_seqno(struct iris_batch * batch)630 finish_seqno(struct iris_batch *batch)
631 {
632 struct iris_fine_fence *sq = iris_fine_fence_new(batch);
633 if (!sq)
634 return;
635
636 iris_fine_fence_reference(batch->screen, &batch->last_fence, sq);
637 iris_fine_fence_reference(batch->screen, &sq, NULL);
638 }
639
640 /**
641 * Terminate a batch with MI_BATCH_BUFFER_END.
642 */
643 static void
iris_finish_batch(struct iris_batch * batch)644 iris_finish_batch(struct iris_batch *batch)
645 {
646 const struct intel_device_info *devinfo = batch->screen->devinfo;
647
648 if (devinfo->ver == 12 && batch->name == IRIS_BATCH_RENDER) {
649 /* We re-emit constants at the beginning of every batch as a hardware
650 * bug workaround, so invalidate indirect state pointers in order to
651 * save ourselves the overhead of restoring constants redundantly when
652 * the next render batch is executed.
653 */
654 iris_emit_pipe_control_flush(batch, "ISP invalidate at batch end",
655 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE |
656 PIPE_CONTROL_STALL_AT_SCOREBOARD |
657 PIPE_CONTROL_CS_STALL);
658 }
659
660 add_aux_map_bos_to_batch(batch);
661
662 finish_seqno(batch);
663
664 trace_intel_end_batch(&batch->trace, batch->name);
665
666 struct iris_context *ice = batch->ice;
667 if (ice->utrace.end_frame != ice->frame) {
668 trace_intel_end_frame(&batch->trace, batch, ice->utrace.end_frame);
669 ice->utrace.end_frame = ice->frame;
670 }
671
672 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
673 uint32_t *map = batch->map_next;
674
675 map[0] = (0xA << 23);
676
677 batch->map_next += 4;
678
679 record_batch_sizes(batch);
680 }
681
682 /**
683 * Replace our current GEM context with a new one (in case it got banned).
684 */
685 static bool
replace_kernel_ctx(struct iris_batch * batch)686 replace_kernel_ctx(struct iris_batch *batch)
687 {
688 struct iris_screen *screen = batch->screen;
689 struct iris_bufmgr *bufmgr = screen->bufmgr;
690 const struct intel_device_info *devinfo = iris_bufmgr_get_device_info(bufmgr);
691
692 threaded_context_unwrap_sync(&batch->ice->ctx);
693
694 switch (devinfo->kmd_type) {
695 case INTEL_KMD_TYPE_I915:
696 return iris_i915_replace_batch(batch);
697 case INTEL_KMD_TYPE_XE:
698 return iris_xe_replace_batch(batch);
699 default:
700 unreachable("missing");
701 return false;
702 }
703 }
704
705 enum pipe_reset_status
iris_batch_check_for_reset(struct iris_batch * batch)706 iris_batch_check_for_reset(struct iris_batch *batch)
707 {
708 struct iris_screen *screen = batch->screen;
709 struct iris_bufmgr *bufmgr = screen->bufmgr;
710 struct iris_context *ice = batch->ice;
711 const struct iris_kmd_backend *backend;
712 enum pipe_reset_status status = PIPE_NO_RESET;
713
714 /* Banned context was already signalled to application */
715 if (ice->context_reset_signaled)
716 return status;
717
718 backend = iris_bufmgr_get_kernel_driver_backend(bufmgr);
719 status = backend->batch_check_for_reset(batch);
720
721 if (status != PIPE_NO_RESET)
722 ice->context_reset_signaled = true;
723
724 return status;
725 }
726
727 static void
move_syncobj_to_batch(struct iris_batch * batch,struct iris_syncobj ** p_syncobj,uint32_t flags)728 move_syncobj_to_batch(struct iris_batch *batch,
729 struct iris_syncobj **p_syncobj,
730 uint32_t flags)
731 {
732 struct iris_bufmgr *bufmgr = batch->screen->bufmgr;
733
734 if (!*p_syncobj)
735 return;
736
737 bool found = false;
738 util_dynarray_foreach(&batch->syncobjs, struct iris_syncobj *, s) {
739 if (*p_syncobj == *s) {
740 found = true;
741 break;
742 }
743 }
744
745 if (!found)
746 iris_batch_add_syncobj(batch, *p_syncobj, flags);
747
748 iris_syncobj_reference(bufmgr, p_syncobj, NULL);
749 }
750
751 static void
update_bo_syncobjs(struct iris_batch * batch,struct iris_bo * bo,bool write)752 update_bo_syncobjs(struct iris_batch *batch, struct iris_bo *bo, bool write)
753 {
754 struct iris_screen *screen = batch->screen;
755 struct iris_bufmgr *bufmgr = screen->bufmgr;
756 struct iris_context *ice = batch->ice;
757
758 simple_mtx_assert_locked(iris_bufmgr_get_bo_deps_lock(bufmgr));
759
760 /* Make sure bo->deps is big enough */
761 if (screen->id >= bo->deps_size) {
762 int new_size = screen->id + 1;
763 bo->deps = realloc(bo->deps, new_size * sizeof(bo->deps[0]));
764 assert(bo->deps);
765 memset(&bo->deps[bo->deps_size], 0,
766 sizeof(bo->deps[0]) * (new_size - bo->deps_size));
767
768 bo->deps_size = new_size;
769 }
770
771 /* When it comes to execbuf submission of non-shared buffers, we only need
772 * to care about the reads and writes done by the other batches of our own
773 * screen, and we also don't care about the reads and writes done by our
774 * own batch, although we need to track them. Just note that other places of
775 * our code may need to care about all the operations done by every batch
776 * on every screen.
777 */
778 struct iris_bo_screen_deps *bo_deps = &bo->deps[screen->id];
779 int batch_idx = batch->name;
780
781 /* Make our batch depend on additional syncobjs depending on what other
782 * batches have been doing to this bo.
783 *
784 * We also look at the dependencies set by our own batch since those could
785 * have come from a different context, and apps don't like it when we don't
786 * do inter-context tracking.
787 */
788 iris_foreach_batch(ice, batch_i) {
789 unsigned i = batch_i->name;
790
791 /* If the bo is being written to by others, wait for them. */
792 if (bo_deps->write_syncobjs[i])
793 move_syncobj_to_batch(batch, &bo_deps->write_syncobjs[i],
794 IRIS_BATCH_FENCE_WAIT);
795
796 /* If we're writing to the bo, wait on the reads from other batches. */
797 if (write)
798 move_syncobj_to_batch(batch, &bo_deps->read_syncobjs[i],
799 IRIS_BATCH_FENCE_WAIT);
800 }
801
802 struct iris_syncobj *batch_syncobj =
803 iris_batch_get_signal_syncobj(batch);
804
805 /* Update bo_deps depending on what we're doing with the bo in this batch
806 * by putting the batch's syncobj in the bo_deps lists accordingly. Only
807 * keep track of the last time we wrote to or read the BO.
808 */
809 if (write) {
810 iris_syncobj_reference(bufmgr, &bo_deps->write_syncobjs[batch_idx],
811 batch_syncobj);
812 } else {
813 iris_syncobj_reference(bufmgr, &bo_deps->read_syncobjs[batch_idx],
814 batch_syncobj);
815 }
816 }
817
818 void
iris_batch_update_syncobjs(struct iris_batch * batch)819 iris_batch_update_syncobjs(struct iris_batch *batch)
820 {
821 for (int i = 0; i < batch->exec_count; i++) {
822 struct iris_bo *bo = batch->exec_bos[i];
823 bool write = BITSET_TEST(batch->bos_written, i);
824
825 if (bo == batch->screen->workaround_bo)
826 continue;
827
828 update_bo_syncobjs(batch, bo, write);
829 }
830 }
831
832 /**
833 * Convert the syncobj which will be signaled when this batch completes
834 * to a SYNC_FILE object, for use with import/export sync ioctls.
835 */
836 bool
iris_batch_syncobj_to_sync_file_fd(struct iris_batch * batch,int * out_fd)837 iris_batch_syncobj_to_sync_file_fd(struct iris_batch *batch, int *out_fd)
838 {
839 int drm_fd = batch->screen->fd;
840
841 struct iris_syncobj *batch_syncobj =
842 iris_batch_get_signal_syncobj(batch);
843
844 struct drm_syncobj_handle syncobj_to_fd_ioctl = {
845 .handle = batch_syncobj->handle,
846 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE,
847 .fd = -1,
848 };
849 if (intel_ioctl(drm_fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD,
850 &syncobj_to_fd_ioctl)) {
851 fprintf(stderr, "DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD ioctl failed (%d)\n",
852 errno);
853 return false;
854 }
855
856 assert(syncobj_to_fd_ioctl.fd >= 0);
857 *out_fd = syncobj_to_fd_ioctl.fd;
858
859 return true;
860 }
861
862 const char *
iris_batch_name_to_string(enum iris_batch_name name)863 iris_batch_name_to_string(enum iris_batch_name name)
864 {
865 const char *names[IRIS_BATCH_COUNT] = {
866 [IRIS_BATCH_RENDER] = "render",
867 [IRIS_BATCH_COMPUTE] = "compute",
868 [IRIS_BATCH_BLITTER] = "blitter",
869 };
870 return names[name];
871 }
872
873 bool
iris_batch_is_banned(struct iris_bufmgr * bufmgr,int ret)874 iris_batch_is_banned(struct iris_bufmgr *bufmgr, int ret)
875 {
876 enum intel_kmd_type kmd_type = iris_bufmgr_get_device_info(bufmgr)->kmd_type;
877
878 assert(ret < 0);
879 /* In i915 EIO means our context is banned, while on Xe ECANCELED means
880 * our exec queue was banned
881 */
882 if ((kmd_type == INTEL_KMD_TYPE_I915 && ret == -EIO) ||
883 (kmd_type == INTEL_KMD_TYPE_XE && ret == -ECANCELED))
884 return true;
885
886 return false;
887 }
888
889 /**
890 * Flush the batch buffer, submitting it to the GPU and resetting it so
891 * we're ready to emit the next batch.
892 */
893 void
_iris_batch_flush(struct iris_batch * batch,const char * file,int line)894 _iris_batch_flush(struct iris_batch *batch, const char *file, int line)
895 {
896 struct iris_screen *screen = batch->screen;
897 struct iris_context *ice = batch->ice;
898 struct iris_bufmgr *bufmgr = screen->bufmgr;
899
900 /* If a fence signals we need to flush it. */
901 if (iris_batch_bytes_used(batch) == 0 && !batch->contains_fence_signal)
902 return;
903
904 iris_measure_batch_end(ice, batch);
905
906 iris_finish_batch(batch);
907
908 if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_SUBMIT | DEBUG_PIPE_CONTROL)) {
909 const char *basefile = strstr(file, "iris/");
910 if (basefile)
911 file = basefile + 5;
912
913 enum intel_kmd_type kmd_type = iris_bufmgr_get_device_info(bufmgr)->kmd_type;
914 uint32_t batch_ctx_id = kmd_type == INTEL_KMD_TYPE_I915 ?
915 batch->i915.ctx_id : batch->xe.exec_queue_id;
916 fprintf(stderr, "%19s:%-3d: %s batch [%u] flush with %5db (%0.1f%%) "
917 "(cmds), %4d BOs (%0.1fMb aperture)\n",
918 file, line, iris_batch_name_to_string(batch->name),
919 batch_ctx_id, batch->total_chained_batch_size,
920 100.0f * batch->total_chained_batch_size / BATCH_SZ,
921 batch->exec_count,
922 (float) batch->aperture_space / (1024 * 1024));
923
924 }
925
926 uint64_t start_ts = intel_ds_begin_submit(&batch->ds);
927 uint64_t submission_id = batch->ds.submission_id;
928 int ret = iris_bufmgr_get_kernel_driver_backend(bufmgr)->batch_submit(batch);
929 intel_ds_end_submit(&batch->ds, start_ts);
930
931 /* When batch submission fails, our end-of-batch syncobj remains
932 * unsignalled, and in fact is not even considered submitted.
933 *
934 * In the hang recovery case (-EIO) or -ENOMEM, we recreate our context and
935 * attempt to carry on. In that case, we need to signal our syncobj,
936 * dubiously claiming that this batch completed, because future batches may
937 * depend on it. If we don't, then execbuf would fail with -EINVAL for
938 * those batches, because they depend on a syncobj that's considered to be
939 * "never submitted". This would lead to an abort(). So here, we signal
940 * the failing batch's syncobj to try and allow further progress to be
941 * made, knowing we may have broken our dependency tracking.
942 */
943 if (ret < 0)
944 iris_syncobj_signal(screen->bufmgr, iris_batch_get_signal_syncobj(batch));
945
946 batch->exec_count = 0;
947 batch->max_gem_handle = 0;
948 batch->aperture_space = 0;
949
950 util_dynarray_foreach(&batch->syncobjs, struct iris_syncobj *, s)
951 iris_syncobj_reference(screen->bufmgr, s, NULL);
952 util_dynarray_clear(&batch->syncobjs);
953
954 util_dynarray_clear(&batch->exec_fences);
955
956 if (INTEL_DEBUG(DEBUG_SYNC)) {
957 dbg_printf("waiting for idle\n");
958 iris_bo_wait_rendering(batch->bo); /* if execbuf failed; this is a nop */
959 }
960
961 if (u_trace_should_process(&ice->ds.trace_context))
962 iris_utrace_flush(batch, submission_id);
963
964 /* Start a new batch buffer. */
965 iris_batch_reset(batch);
966
967 /* Check if context or engine was banned, if yes try to replace it
968 * with a new logical context, and inform iris_context that all state
969 * has been lost and needs to be re-initialized. If this succeeds,
970 * dubiously claim success...
971 */
972 if (ret && iris_batch_is_banned(bufmgr, ret)) {
973 enum pipe_reset_status status = iris_batch_check_for_reset(batch);
974
975 if (status != PIPE_NO_RESET || ice->context_reset_signaled)
976 replace_kernel_ctx(batch);
977
978 if (batch->reset->reset) {
979 /* Tell gallium frontends the device is lost and it was our fault. */
980 batch->reset->reset(batch->reset->data, status);
981 }
982
983 ret = 0;
984 }
985
986 if (ret < 0) {
987 #ifdef DEBUG
988 const bool color = INTEL_DEBUG(DEBUG_COLOR);
989 fprintf(stderr, "%siris: Failed to submit batchbuffer: %-80s%s\n",
990 color ? "\e[1;41m" : "", strerror(-ret), color ? "\e[0m" : "");
991 #endif
992 abort();
993 }
994 }
995
996 /**
997 * Does the current batch refer to the given BO?
998 *
999 * (In other words, is the BO in the current batch's validation list?)
1000 */
1001 bool
iris_batch_references(struct iris_batch * batch,struct iris_bo * bo)1002 iris_batch_references(struct iris_batch *batch, struct iris_bo *bo)
1003 {
1004 return find_exec_index(batch, bo) != -1;
1005 }
1006
1007 /**
1008 * Updates the state of the noop feature. Returns true if there was a noop
1009 * transition that led to state invalidation.
1010 */
1011 bool
iris_batch_prepare_noop(struct iris_batch * batch,bool noop_enable)1012 iris_batch_prepare_noop(struct iris_batch *batch, bool noop_enable)
1013 {
1014 if (batch->noop_enabled == noop_enable)
1015 return 0;
1016
1017 batch->noop_enabled = noop_enable;
1018
1019 iris_batch_flush(batch);
1020
1021 /* If the batch was empty, flush had no effect, so insert our noop. */
1022 if (iris_batch_bytes_used(batch) == 0)
1023 iris_batch_maybe_noop(batch);
1024
1025 /* We only need to update the entire state if we transition from noop ->
1026 * not-noop.
1027 */
1028 return !batch->noop_enabled;
1029 }
1030