1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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 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.
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 "brw_context.h"
36 intel_batchbuffer_reset(struct brw_context
*brw
);
38 struct cached_batch_item
{
39 struct cached_batch_item
*next
;
45 intel_batchbuffer_clear_cache(struct brw_context
*brw
)
47 struct cached_batch_item
*item
= brw
->batch
.cached_items
;
50 struct cached_batch_item
*next
= item
->next
;
55 brw
->batch
.cached_items
= NULL
;
59 intel_batchbuffer_init(struct brw_context
*brw
)
61 intel_batchbuffer_reset(brw
);
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.
68 brw
->batch
.workaround_bo
= drm_intel_bo_alloc(brw
->bufmgr
,
69 "pipe_control workaround",
73 brw
->batch
.need_workaround_flush
= true;
76 brw
->batch
.cpu_map
= malloc(BATCH_SZ
);
77 brw
->batch
.map
= brw
->batch
.cpu_map
;
82 intel_batchbuffer_reset(struct brw_context
*brw
)
84 if (brw
->batch
.last_bo
!= NULL
) {
85 drm_intel_bo_unreference(brw
->batch
.last_bo
);
86 brw
->batch
.last_bo
= NULL
;
88 brw
->batch
.last_bo
= brw
->batch
.bo
;
90 intel_batchbuffer_clear_cache(brw
);
92 brw
->batch
.bo
= drm_intel_bo_alloc(brw
->bufmgr
, "batchbuffer",
95 drm_intel_bo_map(brw
->batch
.bo
, true);
96 brw
->batch
.map
= brw
->batch
.bo
->virtual;
99 brw
->batch
.reserved_space
= BATCH_RESERVED
;
100 brw
->batch
.state_batch_offset
= brw
->batch
.bo
->size
;
102 brw
->batch
.needs_sol_reset
= false;
106 intel_batchbuffer_save_state(struct brw_context
*brw
)
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
);
114 intel_batchbuffer_reset_to_saved(struct brw_context
*brw
)
116 drm_intel_gem_bo_clear_relocs(brw
->batch
.bo
, brw
->batch
.saved
.reloc_count
);
118 brw
->batch
.used
= brw
->batch
.saved
.used
;
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.
123 intel_batchbuffer_clear_cache(brw
);
127 intel_batchbuffer_free(struct brw_context
*brw
)
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
);
137 do_batch_dump(struct brw_context
*brw
)
139 struct drm_intel_decode
*decode
;
140 struct intel_batchbuffer
*batch
= &brw
->batch
;
143 decode
= drm_intel_decode_context_alloc(brw
->intelScreen
->deviceID
);
147 ret
= drm_intel_bo_map(batch
->bo
, false);
149 drm_intel_decode_set_batch_pointer(decode
,
155 "WARNING: failed to map batchbuffer (%s), "
156 "dumping uploaded data instead.\n", strerror(ret
));
158 drm_intel_decode_set_batch_pointer(decode
,
164 drm_intel_decode(decode
);
166 drm_intel_decode_context_free(decode
);
169 drm_intel_bo_unmap(batch
->bo
);
171 brw_debug_batch(brw
);
176 * Called when starting a new batch buffer.
179 brw_new_batch(struct brw_context
*brw
)
181 /* Create a new batchbuffer and reset the associated state: */
182 intel_batchbuffer_reset(brw
);
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).
190 if (brw
->hw_ctx
== NULL
)
191 brw
->state
.dirty
.brw
|= BRW_NEW_CONTEXT
;
193 brw
->state
.dirty
.brw
|= BRW_NEW_BATCH
;
195 /* Assume that the last command before the start of our batch was a
196 * primitive, for safety.
198 brw
->batch
.need_workaround_flush
= true;
200 brw
->state_batch_count
= 0;
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.
209 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
210 brw_collect_and_report_shader_time(brw
);
214 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
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.
223 brw_finish_batch(struct brw_context
*brw
)
225 brw_emit_query_end(brw
);
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
;
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
237 brw
->cache
.bo_used_by_gpu
= true;
240 /* TODO: Push this whole function into bufmgr.
243 do_flush_locked(struct brw_context
*brw
)
245 struct intel_batchbuffer
*batch
= &brw
->batch
;
249 drm_intel_bo_unmap(batch
->bo
);
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
);
260 if (!brw
->intelScreen
->no_hw
) {
263 if (brw
->gen
< 6 || !batch
->is_blit
) {
264 flags
= I915_EXEC_RENDER
;
266 flags
= I915_EXEC_BLT
;
269 if (batch
->needs_sol_reset
)
270 flags
|= I915_EXEC_GEN7_SOL_RESET
;
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,
279 ret
= drm_intel_gem_bo_context_exec(batch
->bo
, brw
->hw_ctx
,
280 4 * batch
->used
, flags
);
285 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
289 fprintf(stderr
, "intel_do_flush_locked failed: %s\n", strerror(-ret
));
297 _intel_batchbuffer_flush(struct brw_context
*brw
,
298 const char *file
, int line
)
302 if (brw
->batch
.used
== 0)
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
);
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
,
318 100.0f
* total_bytes
/ BATCH_SZ
);
321 brw
->batch
.reserved_space
= 0;
323 brw_finish_batch(brw
);
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
);
332 intel_upload_finish(brw
);
334 /* Check that we didn't just wrap our batchbuffer at a bad time. */
335 assert(!brw
->no_batch_wrap
);
337 ret
= do_flush_locked(brw
);
339 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
340 fprintf(stderr
, "waiting for idle\n");
341 drm_intel_bo_wait_rendering(brw
->batch
.bo
);
344 /* Start a new batch buffer. */
351 /* This is the only way buffers get added to the validate list.
354 intel_batchbuffer_emit_reloc(struct brw_context
*brw
,
355 drm_intel_bo
*buffer
,
356 uint32_t read_domains
, uint32_t write_domain
,
361 ret
= drm_intel_bo_emit_reloc(brw
->batch
.bo
, 4*brw
->batch
.used
,
363 read_domains
, write_domain
);
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
372 intel_batchbuffer_emit_dword(brw
, buffer
->offset
+ delta
);
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
,
386 ret
= drm_intel_bo_emit_reloc_fence(brw
->batch
.bo
, 4*brw
->batch
.used
,
388 read_domains
, write_domain
);
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
397 intel_batchbuffer_emit_dword(brw
, buffer
->offset
+ delta
);
403 intel_batchbuffer_data(struct brw_context
*brw
,
404 const void *data
, GLuint bytes
, bool is_blit
)
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;
413 intel_batchbuffer_cached_advance(struct brw_context
*brw
)
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;
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
) {
429 item
->next
= brw
->batch
.cached_items
;
430 brw
->batch
.cached_items
= item
;
432 brw
->batch
.used
= brw
->batch
.emit
;
441 item
= malloc(sizeof(struct cached_batch_item
));
445 item
->next
= brw
->batch
.cached_items
;
446 brw
->batch
.cached_items
= item
;
450 item
->header
= brw
->batch
.emit
;
454 * Restriction [DevSNB, DevIVB]:
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).
466 intel_emit_depth_stall_flushes(struct brw_context
*brw
)
468 assert(brw
->gen
>= 6 && brw
->gen
<= 7);
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 */
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 */
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 */
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."
501 gen7_emit_vs_workaround_flush(struct brw_context
*brw
)
503 assert(brw
->gen
== 7);
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 */
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:
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 !=
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.
527 * And the workaround for these two requires this workaround first:
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
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
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)"
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.
552 intel_emit_post_sync_nonzero_flush(struct brw_context
*brw
)
554 if (!brw
->batch
.need_workaround_flush
)
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 */
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 */
573 brw
->batch
.need_workaround_flush
= false;
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.
580 * This is also used for the always_flush_cache driconf debug option.
583 intel_batchbuffer_emit_mi_flush(struct brw_context
*brw
)
586 if (brw
->batch
.is_blit
) {
588 OUT_BATCH(MI_FLUSH_DW
);
595 /* Hardware workaround: SNB B-Spec says:
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.
601 intel_emit_post_sync_nonzero_flush(brw
);
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 */
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 */