1 /**************************************************************************
3 * Copyright 2006 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
37 intel_batchbuffer_reset(struct brw_context
*brw
);
40 intel_batchbuffer_init(struct brw_context
*brw
)
42 intel_batchbuffer_reset(brw
);
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.
49 brw
->batch
.workaround_bo
= drm_intel_bo_alloc(brw
->bufmgr
,
50 "pipe_control workaround",
54 brw
->batch
.need_workaround_flush
= true;
57 brw
->batch
.cpu_map
= malloc(BATCH_SZ
);
58 brw
->batch
.map
= brw
->batch
.cpu_map
;
63 intel_batchbuffer_reset(struct brw_context
*brw
)
65 if (brw
->batch
.last_bo
!= NULL
) {
66 drm_intel_bo_unreference(brw
->batch
.last_bo
);
67 brw
->batch
.last_bo
= NULL
;
69 brw
->batch
.last_bo
= brw
->batch
.bo
;
71 brw_render_cache_set_clear(brw
);
73 brw
->batch
.bo
= drm_intel_bo_alloc(brw
->bufmgr
, "batchbuffer",
76 drm_intel_bo_map(brw
->batch
.bo
, true);
77 brw
->batch
.map
= brw
->batch
.bo
->virtual;
80 brw
->batch
.reserved_space
= BATCH_RESERVED
;
81 brw
->batch
.state_batch_offset
= brw
->batch
.bo
->size
;
83 brw
->batch
.needs_sol_reset
= false;
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.
88 brw
->batch
.ring
= UNKNOWN_RING
;
92 intel_batchbuffer_save_state(struct brw_context
*brw
)
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
);
100 intel_batchbuffer_reset_to_saved(struct brw_context
*brw
)
102 drm_intel_gem_bo_clear_relocs(brw
->batch
.bo
, brw
->batch
.saved
.reloc_count
);
104 brw
->batch
.used
= brw
->batch
.saved
.used
;
105 if (brw
->batch
.used
== 0)
106 brw
->batch
.ring
= UNKNOWN_RING
;
110 intel_batchbuffer_free(struct brw_context
*brw
)
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
);
119 do_batch_dump(struct brw_context
*brw
)
121 struct drm_intel_decode
*decode
;
122 struct intel_batchbuffer
*batch
= &brw
->batch
;
125 decode
= drm_intel_decode_context_alloc(brw
->intelScreen
->deviceID
);
129 ret
= drm_intel_bo_map(batch
->bo
, false);
131 drm_intel_decode_set_batch_pointer(decode
,
137 "WARNING: failed to map batchbuffer (%s), "
138 "dumping uploaded data instead.\n", strerror(ret
));
140 drm_intel_decode_set_batch_pointer(decode
,
146 drm_intel_decode_set_output_file(decode
, stderr
);
147 drm_intel_decode(decode
);
149 drm_intel_decode_context_free(decode
);
152 drm_intel_bo_unmap(batch
->bo
);
154 brw_debug_batch(brw
);
159 intel_batchbuffer_emit_render_ring_prelude(struct brw_context
*brw
)
161 /* We may need to enable and snapshot OA counters. */
162 brw_perf_monitor_new_batch(brw
);
166 * Called when starting a new batch buffer.
169 brw_new_batch(struct brw_context
*brw
)
171 /* Create a new batchbuffer and reset the associated state: */
172 intel_batchbuffer_reset(brw
);
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).
180 if (brw
->hw_ctx
== NULL
)
181 brw
->state
.dirty
.brw
|= BRW_NEW_CONTEXT
;
183 brw
->state
.dirty
.brw
|= BRW_NEW_BATCH
;
185 /* Assume that the last command before the start of our batch was a
186 * primitive, for safety.
188 brw
->batch
.need_workaround_flush
= true;
190 brw
->state_batch_count
= 0;
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.
199 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
200 brw_collect_and_report_shader_time(brw
);
202 if (INTEL_DEBUG
& DEBUG_PERFMON
)
203 brw_dump_perf_monitors(brw
);
207 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
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.
216 brw_finish_batch(struct brw_context
*brw
)
218 /* Capture the closing pipeline statistics register values necessary to
219 * support query objects (in the non-hardware context world).
221 brw_emit_query_end(brw
);
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
);
227 if (brw
->curbe
.curbe_bo
) {
228 drm_intel_bo_unreference(brw
->curbe
.curbe_bo
);
229 brw
->curbe
.curbe_bo
= NULL
;
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
236 brw
->cache
.bo_used_by_gpu
= true;
239 /* TODO: Push this whole function into bufmgr.
242 do_flush_locked(struct brw_context
*brw
)
244 struct intel_batchbuffer
*batch
= &brw
->batch
;
248 drm_intel_bo_unmap(batch
->bo
);
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
);
259 if (!brw
->intelScreen
->no_hw
) {
262 if (brw
->gen
>= 6 && batch
->ring
== BLT_RING
) {
263 flags
= I915_EXEC_BLT
;
265 flags
= I915_EXEC_RENDER
;
267 if (batch
->needs_sol_reset
)
268 flags
|= I915_EXEC_GEN7_SOL_RESET
;
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,
277 ret
= drm_intel_gem_bo_context_exec(batch
->bo
, brw
->hw_ctx
,
278 4 * batch
->used
, flags
);
283 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
287 fprintf(stderr
, "intel_do_flush_locked failed: %s\n", strerror(-ret
));
295 _intel_batchbuffer_flush(struct brw_context
*brw
,
296 const char *file
, int line
)
300 if (brw
->batch
.used
== 0)
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
);
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
,
316 100.0f
* total_bytes
/ BATCH_SZ
);
319 brw
->batch
.reserved_space
= 0;
321 brw_finish_batch(brw
);
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
);
330 intel_upload_finish(brw
);
332 /* Check that we didn't just wrap our batchbuffer at a bad time. */
333 assert(!brw
->no_batch_wrap
);
335 ret
= do_flush_locked(brw
);
337 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
338 fprintf(stderr
, "waiting for idle\n");
339 drm_intel_bo_wait_rendering(brw
->batch
.bo
);
342 /* Start a new batch buffer. */
349 /* This is the only way buffers get added to the validate list.
352 intel_batchbuffer_emit_reloc(struct brw_context
*brw
,
353 drm_intel_bo
*buffer
,
354 uint32_t read_domains
, uint32_t write_domain
,
359 ret
= drm_intel_bo_emit_reloc(brw
->batch
.bo
, 4*brw
->batch
.used
,
361 read_domains
, write_domain
);
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
370 intel_batchbuffer_emit_dword(brw
, buffer
->offset64
+ delta
);
376 intel_batchbuffer_emit_reloc64(struct brw_context
*brw
,
377 drm_intel_bo
*buffer
,
378 uint32_t read_domains
, uint32_t write_domain
,
381 int ret
= drm_intel_bo_emit_reloc(brw
->batch
.bo
, 4*brw
->batch
.used
,
383 read_domains
, write_domain
);
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
391 uint64_t offset
= buffer
->offset64
+ delta
;
392 intel_batchbuffer_emit_dword(brw
, offset
);
393 intel_batchbuffer_emit_dword(brw
, offset
>> 32);
400 intel_batchbuffer_data(struct brw_context
*brw
,
401 const void *data
, GLuint bytes
, enum brw_gpu_ring ring
)
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;
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:
414 * - Render Target Cache Flush
415 * - Depth Cache Flush
416 * - Stall at Pixel Scoreboard
417 * - Post-Sync Operation
420 * I chose "Stall at Pixel Scoreboard" since we've used it effectively
421 * in the past, but the choice is fairly arbitrary.
424 gen8_add_cs_stall_workaround_bits(uint32_t *flags
)
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
;
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."
437 if ((*flags
& PIPE_CONTROL_CS_STALL
) != 0 && (*flags
& wa_bits
) == 0)
438 *flags
|= PIPE_CONTROL_STALL_AT_SCOREBOARD
;
442 * Emit a PIPE_CONTROL with various flushing flags.
444 * The caller is responsible for deciding what flags are appropriate for the
448 brw_emit_pipe_control_flush(struct brw_context
*brw
, uint32_t flags
)
451 gen8_add_cs_stall_workaround_bits(&flags
);
454 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (6 - 2));
461 } else if (brw
->gen
>= 6) {
463 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
471 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| flags
| (4 - 2));
480 * Emit a PIPE_CONTROL that writes to a buffer object.
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
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
)
493 gen8_add_cs_stall_workaround_bits(&flags
);
496 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (6 - 2));
498 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
500 OUT_BATCH(imm_lower
);
501 OUT_BATCH(imm_upper
);
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+.
507 unsigned gen6_gtt
= brw
->gen
== 6 ? PIPE_CONTROL_GLOBAL_GTT_WRITE
: 0;
510 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (5 - 2));
512 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
514 OUT_BATCH(imm_lower
);
515 OUT_BATCH(imm_upper
);
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
);
529 * Restriction [DevSNB, DevIVB]:
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).
541 intel_emit_depth_stall_flushes(struct brw_context
*brw
)
543 assert(brw
->gen
>= 6 && brw
->gen
<= 8);
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
);
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."
559 gen7_emit_vs_workaround_flush(struct brw_context
*brw
)
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,
571 * Emit a PIPE_CONTROL command for gen7 with the CS Stall bit set.
574 gen7_emit_cs_stall_flush(struct brw_context
*brw
)
576 brw_emit_pipe_control_write(brw
,
577 PIPE_CONTROL_CS_STALL
578 | PIPE_CONTROL_WRITE_IMMEDIATE
,
579 brw
->batch
.workaround_bo
, 0,
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:
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 !=
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.
597 * And the workaround for these two requires this workaround first:
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
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
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)"
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.
622 intel_emit_post_sync_nonzero_flush(struct brw_context
*brw
)
624 if (!brw
->batch
.need_workaround_flush
)
627 brw_emit_pipe_control_flush(brw
,
628 PIPE_CONTROL_CS_STALL
|
629 PIPE_CONTROL_STALL_AT_SCOREBOARD
);
631 brw_emit_pipe_control_write(brw
, PIPE_CONTROL_WRITE_IMMEDIATE
,
632 brw
->batch
.workaround_bo
, 0, 0, 0);
634 brw
->batch
.need_workaround_flush
= false;
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.
641 * This is also used for the always_flush_cache driconf debug option.
644 intel_batchbuffer_emit_mi_flush(struct brw_context
*brw
)
646 if (brw
->batch
.ring
== BLT_RING
&& brw
->gen
>= 6) {
648 OUT_BATCH(MI_FLUSH_DW
);
654 int flags
= PIPE_CONTROL_NO_WRITE
| PIPE_CONTROL_WRITE_FLUSH
;
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
;
663 /* Hardware workaround: SNB B-Spec says:
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.
669 intel_emit_post_sync_nonzero_flush(brw
);
672 brw_emit_pipe_control_flush(brw
, flags
);
675 brw_render_cache_set_clear(brw
);
679 brw_load_register_mem(struct brw_context
*brw
,
682 uint32_t read_domains
, uint32_t write_domain
,
685 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
686 assert(brw
->gen
>= 7);
690 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (4 - 2));
692 OUT_RELOC64(bo
, read_domains
, write_domain
, offset
);
696 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
698 OUT_RELOC(bo
, read_domains
, write_domain
, offset
);