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