cba81bce7e216c0539e2be2a911a467710214592
[mesa.git] / src / mesa / drivers / dri / i965 / intel_batchbuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2006 VMware, Inc.
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 VMWARE 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 "intel_fbo.h"
34 #include "brw_context.h"
35
36 static void
37 intel_batchbuffer_reset(struct brw_context *brw);
38
39 void
40 intel_batchbuffer_init(struct brw_context *brw)
41 {
42 intel_batchbuffer_reset(brw);
43
44 if (brw->gen >= 6) {
45 /* We can't just use brw_state_batch to get a chunk of space for
46 * the gen6 workaround because it involves actually writing to
47 * the buffer, and the kernel doesn't let us write to the batch.
48 */
49 brw->batch.workaround_bo = drm_intel_bo_alloc(brw->bufmgr,
50 "pipe_control workaround",
51 4096, 4096);
52 }
53
54 brw->batch.need_workaround_flush = true;
55
56 if (!brw->has_llc) {
57 brw->batch.cpu_map = malloc(BATCH_SZ);
58 brw->batch.map = brw->batch.cpu_map;
59 }
60 }
61
62 static void
63 intel_batchbuffer_reset(struct brw_context *brw)
64 {
65 if (brw->batch.last_bo != NULL) {
66 drm_intel_bo_unreference(brw->batch.last_bo);
67 brw->batch.last_bo = NULL;
68 }
69 brw->batch.last_bo = brw->batch.bo;
70
71 brw_render_cache_set_clear(brw);
72
73 brw->batch.bo = drm_intel_bo_alloc(brw->bufmgr, "batchbuffer",
74 BATCH_SZ, 4096);
75 if (brw->has_llc) {
76 drm_intel_bo_map(brw->batch.bo, true);
77 brw->batch.map = brw->batch.bo->virtual;
78 }
79
80 brw->batch.reserved_space = BATCH_RESERVED;
81 brw->batch.state_batch_offset = brw->batch.bo->size;
82 brw->batch.used = 0;
83 brw->batch.needs_sol_reset = false;
84
85 /* We don't know what ring the new batch will be sent to until we see the
86 * first BEGIN_BATCH or BEGIN_BATCH_BLT. Mark it as unknown.
87 */
88 brw->batch.ring = UNKNOWN_RING;
89 }
90
91 void
92 intel_batchbuffer_save_state(struct brw_context *brw)
93 {
94 brw->batch.saved.used = brw->batch.used;
95 brw->batch.saved.reloc_count =
96 drm_intel_gem_bo_get_reloc_count(brw->batch.bo);
97 }
98
99 void
100 intel_batchbuffer_reset_to_saved(struct brw_context *brw)
101 {
102 drm_intel_gem_bo_clear_relocs(brw->batch.bo, brw->batch.saved.reloc_count);
103
104 brw->batch.used = brw->batch.saved.used;
105 if (brw->batch.used == 0)
106 brw->batch.ring = UNKNOWN_RING;
107 }
108
109 void
110 intel_batchbuffer_free(struct brw_context *brw)
111 {
112 free(brw->batch.cpu_map);
113 drm_intel_bo_unreference(brw->batch.last_bo);
114 drm_intel_bo_unreference(brw->batch.bo);
115 drm_intel_bo_unreference(brw->batch.workaround_bo);
116 }
117
118 static void
119 do_batch_dump(struct brw_context *brw)
120 {
121 struct drm_intel_decode *decode;
122 struct intel_batchbuffer *batch = &brw->batch;
123 int ret;
124
125 decode = drm_intel_decode_context_alloc(brw->intelScreen->deviceID);
126 if (!decode)
127 return;
128
129 ret = drm_intel_bo_map(batch->bo, false);
130 if (ret == 0) {
131 drm_intel_decode_set_batch_pointer(decode,
132 batch->bo->virtual,
133 batch->bo->offset64,
134 batch->used);
135 } else {
136 fprintf(stderr,
137 "WARNING: failed to map batchbuffer (%s), "
138 "dumping uploaded data instead.\n", strerror(ret));
139
140 drm_intel_decode_set_batch_pointer(decode,
141 batch->map,
142 batch->bo->offset64,
143 batch->used);
144 }
145
146 drm_intel_decode_set_output_file(decode, stderr);
147 drm_intel_decode(decode);
148
149 drm_intel_decode_context_free(decode);
150
151 if (ret == 0) {
152 drm_intel_bo_unmap(batch->bo);
153
154 brw_debug_batch(brw);
155 }
156 }
157
158 void
159 intel_batchbuffer_emit_render_ring_prelude(struct brw_context *brw)
160 {
161 /* We may need to enable and snapshot OA counters. */
162 brw_perf_monitor_new_batch(brw);
163 }
164
165 /**
166 * Called when starting a new batch buffer.
167 */
168 static void
169 brw_new_batch(struct brw_context *brw)
170 {
171 /* Create a new batchbuffer and reset the associated state: */
172 intel_batchbuffer_reset(brw);
173
174 /* If the kernel supports hardware contexts, then most hardware state is
175 * preserved between batches; we only need to re-emit state that is required
176 * to be in every batch. Otherwise we need to re-emit all the state that
177 * would otherwise be stored in the context (which for all intents and
178 * purposes means everything).
179 */
180 if (brw->hw_ctx == NULL)
181 brw->state.dirty.brw |= BRW_NEW_CONTEXT;
182
183 brw->state.dirty.brw |= BRW_NEW_BATCH;
184
185 /* Assume that the last command before the start of our batch was a
186 * primitive, for safety.
187 */
188 brw->batch.need_workaround_flush = true;
189
190 brw->state_batch_count = 0;
191
192 brw->ib.type = -1;
193
194 /* We need to periodically reap the shader time results, because rollover
195 * happens every few seconds. We also want to see results every once in a
196 * while, because many programs won't cleanly destroy our context, so the
197 * end-of-run printout may not happen.
198 */
199 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
200 brw_collect_and_report_shader_time(brw);
201
202 if (INTEL_DEBUG & DEBUG_PERFMON)
203 brw_dump_perf_monitors(brw);
204 }
205
206 /**
207 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
208 * sending it off.
209 *
210 * This function can emit state (say, to preserve registers that aren't saved
211 * between batches). All of this state MUST fit in the reserved space at the
212 * end of the batchbuffer. If you add more GPU state, increase the reserved
213 * space by updating the BATCH_RESERVED macro.
214 */
215 static void
216 brw_finish_batch(struct brw_context *brw)
217 {
218 /* Capture the closing pipeline statistics register values necessary to
219 * support query objects (in the non-hardware context world).
220 */
221 brw_emit_query_end(brw);
222
223 /* We may also need to snapshot and disable OA counters. */
224 if (brw->batch.ring == RENDER_RING)
225 brw_perf_monitor_finish_batch(brw);
226
227 if (brw->curbe.curbe_bo) {
228 drm_intel_bo_unreference(brw->curbe.curbe_bo);
229 brw->curbe.curbe_bo = NULL;
230 }
231
232 /* Mark that the current program cache BO has been used by the GPU.
233 * It will be reallocated if we need to put new programs in for the
234 * next batch.
235 */
236 brw->cache.bo_used_by_gpu = true;
237 }
238
239 /* TODO: Push this whole function into bufmgr.
240 */
241 static int
242 do_flush_locked(struct brw_context *brw)
243 {
244 struct intel_batchbuffer *batch = &brw->batch;
245 int ret = 0;
246
247 if (brw->has_llc) {
248 drm_intel_bo_unmap(batch->bo);
249 } else {
250 ret = drm_intel_bo_subdata(batch->bo, 0, 4*batch->used, batch->map);
251 if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
252 ret = drm_intel_bo_subdata(batch->bo,
253 batch->state_batch_offset,
254 batch->bo->size - batch->state_batch_offset,
255 (char *)batch->map + batch->state_batch_offset);
256 }
257 }
258
259 if (!brw->intelScreen->no_hw) {
260 int flags;
261
262 if (brw->gen >= 6 && batch->ring == BLT_RING) {
263 flags = I915_EXEC_BLT;
264 } else {
265 flags = I915_EXEC_RENDER;
266 }
267 if (batch->needs_sol_reset)
268 flags |= I915_EXEC_GEN7_SOL_RESET;
269
270 if (ret == 0) {
271 if (unlikely(INTEL_DEBUG & DEBUG_AUB))
272 brw_annotate_aub(brw);
273 if (brw->hw_ctx == NULL || batch->ring != RENDER_RING) {
274 ret = drm_intel_bo_mrb_exec(batch->bo, 4 * batch->used, NULL, 0, 0,
275 flags);
276 } else {
277 ret = drm_intel_gem_bo_context_exec(batch->bo, brw->hw_ctx,
278 4 * batch->used, flags);
279 }
280 }
281 }
282
283 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
284 do_batch_dump(brw);
285
286 if (ret != 0) {
287 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
288 exit(1);
289 }
290
291 return ret;
292 }
293
294 int
295 _intel_batchbuffer_flush(struct brw_context *brw,
296 const char *file, int line)
297 {
298 int ret;
299
300 if (brw->batch.used == 0)
301 return 0;
302
303 if (brw->first_post_swapbuffers_batch == NULL) {
304 brw->first_post_swapbuffers_batch = brw->batch.bo;
305 drm_intel_bo_reference(brw->first_post_swapbuffers_batch);
306 }
307
308 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
309 int bytes_for_commands = 4 * brw->batch.used;
310 int bytes_for_state = brw->batch.bo->size - brw->batch.state_batch_offset;
311 int total_bytes = bytes_for_commands + bytes_for_state;
312 fprintf(stderr, "%s:%d: Batchbuffer flush with %4db (pkt) + "
313 "%4db (state) = %4db (%0.1f%%)\n", file, line,
314 bytes_for_commands, bytes_for_state,
315 total_bytes,
316 100.0f * total_bytes / BATCH_SZ);
317 }
318
319 brw->batch.reserved_space = 0;
320
321 brw_finish_batch(brw);
322
323 /* Mark the end of the buffer. */
324 intel_batchbuffer_emit_dword(brw, MI_BATCH_BUFFER_END);
325 if (brw->batch.used & 1) {
326 /* Round batchbuffer usage to 2 DWORDs. */
327 intel_batchbuffer_emit_dword(brw, MI_NOOP);
328 }
329
330 intel_upload_finish(brw);
331
332 /* Check that we didn't just wrap our batchbuffer at a bad time. */
333 assert(!brw->no_batch_wrap);
334
335 ret = do_flush_locked(brw);
336
337 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
338 fprintf(stderr, "waiting for idle\n");
339 drm_intel_bo_wait_rendering(brw->batch.bo);
340 }
341
342 /* Start a new batch buffer. */
343 brw_new_batch(brw);
344
345 return ret;
346 }
347
348
349 /* This is the only way buffers get added to the validate list.
350 */
351 bool
352 intel_batchbuffer_emit_reloc(struct brw_context *brw,
353 drm_intel_bo *buffer,
354 uint32_t read_domains, uint32_t write_domain,
355 uint32_t delta)
356 {
357 int ret;
358
359 ret = drm_intel_bo_emit_reloc(brw->batch.bo, 4*brw->batch.used,
360 buffer, delta,
361 read_domains, write_domain);
362 assert(ret == 0);
363 (void)ret;
364
365 /*
366 * Using the old buffer offset, write in what the right data would be, in case
367 * the buffer doesn't move and we can short-circuit the relocation processing
368 * in the kernel
369 */
370 intel_batchbuffer_emit_dword(brw, buffer->offset64 + delta);
371
372 return true;
373 }
374
375 bool
376 intel_batchbuffer_emit_reloc64(struct brw_context *brw,
377 drm_intel_bo *buffer,
378 uint32_t read_domains, uint32_t write_domain,
379 uint32_t delta)
380 {
381 int ret = drm_intel_bo_emit_reloc(brw->batch.bo, 4*brw->batch.used,
382 buffer, delta,
383 read_domains, write_domain);
384 assert(ret == 0);
385 (void) ret;
386
387 /* Using the old buffer offset, write in what the right data would be, in
388 * case the buffer doesn't move and we can short-circuit the relocation
389 * processing in the kernel
390 */
391 uint64_t offset = buffer->offset64 + delta;
392 intel_batchbuffer_emit_dword(brw, offset);
393 intel_batchbuffer_emit_dword(brw, offset >> 32);
394
395 return true;
396 }
397
398
399 void
400 intel_batchbuffer_data(struct brw_context *brw,
401 const void *data, GLuint bytes, enum brw_gpu_ring ring)
402 {
403 assert((bytes & 3) == 0);
404 intel_batchbuffer_require_space(brw, bytes, ring);
405 __memcpy(brw->batch.map + brw->batch.used, data, bytes);
406 brw->batch.used += bytes >> 2;
407 }
408
409 /**
410 * According to the latest documentation, any PIPE_CONTROL with the
411 * "Command Streamer Stall" bit set must also have another bit set,
412 * with five different options:
413 *
414 * - Render Target Cache Flush
415 * - Depth Cache Flush
416 * - Stall at Pixel Scoreboard
417 * - Post-Sync Operation
418 * - Depth Stall
419 *
420 * I chose "Stall at Pixel Scoreboard" since we've used it effectively
421 * in the past, but the choice is fairly arbitrary.
422 */
423 static void
424 gen8_add_cs_stall_workaround_bits(uint32_t *flags)
425 {
426 uint32_t wa_bits = PIPE_CONTROL_WRITE_FLUSH |
427 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
428 PIPE_CONTROL_WRITE_IMMEDIATE |
429 PIPE_CONTROL_WRITE_DEPTH_COUNT |
430 PIPE_CONTROL_WRITE_TIMESTAMP |
431 PIPE_CONTROL_STALL_AT_SCOREBOARD |
432 PIPE_CONTROL_DEPTH_STALL;
433
434 /* If we're doing a CS stall, and don't already have one of the
435 * workaround bits set, add "Stall at Pixel Scoreboard."
436 */
437 if ((*flags & PIPE_CONTROL_CS_STALL) != 0 && (*flags & wa_bits) == 0)
438 *flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
439 }
440
441 /**
442 * Emit a PIPE_CONTROL with various flushing flags.
443 *
444 * The caller is responsible for deciding what flags are appropriate for the
445 * given generation.
446 */
447 void
448 brw_emit_pipe_control_flush(struct brw_context *brw, uint32_t flags)
449 {
450 if (brw->gen >= 8) {
451 gen8_add_cs_stall_workaround_bits(&flags);
452
453 BEGIN_BATCH(6);
454 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (6 - 2));
455 OUT_BATCH(flags);
456 OUT_BATCH(0);
457 OUT_BATCH(0);
458 OUT_BATCH(0);
459 OUT_BATCH(0);
460 ADVANCE_BATCH();
461 } else if (brw->gen >= 6) {
462 BEGIN_BATCH(5);
463 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
464 OUT_BATCH(flags);
465 OUT_BATCH(0);
466 OUT_BATCH(0);
467 OUT_BATCH(0);
468 ADVANCE_BATCH();
469 } else {
470 BEGIN_BATCH(4);
471 OUT_BATCH(_3DSTATE_PIPE_CONTROL | flags | (4 - 2));
472 OUT_BATCH(0);
473 OUT_BATCH(0);
474 OUT_BATCH(0);
475 ADVANCE_BATCH();
476 }
477 }
478
479 /**
480 * Emit a PIPE_CONTROL that writes to a buffer object.
481 *
482 * \p flags should contain one of the following items:
483 * - PIPE_CONTROL_WRITE_IMMEDIATE
484 * - PIPE_CONTROL_WRITE_TIMESTAMP
485 * - PIPE_CONTROL_WRITE_DEPTH_COUNT
486 */
487 void
488 brw_emit_pipe_control_write(struct brw_context *brw, uint32_t flags,
489 drm_intel_bo *bo, uint32_t offset,
490 uint32_t imm_lower, uint32_t imm_upper)
491 {
492 if (brw->gen >= 8) {
493 gen8_add_cs_stall_workaround_bits(&flags);
494
495 BEGIN_BATCH(6);
496 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (6 - 2));
497 OUT_BATCH(flags);
498 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
499 offset);
500 OUT_BATCH(imm_lower);
501 OUT_BATCH(imm_upper);
502 ADVANCE_BATCH();
503 } else if (brw->gen >= 6) {
504 /* PPGTT/GGTT is selected by DW2 bit 2 on Sandybridge, but DW1 bit 24
505 * on later platforms. We always use PPGTT on Gen7+.
506 */
507 unsigned gen6_gtt = brw->gen == 6 ? PIPE_CONTROL_GLOBAL_GTT_WRITE : 0;
508
509 BEGIN_BATCH(5);
510 OUT_BATCH(_3DSTATE_PIPE_CONTROL | (5 - 2));
511 OUT_BATCH(flags);
512 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
513 gen6_gtt | offset);
514 OUT_BATCH(imm_lower);
515 OUT_BATCH(imm_upper);
516 ADVANCE_BATCH();
517 } else {
518 BEGIN_BATCH(4);
519 OUT_BATCH(_3DSTATE_PIPE_CONTROL | flags | (4 - 2));
520 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
521 PIPE_CONTROL_GLOBAL_GTT_WRITE | offset);
522 OUT_BATCH(imm_lower);
523 OUT_BATCH(imm_upper);
524 ADVANCE_BATCH();
525 }
526 }
527
528 /**
529 * Restriction [DevSNB, DevIVB]:
530 *
531 * Prior to changing Depth/Stencil Buffer state (i.e. any combination of
532 * 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS, 3DSTATE_STENCIL_BUFFER,
533 * 3DSTATE_HIER_DEPTH_BUFFER) SW must first issue a pipelined depth stall
534 * (PIPE_CONTROL with Depth Stall bit set), followed by a pipelined depth
535 * cache flush (PIPE_CONTROL with Depth Flush Bit set), followed by
536 * another pipelined depth stall (PIPE_CONTROL with Depth Stall bit set),
537 * unless SW can otherwise guarantee that the pipeline from WM onwards is
538 * already flushed (e.g., via a preceding MI_FLUSH).
539 */
540 void
541 intel_emit_depth_stall_flushes(struct brw_context *brw)
542 {
543 assert(brw->gen >= 6 && brw->gen <= 8);
544
545 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_DEPTH_STALL);
546 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_DEPTH_CACHE_FLUSH);
547 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_DEPTH_STALL);
548 }
549
550 /**
551 * From the Ivybridge PRM, Volume 2 Part 1, Section 3.2 (VS Stage Input):
552 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
553 * stall needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
554 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
555 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL needs
556 * to be sent before any combination of VS associated 3DSTATE."
557 */
558 void
559 gen7_emit_vs_workaround_flush(struct brw_context *brw)
560 {
561 assert(brw->gen == 7);
562 brw_emit_pipe_control_write(brw,
563 PIPE_CONTROL_WRITE_IMMEDIATE
564 | PIPE_CONTROL_DEPTH_STALL,
565 brw->batch.workaround_bo, 0,
566 0, 0);
567 }
568
569
570 /**
571 * Emit a PIPE_CONTROL command for gen7 with the CS Stall bit set.
572 */
573 void
574 gen7_emit_cs_stall_flush(struct brw_context *brw)
575 {
576 brw_emit_pipe_control_write(brw,
577 PIPE_CONTROL_CS_STALL
578 | PIPE_CONTROL_WRITE_IMMEDIATE,
579 brw->batch.workaround_bo, 0,
580 0, 0);
581 }
582
583
584 /**
585 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
586 * implementing two workarounds on gen6. From section 1.4.7.1
587 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
588 *
589 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
590 * produced by non-pipelined state commands), software needs to first
591 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
592 * 0.
593 *
594 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
595 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
596 *
597 * And the workaround for these two requires this workaround first:
598 *
599 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
600 * BEFORE the pipe-control with a post-sync op and no write-cache
601 * flushes.
602 *
603 * And this last workaround is tricky because of the requirements on
604 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
605 * volume 2 part 1:
606 *
607 * "1 of the following must also be set:
608 * - Render Target Cache Flush Enable ([12] of DW1)
609 * - Depth Cache Flush Enable ([0] of DW1)
610 * - Stall at Pixel Scoreboard ([1] of DW1)
611 * - Depth Stall ([13] of DW1)
612 * - Post-Sync Operation ([13] of DW1)
613 * - Notify Enable ([8] of DW1)"
614 *
615 * The cache flushes require the workaround flush that triggered this
616 * one, so we can't use it. Depth stall would trigger the same.
617 * Post-sync nonzero is what triggered this second workaround, so we
618 * can't use that one either. Notify enable is IRQs, which aren't
619 * really our business. That leaves only stall at scoreboard.
620 */
621 void
622 intel_emit_post_sync_nonzero_flush(struct brw_context *brw)
623 {
624 if (!brw->batch.need_workaround_flush)
625 return;
626
627 brw_emit_pipe_control_flush(brw,
628 PIPE_CONTROL_CS_STALL |
629 PIPE_CONTROL_STALL_AT_SCOREBOARD);
630
631 brw_emit_pipe_control_write(brw, PIPE_CONTROL_WRITE_IMMEDIATE,
632 brw->batch.workaround_bo, 0, 0, 0);
633
634 brw->batch.need_workaround_flush = false;
635 }
636
637 /* Emit a pipelined flush to either flush render and texture cache for
638 * reading from a FBO-drawn texture, or flush so that frontbuffer
639 * render appears on the screen in DRI1.
640 *
641 * This is also used for the always_flush_cache driconf debug option.
642 */
643 void
644 intel_batchbuffer_emit_mi_flush(struct brw_context *brw)
645 {
646 if (brw->batch.ring == BLT_RING && brw->gen >= 6) {
647 BEGIN_BATCH_BLT(4);
648 OUT_BATCH(MI_FLUSH_DW);
649 OUT_BATCH(0);
650 OUT_BATCH(0);
651 OUT_BATCH(0);
652 ADVANCE_BATCH();
653 } else {
654 int flags = PIPE_CONTROL_NO_WRITE | PIPE_CONTROL_WRITE_FLUSH;
655 if (brw->gen >= 6) {
656 flags |= PIPE_CONTROL_INSTRUCTION_FLUSH |
657 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
658 PIPE_CONTROL_VF_CACHE_INVALIDATE |
659 PIPE_CONTROL_TC_FLUSH |
660 PIPE_CONTROL_CS_STALL;
661
662 if (brw->gen == 6) {
663 /* Hardware workaround: SNB B-Spec says:
664 *
665 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache
666 * Flush Enable =1, a PIPE_CONTROL with any non-zero
667 * post-sync-op is required.
668 */
669 intel_emit_post_sync_nonzero_flush(brw);
670 }
671 }
672 brw_emit_pipe_control_flush(brw, flags);
673 }
674
675 brw_render_cache_set_clear(brw);
676 }
677
678 void
679 brw_load_register_mem(struct brw_context *brw,
680 uint32_t reg,
681 drm_intel_bo *bo,
682 uint32_t read_domains, uint32_t write_domain,
683 uint32_t offset)
684 {
685 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
686 assert(brw->gen >= 7);
687
688 if (brw->gen >= 8) {
689 BEGIN_BATCH(4);
690 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (4 - 2));
691 OUT_BATCH(reg);
692 OUT_RELOC64(bo, read_domains, write_domain, offset);
693 ADVANCE_BATCH();
694 } else {
695 BEGIN_BATCH(3);
696 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
697 OUT_BATCH(reg);
698 OUT_RELOC(bo, read_domains, write_domain, offset);
699 ADVANCE_BATCH();
700 }
701 }