i965: Assert the execobject handles match for this device
[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 #include "common/gen_gem.h"
36
37 #include "util/hash_table.h"
38
39 #include <xf86drm.h>
40 #include "drm-uapi/i915_drm.h"
41
42 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
43
44 /**
45 * Target sizes of the batch and state buffers. We create the initial
46 * buffers at these sizes, and flush when they're nearly full. If we
47 * underestimate how close we are to the end, and suddenly need more space
48 * in the middle of a draw, we can grow the buffers, and finish the draw.
49 * At that point, we'll be over our target size, so the next operation
50 * should flush. Each time we flush the batch, we recreate both buffers
51 * at the original target size, so it doesn't grow without bound.
52 */
53 #define BATCH_SZ (20 * 1024)
54 #define STATE_SZ (16 * 1024)
55
56 static void
57 intel_batchbuffer_reset(struct brw_context *brw);
58 static void
59 brw_new_batch(struct brw_context *brw);
60
61 static void
62 dump_validation_list(struct intel_batchbuffer *batch)
63 {
64 fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
65
66 for (int i = 0; i < batch->exec_count; i++) {
67 uint64_t flags = batch->validation_list[i].flags;
68 assert(batch->validation_list[i].handle ==
69 batch->exec_bos[i]->gem_handle);
70 fprintf(stderr, "[%2d]: %2d %-14s %p %s%-7s @ 0x%016llx%s (%"PRIu64"B)\n",
71 i,
72 batch->validation_list[i].handle,
73 batch->exec_bos[i]->name,
74 batch->exec_bos[i],
75 (flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) ? "(48b" : "(32b",
76 (flags & EXEC_OBJECT_WRITE) ? " write)" : ")",
77 batch->validation_list[i].offset,
78 (flags & EXEC_OBJECT_PINNED) ? " (pinned)" : "",
79 batch->exec_bos[i]->size);
80 }
81 }
82
83 static struct gen_batch_decode_bo
84 decode_get_bo(void *v_brw, uint64_t address)
85 {
86 struct brw_context *brw = v_brw;
87 struct intel_batchbuffer *batch = &brw->batch;
88
89 for (int i = 0; i < batch->exec_count; i++) {
90 struct brw_bo *bo = batch->exec_bos[i];
91 /* The decoder zeroes out the top 16 bits, so we need to as well */
92 uint64_t bo_address = bo->gtt_offset & (~0ull >> 16);
93
94 if (address >= bo_address && address < bo_address + bo->size) {
95 return (struct gen_batch_decode_bo) {
96 .addr = address,
97 .size = bo->size,
98 .map = brw_bo_map(brw, bo, MAP_READ) + (address - bo_address),
99 };
100 }
101 }
102
103 return (struct gen_batch_decode_bo) { };
104 }
105
106 static unsigned
107 decode_get_state_size(void *v_brw, uint32_t offset_from_dsba)
108 {
109 struct brw_context *brw = v_brw;
110 struct intel_batchbuffer *batch = &brw->batch;
111 struct hash_entry *entry =
112 _mesa_hash_table_search(batch->state_batch_sizes,
113 (void *) (uintptr_t) offset_from_dsba);
114 return entry ? (uintptr_t) entry->data : 0;
115 }
116
117 static bool
118 uint_key_compare(const void *a, const void *b)
119 {
120 return a == b;
121 }
122
123 static uint32_t
124 uint_key_hash(const void *key)
125 {
126 return (uintptr_t) key;
127 }
128
129 static void
130 init_reloc_list(struct brw_reloc_list *rlist, int count)
131 {
132 rlist->reloc_count = 0;
133 rlist->reloc_array_size = count;
134 rlist->relocs = malloc(rlist->reloc_array_size *
135 sizeof(struct drm_i915_gem_relocation_entry));
136 }
137
138 void
139 intel_batchbuffer_init(struct brw_context *brw)
140 {
141 struct intel_screen *screen = brw->screen;
142 struct intel_batchbuffer *batch = &brw->batch;
143 const struct gen_device_info *devinfo = &screen->devinfo;
144
145 batch->use_shadow_copy = !devinfo->has_llc;
146
147 init_reloc_list(&batch->batch_relocs, 250);
148 init_reloc_list(&batch->state_relocs, 250);
149
150 batch->batch.map = NULL;
151 batch->state.map = NULL;
152 batch->exec_count = 0;
153 batch->exec_array_size = 100;
154 batch->exec_bos =
155 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
156 batch->validation_list =
157 malloc(batch->exec_array_size * sizeof(batch->validation_list[0]));
158
159 if (INTEL_DEBUG & DEBUG_BATCH) {
160 batch->state_batch_sizes =
161 _mesa_hash_table_create(NULL, uint_key_hash, uint_key_compare);
162
163 const unsigned decode_flags =
164 GEN_BATCH_DECODE_FULL |
165 ((INTEL_DEBUG & DEBUG_COLOR) ? GEN_BATCH_DECODE_IN_COLOR : 0) |
166 GEN_BATCH_DECODE_OFFSETS |
167 GEN_BATCH_DECODE_FLOATS;
168
169 gen_batch_decode_ctx_init(&batch->decoder, devinfo, stderr,
170 decode_flags, NULL, decode_get_bo,
171 decode_get_state_size, brw);
172 batch->decoder.max_vbo_decoded_lines = 100;
173 }
174
175 batch->use_batch_first =
176 screen->kernel_features & KERNEL_ALLOWS_EXEC_BATCH_FIRST;
177
178 /* PIPE_CONTROL needs a w/a but only on gen6 */
179 batch->valid_reloc_flags = EXEC_OBJECT_WRITE;
180 if (devinfo->gen == 6)
181 batch->valid_reloc_flags |= EXEC_OBJECT_NEEDS_GTT;
182
183 intel_batchbuffer_reset(brw);
184 }
185
186 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
187
188 static unsigned
189 add_exec_bo(struct intel_batchbuffer *batch, struct brw_bo *bo)
190 {
191 assert(bo->bufmgr == batch->batch.bo->bufmgr);
192
193 unsigned index = READ_ONCE(bo->index);
194
195 if (index < batch->exec_count && batch->exec_bos[index] == bo)
196 return index;
197
198 /* May have been shared between multiple active batches */
199 for (index = 0; index < batch->exec_count; index++) {
200 if (batch->exec_bos[index] == bo)
201 return index;
202 }
203
204 brw_bo_reference(bo);
205
206 if (batch->exec_count == batch->exec_array_size) {
207 batch->exec_array_size *= 2;
208 batch->exec_bos =
209 realloc(batch->exec_bos,
210 batch->exec_array_size * sizeof(batch->exec_bos[0]));
211 batch->validation_list =
212 realloc(batch->validation_list,
213 batch->exec_array_size * sizeof(batch->validation_list[0]));
214 }
215
216 batch->validation_list[batch->exec_count] =
217 (struct drm_i915_gem_exec_object2) {
218 .handle = bo->gem_handle,
219 .offset = bo->gtt_offset,
220 .flags = bo->kflags,
221 };
222
223 bo->index = batch->exec_count;
224 batch->exec_bos[batch->exec_count] = bo;
225 batch->aperture_space += bo->size;
226
227 return batch->exec_count++;
228 }
229
230 static void
231 recreate_growing_buffer(struct brw_context *brw,
232 struct brw_growing_bo *grow,
233 const char *name, unsigned size,
234 enum brw_memory_zone memzone)
235 {
236 struct intel_screen *screen = brw->screen;
237 struct intel_batchbuffer *batch = &brw->batch;
238 struct brw_bufmgr *bufmgr = screen->bufmgr;
239
240 /* We can't grow buffers when using softpin, so just overallocate them. */
241 if (brw_using_softpin(bufmgr))
242 size *= 2;
243
244 grow->bo = brw_bo_alloc(bufmgr, name, size, memzone);
245 grow->bo->kflags |= can_do_exec_capture(screen) ? EXEC_OBJECT_CAPTURE : 0;
246 grow->partial_bo = NULL;
247 grow->partial_bo_map = NULL;
248 grow->partial_bytes = 0;
249 grow->memzone = memzone;
250
251 if (batch->use_shadow_copy)
252 grow->map = realloc(grow->map, grow->bo->size);
253 else
254 grow->map = brw_bo_map(brw, grow->bo, MAP_READ | MAP_WRITE);
255 }
256
257 static void
258 intel_batchbuffer_reset(struct brw_context *brw)
259 {
260 struct intel_batchbuffer *batch = &brw->batch;
261
262 if (batch->last_bo != NULL) {
263 brw_bo_unreference(batch->last_bo);
264 batch->last_bo = NULL;
265 }
266 batch->last_bo = batch->batch.bo;
267
268 recreate_growing_buffer(brw, &batch->batch, "batchbuffer", BATCH_SZ,
269 BRW_MEMZONE_OTHER);
270 batch->map_next = batch->batch.map;
271
272 recreate_growing_buffer(brw, &batch->state, "statebuffer", STATE_SZ,
273 BRW_MEMZONE_DYNAMIC);
274
275 /* Avoid making 0 a valid state offset - otherwise the decoder will try
276 * and decode data when we use offset 0 as a null pointer.
277 */
278 batch->state_used = 1;
279
280 add_exec_bo(batch, batch->batch.bo);
281 assert(batch->batch.bo->index == 0);
282
283 batch->needs_sol_reset = false;
284 batch->state_base_address_emitted = false;
285
286 if (batch->state_batch_sizes)
287 _mesa_hash_table_clear(batch->state_batch_sizes, NULL);
288 }
289
290 static void
291 intel_batchbuffer_reset_and_clear_render_cache(struct brw_context *brw)
292 {
293 intel_batchbuffer_reset(brw);
294 brw_cache_sets_clear(brw);
295 }
296
297 void
298 intel_batchbuffer_save_state(struct brw_context *brw)
299 {
300 brw->batch.saved.map_next = brw->batch.map_next;
301 brw->batch.saved.batch_reloc_count = brw->batch.batch_relocs.reloc_count;
302 brw->batch.saved.state_reloc_count = brw->batch.state_relocs.reloc_count;
303 brw->batch.saved.exec_count = brw->batch.exec_count;
304 }
305
306 bool
307 intel_batchbuffer_saved_state_is_empty(struct brw_context *brw)
308 {
309 struct intel_batchbuffer *batch = &brw->batch;
310 return (batch->saved.map_next == batch->batch.map);
311 }
312
313 void
314 intel_batchbuffer_reset_to_saved(struct brw_context *brw)
315 {
316 for (int i = brw->batch.saved.exec_count;
317 i < brw->batch.exec_count; i++) {
318 brw_bo_unreference(brw->batch.exec_bos[i]);
319 }
320 brw->batch.batch_relocs.reloc_count = brw->batch.saved.batch_reloc_count;
321 brw->batch.state_relocs.reloc_count = brw->batch.saved.state_reloc_count;
322 brw->batch.exec_count = brw->batch.saved.exec_count;
323
324 brw->batch.map_next = brw->batch.saved.map_next;
325 if (USED_BATCH(brw->batch) == 0)
326 brw_new_batch(brw);
327 }
328
329 void
330 intel_batchbuffer_free(struct intel_batchbuffer *batch)
331 {
332 if (batch->use_shadow_copy) {
333 free(batch->batch.map);
334 free(batch->state.map);
335 }
336
337 for (int i = 0; i < batch->exec_count; i++) {
338 brw_bo_unreference(batch->exec_bos[i]);
339 }
340 free(batch->batch_relocs.relocs);
341 free(batch->state_relocs.relocs);
342 free(batch->exec_bos);
343 free(batch->validation_list);
344
345 brw_bo_unreference(batch->last_bo);
346 brw_bo_unreference(batch->batch.bo);
347 brw_bo_unreference(batch->state.bo);
348 if (batch->state_batch_sizes) {
349 _mesa_hash_table_destroy(batch->state_batch_sizes, NULL);
350 gen_batch_decode_ctx_finish(&batch->decoder);
351 }
352 }
353
354 /**
355 * Finish copying the old batch/state buffer's contents to the new one
356 * after we tried to "grow" the buffer in an earlier operation.
357 */
358 static void
359 finish_growing_bos(struct brw_growing_bo *grow)
360 {
361 struct brw_bo *old_bo = grow->partial_bo;
362 if (!old_bo)
363 return;
364
365 memcpy(grow->map, grow->partial_bo_map, grow->partial_bytes);
366
367 grow->partial_bo = NULL;
368 grow->partial_bo_map = NULL;
369 grow->partial_bytes = 0;
370
371 brw_bo_unreference(old_bo);
372 }
373
374 static void
375 replace_bo_in_reloc_list(struct brw_reloc_list *rlist,
376 uint32_t old_handle, uint32_t new_handle)
377 {
378 for (int i = 0; i < rlist->reloc_count; i++) {
379 if (rlist->relocs[i].target_handle == old_handle)
380 rlist->relocs[i].target_handle = new_handle;
381 }
382 }
383
384 /**
385 * Grow either the batch or state buffer to a new larger size.
386 *
387 * We can't actually grow buffers, so we allocate a new one, copy over
388 * the existing contents, and update our lists to refer to the new one.
389 *
390 * Note that this is only temporary - each new batch recreates the buffers
391 * at their original target size (BATCH_SZ or STATE_SZ).
392 */
393 static void
394 grow_buffer(struct brw_context *brw,
395 struct brw_growing_bo *grow,
396 unsigned existing_bytes,
397 unsigned new_size)
398 {
399 struct intel_batchbuffer *batch = &brw->batch;
400 struct brw_bufmgr *bufmgr = brw->bufmgr;
401 struct brw_bo *bo = grow->bo;
402
403 /* We can't grow buffers that are softpinned, as the growing mechanism
404 * involves putting a larger buffer at the same gtt_offset...and we've
405 * only allocated the smaller amount of VMA. Without relocations, this
406 * simply won't work. This should never happen, however.
407 */
408 assert(!(bo->kflags & EXEC_OBJECT_PINNED));
409
410 perf_debug("Growing %s - ran out of space\n", bo->name);
411
412 if (grow->partial_bo) {
413 /* We've already grown once, and now we need to do it again.
414 * Finish our last grow operation so we can start a new one.
415 * This should basically never happen.
416 */
417 perf_debug("Had to grow multiple times");
418 finish_growing_bos(grow);
419 }
420
421 struct brw_bo *new_bo =
422 brw_bo_alloc(bufmgr, bo->name, new_size, grow->memzone);
423
424 /* Copy existing data to the new larger buffer */
425 grow->partial_bo_map = grow->map;
426
427 if (batch->use_shadow_copy) {
428 /* We can't safely use realloc, as it may move the existing buffer,
429 * breaking existing pointers the caller may still be using. Just
430 * malloc a new copy and memcpy it like the normal BO path.
431 *
432 * Use bo->size rather than new_size because the bufmgr may have
433 * rounded up the size, and we want the shadow size to match.
434 */
435 grow->map = malloc(new_bo->size);
436 } else {
437 grow->map = brw_bo_map(brw, new_bo, MAP_READ | MAP_WRITE);
438 }
439
440 /* Try to put the new BO at the same GTT offset as the old BO (which
441 * we're throwing away, so it doesn't need to be there).
442 *
443 * This guarantees that our relocations continue to work: values we've
444 * already written into the buffer, values we're going to write into the
445 * buffer, and the validation/relocation lists all will match.
446 *
447 * Also preserve kflags for EXEC_OBJECT_CAPTURE.
448 */
449 new_bo->gtt_offset = bo->gtt_offset;
450 new_bo->index = bo->index;
451 new_bo->kflags = bo->kflags;
452
453 /* Batch/state buffers are per-context, and if we've run out of space,
454 * we must have actually used them before, so...they will be in the list.
455 */
456 assert(bo->index < batch->exec_count);
457 assert(batch->exec_bos[bo->index] == bo);
458
459 /* Update the validation list to use the new BO. */
460 batch->validation_list[bo->index].handle = new_bo->gem_handle;
461
462 if (!batch->use_batch_first) {
463 /* We're not using I915_EXEC_HANDLE_LUT, which means we need to go
464 * update the relocation list entries to point at the new BO as well.
465 * (With newer kernels, the "handle" is an offset into the validation
466 * list, which remains unchanged, so we can skip this.)
467 */
468 replace_bo_in_reloc_list(&batch->batch_relocs,
469 bo->gem_handle, new_bo->gem_handle);
470 replace_bo_in_reloc_list(&batch->state_relocs,
471 bo->gem_handle, new_bo->gem_handle);
472 }
473
474 /* Exchange the two BOs...without breaking pointers to the old BO.
475 *
476 * Consider this scenario:
477 *
478 * 1. Somebody calls brw_state_batch() to get a region of memory, and
479 * and then creates a brw_address pointing to brw->batch.state.bo.
480 * 2. They then call brw_state_batch() a second time, which happens to
481 * grow and replace the state buffer. They then try to emit a
482 * relocation to their first section of memory.
483 *
484 * If we replace the brw->batch.state.bo pointer at step 2, we would
485 * break the address created in step 1. They'd have a pointer to the
486 * old destroyed BO. Emitting a relocation would add this dead BO to
487 * the validation list...causing /both/ statebuffers to be in the list,
488 * and all kinds of disasters.
489 *
490 * This is not a contrived case - BLORP vertex data upload hits this.
491 *
492 * There are worse scenarios too. Fences for GL sync objects reference
493 * brw->batch.batch.bo. If we replaced the batch pointer when growing,
494 * we'd need to chase down every fence and update it to point to the
495 * new BO. Otherwise, it would refer to a "batch" that never actually
496 * gets submitted, and would fail to trigger.
497 *
498 * To work around both of these issues, we transmutate the buffers in
499 * place, making the existing struct brw_bo represent the new buffer,
500 * and "new_bo" represent the old BO. This is highly unusual, but it
501 * seems like a necessary evil.
502 *
503 * We also defer the memcpy of the existing batch's contents. Callers
504 * may make multiple brw_state_batch calls, and retain pointers to the
505 * old BO's map. We'll perform the memcpy in finish_growing_bo() when
506 * we finally submit the batch, at which point we've finished uploading
507 * state, and nobody should have any old references anymore.
508 *
509 * To do that, we keep a reference to the old BO in grow->partial_bo,
510 * and store the number of bytes to copy in grow->partial_bytes. We
511 * can monkey with the refcounts directly without atomics because these
512 * are per-context BOs and they can only be touched by this thread.
513 */
514 assert(new_bo->refcount == 1);
515 new_bo->refcount = bo->refcount;
516 bo->refcount = 1;
517
518 struct brw_bo tmp;
519 memcpy(&tmp, bo, sizeof(struct brw_bo));
520 memcpy(bo, new_bo, sizeof(struct brw_bo));
521 memcpy(new_bo, &tmp, sizeof(struct brw_bo));
522
523 grow->partial_bo = new_bo; /* the one reference of the OLD bo */
524 grow->partial_bytes = existing_bytes;
525 }
526
527 void
528 intel_batchbuffer_require_space(struct brw_context *brw, GLuint sz)
529 {
530 struct intel_batchbuffer *batch = &brw->batch;
531
532 const unsigned batch_used = USED_BATCH(*batch) * 4;
533 if (batch_used + sz >= BATCH_SZ && !batch->no_wrap) {
534 intel_batchbuffer_flush(brw);
535 } else if (batch_used + sz >= batch->batch.bo->size) {
536 const unsigned new_size =
537 MIN2(batch->batch.bo->size + batch->batch.bo->size / 2,
538 MAX_BATCH_SIZE);
539 grow_buffer(brw, &batch->batch, batch_used, new_size);
540 batch->map_next = (void *) batch->batch.map + batch_used;
541 assert(batch_used + sz < batch->batch.bo->size);
542 }
543 }
544
545 /**
546 * Called when starting a new batch buffer.
547 */
548 static void
549 brw_new_batch(struct brw_context *brw)
550 {
551 /* Unreference any BOs held by the previous batch, and reset counts. */
552 for (int i = 0; i < brw->batch.exec_count; i++) {
553 brw_bo_unreference(brw->batch.exec_bos[i]);
554 brw->batch.exec_bos[i] = NULL;
555 }
556 brw->batch.batch_relocs.reloc_count = 0;
557 brw->batch.state_relocs.reloc_count = 0;
558 brw->batch.exec_count = 0;
559 brw->batch.aperture_space = 0;
560
561 brw_bo_unreference(brw->batch.state.bo);
562
563 /* Create a new batchbuffer and reset the associated state: */
564 intel_batchbuffer_reset_and_clear_render_cache(brw);
565
566 /* If the kernel supports hardware contexts, then most hardware state is
567 * preserved between batches; we only need to re-emit state that is required
568 * to be in every batch. Otherwise we need to re-emit all the state that
569 * would otherwise be stored in the context (which for all intents and
570 * purposes means everything).
571 */
572 if (brw->hw_ctx == 0) {
573 brw->ctx.NewDriverState |= BRW_NEW_CONTEXT;
574 brw_upload_invariant_state(brw);
575 }
576
577 brw->ctx.NewDriverState |= BRW_NEW_BATCH;
578
579 brw->ib.index_size = -1;
580
581 /* We need to periodically reap the shader time results, because rollover
582 * happens every few seconds. We also want to see results every once in a
583 * while, because many programs won't cleanly destroy our context, so the
584 * end-of-run printout may not happen.
585 */
586 if (INTEL_DEBUG & DEBUG_SHADER_TIME)
587 brw_collect_and_report_shader_time(brw);
588 }
589
590 /**
591 * Called from intel_batchbuffer_flush before emitting MI_BATCHBUFFER_END and
592 * sending it off.
593 *
594 * This function can emit state (say, to preserve registers that aren't saved
595 * between batches).
596 */
597 static void
598 brw_finish_batch(struct brw_context *brw)
599 {
600 const struct gen_device_info *devinfo = &brw->screen->devinfo;
601
602 brw->batch.no_wrap = true;
603
604 /* Capture the closing pipeline statistics register values necessary to
605 * support query objects (in the non-hardware context world).
606 */
607 brw_emit_query_end(brw);
608
609 /* Work around L3 state leaks into contexts set MI_RESTORE_INHIBIT which
610 * assume that the L3 cache is configured according to the hardware
611 * defaults. On Kernel 4.16+, we no longer need to do this.
612 */
613 if (devinfo->gen >= 7 &&
614 !(brw->screen->kernel_features & KERNEL_ALLOWS_CONTEXT_ISOLATION))
615 gen7_restore_default_l3_config(brw);
616
617 if (devinfo->is_haswell) {
618 /* From the Haswell PRM, Volume 2b, Command Reference: Instructions,
619 * 3DSTATE_CC_STATE_POINTERS > "Note":
620 *
621 * "SW must program 3DSTATE_CC_STATE_POINTERS command at the end of every
622 * 3D batch buffer followed by a PIPE_CONTROL with RC flush and CS stall."
623 *
624 * From the example in the docs, it seems to expect a regular pipe control
625 * flush here as well. We may have done it already, but meh.
626 *
627 * See also WaAvoidRCZCounterRollover.
628 */
629 brw_emit_mi_flush(brw);
630 BEGIN_BATCH(2);
631 OUT_BATCH(_3DSTATE_CC_STATE_POINTERS << 16 | (2 - 2));
632 OUT_BATCH(brw->cc.state_offset | 1);
633 ADVANCE_BATCH();
634 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_RENDER_TARGET_FLUSH |
635 PIPE_CONTROL_CS_STALL);
636 }
637
638 /* Do not restore push constant packets during context restore. */
639 if (devinfo->gen >= 7)
640 gen10_emit_isp_disable(brw);
641
642 /* Emit MI_BATCH_BUFFER_END to finish our batch. Note that execbuf2
643 * requires our batch size to be QWord aligned, so we pad it out if
644 * necessary by emitting an extra MI_NOOP after the end.
645 */
646 intel_batchbuffer_require_space(brw, 8);
647 *brw->batch.map_next++ = MI_BATCH_BUFFER_END;
648 if (USED_BATCH(brw->batch) & 1) {
649 *brw->batch.map_next++ = MI_NOOP;
650 }
651
652 brw->batch.no_wrap = false;
653 }
654
655 static void
656 throttle(struct brw_context *brw)
657 {
658 /* Wait for the swapbuffers before the one we just emitted, so we
659 * don't get too many swaps outstanding for apps that are GPU-heavy
660 * but not CPU-heavy.
661 *
662 * We're using intelDRI2Flush (called from the loader before
663 * swapbuffer) and glFlush (for front buffer rendering) as the
664 * indicator that a frame is done and then throttle when we get
665 * here as we prepare to render the next frame. At this point for
666 * round trips for swap/copy and getting new buffers are done and
667 * we'll spend less time waiting on the GPU.
668 *
669 * Unfortunately, we don't have a handle to the batch containing
670 * the swap, and getting our hands on that doesn't seem worth it,
671 * so we just use the first batch we emitted after the last swap.
672 */
673 if (brw->need_swap_throttle && brw->throttle_batch[0]) {
674 if (brw->throttle_batch[1]) {
675 if (!brw->disable_throttling) {
676 brw_bo_wait_rendering(brw->throttle_batch[1]);
677 }
678 brw_bo_unreference(brw->throttle_batch[1]);
679 }
680 brw->throttle_batch[1] = brw->throttle_batch[0];
681 brw->throttle_batch[0] = NULL;
682 brw->need_swap_throttle = false;
683 /* Throttling here is more precise than the throttle ioctl, so skip it */
684 brw->need_flush_throttle = false;
685 }
686
687 if (brw->need_flush_throttle) {
688 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
689 drmCommandNone(dri_screen->fd, DRM_I915_GEM_THROTTLE);
690 brw->need_flush_throttle = false;
691 }
692 }
693
694 static int
695 execbuffer(int fd,
696 struct intel_batchbuffer *batch,
697 uint32_t ctx_id,
698 int used,
699 int in_fence,
700 int *out_fence,
701 int flags)
702 {
703 struct drm_i915_gem_execbuffer2 execbuf = {
704 .buffers_ptr = (uintptr_t) batch->validation_list,
705 .buffer_count = batch->exec_count,
706 .batch_start_offset = 0,
707 .batch_len = used,
708 .flags = flags,
709 .rsvd1 = ctx_id, /* rsvd1 is actually the context ID */
710 };
711
712 unsigned long cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2;
713
714 if (in_fence != -1) {
715 execbuf.rsvd2 = in_fence;
716 execbuf.flags |= I915_EXEC_FENCE_IN;
717 }
718
719 if (out_fence != NULL) {
720 cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2_WR;
721 *out_fence = -1;
722 execbuf.flags |= I915_EXEC_FENCE_OUT;
723 }
724
725 int ret = drmIoctl(fd, cmd, &execbuf);
726 if (ret != 0)
727 ret = -errno;
728
729 for (int i = 0; i < batch->exec_count; i++) {
730 struct brw_bo *bo = batch->exec_bos[i];
731
732 bo->idle = false;
733 bo->index = -1;
734
735 /* Update brw_bo::gtt_offset */
736 if (batch->validation_list[i].offset != bo->gtt_offset) {
737 DBG("BO %d migrated: 0x%" PRIx64 " -> 0x%llx\n",
738 bo->gem_handle, bo->gtt_offset,
739 batch->validation_list[i].offset);
740 assert(!(bo->kflags & EXEC_OBJECT_PINNED));
741 bo->gtt_offset = batch->validation_list[i].offset;
742 }
743 }
744
745 if (ret == 0 && out_fence != NULL)
746 *out_fence = execbuf.rsvd2 >> 32;
747
748 return ret;
749 }
750
751 static int
752 submit_batch(struct brw_context *brw, int in_fence_fd, int *out_fence_fd)
753 {
754 __DRIscreen *dri_screen = brw->screen->driScrnPriv;
755 struct intel_batchbuffer *batch = &brw->batch;
756 int ret = 0;
757
758 if (batch->use_shadow_copy) {
759 void *bo_map = brw_bo_map(brw, batch->batch.bo, MAP_WRITE);
760 memcpy(bo_map, batch->batch.map, 4 * USED_BATCH(*batch));
761
762 bo_map = brw_bo_map(brw, batch->state.bo, MAP_WRITE);
763 memcpy(bo_map, batch->state.map, batch->state_used);
764 }
765
766 brw_bo_unmap(batch->batch.bo);
767 brw_bo_unmap(batch->state.bo);
768
769 if (!brw->screen->no_hw) {
770 /* The requirement for using I915_EXEC_NO_RELOC are:
771 *
772 * The addresses written in the objects must match the corresponding
773 * reloc.gtt_offset which in turn must match the corresponding
774 * execobject.offset.
775 *
776 * Any render targets written to in the batch must be flagged with
777 * EXEC_OBJECT_WRITE.
778 *
779 * To avoid stalling, execobject.offset should match the current
780 * address of that object within the active context.
781 */
782 int flags = I915_EXEC_NO_RELOC | I915_EXEC_RENDER;
783
784 if (batch->needs_sol_reset)
785 flags |= I915_EXEC_GEN7_SOL_RESET;
786
787 /* Set statebuffer relocations */
788 const unsigned state_index = batch->state.bo->index;
789 if (state_index < batch->exec_count &&
790 batch->exec_bos[state_index] == batch->state.bo) {
791 struct drm_i915_gem_exec_object2 *entry =
792 &batch->validation_list[state_index];
793 assert(entry->handle == batch->state.bo->gem_handle);
794 entry->relocation_count = batch->state_relocs.reloc_count;
795 entry->relocs_ptr = (uintptr_t) batch->state_relocs.relocs;
796 }
797
798 /* Set batchbuffer relocations */
799 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[0];
800 assert(entry->handle == batch->batch.bo->gem_handle);
801 entry->relocation_count = batch->batch_relocs.reloc_count;
802 entry->relocs_ptr = (uintptr_t) batch->batch_relocs.relocs;
803
804 if (batch->use_batch_first) {
805 flags |= I915_EXEC_BATCH_FIRST | I915_EXEC_HANDLE_LUT;
806 } else {
807 /* Move the batch to the end of the validation list */
808 struct drm_i915_gem_exec_object2 tmp;
809 struct brw_bo *tmp_bo;
810 const unsigned index = batch->exec_count - 1;
811
812 tmp = *entry;
813 *entry = batch->validation_list[index];
814 batch->validation_list[index] = tmp;
815
816 tmp_bo = batch->exec_bos[0];
817 batch->exec_bos[0] = batch->exec_bos[index];
818 batch->exec_bos[index] = tmp_bo;
819 }
820
821 ret = execbuffer(dri_screen->fd, batch, brw->hw_ctx,
822 4 * USED_BATCH(*batch),
823 in_fence_fd, out_fence_fd, flags);
824
825 throttle(brw);
826 }
827
828 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
829 gen_print_batch(&batch->decoder, batch->batch.map,
830 4 * USED_BATCH(*batch),
831 batch->batch.bo->gtt_offset);
832 }
833
834 if (brw->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
835 brw_check_for_reset(brw);
836
837 if (ret != 0) {
838 fprintf(stderr, "i965: Failed to submit batchbuffer: %s\n",
839 strerror(-ret));
840 exit(1);
841 }
842
843 return ret;
844 }
845
846 /**
847 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
848 * of the fd.
849 *
850 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
851 * of the returned fd.
852 */
853 int
854 _intel_batchbuffer_flush_fence(struct brw_context *brw,
855 int in_fence_fd, int *out_fence_fd,
856 const char *file, int line)
857 {
858 int ret;
859
860 if (USED_BATCH(brw->batch) == 0)
861 return 0;
862
863 /* Check that we didn't just wrap our batchbuffer at a bad time. */
864 assert(!brw->batch.no_wrap);
865
866 brw_finish_batch(brw);
867 brw_upload_finish(&brw->upload);
868
869 finish_growing_bos(&brw->batch.batch);
870 finish_growing_bos(&brw->batch.state);
871
872 if (brw->throttle_batch[0] == NULL) {
873 brw->throttle_batch[0] = brw->batch.batch.bo;
874 brw_bo_reference(brw->throttle_batch[0]);
875 }
876
877 if (unlikely(INTEL_DEBUG & (DEBUG_BATCH | DEBUG_SUBMIT))) {
878 int bytes_for_commands = 4 * USED_BATCH(brw->batch);
879 int bytes_for_state = brw->batch.state_used;
880 fprintf(stderr, "%19s:%-3d: Batchbuffer flush with %5db (%0.1f%%) (pkt),"
881 " %5db (%0.1f%%) (state), %4d BOs (%0.1fMb aperture),"
882 " %4d batch relocs, %4d state relocs\n", file, line,
883 bytes_for_commands, 100.0f * bytes_for_commands / BATCH_SZ,
884 bytes_for_state, 100.0f * bytes_for_state / STATE_SZ,
885 brw->batch.exec_count,
886 (float) (brw->batch.aperture_space / (1024 * 1024)),
887 brw->batch.batch_relocs.reloc_count,
888 brw->batch.state_relocs.reloc_count);
889
890 dump_validation_list(&brw->batch);
891 }
892
893 ret = submit_batch(brw, in_fence_fd, out_fence_fd);
894
895 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
896 fprintf(stderr, "waiting for idle\n");
897 brw_bo_wait_rendering(brw->batch.batch.bo);
898 }
899
900 /* Start a new batch buffer. */
901 brw_new_batch(brw);
902
903 return ret;
904 }
905
906 bool
907 brw_batch_references(struct intel_batchbuffer *batch, struct brw_bo *bo)
908 {
909 unsigned index = READ_ONCE(bo->index);
910 if (index < batch->exec_count && batch->exec_bos[index] == bo)
911 return true;
912
913 for (int i = 0; i < batch->exec_count; i++) {
914 if (batch->exec_bos[i] == bo)
915 return true;
916 }
917 return false;
918 }
919
920 /* This is the only way buffers get added to the validate list.
921 */
922 static uint64_t
923 emit_reloc(struct intel_batchbuffer *batch,
924 struct brw_reloc_list *rlist, uint32_t offset,
925 struct brw_bo *target, int32_t target_offset,
926 unsigned int reloc_flags)
927 {
928 assert(target != NULL);
929
930 if (target->kflags & EXEC_OBJECT_PINNED) {
931 brw_use_pinned_bo(batch, target, reloc_flags & RELOC_WRITE);
932 return gen_canonical_address(target->gtt_offset + target_offset);
933 }
934
935 unsigned int index = add_exec_bo(batch, target);
936 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[index];
937
938 if (rlist->reloc_count == rlist->reloc_array_size) {
939 rlist->reloc_array_size *= 2;
940 rlist->relocs = realloc(rlist->relocs,
941 rlist->reloc_array_size *
942 sizeof(struct drm_i915_gem_relocation_entry));
943 }
944
945 if (reloc_flags & RELOC_32BIT) {
946 /* Restrict this buffer to the low 32 bits of the address space.
947 *
948 * Altering the validation list flags restricts it for this batch,
949 * but we also alter the BO's kflags to restrict it permanently
950 * (until the BO is destroyed and put back in the cache). Buffers
951 * may stay bound across batches, and we want keep it constrained.
952 */
953 target->kflags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
954 entry->flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
955
956 /* RELOC_32BIT is not an EXEC_OBJECT_* flag, so get rid of it. */
957 reloc_flags &= ~RELOC_32BIT;
958 }
959
960 if (reloc_flags)
961 entry->flags |= reloc_flags & batch->valid_reloc_flags;
962
963 rlist->relocs[rlist->reloc_count++] =
964 (struct drm_i915_gem_relocation_entry) {
965 .offset = offset,
966 .delta = target_offset,
967 .target_handle = batch->use_batch_first ? index : target->gem_handle,
968 .presumed_offset = entry->offset,
969 };
970
971 /* Using the old buffer offset, write in what the right data would be, in
972 * case the buffer doesn't move and we can short-circuit the relocation
973 * processing in the kernel
974 */
975 return entry->offset + target_offset;
976 }
977
978 void
979 brw_use_pinned_bo(struct intel_batchbuffer *batch, struct brw_bo *bo,
980 unsigned writable_flag)
981 {
982 assert(bo->kflags & EXEC_OBJECT_PINNED);
983 assert((writable_flag & ~EXEC_OBJECT_WRITE) == 0);
984
985 unsigned int index = add_exec_bo(batch, bo);
986 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[index];
987 assert(entry->offset == bo->gtt_offset);
988
989 if (writable_flag)
990 entry->flags |= EXEC_OBJECT_WRITE;
991 }
992
993 uint64_t
994 brw_batch_reloc(struct intel_batchbuffer *batch, uint32_t batch_offset,
995 struct brw_bo *target, uint32_t target_offset,
996 unsigned int reloc_flags)
997 {
998 assert(batch_offset <= batch->batch.bo->size - sizeof(uint32_t));
999
1000 return emit_reloc(batch, &batch->batch_relocs, batch_offset,
1001 target, target_offset, reloc_flags);
1002 }
1003
1004 uint64_t
1005 brw_state_reloc(struct intel_batchbuffer *batch, uint32_t state_offset,
1006 struct brw_bo *target, uint32_t target_offset,
1007 unsigned int reloc_flags)
1008 {
1009 assert(state_offset <= batch->state.bo->size - sizeof(uint32_t));
1010
1011 return emit_reloc(batch, &batch->state_relocs, state_offset,
1012 target, target_offset, reloc_flags);
1013 }
1014
1015 /**
1016 * Reserve some space in the statebuffer, or flush.
1017 *
1018 * This is used to estimate when we're near the end of the batch,
1019 * so we can flush early.
1020 */
1021 void
1022 brw_require_statebuffer_space(struct brw_context *brw, int size)
1023 {
1024 if (brw->batch.state_used + size >= STATE_SZ)
1025 intel_batchbuffer_flush(brw);
1026 }
1027
1028 /**
1029 * Allocates a block of space in the batchbuffer for indirect state.
1030 */
1031 void *
1032 brw_state_batch(struct brw_context *brw,
1033 int size,
1034 int alignment,
1035 uint32_t *out_offset)
1036 {
1037 struct intel_batchbuffer *batch = &brw->batch;
1038
1039 assert(size < batch->state.bo->size);
1040
1041 uint32_t offset = ALIGN(batch->state_used, alignment);
1042
1043 if (offset + size >= STATE_SZ && !batch->no_wrap) {
1044 intel_batchbuffer_flush(brw);
1045 offset = ALIGN(batch->state_used, alignment);
1046 } else if (offset + size >= batch->state.bo->size) {
1047 const unsigned new_size =
1048 MIN2(batch->state.bo->size + batch->state.bo->size / 2,
1049 MAX_STATE_SIZE);
1050 grow_buffer(brw, &batch->state, batch->state_used, new_size);
1051 assert(offset + size < batch->state.bo->size);
1052 }
1053
1054 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
1055 _mesa_hash_table_insert(batch->state_batch_sizes,
1056 (void *) (uintptr_t) offset,
1057 (void *) (uintptr_t) size);
1058 }
1059
1060 batch->state_used = offset + size;
1061
1062 *out_offset = offset;
1063 return batch->state.map + (offset >> 2);
1064 }
1065
1066 void
1067 intel_batchbuffer_data(struct brw_context *brw,
1068 const void *data, GLuint bytes)
1069 {
1070 assert((bytes & 3) == 0);
1071 intel_batchbuffer_require_space(brw, bytes);
1072 memcpy(brw->batch.map_next, data, bytes);
1073 brw->batch.map_next += bytes >> 2;
1074 }
1075
1076 static void
1077 load_sized_register_mem(struct brw_context *brw,
1078 uint32_t reg,
1079 struct brw_bo *bo,
1080 uint32_t offset,
1081 int size)
1082 {
1083 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1084 int i;
1085
1086 /* MI_LOAD_REGISTER_MEM only exists on Gen7+. */
1087 assert(devinfo->gen >= 7);
1088
1089 if (devinfo->gen >= 8) {
1090 BEGIN_BATCH(4 * size);
1091 for (i = 0; i < size; i++) {
1092 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (4 - 2));
1093 OUT_BATCH(reg + i * 4);
1094 OUT_RELOC64(bo, 0, offset + i * 4);
1095 }
1096 ADVANCE_BATCH();
1097 } else {
1098 BEGIN_BATCH(3 * size);
1099 for (i = 0; i < size; i++) {
1100 OUT_BATCH(GEN7_MI_LOAD_REGISTER_MEM | (3 - 2));
1101 OUT_BATCH(reg + i * 4);
1102 OUT_RELOC(bo, 0, offset + i * 4);
1103 }
1104 ADVANCE_BATCH();
1105 }
1106 }
1107
1108 void
1109 brw_load_register_mem(struct brw_context *brw,
1110 uint32_t reg,
1111 struct brw_bo *bo,
1112 uint32_t offset)
1113 {
1114 load_sized_register_mem(brw, reg, bo, offset, 1);
1115 }
1116
1117 void
1118 brw_load_register_mem64(struct brw_context *brw,
1119 uint32_t reg,
1120 struct brw_bo *bo,
1121 uint32_t offset)
1122 {
1123 load_sized_register_mem(brw, reg, bo, offset, 2);
1124 }
1125
1126 /*
1127 * Write an arbitrary 32-bit register to a buffer via MI_STORE_REGISTER_MEM.
1128 */
1129 void
1130 brw_store_register_mem32(struct brw_context *brw,
1131 struct brw_bo *bo, uint32_t reg, uint32_t offset)
1132 {
1133 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1134
1135 assert(devinfo->gen >= 6);
1136
1137 if (devinfo->gen >= 8) {
1138 BEGIN_BATCH(4);
1139 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
1140 OUT_BATCH(reg);
1141 OUT_RELOC64(bo, RELOC_WRITE, offset);
1142 ADVANCE_BATCH();
1143 } else {
1144 BEGIN_BATCH(3);
1145 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
1146 OUT_BATCH(reg);
1147 OUT_RELOC(bo, RELOC_WRITE | RELOC_NEEDS_GGTT, offset);
1148 ADVANCE_BATCH();
1149 }
1150 }
1151
1152 /*
1153 * Write an arbitrary 64-bit register to a buffer via MI_STORE_REGISTER_MEM.
1154 */
1155 void
1156 brw_store_register_mem64(struct brw_context *brw,
1157 struct brw_bo *bo, uint32_t reg, uint32_t offset)
1158 {
1159 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1160
1161 assert(devinfo->gen >= 6);
1162
1163 /* MI_STORE_REGISTER_MEM only stores a single 32-bit value, so to
1164 * read a full 64-bit register, we need to do two of them.
1165 */
1166 if (devinfo->gen >= 8) {
1167 BEGIN_BATCH(8);
1168 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
1169 OUT_BATCH(reg);
1170 OUT_RELOC64(bo, RELOC_WRITE, offset);
1171 OUT_BATCH(MI_STORE_REGISTER_MEM | (4 - 2));
1172 OUT_BATCH(reg + sizeof(uint32_t));
1173 OUT_RELOC64(bo, RELOC_WRITE, offset + sizeof(uint32_t));
1174 ADVANCE_BATCH();
1175 } else {
1176 BEGIN_BATCH(6);
1177 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
1178 OUT_BATCH(reg);
1179 OUT_RELOC(bo, RELOC_WRITE | RELOC_NEEDS_GGTT, offset);
1180 OUT_BATCH(MI_STORE_REGISTER_MEM | (3 - 2));
1181 OUT_BATCH(reg + sizeof(uint32_t));
1182 OUT_RELOC(bo, RELOC_WRITE | RELOC_NEEDS_GGTT, offset + sizeof(uint32_t));
1183 ADVANCE_BATCH();
1184 }
1185 }
1186
1187 /*
1188 * Write a 32-bit register using immediate data.
1189 */
1190 void
1191 brw_load_register_imm32(struct brw_context *brw, uint32_t reg, uint32_t imm)
1192 {
1193 assert(brw->screen->devinfo.gen >= 6);
1194
1195 BEGIN_BATCH(3);
1196 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
1197 OUT_BATCH(reg);
1198 OUT_BATCH(imm);
1199 ADVANCE_BATCH();
1200 }
1201
1202 /*
1203 * Write a 64-bit register using immediate data.
1204 */
1205 void
1206 brw_load_register_imm64(struct brw_context *brw, uint32_t reg, uint64_t imm)
1207 {
1208 assert(brw->screen->devinfo.gen >= 6);
1209
1210 BEGIN_BATCH(5);
1211 OUT_BATCH(MI_LOAD_REGISTER_IMM | (5 - 2));
1212 OUT_BATCH(reg);
1213 OUT_BATCH(imm & 0xffffffff);
1214 OUT_BATCH(reg + 4);
1215 OUT_BATCH(imm >> 32);
1216 ADVANCE_BATCH();
1217 }
1218
1219 /*
1220 * Copies a 32-bit register.
1221 */
1222 void
1223 brw_load_register_reg(struct brw_context *brw, uint32_t dest, uint32_t src)
1224 {
1225 assert(brw->screen->devinfo.gen >= 8 || brw->screen->devinfo.is_haswell);
1226
1227 BEGIN_BATCH(3);
1228 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
1229 OUT_BATCH(src);
1230 OUT_BATCH(dest);
1231 ADVANCE_BATCH();
1232 }
1233
1234 /*
1235 * Copies a 64-bit register.
1236 */
1237 void
1238 brw_load_register_reg64(struct brw_context *brw, uint32_t dest, uint32_t src)
1239 {
1240 assert(brw->screen->devinfo.gen >= 8 || brw->screen->devinfo.is_haswell);
1241
1242 BEGIN_BATCH(6);
1243 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
1244 OUT_BATCH(src);
1245 OUT_BATCH(dest);
1246 OUT_BATCH(MI_LOAD_REGISTER_REG | (3 - 2));
1247 OUT_BATCH(src + sizeof(uint32_t));
1248 OUT_BATCH(dest + sizeof(uint32_t));
1249 ADVANCE_BATCH();
1250 }
1251
1252 /*
1253 * Write 32-bits of immediate data to a GPU memory buffer.
1254 */
1255 void
1256 brw_store_data_imm32(struct brw_context *brw, struct brw_bo *bo,
1257 uint32_t offset, uint32_t imm)
1258 {
1259 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1260
1261 assert(devinfo->gen >= 6);
1262
1263 BEGIN_BATCH(4);
1264 OUT_BATCH(MI_STORE_DATA_IMM | (4 - 2));
1265 if (devinfo->gen >= 8)
1266 OUT_RELOC64(bo, RELOC_WRITE, offset);
1267 else {
1268 OUT_BATCH(0); /* MBZ */
1269 OUT_RELOC(bo, RELOC_WRITE, offset);
1270 }
1271 OUT_BATCH(imm);
1272 ADVANCE_BATCH();
1273 }
1274
1275 /*
1276 * Write 64-bits of immediate data to a GPU memory buffer.
1277 */
1278 void
1279 brw_store_data_imm64(struct brw_context *brw, struct brw_bo *bo,
1280 uint32_t offset, uint64_t imm)
1281 {
1282 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1283
1284 assert(devinfo->gen >= 6);
1285
1286 BEGIN_BATCH(5);
1287 OUT_BATCH(MI_STORE_DATA_IMM | (5 - 2));
1288 if (devinfo->gen >= 8)
1289 OUT_RELOC64(bo, RELOC_WRITE, offset);
1290 else {
1291 OUT_BATCH(0); /* MBZ */
1292 OUT_RELOC(bo, RELOC_WRITE, offset);
1293 }
1294 OUT_BATCH(imm & 0xffffffffu);
1295 OUT_BATCH(imm >> 32);
1296 ADVANCE_BATCH();
1297 }