2 * Copyright © 2015 Intel Corporation
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:
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
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
30 #include "anv_private.h"
32 #include "genxml/gen8_pack.h"
34 #include "util/debug.h"
36 /** \file anv_batch_chain.c
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.
45 /*-----------------------------------------------------------------------*
46 * Functions related to anv_reloc_list
47 *-----------------------------------------------------------------------*/
50 anv_reloc_list_init_clone(struct anv_reloc_list
*list
,
51 const VkAllocationCallbacks
*alloc
,
52 const struct anv_reloc_list
*other_list
)
55 list
->num_relocs
= other_list
->num_relocs
;
56 list
->array_length
= other_list
->array_length
;
59 list
->array_length
= 256;
63 vk_alloc(alloc
, list
->array_length
* sizeof(*list
->relocs
), 8,
64 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
66 if (list
->relocs
== NULL
)
67 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
70 vk_alloc(alloc
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
71 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
73 if (list
->reloc_bos
== NULL
) {
74 vk_free(alloc
, list
->relocs
);
75 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
78 list
->deps
= _mesa_pointer_set_create(NULL
);
81 vk_free(alloc
, list
->relocs
);
82 vk_free(alloc
, list
->reloc_bos
);
83 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
87 memcpy(list
->relocs
, other_list
->relocs
,
88 list
->array_length
* sizeof(*list
->relocs
));
89 memcpy(list
->reloc_bos
, other_list
->reloc_bos
,
90 list
->array_length
* sizeof(*list
->reloc_bos
));
91 set_foreach(other_list
->deps
, entry
) {
92 _mesa_set_add_pre_hashed(list
->deps
, entry
->hash
, entry
->key
);
100 anv_reloc_list_init(struct anv_reloc_list
*list
,
101 const VkAllocationCallbacks
*alloc
)
103 return anv_reloc_list_init_clone(list
, alloc
, NULL
);
107 anv_reloc_list_finish(struct anv_reloc_list
*list
,
108 const VkAllocationCallbacks
*alloc
)
110 vk_free(alloc
, list
->relocs
);
111 vk_free(alloc
, list
->reloc_bos
);
112 _mesa_set_destroy(list
->deps
, NULL
);
116 anv_reloc_list_grow(struct anv_reloc_list
*list
,
117 const VkAllocationCallbacks
*alloc
,
118 size_t num_additional_relocs
)
120 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
123 size_t new_length
= list
->array_length
* 2;
124 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
127 struct drm_i915_gem_relocation_entry
*new_relocs
=
128 vk_alloc(alloc
, new_length
* sizeof(*list
->relocs
), 8,
129 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
130 if (new_relocs
== NULL
)
131 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
133 struct anv_bo
**new_reloc_bos
=
134 vk_alloc(alloc
, new_length
* sizeof(*list
->reloc_bos
), 8,
135 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
136 if (new_reloc_bos
== NULL
) {
137 vk_free(alloc
, new_relocs
);
138 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
141 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
142 memcpy(new_reloc_bos
, list
->reloc_bos
,
143 list
->num_relocs
* sizeof(*list
->reloc_bos
));
145 vk_free(alloc
, list
->relocs
);
146 vk_free(alloc
, list
->reloc_bos
);
148 list
->array_length
= new_length
;
149 list
->relocs
= new_relocs
;
150 list
->reloc_bos
= new_reloc_bos
;
156 anv_reloc_list_add(struct anv_reloc_list
*list
,
157 const VkAllocationCallbacks
*alloc
,
158 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
160 struct drm_i915_gem_relocation_entry
*entry
;
163 if (target_bo
->flags
& EXEC_OBJECT_PINNED
) {
164 _mesa_set_add(list
->deps
, target_bo
);
168 VkResult result
= anv_reloc_list_grow(list
, alloc
, 1);
169 if (result
!= VK_SUCCESS
)
172 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
173 index
= list
->num_relocs
++;
174 list
->reloc_bos
[index
] = target_bo
;
175 entry
= &list
->relocs
[index
];
176 entry
->target_handle
= target_bo
->gem_handle
;
177 entry
->delta
= delta
;
178 entry
->offset
= offset
;
179 entry
->presumed_offset
= target_bo
->offset
;
180 entry
->read_domains
= 0;
181 entry
->write_domain
= 0;
182 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry
, sizeof(*entry
)));
188 anv_reloc_list_append(struct anv_reloc_list
*list
,
189 const VkAllocationCallbacks
*alloc
,
190 struct anv_reloc_list
*other
, uint32_t offset
)
192 VkResult result
= anv_reloc_list_grow(list
, alloc
, other
->num_relocs
);
193 if (result
!= VK_SUCCESS
)
196 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
197 other
->num_relocs
* sizeof(other
->relocs
[0]));
198 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
199 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
201 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
202 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
204 list
->num_relocs
+= other
->num_relocs
;
206 set_foreach(other
->deps
, entry
) {
207 _mesa_set_add_pre_hashed(list
->deps
, entry
->hash
, entry
->key
);
213 /*-----------------------------------------------------------------------*
214 * Functions related to anv_batch
215 *-----------------------------------------------------------------------*/
218 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
220 if (batch
->next
+ num_dwords
* 4 > batch
->end
) {
221 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
222 if (result
!= VK_SUCCESS
) {
223 anv_batch_set_error(batch
, result
);
228 void *p
= batch
->next
;
230 batch
->next
+= num_dwords
* 4;
231 assert(batch
->next
<= batch
->end
);
237 anv_batch_emit_reloc(struct anv_batch
*batch
,
238 void *location
, struct anv_bo
*bo
, uint32_t delta
)
240 VkResult result
= anv_reloc_list_add(batch
->relocs
, batch
->alloc
,
241 location
- batch
->start
, bo
, delta
);
242 if (result
!= VK_SUCCESS
) {
243 anv_batch_set_error(batch
, result
);
247 return bo
->offset
+ delta
;
251 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
253 uint32_t size
, offset
;
255 size
= other
->next
- other
->start
;
256 assert(size
% 4 == 0);
258 if (batch
->next
+ size
> batch
->end
) {
259 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
260 if (result
!= VK_SUCCESS
) {
261 anv_batch_set_error(batch
, result
);
266 assert(batch
->next
+ size
<= batch
->end
);
268 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other
->start
, size
));
269 memcpy(batch
->next
, other
->start
, size
);
271 offset
= batch
->next
- batch
->start
;
272 VkResult result
= anv_reloc_list_append(batch
->relocs
, batch
->alloc
,
273 other
->relocs
, offset
);
274 if (result
!= VK_SUCCESS
) {
275 anv_batch_set_error(batch
, result
);
282 /*-----------------------------------------------------------------------*
283 * Functions related to anv_batch_bo
284 *-----------------------------------------------------------------------*/
287 anv_batch_bo_create(struct anv_cmd_buffer
*cmd_buffer
,
288 struct anv_batch_bo
**bbo_out
)
292 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
293 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
295 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
297 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
298 ANV_CMD_BUFFER_BATCH_SIZE
);
299 if (result
!= VK_SUCCESS
)
302 result
= anv_reloc_list_init(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
303 if (result
!= VK_SUCCESS
)
311 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
313 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
319 anv_batch_bo_clone(struct anv_cmd_buffer
*cmd_buffer
,
320 const struct anv_batch_bo
*other_bbo
,
321 struct anv_batch_bo
**bbo_out
)
325 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
326 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
328 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
330 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
332 if (result
!= VK_SUCCESS
)
335 result
= anv_reloc_list_init_clone(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
,
337 if (result
!= VK_SUCCESS
)
340 bbo
->length
= other_bbo
->length
;
341 memcpy(bbo
->bo
.map
, other_bbo
->bo
.map
, other_bbo
->length
);
348 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
350 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
356 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
357 size_t batch_padding
)
359 batch
->next
= batch
->start
= bbo
->bo
.map
;
360 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
361 batch
->relocs
= &bbo
->relocs
;
362 bbo
->relocs
.num_relocs
= 0;
363 _mesa_set_clear(bbo
->relocs
.deps
, NULL
);
367 anv_batch_bo_continue(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
368 size_t batch_padding
)
370 batch
->start
= bbo
->bo
.map
;
371 batch
->next
= bbo
->bo
.map
+ bbo
->length
;
372 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
373 batch
->relocs
= &bbo
->relocs
;
377 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
379 assert(batch
->start
== bbo
->bo
.map
);
380 bbo
->length
= batch
->next
- batch
->start
;
381 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
385 anv_batch_bo_grow(struct anv_cmd_buffer
*cmd_buffer
, struct anv_batch_bo
*bbo
,
386 struct anv_batch
*batch
, size_t aditional
,
387 size_t batch_padding
)
389 assert(batch
->start
== bbo
->bo
.map
);
390 bbo
->length
= batch
->next
- batch
->start
;
392 size_t new_size
= bbo
->bo
.size
;
393 while (new_size
<= bbo
->length
+ aditional
+ batch_padding
)
396 if (new_size
== bbo
->bo
.size
)
399 struct anv_bo new_bo
;
400 VkResult result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
402 if (result
!= VK_SUCCESS
)
405 memcpy(new_bo
.map
, bbo
->bo
.map
, bbo
->length
);
407 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
410 anv_batch_bo_continue(bbo
, batch
, batch_padding
);
416 anv_batch_bo_link(struct anv_cmd_buffer
*cmd_buffer
,
417 struct anv_batch_bo
*prev_bbo
,
418 struct anv_batch_bo
*next_bbo
,
419 uint32_t next_bbo_offset
)
421 MAYBE_UNUSED
const uint32_t bb_start_offset
=
422 prev_bbo
->length
- GEN8_MI_BATCH_BUFFER_START_length
* 4;
423 MAYBE_UNUSED
const uint32_t *bb_start
= prev_bbo
->bo
.map
+ bb_start_offset
;
425 /* Make sure we're looking at a MI_BATCH_BUFFER_START */
426 assert(((*bb_start
>> 29) & 0x07) == 0);
427 assert(((*bb_start
>> 23) & 0x3f) == 49);
429 if (cmd_buffer
->device
->instance
->physicalDevice
.use_softpin
) {
430 assert(prev_bbo
->bo
.flags
& EXEC_OBJECT_PINNED
);
431 assert(next_bbo
->bo
.flags
& EXEC_OBJECT_PINNED
);
433 write_reloc(cmd_buffer
->device
,
434 prev_bbo
->bo
.map
+ bb_start_offset
+ 4,
435 next_bbo
->bo
.offset
+ next_bbo_offset
, true);
437 uint32_t reloc_idx
= prev_bbo
->relocs
.num_relocs
- 1;
438 assert(prev_bbo
->relocs
.relocs
[reloc_idx
].offset
== bb_start_offset
+ 4);
440 prev_bbo
->relocs
.reloc_bos
[reloc_idx
] = &next_bbo
->bo
;
441 prev_bbo
->relocs
.relocs
[reloc_idx
].delta
= next_bbo_offset
;
443 /* Use a bogus presumed offset to force a relocation */
444 prev_bbo
->relocs
.relocs
[reloc_idx
].presumed_offset
= -1;
449 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
,
450 struct anv_cmd_buffer
*cmd_buffer
)
452 anv_reloc_list_finish(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
453 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
454 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
458 anv_batch_bo_list_clone(const struct list_head
*list
,
459 struct anv_cmd_buffer
*cmd_buffer
,
460 struct list_head
*new_list
)
462 VkResult result
= VK_SUCCESS
;
464 list_inithead(new_list
);
466 struct anv_batch_bo
*prev_bbo
= NULL
;
467 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
468 struct anv_batch_bo
*new_bbo
= NULL
;
469 result
= anv_batch_bo_clone(cmd_buffer
, bbo
, &new_bbo
);
470 if (result
!= VK_SUCCESS
)
472 list_addtail(&new_bbo
->link
, new_list
);
475 anv_batch_bo_link(cmd_buffer
, prev_bbo
, new_bbo
, 0);
480 if (result
!= VK_SUCCESS
) {
481 list_for_each_entry_safe(struct anv_batch_bo
, bbo
, new_list
, link
)
482 anv_batch_bo_destroy(bbo
, cmd_buffer
);
488 /*-----------------------------------------------------------------------*
489 * Functions related to anv_batch_bo
490 *-----------------------------------------------------------------------*/
492 static struct anv_batch_bo
*
493 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer
*cmd_buffer
)
495 return LIST_ENTRY(struct anv_batch_bo
, cmd_buffer
->batch_bos
.prev
, link
);
499 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
)
501 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
502 return (struct anv_address
) {
503 .bo
= &anv_binding_table_pool(cmd_buffer
->device
)->block_pool
.bo
,
504 .offset
= bt_block
->offset
,
509 emit_batch_buffer_start(struct anv_cmd_buffer
*cmd_buffer
,
510 struct anv_bo
*bo
, uint32_t offset
)
512 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
513 * offsets. The high 16 bits are in the last dword, so we can use the gen8
514 * version in either case, as long as we set the instruction length in the
515 * header accordingly. This means that we always emit three dwords here
516 * and all the padding and adjustment we do in this file works for all
520 #define GEN7_MI_BATCH_BUFFER_START_length 2
521 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
523 const uint32_t gen7_length
=
524 GEN7_MI_BATCH_BUFFER_START_length
- GEN7_MI_BATCH_BUFFER_START_length_bias
;
525 const uint32_t gen8_length
=
526 GEN8_MI_BATCH_BUFFER_START_length
- GEN8_MI_BATCH_BUFFER_START_length_bias
;
528 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_START
, bbs
) {
529 bbs
.DWordLength
= cmd_buffer
->device
->info
.gen
< 8 ?
530 gen7_length
: gen8_length
;
531 bbs
.SecondLevelBatchBuffer
= Firstlevelbatch
;
532 bbs
.AddressSpaceIndicator
= ASI_PPGTT
;
533 bbs
.BatchBufferStartAddress
= (struct anv_address
) { bo
, offset
};
538 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer
*cmd_buffer
,
539 struct anv_batch_bo
*bbo
)
541 struct anv_batch
*batch
= &cmd_buffer
->batch
;
542 struct anv_batch_bo
*current_bbo
=
543 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
545 /* We set the end of the batch a little short so we would be sure we
546 * have room for the chaining command. Since we're about to emit the
547 * chaining command, let's set it back where it should go.
549 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
550 assert(batch
->end
== current_bbo
->bo
.map
+ current_bbo
->bo
.size
);
552 emit_batch_buffer_start(cmd_buffer
, &bbo
->bo
, 0);
554 anv_batch_bo_finish(current_bbo
, batch
);
558 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
560 struct anv_cmd_buffer
*cmd_buffer
= _data
;
561 struct anv_batch_bo
*new_bbo
;
563 VkResult result
= anv_batch_bo_create(cmd_buffer
, &new_bbo
);
564 if (result
!= VK_SUCCESS
)
567 struct anv_batch_bo
**seen_bbo
= u_vector_add(&cmd_buffer
->seen_bbos
);
568 if (seen_bbo
== NULL
) {
569 anv_batch_bo_destroy(new_bbo
, cmd_buffer
);
570 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
574 cmd_buffer_chain_to_batch_bo(cmd_buffer
, new_bbo
);
576 list_addtail(&new_bbo
->link
, &cmd_buffer
->batch_bos
);
578 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
584 anv_cmd_buffer_grow_batch(struct anv_batch
*batch
, void *_data
)
586 struct anv_cmd_buffer
*cmd_buffer
= _data
;
587 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
589 anv_batch_bo_grow(cmd_buffer
, bbo
, &cmd_buffer
->batch
, 4096,
590 GEN8_MI_BATCH_BUFFER_START_length
* 4);
595 /** Allocate a binding table
597 * This function allocates a binding table. This is a bit more complicated
598 * than one would think due to a combination of Vulkan driver design and some
599 * unfortunate hardware restrictions.
601 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
602 * the binding table pointer which means that all binding tables need to live
603 * in the bottom 64k of surface state base address. The way the GL driver has
604 * classically dealt with this restriction is to emit all surface states
605 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
606 * isn't really an option in Vulkan for a couple of reasons:
608 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
609 * to live in their own buffer and we have to be able to re-emit
610 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
611 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
612 * (it's not that hard to hit 64k of just binding tables), we allocate
613 * surface state objects up-front when VkImageView is created. In order
614 * for this to work, surface state objects need to be allocated from a
617 * 2) We tried to design the surface state system in such a way that it's
618 * already ready for bindless texturing. The way bindless texturing works
619 * on our hardware is that you have a big pool of surface state objects
620 * (with its own state base address) and the bindless handles are simply
621 * offsets into that pool. With the architecture we chose, we already
622 * have that pool and it's exactly the same pool that we use for regular
623 * surface states so we should already be ready for bindless.
625 * 3) For render targets, we need to be able to fill out the surface states
626 * later in vkBeginRenderPass so that we can assign clear colors
627 * correctly. One way to do this would be to just create the surface
628 * state data and then repeatedly copy it into the surface state BO every
629 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
630 * rather annoying and just being able to allocate them up-front and
631 * re-use them for the entire render pass.
633 * While none of these are technically blockers for emitting state on the fly
634 * like we do in GL, the ability to have a single surface state pool is
635 * simplifies things greatly. Unfortunately, it comes at a cost...
637 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
638 * place the binding tables just anywhere in surface state base address.
639 * Because 64k isn't a whole lot of space, we can't simply restrict the
640 * surface state buffer to 64k, we have to be more clever. The solution we've
641 * chosen is to have a block pool with a maximum size of 2G that starts at
642 * zero and grows in both directions. All surface states are allocated from
643 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
644 * binding tables from the bottom of the pool (negative offsets). Every time
645 * we allocate a new binding table block, we set surface state base address to
646 * point to the bottom of the binding table block. This way all of the
647 * binding tables in the block are in the bottom 64k of surface state base
648 * address. When we fill out the binding table, we add the distance between
649 * the bottom of our binding table block and zero of the block pool to the
650 * surface state offsets so that they are correct relative to out new surface
651 * state base address at the bottom of the binding table block.
653 * \see adjust_relocations_from_block_pool()
654 * \see adjust_relocations_too_block_pool()
656 * \param[in] entries The number of surface state entries the binding
657 * table should be able to hold.
659 * \param[out] state_offset The offset surface surface state base address
660 * where the surface states live. This must be
661 * added to the surface state offset when it is
662 * written into the binding table entry.
664 * \return An anv_state representing the binding table
667 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
668 uint32_t entries
, uint32_t *state_offset
)
670 struct anv_device
*device
= cmd_buffer
->device
;
671 struct anv_state_pool
*state_pool
= &device
->surface_state_pool
;
672 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
673 struct anv_state state
;
675 state
.alloc_size
= align_u32(entries
* 4, 32);
677 if (cmd_buffer
->bt_next
+ state
.alloc_size
> state_pool
->block_size
)
678 return (struct anv_state
) { 0 };
680 state
.offset
= cmd_buffer
->bt_next
;
681 state
.map
= anv_block_pool_map(&anv_binding_table_pool(device
)->block_pool
,
682 bt_block
->offset
+ state
.offset
);
684 cmd_buffer
->bt_next
+= state
.alloc_size
;
686 if (device
->instance
->physicalDevice
.use_softpin
) {
687 assert(bt_block
->offset
>= 0);
688 *state_offset
= device
->surface_state_pool
.block_pool
.start_address
-
689 device
->binding_table_pool
.block_pool
.start_address
- bt_block
->offset
;
691 assert(bt_block
->offset
< 0);
692 *state_offset
= -bt_block
->offset
;
699 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
)
701 struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
702 return anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
703 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
707 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
708 uint32_t size
, uint32_t alignment
)
710 return anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
715 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
)
717 struct anv_state
*bt_block
= u_vector_add(&cmd_buffer
->bt_block_states
);
718 if (bt_block
== NULL
) {
719 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
720 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
723 *bt_block
= anv_binding_table_pool_alloc(cmd_buffer
->device
);
724 cmd_buffer
->bt_next
= 0;
730 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
732 struct anv_batch_bo
*batch_bo
;
735 list_inithead(&cmd_buffer
->batch_bos
);
737 result
= anv_batch_bo_create(cmd_buffer
, &batch_bo
);
738 if (result
!= VK_SUCCESS
)
741 list_addtail(&batch_bo
->link
, &cmd_buffer
->batch_bos
);
743 cmd_buffer
->batch
.alloc
= &cmd_buffer
->pool
->alloc
;
744 cmd_buffer
->batch
.user_data
= cmd_buffer
;
746 if (cmd_buffer
->device
->can_chain_batches
) {
747 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
749 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_grow_batch
;
752 anv_batch_bo_start(batch_bo
, &cmd_buffer
->batch
,
753 GEN8_MI_BATCH_BUFFER_START_length
* 4);
755 int success
= u_vector_init(&cmd_buffer
->seen_bbos
,
756 sizeof(struct anv_bo
*),
757 8 * sizeof(struct anv_bo
*));
761 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) = batch_bo
;
763 /* u_vector requires power-of-two size elements */
764 unsigned pow2_state_size
= util_next_power_of_two(sizeof(struct anv_state
));
765 success
= u_vector_init(&cmd_buffer
->bt_block_states
,
766 pow2_state_size
, 8 * pow2_state_size
);
770 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
,
771 &cmd_buffer
->pool
->alloc
);
772 if (result
!= VK_SUCCESS
)
774 cmd_buffer
->last_ss_pool_center
= 0;
776 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
777 if (result
!= VK_SUCCESS
)
783 u_vector_finish(&cmd_buffer
->bt_block_states
);
785 u_vector_finish(&cmd_buffer
->seen_bbos
);
787 anv_batch_bo_destroy(batch_bo
, cmd_buffer
);
793 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
795 struct anv_state
*bt_block
;
796 u_vector_foreach(bt_block
, &cmd_buffer
->bt_block_states
)
797 anv_binding_table_pool_free(cmd_buffer
->device
, *bt_block
);
798 u_vector_finish(&cmd_buffer
->bt_block_states
);
800 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
);
802 u_vector_finish(&cmd_buffer
->seen_bbos
);
804 /* Destroy all of the batch buffers */
805 list_for_each_entry_safe(struct anv_batch_bo
, bbo
,
806 &cmd_buffer
->batch_bos
, link
) {
807 anv_batch_bo_destroy(bbo
, cmd_buffer
);
812 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
814 /* Delete all but the first batch bo */
815 assert(!list_empty(&cmd_buffer
->batch_bos
));
816 while (cmd_buffer
->batch_bos
.next
!= cmd_buffer
->batch_bos
.prev
) {
817 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
818 list_del(&bbo
->link
);
819 anv_batch_bo_destroy(bbo
, cmd_buffer
);
821 assert(!list_empty(&cmd_buffer
->batch_bos
));
823 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer
),
825 GEN8_MI_BATCH_BUFFER_START_length
* 4);
827 while (u_vector_length(&cmd_buffer
->bt_block_states
) > 1) {
828 struct anv_state
*bt_block
= u_vector_remove(&cmd_buffer
->bt_block_states
);
829 anv_binding_table_pool_free(cmd_buffer
->device
, *bt_block
);
831 assert(u_vector_length(&cmd_buffer
->bt_block_states
) == 1);
832 cmd_buffer
->bt_next
= 0;
834 cmd_buffer
->surface_relocs
.num_relocs
= 0;
835 _mesa_set_clear(cmd_buffer
->surface_relocs
.deps
, NULL
);
836 cmd_buffer
->last_ss_pool_center
= 0;
838 /* Reset the list of seen buffers */
839 cmd_buffer
->seen_bbos
.head
= 0;
840 cmd_buffer
->seen_bbos
.tail
= 0;
842 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) =
843 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
847 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
)
849 struct anv_batch_bo
*batch_bo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
851 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
) {
852 /* When we start a batch buffer, we subtract a certain amount of
853 * padding from the end to ensure that we always have room to emit a
854 * BATCH_BUFFER_START to chain to the next BO. We need to remove
855 * that padding before we end the batch; otherwise, we may end up
856 * with our BATCH_BUFFER_END in another BO.
858 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
859 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
.map
+ batch_bo
->bo
.size
);
861 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_END
, bbe
);
863 /* Round batch up to an even number of dwords. */
864 if ((cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
) & 4)
865 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_NOOP
, noop
);
867 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
;
869 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
870 /* If this is a secondary command buffer, we need to determine the
871 * mode in which it will be executed with vkExecuteCommands. We
872 * determine this statically here so that this stays in sync with the
873 * actual ExecuteCommands implementation.
875 const uint32_t length
= cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
;
876 if (!cmd_buffer
->device
->can_chain_batches
) {
877 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
;
878 } else if ((cmd_buffer
->batch_bos
.next
== cmd_buffer
->batch_bos
.prev
) &&
879 (length
< ANV_CMD_BUFFER_BATCH_SIZE
/ 2)) {
880 /* If the secondary has exactly one batch buffer in its list *and*
881 * that batch buffer is less than half of the maximum size, we're
882 * probably better of simply copying it into our batch.
884 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_EMIT
;
885 } else if (!(cmd_buffer
->usage_flags
&
886 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
)) {
887 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_CHAIN
;
889 /* In order to chain, we need this command buffer to contain an
890 * MI_BATCH_BUFFER_START which will jump back to the calling batch.
891 * It doesn't matter where it points now so long as has a valid
892 * relocation. We'll adjust it later as part of the chaining
895 * We set the end of the batch a little short so we would be sure we
896 * have room for the chaining command. Since we're about to emit the
897 * chaining command, let's set it back where it should go.
899 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
900 assert(cmd_buffer
->batch
.start
== batch_bo
->bo
.map
);
901 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
.map
+ batch_bo
->bo
.size
);
903 emit_batch_buffer_start(cmd_buffer
, &batch_bo
->bo
, 0);
904 assert(cmd_buffer
->batch
.start
== batch_bo
->bo
.map
);
906 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
;
910 anv_batch_bo_finish(batch_bo
, &cmd_buffer
->batch
);
914 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer
*cmd_buffer
,
915 struct list_head
*list
)
917 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
918 struct anv_batch_bo
**bbo_ptr
= u_vector_add(&cmd_buffer
->seen_bbos
);
920 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
929 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
930 struct anv_cmd_buffer
*secondary
)
932 switch (secondary
->exec_mode
) {
933 case ANV_CMD_BUFFER_EXEC_MODE_EMIT
:
934 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
936 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
: {
937 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(primary
);
938 unsigned length
= secondary
->batch
.end
- secondary
->batch
.start
;
939 anv_batch_bo_grow(primary
, bbo
, &primary
->batch
, length
,
940 GEN8_MI_BATCH_BUFFER_START_length
* 4);
941 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
944 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN
: {
945 struct anv_batch_bo
*first_bbo
=
946 list_first_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
947 struct anv_batch_bo
*last_bbo
=
948 list_last_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
950 emit_batch_buffer_start(primary
, &first_bbo
->bo
, 0);
952 struct anv_batch_bo
*this_bbo
= anv_cmd_buffer_current_batch_bo(primary
);
953 assert(primary
->batch
.start
== this_bbo
->bo
.map
);
954 uint32_t offset
= primary
->batch
.next
- primary
->batch
.start
;
956 /* Make the tail of the secondary point back to right after the
957 * MI_BATCH_BUFFER_START in the primary batch.
959 anv_batch_bo_link(primary
, last_bbo
, this_bbo
, offset
);
961 anv_cmd_buffer_add_seen_bbos(primary
, &secondary
->batch_bos
);
964 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
: {
965 struct list_head copy_list
;
966 VkResult result
= anv_batch_bo_list_clone(&secondary
->batch_bos
,
969 if (result
!= VK_SUCCESS
)
972 anv_cmd_buffer_add_seen_bbos(primary
, ©_list
);
974 struct anv_batch_bo
*first_bbo
=
975 list_first_entry(©_list
, struct anv_batch_bo
, link
);
976 struct anv_batch_bo
*last_bbo
=
977 list_last_entry(©_list
, struct anv_batch_bo
, link
);
979 cmd_buffer_chain_to_batch_bo(primary
, first_bbo
);
981 list_splicetail(©_list
, &primary
->batch_bos
);
983 anv_batch_bo_continue(last_bbo
, &primary
->batch
,
984 GEN8_MI_BATCH_BUFFER_START_length
* 4);
988 assert(!"Invalid execution mode");
991 anv_reloc_list_append(&primary
->surface_relocs
, &primary
->pool
->alloc
,
992 &secondary
->surface_relocs
, 0);
996 struct drm_i915_gem_execbuffer2 execbuf
;
998 struct drm_i915_gem_exec_object2
* objects
;
1000 struct anv_bo
** bos
;
1002 /* Allocated length of the 'objects' and 'bos' arrays */
1003 uint32_t array_length
;
1007 uint32_t fence_count
;
1008 uint32_t fence_array_length
;
1009 struct drm_i915_gem_exec_fence
* fences
;
1010 struct anv_syncobj
** syncobjs
;
1014 anv_execbuf_init(struct anv_execbuf
*exec
)
1016 memset(exec
, 0, sizeof(*exec
));
1020 anv_execbuf_finish(struct anv_execbuf
*exec
,
1021 const VkAllocationCallbacks
*alloc
)
1023 vk_free(alloc
, exec
->objects
);
1024 vk_free(alloc
, exec
->bos
);
1025 vk_free(alloc
, exec
->fences
);
1026 vk_free(alloc
, exec
->syncobjs
);
1030 _compare_bo_handles(const void *_bo1
, const void *_bo2
)
1032 struct anv_bo
* const *bo1
= _bo1
;
1033 struct anv_bo
* const *bo2
= _bo2
;
1035 return (*bo1
)->gem_handle
- (*bo2
)->gem_handle
;
1039 anv_execbuf_add_bo(struct anv_execbuf
*exec
,
1041 struct anv_reloc_list
*relocs
,
1042 uint32_t extra_flags
,
1043 const VkAllocationCallbacks
*alloc
)
1045 struct drm_i915_gem_exec_object2
*obj
= NULL
;
1047 if (bo
->index
< exec
->bo_count
&& exec
->bos
[bo
->index
] == bo
)
1048 obj
= &exec
->objects
[bo
->index
];
1051 /* We've never seen this one before. Add it to the list and assign
1052 * an id that we can use later.
1054 if (exec
->bo_count
>= exec
->array_length
) {
1055 uint32_t new_len
= exec
->objects
? exec
->array_length
* 2 : 64;
1057 struct drm_i915_gem_exec_object2
*new_objects
=
1058 vk_alloc(alloc
, new_len
* sizeof(*new_objects
),
1059 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1060 if (new_objects
== NULL
)
1061 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1063 struct anv_bo
**new_bos
=
1064 vk_alloc(alloc
, new_len
* sizeof(*new_bos
),
1065 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1066 if (new_bos
== NULL
) {
1067 vk_free(alloc
, new_objects
);
1068 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1071 if (exec
->objects
) {
1072 memcpy(new_objects
, exec
->objects
,
1073 exec
->bo_count
* sizeof(*new_objects
));
1074 memcpy(new_bos
, exec
->bos
,
1075 exec
->bo_count
* sizeof(*new_bos
));
1078 vk_free(alloc
, exec
->objects
);
1079 vk_free(alloc
, exec
->bos
);
1081 exec
->objects
= new_objects
;
1082 exec
->bos
= new_bos
;
1083 exec
->array_length
= new_len
;
1086 assert(exec
->bo_count
< exec
->array_length
);
1088 bo
->index
= exec
->bo_count
++;
1089 obj
= &exec
->objects
[bo
->index
];
1090 exec
->bos
[bo
->index
] = bo
;
1092 obj
->handle
= bo
->gem_handle
;
1093 obj
->relocation_count
= 0;
1094 obj
->relocs_ptr
= 0;
1096 obj
->offset
= bo
->offset
;
1097 obj
->flags
= (bo
->flags
& ~ANV_BO_FLAG_MASK
) | extra_flags
;
1102 if (relocs
!= NULL
) {
1103 assert(obj
->relocation_count
== 0);
1105 if (relocs
->num_relocs
> 0) {
1106 /* This is the first time we've ever seen a list of relocations for
1107 * this BO. Go ahead and set the relocations and then walk the list
1108 * of relocations and add them all.
1110 exec
->has_relocs
= true;
1111 obj
->relocation_count
= relocs
->num_relocs
;
1112 obj
->relocs_ptr
= (uintptr_t) relocs
->relocs
;
1114 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1117 /* A quick sanity check on relocations */
1118 assert(relocs
->relocs
[i
].offset
< bo
->size
);
1119 result
= anv_execbuf_add_bo(exec
, relocs
->reloc_bos
[i
], NULL
,
1120 extra_flags
, alloc
);
1122 if (result
!= VK_SUCCESS
)
1127 if (relocs
->deps
&& relocs
->deps
->entries
> 0) {
1128 const uint32_t entries
= relocs
->deps
->entries
;
1129 struct anv_bo
**bos
=
1130 vk_alloc(alloc
, entries
* sizeof(*bos
),
1131 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1133 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1135 struct anv_bo
**bo
= bos
;
1136 set_foreach(relocs
->deps
, entry
) {
1137 *bo
++ = (void *)entry
->key
;
1140 qsort(bos
, entries
, sizeof(struct anv_bo
*), _compare_bo_handles
);
1142 VkResult result
= VK_SUCCESS
;
1143 for (bo
= bos
; bo
< bos
+ entries
; bo
++) {
1144 result
= anv_execbuf_add_bo(exec
, *bo
, NULL
, extra_flags
, alloc
);
1145 if (result
!= VK_SUCCESS
)
1149 vk_free(alloc
, bos
);
1151 if (result
!= VK_SUCCESS
)
1160 anv_execbuf_add_syncobj(struct anv_execbuf
*exec
,
1161 uint32_t handle
, uint32_t flags
,
1162 const VkAllocationCallbacks
*alloc
)
1166 if (exec
->fence_count
>= exec
->fence_array_length
) {
1167 uint32_t new_len
= MAX2(exec
->fence_array_length
* 2, 64);
1169 exec
->fences
= vk_realloc(alloc
, exec
->fences
,
1170 new_len
* sizeof(*exec
->fences
),
1171 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1172 if (exec
->fences
== NULL
)
1173 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1175 exec
->fence_array_length
= new_len
;
1178 exec
->fences
[exec
->fence_count
] = (struct drm_i915_gem_exec_fence
) {
1183 exec
->fence_count
++;
1189 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
1190 struct anv_reloc_list
*list
)
1192 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
1193 list
->relocs
[i
].target_handle
= list
->reloc_bos
[i
]->index
;
1197 adjust_relocations_from_state_pool(struct anv_state_pool
*pool
,
1198 struct anv_reloc_list
*relocs
,
1199 uint32_t last_pool_center_bo_offset
)
1201 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1202 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1204 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1205 /* All of the relocations from this block pool to other BO's should
1206 * have been emitted relative to the surface block pool center. We
1207 * need to add the center offset to make them relative to the
1208 * beginning of the actual GEM bo.
1210 relocs
->relocs
[i
].offset
+= delta
;
1215 adjust_relocations_to_state_pool(struct anv_state_pool
*pool
,
1216 struct anv_bo
*from_bo
,
1217 struct anv_reloc_list
*relocs
,
1218 uint32_t last_pool_center_bo_offset
)
1220 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1221 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1223 /* When we initially emit relocations into a block pool, we don't
1224 * actually know what the final center_bo_offset will be so we just emit
1225 * it as if center_bo_offset == 0. Now that we know what the center
1226 * offset is, we need to walk the list of relocations and adjust any
1227 * relocations that point to the pool bo with the correct offset.
1229 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1230 if (relocs
->reloc_bos
[i
] == &pool
->block_pool
.bo
) {
1231 /* Adjust the delta value in the relocation to correctly
1232 * correspond to the new delta. Initially, this value may have
1233 * been negative (if treated as unsigned), but we trust in
1234 * uint32_t roll-over to fix that for us at this point.
1236 relocs
->relocs
[i
].delta
+= delta
;
1238 /* Since the delta has changed, we need to update the actual
1239 * relocated value with the new presumed value. This function
1240 * should only be called on batch buffers, so we know it isn't in
1241 * use by the GPU at the moment.
1243 assert(relocs
->relocs
[i
].offset
< from_bo
->size
);
1244 write_reloc(pool
->block_pool
.device
,
1245 from_bo
->map
+ relocs
->relocs
[i
].offset
,
1246 relocs
->relocs
[i
].presumed_offset
+
1247 relocs
->relocs
[i
].delta
, false);
1253 anv_reloc_list_apply(struct anv_device
*device
,
1254 struct anv_reloc_list
*list
,
1256 bool always_relocate
)
1258 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
1259 struct anv_bo
*target_bo
= list
->reloc_bos
[i
];
1260 if (list
->relocs
[i
].presumed_offset
== target_bo
->offset
&&
1264 void *p
= bo
->map
+ list
->relocs
[i
].offset
;
1265 write_reloc(device
, p
, target_bo
->offset
+ list
->relocs
[i
].delta
, true);
1266 list
->relocs
[i
].presumed_offset
= target_bo
->offset
;
1271 * This function applies the relocation for a command buffer and writes the
1272 * actual addresses into the buffers as per what we were told by the kernel on
1273 * the previous execbuf2 call. This should be safe to do because, for each
1274 * relocated address, we have two cases:
1276 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1277 * not in use by the GPU so updating the address is 100% ok. It won't be
1278 * in-use by the GPU (from our context) again until the next execbuf2
1279 * happens. If the kernel decides to move it in the next execbuf2, it
1280 * will have to do the relocations itself, but that's ok because it should
1281 * have all of the information needed to do so.
1283 * 2) The target BO is active (as seen by the kernel). In this case, it
1284 * hasn't moved since the last execbuffer2 call because GTT shuffling
1285 * *only* happens when the BO is idle. (From our perspective, it only
1286 * happens inside the execbuffer2 ioctl, but the shuffling may be
1287 * triggered by another ioctl, with full-ppgtt this is limited to only
1288 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1289 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1290 * address and the relocated value we are writing into the BO will be the
1291 * same as the value that is already there.
1293 * There is also a possibility that the target BO is active but the exact
1294 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1295 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1296 * may be stale but it's still safe to write the relocation because that
1297 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1298 * won't be until the next execbuf2 call.
1300 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1301 * need to bother. We want to do this because the surface state buffer is
1302 * used by every command buffer so, if the kernel does the relocations, it
1303 * will always be busy and the kernel will always stall. This is also
1304 * probably the fastest mechanism for doing relocations since the kernel would
1305 * have to make a full copy of all the relocations lists.
1308 relocate_cmd_buffer(struct anv_cmd_buffer
*cmd_buffer
,
1309 struct anv_execbuf
*exec
)
1311 if (!exec
->has_relocs
)
1314 static int userspace_relocs
= -1;
1315 if (userspace_relocs
< 0)
1316 userspace_relocs
= env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1317 if (!userspace_relocs
)
1320 /* First, we have to check to see whether or not we can even do the
1321 * relocation. New buffers which have never been submitted to the kernel
1322 * don't have a valid offset so we need to let the kernel do relocations so
1323 * that we can get offsets for them. On future execbuf2 calls, those
1324 * buffers will have offsets and we will be able to skip relocating.
1325 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1327 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++) {
1328 if (exec
->bos
[i
]->offset
== (uint64_t)-1)
1332 /* Since surface states are shared between command buffers and we don't
1333 * know what order they will be submitted to the kernel, we don't know
1334 * what address is actually written in the surface state object at any
1335 * given time. The only option is to always relocate them.
1337 anv_reloc_list_apply(cmd_buffer
->device
, &cmd_buffer
->surface_relocs
,
1338 &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
1339 true /* always relocate surface states */);
1341 /* Since we own all of the batch buffers, we know what values are stored
1342 * in the relocated addresses and only have to update them if the offsets
1345 struct anv_batch_bo
**bbo
;
1346 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1347 anv_reloc_list_apply(cmd_buffer
->device
,
1348 &(*bbo
)->relocs
, &(*bbo
)->bo
, false);
1351 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++)
1352 exec
->objects
[i
].offset
= exec
->bos
[i
]->offset
;
1358 setup_execbuf_for_cmd_buffer(struct anv_execbuf
*execbuf
,
1359 struct anv_cmd_buffer
*cmd_buffer
)
1361 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1362 struct anv_state_pool
*ss_pool
=
1363 &cmd_buffer
->device
->surface_state_pool
;
1365 adjust_relocations_from_state_pool(ss_pool
, &cmd_buffer
->surface_relocs
,
1366 cmd_buffer
->last_ss_pool_center
);
1367 VkResult result
= anv_execbuf_add_bo(execbuf
, &ss_pool
->block_pool
.bo
,
1368 &cmd_buffer
->surface_relocs
, 0,
1369 &cmd_buffer
->device
->alloc
);
1370 if (result
!= VK_SUCCESS
)
1373 /* First, we walk over all of the bos we've seen and add them and their
1374 * relocations to the validate list.
1376 struct anv_batch_bo
**bbo
;
1377 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1378 adjust_relocations_to_state_pool(ss_pool
, &(*bbo
)->bo
, &(*bbo
)->relocs
,
1379 cmd_buffer
->last_ss_pool_center
);
1381 result
= anv_execbuf_add_bo(execbuf
, &(*bbo
)->bo
, &(*bbo
)->relocs
, 0,
1382 &cmd_buffer
->device
->alloc
);
1383 if (result
!= VK_SUCCESS
)
1387 /* Now that we've adjusted all of the surface state relocations, we need to
1388 * record the surface state pool center so future executions of the command
1389 * buffer can adjust correctly.
1391 cmd_buffer
->last_ss_pool_center
= ss_pool
->block_pool
.center_bo_offset
;
1393 struct anv_batch_bo
*first_batch_bo
=
1394 list_first_entry(&cmd_buffer
->batch_bos
, struct anv_batch_bo
, link
);
1396 /* The kernel requires that the last entry in the validation list be the
1397 * batch buffer to execute. We can simply swap the element
1398 * corresponding to the first batch_bo in the chain with the last
1399 * element in the list.
1401 if (first_batch_bo
->bo
.index
!= execbuf
->bo_count
- 1) {
1402 uint32_t idx
= first_batch_bo
->bo
.index
;
1403 uint32_t last_idx
= execbuf
->bo_count
- 1;
1405 struct drm_i915_gem_exec_object2 tmp_obj
= execbuf
->objects
[idx
];
1406 assert(execbuf
->bos
[idx
] == &first_batch_bo
->bo
);
1408 execbuf
->objects
[idx
] = execbuf
->objects
[last_idx
];
1409 execbuf
->bos
[idx
] = execbuf
->bos
[last_idx
];
1410 execbuf
->bos
[idx
]->index
= idx
;
1412 execbuf
->objects
[last_idx
] = tmp_obj
;
1413 execbuf
->bos
[last_idx
] = &first_batch_bo
->bo
;
1414 first_batch_bo
->bo
.index
= last_idx
;
1417 /* If we are pinning our BOs, we shouldn't have to relocate anything */
1418 if (cmd_buffer
->device
->instance
->physicalDevice
.use_softpin
)
1419 assert(!execbuf
->has_relocs
);
1421 /* Now we go through and fixup all of the relocation lists to point to
1422 * the correct indices in the object array. We have to do this after we
1423 * reorder the list above as some of the indices may have changed.
1425 if (execbuf
->has_relocs
) {
1426 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
)
1427 anv_cmd_buffer_process_relocs(cmd_buffer
, &(*bbo
)->relocs
);
1429 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
1432 if (!cmd_buffer
->device
->info
.has_llc
) {
1433 __builtin_ia32_mfence();
1434 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1435 for (uint32_t i
= 0; i
< (*bbo
)->length
; i
+= CACHELINE_SIZE
)
1436 __builtin_ia32_clflush((*bbo
)->bo
.map
+ i
);
1440 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1441 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1442 .buffer_count
= execbuf
->bo_count
,
1443 .batch_start_offset
= 0,
1444 .batch_len
= batch
->next
- batch
->start
,
1449 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1450 .rsvd1
= cmd_buffer
->device
->context_id
,
1454 if (relocate_cmd_buffer(cmd_buffer
, execbuf
)) {
1455 /* If we were able to successfully relocate everything, tell the kernel
1456 * that it can skip doing relocations. The requirement for using
1459 * 1) The addresses written in the objects must match the corresponding
1460 * reloc.presumed_offset which in turn must match the corresponding
1461 * execobject.offset.
1463 * 2) To avoid stalling, execobject.offset should match the current
1464 * address of that object within the active context.
1466 * In order to satisfy all of the invariants that make userspace
1467 * relocations to be safe (see relocate_cmd_buffer()), we need to
1468 * further ensure that the addresses we use match those used by the
1469 * kernel for the most recent execbuf2.
1471 * The kernel may still choose to do relocations anyway if something has
1472 * moved in the GTT. In this case, the relocation list still needs to be
1473 * valid. All relocations on the batch buffers are already valid and
1474 * kept up-to-date. For surface state relocations, by applying the
1475 * relocations in relocate_cmd_buffer, we ensured that the address in
1476 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1477 * safe for the kernel to relocate them as needed.
1479 execbuf
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
1481 /* In the case where we fall back to doing kernel relocations, we need
1482 * to ensure that the relocation list is valid. All relocations on the
1483 * batch buffers are already valid and kept up-to-date. Since surface
1484 * states are shared between command buffers and we don't know what
1485 * order they will be submitted to the kernel, we don't know what
1486 * address is actually written in the surface state object at any given
1487 * time. The only option is to set a bogus presumed offset and let the
1488 * kernel relocate them.
1490 for (size_t i
= 0; i
< cmd_buffer
->surface_relocs
.num_relocs
; i
++)
1491 cmd_buffer
->surface_relocs
.relocs
[i
].presumed_offset
= -1;
1498 setup_empty_execbuf(struct anv_execbuf
*execbuf
, struct anv_device
*device
)
1500 VkResult result
= anv_execbuf_add_bo(execbuf
, &device
->trivial_batch_bo
,
1501 NULL
, 0, &device
->alloc
);
1502 if (result
!= VK_SUCCESS
)
1505 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1506 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1507 .buffer_count
= execbuf
->bo_count
,
1508 .batch_start_offset
= 0,
1509 .batch_len
= 8, /* GEN7_MI_BATCH_BUFFER_END and NOOP */
1510 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1511 .rsvd1
= device
->context_id
,
1519 anv_cmd_buffer_execbuf(struct anv_device
*device
,
1520 struct anv_cmd_buffer
*cmd_buffer
,
1521 const VkSemaphore
*in_semaphores
,
1522 uint32_t num_in_semaphores
,
1523 const VkSemaphore
*out_semaphores
,
1524 uint32_t num_out_semaphores
,
1527 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1529 struct anv_execbuf execbuf
;
1530 anv_execbuf_init(&execbuf
);
1533 VkResult result
= VK_SUCCESS
;
1534 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1535 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1536 struct anv_semaphore_impl
*impl
=
1537 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1538 &semaphore
->temporary
: &semaphore
->permanent
;
1540 switch (impl
->type
) {
1541 case ANV_SEMAPHORE_TYPE_BO
:
1542 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1544 if (result
!= VK_SUCCESS
)
1548 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1549 if (in_fence
== -1) {
1550 in_fence
= impl
->fd
;
1552 int merge
= anv_gem_sync_file_merge(device
, in_fence
, impl
->fd
);
1554 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1564 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1565 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1566 I915_EXEC_FENCE_WAIT
,
1568 if (result
!= VK_SUCCESS
)
1577 bool need_out_fence
= false;
1578 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1579 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1581 /* Under most circumstances, out fences won't be temporary. However,
1582 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1584 * "If the import is temporary, the implementation must restore the
1585 * semaphore to its prior permanent state after submitting the next
1586 * semaphore wait operation."
1588 * The spec says nothing whatsoever about signal operations on
1589 * temporarily imported semaphores so it appears they are allowed.
1590 * There are also CTS tests that require this to work.
1592 struct anv_semaphore_impl
*impl
=
1593 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1594 &semaphore
->temporary
: &semaphore
->permanent
;
1596 switch (impl
->type
) {
1597 case ANV_SEMAPHORE_TYPE_BO
:
1598 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1599 EXEC_OBJECT_WRITE
, &device
->alloc
);
1600 if (result
!= VK_SUCCESS
)
1604 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1605 need_out_fence
= true;
1608 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1609 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1610 I915_EXEC_FENCE_SIGNAL
,
1612 if (result
!= VK_SUCCESS
)
1622 /* Under most circumstances, out fences won't be temporary. However,
1623 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1625 * "If the import is temporary, the implementation must restore the
1626 * semaphore to its prior permanent state after submitting the next
1627 * semaphore wait operation."
1629 * The spec says nothing whatsoever about signal operations on
1630 * temporarily imported semaphores so it appears they are allowed.
1631 * There are also CTS tests that require this to work.
1633 struct anv_fence_impl
*impl
=
1634 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1635 &fence
->temporary
: &fence
->permanent
;
1637 switch (impl
->type
) {
1638 case ANV_FENCE_TYPE_BO
:
1639 result
= anv_execbuf_add_bo(&execbuf
, &impl
->bo
.bo
, NULL
,
1640 EXEC_OBJECT_WRITE
, &device
->alloc
);
1641 if (result
!= VK_SUCCESS
)
1645 case ANV_FENCE_TYPE_SYNCOBJ
:
1646 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1647 I915_EXEC_FENCE_SIGNAL
,
1649 if (result
!= VK_SUCCESS
)
1654 unreachable("Invalid fence type");
1659 result
= setup_execbuf_for_cmd_buffer(&execbuf
, cmd_buffer
);
1661 result
= setup_empty_execbuf(&execbuf
, device
);
1663 if (result
!= VK_SUCCESS
)
1666 if (execbuf
.fence_count
> 0) {
1667 assert(device
->instance
->physicalDevice
.has_syncobj
);
1668 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_ARRAY
;
1669 execbuf
.execbuf
.num_cliprects
= execbuf
.fence_count
;
1670 execbuf
.execbuf
.cliprects_ptr
= (uintptr_t) execbuf
.fences
;
1673 if (in_fence
!= -1) {
1674 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_IN
;
1675 execbuf
.execbuf
.rsvd2
|= (uint32_t)in_fence
;
1679 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
1681 result
= anv_device_execbuf(device
, &execbuf
.execbuf
, execbuf
.bos
);
1683 /* Execbuf does not consume the in_fence. It's our job to close it. */
1687 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1688 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1689 /* From the Vulkan 1.0.53 spec:
1691 * "If the import is temporary, the implementation must restore the
1692 * semaphore to its prior permanent state after submitting the next
1693 * semaphore wait operation."
1695 * This has to happen after the execbuf in case we close any syncobjs in
1698 anv_semaphore_reset_temporary(device
, semaphore
);
1701 if (fence
&& fence
->permanent
.type
== ANV_FENCE_TYPE_BO
) {
1702 /* BO fences can't be shared, so they can't be temporary. */
1703 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
1705 /* Once the execbuf has returned, we need to set the fence state to
1706 * SUBMITTED. We can't do this before calling execbuf because
1707 * anv_GetFenceStatus does take the global device lock before checking
1710 * We set the fence state to SUBMITTED regardless of whether or not the
1711 * execbuf succeeds because we need to ensure that vkWaitForFences() and
1712 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
1713 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
1715 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SUBMITTED
;
1718 if (result
== VK_SUCCESS
&& need_out_fence
) {
1719 int out_fence
= execbuf
.execbuf
.rsvd2
>> 32;
1720 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1721 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1722 /* Out fences can't have temporary state because that would imply
1723 * that we imported a sync file and are trying to signal it.
1725 assert(semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
);
1726 struct anv_semaphore_impl
*impl
= &semaphore
->permanent
;
1728 if (impl
->type
== ANV_SEMAPHORE_TYPE_SYNC_FILE
) {
1729 assert(impl
->fd
== -1);
1730 impl
->fd
= dup(out_fence
);
1736 anv_execbuf_finish(&execbuf
, &device
->alloc
);