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