56811278ceeaa8e642df29534b3e2619e07b6ddf
[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 drm_bacon_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 drm_bacon_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 drm_bacon_bufmgr *bufmgr,
93 bool has_llc)
94 {
95 if (batch->last_bo != NULL) {
96 drm_bacon_bo_unreference(batch->last_bo);
97 batch->last_bo = NULL;
98 }
99 batch->last_bo = batch->bo;
100
101 batch->bo = drm_bacon_bo_alloc(bufmgr, "batchbuffer", BATCH_SZ, 4096);
102 if (has_llc) {
103 drm_bacon_bo_map(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 drm_bacon_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 drm_bacon_bo_unreference(batch->exec_bos[i]);
162 }
163 }
164 free(batch->relocs);
165 free(batch->exec_bos);
166 free(batch->exec_objects);
167
168 drm_bacon_bo_unreference(batch->last_bo);
169 drm_bacon_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 = drm_bacon_bo_map(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 drm_bacon_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 drm_bacon_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 == NULL)
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 drm_bacon_bo_wait_rendering(brw->throttle_batch[1]);
479 drm_bacon_bo_unreference(brw->throttle_batch[1]);
480 }
481 brw->throttle_batch[1] = brw->throttle_batch[0];
482 brw->throttle_batch[0] = NULL;
483 brw->need_swap_throttle = false;
484 /* Throttling here is more precise than the throttle ioctl, so skip it */
485 brw->need_flush_throttle = false;
486 }
487
488 if (brw->need_flush_throttle) {
489 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
490 drmCommandNone(dri_screen->fd, DRM_I915_GEM_THROTTLE);
491 brw->need_flush_throttle = false;
492 }
493 }
494
495 static void
496 add_exec_bo(struct intel_batchbuffer *batch, drm_bacon_bo *bo)
497 {
498 if (bo != batch->bo) {
499 for (int i = 0; i < batch->exec_count; i++) {
500 if (batch->exec_bos[i] == bo)
501 return;
502 }
503
504 drm_bacon_bo_reference(bo);
505 }
506
507 if (batch->exec_count == batch->exec_array_size) {
508 batch->exec_array_size *= 2;
509 batch->exec_bos =
510 realloc(batch->exec_bos,
511 batch->exec_array_size * sizeof(batch->exec_bos[0]));
512 batch->exec_objects =
513 realloc(batch->exec_objects,
514 batch->exec_array_size * sizeof(batch->exec_objects[0]));
515 }
516
517 struct drm_i915_gem_exec_object2 *validation_entry =
518 &batch->exec_objects[batch->exec_count];
519 validation_entry->handle = bo->gem_handle;
520 if (bo == batch->bo) {
521 validation_entry->relocation_count = batch->reloc_count;
522 validation_entry->relocs_ptr = (uintptr_t) batch->relocs;
523 } else {
524 validation_entry->relocation_count = 0;
525 validation_entry->relocs_ptr = 0;
526 }
527 validation_entry->alignment = bo->align;
528 validation_entry->offset = bo->offset64;
529 validation_entry->flags = 0;
530 validation_entry->rsvd1 = 0;
531 validation_entry->rsvd2 = 0;
532
533 batch->exec_bos[batch->exec_count] = bo;
534 batch->exec_count++;
535 batch->aperture_space += bo->size;
536 }
537
538 static int
539 execbuffer(int fd,
540 struct intel_batchbuffer *batch,
541 drm_bacon_context *ctx,
542 int used,
543 int in_fence,
544 int *out_fence,
545 int flags)
546 {
547 uint32_t ctx_id = 0;
548 drm_bacon_gem_context_get_id(ctx, &ctx_id);
549
550 struct drm_i915_gem_execbuffer2 execbuf = {
551 .buffers_ptr = (uintptr_t) batch->exec_objects,
552 .buffer_count = batch->exec_count,
553 .batch_start_offset = 0,
554 .batch_len = used,
555 .flags = flags,
556 .rsvd1 = ctx_id, /* rsvd1 is actually the context ID */
557 };
558
559 unsigned long cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2;
560
561 if (in_fence != -1) {
562 execbuf.rsvd2 = in_fence;
563 execbuf.flags |= I915_EXEC_FENCE_IN;
564 }
565
566 if (out_fence != NULL) {
567 cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2_WR;
568 *out_fence = -1;
569 execbuf.flags |= I915_EXEC_FENCE_OUT;
570 }
571
572 int ret = drmIoctl(fd, cmd, &execbuf);
573 if (ret != 0)
574 ret = -errno;
575
576 for (int i = 0; i < batch->exec_count; i++) {
577 drm_bacon_bo *bo = batch->exec_bos[i];
578
579 bo->idle = false;
580
581 /* Update drm_bacon_bo::offset64 */
582 if (batch->exec_objects[i].offset != bo->offset64) {
583 DBG("BO %d migrated: 0x%" PRIx64 " -> 0x%llx\n",
584 bo->gem_handle, bo->offset64, batch->exec_objects[i].offset);
585 bo->offset64 = batch->exec_objects[i].offset;
586 }
587 }
588
589 if (ret == 0 && out_fence != NULL)
590 *out_fence = execbuf.rsvd2 >> 32;
591
592 return ret;
593 }
594
595 static int
596 do_flush_locked(struct brw_context *brw, int in_fence_fd, int *out_fence_fd)
597 {
598 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
599 struct intel_batchbuffer *batch = &brw->batch;
600 int ret = 0;
601
602 if (brw->has_llc) {
603 drm_bacon_bo_unmap(batch->bo);
604 } else {
605 ret = drm_bacon_bo_subdata(batch->bo, 0, 4 * USED_BATCH(*batch), batch->map);
606 if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
607 ret = drm_bacon_bo_subdata(batch->bo,
608 batch->state_batch_offset,
609 batch->bo->size - batch->state_batch_offset,
610 (char *)batch->map + batch->state_batch_offset);
611 }
612 }
613
614 if (!brw->screen->no_hw) {
615 int flags;
616
617 if (brw->gen >= 6 && batch->ring == BLT_RING) {
618 flags = I915_EXEC_BLT;
619 } else {
620 flags = I915_EXEC_RENDER;
621 }
622 if (batch->needs_sol_reset)
623 flags |= I915_EXEC_GEN7_SOL_RESET;
624
625 if (ret == 0) {
626 void *hw_ctx = batch->ring != RENDER_RING ? NULL : brw->hw_ctx;
627
628 /* Add the batch itself to the end of the validation list */
629 add_exec_bo(batch, batch->bo);
630
631 ret = execbuffer(dri_screen->fd, batch, hw_ctx,
632 4 * USED_BATCH(*batch),
633 in_fence_fd, out_fence_fd, flags);
634 }
635
636 throttle(brw);
637 }
638
639 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
640 do_batch_dump(brw);
641
642 if (brw->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
643 brw_check_for_reset(brw);
644
645 if (ret != 0) {
646 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
647 exit(1);
648 }
649
650 return ret;
651 }
652
653 /**
654 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
655 * of the fd.
656 *
657 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
658 * of the returned fd.
659 */
660 int
661 _intel_batchbuffer_flush_fence(struct brw_context *brw,
662 int in_fence_fd, int *out_fence_fd,
663 const char *file, int line)
664 {
665 int ret;
666
667 if (USED_BATCH(brw->batch) == 0)
668 return 0;
669
670 if (brw->throttle_batch[0] == NULL) {
671 brw->throttle_batch[0] = brw->batch.bo;
672 drm_bacon_bo_reference(brw->throttle_batch[0]);
673 }
674
675 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
676 int bytes_for_commands = 4 * USED_BATCH(brw->batch);
677 int bytes_for_state = brw->batch.bo->size - brw->batch.state_batch_offset;
678 int total_bytes = bytes_for_commands + bytes_for_state;
679 fprintf(stderr, "%s:%d: Batchbuffer flush with %4db (pkt) + "
680 "%4db (state) = %4db (%0.1f%%)\n", file, line,
681 bytes_for_commands, bytes_for_state,
682 total_bytes,
683 100.0f * total_bytes / BATCH_SZ);
684 }
685
686 brw->batch.reserved_space = 0;
687
688 brw_finish_batch(brw);
689
690 /* Mark the end of the buffer. */
691 intel_batchbuffer_emit_dword(&brw->batch, MI_BATCH_BUFFER_END);
692 if (USED_BATCH(brw->batch) & 1) {
693 /* Round batchbuffer usage to 2 DWORDs. */
694 intel_batchbuffer_emit_dword(&brw->batch, MI_NOOP);
695 }
696
697 intel_upload_finish(brw);
698
699 /* Check that we didn't just wrap our batchbuffer at a bad time. */
700 assert(!brw->no_batch_wrap);
701
702 ret = do_flush_locked(brw, in_fence_fd, out_fence_fd);
703
704 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
705 fprintf(stderr, "waiting for idle\n");
706 drm_bacon_bo_wait_rendering(brw->batch.bo);
707 }
708
709 /* Start a new batch buffer. */
710 brw_new_batch(brw);
711
712 return ret;
713 }
714
715 bool
716 brw_batch_has_aperture_space(struct brw_context *brw, unsigned extra_space)
717 {
718 return brw->batch.aperture_space + extra_space <=
719 brw->screen->aperture_threshold;
720 }
721
722 bool
723 brw_batch_references(struct intel_batchbuffer *batch, drm_bacon_bo *bo)
724 {
725 for (int i = 0; i < batch->exec_count; i++) {
726 if (batch->exec_bos[i] == bo)
727 return true;
728 }
729 return false;
730 }
731
732 /* This is the only way buffers get added to the validate list.
733 */
734 uint64_t
735 brw_emit_reloc(struct intel_batchbuffer *batch, uint32_t batch_offset,
736 drm_bacon_bo *target, uint32_t target_offset,
737 uint32_t read_domains, uint32_t write_domain)
738 {
739 if (batch->reloc_count == batch->reloc_array_size) {
740 batch->reloc_array_size *= 2;
741 batch->relocs = realloc(batch->relocs,
742 batch->reloc_array_size *
743 sizeof(struct drm_i915_gem_relocation_entry));
744 }
745
746 /* Check args */
747 assert(batch_offset <= BATCH_SZ - sizeof(uint32_t));
748 assert(_mesa_bitcount(write_domain) <= 1);
749
750 if (target != batch->bo)
751 add_exec_bo(batch, target);
752
753 struct drm_i915_gem_relocation_entry *reloc =
754 &batch->relocs[batch->reloc_count];
755
756 batch->reloc_count++;
757
758 reloc->offset = batch_offset;
759 reloc->delta = target_offset;
760 reloc->target_handle = target->gem_handle;
761 reloc->read_domains = read_domains;
762 reloc->write_domain = write_domain;
763 reloc->presumed_offset = target->offset64;
764
765 /* Using the old buffer offset, write in what the right data would be, in
766 * case the buffer doesn't move and we can short-circuit the relocation
767 * processing in the kernel
768 */
769 return target->offset64 + target_offset;
770 }
771
772 void
773 intel_batchbuffer_data(struct brw_context *brw,
774 const void *data, GLuint bytes, enum brw_gpu_ring ring)
775 {
776 assert((bytes & 3) == 0);
777 intel_batchbuffer_require_space(brw, bytes, ring);
778 memcpy(brw->batch.map_next, data, bytes);
779 brw->batch.map_next += bytes >> 2;
780 }
781
782 static void
783 load_sized_register_mem(struct brw_context *brw,
784 uint32_t reg,
785 drm_bacon_bo *bo,
786 uint32_t read_domains, uint32_t write_domain,
787 uint32_t offset,
788 int size)
789 {
790 int i;
791
792 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
793 assert(brw->gen >= 7);
794
795 if (brw->gen >= 8) {
796 BEGIN_BATCH(4 * size);
797 for (i = 0; i < size; i++) {
798 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (4 - 2));
799 OUT_BATCH(reg + i * 4);
800 OUT_RELOC64(bo, read_domains, write_domain, offset + i * 4);
801 }
802 ADVANCE_BATCH();
803 } else {
804 BEGIN_BATCH(3 * size);
805 for (i = 0; i < size; i++) {
806 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
807 OUT_BATCH(reg + i * 4);
808 OUT_RELOC(bo, read_domains, write_domain, offset + i * 4);
809 }
810 ADVANCE_BATCH();
811 }
812 }
813
814 void
815 brw_load_register_mem(struct brw_context *brw,
816 uint32_t reg,
817 drm_bacon_bo *bo,
818 uint32_t read_domains, uint32_t write_domain,
819 uint32_t offset)
820 {
821 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 1);
822 }
823
824 void
825 brw_load_register_mem64(struct brw_context *brw,
826 uint32_t reg,
827 drm_bacon_bo *bo,
828 uint32_t read_domains, uint32_t write_domain,
829 uint32_t offset)
830 {
831 load_sized_register_mem(brw, reg, bo, read_domains, write_domain, offset, 2);
832 }
833
834 /*
835 * Write an arbitrary 32-bit register to a buffer via MI_STORE_REGISTER_MEM.
836 */
837 void
838 brw_store_register_mem32(struct brw_context *brw,
839 drm_bacon_bo *bo, uint32_t reg, uint32_t offset)
840 {
841 assert(brw->gen >= 6);
842
843 if (brw->gen >= 8) {
844 BEGIN_BATCH(4);
845 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
846 OUT_BATCH(reg);
847 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
848 offset);
849 ADVANCE_BATCH();
850 } else {
851 BEGIN_BATCH(3);
852 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
853 OUT_BATCH(reg);
854 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
855 offset);
856 ADVANCE_BATCH();
857 }
858 }
859
860 /*
861 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
862 */
863 void
864 brw_store_register_mem64(struct brw_context *brw,
865 drm_bacon_bo *bo, uint32_t reg, uint32_t offset)
866 {
867 assert(brw->gen >= 6);
868
869 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
870 * read a full 64-bit register, we need to do two of them.
871 */
872 if (brw->gen >= 8) {
873 BEGIN_BATCH(8);
874 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
875 OUT_BATCH(reg);
876 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
877 offset);
878 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
879 OUT_BATCH(reg + sizeof(uint32_t));
880 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
881 offset + sizeof(uint32_t));
882 ADVANCE_BATCH();
883 } else {
884 BEGIN_BATCH(6);
885 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
886 OUT_BATCH(reg);
887 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
888 offset);
889 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
890 OUT_BATCH(reg + sizeof(uint32_t));
891 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
892 offset + sizeof(uint32_t));
893 ADVANCE_BATCH();
894 }
895 }
896
897 /*
898 * Write a 32-bit register using immediate data.
899 */
900 void
901 brw_load_register_imm32(struct brw_context *brw, uint32_t reg, uint32_t imm)
902 {
903 assert(brw->gen >= 6);
904
905 BEGIN_BATCH(3);
906 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
907 OUT_BATCH(reg);
908 OUT_BATCH(imm);
909 ADVANCE_BATCH();
910 }
911
912 /*
913 * Write a 64-bit register using immediate data.
914 */
915 void
916 brw_load_register_imm64(struct brw_context *brw, uint32_t reg, uint64_t imm)
917 {
918 assert(brw->gen >= 6);
919
920 BEGIN_BATCH(5);
921 OUT_BATCH(MI_LOAD_REGISTER_IMM | (5 - 2));
922 OUT_BATCH(reg);
923 OUT_BATCH(imm & 0xffffffff);
924 OUT_BATCH(reg + 4);
925 OUT_BATCH(imm >> 32);
926 ADVANCE_BATCH();
927 }
928
929 /*
930 * Copies a 32-bit register.
931 */
932 void
933 brw_load_register_reg(struct brw_context *brw, uint32_t src, uint32_t dest)
934 {
935 assert(brw->gen >= 8 || brw->is_haswell);
936
937 BEGIN_BATCH(3);
938 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
939 OUT_BATCH(src);
940 OUT_BATCH(dest);
941 ADVANCE_BATCH();
942 }
943
944 /*
945 * Copies a 64-bit register.
946 */
947 void
948 brw_load_register_reg64(struct brw_context *brw, uint32_t src, uint32_t dest)
949 {
950 assert(brw->gen >= 8 || brw->is_haswell);
951
952 BEGIN_BATCH(6);
953 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
954 OUT_BATCH(src);
955 OUT_BATCH(dest);
956 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
957 OUT_BATCH(src + sizeof(uint32_t));
958 OUT_BATCH(dest + sizeof(uint32_t));
959 ADVANCE_BATCH();
960 }
961
962 /*
963 * Write 32-bits of immediate data to a GPU memory buffer.
964 */
965 void
966 brw_store_data_imm32(struct brw_context *brw, drm_bacon_bo *bo,
967 uint32_t offset, uint32_t imm)
968 {
969 assert(brw->gen >= 6);
970
971 BEGIN_BATCH(4);
972 OUT_BATCH(MI_STORE_DATA_IMM | (4 - 2));
973 if (brw->gen >= 8)
974 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
975 offset);
976 else {
977 OUT_BATCH(0); /* MBZ */
978 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
979 offset);
980 }
981 OUT_BATCH(imm);
982 ADVANCE_BATCH();
983 }
984
985 /*
986 * Write 64-bits of immediate data to a GPU memory buffer.
987 */
988 void
989 brw_store_data_imm64(struct brw_context *brw, drm_bacon_bo *bo,
990 uint32_t offset, uint64_t imm)
991 {
992 assert(brw->gen >= 6);
993
994 BEGIN_BATCH(5);
995 OUT_BATCH(MI_STORE_DATA_IMM | (5 - 2));
996 if (brw->gen >= 8)
997 OUT_RELOC64(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
998 offset);
999 else {
1000 OUT_BATCH(0); /* MBZ */
1001 OUT_RELOC(bo, I915_GEM_DOMAIN_INSTRUCTION, I915_GEM_DOMAIN_INSTRUCTION,
1002 offset);
1003 }
1004 OUT_BATCH(imm & 0xffffffffu);
1005 OUT_BATCH(imm >> 32);
1006 ADVANCE_BATCH();
1007 }