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"
40 intel_batchbuffer_reset(struct brw_context
*brw
);
43 intel_batchbuffer_init(struct brw_context
*brw
)
45 intel_batchbuffer_reset(brw
);
48 /* We can't just use brw_state_batch to get a chunk of space for
49 * the gen6 workaround because it involves actually writing to
50 * the buffer, and the kernel doesn't let us write to the batch.
52 brw
->batch
.workaround_bo
= drm_intel_bo_alloc(brw
->bufmgr
,
53 "pipe_control workaround",
58 brw
->batch
.cpu_map
= malloc(BATCH_SZ
);
59 brw
->batch
.map
= brw
->batch
.cpu_map
;
64 intel_batchbuffer_reset(struct brw_context
*brw
)
66 if (brw
->batch
.last_bo
!= NULL
) {
67 drm_intel_bo_unreference(brw
->batch
.last_bo
);
68 brw
->batch
.last_bo
= NULL
;
70 brw
->batch
.last_bo
= brw
->batch
.bo
;
72 brw_render_cache_set_clear(brw
);
74 brw
->batch
.bo
= drm_intel_bo_alloc(brw
->bufmgr
, "batchbuffer",
77 drm_intel_bo_map(brw
->batch
.bo
, true);
78 brw
->batch
.map
= brw
->batch
.bo
->virtual;
81 brw
->batch
.reserved_space
= BATCH_RESERVED
;
82 brw
->batch
.state_batch_offset
= brw
->batch
.bo
->size
;
84 brw
->batch
.needs_sol_reset
= false;
85 brw
->batch
.pipe_controls_since_last_cs_stall
= 0;
87 /* We don't know what ring the new batch will be sent to until we see the
88 * first BEGIN_BATCH or BEGIN_BATCH_BLT. Mark it as unknown.
90 brw
->batch
.ring
= UNKNOWN_RING
;
94 intel_batchbuffer_save_state(struct brw_context
*brw
)
96 brw
->batch
.saved
.used
= brw
->batch
.used
;
97 brw
->batch
.saved
.reloc_count
=
98 drm_intel_gem_bo_get_reloc_count(brw
->batch
.bo
);
102 intel_batchbuffer_reset_to_saved(struct brw_context
*brw
)
104 drm_intel_gem_bo_clear_relocs(brw
->batch
.bo
, brw
->batch
.saved
.reloc_count
);
106 brw
->batch
.used
= brw
->batch
.saved
.used
;
107 if (brw
->batch
.used
== 0)
108 brw
->batch
.ring
= UNKNOWN_RING
;
112 intel_batchbuffer_free(struct brw_context
*brw
)
114 free(brw
->batch
.cpu_map
);
115 drm_intel_bo_unreference(brw
->batch
.last_bo
);
116 drm_intel_bo_unreference(brw
->batch
.bo
);
117 drm_intel_bo_unreference(brw
->batch
.workaround_bo
);
121 do_batch_dump(struct brw_context
*brw
)
123 struct drm_intel_decode
*decode
;
124 struct intel_batchbuffer
*batch
= &brw
->batch
;
127 decode
= drm_intel_decode_context_alloc(brw
->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_set_output_file(decode
, stderr
);
149 drm_intel_decode(decode
);
151 drm_intel_decode_context_free(decode
);
154 drm_intel_bo_unmap(batch
->bo
);
156 brw_debug_batch(brw
);
161 intel_batchbuffer_emit_render_ring_prelude(struct brw_context
*brw
)
163 /* We may need to enable and snapshot OA counters. */
164 brw_perf_monitor_new_batch(brw
);
168 * Called when starting a new batch buffer.
171 brw_new_batch(struct brw_context
*brw
)
173 /* Create a new batchbuffer and reset the associated state: */
174 drm_intel_gem_bo_clear_relocs(brw
->batch
.bo
, 0);
175 intel_batchbuffer_reset(brw
);
177 /* If the kernel supports hardware contexts, then most hardware state is
178 * preserved between batches; we only need to re-emit state that is required
179 * to be in every batch. Otherwise we need to re-emit all the state that
180 * would otherwise be stored in the context (which for all intents and
181 * purposes means everything).
183 if (brw
->hw_ctx
== NULL
)
184 brw
->ctx
.NewDriverState
|= BRW_NEW_CONTEXT
;
186 brw
->ctx
.NewDriverState
|= BRW_NEW_BATCH
;
188 brw
->state_batch_count
= 0;
192 /* We need to periodically reap the shader time results, because rollover
193 * happens every few seconds. We also want to see results every once in a
194 * while, because many programs won't cleanly destroy our context, so the
195 * end-of-run printout may not happen.
197 if (INTEL_DEBUG
& DEBUG_SHADER_TIME
)
198 brw_collect_and_report_shader_time(brw
);
200 if (INTEL_DEBUG
& DEBUG_PERFMON
)
201 brw_dump_perf_monitors(brw
);
205 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
208 * This function can emit state (say, to preserve registers that aren't saved
209 * between batches). All of this state MUST fit in the reserved space at the
210 * end of the batchbuffer. If you add more GPU state, increase the reserved
211 * space by updating the BATCH_RESERVED macro.
214 brw_finish_batch(struct brw_context
*brw
)
216 /* Capture the closing pipeline statistics register values necessary to
217 * support query objects (in the non-hardware context world).
219 brw_emit_query_end(brw
);
221 /* We may also need to snapshot and disable OA counters. */
222 if (brw
->batch
.ring
== RENDER_RING
)
223 brw_perf_monitor_finish_batch(brw
);
225 /* Mark that the current program cache BO has been used by the GPU.
226 * It will be reallocated if we need to put new programs in for the
229 brw
->cache
.bo_used_by_gpu
= true;
233 throttle(struct brw_context
*brw
)
235 /* Wait for the swapbuffers before the one we just emitted, so we
236 * don't get too many swaps outstanding for apps that are GPU-heavy
239 * We're using intelDRI2Flush (called from the loader before
240 * swapbuffer) and glFlush (for front buffer rendering) as the
241 * indicator that a frame is done and then throttle when we get
242 * here as we prepare to render the next frame. At this point for
243 * round trips for swap/copy and getting new buffers are done and
244 * we'll spend less time waiting on the GPU.
246 * Unfortunately, we don't have a handle to the batch containing
247 * the swap, and getting our hands on that doesn't seem worth it,
248 * so we just use the first batch we emitted after the last swap.
250 if (brw
->need_swap_throttle
&& brw
->throttle_batch
[0]) {
251 if (brw
->throttle_batch
[1]) {
252 if (!brw
->disable_throttling
)
253 drm_intel_bo_wait_rendering(brw
->throttle_batch
[1]);
254 drm_intel_bo_unreference(brw
->throttle_batch
[1]);
256 brw
->throttle_batch
[1] = brw
->throttle_batch
[0];
257 brw
->throttle_batch
[0] = NULL
;
258 brw
->need_swap_throttle
= false;
259 /* Throttling here is more precise than the throttle ioctl, so skip it */
260 brw
->need_flush_throttle
= false;
263 if (brw
->need_flush_throttle
) {
264 __DRIscreen
*psp
= brw
->intelScreen
->driScrnPriv
;
265 drmCommandNone(psp
->fd
, DRM_I915_GEM_THROTTLE
);
266 brw
->need_flush_throttle
= false;
270 /* TODO: Push this whole function into bufmgr.
273 do_flush_locked(struct brw_context
*brw
)
275 struct intel_batchbuffer
*batch
= &brw
->batch
;
279 drm_intel_bo_unmap(batch
->bo
);
281 ret
= drm_intel_bo_subdata(batch
->bo
, 0, 4*batch
->used
, batch
->map
);
282 if (ret
== 0 && batch
->state_batch_offset
!= batch
->bo
->size
) {
283 ret
= drm_intel_bo_subdata(batch
->bo
,
284 batch
->state_batch_offset
,
285 batch
->bo
->size
- batch
->state_batch_offset
,
286 (char *)batch
->map
+ batch
->state_batch_offset
);
290 if (!brw
->intelScreen
->no_hw
) {
293 if (brw
->gen
>= 6 && batch
->ring
== BLT_RING
) {
294 flags
= I915_EXEC_BLT
;
296 flags
= I915_EXEC_RENDER
;
298 if (batch
->needs_sol_reset
)
299 flags
|= I915_EXEC_GEN7_SOL_RESET
;
302 if (unlikely(INTEL_DEBUG
& DEBUG_AUB
))
303 brw_annotate_aub(brw
);
305 if (brw
->hw_ctx
== NULL
|| batch
->ring
!= RENDER_RING
) {
306 ret
= drm_intel_bo_mrb_exec(batch
->bo
, 4 * batch
->used
, NULL
, 0, 0,
309 ret
= drm_intel_gem_bo_context_exec(batch
->bo
, brw
->hw_ctx
,
310 4 * batch
->used
, flags
);
317 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
))
321 fprintf(stderr
, "intel_do_flush_locked failed: %s\n", strerror(-ret
));
329 _intel_batchbuffer_flush(struct brw_context
*brw
,
330 const char *file
, int line
)
334 if (brw
->batch
.used
== 0)
337 if (brw
->throttle_batch
[0] == NULL
) {
338 brw
->throttle_batch
[0] = brw
->batch
.bo
;
339 drm_intel_bo_reference(brw
->throttle_batch
[0]);
342 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
343 int bytes_for_commands
= 4 * brw
->batch
.used
;
344 int bytes_for_state
= brw
->batch
.bo
->size
- brw
->batch
.state_batch_offset
;
345 int total_bytes
= bytes_for_commands
+ bytes_for_state
;
346 fprintf(stderr
, "%s:%d: Batchbuffer flush with %4db (pkt) + "
347 "%4db (state) = %4db (%0.1f%%)\n", file
, line
,
348 bytes_for_commands
, bytes_for_state
,
350 100.0f
* total_bytes
/ BATCH_SZ
);
353 brw
->batch
.reserved_space
= 0;
355 brw_finish_batch(brw
);
357 /* Mark the end of the buffer. */
358 intel_batchbuffer_emit_dword(brw
, MI_BATCH_BUFFER_END
);
359 if (brw
->batch
.used
& 1) {
360 /* Round batchbuffer usage to 2 DWORDs. */
361 intel_batchbuffer_emit_dword(brw
, MI_NOOP
);
364 intel_upload_finish(brw
);
366 /* Check that we didn't just wrap our batchbuffer at a bad time. */
367 assert(!brw
->no_batch_wrap
);
369 ret
= do_flush_locked(brw
);
371 if (unlikely(INTEL_DEBUG
& DEBUG_SYNC
)) {
372 fprintf(stderr
, "waiting for idle\n");
373 drm_intel_bo_wait_rendering(brw
->batch
.bo
);
376 /* Start a new batch buffer. */
383 /* This is the only way buffers get added to the validate list.
386 intel_batchbuffer_emit_reloc(struct brw_context
*brw
,
387 drm_intel_bo
*buffer
,
388 uint32_t read_domains
, uint32_t write_domain
,
393 ret
= drm_intel_bo_emit_reloc(brw
->batch
.bo
, 4*brw
->batch
.used
,
395 read_domains
, write_domain
);
399 /* Using the old buffer offset, write in what the right data would be, in
400 * case the buffer doesn't move and we can short-circuit the relocation
401 * processing in the kernel
403 intel_batchbuffer_emit_dword(brw
, buffer
->offset64
+ delta
);
409 intel_batchbuffer_emit_reloc64(struct brw_context
*brw
,
410 drm_intel_bo
*buffer
,
411 uint32_t read_domains
, uint32_t write_domain
,
414 int ret
= drm_intel_bo_emit_reloc(brw
->batch
.bo
, 4*brw
->batch
.used
,
416 read_domains
, write_domain
);
420 /* Using the old buffer offset, write in what the right data would be, in
421 * case the buffer doesn't move and we can short-circuit the relocation
422 * processing in the kernel
424 uint64_t offset
= buffer
->offset64
+ delta
;
425 intel_batchbuffer_emit_dword(brw
, offset
);
426 intel_batchbuffer_emit_dword(brw
, offset
>> 32);
433 intel_batchbuffer_data(struct brw_context
*brw
,
434 const void *data
, GLuint bytes
, enum brw_gpu_ring ring
)
436 assert((bytes
& 3) == 0);
437 intel_batchbuffer_require_space(brw
, bytes
, ring
);
438 memcpy(brw
->batch
.map
+ brw
->batch
.used
, data
, bytes
);
439 brw
->batch
.used
+= bytes
>> 2;
443 load_sized_register_mem(struct brw_context
*brw
,
446 uint32_t read_domains
, uint32_t write_domain
,
452 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
453 assert(brw
->gen
>= 7);
456 BEGIN_BATCH(4 * size
);
457 for (i
= 0; i
< size
; i
++) {
458 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (4 - 2));
459 OUT_BATCH(reg
+ i
* 4);
460 OUT_RELOC64(bo
, read_domains
, write_domain
, offset
+ i
* 4);
464 BEGIN_BATCH(3 * size
);
465 for (i
= 0; i
< size
; i
++) {
466 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM
| (3 - 2));
467 OUT_BATCH(reg
+ i
* 4);
468 OUT_RELOC(bo
, read_domains
, write_domain
, offset
+ i
* 4);
475 brw_load_register_mem(struct brw_context
*brw
,
478 uint32_t read_domains
, uint32_t write_domain
,
481 load_sized_register_mem(brw
, reg
, bo
, read_domains
, write_domain
, offset
, 1);
485 brw_load_register_mem64(struct brw_context
*brw
,
488 uint32_t read_domains
, uint32_t write_domain
,
491 load_sized_register_mem(brw
, reg
, bo
, read_domains
, write_domain
, offset
, 2);