anv: Add a device parameter to anv_execbuf_add_bo
[mesa.git] / src / intel / vulkan / anv_batch_chain.c
1 /*
2 * Copyright © 2015 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 "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "genxml/gen8_pack.h"
33
34 #include "util/debug.h"
35
36 /** \file anv_batch_chain.c
37 *
38 * This file contains functions related to anv_cmd_buffer as a data
39 * structure. This involves everything required to create and destroy
40 * the actual batch buffers as well as link them together and handle
41 * relocations and surface state. It specifically does *not* contain any
42 * handling of actual vkCmd calls beyond vkCmdExecuteCommands.
43 */
44
45 /*-----------------------------------------------------------------------*
46 * Functions related to anv_reloc_list
47 *-----------------------------------------------------------------------*/
48
49 VkResult
50 anv_reloc_list_init(struct anv_reloc_list *list,
51 const VkAllocationCallbacks *alloc)
52 {
53 memset(list, 0, sizeof(*list));
54 return VK_SUCCESS;
55 }
56
57 static VkResult
58 anv_reloc_list_init_clone(struct anv_reloc_list *list,
59 const VkAllocationCallbacks *alloc,
60 const struct anv_reloc_list *other_list)
61 {
62 list->num_relocs = other_list->num_relocs;
63 list->array_length = other_list->array_length;
64
65 if (list->num_relocs > 0) {
66 list->relocs =
67 vk_alloc(alloc, list->array_length * sizeof(*list->relocs), 8,
68 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
69 if (list->relocs == NULL)
70 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
71
72 list->reloc_bos =
73 vk_alloc(alloc, list->array_length * sizeof(*list->reloc_bos), 8,
74 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
75 if (list->reloc_bos == NULL) {
76 vk_free(alloc, list->relocs);
77 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
78 }
79
80 memcpy(list->relocs, other_list->relocs,
81 list->array_length * sizeof(*list->relocs));
82 memcpy(list->reloc_bos, other_list->reloc_bos,
83 list->array_length * sizeof(*list->reloc_bos));
84 } else {
85 list->relocs = NULL;
86 list->reloc_bos = NULL;
87 }
88
89 if (other_list->deps) {
90 list->deps = _mesa_set_clone(other_list->deps, NULL);
91 if (!list->deps) {
92 vk_free(alloc, list->relocs);
93 vk_free(alloc, list->reloc_bos);
94 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
95 }
96 } else {
97 list->deps = NULL;
98 }
99
100 return VK_SUCCESS;
101 }
102
103 void
104 anv_reloc_list_finish(struct anv_reloc_list *list,
105 const VkAllocationCallbacks *alloc)
106 {
107 vk_free(alloc, list->relocs);
108 vk_free(alloc, list->reloc_bos);
109 if (list->deps != NULL)
110 _mesa_set_destroy(list->deps, NULL);
111 }
112
113 static VkResult
114 anv_reloc_list_grow(struct anv_reloc_list *list,
115 const VkAllocationCallbacks *alloc,
116 size_t num_additional_relocs)
117 {
118 if (list->num_relocs + num_additional_relocs <= list->array_length)
119 return VK_SUCCESS;
120
121 size_t new_length = MAX2(16, list->array_length * 2);
122 while (new_length < list->num_relocs + num_additional_relocs)
123 new_length *= 2;
124
125 struct drm_i915_gem_relocation_entry *new_relocs =
126 vk_realloc(alloc, list->relocs,
127 new_length * sizeof(*list->relocs), 8,
128 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
129 if (new_relocs == NULL)
130 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
131 list->relocs = new_relocs;
132
133 struct anv_bo **new_reloc_bos =
134 vk_realloc(alloc, list->reloc_bos,
135 new_length * sizeof(*list->reloc_bos), 8,
136 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
137 if (new_reloc_bos == NULL)
138 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
139 list->reloc_bos = new_reloc_bos;
140
141 list->array_length = new_length;
142
143 return VK_SUCCESS;
144 }
145
146 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
147
148 VkResult
149 anv_reloc_list_add(struct anv_reloc_list *list,
150 const VkAllocationCallbacks *alloc,
151 uint32_t offset, struct anv_bo *target_bo, uint32_t delta,
152 uint64_t *address_u64_out)
153 {
154 struct drm_i915_gem_relocation_entry *entry;
155 int index;
156
157 struct anv_bo *unwrapped_target_bo = anv_bo_unwrap(target_bo);
158 uint64_t target_bo_offset = READ_ONCE(unwrapped_target_bo->offset);
159 if (address_u64_out)
160 *address_u64_out = target_bo_offset + delta;
161
162 if (unwrapped_target_bo->flags & EXEC_OBJECT_PINNED) {
163 if (list->deps == NULL) {
164 list->deps = _mesa_pointer_set_create(NULL);
165 if (unlikely(list->deps == NULL))
166 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
167 }
168 _mesa_set_add(list->deps, target_bo);
169 return VK_SUCCESS;
170 }
171
172 VkResult result = anv_reloc_list_grow(list, alloc, 1);
173 if (result != VK_SUCCESS)
174 return result;
175
176 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
177 index = list->num_relocs++;
178 list->reloc_bos[index] = target_bo;
179 entry = &list->relocs[index];
180 entry->target_handle = -1; /* See also anv_cmd_buffer_process_relocs() */
181 entry->delta = delta;
182 entry->offset = offset;
183 entry->presumed_offset = target_bo_offset;
184 entry->read_domains = 0;
185 entry->write_domain = 0;
186 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry, sizeof(*entry)));
187
188 return VK_SUCCESS;
189 }
190
191 static VkResult
192 anv_reloc_list_append(struct anv_reloc_list *list,
193 const VkAllocationCallbacks *alloc,
194 struct anv_reloc_list *other, uint32_t offset)
195 {
196 VkResult result = anv_reloc_list_grow(list, alloc, other->num_relocs);
197 if (result != VK_SUCCESS)
198 return result;
199
200 if (other->num_relocs > 0) {
201 memcpy(&list->relocs[list->num_relocs], &other->relocs[0],
202 other->num_relocs * sizeof(other->relocs[0]));
203 memcpy(&list->reloc_bos[list->num_relocs], &other->reloc_bos[0],
204 other->num_relocs * sizeof(other->reloc_bos[0]));
205
206 for (uint32_t i = 0; i < other->num_relocs; i++)
207 list->relocs[i + list->num_relocs].offset += offset;
208
209 list->num_relocs += other->num_relocs;
210 }
211
212 if (other->deps) {
213 if (list->deps == NULL) {
214 list->deps = _mesa_pointer_set_create(NULL);
215 if (unlikely(list->deps == NULL))
216 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
217 }
218 set_foreach(other->deps, entry)
219 _mesa_set_add_pre_hashed(list->deps, entry->hash, entry->key);
220 }
221
222 return VK_SUCCESS;
223 }
224
225 /*-----------------------------------------------------------------------*
226 * Functions related to anv_batch
227 *-----------------------------------------------------------------------*/
228
229 void *
230 anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords)
231 {
232 if (batch->next + num_dwords * 4 > batch->end) {
233 VkResult result = batch->extend_cb(batch, batch->user_data);
234 if (result != VK_SUCCESS) {
235 anv_batch_set_error(batch, result);
236 return NULL;
237 }
238 }
239
240 void *p = batch->next;
241
242 batch->next += num_dwords * 4;
243 assert(batch->next <= batch->end);
244
245 return p;
246 }
247
248 uint64_t
249 anv_batch_emit_reloc(struct anv_batch *batch,
250 void *location, struct anv_bo *bo, uint32_t delta)
251 {
252 uint64_t address_u64 = 0;
253 VkResult result = anv_reloc_list_add(batch->relocs, batch->alloc,
254 location - batch->start, bo, delta,
255 &address_u64);
256 if (result != VK_SUCCESS) {
257 anv_batch_set_error(batch, result);
258 return 0;
259 }
260
261 return address_u64;
262 }
263
264 void
265 anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
266 {
267 uint32_t size, offset;
268
269 size = other->next - other->start;
270 assert(size % 4 == 0);
271
272 if (batch->next + size > batch->end) {
273 VkResult result = batch->extend_cb(batch, batch->user_data);
274 if (result != VK_SUCCESS) {
275 anv_batch_set_error(batch, result);
276 return;
277 }
278 }
279
280 assert(batch->next + size <= batch->end);
281
282 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other->start, size));
283 memcpy(batch->next, other->start, size);
284
285 offset = batch->next - batch->start;
286 VkResult result = anv_reloc_list_append(batch->relocs, batch->alloc,
287 other->relocs, offset);
288 if (result != VK_SUCCESS) {
289 anv_batch_set_error(batch, result);
290 return;
291 }
292
293 batch->next += size;
294 }
295
296 /*-----------------------------------------------------------------------*
297 * Functions related to anv_batch_bo
298 *-----------------------------------------------------------------------*/
299
300 static VkResult
301 anv_batch_bo_create(struct anv_cmd_buffer *cmd_buffer,
302 struct anv_batch_bo **bbo_out)
303 {
304 VkResult result;
305
306 struct anv_batch_bo *bbo = vk_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
307 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
308 if (bbo == NULL)
309 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
310
311 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool,
312 ANV_CMD_BUFFER_BATCH_SIZE, &bbo->bo);
313 if (result != VK_SUCCESS)
314 goto fail_alloc;
315
316 result = anv_reloc_list_init(&bbo->relocs, &cmd_buffer->pool->alloc);
317 if (result != VK_SUCCESS)
318 goto fail_bo_alloc;
319
320 *bbo_out = bbo;
321
322 return VK_SUCCESS;
323
324 fail_bo_alloc:
325 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, bbo->bo);
326 fail_alloc:
327 vk_free(&cmd_buffer->pool->alloc, bbo);
328
329 return result;
330 }
331
332 static VkResult
333 anv_batch_bo_clone(struct anv_cmd_buffer *cmd_buffer,
334 const struct anv_batch_bo *other_bbo,
335 struct anv_batch_bo **bbo_out)
336 {
337 VkResult result;
338
339 struct anv_batch_bo *bbo = vk_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
340 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
341 if (bbo == NULL)
342 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
343
344 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool,
345 other_bbo->bo->size, &bbo->bo);
346 if (result != VK_SUCCESS)
347 goto fail_alloc;
348
349 result = anv_reloc_list_init_clone(&bbo->relocs, &cmd_buffer->pool->alloc,
350 &other_bbo->relocs);
351 if (result != VK_SUCCESS)
352 goto fail_bo_alloc;
353
354 bbo->length = other_bbo->length;
355 memcpy(bbo->bo->map, other_bbo->bo->map, other_bbo->length);
356 *bbo_out = bbo;
357
358 return VK_SUCCESS;
359
360 fail_bo_alloc:
361 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, bbo->bo);
362 fail_alloc:
363 vk_free(&cmd_buffer->pool->alloc, bbo);
364
365 return result;
366 }
367
368 static void
369 anv_batch_bo_start(struct anv_batch_bo *bbo, struct anv_batch *batch,
370 size_t batch_padding)
371 {
372 batch->next = batch->start = bbo->bo->map;
373 batch->end = bbo->bo->map + bbo->bo->size - batch_padding;
374 batch->relocs = &bbo->relocs;
375 bbo->relocs.num_relocs = 0;
376 _mesa_set_clear(bbo->relocs.deps, NULL);
377 }
378
379 static void
380 anv_batch_bo_continue(struct anv_batch_bo *bbo, struct anv_batch *batch,
381 size_t batch_padding)
382 {
383 batch->start = bbo->bo->map;
384 batch->next = bbo->bo->map + bbo->length;
385 batch->end = bbo->bo->map + bbo->bo->size - batch_padding;
386 batch->relocs = &bbo->relocs;
387 }
388
389 static void
390 anv_batch_bo_finish(struct anv_batch_bo *bbo, struct anv_batch *batch)
391 {
392 assert(batch->start == bbo->bo->map);
393 bbo->length = batch->next - batch->start;
394 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->start, bbo->length));
395 }
396
397 static VkResult
398 anv_batch_bo_grow(struct anv_cmd_buffer *cmd_buffer, struct anv_batch_bo *bbo,
399 struct anv_batch *batch, size_t aditional,
400 size_t batch_padding)
401 {
402 assert(batch->start == bbo->bo->map);
403 bbo->length = batch->next - batch->start;
404
405 size_t new_size = bbo->bo->size;
406 while (new_size <= bbo->length + aditional + batch_padding)
407 new_size *= 2;
408
409 if (new_size == bbo->bo->size)
410 return VK_SUCCESS;
411
412 struct anv_bo *new_bo;
413 VkResult result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool,
414 new_size, &new_bo);
415 if (result != VK_SUCCESS)
416 return result;
417
418 memcpy(new_bo->map, bbo->bo->map, bbo->length);
419
420 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, bbo->bo);
421
422 bbo->bo = new_bo;
423 anv_batch_bo_continue(bbo, batch, batch_padding);
424
425 return VK_SUCCESS;
426 }
427
428 static void
429 anv_batch_bo_link(struct anv_cmd_buffer *cmd_buffer,
430 struct anv_batch_bo *prev_bbo,
431 struct anv_batch_bo *next_bbo,
432 uint32_t next_bbo_offset)
433 {
434 const uint32_t bb_start_offset =
435 prev_bbo->length - GEN8_MI_BATCH_BUFFER_START_length * 4;
436 ASSERTED const uint32_t *bb_start = prev_bbo->bo->map + bb_start_offset;
437
438 /* Make sure we're looking at a MI_BATCH_BUFFER_START */
439 assert(((*bb_start >> 29) & 0x07) == 0);
440 assert(((*bb_start >> 23) & 0x3f) == 49);
441
442 if (cmd_buffer->device->instance->physicalDevice.use_softpin) {
443 assert(prev_bbo->bo->flags & EXEC_OBJECT_PINNED);
444 assert(next_bbo->bo->flags & EXEC_OBJECT_PINNED);
445
446 write_reloc(cmd_buffer->device,
447 prev_bbo->bo->map + bb_start_offset + 4,
448 next_bbo->bo->offset + next_bbo_offset, true);
449 } else {
450 uint32_t reloc_idx = prev_bbo->relocs.num_relocs - 1;
451 assert(prev_bbo->relocs.relocs[reloc_idx].offset == bb_start_offset + 4);
452
453 prev_bbo->relocs.reloc_bos[reloc_idx] = next_bbo->bo;
454 prev_bbo->relocs.relocs[reloc_idx].delta = next_bbo_offset;
455
456 /* Use a bogus presumed offset to force a relocation */
457 prev_bbo->relocs.relocs[reloc_idx].presumed_offset = -1;
458 }
459 }
460
461 static void
462 anv_batch_bo_destroy(struct anv_batch_bo *bbo,
463 struct anv_cmd_buffer *cmd_buffer)
464 {
465 anv_reloc_list_finish(&bbo->relocs, &cmd_buffer->pool->alloc);
466 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, bbo->bo);
467 vk_free(&cmd_buffer->pool->alloc, bbo);
468 }
469
470 static VkResult
471 anv_batch_bo_list_clone(const struct list_head *list,
472 struct anv_cmd_buffer *cmd_buffer,
473 struct list_head *new_list)
474 {
475 VkResult result = VK_SUCCESS;
476
477 list_inithead(new_list);
478
479 struct anv_batch_bo *prev_bbo = NULL;
480 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
481 struct anv_batch_bo *new_bbo = NULL;
482 result = anv_batch_bo_clone(cmd_buffer, bbo, &new_bbo);
483 if (result != VK_SUCCESS)
484 break;
485 list_addtail(&new_bbo->link, new_list);
486
487 if (prev_bbo)
488 anv_batch_bo_link(cmd_buffer, prev_bbo, new_bbo, 0);
489
490 prev_bbo = new_bbo;
491 }
492
493 if (result != VK_SUCCESS) {
494 list_for_each_entry_safe(struct anv_batch_bo, bbo, new_list, link)
495 anv_batch_bo_destroy(bbo, cmd_buffer);
496 }
497
498 return result;
499 }
500
501 /*-----------------------------------------------------------------------*
502 * Functions related to anv_batch_bo
503 *-----------------------------------------------------------------------*/
504
505 static struct anv_batch_bo *
506 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer *cmd_buffer)
507 {
508 return LIST_ENTRY(struct anv_batch_bo, cmd_buffer->batch_bos.prev, link);
509 }
510
511 struct anv_address
512 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer)
513 {
514 struct anv_state *bt_block = u_vector_head(&cmd_buffer->bt_block_states);
515 return (struct anv_address) {
516 .bo = anv_binding_table_pool(cmd_buffer->device)->block_pool.bo,
517 .offset = bt_block->offset,
518 };
519 }
520
521 static void
522 emit_batch_buffer_start(struct anv_cmd_buffer *cmd_buffer,
523 struct anv_bo *bo, uint32_t offset)
524 {
525 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
526 * offsets. The high 16 bits are in the last dword, so we can use the gen8
527 * version in either case, as long as we set the instruction length in the
528 * header accordingly. This means that we always emit three dwords here
529 * and all the padding and adjustment we do in this file works for all
530 * gens.
531 */
532
533 #define GEN7_MI_BATCH_BUFFER_START_length 2
534 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
535
536 const uint32_t gen7_length =
537 GEN7_MI_BATCH_BUFFER_START_length - GEN7_MI_BATCH_BUFFER_START_length_bias;
538 const uint32_t gen8_length =
539 GEN8_MI_BATCH_BUFFER_START_length - GEN8_MI_BATCH_BUFFER_START_length_bias;
540
541 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_START, bbs) {
542 bbs.DWordLength = cmd_buffer->device->info.gen < 8 ?
543 gen7_length : gen8_length;
544 bbs.SecondLevelBatchBuffer = Firstlevelbatch;
545 bbs.AddressSpaceIndicator = ASI_PPGTT;
546 bbs.BatchBufferStartAddress = (struct anv_address) { bo, offset };
547 }
548 }
549
550 static void
551 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer *cmd_buffer,
552 struct anv_batch_bo *bbo)
553 {
554 struct anv_batch *batch = &cmd_buffer->batch;
555 struct anv_batch_bo *current_bbo =
556 anv_cmd_buffer_current_batch_bo(cmd_buffer);
557
558 /* We set the end of the batch a little short so we would be sure we
559 * have room for the chaining command. Since we're about to emit the
560 * chaining command, let's set it back where it should go.
561 */
562 batch->end += GEN8_MI_BATCH_BUFFER_START_length * 4;
563 assert(batch->end == current_bbo->bo->map + current_bbo->bo->size);
564
565 emit_batch_buffer_start(cmd_buffer, bbo->bo, 0);
566
567 anv_batch_bo_finish(current_bbo, batch);
568 }
569
570 static VkResult
571 anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
572 {
573 struct anv_cmd_buffer *cmd_buffer = _data;
574 struct anv_batch_bo *new_bbo;
575
576 VkResult result = anv_batch_bo_create(cmd_buffer, &new_bbo);
577 if (result != VK_SUCCESS)
578 return result;
579
580 struct anv_batch_bo **seen_bbo = u_vector_add(&cmd_buffer->seen_bbos);
581 if (seen_bbo == NULL) {
582 anv_batch_bo_destroy(new_bbo, cmd_buffer);
583 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
584 }
585 *seen_bbo = new_bbo;
586
587 cmd_buffer_chain_to_batch_bo(cmd_buffer, new_bbo);
588
589 list_addtail(&new_bbo->link, &cmd_buffer->batch_bos);
590
591 anv_batch_bo_start(new_bbo, batch, GEN8_MI_BATCH_BUFFER_START_length * 4);
592
593 return VK_SUCCESS;
594 }
595
596 static VkResult
597 anv_cmd_buffer_grow_batch(struct anv_batch *batch, void *_data)
598 {
599 struct anv_cmd_buffer *cmd_buffer = _data;
600 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
601
602 anv_batch_bo_grow(cmd_buffer, bbo, &cmd_buffer->batch, 4096,
603 GEN8_MI_BATCH_BUFFER_START_length * 4);
604
605 return VK_SUCCESS;
606 }
607
608 /** Allocate a binding table
609 *
610 * This function allocates a binding table. This is a bit more complicated
611 * than one would think due to a combination of Vulkan driver design and some
612 * unfortunate hardware restrictions.
613 *
614 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
615 * the binding table pointer which means that all binding tables need to live
616 * in the bottom 64k of surface state base address. The way the GL driver has
617 * classically dealt with this restriction is to emit all surface states
618 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
619 * isn't really an option in Vulkan for a couple of reasons:
620 *
621 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
622 * to live in their own buffer and we have to be able to re-emit
623 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
624 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
625 * (it's not that hard to hit 64k of just binding tables), we allocate
626 * surface state objects up-front when VkImageView is created. In order
627 * for this to work, surface state objects need to be allocated from a
628 * global buffer.
629 *
630 * 2) We tried to design the surface state system in such a way that it's
631 * already ready for bindless texturing. The way bindless texturing works
632 * on our hardware is that you have a big pool of surface state objects
633 * (with its own state base address) and the bindless handles are simply
634 * offsets into that pool. With the architecture we chose, we already
635 * have that pool and it's exactly the same pool that we use for regular
636 * surface states so we should already be ready for bindless.
637 *
638 * 3) For render targets, we need to be able to fill out the surface states
639 * later in vkBeginRenderPass so that we can assign clear colors
640 * correctly. One way to do this would be to just create the surface
641 * state data and then repeatedly copy it into the surface state BO every
642 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
643 * rather annoying and just being able to allocate them up-front and
644 * re-use them for the entire render pass.
645 *
646 * While none of these are technically blockers for emitting state on the fly
647 * like we do in GL, the ability to have a single surface state pool is
648 * simplifies things greatly. Unfortunately, it comes at a cost...
649 *
650 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
651 * place the binding tables just anywhere in surface state base address.
652 * Because 64k isn't a whole lot of space, we can't simply restrict the
653 * surface state buffer to 64k, we have to be more clever. The solution we've
654 * chosen is to have a block pool with a maximum size of 2G that starts at
655 * zero and grows in both directions. All surface states are allocated from
656 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
657 * binding tables from the bottom of the pool (negative offsets). Every time
658 * we allocate a new binding table block, we set surface state base address to
659 * point to the bottom of the binding table block. This way all of the
660 * binding tables in the block are in the bottom 64k of surface state base
661 * address. When we fill out the binding table, we add the distance between
662 * the bottom of our binding table block and zero of the block pool to the
663 * surface state offsets so that they are correct relative to out new surface
664 * state base address at the bottom of the binding table block.
665 *
666 * \see adjust_relocations_from_block_pool()
667 * \see adjust_relocations_too_block_pool()
668 *
669 * \param[in] entries The number of surface state entries the binding
670 * table should be able to hold.
671 *
672 * \param[out] state_offset The offset surface surface state base address
673 * where the surface states live. This must be
674 * added to the surface state offset when it is
675 * written into the binding table entry.
676 *
677 * \return An anv_state representing the binding table
678 */
679 struct anv_state
680 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
681 uint32_t entries, uint32_t *state_offset)
682 {
683 struct anv_device *device = cmd_buffer->device;
684 struct anv_state_pool *state_pool = &device->surface_state_pool;
685 struct anv_state *bt_block = u_vector_head(&cmd_buffer->bt_block_states);
686 struct anv_state state;
687
688 state.alloc_size = align_u32(entries * 4, 32);
689
690 if (cmd_buffer->bt_next + state.alloc_size > state_pool->block_size)
691 return (struct anv_state) { 0 };
692
693 state.offset = cmd_buffer->bt_next;
694 state.map = anv_block_pool_map(&anv_binding_table_pool(device)->block_pool,
695 bt_block->offset + state.offset);
696
697 cmd_buffer->bt_next += state.alloc_size;
698
699 if (device->instance->physicalDevice.use_softpin) {
700 assert(bt_block->offset >= 0);
701 *state_offset = device->surface_state_pool.block_pool.start_address -
702 device->binding_table_pool.block_pool.start_address - bt_block->offset;
703 } else {
704 assert(bt_block->offset < 0);
705 *state_offset = -bt_block->offset;
706 }
707
708 return state;
709 }
710
711 struct anv_state
712 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer)
713 {
714 struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
715 return anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
716 isl_dev->ss.size, isl_dev->ss.align);
717 }
718
719 struct anv_state
720 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
721 uint32_t size, uint32_t alignment)
722 {
723 return anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream,
724 size, alignment);
725 }
726
727 VkResult
728 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer)
729 {
730 struct anv_state *bt_block = u_vector_add(&cmd_buffer->bt_block_states);
731 if (bt_block == NULL) {
732 anv_batch_set_error(&cmd_buffer->batch, VK_ERROR_OUT_OF_HOST_MEMORY);
733 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
734 }
735
736 *bt_block = anv_binding_table_pool_alloc(cmd_buffer->device);
737 cmd_buffer->bt_next = 0;
738
739 return VK_SUCCESS;
740 }
741
742 VkResult
743 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
744 {
745 struct anv_batch_bo *batch_bo;
746 VkResult result;
747
748 list_inithead(&cmd_buffer->batch_bos);
749
750 result = anv_batch_bo_create(cmd_buffer, &batch_bo);
751 if (result != VK_SUCCESS)
752 return result;
753
754 list_addtail(&batch_bo->link, &cmd_buffer->batch_bos);
755
756 cmd_buffer->batch.alloc = &cmd_buffer->pool->alloc;
757 cmd_buffer->batch.user_data = cmd_buffer;
758
759 if (cmd_buffer->device->can_chain_batches) {
760 cmd_buffer->batch.extend_cb = anv_cmd_buffer_chain_batch;
761 } else {
762 cmd_buffer->batch.extend_cb = anv_cmd_buffer_grow_batch;
763 }
764
765 anv_batch_bo_start(batch_bo, &cmd_buffer->batch,
766 GEN8_MI_BATCH_BUFFER_START_length * 4);
767
768 int success = u_vector_init(&cmd_buffer->seen_bbos,
769 sizeof(struct anv_bo *),
770 8 * sizeof(struct anv_bo *));
771 if (!success)
772 goto fail_batch_bo;
773
774 *(struct anv_batch_bo **)u_vector_add(&cmd_buffer->seen_bbos) = batch_bo;
775
776 /* u_vector requires power-of-two size elements */
777 unsigned pow2_state_size = util_next_power_of_two(sizeof(struct anv_state));
778 success = u_vector_init(&cmd_buffer->bt_block_states,
779 pow2_state_size, 8 * pow2_state_size);
780 if (!success)
781 goto fail_seen_bbos;
782
783 result = anv_reloc_list_init(&cmd_buffer->surface_relocs,
784 &cmd_buffer->pool->alloc);
785 if (result != VK_SUCCESS)
786 goto fail_bt_blocks;
787 cmd_buffer->last_ss_pool_center = 0;
788
789 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
790 if (result != VK_SUCCESS)
791 goto fail_bt_blocks;
792
793 return VK_SUCCESS;
794
795 fail_bt_blocks:
796 u_vector_finish(&cmd_buffer->bt_block_states);
797 fail_seen_bbos:
798 u_vector_finish(&cmd_buffer->seen_bbos);
799 fail_batch_bo:
800 anv_batch_bo_destroy(batch_bo, cmd_buffer);
801
802 return result;
803 }
804
805 void
806 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
807 {
808 struct anv_state *bt_block;
809 u_vector_foreach(bt_block, &cmd_buffer->bt_block_states)
810 anv_binding_table_pool_free(cmd_buffer->device, *bt_block);
811 u_vector_finish(&cmd_buffer->bt_block_states);
812
813 anv_reloc_list_finish(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc);
814
815 u_vector_finish(&cmd_buffer->seen_bbos);
816
817 /* Destroy all of the batch buffers */
818 list_for_each_entry_safe(struct anv_batch_bo, bbo,
819 &cmd_buffer->batch_bos, link) {
820 anv_batch_bo_destroy(bbo, cmd_buffer);
821 }
822 }
823
824 void
825 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
826 {
827 /* Delete all but the first batch bo */
828 assert(!list_is_empty(&cmd_buffer->batch_bos));
829 while (cmd_buffer->batch_bos.next != cmd_buffer->batch_bos.prev) {
830 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
831 list_del(&bbo->link);
832 anv_batch_bo_destroy(bbo, cmd_buffer);
833 }
834 assert(!list_is_empty(&cmd_buffer->batch_bos));
835
836 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer),
837 &cmd_buffer->batch,
838 GEN8_MI_BATCH_BUFFER_START_length * 4);
839
840 while (u_vector_length(&cmd_buffer->bt_block_states) > 1) {
841 struct anv_state *bt_block = u_vector_remove(&cmd_buffer->bt_block_states);
842 anv_binding_table_pool_free(cmd_buffer->device, *bt_block);
843 }
844 assert(u_vector_length(&cmd_buffer->bt_block_states) == 1);
845 cmd_buffer->bt_next = 0;
846
847 cmd_buffer->surface_relocs.num_relocs = 0;
848 _mesa_set_clear(cmd_buffer->surface_relocs.deps, NULL);
849 cmd_buffer->last_ss_pool_center = 0;
850
851 /* Reset the list of seen buffers */
852 cmd_buffer->seen_bbos.head = 0;
853 cmd_buffer->seen_bbos.tail = 0;
854
855 *(struct anv_batch_bo **)u_vector_add(&cmd_buffer->seen_bbos) =
856 anv_cmd_buffer_current_batch_bo(cmd_buffer);
857 }
858
859 void
860 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer)
861 {
862 struct anv_batch_bo *batch_bo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
863
864 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
865 /* When we start a batch buffer, we subtract a certain amount of
866 * padding from the end to ensure that we always have room to emit a
867 * BATCH_BUFFER_START to chain to the next BO. We need to remove
868 * that padding before we end the batch; otherwise, we may end up
869 * with our BATCH_BUFFER_END in another BO.
870 */
871 cmd_buffer->batch.end += GEN8_MI_BATCH_BUFFER_START_length * 4;
872 assert(cmd_buffer->batch.end == batch_bo->bo->map + batch_bo->bo->size);
873
874 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_END, bbe);
875
876 /* Round batch up to an even number of dwords. */
877 if ((cmd_buffer->batch.next - cmd_buffer->batch.start) & 4)
878 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_NOOP, noop);
879
880 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_PRIMARY;
881 } else {
882 assert(cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY);
883 /* If this is a secondary command buffer, we need to determine the
884 * mode in which it will be executed with vkExecuteCommands. We
885 * determine this statically here so that this stays in sync with the
886 * actual ExecuteCommands implementation.
887 */
888 const uint32_t length = cmd_buffer->batch.next - cmd_buffer->batch.start;
889 if (!cmd_buffer->device->can_chain_batches) {
890 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT;
891 } else if ((cmd_buffer->batch_bos.next == cmd_buffer->batch_bos.prev) &&
892 (length < ANV_CMD_BUFFER_BATCH_SIZE / 2)) {
893 /* If the secondary has exactly one batch buffer in its list *and*
894 * that batch buffer is less than half of the maximum size, we're
895 * probably better of simply copying it into our batch.
896 */
897 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_EMIT;
898 } else if (!(cmd_buffer->usage_flags &
899 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
900 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_CHAIN;
901
902 /* In order to chain, we need this command buffer to contain an
903 * MI_BATCH_BUFFER_START which will jump back to the calling batch.
904 * It doesn't matter where it points now so long as has a valid
905 * relocation. We'll adjust it later as part of the chaining
906 * process.
907 *
908 * We set the end of the batch a little short so we would be sure we
909 * have room for the chaining command. Since we're about to emit the
910 * chaining command, let's set it back where it should go.
911 */
912 cmd_buffer->batch.end += GEN8_MI_BATCH_BUFFER_START_length * 4;
913 assert(cmd_buffer->batch.start == batch_bo->bo->map);
914 assert(cmd_buffer->batch.end == batch_bo->bo->map + batch_bo->bo->size);
915
916 emit_batch_buffer_start(cmd_buffer, batch_bo->bo, 0);
917 assert(cmd_buffer->batch.start == batch_bo->bo->map);
918 } else {
919 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN;
920 }
921 }
922
923 anv_batch_bo_finish(batch_bo, &cmd_buffer->batch);
924 }
925
926 static VkResult
927 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer *cmd_buffer,
928 struct list_head *list)
929 {
930 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
931 struct anv_batch_bo **bbo_ptr = u_vector_add(&cmd_buffer->seen_bbos);
932 if (bbo_ptr == NULL)
933 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
934
935 *bbo_ptr = bbo;
936 }
937
938 return VK_SUCCESS;
939 }
940
941 void
942 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
943 struct anv_cmd_buffer *secondary)
944 {
945 switch (secondary->exec_mode) {
946 case ANV_CMD_BUFFER_EXEC_MODE_EMIT:
947 anv_batch_emit_batch(&primary->batch, &secondary->batch);
948 break;
949 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT: {
950 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(primary);
951 unsigned length = secondary->batch.end - secondary->batch.start;
952 anv_batch_bo_grow(primary, bbo, &primary->batch, length,
953 GEN8_MI_BATCH_BUFFER_START_length * 4);
954 anv_batch_emit_batch(&primary->batch, &secondary->batch);
955 break;
956 }
957 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN: {
958 struct anv_batch_bo *first_bbo =
959 list_first_entry(&secondary->batch_bos, struct anv_batch_bo, link);
960 struct anv_batch_bo *last_bbo =
961 list_last_entry(&secondary->batch_bos, struct anv_batch_bo, link);
962
963 emit_batch_buffer_start(primary, first_bbo->bo, 0);
964
965 struct anv_batch_bo *this_bbo = anv_cmd_buffer_current_batch_bo(primary);
966 assert(primary->batch.start == this_bbo->bo->map);
967 uint32_t offset = primary->batch.next - primary->batch.start;
968
969 /* Make the tail of the secondary point back to right after the
970 * MI_BATCH_BUFFER_START in the primary batch.
971 */
972 anv_batch_bo_link(primary, last_bbo, this_bbo, offset);
973
974 anv_cmd_buffer_add_seen_bbos(primary, &secondary->batch_bos);
975 break;
976 }
977 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN: {
978 struct list_head copy_list;
979 VkResult result = anv_batch_bo_list_clone(&secondary->batch_bos,
980 secondary,
981 &copy_list);
982 if (result != VK_SUCCESS)
983 return; /* FIXME */
984
985 anv_cmd_buffer_add_seen_bbos(primary, &copy_list);
986
987 struct anv_batch_bo *first_bbo =
988 list_first_entry(&copy_list, struct anv_batch_bo, link);
989 struct anv_batch_bo *last_bbo =
990 list_last_entry(&copy_list, struct anv_batch_bo, link);
991
992 cmd_buffer_chain_to_batch_bo(primary, first_bbo);
993
994 list_splicetail(&copy_list, &primary->batch_bos);
995
996 anv_batch_bo_continue(last_bbo, &primary->batch,
997 GEN8_MI_BATCH_BUFFER_START_length * 4);
998 break;
999 }
1000 default:
1001 assert(!"Invalid execution mode");
1002 }
1003
1004 anv_reloc_list_append(&primary->surface_relocs, &primary->pool->alloc,
1005 &secondary->surface_relocs, 0);
1006 }
1007
1008 struct anv_execbuf {
1009 struct drm_i915_gem_execbuffer2 execbuf;
1010
1011 struct drm_i915_gem_exec_object2 * objects;
1012 uint32_t bo_count;
1013 struct anv_bo ** bos;
1014
1015 /* Allocated length of the 'objects' and 'bos' arrays */
1016 uint32_t array_length;
1017
1018 bool has_relocs;
1019
1020 uint32_t fence_count;
1021 uint32_t fence_array_length;
1022 struct drm_i915_gem_exec_fence * fences;
1023 struct anv_syncobj ** syncobjs;
1024 };
1025
1026 static void
1027 anv_execbuf_init(struct anv_execbuf *exec)
1028 {
1029 memset(exec, 0, sizeof(*exec));
1030 }
1031
1032 static void
1033 anv_execbuf_finish(struct anv_execbuf *exec,
1034 const VkAllocationCallbacks *alloc)
1035 {
1036 vk_free(alloc, exec->objects);
1037 vk_free(alloc, exec->bos);
1038 vk_free(alloc, exec->fences);
1039 vk_free(alloc, exec->syncobjs);
1040 }
1041
1042 static int
1043 _compare_bo_handles(const void *_bo1, const void *_bo2)
1044 {
1045 struct anv_bo * const *bo1 = _bo1;
1046 struct anv_bo * const *bo2 = _bo2;
1047
1048 return (*bo1)->gem_handle - (*bo2)->gem_handle;
1049 }
1050
1051 static VkResult
1052 anv_execbuf_add_bo_set(struct anv_device *device,
1053 struct anv_execbuf *exec,
1054 struct set *deps,
1055 uint32_t extra_flags,
1056 const VkAllocationCallbacks *alloc);
1057
1058 static VkResult
1059 anv_execbuf_add_bo(struct anv_device *device,
1060 struct anv_execbuf *exec,
1061 struct anv_bo *bo,
1062 struct anv_reloc_list *relocs,
1063 uint32_t extra_flags,
1064 const VkAllocationCallbacks *alloc)
1065 {
1066 struct drm_i915_gem_exec_object2 *obj = NULL;
1067
1068 bo = anv_bo_unwrap(bo);
1069
1070 if (bo->index < exec->bo_count && exec->bos[bo->index] == bo)
1071 obj = &exec->objects[bo->index];
1072
1073 if (obj == NULL) {
1074 /* We've never seen this one before. Add it to the list and assign
1075 * an id that we can use later.
1076 */
1077 if (exec->bo_count >= exec->array_length) {
1078 uint32_t new_len = exec->objects ? exec->array_length * 2 : 64;
1079
1080 struct drm_i915_gem_exec_object2 *new_objects =
1081 vk_alloc(alloc, new_len * sizeof(*new_objects),
1082 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1083 if (new_objects == NULL)
1084 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1085
1086 struct anv_bo **new_bos =
1087 vk_alloc(alloc, new_len * sizeof(*new_bos),
1088 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1089 if (new_bos == NULL) {
1090 vk_free(alloc, new_objects);
1091 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1092 }
1093
1094 if (exec->objects) {
1095 memcpy(new_objects, exec->objects,
1096 exec->bo_count * sizeof(*new_objects));
1097 memcpy(new_bos, exec->bos,
1098 exec->bo_count * sizeof(*new_bos));
1099 }
1100
1101 vk_free(alloc, exec->objects);
1102 vk_free(alloc, exec->bos);
1103
1104 exec->objects = new_objects;
1105 exec->bos = new_bos;
1106 exec->array_length = new_len;
1107 }
1108
1109 assert(exec->bo_count < exec->array_length);
1110
1111 bo->index = exec->bo_count++;
1112 obj = &exec->objects[bo->index];
1113 exec->bos[bo->index] = bo;
1114
1115 obj->handle = bo->gem_handle;
1116 obj->relocation_count = 0;
1117 obj->relocs_ptr = 0;
1118 obj->alignment = 0;
1119 obj->offset = bo->offset;
1120 obj->flags = bo->flags | extra_flags;
1121 obj->rsvd1 = 0;
1122 obj->rsvd2 = 0;
1123 }
1124
1125 if (relocs != NULL) {
1126 assert(obj->relocation_count == 0);
1127
1128 if (relocs->num_relocs > 0) {
1129 /* This is the first time we've ever seen a list of relocations for
1130 * this BO. Go ahead and set the relocations and then walk the list
1131 * of relocations and add them all.
1132 */
1133 exec->has_relocs = true;
1134 obj->relocation_count = relocs->num_relocs;
1135 obj->relocs_ptr = (uintptr_t) relocs->relocs;
1136
1137 for (size_t i = 0; i < relocs->num_relocs; i++) {
1138 VkResult result;
1139
1140 /* A quick sanity check on relocations */
1141 assert(relocs->relocs[i].offset < bo->size);
1142 result = anv_execbuf_add_bo(device, exec, relocs->reloc_bos[i],
1143 NULL, extra_flags, alloc);
1144
1145 if (result != VK_SUCCESS)
1146 return result;
1147 }
1148 }
1149
1150 return anv_execbuf_add_bo_set(device, exec, relocs->deps,
1151 extra_flags, alloc);
1152 }
1153
1154 return VK_SUCCESS;
1155 }
1156
1157 /* Add BO dependencies to execbuf */
1158 static VkResult
1159 anv_execbuf_add_bo_set(struct anv_device *device,
1160 struct anv_execbuf *exec,
1161 struct set *deps,
1162 uint32_t extra_flags,
1163 const VkAllocationCallbacks *alloc)
1164 {
1165 if (!deps || deps->entries <= 0)
1166 return VK_SUCCESS;
1167
1168 const uint32_t entries = deps->entries;
1169 struct anv_bo **bos =
1170 vk_alloc(alloc, entries * sizeof(*bos),
1171 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1172 if (bos == NULL)
1173 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1174
1175 struct anv_bo **bo = bos;
1176 set_foreach(deps, entry) {
1177 *bo++ = (void *)entry->key;
1178 }
1179
1180 qsort(bos, entries, sizeof(struct anv_bo*), _compare_bo_handles);
1181
1182 VkResult result = VK_SUCCESS;
1183 for (bo = bos; bo < bos + entries; bo++) {
1184 result = anv_execbuf_add_bo(device, exec, *bo, NULL, extra_flags, alloc);
1185 if (result != VK_SUCCESS)
1186 break;
1187 }
1188
1189 vk_free(alloc, bos);
1190
1191 return result;
1192 }
1193
1194 static VkResult
1195 anv_execbuf_add_syncobj(struct anv_execbuf *exec,
1196 uint32_t handle, uint32_t flags,
1197 const VkAllocationCallbacks *alloc)
1198 {
1199 assert(flags != 0);
1200
1201 if (exec->fence_count >= exec->fence_array_length) {
1202 uint32_t new_len = MAX2(exec->fence_array_length * 2, 64);
1203
1204 exec->fences = vk_realloc(alloc, exec->fences,
1205 new_len * sizeof(*exec->fences),
1206 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1207 if (exec->fences == NULL)
1208 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1209
1210 exec->fence_array_length = new_len;
1211 }
1212
1213 exec->fences[exec->fence_count] = (struct drm_i915_gem_exec_fence) {
1214 .handle = handle,
1215 .flags = flags,
1216 };
1217
1218 exec->fence_count++;
1219
1220 return VK_SUCCESS;
1221 }
1222
1223 static void
1224 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
1225 struct anv_reloc_list *list)
1226 {
1227 for (size_t i = 0; i < list->num_relocs; i++)
1228 list->relocs[i].target_handle = anv_bo_unwrap(list->reloc_bos[i])->index;
1229 }
1230
1231 static void
1232 adjust_relocations_from_state_pool(struct anv_state_pool *pool,
1233 struct anv_reloc_list *relocs,
1234 uint32_t last_pool_center_bo_offset)
1235 {
1236 assert(last_pool_center_bo_offset <= pool->block_pool.center_bo_offset);
1237 uint32_t delta = pool->block_pool.center_bo_offset - last_pool_center_bo_offset;
1238
1239 for (size_t i = 0; i < relocs->num_relocs; i++) {
1240 /* All of the relocations from this block pool to other BO's should
1241 * have been emitted relative to the surface block pool center. We
1242 * need to add the center offset to make them relative to the
1243 * beginning of the actual GEM bo.
1244 */
1245 relocs->relocs[i].offset += delta;
1246 }
1247 }
1248
1249 static void
1250 adjust_relocations_to_state_pool(struct anv_state_pool *pool,
1251 struct anv_bo *from_bo,
1252 struct anv_reloc_list *relocs,
1253 uint32_t last_pool_center_bo_offset)
1254 {
1255 assert(!from_bo->is_wrapper);
1256 assert(last_pool_center_bo_offset <= pool->block_pool.center_bo_offset);
1257 uint32_t delta = pool->block_pool.center_bo_offset - last_pool_center_bo_offset;
1258
1259 /* When we initially emit relocations into a block pool, we don't
1260 * actually know what the final center_bo_offset will be so we just emit
1261 * it as if center_bo_offset == 0. Now that we know what the center
1262 * offset is, we need to walk the list of relocations and adjust any
1263 * relocations that point to the pool bo with the correct offset.
1264 */
1265 for (size_t i = 0; i < relocs->num_relocs; i++) {
1266 if (relocs->reloc_bos[i] == pool->block_pool.bo) {
1267 /* Adjust the delta value in the relocation to correctly
1268 * correspond to the new delta. Initially, this value may have
1269 * been negative (if treated as unsigned), but we trust in
1270 * uint32_t roll-over to fix that for us at this point.
1271 */
1272 relocs->relocs[i].delta += delta;
1273
1274 /* Since the delta has changed, we need to update the actual
1275 * relocated value with the new presumed value. This function
1276 * should only be called on batch buffers, so we know it isn't in
1277 * use by the GPU at the moment.
1278 */
1279 assert(relocs->relocs[i].offset < from_bo->size);
1280 write_reloc(pool->block_pool.device,
1281 from_bo->map + relocs->relocs[i].offset,
1282 relocs->relocs[i].presumed_offset +
1283 relocs->relocs[i].delta, false);
1284 }
1285 }
1286 }
1287
1288 static void
1289 anv_reloc_list_apply(struct anv_device *device,
1290 struct anv_reloc_list *list,
1291 struct anv_bo *bo,
1292 bool always_relocate)
1293 {
1294 bo = anv_bo_unwrap(bo);
1295
1296 for (size_t i = 0; i < list->num_relocs; i++) {
1297 struct anv_bo *target_bo = anv_bo_unwrap(list->reloc_bos[i]);
1298 if (list->relocs[i].presumed_offset == target_bo->offset &&
1299 !always_relocate)
1300 continue;
1301
1302 void *p = bo->map + list->relocs[i].offset;
1303 write_reloc(device, p, target_bo->offset + list->relocs[i].delta, true);
1304 list->relocs[i].presumed_offset = target_bo->offset;
1305 }
1306 }
1307
1308 /**
1309 * This function applies the relocation for a command buffer and writes the
1310 * actual addresses into the buffers as per what we were told by the kernel on
1311 * the previous execbuf2 call. This should be safe to do because, for each
1312 * relocated address, we have two cases:
1313 *
1314 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1315 * not in use by the GPU so updating the address is 100% ok. It won't be
1316 * in-use by the GPU (from our context) again until the next execbuf2
1317 * happens. If the kernel decides to move it in the next execbuf2, it
1318 * will have to do the relocations itself, but that's ok because it should
1319 * have all of the information needed to do so.
1320 *
1321 * 2) The target BO is active (as seen by the kernel). In this case, it
1322 * hasn't moved since the last execbuffer2 call because GTT shuffling
1323 * *only* happens when the BO is idle. (From our perspective, it only
1324 * happens inside the execbuffer2 ioctl, but the shuffling may be
1325 * triggered by another ioctl, with full-ppgtt this is limited to only
1326 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1327 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1328 * address and the relocated value we are writing into the BO will be the
1329 * same as the value that is already there.
1330 *
1331 * There is also a possibility that the target BO is active but the exact
1332 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1333 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1334 * may be stale but it's still safe to write the relocation because that
1335 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1336 * won't be until the next execbuf2 call.
1337 *
1338 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1339 * need to bother. We want to do this because the surface state buffer is
1340 * used by every command buffer so, if the kernel does the relocations, it
1341 * will always be busy and the kernel will always stall. This is also
1342 * probably the fastest mechanism for doing relocations since the kernel would
1343 * have to make a full copy of all the relocations lists.
1344 */
1345 static bool
1346 relocate_cmd_buffer(struct anv_cmd_buffer *cmd_buffer,
1347 struct anv_execbuf *exec)
1348 {
1349 if (!exec->has_relocs)
1350 return true;
1351
1352 static int userspace_relocs = -1;
1353 if (userspace_relocs < 0)
1354 userspace_relocs = env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1355 if (!userspace_relocs)
1356 return false;
1357
1358 /* First, we have to check to see whether or not we can even do the
1359 * relocation. New buffers which have never been submitted to the kernel
1360 * don't have a valid offset so we need to let the kernel do relocations so
1361 * that we can get offsets for them. On future execbuf2 calls, those
1362 * buffers will have offsets and we will be able to skip relocating.
1363 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1364 */
1365 for (uint32_t i = 0; i < exec->bo_count; i++) {
1366 assert(!exec->bos[i]->is_wrapper);
1367 if (exec->bos[i]->offset == (uint64_t)-1)
1368 return false;
1369 }
1370
1371 /* Since surface states are shared between command buffers and we don't
1372 * know what order they will be submitted to the kernel, we don't know
1373 * what address is actually written in the surface state object at any
1374 * given time. The only option is to always relocate them.
1375 */
1376 struct anv_bo *surface_state_bo =
1377 anv_bo_unwrap(cmd_buffer->device->surface_state_pool.block_pool.bo);
1378 anv_reloc_list_apply(cmd_buffer->device, &cmd_buffer->surface_relocs,
1379 surface_state_bo,
1380 true /* always relocate surface states */);
1381
1382 /* Since we own all of the batch buffers, we know what values are stored
1383 * in the relocated addresses and only have to update them if the offsets
1384 * have changed.
1385 */
1386 struct anv_batch_bo **bbo;
1387 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1388 anv_reloc_list_apply(cmd_buffer->device,
1389 &(*bbo)->relocs, (*bbo)->bo, false);
1390 }
1391
1392 for (uint32_t i = 0; i < exec->bo_count; i++)
1393 exec->objects[i].offset = exec->bos[i]->offset;
1394
1395 return true;
1396 }
1397
1398 static VkResult
1399 setup_execbuf_for_cmd_buffer(struct anv_execbuf *execbuf,
1400 struct anv_cmd_buffer *cmd_buffer)
1401 {
1402 struct anv_batch *batch = &cmd_buffer->batch;
1403 struct anv_state_pool *ss_pool =
1404 &cmd_buffer->device->surface_state_pool;
1405
1406 adjust_relocations_from_state_pool(ss_pool, &cmd_buffer->surface_relocs,
1407 cmd_buffer->last_ss_pool_center);
1408 VkResult result;
1409 if (cmd_buffer->device->instance->physicalDevice.use_softpin) {
1410 anv_block_pool_foreach_bo(bo, &ss_pool->block_pool) {
1411 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1412 bo, NULL, 0,
1413 &cmd_buffer->device->alloc);
1414 if (result != VK_SUCCESS)
1415 return result;
1416 }
1417 /* Add surface dependencies (BOs) to the execbuf */
1418 anv_execbuf_add_bo_set(cmd_buffer->device, execbuf,
1419 cmd_buffer->surface_relocs.deps, 0,
1420 &cmd_buffer->device->alloc);
1421
1422 /* Add the BOs for all memory objects */
1423 list_for_each_entry(struct anv_device_memory, mem,
1424 &cmd_buffer->device->memory_objects, link) {
1425 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1426 mem->bo, NULL, 0,
1427 &cmd_buffer->device->alloc);
1428 if (result != VK_SUCCESS)
1429 return result;
1430 }
1431
1432 struct anv_block_pool *pool;
1433 pool = &cmd_buffer->device->dynamic_state_pool.block_pool;
1434 anv_block_pool_foreach_bo(bo, pool) {
1435 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1436 bo, NULL, 0,
1437 &cmd_buffer->device->alloc);
1438 if (result != VK_SUCCESS)
1439 return result;
1440 }
1441
1442 pool = &cmd_buffer->device->instruction_state_pool.block_pool;
1443 anv_block_pool_foreach_bo(bo, pool) {
1444 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1445 bo, NULL, 0,
1446 &cmd_buffer->device->alloc);
1447 if (result != VK_SUCCESS)
1448 return result;
1449 }
1450
1451 pool = &cmd_buffer->device->binding_table_pool.block_pool;
1452 anv_block_pool_foreach_bo(bo, pool) {
1453 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1454 bo, NULL, 0,
1455 &cmd_buffer->device->alloc);
1456 if (result != VK_SUCCESS)
1457 return result;
1458 }
1459 } else {
1460 /* Since we aren't in the softpin case, all of our STATE_BASE_ADDRESS BOs
1461 * will get added automatically by processing relocations on the batch
1462 * buffer. We have to add the surface state BO manually because it has
1463 * relocations of its own that we need to be sure are processsed.
1464 */
1465 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1466 ss_pool->block_pool.bo,
1467 &cmd_buffer->surface_relocs, 0,
1468 &cmd_buffer->device->alloc);
1469 if (result != VK_SUCCESS)
1470 return result;
1471 }
1472
1473 /* First, we walk over all of the bos we've seen and add them and their
1474 * relocations to the validate list.
1475 */
1476 struct anv_batch_bo **bbo;
1477 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1478 adjust_relocations_to_state_pool(ss_pool, (*bbo)->bo, &(*bbo)->relocs,
1479 cmd_buffer->last_ss_pool_center);
1480
1481 result = anv_execbuf_add_bo(cmd_buffer->device, execbuf,
1482 (*bbo)->bo, &(*bbo)->relocs, 0,
1483 &cmd_buffer->device->alloc);
1484 if (result != VK_SUCCESS)
1485 return result;
1486 }
1487
1488 /* Now that we've adjusted all of the surface state relocations, we need to
1489 * record the surface state pool center so future executions of the command
1490 * buffer can adjust correctly.
1491 */
1492 cmd_buffer->last_ss_pool_center = ss_pool->block_pool.center_bo_offset;
1493
1494 struct anv_batch_bo *first_batch_bo =
1495 list_first_entry(&cmd_buffer->batch_bos, struct anv_batch_bo, link);
1496
1497 /* The kernel requires that the last entry in the validation list be the
1498 * batch buffer to execute. We can simply swap the element
1499 * corresponding to the first batch_bo in the chain with the last
1500 * element in the list.
1501 */
1502 if (first_batch_bo->bo->index != execbuf->bo_count - 1) {
1503 uint32_t idx = first_batch_bo->bo->index;
1504 uint32_t last_idx = execbuf->bo_count - 1;
1505
1506 struct drm_i915_gem_exec_object2 tmp_obj = execbuf->objects[idx];
1507 assert(execbuf->bos[idx] == first_batch_bo->bo);
1508
1509 execbuf->objects[idx] = execbuf->objects[last_idx];
1510 execbuf->bos[idx] = execbuf->bos[last_idx];
1511 execbuf->bos[idx]->index = idx;
1512
1513 execbuf->objects[last_idx] = tmp_obj;
1514 execbuf->bos[last_idx] = first_batch_bo->bo;
1515 first_batch_bo->bo->index = last_idx;
1516 }
1517
1518 /* If we are pinning our BOs, we shouldn't have to relocate anything */
1519 if (cmd_buffer->device->instance->physicalDevice.use_softpin)
1520 assert(!execbuf->has_relocs);
1521
1522 /* Now we go through and fixup all of the relocation lists to point to
1523 * the correct indices in the object array. We have to do this after we
1524 * reorder the list above as some of the indices may have changed.
1525 */
1526 if (execbuf->has_relocs) {
1527 u_vector_foreach(bbo, &cmd_buffer->seen_bbos)
1528 anv_cmd_buffer_process_relocs(cmd_buffer, &(*bbo)->relocs);
1529
1530 anv_cmd_buffer_process_relocs(cmd_buffer, &cmd_buffer->surface_relocs);
1531 }
1532
1533 if (!cmd_buffer->device->info.has_llc) {
1534 __builtin_ia32_mfence();
1535 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1536 for (uint32_t i = 0; i < (*bbo)->length; i += CACHELINE_SIZE)
1537 __builtin_ia32_clflush((*bbo)->bo->map + i);
1538 }
1539 }
1540
1541 execbuf->execbuf = (struct drm_i915_gem_execbuffer2) {
1542 .buffers_ptr = (uintptr_t) execbuf->objects,
1543 .buffer_count = execbuf->bo_count,
1544 .batch_start_offset = 0,
1545 .batch_len = batch->next - batch->start,
1546 .cliprects_ptr = 0,
1547 .num_cliprects = 0,
1548 .DR1 = 0,
1549 .DR4 = 0,
1550 .flags = I915_EXEC_HANDLE_LUT | I915_EXEC_RENDER,
1551 .rsvd1 = cmd_buffer->device->context_id,
1552 .rsvd2 = 0,
1553 };
1554
1555 if (relocate_cmd_buffer(cmd_buffer, execbuf)) {
1556 /* If we were able to successfully relocate everything, tell the kernel
1557 * that it can skip doing relocations. The requirement for using
1558 * NO_RELOC is:
1559 *
1560 * 1) The addresses written in the objects must match the corresponding
1561 * reloc.presumed_offset which in turn must match the corresponding
1562 * execobject.offset.
1563 *
1564 * 2) To avoid stalling, execobject.offset should match the current
1565 * address of that object within the active context.
1566 *
1567 * In order to satisfy all of the invariants that make userspace
1568 * relocations to be safe (see relocate_cmd_buffer()), we need to
1569 * further ensure that the addresses we use match those used by the
1570 * kernel for the most recent execbuf2.
1571 *
1572 * The kernel may still choose to do relocations anyway if something has
1573 * moved in the GTT. In this case, the relocation list still needs to be
1574 * valid. All relocations on the batch buffers are already valid and
1575 * kept up-to-date. For surface state relocations, by applying the
1576 * relocations in relocate_cmd_buffer, we ensured that the address in
1577 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1578 * safe for the kernel to relocate them as needed.
1579 */
1580 execbuf->execbuf.flags |= I915_EXEC_NO_RELOC;
1581 } else {
1582 /* In the case where we fall back to doing kernel relocations, we need
1583 * to ensure that the relocation list is valid. All relocations on the
1584 * batch buffers are already valid and kept up-to-date. Since surface
1585 * states are shared between command buffers and we don't know what
1586 * order they will be submitted to the kernel, we don't know what
1587 * address is actually written in the surface state object at any given
1588 * time. The only option is to set a bogus presumed offset and let the
1589 * kernel relocate them.
1590 */
1591 for (size_t i = 0; i < cmd_buffer->surface_relocs.num_relocs; i++)
1592 cmd_buffer->surface_relocs.relocs[i].presumed_offset = -1;
1593 }
1594
1595 return VK_SUCCESS;
1596 }
1597
1598 static VkResult
1599 setup_empty_execbuf(struct anv_execbuf *execbuf, struct anv_device *device)
1600 {
1601 VkResult result = anv_execbuf_add_bo(device, execbuf,
1602 device->trivial_batch_bo,
1603 NULL, 0, &device->alloc);
1604 if (result != VK_SUCCESS)
1605 return result;
1606
1607 execbuf->execbuf = (struct drm_i915_gem_execbuffer2) {
1608 .buffers_ptr = (uintptr_t) execbuf->objects,
1609 .buffer_count = execbuf->bo_count,
1610 .batch_start_offset = 0,
1611 .batch_len = 8, /* GEN7_MI_BATCH_BUFFER_END and NOOP */
1612 .flags = I915_EXEC_HANDLE_LUT | I915_EXEC_RENDER,
1613 .rsvd1 = device->context_id,
1614 .rsvd2 = 0,
1615 };
1616
1617 return VK_SUCCESS;
1618 }
1619
1620 VkResult
1621 anv_cmd_buffer_execbuf(struct anv_device *device,
1622 struct anv_cmd_buffer *cmd_buffer,
1623 const VkSemaphore *in_semaphores,
1624 uint32_t num_in_semaphores,
1625 const VkSemaphore *out_semaphores,
1626 uint32_t num_out_semaphores,
1627 VkFence _fence)
1628 {
1629 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1630 UNUSED struct anv_physical_device *pdevice = &device->instance->physicalDevice;
1631
1632 struct anv_execbuf execbuf;
1633 anv_execbuf_init(&execbuf);
1634
1635 int in_fence = -1;
1636 VkResult result = VK_SUCCESS;
1637 for (uint32_t i = 0; i < num_in_semaphores; i++) {
1638 ANV_FROM_HANDLE(anv_semaphore, semaphore, in_semaphores[i]);
1639 struct anv_semaphore_impl *impl =
1640 semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE ?
1641 &semaphore->temporary : &semaphore->permanent;
1642
1643 switch (impl->type) {
1644 case ANV_SEMAPHORE_TYPE_BO:
1645 assert(!pdevice->has_syncobj);
1646 result = anv_execbuf_add_bo(device, &execbuf, impl->bo, NULL,
1647 0, &device->alloc);
1648 if (result != VK_SUCCESS)
1649 return result;
1650 break;
1651
1652 case ANV_SEMAPHORE_TYPE_SYNC_FILE:
1653 assert(!pdevice->has_syncobj);
1654 if (in_fence == -1) {
1655 in_fence = impl->fd;
1656 } else {
1657 int merge = anv_gem_sync_file_merge(device, in_fence, impl->fd);
1658 if (merge == -1)
1659 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
1660
1661 close(impl->fd);
1662 close(in_fence);
1663 in_fence = merge;
1664 }
1665
1666 impl->fd = -1;
1667 break;
1668
1669 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
1670 result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
1671 I915_EXEC_FENCE_WAIT,
1672 &device->alloc);
1673 if (result != VK_SUCCESS)
1674 return result;
1675 break;
1676
1677 default:
1678 break;
1679 }
1680 }
1681
1682 bool need_out_fence = false;
1683 for (uint32_t i = 0; i < num_out_semaphores; i++) {
1684 ANV_FROM_HANDLE(anv_semaphore, semaphore, out_semaphores[i]);
1685
1686 /* Under most circumstances, out fences won't be temporary. However,
1687 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1688 *
1689 * "If the import is temporary, the implementation must restore the
1690 * semaphore to its prior permanent state after submitting the next
1691 * semaphore wait operation."
1692 *
1693 * The spec says nothing whatsoever about signal operations on
1694 * temporarily imported semaphores so it appears they are allowed.
1695 * There are also CTS tests that require this to work.
1696 */
1697 struct anv_semaphore_impl *impl =
1698 semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE ?
1699 &semaphore->temporary : &semaphore->permanent;
1700
1701 switch (impl->type) {
1702 case ANV_SEMAPHORE_TYPE_BO:
1703 assert(!pdevice->has_syncobj);
1704 result = anv_execbuf_add_bo(device, &execbuf, impl->bo, NULL,
1705 EXEC_OBJECT_WRITE, &device->alloc);
1706 if (result != VK_SUCCESS)
1707 return result;
1708 break;
1709
1710 case ANV_SEMAPHORE_TYPE_SYNC_FILE:
1711 assert(!pdevice->has_syncobj);
1712 need_out_fence = true;
1713 break;
1714
1715 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
1716 result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
1717 I915_EXEC_FENCE_SIGNAL,
1718 &device->alloc);
1719 if (result != VK_SUCCESS)
1720 return result;
1721 break;
1722
1723 default:
1724 break;
1725 }
1726 }
1727
1728 if (fence) {
1729 /* Under most circumstances, out fences won't be temporary. However,
1730 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1731 *
1732 * "If the import is temporary, the implementation must restore the
1733 * semaphore to its prior permanent state after submitting the next
1734 * semaphore wait operation."
1735 *
1736 * The spec says nothing whatsoever about signal operations on
1737 * temporarily imported semaphores so it appears they are allowed.
1738 * There are also CTS tests that require this to work.
1739 */
1740 struct anv_fence_impl *impl =
1741 fence->temporary.type != ANV_FENCE_TYPE_NONE ?
1742 &fence->temporary : &fence->permanent;
1743
1744 switch (impl->type) {
1745 case ANV_FENCE_TYPE_BO:
1746 assert(!pdevice->has_syncobj_wait);
1747 result = anv_execbuf_add_bo(device, &execbuf, impl->bo.bo, NULL,
1748 EXEC_OBJECT_WRITE, &device->alloc);
1749 if (result != VK_SUCCESS)
1750 return result;
1751 break;
1752
1753 case ANV_FENCE_TYPE_SYNCOBJ:
1754 result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
1755 I915_EXEC_FENCE_SIGNAL,
1756 &device->alloc);
1757 if (result != VK_SUCCESS)
1758 return result;
1759 break;
1760
1761 default:
1762 unreachable("Invalid fence type");
1763 }
1764 }
1765
1766 if (cmd_buffer) {
1767 if (unlikely(INTEL_DEBUG & DEBUG_BATCH)) {
1768 struct anv_batch_bo **bo = u_vector_tail(&cmd_buffer->seen_bbos);
1769
1770 device->cmd_buffer_being_decoded = cmd_buffer;
1771 gen_print_batch(&device->decoder_ctx, (*bo)->bo->map,
1772 (*bo)->bo->size, (*bo)->bo->offset, false);
1773 device->cmd_buffer_being_decoded = NULL;
1774 }
1775
1776 result = setup_execbuf_for_cmd_buffer(&execbuf, cmd_buffer);
1777 } else {
1778 result = setup_empty_execbuf(&execbuf, device);
1779 }
1780
1781 if (result != VK_SUCCESS)
1782 return result;
1783
1784 if (execbuf.fence_count > 0) {
1785 assert(device->instance->physicalDevice.has_syncobj);
1786 execbuf.execbuf.flags |= I915_EXEC_FENCE_ARRAY;
1787 execbuf.execbuf.num_cliprects = execbuf.fence_count;
1788 execbuf.execbuf.cliprects_ptr = (uintptr_t) execbuf.fences;
1789 }
1790
1791 if (in_fence != -1) {
1792 execbuf.execbuf.flags |= I915_EXEC_FENCE_IN;
1793 execbuf.execbuf.rsvd2 |= (uint32_t)in_fence;
1794 }
1795
1796 if (need_out_fence)
1797 execbuf.execbuf.flags |= I915_EXEC_FENCE_OUT;
1798
1799 result = anv_device_execbuf(device, &execbuf.execbuf, execbuf.bos);
1800
1801 /* Execbuf does not consume the in_fence. It's our job to close it. */
1802 if (in_fence != -1)
1803 close(in_fence);
1804
1805 for (uint32_t i = 0; i < num_in_semaphores; i++) {
1806 ANV_FROM_HANDLE(anv_semaphore, semaphore, in_semaphores[i]);
1807 /* From the Vulkan 1.0.53 spec:
1808 *
1809 * "If the import is temporary, the implementation must restore the
1810 * semaphore to its prior permanent state after submitting the next
1811 * semaphore wait operation."
1812 *
1813 * This has to happen after the execbuf in case we close any syncobjs in
1814 * the process.
1815 */
1816 anv_semaphore_reset_temporary(device, semaphore);
1817 }
1818
1819 if (fence && fence->permanent.type == ANV_FENCE_TYPE_BO) {
1820 assert(!pdevice->has_syncobj_wait);
1821 /* BO fences can't be shared, so they can't be temporary. */
1822 assert(fence->temporary.type == ANV_FENCE_TYPE_NONE);
1823
1824 /* Once the execbuf has returned, we need to set the fence state to
1825 * SUBMITTED. We can't do this before calling execbuf because
1826 * anv_GetFenceStatus does take the global device lock before checking
1827 * fence->state.
1828 *
1829 * We set the fence state to SUBMITTED regardless of whether or not the
1830 * execbuf succeeds because we need to ensure that vkWaitForFences() and
1831 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
1832 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
1833 */
1834 fence->permanent.bo.state = ANV_BO_FENCE_STATE_SUBMITTED;
1835 }
1836
1837 if (result == VK_SUCCESS && need_out_fence) {
1838 assert(!pdevice->has_syncobj_wait);
1839 int out_fence = execbuf.execbuf.rsvd2 >> 32;
1840 for (uint32_t i = 0; i < num_out_semaphores; i++) {
1841 ANV_FROM_HANDLE(anv_semaphore, semaphore, out_semaphores[i]);
1842 /* Out fences can't have temporary state because that would imply
1843 * that we imported a sync file and are trying to signal it.
1844 */
1845 assert(semaphore->temporary.type == ANV_SEMAPHORE_TYPE_NONE);
1846 struct anv_semaphore_impl *impl = &semaphore->permanent;
1847
1848 if (impl->type == ANV_SEMAPHORE_TYPE_SYNC_FILE) {
1849 assert(impl->fd == -1);
1850 impl->fd = dup(out_fence);
1851 }
1852 }
1853 close(out_fence);
1854 }
1855
1856 anv_execbuf_finish(&execbuf, &device->alloc);
1857
1858 return result;
1859 }