i965: Fix typos in license
[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 static void
110 do_batch_dump(struct brw_context *brw)
111 {
112 struct drm_intel_decode *decode;
113 struct intel_batchbuffer *batch = &brw->batch;
114 int ret;
115
116 decode = drm_intel_decode_context_alloc(brw->intelScreen->deviceID);
117 if (!decode)
118 return;
119
120 ret = drm_intel_bo_map(batch->bo, false);
121 if (ret == 0) {
122 drm_intel_decode_set_batch_pointer(decode,
123 batch->bo->virtual,
124 batch->bo->offset64,
125 USED_BATCH(*batch));
126 } else {
127 fprintf(stderr,
128 "WARNING: failed to map batchbuffer (%s), "
129 "dumping uploaded data instead.\n", strerror(ret));
130
131 drm_intel_decode_set_batch_pointer(decode,
132 batch->map,
133 batch->bo->offset64,
134 USED_BATCH(*batch));
135 }
136
137 drm_intel_decode_set_output_file(decode, stderr);
138 drm_intel_decode(decode);
139
140 drm_intel_decode_context_free(decode);
141
142 if (ret == 0) {
143 drm_intel_bo_unmap(batch->bo);
144
145 brw_debug_batch(brw);
146 }
147 }
148
149 void
150 intel_batchbuffer_emit_render_ring_prelude(struct brw_context *brw)
151 {
152 /* We may need to enable and snapshot OA counters. */
153 brw_perf_monitor_new_batch(brw);
154 }
155
156 /**
157 * Called when starting a new batch buffer.
158 */
159 static void
160 brw_new_batch(struct brw_context *brw)
161 {
162 /* Create a new batchbuffer and reset the associated state: */
163 drm_intel_gem_bo_clear_relocs(brw->batch.bo, 0);
164 intel_batchbuffer_reset(brw);
165
166 /* If the kernel supports hardware contexts, then most hardware state is
167 * preserved between batches; we only need to re-emit state that is required
168 * to be in every batch. Otherwise we need to re-emit all the state that
169 * would otherwise be stored in the context (which for all intents and
170 * purposes means everything).
171 */
172 if (brw->hw_ctx == NULL)
173 brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;
174
175 brw->ctx.NewDriverState |= BRW_NEW_BATCH;
176
177 brw->state_batch_count = 0;
178
179 brw->ib.type = -1;
180
181 /* We need to periodically reap the shader time results, because rollover
182 * happens every few seconds. We also want to see results every once in a
183 * while, because many programs won't cleanly destroy our context, so the
184 * end-of-run printout may not happen.
185 */
186 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
187 brw_collect_and_report_shader_time(brw);
188
189 if (INTEL_DEBUG & DEBUG_PERFMON)
190 brw_dump_perf_monitors(brw);
191 }
192
193 /**
194 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
195 * sending it off.
196 *
197 * This function can emit state (say, to preserve registers that aren't saved
198 * between batches). All of this state MUST fit in the reserved space at the
199 * end of the batchbuffer. If you add more GPU state, increase the reserved
200 * space by updating the BATCH_RESERVED macro.
201 */
202 static void
203 brw_finish_batch(struct brw_context *brw)
204 {
205 /* Capture the closing pipeline statistics register values necessary to
206 * support query objects (in the non-hardware context world).
207 */
208 brw_emit_query_end(brw);
209
210 if (brw->batch.ring == RENDER_RING) {
211 /* We may also need to snapshot and disable OA counters. */
212 brw_perf_monitor_finish_batch(brw);
213
214 if (brw->is_haswell) {
215 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
216 * 3DSTATE_CC_STATE_POINTERS > "Note":
217 *
218 * "SW must program 3DSTATE_CC_STATE_POINTERS command at the end of every
219 * 3D batch buffer followed by a PIPE_CONTROL with RC flush and CS stall."
220 *
221 * From the example in the docs, it seems to expect a regular pipe control
222 * flush here as well. We may have done it already, but meh.
223 *
224 * See also WaAvoidRCZCounterRollover.
225 */
226 brw_emit_mi_flush(brw);
227 BEGIN_BATCH(2);
228 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (2 - 2));
229 OUT_BATCH(brw->cc.state_offset | 1);
230 ADVANCE_BATCH();
231 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH |
232 PIPE_CONTROL_CS_STALL);
233 }
234 }
235
236 /* Mark that the current program cache BO has been used by the GPU.
237 * It will be reallocated if we need to put new programs in for the
238 * next batch.
239 */
240 brw->cache.bo_used_by_gpu = true;
241 }
242
243 static void
244 throttle(struct brw_context *brw)
245 {
246 /* Wait for the swapbuffers before the one we just emitted, so we
247 * don't get too many swaps outstanding for apps that are GPU-heavy
248 * but not CPU-heavy.
249 *
250 * We're using intelDRI2Flush (called from the loader before
251 * swapbuffer) and glFlush (for front buffer rendering) as the
252 * indicator that a frame is done and then throttle when we get
253 * here as we prepare to render the next frame. At this point for
254 * round trips for swap/copy and getting new buffers are done and
255 * we'll spend less time waiting on the GPU.
256 *
257 * Unfortunately, we don't have a handle to the batch containing
258 * the swap, and getting our hands on that doesn't seem worth it,
259 * so we just use the first batch we emitted after the last swap.
260 */
261 if (brw->need_swap_throttle && brw->throttle_batch[0]) {
262 if (brw->throttle_batch[1]) {
263 if (!brw->disable_throttling)
264 drm_intel_bo_wait_rendering(brw->throttle_batch[1]);
265 drm_intel_bo_unreference(brw->throttle_batch[1]);
266 }
267 brw->throttle_batch[1] = brw->throttle_batch[0];
268 brw->throttle_batch[0] = NULL;
269 brw->need_swap_throttle = false;
270 /* Throttling here is more precise than the throttle ioctl, so skip it */
271 brw->need_flush_throttle = false;
272 }
273
274 if (brw->need_flush_throttle) {
275 __DRIscreen *psp = brw->intelScreen->driScrnPriv;
276 drmCommandNone(psp->fd, DRM_I915_GEM_THROTTLE);
277 brw->need_flush_throttle = false;
278 }
279 }
280
281 /* Drop when RS headers get pulled to libdrm */
282 #ifndef I915_EXEC_RESOURCE_STREAMER
283 #define I915_EXEC_RESOURCE_STREAMER (1<<15)
284 #endif
285
286 /* TODO: Push this whole function into bufmgr.
287 */
288 static int
289 do_flush_locked(struct brw_context *brw)
290 {
291 struct intel_batchbuffer *batch = &brw->batch;
292 int ret = 0;
293
294 if (brw->has_llc) {
295 drm_intel_bo_unmap(batch->bo);
296 } else {
297 ret = drm_intel_bo_subdata(batch->bo, 0, 4 * USED_BATCH(*batch), batch->map);
298 if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
299 ret = drm_intel_bo_subdata(batch->bo,
300 batch->state_batch_offset,
301 batch->bo->size - batch->state_batch_offset,
302 (char *)batch->map + batch->state_batch_offset);
303 }
304 }
305
306 if (!brw->intelScreen->no_hw) {
307 int flags;
308
309 if (brw->gen >= 6 && batch->ring == BLT_RING) {
310 flags = I915_EXEC_BLT;
311 } else {
312 flags = I915_EXEC_RENDER |
313 (brw->use_resource_streamer ? I915_EXEC_RESOURCE_STREAMER : 0);
314 }
315 if (batch->needs_sol_reset)
316 flags |= I915_EXEC_GEN7_SOL_RESET;
317
318 if (ret == 0) {
319 if (unlikely(INTEL_DEBUG & DEBUG_AUB))
320 brw_annotate_aub(brw);
321
322 if (brw->hw_ctx == NULL || batch->ring != RENDER_RING) {
323 ret = drm_intel_bo_mrb_exec(batch->bo, 4 * USED_BATCH(*batch),
324 NULL, 0, 0, flags);
325 } else {
326 ret = drm_intel_gem_bo_context_exec(batch->bo, brw->hw_ctx,
327 4 * USED_BATCH(*batch), flags);
328 }
329 }
330
331 throttle(brw);
332 }
333
334 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
335 do_batch_dump(brw);
336
337 if (ret != 0) {
338 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
339 exit(1);
340 }
341
342 return ret;
343 }
344
345 int
346 _intel_batchbuffer_flush(struct brw_context *brw,
347 const char *file, int line)
348 {
349 int ret;
350
351 if (USED_BATCH(brw->batch) == 0)
352 return 0;
353
354 if (brw->throttle_batch[0] == NULL) {
355 brw->throttle_batch[0] = brw->batch.bo;
356 drm_intel_bo_reference(brw->throttle_batch[0]);
357 }
358
359 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
360 int bytes_for_commands = 4 * USED_BATCH(brw->batch);
361 int bytes_for_state = brw->batch.bo->size - brw->batch.state_batch_offset;
362 int total_bytes = bytes_for_commands + bytes_for_state;
363 fprintf(stderr, "%s:%d: Batchbuffer flush with %4db (pkt) + "
364 "%4db (state) = %4db (%0.1f%%)\n", file, line,
365 bytes_for_commands, bytes_for_state,
366 total_bytes,
367 100.0f * total_bytes / BATCH_SZ);
368 }
369
370 brw->batch.reserved_space = 0;
371
372 brw_finish_batch(brw);
373
374 /* Mark the end of the buffer. */
375 intel_batchbuffer_emit_dword(brw, MI_BATCH_BUFFER_END);
376 if (USED_BATCH(brw->batch) & 1) {
377 /* Round batchbuffer usage to 2 DWORDs. */
378 intel_batchbuffer_emit_dword(brw, MI_NOOP);
379 }
380
381 intel_upload_finish(brw);
382
383 /* Check that we didn't just wrap our batchbuffer at a bad time. */
384 assert(!brw->no_batch_wrap);
385
386 ret = do_flush_locked(brw);
387
388 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
389 fprintf(stderr, "waiting for idle\n");
390 drm_intel_bo_wait_rendering(brw->batch.bo);
391 }
392
393 if (brw->use_resource_streamer)
394 gen7_reset_hw_bt_pool_offsets(brw);
395
396 /* Start a new batch buffer. */
397 brw_new_batch(brw);
398
399 return ret;
400 }
401
402
403 /* This is the only way buffers get added to the validate list.
404 */
405 uint32_t
406 intel_batchbuffer_reloc(struct brw_context *brw,
407 drm_intel_bo *buffer, uint32_t offset,
408 uint32_t read_domains, uint32_t write_domain,
409 uint32_t delta)
410 {
411 int ret;
412
413 ret = drm_intel_bo_emit_reloc(brw->batch.bo, offset,
414 buffer, delta,
415 read_domains, write_domain);
416 assert(ret == 0);
417 (void)ret;
418
419 /* Using the old buffer offset, write in what the right data would be, in
420 * case the buffer doesn't move and we can short-circuit the relocation
421 * processing in the kernel
422 */
423 return buffer->offset64 + delta;
424 }
425
426 uint64_t
427 intel_batchbuffer_reloc64(struct brw_context *brw,
428 drm_intel_bo *buffer, uint32_t offset,
429 uint32_t read_domains, uint32_t write_domain,
430 uint32_t delta)
431 {
432 int ret = drm_intel_bo_emit_reloc(brw->batch.bo, offset,
433 buffer, delta,
434 read_domains, write_domain);
435 assert(ret == 0);
436 (void) ret;
437
438 /* Using the old buffer offset, write in what the right data would be, in
439 * case the buffer doesn't move and we can short-circuit the relocation
440 * processing in the kernel
441 */
442 return buffer->offset64 + delta;
443 }
444
445
446 void
447 intel_batchbuffer_data(struct brw_context *brw,
448 const void *data, GLuint bytes, enum brw_gpu_ring ring)
449 {
450 assert((bytes & 3) == 0);
451 intel_batchbuffer_require_space(brw, bytes, ring);
452 memcpy(brw->batch.map_next, data, bytes);
453 brw->batch.map_next += bytes >> 2;
454 }
455
456 static void
457 load_sized_register_mem(struct brw_context *brw,
458 uint32_t reg,
459 drm_intel_bo *bo,
460 uint32_t read_domains, uint32_t write_domain,
461 uint32_t offset,
462 int size)
463 {
464 int i;
465
466 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
467 assert(brw->gen >= 7);
468
469 if (brw->gen >= 8) {
470 BEGIN_BATCH(4 * size);
471 for (i = 0; i < size; i++) {
472 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (4 - 2));
473 OUT_BATCH(reg + i * 4);
474 OUT_RELOC64(bo, read_domains, write_domain, offset + i * 4);
475 }
476 ADVANCE_BATCH();
477 } else {
478 BEGIN_BATCH(3 * size);
479 for (i = 0; i < size; i++) {
480 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
481 OUT_BATCH(reg + i * 4);
482 OUT_RELOC(bo, read_domains, write_domain, offset + i * 4);
483 }
484 ADVANCE_BATCH();
485 }
486 }
487
488 void
489 brw_load_register_mem(struct brw_context *brw,
490 uint32_t reg,
491 drm_intel_bo *bo,
492 uint32_t read_domains, uint32_t write_domain,
493 uint32_t offset)
494 {
495 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 1);
496 }
497
498 void
499 brw_load_register_mem64(struct brw_context *brw,
500 uint32_t reg,
501 drm_intel_bo *bo,
502 uint32_t read_domains, uint32_t write_domain,
503 uint32_t offset)
504 {
505 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 2);
506 }