~mmach/netext73/mesa-haswell

« back to all changes in this revision

Viewing changes to src/gallium/drivers/crocus/crocus_batch.c

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 crocus_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 "crocus_batch.h"
41
 
#include "crocus_bufmgr.h"
42
 
#include "crocus_context.h"
43
 
#include "crocus_fence.h"
44
 
 
45
 
#include "drm-uapi/i915_drm.h"
46
 
 
47
 
#include "intel/common/intel_gem.h"
48
 
#include "util/hash_table.h"
49
 
#include "util/set.h"
50
 
#include "util/u_upload_mgr.h"
51
 
 
52
 
#include <errno.h>
53
 
#include <xf86drm.h>
54
 
 
55
 
#if HAVE_VALGRIND
56
 
#include <memcheck.h>
57
 
#include <valgrind.h>
58
 
#define VG(x) x
59
 
#else
60
 
#define VG(x)
61
 
#endif
62
 
 
63
 
#define FILE_DEBUG_FLAG DEBUG_BUFMGR
64
 
 
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.
68
 
 */
69
 
#define BATCH_RESERVED(devinfo) ((devinfo)->platform == INTEL_PLATFORM_HSW ? 32 : 16)
70
 
 
71
 
static void crocus_batch_reset(struct crocus_batch *batch);
72
 
 
73
 
static unsigned
74
 
num_fences(struct crocus_batch *batch)
75
 
{
76
 
   return util_dynarray_num_elements(&batch->exec_fences,
77
 
                                     struct drm_i915_gem_exec_fence);
78
 
}
79
 
 
80
 
/**
81
 
 * Debugging code to dump the fence list, used by INTEL_DEBUG=submit.
82
 
 */
83
 
static void
84
 
dump_fence_list(struct crocus_batch *batch)
85
 
{
86
 
   fprintf(stderr, "Fence list (length %u):      ", num_fences(batch));
87
 
 
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) ? "..." : "",
92
 
              f->handle,
93
 
              (f->flags & I915_EXEC_FENCE_SIGNAL) ? "!" : "");
94
 
   }
95
 
 
96
 
   fprintf(stderr, "\n");
97
 
}
98
 
 
99
 
/**
100
 
 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
101
 
 */
102
 
static void
103
 
dump_validation_list(struct crocus_batch *batch)
104
 
{
105
 
   fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
106
 
 
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);
111
 
      fprintf(stderr,
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)" : "");
117
 
   }
118
 
}
119
 
 
120
 
/**
121
 
 * Return BO information to the batch decoder (for debugging).
122
 
 */
123
 
static struct intel_batch_decode_bo
124
 
decode_get_bo(void *v_batch, bool ppgtt, uint64_t address)
125
 
{
126
 
   struct crocus_batch *batch = v_batch;
127
 
 
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);
132
 
 
133
 
      if (address >= bo_address && address < bo_address + bo->size) {
134
 
         return (struct intel_batch_decode_bo){
135
 
            .addr = address,
136
 
            .size = bo->size,
137
 
            .map = crocus_bo_map(batch->dbg, bo, MAP_READ) +
138
 
                   (address - bo_address),
139
 
         };
140
 
      }
141
 
   }
142
 
 
143
 
   return (struct intel_batch_decode_bo) { };
144
 
}
145
 
 
146
 
static unsigned
147
 
decode_get_state_size(void *v_batch, uint64_t address,
148
 
                      uint64_t base_address)
149
 
{
150
 
   struct crocus_batch *batch = v_batch;
151
 
 
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
158
 
    * enough for now.
159
 
    */
160
 
   unsigned size = (uintptr_t)
161
 
      _mesa_hash_table_u64_search(batch->state_sizes, address - base_address);
162
 
 
163
 
   return size;
164
 
}
165
 
 
166
 
/**
167
 
 * Decode the current batch.
168
 
 */
169
 
static void
170
 
decode_batch(struct crocus_batch *batch)
171
 
{
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);
175
 
}
176
 
 
177
 
static void
178
 
init_reloc_list(struct crocus_reloc_list *rlist, int count)
179
 
{
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));
184
 
}
185
 
 
186
 
void
187
 
crocus_init_batch(struct crocus_context *ice,
188
 
                  enum crocus_batch_name name,
189
 
                  int priority)
190
 
{
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;
194
 
 
195
 
   batch->ice = ice;
196
 
   batch->screen = screen;
197
 
   batch->dbg = &ice->dbg;
198
 
   batch->reset = &ice->reset;
199
 
   batch->name = name;
200
 
   batch->contains_fence_signal = false;
201
 
 
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);
206
 
   }
207
 
   crocus_fine_fence_init(batch);
208
 
 
209
 
   batch->hw_ctx_id = crocus_create_hw_context(screen->bufmgr);
210
 
   assert(batch->hw_ctx_id);
211
 
 
212
 
   crocus_hw_context_set_priority(screen->bufmgr, batch->hw_ctx_id, priority);
213
 
 
214
 
   batch->valid_reloc_flags = EXEC_OBJECT_WRITE;
215
 
   if (devinfo->ver == 6)
216
 
      batch->valid_reloc_flags |= EXEC_OBJECT_NEEDS_GTT;
217
 
 
218
 
   if (INTEL_DEBUG(DEBUG_BATCH)) {
219
 
      /* The shadow doesn't get relocs written so state decode fails. */
220
 
      batch->use_shadow_copy = false;
221
 
   } else
222
 
      batch->use_shadow_copy = !devinfo->has_llc;
223
 
 
224
 
   util_dynarray_init(&batch->exec_fences, ralloc_context(NULL));
225
 
   util_dynarray_init(&batch->syncobjs, ralloc_context(NULL));
226
 
 
227
 
   init_reloc_list(&batch->command.relocs, 250);
228
 
   init_reloc_list(&batch->state.relocs, 250);
229
 
 
230
 
   batch->exec_count = 0;
231
 
   batch->exec_array_size = 100;
232
 
   batch->exec_bos =
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]));
236
 
 
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);
241
 
 
242
 
   memset(batch->other_batches, 0, sizeof(batch->other_batches));
243
 
 
244
 
   for (int i = 0, j = 0; i < ice->batch_count; i++) {
245
 
      if (i != name)
246
 
         batch->other_batches[j++] = &ice->batches[i];
247
 
   }
248
 
 
249
 
   if (INTEL_DEBUG(DEBUG_BATCH)) {
250
 
 
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;
256
 
 
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;
261
 
   }
262
 
 
263
 
   crocus_batch_reset(batch);
264
 
}
265
 
 
266
 
static int
267
 
find_exec_index(struct crocus_batch *batch, struct crocus_bo *bo)
268
 
{
269
 
   unsigned index = READ_ONCE(bo->index);
270
 
 
271
 
   if (index < batch->exec_count && batch->exec_bos[index] == bo)
272
 
      return index;
273
 
 
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)
277
 
         return index;
278
 
   }
279
 
   return -1;
280
 
}
281
 
 
282
 
static struct drm_i915_gem_exec_object2 *
283
 
find_validation_entry(struct crocus_batch *batch, struct crocus_bo *bo)
284
 
{
285
 
   int index = find_exec_index(batch, bo);
286
 
 
287
 
   if (index == -1)
288
 
      return NULL;
289
 
   return &batch->validation_list[index];
290
 
}
291
 
 
292
 
static void
293
 
ensure_exec_obj_space(struct crocus_batch *batch, uint32_t count)
294
 
{
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]));
302
 
   }
303
 
}
304
 
 
305
 
static struct drm_i915_gem_exec_object2 *
306
 
crocus_use_bo(struct crocus_batch *batch, struct crocus_bo *bo, bool writable)
307
 
{
308
 
   assert(bo->bufmgr == batch->command.bo->bufmgr);
309
 
 
310
 
   struct drm_i915_gem_exec_object2 *existing_entry =
311
 
      find_validation_entry(batch, bo);
312
 
 
313
 
   if (existing_entry) {
314
 
      /* The BO is already in the validation list; mark it writable */
315
 
      if (writable)
316
 
         existing_entry->flags |= EXEC_OBJECT_WRITE;
317
 
      return existing_entry;
318
 
   }
319
 
 
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.
323
 
       */
324
 
      for (int b = 0; b < ARRAY_SIZE(batch->other_batches); b++) {
325
 
 
326
 
         if (!batch->other_batches[b])
327
 
            continue;
328
 
         struct drm_i915_gem_exec_object2 *other_entry =
329
 
            find_validation_entry(batch->other_batches[b], bo);
330
 
 
331
 
         /* If the buffer is referenced by another batch, and either batch
332
 
          * intends to write it, then flush the other batch and synchronize.
333
 
          *
334
 
          * Consider these cases:
335
 
          *
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)
340
 
          *
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.
344
 
          */
345
 
         if (other_entry &&
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);
351
 
         }
352
 
      }
353
 
   }
354
 
 
355
 
   /* Bump the ref count since the batch is now using this bo. */
356
 
   crocus_bo_reference(bo);
357
 
 
358
 
   ensure_exec_obj_space(batch, 1);
359
 
 
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),
365
 
      };
366
 
 
367
 
   bo->index = batch->exec_count;
368
 
   batch->exec_bos[batch->exec_count] = bo;
369
 
   batch->aperture_space += bo->size;
370
 
 
371
 
   batch->exec_count++;
372
 
 
373
 
   return &batch->validation_list[batch->exec_count - 1];
374
 
}
375
 
 
376
 
static uint64_t
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)
381
 
{
382
 
   assert(target != NULL);
383
 
 
384
 
   if (target == batch->ice->workaround_bo)
385
 
      reloc_flags &= ~RELOC_WRITE;
386
 
 
387
 
   bool writable = reloc_flags & RELOC_WRITE;
388
 
 
389
 
   struct drm_i915_gem_exec_object2 *entry =
390
 
      crocus_use_bo(batch, target, writable);
391
 
 
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));
397
 
   }
398
 
 
399
 
   if (reloc_flags & RELOC_32BIT) {
400
 
      /* Restrict this buffer to the low 32 bits of the address space.
401
 
       *
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.
406
 
       */
407
 
      target->kflags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
408
 
      entry->flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
409
 
 
410
 
      /* RELOC_32BIT is not an EXEC_OBJECT_* flag, so get rid of it. */
411
 
      reloc_flags &= ~RELOC_32BIT;
412
 
   }
413
 
 
414
 
   if (reloc_flags)
415
 
      entry->flags |= reloc_flags & batch->valid_reloc_flags;
416
 
 
417
 
   rlist->relocs[rlist->reloc_count++] =
418
 
      (struct drm_i915_gem_relocation_entry) {
419
 
         .offset = offset,
420
 
         .delta = target_offset,
421
 
         .target_handle = find_exec_index(batch, target),
422
 
         .presumed_offset = entry->offset,
423
 
      };
424
 
 
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
428
 
    */
429
 
   return entry->offset + target_offset;
430
 
}
431
 
 
432
 
uint64_t
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)
436
 
{
437
 
   assert(batch_offset <= batch->command.bo->size - sizeof(uint32_t));
438
 
 
439
 
   return emit_reloc(batch, &batch->command.relocs, batch_offset,
440
 
                     target, target_offset, reloc_flags);
441
 
}
442
 
 
443
 
uint64_t
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)
447
 
{
448
 
   assert(state_offset <= batch->state.bo->size - sizeof(uint32_t));
449
 
 
450
 
   return emit_reloc(batch, &batch->state.relocs, state_offset,
451
 
                     target, target_offset, reloc_flags);
452
 
}
453
 
 
454
 
static void
455
 
recreate_growing_buffer(struct crocus_batch *batch,
456
 
                        struct crocus_growing_bo *grow,
457
 
                        const char *name, unsigned size)
458
 
{
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);
468
 
   else
469
 
      grow->map = crocus_bo_map(NULL, grow->bo, MAP_READ | MAP_WRITE);
470
 
   grow->map_next = grow->map;
471
 
}
472
 
 
473
 
static void
474
 
create_batch(struct crocus_batch *batch)
475
 
{
476
 
   struct crocus_screen *screen = batch->screen;
477
 
 
478
 
   recreate_growing_buffer(batch, &batch->command,
479
 
                           "command buffer",
480
 
                           BATCH_SZ + BATCH_RESERVED(&screen->devinfo));
481
 
 
482
 
   crocus_use_bo(batch, batch->command.bo, false);
483
 
 
484
 
   /* Always add workaround_bo which contains a driver identifier to be
485
 
    * recorded in error states.
486
 
    */
487
 
   crocus_use_bo(batch, batch->ice->workaround_bo, false);
488
 
 
489
 
   recreate_growing_buffer(batch, &batch->state,
490
 
                           "state buffer",
491
 
                           STATE_SZ);
492
 
 
493
 
   batch->state.used = 1;
494
 
   crocus_use_bo(batch, batch->state.bo, false);
495
 
}
496
 
 
497
 
static void
498
 
crocus_batch_maybe_noop(struct crocus_batch *batch)
499
 
{
500
 
   /* We only insert the NOOP at the beginning of the batch. */
501
 
   assert(crocus_batch_bytes_used(batch) == 0);
502
 
 
503
 
   if (batch->noop_enabled) {
504
 
      /* Emit MI_BATCH_BUFFER_END to prevent any further command to be
505
 
       * executed.
506
 
       */
507
 
      uint32_t *map = batch->command.map_next;
508
 
 
509
 
      map[0] = (0xA << 23);
510
 
 
511
 
      batch->command.map_next += 4;
512
 
   }
513
 
}
514
 
 
515
 
static void
516
 
crocus_batch_reset(struct crocus_batch *batch)
517
 
{
518
 
   struct crocus_screen *screen = batch->screen;
519
 
 
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);
527
 
 
528
 
   create_batch(batch);
529
 
   assert(batch->command.bo->index == 0);
530
 
 
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);
536
 
 
537
 
   crocus_cache_sets_clear(batch);
538
 
}
539
 
 
540
 
void
541
 
crocus_batch_free(struct crocus_batch *batch)
542
 
{
543
 
   struct crocus_screen *screen = batch->screen;
544
 
   struct crocus_bufmgr *bufmgr = screen->bufmgr;
545
 
 
546
 
   if (batch->use_shadow_copy) {
547
 
      free(batch->command.map);
548
 
      free(batch->state.map);
549
 
   }
550
 
 
551
 
   for (int i = 0; i < batch->exec_count; i++) {
552
 
      crocus_bo_unreference(batch->exec_bos[i]);
553
 
   }
554
 
 
555
 
   pipe_resource_reference(&batch->fine_fences.ref.res, NULL);
556
 
 
557
 
   free(batch->command.relocs.relocs);
558
 
   free(batch->state.relocs.relocs);
559
 
   free(batch->exec_bos);
560
 
   free(batch->validation_list);
561
 
 
562
 
   ralloc_free(batch->exec_fences.mem_ctx);
563
 
 
564
 
   util_dynarray_foreach(&batch->syncobjs, struct crocus_syncobj *, s)
565
 
      crocus_syncobj_reference(screen, s, NULL);
566
 
   ralloc_free(batch->syncobjs.mem_ctx);
567
 
 
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);
571
 
 
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;
577
 
 
578
 
   crocus_destroy_hw_context(bufmgr, batch->hw_ctx_id);
579
 
 
580
 
   _mesa_hash_table_destroy(batch->cache.render, NULL);
581
 
   _mesa_set_destroy(batch->cache.depth, NULL);
582
 
 
583
 
   if (batch->state_sizes) {
584
 
      _mesa_hash_table_u64_destroy(batch->state_sizes);
585
 
      intel_batch_decode_ctx_finish(&batch->decoder);
586
 
   }
587
 
}
588
 
 
589
 
/**
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.
592
 
 */
593
 
void
594
 
crocus_batch_maybe_flush(struct crocus_batch *batch, unsigned estimate)
595
 
{
596
 
   if (batch->command.bo != batch->exec_bos[0] ||
597
 
       crocus_batch_bytes_used(batch) + estimate >= BATCH_SZ) {
598
 
      crocus_batch_flush(batch);
599
 
   }
600
 
}
601
 
 
602
 
/**
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.
605
 
 */
606
 
static void
607
 
finish_growing_bos(struct crocus_growing_bo *grow)
608
 
{
609
 
   struct crocus_bo *old_bo = grow->partial_bo;
610
 
   if (!old_bo)
611
 
      return;
612
 
 
613
 
   memcpy(grow->map, grow->partial_bo_map, grow->partial_bytes);
614
 
 
615
 
   grow->partial_bo = NULL;
616
 
   grow->partial_bo_map = NULL;
617
 
   grow->partial_bytes = 0;
618
 
 
619
 
   crocus_bo_unreference(old_bo);
620
 
}
621
 
 
622
 
void
623
 
crocus_grow_buffer(struct crocus_batch *batch, bool grow_state,
624
 
                   unsigned used,
625
 
                   unsigned new_size)
626
 
{
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;
631
 
 
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.
636
 
       */
637
 
      finish_growing_bos(grow);
638
 
   }
639
 
 
640
 
   struct crocus_bo *new_bo = crocus_bo_alloc(bufmgr, bo->name, new_size);
641
 
 
642
 
   /* Copy existing data to the new larger buffer */
643
 
   grow->partial_bo_map = grow->map;
644
 
 
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.
649
 
       *
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.
652
 
       */
653
 
      grow->map = malloc(new_bo->size);
654
 
   } else {
655
 
      grow->map = crocus_bo_map(NULL, new_bo, MAP_READ | MAP_WRITE);
656
 
   }
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).
659
 
    *
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.
663
 
    *
664
 
    * Also preserve kflags for EXEC_OBJECT_CAPTURE.
665
 
    */
666
 
   new_bo->gtt_offset = bo->gtt_offset;
667
 
   new_bo->index = bo->index;
668
 
   new_bo->kflags = bo->kflags;
669
 
 
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.
672
 
    */
673
 
   assert(bo->index < batch->exec_count);
674
 
   assert(batch->exec_bos[bo->index] == bo);
675
 
 
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.
679
 
    *
680
 
    * Consider this scenario:
681
 
    *
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.
687
 
    *
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.
693
 
    *
694
 
    * This is not a contrived case - BLORP vertex data upload hits this.
695
 
    *
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.
701
 
    *
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.
706
 
    *
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.
712
 
    *
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.
717
 
    */
718
 
   assert(new_bo->refcount == 1);
719
 
   new_bo->refcount = bo->refcount;
720
 
   bo->refcount = 1;
721
 
 
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));
726
 
 
727
 
   grow->partial_bo = new_bo; /* the one reference of the OLD bo */
728
 
   grow->partial_bytes = used;
729
 
}
730
 
 
731
 
static void
732
 
finish_seqno(struct crocus_batch *batch)
733
 
{
734
 
   struct crocus_fine_fence *sq = crocus_fine_fence_new(batch, CROCUS_FENCE_END);
735
 
   if (!sq)
736
 
      return;
737
 
 
738
 
   crocus_fine_fence_reference(batch->screen, &batch->last_fence, sq);
739
 
   crocus_fine_fence_reference(batch->screen, &sq, NULL);
740
 
}
741
 
 
742
 
/**
743
 
 * Terminate a batch with MI_BATCH_BUFFER_END.
744
 
 */
745
 
static void
746
 
crocus_finish_batch(struct crocus_batch *batch)
747
 
{
748
 
 
749
 
   batch->no_wrap = true;
750
 
   if (batch->screen->vtbl.finish_batch)
751
 
      batch->screen->vtbl.finish_batch(batch);
752
 
 
753
 
   finish_seqno(batch);
754
 
 
755
 
   /* Emit MI_BATCH_BUFFER_END to finish our batch. */
756
 
   uint32_t *map = batch->command.map_next;
757
 
 
758
 
   map[0] = (0xA << 23);
759
 
 
760
 
   batch->command.map_next += 4;
761
 
   VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->command.map, crocus_batch_bytes_used(batch)));
762
 
 
763
 
   if (batch->command.bo == batch->exec_bos[0])
764
 
      batch->primary_batch_size = crocus_batch_bytes_used(batch);
765
 
   batch->no_wrap = false;
766
 
}
767
 
 
768
 
/**
769
 
 * Replace our current GEM context with a new one (in case it got banned).
770
 
 */
771
 
static bool
772
 
replace_hw_ctx(struct crocus_batch *batch)
773
 
{
774
 
   struct crocus_screen *screen = batch->screen;
775
 
   struct crocus_bufmgr *bufmgr = screen->bufmgr;
776
 
 
777
 
   uint32_t new_ctx = crocus_clone_hw_context(bufmgr, batch->hw_ctx_id);
778
 
   if (!new_ctx)
779
 
      return false;
780
 
 
781
 
   crocus_destroy_hw_context(bufmgr, batch->hw_ctx_id);
782
 
   batch->hw_ctx_id = new_ctx;
783
 
 
784
 
   /* Notify the context that state must be re-initialized. */
785
 
   crocus_lost_context_state(batch);
786
 
 
787
 
   return true;
788
 
}
789
 
 
790
 
enum pipe_reset_status
791
 
crocus_batch_check_for_reset(struct crocus_batch *batch)
792
 
{
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 };
796
 
 
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));
799
 
 
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.
803
 
       */
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.
809
 
       */
810
 
      status = PIPE_INNOCENT_CONTEXT_RESET;
811
 
   }
812
 
 
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.
817
 
       */
818
 
      replace_hw_ctx(batch);
819
 
   }
820
 
 
821
 
   return status;
822
 
}
823
 
 
824
 
/**
825
 
 * Submit the batch to the GPU via execbuffer2.
826
 
 */
827
 
static int
828
 
submit_batch(struct crocus_batch *batch)
829
 
{
830
 
 
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));
834
 
 
835
 
      bo_map = crocus_bo_map(batch->dbg, batch->state.bo, MAP_WRITE);
836
 
      memcpy(bo_map, batch->state.map, batch->state.used);
837
 
   }
838
 
 
839
 
   crocus_bo_unmap(batch->command.bo);
840
 
   crocus_bo_unmap(batch->state.bo);
841
 
 
842
 
   /* The requirement for using I915_EXEC_NO_RELOC are:
843
 
    *
844
 
    *   The addresses written in the objects must match the corresponding
845
 
    *   reloc.gtt_offset which in turn must match the corresponding
846
 
    *   execobject.offset.
847
 
    *
848
 
    *   Any render targets written to in the batch must be flagged with
849
 
    *   EXEC_OBJECT_WRITE.
850
 
    *
851
 
    *   To avoid stalling, execobject.offset should match the current
852
 
    *   address of that object within the active context.
853
 
    */
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;
863
 
   }
864
 
 
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;
870
 
 
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 |
878
 
               I915_EXEC_NO_RELOC |
879
 
               I915_EXEC_BATCH_FIRST |
880
 
               I915_EXEC_HANDLE_LUT,
881
 
      .rsvd1 = batch->hw_ctx_id, /* rsvd1 is actually the context ID */
882
 
   };
883
 
 
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);
889
 
   }
890
 
 
891
 
   int ret = 0;
892
 
   if (!batch->screen->devinfo.no_hw &&
893
 
       intel_ioctl(batch->screen->fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf))
894
 
      ret = -errno;
895
 
 
896
 
   for (int i = 0; i < batch->exec_count; i++) {
897
 
      struct crocus_bo *bo = batch->exec_bos[i];
898
 
 
899
 
      bo->idle = false;
900
 
      bo->index = -1;
901
 
 
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;
909
 
      }
910
 
   }
911
 
 
912
 
   return ret;
913
 
}
914
 
 
915
 
static const char *
916
 
batch_name_to_string(enum crocus_batch_name name)
917
 
{
918
 
   const char *names[CROCUS_BATCH_COUNT] = {
919
 
      [CROCUS_BATCH_RENDER] = "render",
920
 
      [CROCUS_BATCH_COMPUTE] = "compute",
921
 
   };
922
 
   return names[name];
923
 
}
924
 
 
925
 
/**
926
 
 * Flush the batch buffer, submitting it to the GPU and resetting it so
927
 
 * we're ready to emit the next batch.
928
 
 *
929
 
 * \param in_fence_fd is ignored if -1.  Otherwise, this function takes
930
 
 * ownership of the fd.
931
 
 *
932
 
 * \param out_fence_fd is ignored if NULL.  Otherwise, the caller must
933
 
 * take ownership of the returned fd.
934
 
 */
935
 
void
936
 
_crocus_batch_flush(struct crocus_batch *batch, const char *file, int line)
937
 
{
938
 
   struct crocus_screen *screen = batch->screen;
939
 
 
940
 
   /* If a fence signals we need to flush it. */
941
 
   if (crocus_batch_bytes_used(batch) == 0 && !batch->contains_fence_signal)
942
 
      return;
943
 
 
944
 
   assert(!batch->no_wrap);
945
 
   crocus_finish_batch(batch);
946
 
 
947
 
   finish_growing_bos(&batch->command);
948
 
   finish_growing_bos(&batch->state);
949
 
   int ret = submit_batch(batch);
950
 
 
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;
957
 
      }
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,
964
 
              batch->exec_count,
965
 
              (float) batch->aperture_space / (1024 * 1024),
966
 
              batch->command.relocs.reloc_count,
967
 
              batch->state.relocs.reloc_count);
968
 
 
969
 
      if (INTEL_DEBUG(DEBUG_BATCH | DEBUG_SUBMIT)) {
970
 
         dump_fence_list(batch);
971
 
         dump_validation_list(batch);
972
 
      }
973
 
 
974
 
      if (INTEL_DEBUG(DEBUG_BATCH)) {
975
 
         decode_batch(batch);
976
 
      }
977
 
   }
978
 
 
979
 
   for (int i = 0; i < batch->exec_count; i++) {
980
 
      struct crocus_bo *bo = batch->exec_bos[i];
981
 
      crocus_bo_unreference(bo);
982
 
   }
983
 
 
984
 
   batch->command.relocs.reloc_count = 0;
985
 
   batch->state.relocs.reloc_count = 0;
986
 
   batch->exec_count = 0;
987
 
   batch->aperture_space = 0;
988
 
 
989
 
   util_dynarray_foreach(&batch->syncobjs, struct crocus_syncobj *, s)
990
 
      crocus_syncobj_reference(screen, s, NULL);
991
 
   util_dynarray_clear(&batch->syncobjs);
992
 
 
993
 
   util_dynarray_clear(&batch->exec_fences);
994
 
 
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 */
998
 
   }
999
 
 
1000
 
   /* Start a new batch buffer. */
1001
 
   crocus_batch_reset(batch);
1002
 
 
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...
1007
 
    */
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);
1012
 
      }
1013
 
 
1014
 
      ret = 0;
1015
 
   }
1016
 
 
1017
 
   if (ret < 0) {
1018
 
#ifdef DEBUG
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" : "");
1022
 
#endif
1023
 
      abort();
1024
 
   }
1025
 
}
1026
 
 
1027
 
/**
1028
 
 * Does the current batch refer to the given BO?
1029
 
 *
1030
 
 * (In other words, is the BO in the current batch's validation list?)
1031
 
 */
1032
 
bool
1033
 
crocus_batch_references(struct crocus_batch *batch, struct crocus_bo *bo)
1034
 
{
1035
 
   return find_validation_entry(batch, bo) != NULL;
1036
 
}
1037
 
 
1038
 
/**
1039
 
 * Updates the state of the noop feature.  Returns true if there was a noop
1040
 
 * transition that led to state invalidation.
1041
 
 */
1042
 
bool
1043
 
crocus_batch_prepare_noop(struct crocus_batch *batch, bool noop_enable)
1044
 
{
1045
 
   if (batch->noop_enabled == noop_enable)
1046
 
      return 0;
1047
 
 
1048
 
   batch->noop_enabled = noop_enable;
1049
 
 
1050
 
   crocus_batch_flush(batch);
1051
 
 
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);
1055
 
 
1056
 
   /* We only need to update the entire state if we transition from noop ->
1057
 
    * not-noop.
1058
 
    */
1059
 
   return !batch->noop_enabled;
1060
 
}