2
* Copyright © 2017 Intel Corporation
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:
11
* The above copyright notice and this permission notice shall be included
12
* in all copies or substantial portions of the Software.
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.
24
* @file crocus_batch.c
26
* Batchbuffer and command submission module.
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.
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.
40
#include "crocus_batch.h"
41
#include "crocus_bufmgr.h"
42
#include "crocus_context.h"
43
#include "crocus_fence.h"
45
#include "drm-uapi/i915_drm.h"
47
#include "intel/common/intel_gem.h"
48
#include "util/hash_table.h"
50
#include "util/u_upload_mgr.h"
63
#define FILE_DEBUG_FLAG DEBUG_BUFMGR
65
/* Terminating the batch takes either 4 bytes for MI_BATCH_BUFFER_END
66
* or 12 bytes for MI_BATCH_BUFFER_START (when chaining). Plus, we may
67
* need an extra 4 bytes to pad out to the nearest QWord. So reserve 16.
69
#define BATCH_RESERVED(devinfo) ((devinfo)->platform == INTEL_PLATFORM_HSW ? 32 : 16)
71
static void crocus_batch_reset(struct crocus_batch *batch);
74
num_fences(struct crocus_batch *batch)
76
return util_dynarray_num_elements(&batch->exec_fences,
77
struct drm_i915_gem_exec_fence);
81
* Debugging code to dump the fence list, used by INTEL_DEBUG=submit.
84
dump_fence_list(struct crocus_batch *batch)
86
fprintf(stderr, "Fence list (length %u): ", num_fences(batch));
88
util_dynarray_foreach(&batch->exec_fences,
89
struct drm_i915_gem_exec_fence, f) {
90
fprintf(stderr, "%s%u%s ",
91
(f->flags & I915_EXEC_FENCE_WAIT) ? "..." : "",
93
(f->flags & I915_EXEC_FENCE_SIGNAL) ? "!" : "");
96
fprintf(stderr, "\n");
100
* Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
103
dump_validation_list(struct crocus_batch *batch)
105
fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
107
for (int i = 0; i < batch->exec_count; i++) {
108
uint64_t flags = batch->validation_list[i].flags;
109
assert(batch->validation_list[i].handle ==
110
batch->exec_bos[i]->gem_handle);
112
"[%2d]: %2d %-14s @ 0x%"PRIx64" (%" PRIu64 "B)\t %2d refs %s\n", i,
113
batch->validation_list[i].handle, batch->exec_bos[i]->name,
114
(uint64_t)batch->validation_list[i].offset, batch->exec_bos[i]->size,
115
batch->exec_bos[i]->refcount,
116
(flags & EXEC_OBJECT_WRITE) ? " (write)" : "");
121
* Return BO information to the batch decoder (for debugging).
123
static struct intel_batch_decode_bo
124
decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
126
struct crocus_batch *batch = v_batch;
128
for (int i = 0; i < batch->exec_count; i++) {
129
struct crocus_bo *bo = batch->exec_bos[i];
130
/* The decoder zeroes out the top 16 bits, so we need to as well */
131
uint64_t bo_address = bo->gtt_offset & (~0ull >> 16);
133
if (address >= bo_address && address < bo_address + bo->size) {
134
return (struct intel_batch_decode_bo){
137
.map = crocus_bo_map(batch->dbg, bo, MAP_READ) +
138
(address - bo_address),
143
return (struct intel_batch_decode_bo) { };
147
decode_get_state_size(void *v_batch, uint64_t address,
148
uint64_t base_address)
150
struct crocus_batch *batch = v_batch;
152
/* The decoder gives us offsets from a base address, which is not great.
153
* Binding tables are relative to surface state base address, and other
154
* state is relative to dynamic state base address. These could alias,
155
* but in practice it's unlikely because surface offsets are always in
156
* the [0, 64K) range, and we assign dynamic state addresses starting at
157
* the top of the 4GB range. We should fix this but it's likely good
160
unsigned size = (uintptr_t)
161
_mesa_hash_table_u64_search(batch->state_sizes, address - base_address);
167
* Decode the current batch.
170
decode_batch(struct crocus_batch *batch)
172
void *map = crocus_bo_map(batch->dbg, batch->exec_bos[0], MAP_READ);
173
intel_print_batch(&batch->decoder, map, batch->primary_batch_size,
174
batch->exec_bos[0]->gtt_offset, false);
178
init_reloc_list(struct crocus_reloc_list *rlist, int count)
180
rlist->reloc_count = 0;
181
rlist->reloc_array_size = count;
182
rlist->relocs = malloc(rlist->reloc_array_size *
183
sizeof(struct drm_i915_gem_relocation_entry));
187
crocus_init_batch(struct crocus_context *ice,
188
enum crocus_batch_name name,
191
struct crocus_batch *batch = &ice->batches[name];
192
struct crocus_screen *screen = (struct crocus_screen *)ice->ctx.screen;
193
struct intel_device_info *devinfo = &screen->devinfo;
196
batch->screen = screen;
197
batch->dbg = &ice->dbg;
198
batch->reset = &ice->reset;
200
batch->contains_fence_signal = false;
202
if (devinfo->ver >= 7) {
203
batch->fine_fences.uploader =
204
u_upload_create(&ice->ctx, 4096, PIPE_BIND_CUSTOM,
205
PIPE_USAGE_STAGING, 0);
207
crocus_fine_fence_init(batch);
209
batch->hw_ctx_id = crocus_create_hw_context(screen->bufmgr);
210
assert(batch->hw_ctx_id);
212
crocus_hw_context_set_priority(screen->bufmgr, batch->hw_ctx_id, priority);
214
batch->valid_reloc_flags = EXEC_OBJECT_WRITE;
215
if (devinfo->ver == 6)
216
batch->valid_reloc_flags |= EXEC_OBJECT_NEEDS_GTT;
218
if (INTEL_DEBUG(DEBUG_BATCH)) {
219
/* The shadow doesn't get relocs written so state decode fails. */
220
batch->use_shadow_copy = false;
222
batch->use_shadow_copy = !devinfo->has_llc;
224
util_dynarray_init(&batch->exec_fences, ralloc_context(NULL));
225
util_dynarray_init(&batch->syncobjs, ralloc_context(NULL));
227
init_reloc_list(&batch->command.relocs, 250);
228
init_reloc_list(&batch->state.relocs, 250);
230
batch->exec_count = 0;
231
batch->exec_array_size = 100;
233
malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
234
batch->validation_list =
235
malloc(batch->exec_array_size * sizeof(batch->validation_list[0]));
237
batch->cache.render = _mesa_hash_table_create(NULL, NULL,
238
_mesa_key_pointer_equal);
239
batch->cache.depth = _mesa_set_create(NULL, NULL,
240
_mesa_key_pointer_equal);
242
memset(batch->other_batches, 0, sizeof(batch->other_batches));
244
for (int i = 0, j = 0; i < ice->batch_count; i++) {
246
batch->other_batches[j++] = &ice->batches[i];
249
if (INTEL_DEBUG(DEBUG_BATCH)) {
251
batch->state_sizes = _mesa_hash_table_u64_create(NULL);
252
const unsigned decode_flags =
253
INTEL_BATCH_DECODE_FULL |
254
(INTEL_DEBUG(DEBUG_COLOR) ? INTEL_BATCH_DECODE_IN_COLOR : 0) |
255
INTEL_BATCH_DECODE_OFFSETS | INTEL_BATCH_DECODE_FLOATS;
257
intel_batch_decode_ctx_init(&batch->decoder, &screen->devinfo, stderr,
258
decode_flags, NULL, decode_get_bo,
259
decode_get_state_size, batch);
260
batch->decoder.max_vbo_decoded_lines = 32;
263
crocus_batch_reset(batch);
267
find_exec_index(struct crocus_batch *batch, struct crocus_bo *bo)
269
unsigned index = READ_ONCE(bo->index);
271
if (index < batch->exec_count && batch->exec_bos[index] == bo)
274
/* May have been shared between multiple active batches */
275
for (index = 0; index < batch->exec_count; index++) {
276
if (batch->exec_bos[index] == bo)
282
static struct drm_i915_gem_exec_object2 *
283
find_validation_entry(struct crocus_batch *batch, struct crocus_bo *bo)
285
int index = find_exec_index(batch, bo);
289
return &batch->validation_list[index];
293
ensure_exec_obj_space(struct crocus_batch *batch, uint32_t count)
295
while (batch->exec_count + count > batch->exec_array_size) {
296
batch->exec_array_size *= 2;
297
batch->exec_bos = realloc(
298
batch->exec_bos, batch->exec_array_size * sizeof(batch->exec_bos[0]));
299
batch->validation_list =
300
realloc(batch->validation_list,
301
batch->exec_array_size * sizeof(batch->validation_list[0]));
305
static struct drm_i915_gem_exec_object2 *
306
crocus_use_bo(struct crocus_batch *batch, struct crocus_bo *bo, bool writable)
308
assert(bo->bufmgr == batch->command.bo->bufmgr);
310
struct drm_i915_gem_exec_object2 *existing_entry =
311
find_validation_entry(batch, bo);
313
if (existing_entry) {
314
/* The BO is already in the validation list; mark it writable */
316
existing_entry->flags |= EXEC_OBJECT_WRITE;
317
return existing_entry;
320
if (bo != batch->command.bo && bo != batch->state.bo) {
321
/* This is the first time our batch has seen this BO. Before we use it,
322
* we may need to flush and synchronize with other batches.
324
for (int b = 0; b < ARRAY_SIZE(batch->other_batches); b++) {
326
if (!batch->other_batches[b])
328
struct drm_i915_gem_exec_object2 *other_entry =
329
find_validation_entry(batch->other_batches[b], bo);
331
/* If the buffer is referenced by another batch, and either batch
332
* intends to write it, then flush the other batch and synchronize.
334
* Consider these cases:
336
* 1. They read, we read => No synchronization required.
337
* 2. They read, we write => Synchronize (they need the old value)
338
* 3. They write, we read => Synchronize (we need their new value)
339
* 4. They write, we write => Synchronize (order writes)
341
* The read/read case is very common, as multiple batches usually
342
* share a streaming state buffer or shader assembly buffer, and
343
* we want to avoid synchronizing in this case.
346
((other_entry->flags & EXEC_OBJECT_WRITE) || writable)) {
347
crocus_batch_flush(batch->other_batches[b]);
348
crocus_batch_add_syncobj(batch,
349
batch->other_batches[b]->last_fence->syncobj,
350
I915_EXEC_FENCE_WAIT);
355
/* Bump the ref count since the batch is now using this bo. */
356
crocus_bo_reference(bo);
358
ensure_exec_obj_space(batch, 1);
360
batch->validation_list[batch->exec_count] =
361
(struct drm_i915_gem_exec_object2) {
362
.handle = bo->gem_handle,
363
.offset = bo->gtt_offset,
364
.flags = bo->kflags | (writable ? EXEC_OBJECT_WRITE : 0),
367
bo->index = batch->exec_count;
368
batch->exec_bos[batch->exec_count] = bo;
369
batch->aperture_space += bo->size;
373
return &batch->validation_list[batch->exec_count - 1];
377
emit_reloc(struct crocus_batch *batch,
378
struct crocus_reloc_list *rlist, uint32_t offset,
379
struct crocus_bo *target, int32_t target_offset,
380
unsigned int reloc_flags)
382
assert(target != NULL);
384
if (target == batch->ice->workaround_bo)
385
reloc_flags &= ~RELOC_WRITE;
387
bool writable = reloc_flags & RELOC_WRITE;
389
struct drm_i915_gem_exec_object2 *entry =
390
crocus_use_bo(batch, target, writable);
392
if (rlist->reloc_count == rlist->reloc_array_size) {
393
rlist->reloc_array_size *= 2;
394
rlist->relocs = realloc(rlist->relocs,
395
rlist->reloc_array_size *
396
sizeof(struct drm_i915_gem_relocation_entry));
399
if (reloc_flags & RELOC_32BIT) {
400
/* Restrict this buffer to the low 32 bits of the address space.
402
* Altering the validation list flags restricts it for this batch,
403
* but we also alter the BO's kflags to restrict it permanently
404
* (until the BO is destroyed and put back in the cache). Buffers
405
* may stay bound across batches, and we want keep it constrained.
407
target->kflags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
408
entry->flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
410
/* RELOC_32BIT is not an EXEC_OBJECT_* flag, so get rid of it. */
411
reloc_flags &= ~RELOC_32BIT;
415
entry->flags |= reloc_flags & batch->valid_reloc_flags;
417
rlist->relocs[rlist->reloc_count++] =
418
(struct drm_i915_gem_relocation_entry) {
420
.delta = target_offset,
421
.target_handle = find_exec_index(batch, target),
422
.presumed_offset = entry->offset,
425
/* Using the old buffer offset, write in what the right data would be, in
426
* case the buffer doesn't move and we can short-circuit the relocation
427
* processing in the kernel
429
return entry->offset + target_offset;
433
crocus_command_reloc(struct crocus_batch *batch, uint32_t batch_offset,
434
struct crocus_bo *target, uint32_t target_offset,
435
unsigned int reloc_flags)
437
assert(batch_offset <= batch->command.bo->size - sizeof(uint32_t));
439
return emit_reloc(batch, &batch->command.relocs, batch_offset,
440
target, target_offset, reloc_flags);
444
crocus_state_reloc(struct crocus_batch *batch, uint32_t state_offset,
445
struct crocus_bo *target, uint32_t target_offset,
446
unsigned int reloc_flags)
448
assert(state_offset <= batch->state.bo->size - sizeof(uint32_t));
450
return emit_reloc(batch, &batch->state.relocs, state_offset,
451
target, target_offset, reloc_flags);
455
recreate_growing_buffer(struct crocus_batch *batch,
456
struct crocus_growing_bo *grow,
457
const char *name, unsigned size)
459
struct crocus_screen *screen = batch->screen;
460
struct crocus_bufmgr *bufmgr = screen->bufmgr;
461
grow->bo = crocus_bo_alloc(bufmgr, name, size);
462
grow->bo->kflags |= EXEC_OBJECT_CAPTURE;
463
grow->partial_bo = NULL;
464
grow->partial_bo_map = NULL;
465
grow->partial_bytes = 0;
466
if (batch->use_shadow_copy)
467
grow->map = realloc(grow->map, grow->bo->size);
469
grow->map = crocus_bo_map(NULL, grow->bo, MAP_READ | MAP_WRITE);
470
grow->map_next = grow->map;
474
create_batch(struct crocus_batch *batch)
476
struct crocus_screen *screen = batch->screen;
478
recreate_growing_buffer(batch, &batch->command,
480
BATCH_SZ + BATCH_RESERVED(&screen->devinfo));
482
crocus_use_bo(batch, batch->command.bo, false);
484
/* Always add workaround_bo which contains a driver identifier to be
485
* recorded in error states.
487
crocus_use_bo(batch, batch->ice->workaround_bo, false);
489
recreate_growing_buffer(batch, &batch->state,
493
batch->state.used = 1;
494
crocus_use_bo(batch, batch->state.bo, false);
498
crocus_batch_maybe_noop(struct crocus_batch *batch)
500
/* We only insert the NOOP at the beginning of the batch. */
501
assert(crocus_batch_bytes_used(batch) == 0);
503
if (batch->noop_enabled) {
504
/* Emit MI_BATCH_BUFFER_END to prevent any further command to be
507
uint32_t *map = batch->command.map_next;
509
map[0] = (0xA << 23);
511
batch->command.map_next += 4;
516
crocus_batch_reset(struct crocus_batch *batch)
518
struct crocus_screen *screen = batch->screen;
520
crocus_bo_unreference(batch->command.bo);
521
crocus_bo_unreference(batch->state.bo);
522
batch->primary_batch_size = 0;
523
batch->contains_draw = false;
524
batch->contains_fence_signal = false;
525
batch->state_base_address_emitted = false;
526
batch->screen->vtbl.batch_reset_dirty(batch);
529
assert(batch->command.bo->index == 0);
531
if (batch->state_sizes)
532
_mesa_hash_table_u64_clear(batch->state_sizes);
533
struct crocus_syncobj *syncobj = crocus_create_syncobj(screen);
534
crocus_batch_add_syncobj(batch, syncobj, I915_EXEC_FENCE_SIGNAL);
535
crocus_syncobj_reference(screen, &syncobj, NULL);
537
crocus_cache_sets_clear(batch);
541
crocus_batch_free(struct crocus_batch *batch)
543
struct crocus_screen *screen = batch->screen;
544
struct crocus_bufmgr *bufmgr = screen->bufmgr;
546
if (batch->use_shadow_copy) {
547
free(batch->command.map);
548
free(batch->state.map);
551
for (int i = 0; i < batch->exec_count; i++) {
552
crocus_bo_unreference(batch->exec_bos[i]);
555
pipe_resource_reference(&batch->fine_fences.ref.res, NULL);
557
free(batch->command.relocs.relocs);
558
free(batch->state.relocs.relocs);
559
free(batch->exec_bos);
560
free(batch->validation_list);
562
ralloc_free(batch->exec_fences.mem_ctx);
564
util_dynarray_foreach(&batch->syncobjs, struct crocus_syncobj *, s)
565
crocus_syncobj_reference(screen, s, NULL);
566
ralloc_free(batch->syncobjs.mem_ctx);
568
crocus_fine_fence_reference(batch->screen, &batch->last_fence, NULL);
569
if (batch_has_fine_fence(batch))
570
u_upload_destroy(batch->fine_fences.uploader);
572
crocus_bo_unreference(batch->command.bo);
573
crocus_bo_unreference(batch->state.bo);
574
batch->command.bo = NULL;
575
batch->command.map = NULL;
576
batch->command.map_next = NULL;
578
crocus_destroy_hw_context(bufmgr, batch->hw_ctx_id);
580
_mesa_hash_table_destroy(batch->cache.render, NULL);
581
_mesa_set_destroy(batch->cache.depth, NULL);
583
if (batch->state_sizes) {
584
_mesa_hash_table_u64_destroy(batch->state_sizes);
585
intel_batch_decode_ctx_finish(&batch->decoder);
590
* If we've chained to a secondary batch, or are getting near to the end,
591
* then flush. This should only be called between draws.
594
crocus_batch_maybe_flush(struct crocus_batch *batch, unsigned estimate)
596
if (batch->command.bo != batch->exec_bos[0] ||
597
crocus_batch_bytes_used(batch) + estimate >= BATCH_SZ) {
598
crocus_batch_flush(batch);
603
* Finish copying the old batch/state buffer's contents to the new one
604
* after we tried to "grow" the buffer in an earlier operation.
607
finish_growing_bos(struct crocus_growing_bo *grow)
609
struct crocus_bo *old_bo = grow->partial_bo;
613
memcpy(grow->map, grow->partial_bo_map, grow->partial_bytes);
615
grow->partial_bo = NULL;
616
grow->partial_bo_map = NULL;
617
grow->partial_bytes = 0;
619
crocus_bo_unreference(old_bo);
623
crocus_grow_buffer(struct crocus_batch *batch, bool grow_state,
627
struct crocus_screen *screen = batch->screen;
628
struct crocus_bufmgr *bufmgr = screen->bufmgr;
629
struct crocus_growing_bo *grow = grow_state ? &batch->state : &batch->command;
630
struct crocus_bo *bo = grow->bo;
632
if (grow->partial_bo) {
633
/* We've already grown once, and now we need to do it again.
634
* Finish our last grow operation so we can start a new one.
635
* This should basically never happen.
637
finish_growing_bos(grow);
640
struct crocus_bo *new_bo = crocus_bo_alloc(bufmgr, bo->name, new_size);
642
/* Copy existing data to the new larger buffer */
643
grow->partial_bo_map = grow->map;
645
if (batch->use_shadow_copy) {
646
/* We can't safely use realloc, as it may move the existing buffer,
647
* breaking existing pointers the caller may still be using. Just
648
* malloc a new copy and memcpy it like the normal BO path.
650
* Use bo->size rather than new_size because the bufmgr may have
651
* rounded up the size, and we want the shadow size to match.
653
grow->map = malloc(new_bo->size);
655
grow->map = crocus_bo_map(NULL, new_bo, MAP_READ | MAP_WRITE);
657
/* Try to put the new BO at the same GTT offset as the old BO (which
658
* we're throwing away, so it doesn't need to be there).
660
* This guarantees that our relocations continue to work: values we've
661
* already written into the buffer, values we're going to write into the
662
* buffer, and the validation/relocation lists all will match.
664
* Also preserve kflags for EXEC_OBJECT_CAPTURE.
666
new_bo->gtt_offset = bo->gtt_offset;
667
new_bo->index = bo->index;
668
new_bo->kflags = bo->kflags;
670
/* Batch/state buffers are per-context, and if we've run out of space,
671
* we must have actually used them before, so...they will be in the list.
673
assert(bo->index < batch->exec_count);
674
assert(batch->exec_bos[bo->index] == bo);
676
/* Update the validation list to use the new BO. */
677
batch->validation_list[bo->index].handle = new_bo->gem_handle;
678
/* Exchange the two BOs...without breaking pointers to the old BO.
680
* Consider this scenario:
682
* 1. Somebody calls brw_state_batch() to get a region of memory, and
683
* and then creates a brw_address pointing to brw->batch.state.bo.
684
* 2. They then call brw_state_batch() a second time, which happens to
685
* grow and replace the state buffer. They then try to emit a
686
* relocation to their first section of memory.
688
* If we replace the brw->batch.state.bo pointer at step 2, we would
689
* break the address created in step 1. They'd have a pointer to the
690
* old destroyed BO. Emitting a relocation would add this dead BO to
691
* the validation list...causing /both/ statebuffers to be in the list,
692
* and all kinds of disasters.
694
* This is not a contrived case - BLORP vertex data upload hits this.
696
* There are worse scenarios too. Fences for GL sync objects reference
697
* brw->batch.batch.bo. If we replaced the batch pointer when growing,
698
* we'd need to chase down every fence and update it to point to the
699
* new BO. Otherwise, it would refer to a "batch" that never actually
700
* gets submitted, and would fail to trigger.
702
* To work around both of these issues, we transmutate the buffers in
703
* place, making the existing struct brw_bo represent the new buffer,
704
* and "new_bo" represent the old BO. This is highly unusual, but it
705
* seems like a necessary evil.
707
* We also defer the memcpy of the existing batch's contents. Callers
708
* may make multiple brw_state_batch calls, and retain pointers to the
709
* old BO's map. We'll perform the memcpy in finish_growing_bo() when
710
* we finally submit the batch, at which point we've finished uploading
711
* state, and nobody should have any old references anymore.
713
* To do that, we keep a reference to the old BO in grow->partial_bo,
714
* and store the number of bytes to copy in grow->partial_bytes. We
715
* can monkey with the refcounts directly without atomics because these
716
* are per-context BOs and they can only be touched by this thread.
718
assert(new_bo->refcount == 1);
719
new_bo->refcount = bo->refcount;
722
struct crocus_bo tmp;
723
memcpy(&tmp, bo, sizeof(struct crocus_bo));
724
memcpy(bo, new_bo, sizeof(struct crocus_bo));
725
memcpy(new_bo, &tmp, sizeof(struct crocus_bo));
727
grow->partial_bo = new_bo; /* the one reference of the OLD bo */
728
grow->partial_bytes = used;
732
finish_seqno(struct crocus_batch *batch)
734
struct crocus_fine_fence *sq = crocus_fine_fence_new(batch, CROCUS_FENCE_END);
738
crocus_fine_fence_reference(batch->screen, &batch->last_fence, sq);
739
crocus_fine_fence_reference(batch->screen, &sq, NULL);
743
* Terminate a batch with MI_BATCH_BUFFER_END.
746
crocus_finish_batch(struct crocus_batch *batch)
749
batch->no_wrap = true;
750
if (batch->screen->vtbl.finish_batch)
751
batch->screen->vtbl.finish_batch(batch);
755
/* Emit MI_BATCH_BUFFER_END to finish our batch. */
756
uint32_t *map = batch->command.map_next;
758
map[0] = (0xA << 23);
760
batch->command.map_next += 4;
761
VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->command.map, crocus_batch_bytes_used(batch)));
763
if (batch->command.bo == batch->exec_bos[0])
764
batch->primary_batch_size = crocus_batch_bytes_used(batch);
765
batch->no_wrap = false;
769
* Replace our current GEM context with a new one (in case it got banned).
772
replace_hw_ctx(struct crocus_batch *batch)
774
struct crocus_screen *screen = batch->screen;
775
struct crocus_bufmgr *bufmgr = screen->bufmgr;
777
uint32_t new_ctx = crocus_clone_hw_context(bufmgr, batch->hw_ctx_id);
781
crocus_destroy_hw_context(bufmgr, batch->hw_ctx_id);
782
batch->hw_ctx_id = new_ctx;
784
/* Notify the context that state must be re-initialized. */
785
crocus_lost_context_state(batch);
790
enum pipe_reset_status
791
crocus_batch_check_for_reset(struct crocus_batch *batch)
793
struct crocus_screen *screen = batch->screen;
794
enum pipe_reset_status status = PIPE_NO_RESET;
795
struct drm_i915_reset_stats stats = { .ctx_id = batch->hw_ctx_id };
797
if (drmIoctl(screen->fd, DRM_IOCTL_I915_GET_RESET_STATS, &stats))
798
DBG("DRM_IOCTL_I915_GET_RESET_STATS failed: %s\n", strerror(errno));
800
if (stats.batch_active != 0) {
801
/* A reset was observed while a batch from this hardware context was
802
* executing. Assume that this context was at fault.
804
status = PIPE_GUILTY_CONTEXT_RESET;
805
} else if (stats.batch_pending != 0) {
806
/* A reset was observed while a batch from this context was in progress,
807
* but the batch was not executing. In this case, assume that the
808
* context was not at fault.
810
status = PIPE_INNOCENT_CONTEXT_RESET;
813
if (status != PIPE_NO_RESET) {
814
/* Our context is likely banned, or at least in an unknown state.
815
* Throw it away and start with a fresh context. Ideally this may
816
* catch the problem before our next execbuf fails with -EIO.
818
replace_hw_ctx(batch);
825
* Submit the batch to the GPU via execbuffer2.
828
submit_batch(struct crocus_batch *batch)
831
if (batch->use_shadow_copy) {
832
void *bo_map = crocus_bo_map(batch->dbg, batch->command.bo, MAP_WRITE);
833
memcpy(bo_map, batch->command.map, crocus_batch_bytes_used(batch));
835
bo_map = crocus_bo_map(batch->dbg, batch->state.bo, MAP_WRITE);
836
memcpy(bo_map, batch->state.map, batch->state.used);
839
crocus_bo_unmap(batch->command.bo);
840
crocus_bo_unmap(batch->state.bo);
842
/* The requirement for using I915_EXEC_NO_RELOC are:
844
* The addresses written in the objects must match the corresponding
845
* reloc.gtt_offset which in turn must match the corresponding
848
* Any render targets written to in the batch must be flagged with
851
* To avoid stalling, execobject.offset should match the current
852
* address of that object within the active context.
854
/* Set statebuffer relocations */
855
const unsigned state_index = batch->state.bo->index;
856
if (state_index < batch->exec_count &&
857
batch->exec_bos[state_index] == batch->state.bo) {
858
struct drm_i915_gem_exec_object2 *entry =
859
&batch->validation_list[state_index];
860
assert(entry->handle == batch->state.bo->gem_handle);
861
entry->relocation_count = batch->state.relocs.reloc_count;
862
entry->relocs_ptr = (uintptr_t)batch->state.relocs.relocs;
865
/* Set batchbuffer relocations */
866
struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[0];
867
assert(entry->handle == batch->command.bo->gem_handle);
868
entry->relocation_count = batch->command.relocs.reloc_count;
869
entry->relocs_ptr = (uintptr_t)batch->command.relocs.relocs;
871
struct drm_i915_gem_execbuffer2 execbuf = {
872
.buffers_ptr = (uintptr_t)batch->validation_list,
873
.buffer_count = batch->exec_count,
874
.batch_start_offset = 0,
875
/* This must be QWord aligned. */
876
.batch_len = ALIGN(batch->primary_batch_size, 8),
877
.flags = I915_EXEC_RENDER |
879
I915_EXEC_BATCH_FIRST |
880
I915_EXEC_HANDLE_LUT,
881
.rsvd1 = batch->hw_ctx_id, /* rsvd1 is actually the context ID */
884
if (num_fences(batch)) {
885
execbuf.flags |= I915_EXEC_FENCE_ARRAY;
886
execbuf.num_cliprects = num_fences(batch);
887
execbuf.cliprects_ptr =
888
(uintptr_t)util_dynarray_begin(&batch->exec_fences);
892
if (!batch->screen->devinfo.no_hw &&
893
intel_ioctl(batch->screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf))
896
for (int i = 0; i < batch->exec_count; i++) {
897
struct crocus_bo *bo = batch->exec_bos[i];
902
/* Update brw_bo::gtt_offset */
903
if (batch->validation_list[i].offset != bo->gtt_offset) {
904
DBG("BO %d migrated: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
905
bo->gem_handle, bo->gtt_offset,
906
(uint64_t)batch->validation_list[i].offset);
907
assert(!(bo->kflags & EXEC_OBJECT_PINNED));
908
bo->gtt_offset = batch->validation_list[i].offset;
916
batch_name_to_string(enum crocus_batch_name name)
918
const char *names[CROCUS_BATCH_COUNT] = {
919
[CROCUS_BATCH_RENDER] = "render",
920
[CROCUS_BATCH_COMPUTE] = "compute",
926
* Flush the batch buffer, submitting it to the GPU and resetting it so
927
* we're ready to emit the next batch.
929
* \param in_fence_fd is ignored if -1. Otherwise, this function takes
930
* ownership of the fd.
932
* \param out_fence_fd is ignored if NULL. Otherwise, the caller must
933
* take ownership of the returned fd.
936
_crocus_batch_flush(struct crocus_batch *batch, const char *file, int line)
938
struct crocus_screen *screen = batch->screen;
940
/* If a fence signals we need to flush it. */
941
if (crocus_batch_bytes_used(batch) == 0 && !batch->contains_fence_signal)
944
assert(!batch->no_wrap);
945
crocus_finish_batch(batch);
947
finish_growing_bos(&batch->command);
948
finish_growing_bos(&batch->state);
949
int ret = submit_batch(batch);
951
if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_SUBMIT | DEBUG_PIPE_CONTROL)) {
952
int bytes_for_commands = crocus_batch_bytes_used(batch);
953
int second_bytes = 0;
954
if (batch->command.bo != batch->exec_bos[0]) {
955
second_bytes = bytes_for_commands;
956
bytes_for_commands += batch->primary_batch_size;
958
fprintf(stderr, "%19s:%-3d: %s batch [%u] flush with %5d+%5db (%0.1f%%) "
959
"(cmds), %4d BOs (%0.1fMb aperture),"
960
" %4d command relocs, %4d state relocs\n",
961
file, line, batch_name_to_string(batch->name), batch->hw_ctx_id,
962
batch->primary_batch_size, second_bytes,
963
100.0f * bytes_for_commands / BATCH_SZ,
965
(float) batch->aperture_space / (1024 * 1024),
966
batch->command.relocs.reloc_count,
967
batch->state.relocs.reloc_count);
969
if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_SUBMIT)) {
970
dump_fence_list(batch);
971
dump_validation_list(batch);
974
if (INTEL_DEBUG(DEBUG_BATCH)) {
979
for (int i = 0; i < batch->exec_count; i++) {
980
struct crocus_bo *bo = batch->exec_bos[i];
981
crocus_bo_unreference(bo);
984
batch->command.relocs.reloc_count = 0;
985
batch->state.relocs.reloc_count = 0;
986
batch->exec_count = 0;
987
batch->aperture_space = 0;
989
util_dynarray_foreach(&batch->syncobjs, struct crocus_syncobj *, s)
990
crocus_syncobj_reference(screen, s, NULL);
991
util_dynarray_clear(&batch->syncobjs);
993
util_dynarray_clear(&batch->exec_fences);
995
if (INTEL_DEBUG(DEBUG_SYNC)) {
996
dbg_printf("waiting for idle\n");
997
crocus_bo_wait_rendering(batch->command.bo); /* if execbuf failed; this is a nop */
1000
/* Start a new batch buffer. */
1001
crocus_batch_reset(batch);
1003
/* EIO means our context is banned. In this case, try and replace it
1004
* with a new logical context, and inform crocus_context that all state
1005
* has been lost and needs to be re-initialized. If this succeeds,
1006
* dubiously claim success...
1008
if (ret == -EIO && replace_hw_ctx(batch)) {
1009
if (batch->reset->reset) {
1010
/* Tell the state tracker the device is lost and it was our fault. */
1011
batch->reset->reset(batch->reset->data, PIPE_GUILTY_CONTEXT_RESET);
1019
const bool color = INTEL_DEBUG(DEBUG_COLOR);
1020
fprintf(stderr, "%scrocus: Failed to submit batchbuffer: %-80s%s\n",
1021
color ? "\e[1;41m" : "", strerror(-ret), color ? "\e[0m" : "");
1028
* Does the current batch refer to the given BO?
1030
* (In other words, is the BO in the current batch's validation list?)
1033
crocus_batch_references(struct crocus_batch *batch, struct crocus_bo *bo)
1035
return find_validation_entry(batch, bo) != NULL;
1039
* Updates the state of the noop feature. Returns true if there was a noop
1040
* transition that led to state invalidation.
1043
crocus_batch_prepare_noop(struct crocus_batch *batch, bool noop_enable)
1045
if (batch->noop_enabled == noop_enable)
1048
batch->noop_enabled = noop_enable;
1050
crocus_batch_flush(batch);
1052
/* If the batch was empty, flush had no effect, so insert our noop. */
1053
if (crocus_batch_bytes_used(batch) == 0)
1054
crocus_batch_maybe_noop(batch);
1056
/* We only need to update the entire state if we transition from noop ->
1059
return !batch->noop_enabled;