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