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