6e4b55cf9ec17b8376b0f42e8458d37a94c8a3a8
[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 "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"
35
36 #include "util/hash_table.h"
37
38 #include <xf86drm.h>
39 #include <i915_drm.h>
40
41 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
42
43 static void
44 intel_batchbuffer_reset(struct intel_batchbuffer *batch,
45 struct brw_bufmgr *bufmgr,
46 bool has_llc);
47
48 static bool
49 uint_key_compare(const void *a, const void *b)
50 {
51 return a == b;
52 }
53
54 static uint32_t
55 uint_key_hash(const void *key)
56 {
57 return (uintptr_t) key;
58 }
59
60 void
61 intel_batchbuffer_init(struct intel_batchbuffer *batch,
62 struct brw_bufmgr *bufmgr,
63 bool has_llc)
64 {
65 intel_batchbuffer_reset(batch, bufmgr, has_llc);
66
67 if (!has_llc) {
68 batch->cpu_map = malloc(BATCH_SZ);
69 batch->map = batch->cpu_map;
70 batch->map_next = batch->cpu_map;
71 }
72
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;
79 batch->exec_bos =
80 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
81 batch->exec_objects =
82 malloc(batch->exec_array_size * sizeof(batch->exec_objects[0]));
83
84 if (INTEL_DEBUG & DEBUG_BATCH) {
85 batch->state_batch_sizes =
86 _mesa_hash_table_create(NULL, uint_key_hash, uint_key_compare);
87 }
88 }
89
90 static void
91 intel_batchbuffer_reset(struct intel_batchbuffer *batch,
92 struct brw_bufmgr *bufmgr,
93 bool has_llc)
94 {
95 if (batch->last_bo != NULL) {
96 brw_bo_unreference(batch->last_bo);
97 batch->last_bo = NULL;
98 }
99 batch->last_bo = batch->bo;
100
101 batch->bo = brw_bo_alloc(bufmgr, "batchbuffer", BATCH_SZ, 4096);
102 if (has_llc) {
103 brw_bo_map(NULL, batch->bo, true);
104 batch->map = batch->bo->virtual;
105 }
106 batch->map_next = batch->map;
107
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;
112
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.
115 */
116 batch->ring = UNKNOWN_RING;
117
118 if (batch->state_batch_sizes)
119 _mesa_hash_table_clear(batch->state_batch_sizes, NULL);
120 }
121
122 static void
123 intel_batchbuffer_reset_and_clear_render_cache(struct brw_context *brw)
124 {
125 intel_batchbuffer_reset(&brw->batch, brw->bufmgr, brw->has_llc);
126 brw_render_cache_set_clear(brw);
127 }
128
129 void
130 intel_batchbuffer_save_state(struct brw_context *brw)
131 {
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;
135 }
136
137 void
138 intel_batchbuffer_reset_to_saved(struct brw_context *brw)
139 {
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]);
144 }
145 }
146 brw->batch.reloc_count = brw->batch.saved.reloc_count;
147 brw->batch.exec_count = brw->batch.saved.exec_count;
148
149 brw->batch.map_next = brw->batch.saved.map_next;
150 if (USED_BATCH(brw->batch) == 0)
151 brw->batch.ring = UNKNOWN_RING;
152 }
153
154 void
155 intel_batchbuffer_free(struct intel_batchbuffer *batch)
156 {
157 free(batch->cpu_map);
158
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]);
162 }
163 }
164 free(batch->relocs);
165 free(batch->exec_bos);
166 free(batch->exec_objects);
167
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);
172 }
173
174 void
175 intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
176 enum brw_gpu_ring ring)
177 {
178 /* If we're switching rings, implicitly flush the batch. */
179 if (unlikely(ring != brw->batch.ring) && brw->batch.ring != UNKNOWN_RING &&
180 brw->gen >= 6) {
181 intel_batchbuffer_flush(brw);
182 }
183
184 #ifdef DEBUG
185 assert(sz < BATCH_SZ - BATCH_RESERVED);
186 #endif
187 if (intel_batchbuffer_space(&brw->batch) < sz)
188 intel_batchbuffer_flush(brw);
189
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.
192 */
193 brw->batch.ring = ring;
194 }
195
196 #ifdef DEBUG
197 #define CSI "\e["
198 #define BLUE_HEADER CSI "0;44m"
199 #define NORMAL CSI "0m"
200
201
202 static void
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)
206 {
207 struct gen_group *group = gen_spec_find_struct(spec, struct_name);
208 if (!group)
209 return;
210
211 fprintf(stderr, "%s\n", struct_name);
212 gen_print_group(stderr, group, gtt_offset + offset,
213 &data[offset / 4], color);
214 }
215
216 static void
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)
221 {
222 struct gen_group *group = gen_spec_find_struct(spec, struct_name);
223 if (!group)
224 return;
225
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);
231 }
232 }
233
234 static void
235 do_batch_dump(struct brw_context *brw)
236 {
237 struct intel_batchbuffer *batch = &brw->batch;
238 struct gen_spec *spec = gen_spec_load(&brw->screen->devinfo);
239
240 if (batch->ring != RENDER_RING)
241 return;
242
243 int ret = brw_bo_map(brw, batch->bo, false);
244 if (ret != 0) {
245 fprintf(stderr,
246 "WARNING: failed to map batchbuffer (%s), "
247 "dumping uploaded data instead.\n", strerror(ret));
248 }
249
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;
253 int length;
254
255 bool color = INTEL_DEBUG & DEBUG_COLOR;
256 const char *header_color = color ? BLUE_HEADER : "";
257 const char *reset_color = color ? NORMAL : "";
258
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);
264 if (inst == NULL) {
265 fprintf(stderr, "unknown instruction %08x\n", p[0]);
266 continue;
267 }
268
269 uint64_t offset = gtt_offset + 4 * (p - data);
270
271 fprintf(stderr, "%s0x%08"PRIx64": 0x%08x: %-80s%s\n", header_color,
272 offset, p[0], gen_group_get_name(inst), reset_color);
273
274 gen_print_group(stderr, inst, offset, p, color);
275
276 switch (gen_group_get_opcode(inst) >> 16) {
277 case _3DSTATE_PIPELINED_POINTERS:
278 /* TODO: Decode Gen4-5 pipelined pointers */
279 break;
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");
287 if (!group)
288 break;
289
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);
297 }
298 break;
299 }
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);
307 break;
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);
315 break;
316 case _3DSTATE_VIEWPORT_STATE_POINTERS_CC:
317 decode_structs(brw, spec, "CC_VIEWPORT", data,
318 gtt_offset, p[1] & ~0x3fu, 2 * 4, color);
319 break;
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);
323 break;
324 case _3DSTATE_SCISSOR_STATE_POINTERS:
325 decode_structs(brw, spec, "SCISSOR_RECT", data,
326 gtt_offset, p[1] & ~0x1fu, 2 * 4, color);
327 break;
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);
332 break;
333 case _3DSTATE_CC_STATE_POINTERS:
334 if (brw->gen >= 7) {
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);
344 }
345 break;
346 case _3DSTATE_DEPTH_STENCIL_STATE_POINTERS:
347 decode_struct(brw, spec, "DEPTH_STENCIL_STATE", data,
348 gtt_offset, p[1] & ~0x3fu, color);
349 break;
350 }
351 }
352
353 if (ret == 0) {
354 brw_bo_unmap(batch->bo);
355 }
356 }
357 #else
358 static void do_batch_dump(struct brw_context *brw) { }
359 #endif
360
361 /**
362 * Called when starting a new batch buffer.
363 */
364 static void
365 brw_new_batch(struct brw_context *brw)
366 {
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]);
371 }
372 brw->batch.exec_bos[i] = NULL;
373 }
374 brw->batch.reloc_count = 0;
375 brw->batch.exec_count = 0;
376 brw->batch.aperture_space = BATCH_SZ;
377
378 /* Create a new batchbuffer and reset the associated state: */
379 intel_batchbuffer_reset_and_clear_render_cache(brw);
380
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).
386 */
387 if (brw->hw_ctx == 0)
388 brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;
389
390 brw->ctx.NewDriverState |= BRW_NEW_BATCH;
391
392 brw->ib.type = -1;
393
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.
398 */
399 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
400 brw_collect_and_report_shader_time(brw);
401 }
402
403 /**
404 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
405 * sending it off.
406 *
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.
411 */
412 static void
413 brw_finish_batch(struct brw_context *brw)
414 {
415 /* Capture the closing pipeline statistics register values necessary to
416 * support query objects (in the non-hardware context world).
417 */
418 brw_emit_query_end(brw);
419
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
423 * defaults.
424 */
425 if (brw->gen >= 7)
426 gen7_restore_default_l3_config(brw);
427
428 if (brw->is_haswell) {
429 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
430 * 3DSTATE_CC_STATE_POINTERS > "Note":
431 *
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."
434 *
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.
437 *
438 * See also WaAvoidRCZCounterRollover.
439 */
440 brw_emit_mi_flush(brw);
441 BEGIN_BATCH(2);
442 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (2 - 2));
443 OUT_BATCH(brw->cc.state_offset | 1);
444 ADVANCE_BATCH();
445 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH |
446 PIPE_CONTROL_CS_STALL);
447 }
448 }
449
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
452 * next batch.
453 */
454 brw->cache.bo_used_by_gpu = true;
455 }
456
457 static void
458 throttle(struct brw_context *brw)
459 {
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
462 * but not CPU-heavy.
463 *
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.
470 *
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.
474 */
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...
480 */
481 brw_bo_wait_rendering(NULL, brw->throttle_batch[1]);
482 }
483 brw_bo_unreference(brw->throttle_batch[1]);
484 }
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;
490 }
491
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;
496 }
497 }
498
499 static void
500 add_exec_bo(struct intel_batchbuffer *batch, struct brw_bo *bo)
501 {
502 if (bo != batch->bo) {
503 for (int i = 0; i < batch->exec_count; i++) {
504 if (batch->exec_bos[i] == bo)
505 return;
506 }
507
508 brw_bo_reference(bo);
509 }
510
511 if (batch->exec_count == batch->exec_array_size) {
512 batch->exec_array_size *= 2;
513 batch->exec_bos =
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]));
519 }
520
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;
527 } else {
528 validation_entry->relocation_count = 0;
529 validation_entry->relocs_ptr = 0;
530 }
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;
536
537 batch->exec_bos[batch->exec_count] = bo;
538 batch->exec_count++;
539 batch->aperture_space += bo->size;
540 }
541
542 static int
543 execbuffer(int fd,
544 struct intel_batchbuffer *batch,
545 uint32_t ctx_id,
546 int used,
547 int in_fence,
548 int *out_fence,
549 int flags)
550 {
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,
555 .batch_len = used,
556 .flags = flags,
557 .rsvd1 = ctx_id, /* rsvd1 is actually the context ID */
558 };
559
560 unsigned long cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2;
561
562 if (in_fence != -1) {
563 execbuf.rsvd2 = in_fence;
564 execbuf.flags |= I915_EXEC_FENCE_IN;
565 }
566
567 if (out_fence != NULL) {
568 cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2_WR;
569 *out_fence = -1;
570 execbuf.flags |= I915_EXEC_FENCE_OUT;
571 }
572
573 int ret = drmIoctl(fd, cmd, &execbuf);
574 if (ret != 0)
575 ret = -errno;
576
577 for (int i = 0; i < batch->exec_count; i++) {
578 struct brw_bo *bo = batch->exec_bos[i];
579
580 bo->idle = false;
581
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;
587 }
588 }
589
590 if (ret == 0 && out_fence != NULL)
591 *out_fence = execbuf.rsvd2 >> 32;
592
593 return ret;
594 }
595
596 static int
597 do_flush_locked(struct brw_context *brw, int in_fence_fd, int *out_fence_fd)
598 {
599 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
600 struct intel_batchbuffer *batch = &brw->batch;
601 int ret = 0;
602
603 if (brw->has_llc) {
604 brw_bo_unmap(batch->bo);
605 } else {
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);
612 }
613 }
614
615 if (!brw->screen->no_hw) {
616 int flags;
617
618 if (brw->gen >= 6 && batch->ring == BLT_RING) {
619 flags = I915_EXEC_BLT;
620 } else {
621 flags = I915_EXEC_RENDER;
622 }
623 if (batch->needs_sol_reset)
624 flags |= I915_EXEC_GEN7_SOL_RESET;
625
626 if (ret == 0) {
627 uint32_t hw_ctx = batch->ring == RENDER_RING ? brw->hw_ctx : 0;
628
629 /* Add the batch itself to the end of the validation list */
630 add_exec_bo(batch, batch->bo);
631
632 ret = execbuffer(dri_screen->fd, batch, hw_ctx,
633 4 * USED_BATCH(*batch),
634 in_fence_fd, out_fence_fd, flags);
635 }
636
637 throttle(brw);
638 }
639
640 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
641 do_batch_dump(brw);
642
643 if (brw->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
644 brw_check_for_reset(brw);
645
646 if (ret != 0) {
647 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
648 exit(1);
649 }
650
651 return ret;
652 }
653
654 /**
655 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
656 * of the fd.
657 *
658 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
659 * of the returned fd.
660 */
661 int
662 _intel_batchbuffer_flush_fence(struct brw_context *brw,
663 int in_fence_fd, int *out_fence_fd,
664 const char *file, int line)
665 {
666 int ret;
667
668 if (USED_BATCH(brw->batch) == 0)
669 return 0;
670
671 if (brw->throttle_batch[0] == NULL) {
672 brw->throttle_batch[0] = brw->batch.bo;
673 brw_bo_reference(brw->throttle_batch[0]);
674 }
675
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,
683 total_bytes,
684 100.0f * total_bytes / BATCH_SZ);
685 }
686
687 brw->batch.reserved_space = 0;
688
689 brw_finish_batch(brw);
690
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);
696 }
697
698 intel_upload_finish(brw);
699
700 /* Check that we didn't just wrap our batchbuffer at a bad time. */
701 assert(!brw->no_batch_wrap);
702
703 ret = do_flush_locked(brw, in_fence_fd, out_fence_fd);
704
705 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
706 fprintf(stderr, "waiting for idle\n");
707 brw_bo_wait_rendering(brw, brw->batch.bo);
708 }
709
710 /* Start a new batch buffer. */
711 brw_new_batch(brw);
712
713 return ret;
714 }
715
716 bool
717 brw_batch_has_aperture_space(struct brw_context *brw, unsigned extra_space)
718 {
719 return brw->batch.aperture_space + extra_space <=
720 brw->screen->aperture_threshold;
721 }
722
723 bool
724 brw_batch_references(struct intel_batchbuffer *batch, struct brw_bo *bo)
725 {
726 for (int i = 0; i < batch->exec_count; i++) {
727 if (batch->exec_bos[i] == bo)
728 return true;
729 }
730 return false;
731 }
732
733 /* This is the only way buffers get added to the validate list.
734 */
735 uint64_t
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)
739 {
740 uint64_t offset64;
741
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));
747 }
748
749 /* Check args */
750 assert(batch_offset <= BATCH_SZ - sizeof(uint32_t));
751 assert(_mesa_bitcount(write_domain) <= 1);
752
753 if (target != batch->bo)
754 add_exec_bo(batch, target);
755
756 struct drm_i915_gem_relocation_entry *reloc =
757 &batch->relocs[batch->reloc_count];
758
759 batch->reloc_count++;
760
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;
769
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
773 */
774 return offset64 + target_offset;
775 }
776
777 void
778 intel_batchbuffer_data(struct brw_context *brw,
779 const void *data, GLuint bytes, enum brw_gpu_ring ring)
780 {
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;
785 }
786
787 static void
788 load_sized_register_mem(struct brw_context *brw,
789 uint32_t reg,
790 struct brw_bo *bo,
791 uint32_t read_domains, uint32_t write_domain,
792 uint32_t offset,
793 int size)
794 {
795 int i;
796
797 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
798 assert(brw->gen >= 7);
799
800 if (brw->gen >= 8) {
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);
806 }
807 ADVANCE_BATCH();
808 } else {
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);
814 }
815 ADVANCE_BATCH();
816 }
817 }
818
819 void
820 brw_load_register_mem(struct brw_context *brw,
821 uint32_t reg,
822 struct brw_bo *bo,
823 uint32_t read_domains, uint32_t write_domain,
824 uint32_t offset)
825 {
826 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 1);
827 }
828
829 void
830 brw_load_register_mem64(struct brw_context *brw,
831 uint32_t reg,
832 struct brw_bo *bo,
833 uint32_t read_domains, uint32_t write_domain,
834 uint32_t offset)
835 {
836 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 2);
837 }
838
839 /*
840 * Write an arbitrary 32-bit register to a buffer via MI_STORE_REGISTER_MEM.
841 */
842 void
843 brw_store_register_mem32(struct brw_context *brw,
844 struct brw_bo *bo, uint32_t reg, uint32_t offset)
845 {
846 assert(brw->gen >= 6);
847
848 if (brw->gen >= 8) {
849 BEGIN_BATCH(4);
850 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
851 OUT_BATCH(reg);
852 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
853 offset);
854 ADVANCE_BATCH();
855 } else {
856 BEGIN_BATCH(3);
857 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
858 OUT_BATCH(reg);
859 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
860 offset);
861 ADVANCE_BATCH();
862 }
863 }
864
865 /*
866 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
867 */
868 void
869 brw_store_register_mem64(struct brw_context *brw,
870 struct brw_bo *bo, uint32_t reg, uint32_t offset)
871 {
872 assert(brw->gen >= 6);
873
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.
876 */
877 if (brw->gen >= 8) {
878 BEGIN_BATCH(8);
879 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
880 OUT_BATCH(reg);
881 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
882 offset);
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));
887 ADVANCE_BATCH();
888 } else {
889 BEGIN_BATCH(6);
890 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
891 OUT_BATCH(reg);
892 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
893 offset);
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));
898 ADVANCE_BATCH();
899 }
900 }
901
902 /*
903 * Write a 32-bit register using immediate data.
904 */
905 void
906 brw_load_register_imm32(struct brw_context *brw, uint32_t reg, uint32_t imm)
907 {
908 assert(brw->gen >= 6);
909
910 BEGIN_BATCH(3);
911 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
912 OUT_BATCH(reg);
913 OUT_BATCH(imm);
914 ADVANCE_BATCH();
915 }
916
917 /*
918 * Write a 64-bit register using immediate data.
919 */
920 void
921 brw_load_register_imm64(struct brw_context *brw, uint32_t reg, uint64_t imm)
922 {
923 assert(brw->gen >= 6);
924
925 BEGIN_BATCH(5);
926 OUT_BATCH(MI_LOAD_REGISTER_IMM | (5 - 2));
927 OUT_BATCH(reg);
928 OUT_BATCH(imm & 0xffffffff);
929 OUT_BATCH(reg + 4);
930 OUT_BATCH(imm >> 32);
931 ADVANCE_BATCH();
932 }
933
934 /*
935 * Copies a 32-bit register.
936 */
937 void
938 brw_load_register_reg(struct brw_context *brw, uint32_t src, uint32_t dest)
939 {
940 assert(brw->gen >= 8 || brw->is_haswell);
941
942 BEGIN_BATCH(3);
943 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
944 OUT_BATCH(src);
945 OUT_BATCH(dest);
946 ADVANCE_BATCH();
947 }
948
949 /*
950 * Copies a 64-bit register.
951 */
952 void
953 brw_load_register_reg64(struct brw_context *brw, uint32_t src, uint32_t dest)
954 {
955 assert(brw->gen >= 8 || brw->is_haswell);
956
957 BEGIN_BATCH(6);
958 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
959 OUT_BATCH(src);
960 OUT_BATCH(dest);
961 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
962 OUT_BATCH(src + sizeof(uint32_t));
963 OUT_BATCH(dest + sizeof(uint32_t));
964 ADVANCE_BATCH();
965 }
966
967 /*
968 * Write 32-bits of immediate data to a GPU memory buffer.
969 */
970 void
971 brw_store_data_imm32(struct brw_context *brw, struct brw_bo *bo,
972 uint32_t offset, uint32_t imm)
973 {
974 assert(brw->gen >= 6);
975
976 BEGIN_BATCH(4);
977 OUT_BATCH(MI_STORE_DATA_IMM | (4 - 2));
978 if (brw->gen >= 8)
979 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
980 offset);
981 else {
982 OUT_BATCH(0); /* MBZ */
983 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
984 offset);
985 }
986 OUT_BATCH(imm);
987 ADVANCE_BATCH();
988 }
989
990 /*
991 * Write 64-bits of immediate data to a GPU memory buffer.
992 */
993 void
994 brw_store_data_imm64(struct brw_context *brw, struct brw_bo *bo,
995 uint32_t offset, uint64_t imm)
996 {
997 assert(brw->gen >= 6);
998
999 BEGIN_BATCH(5);
1000 OUT_BATCH(MI_STORE_DATA_IMM | (5 - 2));
1001 if (brw->gen >= 8)
1002 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
1003 offset);
1004 else {
1005 OUT_BATCH(0); /* MBZ */
1006 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
1007 offset);
1008 }
1009 OUT_BATCH(imm & 0xffffffffu);
1010 OUT_BATCH(imm >> 32);
1011 ADVANCE_BATCH();
1012 }