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
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "iris_batch.h"
26 #include "iris_bufmgr.h"
27 #include "iris_context.h"
29 #include "drm-uapi/i915_drm.h"
31 #include "util/hash_table.h"
32 #include "main/macros.h"
37 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
40 * Target sizes of the batch and state buffers. We create the initial
41 * buffers at these sizes, and flush when they're nearly full. If we
42 * underestimate how close we are to the end, and suddenly need more space
43 * in the middle of a draw, we can grow the buffers, and finish the draw.
44 * At that point, we'll be over our target size, so the next operation
45 * should flush. Each time we flush the batch, we recreate both buffers
46 * at the original target size, so it doesn't grow without bound.
48 #define BATCH_SZ (20 * 1024)
49 #define STATE_SZ (18 * 1024)
51 static void decode_batch(struct iris_batch
*batch
);
54 iris_batch_reset(struct iris_batch
*batch
);
57 dump_validation_list(struct iris_batch
*batch
)
59 fprintf(stderr
, "Validation list (length %d):\n", batch
->exec_count
);
61 for (int i
= 0; i
< batch
->exec_count
; i
++) {
62 uint64_t flags
= batch
->validation_list
[i
].flags
;
63 assert(batch
->validation_list
[i
].handle
==
64 batch
->exec_bos
[i
]->gem_handle
);
65 fprintf(stderr
, "[%2d]: %2d %-14s %p %s%-7s @ 0x%016llx (%"PRIu64
"B)\n",
67 batch
->validation_list
[i
].handle
,
68 batch
->exec_bos
[i
]->name
,
70 (flags
& EXEC_OBJECT_SUPPORTS_48B_ADDRESS
) ? "(48b" : "(32b",
71 (flags
& EXEC_OBJECT_WRITE
) ? " write)" : ")",
72 batch
->validation_list
[i
].offset
,
73 batch
->exec_bos
[i
]->size
);
77 static struct gen_batch_decode_bo
78 decode_get_bo(void *v_batch
, uint64_t address
)
80 struct iris_batch
*batch
= v_batch
;
82 for (int i
= 0; i
< batch
->exec_count
; i
++) {
83 struct iris_bo
*bo
= batch
->exec_bos
[i
];
84 /* The decoder zeroes out the top 16 bits, so we need to as well */
85 uint64_t bo_address
= bo
->gtt_offset
& (~0ull >> 16);
87 if (address
>= bo_address
&& address
< bo_address
+ bo
->size
) {
88 return (struct gen_batch_decode_bo
) {
91 .map
= iris_bo_map(batch
->dbg
, bo
, MAP_READ
) +
92 (address
- bo_address
),
97 return (struct gen_batch_decode_bo
) { };
101 uint_key_compare(const void *a
, const void *b
)
107 uint_key_hash(const void *key
)
109 return (uintptr_t) key
;
113 create_batch_buffer(struct iris_bufmgr
*bufmgr
,
114 struct iris_batch_buffer
*buf
,
115 const char *name
, unsigned size
)
117 buf
->bo
= iris_bo_alloc(bufmgr
, name
, size
, IRIS_MEMZONE_OTHER
);
118 buf
->bo
->kflags
|= EXEC_OBJECT_CAPTURE
;
119 buf
->map
= iris_bo_map(NULL
, buf
->bo
, MAP_READ
| MAP_WRITE
);
120 buf
->map_next
= buf
->map
;
124 iris_init_batch(struct iris_batch
*batch
,
125 struct iris_screen
*screen
,
126 struct pipe_debug_callback
*dbg
,
129 batch
->screen
= screen
;
132 /* ring should be one of I915_EXEC_RENDER, I915_EXEC_BLT, etc. */
133 assert((ring
& ~I915_EXEC_RING_MASK
) == 0);
134 assert(util_bitcount(ring
) == 1);
137 batch
->exec_count
= 0;
138 batch
->exec_array_size
= 100;
140 malloc(batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
141 batch
->validation_list
=
142 malloc(batch
->exec_array_size
* sizeof(batch
->validation_list
[0]));
144 if (unlikely(INTEL_DEBUG
)) {
146 _mesa_hash_table_create(NULL
, uint_key_hash
, uint_key_compare
);
148 const unsigned decode_flags
=
149 GEN_BATCH_DECODE_FULL
|
150 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
151 GEN_BATCH_DECODE_OFFSETS
|
152 GEN_BATCH_DECODE_FLOATS
;
154 gen_batch_decode_ctx_init(&batch
->decoder
, &screen
->devinfo
,
155 stderr
, decode_flags
, NULL
,
156 decode_get_bo
, NULL
, batch
);
159 iris_batch_reset(batch
);
162 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
165 add_exec_bo(struct iris_batch
*batch
, struct iris_bo
*bo
)
167 unsigned index
= READ_ONCE(bo
->index
);
169 if (index
< batch
->exec_count
&& batch
->exec_bos
[index
] == bo
)
172 /* May have been shared between multiple active batches */
173 for (index
= 0; index
< batch
->exec_count
; index
++) {
174 if (batch
->exec_bos
[index
] == bo
)
178 iris_bo_reference(bo
);
180 if (batch
->exec_count
== batch
->exec_array_size
) {
181 batch
->exec_array_size
*= 2;
183 realloc(batch
->exec_bos
,
184 batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
185 batch
->validation_list
=
186 realloc(batch
->validation_list
,
187 batch
->exec_array_size
* sizeof(batch
->validation_list
[0]));
190 batch
->validation_list
[batch
->exec_count
] =
191 (struct drm_i915_gem_exec_object2
) {
192 .handle
= bo
->gem_handle
,
193 .offset
= bo
->gtt_offset
,
197 bo
->index
= batch
->exec_count
;
198 batch
->exec_bos
[batch
->exec_count
] = bo
;
199 batch
->aperture_space
+= bo
->size
;
201 return batch
->exec_count
++;
205 iris_batch_reset(struct iris_batch
*batch
)
207 struct iris_screen
*screen
= batch
->screen
;
208 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
210 if (batch
->last_cmd_bo
!= NULL
) {
211 iris_bo_unreference(batch
->last_cmd_bo
);
212 batch
->last_cmd_bo
= NULL
;
214 batch
->last_cmd_bo
= batch
->cmdbuf
.bo
;
216 create_batch_buffer(bufmgr
, &batch
->cmdbuf
, "command buffer", BATCH_SZ
);
218 add_exec_bo(batch
, batch
->cmdbuf
.bo
);
219 assert(batch
->cmdbuf
.bo
->index
== 0);
221 if (batch
->state_sizes
)
222 _mesa_hash_table_clear(batch
->state_sizes
, NULL
);
226 iris_batch_reset_and_clear_render_cache(struct iris_batch
*batch
)
228 iris_batch_reset(batch
);
229 // XXX: iris_render_cache_set_clear(batch);
233 free_batch_buffer(struct iris_batch_buffer
*buf
)
235 iris_bo_unreference(buf
->bo
);
238 buf
->map_next
= NULL
;
242 iris_batch_free(struct iris_batch
*batch
)
244 for (int i
= 0; i
< batch
->exec_count
; i
++) {
245 iris_bo_unreference(batch
->exec_bos
[i
]);
247 free(batch
->exec_bos
);
248 free(batch
->validation_list
);
249 free_batch_buffer(&batch
->cmdbuf
);
251 iris_bo_unreference(batch
->last_cmd_bo
);
253 if (batch
->state_sizes
) {
254 _mesa_hash_table_destroy(batch
->state_sizes
, NULL
);
255 gen_batch_decode_ctx_finish(&batch
->decoder
);
260 * Finish copying the old batch/state buffer's contents to the new one
261 * after we tried to "grow" the buffer in an earlier operation.
264 finish_growing_bos(struct iris_batch_buffer
*buf
)
266 struct iris_bo
*old_bo
= buf
->partial_bo
;
270 void *old_map
= old_bo
->map_cpu
? old_bo
->map_cpu
: old_bo
->map_wc
;
271 memcpy(buf
->map
, old_map
, buf
->partial_bytes
);
273 buf
->partial_bo
= NULL
;
274 buf
->partial_bytes
= 0;
276 iris_bo_unreference(old_bo
);
280 buffer_bytes_used(struct iris_batch_buffer
*buf
)
282 return buf
->map_next
- buf
->map
;
286 * Grow either the batch or state buffer to a new larger size.
288 * We can't actually grow buffers, so we allocate a new one, copy over
289 * the existing contents, and update our lists to refer to the new one.
291 * Note that this is only temporary - each new batch recreates the buffers
292 * at their original target size (BATCH_SZ or STATE_SZ).
295 grow_buffer(struct iris_batch
*batch
,
296 struct iris_batch_buffer
*buf
,
299 struct iris_bufmgr
*bufmgr
= batch
->screen
->bufmgr
;
300 struct iris_bo
*bo
= buf
->bo
;
302 perf_debug(batch
->dbg
, "Growing %s - ran out of space\n", bo
->name
);
304 if (buf
->partial_bo
) {
305 /* We've already grown once, and now we need to do it again.
306 * Finish our last grow operation so we can start a new one.
307 * This should basically never happen.
309 perf_debug(batch
->dbg
, "Had to grow multiple times");
310 finish_growing_bos(buf
);
313 const unsigned existing_bytes
= buffer_bytes_used(buf
);
315 struct iris_bo
*new_bo
=
316 iris_bo_alloc(bufmgr
, bo
->name
, new_size
, IRIS_MEMZONE_OTHER
);
318 buf
->map
= iris_bo_map(NULL
, new_bo
, MAP_READ
| MAP_WRITE
);
319 buf
->map_next
= buf
->map
+ existing_bytes
;
321 /* Try to put the new BO at the same GTT offset as the old BO (which
322 * we're throwing away, so it doesn't need to be there).
324 * This guarantees that our relocations continue to work: values we've
325 * already written into the buffer, values we're going to write into the
326 * buffer, and the validation/relocation lists all will match.
328 * Also preserve kflags for EXEC_OBJECT_CAPTURE.
330 new_bo
->gtt_offset
= bo
->gtt_offset
;
331 new_bo
->index
= bo
->index
;
332 new_bo
->kflags
= bo
->kflags
;
334 /* Batch/state buffers are per-context, and if we've run out of space,
335 * we must have actually used them before, so...they will be in the list.
337 assert(bo
->index
< batch
->exec_count
);
338 assert(batch
->exec_bos
[bo
->index
] == bo
);
340 /* Update the validation list to use the new BO. */
341 batch
->exec_bos
[bo
->index
] = new_bo
;
342 batch
->validation_list
[bo
->index
].handle
= new_bo
->gem_handle
;
344 /* Exchange the two BOs...without breaking pointers to the old BO.
346 * Consider this scenario:
348 * 1. Somebody calls iris_state_batch() to get a region of memory, and
349 * and then creates a iris_address pointing to iris->batch.state.bo.
350 * 2. They then call iris_state_batch() a second time, which happens to
351 * grow and replace the state buffer. They then try to emit a
352 * relocation to their first section of memory.
354 * If we replace the iris->batch.state.bo pointer at step 2, we would
355 * break the address created in step 1. They'd have a pointer to the
356 * old destroyed BO. Emitting a relocation would add this dead BO to
357 * the validation list...causing /both/ statebuffers to be in the list,
358 * and all kinds of disasters.
360 * This is not a contrived case - BLORP vertex data upload hits this.
362 * There are worse scenarios too. Fences for GL sync objects reference
363 * iris->batch.batch.bo. If we replaced the batch pointer when growing,
364 * we'd need to chase down every fence and update it to point to the
365 * new BO. Otherwise, it would refer to a "batch" that never actually
366 * gets submitted, and would fail to trigger.
368 * To work around both of these issues, we transmutate the buffers in
369 * place, making the existing struct iris_bo represent the new buffer,
370 * and "new_bo" represent the old BO. This is highly unusual, but it
371 * seems like a necessary evil.
373 * We also defer the memcpy of the existing batch's contents. Callers
374 * may make multiple iris_state_batch calls, and retain pointers to the
375 * old BO's map. We'll perform the memcpy in finish_growing_bo() when
376 * we finally submit the batch, at which point we've finished uploading
377 * state, and nobody should have any old references anymore.
379 * To do that, we keep a reference to the old BO in grow->partial_bo,
380 * and store the number of bytes to copy in grow->partial_bytes. We
381 * can monkey with the refcounts directly without atomics because these
382 * are per-context BOs and they can only be touched by this thread.
384 assert(new_bo
->refcount
== 1);
385 new_bo
->refcount
= bo
->refcount
;
389 memcpy(&tmp
, bo
, sizeof(struct iris_bo
));
390 memcpy(bo
, new_bo
, sizeof(struct iris_bo
));
391 memcpy(new_bo
, &tmp
, sizeof(struct iris_bo
));
393 buf
->partial_bo
= new_bo
; /* the one reference of the OLD bo */
394 buf
->partial_bytes
= existing_bytes
;
398 require_buffer_space(struct iris_batch
*batch
,
399 struct iris_batch_buffer
*buf
,
401 unsigned flush_threshold
,
402 unsigned max_buffer_size
)
404 const unsigned required_bytes
= buffer_bytes_used(buf
) + size
;
406 if (!batch
->no_wrap
&& required_bytes
>= flush_threshold
) {
407 iris_batch_flush(batch
);
408 } else if (required_bytes
>= buf
->bo
->size
) {
409 grow_buffer(batch
, buf
,
410 MIN2(buf
->bo
->size
+ buf
->bo
->size
/ 2, max_buffer_size
));
411 assert(required_bytes
< buf
->bo
->size
);
417 iris_require_command_space(struct iris_batch
*batch
, unsigned size
)
419 require_buffer_space(batch
, &batch
->cmdbuf
, size
, BATCH_SZ
, MAX_BATCH_SIZE
);
423 iris_get_command_space(struct iris_batch
*batch
, unsigned bytes
)
425 iris_require_command_space(batch
, bytes
);
426 void *map
= batch
->cmdbuf
.map_next
;
427 batch
->cmdbuf
.map_next
+= bytes
;
432 iris_batch_emit(struct iris_batch
*batch
, const void *data
, unsigned size
)
434 void *map
= iris_get_command_space(batch
, size
);
435 memcpy(map
, data
, size
);
439 * Called from iris_batch_flush before emitting MI_BATCHBUFFER_END and
442 * This function can emit state (say, to preserve registers that aren't saved
446 iris_finish_batch(struct iris_batch
*batch
)
448 batch
->no_wrap
= true;
452 /* Emit MI_BATCH_BUFFER_END to finish our batch. Note that execbuf2
453 * requires our batch size to be QWord aligned, so we pad it out if
454 * necessary by emitting an extra MI_NOOP after the end.
456 const uint32_t MI_BATCH_BUFFER_END_AND_NOOP
[2] = { (0xA << 23), 0 };
457 const bool qword_aligned
= (buffer_bytes_used(&batch
->cmdbuf
) % 8) == 0;
458 iris_batch_emit(batch
, MI_BATCH_BUFFER_END_AND_NOOP
, qword_aligned
? 8 : 4);
460 batch
->no_wrap
= false;
464 submit_batch(struct iris_batch
*batch
, int in_fence_fd
, int *out_fence_fd
)
466 iris_bo_unmap(batch
->cmdbuf
.bo
);
468 /* The requirement for using I915_EXEC_NO_RELOC are:
470 * The addresses written in the objects must match the corresponding
471 * reloc.gtt_offset which in turn must match the corresponding
474 * Any render targets written to in the batch must be flagged with
477 * To avoid stalling, execobject.offset should match the current
478 * address of that object within the active context.
480 struct drm_i915_gem_execbuffer2 execbuf
= {
481 .buffers_ptr
= (uintptr_t) batch
->validation_list
,
482 .buffer_count
= batch
->exec_count
,
483 .batch_start_offset
= 0,
484 .batch_len
= buffer_bytes_used(&batch
->cmdbuf
),
485 .flags
= batch
->ring
|
487 I915_EXEC_BATCH_FIRST
|
488 I915_EXEC_HANDLE_LUT
,
489 .rsvd1
= batch
->hw_ctx_id
, /* rsvd1 is actually the context ID */
492 unsigned long cmd
= DRM_IOCTL_I915_GEM_EXECBUFFER2
;
494 if (in_fence_fd
!= -1) {
495 execbuf
.rsvd2
= in_fence_fd
;
496 execbuf
.flags
|= I915_EXEC_FENCE_IN
;
499 if (out_fence_fd
!= NULL
) {
500 cmd
= DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
;
502 execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
505 int ret
= drm_ioctl(batch
->screen
->fd
, cmd
, &execbuf
);
508 DBG("execbuf FAILED: errno = %d\n", -ret
);
510 DBG("execbuf succeeded\n");
513 for (int i
= 0; i
< batch
->exec_count
; i
++) {
514 struct iris_bo
*bo
= batch
->exec_bos
[i
];
520 if (ret
== 0 && out_fence_fd
!= NULL
)
521 *out_fence_fd
= execbuf
.rsvd2
>> 32;
527 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
530 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
531 * of the returned fd.
534 _iris_batch_flush_fence(struct iris_batch
*batch
,
535 int in_fence_fd
, int *out_fence_fd
,
536 const char *file
, int line
)
538 if (buffer_bytes_used(&batch
->cmdbuf
) == 0)
541 /* Check that we didn't just wrap our batchbuffer at a bad time. */
542 assert(!batch
->no_wrap
);
544 iris_finish_batch(batch
);
546 if (unlikely(INTEL_DEBUG
& (DEBUG_BATCH
| DEBUG_SUBMIT
))) {
547 int bytes_for_commands
= buffer_bytes_used(&batch
->cmdbuf
);
548 fprintf(stderr
, "%19s:%-3d: Batchbuffer flush with %5db (%0.1f%%), "
549 "%4d BOs (%0.1fMb aperture)\n",
551 bytes_for_commands
, 100.0f
* bytes_for_commands
/ BATCH_SZ
,
553 (float) batch
->aperture_space
/ (1024 * 1024));
554 dump_validation_list(batch
);
557 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
560 int ret
= submit_batch(batch
, in_fence_fd
, out_fence_fd
);
567 //if (iris->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
568 //iris_check_for_reset(ice);
570 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
571 dbg_printf("waiting for idle\n");
572 iris_bo_wait_rendering(batch
->cmdbuf
.bo
);
575 /* Clean up after the batch we submitted and prepare for a new one. */
576 for (int i
= 0; i
< batch
->exec_count
; i
++) {
577 iris_bo_unreference(batch
->exec_bos
[i
]);
578 batch
->exec_bos
[i
] = NULL
;
580 batch
->exec_count
= 0;
581 batch
->aperture_space
= 0;
583 /* Start a new batch buffer. */
584 iris_batch_reset_and_clear_render_cache(batch
);
590 iris_batch_references(struct iris_batch
*batch
, struct iris_bo
*bo
)
592 unsigned index
= READ_ONCE(bo
->index
);
593 if (index
< batch
->exec_count
&& batch
->exec_bos
[index
] == bo
)
596 for (int i
= 0; i
< batch
->exec_count
; i
++) {
597 if (batch
->exec_bos
[i
] == bo
)
603 /* This is the only way buffers get added to the validate list.
606 iris_use_pinned_bo(struct iris_batch
*batch
,
610 assert(bo
->kflags
& EXEC_OBJECT_PINNED
);
611 unsigned index
= add_exec_bo(batch
, bo
);
613 batch
->validation_list
[index
].flags
|= EXEC_OBJECT_WRITE
;
617 decode_batch(struct iris_batch
*batch
)
619 gen_print_batch(&batch
->decoder
, batch
->cmdbuf
.map
,
620 buffer_bytes_used(&batch
->cmdbuf
),
621 batch
->cmdbuf
.bo
->gtt_offset
);