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_context.h"
29 #include "intel_batchbuffer.h"
30 #include "intel_buffer_objects.h"
31 #include "intel_reg.h"
32 #include "intel_bufmgr.h"
33 #include "intel_buffers.h"
35 struct cached_batch_item
{
36 struct cached_batch_item
*next
;
41 static void clear_cache( struct intel_context
*intel
)
43 struct cached_batch_item
*item
= intel
->batch
.cached_items
;
46 struct cached_batch_item
*next
= item
->next
;
51 intel
->batch
.cached_items
= NULL
;
55 intel_batchbuffer_init(struct intel_context
*intel
)
57 intel_batchbuffer_reset(intel
);
59 if (intel
->gen
>= 6) {
60 /* We can't just use brw_state_batch to get a chunk of space for
61 * the gen6 workaround because it involves actually writing to
62 * the buffer, and the kernel doesn't let us write to the batch.
64 intel
->batch
.workaround_bo
= drm_intel_bo_alloc(intel
->bufmgr
,
65 "pipe_control workaround",
71 intel_batchbuffer_reset(struct intel_context
*intel
)
73 if (intel
->batch
.last_bo
!= NULL
) {
74 drm_intel_bo_unreference(intel
->batch
.last_bo
);
75 intel
->batch
.last_bo
= NULL
;
77 intel
->batch
.last_bo
= intel
->batch
.bo
;
81 intel
->batch
.bo
= drm_intel_bo_alloc(intel
->bufmgr
, "batchbuffer",
82 intel
->maxBatchSize
, 4096);
84 intel
->batch
.reserved_space
= BATCH_RESERVED
;
85 intel
->batch
.state_batch_offset
= intel
->batch
.bo
->size
;
86 intel
->batch
.used
= 0;
87 intel
->batch
.needs_sol_reset
= false;
91 intel_batchbuffer_save_state(struct intel_context
*intel
)
93 intel
->batch
.saved
.used
= intel
->batch
.used
;
94 intel
->batch
.saved
.reloc_count
=
95 drm_intel_gem_bo_get_reloc_count(intel
->batch
.bo
);
99 intel_batchbuffer_reset_to_saved(struct intel_context
*intel
)
101 drm_intel_gem_bo_clear_relocs(intel
->batch
.bo
, intel
->batch
.saved
.reloc_count
);
103 intel
->batch
.used
= intel
->batch
.saved
.used
;
105 /* Cached batch state is dead, since we just cleared some unknown part of the
106 * batchbuffer. Assume that the caller resets any other state necessary.
112 intel_batchbuffer_free(struct intel_context
*intel
)
114 drm_intel_bo_unreference(intel
->batch
.last_bo
);
115 drm_intel_bo_unreference(intel
->batch
.bo
);
116 drm_intel_bo_unreference(intel
->batch
.workaround_bo
);
121 do_batch_dump(struct intel_context
*intel
)
123 struct drm_intel_decode
*decode
;
124 struct intel_batchbuffer
*batch
= &intel
->batch
;
127 decode
= drm_intel_decode_context_alloc(intel
->intelScreen
->deviceID
);
131 ret
= drm_intel_bo_map(batch
->bo
, false);
133 drm_intel_decode_set_batch_pointer(decode
,
139 "WARNING: failed to map batchbuffer (%s), "
140 "dumping uploaded data instead.\n", strerror(ret
));
142 drm_intel_decode_set_batch_pointer(decode
,
148 drm_intel_decode(decode
);
150 drm_intel_decode_context_free(decode
);
153 drm_intel_bo_unmap(batch
->bo
);
155 if (intel
->vtbl
.debug_batch
!= NULL
)
156 intel
->vtbl
.debug_batch(intel
);
160 /* TODO: Push this whole function into bufmgr.
163 do_flush_locked(struct intel_context
*intel
)
165 struct intel_batchbuffer
*batch
= &intel
->batch
;
168 ret
= drm_intel_bo_subdata(batch
->bo
, 0, 4*batch
->used
, batch
->map
);
169 if (ret
== 0 && batch
->state_batch_offset
!= batch
->bo
->size
) {
170 ret
= drm_intel_bo_subdata(batch
->bo
,
171 batch
->state_batch_offset
,
172 batch
->bo
->size
- batch
->state_batch_offset
,
173 (char *)batch
->map
+ batch
->state_batch_offset
);
176 if (!intel
->intelScreen
->no_hw
) {
179 if (intel
->gen
< 6 || !batch
->is_blit
) {
180 flags
= I915_EXEC_RENDER
;
182 flags
= I915_EXEC_BLT
;
185 if (batch
->needs_sol_reset
)
186 flags
|= I915_EXEC_GEN7_SOL_RESET
;
189 if (unlikely(INTEL_DEBUG
& DEBUG_AUB
) && intel
->vtbl
.annotate_aub
)
190 intel
->vtbl
.annotate_aub(intel
);
191 if (intel
->hw_ctx
== NULL
|| batch
->is_blit
) {
192 ret
= drm_intel_bo_mrb_exec(batch
->bo
, 4 * batch
->used
, NULL
, 0, 0,
195 ret
= drm_intel_gem_bo_context_exec(batch
->bo
, intel
->hw_ctx
,
196 4 * batch
->used
, flags
);
201 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
202 do_batch_dump(intel
);
205 fprintf(stderr
, "intel_do_flush_locked failed: %s\n", strerror(-ret
));
208 intel
->vtbl
.new_batch(intel
);
214 _intel_batchbuffer_flush(struct intel_context
*intel
,
215 const char *file
, int line
)
219 if (intel
->batch
.used
== 0)
222 if (intel
->first_post_swapbuffers_batch
== NULL
) {
223 intel
->first_post_swapbuffers_batch
= intel
->batch
.bo
;
224 drm_intel_bo_reference(intel
->first_post_swapbuffers_batch
);
227 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
228 fprintf(stderr
, "%s:%d: Batchbuffer flush with %db used\n", file
, line
,
229 4*intel
->batch
.used
);
231 intel
->batch
.reserved_space
= 0;
233 if (intel
->vtbl
.finish_batch
)
234 intel
->vtbl
.finish_batch(intel
);
236 /* Mark the end of the buffer. */
237 intel_batchbuffer_emit_dword(intel
, MI_BATCH_BUFFER_END
);
238 if (intel
->batch
.used
& 1) {
239 /* Round batchbuffer usage to 2 DWORDs. */
240 intel_batchbuffer_emit_dword(intel
, MI_NOOP
);
243 intel_upload_finish(intel
);
245 /* Check that we didn't just wrap our batchbuffer at a bad time. */
246 assert(!intel
->no_batch_wrap
);
248 ret
= do_flush_locked(intel
);
250 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
251 fprintf(stderr
, "waiting for idle\n");
252 drm_intel_bo_wait_rendering(intel
->batch
.bo
);
257 intel_batchbuffer_reset(intel
);
263 /* This is the only way buffers get added to the validate list.
266 intel_batchbuffer_emit_reloc(struct intel_context
*intel
,
267 drm_intel_bo
*buffer
,
268 uint32_t read_domains
, uint32_t write_domain
,
273 ret
= drm_intel_bo_emit_reloc(intel
->batch
.bo
, 4*intel
->batch
.used
,
275 read_domains
, write_domain
);
280 * Using the old buffer offset, write in what the right data would be, in case
281 * the buffer doesn't move and we can short-circuit the relocation processing
284 intel_batchbuffer_emit_dword(intel
, buffer
->offset
+ delta
);
290 intel_batchbuffer_emit_reloc_fenced(struct intel_context
*intel
,
291 drm_intel_bo
*buffer
,
292 uint32_t read_domains
,
293 uint32_t write_domain
,
298 ret
= drm_intel_bo_emit_reloc_fence(intel
->batch
.bo
, 4*intel
->batch
.used
,
300 read_domains
, write_domain
);
305 * Using the old buffer offset, write in what the right data would
306 * be, in case the buffer doesn't move and we can short-circuit the
307 * relocation processing in the kernel
309 intel_batchbuffer_emit_dword(intel
, buffer
->offset
+ delta
);
315 intel_batchbuffer_data(struct intel_context
*intel
,
316 const void *data
, GLuint bytes
, bool is_blit
)
318 assert((bytes
& 3) == 0);
319 intel_batchbuffer_require_space(intel
, bytes
, is_blit
);
320 __memcpy(intel
->batch
.map
+ intel
->batch
.used
, data
, bytes
);
321 intel
->batch
.used
+= bytes
>> 2;
325 intel_batchbuffer_cached_advance(struct intel_context
*intel
)
327 struct cached_batch_item
**prev
= &intel
->batch
.cached_items
, *item
;
328 uint32_t sz
= (intel
->batch
.used
- intel
->batch
.emit
) * sizeof(uint32_t);
329 uint32_t *start
= intel
->batch
.map
+ intel
->batch
.emit
;
330 uint16_t op
= *start
>> 16;
336 old
= intel
->batch
.map
+ item
->header
;
337 if (op
== *old
>> 16) {
338 if (item
->size
== sz
&& memcmp(old
, start
, sz
) == 0) {
339 if (prev
!= &intel
->batch
.cached_items
) {
341 item
->next
= intel
->batch
.cached_items
;
342 intel
->batch
.cached_items
= item
;
344 intel
->batch
.used
= intel
->batch
.emit
;
353 item
= malloc(sizeof(struct cached_batch_item
));
357 item
->next
= intel
->batch
.cached_items
;
358 intel
->batch
.cached_items
= item
;
362 item
->header
= intel
->batch
.emit
;
366 * Restriction [DevSNB, DevIVB]:
368 * Prior to changing Depth/Stencil Buffer state (i.e. any combination of
369 * 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS, 3DSTATE_STENCIL_BUFFER,
370 * 3DSTATE_HIER_DEPTH_BUFFER) SW must first issue a pipelined depth stall
371 * (PIPE_CONTROL with Depth Stall bit set), followed by a pipelined depth
372 * cache flush (PIPE_CONTROL with Depth Flush Bit set), followed by
373 * another pipelined depth stall (PIPE_CONTROL with Depth Stall bit set),
374 * unless SW can otherwise guarantee that the pipeline from WM onwards is
375 * already flushed (e.g., via a preceding MI_FLUSH).
378 intel_emit_depth_stall_flushes(struct intel_context
*intel
)
380 assert(intel
->gen
>= 6 && intel
->gen
<= 7);
383 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
384 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
);
385 OUT_BATCH(0); /* address */
386 OUT_BATCH(0); /* write data */
390 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
391 OUT_BATCH(PIPE_CONTROL_DEPTH_CACHE_FLUSH
);
392 OUT_BATCH(0); /* address */
393 OUT_BATCH(0); /* write data */
397 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
398 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
);
399 OUT_BATCH(0); /* address */
400 OUT_BATCH(0); /* write data */
405 * From the BSpec, volume 2a.03: VS Stage Input / State:
406 * "[DevIVB] A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth
407 * stall needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
408 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
409 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL needs
410 * to be sent before any combination of VS associated 3DSTATE."
413 gen7_emit_vs_workaround_flush(struct intel_context
*intel
)
415 assert(intel
->gen
== 7);
418 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
419 OUT_BATCH(PIPE_CONTROL_DEPTH_STALL
| PIPE_CONTROL_WRITE_IMMEDIATE
);
420 OUT_RELOC(intel
->batch
.workaround_bo
,
421 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
, 0);
422 OUT_BATCH(0); /* write data */
427 * Emits a PIPE_CONTROL with a non-zero post-sync operation, for
428 * implementing two workarounds on gen6. From section 1.4.7.1
429 * "PIPE_CONTROL" of the Sandy Bridge PRM volume 2 part 1:
431 * [DevSNB-C+{W/A}] Before any depth stall flush (including those
432 * produced by non-pipelined state commands), software needs to first
433 * send a PIPE_CONTROL with no bits set except Post-Sync Operation !=
436 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache Flush Enable
437 * =1, a PIPE_CONTROL with any non-zero post-sync-op is required.
439 * And the workaround for these two requires this workaround first:
441 * [Dev-SNB{W/A}]: Pipe-control with CS-stall bit set must be sent
442 * BEFORE the pipe-control with a post-sync op and no write-cache
445 * And this last workaround is tricky because of the requirements on
446 * that bit. From section 1.4.7.2.3 "Stall" of the Sandy Bridge PRM
449 * "1 of the following must also be set:
450 * - Render Target Cache Flush Enable ([12] of DW1)
451 * - Depth Cache Flush Enable ([0] of DW1)
452 * - Stall at Pixel Scoreboard ([1] of DW1)
453 * - Depth Stall ([13] of DW1)
454 * - Post-Sync Operation ([13] of DW1)
455 * - Notify Enable ([8] of DW1)"
457 * The cache flushes require the workaround flush that triggered this
458 * one, so we can't use it. Depth stall would trigger the same.
459 * Post-sync nonzero is what triggered this second workaround, so we
460 * can't use that one either. Notify enable is IRQs, which aren't
461 * really our business. That leaves only stall at scoreboard.
464 intel_emit_post_sync_nonzero_flush(struct intel_context
*intel
)
466 if (!intel
->batch
.need_workaround_flush
)
470 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
471 OUT_BATCH(PIPE_CONTROL_CS_STALL
|
472 PIPE_CONTROL_STALL_AT_SCOREBOARD
);
473 OUT_BATCH(0); /* address */
474 OUT_BATCH(0); /* write data */
478 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
479 OUT_BATCH(PIPE_CONTROL_WRITE_IMMEDIATE
);
480 OUT_RELOC(intel
->batch
.workaround_bo
,
481 I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
, 0);
482 OUT_BATCH(0); /* write data */
485 intel
->batch
.need_workaround_flush
= false;
488 /* Emit a pipelined flush to either flush render and texture cache for
489 * reading from a FBO-drawn texture, or flush so that frontbuffer
490 * render appears on the screen in DRI1.
492 * This is also used for the always_flush_cache driconf debug option.
495 intel_batchbuffer_emit_mi_flush(struct intel_context
*intel
)
497 if (intel
->gen
>= 6) {
498 if (intel
->batch
.is_blit
) {
500 OUT_BATCH(MI_FLUSH_DW
);
506 if (intel
->gen
== 6) {
507 /* Hardware workaround: SNB B-Spec says:
509 * [Dev-SNB{W/A}]: Before a PIPE_CONTROL with Write Cache
510 * Flush Enable =1, a PIPE_CONTROL with any non-zero
511 * post-sync-op is required.
513 intel_emit_post_sync_nonzero_flush(intel
);
517 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2));
518 OUT_BATCH(PIPE_CONTROL_INSTRUCTION_FLUSH
|
519 PIPE_CONTROL_WRITE_FLUSH
|
520 PIPE_CONTROL_DEPTH_CACHE_FLUSH
|
521 PIPE_CONTROL_VF_CACHE_INVALIDATE
|
522 PIPE_CONTROL_TC_FLUSH
|
523 PIPE_CONTROL_NO_WRITE
|
524 PIPE_CONTROL_CS_STALL
);
525 OUT_BATCH(0); /* write address */
526 OUT_BATCH(0); /* write data */
529 } else if (intel
->gen
>= 4) {
531 OUT_BATCH(_3DSTATE_PIPE_CONTROL
| (4 - 2) |
532 PIPE_CONTROL_WRITE_FLUSH
|
533 PIPE_CONTROL_NO_WRITE
);
534 OUT_BATCH(0); /* write address */
535 OUT_BATCH(0); /* write data */
536 OUT_BATCH(0); /* write data */