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"
44 #include "drm-uapi/i915_drm.h"
46 #include "util/hash_table.h"
48 #include "main/macros.h"
53 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
55 /* Terminating the batch takes either 4 bytes for MI_BATCH_BUFFER_END
56 * or 12 bytes for MI_BATCH_BUFFER_START (when chaining). Plus, we may
57 * need an extra 4 bytes to pad out to the nearest QWord. So reserve 16.
59 #define BATCH_RESERVED 16
62 iris_batch_reset(struct iris_batch
*batch
);
65 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
68 dump_validation_list(struct iris_batch
*batch
)
70 fprintf(stderr
, "Validation list (length %d):\n", batch
->exec_count
);
72 for (int i
= 0; i
< batch
->exec_count
; i
++) {
73 uint64_t flags
= batch
->validation_list
[i
].flags
;
74 assert(batch
->validation_list
[i
].handle
==
75 batch
->exec_bos
[i
]->gem_handle
);
76 fprintf(stderr
, "[%2d]: %2d %-14s %p %-7s @ 0x%016llx (%"PRIu64
"B) - %d refs\n",
78 batch
->validation_list
[i
].handle
,
79 batch
->exec_bos
[i
]->name
,
81 (flags
& EXEC_OBJECT_WRITE
) ? "(write)" : "",
82 batch
->validation_list
[i
].offset
,
83 batch
->exec_bos
[i
]->size
,
84 batch
->exec_bos
[i
]->refcount
);
89 * Return BO information to the batch decoder (for debugging).
91 static struct gen_batch_decode_bo
92 decode_get_bo(void *v_batch
, uint64_t address
)
94 struct iris_batch
*batch
= v_batch
;
96 for (int i
= 0; i
< batch
->exec_count
; i
++) {
97 struct iris_bo
*bo
= batch
->exec_bos
[i
];
98 /* The decoder zeroes out the top 16 bits, so we need to as well */
99 uint64_t bo_address
= bo
->gtt_offset
& (~0ull >> 16);
101 if (address
>= bo_address
&& address
< bo_address
+ bo
->size
) {
102 return (struct gen_batch_decode_bo
) {
105 .map
= iris_bo_map(batch
->dbg
, bo
, MAP_READ
) +
106 (address
- bo_address
),
111 return (struct gen_batch_decode_bo
) { };
115 * Decode the current batch.
118 decode_batch(struct iris_batch
*batch
)
120 void *map
= iris_bo_map(batch
->dbg
, batch
->exec_bos
[0], MAP_READ
);
121 gen_print_batch(&batch
->decoder
, map
, batch
->primary_batch_size
,
122 batch
->exec_bos
[0]->gtt_offset
);
126 uint_key_compare(const void *a
, const void *b
)
132 uint_key_hash(const void *key
)
134 return (uintptr_t) key
;
138 iris_init_batch(struct iris_batch
*batch
,
139 struct iris_screen
*screen
,
140 struct iris_vtable
*vtbl
,
141 struct pipe_debug_callback
*dbg
,
144 batch
->screen
= screen
;
148 /* engine should be one of I915_EXEC_RENDER, I915_EXEC_BLT, etc. */
149 assert((engine
& ~I915_EXEC_RING_MASK
) == 0);
150 assert(util_bitcount(engine
) == 1);
151 batch
->engine
= engine
;
153 batch
->exec_count
= 0;
154 batch
->exec_array_size
= 100;
156 malloc(batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
157 batch
->validation_list
=
158 malloc(batch
->exec_array_size
* sizeof(batch
->validation_list
[0]));
160 batch
->cache
.render
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
161 _mesa_key_pointer_equal
);
162 batch
->cache
.depth
= _mesa_set_create(NULL
, _mesa_hash_pointer
,
163 _mesa_key_pointer_equal
);
164 if (unlikely(INTEL_DEBUG
)) {
166 _mesa_hash_table_create(NULL
, uint_key_hash
, uint_key_compare
);
168 const unsigned decode_flags
=
169 GEN_BATCH_DECODE_FULL
|
170 ((INTEL_DEBUG
& DEBUG_COLOR
) ? GEN_BATCH_DECODE_IN_COLOR
: 0) |
171 GEN_BATCH_DECODE_OFFSETS
|
172 GEN_BATCH_DECODE_FLOATS
;
174 gen_batch_decode_ctx_init(&batch
->decoder
, &screen
->devinfo
,
175 stderr
, decode_flags
, NULL
,
176 decode_get_bo
, NULL
, batch
);
177 batch
->decoder
.max_vbo_decoded_lines
= 32;
180 iris_batch_reset(batch
);
183 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
186 add_exec_bo(struct iris_batch
*batch
, struct iris_bo
*bo
)
188 unsigned index
= READ_ONCE(bo
->index
);
190 if (index
< batch
->exec_count
&& batch
->exec_bos
[index
] == bo
)
193 /* May have been shared between multiple active batches */
194 for (index
= 0; index
< batch
->exec_count
; index
++) {
195 if (batch
->exec_bos
[index
] == bo
)
199 iris_bo_reference(bo
);
201 if (batch
->exec_count
== batch
->exec_array_size
) {
202 batch
->exec_array_size
*= 2;
204 realloc(batch
->exec_bos
,
205 batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
206 batch
->validation_list
=
207 realloc(batch
->validation_list
,
208 batch
->exec_array_size
* sizeof(batch
->validation_list
[0]));
211 batch
->validation_list
[batch
->exec_count
] =
212 (struct drm_i915_gem_exec_object2
) {
213 .handle
= bo
->gem_handle
,
214 .offset
= bo
->gtt_offset
,
218 bo
->index
= batch
->exec_count
;
219 batch
->exec_bos
[batch
->exec_count
] = bo
;
220 batch
->aperture_space
+= bo
->size
;
222 return batch
->exec_count
++;
226 create_batch(struct iris_batch
*batch
)
228 struct iris_screen
*screen
= batch
->screen
;
229 struct iris_bufmgr
*bufmgr
= screen
->bufmgr
;
231 batch
->bo
= iris_bo_alloc(bufmgr
, "command buffer",
232 BATCH_SZ
+ BATCH_RESERVED
, IRIS_MEMZONE_OTHER
);
233 batch
->bo
->kflags
|= EXEC_OBJECT_CAPTURE
;
234 batch
->map
= iris_bo_map(NULL
, batch
->bo
, MAP_READ
| MAP_WRITE
);
235 batch
->map_next
= batch
->map
;
236 batch
->contains_draw
= false;
238 add_exec_bo(batch
, batch
->bo
);
242 iris_batch_reset(struct iris_batch
*batch
)
244 if (batch
->last_bo
!= NULL
) {
245 iris_bo_unreference(batch
->last_bo
);
246 batch
->last_bo
= NULL
;
248 batch
->last_bo
= batch
->bo
;
249 batch
->primary_batch_size
= 0;
252 assert(batch
->bo
->index
== 0);
254 if (batch
->state_sizes
)
255 _mesa_hash_table_clear(batch
->state_sizes
, NULL
);
257 iris_cache_sets_clear(batch
);
261 iris_batch_free(struct iris_batch
*batch
)
263 for (int i
= 0; i
< batch
->exec_count
; i
++) {
264 iris_bo_unreference(batch
->exec_bos
[i
]);
266 free(batch
->exec_bos
);
267 free(batch
->validation_list
);
268 iris_bo_unreference(batch
->bo
);
271 batch
->map_next
= NULL
;
273 iris_bo_unreference(batch
->last_bo
);
275 _mesa_hash_table_destroy(batch
->cache
.render
, NULL
);
276 _mesa_set_destroy(batch
->cache
.depth
, NULL
);
278 if (batch
->state_sizes
) {
279 _mesa_hash_table_destroy(batch
->state_sizes
, NULL
);
280 gen_batch_decode_ctx_finish(&batch
->decoder
);
285 * If we've chained to a secondary batch, or are getting near to the end,
286 * then flush. This should only be called between draws.
289 iris_batch_maybe_flush(struct iris_batch
*batch
, unsigned estimate
)
291 if (batch
->bo
!= batch
->exec_bos
[0] ||
292 iris_batch_bytes_used(batch
) + estimate
>= BATCH_SZ
) {
293 iris_batch_flush(batch
);
298 iris_chain_to_new_batch(struct iris_batch
*batch
)
300 /* We only support chaining a single time. */
301 assert(batch
->bo
== batch
->exec_bos
[0]);
303 uint32_t *cmd
= batch
->map_next
;
304 uint64_t *addr
= batch
->map_next
+ 4;
305 batch
->map_next
+= 8;
307 /* No longer held by batch->bo, still held by validation list */
308 iris_bo_unreference(batch
->bo
);
309 batch
->primary_batch_size
= iris_batch_bytes_used(batch
);
312 /* Emit MI_BATCH_BUFFER_START to chain to another batch. */
313 *cmd
= (0x31 << 23) | (1 << 8) | (3 - 2);
314 *addr
= batch
->bo
->gtt_offset
;
318 * Terminate a batch with MI_BATCH_BUFFER_END.
321 iris_finish_batch(struct iris_batch
*batch
)
325 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
326 uint32_t *map
= batch
->map_next
;
328 map
[0] = (0xA << 23);
330 batch
->map_next
+= 4;
332 if (batch
->bo
== batch
->exec_bos
[0])
333 batch
->primary_batch_size
= iris_batch_bytes_used(batch
);
337 * Submit the batch to the GPU via execbuffer2.
340 submit_batch(struct iris_batch
*batch
, int in_fence_fd
, int *out_fence_fd
)
342 iris_bo_unmap(batch
->bo
);
344 /* The requirement for using I915_EXEC_NO_RELOC are:
346 * The addresses written in the objects must match the corresponding
347 * reloc.gtt_offset which in turn must match the corresponding
350 * Any render targets written to in the batch must be flagged with
353 * To avoid stalling, execobject.offset should match the current
354 * address of that object within the active context.
356 struct drm_i915_gem_execbuffer2 execbuf
= {
357 .buffers_ptr
= (uintptr_t) batch
->validation_list
,
358 .buffer_count
= batch
->exec_count
,
359 .batch_start_offset
= 0,
360 /* This must be QWord aligned. */
361 .batch_len
= ALIGN(batch
->primary_batch_size
, 8),
362 .flags
= batch
->engine
|
364 I915_EXEC_BATCH_FIRST
|
365 I915_EXEC_HANDLE_LUT
,
366 .rsvd1
= batch
->hw_ctx_id
, /* rsvd1 is actually the context ID */
369 unsigned long cmd
= DRM_IOCTL_I915_GEM_EXECBUFFER2
;
371 if (in_fence_fd
!= -1) {
372 execbuf
.rsvd2
= in_fence_fd
;
373 execbuf
.flags
|= I915_EXEC_FENCE_IN
;
376 if (out_fence_fd
!= NULL
) {
377 cmd
= DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
;
379 execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
382 int ret
= drm_ioctl(batch
->screen
->fd
, cmd
, &execbuf
);
385 DBG("execbuf FAILED: errno = %d\n", -ret
);
386 fprintf(stderr
, "execbuf FAILED: errno = %d\n", -ret
);
389 DBG("execbuf succeeded\n");
392 for (int i
= 0; i
< batch
->exec_count
; i
++) {
393 struct iris_bo
*bo
= batch
->exec_bos
[i
];
399 if (ret
== 0 && out_fence_fd
!= NULL
)
400 *out_fence_fd
= execbuf
.rsvd2
>> 32;
406 * Flush the batch buffer, submitting it to the GPU and resetting it so
407 * we're ready to emit the next batch.
409 * \param in_fence_fd is ignored if -1. Otherwise, this function takes
410 * ownership of the fd.
412 * \param out_fence_fd is ignored if NULL. Otherwise, the caller must
413 * take ownership of the returned fd.
416 _iris_batch_flush_fence(struct iris_batch
*batch
,
417 int in_fence_fd
, int *out_fence_fd
,
418 const char *file
, int line
)
420 if (iris_batch_bytes_used(batch
) == 0)
423 iris_finish_batch(batch
);
425 if (unlikely(INTEL_DEBUG
& (DEBUG_BATCH
| DEBUG_SUBMIT
))) {
426 int bytes_for_commands
= iris_batch_bytes_used(batch
);
427 int second_bytes
= 0;
428 if (batch
->bo
!= batch
->exec_bos
[0]) {
429 second_bytes
= bytes_for_commands
;
430 bytes_for_commands
+= batch
->primary_batch_size
;
432 fprintf(stderr
, "%19s:%-3d: Batchbuffer flush with %5d+%5db (%0.1f%%) "
433 "(cmds), %4d BOs (%0.1fMb aperture)\n",
435 batch
->primary_batch_size
, second_bytes
,
436 100.0f
* bytes_for_commands
/ BATCH_SZ
,
438 (float) batch
->aperture_space
/ (1024 * 1024));
439 dump_validation_list(batch
);
442 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
446 int ret
= submit_batch(batch
, in_fence_fd
, out_fence_fd
);
451 //if (iris->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
452 //iris_check_for_reset(ice);
454 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
455 dbg_printf("waiting for idle\n");
456 iris_bo_wait_rendering(batch
->bo
);
460 const bool color
= INTEL_DEBUG
& DEBUG_COLOR
;
461 fprintf(stderr
, "%siris: Failed to submit batchbuffer: %-80s%s\n",
462 color
? "\e[1;41m" : "", strerror(-ret
), color
? "\e[0m" : "");
467 /* Clean up after the batch we submitted and prepare for a new one. */
468 for (int i
= 0; i
< batch
->exec_count
; i
++) {
469 iris_bo_unreference(batch
->exec_bos
[i
]);
470 batch
->exec_bos
[i
] = NULL
;
472 batch
->exec_count
= 0;
473 batch
->aperture_space
= 0;
475 /* Start a new batch buffer. */
476 iris_batch_reset(batch
);
482 * Does the current batch refer to the given BO?
484 * (In other words, is the BO in the current batch's validation list?)
487 iris_batch_references(struct iris_batch
*batch
, struct iris_bo
*bo
)
489 unsigned index
= READ_ONCE(bo
->index
);
490 if (index
< batch
->exec_count
&& batch
->exec_bos
[index
] == bo
)
493 for (int i
= 0; i
< batch
->exec_count
; i
++) {
494 if (batch
->exec_bos
[i
] == bo
)
501 * Add a buffer to the current batch's validation list.
503 * You must call this on any BO you wish to use in this batch, to ensure
504 * that it's resident when the GPU commands execute.
507 iris_use_pinned_bo(struct iris_batch
*batch
,
511 assert(bo
->kflags
& EXEC_OBJECT_PINNED
);
512 unsigned index
= add_exec_bo(batch
, bo
);
514 batch
->validation_list
[index
].flags
|= EXEC_OBJECT_WRITE
;