iris: initial render state upload
[mesa.git] / src / gallium / drivers / iris / iris_batch.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "iris_batch.h"
26 #include "iris_bufmgr.h"
27 #include "iris_context.h"
28
29 #include "drm-uapi/i915_drm.h"
30
31 #include "util/hash_table.h"
32 #include "main/macros.h"
33
34 #include <errno.h>
35 #include <xf86drm.h>
36
37 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
38
39 /**
40 * Target sizes of the batch and state buffers. We create the initial
41 * buffers at these sizes, and flush when they're nearly full. If we
42 * underestimate how close we are to the end, and suddenly need more space
43 * in the middle of a draw, we can grow the buffers, and finish the draw.
44 * At that point, we'll be over our target size, so the next operation
45 * should flush. Each time we flush the batch, we recreate both buffers
46 * at the original target size, so it doesn't grow without bound.
47 */
48 #define BATCH_SZ (20 * 1024)
49 #define STATE_SZ (18 * 1024)
50
51 /* The kernel assumes batchbuffers are smaller than 256kB. */
52 #define MAX_BATCH_SIZE (256 * 1024)
53
54 /* 3DSTATE_BINDING_TABLE_POINTERS has a U16 offset from Surface State Base
55 * Address, which means that we can't put binding tables beyond 64kB. This
56 * effectively limits the maximum statebuffer size to 64kB.
57 */
58 #define MAX_STATE_SIZE (64 * 1024)
59
60 static void
61 iris_batch_reset(struct iris_batch *batch);
62
63 UNUSED static void
64 dump_validation_list(struct iris_batch *batch)
65 {
66 fprintf(stderr, "Validation list (length %d):\n", batch->exec_count);
67
68 for (int i = 0; i < batch->exec_count; i++) {
69 assert(batch->validation_list[i].handle ==
70 batch->exec_bos[i]->gem_handle);
71 fprintf(stderr, "[%d] = %d %s %p\n", i,
72 batch->validation_list[i].handle,
73 batch->exec_bos[i]->name,
74 batch->exec_bos[i]);
75 }
76 }
77
78 static bool
79 uint_key_compare(const void *a, const void *b)
80 {
81 return a == b;
82 }
83
84 static uint32_t
85 uint_key_hash(const void *key)
86 {
87 return (uintptr_t) key;
88 }
89
90 static void
91 init_reloc_list(struct iris_reloc_list *rlist, int count)
92 {
93 rlist->reloc_count = 0;
94 rlist->reloc_array_size = count;
95 rlist->relocs = malloc(rlist->reloc_array_size *
96 sizeof(struct drm_i915_gem_relocation_entry));
97 }
98
99 static void
100 create_batch_buffer(struct iris_bufmgr *bufmgr,
101 struct iris_batch_buffer *buf,
102 const char *name, unsigned size)
103 {
104 buf->bo = iris_bo_alloc(bufmgr, name, size, 4096);
105 buf->bo->kflags |= EXEC_OBJECT_CAPTURE;
106 buf->map = iris_bo_map(NULL, buf->bo, MAP_READ | MAP_WRITE);
107 buf->map_next = buf->map;
108 }
109
110 void
111 iris_batch_init(struct iris_batch *batch,
112 struct iris_screen *screen,
113 struct pipe_debug_callback *dbg)
114 {
115 batch->screen = screen;
116 batch->dbg = dbg;
117
118 init_reloc_list(&batch->cmdbuf.relocs, 256);
119 init_reloc_list(&batch->statebuf.relocs, 256);
120
121 batch->exec_count = 0;
122 batch->exec_array_size = 100;
123 batch->exec_bos =
124 malloc(batch->exec_array_size * sizeof(batch->exec_bos[0]));
125 batch->validation_list =
126 malloc(batch->exec_array_size * sizeof(batch->validation_list[0]));
127
128 if (unlikely(INTEL_DEBUG)) {
129 batch->state_sizes =
130 _mesa_hash_table_create(NULL, uint_key_hash, uint_key_compare);
131 }
132
133 iris_batch_reset(batch);
134 }
135
136 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
137
138 static unsigned
139 add_exec_bo(struct iris_batch *batch, struct iris_bo *bo)
140 {
141 unsigned index = READ_ONCE(bo->index);
142
143 if (index < batch->exec_count && batch->exec_bos[index] == bo)
144 return index;
145
146 /* May have been shared between multiple active batches */
147 for (index = 0; index < batch->exec_count; index++) {
148 if (batch->exec_bos[index] == bo)
149 return index;
150 }
151
152 iris_bo_reference(bo);
153
154 if (batch->exec_count == batch->exec_array_size) {
155 batch->exec_array_size *= 2;
156 batch->exec_bos =
157 realloc(batch->exec_bos,
158 batch->exec_array_size * sizeof(batch->exec_bos[0]));
159 batch->validation_list =
160 realloc(batch->validation_list,
161 batch->exec_array_size * sizeof(batch->validation_list[0]));
162 }
163
164 batch->validation_list[batch->exec_count] =
165 (struct drm_i915_gem_exec_object2) {
166 .handle = bo->gem_handle,
167 .alignment = bo->align,
168 .offset = bo->gtt_offset,
169 .flags = bo->kflags,
170 };
171
172 bo->index = batch->exec_count;
173 batch->exec_bos[batch->exec_count] = bo;
174 batch->aperture_space += bo->size;
175
176 return batch->exec_count++;
177 }
178
179 static void
180 iris_batch_reset(struct iris_batch *batch)
181 {
182 struct iris_screen *screen = batch->screen;
183 struct iris_bufmgr *bufmgr = screen->bufmgr;
184
185 if (batch->last_cmd_bo != NULL) {
186 iris_bo_unreference(batch->last_cmd_bo);
187 batch->last_cmd_bo = NULL;
188 }
189 batch->last_cmd_bo = batch->cmdbuf.bo;
190
191 create_batch_buffer(bufmgr, &batch->cmdbuf, "command buffer", BATCH_SZ);
192 create_batch_buffer(bufmgr, &batch->statebuf, "state buffer", STATE_SZ);
193
194 /* Avoid making 0 a valid state offset - otherwise the decoder will try
195 * and decode data when we use offset 0 as a null pointer.
196 */
197 batch->statebuf.map_next += 1;
198
199 add_exec_bo(batch, batch->cmdbuf.bo);
200 assert(batch->cmdbuf.bo->index == 0);
201
202 if (batch->state_sizes)
203 _mesa_hash_table_clear(batch->state_sizes, NULL);
204 }
205
206 static void
207 iris_batch_reset_and_clear_render_cache(struct iris_batch *batch)
208 {
209 iris_batch_reset(batch);
210 // XXX: iris_render_cache_set_clear(batch);
211 }
212
213 static void
214 free_batch_buffer(struct iris_batch_buffer *buf)
215 {
216 iris_bo_unreference(buf->bo);
217 buf->bo = NULL;
218 buf->map = NULL;
219 buf->map_next = NULL;
220
221 free(buf->relocs.relocs);
222 buf->relocs.relocs = NULL;
223 buf->relocs.reloc_array_size = 0;
224 }
225
226 void
227 iris_batch_free(struct iris_batch *batch)
228 {
229 for (int i = 0; i < batch->exec_count; i++) {
230 iris_bo_unreference(batch->exec_bos[i]);
231 }
232 free(batch->exec_bos);
233 free(batch->validation_list);
234 free_batch_buffer(&batch->cmdbuf);
235 free_batch_buffer(&batch->statebuf);
236
237 iris_bo_unreference(batch->last_cmd_bo);
238
239 if (batch->state_sizes)
240 _mesa_hash_table_destroy(batch->state_sizes, NULL);
241 }
242
243 /**
244 * Finish copying the old batch/state buffer's contents to the new one
245 * after we tried to "grow" the buffer in an earlier operation.
246 */
247 static void
248 finish_growing_bos(struct iris_batch_buffer *buf)
249 {
250 struct iris_bo *old_bo = buf->partial_bo;
251 if (!old_bo)
252 return;
253
254 void *old_map = old_bo->map_cpu ? old_bo->map_cpu : old_bo->map_wc;
255 memcpy(buf->map, old_map, buf->partial_bytes);
256
257 buf->partial_bo = NULL;
258 buf->partial_bytes = 0;
259
260 iris_bo_unreference(old_bo);
261 }
262
263 static unsigned
264 buffer_bytes_used(struct iris_batch_buffer *buf)
265 {
266 return buf->map_next - buf->map;
267 }
268
269 /**
270 * Grow either the batch or state buffer to a new larger size.
271 *
272 * We can't actually grow buffers, so we allocate a new one, copy over
273 * the existing contents, and update our lists to refer to the new one.
274 *
275 * Note that this is only temporary - each new batch recreates the buffers
276 * at their original target size (BATCH_SZ or STATE_SZ).
277 */
278 static void
279 grow_buffer(struct iris_batch *batch,
280 struct iris_batch_buffer *buf,
281 unsigned new_size)
282 {
283 struct iris_bufmgr *bufmgr = batch->screen->bufmgr;
284 struct iris_bo *bo = buf->bo;
285
286 perf_debug(batch->dbg, "Growing %s - ran out of space\n", bo->name);
287
288 if (buf->partial_bo) {
289 /* We've already grown once, and now we need to do it again.
290 * Finish our last grow operation so we can start a new one.
291 * This should basically never happen.
292 */
293 perf_debug(batch->dbg, "Had to grow multiple times");
294 finish_growing_bos(buf);
295 }
296
297 const unsigned existing_bytes = buffer_bytes_used(buf);
298
299 struct iris_bo *new_bo =
300 iris_bo_alloc(bufmgr, bo->name, new_size, bo->align);
301
302 buf->map = iris_bo_map(NULL, new_bo, MAP_READ | MAP_WRITE);
303 buf->map_next = buf->map + existing_bytes;
304
305 /* Try to put the new BO at the same GTT offset as the old BO (which
306 * we're throwing away, so it doesn't need to be there).
307 *
308 * This guarantees that our relocations continue to work: values we've
309 * already written into the buffer, values we're going to write into the
310 * buffer, and the validation/relocation lists all will match.
311 *
312 * Also preserve kflags for EXEC_OBJECT_CAPTURE.
313 */
314 new_bo->gtt_offset = bo->gtt_offset;
315 new_bo->index = bo->index;
316 new_bo->kflags = bo->kflags;
317
318 /* Batch/state buffers are per-context, and if we've run out of space,
319 * we must have actually used them before, so...they will be in the list.
320 */
321 assert(bo->index < batch->exec_count);
322 assert(batch->exec_bos[bo->index] == bo);
323
324 /* Update the validation list to use the new BO. */
325 batch->exec_bos[bo->index] = new_bo;
326 batch->validation_list[bo->index].handle = new_bo->gem_handle;
327
328 /* Exchange the two BOs...without breaking pointers to the old BO.
329 *
330 * Consider this scenario:
331 *
332 * 1. Somebody calls brw_state_batch() to get a region of memory, and
333 * and then creates a brw_address pointing to brw->batch.state.bo.
334 * 2. They then call brw_state_batch() a second time, which happens to
335 * grow and replace the state buffer. They then try to emit a
336 * relocation to their first section of memory.
337 *
338 * If we replace the brw->batch.state.bo pointer at step 2, we would
339 * break the address created in step 1. They'd have a pointer to the
340 * old destroyed BO. Emitting a relocation would add this dead BO to
341 * the validation list...causing /both/ statebuffers to be in the list,
342 * and all kinds of disasters.
343 *
344 * This is not a contrived case - BLORP vertex data upload hits this.
345 *
346 * There are worse scenarios too. Fences for GL sync objects reference
347 * brw->batch.batch.bo. If we replaced the batch pointer when growing,
348 * we'd need to chase down every fence and update it to point to the
349 * new BO. Otherwise, it would refer to a "batch" that never actually
350 * gets submitted, and would fail to trigger.
351 *
352 * To work around both of these issues, we transmutate the buffers in
353 * place, making the existing struct brw_bo represent the new buffer,
354 * and "new_bo" represent the old BO. This is highly unusual, but it
355 * seems like a necessary evil.
356 *
357 * We also defer the memcpy of the existing batch's contents. Callers
358 * may make multiple brw_state_batch calls, and retain pointers to the
359 * old BO's map. We'll perform the memcpy in finish_growing_bo() when
360 * we finally submit the batch, at which point we've finished uploading
361 * state, and nobody should have any old references anymore.
362 *
363 * To do that, we keep a reference to the old BO in grow->partial_bo,
364 * and store the number of bytes to copy in grow->partial_bytes. We
365 * can monkey with the refcounts directly without atomics because these
366 * are per-context BOs and they can only be touched by this thread.
367 */
368 assert(new_bo->refcount == 1);
369 new_bo->refcount = bo->refcount;
370 bo->refcount = 1;
371
372 struct iris_bo tmp;
373 memcpy(&tmp, bo, sizeof(struct iris_bo));
374 memcpy(bo, new_bo, sizeof(struct iris_bo));
375 memcpy(new_bo, &tmp, sizeof(struct iris_bo));
376
377 buf->partial_bo = new_bo; /* the one reference of the OLD bo */
378 buf->partial_bytes = existing_bytes;
379 }
380
381 static void
382 require_buffer_space(struct iris_batch *batch,
383 struct iris_batch_buffer *buf,
384 unsigned size,
385 unsigned flush_threshold,
386 unsigned max_buffer_size)
387 {
388 const unsigned required_bytes = buffer_bytes_used(buf) + size;
389
390 if (!batch->no_wrap && required_bytes >= flush_threshold) {
391 iris_batch_flush(batch);
392 } else if (required_bytes >= buf->bo->size) {
393 grow_buffer(batch, buf,
394 MIN2(buf->bo->size + buf->bo->size / 2, max_buffer_size));
395 assert(required_bytes < buf->bo->size);
396 }
397 }
398
399
400 void
401 iris_require_command_space(struct iris_batch *batch, unsigned size)
402 {
403 require_buffer_space(batch, &batch->cmdbuf, size, BATCH_SZ, MAX_BATCH_SIZE);
404 }
405
406 /**
407 * Reserve some space in the statebuffer, or flush.
408 *
409 * This is used to estimate when we're near the end of the batch,
410 * so we can flush early.
411 */
412 void
413 iris_require_state_space(struct iris_batch *batch, unsigned size)
414 {
415 require_buffer_space(batch, &batch->statebuf, size, STATE_SZ,
416 MAX_STATE_SIZE);
417 }
418
419 void
420 iris_batch_emit(struct iris_batch *batch, const void *data, unsigned size)
421 {
422 iris_require_command_space(batch, size);
423 memcpy(batch->cmdbuf.map_next, data, size);
424 }
425
426 /**
427 * Called when starting a new batch buffer.
428 */
429 static void
430 iris_new_batch(struct iris_batch *batch)
431 {
432 /* Unreference any BOs held by the previous batch, and reset counts. */
433 for (int i = 0; i < batch->exec_count; i++) {
434 iris_bo_unreference(batch->exec_bos[i]);
435 batch->exec_bos[i] = NULL;
436 }
437 batch->cmdbuf.relocs.reloc_count = 0;
438 batch->statebuf.relocs.reloc_count = 0;
439 batch->exec_count = 0;
440 batch->aperture_space = 0;
441
442 iris_bo_unreference(batch->statebuf.bo);
443
444 /* Create a new batchbuffer and reset the associated state: */
445 iris_batch_reset_and_clear_render_cache(batch);
446 }
447
448 /**
449 * Called from iris_batch_flush before emitting MI_BATCHBUFFER_END and
450 * sending it off.
451 *
452 * This function can emit state (say, to preserve registers that aren't saved
453 * between batches).
454 */
455 static void
456 iris_finish_batch(struct iris_batch *batch)
457 {
458 batch->no_wrap = true;
459
460 /* Mark the end of the buffer. */
461 const uint32_t MI_BATCH_BUFFER_END = (0xA << 23);
462 iris_batch_emit(batch, &MI_BATCH_BUFFER_END, sizeof(uint32_t));
463
464 batch->no_wrap = false;
465 }
466
467 static int
468 submit_batch(struct iris_batch *batch, int in_fence_fd, int *out_fence_fd)
469 {
470 iris_bo_unmap(batch->cmdbuf.bo);
471 iris_bo_unmap(batch->statebuf.bo);
472
473 /* The requirement for using I915_EXEC_NO_RELOC are:
474 *
475 * The addresses written in the objects must match the corresponding
476 * reloc.gtt_offset which in turn must match the corresponding
477 * execobject.offset.
478 *
479 * Any render targets written to in the batch must be flagged with
480 * EXEC_OBJECT_WRITE.
481 *
482 * To avoid stalling, execobject.offset should match the current
483 * address of that object within the active context.
484 */
485 /* Set statebuffer relocations */
486 const unsigned state_index = batch->statebuf.bo->index;
487 if (state_index < batch->exec_count &&
488 batch->exec_bos[state_index] == batch->statebuf.bo) {
489 struct drm_i915_gem_exec_object2 *entry =
490 &batch->validation_list[state_index];
491 assert(entry->handle == batch->statebuf.bo->gem_handle);
492 entry->relocation_count = batch->statebuf.relocs.reloc_count;
493 entry->relocs_ptr = (uintptr_t) batch->statebuf.relocs.relocs;
494 }
495
496 /* Set batchbuffer relocations */
497 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[0];
498 assert(entry->handle == batch->cmdbuf.bo->gem_handle);
499 entry->relocation_count = batch->cmdbuf.relocs.reloc_count;
500 entry->relocs_ptr = (uintptr_t) batch->cmdbuf.relocs.relocs;
501
502 struct drm_i915_gem_execbuffer2 execbuf = {
503 .buffers_ptr = (uintptr_t) batch->validation_list,
504 .buffer_count = batch->exec_count,
505 .batch_start_offset = 0,
506 .batch_len = buffer_bytes_used(&batch->cmdbuf),
507 .flags = I915_EXEC_NO_RELOC |
508 I915_EXEC_BATCH_FIRST |
509 I915_EXEC_HANDLE_LUT |
510 I915_EXEC_RENDER,
511 .rsvd1 = batch->hw_ctx_id, /* rsvd1 is actually the context ID */
512 };
513
514 unsigned long cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2;
515
516 if (in_fence_fd != -1) {
517 execbuf.rsvd2 = in_fence_fd;
518 execbuf.flags |= I915_EXEC_FENCE_IN;
519 }
520
521 if (out_fence_fd != NULL) {
522 cmd = DRM_IOCTL_I915_GEM_EXECBUFFER2_WR;
523 *out_fence_fd = -1;
524 execbuf.flags |= I915_EXEC_FENCE_OUT;
525 }
526
527 int ret = drm_ioctl(batch->screen->fd, cmd, &execbuf);
528 if (ret != 0)
529 ret = -errno;
530
531 for (int i = 0; i < batch->exec_count; i++) {
532 struct iris_bo *bo = batch->exec_bos[i];
533
534 bo->idle = false;
535 bo->index = -1;
536
537 /* Update iris_bo::gtt_offset */
538 if (batch->validation_list[i].offset != bo->gtt_offset) {
539 DBG("BO %d migrated: 0x%" PRIx64 " -> 0x%llx\n",
540 bo->gem_handle, bo->gtt_offset,
541 batch->validation_list[i].offset);
542 bo->gtt_offset = batch->validation_list[i].offset;
543 }
544 }
545
546 if (ret == 0 && out_fence_fd != NULL)
547 *out_fence_fd = execbuf.rsvd2 >> 32;
548
549 return ret;
550 }
551
552 /**
553 * The in_fence_fd is ignored if -1. Otherwise this function takes ownership
554 * of the fd.
555 *
556 * The out_fence_fd is ignored if NULL. Otherwise, the caller takes ownership
557 * of the returned fd.
558 */
559 int
560 _iris_batch_flush_fence(struct iris_batch *batch,
561 int in_fence_fd, int *out_fence_fd,
562 const char *file, int line)
563 {
564 if (buffer_bytes_used(&batch->cmdbuf) == 0)
565 return 0;
566
567 /* Check that we didn't just wrap our batchbuffer at a bad time. */
568 assert(!batch->no_wrap);
569
570 iris_finish_batch(batch);
571
572 if (unlikely(INTEL_DEBUG & (DEBUG_BATCH | DEBUG_SUBMIT))) {
573 int bytes_for_commands = buffer_bytes_used(&batch->cmdbuf);
574 int bytes_for_state = buffer_bytes_used(&batch->statebuf);
575 fprintf(stderr, "%19s:%-3d: Batchbuffer flush with %5db (%0.1f%%) (pkt),"
576 " %5db (%0.1f%%) (state), %4d BOs (%0.1fMb aperture),"
577 " %4d batch relocs, %4d state relocs\n", file, line,
578 bytes_for_commands, 100.0f * bytes_for_commands / BATCH_SZ,
579 bytes_for_state, 100.0f * bytes_for_state / STATE_SZ,
580 batch->exec_count,
581 (float) batch->aperture_space / (1024 * 1024),
582 batch->cmdbuf.relocs.reloc_count,
583 batch->statebuf.relocs.reloc_count);
584 }
585
586 int ret = submit_batch(batch, in_fence_fd, out_fence_fd);
587 if (ret < 0)
588 return ret;
589
590 //throttle(brw);
591
592 //if (unlikely(INTEL_DEBUG & DEBUG_BATCH))
593 //do_batch_dump(brw);
594
595 //if (brw->ctx.Const.ResetStrategy == GL_LOSE_CONTEXT_ON_RESET_ARB)
596 //iris_check_for_reset(ice);
597
598 if (unlikely(INTEL_DEBUG & DEBUG_SYNC)) {
599 dbg_printf("waiting for idle\n");
600 iris_bo_wait_rendering(batch->cmdbuf.bo);
601 }
602
603 /* Start a new batch buffer. */
604 iris_new_batch(batch);
605
606 return 0;
607 }
608
609 bool
610 iris_batch_references(struct iris_batch *batch, struct iris_bo *bo)
611 {
612 unsigned index = READ_ONCE(bo->index);
613 if (index < batch->exec_count && batch->exec_bos[index] == bo)
614 return true;
615
616 for (int i = 0; i < batch->exec_count; i++) {
617 if (batch->exec_bos[i] == bo)
618 return true;
619 }
620 return false;
621 }
622
623 /* This is the only way buffers get added to the validate list.
624 */
625 static uint64_t
626 emit_reloc(struct iris_batch *batch,
627 struct iris_reloc_list *rlist, uint32_t offset,
628 struct iris_bo *target, uint32_t target_offset,
629 unsigned int reloc_flags)
630 {
631 assert(target != NULL);
632
633 if (rlist->reloc_count == rlist->reloc_array_size) {
634 rlist->reloc_array_size *= 2;
635 rlist->relocs = realloc(rlist->relocs,
636 rlist->reloc_array_size *
637 sizeof(struct drm_i915_gem_relocation_entry));
638 }
639
640 unsigned int index = add_exec_bo(batch, target);
641 struct drm_i915_gem_exec_object2 *entry = &batch->validation_list[index];
642
643 rlist->relocs[rlist->reloc_count++] =
644 (struct drm_i915_gem_relocation_entry) {
645 .offset = offset,
646 .delta = target_offset,
647 .target_handle = index,
648 .presumed_offset = entry->offset,
649 };
650
651 /* Using the old buffer offset, write in what the right data would be, in
652 * case the buffer doesn't move and we can short-circuit the relocation
653 * processing in the kernel
654 */
655 return entry->offset + target_offset;
656 }
657
658 uint64_t
659 iris_batch_reloc(struct iris_batch *batch, uint32_t batch_offset,
660 struct iris_bo *target, uint32_t target_offset,
661 unsigned int reloc_flags)
662 {
663 assert(batch_offset <= batch->cmdbuf.bo->size - sizeof(uint32_t));
664
665 return emit_reloc(batch, &batch->cmdbuf.relocs, batch_offset,
666 target, target_offset, reloc_flags);
667 }
668
669 uint64_t
670 iris_state_reloc(struct iris_batch *batch, uint32_t state_offset,
671 struct iris_bo *target, uint32_t target_offset,
672 unsigned int reloc_flags)
673 {
674 assert(state_offset <= batch->statebuf.bo->size - sizeof(uint32_t));
675
676 return emit_reloc(batch, &batch->statebuf.relocs, state_offset,
677 target, target_offset, reloc_flags);
678 }
679
680
681 static uint32_t
682 iris_state_entry_size(struct iris_batch *batch, uint32_t offset)
683 {
684 struct hash_entry *entry =
685 _mesa_hash_table_search(batch->state_sizes, (void *)(uintptr_t) offset);
686 return entry ? (uintptr_t) entry->data : 0;
687 }
688
689 /**
690 * Allocates a block of space in the batchbuffer for indirect state.
691 */
692 void *
693 iris_alloc_state(struct iris_batch *batch,
694 int size, int alignment,
695 uint32_t *out_offset)
696 {
697 assert(size < batch->cmdbuf.bo->size);
698
699 const unsigned existing_bytes = buffer_bytes_used(&batch->statebuf);
700 unsigned aligned_size =
701 ALIGN(existing_bytes, alignment) - existing_bytes + size;
702
703 require_buffer_space(batch, &batch->statebuf, aligned_size,
704 STATE_SZ, MAX_STATE_SIZE);
705
706 unsigned offset = ALIGN(buffer_bytes_used(&batch->statebuf), alignment);
707
708 if (unlikely(batch->state_sizes)) {
709 _mesa_hash_table_insert(batch->state_sizes,
710 (void *) (uintptr_t) offset,
711 (void *) (uintptr_t) size);
712 }
713
714 batch->statebuf.map_next += aligned_size;
715
716 *out_offset = offset;
717 return batch->statebuf.map_next;
718 }
719
720 uint32_t
721 iris_emit_state(struct iris_batch *batch,
722 const void *data,
723 int size, int alignment)
724 {
725 uint32_t out_offset;
726 void *dest = iris_alloc_state(batch, size, alignment, &out_offset);
727 memcpy(dest, data, size);
728 return out_offset;
729 }