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 "brw_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"
34 #include "common/gen_decoder.h"
36 #include "util/hash_table.h"
41 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
44 intel_batchbuffer_reset(struct intel_batchbuffer
*batch
,
45 struct brw_bufmgr
*bufmgr
,
49 uint_key_compare(const void *a
, const void *b
)
55 uint_key_hash(const void *key
)
57 return (uintptr_t) key
;
61 intel_batchbuffer_init(struct intel_batchbuffer
*batch
,
62 struct brw_bufmgr
*bufmgr
,
65 intel_batchbuffer_reset(batch
, bufmgr
, has_llc
);
68 batch
->cpu_map
= malloc(BATCH_SZ
);
69 batch
->map
= batch
->cpu_map
;
70 batch
->map_next
= batch
->cpu_map
;
73 batch
->reloc_count
= 0;
74 batch
->reloc_array_size
= 250;
75 batch
->relocs
= malloc(batch
->reloc_array_size
*
76 sizeof(struct drm_i915_gem_relocation_entry
));
77 batch
->exec_count
= 0;
78 batch
->exec_array_size
= 100;
80 malloc(batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
82 malloc(batch
->exec_array_size
* sizeof(batch
->exec_objects
[0]));
84 if (INTEL_DEBUG
& DEBUG_BATCH
) {
85 batch
->state_batch_sizes
=
86 _mesa_hash_table_create(NULL
, uint_key_hash
, uint_key_compare
);
91 intel_batchbuffer_reset(struct intel_batchbuffer
*batch
,
92 struct brw_bufmgr
*bufmgr
,
95 if (batch
->last_bo
!= NULL
) {
96 brw_bo_unreference(batch
->last_bo
);
97 batch
->last_bo
= NULL
;
99 batch
->last_bo
= batch
->bo
;
101 batch
->bo
= brw_bo_alloc(bufmgr
, "batchbuffer", BATCH_SZ
, 4096);
103 brw_bo_map(NULL
, batch
->bo
, true);
104 batch
->map
= batch
->bo
->virtual;
106 batch
->map_next
= batch
->map
;
108 batch
->reserved_space
= BATCH_RESERVED
;
109 batch
->state_batch_offset
= batch
->bo
->size
;
110 batch
->needs_sol_reset
= false;
111 batch
->state_base_address_emitted
= false;
113 /* We don't know what ring the new batch will be sent to until we see the
114 * first BEGIN_BATCH or BEGIN_BATCH_BLT. Mark it as unknown.
116 batch
->ring
= UNKNOWN_RING
;
118 if (batch
->state_batch_sizes
)
119 _mesa_hash_table_clear(batch
->state_batch_sizes
, NULL
);
123 intel_batchbuffer_reset_and_clear_render_cache(struct brw_context
*brw
)
125 intel_batchbuffer_reset(&brw
->batch
, brw
->bufmgr
, brw
->has_llc
);
126 brw_render_cache_set_clear(brw
);
130 intel_batchbuffer_save_state(struct brw_context
*brw
)
132 brw
->batch
.saved
.map_next
= brw
->batch
.map_next
;
133 brw
->batch
.saved
.reloc_count
= brw
->batch
.reloc_count
;
134 brw
->batch
.saved
.exec_count
= brw
->batch
.exec_count
;
138 intel_batchbuffer_reset_to_saved(struct brw_context
*brw
)
140 for (int i
= brw
->batch
.saved
.exec_count
;
141 i
< brw
->batch
.exec_count
; i
++) {
142 if (brw
->batch
.exec_bos
[i
] != brw
->batch
.bo
) {
143 brw_bo_unreference(brw
->batch
.exec_bos
[i
]);
146 brw
->batch
.reloc_count
= brw
->batch
.saved
.reloc_count
;
147 brw
->batch
.exec_count
= brw
->batch
.saved
.exec_count
;
149 brw
->batch
.map_next
= brw
->batch
.saved
.map_next
;
150 if (USED_BATCH(brw
->batch
) == 0)
151 brw
->batch
.ring
= UNKNOWN_RING
;
155 intel_batchbuffer_free(struct intel_batchbuffer
*batch
)
157 free(batch
->cpu_map
);
159 for (int i
= 0; i
< batch
->exec_count
; i
++) {
160 if (batch
->exec_bos
[i
] != batch
->bo
) {
161 brw_bo_unreference(batch
->exec_bos
[i
]);
165 free(batch
->exec_bos
);
166 free(batch
->exec_objects
);
168 brw_bo_unreference(batch
->last_bo
);
169 brw_bo_unreference(batch
->bo
);
170 if (batch
->state_batch_sizes
)
171 _mesa_hash_table_destroy(batch
->state_batch_sizes
, NULL
);
175 intel_batchbuffer_require_space(struct brw_context
*brw
, GLuint sz
,
176 enum brw_gpu_ring ring
)
178 /* If we're switching rings, implicitly flush the batch. */
179 if (unlikely(ring
!= brw
->batch
.ring
) && brw
->batch
.ring
!= UNKNOWN_RING
&&
181 intel_batchbuffer_flush(brw
);
185 assert(sz
< BATCH_SZ
- BATCH_RESERVED
);
187 if (intel_batchbuffer_space(&brw
->batch
) < sz
)
188 intel_batchbuffer_flush(brw
);
190 /* The intel_batchbuffer_flush() calls above might have changed
191 * brw->batch.ring to UNKNOWN_RING, so we need to set it here at the end.
193 brw
->batch
.ring
= ring
;
198 #define BLUE_HEADER CSI "0;44m"
199 #define NORMAL CSI "0m"
203 decode_struct(struct brw_context
*brw
, struct gen_spec
*spec
,
204 const char *struct_name
, uint32_t *data
,
205 uint32_t gtt_offset
, uint32_t offset
, bool color
)
207 struct gen_group
*group
= gen_spec_find_struct(spec
, struct_name
);
211 fprintf(stderr
, "%s\n", struct_name
);
212 gen_print_group(stderr
, group
, gtt_offset
+ offset
,
213 &data
[offset
/ 4], color
);
217 decode_structs(struct brw_context
*brw
, struct gen_spec
*spec
,
218 const char *struct_name
,
219 uint32_t *data
, uint32_t gtt_offset
, uint32_t offset
,
220 int struct_size
, bool color
)
222 struct gen_group
*group
= gen_spec_find_struct(spec
, struct_name
);
226 int entries
= brw_state_batch_size(brw
, offset
) / struct_size
;
227 for (int i
= 0; i
< entries
; i
++) {
228 fprintf(stderr
, "%s %d\n", struct_name
, i
);
229 gen_print_group(stderr
, group
, gtt_offset
+ offset
,
230 &data
[(offset
+ i
* struct_size
) / 4], color
);
235 do_batch_dump(struct brw_context
*brw
)
237 struct intel_batchbuffer
*batch
= &brw
->batch
;
238 struct gen_spec
*spec
= gen_spec_load(&brw
->screen
->devinfo
);
240 if (batch
->ring
!= RENDER_RING
)
243 int ret
= brw_bo_map(brw
, batch
->bo
, false);
246 "WARNING: failed to map batchbuffer (%s), "
247 "dumping uploaded data instead.\n", strerror(ret
));
250 uint32_t *data
= batch
->bo
->virtual ? batch
->bo
->virtual : batch
->map
;
251 uint32_t *end
= data
+ USED_BATCH(*batch
);
252 uint32_t gtt_offset
= batch
->bo
->virtual ? batch
->bo
->offset64
: 0;
255 bool color
= INTEL_DEBUG
& DEBUG_COLOR
;
256 const char *header_color
= color
? BLUE_HEADER
: "";
257 const char *reset_color
= color
? NORMAL
: "";
259 for (uint32_t *p
= data
; p
< end
; p
+= length
) {
260 struct gen_group
*inst
= gen_spec_find_instruction(spec
, p
);
261 length
= gen_group_get_length(inst
, p
);
262 assert(inst
== NULL
|| length
> 0);
263 length
= MAX2(1, length
);
265 fprintf(stderr
, "unknown instruction %08x\n", p
[0]);
269 uint64_t offset
= gtt_offset
+ 4 * (p
- data
);
271 fprintf(stderr
, "%s0x%08"PRIx64
": 0x%08x: %-80s%s\n", header_color
,
272 offset
, p
[0], gen_group_get_name(inst
), reset_color
);
274 gen_print_group(stderr
, inst
, offset
, p
, color
);
276 switch (gen_group_get_opcode(inst
) >> 16) {
277 case _3DSTATE_PIPELINED_POINTERS
:
278 /* TODO: Decode Gen4-5 pipelined pointers */
280 case _3DSTATE_BINDING_TABLE_POINTERS_VS
:
281 case _3DSTATE_BINDING_TABLE_POINTERS_HS
:
282 case _3DSTATE_BINDING_TABLE_POINTERS_DS
:
283 case _3DSTATE_BINDING_TABLE_POINTERS_GS
:
284 case _3DSTATE_BINDING_TABLE_POINTERS_PS
: {
285 struct gen_group
*group
=
286 gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
290 uint32_t bt_offset
= p
[1] & ~0x1fu
;
291 int bt_entries
= brw_state_batch_size(brw
, bt_offset
) / 4;
292 uint32_t *bt_pointers
= &data
[bt_offset
/ 4];
293 for (int i
= 0; i
< bt_entries
; i
++) {
294 fprintf(stderr
, "SURFACE_STATE - BTI = %d\n", i
);
295 gen_print_group(stderr
, group
, gtt_offset
+ bt_pointers
[i
],
296 &data
[bt_pointers
[i
] / 4], color
);
300 case _3DSTATE_SAMPLER_STATE_POINTERS_VS
:
301 case _3DSTATE_SAMPLER_STATE_POINTERS_HS
:
302 case _3DSTATE_SAMPLER_STATE_POINTERS_DS
:
303 case _3DSTATE_SAMPLER_STATE_POINTERS_GS
:
304 case _3DSTATE_SAMPLER_STATE_POINTERS_PS
:
305 decode_structs(brw
, spec
, "SAMPLER_STATE", data
,
306 gtt_offset
, p
[1] & ~0x1fu
, 4 * 4, color
);
308 case _3DSTATE_VIEWPORT_STATE_POINTERS
:
309 decode_structs(brw
, spec
, "CLIP_VIEWPORT", data
,
310 gtt_offset
, p
[1] & ~0x3fu
, 4 * 4, color
);
311 decode_structs(brw
, spec
, "SF_VIEWPORT", data
,
312 gtt_offset
, p
[1] & ~0x3fu
, 8 * 4, color
);
313 decode_structs(brw
, spec
, "CC_VIEWPORT", data
,
314 gtt_offset
, p
[3] & ~0x3fu
, 2 * 4, color
);
316 case _3DSTATE_VIEWPORT_STATE_POINTERS_CC
:
317 decode_structs(brw
, spec
, "CC_VIEWPORT", data
,
318 gtt_offset
, p
[1] & ~0x3fu
, 2 * 4, color
);
320 case _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CL
:
321 decode_structs(brw
, spec
, "SF_CLIP_VIEWPORT", data
,
322 gtt_offset
, p
[1] & ~0x3fu
, 16 * 4, color
);
324 case _3DSTATE_SCISSOR_STATE_POINTERS
:
325 decode_structs(brw
, spec
, "SCISSOR_RECT", data
,
326 gtt_offset
, p
[1] & ~0x1fu
, 2 * 4, color
);
328 case _3DSTATE_BLEND_STATE_POINTERS
:
329 /* TODO: handle Gen8+ extra dword at the beginning */
330 decode_structs(brw
, spec
, "BLEND_STATE", data
,
331 gtt_offset
, p
[1] & ~0x3fu
, 8 * 4, color
);
333 case _3DSTATE_CC_STATE_POINTERS
:
335 decode_struct(brw
, spec
, "COLOR_CALC_STATE", data
,
336 gtt_offset
, p
[1] & ~0x3fu
, color
);
337 } else if (brw
->gen
== 6) {
338 decode_structs(brw
, spec
, "BLEND_STATE", data
,
339 gtt_offset
, p
[1] & ~0x3fu
, 2 * 4, color
);
340 decode_struct(brw
, spec
, "DEPTH_STENCIL_STATE", data
,
341 gtt_offset
, p
[2] & ~0x3fu
, color
);
342 decode_struct(brw
, spec
, "COLOR_CALC_STATE", data
,
343 gtt_offset
, p
[3] & ~0x3fu
, color
);
346 case _3DSTATE_DEPTH_STENCIL_STATE_POINTERS
:
347 decode_struct(brw
, spec
, "DEPTH_STENCIL_STATE", data
,
348 gtt_offset
, p
[1] & ~0x3fu
, color
);
354 brw_bo_unmap(batch
->bo
);
358 static void do_batch_dump(struct brw_context
*brw
) { }
362 * Called when starting a new batch buffer.
365 brw_new_batch(struct brw_context
*brw
)
367 /* Unreference any BOs held by the previous batch, and reset counts. */
368 for (int i
= 0; i
< brw
->batch
.exec_count
; i
++) {
369 if (brw
->batch
.exec_bos
[i
] != brw
->batch
.bo
) {
370 brw_bo_unreference(brw
->batch
.exec_bos
[i
]);
372 brw
->batch
.exec_bos
[i
] = NULL
;
374 brw
->batch
.reloc_count
= 0;
375 brw
->batch
.exec_count
= 0;
376 brw
->batch
.aperture_space
= BATCH_SZ
;
378 /* Create a new batchbuffer and reset the associated state: */
379 intel_batchbuffer_reset_and_clear_render_cache(brw
);
381 /* If the kernel supports hardware contexts, then most hardware state is
382 * preserved between batches; we only need to re-emit state that is required
383 * to be in every batch. Otherwise we need to re-emit all the state that
384 * would otherwise be stored in the context (which for all intents and
385 * purposes means everything).
387 if (brw
->hw_ctx
== 0)
388 brw
->ctx
.NewDriverState
|= BRW_NEW_CONTEXT
;
390 brw
->ctx
.NewDriverState
|= BRW_NEW_BATCH
;
394 /* We need to periodically reap the shader time results, because rollover
395 * happens every few seconds. We also want to see results every once in a
396 * while, because many programs won't cleanly destroy our context, so the
397 * end-of-run printout may not happen.
399 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
400 brw_collect_and_report_shader_time(brw
);
404 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
407 * This function can emit state (say, to preserve registers that aren't saved
408 * between batches). All of this state MUST fit in the reserved space at the
409 * end of the batchbuffer. If you add more GPU state, increase the reserved
410 * space by updating the BATCH_RESERVED macro.
413 brw_finish_batch(struct brw_context
*brw
)
415 /* Capture the closing pipeline statistics register values necessary to
416 * support query objects (in the non-hardware context world).
418 brw_emit_query_end(brw
);
420 if (brw
->batch
.ring
== RENDER_RING
) {
421 /* Work around L3 state leaks into contexts set MI_RESTORE_INHIBIT which
422 * assume that the L3 cache is configured according to the hardware
426 gen7_restore_default_l3_config(brw
);
428 if (brw
->is_haswell
) {
429 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
430 * 3DSTATE_CC_STATE_POINTERS > "Note":
432 * "SW must program 3DSTATE_CC_STATE_POINTERS command at the end of every
433 * 3D batch buffer followed by a PIPE_CONTROL with RC flush and CS stall."
435 * From the example in the docs, it seems to expect a regular pipe control
436 * flush here as well. We may have done it already, but meh.
438 * See also WaAvoidRCZCounterRollover.
440 brw_emit_mi_flush(brw
);
442 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS
<< 16 | (2 - 2));
443 OUT_BATCH(brw
->cc
.state_offset
| 1);
445 brw_emit_pipe_control_flush(brw
, PIPE_CONTROL_RENDER_TARGET_FLUSH
|
446 PIPE_CONTROL_CS_STALL
);
450 /* Mark that the current program cache BO has been used by the GPU.
451 * It will be reallocated if we need to put new programs in for the
454 brw
->cache
.bo_used_by_gpu
= true;
458 throttle(struct brw_context
*brw
)
460 /* Wait for the swapbuffers before the one we just emitted, so we
461 * don't get too many swaps outstanding for apps that are GPU-heavy
464 * We're using intelDRI2Flush (called from the loader before
465 * swapbuffer) and glFlush (for front buffer rendering) as the
466 * indicator that a frame is done and then throttle when we get
467 * here as we prepare to render the next frame. At this point for
468 * round trips for swap/copy and getting new buffers are done and
469 * we'll spend less time waiting on the GPU.
471 * Unfortunately, we don't have a handle to the batch containing
472 * the swap, and getting our hands on that doesn't seem worth it,
473 * so we just use the first batch we emitted after the last swap.
475 if (brw
->need_swap_throttle
&& brw
->throttle_batch
[0]) {
476 if (brw
->throttle_batch
[1]) {
477 if (!brw
->disable_throttling
) {
478 /* Pass NULL rather than brw so we avoid perf_debug warnings;
479 * stalling is common and expected here...
481 brw_bo_wait_rendering(NULL
, brw
->throttle_batch
[1]);
483 brw_bo_unreference(brw
->throttle_batch
[1]);
485 brw
->throttle_batch
[1] = brw
->throttle_batch
[0];
486 brw
->throttle_batch
[0] = NULL
;
487 brw
->need_swap_throttle
= false;
488 /* Throttling here is more precise than the throttle ioctl, so skip it */
489 brw
->need_flush_throttle
= false;
492 if (brw
->need_flush_throttle
) {
493 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
494 drmCommandNone(dri_screen
->fd
, DRM_I915_GEM_THROTTLE
);
495 brw
->need_flush_throttle
= false;
500 add_exec_bo(struct intel_batchbuffer
*batch
, struct brw_bo
*bo
)
502 if (bo
!= batch
->bo
) {
503 for (int i
= 0; i
< batch
->exec_count
; i
++) {
504 if (batch
->exec_bos
[i
] == bo
)
508 brw_bo_reference(bo
);
511 if (batch
->exec_count
== batch
->exec_array_size
) {
512 batch
->exec_array_size
*= 2;
514 realloc(batch
->exec_bos
,
515 batch
->exec_array_size
* sizeof(batch
->exec_bos
[0]));
516 batch
->exec_objects
=
517 realloc(batch
->exec_objects
,
518 batch
->exec_array_size
* sizeof(batch
->exec_objects
[0]));
521 struct drm_i915_gem_exec_object2
*validation_entry
=
522 &batch
->exec_objects
[batch
->exec_count
];
523 validation_entry
->handle
= bo
->gem_handle
;
524 if (bo
== batch
->bo
) {
525 validation_entry
->relocation_count
= batch
->reloc_count
;
526 validation_entry
->relocs_ptr
= (uintptr_t) batch
->relocs
;
528 validation_entry
->relocation_count
= 0;
529 validation_entry
->relocs_ptr
= 0;
531 validation_entry
->alignment
= bo
->align
;
532 validation_entry
->offset
= bo
->offset64
;
533 validation_entry
->flags
= 0;
534 validation_entry
->rsvd1
= 0;
535 validation_entry
->rsvd2
= 0;
537 batch
->exec_bos
[batch
->exec_count
] = bo
;
539 batch
->aperture_space
+= bo
->size
;
544 struct intel_batchbuffer
*batch
,
551 struct drm_i915_gem_execbuffer2 execbuf
= {
552 .buffers_ptr
= (uintptr_t) batch
->exec_objects
,
553 .buffer_count
= batch
->exec_count
,
554 .batch_start_offset
= 0,
557 .rsvd1
= ctx_id
, /* rsvd1 is actually the context ID */
560 unsigned long cmd
= DRM_IOCTL_I915_GEM_EXECBUFFER2
;
562 if (in_fence
!= -1) {
563 execbuf
.rsvd2
= in_fence
;
564 execbuf
.flags
|= I915_EXEC_FENCE_IN
;
567 if (out_fence
!= NULL
) {
568 cmd
= DRM_IOCTL_I915_GEM_EXECBUFFER2_WR
;
570 execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
573 int ret
= drmIoctl(fd
, cmd
, &execbuf
);
577 for (int i
= 0; i
< batch
->exec_count
; i
++) {
578 struct brw_bo
*bo
= batch
->exec_bos
[i
];
582 /* Update brw_bo::offset64 */
583 if (batch
->exec_objects
[i
].offset
!= bo
->offset64
) {
584 DBG("BO %d migrated: 0x%" PRIx64
" -> 0x%llx\n",
585 bo
->gem_handle
, bo
->offset64
, batch
->exec_objects
[i
].offset
);
586 bo
->offset64
= batch
->exec_objects
[i
].offset
;
590 if (ret
== 0 && out_fence
!= NULL
)
591 *out_fence
= execbuf
.rsvd2
>> 32;
597 do_flush_locked(struct brw_context
*brw
, int in_fence_fd
, int *out_fence_fd
)
599 __DRIscreen
*dri_screen
= brw
->screen
->driScrnPriv
;
600 struct intel_batchbuffer
*batch
= &brw
->batch
;
604 brw_bo_unmap(batch
->bo
);
606 ret
= brw_bo_subdata(batch
->bo
, 0, 4 * USED_BATCH(*batch
), batch
->map
);
607 if (ret
== 0 && batch
->state_batch_offset
!= batch
->bo
->size
) {
608 ret
= brw_bo_subdata(batch
->bo
,
609 batch
->state_batch_offset
,
610 batch
->bo
->size
- batch
->state_batch_offset
,
611 (char *)batch
->map
+ batch
->state_batch_offset
);
615 if (!brw
->screen
->no_hw
) {
618 if (brw
->gen
>= 6 && batch
->ring
== BLT_RING
) {
619 flags
= I915_EXEC_BLT
;
621 flags
= I915_EXEC_RENDER
;
623 if (batch
->needs_sol_reset
)
624 flags
|= I915_EXEC_GEN7_SOL_RESET
;
627 uint32_t hw_ctx
= batch
->ring
== RENDER_RING
? brw
->hw_ctx
: 0;
629 /* Add the batch itself to the end of the validation list */
630 add_exec_bo(batch
, batch
->bo
);
632 ret
= execbuffer(dri_screen
->fd
, batch
, hw_ctx
,
633 4 * USED_BATCH(*batch
),
634 in_fence_fd
, out_fence_fd
, flags
);
640 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
643 if (brw
->ctx
.Const
.ResetStrategy
== GL_LOSE_CONTEXT_ON_RESET_ARB
)
644 brw_check_for_reset(brw
);
647 fprintf(stderr
, "intel_do_flush_locked failed: %s\n", strerror(-ret
));
655 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
658 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
659 * of the returned fd.
662 _intel_batchbuffer_flush_fence(struct brw_context
*brw
,
663 int in_fence_fd
, int *out_fence_fd
,
664 const char *file
, int line
)
668 if (USED_BATCH(brw
->batch
) == 0)
671 if (brw
->throttle_batch
[0] == NULL
) {
672 brw
->throttle_batch
[0] = brw
->batch
.bo
;
673 brw_bo_reference(brw
->throttle_batch
[0]);
676 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
677 int bytes_for_commands
= 4 * USED_BATCH(brw
->batch
);
678 int bytes_for_state
= brw
->batch
.bo
->size
- brw
->batch
.state_batch_offset
;
679 int total_bytes
= bytes_for_commands
+ bytes_for_state
;
680 fprintf(stderr
, "%s:%d: Batchbuffer flush with %4db (pkt) + "
681 "%4db (state) = %4db (%0.1f%%)\n", file
, line
,
682 bytes_for_commands
, bytes_for_state
,
684 100.0f
* total_bytes
/ BATCH_SZ
);
687 brw
->batch
.reserved_space
= 0;
689 brw_finish_batch(brw
);
691 /* Mark the end of the buffer. */
692 intel_batchbuffer_emit_dword(&brw
->batch
, MI_BATCH_BUFFER_END
);
693 if (USED_BATCH(brw
->batch
) & 1) {
694 /* Round batchbuffer usage to 2 DWORDs. */
695 intel_batchbuffer_emit_dword(&brw
->batch
, MI_NOOP
);
698 intel_upload_finish(brw
);
700 /* Check that we didn't just wrap our batchbuffer at a bad time. */
701 assert(!brw
->no_batch_wrap
);
703 ret
= do_flush_locked(brw
, in_fence_fd
, out_fence_fd
);
705 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
706 fprintf(stderr
, "waiting for idle\n");
707 brw_bo_wait_rendering(brw
, brw
->batch
.bo
);
710 /* Start a new batch buffer. */
717 brw_batch_has_aperture_space(struct brw_context
*brw
, unsigned extra_space
)
719 return brw
->batch
.aperture_space
+ extra_space
<=
720 brw
->screen
->aperture_threshold
;
724 brw_batch_references(struct intel_batchbuffer
*batch
, struct brw_bo
*bo
)
726 for (int i
= 0; i
< batch
->exec_count
; i
++) {
727 if (batch
->exec_bos
[i
] == bo
)
733 /* This is the only way buffers get added to the validate list.
736 brw_emit_reloc(struct intel_batchbuffer
*batch
, uint32_t batch_offset
,
737 struct brw_bo
*target
, uint32_t target_offset
,
738 uint32_t read_domains
, uint32_t write_domain
)
742 if (batch
->reloc_count
== batch
->reloc_array_size
) {
743 batch
->reloc_array_size
*= 2;
744 batch
->relocs
= realloc(batch
->relocs
,
745 batch
->reloc_array_size
*
746 sizeof(struct drm_i915_gem_relocation_entry
));
750 assert(batch_offset
<= BATCH_SZ
- sizeof(uint32_t));
751 assert(_mesa_bitcount(write_domain
) <= 1);
753 if (target
!= batch
->bo
)
754 add_exec_bo(batch
, target
);
756 struct drm_i915_gem_relocation_entry
*reloc
=
757 &batch
->relocs
[batch
->reloc_count
];
759 batch
->reloc_count
++;
761 /* ensure gcc doesn't reload */
762 offset64
= *((volatile uint64_t *)&target
->offset64
);
763 reloc
->offset
= batch_offset
;
764 reloc
->delta
= target_offset
;
765 reloc
->target_handle
= target
->gem_handle
;
766 reloc
->read_domains
= read_domains
;
767 reloc
->write_domain
= write_domain
;
768 reloc
->presumed_offset
= offset64
;
770 /* Using the old buffer offset, write in what the right data would be, in
771 * case the buffer doesn't move and we can short-circuit the relocation
772 * processing in the kernel
774 return offset64
+ target_offset
;
778 intel_batchbuffer_data(struct brw_context
*brw
,
779 const void *data
, GLuint bytes
, enum brw_gpu_ring ring
)
781 assert((bytes
& 3) == 0);
782 intel_batchbuffer_require_space(brw
, bytes
, ring
);
783 memcpy(brw
->batch
.map_next
, data
, bytes
);
784 brw
->batch
.map_next
+= bytes
>> 2;
788 load_sized_register_mem(struct brw_context
*brw
,
791 uint32_t read_domains
, uint32_t write_domain
,
797 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
798 assert(brw
->gen
>= 7);
801 BEGIN_BATCH(4 * size
);
802 for (i
= 0; i
< size
; i
++) {
803 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (4 - 2));
804 OUT_BATCH(reg
+ i
* 4);
805 OUT_RELOC64(bo
, read_domains
, write_domain
, offset
+ i
* 4);
809 BEGIN_BATCH(3 * size
);
810 for (i
= 0; i
< size
; i
++) {
811 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
812 OUT_BATCH(reg
+ i
* 4);
813 OUT_RELOC(bo
, read_domains
, write_domain
, offset
+ i
* 4);
820 brw_load_register_mem(struct brw_context
*brw
,
823 uint32_t read_domains
, uint32_t write_domain
,
826 load_sized_register_mem(brw
, reg
, bo
, read_domains
, write_domain
, offset
, 1);
830 brw_load_register_mem64(struct brw_context
*brw
,
833 uint32_t read_domains
, uint32_t write_domain
,
836 load_sized_register_mem(brw
, reg
, bo
, read_domains
, write_domain
, offset
, 2);
840 * Write an arbitrary 32-bit register to a buffer via MI_STORE_REGISTER_MEM.
843 brw_store_register_mem32(struct brw_context
*brw
,
844 struct brw_bo
*bo
, uint32_t reg
, uint32_t offset
)
846 assert(brw
->gen
>= 6);
850 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
852 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
857 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
859 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
866 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
869 brw_store_register_mem64(struct brw_context
*brw
,
870 struct brw_bo
*bo
, uint32_t reg
, uint32_t offset
)
872 assert(brw
->gen
>= 6);
874 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
875 * read a full 64-bit register, we need to do two of them.
879 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
881 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
883 OUT_BATCH(MI_STORE_REGISTER_MEM
| (4 - 2));
884 OUT_BATCH(reg
+ sizeof(uint32_t));
885 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
886 offset
+ sizeof(uint32_t));
890 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
892 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
894 OUT_BATCH(MI_STORE_REGISTER_MEM
| (3 - 2));
895 OUT_BATCH(reg
+ sizeof(uint32_t));
896 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
897 offset
+ sizeof(uint32_t));
903 * Write a 32-bit register using immediate data.
906 brw_load_register_imm32(struct brw_context
*brw
, uint32_t reg
, uint32_t imm
)
908 assert(brw
->gen
>= 6);
911 OUT_BATCH(MI_LOAD_REGISTER_IMM
| (3 - 2));
918 * Write a 64-bit register using immediate data.
921 brw_load_register_imm64(struct brw_context
*brw
, uint32_t reg
, uint64_t imm
)
923 assert(brw
->gen
>= 6);
926 OUT_BATCH(MI_LOAD_REGISTER_IMM
| (5 - 2));
928 OUT_BATCH(imm
& 0xffffffff);
930 OUT_BATCH(imm
>> 32);
935 * Copies a 32-bit register.
938 brw_load_register_reg(struct brw_context
*brw
, uint32_t src
, uint32_t dest
)
940 assert(brw
->gen
>= 8 || brw
->is_haswell
);
943 OUT_BATCH(MI_LOAD_REGISTER_REG
| (3 - 2));
950 * Copies a 64-bit register.
953 brw_load_register_reg64(struct brw_context
*brw
, uint32_t src
, uint32_t dest
)
955 assert(brw
->gen
>= 8 || brw
->is_haswell
);
958 OUT_BATCH(MI_LOAD_REGISTER_REG
| (3 - 2));
961 OUT_BATCH(MI_LOAD_REGISTER_REG
| (3 - 2));
962 OUT_BATCH(src
+ sizeof(uint32_t));
963 OUT_BATCH(dest
+ sizeof(uint32_t));
968 * Write 32-bits of immediate data to a GPU memory buffer.
971 brw_store_data_imm32(struct brw_context
*brw
, struct brw_bo
*bo
,
972 uint32_t offset
, uint32_t imm
)
974 assert(brw
->gen
>= 6);
977 OUT_BATCH(MI_STORE_DATA_IMM
| (4 - 2));
979 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
982 OUT_BATCH(0); /* MBZ */
983 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
991 * Write 64-bits of immediate data to a GPU memory buffer.
994 brw_store_data_imm64(struct brw_context
*brw
, struct brw_bo
*bo
,
995 uint32_t offset
, uint64_t imm
)
997 assert(brw
->gen
>= 6);
1000 OUT_BATCH(MI_STORE_DATA_IMM
| (5 - 2));
1002 OUT_RELOC64(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
1005 OUT_BATCH(0); /* MBZ */
1006 OUT_RELOC(bo
, I915_GEM_DOMAIN_INSTRUCTION
, I915_GEM_DOMAIN_INSTRUCTION
,
1009 OUT_BATCH(imm
& 0xffffffffu
);
1010 OUT_BATCH(imm
>> 32);