i965: drop brw->has_llc in favor of devinfo->has_llc
[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_screen *screen,
62 struct intel_batchbuffer *batch)
63 {
64 struct brw_bufmgr *bufmgr = screen->bufmgr;
65 const struct gen_device_info *devinfo = &screen->devinfo;
66
67 if (!devinfo->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->validation_list =
82 malloc(batch->exec_array_size * sizeof(batch->validation_list[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 batch->use_batch_first =
90 screen->kernel_features & KERNEL_ALLOWS_EXEC_BATCH_FIRST;
91
92 /* PIPE_CONTROL needs a w/a but only on gen6 */
93 batch->valid_reloc_flags = EXEC_OBJECT_WRITE;
94 if (devinfo->gen == 6)
95 batch->valid_reloc_flags |= EXEC_OBJECT_NEEDS_GTT;
96
97 intel_batchbuffer_reset(batch, bufmgr, devinfo->has_llc);
98 }
99
100 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
101
102 static unsigned
103 add_exec_bo(struct intel_batchbuffer *batch, struct brw_bo *bo)
104 {
105 unsigned index = READ_ONCE(bo->index);
106
107 if (index < batch->exec_count && batch->exec_bos[index] == bo)
108 return index;
109
110 /* May have been shared between multiple active batches */
111 for (index = 0; index < batch->exec_count; index++) {
112 if (batch->exec_bos[index] == bo)
113 return index;
114 }
115
116 if (bo != batch->bo)
117 brw_bo_reference(bo);
118
119 if (batch->exec_count == batch->exec_array_size) {
120 batch->exec_array_size *= 2;
121 batch->exec_bos =
122 realloc(batch->exec_bos,
123 batch->exec_array_size * sizeof(batch->exec_bos[0]));
124 batch->validation_list =
125 realloc(batch->validation_list,
126 batch->exec_array_size * sizeof(batch->validation_list[0]));
127 }
128
129 batch->validation_list[batch->exec_count] =
130 (struct drm_i915_gem_exec_object2) {
131 .handle = bo->gem_handle,
132 .alignment = bo->align,
133 .offset = bo->offset64,
134 .flags = bo->kflags,
135 };
136
137 bo->index = batch->exec_count;
138 batch->exec_bos[batch->exec_count] = bo;
139 batch->aperture_space += bo->size;
140
141 return batch->exec_count++;
142 }
143
144 static void
145 intel_batchbuffer_reset(struct intel_batchbuffer *batch,
146 struct brw_bufmgr *bufmgr,
147 bool has_llc)
148 {
149 if (batch->last_bo != NULL) {
150 brw_bo_unreference(batch->last_bo);
151 batch->last_bo = NULL;
152 }
153 batch->last_bo = batch->bo;
154
155 batch->bo = brw_bo_alloc(bufmgr, "batchbuffer", BATCH_SZ, 4096);
156 if (has_llc) {
157 batch->map = brw_bo_map(NULL, batch->bo, MAP_READ | MAP_WRITE);
158 }
159 batch->map_next = batch->map;
160
161 add_exec_bo(batch, batch->bo);
162 assert(batch->bo->index == 0);
163
164 batch->reserved_space = BATCH_RESERVED;
165 batch->state_batch_offset = batch->bo->size;
166 batch->needs_sol_reset = false;
167 batch->state_base_address_emitted = false;
168
169 /* We don't know what ring the new batch will be sent to until we see the
170 * first BEGIN_BATCH or BEGIN_BATCH_BLT. Mark it as unknown.
171 */
172 batch->ring = UNKNOWN_RING;
173
174 if (batch->state_batch_sizes)
175 _mesa_hash_table_clear(batch->state_batch_sizes, NULL);
176 }
177
178 static void
179 intel_batchbuffer_reset_and_clear_render_cache(struct brw_context *brw)
180 {
181 const struct gen_device_info *devinfo = &brw->screen->devinfo;
182
183 intel_batchbuffer_reset(&brw->batch, brw->bufmgr, devinfo->has_llc);
184 brw_render_cache_set_clear(brw);
185 }
186
187 void
188 intel_batchbuffer_save_state(struct brw_context *brw)
189 {
190 brw->batch.saved.map_next = brw->batch.map_next;
191 brw->batch.saved.reloc_count = brw->batch.reloc_count;
192 brw->batch.saved.exec_count = brw->batch.exec_count;
193 }
194
195 void
196 intel_batchbuffer_reset_to_saved(struct brw_context *brw)
197 {
198 for (int i = brw->batch.saved.exec_count;
199 i < brw->batch.exec_count; i++) {
200 if (brw->batch.exec_bos[i] != brw->batch.bo) {
201 brw_bo_unreference(brw->batch.exec_bos[i]);
202 }
203 }
204 brw->batch.reloc_count = brw->batch.saved.reloc_count;
205 brw->batch.exec_count = brw->batch.saved.exec_count;
206
207 brw->batch.map_next = brw->batch.saved.map_next;
208 if (USED_BATCH(brw->batch) == 0)
209 brw->batch.ring = UNKNOWN_RING;
210 }
211
212 void
213 intel_batchbuffer_free(struct intel_batchbuffer *batch)
214 {
215 free(batch->cpu_map);
216
217 for (int i = 0; i < batch->exec_count; i++) {
218 if (batch->exec_bos[i] != batch->bo) {
219 brw_bo_unreference(batch->exec_bos[i]);
220 }
221 }
222 free(batch->relocs);
223 free(batch->exec_bos);
224 free(batch->validation_list);
225
226 brw_bo_unreference(batch->last_bo);
227 brw_bo_unreference(batch->bo);
228 if (batch->state_batch_sizes)
229 _mesa_hash_table_destroy(batch->state_batch_sizes, NULL);
230 }
231
232 void
233 intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz,
234 enum brw_gpu_ring ring)
235 {
236 const struct gen_device_info *devinfo = &brw->screen->devinfo;
237
238 /* If we're switching rings, implicitly flush the batch. */
239 if (unlikely(ring != brw->batch.ring) && brw->batch.ring != UNKNOWN_RING &&
240 devinfo->gen >= 6) {
241 intel_batchbuffer_flush(brw);
242 }
243
244 #ifdef DEBUG
245 assert(sz < BATCH_SZ - BATCH_RESERVED);
246 #endif
247 if (intel_batchbuffer_space(&brw->batch) < sz)
248 intel_batchbuffer_flush(brw);
249
250 /* The intel_batchbuffer_flush() calls above might have changed
251 * brw->batch.ring to UNKNOWN_RING, so we need to set it here at the end.
252 */
253 brw->batch.ring = ring;
254 }
255
256 #ifdef DEBUG
257 #define CSI "\e["
258 #define BLUE_HEADER CSI "0;44m"
259 #define NORMAL CSI "0m"
260
261
262 static void
263 decode_struct(struct brw_context *brw, struct gen_spec *spec,
264 const char *struct_name, uint32_t *data,
265 uint32_t gtt_offset, uint32_t offset, bool color)
266 {
267 struct gen_group *group = gen_spec_find_struct(spec, struct_name);
268 if (!group)
269 return;
270
271 fprintf(stderr, "%s\n", struct_name);
272 gen_print_group(stderr, group, gtt_offset + offset,
273 &data[offset / 4], color);
274 }
275
276 static void
277 decode_structs(struct brw_context *brw, struct gen_spec *spec,
278 const char *struct_name,
279 uint32_t *data, uint32_t gtt_offset, uint32_t offset,
280 int struct_size, bool color)
281 {
282 struct gen_group *group = gen_spec_find_struct(spec, struct_name);
283 if (!group)
284 return;
285
286 int entries = brw_state_batch_size(brw, offset) / struct_size;
287 for (int i = 0; i < entries; i++) {
288 fprintf(stderr, "%s %d\n", struct_name, i);
289 gen_print_group(stderr, group, gtt_offset + offset,
290 &data[(offset + i * struct_size) / 4], color);
291 }
292 }
293
294 static void
295 do_batch_dump(struct brw_context *brw)
296 {
297 const struct gen_device_info *devinfo = &brw->screen->devinfo;
298 struct intel_batchbuffer *batch = &brw->batch;
299 struct gen_spec *spec = gen_spec_load(&brw->screen->devinfo);
300
301 if (batch->ring != RENDER_RING)
302 return;
303
304 void *map = brw_bo_map(brw, batch->bo, MAP_READ);
305 if (map == NULL) {
306 fprintf(stderr,
307 "WARNING: failed to map batchbuffer, "
308 "dumping uploaded data instead.\n");
309 }
310
311 uint32_t *data = map ? map : batch->map;
312 uint32_t *end = data + USED_BATCH(*batch);
313 uint32_t gtt_offset = map ? batch->bo->offset64 : 0;
314 int length;
315
316 bool color = INTEL_DEBUG & DEBUG_COLOR;
317 const char *header_color = color ? BLUE_HEADER : "";
318 const char *reset_color = color ? NORMAL : "";
319
320 for (uint32_t *p = data; p < end; p += length) {
321 struct gen_group *inst = gen_spec_find_instruction(spec, p);
322 length = gen_group_get_length(inst, p);
323 assert(inst == NULL || length > 0);
324 length = MAX2(1, length);
325 if (inst == NULL) {
326 fprintf(stderr, "unknown instruction %08x\n", p[0]);
327 continue;
328 }
329
330 uint64_t offset = gtt_offset + 4 * (p - data);
331
332 fprintf(stderr, "%s0x%08"PRIx64": 0x%08x: %-80s%s\n", header_color,
333 offset, p[0], gen_group_get_name(inst), reset_color);
334
335 gen_print_group(stderr, inst, offset, p, color);
336
337 switch (gen_group_get_opcode(inst) >> 16) {
338 case _3DSTATE_PIPELINED_POINTERS:
339 /* Note: these Gen4-5 pointers are full relocations rather than
340 * offsets from the start of the batch. So we need to subtract
341 * gtt_offset (the start of the batch) to obtain an offset we
342 * can add to the map and get at the data.
343 */
344 decode_struct(brw, spec, "VS_STATE", data, gtt_offset,
345 (p[1] & ~0x1fu) - gtt_offset, color);
346 if (p[2] & 1) {
347 decode_struct(brw, spec, "GS_STATE", data, gtt_offset,
348 (p[2] & ~0x1fu) - gtt_offset, color);
349 }
350 if (p[3] & 1) {
351 decode_struct(brw, spec, "CLIP_STATE", data, gtt_offset,
352 (p[3] & ~0x1fu) - gtt_offset, color);
353 }
354 decode_struct(brw, spec, "SF_STATE", data, gtt_offset,
355 (p[4] & ~0x1fu) - gtt_offset, color);
356 decode_struct(brw, spec, "WM_STATE", data, gtt_offset,
357 (p[5] & ~0x1fu) - gtt_offset, color);
358 decode_struct(brw, spec, "COLOR_CALC_STATE", data, gtt_offset,
359 (p[6] & ~0x3fu) - gtt_offset, color);
360 break;
361 case _3DSTATE_BINDING_TABLE_POINTERS_VS:
362 case _3DSTATE_BINDING_TABLE_POINTERS_HS:
363 case _3DSTATE_BINDING_TABLE_POINTERS_DS:
364 case _3DSTATE_BINDING_TABLE_POINTERS_GS:
365 case _3DSTATE_BINDING_TABLE_POINTERS_PS: {
366 struct gen_group *group =
367 gen_spec_find_struct(spec, "RENDER_SURFACE_STATE");
368 if (!group)
369 break;
370
371 uint32_t bt_offset = p[1] & ~0x1fu;
372 int bt_entries = brw_state_batch_size(brw, bt_offset) / 4;
373 uint32_t *bt_pointers = &data[bt_offset / 4];
374 for (int i = 0; i < bt_entries; i++) {
375 fprintf(stderr, "SURFACE_STATE - BTI = %d\n", i);
376 gen_print_group(stderr, group, gtt_offset + bt_pointers[i],
377 &data[bt_pointers[i] / 4], color);
378 }
379 break;
380 }
381 case _3DSTATE_SAMPLER_STATE_POINTERS_VS:
382 case _3DSTATE_SAMPLER_STATE_POINTERS_HS:
383 case _3DSTATE_SAMPLER_STATE_POINTERS_DS:
384 case _3DSTATE_SAMPLER_STATE_POINTERS_GS:
385 case _3DSTATE_SAMPLER_STATE_POINTERS_PS:
386 decode_structs(brw, spec, "SAMPLER_STATE", data,
387 gtt_offset, p[1] & ~0x1fu, 4 * 4, color);
388 break;
389 case _3DSTATE_VIEWPORT_STATE_POINTERS:
390 decode_structs(brw, spec, "CLIP_VIEWPORT", data,
391 gtt_offset, p[1] & ~0x3fu, 4 * 4, color);
392 decode_structs(brw, spec, "SF_VIEWPORT", data,
393 gtt_offset, p[1] & ~0x3fu, 8 * 4, color);
394 decode_structs(brw, spec, "CC_VIEWPORT", data,
395 gtt_offset, p[3] & ~0x3fu, 2 * 4, color);
396 break;
397 case _3DSTATE_VIEWPORT_STATE_POINTERS_CC:
398 decode_structs(brw, spec, "CC_VIEWPORT", data,
399 gtt_offset, p[1] & ~0x3fu, 2 * 4, color);
400 break;
401 case _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CL:
402 decode_structs(brw, spec, "SF_CLIP_VIEWPORT", data,
403 gtt_offset, p[1] & ~0x3fu, 16 * 4, color);
404 break;
405 case _3DSTATE_SCISSOR_STATE_POINTERS:
406 decode_structs(brw, spec, "SCISSOR_RECT", data,
407 gtt_offset, p[1] & ~0x1fu, 2 * 4, color);
408 break;
409 case _3DSTATE_BLEND_STATE_POINTERS:
410 /* TODO: handle Gen8+ extra dword at the beginning */
411 decode_structs(brw, spec, "BLEND_STATE", data,
412 gtt_offset, p[1] & ~0x3fu, 8 * 4, color);
413 break;
414 case _3DSTATE_CC_STATE_POINTERS:
415 if (devinfo->gen >= 7) {
416 decode_struct(brw, spec, "COLOR_CALC_STATE", data,
417 gtt_offset, p[1] & ~0x3fu, color);
418 } else if (devinfo->gen == 6) {
419 decode_structs(brw, spec, "BLEND_STATE", data,
420 gtt_offset, p[1] & ~0x3fu, 2 * 4, color);
421 decode_struct(brw, spec, "DEPTH_STENCIL_STATE", data,
422 gtt_offset, p[2] & ~0x3fu, color);
423 decode_struct(brw, spec, "COLOR_CALC_STATE", data,
424 gtt_offset, p[3] & ~0x3fu, color);
425 }
426 break;
427 case _3DSTATE_DEPTH_STENCIL_STATE_POINTERS:
428 decode_struct(brw, spec, "DEPTH_STENCIL_STATE", data,
429 gtt_offset, p[1] & ~0x3fu, color);
430 break;
431 }
432 }
433
434 if (map != NULL) {
435 brw_bo_unmap(batch->bo);
436 }
437 }
438 #else
439 static void do_batch_dump(struct brw_context *brw) { }
440 #endif
441
442 /**
443 * Called when starting a new batch buffer.
444 */
445 static void
446 brw_new_batch(struct brw_context *brw)
447 {
448 /* Unreference any BOs held by the previous batch, and reset counts. */
449 for (int i = 0; i < brw->batch.exec_count; i++) {
450 if (brw->batch.exec_bos[i] != brw->batch.bo) {
451 brw_bo_unreference(brw->batch.exec_bos[i]);
452 }
453 brw->batch.exec_bos[i] = NULL;
454 }
455 brw->batch.reloc_count = 0;
456 brw->batch.exec_count = 0;
457 brw->batch.aperture_space = BATCH_SZ;
458
459 /* Create a new batchbuffer and reset the associated state: */
460 intel_batchbuffer_reset_and_clear_render_cache(brw);
461
462 /* If the kernel supports hardware contexts, then most hardware state is
463 * preserved between batches; we only need to re-emit state that is required
464 * to be in every batch. Otherwise we need to re-emit all the state that
465 * would otherwise be stored in the context (which for all intents and
466 * purposes means everything).
467 */
468 if (brw->hw_ctx == 0)
469 brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;
470
471 brw->ctx.NewDriverState |= BRW_NEW_BATCH;
472
473 brw->ib.index_size = -1;
474
475 /* We need to periodically reap the shader time results, because rollover
476 * happens every few seconds. We also want to see results every once in a
477 * while, because many programs won't cleanly destroy our context, so the
478 * end-of-run printout may not happen.
479 */
480 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
481 brw_collect_and_report_shader_time(brw);
482 }
483
484 /**
485 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
486 * sending it off.
487 *
488 * This function can emit state (say, to preserve registers that aren't saved
489 * between batches). All of this state MUST fit in the reserved space at the
490 * end of the batchbuffer. If you add more GPU state, increase the reserved
491 * space by updating the BATCH_RESERVED macro.
492 */
493 static void
494 brw_finish_batch(struct brw_context *brw)
495 {
496 const struct gen_device_info *devinfo = &brw->screen->devinfo;
497
498 /* Capture the closing pipeline statistics register values necessary to
499 * support query objects (in the non-hardware context world).
500 */
501 brw_emit_query_end(brw);
502
503 if (brw->batch.ring == RENDER_RING) {
504 /* Work around L3 state leaks into contexts set MI_RESTORE_INHIBIT which
505 * assume that the L3 cache is configured according to the hardware
506 * defaults.
507 */
508 if (devinfo->gen >= 7)
509 gen7_restore_default_l3_config(brw);
510
511 if (devinfo->is_haswell) {
512 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
513 * 3DSTATE_CC_STATE_POINTERS > "Note":
514 *
515 * "SW must program 3DSTATE_CC_STATE_POINTERS command at the end of every
516 * 3D batch buffer followed by a PIPE_CONTROL with RC flush and CS stall."
517 *
518 * From the example in the docs, it seems to expect a regular pipe control
519 * flush here as well. We may have done it already, but meh.
520 *
521 * See also WaAvoidRCZCounterRollover.
522 */
523 brw_emit_mi_flush(brw);
524 BEGIN_BATCH(2);
525 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (2 - 2));
526 OUT_BATCH(brw->cc.state_offset | 1);
527 ADVANCE_BATCH();
528 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH |
529 PIPE_CONTROL_CS_STALL);
530 }
531 }
532 }
533
534 static void
535 throttle(struct brw_context *brw)
536 {
537 /* Wait for the swapbuffers before the one we just emitted, so we
538 * don't get too many swaps outstanding for apps that are GPU-heavy
539 * but not CPU-heavy.
540 *
541 * We're using intelDRI2Flush (called from the loader before
542 * swapbuffer) and glFlush (for front buffer rendering) as the
543 * indicator that a frame is done and then throttle when we get
544 * here as we prepare to render the next frame. At this point for
545 * round trips for swap/copy and getting new buffers are done and
546 * we'll spend less time waiting on the GPU.
547 *
548 * Unfortunately, we don't have a handle to the batch containing
549 * the swap, and getting our hands on that doesn't seem worth it,
550 * so we just use the first batch we emitted after the last swap.
551 */
552 if (brw->need_swap_throttle && brw->throttle_batch[0]) {
553 if (brw->throttle_batch[1]) {
554 if (!brw->disable_throttling) {
555 /* Pass NULL rather than brw so we avoid perf_debug warnings;
556 * stalling is common and expected here...
557 */
558 brw_bo_wait_rendering(brw->throttle_batch[1]);
559 }
560 brw_bo_unreference(brw->throttle_batch[1]);
561 }
562 brw->throttle_batch[1] = brw->throttle_batch[0];
563 brw->throttle_batch[0] = NULL;
564 brw->need_swap_throttle = false;
565 /* Throttling here is more precise than the throttle ioctl, so skip it */
566 brw->need_flush_throttle = false;
567 }
568
569 if (brw->need_flush_throttle) {
570 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
571 drmCommandNone(dri_screen->fd, DRM_I915_GEM_THROTTLE);
572 brw->need_flush_throttle = false;
573 }
574 }
575
576 static int
577 execbuffer(int fd,
578 struct intel_batchbuffer *batch,
579 uint32_t ctx_id,
580 int used,
581 int in_fence,
582 int *out_fence,
583 int flags)
584 {
585 struct drm_i915_gem_execbuffer2 execbuf = {
586 .buffers_ptr = (uintptr_t) batch->validation_list,
587 .buffer_count = batch->exec_count,
588 .batch_start_offset = 0,
589 .batch_len = used,
590 .flags = flags,
591 .rsvd1 = ctx_id, /* rsvd1 is actually the context ID */
592 };
593
594 unsigned long cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2;
595
596 if (in_fence != -1) {
597 execbuf.rsvd2 = in_fence;
598 execbuf.flags |= I915_EXEC_FENCE_IN;
599 }
600
601 if (out_fence != NULL) {
602 cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2_WR;
603 *out_fence = -1;
604 execbuf.flags |= I915_EXEC_FENCE_OUT;
605 }
606
607 int ret = drmIoctl(fd, cmd, &execbuf);
608 if (ret != 0)
609 ret = -errno;
610
611 for (int i = 0; i < batch->exec_count; i++) {
612 struct brw_bo *bo = batch->exec_bos[i];
613
614 bo->idle = false;
615 bo->index = -1;
616
617 /* Update brw_bo::offset64 */
618 if (batch->validation_list[i].offset != bo->offset64) {
619 DBG("BO %d migrated: 0x%" PRIx64 " -> 0x%llx\n",
620 bo->gem_handle, bo->offset64, batch->validation_list[i].offset);
621 bo->offset64 = batch->validation_list[i].offset;
622 }
623 }
624
625 if (ret == 0 && out_fence != NULL)
626 *out_fence = execbuf.rsvd2 >> 32;
627
628 return ret;
629 }
630
631 static int
632 do_flush_locked(struct brw_context *brw, int in_fence_fd, int *out_fence_fd)
633 {
634 const struct gen_device_info *devinfo = &brw->screen->devinfo;
635 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
636 struct intel_batchbuffer *batch = &brw->batch;
637 int ret = 0;
638
639 if (devinfo->has_llc) {
640 brw_bo_unmap(batch->bo);
641 } else {
642 ret = brw_bo_subdata(batch->bo, 0, 4 * USED_BATCH(*batch), batch->map);
643 if (ret == 0 && batch->state_batch_offset != batch->bo->size) {
644 ret = brw_bo_subdata(batch->bo,
645 batch->state_batch_offset,
646 batch->bo->size - batch->state_batch_offset,
647 (char *)batch->map + batch->state_batch_offset);
648 }
649 }
650
651 if (!brw->screen->no_hw) {
652 /* The requirement for using I915_EXEC_NO_RELOC are:
653 *
654 * The addresses written in the objects must match the corresponding
655 * reloc.presumed_offset which in turn must match the corresponding
656 * execobject.offset.
657 *
658 * Any render targets written to in the batch must be flagged with
659 * EXEC_OBJECT_WRITE.
660 *
661 * To avoid stalling, execobject.offset should match the current
662 * address of that object within the active context.
663 */
664 int flags = I915_EXEC_NO_RELOC;
665
666 if (devinfo->gen >= 6 && batch->ring == BLT_RING) {
667 flags |= I915_EXEC_BLT;
668 } else {
669 flags |= I915_EXEC_RENDER;
670 }
671 if (batch->needs_sol_reset)
672 flags |= I915_EXEC_GEN7_SOL_RESET;
673
674 if (ret == 0) {
675 uint32_t hw_ctx = batch->ring == RENDER_RING ? brw->hw_ctx : 0;
676
677 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[0];
678 assert(entry->handle == batch->bo->gem_handle);
679 entry->relocation_count = batch->reloc_count;
680 entry->relocs_ptr = (uintptr_t) batch->relocs;
681
682 if (batch->use_batch_first) {
683 flags |= I915_EXEC_BATCH_FIRST | I915_EXEC_HANDLE_LUT;
684 } else {
685 /* Move the batch to the end of the validation list */
686 struct drm_i915_gem_exec_object2 tmp;
687 const unsigned index = batch->exec_count - 1;
688
689 tmp = *entry;
690 *entry = batch->validation_list[index];
691 batch->validation_list[index] = tmp;
692 }
693
694 ret = execbuffer(dri_screen->fd, batch, hw_ctx,
695 4 * USED_BATCH(*batch),
696 in_fence_fd, out_fence_fd, flags);
697 }
698
699 throttle(brw);
700 }
701
702 if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
703 do_batch_dump(brw);
704
705 if (brw->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
706 brw_check_for_reset(brw);
707
708 if (ret != 0) {
709 fprintf(stderr, "intel_do_flush_locked failed: %s\n", strerror(-ret));
710 exit(1);
711 }
712
713 return ret;
714 }
715
716 /**
717 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
718 * of the fd.
719 *
720 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
721 * of the returned fd.
722 */
723 int
724 _intel_batchbuffer_flush_fence(struct brw_context *brw,
725 int in_fence_fd, int *out_fence_fd,
726 const char *file, int line)
727 {
728 int ret;
729
730 if (USED_BATCH(brw->batch) == 0)
731 return 0;
732
733 if (brw->throttle_batch[0] == NULL) {
734 brw->throttle_batch[0] = brw->batch.bo;
735 brw_bo_reference(brw->throttle_batch[0]);
736 }
737
738 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
739 int bytes_for_commands = 4 * USED_BATCH(brw->batch);
740 int bytes_for_state = brw->batch.bo->size - brw->batch.state_batch_offset;
741 int total_bytes = bytes_for_commands + bytes_for_state;
742 fprintf(stderr, "%s:%d: Batchbuffer flush with %4db (pkt) + "
743 "%4db (state) = %4db (%0.1f%%)\n", file, line,
744 bytes_for_commands, bytes_for_state,
745 total_bytes,
746 100.0f * total_bytes / BATCH_SZ);
747 }
748
749 brw->batch.reserved_space = 0;
750
751 brw_finish_batch(brw);
752
753 /* Mark the end of the buffer. */
754 intel_batchbuffer_emit_dword(&brw->batch, MI_BATCH_BUFFER_END);
755 if (USED_BATCH(brw->batch) & 1) {
756 /* Round batchbuffer usage to 2 DWORDs. */
757 intel_batchbuffer_emit_dword(&brw->batch, MI_NOOP);
758 }
759
760 intel_upload_finish(brw);
761
762 /* Check that we didn't just wrap our batchbuffer at a bad time. */
763 assert(!brw->no_batch_wrap);
764
765 ret = do_flush_locked(brw, in_fence_fd, out_fence_fd);
766
767 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
768 fprintf(stderr, "waiting for idle\n");
769 brw_bo_wait_rendering(brw->batch.bo);
770 }
771
772 /* Start a new batch buffer. */
773 brw_new_batch(brw);
774
775 return ret;
776 }
777
778 bool
779 brw_batch_has_aperture_space(struct brw_context *brw, unsigned extra_space)
780 {
781 return brw->batch.aperture_space + extra_space <=
782 brw->screen->aperture_threshold;
783 }
784
785 bool
786 brw_batch_references(struct intel_batchbuffer *batch, struct brw_bo *bo)
787 {
788 unsigned index = READ_ONCE(bo->index);
789 if (index < batch->exec_count && batch->exec_bos[index] == bo)
790 return true;
791
792 for (int i = 0; i < batch->exec_count; i++) {
793 if (batch->exec_bos[i] == bo)
794 return true;
795 }
796 return false;
797 }
798
799 /* This is the only way buffers get added to the validate list.
800 */
801 uint64_t
802 brw_emit_reloc(struct intel_batchbuffer *batch, uint32_t batch_offset,
803 struct brw_bo *target, uint32_t target_offset,
804 unsigned int reloc_flags)
805 {
806 assert(target != NULL);
807
808 if (batch->reloc_count == batch->reloc_array_size) {
809 batch->reloc_array_size *= 2;
810 batch->relocs = realloc(batch->relocs,
811 batch->reloc_array_size *
812 sizeof(struct drm_i915_gem_relocation_entry));
813 }
814
815 /* Check args */
816 assert(batch_offset <= BATCH_SZ - sizeof(uint32_t));
817
818 unsigned int index = add_exec_bo(batch, target);
819 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[index];
820
821 if (reloc_flags)
822 entry->flags |= reloc_flags & batch->valid_reloc_flags;
823
824 batch->relocs[batch->reloc_count++] =
825 (struct drm_i915_gem_relocation_entry) {
826 .offset = batch_offset,
827 .delta = target_offset,
828 .target_handle = batch->use_batch_first ? index : target->gem_handle,
829 .presumed_offset = entry->offset,
830 };
831
832 /* Using the old buffer offset, write in what the right data would be, in
833 * case the buffer doesn't move and we can short-circuit the relocation
834 * processing in the kernel
835 */
836 return entry->offset + target_offset;
837 }
838
839 void
840 intel_batchbuffer_data(struct brw_context *brw,
841 const void *data, GLuint bytes, enum brw_gpu_ring ring)
842 {
843 assert((bytes & 3) == 0);
844 intel_batchbuffer_require_space(brw, bytes, ring);
845 memcpy(brw->batch.map_next, data, bytes);
846 brw->batch.map_next += bytes >> 2;
847 }
848
849 static void
850 load_sized_register_mem(struct brw_context *brw,
851 uint32_t reg,
852 struct brw_bo *bo,
853 uint32_t offset,
854 int size)
855 {
856 const struct gen_device_info *devinfo = &brw->screen->devinfo;
857 int i;
858
859 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
860 assert(devinfo->gen >= 7);
861
862 if (devinfo->gen >= 8) {
863 BEGIN_BATCH(4 * size);
864 for (i = 0; i < size; i++) {
865 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (4 - 2));
866 OUT_BATCH(reg + i * 4);
867 OUT_RELOC64(bo, 0, offset + i * 4);
868 }
869 ADVANCE_BATCH();
870 } else {
871 BEGIN_BATCH(3 * size);
872 for (i = 0; i < size; i++) {
873 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
874 OUT_BATCH(reg + i * 4);
875 OUT_RELOC(bo, 0, offset + i * 4);
876 }
877 ADVANCE_BATCH();
878 }
879 }
880
881 void
882 brw_load_register_mem(struct brw_context *brw,
883 uint32_t reg,
884 struct brw_bo *bo,
885 uint32_t offset)
886 {
887 load_sized_register_mem(brw, reg, bo, offset, 1);
888 }
889
890 void
891 brw_load_register_mem64(struct brw_context *brw,
892 uint32_t reg,
893 struct brw_bo *bo,
894 uint32_t offset)
895 {
896 load_sized_register_mem(brw, reg, bo, offset, 2);
897 }
898
899 /*
900 * Write an arbitrary 32-bit register to a buffer via MI_STORE_REGISTER_MEM.
901 */
902 void
903 brw_store_register_mem32(struct brw_context *brw,
904 struct brw_bo *bo, uint32_t reg, uint32_t offset)
905 {
906 const struct gen_device_info *devinfo = &brw->screen->devinfo;
907
908 assert(devinfo->gen >= 6);
909
910 if (devinfo->gen >= 8) {
911 BEGIN_BATCH(4);
912 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
913 OUT_BATCH(reg);
914 OUT_RELOC64(bo, RELOC_WRITE, offset);
915 ADVANCE_BATCH();
916 } else {
917 BEGIN_BATCH(3);
918 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
919 OUT_BATCH(reg);
920 OUT_RELOC(bo, RELOC_WRITE | RELOC_NEEDS_GGTT, offset);
921 ADVANCE_BATCH();
922 }
923 }
924
925 /*
926 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
927 */
928 void
929 brw_store_register_mem64(struct brw_context *brw,
930 struct brw_bo *bo, uint32_t reg, uint32_t offset)
931 {
932 const struct gen_device_info *devinfo = &brw->screen->devinfo;
933
934 assert(devinfo->gen >= 6);
935
936 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
937 * read a full 64-bit register, we need to do two of them.
938 */
939 if (devinfo->gen >= 8) {
940 BEGIN_BATCH(8);
941 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
942 OUT_BATCH(reg);
943 OUT_RELOC64(bo, RELOC_WRITE, offset);
944 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
945 OUT_BATCH(reg + sizeof(uint32_t));
946 OUT_RELOC64(bo, RELOC_WRITE, offset + sizeof(uint32_t));
947 ADVANCE_BATCH();
948 } else {
949 BEGIN_BATCH(6);
950 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
951 OUT_BATCH(reg);
952 OUT_RELOC(bo, RELOC_WRITE | RELOC_NEEDS_GGTT, offset);
953 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
954 OUT_BATCH(reg + sizeof(uint32_t));
955 OUT_RELOC(bo, RELOC_WRITE | RELOC_NEEDS_GGTT, offset + sizeof(uint32_t));
956 ADVANCE_BATCH();
957 }
958 }
959
960 /*
961 * Write a 32-bit register using immediate data.
962 */
963 void
964 brw_load_register_imm32(struct brw_context *brw, uint32_t reg, uint32_t imm)
965 {
966 const struct gen_device_info *devinfo = &brw->screen->devinfo;
967
968 assert(devinfo->gen >= 6);
969
970 BEGIN_BATCH(3);
971 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
972 OUT_BATCH(reg);
973 OUT_BATCH(imm);
974 ADVANCE_BATCH();
975 }
976
977 /*
978 * Write a 64-bit register using immediate data.
979 */
980 void
981 brw_load_register_imm64(struct brw_context *brw, uint32_t reg, uint64_t imm)
982 {
983 const struct gen_device_info *devinfo = &brw->screen->devinfo;
984
985 assert(devinfo->gen >= 6);
986
987 BEGIN_BATCH(5);
988 OUT_BATCH(MI_LOAD_REGISTER_IMM | (5 - 2));
989 OUT_BATCH(reg);
990 OUT_BATCH(imm & 0xffffffff);
991 OUT_BATCH(reg + 4);
992 OUT_BATCH(imm >> 32);
993 ADVANCE_BATCH();
994 }
995
996 /*
997 * Copies a 32-bit register.
998 */
999 void
1000 brw_load_register_reg(struct brw_context *brw, uint32_t src, uint32_t dest)
1001 {
1002 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1003
1004 assert(devinfo->gen >= 8 || devinfo->is_haswell);
1005
1006 BEGIN_BATCH(3);
1007 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
1008 OUT_BATCH(src);
1009 OUT_BATCH(dest);
1010 ADVANCE_BATCH();
1011 }
1012
1013 /*
1014 * Copies a 64-bit register.
1015 */
1016 void
1017 brw_load_register_reg64(struct brw_context *brw, uint32_t src, uint32_t dest)
1018 {
1019 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1020
1021 assert(devinfo->gen >= 8 || devinfo->is_haswell);
1022
1023 BEGIN_BATCH(6);
1024 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
1025 OUT_BATCH(src);
1026 OUT_BATCH(dest);
1027 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
1028 OUT_BATCH(src + sizeof(uint32_t));
1029 OUT_BATCH(dest + sizeof(uint32_t));
1030 ADVANCE_BATCH();
1031 }
1032
1033 /*
1034 * Write 32-bits of immediate data to a GPU memory buffer.
1035 */
1036 void
1037 brw_store_data_imm32(struct brw_context *brw, struct brw_bo *bo,
1038 uint32_t offset, uint32_t imm)
1039 {
1040 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1041
1042 assert(devinfo->gen >= 6);
1043
1044 BEGIN_BATCH(4);
1045 OUT_BATCH(MI_STORE_DATA_IMM | (4 - 2));
1046 if (devinfo->gen >= 8)
1047 OUT_RELOC64(bo, RELOC_WRITE, offset);
1048 else {
1049 OUT_BATCH(0); /* MBZ */
1050 OUT_RELOC(bo, RELOC_WRITE, offset);
1051 }
1052 OUT_BATCH(imm);
1053 ADVANCE_BATCH();
1054 }
1055
1056 /*
1057 * Write 64-bits of immediate data to a GPU memory buffer.
1058 */
1059 void
1060 brw_store_data_imm64(struct brw_context *brw, struct brw_bo *bo,
1061 uint32_t offset, uint64_t imm)
1062 {
1063 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1064
1065 assert(devinfo->gen >= 6);
1066
1067 BEGIN_BATCH(5);
1068 OUT_BATCH(MI_STORE_DATA_IMM | (5 - 2));
1069 if (devinfo->gen >= 8)
1070 OUT_RELOC64(bo, 0, offset);
1071 else {
1072 OUT_BATCH(0); /* MBZ */
1073 OUT_RELOC(bo, RELOC_WRITE, offset);
1074 }
1075 OUT_BATCH(imm & 0xffffffffu);
1076 OUT_BATCH(imm >> 32);
1077 ADVANCE_BATCH();
1078 }