2 * Copyright 2006 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "intel_batchbuffer.h"
27 #include "intel_buffer_objects.h"
28 #include "intel_bufmgr.h"
29 #include "intel_buffers.h"
30 #include "intel_fbo.h"
31 #include "brw_context.h"
32 #include "brw_defines.h"
33 #include "brw_state.h"
39 intel_batchbuffer_reset(struct intel_batchbuffer
*batch
, dri_bufmgr
*bufmgr
,
43 intel_batchbuffer_init(struct intel_batchbuffer
*batch
, dri_bufmgr
*bufmgr
,
46 intel_batchbuffer_reset(batch
, bufmgr
, has_llc
);
49 batch
->cpu_map
= malloc(BATCH_SZ
);
50 batch
->map
= batch
->cpu_map
;
51 batch
->map_next
= batch
->cpu_map
;
56 intel_batchbuffer_reset(struct intel_batchbuffer
*batch
, dri_bufmgr
*bufmgr
,
59 if (batch
->last_bo
!= NULL
) {
60 drm_intel_bo_unreference(batch
->last_bo
);
61 batch
->last_bo
= NULL
;
63 batch
->last_bo
= batch
->bo
;
65 batch
->bo
= drm_intel_bo_alloc(bufmgr
, "batchbuffer", BATCH_SZ
, 4096);
67 drm_intel_bo_map(batch
->bo
, true);
68 batch
->map
= batch
->bo
->virtual;
70 batch
->map_next
= batch
->map
;
72 batch
->reserved_space
= BATCH_RESERVED
;
73 batch
->state_batch_offset
= batch
->bo
->size
;
74 batch
->needs_sol_reset
= false;
75 batch
->state_base_address_emitted
= false;
77 /* We don't know what ring the new batch will be sent to until we see the
78 * first BEGIN_BATCH or BEGIN_BATCH_BLT. Mark it as unknown.
80 batch
->ring
= UNKNOWN_RING
;
84 intel_batchbuffer_reset_and_clear_render_cache(struct brw_context
*brw
)
86 intel_batchbuffer_reset(&brw
->batch
, brw
->bufmgr
, brw
->has_llc
);
87 brw_render_cache_set_clear(brw
);
91 intel_batchbuffer_save_state(struct brw_context
*brw
)
93 brw
->batch
.saved
.map_next
= brw
->batch
.map_next
;
94 brw
->batch
.saved
.reloc_count
=
95 drm_intel_gem_bo_get_reloc_count(brw
->batch
.bo
);
99 intel_batchbuffer_reset_to_saved(struct brw_context
*brw
)
101 drm_intel_gem_bo_clear_relocs(brw
->batch
.bo
, brw
->batch
.saved
.reloc_count
);
103 brw
->batch
.map_next
= brw
->batch
.saved
.map_next
;
104 if (USED_BATCH(brw
->batch
) == 0)
105 brw
->batch
.ring
= UNKNOWN_RING
;
109 intel_batchbuffer_free(struct intel_batchbuffer
*batch
)
111 free(batch
->cpu_map
);
112 drm_intel_bo_unreference(batch
->last_bo
);
113 drm_intel_bo_unreference(batch
->bo
);
117 intel_batchbuffer_require_space(struct brw_context
*brw
, GLuint sz
,
118 enum brw_gpu_ring ring
)
120 /* If we're switching rings, implicitly flush the batch. */
121 if (unlikely(ring
!= brw
->batch
.ring
) && brw
->batch
.ring
!= UNKNOWN_RING
&&
123 intel_batchbuffer_flush(brw
);
127 assert(sz
< BATCH_SZ
- BATCH_RESERVED
);
129 if (intel_batchbuffer_space(&brw
->batch
) < sz
)
130 intel_batchbuffer_flush(brw
);
132 enum brw_gpu_ring prev_ring
= brw
->batch
.ring
;
133 /* The intel_batchbuffer_flush() calls above might have changed
134 * brw->batch.ring to UNKNOWN_RING, so we need to set it here at the end.
136 brw
->batch
.ring
= ring
;
138 if (unlikely(prev_ring
== UNKNOWN_RING
&& ring
== RENDER_RING
))
139 intel_batchbuffer_emit_render_ring_prelude(brw
);
143 do_batch_dump(struct brw_context
*brw
)
145 struct drm_intel_decode
*decode
;
146 struct intel_batchbuffer
*batch
= &brw
->batch
;
149 decode
= drm_intel_decode_context_alloc(brw
->screen
->deviceID
);
153 ret
= drm_intel_bo_map(batch
->bo
, false);
155 drm_intel_decode_set_batch_pointer(decode
,
161 "WARNING: failed to map batchbuffer (%s), "
162 "dumping uploaded data instead.\n", strerror(ret
));
164 drm_intel_decode_set_batch_pointer(decode
,
170 drm_intel_decode_set_output_file(decode
, stderr
);
171 drm_intel_decode(decode
);
173 drm_intel_decode_context_free(decode
);
176 drm_intel_bo_unmap(batch
->bo
);
178 brw_debug_batch(brw
);
183 intel_batchbuffer_emit_render_ring_prelude(struct brw_context
*brw
)
185 /* Un-used currently */
189 * Called when starting a new batch buffer.
192 brw_new_batch(struct brw_context
*brw
)
194 /* Create a new batchbuffer and reset the associated state: */
195 drm_intel_gem_bo_clear_relocs(brw
->batch
.bo
, 0);
196 intel_batchbuffer_reset_and_clear_render_cache(brw
);
198 /* If the kernel supports hardware contexts, then most hardware state is
199 * preserved between batches; we only need to re-emit state that is required
200 * to be in every batch. Otherwise we need to re-emit all the state that
201 * would otherwise be stored in the context (which for all intents and
202 * purposes means everything).
204 if (brw
->hw_ctx
== NULL
)
205 brw
->ctx
.NewDriverState
|= BRW_NEW_CONTEXT
;
207 brw
->ctx
.NewDriverState
|= BRW_NEW_BATCH
;
209 brw
->state_batch_count
= 0;
213 /* We need to periodically reap the shader time results, because rollover
214 * happens every few seconds. We also want to see results every once in a
215 * while, because many programs won't cleanly destroy our context, so the
216 * end-of-run printout may not happen.
218 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
219 brw_collect_and_report_shader_time(brw
);
223 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
226 * This function can emit state (say, to preserve registers that aren't saved
227 * between batches). All of this state MUST fit in the reserved space at the
228 * end of the batchbuffer. If you add more GPU state, increase the reserved
229 * space by updating the BATCH_RESERVED macro.
232 brw_finish_batch(struct brw_context
*brw
)
234 /* Capture the closing pipeline statistics register values necessary to
235 * support query objects (in the non-hardware context world).
237 brw_emit_query_end(brw
);
239 if (brw
->batch
.ring
== RENDER_RING
) {
240 /* Work around L3 state leaks into contexts set MI_RESTORE_INHIBIT which
241 * assume that the L3 cache is configured according to the hardware
245 gen7_restore_default_l3_config(brw
);
247 if (brw
->is_haswell
) {
248 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
249 * 3DSTATE_CC_STATE_POINTERS > "Note":
251 * "SW must program 3DSTATE_CC_STATE_POINTERS command at the end of every
252 * 3D batch buffer followed by a PIPE_CONTROL with RC flush and CS stall."
254 * From the example in the docs, it seems to expect a regular pipe control
255 * flush here as well. We may have done it already, but meh.
257 * See also WaAvoidRCZCounterRollover.
259 brw_emit_mi_flush(brw
);
261 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS
<< 16 | (2 - 2));
262 OUT_BATCH(brw
->cc
.state_offset
| 1);
264 brw_emit_pipe_control_flush(brw
, PIPE_CONTROL_RENDER_TARGET_FLUSH
|
265 PIPE_CONTROL_CS_STALL
);
269 /* Mark that the current program cache BO has been used by the GPU.
270 * It will be reallocated if we need to put new programs in for the
273 brw
->cache
.bo_used_by_gpu
= true;
277 throttle(struct brw_context
*brw
)
279 /* Wait for the swapbuffers before the one we just emitted, so we
280 * don't get too many swaps outstanding for apps that are GPU-heavy
283 * We're using intelDRI2Flush (called from the loader before
284 * swapbuffer) and glFlush (for front buffer rendering) as the
285 * indicator that a frame is done and then throttle when we get
286 * here as we prepare to render the next frame. At this point for
287 * round trips for swap/copy and getting new buffers are done and
288 * we'll spend less time waiting on the GPU.
290 * Unfortunately, we don't have a handle to the batch containing
291 * the swap, and getting our hands on that doesn't seem worth it,
292 * so we just use the first batch we emitted after the last swap.
294 if (brw
->need_swap_throttle
&& brw
->throttle_batch
[0]) {
295 if (brw
->throttle_batch
[1]) {
296 if (!brw
->disable_throttling
)
297 drm_intel_bo_wait_rendering(brw
->throttle_batch
[1]);
298 drm_intel_bo_unreference(brw
->throttle_batch
[1]);
300 brw
->throttle_batch
[1] = brw
->throttle_batch
[0];
301 brw
->throttle_batch
[0] = NULL
;
302 brw
->need_swap_throttle
= false;
303 /* Throttling here is more precise than the throttle ioctl, so skip it */
304 brw
->need_flush_throttle
= false;
307 if (brw
->need_flush_throttle
) {
308 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
309 drmCommandNone(dri_screen
->fd
, DRM_I915_GEM_THROTTLE
);
310 brw
->need_flush_throttle
= false;
314 /* TODO: Push this whole function into bufmgr.
317 do_flush_locked(struct brw_context
*brw
, int in_fence_fd
, int *out_fence_fd
)
319 struct intel_batchbuffer
*batch
= &brw
->batch
;
323 drm_intel_bo_unmap(batch
->bo
);
325 ret
= drm_intel_bo_subdata(batch
->bo
, 0, 4 * USED_BATCH(*batch
), batch
->map
);
326 if (ret
== 0 && batch
->state_batch_offset
!= batch
->bo
->size
) {
327 ret
= drm_intel_bo_subdata(batch
->bo
,
328 batch
->state_batch_offset
,
329 batch
->bo
->size
- batch
->state_batch_offset
,
330 (char *)batch
->map
+ batch
->state_batch_offset
);
334 if (!brw
->screen
->no_hw
) {
337 if (brw
->gen
>= 6 && batch
->ring
== BLT_RING
) {
338 flags
= I915_EXEC_BLT
;
340 flags
= I915_EXEC_RENDER
|
341 (brw
->use_resource_streamer
? I915_EXEC_RESOURCE_STREAMER
: 0);
343 if (batch
->needs_sol_reset
)
344 flags
|= I915_EXEC_GEN7_SOL_RESET
;
347 if (unlikely(INTEL_DEBUG
& DEBUG_AUB
))
348 brw_annotate_aub(brw
);
350 if (brw
->hw_ctx
== NULL
|| batch
->ring
!= RENDER_RING
) {
351 assert(in_fence_fd
== -1);
352 assert(out_fence_fd
== NULL
);
353 ret
= drm_intel_bo_mrb_exec(batch
->bo
, 4 * USED_BATCH(*batch
),
356 ret
= drm_intel_gem_bo_fence_exec(batch
->bo
, brw
->hw_ctx
,
357 4 * USED_BATCH(*batch
),
358 in_fence_fd
, out_fence_fd
,
366 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
369 if (brw
->ctx
.Const
.ResetStrategy
== GL_LOSE_CONTEXT_ON_RESET_ARB
)
370 brw_check_for_reset(brw
);
373 fprintf(stderr
, "intel_do_flush_locked failed: %s\n", strerror(-ret
));
381 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
384 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
385 * of the returned fd.
388 _intel_batchbuffer_flush_fence(struct brw_context
*brw
,
389 int in_fence_fd
, int *out_fence_fd
,
390 const char *file
, int line
)
394 if (USED_BATCH(brw
->batch
) == 0)
397 if (brw
->throttle_batch
[0] == NULL
) {
398 brw
->throttle_batch
[0] = brw
->batch
.bo
;
399 drm_intel_bo_reference(brw
->throttle_batch
[0]);
402 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
403 int bytes_for_commands
= 4 * USED_BATCH(brw
->batch
);
404 int bytes_for_state
= brw
->batch
.bo
->size
- brw
->batch
.state_batch_offset
;
405 int total_bytes
= bytes_for_commands
+ bytes_for_state
;
406 fprintf(stderr
, "%s:%d: Batchbuffer flush with %4db (pkt) + "
407 "%4db (state) = %4db (%0.1f%%)\n", file
, line
,
408 bytes_for_commands
, bytes_for_state
,
410 100.0f
* total_bytes
/ BATCH_SZ
);
413 brw
->batch
.reserved_space
= 0;
415 brw_finish_batch(brw
);
417 /* Mark the end of the buffer. */
418 intel_batchbuffer_emit_dword(&brw
->batch
, MI_BATCH_BUFFER_END
);
419 if (USED_BATCH(brw
->batch
) & 1) {
420 /* Round batchbuffer usage to 2 DWORDs. */
421 intel_batchbuffer_emit_dword(&brw
->batch
, MI_NOOP
);
424 intel_upload_finish(brw
);
426 /* Check that we didn't just wrap our batchbuffer at a bad time. */
427 assert(!brw
->no_batch_wrap
);
429 ret
= do_flush_locked(brw
, in_fence_fd
, out_fence_fd
);
431 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
432 fprintf(stderr
, "waiting for idle\n");
433 drm_intel_bo_wait_rendering(brw
->batch
.bo
);
436 if (brw
->use_resource_streamer
)
437 gen7_reset_hw_bt_pool_offsets(brw
);
439 /* Start a new batch buffer. */
446 /* This is the only way buffers get added to the validate list.
449 intel_batchbuffer_reloc(struct intel_batchbuffer
*batch
,
450 drm_intel_bo
*buffer
, uint32_t offset
,
451 uint32_t read_domains
, uint32_t write_domain
,
456 ret
= drm_intel_bo_emit_reloc(batch
->bo
, offset
,
458 read_domains
, write_domain
);
462 /* Using the old buffer offset, write in what the right data would be, in
463 * case the buffer doesn't move and we can short-circuit the relocation
464 * processing in the kernel
466 return buffer
->offset64
+ delta
;
470 intel_batchbuffer_reloc64(struct intel_batchbuffer
*batch
,
471 drm_intel_bo
*buffer
, uint32_t offset
,
472 uint32_t read_domains
, uint32_t write_domain
,
475 int ret
= drm_intel_bo_emit_reloc(batch
->bo
, offset
,
477 read_domains
, write_domain
);
481 /* Using the old buffer offset, write in what the right data would be, in
482 * case the buffer doesn't move and we can short-circuit the relocation
483 * processing in the kernel
485 return buffer
->offset64
+ delta
;
490 intel_batchbuffer_data(struct brw_context
*brw
,
491 const void *data
, GLuint bytes
, enum brw_gpu_ring ring
)
493 assert((bytes
& 3) == 0);
494 intel_batchbuffer_require_space(brw
, bytes
, ring
);
495 memcpy(brw
->batch
.map_next
, data
, bytes
);
496 brw
->batch
.map_next
+= bytes
>> 2;
500 load_sized_register_mem(struct brw_context
*brw
,
503 uint32_t read_domains
, uint32_t write_domain
,
509 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
510 assert(brw
->gen
>= 7);
513 BEGIN_BATCH(4 * size
);
514 for (i
= 0; i
< size
; i
++) {
515 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (4 - 2));
516 OUT_BATCH(reg
+ i
* 4);
517 OUT_RELOC64(bo
, read_domains
, write_domain
, offset
+ i
* 4);
521 BEGIN_BATCH(3 * size
);
522 for (i
= 0; i
< size
; i
++) {
523 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
524 OUT_BATCH(reg
+ i
* 4);
525 OUT_RELOC(bo
, read_domains
, write_domain
, offset
+ i
* 4);
532 brw_load_register_mem(struct brw_context
*brw
,
535 uint32_t read_domains
, uint32_t write_domain
,
538 load_sized_register_mem(brw
, reg
, bo
, read_domains
, write_domain
, offset
, 1);
542 brw_load_register_mem64(struct brw_context
*brw
,
545 uint32_t read_domains
, uint32_t write_domain
,
548 load_sized_register_mem(brw
, reg
, bo
, read_domains
, write_domain
, offset
, 2);
552 * Write an arbitrary 32-bit register to a buffer via MI_STORE_REGISTER_MEM.
555 brw_store_register_mem32(struct brw_context
*brw
,
556 drm_intel_bo
*bo
, uint32_t reg
, uint32_t offset
)
558 assert(brw
->gen
>= 6);
562 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
564 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
569 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
571 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
578 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
581 brw_store_register_mem64(struct brw_context
*brw
,
582 drm_intel_bo
*bo
, uint32_t reg
, uint32_t offset
)
584 assert(brw
->gen
>= 6);
586 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
587 * read a full 64-bit register, we need to do two of them.
591 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
593 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
595 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
596 OUT_BATCH(reg
+ sizeof(uint32_t));
597 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
598 offset
+ sizeof(uint32_t));
602 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
604 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
606 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
607 OUT_BATCH(reg
+ sizeof(uint32_t));
608 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
609 offset
+ sizeof(uint32_t));
615 * Write a 32-bit register using immediate data.
618 brw_load_register_imm32(struct brw_context
*brw
, uint32_t reg
, uint32_t imm
)
620 assert(brw
->gen
>= 6);
623 OUT_BATCH(MI_LOAD_REGISTER_IMM
| (3 - 2));
630 * Write a 64-bit register using immediate data.
633 brw_load_register_imm64(struct brw_context
*brw
, uint32_t reg
, uint64_t imm
)
635 assert(brw
->gen
>= 6);
638 OUT_BATCH(MI_LOAD_REGISTER_IMM
| (5 - 2));
640 OUT_BATCH(imm
& 0xffffffff);
642 OUT_BATCH(imm
>> 32);
647 * Copies a 32-bit register.
650 brw_load_register_reg(struct brw_context
*brw
, uint32_t src
, uint32_t dest
)
652 assert(brw
->gen
>= 8 || brw
->is_haswell
);
655 OUT_BATCH(MI_LOAD_REGISTER_REG
| (3 - 2));
662 * Copies a 64-bit register.
665 brw_load_register_reg64(struct brw_context
*brw
, uint32_t src
, uint32_t dest
)
667 assert(brw
->gen
>= 8 || brw
->is_haswell
);
670 OUT_BATCH(MI_LOAD_REGISTER_REG
| (3 - 2));
673 OUT_BATCH(MI_LOAD_REGISTER_REG
| (3 - 2));
674 OUT_BATCH(src
+ sizeof(uint32_t));
675 OUT_BATCH(dest
+ sizeof(uint32_t));
680 * Write 32-bits of immediate data to a GPU memory buffer.
683 brw_store_data_imm32(struct brw_context
*brw
, drm_intel_bo
*bo
,
684 uint32_t offset
, uint32_t imm
)
686 assert(brw
->gen
>= 6);
689 OUT_BATCH(MI_STORE_DATA_IMM
| (4 - 2));
691 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
694 OUT_BATCH(0); /* MBZ */
695 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
703 * Write 64-bits of immediate data to a GPU memory buffer.
706 brw_store_data_imm64(struct brw_context
*brw
, drm_intel_bo
*bo
,
707 uint32_t offset
, uint64_t imm
)
709 assert(brw
->gen
>= 6);
712 OUT_BATCH(MI_STORE_DATA_IMM
| (5 - 2));
714 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
717 OUT_BATCH(0); /* MBZ */
718 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
721 OUT_BATCH(imm
& 0xffffffffu
);
722 OUT_BATCH(imm
>> 32);