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
add_pixel_hash_table_bo_to_batch(struct iris_batch * batch)630 add_pixel_hash_table_bo_to_batch(struct iris_batch *batch)
631 {
632
633 if (batch->ice->state.pixel_hashing_tables &&
634 batch->name == IRIS_BATCH_RENDER) {
635 struct iris_bo *bo = iris_resource_bo(
636 batch->ice->state.pixel_hashing_tables);
637 ensure_exec_obj_space(batch, 1);
638 add_bo_to_batch(batch, bo, false);
639 }
640 }
641
642 static void
finish_seqno(struct iris_batch * batch)643 finish_seqno(struct iris_batch *batch)
644 {
645 struct iris_fine_fence *sq = iris_fine_fence_new(batch);
646 if (!sq)
647 return;
648
649 iris_fine_fence_reference(batch->screen, &batch->last_fence, sq);
650 iris_fine_fence_reference(batch->screen, &sq, NULL);
651 }
652
653 /**
654 * Terminate a batch with MI_BATCH_BUFFER_END.
655 */
656 static void
iris_finish_batch(struct iris_batch * batch)657 iris_finish_batch(struct iris_batch *batch)
658 {
659 const struct intel_device_info *devinfo = batch->screen->devinfo;
660
661 if (devinfo->ver == 12 && batch->name == IRIS_BATCH_RENDER) {
662 /* We re-emit constants at the beginning of every batch as a hardware
663 * bug workaround, so invalidate indirect state pointers in order to
664 * save ourselves the overhead of restoring constants redundantly when
665 * the next render batch is executed.
666 */
667 iris_emit_pipe_control_flush(batch, "ISP invalidate at batch end",
668 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE |
669 PIPE_CONTROL_STALL_AT_SCOREBOARD |
670 PIPE_CONTROL_CS_STALL);
671 }
672
673 add_aux_map_bos_to_batch(batch);
674 add_pixel_hash_table_bo_to_batch(batch);
675
676 finish_seqno(batch);
677
678 trace_intel_end_batch(&batch->trace, batch->name);
679
680 struct iris_context *ice = batch->ice;
681 if (ice->utrace.end_frame != ice->frame) {
682 trace_intel_end_frame(&batch->trace, batch, ice->utrace.end_frame);
683 ice->utrace.end_frame = ice->frame;
684 }
685
686 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
687 uint32_t *map = batch->map_next;
688
689 map[0] = (0xA << 23);
690
691 batch->map_next += 4;
692
693 record_batch_sizes(batch);
694 }
695
696 /**
697 * Replace our current GEM context with a new one (in case it got banned).
698 */
699 static bool
replace_kernel_ctx(struct iris_batch * batch)700 replace_kernel_ctx(struct iris_batch *batch)
701 {
702 struct iris_screen *screen = batch->screen;
703 struct iris_bufmgr *bufmgr = screen->bufmgr;
704 const struct intel_device_info *devinfo = iris_bufmgr_get_device_info(bufmgr);
705
706 threaded_context_unwrap_sync(&batch->ice->ctx);
707
708 switch (devinfo->kmd_type) {
709 case INTEL_KMD_TYPE_I915:
710 return iris_i915_replace_batch(batch);
711 case INTEL_KMD_TYPE_XE:
712 return iris_xe_replace_batch(batch);
713 default:
714 unreachable("missing");
715 return false;
716 }
717 }
718
719 enum pipe_reset_status
iris_batch_check_for_reset(struct iris_batch * batch)720 iris_batch_check_for_reset(struct iris_batch *batch)
721 {
722 struct iris_screen *screen = batch->screen;
723 struct iris_bufmgr *bufmgr = screen->bufmgr;
724 struct iris_context *ice = batch->ice;
725 const struct iris_kmd_backend *backend;
726 enum pipe_reset_status status = PIPE_NO_RESET;
727
728 /* Banned context was already signalled to application */
729 if (ice->context_reset_signaled)
730 return status;
731
732 backend = iris_bufmgr_get_kernel_driver_backend(bufmgr);
733 status = backend->batch_check_for_reset(batch);
734
735 if (status != PIPE_NO_RESET)
736 ice->context_reset_signaled = true;
737
738 return status;
739 }
740
741 static void
move_syncobj_to_batch(struct iris_batch * batch,struct iris_syncobj ** p_syncobj,uint32_t flags)742 move_syncobj_to_batch(struct iris_batch *batch,
743 struct iris_syncobj **p_syncobj,
744 uint32_t flags)
745 {
746 struct iris_bufmgr *bufmgr = batch->screen->bufmgr;
747
748 if (!*p_syncobj)
749 return;
750
751 bool found = false;
752 util_dynarray_foreach(&batch->syncobjs, struct iris_syncobj *, s) {
753 if (*p_syncobj == *s) {
754 found = true;
755 break;
756 }
757 }
758
759 if (!found)
760 iris_batch_add_syncobj(batch, *p_syncobj, flags);
761
762 iris_syncobj_reference(bufmgr, p_syncobj, NULL);
763 }
764
765 static void
update_bo_syncobjs(struct iris_batch * batch,struct iris_bo * bo,bool write)766 update_bo_syncobjs(struct iris_batch *batch, struct iris_bo *bo, bool write)
767 {
768 struct iris_screen *screen = batch->screen;
769 struct iris_bufmgr *bufmgr = screen->bufmgr;
770 struct iris_context *ice = batch->ice;
771
772 simple_mtx_assert_locked(iris_bufmgr_get_bo_deps_lock(bufmgr));
773
774 /* Make sure bo->deps is big enough */
775 if (screen->id >= bo->deps_size) {
776 int new_size = screen->id + 1;
777 bo->deps = realloc(bo->deps, new_size * sizeof(bo->deps[0]));
778 assert(bo->deps);
779 memset(&bo->deps[bo->deps_size], 0,
780 sizeof(bo->deps[0]) * (new_size - bo->deps_size));
781
782 bo->deps_size = new_size;
783 }
784
785 /* When it comes to execbuf submission of non-shared buffers, we only need
786 * to care about the reads and writes done by the other batches of our own
787 * screen, and we also don't care about the reads and writes done by our
788 * own batch, although we need to track them. Just note that other places of
789 * our code may need to care about all the operations done by every batch
790 * on every screen.
791 */
792 struct iris_bo_screen_deps *bo_deps = &bo->deps[screen->id];
793 int batch_idx = batch->name;
794
795 /* Make our batch depend on additional syncobjs depending on what other
796 * batches have been doing to this bo.
797 *
798 * We also look at the dependencies set by our own batch since those could
799 * have come from a different context, and apps don't like it when we don't
800 * do inter-context tracking.
801 */
802 iris_foreach_batch(ice, batch_i) {
803 unsigned i = batch_i->name;
804
805 /* If the bo is being written to by others, wait for them. */
806 if (bo_deps->write_syncobjs[i])
807 move_syncobj_to_batch(batch, &bo_deps->write_syncobjs[i],
808 IRIS_BATCH_FENCE_WAIT);
809
810 /* If we're writing to the bo, wait on the reads from other batches. */
811 if (write)
812 move_syncobj_to_batch(batch, &bo_deps->read_syncobjs[i],
813 IRIS_BATCH_FENCE_WAIT);
814 }
815
816 struct iris_syncobj *batch_syncobj =
817 iris_batch_get_signal_syncobj(batch);
818
819 /* Update bo_deps depending on what we're doing with the bo in this batch
820 * by putting the batch's syncobj in the bo_deps lists accordingly. Only
821 * keep track of the last time we wrote to or read the BO.
822 */
823 if (write) {
824 iris_syncobj_reference(bufmgr, &bo_deps->write_syncobjs[batch_idx],
825 batch_syncobj);
826 } else {
827 iris_syncobj_reference(bufmgr, &bo_deps->read_syncobjs[batch_idx],
828 batch_syncobj);
829 }
830 }
831
832 void
iris_batch_update_syncobjs(struct iris_batch * batch)833 iris_batch_update_syncobjs(struct iris_batch *batch)
834 {
835 for (int i = 0; i < batch->exec_count; i++) {
836 struct iris_bo *bo = batch->exec_bos[i];
837 bool write = BITSET_TEST(batch->bos_written, i);
838
839 if (bo == batch->screen->workaround_bo)
840 continue;
841
842 update_bo_syncobjs(batch, bo, write);
843 }
844 }
845
846 /**
847 * Convert the syncobj which will be signaled when this batch completes
848 * to a SYNC_FILE object, for use with import/export sync ioctls.
849 */
850 bool
iris_batch_syncobj_to_sync_file_fd(struct iris_batch * batch,int * out_fd)851 iris_batch_syncobj_to_sync_file_fd(struct iris_batch *batch, int *out_fd)
852 {
853 int drm_fd = batch->screen->fd;
854
855 struct iris_syncobj *batch_syncobj =
856 iris_batch_get_signal_syncobj(batch);
857
858 struct drm_syncobj_handle syncobj_to_fd_ioctl = {
859 .handle = batch_syncobj->handle,
860 .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE,
861 .fd = -1,
862 };
863 if (intel_ioctl(drm_fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD,
864 &syncobj_to_fd_ioctl)) {
865 fprintf(stderr, "DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD ioctl failed (%d)\n",
866 errno);
867 return false;
868 }
869
870 assert(syncobj_to_fd_ioctl.fd >= 0);
871 *out_fd = syncobj_to_fd_ioctl.fd;
872
873 return true;
874 }
875
876 const char *
iris_batch_name_to_string(enum iris_batch_name name)877 iris_batch_name_to_string(enum iris_batch_name name)
878 {
879 const char *names[IRIS_BATCH_COUNT] = {
880 [IRIS_BATCH_RENDER] = "render",
881 [IRIS_BATCH_COMPUTE] = "compute",
882 [IRIS_BATCH_BLITTER] = "blitter",
883 };
884 return names[name];
885 }
886
887 bool
iris_batch_is_banned(struct iris_bufmgr * bufmgr,int ret)888 iris_batch_is_banned(struct iris_bufmgr *bufmgr, int ret)
889 {
890 enum intel_kmd_type kmd_type = iris_bufmgr_get_device_info(bufmgr)->kmd_type;
891
892 assert(ret < 0);
893 /* In i915 EIO means our context is banned, while on Xe ECANCELED means
894 * our exec queue was banned
895 */
896 if ((kmd_type == INTEL_KMD_TYPE_I915 && ret == -EIO) ||
897 (kmd_type == INTEL_KMD_TYPE_XE && ret == -ECANCELED))
898 return true;
899
900 return false;
901 }
902
903 /**
904 * Flush the batch buffer, submitting it to the GPU and resetting it so
905 * we're ready to emit the next batch.
906 */
907 void
_iris_batch_flush(struct iris_batch * batch,const char * file,int line)908 _iris_batch_flush(struct iris_batch *batch, const char *file, int line)
909 {
910 struct iris_screen *screen = batch->screen;
911 struct iris_context *ice = batch->ice;
912 struct iris_bufmgr *bufmgr = screen->bufmgr;
913
914 /* If a fence signals we need to flush it. */
915 if (iris_batch_bytes_used(batch) == 0 && !batch->contains_fence_signal)
916 return;
917
918 iris_measure_batch_end(ice, batch);
919
920 iris_finish_batch(batch);
921
922 if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_SUBMIT | DEBUG_PIPE_CONTROL)) {
923 const char *basefile = strstr(file, "iris/");
924 if (basefile)
925 file = basefile + 5;
926
927 enum intel_kmd_type kmd_type = iris_bufmgr_get_device_info(bufmgr)->kmd_type;
928 uint32_t batch_ctx_id = kmd_type == INTEL_KMD_TYPE_I915 ?
929 batch->i915.ctx_id : batch->xe.exec_queue_id;
930 fprintf(stderr, "%19s:%-3d: %s batch [%u] flush with %5db (%0.1f%%) "
931 "(cmds), %4d BOs (%0.1fMb aperture)\n",
932 file, line, iris_batch_name_to_string(batch->name),
933 batch_ctx_id, batch->total_chained_batch_size,
934 100.0f * batch->total_chained_batch_size / BATCH_SZ,
935 batch->exec_count,
936 (float) batch->aperture_space / (1024 * 1024));
937
938 }
939
940 uint64_t start_ts = intel_ds_begin_submit(&batch->ds);
941 uint64_t submission_id = batch->ds.submission_id;
942 int ret = iris_bufmgr_get_kernel_driver_backend(bufmgr)->batch_submit(batch);
943 intel_ds_end_submit(&batch->ds, start_ts);
944
945 /* When batch submission fails, our end-of-batch syncobj remains
946 * unsignalled, and in fact is not even considered submitted.
947 *
948 * In the hang recovery case (-EIO) or -ENOMEM, we recreate our context and
949 * attempt to carry on. In that case, we need to signal our syncobj,
950 * dubiously claiming that this batch completed, because future batches may
951 * depend on it. If we don't, then execbuf would fail with -EINVAL for
952 * those batches, because they depend on a syncobj that's considered to be
953 * "never submitted". This would lead to an abort(). So here, we signal
954 * the failing batch's syncobj to try and allow further progress to be
955 * made, knowing we may have broken our dependency tracking.
956 */
957 if (ret < 0)
958 iris_syncobj_signal(screen->bufmgr, iris_batch_get_signal_syncobj(batch));
959
960 batch->exec_count = 0;
961 batch->max_gem_handle = 0;
962 batch->aperture_space = 0;
963
964 util_dynarray_foreach(&batch->syncobjs, struct iris_syncobj *, s)
965 iris_syncobj_reference(screen->bufmgr, s, NULL);
966 util_dynarray_clear(&batch->syncobjs);
967
968 util_dynarray_clear(&batch->exec_fences);
969
970 if (INTEL_DEBUG(DEBUG_SYNC)) {
971 dbg_printf("waiting for idle\n");
972 iris_bo_wait_rendering(batch->bo); /* if execbuf failed; this is a nop */
973 }
974
975 if (u_trace_should_process(&ice->ds.trace_context))
976 iris_utrace_flush(batch, submission_id);
977
978 /* Start a new batch buffer. */
979 iris_batch_reset(batch);
980
981 /* Check if context or engine was banned, if yes try to replace it
982 * with a new logical context, and inform iris_context that all state
983 * has been lost and needs to be re-initialized. If this succeeds,
984 * dubiously claim success...
985 */
986 if (ret && iris_batch_is_banned(bufmgr, ret)) {
987 enum pipe_reset_status status = iris_batch_check_for_reset(batch);
988
989 if (status != PIPE_NO_RESET || ice->context_reset_signaled)
990 replace_kernel_ctx(batch);
991
992 if (batch->reset->reset) {
993 /* Tell gallium frontends the device is lost and it was our fault. */
994 batch->reset->reset(batch->reset->data, status);
995 }
996
997 ret = 0;
998 }
999
1000 if (ret < 0) {
1001 #if MESA_DEBUG
1002 const bool color = INTEL_DEBUG(DEBUG_COLOR);
1003 fprintf(stderr, "%siris: Failed to submit batchbuffer: %-80s%s\n",
1004 color ? "\e[1;41m" : "", strerror(-ret), color ? "\e[0m" : "");
1005 #endif
1006 abort();
1007 }
1008 }
1009
1010 /**
1011 * Does the current batch refer to the given BO?
1012 *
1013 * (In other words, is the BO in the current batch's validation list?)
1014 */
1015 bool
iris_batch_references(struct iris_batch * batch,struct iris_bo * bo)1016 iris_batch_references(struct iris_batch *batch, struct iris_bo *bo)
1017 {
1018 return find_exec_index(batch, bo) != -1;
1019 }
1020
1021 /**
1022 * Updates the state of the noop feature. Returns true if there was a noop
1023 * transition that led to state invalidation.
1024 */
1025 bool
iris_batch_prepare_noop(struct iris_batch * batch,bool noop_enable)1026 iris_batch_prepare_noop(struct iris_batch *batch, bool noop_enable)
1027 {
1028 if (batch->noop_enabled == noop_enable)
1029 return 0;
1030
1031 batch->noop_enabled = noop_enable;
1032
1033 iris_batch_flush(batch);
1034
1035 /* If the batch was empty, flush had no effect, so insert our noop. */
1036 if (iris_batch_bytes_used(batch) == 0)
1037 iris_batch_maybe_noop(batch);
1038
1039 /* We only need to update the entire state if we transition from noop ->
1040 * not-noop.
1041 */
1042 return !batch->noop_enabled;
1043 }
1044