i965: Rework brw_new_batch to actually start a new batch.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_batchbuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_batchbuffer.h"
29 #include "intel_buffer_objects.h"
30 #include "intel_reg.h"
31 #include "intel_bufmgr.h"
32 #include "intel_buffers.h"
33 #include "brw_context.h"
34
35 static void
36 intel_batchbuffer_reset(struct brw_context *brw);
37
38 struct cached_batch_item {
39 struct cached_batch_item *next;
40 uint16_t header;
41 uint16_t size;
42 };
43
44 void
45 intel_batchbuffer_clear_cache(struct brw_context *brw)
46 {
47 struct cached_batch_item *item = brw->batch.cached_items;
48
49 while (item) {
50 struct cached_batch_item *next = item->next;
51 free(item);
52 item = next;
53 }
54
55 brw->batch.cached_items = NULL;
56 }
57
58 void
59 intel_batchbuffer_init(struct brw_context *brw)
60 {
61 intel_batchbuffer_reset(brw);
62
63 if (brw->gen >= 6) {
64 /* We can't just use brw_state_batch to get a chunk of space for
65 * the gen6 workaround because it involves actually writing to
66 * the buffer, and the kernel doesn't let us write to the batch.
67 */
68 brw->batch.workaround_bo = drm_intel_bo_alloc(brw->bufmgr,
69 "pipe_control workaround",
70 4096, 4096);
71 }
72
73 brw->batch.need_workaround_flush = true;
74
75 if (!brw->has_llc) {
76 brw->batch.cpu_map = malloc(BATCH_SZ);
77 brw->batch.map = brw->batch.cpu_map;
78 }
79 }
80
81 static void
82 intel_batchbuffer_reset(struct brw_context *brw)
83 {
84 if (brw->batch.last_bo != NULL) {
85 drm_intel_bo_unreference(brw->batch.last_bo);
86 brw->batch.last_bo = NULL;
87 }
88 brw->batch.last_bo = brw->batch.bo;
89
90 intel_batchbuffer_clear_cache(brw);
91
92 brw->batch.bo = drm_intel_bo_alloc(brw->bufmgr, "batchbuffer",
93 BATCH_SZ, 4096);
94 if (brw->has_llc) {
95 drm_intel_bo_map(brw->batch.bo, true);
96 brw->batch.map = brw->batch.bo->virtual;
97 }
98
99 brw->batch.reserved_space = BATCH_RESERVED;
100 brw->batch.state_batch_offset = brw->batch.bo->size;
101 brw->batch.used = 0;
102 brw->batch.needs_sol_reset = false;
103 }
104
105 void
106 intel_batchbuffer_save_state(struct brw_context *brw)
107 {
108 brw->batch.saved.used = brw->batch.used;
109 brw->batch.saved.reloc_count =
110 drm_intel_gem_bo_get_reloc_count(brw->batch.bo);
111 }
112
113 void
114 intel_batchbuffer_reset_to_saved(struct brw_context *brw)
115 {
116 drm_intel_gem_bo_clear_relocs(brw->batch.bo, brw->batch.saved.reloc_count);
117
118 brw->batch.used = brw->batch.saved.used;
119
120 /* Cached batch state is dead, since we just cleared some unknown part of the
121 * batchbuffer. Assume that the caller resets any other state necessary.
122 */
123 intel_batchbuffer_clear_cache(brw);
124 }
125
126 void
127 intel_batchbuffer_free(struct brw_context *brw)
128 {
129 free(brw->batch.cpu_map);
130 drm_intel_bo_unreference(brw->batch.last_bo);
131 drm_intel_bo_unreference(brw->batch.bo);
132 drm_intel_bo_unreference(brw->batch.workaround_bo);
133 intel_batchbuffer_clear_cache(brw);
134 }
135
136 static void
137 do_batch_dump(struct brw_context *brw)
138 {
139 struct drm_intel_decode *decode;
140 struct intel_batchbuffer *batch = &brw->batch;
141 int ret;
142
143 decode = drm_intel_decode_context_alloc(brw->intelScreen->deviceID);
144 if (!decode)
145 return;
146
147 ret = drm_intel_bo_map(batch->bo, false);
148 if (ret == 0) {
149 drm_intel_decode_set_batch_pointer(decode,
150 batch->bo->virtual,
151 batch->bo->offset,
152 batch->used);
153 } else {
154 fprintf(stderr,
155 "WARNING: failed to map batchbuffer (%s), "
156 "dumping uploaded data instead.\n", strerror(ret));
157
158 drm_intel_decode_set_batch_pointer(decode,
159 batch->map,
160 batch->bo->offset,
161 batch->used);
162 }
163
164 drm_intel_decode(decode);
165
166 drm_intel_decode_context_free(decode);
167
168 if (ret == 0) {
169 drm_intel_bo_unmap(batch->bo);
170
171 brw_debug_batch(brw);
172 }
173 }
174
175 /**
176 * Called when starting a new batch buffer.
177 */
178 static void
179 brw_new_batch(struct brw_context *brw)
180 {
181 /* Create a new batchbuffer and reset the associated state: */
182 intel_batchbuffer_reset(brw);
183
184 /* If the kernel supports hardware contexts, then most hardware state is
185 * preserved between batches; we only need to re-emit state that is required
186 * to be in every batch. Otherwise we need to re-emit all the state that
187 * would otherwise be stored in the context (which for all intents and
188 * purposes means everything).
189 */
190 if (brw->hw_ctx == NULL)
191 brw->state.dirty.brw |= BRW_NEW_CONTEXT;
192
193 brw->state.dirty.brw |= BRW_NEW_BATCH;
194
195 /* Assume that the last command before the start of our batch was a
196 * primitive, for safety.
197 */
198 brw->batch.need_workaround_flush = true;
199
200 brw->state_batch_count = 0;
201
202 brw->ib.type = -1;
203
204 /* We need to periodically reap the shader time results, because rollover
205 * happens every few seconds. We also want to see results every once in a
206 * while, because many programs won't cleanly destroy our context, so the
207 * end-of-run printout may not happen.
208 */
209 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
210 brw_collect_and_report_shader_time(brw);
211 }
212
213 /**
214 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
215 * sending it off.
216 *
217 * This function can emit state (say, to preserve registers that aren't saved
218 * between batches). All of this state MUST fit in the reserved space at the
219 * end of the batchbuffer. If you add more GPU state, increase the reserved
220 * space by updating the BATCH_RESERVED macro.
221 */
222 static void
223 brw_finish_batch(struct brw_context *brw)
224 {
225 brw_emit_query_end(brw);
226
227 if (brw->curbe.curbe_bo) {
228 drm_intel_gem_bo_unmap_gtt(brw->curbe.curbe_bo);
229 drm_intel_bo_unreference(brw->curbe.curbe_bo);
230 brw->curbe.curbe_bo = NULL;
231 }
232
233 /* Mark that the current program cache BO has been used by the GPU.
234 * It will be reallocated if we need to put new programs in for the
235 * next batch.
236 */
237 brw->cache.bo_used_by_gpu = true;
238 }
239
240 /* TODO: Push this whole function into bufmgr.
241 */
242 static int
243 do_flush_locked(struct brw_context *brw)
244 {
245 struct intel_batchbuffer *batch = &brw->batch;
246 int ret = 0;
247
248 if (brw->has_llc) {
249 drm_intel_bo_unmap(batch->bo);
250 } else {
251 ret = drm_intel_bo_subdata(batch->bo, 0, 4*batch->used, batch->map);
252 if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
253 ret = drm_intel_bo_subdata(batch->bo,
254 batch->state_batch_offset,
255 batch->bo->size - batch->state_batch_offset,
256 (char *)batch->map + batch->state_batch_offset);
257 }
258 }
259
260 if (!brw->intelScreen->no_hw) {
261 int flags;
262
263 if (brw->gen < 6 || !batch->is_blit) {
264 flags = I915_EXEC_RENDER;
265 } else {
266 flags = I915_EXEC_BLT;
267 }
268
269 if (batch->needs_sol_reset)
270 flags |= I915_EXEC_GEN7_SOL_RESET;
271
272 if (ret == 0) {
273 if (unlikely(INTEL_DEBUG & DEBUG_AUB))
274 brw_annotate_aub(brw);
275 if (brw->hw_ctx == NULL || batch->is_blit) {
276 ret = drm_intel_bo_mrb_exec(batch->bo, 4 * batch->used, NULL, 0, 0,
277 flags);
278 } else {
279 ret = drm_intel_gem_bo_context_exec(batch->bo, brw->hw_ctx,
280 4 * batch->used, flags);
281 }
282 }
283 }
284
285 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
286 do_batch_dump(brw);
287
288 if (ret != 0) {
289 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
290 exit(1);
291 }
292
293 return ret;
294 }
295
296 int
297 _intel_batchbuffer_flush(struct brw_context *brw,
298 const char *file, int line)
299 {
300 int ret;
301
302 if (brw->batch.used == 0)
303 return 0;
304
305 if (brw->first_post_swapbuffers_batch == NULL) {
306 brw->first_post_swapbuffers_batch = brw->batch.bo;
307 drm_intel_bo_reference(brw->first_post_swapbuffers_batch);
308 }
309
310 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
311 int bytes_for_commands = 4 * brw->batch.used;
312 int bytes_for_state = brw->batch.bo->size - brw->batch.state_batch_offset;
313 int total_bytes = bytes_for_commands + bytes_for_state;
314 fprintf(stderr, "%s:%d: Batchbuffer flush with %4db (pkt) + "
315 "%4db (state) = %4db (%0.1f%%)\n", file, line,
316 bytes_for_commands, bytes_for_state,
317 total_bytes,
318 100.0f * total_bytes / BATCH_SZ);
319 }
320
321 brw->batch.reserved_space = 0;
322
323 brw_finish_batch(brw);
324
325 /* Mark the end of the buffer. */
326 intel_batchbuffer_emit_dword(brw, MI_BATCH_BUFFER_END);
327 if (brw->batch.used & 1) {
328 /* Round batchbuffer usage to 2 DWORDs. */
329 intel_batchbuffer_emit_dword(brw, MI_NOOP);
330 }
331
332 intel_upload_finish(brw);
333
334 /* Check that we didn't just wrap our batchbuffer at a bad time. */
335 assert(!brw->no_batch_wrap);
336
337 ret = do_flush_locked(brw);
338
339 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
340 fprintf(stderr, "waiting for idle\n");
341 drm_intel_bo_wait_rendering(brw->batch.bo);
342 }
343
344 /* Start a new batch buffer. */
345 brw_new_batch(brw);
346
347 return ret;
348 }
349
350
351 /* This is the only way buffers get added to the validate list.
352 */
353 bool
354 intel_batchbuffer_emit_reloc(struct brw_context *brw,
355 drm_intel_bo *buffer,
356 uint32_t read_domains, uint32_t write_domain,
357 uint32_t delta)
358 {
359 int ret;
360
361 ret = drm_intel_bo_emit_reloc(brw->batch.bo, 4*brw->batch.used,
362 buffer, delta,
363 read_domains, write_domain);
364 assert(ret == 0);
365 (void)ret;
366
367 /*
368 * Using the old buffer offset, write in what the right data would be, in case
369 * the buffer doesn't move and we can short-circuit the relocation processing
370 * in the kernel
371 */
372 intel_batchbuffer_emit_dword(brw, buffer->offset + delta);
373
374 return true;
375 }
376
377 bool
378 intel_batchbuffer_emit_reloc_fenced(struct brw_context *brw,
379 drm_intel_bo *buffer,
380 uint32_t read_domains,
381 uint32_t write_domain,
382 uint32_t delta)
383 {
384 int ret;
385
386 ret = drm_intel_bo_emit_reloc_fence(brw->batch.bo, 4*brw->batch.used,
387 buffer, delta,
388 read_domains, write_domain);
389 assert(ret == 0);
390 (void)ret;
391
392 /*
393 * Using the old buffer offset, write in what the right data would
394 * be, in case the buffer doesn't move and we can short-circuit the
395 * relocation processing in the kernel
396 */
397 intel_batchbuffer_emit_dword(brw, buffer->offset + delta);
398
399 return true;
400 }
401
402 void
403 intel_batchbuffer_data(struct brw_context *brw,
404 const void *data, GLuint bytes, bool is_blit)
405 {
406 assert((bytes & 3) == 0);
407 intel_batchbuffer_require_space(brw, bytes, is_blit);
408 __memcpy(brw->batch.map + brw->batch.used, data, bytes);
409 brw->batch.used += bytes >> 2;
410 }
411
412 void
413 intel_batchbuffer_cached_advance(struct brw_context *brw)
414 {
415 struct cached_batch_item **prev = &brw->batch.cached_items, *item;
416 uint32_t sz = (brw->batch.used - brw->batch.emit) * sizeof(uint32_t);
417 uint32_t *start = brw->batch.map + brw->batch.emit;
418 uint16_t op = *start >> 16;
419
420 while (*prev) {
421 uint32_t *old;
422
423 item = *prev;
424 old = brw->batch.map + item->header;
425 if (op == *old >> 16) {
426 if (item->size == sz && memcmp(old, start, sz) == 0) {
427 if (prev != &brw->batch.cached_items) {
428 *prev = item->next;
429 item->next = brw->batch.cached_items;
430 brw->batch.cached_items = item;
431 }
432 brw->batch.used = brw->batch.emit;
433 return;
434 }
435
436 goto emit;
437 }
438 prev = &item->next;
439 }
440
441 item = malloc(sizeof(struct cached_batch_item));
442 if (item == NULL)
443 return;
444
445 item->next = brw->batch.cached_items;
446 brw->batch.cached_items = item;
447
448 emit:
449 item->size = sz;
450 item->header = brw->batch.emit;
451 }
452
453 /**
454 * Restriction [DevSNB, DevIVB]:
455 *
456 * Prior to changing Depth/Stencil Buffer state (i.e. any combination of
457 * 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS, 3DSTATE_STENCIL_BUFFER,
458 * 3DSTATE_HIER_DEPTH_BUFFER) SW must first issue a pipelined depth stall
459 * (PIPE_CONTROL with Depth Stall bit set), followed by a pipelined depth
460 * cache flush (PIPE_CONTROL with Depth Flush Bit set), followed by
461 * another pipelined depth stall (PIPE_CONTROL with Depth Stall bit set),
462 * unless SW can otherwise guarantee that the pipeline from WM onwards is
463 * already flushed (e.g., via a preceding MI_FLUSH).
464 */
465 void
466 intel_emit_depth_stall_flushes(struct brw_context *brw)
467 {
468 assert(brw->gen >= 6 && brw->gen <= 7);
469
470 BEGIN_BATCH(4);
471 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
472 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL);
473 OUT_BATCH(0); /* address */
474 OUT_BATCH(0); /* write data */
475 ADVANCE_BATCH()
476
477 BEGIN_BATCH(4);
478 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
479 OUT_BATCH(PIPE_CONTROL_DEPTH_CACHE_FLUSH);
480 OUT_BATCH(0); /* address */
481 OUT_BATCH(0); /* write data */
482 ADVANCE_BATCH();
483
484 BEGIN_BATCH(4);
485 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
486 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL);
487 OUT_BATCH(0); /* address */
488 OUT_BATCH(0); /* write data */
489 ADVANCE_BATCH();
490 }
491
492 /**
493 * From the Ivybridge PRM, Volume 2 Part 1, Section 3.2 (VS Stage Input):
494 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
495 * stall needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
496 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
497 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL needs
498 * to be sent before any combination of VS associated 3DSTATE."
499 */
500 void
501 gen7_emit_vs_workaround_flush(struct brw_context *brw)
502 {
503 assert(brw->gen == 7);
504
505 BEGIN_BATCH(4);
506 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
507 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL | PIPE_CONTROL_WRITE_IMMEDIATE);
508 OUT_RELOC(brw->batch.workaround_bo,
509 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION, 0);
510 OUT_BATCH(0); /* write data */
511 ADVANCE_BATCH();
512 }
513
514 /**
515 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
516 * implementing two workarounds on gen6. From section 1.4.7.1
517 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
518 *
519 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
520 * produced by non-pipelined state commands), software needs to first
521 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
522 * 0.
523 *
524 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
525 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
526 *
527 * And the workaround for these two requires this workaround first:
528 *
529 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
530 * BEFORE the pipe-control with a post-sync op and no write-cache
531 * flushes.
532 *
533 * And this last workaround is tricky because of the requirements on
534 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
535 * volume 2 part 1:
536 *
537 * "1 of the following must also be set:
538 * - Render Target Cache Flush Enable ([12] of DW1)
539 * - Depth Cache Flush Enable ([0] of DW1)
540 * - Stall at Pixel Scoreboard ([1] of DW1)
541 * - Depth Stall ([13] of DW1)
542 * - Post-Sync Operation ([13] of DW1)
543 * - Notify Enable ([8] of DW1)"
544 *
545 * The cache flushes require the workaround flush that triggered this
546 * one, so we can't use it. Depth stall would trigger the same.
547 * Post-sync nonzero is what triggered this second workaround, so we
548 * can't use that one either. Notify enable is IRQs, which aren't
549 * really our business. That leaves only stall at scoreboard.
550 */
551 void
552 intel_emit_post_sync_nonzero_flush(struct brw_context *brw)
553 {
554 if (!brw->batch.need_workaround_flush)
555 return;
556
557 BEGIN_BATCH(4);
558 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
559 OUT_BATCH(PIPE_CONTROL_CS_STALL |
560 PIPE_CONTROL_STALL_AT_SCOREBOARD);
561 OUT_BATCH(0); /* address */
562 OUT_BATCH(0); /* write data */
563 ADVANCE_BATCH();
564
565 BEGIN_BATCH(4);
566 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
567 OUT_BATCH(PIPE_CONTROL_WRITE_IMMEDIATE);
568 OUT_RELOC(brw->batch.workaround_bo,
569 I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION, 0);
570 OUT_BATCH(0); /* write data */
571 ADVANCE_BATCH();
572
573 brw->batch.need_workaround_flush = false;
574 }
575
576 /* Emit a pipelined flush to either flush render and texture cache for
577 * reading from a FBO-drawn texture, or flush so that frontbuffer
578 * render appears on the screen in DRI1.
579 *
580 * This is also used for the always_flush_cache driconf debug option.
581 */
582 void
583 intel_batchbuffer_emit_mi_flush(struct brw_context *brw)
584 {
585 if (brw->gen >= 6) {
586 if (brw->batch.is_blit) {
587 BEGIN_BATCH_BLT(4);
588 OUT_BATCH(MI_FLUSH_DW);
589 OUT_BATCH(0);
590 OUT_BATCH(0);
591 OUT_BATCH(0);
592 ADVANCE_BATCH();
593 } else {
594 if (brw->gen == 6) {
595 /* Hardware workaround: SNB B-Spec says:
596 *
597 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache
598 * Flush Enable =1, a PIPE_CONTROL with any non-zero
599 * post-sync-op is required.
600 */
601 intel_emit_post_sync_nonzero_flush(brw);
602 }
603
604 BEGIN_BATCH(4);
605 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2));
606 OUT_BATCH(PIPE_CONTROL_INSTRUCTION_FLUSH |
607 PIPE_CONTROL_WRITE_FLUSH |
608 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
609 PIPE_CONTROL_VF_CACHE_INVALIDATE |
610 PIPE_CONTROL_TC_FLUSH |
611 PIPE_CONTROL_NO_WRITE |
612 PIPE_CONTROL_CS_STALL);
613 OUT_BATCH(0); /* write address */
614 OUT_BATCH(0); /* write data */
615 ADVANCE_BATCH();
616 }
617 } else {
618 BEGIN_BATCH(4);
619 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (4 - 2) |
620 PIPE_CONTROL_WRITE_FLUSH |
621 PIPE_CONTROL_NO_WRITE);
622 OUT_BATCH(0); /* write address */
623 OUT_BATCH(0); /* write data */
624 OUT_BATCH(0); /* write data */
625 ADVANCE_BATCH();
626 }
627 }