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.
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 "iris_batch.h"
41 #include "iris_bufmgr.h"
42 #include "iris_context.h"
43 #include "iris_fence.h"
45 #include "drm-uapi/i915_drm.h"
47 #include "util/hash_table.h"
49 #include "main/macros.h"
54 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
56 /* Terminating the batch takes either 4 bytes for MI_BATCH_BUFFER_END
57 * or 12 bytes for MI_BATCH_BUFFER_START (when chaining). Plus, we may
58 * need an extra 4 bytes to pad out to the nearest QWord. So reserve 16.
60 #define BATCH_RESERVED 16
63 iris_batch_reset(struct iris_batch
*batch
);
66 num_fences(struct iris_batch
*batch
)
68 return util_dynarray_num_elements(&batch
->exec_fences
,
69 struct drm_i915_gem_exec_fence
);
73 * Debugging code to dump the fence list, used by INTEL_DEBUG=submit.
76 dump_fence_list(struct iris_batch
*batch
)
78 fprintf(stderr
, "Fence list (length %u): ", num_fences(batch
));
80 util_dynarray_foreach(&batch
->exec_fences
,
81 struct drm_i915_gem_exec_fence
, f
) {
82 fprintf(stderr
, "%s%u%s ",
83 (f
->flags
& I915_EXEC_FENCE_WAIT
) ? "..." : "",
85 (f
->flags
& I915_EXEC_FENCE_SIGNAL
) ? "!" : "");
88 fprintf(stderr
, "\n");
92 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
95 dump_validation_list(struct iris_batch
*batch
)
97 fprintf(stderr
, "Validation list (length %d):\n", batch
->exec_count
);
99 for (int i
= 0; i
< batch
->exec_count
; i
++) {
100 uint64_t flags
= batch
->validation_list
[i
].flags
;
101 assert(batch
->validation_list
[i
].handle
==
102 batch
->exec_bos
[i
]->gem_handle
);
103 fprintf(stderr
, "[%2d]: %2d %-14s @ 0x%016llx (%"PRIu64
"B)\t %2d refs %s\n",
105 batch
->validation_list
[i
].handle
,
106 batch
->exec_bos
[i
]->name
,
107 batch
->validation_list
[i
].offset
,
108 batch
->exec_bos
[i
]->size
,
109 batch
->exec_bos
[i
]->refcount
,
110 (flags
& EXEC_OBJECT_WRITE
) ? " (write)" : "");
115 * Return BO information to the batch decoder (for debugging).
117 static struct gen_batch_decode_bo
118 decode_get_bo(void *v_batch
, bool ppgtt
, uint64_t address
)
120 struct iris_batch
*batch
= v_batch
;
124 for (int i
= 0; i
< batch
->exec_count
; i
++) {
125 struct iris_bo
*bo
= batch
->exec_bos
[i
];
126 /* The decoder zeroes out the top 16 bits, so we need to as well */
127 uint64_t bo_address
= bo
->gtt_offset
& (~0ull >> 16);
129 if (address
>= bo_address
&& address
< bo_address
+ bo
->size
) {
130 return (struct gen_batch_decode_bo
) {
133 .map
= iris_bo_map(batch
->dbg
, bo
, MAP_READ
) +
134 (address
- bo_address
),
139 return (struct gen_batch_decode_bo
) { };
143 * Decode the current batch.
146 decode_batch(struct iris_batch
*batch
)
148 void *map
= iris_bo_map(batch
->dbg
, batch
->exec_bos
[0], MAP_READ
);
149 gen_print_batch(&batch
->decoder
, map
, batch
->primary_batch_size
,
150 batch
->exec_bos
[0]->gtt_offset
, false);
154 iris_init_batch(struct iris_batch
*batch
,
155 struct iris_screen
*screen
,
156 struct iris_vtable
*vtbl
,
157 struct pipe_debug_callback
*dbg
,
158 struct iris_batch
*all_batches
,
159 enum iris_batch_name name
,
162 batch
->screen
= screen
;
167 /* engine should be one of I915_EXEC_RENDER, I915_EXEC_BLT, etc. */
168 assert((engine
& ~I915_EXEC_RING_MASK
) == 0);
169 assert(util_bitcount(engine
) == 1);
170 batch
->engine
= engine
;
172 batch
->hw_ctx_id
= iris_create_hw_context(screen
->bufmgr
);
173 assert(batch
->hw_ctx_id
);
175 util_dynarray_init(&batch
->exec_fences
, ralloc_context(NULL
));
176 util_dynarray_init(&batch
->syncpts
, ralloc_context(NULL
));
178 batch
->exec_count
= 0;
179 batch
->exec_array_size
= 100;
181 malloc(batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
182 batch
->validation_list
=
183 malloc(batch
->exec_array_size
* sizeof(batch
->validation_list
[0]));
185 batch
->cache
.render
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
186 _mesa_key_pointer_equal
);
187 batch
->cache
.depth
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
188 _mesa_key_pointer_equal
);
190 memset(batch
->other_batches
, 0, sizeof(batch
->other_batches
));
192 for (int i
= 0, j
= 0; i
< IRIS_BATCH_COUNT
; i
++) {
193 if (&all_batches
[i
] != batch
)
194 batch
->other_batches
[j
++] = &all_batches
[i
];
197 if (unlikely(INTEL_DEBUG
)) {
198 const unsigned decode_flags
=
199 GEN_BATCH_DECODE_FULL
|
200 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
201 GEN_BATCH_DECODE_OFFSETS
|
202 GEN_BATCH_DECODE_FLOATS
;
204 /* TODO: track state size so we can print the right # of entries */
205 gen_batch_decode_ctx_init(&batch
->decoder
, &screen
->devinfo
,
206 stderr
, decode_flags
, NULL
,
207 decode_get_bo
, NULL
, batch
);
208 batch
->decoder
.max_vbo_decoded_lines
= 32;
211 iris_batch_reset(batch
);
214 static struct drm_i915_gem_exec_object2
*
215 find_validation_entry(struct iris_batch
*batch
, struct iris_bo
*bo
)
217 unsigned index
= READ_ONCE(bo
->index
);
219 if (index
< batch
->exec_count
&& batch
->exec_bos
[index
] == bo
)
220 return &batch
->validation_list
[index
];
222 /* May have been shared between multiple active batches */
223 for (index
= 0; index
< batch
->exec_count
; index
++) {
224 if (batch
->exec_bos
[index
] == bo
)
225 return &batch
->validation_list
[index
];
232 * Add a buffer to the current batch's validation list.
234 * You must call this on any BO you wish to use in this batch, to ensure
235 * that it's resident when the GPU commands execute.
238 iris_use_pinned_bo(struct iris_batch
*batch
,
242 assert(bo
->kflags
& EXEC_OBJECT_PINNED
);
244 /* Never mark the workaround BO with EXEC_OBJECT_WRITE. We don't care
245 * about the order of any writes to that buffer, and marking it writable
246 * would introduce data dependencies between multiple batches which share
249 if (bo
== batch
->screen
->workaround_bo
)
252 struct drm_i915_gem_exec_object2
*existing_entry
=
253 find_validation_entry(batch
, bo
);
255 if (existing_entry
) {
256 /* The BO is already in the validation list; mark it writable */
258 existing_entry
->flags
|= EXEC_OBJECT_WRITE
;
263 if (bo
!= batch
->bo
) {
264 /* This is the first time our batch has seen this BO. Before we use it,
265 * we may need to flush and synchronize with other batches.
267 for (int b
= 0; b
< ARRAY_SIZE(batch
->other_batches
); b
++) {
268 struct drm_i915_gem_exec_object2
*other_entry
=
269 find_validation_entry(batch
->other_batches
[b
], bo
);
271 /* If the buffer is referenced by another batch, and either batch
272 * intends to write it, then flush the other batch and synchronize.
274 * Consider these cases:
276 * 1. They read, we read => No synchronization required.
277 * 2. They read, we write => Synchronize (they need the old value)
278 * 3. They write, we read => Synchronize (we need their new value)
279 * 4. They write, we write => Synchronize (order writes)
281 * The read/read case is very common, as multiple batches usually
282 * share a streaming state buffer or shader assembly buffer, and
283 * we want to avoid synchronizing in this case.
286 ((other_entry
->flags
& EXEC_OBJECT_WRITE
) || writable
)) {
287 iris_batch_flush(batch
->other_batches
[b
]);
288 iris_batch_add_syncpt(batch
, batch
->other_batches
[b
]->last_syncpt
,
289 I915_EXEC_FENCE_WAIT
);
294 /* Now, take a reference and add it to the validation list. */
295 iris_bo_reference(bo
);
297 if (batch
->exec_count
== batch
->exec_array_size
) {
298 batch
->exec_array_size
*= 2;
300 realloc(batch
->exec_bos
,
301 batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
302 batch
->validation_list
=
303 realloc(batch
->validation_list
,
304 batch
->exec_array_size
* sizeof(batch
->validation_list
[0]));
307 batch
->validation_list
[batch
->exec_count
] =
308 (struct drm_i915_gem_exec_object2
) {
309 .handle
= bo
->gem_handle
,
310 .offset
= bo
->gtt_offset
,
311 .flags
= bo
->kflags
| (writable
? EXEC_OBJECT_WRITE
: 0),
314 bo
->index
= batch
->exec_count
;
315 batch
->exec_bos
[batch
->exec_count
] = bo
;
316 batch
->aperture_space
+= bo
->size
;
322 create_batch(struct iris_batch
*batch
)
324 struct iris_screen
*screen
= batch
->screen
;
325 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
327 batch
->bo
= iris_bo_alloc(bufmgr
, "command buffer",
328 BATCH_SZ
+ BATCH_RESERVED
, IRIS_MEMZONE_OTHER
);
329 batch
->bo
->kflags
|= EXEC_OBJECT_CAPTURE
;
330 batch
->map
= iris_bo_map(NULL
, batch
->bo
, MAP_READ
| MAP_WRITE
);
331 batch
->map_next
= batch
->map
;
333 iris_use_pinned_bo(batch
, batch
->bo
, false);
337 iris_batch_reset(struct iris_batch
*batch
)
339 struct iris_screen
*screen
= batch
->screen
;
341 iris_bo_unreference(batch
->bo
);
342 batch
->primary_batch_size
= 0;
343 batch
->contains_draw
= false;
346 assert(batch
->bo
->index
== 0);
348 struct iris_syncpt
*syncpt
= iris_create_syncpt(screen
);
349 iris_batch_add_syncpt(batch
, syncpt
, I915_EXEC_FENCE_SIGNAL
);
350 iris_syncpt_reference(screen
, &syncpt
, NULL
);
352 iris_cache_sets_clear(batch
);
356 iris_batch_free(struct iris_batch
*batch
)
358 struct iris_screen
*screen
= batch
->screen
;
359 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
361 for (int i
= 0; i
< batch
->exec_count
; i
++) {
362 iris_bo_unreference(batch
->exec_bos
[i
]);
364 free(batch
->exec_bos
);
365 free(batch
->validation_list
);
367 ralloc_free(batch
->exec_fences
.mem_ctx
);
369 util_dynarray_foreach(&batch
->syncpts
, struct iris_syncpt
*, s
)
370 iris_syncpt_reference(screen
, s
, NULL
);
371 ralloc_free(batch
->syncpts
.mem_ctx
);
373 iris_syncpt_reference(screen
, &batch
->last_syncpt
, NULL
);
375 iris_bo_unreference(batch
->bo
);
378 batch
->map_next
= NULL
;
380 iris_destroy_hw_context(bufmgr
, batch
->hw_ctx_id
);
382 _mesa_hash_table_destroy(batch
->cache
.render
, NULL
);
383 _mesa_set_destroy(batch
->cache
.depth
, NULL
);
385 if (unlikely(INTEL_DEBUG
))
386 gen_batch_decode_ctx_finish(&batch
->decoder
);
390 * If we've chained to a secondary batch, or are getting near to the end,
391 * then flush. This should only be called between draws.
394 iris_batch_maybe_flush(struct iris_batch
*batch
, unsigned estimate
)
396 if (batch
->bo
!= batch
->exec_bos
[0] ||
397 iris_batch_bytes_used(batch
) + estimate
>= BATCH_SZ
) {
398 iris_batch_flush(batch
);
403 iris_chain_to_new_batch(struct iris_batch
*batch
)
405 /* We only support chaining a single time. */
406 assert(batch
->bo
== batch
->exec_bos
[0]);
408 uint32_t *cmd
= batch
->map_next
;
409 uint64_t *addr
= batch
->map_next
+ 4;
410 batch
->map_next
+= 8;
412 /* No longer held by batch->bo, still held by validation list */
413 iris_bo_unreference(batch
->bo
);
414 batch
->primary_batch_size
= iris_batch_bytes_used(batch
);
417 /* Emit MI_BATCH_BUFFER_START to chain to another batch. */
418 *cmd
= (0x31 << 23) | (1 << 8) | (3 - 2);
419 *addr
= batch
->bo
->gtt_offset
;
423 * Terminate a batch with MI_BATCH_BUFFER_END.
426 iris_finish_batch(struct iris_batch
*batch
)
428 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
429 uint32_t *map
= batch
->map_next
;
431 map
[0] = (0xA << 23);
433 batch
->map_next
+= 4;
435 if (batch
->bo
== batch
->exec_bos
[0])
436 batch
->primary_batch_size
= iris_batch_bytes_used(batch
);
440 * Submit the batch to the GPU via execbuffer2.
443 submit_batch(struct iris_batch
*batch
)
445 iris_bo_unmap(batch
->bo
);
447 /* The requirement for using I915_EXEC_NO_RELOC are:
449 * The addresses written in the objects must match the corresponding
450 * reloc.gtt_offset which in turn must match the corresponding
453 * Any render targets written to in the batch must be flagged with
456 * To avoid stalling, execobject.offset should match the current
457 * address of that object within the active context.
459 struct drm_i915_gem_execbuffer2 execbuf
= {
460 .buffers_ptr
= (uintptr_t) batch
->validation_list
,
461 .buffer_count
= batch
->exec_count
,
462 .batch_start_offset
= 0,
463 /* This must be QWord aligned. */
464 .batch_len
= ALIGN(batch
->primary_batch_size
, 8),
465 .flags
= batch
->engine
|
467 I915_EXEC_BATCH_FIRST
|
468 I915_EXEC_HANDLE_LUT
,
469 .rsvd1
= batch
->hw_ctx_id
, /* rsvd1 is actually the context ID */
472 if (num_fences(batch
)) {
473 execbuf
.flags
|= I915_EXEC_FENCE_ARRAY
;
474 execbuf
.num_cliprects
= num_fences(batch
);
475 execbuf
.cliprects_ptr
=
476 (uintptr_t)util_dynarray_begin(&batch
->exec_fences
);
479 int ret
= drm_ioctl(batch
->screen
->fd
,
480 DRM_IOCTL_I915_GEM_EXECBUFFER2
,
484 DBG("execbuf FAILED: errno = %d\n", -ret
);
485 fprintf(stderr
, "execbuf FAILED: errno = %d\n", -ret
);
488 DBG("execbuf succeeded\n");
491 for (int i
= 0; i
< batch
->exec_count
; i
++) {
492 struct iris_bo
*bo
= batch
->exec_bos
[i
];
497 iris_bo_unreference(bo
);
504 batch_name_to_string(enum iris_batch_name name
)
506 const char *names
[IRIS_BATCH_COUNT
] = {
507 [IRIS_BATCH_RENDER
] = "render",
508 [IRIS_BATCH_COMPUTE
] = "compute",
514 * Flush the batch buffer, submitting it to the GPU and resetting it so
515 * we're ready to emit the next batch.
517 * \param in_fence_fd is ignored if -1. Otherwise, this function takes
518 * ownership of the fd.
520 * \param out_fence_fd is ignored if NULL. Otherwise, the caller must
521 * take ownership of the returned fd.
524 _iris_batch_flush(struct iris_batch
*batch
, const char *file
, int line
)
526 struct iris_screen
*screen
= batch
->screen
;
528 if (iris_batch_bytes_used(batch
) == 0)
531 iris_finish_batch(batch
);
533 if (unlikely(INTEL_DEBUG
& (DEBUG_BATCH
| DEBUG_SUBMIT
))) {
534 int bytes_for_commands
= iris_batch_bytes_used(batch
);
535 int second_bytes
= 0;
536 if (batch
->bo
!= batch
->exec_bos
[0]) {
537 second_bytes
= bytes_for_commands
;
538 bytes_for_commands
+= batch
->primary_batch_size
;
540 fprintf(stderr
, "%19s:%-3d: %s batch [%u] flush with %5d+%5db (%0.1f%%) "
541 "(cmds), %4d BOs (%0.1fMb aperture)\n",
542 file
, line
, batch_name_to_string(batch
->name
), batch
->hw_ctx_id
,
543 batch
->primary_batch_size
, second_bytes
,
544 100.0f
* bytes_for_commands
/ BATCH_SZ
,
546 (float) batch
->aperture_space
/ (1024 * 1024));
547 dump_fence_list(batch
);
548 dump_validation_list(batch
);
551 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
555 int ret
= submit_batch(batch
);
558 //if (iris->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
559 //iris_check_for_reset(ice);
561 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
562 dbg_printf("waiting for idle\n");
563 iris_bo_wait_rendering(batch
->bo
);
567 const bool color
= INTEL_DEBUG
& DEBUG_COLOR
;
568 fprintf(stderr
, "%siris: Failed to submit batchbuffer: %-80s%s\n",
569 color
? "\e[1;41m" : "", strerror(-ret
), color
? "\e[0m" : "");
574 batch
->exec_count
= 0;
575 batch
->aperture_space
= 0;
577 struct iris_syncpt
*syncpt
=
578 ((struct iris_syncpt
**) util_dynarray_begin(&batch
->syncpts
))[0];
579 iris_syncpt_reference(screen
, &batch
->last_syncpt
, syncpt
);
581 util_dynarray_foreach(&batch
->syncpts
, struct iris_syncpt
*, s
)
582 iris_syncpt_reference(screen
, s
, NULL
);
583 util_dynarray_clear(&batch
->syncpts
);
585 util_dynarray_clear(&batch
->exec_fences
);
587 /* Start a new batch buffer. */
588 iris_batch_reset(batch
);
592 * Does the current batch refer to the given BO?
594 * (In other words, is the BO in the current batch's validation list?)
597 iris_batch_references(struct iris_batch
*batch
, struct iris_bo
*bo
)
599 return find_validation_entry(batch
, bo
) != NULL
;