iris: cross batch flushing
[mesa.git] / src / gallium / drivers / iris / iris_batch.c
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 iris_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 "iris_batch.h"
41 #include "iris_bufmgr.h"
42 #include "iris_context.h"
43
44 #include "drm-uapi/i915_drm.h"
45
46 #include "util/hash_table.h"
47 #include "util/set.h"
48 #include "main/macros.h"
49
50 #include <errno.h>
51 #include <xf86drm.h>
52
53 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
54
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.
58 */
59 #define BATCH_RESERVED 16
60
61 static void
62 iris_batch_reset(struct iris_batch *batch);
63
64 /**
65 * Debugging code to dump the validation list, used by INTEL_DEBUG=submit.
66 */
67 static void
68 dump_validation_list(struct iris_batch *batch)
69 {
70 fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
71
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",
77 i,
78 batch->validation_list[i].handle,
79 batch->exec_bos[i]->name,
80 batch->exec_bos[i],
81 (flags & EXEC_OBJECT_WRITE) ? "(write)" : "",
82 batch->validation_list[i].offset,
83 batch->exec_bos[i]->size,
84 batch->exec_bos[i]->refcount);
85 }
86 }
87
88 /**
89 * Return BO information to the batch decoder (for debugging).
90 */
91 static struct gen_batch_decode_bo
92 decode_get_bo(void *v_batch, uint64_t address)
93 {
94 struct iris_batch *batch = v_batch;
95
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);
100
101 if (address >= bo_address && address < bo_address + bo->size) {
102 return (struct gen_batch_decode_bo) {
103 .addr = address,
104 .size = bo->size,
105 .map = iris_bo_map(batch->dbg, bo, MAP_READ) +
106 (address - bo_address),
107 };
108 }
109 }
110
111 return (struct gen_batch_decode_bo) { };
112 }
113
114 /**
115 * Decode the current batch.
116 */
117 static void
118 decode_batch(struct iris_batch *batch)
119 {
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);
123 }
124
125 static bool
126 uint_key_compare(const void *a, const void *b)
127 {
128 return a == b;
129 }
130
131 static uint32_t
132 uint_key_hash(const void *key)
133 {
134 return (uintptr_t) key;
135 }
136
137 void
138 iris_init_batch(struct iris_batch *batch,
139 struct iris_screen *screen,
140 struct iris_vtable *vtbl,
141 struct pipe_debug_callback *dbg,
142 struct iris_batch **all_batches,
143 uint8_t engine)
144 {
145 batch->screen = screen;
146 batch->vtbl = vtbl;
147 batch->dbg = dbg;
148
149 /* engine should be one of I915_EXEC_RENDER, I915_EXEC_BLT, etc. */
150 assert((engine & ~I915_EXEC_RING_MASK) == 0);
151 assert(util_bitcount(engine) == 1);
152 batch->engine = engine;
153
154 batch->exec_count = 0;
155 batch->exec_array_size = 100;
156 batch->exec_bos =
157 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
158 batch->validation_list =
159 malloc(batch->exec_array_size * sizeof(batch->validation_list[0]));
160
161 batch->cache.render = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
162 _mesa_key_pointer_equal);
163 batch->cache.depth = _mesa_set_create(NULL, _mesa_hash_pointer,
164 _mesa_key_pointer_equal);
165
166 memset(batch->other_batches, 0, sizeof(batch->other_batches));
167
168 for (int i = 0, j = 0; i < IRIS_BATCH_COUNT; i++) {
169 if (all_batches[i] != batch)
170 batch->other_batches[j++] = all_batches[i];
171 }
172
173 if (unlikely(INTEL_DEBUG)) {
174 batch->state_sizes =
175 _mesa_hash_table_create(NULL, uint_key_hash, uint_key_compare);
176
177 const unsigned decode_flags =
178 GEN_BATCH_DECODE_FULL |
179 ((INTEL_DEBUG & DEBUG_COLOR) ? GEN_BATCH_DECODE_IN_COLOR : 0) |
180 GEN_BATCH_DECODE_OFFSETS |
181 GEN_BATCH_DECODE_FLOATS;
182
183 gen_batch_decode_ctx_init(&batch->decoder, &screen->devinfo,
184 stderr, decode_flags, NULL,
185 decode_get_bo, NULL, batch);
186 batch->decoder.max_vbo_decoded_lines = 32;
187 }
188
189 iris_batch_reset(batch);
190 }
191
192 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
193
194 static unsigned
195 add_exec_bo(struct iris_batch *batch, struct iris_bo *bo)
196 {
197 unsigned index = READ_ONCE(bo->index);
198
199 if (index < batch->exec_count && batch->exec_bos[index] == bo)
200 return index;
201
202 /* May have been shared between multiple active batches */
203 for (index = 0; index < batch->exec_count; index++) {
204 if (batch->exec_bos[index] == bo)
205 return index;
206 }
207
208 /* This is the first time our batch has seen this BO. Before we use it,
209 * we need to see if other batches reference it - if so, we should flush
210 * those first.
211 */
212 for (int b = 0; b < ARRAY_SIZE(batch->other_batches); b++) {
213 if (iris_batch_references(batch->other_batches[b], bo))
214 iris_batch_flush(batch->other_batches[b]);
215 }
216
217 /* Now, take a reference and add it to the validation list. */
218 iris_bo_reference(bo);
219
220 if (batch->exec_count == batch->exec_array_size) {
221 batch->exec_array_size *= 2;
222 batch->exec_bos =
223 realloc(batch->exec_bos,
224 batch->exec_array_size * sizeof(batch->exec_bos[0]));
225 batch->validation_list =
226 realloc(batch->validation_list,
227 batch->exec_array_size * sizeof(batch->validation_list[0]));
228 }
229
230 batch->validation_list[batch->exec_count] =
231 (struct drm_i915_gem_exec_object2) {
232 .handle = bo->gem_handle,
233 .offset = bo->gtt_offset,
234 .flags = bo->kflags,
235 };
236
237 bo->index = batch->exec_count;
238 batch->exec_bos[batch->exec_count] = bo;
239 batch->aperture_space += bo->size;
240
241 return batch->exec_count++;
242 }
243
244 static void
245 create_batch(struct iris_batch *batch)
246 {
247 struct iris_screen *screen = batch->screen;
248 struct iris_bufmgr *bufmgr = screen->bufmgr;
249
250 batch->bo = iris_bo_alloc(bufmgr, "command buffer",
251 BATCH_SZ + BATCH_RESERVED, IRIS_MEMZONE_OTHER);
252 batch->bo->kflags |= EXEC_OBJECT_CAPTURE;
253 batch->map = iris_bo_map(NULL, batch->bo, MAP_READ | MAP_WRITE);
254 batch->map_next = batch->map;
255
256 add_exec_bo(batch, batch->bo);
257 }
258
259 static void
260 iris_batch_reset(struct iris_batch *batch)
261 {
262 if (batch->last_bo != NULL) {
263 iris_bo_unreference(batch->last_bo);
264 batch->last_bo = NULL;
265 }
266 batch->last_bo = batch->bo;
267 batch->primary_batch_size = 0;
268 batch->contains_draw = false;
269
270 create_batch(batch);
271 assert(batch->bo->index == 0);
272
273 if (batch->state_sizes)
274 _mesa_hash_table_clear(batch->state_sizes, NULL);
275
276 iris_cache_sets_clear(batch);
277 }
278
279 void
280 iris_batch_free(struct iris_batch *batch)
281 {
282 for (int i = 0; i < batch->exec_count; i++) {
283 iris_bo_unreference(batch->exec_bos[i]);
284 }
285 free(batch->exec_bos);
286 free(batch->validation_list);
287 iris_bo_unreference(batch->bo);
288 batch->bo = NULL;
289 batch->map = NULL;
290 batch->map_next = NULL;
291
292 iris_bo_unreference(batch->last_bo);
293
294 _mesa_hash_table_destroy(batch->cache.render, NULL);
295 _mesa_set_destroy(batch->cache.depth, NULL);
296
297 if (batch->state_sizes) {
298 _mesa_hash_table_destroy(batch->state_sizes, NULL);
299 gen_batch_decode_ctx_finish(&batch->decoder);
300 }
301 }
302
303 /**
304 * If we've chained to a secondary batch, or are getting near to the end,
305 * then flush. This should only be called between draws.
306 */
307 void
308 iris_batch_maybe_flush(struct iris_batch *batch, unsigned estimate)
309 {
310 if (batch->bo != batch->exec_bos[0] ||
311 iris_batch_bytes_used(batch) + estimate >= BATCH_SZ) {
312 iris_batch_flush(batch);
313 }
314 }
315
316 void
317 iris_chain_to_new_batch(struct iris_batch *batch)
318 {
319 /* We only support chaining a single time. */
320 assert(batch->bo == batch->exec_bos[0]);
321
322 uint32_t *cmd = batch->map_next;
323 uint64_t *addr = batch->map_next + 4;
324 batch->map_next += 8;
325
326 /* No longer held by batch->bo, still held by validation list */
327 iris_bo_unreference(batch->bo);
328 batch->primary_batch_size = iris_batch_bytes_used(batch);
329 create_batch(batch);
330
331 /* Emit MI_BATCH_BUFFER_START to chain to another batch. */
332 *cmd = (0x31 << 23) | (1 << 8) | (3 - 2);
333 *addr = batch->bo->gtt_offset;
334 }
335
336 /**
337 * Terminate a batch with MI_BATCH_BUFFER_END.
338 */
339 static void
340 iris_finish_batch(struct iris_batch *batch)
341 {
342 // XXX: ISP DIS
343
344 /* Emit MI_BATCH_BUFFER_END to finish our batch. */
345 uint32_t *map = batch->map_next;
346
347 map[0] = (0xA << 23);
348
349 batch->map_next += 4;
350
351 if (batch->bo == batch->exec_bos[0])
352 batch->primary_batch_size = iris_batch_bytes_used(batch);
353 }
354
355 /**
356 * Submit the batch to the GPU via execbuffer2.
357 */
358 static int
359 submit_batch(struct iris_batch *batch, int in_fence_fd, int *out_fence_fd)
360 {
361 iris_bo_unmap(batch->bo);
362
363 /* The requirement for using I915_EXEC_NO_RELOC are:
364 *
365 * The addresses written in the objects must match the corresponding
366 * reloc.gtt_offset which in turn must match the corresponding
367 * execobject.offset.
368 *
369 * Any render targets written to in the batch must be flagged with
370 * EXEC_OBJECT_WRITE.
371 *
372 * To avoid stalling, execobject.offset should match the current
373 * address of that object within the active context.
374 */
375 struct drm_i915_gem_execbuffer2 execbuf = {
376 .buffers_ptr = (uintptr_t) batch->validation_list,
377 .buffer_count = batch->exec_count,
378 .batch_start_offset = 0,
379 /* This must be QWord aligned. */
380 .batch_len = ALIGN(batch->primary_batch_size, 8),
381 .flags = batch->engine |
382 I915_EXEC_NO_RELOC |
383 I915_EXEC_BATCH_FIRST |
384 I915_EXEC_HANDLE_LUT,
385 .rsvd1 = batch->hw_ctx_id, /* rsvd1 is actually the context ID */
386 };
387
388 unsigned long cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2;
389
390 if (in_fence_fd != -1) {
391 execbuf.rsvd2 = in_fence_fd;
392 execbuf.flags |= I915_EXEC_FENCE_IN;
393 }
394
395 if (out_fence_fd != NULL) {
396 cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2_WR;
397 *out_fence_fd = -1;
398 execbuf.flags |= I915_EXEC_FENCE_OUT;
399 }
400
401 int ret = drm_ioctl(batch->screen->fd, cmd, &execbuf);
402 if (ret != 0) {
403 ret = -errno;
404 DBG("execbuf FAILED: errno = %d\n", -ret);
405 fprintf(stderr, "execbuf FAILED: errno = %d\n", -ret);
406 abort();
407 } else {
408 DBG("execbuf succeeded\n");
409 }
410
411 for (int i = 0; i < batch->exec_count; i++) {
412 struct iris_bo *bo = batch->exec_bos[i];
413
414 bo->idle = false;
415 bo->index = -1;
416 }
417
418 if (ret == 0 && out_fence_fd != NULL)
419 *out_fence_fd = execbuf.rsvd2 >> 32;
420
421 return ret;
422 }
423
424 /**
425 * Flush the batch buffer, submitting it to the GPU and resetting it so
426 * we're ready to emit the next batch.
427 *
428 * \param in_fence_fd is ignored if -1. Otherwise, this function takes
429 * ownership of the fd.
430 *
431 * \param out_fence_fd is ignored if NULL. Otherwise, the caller must
432 * take ownership of the returned fd.
433 */
434 int
435 _iris_batch_flush_fence(struct iris_batch *batch,
436 int in_fence_fd, int *out_fence_fd,
437 const char *file, int line)
438 {
439 if (iris_batch_bytes_used(batch) == 0)
440 return 0;
441
442 iris_finish_batch(batch);
443
444 if (unlikely(INTEL_DEBUG & (DEBUG_BATCH | DEBUG_SUBMIT))) {
445 int bytes_for_commands = iris_batch_bytes_used(batch);
446 int second_bytes = 0;
447 if (batch->bo != batch->exec_bos[0]) {
448 second_bytes = bytes_for_commands;
449 bytes_for_commands += batch->primary_batch_size;
450 }
451 fprintf(stderr, "%19s:%-3d: Batchbuffer flush with %5d+%5db (%0.1f%%) "
452 "(cmds), %4d BOs (%0.1fMb aperture)\n",
453 file, line,
454 batch->primary_batch_size, second_bytes,
455 100.0f * bytes_for_commands / BATCH_SZ,
456 batch->exec_count,
457 (float) batch->aperture_space / (1024 * 1024));
458 dump_validation_list(batch);
459 }
460
461 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
462 decode_batch(batch);
463 }
464
465 int ret = submit_batch(batch, in_fence_fd, out_fence_fd);
466
467 //throttle(iris);
468
469 if (ret >= 0) {
470 //if (iris->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
471 //iris_check_for_reset(ice);
472
473 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
474 dbg_printf("waiting for idle\n");
475 iris_bo_wait_rendering(batch->bo);
476 }
477 } else {
478 #ifdef DEBUG
479 const bool color = INTEL_DEBUG & DEBUG_COLOR;
480 fprintf(stderr, "%siris: Failed to submit batchbuffer: %-80s%s\n",
481 color ? "\e[1;41m" : "", strerror(-ret), color ? "\e[0m" : "");
482 abort();
483 #endif
484 }
485
486 /* Clean up after the batch we submitted and prepare for a new one. */
487 for (int i = 0; i < batch->exec_count; i++) {
488 iris_bo_unreference(batch->exec_bos[i]);
489 batch->exec_bos[i] = NULL;
490 }
491 batch->exec_count = 0;
492 batch->aperture_space = 0;
493
494 /* Start a new batch buffer. */
495 iris_batch_reset(batch);
496
497 return 0;
498 }
499
500 /**
501 * Does the current batch refer to the given BO?
502 *
503 * (In other words, is the BO in the current batch's validation list?)
504 */
505 bool
506 iris_batch_references(struct iris_batch *batch, struct iris_bo *bo)
507 {
508 unsigned index = READ_ONCE(bo->index);
509 if (index < batch->exec_count && batch->exec_bos[index] == bo)
510 return true;
511
512 for (int i = 0; i < batch->exec_count; i++) {
513 if (batch->exec_bos[i] == bo)
514 return true;
515 }
516 return false;
517 }
518
519 /**
520 * Add a buffer to the current batch's validation list.
521 *
522 * You must call this on any BO you wish to use in this batch, to ensure
523 * that it's resident when the GPU commands execute.
524 */
525 void
526 iris_use_pinned_bo(struct iris_batch *batch,
527 struct iris_bo *bo,
528 bool writable)
529 {
530 assert(bo->kflags & EXEC_OBJECT_PINNED);
531 unsigned index = add_exec_bo(batch, bo);
532 if (writable)
533 batch->validation_list[index].flags |= EXEC_OBJECT_WRITE;
534 }