i965: Don't inline intel_batchbuffer_require_space().
[mesa.git] / src / mesa / drivers / dri / i965 / intel_batchbuffer.c
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 */
25
26 #include "intel_batchbuffer.h"
27 #include "intel_buffer_objects.h"
28 #include "intel_reg.h"
29 #include "intel_bufmgr.h"
30 #include "intel_buffers.h"
31 #include "intel_fbo.h"
32 #include "brw_context.h"
33 #include "brw_defines.h"
34 #include "brw_state.h"
35
36 #include <xf86drm.h>
37 #include <i915_drm.h>
38
39 static void
40 intel_batchbuffer_reset(struct brw_context *brw);
41
42 void
43 intel_batchbuffer_init(struct brw_context *brw)
44 {
45 intel_batchbuffer_reset(brw);
46
47 if (!brw->has_llc) {
48 brw->batch.cpu_map = malloc(BATCH_SZ);
49 brw->batch.map = brw->batch.cpu_map;
50 brw->batch.map_next = brw->batch.cpu_map;
51 }
52 }
53
54 static void
55 intel_batchbuffer_reset(struct brw_context *brw)
56 {
57 if (brw->batch.last_bo != NULL) {
58 drm_intel_bo_unreference(brw->batch.last_bo);
59 brw->batch.last_bo = NULL;
60 }
61 brw->batch.last_bo = brw->batch.bo;
62
63 brw_render_cache_set_clear(brw);
64
65 brw->batch.bo = drm_intel_bo_alloc(brw->bufmgr, "batchbuffer",
66 BATCH_SZ, 4096);
67 if (brw->has_llc) {
68 drm_intel_bo_map(brw->batch.bo, true);
69 brw->batch.map = brw->batch.bo->virtual;
70 }
71 brw->batch.map_next = brw->batch.map;
72
73 brw->batch.reserved_space = BATCH_RESERVED;
74 brw->batch.state_batch_offset = brw->batch.bo->size;
75 brw->batch.needs_sol_reset = false;
76
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.
79 */
80 brw->batch.ring = UNKNOWN_RING;
81 }
82
83 void
84 intel_batchbuffer_save_state(struct brw_context *brw)
85 {
86 brw->batch.saved.map_next = brw->batch.map_next;
87 brw->batch.saved.reloc_count =
88 drm_intel_gem_bo_get_reloc_count(brw->batch.bo);
89 }
90
91 void
92 intel_batchbuffer_reset_to_saved(struct brw_context *brw)
93 {
94 drm_intel_gem_bo_clear_relocs(brw->batch.bo, brw->batch.saved.reloc_count);
95
96 brw->batch.map_next = brw->batch.saved.map_next;
97 if (USED_BATCH(brw->batch) == 0)
98 brw->batch.ring = UNKNOWN_RING;
99 }
100
101 void
102 intel_batchbuffer_free(struct brw_context *brw)
103 {
104 free(brw->batch.cpu_map);
105 drm_intel_bo_unreference(brw->batch.last_bo);
106 drm_intel_bo_unreference(brw->batch.bo);
107 }
108
109 void
110 intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
111 enum brw_gpu_ring ring)
112 {
113 /* If we're switching rings, implicitly flush the batch. */
114 if (unlikely(ring != brw->batch.ring) && brw->batch.ring != UNKNOWN_RING &&
115 brw->gen >= 6) {
116 intel_batchbuffer_flush(brw);
117 }
118
119 #ifdef DEBUG
120 assert(sz < BATCH_SZ - BATCH_RESERVED);
121 #endif
122 if (intel_batchbuffer_space(brw) < sz)
123 intel_batchbuffer_flush(brw);
124
125 enum brw_gpu_ring prev_ring = brw->batch.ring;
126 /* The intel_batchbuffer_flush() calls above might have changed
127 * brw->batch.ring to UNKNOWN_RING, so we need to set it here at the end.
128 */
129 brw->batch.ring = ring;
130
131 if (unlikely(prev_ring == UNKNOWN_RING && ring == RENDER_RING))
132 intel_batchbuffer_emit_render_ring_prelude(brw);
133 }
134
135 static void
136 do_batch_dump(struct brw_context *brw)
137 {
138 struct drm_intel_decode *decode;
139 struct intel_batchbuffer *batch = &brw->batch;
140 int ret;
141
142 decode = drm_intel_decode_context_alloc(brw->intelScreen->deviceID);
143 if (!decode)
144 return;
145
146 ret = drm_intel_bo_map(batch->bo, false);
147 if (ret == 0) {
148 drm_intel_decode_set_batch_pointer(decode,
149 batch->bo->virtual,
150 batch->bo->offset64,
151 USED_BATCH(*batch));
152 } else {
153 fprintf(stderr,
154 "WARNING: failed to map batchbuffer (%s), "
155 "dumping uploaded data instead.\n", strerror(ret));
156
157 drm_intel_decode_set_batch_pointer(decode,
158 batch->map,
159 batch->bo->offset64,
160 USED_BATCH(*batch));
161 }
162
163 drm_intel_decode_set_output_file(decode, stderr);
164 drm_intel_decode(decode);
165
166 drm_intel_decode_context_free(decode);
167
168 if (ret == 0) {
169 drm_intel_bo_unmap(batch->bo);
170
171 brw_debug_batch(brw);
172 }
173 }
174
175 void
176 intel_batchbuffer_emit_render_ring_prelude(struct brw_context *brw)
177 {
178 /* We may need to enable and snapshot OA counters. */
179 brw_perf_monitor_new_batch(brw);
180 }
181
182 /**
183 * Called when starting a new batch buffer.
184 */
185 static void
186 brw_new_batch(struct brw_context *brw)
187 {
188 /* Create a new batchbuffer and reset the associated state: */
189 drm_intel_gem_bo_clear_relocs(brw->batch.bo, 0);
190 intel_batchbuffer_reset(brw);
191
192 /* If the kernel supports hardware contexts, then most hardware state is
193 * preserved between batches; we only need to re-emit state that is required
194 * to be in every batch. Otherwise we need to re-emit all the state that
195 * would otherwise be stored in the context (which for all intents and
196 * purposes means everything).
197 */
198 if (brw->hw_ctx == NULL)
199 brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;
200
201 brw->ctx.NewDriverState |= BRW_NEW_BATCH;
202
203 brw->state_batch_count = 0;
204
205 brw->ib.type = -1;
206
207 /* We need to periodically reap the shader time results, because rollover
208 * happens every few seconds. We also want to see results every once in a
209 * while, because many programs won't cleanly destroy our context, so the
210 * end-of-run printout may not happen.
211 */
212 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
213 brw_collect_and_report_shader_time(brw);
214
215 if (INTEL_DEBUG & DEBUG_PERFMON)
216 brw_dump_perf_monitors(brw);
217 }
218
219 /**
220 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
221 * sending it off.
222 *
223 * This function can emit state (say, to preserve registers that aren't saved
224 * between batches). All of this state MUST fit in the reserved space at the
225 * end of the batchbuffer. If you add more GPU state, increase the reserved
226 * space by updating the BATCH_RESERVED macro.
227 */
228 static void
229 brw_finish_batch(struct brw_context *brw)
230 {
231 /* Capture the closing pipeline statistics register values necessary to
232 * support query objects (in the non-hardware context world).
233 */
234 brw_emit_query_end(brw);
235
236 if (brw->batch.ring == RENDER_RING) {
237 /* Work around L3 state leaks into contexts set MI_RESTORE_INHIBIT which
238 * assume that the L3 cache is configured according to the hardware
239 * defaults.
240 */
241 if (brw->gen >= 7)
242 gen7_restore_default_l3_config(brw);
243
244 /* We may also need to snapshot and disable OA counters. */
245 brw_perf_monitor_finish_batch(brw);
246
247 if (brw->is_haswell) {
248 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
249 * 3DSTATE_CC_STATE_POINTERS > "Note":
250 *
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."
253 *
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.
256 *
257 * See also WaAvoidRCZCounterRollover.
258 */
259 brw_emit_mi_flush(brw);
260 BEGIN_BATCH(2);
261 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (2 - 2));
262 OUT_BATCH(brw->cc.state_offset | 1);
263 ADVANCE_BATCH();
264 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH |
265 PIPE_CONTROL_CS_STALL);
266 }
267 }
268
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
271 * next batch.
272 */
273 brw->cache.bo_used_by_gpu = true;
274 }
275
276 static void
277 throttle(struct brw_context *brw)
278 {
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
281 * but not CPU-heavy.
282 *
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.
289 *
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.
293 */
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]);
299 }
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;
305 }
306
307 if (brw->need_flush_throttle) {
308 __DRIscreen *psp = brw->intelScreen->driScrnPriv;
309 drmCommandNone(psp->fd, DRM_I915_GEM_THROTTLE);
310 brw->need_flush_throttle = false;
311 }
312 }
313
314 /* Drop when RS headers get pulled to libdrm */
315 #ifndef I915_EXEC_RESOURCE_STREAMER
316 #define I915_EXEC_RESOURCE_STREAMER (1<<15)
317 #endif
318
319 /* TODO: Push this whole function into bufmgr.
320 */
321 static int
322 do_flush_locked(struct brw_context *brw)
323 {
324 struct intel_batchbuffer *batch = &brw->batch;
325 int ret = 0;
326
327 if (brw->has_llc) {
328 drm_intel_bo_unmap(batch->bo);
329 } else {
330 ret = drm_intel_bo_subdata(batch->bo, 0, 4 * USED_BATCH(*batch), batch->map);
331 if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
332 ret = drm_intel_bo_subdata(batch->bo,
333 batch->state_batch_offset,
334 batch->bo->size - batch->state_batch_offset,
335 (char *)batch->map + batch->state_batch_offset);
336 }
337 }
338
339 if (!brw->intelScreen->no_hw) {
340 int flags;
341
342 if (brw->gen >= 6 && batch->ring == BLT_RING) {
343 flags = I915_EXEC_BLT;
344 } else {
345 flags = I915_EXEC_RENDER |
346 (brw->use_resource_streamer ? I915_EXEC_RESOURCE_STREAMER : 0);
347 }
348 if (batch->needs_sol_reset)
349 flags |= I915_EXEC_GEN7_SOL_RESET;
350
351 if (ret == 0) {
352 if (unlikely(INTEL_DEBUG & DEBUG_AUB))
353 brw_annotate_aub(brw);
354
355 if (brw->hw_ctx == NULL || batch->ring != RENDER_RING) {
356 ret = drm_intel_bo_mrb_exec(batch->bo, 4 * USED_BATCH(*batch),
357 NULL, 0, 0, flags);
358 } else {
359 ret = drm_intel_gem_bo_context_exec(batch->bo, brw->hw_ctx,
360 4 * USED_BATCH(*batch), flags);
361 }
362 }
363
364 throttle(brw);
365 }
366
367 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
368 do_batch_dump(brw);
369
370 if (ret != 0) {
371 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
372 exit(1);
373 }
374
375 return ret;
376 }
377
378 int
379 _intel_batchbuffer_flush(struct brw_context *brw,
380 const char *file, int line)
381 {
382 int ret;
383
384 if (USED_BATCH(brw->batch) == 0)
385 return 0;
386
387 if (brw->throttle_batch[0] == NULL) {
388 brw->throttle_batch[0] = brw->batch.bo;
389 drm_intel_bo_reference(brw->throttle_batch[0]);
390 }
391
392 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
393 int bytes_for_commands = 4 * USED_BATCH(brw->batch);
394 int bytes_for_state = brw->batch.bo->size - brw->batch.state_batch_offset;
395 int total_bytes = bytes_for_commands + bytes_for_state;
396 fprintf(stderr, "%s:%d: Batchbuffer flush with %4db (pkt) + "
397 "%4db (state) = %4db (%0.1f%%)\n", file, line,
398 bytes_for_commands, bytes_for_state,
399 total_bytes,
400 100.0f * total_bytes / BATCH_SZ);
401 }
402
403 brw->batch.reserved_space = 0;
404
405 brw_finish_batch(brw);
406
407 /* Mark the end of the buffer. */
408 intel_batchbuffer_emit_dword(brw, MI_BATCH_BUFFER_END);
409 if (USED_BATCH(brw->batch) & 1) {
410 /* Round batchbuffer usage to 2 DWORDs. */
411 intel_batchbuffer_emit_dword(brw, MI_NOOP);
412 }
413
414 intel_upload_finish(brw);
415
416 /* Check that we didn't just wrap our batchbuffer at a bad time. */
417 assert(!brw->no_batch_wrap);
418
419 ret = do_flush_locked(brw);
420
421 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
422 fprintf(stderr, "waiting for idle\n");
423 drm_intel_bo_wait_rendering(brw->batch.bo);
424 }
425
426 if (brw->use_resource_streamer)
427 gen7_reset_hw_bt_pool_offsets(brw);
428
429 /* Start a new batch buffer. */
430 brw_new_batch(brw);
431
432 return ret;
433 }
434
435
436 /* This is the only way buffers get added to the validate list.
437 */
438 uint32_t
439 intel_batchbuffer_reloc(struct brw_context *brw,
440 drm_intel_bo *buffer, uint32_t offset,
441 uint32_t read_domains, uint32_t write_domain,
442 uint32_t delta)
443 {
444 int ret;
445
446 ret = drm_intel_bo_emit_reloc(brw->batch.bo, offset,
447 buffer, delta,
448 read_domains, write_domain);
449 assert(ret == 0);
450 (void)ret;
451
452 /* Using the old buffer offset, write in what the right data would be, in
453 * case the buffer doesn't move and we can short-circuit the relocation
454 * processing in the kernel
455 */
456 return buffer->offset64 + delta;
457 }
458
459 uint64_t
460 intel_batchbuffer_reloc64(struct brw_context *brw,
461 drm_intel_bo *buffer, uint32_t offset,
462 uint32_t read_domains, uint32_t write_domain,
463 uint32_t delta)
464 {
465 int ret = drm_intel_bo_emit_reloc(brw->batch.bo, offset,
466 buffer, delta,
467 read_domains, write_domain);
468 assert(ret == 0);
469 (void) ret;
470
471 /* Using the old buffer offset, write in what the right data would be, in
472 * case the buffer doesn't move and we can short-circuit the relocation
473 * processing in the kernel
474 */
475 return buffer->offset64 + delta;
476 }
477
478
479 void
480 intel_batchbuffer_data(struct brw_context *brw,
481 const void *data, GLuint bytes, enum brw_gpu_ring ring)
482 {
483 assert((bytes & 3) == 0);
484 intel_batchbuffer_require_space(brw, bytes, ring);
485 memcpy(brw->batch.map_next, data, bytes);
486 brw->batch.map_next += bytes >> 2;
487 }
488
489 static void
490 load_sized_register_mem(struct brw_context *brw,
491 uint32_t reg,
492 drm_intel_bo *bo,
493 uint32_t read_domains, uint32_t write_domain,
494 uint32_t offset,
495 int size)
496 {
497 int i;
498
499 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
500 assert(brw->gen >= 7);
501
502 if (brw->gen >= 8) {
503 BEGIN_BATCH(4 * size);
504 for (i = 0; i < size; i++) {
505 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (4 - 2));
506 OUT_BATCH(reg + i * 4);
507 OUT_RELOC64(bo, read_domains, write_domain, offset + i * 4);
508 }
509 ADVANCE_BATCH();
510 } else {
511 BEGIN_BATCH(3 * size);
512 for (i = 0; i < size; i++) {
513 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
514 OUT_BATCH(reg + i * 4);
515 OUT_RELOC(bo, read_domains, write_domain, offset + i * 4);
516 }
517 ADVANCE_BATCH();
518 }
519 }
520
521 void
522 brw_load_register_mem(struct brw_context *brw,
523 uint32_t reg,
524 drm_intel_bo *bo,
525 uint32_t read_domains, uint32_t write_domain,
526 uint32_t offset)
527 {
528 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 1);
529 }
530
531 void
532 brw_load_register_mem64(struct brw_context *brw,
533 uint32_t reg,
534 drm_intel_bo *bo,
535 uint32_t read_domains, uint32_t write_domain,
536 uint32_t offset)
537 {
538 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 2);
539 }