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